{"id":395,"date":"2014-03-10T21:08:00","date_gmt":"2014-03-10T13:08:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=395"},"modified":"2020-06-06T21:09:03","modified_gmt":"2020-06-06T13:09:03","slug":"linux-%e4%b8%8b%e7%9b%91%e6%8e%a7%e7%a8%8b%e5%ba%8f%e5%8d%87%e7%ba%a7%e7%89%88","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=395","title":{"rendered":"Linux \u4e0b\u76d1\u63a7\u7a0b\u5e8f\u5347\u7ea7\u7248"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h>\n#include&lt;unistd.h>\n\nclass VMSTAT\n{\npublic:\n\tdouble stat&#91;20];\n\t\/*\n\t *       Proc\n\t *\n\t * 0:r: The number of processes waiting for run time.\n\t * 1:b: The number of processes in uninterruptible sleep.\n\t *\n\t *       Memory\n\t *\n\t * 2:swpd: the amount of virtual memory used.\n\t * 3:free: the amount of idle memory.\n\t * 4:buff: the amount of memory used as buffers.\n\t * 5:cache: the amount of memory used as cache.\n\t *\n\t *       Swap\n\t *\n\t * 6:si: Amount of memory swapped in from disk (\/s).\n\t * 7:Amount of memory swapped to disk (\/s).\n\t *\n\t *       IO\n\t *\n\t * 8:bi: Blocks received from a block device (blocks\/s).\n\t * 9:bo: Blocks sent to a block device (blocks\/s).\n\t *\n\t *       System\n\t *\n\t * 10:in: The number of interrupts per second, including the clock.\n\t * 11:cs: The number of context switches per second.\n\t *\n\t *       CPU\n\t *\n\t * 12:us: Time spent running non-kernel code. (user time, including nice time)\n\t * 13:sy: Time spent running kernel code. (system time)\n\t * 14:id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.\n\t * 15:wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.\n\t * 16:st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.\n\t *\n\t *       Add\n\t *\n\t * 17:Total Memory\n\t * 18:Used Memory\n\t * 19:CPU Usage\n\t *\/\n\n\tvoid update()\n\t{\n\n\t\t\/\/init array\n\t\tFILE *file = popen(\"vmstat\", \"r\");\n\t\tchar buf&#91;300];\n\t\tfgets(buf, 300, file);\n\t\tfgets(buf, 300, file);\n\t\tfgets(buf, 300, file);\n\n\t\tint index = 0;\n\t\tfor (int i = 0; i &lt; 300; i++)\n\t\t{\n\t\t\twhile (buf&#91;i] &lt; '0' || buf&#91;i] > '9')\n\t\t\t\ti++;\n\t\t\tint tmp = 0;\n\t\t\twhile (buf&#91;i] >= '0' &amp;&amp; buf&#91;i] &lt;= '9')\n\t\t\t{\n\t\t\t\ttmp = tmp * 10 + (buf&#91;i] - '0');\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tstat&#91;index++] = tmp * 1.0;\n\t\t\tif (index == 17)\n\t\t\t\tbreak;\n\t\t}\n\t\tpclose(file);\n\n\t\t\/\/total memory\n\t\tfile = popen(\"cat \/proc\/meminfo\", \"r\");\n\t\tdouble total = 0;\n\t\tfgets(buf, 300, file);\n\t\tchar str&#91;7];\n\t\tfor (int i = 17; i &lt; 24; i++)\n\t\t\tstr&#91;i - 17] = buf&#91;i];\n\n\t\tfor (int i = 0; i &lt; 7; i++)\n\t\t{\n\t\t\ttotal = total * 10 + (str&#91;i] == ' ' ? 0 : (str&#91;i] - '0'));\n\t\t}\n\n\t\t\/\/calculate all\n\t\tstat&#91;17] = total;\n\t\tstat&#91;18] = total - stat&#91;3];\n\t\tstat&#91;19] = 100 - stat&#91;14];\n\t\tpclose(file);\n\n\t}\n\n};\n\nint main()\n{\n\n\tVMSTAT s;\n\twhile (1)\n\t{\n\t\ts.update();\n\t\tprintf(\"Total: %d MB  Aviliable: %d MB  Used: %d MB  CPU: %d %\\n\",\n\t\t\t\tint(s.stat&#91;17] \/ 1000), int(s.stat&#91;3] \/ 1000),\n\t\t\t\tint(s.stat&#91;18] \/ 1000), int(s.stat&#91;19]));\n\t\tsleep(1);\n\t}\n\n\treturn 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-06-03-play-ground"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=395"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}