使用gdb和ps定位程序cpu占用问题

先使用ps获取子进程cpu占用时间,可以看出那个线程cpu占用严重
ps -m -o tid,cpu,time -p 6321
TID CPU     TIME
–   – 00:30:50
6321   – 00:00:00
6322   – 00:30:50
6323   – 00:00:00
6324   – 00:00:00
6325   – 00:00:00
6326   – 00:00:00
6327   – 00:00:00
6328   – 00:00:00

 在用gdb attach到进程上去看这个具体的线程信息
gdb -p  6321
 info thread
8 Thread 0x7fbfd14cf700 (LWP 6322)  0x00007fbfd221303d in waitpid () from /lib64/libpthread.so.0
7 Thread 0x7fbfd0cce700 (LWP 6323)  0x00007fbfd220f75b in pthread_cond_timedwait@@GLIBC_2.3.2 ()
from /lib64/libpthread.so.0
6 Thread 0x7fbfcbfff700 (LWP 6324)  0x00007fbfd15b62c3 in epoll_wait () from /lib64/libc.so.6
5 Thread 0x7fbfd2a45700 (LWP 6325)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
4 Thread 0x7fbfd04cd700 (LWP 6326)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x7fbfd03cc700 (LWP 6327)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
2 Thread 0x7fbfd02cb700 (LWP 6328)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
* 1 Thread 0x7fbfd2a47720 (LWP 6321)  0x00007fbfd15b62c3 in epoll_wait () from /lib64/libc.so.6
(gdb) thread 8
[Switching to thread 8 (Thread 0x7fbfd14cf700 (LWP 6322))]#0  0x00007fbfd221303d in waitpid ()
from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007fbfd221303d in waitpid () from /lib64/libpthread.so.0
#1  0x00000000005ac003 in idata::factory::processor::task_executor::CTaskMonitor::signal_thread (this=0x882eb1)
at ./task_monitor.cpp:46
经过查看是一个死循环对waitpid进行不断调用,致使cpu奇高。
程序中是由一个线程去wait子进程的结束,先是使用sigwait去等待子进程结束,等有子进程结束的信号发生后,在使用waitpid获取子进程的id和返回码,进行记录。在使用waitpid的时候做了一个循环把所有子进程的信息都取出来。这里这个循环没有再取没有数据之后跳出导致了重复的获取。

Leave a Reply

Your email address will not be published. Required fields are marked *