{"id":2870,"date":"2011-04-08T17:13:09","date_gmt":"2011-04-08T09:13:09","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2870"},"modified":"2011-04-16T18:17:41","modified_gmt":"2011-04-16T10:17:41","slug":"linux2-6%e8%bf%9b%e7%a8%8b%e8%b0%83%e5%ba%a6%e5%88%86%e6%9e%903-%e4%b8%8e%e8%b0%83%e5%ba%a6%e6%9c%89%e5%85%b3%e7%9a%84%e5%87%bd%e6%95%b0%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2870","title":{"rendered":"Linux2.6\u8fdb\u7a0b\u8c03\u5ea6\u5206\u6790(3)-\u4e0e\u8c03\u5ea6\u6709\u5173\u7684\u51fd\u6570\u5206\u6790"},"content":{"rendered":"<p>\u524d\u9762\u4e24\u7bc7\u6587\u7ae0\u4ece\u539f\u7406\u89d2\u5ea6\u5206\u6790\u4e86\u8fdb\u7a0b\u7684\u8c03\u5ea6\uff0c\u672c\u6587\u5c06\u4ece\u5177\u4f53\u7684\u6e90\u7801\u51fa\u53d1\uff0c\u5206\u6790\u4e0e\u8fdb\u7a0b\u8fdb\u7a0b\u8c03\u5ea6\u5bc6\u5207\u76f8\u5173\u7684\u51e0\u4e2a\u51fd\u6570\u3002<\/p>\n<h3>1.\u65f6\u95f4\u7247\u7684\u5206\u914d\uff1atask_timeslice()<\/h3>\n<p>\u6b63\u5982\u6211\u4eec\u6240\u77e5\u7684\u90a3\u6837\uff0c\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u4e0e\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7\u6709\u76f4\u63a5\u7684\u5173\u7cfb\u3002\u4ece\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u6839\u636e\u8fdb\u7a0b\u9759\u6001\u4f18\u5148\u7ea7static_prio\u4e0eNICE_TO_PRIO(0)\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u8fdb\u7a0b\u65f6\u95f4\u7247\u7684\u5206\u914d\u53ef\u4ee5\u5206\u4e3a\u4e24\u6761\u8def\u7ebf\u3002\u4ee5\u4e0b\u4ee3\u7801\u5982\u65e0\u7279\u522b\u8bf4\u660e\u5747\u4f4d\u4e8elinux\/kernel\/sched.c\u4e0b\u3002<\/p>\n<pre class=\"brush:c\">static unsigned int task_timeslice(task_t *p)\r\n{\r\n\tif (p-&gt;static_prio &lt; NICE_TO_PRIO(0)) \t\treturn SCALE_PRIO(DEF_TIMESLICE*4, p-&gt;static_prio);\r\n\telse\r\n\t\treturn SCALE_PRIO(DEF_TIMESLICE, p-&gt;static_prio);\r\n}<\/pre>\n<p>NICE_TO_PRIO\u4ee5\u53caPRIO_TO_NICE\u5b8f\u7684\u4f5c\u7528\u5c06\u8fdb\u884cnice\u503c\u548c\u8fdb\u7a0b\u9759\u6001\u4f18\u5148\u7ea7\u4e4b\u95f4\u7684\u8f6c\u6362\u3002nice\u4e5f\u7528\u6765\u8868\u793a\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4e0e\u9759\u6001\u4f18\u5148\u7ea7\u7684\u5927\u5c0f\u8303\u56f4\u4e0d\u540c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06nice\u770b\u4f5c\u662fstatic_prio\u7684\u7f29\u5f71\u3002<\/p>\n<pre class=\"brush:c\">#define MAX_USER_RT_PRIO        100\r\n#define MAX_RT_PRIO             MAX_USER_RT_PRIO\r\n#define NICE_TO_PRIO(nice)\t(MAX_RT_PRIO + (nice) + 20)\r\n#define PRIO_TO_NICE(prio)\t((prio) - MAX_RT_PRIO - 20)<\/pre>\n<p>\u76ee\u524d\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u666e\u901a\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7\u5927\u5c0f\u8303\u56f4\u662f100\u5230139\uff0c\u56e0\u6b64\u4ece\u4e0a\u9762\u7684\u4e00\u4e9b\u5217\u5b8f\u53ef\u4ee5\u5f97\u77e5\uff0cnice\u7684\u53d6\u503c\u8303\u56f4\u4e3a-20\u523019\u3002<\/p>\n<p>\u56e0\u6b64\uff0cNICE_TO_PRIO(0)\u53d6\u503c\u4e3a120\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fdb\u7a0b\u65f6\u95f4\u7247\u5206\u914d\u7684\u4e24\u6761\u8def\u7ebf\u662f\u6839\u636e\u9759\u6001\u4f18\u5148\u7ea7120\u8fdb\u884c\u5212\u5206\u7684\u3002\u4eceSCALE_PRIO\u5b8f\u7684\u5b9a\u4e49\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u8be5\u5b8f\u7684\u4f5c\u7528\u662f\u53d6\u4e24\u4e2a\u6570\u503c\uff08\u5177\u4f53\u5e94\u8be5\u662f\u65f6\u95f4\u7247\uff09\u7684\u6700\u5927\u8005\u3002<\/p>\n<pre class=\"brush:c\">#define MAX_PRIO                (MAX_RT_PRIO + 40)\r\n#define USER_PRIO(p)\t\t((p)-MAX_RT_PRIO)\r\n#define MAX_USER_PRIO\t\t(USER_PRIO(MAX_PRIO))\r\n#define DEF_TIMESLICE\t\t(100 * HZ \/ 1000)\r\n#define MIN_TIMESLICE           max(5 * HZ \/ 1000, 1)\r\n# define HZ             1000  \/\/\u4f4d\u4e8elinux\/include\/asm-i386\/param.h\r\n#define SCALE_PRIO(x, prio) \\\r\nmax(x * (MAX_PRIO - prio) \/ (MAX_USER_PRIO\/2), MIN_TIMESLICE)<\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u5b8f\u5b9a\u4e49\u53ef\u77e5\uff0c(MAX_USER_PRIO\/2)\u4e3a20\u3002\u5f53\u8fdb\u7a0b\u9759\u6001\u4f18\u5148\u7ea7\u5c0f\u4e8e120\u65f6\uff0cx\u7684\u503c\u4e3aDEF_TIMESLICE*4\uff0c\u5177\u4f53\u5373\u4e3a400ms\uff1b\u5426\u5219x\u4e3a100ms\u3002\u56e0\u6b64\u5bf9\u4e8eSCALE_PRIO\u5b8f\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u516c\u5f0f\u6765\u8868\u8fbe\uff1a<\/p>\n<p><strong>\u9759\u6001\u4f18\u5148\u7ea7&lt;120\uff0c\u57fa\u672c\u65f6\u95f4\u7247=max((140-\u9759\u6001\u4f18\u5148\u7ea7)*20, MIN_TIMESLICE) <\/strong><\/p>\n<p><strong>\u9759\u6001\u4f18\u5148\u7ea7&gt;=120\uff0c\u57fa\u672c\u65f6\u95f4\u7247=max((140-\u9759\u6001\u4f18\u5148\u7ea7)*5, MIN_TIMESLICE)<\/strong><\/p>\n<p>\u5176\u4e2dMIN_TIMESLICE\u4e3a\u7cfb\u7edf\u6240\u89c4\u5b9a\u7684\u6700\u5c0f\u65f6\u95f4\u7247\u5927\u5c0f\u3002<\/p>\n<h3>2.\u5bf9\u53ef\u8fd0\u884c\u961f\u5217\u7684\u64cd\u4f5c<\/h3>\n<p>\u5728\u4f18\u5148\u7ea7\u6570\u7ec4\u7ed3\u6784prio_array\u4e2d\uff0c\u6570\u7ec4queue\u7528\u6765\u8868\u793a\u7cfb\u7edf\u4e2d\u6bcf\u79cd\u4f18\u5148\u7ea7\u8fdb\u7a0b\u6240\u5f62\u6210\u7684\u53ef\u8fd0\u884c\u961f\u5217\uff0c\u800c\u4e14\u8fc7\u671f\u8fdb\u7a0b\u548c\u6d3b\u52a8\u8fdb\u7a0b\u5206\u522b\u5bf9\u5e94\u8fd9\u6837\u4e00\u4e2a\u6570\u7ec4\u3002<\/p>\n<p>\u5982\u679c\u8fdb\u7a0b\u4ecd\u7136\u5904\u4e8e\u6d3b\u52a8\u8fdb\u7a0b\u961f\u5217\u4e2d\uff0c\u5373\u8bf4\u660e\u8be5\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u672a\u7528\u5b8c\u3002\u5f53\u8be5\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\u5c31\u9700\u8981\u79bb\u5f00\u6d3b\u52a8\u8fdb\u7a0b\u961f\u5217\u5e76\u8fdb\u5165\u8fc7\u671f\u8fdb\u7a0b\u961f\u5217\u3002\u53ef\u8fd0\u884c\u8fdb\u7a0b\u8fdb\u5165\u8fdb\u7a0b\u961f\u5217\u662f\u901a\u8fc7enqueue_task\u51fd\u6570\u5b8c\u6210\u7684\uff0c\u800c\u79bb\u5f00\u8fdb\u7a0b\u961f\u5217\u662f\u901a\u8fc7dequeue_task\u51fd\u6570\u5b8c\u6210\u7684\u3002<\/p>\n<p>\u6bcf\u4e2a\u8fdb\u7a0b\u7684task_struct\u7ed3\u6784\u4e2d\u90fd\u6709list_head\u7c7b\u578b\u7684run_list\u5b57\u6bb5\uff0c\u5c06\u8fdb\u7a0b\u4ece\u53ef\u8fd0\u884c\u961f\u5217\u4e2d\u5220\u9664\u8d77\u59cb\u5c31\u662f\u5bf9\u53cc\u8054\u8868\u7684\u64cd\u4f5c\uff0c\u540c\u65f6\u6211\u4eec\u9700\u8981\u66f4\u65b0\u4f18\u5148\u7ea7\u6570\u7ec4\u7ed3\u6784\u4e2d\u6d3b\u52a8\u8fdb\u7a0b\u7684\u6570\u76eenr_active\u3002\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u4f18\u5148\u7ea7\u6240\u5bf9\u5e94\u7684\u53ef\u8fd0\u884c\u961f\u5217\u5df2\u7a7a\uff0c\u90a3\u4e48\u8fd8\u8981\u6e05\u9664\u4f18\u5148\u7ea7\u4f4d\u56fe\u4e2d\u8be5\u8fdb\u7a0b\u4f18\u5148\u7ea7\u6240\u5bf9\u5e94\u7684\u90a3\u4e2a\u4f4d\u3002<\/p>\n<p>\u5982\u679c\u8981\u8fdb\u7a0b\u67d0\u4e2a\u53ef\u8fd0\u884c\u961f\u5217\uff0c\u6240\u505a\u7684\u5de5\u4f5c\u57fa\u672c\u4e0a\u8ddf\u5220\u9664\u76f8\u53cd\u3002\u4e0d\u8fc7\u8be5\u51fd\u6570\u9996\u5148\u901a\u8fc7sched_info_queued\u51fd\u6570\u66f4\u65b0\u8be5\u8fdb\u7a0b\u6700\u540e\u8fdb\u5165\u53ef\u8fd0\u884c\u961f\u5217\u7684\u65f6\u95f4\u6233\uff0c\u5e76\u4e14\u5728\u6700\u540e\u66f4\u65b0\u8be5\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u7684array\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u6307\u5411\u5f53\u524d\u8fdb\u7a0b\u6240\u5728\u7684\u4f18\u5148\u7ea7\u6570\u7ec4\u3002<\/p>\n<pre class=\"brush:c\"> static void dequeue_task(struct task_struct *p, prio_array_t *array)\r\n  {\r\n          array-&gt;nr_active--;\r\n          list_del(&amp;p-&gt;run_list);\r\n          if (list_empty(array-&gt;queue + p-&gt;prio))\r\n                  __clear_bit(p-&gt;prio, array-&gt;bitmap);\r\n  }\r\n\r\n  static void enqueue_task(struct task_struct *p, prio_array_t *array)\r\n  {\r\n          sched_info_queued(p);\r\n          list_add_tail(&amp;p-&gt;run_list, array-&gt;queue + p-&gt;prio);\r\n          __set_bit(p-&gt;prio, array-&gt;bitmap);\r\n          array-&gt;nr_active++;\r\n         p-&gt;array = array;\r\n  }<\/pre>\n<h3>3.schedule_tick()<\/h3>\n<p>schedule_tick\u51fd\u6570\u7528\u6765\u66f4\u65b0\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\uff0c\u5b83\u88ab\u8c03\u7528\u65f6\u672c\u5730\u4e2d\u65ad\u88ab\u7981\u6b62\uff0c\u8be5\u51fd\u6570\u7684\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\u3002<\/p>\n<p>1.\u9996\u5148\u901a\u8fc7\u76f8\u5e94\u7684\u51fd\u6570\u548c\u5b8f\u83b7\u5f97\u5f53\u524d\u5904\u7406\u5668\u7684\u7f16\u53f7\u3001\u5f53\u524d\u53ef\u8fd0\u884c\u961f\u5217\u548c\u5f53\u524d\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u5c31\uff0c\u518d\u901a\u8fc7sched_clock\u51fd\u6570\u83b7\u5f97\u6700\u8fd1\u4e00\u6b21\u5b9a\u65f6\u5668\u4e2d\u65ad\u7684\u65f6\u95f4\u6233\u3002\u5982\u679carray\u6ca1\u6709\u6307\u5411\u672c\u5730\u6d3b\u52a8\u8fdb\u7a0b\u961f\u5217\uff0c\u5219\u8bbe\u7f6eTIF_NEED_RESCHED\u6807\u5fd7\uff0c\u4ee5\u4fbf\u5728\u7a0d\u5019\u5f3a\u5236\u8fdb\u7a0b\u91cd\u65b0\u8c03\u5ea6\u3002<\/p>\n<pre class=\"brush:c\">void scheduler_tick(void)\r\n{\r\n\tint cpu = smp_processor_id();\r\n\trunqueue_t *rq = this_rq();\r\n\ttask_t *p = current;\r\n\r\n\trq-&gt;timestamp_last_tick = sched_clock();\r\n\r\n\tif (p == rq-&gt;idle) {\r\n\t\tif (wake_priority_sleeper(rq))\r\n\t\t\tgoto out;\r\n\t\trebalance_tick(cpu, rq, SCHED_IDLE);\r\n\t\treturn;\r\n\t}\r\n\tif (p-&gt;array != rq-&gt;active) {\r\n\t\tset_tsk_need_resched(p);\r\n\t\tgoto out;\r\n\t}<\/pre>\n<p>2.\u7531\u4e8e\u5b9e\u65f6\u8fdb\u7a0b\u548c\u666e\u901a\u8fdb\u7a0b\u7684\u8c03\u5ea6\u65b9\u6cd5\u4e0d\u540c\uff0c\u56e0\u6b64\u8fd9\u4e24\u79cd\u8fdb\u7a0b\u5bf9\u65f6\u95f4\u7247\u7684\u66f4\u65b0\u65b9\u5f0f\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u4e0b\u9762\u4ec5\u8bf4\u660e\u666e\u901a\u8fdb\u7a0b\u66f4\u65b0\u65f6\u95f4\u7247\u7684\u65b9\u5f0f\u3002\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u662f\u666e\u901a\u8fdb\u7a0b\uff0c\u5219\u9012\u51cf\u5f53\u524d\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u3002<br \/>\n3.\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u65f6\u95f4\u7247\u7528\u5b8c\uff0c\u9996\u5148\u4ece\u5f53\u524d\u6d3b\u52a8\u8fdb\u7a0b\u96c6\u5408\u4e2d\u5220\u9664\u8be5\u8fdb\u7a0b\uff0c\u7136\u540e\u901a\u8fc7set_tsk_need_resched\u51fd\u6570\u8bbe\u7f6eTIF_NEED_RESCHED\u6807\u5fd7\u3002<\/p>\n<p>\u63a5\u7740\u901a\u8fc7effective_prio\u51fd\u6570\u66f4\u65b0\u5f53\u524d\u8fdb\u7a0b\u7684\u52a8\u6001\u4f18\u5148\u7ea7\uff0c\u5728\u8fdb\u7a0b\u8c03\u5ea6\u7684\u57fa\u672c\u539f\u7406\u4e2d\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u8fdb\u7a0b\u7684\u52a8\u6001\u4f18\u5148\u7ea7\u662f\u4ee5\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7(static_prio)\u4e3a\u57fa\u6570\uff0c\u5728\u901a\u8fc7bonus\u9002\u5f53\u7684\u5bf9\u5176\u60e9\u7f5a\u6216\u5956\u52b1\u3002<\/p>\n<pre class=\"brush:c\">static int effective_prio(task_t *p)\r\n{\r\n\tint bonus, prio;\r\n\r\n\tif (rt_task(p))\r\n\t\treturn p-&gt;prio;\r\n\r\n\tbonus = CURRENT_BONUS(p) - MAX_BONUS \/ 2;\r\n\r\n\tprio = p-&gt;static_prio - bonus;\r\n\tif (prio &lt; MAX_RT_PRIO) \t\tprio = MAX_RT_PRIO; \tif (prio &gt; MAX_PRIO-1)\r\n\t\tprio = MAX_PRIO-1;\r\n\treturn prio;\r\n}<\/pre>\n<p>\u901a\u8fc7effective_prio\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8fdb\u7a0b\u52a8\u6001\u4f18\u5148\u7ea7\u7684\u8ba1\u7b97\u89c4\u5219\uff1a<\/p>\n<p>\u52a8\u6001\u4f18\u5148\u7ea7=max(100 , min(\u9759\u6001\u4f18\u5148\u7ea7 \u2013 bonus + 5) , 139)<\/p>\n<p>\u518d\u901a\u8fc7task_timeslice\u51fd\u6570\u5bf9\u5f53\u524d\u8fdb\u7a0b\u91cd\u65b0\u5206\u914d\u65f6\u95f4\u7247\uff0c\u56e0\u4e3a\u6211\u73b0\u5728\u6240\u5904\u7684\u5206\u6790\u73af\u5883\u662f\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u5df2\u7ecf\u7528\u5b8c\u3002\u7136\u540e\u5c06first_time_slice\u7684\u503c\u8bbe\u7f6e\u4e3a0\uff0c\u8bf4\u660e\u5f53\u524d\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u53ef\u4ee5\u7528\u5b8c\u3002<\/p>\n<p>\u4e0a\u8ff0\u8fc7\u7a0b\u7684\u4ee3\u7801\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">\tif (rt_task(p)) {\r\n\t\t\/*\r\n\t\t *\u66f4\u65b0\u5b9e\u65f6\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\r\n\t\t *\/\r\n\t}\r\n\tif (!--p-&gt;time_slice) {\r\n\t\tdequeue_task(p, rq-&gt;active);\r\n\t\tset_tsk_need_resched(p);\r\n\t\tp-&gt;prio = effective_prio(p);\r\n\t\tp-&gt;time_slice = task_timeslice(p);\r\n\t\tp-&gt;first_time_slice = 0;<\/pre>\n<p>4.\u8fd0\u884c\u961f\u5217\u7ed3\u6784\u4e2d\u7684expired_timestamp\u5b57\u6bb5\u7528\u6765\u63cf\u8ff0\u8fc7\u671f\u8fdb\u7a0b\u961f\u5217\u4e2d\u6700\u65e9\u8fdb\u7a0b\u88ab\u63d2\u5165\u961f\u5217\u7684\u65f6\u95f4\uff0c\u5982\u679c\u672c\u5730\u8fd0\u884c\u961f\u5217\u4e2d\u8be5\u5b57\u6bb5\u7b49\u4e8e0\uff0c\u5219\u8bf4\u660e\u5f53\u524d\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u4e3a\u7a7a\u3002\u56e0\u6b64\u5c06\u5f53\u524d\u7684\u65f6\u949f\u8282\u62cd\u8d4b\u503c\u7ed9\u8be5\u5b57\u6bb5\u3002<\/p>\n<p>\u7531\u4e8e\u5f53\u524d\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u5df2\u7ecf\u7528\u5b8c\uff0c\u56e0\u6b64\u63a5\u4e0b\u6765\u5e94\u8be5\u5224\u5b9a\u662f\u5c06\u5f53\u524d\u8fdb\u7a0b\u63d2\u5165\u6d3b\u52a8\u8fdb\u7a0b\u96c6\u5408\u8fd8\u662f\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u3002\u53ef\u80fd\u6b64\u65f6\u4f60\u4f1a\u6709\u7591\u60d1\uff1a\u65e2\u7136\u5f53\u524d\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u5df2\u7ecf\u7528\u5b8c\uff0c\u5c31\u5e94\u8be5\u76f4\u63a5\u63d2\u5165\u8fc7\u671f\u8fdb\u7a0b\u961f\u5217\uff0c\u4e3a\u4f55\u8fd8\u8981\u8fdb\u884c\u5224\u65ad\u63d2\u5165\u90a3\u4e2a\u8fdb\u7a0b\u96c6\u5408\uff1f<\/p>\n<p>\u6b63\u5982\u57fa\u672c\u539f\u7406\u4e2d\u6240\u8bf4\u7684\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u603b\u504f\u5411\u4ea4\u4e92\u8fdb\u7a0b\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u80fd\u529b\u3002\u56e0\u6b64\u5f53\u4ea4\u4e92\u578b\u8fdb\u7a0b\u4f7f\u7528\u5b8c\u65f6\u95f4\u7247\u540e\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u603b\u662f\u91cd\u65b0\u586b\u5145\u65f6\u95f4\u7247\u5e76\u628a\u5b83\u7559\u5728\u6d3b\u52a8\u8fdb\u7a0b\u96c6\u5408\u4e2d\u3002\u4f46\u8c03\u5ea6\u7a0b\u5e8f\u5e76\u4e0d\u6c38\u8fdc\u90fd\u504f\u5411\u4ea4\u4e92\u578b\u7a0b\u5e8f\uff0c\u5982\u679c\u6700\u65e9\u8fdb\u5165\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u7684\u8fdb\u7a0b\u5df2\u7ecf\u7b49\u5f85\u4e86\u5f88\u957f\u65f6\u95f4\uff0c\u6216\u8fc7\u671f\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7\u6bd4\u4ea4\u4e92\u8fdb\u7a0b\u7684\u9759\u6001\u4f18\u5148\u7ea7\u9ad8\uff0c\u6b64\u65f6\u8c03\u5ea6\u7a0b\u5e8f\u5c31\u4f1a\u5c06\u65f6\u95f4\u7247\u7528\u5b8c\u7684\u4ea4\u4e92\u8fdb\u7a0b\u8f6c\u79fb\u5230\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u4e2d\u3002EXPIRED_STARVING\u5b8f\u5b8c\u6210\u7684\u5de5\u4f5c\u5c31\u662f\u5224\u65ad\u4e0a\u8ff0\u4e24\u79cd\u60c5\u51b5\uff0c\u81f3\u5c11\u5176\u4e00\u5426\u548c\uff0c\u8be5\u5b8f\u5c31\u4ea7\u751f\u503c1\u3002<\/p>\n<p>\u5982\u679c\u8bf4\u5f53\u524d\u8fdb\u7a0b\u5df2\u7ecf\u79fb\u5165\u5230\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u4e2d\uff0c\u8fd8\u9700\u6839\u636e\u5f53\u524d\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u66f4\u65b0\u8fd0\u884c\u961f\u5217\u7ed3\u6784\u4e2d\u7684best_expired_prio\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u7528\u4e8e\u8bb0\u5f55\u8fc7\u671f\u8fdb\u7a0b\u96c6\u5408\u4e2d\u6700\u9ad8\u7684\u9759\u6001\u4f18\u5148\u7ea7\u3002<\/p>\n<p>\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u662f\u4ea4\u4e92\u8fdb\u7a0b\uff0c\u800c\u4e14\u4e0d\u6ee1\u8db3EXPIRED_STARVING\u5b8f\uff0c\u5219\u76f4\u63a5\u5c06\u8be5\u4ea4\u4e92\u8fdb\u7a0b\u7ee7\u7eed\u63d2\u5165\u6d3b\u52a8\u8fdb\u7a0b\u96c6\u5408\u4e2d\u3002<\/p>\n<pre class=\"brush:c\">\t\tif (!rq-&gt;expired_timestamp)\r\n\t\t\trq-&gt;expired_timestamp = jiffies;\r\n\t\tif (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) {\r\n\t\t\tenqueue_task(p, rq-&gt;expired);\r\n\t\t\tif (p-&gt;static_prio &lt; rq-&gt;best_expired_prio)\r\n\t\t\t\trq-&gt;best_expired_prio = p-&gt;static_prio;\r\n\t\t} else\r\n\t\t\tenqueue_task(p, rq-&gt;active);<\/pre>\n<p>5.\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u5e76\u672a\u4f7f\u7528\u5b8c\u65f6\u95f4\u7247\uff0c\u5219\u68c0\u67e5\u5f53\u524d\u8fdb\u7a0b\u7684\u5269\u4f59\u65f6\u95f4\u7247\u662f\u5426\u592a\u957f\u3002\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u65f6\u95f4\u7247\u8fc7\u957f\u7684\u8bdd\uff0c\u5c31\u5c06\u8be5\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u5206\u6210\u82e5\u5e72\u4e2a\u66f4\u5c0f\u7684\u65f6\u95f4\u6bb5\uff0c\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u62e5\u6709\u8f83\u957f\u65f6\u95f4\u7247\u7684\u8fdb\u7a0b\u957f\u65f6\u95f4\u9738\u5360CPU\u3002\u5e76\u4e14\u8c03\u5ea6\u7a0b\u5e8f\u4f1a\u5c06\u8fd9\u6837\u7684\u8fdb\u7a0b\u653e\u5165\u4e0e\u8be5\u8fdb\u7a0b\u4f18\u5148\u7ea7\u6240\u5bf9\u5e94\u7684\u6d3b\u52a8\u8fdb\u7a0b\u961f\u5217\u7684\u672b\u5c3e\uff0c\u7a0d\u5019\u518d\u6b21\u5bf9\u5176\u96c6\u6210\u8c03\u5ea6\u3002<\/p>\n<pre class=\"brush:c\">\r\n\t} else {\r\n\t\tif (TASK_INTERACTIVE(p) && !((task_timeslice(p) -\r\n\t\t\tp->time_slice) % TIMESLICE_GRANULARITY(p)) &&\r\n\t\t\t(p->time_slice >= TIMESLICE_GRANULARITY(p)) &&\r\n\t\t\t(p->array == rq->active)) {\r\n\r\n\t\t\trequeue_task(p, rq->active);\r\n\t\t\tset_tsk_need_resched(p);\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>\u81f3\u6b64\uff0c\u8be5\u51fd\u6570\u5206\u6790\u5b8c\u6bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u4e24\u7bc7\u6587\u7ae0\u4ece\u539f\u7406\u89d2\u5ea6\u5206\u6790\u4e86\u8fdb\u7a0b\u7684\u8c03\u5ea6\uff0c\u672c\u6587\u5c06\u4ece\u5177\u4f53\u7684\u6e90\u7801\u51fa\u53d1\uff0c\u5206\u6790\u4e0e\u8fdb\u7a0b\u8fdb\u7a0b\u8c03\u5ea6\u5bc6\u5207\u76f8\u5173\u7684\u51e0\u4e2a\u51fd\u6570\u3002 1.\u65f6 [&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":[108,161,183,212],"class_list":["post-2870","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-161","tag-183","tag-212"],"views":7133,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2870","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=2870"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2870\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2870"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}