valgrind,分析计时器已过期?
我尝试使用 valgrind 分析一个简单的 c prog:
[zsun@nel6005001 ~]$ valgrind --tool=memcheck ./fl.out
==2238== Memcheck,内存错误检测器
==2238== 版权所有 (C) 2002-2009,GNU GPL,作者:Julian Seward 等人。
==2238== 使用 Valgrind-3.5.0 和 LibVEX;使用 -h 重新运行以获取版权信息
==2238== 命令:./fl.out
==2238==
==2238==
==2238== 堆摘要:
==2238== 退出时使用:1,168 字节,1 个块
==2238== 总堆使用量:1 次分配,0 次释放,已分配 1,168 字节
==2238==
==2238== 泄漏摘要:
==2238==肯定丢失了:0个块中的0个字节
==2238==间接丢失:0个块中的0个字节
==2238==可能丢失:0个块中的0个字节
==2238== 仍然可达:1,168 字节,1 个块
==2238== 被抑制:0 个块中有 0 个字节
==2238== 使用 --leak-check=full 重新运行以查看泄漏内存的详细信息
==2238==
==2238== 对于检测到和抑制的错误的计数,请使用以下命令重新运行:-v
==2238== 错误摘要:0 个上下文中的 0 个错误(已抑制:8 个中的 12 个)
分析计时器已过期
我尝试分析的 c 代码如下:
void forloop(void){
int fac=1;
int count=5;
int i,k;
for (i = 1; i <= count; i++){
for(k=1;k<=count;k++){
fac = fac * i;
}
}
}
显示“分析计时器已过期”,这是什么意思?如何解决这个问题呢?谢谢!
I try to profile a simple c prog using valgrind:
[zsun@nel6005001 ~]$ valgrind --tool=memcheck ./fl.out
==2238== Memcheck, a memory error detector
==2238== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2238== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==2238== Command: ./fl.out
==2238==
==2238==
==2238== HEAP SUMMARY:
==2238== in use at exit: 1,168 bytes in 1 blocks
==2238== total heap usage: 1 allocs, 0 frees, 1,168 bytes allocated
==2238==
==2238== LEAK SUMMARY:
==2238== definitely lost: 0 bytes in 0 blocks
==2238== indirectly lost: 0 bytes in 0 blocks
==2238== possibly lost: 0 bytes in 0 blocks
==2238== still reachable: 1,168 bytes in 1 blocks
==2238== suppressed: 0 bytes in 0 blocks
==2238== Rerun with --leak-check=full to see details of leaked memory
==2238==
==2238== For counts of detected and suppressed errors, rerun with: -v
==2238== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
Profiling timer expired
The c code I am trying to profile is the following:
void forloop(void){
int fac=1;
int count=5;
int i,k;
for (i = 1; i <= count; i++){
for(k=1;k<=count;k++){
fac = fac * i;
}
}
}
"Profiling timer expired" shows up, what does it mean? How to solve this problem? thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您正在使用
-pg
编译的程序上使用 valgrind。您不能同时使用 valgrind 和 gprof。如果您使用的是 Linux 并且需要在 valgrind 下分析程序的实际模拟,则 valgrind 手册建议使用 OProfile。The problem is that you are using valgrind on a program compiled with
-pg
. You cannot use valgrind and gprof together. The valgrind manual suggests using OProfile if you are on Linux and need to profile the actual emulation of the program under valgrind.顺便说一句,这不是计算阶乘。
如果您真的想找出时间都花在哪里,您可以尝试 堆栈截图。我在你的代码周围放置了一个无限循环,并获取了其中的 10 个。这是代码:
这是堆栈截图,重新排序后最常见的位于顶部:
这告诉您什么?它说第 12 行消耗了大约 40% 的时间,第 13 行消耗了大约 20% 的时间。它还告诉您第 23 行消耗了几乎 100% 的时间。
这意味着在第 12 行展开循环可能会带来大约 100/(100-40) = 100/60 = 1.67x 的加速系数。当然,还有其他方法可以加快此代码的速度,例如如果您确实想计算阶乘,则可以消除内部循环。
我只是指出这一点,因为这是一种非常简单的分析方法。
By the way, this isn't computing factorial.
If you're really trying to find out where the time goes, you could try stackshots. I put an infinite loop around your code and took 10 of them. Here's the code:
And here are the stackshots, re-ordered with the most frequent at the top:
What does this tell you? It says that line 12 consumes about 40% of the time, and line 13 consumes about 20% of the time. It also tells you that line 23 consumes nearly 100% of the time.
That means unrolling the loop at line 12 might potentially give you a speedup factor of 100/(100-40) = 100/60 = 1.67x approximately. Of course there are other ways to speed up this code as well, such as by eliminating the inner loop, if you're really trying to compute factorial.
I'm just pointing this out because it's a bone-simple way to do profiling.
您将无法像这样计算
10000!
。您将需要某种bignum
实现来计算阶乘。这是因为int
“通常”是 4 个字节长,这意味着“通常”它可以容纳2^32 - 1
(signed int,2^31< /code>) -
13!
不仅如此。即使您使用了unsigned long
(“通常”8 个字节),当您达到21!
时,您也会溢出。至于“分析计时器过期”的含义 - 这意味着 valgrind 收到了信号
SIGPROF
:http://en.wikipedia.org/wiki/SIGPROF(可能意味着您的程序花费了太长时间)。You are not going to be able to compute
10000!
like that. You will need some sort ofbignum
implementation for computing factorials. This is becauseint
is "usually" 4 bytes long which means that "usually" it can hold2^32 - 1
(signed int,2^31
) -13!
is more than that. Even if you used anunsigned long
("usually" 8 bytes) you'd overflow by the time you reached21!
.As for what it "profiling timer expired" means - it means valgrind received the signal
SIGPROF
: http://en.wikipedia.org/wiki/SIGPROF (probably means your program took too long).