简体   繁体   English

剖析 C++ 编译过程

[英]Profiling the C++ compilation process

I tend to write rather large templated header-only C++ libraries and my users commonly complain about compilation times.我倾向于编写相当大的模板化头文件 C++ 库,我的用户通常会抱怨编译时间。 After thinking about the matter, it occurred to me that I have no idea where the time is going .想了想之后,突然觉得自己不知道时间都去哪儿了 Is there some simple way to profile the C++ compilation process with common compilers, such as g++, icc, and xlC?是否有一些简单的方法可以使用通用编译器(例如 g++、icc 和 xlC)来分析 C++ 编译过程? For instance, is it possible to get an idea of how much time is spent within each of the phases of C++ compilation ?例如,是否有可能了解在C++ 编译的每个阶段中花费了多少时间?

For GCC there are debugging options to find how much time is spent within each of the phases of C++ compilation?对于GCC ,有调试选项可以查看how much time is spent within each of the phases of C++ compilation?

-Q Makes the compiler print out each function name as it is compiled, and print some statistics about each pass when it finishes. -Q使编译器在编译时打印出每个函数名称,并在完成时打印有关每个传递的一些统计信息。

-ftime-report Makes the compiler print some statistics about the time consumed by each pass when it finishes. -ftime-report使编译器在完成时打印有关每次传递消耗的时间的一些统计信息。

Passes are described in GCCINT 9: Passes and Files of the Compiler .GCCINT 9: Passes and Files of the Compiler中描述了通道

You can post output of g++ compilation of single source file with -v -ftime-report here to discuss it .您可以在此处使用-v -ftime-report单个源文件的 g++ 编译输出以进行讨论 There could be some help on the GCC mailing list . GCC邮件列表上可能有一些帮助。


For compilers other than GCC (or GCC more ancient than 3.3.6 ) see the other options in this thread.对于GCC (或比3.3.6更古老的GCC)以外的编译器,请参阅此线程中的其他选项。

There's a tool from the Boost project, which could be useful for pretty much any compiler and build system. Boost 项目中有一个工具,它对几乎所有编译器和构建系统都有用。

The tool requires source code instrumentation with TEMPLATE_PROFILE_ENTER() and TEMPLATE_PROFILE_EXIT() macro calls.该工具需要使用TEMPLATE_PROFILE_ENTER()TEMPLATE_PROFILE_EXIT()宏调用进行源代码检测 These macros then generate specific diagnostics (warnings) at compile-time, which are timed and collected along with instantiation callstacks (which consequently allow building and visualizing callgraphs) by a script.这些宏然后在编译时生成特定的诊断(警告),这些诊断(警告)与实例化调用堆栈(因此允许构建和可视化调用图)一起由脚本计时和收集。 Not bad, IMO.不错,海事组织。

I didn't use it yet though.不过我还没用。

Clang 9 (and newer) has a -ftime-trace flag, which makes it output a profiling report as JSON (in addition to an object file). Clang 9(和更新版本)有一个-ftime-trace标志,这使得它以 JSON 格式输出分析报告(除了目标文件)。

You can import this file into a profiler that comes with Chrome ( chrome://tracing ) to get a nice visualisation:您可以将此文件导入 Chrome 随附的分析器 ( chrome://tracing ) 中以获得不错的可视化效果:

图片

The bars correspond to headers that had to be parsed, and for each header, specific classes (and probably other constructs) that had to be parsed.条形对应于必须解析的标题,并且对于每个标题,必须解析特定的类(可能还有其他结构)。 It also reports time spent on instantiating specific templates.它还报告实例化特定模板所花费的时间。

我还没有尝试过,但 templight 看起来很有希望: https : //github.com/mikael-s-persson/templight

You can separate them out to some extent (I'm assuming make )您可以在某种程度上将它们分开(我假设是make

  • add a build rule that only preprocesses files (using the -E switch), and a .PHONY target that depends on the preprocessor output files just like the normal binary target depends on .o files.添加仅预处理文件的构建规则(使用-E开关),以及依赖于预处理器输出文件的.PHONY目标,就像普通的二进制目标依赖于.o文件一样。 Measure how long it takes to build this target测量构建这个目标需要多长时间
  • add a 'PHONY target that depends on all the .o files but doesn't link them.添加一个依赖于所有.o文件但不链接它们的'PHONY目标'PHONY Measure how long it takes to build this target (from clean)测量构建这个目标需要多长时间(从干净)
  • measure how long it takes to do a clean build of the usual binary测量对通常的二进制文件进行干净构建所需的时间

Now you have some idea how long it takes to pre-process, compile, and link.现在您已经知道预处理、编译和链接需要多长时间。 You can also compare optimized and non-optimized ( -O0 ) versions of the second and third target, to see how long is spent in the optimizer.您还可以比较第二个和第三个目标的优化和非优化 ( -O0 ) 版本,以查看在优化器中花费的时间。

您可能能够通过strace -e trace=process -f -r -ttt -T上的某些变体获得一些吸引力,至少对于像 g++ 这样被分解为多个进程的编译器而言是这样。

Others have already suggested the-ftime-report command line flag for GCC, which makes the compiler print some statistics about the time consumed by each compilation phase.其他人已经建议 GCC 使用-ftime-report命令行标志,这使得编译器打印一些关于每个编译阶段消耗的时间的统计信息。 The drawback is that it only shows summary for one unit.缺点是它只显示一个单元的摘要。

I've written a Python script , which allows to print total summary on all units, by each compilation phase, given the project build log file.我写了一个 Python脚本,它允许在每个编译阶段打印所有单元的总摘要,给定项目构建日志文件。 It also allows sorting by different phases.它还允许按不同阶段进行排序。 And it also allows to compare two log files (eg, if you're trying to understand the impact of your changes).它还允许比较两个日志文件(例如,如果您想了解更改的影响)。

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

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