同事的多线程代码中,存在100%CPU占用的bug,未知问题所在。
其实只要知道几个命令,定位这个问题很简单。
这里记录一下调试过程。
1. 用ps 查看哪个线程消耗了CPU。
$ ps -m -o tid,cpu,time -p 4172
TID CPU TIME
- - 00:01:22
4172 - 00:00:17
4183 - 00:00:00
4184 - 00:00:00
4185 - 00:00:00
4186 - 00:00:00
4187 - 00:00:00
4188 - 00:00:01
4189 - 00:00:00
4190 - 00:00:59
4192 - 00:00:00
4194 - 00:00:00
4195 - 00:00:02
4196 - 00:00:00
4197 - 00:00:00
4198 - 00:00:00
4227 - 00:00:00
4547 - 00:00:00
2. gdb 附到该进程
$ gdb a.out 4172
......
(gdb) thread 4190
(gdb) set scheduler-locking on
(gdb) n ....
3. 分析代码
在这个例子中,bug产生的原因是select的不当使用
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
for ( ... )
{
select(0, 0, 0, 0, &tv);
......
}
只需要把赋值tv的两个语句移到调用select语句之前即可。
原因看man select:
select() may update the timeout argument to indicate how much time was left
http://www.douban.com/note/61721358/
ps+gdb查找100%CPU bug
最新推荐文章于 2024-07-28 21:49:04 发布