尊龙凯时人生就是博

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

1. Åä¾°

ÓÐʱ¼ä»áÓöµ½Ò»Ð©ÒÉÄÑÔÓÖ¢£¬²¢ÇÒ¼à¿Ø²å¼þ²¢²»¿ÉÒ»ÑÛÁ¢Âí·¢Ã÷ÎÊÌâµÄȪԴ¡£Õâʱ¼ä¾ÍÐèÒªµÇ¼ЧÀÍÆ÷½øÒ»²½ÉîÈëÆÊÎöÎÊÌâµÄȪԴ¡£ÄÇôÆÊÎöÎÊÌâÐèÒªÓÐÒ»¶¨µÄÊÖÒÕÂÄÀú»ýÀÛ£¬²¢ÇÒÓÐЩÎÊÌâÉæ¼°µ½µÄÁìÓòºÜÊǹ㣬²Å»ª¶¨Î»µ½ÎÊÌâ¡£ÒÔÊÇ£¬ÆÊÎöÎÊÌâºÍ²È¿ÓÊǺÜÊÇÄ¥Á¶Ò»Ð¡ÎÒ˽È˵ÄÉú³¤ºÍÌáÉý×ÔÎÒÄÜÁ¦¡£ÈôÊÇÎÒÃÇÓÐÒ»Ì׺õÄÆÊÎö¹¤¾ß£¬Äǽ«ÊÇÊ°빦±¶£¬Äܹ»×ÊÖú¸÷ÈË¿ìËÙ¶¨Î»ÎÊÌ⣬½ÚÔ¼¸÷ÈËÐí¶àʱ¼ä×ö¸üÉîÈëµÄÊÂÇé¡£

2. ˵Ã÷

±¾ÆªÎÄÕÂÖ÷ÒªÏÈÈÝÖÖÖÖÎÊÌⶨλµÄ¹¤¾ßÒÔ¼°»áÍŽ᰸ÀýÆÊÎöÎÊÌâ¡£

3. ÆÊÎöÎÊÌâµÄÒªÁìÂÛ

Ì×ÓÃ5W2HÒªÁ죬¿ÉÒÔÌá³öÐÔÄÜÆÊÎöµÄ¼¸¸öÎÊÌâ

What-Õ÷ÏóÊÇʲôÑùµÄ

When-ʲôʱ¼ä±¬·¢

Why-Ϊʲô»á±¬·¢

Where-ÄĸöµØ·½±¬·¢µÄÎÊÌâ

How much-ãýÃðÁ˼¸¶à×ÊÔ´

How to do-Ôõô½â¾öÎÊÌâ

4. cpu

4.1 ˵Ã÷

Õë¶ÔÓ¦ÓóÌÐò£¬ÎÒÃÇͨ³£¹Ø×¢µÄÊÇÄÚºËCPUµ÷ÀíÆ÷¹¦Ð§ºÍÐÔÄÜ¡£

Ï̵߳Ä״̬ÆÊÎöÖ÷ÒªÊÇÆÊÎöÏ̵߳Äʱ¼äÓÃÔÚʲôµØ·½£¬¶øÏß³Ì״̬µÄ·ÖÀàÒ»Ñùƽ³£·ÖΪ£º

on-CPU£ºÖ´ÐÐÖУ¬Ö´ÐÐÖеÄʱ¼äͨ³£ÓÖ·ÖΪÓû§Ì¬Ê±¼äuserºÍϵͳ̬ʱ¼äsys¡£

off-CPU£ºÆÚ´ýÏÂÒ»ÂÖÉÏCPU£¬»òÕßÆÚ´ýI/O¡¢Ëø¡¢»»Ò³µÈµÈ£¬Æä״̬¿ÉÒÔϸ·ÖΪ¿ÉÖ´ÐС¢ÄäÃû»»Ò³¡¢Ë¯Ãß¡¢Ëø¡¢¿ÕÏеÈ״̬¡£

ÈôÊÇ´ó×Úʱ¼ä»¨ÔÚCPUÉÏ£¬¶ÔCPUµÄÆÊÎöÄܹ»Ñ¸ËÙÚ¹ÊÍÔµ¹ÊÔ­ÓÉ£»ÈôÊÇϵͳʱ¼ä´ó×Ú´¦ÓÚoff-cpu״̬£¬¶¨Î»ÎÊÌâ¾Í»á·ÑʱÐí¶à¡£¿ÉÊÇÈÔÈ»ÐèÒªÇåÎúһЩ¿´·¨£º

