24 #ifndef PXR_BASE_TF_MALLOC_TAG_H 25 #define PXR_BASE_TF_MALLOC_TAG_H 28 #include "pxr/base/tf/api.h" 37 PXR_NAMESPACE_OPEN_SCOPE
42 struct Tf_MallocPathNode;
133 size_t maxPrintedNodes = 100000)
const;
143 const std::string &rootName)
const;
148 std::ostream &out)
const;
188 TF_API
static bool Initialize(std::string* errMsg);
195 return TfMallocTag::_isInitialized;
224 TF_API
static bool GetCallTree(CallTree* tree,
bool skipRepeated =
true);
258 Auto& operator=(
const Auto &) =
delete;
280 template <
class Str,
class... Strs>
281 explicit Auto(Str &&name1, Strs &&... nameN)
282 : _threadData(
TfMallocTag::_Push(_CStr(std::forward<Str>(name1))))
284 ? 1 + _PushImpl(std::forward<Strs>(nameN)...)
299 TfMallocTag::_End(_threadData);
301 _threadData =
nullptr;
315 char const *_CStr(
char const *cstr)
const {
return cstr; }
316 char const *_CStr(std::string
const &str)
const {
return str.c_str(); }
318 template <
class Str,
class... Strs>
319 int _PushImpl(Str &&tag, Strs &&... rest) {
320 TfMallocTag::_Begin(_CStr(std::forward<Str>(tag)), _threadData);
321 return 1 + _PushImpl(std::forward<Strs>(rest)...);
329 _ThreadData* _threadData;
349 static void Push(
const std::string& name) {
354 static void Push(
const char* name) {
364 if (TfMallocTag::_isInitialized) {
419 friend struct _TemporaryDisabler;
421 friend struct Tf_MallocGlobalData;
423 static bool _Initialize(std::string* errMsg);
425 static inline _ThreadData *_Push(
char const *name) {
426 if (TfMallocTag::_isInitialized) {
432 TF_API
static _ThreadData *_Begin(
char const *name,
433 _ThreadData *threadData =
nullptr);
434 TF_API
static void _End(_ThreadData *threadData =
nullptr);
436 static void* _MallocWrapper(
size_t,
const void*);
437 static void* _ReallocWrapper(
void*,
size_t,
const void*);
438 static void* _MemalignWrapper(
size_t,
size_t,
const void*);
439 static void _FreeWrapper(
void*,
const void*);
443 friend class TfMallocTag::Tls;
444 TF_API
static std::atomic<bool> _isInitialized;
487 PXR_NAMESPACE_CLOSE_SCOPE
489 #define TF_MALLOC_TAG_NEW(name1, name2) \ 491 inline void* operator new(::std::size_t, void* ptr) { \ 495 inline void* operator new(::std::size_t s) { \ 496 PXR_NS::TfAutoMallocTag tag(name1, name2); \ 500 inline void* operator new[](::std::size_t s) { \ 501 PXR_NS::TfAutoMallocTag tag(name1, name2); \ 506 inline void operator delete(void* ptr, void* place) {} \ 508 inline void operator delete(void* ptr, size_t) { \ 512 inline void operator delete[] (void* ptr, size_t) { \ static void Push(const char *name)
Summary data structure for malloc statistics.
static TF_API size_t GetTotalBytes()
Return total number of allocated bytes.
This struct is used to represent a call stack taken for an allocation that was billed under a specifi...
size_t nAllocations
The number of allocations for this node.
void Release()
Pop the tag from the stack before it is destructed.
TF_API void Report(std::ostream &out, const std::string &rootName) const
Generates a report to the ostream out.
size_t nBytes
Allocated bytes by this or descendant nodes.
static TF_API bool GetCallTree(CallTree *tree, bool skipRepeated=true)
Return a snapshot of memory usage.
size_t size
The amount of allocated memory (accumulated over all allocations sharing this call stack).
Print both tree and call sites.
std::vector< CallStackInfo > capturedCallStacks
The captured malloc stacks.
std::vector< uintptr_t > stack
The stack frame pointers.
std::vector< CallSite > callSites
All call sites.
std::string name
Tag name.
PathNode root
Root node of the call-site hierarchy.
Print the full call tree.
static TF_API void SetDebugMatchList(const std::string &matchList)
Sets the tags to trap in the debugger.
Print just the call sites > 0.1%.
static void Pop()
Manually pop a tag from the stack.
static TF_API void SetCapturedMallocStacksMatchList(const std::string &matchList)
Sets the tags to trace.
PrintSetting
Specify which parts of the report to print.
Auto(Str &&name1, Strs &&... nameN)
Push one or more memory tags onto the local-call stack with names name1 ...
std::string siteName
Tag name.
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...
std::vector< PathNode > children
Children nodes.
Top-down memory tagging system.
static bool IsInitialized()
Return true if the tagging system is active.
static void Push(const std::string &name)
Manually push a tag onto the stack.
size_t numAllocations
The number of allocations (always one unless stack frames have been combined to create unique stacks)...
size_t nBytesDirect
Allocated bytes (only for this node).
static TF_API size_t GetMaxTotalBytes()
Return the maximum total number of bytes that have ever been allocated at one time.
Node in the call tree structure.
Record of the bytes allocated under each different tag.
static TF_API bool Initialize(std::string *errMsg)
Initialize the memory tagging system.
TF_API std::string GetPrettyPrintString(PrintSetting setting=BOTH, size_t maxPrintedNodes=100000) const
Return the malloc report string.
~Auto()
Pop a memory tag from the local-call stack.