All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mallocTag.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_MALLOC_TAG_H
8#define PXR_BASE_TF_MALLOC_TAG_H
9
10#include "pxr/pxr.h"
11#include "pxr/base/tf/api.h"
12
13#include <atomic>
14#include <cstdlib>
15#include <cstdint>
16#include <iosfwd>
17#include <string>
18#include <vector>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
24
25struct Tf_MallocPathNode;
26
34public:
35 struct CallStackInfo;
36
43 struct CallTree {
63 struct PathNode {
64 size_t nBytes,
66 size_t nAllocations;
67 std::string siteName;
68 std::vector<PathNode>
70 };
71
80 struct CallSite {
81 std::string name;
82 size_t nBytes;
83 };
84
87 TREE = 0,
89 BOTH
90 };
91
92
95
116 TF_API
118 size_t maxPrintedNodes = 100000) const;
119
124 TF_API
125 void Report(
126 std::ostream &out,
127 const std::string &rootName) const;
128
130 TF_API
131 void Report(
132 std::ostream &out) const;
133
137 TF_API
139 std::istream &in);
140
142
143
145 std::vector<CallSite> callSites;
146
149
151 std::vector<CallStackInfo> capturedCallStacks;
152 };
153
158 {
160 std::vector<uintptr_t> stack;
161
164 size_t size;
165
169 };
170
182 TF_API static bool Initialize(std::string* errMsg);
183
188 static bool IsInitialized() {
189 return TfMallocTag::_isInitialized;
190 }
191
197 TF_API static size_t GetTotalBytes();
198
204 TF_API static size_t GetMaxTotalBytes();
205
218 TF_API static bool GetCallTree(CallTree* tree, bool skipRepeated = true);
219
220private:
221
222 struct _ThreadData;
223
224public:
225
249 class Auto {
250 public:
251 Auto(const Auto &) = delete;
252 Auto& operator=(const Auto &) = delete;
253
254 Auto(Auto &&) = delete;
255 Auto& operator=(Auto &&) = delete;
256
274 template <class Str, class... Strs>
275 explicit Auto(Str &&name1, Strs &&... nameN)
276 : _threadData(TfMallocTag::_Push(_CStr(std::forward<Str>(name1))))
277 , _nTags(_threadData ? 1 + sizeof...(Strs) : 0) {
278 if (_threadData) {
279 (..., TfMallocTag::_Begin(
280 _CStr(std::forward<Strs>(nameN)), _threadData));
281 }
282 }
283
294 inline void Release() {
295 if (_threadData) {
296 TfMallocTag::_End(_nTags, _threadData);
297 _threadData = nullptr;
298 }
299 }
300
306 inline ~Auto() {
307 Release();
308 }
309
310 private:
311
312 char const *_CStr(char const *cstr) const { return cstr; }
313 char const *_CStr(std::string const &str) const { return str.c_str(); }
314
315 _ThreadData* _threadData;
316 int _nTags;
317
318 friend class TfMallocTag;
319 };
320
321 // A historical compatibility: before Auto could accept only one argument,
322 // so Auto2 existed to handle two arguments. Now Auto can accept any number
323 // of arguments, so Auto2 is just an alias for Auto.
324 using Auto2 = Auto;
325
335 static void Push(const std::string& name) {
336 _Push(name.c_str());
337 }
338
340 static void Push(const char* name) {
341 _Push(name);
342 }
343
349 static void Pop() {
350 if (TfMallocTag::_isInitialized) {
351 _End();
352 }
353 }
354
372 TF_API static void SetDebugMatchList(const std::string& matchList);
373
395 TF_API static void SetCapturedMallocStacksMatchList(const std::string& matchList);
396
402 TF_API static std::vector<std::vector<uintptr_t> > GetCapturedMallocStacks();
403
404private:
405 friend struct _TemporaryDisabler;
406
407 friend struct Tf_MallocGlobalData;
408
409 static bool _Initialize(std::string* errMsg);
410
411 static inline _ThreadData *_Push(char const *name) {
412 if (TfMallocTag::_isInitialized) {
413 return _Begin(name);
414 }
415 return nullptr;
416 }
417
418 TF_API static _ThreadData *_Begin(char const *name,
419 _ThreadData *threadData = nullptr);
420 TF_API static void _End(int nTags = 1, _ThreadData *threadData = nullptr);
421
422 static void* _MallocWrapper(size_t, const void*);
423 static void* _ReallocWrapper(void*, size_t, const void*);
424 static void* _MemalignWrapper(size_t, size_t, const void*);
425 static void _FreeWrapper(void*, const void*);
426
427 friend class TfMallocTag::Auto;
428 class Tls;
429 friend class TfMallocTag::Tls;
430 TF_API static std::atomic<bool> _isInitialized;
431};
432
435
438
472//
473PXR_NAMESPACE_CLOSE_SCOPE
474
475#define TF_MALLOC_TAG_NEW(name1, name2) \
476 /* this is for STL purposes */ \
477 inline void* operator new(::std::size_t, void* ptr) { \
478 return ptr; \
479 } \
480 \
481 inline void* operator new(::std::size_t s) { \
482 PXR_NS::TfAutoMallocTag tag(name1, name2); \
483 return malloc(s); \
484 } \
485 \
486 inline void* operator new[](::std::size_t s) { \
487 PXR_NS::TfAutoMallocTag tag(name1, name2); \
488 return malloc(s); \
489 } \
490 \
491 /* Required due to the placement-new override above. */ \
492 inline void operator delete(void* ptr, void* place) {} \
493 \
494 inline void operator delete(void* ptr, size_t) { \
495 free(ptr); \
496 } \
497 \
498 inline void operator delete[] (void* ptr, size_t) { \
499 free(ptr); \
500 } \
501
502#endif
Scoped (i.e.
Definition: mallocTag.h:249
Auto(Str &&name1, Strs &&... nameN)
Push one or more memory tags onto the local-call stack with names name1 ... nameN.
Definition: mallocTag.h:275
~Auto()
Pop a memory tag from the local-call stack.
Definition: mallocTag.h:306
void Release()
Pop the tag from the stack before it is destructed.
Definition: mallocTag.h:294
Top-down memory tagging system.
Definition: mallocTag.h:33
static void Push(const char *name)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: mallocTag.h:340
static TF_API void SetDebugMatchList(const std::string &matchList)
Sets the tags to trap in the debugger.
size_t numAllocations
The number of allocations (always one unless stack frames have been combined to create unique stacks)...
Definition: mallocTag.h:168
static TF_API std::vector< std::vector< uintptr_t > > GetCapturedMallocStacks()
Returns the captured malloc stack traces for allocations billed to the malloc tags passed to SetCaptu...
static TF_API void SetCapturedMallocStacksMatchList(const std::string &matchList)
Sets the tags to trace.
static void Push(const std::string &name)
Manually push a tag onto the stack.
Definition: mallocTag.h:335
std::vector< uintptr_t > stack
The stack frame pointers.
Definition: mallocTag.h:160
size_t size
The amount of allocated memory (accumulated over all allocations sharing this call stack).
Definition: mallocTag.h:164
static TF_API size_t GetTotalBytes()
Return total number of allocated bytes.
static TF_API bool Initialize(std::string *errMsg)
Initialize the memory tagging system.
static bool IsInitialized()
Return true if the tagging system is active.
Definition: mallocTag.h:188
static void Pop()
Manually pop a tag from the stack.
Definition: mallocTag.h:349
static TF_API bool GetCallTree(CallTree *tree, bool skipRepeated=true)
Return a snapshot of memory usage.
static TF_API size_t GetMaxTotalBytes()
Return the maximum total number of bytes that have ever been allocated at one time.
This struct is used to represent a call stack taken for an allocation that was billed under a specifi...
Definition: mallocTag.h:158
STL namespace.
Summary data structure for malloc statistics.
Definition: mallocTag.h:43
std::vector< PathNode > children
Children nodes.
Definition: mallocTag.h:69
TF_API std::string GetPrettyPrintString(PrintSetting setting=BOTH, size_t maxPrintedNodes=100000) const
Return the malloc report string.
size_t nAllocations
The number of allocations for this node.
Definition: mallocTag.h:66
std::string siteName
Tag name.
Definition: mallocTag.h:67
std::vector< CallStackInfo > capturedCallStacks
The captured malloc stacks.
Definition: mallocTag.h:151
size_t nBytes
Allocated bytes by this or descendant nodes.
Definition: mallocTag.h:64
TF_API void Report(std::ostream &out) const
Generates a report to the ostream out.
size_t nBytesDirect
Allocated bytes (only for this node).
Definition: mallocTag.h:65
std::string name
Tag name.
Definition: mallocTag.h:81
PrintSetting
Specify which parts of the report to print.
Definition: mallocTag.h:86
@ BOTH
Print both tree and call sites.
Definition: mallocTag.h:89
@ TREE
Print the full call tree.
Definition: mallocTag.h:87
@ CALLSITES
Print just the call sites > 0.1%.
Definition: mallocTag.h:88
PathNode root
Root node of the call-site hierarchy.
Definition: mallocTag.h:148
TF_API void Report(std::ostream &out, const std::string &rootName) const
Generates a report to the ostream out.
TF_API bool LoadReport(std::istream &in)
Load the contents of in into the root of the call tree.
std::vector< CallSite > callSites
All call sites.
Definition: mallocTag.h:145
Record of the bytes allocated under each different tag.
Definition: mallocTag.h:80
Node in the call tree structure.
Definition: mallocTag.h:63