简体   繁体   English

在执行过程中将 callgrind/valgrind 附加到程序

[英]attaching callgrind/valgrind to program in mid-way of its execution

I use Valgrind to detect issues by running a program from the beginning.我使用 Valgrind 从一开始就运行程序来检测问题。 Now I have memory/performance issues in a very specific moment in the program.现在我在程序的某个特定时刻遇到了内存/性能问题。 Unfortunately there is no feasible way to make a shortcut to this place from the start.不幸的是,从一开始就没有可行的方法可以快捷地到达这个地方。

Is there a way to instrument the c++ program ( Valgrind/Callgrind ) in its mid-way execution, like attaching to the process?有没有办法在 c++ 程序( Valgrind/Callgrind )的中途执行中检测它,比如附加到进程中?

Already answered here: How use callgrind to profiling only a certain period of program execution?已经在这里回答了: 如何使用 callgrind 只分析程序执行的某个时间段?

There is no way to use valgrind on an already running program.无法在已经运行的程序上使用 valgrind。

For callgrind, you can however somewhat speed it up by only recording data later during execution.但是,对于 callgrind,您可以通过稍后在执行期间仅记录数据来加快其速度。

For this, you might eg look at callgrind options为此,您可以查看 callgrind 选项

    --instr-atstart=no|yes    Do instrumentation at callgrind start [yes]
    --collect-atstart=no|yes  Collect at process/thread start [yes]
    --toggle-collect=<func>   Toggle collection on enter/leave function

You can also control such aspects from within your program.您还可以在程序中控制这些方面。

Refer to valgrind/callgrind user manual for more details.有关详细信息,请参阅 valgrind/callgrind 用户手册。

There are two things that Callgrind does that slows down execution. Callgrind 有两件事会减慢执行速度。

  • Counting operations (the collect part)计数操作(收集部分)
  • Simulating the cache and branch predictor (the instrumentation part).模拟缓存和分支预测器(检测部分)。 This depends on the --cache-sim and --branch-sim options, which both default to "no".这取决于--cache-sim--branch-sim选项,它们都默认为“no”。 If you are using these options and you disable instrumentation then I expect that there will be some impact on the accuracy of the modelling as the cache and predictor won't be "warm" when they are toggled.如果您正在使用这些选项并禁用检测,那么我预计会对建模的准确性产生一些影响,因为缓存和预测器在切换时不会“温暖”。

There are a few other approaches that you could use.您可以使用其他一些方法。

  1. Use the client request mechanisms.使用客户端请求机制。 This would require you to include a Valgrind header and add a few lines that use Valgrind macros CALLGRIND_START_INSTRUMENTATION / CALLGRIND_STOP_INSTRUMENTATION and CALLGRIND_TOGGLE_COLLECT to start and stop instrumentation/collection.这将要求您包含 Valgrind header 并添加几行使用 Valgrind 宏CALLGRIND_START_INSTRUMENTATION / CALLGRIND_STOP_INSTRUMENTATIONCALLGRIND_TOGGLE_COLLECT来启动和停止检测/收集。 See the manual for details.有关详细信息, 请参阅手册 Then just run your application under Valgrind with --instr-atstart=no --collect-atstart=no然后只需使用--instr-atstart=no --collect-atstart=no在 Valgrind 下运行您的应用程序

  2. Use the gdb monitor commands .使用 gdb 监控命令 In this case you would have two terminals.在这种情况下,您将有两个终端。 In the first you would run Valgrind with --instr-atstart=no --collect-atstart=no --vgdb=yes .首先,您将使用--instr-atstart=no --collect-atstart=no --vgdb=yes运行 Valgrind。 In the second terminal run gdb yourapplication then from the gdb prompt (gdb) target remote | vgdb在第二个终端运行gdb yourapplication然后从 gdb 提示符(gdb) target remote | vgdb (gdb) target remote | vgdb . (gdb) target remote | vgdb Then you can use the monitor commands as described in the manual, which include collect and instrumentation control.然后您可以使用手册中描述的监控命令,其中包括收集和检测控制。

  3. callgrind_control , part of the Valgrind distribution. callgrind_control ,Valgrind 发行版的一部分。 To be honest I've never used this.老实说我从来没有用过这个。

I recently did some profiling using the first technique without cache/branch sim.我最近使用第一种没有缓存/分支 sim 的技术进行了一些分析。 When I used Callgrind on the whole run I saw a 23x runtime increase compared to running outside of Callgrind.当我在整个运行过程中使用 Callgrind 时,我发现与在 Callgrind 之外运行相比,运行时间增加了 23 倍。 When I profiled only the one function that I wanted to analyze, this fell to about a 5x slowdown.当我只分析我想分析的一个 function 时,速度下降到大约 5 倍。 Obviously this will very greatly case by case.显然,这将非常逐案。

Thanks all for help.谢谢大家的帮助。 Seems It's been already answered here: How use callgrind to profiling only a certain period of program execution?似乎这里已经回答了: How use callgrind to profiling only a period of program execution? but not marked as answered for some reason.但由于某种原因未标记为已回答。

Summary: Starting callgrind with instrumentation off摘要:在关闭检测的情况下启动 callgrind

valgrind --tool=callgrind --instr-atstart=no <PROG>

Controling instrumentation ( can be executed in other shell )控制仪表(可在其他shell中执行)

callgrind_control -i on/off

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM