程序调试之CPU 占用率高

39 篇文章 0 订阅
9 篇文章 0 订阅

cpu: ls2k
基本思路:找到占用cpu 使用率最高的线程,之后查看此线程执行的函数栈,找到相应的函数。
查看线程的cpu占用率可以用top , top -Hp 或者是 ps 命令。之后用gdb attach,查看线程信息,根据线程号即tid找到相应的线程并切换到此线程,之后查看此时的函数栈。

详细过程如下:

通过pstop 找到线程号

通过`ps` 命令打印出pid, tid, cpu 占用率,cmd 等信息
[sancog@localhost build-qt-client-unknown-Debug]$ ps H -e -o pid,tid,pcpu,cmd --sort=pcpu | grep qt-client
12549 12554  0.0 /home/sancog/build-qt-client-unknown-Debug/qt-client
12555 12557  0.0 ./qt-client
12555 12559  0.0 ./qt-client
12555 12560  0.0 ./qt-client
12555 12565  0.0 ./qt-client
12555 12566  0.0 ./qt-client
12555 12567  0.0 ./qt-client
12555 12568  0.0 ./qt-client
12580 12580  0.0 grep --color=auto qt-client
12555 12556  0.1 ./qt-client
12549 12549  0.9 /home/sancog/build-qt-client-unknown-Debug/qt-client
12555 12555 74.9 ./qt-client

通过上面可知进程号为 12555
[sancog@localhost build-qt-client-unknown-Debug]$ awk '{print $1, $2, $14, $15}' /proc/12555/task/*/stat
12555 (qt-client) 20613 106
12556 (QXcbEventReader) 9 20
12557 (QDBusConnection) 3 0
12559 (gdbus) 1 0
12560 (gmain) 0 0
12565 (qt-client) 3 3
12566 (qt-client) 3 3
12567 (qt-client) 3 3
12568 (qt-client) 2 4

上面通过ps 命令找到到cpu占用率最高的线程tid12555

$ top
在这里插入图片描述
这里先通过top命令找到进程号12555

$ top -Hp 12555
在这里插入图片描述
根据进程号12555找到里面cpu占用率最高的线程号tid12555

## 启动GDB

$ gdb
(gdb) attach 12555		//这里的12555 即为上面找到的进程号
Attaching to process 12555
Reading symbols from /home/sancog/build-qt-client-unknown-Debug/qt-client...done.
Reading symbols from /usr/lib64/libavutil.so.54...Reading symbols from /usr/lib64/libavutil.so.54...(no debugging symbols found)...done.
(no debugging symbols found)...done.

(gdb) info threads
  Id   Target Id         Frame 
  9    Thread 0xffef5aaf90 (LWP 12556) "QXcbEventReader" 0x000000fff4c751cc in poll () from /usr/lib64/libc.so.6
  8    Thread 0xffedfbef90 (LWP 12557) "QDBusConnection" 0x000000fff4c751cc in poll () from /usr/lib64/libc.so.6
  7    Thread 0xffec9c2f90 (LWP 12559) "gdbus" 0x000000fff4c751cc in poll ()
   from /usr/lib64/libc.so.6
  6    Thread 0xffdfffef90 (LWP 12560) "gmain" 0x000000fff4c751cc in poll ()
   from /usr/lib64/libc.so.6
  5    Thread 0xffdeffef90 (LWP 12565) "qt-client" 0x000000fff4c751cc in poll
    () from /usr/lib64/libc.so.6
  4    Thread 0xffdf7fef90 (LWP 12566) "qt-client" 0x000000fff4c751cc in poll
    () from /usr/lib64/libc.so.6
  3    Thread 0xffdcfdef90 (LWP 12567) "qt-client" 0x000000fff4c751cc in poll
    () from /usr/lib64/libc.so.6
  2    Thread 0xffcfffef90 (LWP 12568) "qt-client" 0x000000fff4c751cc in poll
    () from /usr/lib64/libc.so.6
* 1    Thread 0xfff04c8000 (LWP 12555) "qt-client" 0x000000fff5b6aa10 in QPainter::drawLines(QLine const*, int) () from /usr/lib64/libQt5Gui.so.5			//这里的12555 为上面查到的线程号
(gdb) thread 1					//切换到12555 线程
[Switching to thread 1 (Thread 0xfff04c8000 (LWP 12555))]
#0  0x000000fff5b6aa10 in QPainter::drawLines(QLine const*, int) ()
   from /usr/lib64/libQt5Gui.so.5
(gdb) bt						//查看函数栈
#0  0x000000fff5b6aa10 in QPainter::drawLines(QLine const*, int) ()
    at /usr/lib64/libQt5Gui.so.5
#1  0x000000012005fd60 in QPainter::drawLine(int, int, int, int) (this=0x12bf059d0, x1=67, y1=0, x2=72, y2=0) at /usr/include/qt5/QtGui/qpainter.h:502
#2  0x000000012005ea38 in MeterTable::draw_scaleLine(QPainter*) (this=
    0x12ae69530, p=0x12bf059d0) at ../qt-client/metertable.cpp:223
#3  0x000000012005dbc4 in MeterTable::paintEvent(QPaintEvent*) (this=0x12ae69530) at ../qt-client/metertable.cpp:139
#4  0x000000fff5f37bc0 in QWidget::event(QEvent*) ()
    at /usr/lib64/libQt5Widgets.so.5
(gdb) 

通过上面的分析,我们可知这个程序的瓶颈在图像重绘部分。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值