´¦ÀíÆ÷

ºË

Ó²¼þÏß³Ì

CPUÄڴ滺´æ

ʱÖÓƵÂÊ

ÿָÁîÖÜÆÚÊýCPIºÍÿÖÜÆÚÖ¸ÁîÊýIPC

CPUÖ¸Áî

ʹÓÃÂÊ

Óû§Ê±¼ä£¯ÄÚºËʱ¼ä

µ÷ÀíÆ÷

ÔËÐÐÐÐÁÐ

ÇÀÕ¼

¶àÀú³Ì

¶àÏß³Ì

×Ö³¤

4.2 ÆÊÎö¹¤¾ß

˵Ã÷:

uptime£¬vmstat£¬mpstat£¬top£¬pidstat Ö»ÄÜÅÌÎʵ½cpu¼°¸ºÔصĵÄʹÓÃÇéÐΡ£

perf¿ÉÒÔËæ×ŵ½Àú³ÌÄÚ²¿Ïêϸº¯ÊýºÄʱÇéÐΣ¬²¢ÇÒ¿ÉÒÔÖ¸¶¨Äں˺¯Êý¾ÙÐÐͳ¼Æ£¬Ö¸ÄÄ´òÄÄ¡£

4.3 ʹÓ÷½·¨

//Éó²éϵͳcpuʹÓÃÇéÐÎtop
//Éó²éËùÓÐcpuºËÐÅÏ¢mpstat -P ALL 1
//Éó²écpuʹÓÃÇéÐÎÒÔ¼°Æ½¾ù¸ºÔØvmstat 1
//Àú³ÌcpuµÄͳ¼ÆÐÅÏ¢pidstat -u 1 -p pid
//¸ú×ÙÀú³ÌÄÚ²¿º¯Êý¼¶cpuʹÓÃÇéÐÎ perf top -p pid -e cpu-clock

µÇ¼ºó¸´ÖÆ

5. ÄÚ´æ

5.1 ˵Ã÷

ÄÚ´æÊÇΪÌá¸ßЧÂʶøÉú£¬ÏÖʵÆÊÎöÎÊÌâµÄʱ¼ä£¬ÄÚ´æ·ºÆðÎÊÌâ¿ÉÄܲ»µ«ÊÇÓ°ÏìÐÔÄÜ£¬¶øÊÇÓ°ÏìЧÀÍ»òÕßÒýÆðÆäËûÎÊÌ⡣ͬÑù¹ØÓÚÄÚ´æÓÐЩ¿´·¨ÐèÒªÇåÎú£º

Å£±Æ°¡£¡½Ó˽»î±Ø±¸µÄ N ¸ö¿ªÔ´ÏîÄ¿£¡¸ÏæÕä²Ø

µÇ¼ºó¸´ÖÆ

Ö÷´æ

ÐéÄâÄÚ´æ

³£×¤ÄÚ´æ

µØµã¿Õ¼ä

OOM

Ò³»º´æ

ȱҳ

»»Ò³

½»Á÷¿Õ¼ä

½»Á÷

Óû§·ÖÅÉÆ÷libc¡¢glibc¡¢libmallocºÍmtmalloc

LINUXÄں˼¶SLUB·ÖÅÉÆ÷

5.2 ÆÊÎö¹¤¾ß

˵Ã÷£º

free£¬vmstat£¬top£¬pidstat£¬pmapÖ»ÄÜͳ¼ÆÄÚ´æÐÅÏ¢ÒÔ¼°Àú³ÌµÄÄÚ´æʹÓÃÇéÐΡ£

valgrind ¿ÉÒÔÆÊÎöÄÚ´æ×ß©ÎÊÌâ¡£

dtrace ¶¯Ì¬¸ú×Ù¡£ÐèÒª¶ÔÄں˺¯ÊýÓкÜÉîÈëµÄÏàʶ£¬Í¨¹ýDÓïÑÔ±àд¾ç±¾Íê³É¸ú×Ù¡£

5.3 ʹÓ÷½·¨

