用sysbench测试cpu性能,然后利用perf 找到具体哪个进程的哪个函数在消耗CPU
gjw@gjw:/etc/apt$ sysbench --threads=1 --time=600 cpu run
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Prime numbers limit: 10000
Initializing worker threads...
Threads started!
查看sysbench进程
ps -egjw@gjw:~$ ps -ef | grep sysbench
gjw 31921 1294 77 11:20 pts/0 00:04:39 sysbench --threads=1 --time=600 cpu run
perf top获取进程信息:
sudo perf top -g -p 31921
Samples: 824 of event 'cycles:ppp', Event count (approx.): 618842235
Children Self Shared Object Symbol
+ 100.00% 100.00% sysbench [.] cpu_execute_event
然后在sysbench源码中查看cpu_execute_event 函数:
gjw@gjw:~/soft/sysbench-master$ grep -r cpu_execute_event
Binary file src/tests/cpu/libsbcpu.a matches
Binary file src/tests/cpu/libsbcpu_a-sb_cpu.o matches
src/tests/cpu/sb_cpu.c:static int cpu_execute_event(sb_event_t *, int);
src/tests/cpu/sb_cpu.c: .execute_event = cpu_execute_event,
src/tests/cpu/sb_cpu.c:int cpu_execute_event(sb_event_t *r, int thread_id)
Binary file src/sysbench matches
gjw@gjw:~/soft/sysbench-master$ vi src/tests/cpu/sb_cpu.c
int cpu_execute_event(sb_event_t *r, int thread_id)
{
unsigned long long c;
unsigned long long l;
double t;
unsigned long long n=0;
(void)thread_id; /* unused */
(void)r; /* unused */
/* So far we're using very simple test prime number tests in 64bit */
for(c=3; c < max_prime; c++)
{
t = sqrt((double)c);
for(l = 2; l <= t; l++)
if (c % l == 0)
break;
if (l > t )
n++;
}
return 0;
可以看到该函数在计算素数,由此完成了一个cpu使用率很高的案例分析
展开