基本思路
- top: 查看到CPU占比高的进程号pid;
- top -H -p $pid: 查看进程内各个线程占用的CPU百分比,找到CPU占比高的线程ID;
- gstack $pid > gstack.log: 找到线程ID对应线程号;
- gcore $pid: 使用gcore命令转存进程映像及内存上下文,生成core文件;
- strace -T -r -c -p $pid 或 strace -o output.txt -T -tt -e trace=all -p $pid:用strace命令查看系统调用和花费的时间;
- gdb: 然后用gdb调试对应的线程;
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <string.h> void *func1(void *arg) { int n = 0; while(1) { n++; usleep(100); } } void *func2(void *arg) { char hello[] = "Hello world"; while(1) { int n = 1235 * 356 * 987; char *pstr = (char*)malloc(1024); memcpy(pstr, hello, strlen(hello)); free(pstr); } } int main() { pthread_t thread_1; pthread_create(&thread_1, NULL, func1, NULL); pthread_t thread_2; pthread_create(&thread_2, NULL, func2, NULL); pthread_join(thread_1, NULL); pthread_join(thread_1, NULL); return 0; } |