//Éó²éϵͳÄÚ´æʹÓÃÇéÐÎfree -m//ÐéÄâÄÚ´æͳ¼ÆÐÅÏ¢vmstat 1//Éó²éϵͳÄÚ´æÇéÐÎtop//1sÊÕÂÞÖÜÆÚ£¬»ñÈ¡ÄÚ´æµÄͳ¼ÆÐÅÏ¢pidstat -p pid -r 1//Éó²éÀú³ÌµÄÄÚ´æÓ³ÏñÐÅÏ¢pmap -d pid//¼ì²â³ÌÐòÄÚ´æÎÊÌâvalgrind --tool=memcheck --leak-check=full --log-file=./log.txt  ./³ÌÐòÃû

µÇ¼ºó¸´ÖÆ

6. ´ÅÅÌIO

6.1 ˵Ã÷

´ÅÅÌͨ³£ÊÇÅÌËã»ú×îÂýµÄ×Óϵͳ£¬Ò²ÊÇ×îÈÝÒ×·ºÆðÐÔÄÜÆ¿¾±µÄµØ·½£¬ÓÉÓÚ´ÅÅÌÀë CPU ¾àÀë×îÔ¶²¢ÇÒ CPU »á¼û´ÅÅÌÒªÉæ¼°µ½»úе²Ù×÷£¬ºÃ±ÈתÖᡢѰ¹ìµÈ¡£»á¼ûÓ²Å̺ͻá¼ûÄÚ´æÖ®¼äµÄËÙÂʲî±ðÊÇÒÔÊýÄ¿¼¶À´ÅÌËãµÄ£¬¾ÍÏñ1ÌìºÍ1·ÖÖӵIJî±ðÒ»Ñù¡£Òª¼à²â IO ÐÔÄÜ£¬ÓÐÐëÒªÏàʶһÏ»ùÀ´Ô´ÀíºÍ Linux ÊÇÈçÄÇÀïÖÃÓ²Å̺ÍÄÚ´æÖ®¼äµÄ IO µÄ¡£

ÔÚÃ÷È·´ÅÅÌIO֮ǰ£¬Í¬ÑùÎÒÃÇÐèÒªÃ÷ȷһЩ¿´·¨£¬ÀýÈ磺

Îļþϵͳ

VFS

Îļþϵͳ»º´æ

Ò³»º´æpage cache

»º³åÇø¸ßËÙ»º´æbuffer cache

Ŀ¼»º´æ

inode

inode»º´æ

noopŲÓÃÕ½ÂÔ

6.2 ÆÊÎö¹¤¾ß

6.3 ʹÓ÷½·¨

//Éó²éϵͳioÐÅÏ¢iotop//ͳ¼ÆioÏêϸÐÅÏ¢iostat -d -x -k 1 10//Éó²éÀú³Ì¼¶ioµÄÐÅÏ¢pidstat -d 1 -p  pid//Éó²éϵͳIOµÄÇëÇ󣬺ñȿÉÒÔÔÚ·¢Ã÷ϵͳIOÒ쳣ʱ£¬¿ÉÒÔʹÓøÃÏÂÁî¾ÙÐÐÊӲ죬¾ÍÄÜÖ¸¶¨¾¿¾¹ÊÇʲôԵ¹ÊÔ­Óɵ¼ÖµÄIOÒì³£perf record -e block:block_rq_issue -ag^Cperf report

µÇ¼ºó¸´ÖÆ

7. ÍøÂç

7.1 ˵Ã÷

ÍøÂçµÄ¼à²âÊÇËùÓÐ Linux ×ÓϵͳÄÚÀï×îÖØ´óµÄ£¬ÓÐÌ«¶àµÄÒòËØÔÚÄÚÀºÃ±È£ºÑÓ³Ù¡¢ÛÕ±Õ¡¢³åÍ»¡¢¶ª°üµÈ£¬¸üÔãµÄÊÇÓë Linux Ö÷»úÏàÁ¬µÄ·ÓÉÆ÷¡¢½»Á÷»ú¡¢ÎÞÏßÐźŶ¼»áÓ°Ïìµ½ÕûÌåÍøÂç²¢ÇÒºÜÄÑÅжÏÊÇÓÉÓÚ Linux ÍøÂç×ÓϵͳµÄÎÊÌâÕվɱðµÄ×°±¸µÄÎÊÌ⣬ÔöÌíÁ˼à²âºÍÅжϵÄÖØƯºó¡£ÏÖÔÚÎÒÃÇʹÓõÄËùÓÐÍø¿¨¶¼³ÆΪ×Ô˳ӦÍø¿¨£¬Òâ˼ÊÇ˵ÄÜƾ֤ÍøÂçÉϵIJî±ðÍøÂç×°±¸µ¼ÖµIJî±ðÍøÂçËÙÂʺÍÊÂÇéģʽ¾ÙÐÐ×Ô¶¯µ÷½â¡£

