gcc的-pg标志如何工作? [英] How does gcc's -pg flag work?

查看:504
本文介绍了gcc的-pg标志如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 -pg (或 -p )如何在编译C代码与 gcc

I'm trying to understand how does the -pg (or -p) flag works when compiling a C code with gcc.

官方gcc文档只有状态


-pg

生成额外的代码以写入适用于分析程序gprof的概要文件信息。您必须在编译所需数据的源文件时使用此选项,并且您还必须在链接时使用它。

-pg
Generate 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手册,9.1实施分析

Compiling with -pg instruments your code so that gprof reports detailed information, see gprof's manual, 9.1 Implementation of Profiling


你的程序中的每个函数是如何编译的,所以当它被调用时,它会隐藏一些关于调用它的地方的信息。从这里,分析器可以弄清楚什么函数调用它,并且可以计算它被调用的次数。当你的程序使用 -pg'选项编译时,编译器会进行这种更改,这会导致每个函数调用 mcount (或 mcount _mcount`,这取决于操作系统和编译器)。

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 callmcount(ormcount, or_mcount`, depending on the OS and compiler) as one of its first operations.

包含在性能分析库中的 mcount 例程负责在内存调用图表中记录其父例程子)和其父级的父级。这通常通过检查堆栈帧来找到子节点的地址和原始父节点的返回地址来完成。因为这是一个非常依赖于机器的操作,所以 mcount 本身通常是一个简短的汇编语言存根例程,提取所需的信息,然后调用 __ mcount_internal具有两个参数 - frompc selfpc __ mcount_internal 负责维护内存中调用图,记录 frompc selfpc ,以及每个这些调用弧的遍历次数。

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.

...

请注意,使用这样的检测分析器,您可以分析在没有分析检测的情况下编译的相同代码。有一个与仪器代码本身相关的开销。此外,检测代码可能会改变指令和数据缓存的使用。

Please note that with such an instrumenting profiler, you're profiling the same code you would compile in release without profiling instrumentation. There is an overhead associated with the instrumentation code itself. Also, the instrumentation code may alter instruction and data cache usage.

与检测分析器相反,抽样分析器如通过使用操作系统中断以定期间隔查看目标程序的程序计数器来处理非测试代码。

Contrary to an instrumenting profiler, a sampling profiler like Intel VTune works on non instrumented code by looking at the target program's program counter at regular intervals using operating system interrupts. It can also query special CPU registers to give you even more insight of what's going on.

另请参阅 Profplers Instrumenting Vs Sampling

这篇关于gcc的-pg标志如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