简体   繁体   English

C ++中的多线程内存分析

[英]Multithread Memory Profiling in C++

I am working on profiling the memory usage of multiple threads in my application. 我正在分析应用程序中多个线程的内存使用情况。 I would like to be able to track the maximum allocation/current allocation of any given thread that is running. 我希望能够跟踪正在运行的任何给定线程的最大分配/当前分配。 In order to so, I planned on interposing on mallocs/frees. 为了做到这一点,我计划在mallocs / frees上进行设置。 During each call to malloc, I would update the allocation records for the particular thread in a static map that associated thread ids to their particular metadata record. 在对malloc的每次调用期间,我都会在静态映射中更新该线程的分配记录,该映射将线程ID与其特定的元数据记录相关联。 I am currently having issues during process exit. 我目前在流程退出期间遇到问题。 I think the issue is that when all the destructors are called for cleanup, the static map and lock protecting it have to be destroyed. 我认为问题在于,当所有析构函数都需要进行清理时,必须销毁静态映射和保护它的锁。 My interposed mallocs/frees, however, acquire the lock before updating the profiling metadata structures. 但是,我插入的malloc / frees在更新概要分析元数据结构之前获取了锁。 Eventually, the lock is destroyed, but there are subsequent calls to malloc/free that result in an attempt to acquire the no longer existent lock resulting in a segfault. 最终,该锁被破坏了,但是随后对malloc / free的调用导致尝试获取不再存在的锁,从而导致段错误。

Another issue that I am concerned about is that there are internal calls to malloc generated within my interposed malloc to allocate entries in the map. 我担心的另一个问题是,在插入的malloc中生成了对malloc的内部调用,以在映射中分配条目。

Any ideas on ways of approaching the problem of profiling memory usage on a per thread basis? 关于解决基于每个线程的内存使用情况分析问题的方法的任何想法? Any suggestions on data structures to track the usage of each thread? 关于跟踪每个线程的使用情况的数据结构有什么建议吗? Does the above approach seem reasonable or are there any other ways of approaching the problem? 以上方法看起来是否合理,或者是否有其他方法可以解决问题?

If you store your "extra" data as part of the allocation itself (before is easier, but you could do it after too - just need a size somewhere), then you shouldn't need any locks at all. 如果您将“额外”数据存储为分配本身的一部分(之前比较容易,但是您也可以在之后进行-只需在某个地方设置大小),那么根本就不需要任何锁。 Just a tad more memory. 只是一点点的内存。 Of course, you will need to use atomics to update any lists of items. 当然,您将需要使用原子来更新任何项目列表。

If you look at this answer: 如果您看以下答案:

Setting memory on a custom heap 在自定义堆上设置内存

and imagine that HeapAlloc and HeapFree are malloc and free respectively. 并想象HeapAllocHeapFree分别是mallocfree Then add code to store which thread is being used for the allocation. 然后添加代码以存储正在使用哪个线程进行分配。

So, instead of using a map, you simply update a linked list (using atomics to prevent multiple updates). 因此,您无需使用地图,而只需更新链表(使用原子防止多次更新)。 This does of course make it a little more difficult to make the up to date measurements per thread, you'll have to scan the list of allocations. 当然,这确实使每个线程的最新度量变得有些困难,您必须扫描分配列表。

Of course, this only works for DIRECT calls to malloc and free . 当然,这仅适用于对mallocfree直接调用。

The same principle would be possible by "injecting" a replacement malloc / free function (built along the principles in the other post, but of course not using the original malloc to allocate the memory, and not using free to free the memory). 通过“注入”替换的malloc / free函数(按照另一篇文章中的原理构建,但当然不使用原始malloc分配内存,也不使用free释放内存),可以实现相同的原理。

This is a complicated thing to do and make work for all cases. 这是一件复杂的事情,要在所有情况下都可以做。 There are many issues that you'll miss and only ever find through trial and error. 您会错过许多问题,只有通过反复试验才能找到。 I should know, I've been responsible for building a tool that does what you are trying to do. 我应该知道,我负责构建可以完成您想做的事情的工具。 We've been doing this since 1999, available commercially since 2002. 自1999年以来,我们就一直在这样做,自2002年以来就可以在市场上购买。

If you are using Windows, C++ Memory Validator can give you per-thread profiling statistics. 如果使用Windows,则C ++ Memory Validator可以为您提供每个线程分析的统计信息。 http://www.softwareverify.com/cpp-memory.php . http://www.softwareverify.com/cpp-memory.php

The Objects tab and Sizes tab both have Threads sub-tabs which allow you to view data per thread. “对象”选项卡和“大小”选项卡都有“线程”子选项卡,可让您查看每个线程的数据。 You can also run advanced queries on the Analysis tab that will allow you to view data on a per-thread basis. 您还可以在“分析”选项卡上运行高级查询,这将使您可以按线程查看数据。

Spend your time on your job, not writing tools. 花时间在工作上,而不是在写工具。

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

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