7.2 ÆÊÎö¹¤¾ß

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

7.3 ʹÓ÷½·¨

//ÏÔʾÍøÂçͳ¼ÆÐÅÏ¢netstat -s//ÏÔʾĿ½ñUDPÅþÁ¬×´Ì¬netstat -nu//ÏÔʾUDP¶Ë¿ÚºÅµÄʹÓÃÇéÐÎnetstat -apu//ͳ¼Æ»úеÖÐÍøÂçÅþÁ¬¸÷¸ö״̬¸öÊýnetstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'//ÏÔʾTCPÅþÁ¬ss -t -a//ÏÔʾsocketsÕªÒªÐÅÏ¢ss -s//ÏÔʾËùÓÐudp socketsss -u -a//tcp,etcp״̬sar -n TCP,ETCP 1//Éó²éÍøÂçIOsar -n DEV 1//×¥°üÒÔ°üΪµ¥Î»¾ÙÐÐÊä³ötcpdump -i eth1 host 192.168.1.1 and port 80 //×¥°üÒÔÁ÷Ϊµ¥Î»ÏÔʾÊý¾ÝÄÚÈÝtcpflow -cp host 192.168.1.1

µÇ¼ºó¸´ÖÆ

8. ϵͳ¸ºÔØ

8.1 ˵Ã÷

Load ¾ÍÊǶÔÅÌËã»ú¸É»î¼¸¶àµÄ»³±§£¨WikiPedia£ºthe system Load is a measure of the amount of work that a compute system is doing£©¼òÆÓµÄ˵ÊÇÀú³ÌÐÐÁеij¤¶È¡£Load Average ¾ÍÊÇÒ»¶Îʱ¼ä£¨1·ÖÖÓ¡¢5·ÖÖÓ¡¢15·ÖÖÓ£©ÄÚƽ¾ùLoad¡£

8.2 ÆÊÎö¹¤¾ß

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

8.3 ʹÓ÷½·¨

//Éó²é¸ºÔØÇéÐÎuptimetopvmstat//ͳ¼ÆϵͳŲÓúÄʱÇéÐÎstrace -c -p pid//¸ú×ÙÖ¸¶¨µÄϵͳ²Ù×÷ÀýÈçepoll_waitstrace -T -e epoll_wait -p pid//Éó²éÄÚºËÈÕÖ¾ÐÅÏ¢dmesg

µÇ¼ºó¸´ÖÆ

9. »ðÑæͼ

9.1 ˵Ã÷

»ðÑæͼ£¨Flame GraphÊÇ Bredan Gregg ½¨ÉèµÄÒ»ÖÖÐÔÄÜÆÊÎöͼ±í£¬ÓÉÓÚËüµÄÑù×Ó½üËÆ ?¶øµÃÃû¡£
»ðÑæͼÖ÷ÒªÊÇÓÃÀ´Õ¹Ê¾ CPUµÄŲÓÃÕ»¡£
y
ÖáÌåÏÖŲÓÃÕ»£¬Ã¿Ò»²ã¶¼ÊÇÒ»¸öº¯Êý¡£Å²ÓÃÕ»Ô½É»ðÑæ¾ÍÔ½¸ß£¬¶¥²¿¾ÍÊÇÕýÔÚÖ´Ðеĺ¯Êý£¬Ï·½¶¼ÊÇËüµÄ¸¸º¯Êý¡£
x
ÖáÌåÏÖ³éÑùÊý£¬ÈôÊÇÒ»¸öº¯ÊýÔÚ x ÖáÕ¼ÓеĿí¶ÈÔ½¿í£¬¾ÍÌåÏÖËü±»³éµ½µÄ´ÎÊý¶à£¬¼´Ö´ÐеÄʱ¼ä³¤¡£×¢ÖØ£¬x Öá²»´ú±íʱ¼ä£¬¶øÊÇËùÓеÄŲÓÃÕ»ºÏ²¢ºó£¬°´×Öĸ˳ÐòÅÅÁеÄ¡£
»ðÑæͼ¾ÍÊÇ¿´¶¥²ãµÄÄĸöº¯ÊýÕ¼ÓеĿí¶È×î´ó¡£Ö»ÒªÓС±Æ½¶¥¡±£¨plateaus£©£¬¾ÍÌåÏָú¯Êý¿ÉÄܱ£´æÐÔÄÜÎÊÌâ¡£ÑÕɫûÓÐÌØÊâ¼ÄÒ壬ÓÉÓÚ»ðÑæͼÌåÏÖµÄÊÇ CPU µÄæµˮƽ£¬ÒÔÊÇÒ»Ñùƽ³£Ñ¡Ôñůɫµ÷¡£

