ÔõÑùʹÓÃLinux¾ÙÐÐÀú³Ìµ÷ÀíÓÅ»¯
ÔõÑùʹÓÃlinux¾ÙÐÐÀú³Ìµ÷ÀíÓÅ»¯
Ëæ×ÅÅÌËã»úÊÖÒÕµÄÉú³¤ºÍ»¥ÁªÍøµÄÆÕ¼°£¬¶ÔÅÌËã»úϵͳµÄÐÔÄÜÒªÇóÔ½À´Ô½¸ß¡£Àú³Ìµ÷Àí×÷Ϊ²Ù×÷ϵͳµÄÖ÷Òª¹¦Ð§Ö®Ò»£¬ÔÚÌá¸ßϵͳÐÔÄÜ·½ÃæÆð×ÅÖ÷Òª×÷Óá£Linux×÷Ϊһ¸ö¿ªÔ´µÄ²Ù×÷ϵͳ£¬¾ßÓÐÓÅÒìµÄ¿É¶¨ÖÆÐÔºÍÀ©Õ¹ÐÔ£¬Ê¹µÃÎÒÃÇ¿ÉÒÔͨ¹ýÓÅ»¯Àú³Ìµ÷ÀíÀ´Ìá¸ßÅÌËã»úϵͳµÄÐÔÄÜ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlinux¾ÙÐÐÀú³Ìµ÷ÀíÓÅ»¯£¬²¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢ÏàʶLinuxµÄÀú³Ìµ÷ÀíÆ÷
LinuxµÄÀú³Ìµ÷ÀíÆ÷ÈÏÕæ¾öÒéÓ¦¸ÃÔËÐÐÄĸöÀú³Ì£¬²¢¾öÒéÀú³ÌÔÚCPUÉÏÔËÐеÄʱ¼ä¡£LinuxʹÓÃÍêÈ«¹«Õýµ÷Àí£¨Completely Fair Scheduler£¬CFS£©×÷ΪĬÈϵÄÀú³Ìµ÷ÀíËã·¨¡£CFSͨ¹ýÅÌËãÿ¸öÀú³ÌµÄÐéÄâÔËÐÐʱ¼ä£¬À´ÊµÏÖ¶ÔÀú³ÌµÄµ÷Àí¡£CFS»áƾ֤Àú³ÌµÄÓÅÏȼ¶À´·ÖÅÉÔËÐÐʱ¼äƬ£¬ÓÅÏȼ¶Ô½¸ßµÄÀú³Ì½«»ñµÃ¸ü¶àµÄÔËÐÐʱ¼ä¡£
¶þ¡¢Ê¹ÓÃniceºÍreniceµ÷½âÀú³ÌÓÅÏȼ¶
LinuxÌṩÁËniceºÍreniceÏÂÁîÀ´µ÷½âÀú³ÌµÄÓÅÏȼ¶¡£niceÏÂÁîÓÃÀ´Æô¶¯ÐÂÀú³Ì£¬²¢ÇÒ¿ÉÒÔÉèÖÃÀú³ÌµÄÓÅÏȼ¶¡£ÓÅÏȼ¶¹æģΪ-20µ½19£¬ÆäÖÐ-20Ϊ×î¸ßÓÅÏȼ¶£¬19Ϊ×îµÍÓÅÏȼ¶¡£
ʾÀý´úÂëÈçÏ£º
nice -n 10 ./myprogram
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫ÒÔÓÅÏȼ¶Îª10Æô¶¯myprogramÀú³Ì¡£
reniceÏÂÁîÓÃÀ´µ÷½âÒѾÔËÐеÄÀú³ÌµÄÓÅÏȼ¶¡£reniceÏÂÁîÐèÒªÖ¸¶¨Àú³ÌµÄPIDºÍеÄÓÅÏȼ¶¡£
ʾÀý´úÂëÈçÏ£º
renice 10 12345
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫½«PIDΪ12345µÄÀú³ÌµÄÓÅÏȼ¶µ÷½âΪ10¡£
Èý¡¢Ê¹ÓÃsched_setschedulerÉèÖÃÀú³Ìµ÷ÀíÕ½ÂÔ
LinuxÌṩÁËsched_setschedulerº¯ÊýÀ´ÉèÖÃÀú³ÌµÄµ÷ÀíÕ½ÂÔ¡£¿ÉÒÔͨ¹ýŲÓô˺¯ÊýÀÍÆÈ´»Àú³ÌµÄµ÷ÀíÕ½ÂÔΪʵʱµ÷Àí»òÕßͨË×µ÷Àí¡£
ʾÀý´úÂëÈçÏ£º
#include <sched.h> int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param);
µÇ¼ºó¸´ÖÆ
ÆäÖÐpidΪÀú³ÌµÄPID£¬policyΪµ÷ÀíÕ½ÂÔ£¬paramΪµ÷Àí²ÎÊý¡£
³£¼ûµÄµ÷ÀíÕ½ÂÔÓÐÈýÖÖ£º
SCHED_FIFO£ºÊµÊ±ÏȽøÏȳöÕ½ÂÔ¡£
SCHED_RR£ºÊµÊ±ÂÖתսÂÔ¡£
SCHED_OTHER£ºÍ¨Ë×µ÷ÀíÕ½ÂÔ¡£
ʾÀý´úÂëÈçÏ£º
#include <sched.h> int main() { struct sched_param scheduling_param; scheduling_param.sched_priority = 1; //ÓÅÏȼ¶Îª1 sched_setscheduler(getpid(), SCHED_FIFO, &scheduling_param); //...ÆäËû´úÂë return 0; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Ŀ½ñÀú³ÌµÄµ÷ÀíÕ½ÂÔÉèÖÃΪʵʱÏȽøÏȳöÕ½ÂÔ£¬²¢½«ÓÅÏȼ¶ÉèÖÃΪ1¡£
ËÄ¡¢Ê¹ÓÃcgroupsÏÞÖÆÀú³Ì×ÊÔ´
cgroupsÊÇLinuxÄÚºËÌṩµÄÒ»ÖÖ×ÊÔ´¿ØÖÆ»úÖÆ£¬¿ÉÒÔÓÃÓÚÏÞÖÆÀú³ÌµÄ×ÊԴʹÓá£cgroups¿ÉÒÔÉèÖÃÀú³ÌµÄCPUÅä¶î£¬ÄÚ´æʹÓÃÏÞÖƵȣ¬´Ó¶ø¶ÔÀú³ÌµÄ×ÊԴʹÓþÙÐÐÓÅ»¯¡£
ʾÀý´úÂëÈçÏ£º
# ½¨ÉèÒ»¸öÃûΪmygroupµÄcgroup sudo cgcreate -g cpu,cpuacct,memory:/mygroup # ½«Ö¸¶¨µÄÀú³ÌPID¼ÓÈëµ½mygroup sudo cgclassify -g cpu,cpuacct,memory:/mygroup <PID> # ÉèÖÃmygroupµÄCPUÅä¶îΪ50% sudo cgset -r cpu.cfs_quota_us=50000 /mygroup # ÉèÖÃmygroupµÄÄÚ´æÏÞÖÆΪ1GB sudo cgset -r memory.limit_in_bytes=1G /mygroup
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽨ÉèÁËÒ»¸öÃûΪmygroupµÄcgroup£¬²¢½«Ö¸¶¨µÄÀú³ÌPID¼ÓÈëµ½mygroup¡£È»ºóÉèÖÃmygroupµÄCPUÅä¶îΪ50%£¬ÄÚ´æÏÞÖÆΪ1GB¡£
½áÓï
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃlinux¾ÙÐÐÀú³Ìµ÷ÀíÓÅ»¯£¬²¢¸ø³öÁËÏìÓ¦µÄ´úÂëʾÀý¡£Í¨¹ýµ÷½âÀú³ÌµÄÓÅÏȼ¶¡¢ÉèÖÃÀú³ÌµÄµ÷ÀíÕ½ÂÔÒÔ¼°ÏÞÖÆÀú³ÌµÄ×ÊԴʹÓ㬿ÉÒÔÓÐÓÃÌá¸ßÅÌËã»úϵͳµÄÐÔÄÜ¡£Ï£Íû±¾ÎÄ¿ÉÒÔ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·LinuxµÄÀú³Ìµ÷Àí»úÖÆ£¬²¢ÔÚÏÖʵӦÓÃÖÐÊ©Õ¹×÷Óá£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLinux¾ÙÐÐÀú³Ìµ÷ÀíÓÅ»¯µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