{"id":3748,"date":"2012-03-02T11:34:55","date_gmt":"2012-03-02T03:34:55","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3748"},"modified":"2012-03-27T16:44:00","modified_gmt":"2012-03-27T08:44:00","slug":"linux%e9%a1%b5%e6%a1%86%e5%88%86%e9%85%8d%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%9e%e7%8e%b0-%e5%bf%ab%e9%80%9f%e5%88%86%e9%85%8d%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3748","title":{"rendered":"Linux\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u5b9e\u73b0\uff083\uff09-\u5feb\u901f\u5206\u914d\u51fd\u6570"},"content":{"rendered":"<p>\u5173\u4e8e\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u524d\u6587\u4e2d\u5df2\u7ecf\u5bf9<a href=\"http:\/\/edsionte.com\/techblog\/archives\/3681\" target=\"_blank\">\u4e3b\u4f53<\/a>\u5b9e\u73b0\u51fd\u6570\u548c<a href=\"http:\/\/edsionte.com\/techblog\/archives\/3738\" target=\"_blank\">\u6162\u901f<\/a>\u5206\u914d\u51fd\u6570\u4f5c\u4e86\u7b80\u5355\u8bf4\u660e\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e3b\u8981\u7684\u529f\u80fd\u5373\u6839\u636e\u7cfb\u7edf\u5185\u5b58\u5b9e\u65f6\u7684\u4f7f\u7528\u60c5\u51b5\u53ca\u65f6\u8c03\u6574\u5206\u914d\u9650\u5236\uff0c\u5e76\u518d\u6b21\u8c03\u7528\u5feb\u901f\u5206\u914d\u51fd\u6570get_page_from_freelist()\u3002<\/p>\n<p>\u8be5\u51fd\u6570\u53ef\u4ee5\u770b\u4f5c\u662f\u4f19\u4f34\u7cfb\u7edf\u7684\u524d\u7f6e\u51fd\u6570\uff0c\u5b83\u901a\u8fc7\u4f20\u9012\u8fdb\u6765\u7684\u5206\u914d\u6807\u5fd7\u548c\u5206\u914d\u9636\uff0c\u5e76\u7ed3\u5408\u7cfb\u7edf\u5f53\u65f6\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u6765\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u8fdb\u884c\u5185\u5b58\u5206\u914d\u3002\u5982\u679c\u53ef\u4ee5\u5206\u914d\uff0c\u90a3\u4e48\u8fdb\u884c\u5b9e\u9645\u7684\u5185\u5b58\u5206\u914d\u5de5\u4f5c\uff0c\u65e2\u8c03\u7528\u90a3\u4e9b\u57fa\u4e8e\u4f19\u4f34\u7b97\u6cd5\u800c\u5b9e\u73b0\u7684\u5185\u5b58\u5206\u914d\u51fd\u6570\u3002<\/p>\n<h3>3.\u5feb\u901f\u5206\u914d\u51fd\u6570<\/h3>\n<pre class=\"brush:c\">static struct page *\r\nget_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,\r\n\t\tstruct zonelist *zonelist, int high_zoneidx, int alloc_flags,\r\n\t\tstruct zone *preferred_zone, int migratetype)\r\n{\r\n\tstruct zoneref *z;\r\n\tstruct page *page = NULL;\r\n\tint classzone_idx;\r\n\tstruct zone *zone;\r\n\tnodemask_t *allowednodes = NULL;\/* zonelist_cache approximation *\/\r\n\tint zlc_active = 0;\t\t\/* set if using zonelist_cache *\/\r\n\tint did_zlc_setup = 0;\t\t\/* just call zlc_setup() one time *\/\r\n\r\n\tclasszone_idx = zone_idx(preferred_zone);<\/pre>\n<p>\u73b0\u5728\u5f00\u59cb\u904d\u5386\u6307\u5b9azonelist\u4e0a\u7684zone\u3002\u5728\u901a\u8fc7for_each_zone_zonelist_nodemask()\u904d\u5386\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u8fc7\u6ee4\u6389\u90a3\u4e9b\u7d22\u5f15\u503c\u5927\u4e8ehigh_zoneidx\u7684zone\u3002\u6bd4\u5982\u5f53\u524dhigh_zoneidx\u6240\u6307\u5b9a\u7684zone\u4e3aZONE_NORMAL\uff0c\u90a3\u4e48\u5f53\u524d\u904d\u5386\u7684zone\u53ea\u80fd\u4e3aZONE_DMA\u6216ZONE_NORMAL\u7c7b\u578b\uff0c\u800c\u4e0d\u80fd\u662fZONE_HIGH\uff0c\u8fd9\u4e9b\u5173\u7cfb\u5224\u65ad\u7684\u4f9d\u636e\u5373\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u5bf9\u5e94\u7684\u7d22\u5f15\u503c\u3002\u56e0\u4e3a\u5185\u5b58\u7ba1\u7406\u533a\u7684\u7c7b\u578b\u662f\u901a\u8fc7\u679a\u4e3e\u7c7b\u578b\u6765\u63cf\u8ff0\u7684\uff0c\u56e0\u6b64\u7d22\u5f15\u503c\u4e5f\u5c31\u662f\u8fd9\u4e9b\u7c7b\u578b\u5bf9\u5e94\u7684\u679a\u4e3e\u503c\u3002<\/p>\n<pre class=\"brush:c\">zonelist_scan:\r\n\tfor_each_zone_zonelist_nodemask(zone, z, zonelist,\r\n\t\t\t\t\t\thigh_zoneidx, nodemask) {\r\n\t\tif (NUMA_BUILD &amp;&amp; zlc_active &amp;&amp;\r\n\t\t\t!zlc_zone_worth_trying(zonelist, z, allowednodes))\r\n\t\t\t\tcontinue;\r\n\t\tif ((alloc_flags &amp; ALLOC_CPUSET) &amp;&amp;\r\n\t\t\t!cpuset_zone_allowed_softwall(zone, gfp_mask))\r\n\t\t\t\tgoto try_next_zone;<\/pre>\n<p>\u5373\u4fbf\u5f53\u524dzone\u7684\u7d22\u5f15\u5c0f\u4e8ehigh_zoneidx\uff0c\u4e5f\u4e0d\u80fd\u6025\u4e8e\u5206\u914d\u5185\u5b58\uff0c\u8fd8\u8981\u68c0\u67e5\u8fd9\u4e2a\u5185\u5b58\u7ba1\u7406\u533a\u662f\u7a7a\u95f2\u7684\u9875\u6846\u662f\u5426\u5145\u8db3\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u8fc7\u4e0a\u8ff0\u4e24\u4e2aif\u8bed\u53e5\u6765\u5b8c\u6210\u3002\u63a5\u4e0b\u6765\uff0c\u5982\u679c\u5206\u914d\u6807\u5fd7\u4e2d\u8bbe\u7f6e\u4e86ALLOC_NO_WATERMARKS\uff0c\u5373\u8868\u660e\u6b64\u523b\u4e0d\u518d\u8003\u8651\u5206\u914d\u6c34\u4f4d\u7ebf\u3002\u5426\u5219\u5c31\u8981\u5206\u6790\u6b64\u523b\u5185\u5b58\u7684\u6c34\u4f4d\u7ebf\u3002<\/p>\n<p>\u901a\u8fc7\u5206\u914d\u6807\u5fd7\u4ece\u6c34\u4f4d\u7ebf\u6570\u7ec4\u4e2d\u83b7\u5f97\u5f53\u524d\u7684\u6c34\u4f4d\u7ebfmark\uff0c\u518d\u4f20\u5165zone_watermark_ok()\u5224\u65ad\u5728\u5f53\u524d\u7684\u6c34\u4f4d\u7ebf\u4e0b\u662f\u5426\u53ef\u4ee5\u5206\u914d\u5185\u5b58\u3002\u5982\u679c\u53ef\u4ee5\u5206\u914d\u5185\u5b58\u5219\u8df3\u5165try_this_zone\u3002<\/p>\n<pre class=\"brush:c\">\t\tBUILD_BUG_ON(ALLOC_NO_WATERMARKS &lt; NR_WMARK); \t\tif (!(alloc_flags &amp; ALLOC_NO_WATERMARKS)) { \t\t\tunsigned long mark; \t\t\tint ret; \t\t\tmark = zone-&gt;watermark[alloc_flags &amp; ALLOC_WMARK_MASK];\r\n\t\t\tif (zone_watermark_ok(zone, order, mark,\r\n\t\t\t\t    classzone_idx, alloc_flags))\r\n\t\t\t\tgoto try_this_zone;\r\n\r\n\t\t\tif (zone_reclaim_mode == 0)\r\n\t\t\t\tgoto this_zone_full;<\/pre>\n<p>\u5982\u679c\u8fd0\u884c\u5230\u6b64\u5904\uff0c\u8bf4\u660e\u6b64\u523b\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3\uff0c\u90a3\u4e48\u901a\u8fc7zone_reclaim()\u8fdb\u884c\u5185\u5b58\u56de\u6536\uff0c\u56de\u6536\u7684\u60c5\u51b5\u901a\u8fc7ret\u6765\u63cf\u8ff0\u3002\u5982\u679c\u7ed3\u679c\u662fZONE_RECLAIM_NOSCAN\uff0c\u8bf4\u660e\u5e76\u6ca1\u6709\u8fdb\u884c\u56de\u6536\uff0c\u90a3\u4e48\u76f4\u63a5\u5c1d\u8bd5\u4e0b\u4e00\u4e2azone\uff1b\u5982\u679c\u7ed3\u679c\u662fZONE_RECLAIM_FULL\uff0c\u8bf4\u660e\u867d\u7136\u8fdb\u884c\u4e86\u56de\u6536\u4f46\u662f\u5e76\u6ca1\u6709\u56de\u6536\u5230\uff1b\u9ed8\u8ba4\u7684\u60c5\u51b5\u5219\u662f\u6ca1\u6709\u56de\u6536\u5230\u8db3\u591f\u591a\u7684\u5185\u5b58\u3002\u540e\u4e24\u79cd\u60c5\u51b5\u5747\u8df3\u5165this_zone_full\u5904\u3002<\/p>\n<pre class=\"brush:c\">\t\t\tret = zone_reclaim(zone, gfp_mask, order);\r\n\t\t\tswitch (ret) {\r\n\t\t\tcase ZONE_RECLAIM_NOSCAN:\r\n\t\t\t\t\/* did not scan *\/\r\n\t\t\t\tgoto try_next_zone;\r\n\t\t\tcase ZONE_RECLAIM_FULL:\r\n\t\t\t\t\/* scanned but unreclaimable *\/\r\n\t\t\t\tgoto this_zone_full;\r\n\t\t\tdefault:\r\n\t\t\t\t\/* did we reclaim enough *\/\r\n\t\t\t\tif (!zone_watermark_ok(zone, order, mark,\r\n\t\t\t\t\t\tclasszone_idx, alloc_flags))\r\n\t\t\t\t\tgoto this_zone_full;\r\n\t\t\t}\r\n\t\t}<\/pre>\n<p>\u5982\u679c\u8df3\u5230\u6b64\u6807\u53f7\u5904\u8bf4\u660e\u53ef\u4ee5\u5728\u5f53\u524dzone\u4e0a\u5206\u914d\u5185\u5b58\uff0c\u968f\u5373\u8c03\u7528buffered_rmqueue()\u8fdb\u5165\u4f19\u4f34\u7b97\u6cd5\u3002<\/p>\n<pre class=\"brush:c\">try_this_zone:\r\n\t\tpage = buffered_rmqueue(preferred_zone, zone, order,\r\n\t\t\t\t\t\tgfp_mask, migratetype);\r\n\t\tif (page)\r\n\t\t\tbreak;<\/pre>\n<p>\u8df3\u5230\u6b64\u5904\u8bf4\u660e\u5f53\u524dzone\u7684\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3\uff0c\u90a3\u4e48\u6807\u8bb0\u5b83\u3002\u8fd9\u6837\u4e0b\u6b21\u5206\u914d\u65f6\u76f4\u63a5\u5c06\u5176\u5ffd\u7565\u3002<\/p>\n<pre class=\"brush:c\">this_zone_full:\r\n\t\tif (NUMA_BUILD)\r\n\t\t        zlc_mark_zone_full(zonelist, z);<\/pre>\n<p>\u6b64\u5904\u8bf4\u660e\u5f53\u524dzone\u4e0a\u7684\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3\uff0c\u5219\u9700\u8981\u5728\u5176\u4ed6zone\u4e0a\u5c1d\u8bd5\u5206\u914d\u3002<\/p>\n<pre class=\"brush:c\">try_next_zone:\r\n\t\tif (NUMA_BUILD &amp;&amp; !did_zlc_setup &amp;&amp; nr_online_nodes &gt; 1) {\r\n\t\t\tallowednodes = zlc_setup(zonelist, alloc_flags);\r\n\t\t\tzlc_active = 1;\r\n\t\t\tdid_zlc_setup = 1;\r\n\t\t}\r\n\t}<\/pre>\n<p>\u6b64\u65f6\uff0c\u904d\u5386zone\u7684\u5faa\u73af\u7ed3\u675f\u3002\u5982\u679c\u7b2c\u4e00\u6b21\u5faa\u73af\u7ed3\u675f\u540epage\u4ecd\u672a\u7a7a\uff0c\u5219\u8fdb\u884c\u7b2c\u4e8c\u6b21\u5206\u914d\uff0c\u5373\u8df3\u5165zonelist_scan\u91cd\u65b0\u904d\u5386\u3002\u5f53\u7b2c\u4e8c\u6b21\u5206\u914d\u7ed3\u675f\u540e\u4e0d\u7ba1\u7ed3\u679c\u5982\u4f55\u5747\u8fd4\u56de\u3002\u5faa\u73af\u7684\u6b21\u6570\u7531alc_active\u63a7\u5236\u3002<\/p>\n<pre class=\"brush:c\">\tif (unlikely(NUMA_BUILD &amp;&amp; page == NULL &amp;&amp; zlc_active)) {\r\n\t\t\/* Disable zlc cache for second zonelist scan *\/\r\n\t\tzlc_active = 0;\r\n\t\tgoto zonelist_scan;\r\n\t}\r\n\treturn page;\r\n}<\/pre>\n<p>\u81f3\u6b64\uff0c\u5feb\u901f\u5206\u914d\u51fd\u6570\u5206\u6790\u5b8c\u6bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u524d\u6587\u4e2d\u5df2\u7ecf\u5bf9\u4e3b\u4f53\u5b9e\u73b0\u51fd\u6570\u548c\u6162\u901f\u5206\u914d\u51fd\u6570\u4f5c\u4e86\u7b80\u5355\u8bf4\u660e\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e3b\u8981\u7684\u529f\u80fd\u5373\u6839\u636e\u7cfb\u7edf\u5185\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[407,313,302],"class_list":["post-3748","post","type-post","status-publish","format-standard","hentry","category-266","tag-407","tag-313","tag-302"],"views":9007,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3748"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3748\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3748"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}