³£¼ûµÄ»ðÑæͼÀàÐÍÓÐ On-CPU¡¢Off-CPU¡¢Memory¡¢Hot/Cold¡¢DifferentialµÈµÈ¡£

9.2 ×°ÖÃÒÀÀµ¿â

//×°ÖÃsystemtap£¬Ä¬ÈÏϵͳÒÑ×°ÖÃyum install systemtap systemtap-runtime//Äں˵÷ÊÔ¿â±ØÐè¸úÄں˰汾¶ÔÓ¦£¬ÀýÈ磺uname -r 2.6.18-308.el5kernel-debuginfo-2.6.18-308.el5.x86_64.rpmkernel-devel-2.6.18-308.el5.x86_64.rpmkernel-debuginfo-common-2.6.18-308.el5.x86_64.rpm//×°ÖÃÄں˵÷ÊÔ¿âdebuginfo-install --enablerepo=debuginfo search kerneldebuginfo-install --enablerepo=debuginfo  search glibc

µÇ¼ºó¸´ÖÆ

9.3 ×°ÖÃ

git clone https://github.com/lidaohang/quick_location.gitcd quick_location

µÇ¼ºó¸´ÖÆ

9.4 CPU¼¶±ð»ðÑæͼ

cpuÕ¼Óùý¸ß£¬»òÕßʹÓÃÂÊÌá²»ÉÏÀ´£¬ÄãÄÜ¿ìËÙ¶¨Î»µ½´úÂëµÄÄÄ¿éÓÐÎÊÌâÂ𠣿

Ò»Ñùƽ³£µÄ×ö·¨¿ÉÄܾÍÊÇͨ¹ýÈÕÖ¾µÈ·½·¨È¥È·¶¨ÎÊÌâ¡£ÏÖÔÚÎÒÃÇÓÐÁË»ðÑæͼ£¬Äܹ»ºÜÊÇÇåÎúµÄ·¢Ã÷Äĸöº¯ÊýÕ¼ÓÃcpu¹ý¸ß£¬»òÕß¹ýµÍµ¼ÖµÄÎÊÌâ¡£ÁíÍ⣬ËÑË÷ÃñÖÚºÅLinux¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°ºï×Ó¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£

9.4.1 on-CPU

cpuÕ¼Óùý¸ß£¬Ö´ÐÐÖеÄʱ¼äͨ³£ÓÖ·ÖΪÓû§Ì¬Ê±¼äuserºÍϵͳ̬ʱ¼äsys¡£
ʹÓ÷½·¨£º

//on-CPU usersh ngx_on_cpu_u.sh pid//½øÈëЧ¹ûĿ¼ cd ngx_on_cpu_u//on-CPU kernelsh ngx_on_cpu_k.sh pid//½øÈëЧ¹ûĿ¼ cd ngx_on_cpu_k//¿ªÒ»¸öÔÝʱ¶Ë¿Ú 8088 python -m SimpleHTTPServer 8088//·­¿ªä¯ÀÀÆ÷ÊäÈëµØµã127.0.0.1:8088/pid.svg

µÇ¼ºó¸´ÖÆ

DEMO£º

µÇ¼ºó¸´ÖÆ

#include <stdio.h>#include <stdlib.h>
void foo3(){  }
void foo2(){    int i;    for(i=0 ; i < 10; i++)           foo3();}
void foo1(){    int i;  for(i = 0; i< 1000; i++)      foo3();}
int main(void){    int i;    for( i =0; i< 1000000000; i++) {          foo1();          foo2();    }}

µÇ¼ºó¸´ÖÆ

DEMO»ðÑæͼ£º

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

9.4.2 off-CPU

