ããã°ãã¯ã @matsumotoryã§ãã
ä¸è¨ã¨ã³ããªã«ããã¦ãããã»ã¹ã®å¤§éã¡ã¢ãªç¢ºä¿ã«ä¼´ããã¼ã¸ãã¼ãã«ãµã¤ãºã¨ãã¼ã¸ãã¼ãã«ã¨ã³ããªæ°ã®è¥å¤§åã«ããcloneãexecveã®æ§è½å£åã¨CPU使ç¨æéã®å°æåé¡ãããã³ããããã®è§£æ±ºæ¹æ³ã«ã¤ãã¦ã·ã¹ãã ã³ã¼ã«ã¬ãã«ã§ç¢ºèªãã¾ããã
ããã§ä»åã¯ãã·ã¹ãã ã³ã¼ã«ããã®ã«ã¼ãã«å é¨ã®å¦çã®æ§è½ãã¨ããããã¯ãããå®è·µçãªç°å¢ã§ããApache httpdã¨mod_cgiãç¨ãã¦ãphpinfo()ãå®è¡ããã ãã®CGIã«å¯¾ãã¦ãã³ããã¼ã¯ããããæã«ã©ããããCPUã®idleã空ãããã·ã¹ãã CPUã®ä½¿ç¨éãå¤ãããããåå示ãã解決æ¹æ³ã®1ã¤ã§ããHugePagesã使ããã©ããã®è¦³ç¹ã§æ¯è¼ãã¦ã¿ã¾ãããã
ç¹å®æ¡ä»¶ä¸ã®Webãµã¼ãç°å¢ã®ã·ã¹ãã CPUã«èµ·å ããé«è² è·åé¡ãããã·ã¹ãã ã³ã¼ã«ãã«ã¼ãã«ã®ã½ã¼ã¹ã³ã¼ããèªã¿ãªããæ ¹æ¬åå ã®ä»®èª¬ããã¦ããããååã¨ã³ããªã§æ¤è¨¼ãã¦ãã¯ããªè¦ç¹ã§å¹æã確èªããä¸ã§ãä»åã¯å度å®è·µçãªç°å¢ã§è² è·æ¤è¨¼ãè¡ã£ã¦ãOSã»ã«ã¼ãã«ã¬ãã«ã®å¯¾å¿ãå ¨ä½ã¨ãã¦å®è·µçãªç°å¢ã§ã©ããããå¹æããããã確èªãããã¨ããæå³ã§ãã
ã¾ãã¯ãã¼ã¸ãµã¤ãºãããã©ã«ã4KBã®httpd
å¼ç¤¾ã§ã¯è¶ é«éç©ãªWebãã¹ãã£ã³ã°ãæä¾ãã¦ããã®ã§ãhttpd+mod_cgiã«ããã¦ã¯ããã®è¶ é«éç©ãåç¾ãã¹ã10ä¸vhostãä½æãã¦ããã¦ãäºãåpreforkãããhttpdããã»ã¹ã4GBç¨åº¦ã¡ã¢ãªã確ä¿ãã¦ããç¶æ ãåç¾ãã¾ããããã¯ãä¸è¨ã¨ã³ããªã§cloneã®é度ãæ¤è¨¼ããæã¨åãç°å¢ï¼CPUã³ã¢24åãã¡ã¢ãª32GBã256åã®prefork MPMï¼ã«ãªãã¾ãã
- 以ä¸ã®ããã«æ®éã«èµ·å
/usr/sbin/httpd
ã§æ®éã«èµ·åããå¾ã以ä¸ã®ãã³ããã¼ã¯ãå®æ½ãã¾ãããã®ãã³ããã¼ã¯ã«ãã£ã¦ãããããé«è² è·ç¶æ ãåç¾ãã¾ãã
ab -l -HUser-Agent:hoge -k -c 150 -n 10000000 http://test.example.jp/info.php
- ãã³ããã¼ã¯ä¸ã®
top
ããã¦ããã³ããã¼ã¯ä¸ã®CPU使ç¨çãã³ã¢åä½ã§è¦ã¦ã¿ã¾ãããã
top - 18:07:01 up 9 days, 13 min, 4 users, load average: 140.95, 88.39, 43.33 Tasks: 689 total, 151 running, 538 sleeping, 0 stopped, 0 zombie Cpu0 : 4.3%us, 95.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu1 : 3.6%us, 95.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.7%si, 0.0%st Cpu2 : 3.0%us, 96.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Cpu3 : 7.2%us, 92.4%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Cpu4 : 12.2%us, 87.5%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Cpu5 : 3.6%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0.0%hi, 0.0%si, 0.0%st Cpu21 : 10.6%us, 89.4%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu22 : 24.1%us, 75.9%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu23 : 7.3%us, 92.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 32862764k total, 29007852k used, 3854912k free, 13300k buffers Swap: 16777212k total, 222652k used, 16554560k free, 199396k cached
ãã®ããã«ãé常ã®ãã¼ã¸ãµã¤ãºã§ã¯idleã¯0%
ã§ãããç¹ã«ã·ã¹ãã CPUã®å°ææéã90ï¼
åå¾ã¨é常ã«å¤§ãããã¨ããããã¾ããããããã«å®å
¨ã«CPUã100%使ããã£ã¦ããç¶æ³ã¨ããã¾ãã
- perfã§kernelã®ã·ã³ãã«ã®CPU使ç¨çã確èª
ç¶ãã¦ãã¦ã¼ã¶ã©ã³ãã¨ã«ã¼ãã«ã§å®è¡ãããã¦ãå¦çã®ã·ã³ãã«ãçµ±ä¸çã«ã¿ã¦ããã®ä½¿ç¨çã確èªãã¾ãã
Samples: 12M of event 'cycles', Event count (approx.): 873395738882 21.55% [kernel] [k] zap_pte_range 20.55% [kernel] [k] copy_pte_range 5.72% [kernel] [k] change_pte_range 5.47% [kernel] [k] page_remove_rmap 5.00% [kernel] [k] free_pages_and_swap_cache 3.47% ld-2.12.so [.] do_lookup_x 3.16% [kernel] [k] release_pages 2.37% ld-2.12.so [.] strcmp 1.48% [kernel] [k] vm_normal_page 1.43% ld-2.12.so [.] _dl_map_object 1.13% ld-2.12.so [.] _dl_relocate_object 1.10% [kernel] [k] copy_page 1.04% [kernel] [k] __tlb_remove_page 0.91% [kernel] [k] clear_page_c_e 0.89% libc-2.12.so [.] _int_free 0.71% [kernel] [k] _raw_spin_lock 0.58% libc-2.12.so [.] _int_malloc
ããã¾ã§ã®èª¿æ»ãåã³ãååã®ã¨ã³ããªã®èª¿æ»ã®ã¨ãããpteï¼ãã¼ã¸ãã¼ãã«ã¨ã³ããªï¼ã®æ°ã«æ¯ä¾ãã¦å¦çã³ã¹ããé«ããªãã·ã³ãã«ï¼clone
ããå¼ã°ããcopy_pte_range
ãevecve
ããå¼ã°ããzap_pte_range
ãªã©ï¼ãæ¯é
çã§ãããã¨ããããã¾ãã
ããã¯ãé常ã«å®ç¨ä¸å³ããç¶æ³ã§ããã
HugePagesã使ã£ã¦èµ·åããhttpdã®å ´å
ã§ã¯ãååã®ã¨ã³ããªã§èª¿æ»ããã¨ãããpteãå°ãªããããã¨ã§clone
ããå¼ã°ããcopy_pte_range
ãevecve
ããå¼ã°ããzap_pte_range
ã®ã³ã¹ãã大å¹
ã«åæ¸ããäºãåççã«ãããã¾ããããããå®ç°å¢ããã³ããã¼ã¯ä¸ã§ã©ã®ãããªå¹æããããããã詳細ã«ç¢ºèªãã¾ãã
- 以ä¸ã®ããã«HugePagesã使ã£ã¦httpdãèµ·å
HUGETLB_MORECORE=yes LD_PRELOAD=/usr/lib64/libhugetlbfs.so /usr/sbin/httpd
HugePagesãæ¢ã«20GB確ä¿ããã¦ããç¶æ ã§ãä¸è¨ã®ããã«libhugetlbfsçµç±ã§èµ·åãã¾ããHugePagesã®ç¢ºä¿ã®ä»æ¹ã¯ååã®ã¨ã³ããªã®èª¿æ»ãåç §ãã¦ãã ããã
次ã«åæ§ã®ãã³ããã¼ã¯ãæµãã¾ãã
top
top - 15:51:57 up 8 days, 21:58, 3 users, load average: 87.35, 50.87, 28.25 Tasks: 599 total, 7 running, 592 sleeping, 0 stopped, 0 zombie Cpu0 : 9.6%us, 16.8%sy, 0.0%ni, 72.6%id, 0.3%wa, 0.0%hi, 0.7%si, 0.0%st Cpu1 : 19.1%us, 21.1%sy, 0.0%ni, 59.4%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Cpu2 : 17.6%us, 18.0%sy, 0.0%ni, 64.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 10.2%us, 15.6%sy, 0.0%ni, 73.6%id, 0.3%wa, 0.0%hi, 0.3%si, 0.0%st Cpu4 : 12.5%us, 17.9%sy, 0.0%ni, 69.3%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Cpu5 : 12.7%us, 18.7%sy, 0.0%ni, 68.0%id, 0.3%wa, 0.0%hi, 0.3%si, 0.0%st Cpu6 : 13.6%us, 22.8%sy, 0.0%ni, 63.3%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu7 : 10.1%us, 35.8%sy, 0.0%ni, 53.7%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu8 : 10.3%us, 26.8%sy, 0.0%ni, 62.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu9 : 14.6%us, 26.9%sy, 0.0%ni, 58.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu10 : 7.5%us, 23.6%sy, 0.0%ni, 68.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu11 : 16.7%us, 23.2%sy, 0.0%ni, 60.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu12 : 9.1%us, 9.4%sy, 0.0%ni, 81.2%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu13 : 7.7%us, 11.7%sy, 0.0%ni, 80.2%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu14 : 6.4%us, 9.1%sy, 0.0%ni, 84.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu15 : 13.0%us, 10.4%sy, 0.0%ni, 76.3%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu16 : 9.8%us, 11.4%sy, 0.0%ni, 78.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu17 : 7.4%us, 10.7%sy, 0.0%ni, 81.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu18 : 10.1%us, 11.8%sy, 0.0%ni, 78.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu19 : 11.7%us, 17.1%sy, 0.0%ni, 71.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu20 : 3.4%us, 12.8%sy, 0.0%ni, 83.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu21 : 7.0%us, 11.0%sy, 0.0%ni, 81.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu22 : 15.4%us, 13.4%sy, 0.0%ni, 71.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu23 : 10.4%us, 11.1%sy, 0.0%ni, 78.2%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 32862764k total, 22727968k used, 10134796k free, 151744k buffers Swap: 16777212k total, 34764k used, 16742448k free, 944064k cached
ãã®ããã«è¦ãç¬éå¹æãåããç¨åº¦ã«ãã·ã¹ãã CPUã90%åå¾ã§é æã¡ãã¦ããã®ãã³ã¢å¹³å15%
ã¬ãã«ã¾ã§æ¹åãidleã0%
ã®å
¨ã空ããªãã®ç¶æ
ãããä¸æ°ã«ã³ã¢ã«ãã£ã¦ã¯80%
ç¨åº¦ã¾ã§ç©ºããã§ãæ¹åãã¦ãããã¨ãåããã¾ããã¾ããæ§è½ãæ¦ãæ°å以ä¸ã«ãªã£ã¦ãã¾ãããCPU使ç¨çããããã©ã«ãã®ãã¼ã¸ãµã¤ãºã§100ï¼
使ç¨ãã¦ããç¶æ
ã¨æ¯ã¹ãã¨ãå
¨ä½çã«CPU使ç¨ç20%ãã30ï¼
ã¨ãã£ãå¤ã¾ã§æ¹åãããã¨ãã§ãã¦ãã¾ãã
perf
æå¾ã«perf
ã§ããã¾ã§æ¯é
çã«ã·ã¹ãã CPUãå°æãã¦ããpteå¨ãã®å¦çã確èªãã¦ã¿ã¾ãããã
Samples: 17M of event 'cycles', Event count (approx.): 268305691276 7.10% [kernel] [k] rwsem_spin_on_owner 6.47% [kernel] [k] anon_vma_interval_tree_insert 6.04% ld-2.12.so [.] do_lookup_x 5.60% [unknown] [.] 0x00007fbbab2c8bff 4.96% [kernel] [k] osq_lock 4.79% ld-2.12.so [.] strcmp 4.32% [kernel] [k] copy_page 1.85% ld-2.12.so [.] _dl_map_object 1.71% [kernel] [k] native_queued_spin_lock_slowpath 1.28% [kernel] [k] zap_pte_range 1.23% libc-2.12.so [.] _int_free 1.13% ld-2.12.so [.] _dl_name_match_p 1.13% ld-2.12.so [.] _dl_relocate_object 1.10% libc-2.12.so [.] _int_malloc 1.06% [kernel] [k] anon_vma_interval_tree_remove 0.99% [kernel] [k] clear_page_c_e 0.91% [kernel] [k] page_fault
ãã®ããã«ã»ã¼pteã«é¢ããå¦çã®ãªã¼ãã¼ãããããªããªã£ããã¨ããããã¾ãã
ã¾ã¨ã
HugePagesåãããã¨ã«ãã£ã¦ã24ã³ã¢ãã¹ã¦ã®CPUã主ã«ã·ã¹ãã CPU使ç¨æéã«ãã£ã¦å°æããããããªç¶æ³ä¸ã«ããã¦ãidleãã³ã¢ã«ãã£ã¦ã¯80%è¿ã空ãããã¨ãã§ããããã«ãªããCPU使ç¨çãã»ã¼100%ãã10%ã¾ã§ã·ã¹ãã CPUãä½æ¸ãããã¨ãã§ãã¾ããã
ããã¯ãfork -> execve suexec -> execve php-cgi
ã«ããããpteã®copyãzapã®ã«ã¼ããåç´è¨ç®ã§1/512ã®æ°ã«å¤§å¹
ä½æ¸ããããããã¨èãããã¾ãã
ç¹ã«ãååçç®ããclone
ã ãã§ãªãexecve
ã®zap_pte_range
ãªã©ã§ãå¹ãã¦ããã®ã§ãçµæçã«ã¯é常ã«ã·ã¹ãã CPUãç¯ç´ã§ããäºã«ãªãã¾ãã
ä»å¾ã¯ãããã«ãã£ã¦CPUã®idleãä½ããæ§ã«ãªã£ãä¸æ¹ã§ããã®ãããªforkå ã®ããã»ã¹ã4GBãæ±ãããããªç¶æ³ã§ãCGIãå®è¡ããå ´åã«ããããCPUãããã«å¹çãã使ã£ã¦ã¬ã¹ãã³ã¹ãéãè¿ãããã«ã¯ã©ããããããããã¾ãããã«ãããã»ã¹å¯¾å¿ã®èªä½cgiãã¼ã¢ã³ã«ãã£ã¦èµ·åæããããã»ã¹ã®æ±ããã¡ã¢ãªãµã¤ãºãæ¸ããã¦ããã¦forkï¼ãvforkï¼execã®ã¿ãæ å½ãããã¨ãã£ãã¢ããã¼ãã«ã¤ãã¦ã調æ»ã»éçºãã¦ããã¾ãã®ã§ãæå¾ ãã ããã