使用 callgrind 追踪不必要的副本
Tracking down unnecessary copies with callgrind
我正在研究 C++ 项目(光线追踪器),我一直在研究并尝试优化代码库。我不是 C++ 专家,而且我确信我犯过很多初学者错误,尤其是在意外复制方面(见下文)。我一直试图找到我的代码的热点区域,但我注意到 callgrind_annotate
中最上面的几行输出主要是内部内存调用:
--------------------------------------------------------------------------------
Ir
--------------------------------------------------------------------------------
195,515,649 PROGRAM TOTALS
--------------------------------------------------------------------------------
Ir file:function
--------------------------------------------------------------------------------
14,526,540 src/matrix.cpp:matrix::operator*(tuple const&) const [./rt.debug.exe]
10,603,101 ???:_platform_memmove$VARIANT$Haswell [/usr/lib/system/libsystem_platform.dylib]
7,481,047 ???:tiny_free_no_lock [/usr/lib/system/libsystem_malloc.dylib]
7,290,995 ???:tiny_free_list_add_ptr [/usr/lib/system/libsystem_malloc.dylib]
6,463,127 ???:szone_malloc_should_clear [/usr/lib/system/libsystem_malloc.dylib]
6,411,076 ???:szone_size [/usr/lib/system/libsystem_malloc.dylib]
6,268,455 src/tuple.cpp:tuple::tuple(double const&, double const&, double const&, double const&) [./rt.debug.exe]
4,726,152 src/scene_objects/scene_object.cpp:scene_object::intersect(ray) [./rt.debug.exe]
4,705,650 ???:tiny_malloc_from_free_list [/usr/lib/system/libsystem_malloc.dylib]
4,415,582 ???:free [/usr/lib/system/libsystem_malloc.dylib]
...
很明显,发生了一些额外的内存分配,如果我知道如何跟踪它,我可以将其删除。
那么,我怎样才能更好地跟踪 是什么 导致了那些额外的内存 allocations/deallocations?
注意:我已经使用 -g
和 -O3
进行编译。
valgrind 提供了几种方法来追踪执行大量 allocation/deallocations.
的代码
除其他外,valgrind memcheck 和 massif 工具提供了一种记录对分配和释放函数的完整调用集的方法。然后可以使用 kcachegrind 等工具将记录的数据可视化。
例如,使用 memcheck,您可以:
valgrind --tool=memcheck --xtree-memory=full <your_program> <your_args>
这将(默认情况下)生成一个文件 xtmemory.kcg,您可以使用 kcachegrind 对其进行可视化。
有关详细信息,请参阅 https://www.valgrind.org/docs/manual/manual-core.html#manual-core.xtree。
dhat 工具还允许检查哪些内存已分配但未被大量使用或使用时间过长。
我正在研究 C++ 项目(光线追踪器),我一直在研究并尝试优化代码库。我不是 C++ 专家,而且我确信我犯过很多初学者错误,尤其是在意外复制方面(见下文)。我一直试图找到我的代码的热点区域,但我注意到 callgrind_annotate
中最上面的几行输出主要是内部内存调用:
--------------------------------------------------------------------------------
Ir
--------------------------------------------------------------------------------
195,515,649 PROGRAM TOTALS
--------------------------------------------------------------------------------
Ir file:function
--------------------------------------------------------------------------------
14,526,540 src/matrix.cpp:matrix::operator*(tuple const&) const [./rt.debug.exe]
10,603,101 ???:_platform_memmove$VARIANT$Haswell [/usr/lib/system/libsystem_platform.dylib]
7,481,047 ???:tiny_free_no_lock [/usr/lib/system/libsystem_malloc.dylib]
7,290,995 ???:tiny_free_list_add_ptr [/usr/lib/system/libsystem_malloc.dylib]
6,463,127 ???:szone_malloc_should_clear [/usr/lib/system/libsystem_malloc.dylib]
6,411,076 ???:szone_size [/usr/lib/system/libsystem_malloc.dylib]
6,268,455 src/tuple.cpp:tuple::tuple(double const&, double const&, double const&, double const&) [./rt.debug.exe]
4,726,152 src/scene_objects/scene_object.cpp:scene_object::intersect(ray) [./rt.debug.exe]
4,705,650 ???:tiny_malloc_from_free_list [/usr/lib/system/libsystem_malloc.dylib]
4,415,582 ???:free [/usr/lib/system/libsystem_malloc.dylib]
...
很明显,发生了一些额外的内存分配,如果我知道如何跟踪它,我可以将其删除。
那么,我怎样才能更好地跟踪 是什么 导致了那些额外的内存 allocations/deallocations?
注意:我已经使用 -g
和 -O3
进行编译。
valgrind 提供了几种方法来追踪执行大量 allocation/deallocations.
的代码除其他外,valgrind memcheck 和 massif 工具提供了一种记录对分配和释放函数的完整调用集的方法。然后可以使用 kcachegrind 等工具将记录的数据可视化。
例如,使用 memcheck,您可以:
valgrind --tool=memcheck --xtree-memory=full <your_program> <your_args>
这将(默认情况下)生成一个文件 xtmemory.kcg,您可以使用 kcachegrind 对其进行可视化。
有关详细信息,请参阅 https://www.valgrind.org/docs/manual/manual-core.html#manual-core.xtree。
dhat 工具还允许检查哪些内存已分配但未被大量使用或使用时间过长。