cpu¹ýµÍ£¬Ê¹ÓÃÂʲ»¸ß¡£ÆÚ´ýÏÂÒ»ÂÖCPU£¬»òÕßÆÚ´ýI/O¡¢Ëø¡¢»»Ò³µÈµÈ£¬Æä״̬¿ÉÒÔϸ·ÖΪ¿ÉÖ´ÐС¢ÄäÃû»»Ò³¡¢Ë¯Ãß¡¢Ëø¡¢¿ÕÏеÈ״̬¡£

ʹÓ÷½·¨£º

// off-CPU usersh ngx_off_cpu_u.sh pid//½øÈëЧ¹ûĿ¼cd ngx_off_cpu_u//off-CPU kernelsh ngx_off_cpu_k.sh pid//½øÈëЧ¹ûĿ¼cd ngx_off_cpu_k//¿ªÒ»¸öÔÝʱ¶Ë¿Ú8088python -m SimpleHTTPServer 8088//·­¿ªä¯ÀÀÆ÷ÊäÈëµØµã127.0.0.1:8088/pid.svg

µÇ¼ºó¸´ÖÆ

¹ÙÍøDEMO£º

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

9.5 Äڴ漶±ð»ðÑæͼ

ÈôÊÇÏßÉϳÌÐò·ºÆðÁËÄÚ´æ×ß©£¬²¢ÇÒÖ»ÔÚÌض¨µÄ³¡¾°²Å»á·ºÆð¡£Õâ¸öʱ¼äÎÒÃÇÔõô°ìÄØ £¿ÓÐʲôºÃµÄ·½·¨ºÍ¹¤¾ßÄÜ¿ìËٵķ¢Ã÷´úÂëµÄÎÊÌâÄØ £¿Í¬ÑùÄڴ漶±ð»ðÑæͼ°ïÄã¿ìËÙÆÊÎöÎÊÌâµÄȪԴ¡£

ʹÓ÷½·¨£º

sh ngx_on_memory.sh pid//½øÈëЧ¹ûĿ¼cd ngx_on_memory//¿ªÒ»¸öÔÝʱ¶Ë¿Ú8088python -m SimpleHTTPServer 8088//·­¿ªä¯ÀÀÆ÷ÊäÈëµØµã127.0.0.1:8088/pid.svg

µÇ¼ºó¸´ÖÆ

¹ÙÍøDEMO£º

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

9.6 ÐÔÄÜ»ØÍË-ºìÀ¶²î·Ö»ðÑæͼ

ÄãÄÜ¿ìËÙ¶¨Î»CPUÐÔÄÜ»ØÍ˵ÄÎÊÌâô £¿ÈôÊÇÄãµÄÊÂÇéÇéÐκÜÊÇÖØ´óÇÒת±ä¿ìËÙ£¬ÄÇôʹÓÃÏÖÓеŤ¾ßÊÇÀ´¶¨Î»ÕâÀàÎÊÌâÊǺܾßÓÐÌôÕ½ÐԵġ£µ±Ä㻨µôÊýÖÜʱ¼ä°Ñ¸ùÒòÕÒµ½Ê±£¬´úÂëÒѾ­Óֱ任Á˺ü¸ÂÖ£¬ÐµÄÐÔÄÜÎÊÌâÓÖðÁ˳öÀ´¡£Ö÷Òª¿ÉÒÔÓõ½Ã¿´Î¹¹½¨ÖУ¬Ã¿´ÎÉÏÏß×ö±ÈÕÕ¿´£¬ÈôÊÇËðʧÑÏÖØ¿ÉÒÔÁ¢Âí½â¾öÐÞ¸´¡£

ͨ¹ýץȡÁËÁ½ÕÅͨË׵ĻðÑæͼ£¬È»ºó¾ÙÐбÈÕÕ£¬²¢¶Ô²î±ð²¿·Ö¾ÙÐбêÉ«£ººìÉ«ÌåÏÖÉÏÉý£¬À¶É«ÌåÏÖϽµ¡£²î·Ö»ðÑæͼÊÇÒÔÄ¿½ñ£¨¡°Ð޸ĺ󡱣©µÄprofileÎļþ×÷Ϊ»ù×¼£¬ÐÎ×´ºÍ¾Þϸ¶¼¼á³ÖÎȹÌ¡£Òò´ËÄãͨ¹ýÉ«²ÊµÄ²î±ð¾ÍÄܹ»ºÜÖ±¹ÛµÄÕÒµ½²î±ð²¿·Ö£¬ÇÒ¿ÉÒÔ¿´³öΪʲô»áÓÐÕâÑùµÄ²î±ð¡£

