cpu: ls2k
基本思路:找到占用cpu
使用率最高的线程,之后查看此线程执行的函数栈,找到相应的函数。
查看线程的cpu
占用率可以用top , top -Hp
或者是 ps
命令。之后用gdb attach
,查看线程信息,根据线程号即tid
找到相应的线程并切换到此线程,之后查看此时的函数栈。
详细过程如下:
通过ps
或 top
找到线程号
通过`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
占用率最高的线程tid
为12555
$ top
这里先通过top
命令找到进程号12555
$ top -Hp 12555
根据进程号12555
找到里面cpu
占用率最高的线程号tid
为12555
## 启动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)
通过上面的分析,我们可知这个程序的瓶颈在图像重绘部分。