我正在尝试使用 gprof 分析我的 C++ 应用程序,我想计算函数执行时间。然而,如果某些函数运行速度超过 0.01 秒,gprof 上会报告值为 0.00。如何将 gprof 的报告时间更改为纳秒?
谢谢您的建议。
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 3 0.00 0.00 access_counter
0.00 0.00 0.00 2 0.00 0.00 get_counter
0.00 0.00 0.00 1 0.00 0.00 start_counter
Copyright (C) 2012-2015 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
.....
Call graph (explanation follows)
granularity: each sample hit covers 2 byte(s) no time propagated
index % time self children called name
0.00 0.00 1/3 start_counter [3]
0.00 0.00 2/3 get_counter [2]
[1] 0.0 0.00 0.00 3 access_counter [1]
-----------------------------------------------
0.00 0.00 2/2 main [9]
[2] 0.0 0.00 0.00 2 get_counter [2]
0.00 0.00 2/3 access_counter [1]
-----------------------------------------------
0.00 0.00 1/1 main [9]
[3] 0.0 0.00 0.00 1 start_counter [3]
0.00 0.00 1/3 access_counter [1]
-----------------------------------------------
据我了解,这取决于记录分析数据的频率。分析过程由 glibc [
1] 中的
__moncontrol
函数控制,其中调用 __profil
来启动计时器 [2]。这里我们真正关心的是计时器的间隔,它由决定
timer.it_value.tv_usec = 1000000 / __profile_frequency ();
其中
__profile_frequency
将尝试尽可能获得最小的粒度。所以总而言之,它取决于机器。此外,gprof 手册指出它通常约为 10 毫秒,因此我认为我们不太可能追踪到纳秒。
通常,程序计数器在运行时每秒被查看大约 100 次,但确切的频率可能因系统而异。