ʹÓ÷½·¨£º

cd quick_location//ץȡ´úÂëÐÞ¸ÄÇ°µÄprofile 1Îļþperf record -F 99 -p pid -g -- sleep 30perf script > out.stacks1//ץȡ´úÂëÐ޸ĺóµÄprofile 2Îļþperf record -F 99 -p pid -g -- sleep 30perf script > out.stacks2//ÌìÉú²î·Ö»ðÑæͼ:./FlameGraph/stackcollapse-perf.pl ../out.stacks1 > out.folded1./FlameGraph/stackcollapse-perf.pl ../out.stacks2 > out.folded2./FlameGraph/difffolded.pl out.folded1 out.folded2 | ./FlameGraph/flamegraph.pl > diff2.svg

µÇ¼ºó¸´ÖÆ

DEMO£º

//test.c#include <stdio.h>#include <stdlib.h>
void foo3(){  }
void foo2(){    int i;    for(i=0 ; i < 10; i++)      foo3();}
void foo1(){    int i;    for(i = 0; i< 1000; i++)       foo3();}
int main(void){    int i;  for( i =0; i< 1000000000; i++) {      foo1();      foo2();    }}
//test1.c#include <stdio.h>#include <stdlib.h>
void foo3(){
}
void foo2(){  int i;  for(i=0 ; i < 10; i++)         foo3();}
void foo1(){    int i;    for(i = 0; i< 1000; i++)         foo3();}
void add(){    int i;    for(i = 0; i< 10000; i++)       foo3();}
int main(void){    int i;    for( i =0; i< 1000000000; i++) {    foo1();    foo2();    add();  }}

µÇ¼ºó¸´ÖÆ

DEMOºìÀ¶²î·Ö»ðÑæͼ£º

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

10. °¸ÀýÆÊÎö

10.1 ½ÓÈë²ãnginx¼¯ÈºÒì³£Õ÷Ïó

ͨ¹ý¼à¿Ø²å¼þ·¢Ã÷ÔÚ 2017.09.25 19 µãnginx¼¯ÈºÇëÇóÁ÷Á¿·ºÆð´ó×ÚµÄ499£¬5xx״̬Âë¡£²¢ÇÒ·¢Ã÷»úеcpuʹÓÃÂÊÉý¸ß£¬ÏÖÔÚÒ»Ö±Ò»Á¬ÖС£ÁíÍ⣬ËÑË÷ÃñÖںŶ¥¼¶Ëã·¨ºǫ́»Ø¸´¡°Ëã·¨¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£

10.2 ÆÊÎönginxÏà¹ØÖ¸±ê

a) **ÆÊÎönginxÇëÇóÁ÷Á¿£º

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

½áÂÛ£º

ͨ¹ýÉÏͼ·¢Ã÷Á÷Á¿²¢Ã»ÓÐÍ»Ôö£¬·´¶øϽµÁË£¬¸úÇëÇóÁ÷Á¿Í»Ôöû¹Øϵ¡£

b) **ÆÊÎönginxÏìӦʱ¼ä

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

½áÂÛ£º

ͨ¹ýÉÏͼ·¢Ã÷nginxµÄÏìӦʱ¼äÓÐÔöÌí¿ÉÄܸúnginx×ÔÉíÓйØϵ»òÕ߸úºó¶ËupstreamÏìӦʱ¼äÓйØϵ¡£

c) **ÆÊÎönginx upstreamÏìӦʱ¼ä

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

½áÂÛ£º

ͨ¹ýÉÏͼ·¢Ã÷nginx upstream ÏìӦʱ¼äÓÐÔöÌí£¬ÏÖÔÚÍƲâ¿ÉÄܺó¶ËupstreamÏìӦʱ¼äÍÏסnginx£¬µ¼ÖÂnginx·ºÆðÇëÇóÁ÷Á¿Òì³£¡£

10.3 ÆÊÎöϵͳcpuÇéÐÎ

a) **ͨ¹ýtopÊÓ²ìϵͳָ±ê

top

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

½áÂÛ£º

·¢Ã÷nginx worker cpu½ÏÁ¿¸ß

b) **ÆÊÎönginxÀú³ÌÄÚ²¿cpuÇéÐÎ

perf top -p pid

½áÂÛ£º

·¢Ã÷Ö÷Òª¿ªÏúÔÚfree,malloc,jsonÆÊÎöÉÏÃæ

10.4 »ðÑæͼÆÊÎöcpu

a) **ÌìÉúÓû§Ì¬cpu»ðÑæͼ

//on-CPU usersh ngx_on_cpu_u.sh pid//½øÈëЧ¹ûĿ¼cd ngx_on_cpu_u//¿ªÒ»¸öÔÝʱ¶Ë¿Ú8088python -m SimpleHTTPServer 8088//·­¿ªä¯ÀÀÆ÷ÊäÈëµØµã127.0.0.1:8088/pid.svg

µÇ¼ºó¸´ÖÆ

Linux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~

½áÂÛ£º

·¢Ã÷´úÂëÄÚÀïÓÐƵÈÔµÄÆÊÎöjson²Ù×÷£¬²¢ÇÒ·¢Ã÷Õâ¸öjson¿âÐÔÄܲ»¸ß£¬Õ¼ÓÃcpuͦ¸ß¡£

10.5 °¸Àý×ܽá

a) ÆÊÎöÇëÇóÁ÷Á¿Òì³££¬µÃ³önginx upstreamºó¶Ë»úеÏìӦʱ¼äÀ­³¤

b) ÆÊÎönginxÀú³Ìcpu¸ß£¬µÃ³önginxÄÚ²¿Ä £¿é´úÂëÓкÄʱµÄjsonÆÊÎöÒÔ¼°ÄÚ´æ·ÖÅɽÓÄɲÙ×÷

10.5.1 ÉîÈëÆÊÎö

ƾ֤ÒÔÉÏÁ½µãÎÊÌâÆÊÎöµÄ½áÂÛ£¬ÎÒÃǽøÒ»²½ÉîÈëÆÊÎö¡£

ºó¶ËupstreamÏìÓ¦À­³¤£¬×î¶à¿ÉÄÜÓ°ÏìnginxµÄ´¦ÀíÄÜÁ¦¡£¿ÉÊDz»¿ÉÄÜ»áÓ°ÏìnginxÄÚ²¿Ä £¿éÕ¼Óùý¶àµÄcpu²Ù×÷¡£²¢ÇÒÆäʱռÓÃcpu¸ßµÄÄ £¿é£¬ÊÇÔÚÇëÇóµÄʱ¼ä²Å»á×ßµÄÂß¼­¡£²»Ì«¿ÉÄÜÊÇupstramºó¶ËÍÏסnginx£¬´Ó¶ø´¥·¢Õâ¸öcpuµÄºÄʱ²Ù×÷¡£

10.5.2 ½â¾ö·½·¨

Óöµ½ÕâÖÖÎÊÌ⣬ÎÒÃÇÓÅÏȽâ¾öÒÑÖªµÄ£¬²¢ÇÒºÜÊÇÃ÷È·µÄÎÊÌâ¡£ÄǾÍÊÇcpu¸ßµÄÎÊÌâ¡£½â¾ö·½·¨ÏȽµ¼¶¹Ø±ÕÕ¼ÓÃcpu¹ý¸ßµÄÄ £¿é£¬È»ºó¾ÙÐÐÊӲ졣¾­Óɽµ¼¶¹Ø±Õ¸ÃÄ £¿écpu½µÏÂÀ´ÁË£¬²¢ÇÒnginxÇëÇóÁ÷Á¿Ò²Õý³£ÁË¡£Ö®ÒÔÊÇ»áÓ°Ïìupstreamʱ¼äÀ­³¤£¬ÓÉÓÚupstreamºó¶ËµÄЧÀÍŲÓõĽӿڿÉÄÜÊǸö»·Â·ÔÙ´Î×߻ص½nginx¡£

11.²Î¿¼×ÊÁÏ

http://www.brendangregg.com/index.html

http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html

http://www.brendangregg.com/FlameGraphs/memoryflamegraphs.html

http://www.brendangregg.com/FlameGraphs/offcpuflamegraphs.html

http://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html

https://github.com/openresty/openresty-systemtap-toolkit

https://github.com/brendangregg/FlameGraph

https://www.slideshare.net/brendangregg/blazing-performance-with-flame-graphs

ÒÔÉϾÍÊÇLinux ÔËά¹ÊÕÏÅŲé˼Ð÷£¬ÓÐÕâƪÎÄÕ¾͹»ÁË~µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