{"id":3819,"date":"2012-04-05T11:55:25","date_gmt":"2012-04-05T03:55:25","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3819"},"modified":"2012-04-30T22:33:01","modified_gmt":"2012-04-30T14:33:01","slug":"%e8%b0%83%e5%ba%a6%e5%99%a8%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3819","title":{"rendered":"\u57fa\u4e8eCFS\u7b97\u6cd5\u7684schedule()\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<p>\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u7b97\u6cd5\u5728\u4e0d\u65ad\u53d8\u5316\uff0c2.4\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u5668\u662f\u5728\u6240\u6709\u7684\u8fdb\u7a0b\u4e2d\u9009\u62e9\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u8fdb\u7a0b\uff0c2.6\u5185\u6838\u524d\u671f\u7684\u8c03\u5ea6\u5668\u662f\u57fa\u4e8eO(1)\u7b97\u6cd5\u7684\uff0c\u800c2.6.23\u7248\u672c\u4e4b\u540e\u7684\u5185\u6838\u91c7\u7528CFS\u8c03\u5ea6\u7b97\u6cd5\uff0c\u5e76\u540c\u65f6\u5bf9\u8c03\u5ea6\u5668\u8fdb\u884c\u4e86\u6bd4\u8f83\u5927\u7684\u6539\u5584\u3002\u5185\u6838\u4e3b\u8981\u662f\u5f15\u5165\u4e86\u8c03\u5ea6\u5668\u7c7b\u6765\u589e\u52a0\u8c03\u5ea6\u5668\u7684\u53ef\u6269\u5c55\u6027\u3002\u8c03\u5ea6\u5668\u7c7b\u5c06\u5404\u79cd\u8c03\u5ea6\u7b56\u7565\u6a21\u5757\u5316\uff0c\u5c01\u88c5\u4e86\u5bf9\u4e0d\u540c\u8c03\u5ea6\u7b56\u7565\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>\u5185\u6838\u4e2d\u5bf9\u8fdb\u7a0b\u8c03\u5ea6\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u5176\u4e00\u4e3a\u5468\u671f\u6027\u8c03\u5ea6\u5668\uff08generic scheduler\uff09\uff0c\u5b83\u5bf9\u8fdb\u884c\u8fdb\u884c\u5468\u671f\u6027\u7684\u8c03\u5ea6\uff0c\u4ee5\u56fa\u5b9a\u7684\u9891\u7387\u8fd0\u884c\uff1b\u5176\u4e8c\u4e3a\u4e3b\u8c03\u5ea6\u5668\uff08main scheduler\uff09\uff0c\u5982\u679c\u8fdb\u7a0b\u8981\u8fdb\u884c\u7761\u7720\u6216\u56e0\u4e3a\u5176\u4ed6\u539f\u56e0\u4e3b\u52a8\u653e\u5f03CPU\uff0c\u90a3\u4e48\u5c31\u76f4\u63a5\u8c03\u7528\u4e3b\u8c03\u5ea6\u5668\u3002<\/p>\n<p>\u5185\u6838\u7684\u4e3b\u8c03\u5ea6\u5668\u662f\u901a\u8fc7schedule()\u5b9e\u73b0\u7684\uff0c\u8be5\u51fd\u6570\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u6311\u9009\u4e0b\u4e00\u4e2a\u5e94\u8be5\u88ab\u8c03\u5ea6\u7684\u8fdb\u7a0bnext\u3002<br \/>\n\u8be5\u51fd\u6570\u9996\u5148\u7981\u6b62\u5185\u6838\u62a2\u5360\uff0c\u5e76\u4e14\u4f9d\u6b21\u83b7\u53d6\u5f53\u524dCPU\u7f16\u53f7cpu\u3001\u5f53\u524dCPU\u5bf9\u5e94\u7684\u8fd0\u884c\u961f\u5217rq\u3001\u5f53\u524d\u8fdb\u7a0b\u7684\u5207\u6362\u6b21\u6570switch_count\u4ee5\u53ca\u5f53\u524d\u8fdb\u7a0b\u7684\u63cf\u8ff0\u7b26prev\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nasmlinkage void __sched schedule(void)\r\n{\r\n\tstruct task_struct *prev, *next;\r\n\tunsigned long *switch_count;\r\n\tstruct rq *rq;\r\n\tint cpu;\r\n\r\nneed_resched:\r\n\tpreempt_disable();\r\n\tcpu = smp_processor_id();\r\n\trq = cpu_rq(cpu);\r\n\trcu_sched_qs(cpu);\r\n\tprev = rq-&gt;curr;\r\n\tswitch_count = &amp;prev-&gt;nivcsw;\r\n\r\n\trelease_kernel_lock(prev);\r\nneed_resched_nonpreemptible:\r\n\r\n\tschedule_debug(prev);\r\n\r\n\tif (sched_feat(HRTICK))\r\n\t\thrtick_clear(rq);<\/pre>\n<p>\u63a5\u4e0b\u6765\u901a\u8fc7update_rq_clock()\u66f4\u65b0\u5c31\u7eea\u961f\u5217\u4e0a\u7684\u65f6\u949f\uff0c\u63a5\u7740\u901a\u8fc7clear_tsk_need_resched()\u6e05\u9664\u5f53\u524d\u8fdb\u7a0bprev\u7684\u91cd\u65b0\u8c03\u5ea6\u6807\u5fd7TIF_NEED_RESCHED\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\traw_spin_lock_irq(&amp;rq-&gt;lock);\r\n\tupdate_rq_clock(rq);\r\n\tclear_tsk_need_resched(prev);<\/pre>\n<p>\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u662f\u53ef\u4e2d\u65ad\u7761\u7720\u72b6\u6001\uff08\u53ef\u8fd0\u6027\u72b6\u6001TASK_RUNNING\u5b8f\u7684\u503c\u4e3a0\uff09\uff0c\u4f46\u5b83\u5374\u6536\u5230\u4e86\u67d0\u4e2a\u5524\u9192\u5b83\u7684\u4fe1\u53f7\uff0c\u90a3\u4e48\u5f53\u524d\u8fdb\u7a0b\u7684\u6807\u5fd7\u88ab\u66f4\u65b0\u4e3aTASK_RUNNING,\u7b49\u5f85\u518d\u6b21\u88ab\u8c03\u5ea6\u3002\u5426\u5219\uff0c\u901a\u8fc7deactivate_task()\u5c06\u5f53\u524d\u8fdb\u7a0bprev\u4ece\u5c31\u7eea\u961f\u5217\u4e2d\u5220\u9664\u3002<\/p>\n<p>\u8fd9\u91cc\u7684deactivate_task()\u6839\u636e\u8c03\u5ea6\u7c7b\u7684\u4e0d\u540c\u5b9e\u73b0\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u8fd9\u4e9b\u5dee\u5f02\u5bf9\u4e3b\u8c03\u5ea6\u5668\u662f\u900f\u660e\u7684\uff0c\u56e0\u4e3a\u8c03\u5ea6\u5668\u7c7b\u5728\u5404\u79cd\u8c03\u5ea6\u5b9e\u4f8b\u548c\u8c03\u5ea6\u5668\u4e4b\u95f4\u8d77\u5230\u4e86\u8fde\u63a5\u4f5c\u7528\u3002\u8be5\u51fd\u6570\u7684\u6838\u5fc3\u8bed\u53e5\u5373\u4e3a\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">p-&gt;sched_class-&gt;dequeue_task(rq, p, sleep);<\/pre>\n<p>sched_class\u662f\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u63cf\u8ff0\u5f53\u524d\u8fdb\u7a0b\u6240\u5c5e\u8c03\u5ea6\u7c7b\u7684\u5b57\u6bb5\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5b57\u6bb5\u56de\u8c03\u94a9\u5b50\u51fd\u6570dequeue_task()\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tif (prev-&gt;state &amp;&amp; !(preempt_count() &amp; PREEMPT_ACTIVE)) {\r\n\t\tif (unlikely(signal_pending_state(prev-&gt;state, prev)))\r\n\t\t\tprev-&gt;state = TASK_RUNNING;\r\n\t\telse\r\n\t\t\tdeactivate_task(rq, prev, 1);\r\n\t\tswitch_count = &amp;prev-&gt;nvcsw;\r\n\t}\r\n\r\n\tpre_schedule(rq, prev);\r\n\r\n\tif (unlikely(!rq-&gt;nr_running))\r\n\t\tidle_balance(cpu, rq);\r\n<\/pre>\n<p>\u901a\u8fc7put_prev_task()\u5c06prev\u8fdb\u7a0b\u91cd\u65b0\u63d2\u5165\u5230\u5c31\u7eea\u961f\u5217\u5408\u9002\u7684\u4f4d\u7f6e\u4e2d\u3002\u518d\u901a\u8fc7pick_next_task()\u5728\u5f53\u524d\u7684\u5c31\u7eea\u961f\u5217\u4e2d\u6311\u9009\u4e0b\u4e00\u4e2a\u5e94\u8be5\u88ab\u6267\u884c\u7684\u8fdb\u7a0bnext\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u5c5e\u4e8e\u8c03\u5ea6\u5668\u7c7b\u4e2d\u7684\u94a9\u5b50\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u5177\u4f53\u5b9e\u73b0\u6839\u636e\u8c03\u5ea6\u5b9e\u4f8b\u7684\u4e0d\u540c\u800c\u4e0d\u540c\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tput_prev_task(rq, prev);\r\n\tnext = pick_next_task(rq);<\/pre>\n<p>\u6709\u65f6\u5019\uff0c\u8c03\u5ea6\u5668\u6240\u9009\u7684\u4e0b\u4e00\u4e2a\u88ab\u6267\u884c\u7684\u8fdb\u7a0b\u6070\u597d\u5c31\u662f\u5f53\u524d\u8fdb\u7a0b\uff0c\u90a3\u4e48\u8c03\u5ea6\u5668\u5c31\u4e0d\u5fc5\u8017\u8d39\u7cbe\u529b\u53bb\u6267\u884c\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u4f46\u8fd9\u79cd\u60c5\u51b5\u4e0d\u662f\u7ecf\u5e38\u53d1\u751f\u7684\u3002\u5982\u679cprev\u548cnext\u4e0d\u662f\u540c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5148\u901a\u8fc7sched_info_switch()\u66f4\u65b0\u4e24\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u76f8\u5173\u5b57\u6bb5\uff0c\u5e76\u4e14\u66f4\u65b0\u53ef\u8fd0\u884c\u961f\u5217\u7684\u76f8\u5173\u5b57\u6bb5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u8c03\u7528context_switch()\u8fdb\u884cprev\u548cnext\u4e24\u4e2a\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u8be5\u51fd\u6570\u7531\u4e00\u6bb5\u6c47\u7f16\u4ee3\u7801\u7ec4\u6210\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tif (likely(prev != next)) {\r\n\t\tsched_info_switch(prev, next);\r\n\t\tperf_event_task_sched_out(prev, next);\r\n\r\n\t\trq-&gt;nr_switches++;\r\n\t\trq-&gt;curr = next;\r\n\t\t++*switch_count;\r\n\r\n\t\tcontext_switch(rq, prev, next); \/* unlocks the rq *\/\r\n\t\t\/*\r\n\t\t * the context switch might have flipped the stack from under\r\n\t\t * us, hence refresh the local variables.\r\n\t\t *\/\r\n\t\tcpu = smp_processor_id();\r\n\t\trq = cpu_rq(cpu);\r\n\t} else\r\n\t\traw_spin_unlock_irq(&amp;rq-&gt;lock);<\/pre>\n<p>\u5207\u6362\u5b8c\u6bd5\u540e\uff0c\u5f53\u524d\u7684\u8fdb\u7a0b\u5c31\u662f\u65b0\u9009\u62e9\u7684\u8fdb\u7a0b\uff0c\u5b83\u4f1a\u5f00\u59cb\u6267\u884c\u3002\u800c\u88ab\u5207\u6362\u51fa\u53bb\u7684\u8fdb\u7a0b\u91cd\u65b0\u8fd0\u884c\u65f6\u4f1a\u4ece\u5207\u6362\u51fd\u6570\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tpost_schedule(rq);\r\n\r\n\tif (unlikely(reacquire_kernel_lock(current) &lt; 0)) { \t\tprev = rq-&gt;curr;\r\n\t\tswitch_count = &amp;prev-&gt;nivcsw;\r\n\t\tgoto need_resched_nonpreemptible;\r\n\t}\r\n\r\n\tpreempt_enable_no_resched();\r\n\tif (need_resched())\r\n\t\tgoto need_resched;\r\n}<\/pre>\n<p>\u6839\u636e\u4e0a\u8ff0\u5bf9\u4e3b\u8c03\u5ea6\u5668\u51fd\u6570\u6e90\u7801\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4e3b\u8c03\u5ea6\u5668\u7684\u4e3b\u8981\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n<p>1.\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u63cf\u8ff0\u7b26\u4ee5\u53ca\u672c\u5730CPU\u7684\u8fd0\u884c\u961f\u5217<\/p>\n<p>2.\u5c06\u5f53\u524d\u8fdb\u7a0bprev\u653e\u5165\u53ef\u8fd0\u884c\u961f\u5217\u4e2d\uff0c\u7b49\u5f85\u4e0b\u4e00\u6b21\u88ab\u91cd\u65b0\u8c03\u5ea6<\/p>\n<p>3.\u5728\u5f53\u524d\u7684\u53ef\u8fd0\u884c\u961f\u5217\u4e2d\u9009\u53d6\u4e0b\u4e00\u4e2a\u88ab\u8c03\u5ea6\u7684\u65b0\u8fdb\u7a0bnext<\/p>\n<p>4.\u4ece\u5f53\u524d\u8fdb\u7a0b\u5207\u6362\u5230\u65b0\u8fdb\u7a0b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u7b97\u6cd5\u5728\u4e0d\u65ad\u53d8\u5316\uff0c2.4\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u5668\u662f\u5728\u6240\u6709\u7684\u8fdb\u7a0b\u4e2d\u9009\u62e9\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u8fdb\u7a0b\uff0c2.6\u5185\u6838\u524d\u671f\u7684\u8c03\u5ea6\u5668\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[327,328,326,212],"class_list":["post-3819","post","type-post","status-publish","format-standard","hentry","category-linux","tag-cfs","tag-328","tag-326","tag-212"],"views":9044,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3819","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=3819"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3819\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3819"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}