gpt4 book ai didi

gcc - GCC 的 '-pg' 标志如何与分析器相关?

转载 作者:行者123 更新时间:2023-12-01 23:43:38 25 4
gpt4 key购买 nike

我试图了解使用 GCC 编译 C 代码时 -pg(或 -p)标志如何工作。

GCC 官方文档 only states :

-pgGenerate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.

这真的让我很感兴趣,因为我正在对分析器进行一些小研究。我正在尝试选择最适合这项工作的工具。

最佳答案

使用 -pg 进行编译检测您的代码,以便 Gprof报告详细信息。请参阅gprof's manual, 9.1 Implementation of Profiling :

Profiling works by changing how every function in your program is compiled so that when it is called, it will stash away some information about where it was called from. From this, the profiler can figure out what function called it, and can count how many times it was called. This change is made by the compiler when your program is compiled with the -pg option, which causes every function to call mcount (or _mcount, or __mcount, depending on the OS and compiler) as one of its first operations.

The mcount routine, included in the profiling library, is responsible for recording in an in-memory call graph table both its parent routine (the child) and its parent's parent. This is typically done by examining the stack frame to find both the address of the child, and the return address in the original parent. Since this is a very machine-dependent operation, mcount itself is typically a short assembly-language stub routine that extracts the required information, and then calls __mcount_internal (a normal C function) with two arguments—frompc and selfpc. __mcount_internal is responsible for maintaining the in-memory call graph, which records frompc, selfpc, and the number of times each of these call arcs was traversed.

...

请注意,使用这样的检测分析器,您将分析与在发行版中编译的相同代码,而无需分析检测。检测代码本身会产生一定的开销。此外,检测代码可能会改变指令和数据缓存的使用。

与仪器分析器相反,采样分析器如 Intel VTune通过使用操作系统中断定期查看目标程序的程序计数器来处理非检测代码。它还可以查询特殊的 CPU 寄存器,让您更深入地了解正在发生的情况。

另请参阅Profilers Instrumenting Vs Sampling .

关于gcc - GCC 的 '-pg' 标志如何与分析器相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7290131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com