7#ifndef PXR_BASE_TF_MALLOC_TAG_H
8#define PXR_BASE_TF_MALLOC_TAG_H
11#include "pxr/base/tf/api.h"
22PXR_NAMESPACE_OPEN_SCOPE
27struct Tf_MallocPathNode;
120 size_t maxPrintedNodes = 100000)
const;
129 const std::string &rootName)
const;
134 std::ostream &out)
const;
191 return TfMallocTag::_isInitialized;
254 Auto& operator=(
const Auto &) =
delete;
276 template <
class Str,
class... Strs>
277 explicit Auto(Str &&name1, Strs &&... nameN)
278 : _threadData(
TfMallocTag::_Push(_CStr(
std::forward<Str>(name1))))
279 , _nTags(_threadData ? 1 + sizeof...(Strs) : 0) {
281 (..., TfMallocTag::_Begin(
282 _CStr(std::forward<Strs>(nameN)), _threadData));
298 TfMallocTag::_End(_nTags, _threadData);
299 _threadData =
nullptr;
314 char const *_CStr(
char const *cstr)
const {
return cstr; }
315 char const *_CStr(std::string
const &str)
const {
return str.c_str(); }
317 _ThreadData* _threadData;
342 explicit StackState(Tf_MallocPathNode *top) : _top(top) {}
344 Tf_MallocPathNode *_top =
nullptr;
366 , _tls(state._top ? _Push() :
nullptr) {}
373 if (ARCH_UNLIKELY(_tls)) {
379 TF_API _ThreadData *_Push()
const;
380 TF_API
void _Pop()
const;
395 return ARCH_UNLIKELY(TfMallocTag::_isInitialized)
408 static void Push(
const std::string& name) {
413 static void Push(
const char* name) {
423 if (TfMallocTag::_isInitialized) {
479 friend struct _TemporaryDisabler;
481 friend struct Tf_MallocGlobalData;
483 static bool _Initialize(std::string* errMsg);
485 static inline _ThreadData *_Push(
char const *name) {
486 if (TfMallocTag::_isInitialized) {
492 TF_API
static _ThreadData *_Begin(
char const *name,
493 _ThreadData *threadData =
nullptr);
494 TF_API
static void _End(
int nTags = 1, _ThreadData *threadData =
nullptr);
496 TF_API
static StackState _GetCurrentStackState();
498 static void* _MallocWrapper(
size_t,
const void*);
499 static void* _ReallocWrapper(
void*,
size_t,
const void*);
500 static void* _MemalignWrapper(
size_t,
size_t,
const void*);
501 static void _FreeWrapper(
void*,
const void*);
505 friend class TfMallocTag::Tls;
506 TF_API
static std::atomic<bool> _isInitialized;
549PXR_NAMESPACE_CLOSE_SCOPE
551#define TF_MALLOC_TAG_NEW(name1, name2) \
553 ARCH_ALWAYS_INLINE inline void* operator new(::std::size_t, void* ptr) { \
557 ARCH_ALWAYS_INLINE inline void* operator new(::std::size_t s) { \
558 PXR_NS::TfAutoMallocTag tag(name1, name2); \
559 return ::operator new(s, std::nothrow); \
562 ARCH_ALWAYS_INLINE inline void* operator new[](::std::size_t s) { \
563 PXR_NS::TfAutoMallocTag tag(name1, name2); \
564 return ::operator new[](s, std::nothrow); \
568 ARCH_ALWAYS_INLINE inline void operator delete(void* ptr, void* place) {} \
570 ARCH_ALWAYS_INLINE inline void operator delete(void* ptr, size_t) { \
571 ::operator delete(ptr, std::nothrow); \
574 ARCH_ALWAYS_INLINE inline void operator delete[] (void* ptr, size_t) { \
575 ::operator delete[](ptr, std::nothrow); \
Auto(Str &&name1, Strs &&... nameN)
Push one or more memory tags onto the local-call stack with names name1 ... nameN.
~Auto()
Pop a memory tag from the local-call stack.
void Release()
Pop the tag from the stack before it is destructed.
An object that represents a snapshot of a thread's TfMallocTag stack state.
Top-down memory tagging system.
static void Push(const char *name)
This is an overloaded member function, provided for convenience. It differs from the above function o...
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)...
static StackState GetCurrentStackState()
Capture the current thread's TfMallocTag stack state and return it.
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.
std::vector< uintptr_t > stack
The stack frame pointers.
size_t size
The amount of allocated memory (accumulated over all allocations sharing this call stack).
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.
static void Pop()
Manually pop a tag from the stack.
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...
Summary data structure for malloc statistics.
std::vector< PathNode > children
Children nodes.
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.
std::string siteName
Tag name.
std::vector< CallStackInfo > capturedCallStacks
The captured malloc stacks.
size_t nBytes
Mem allocated including descendant nodes.
TF_API void Report(std::ostream &out) const
Generates a report to the ostream out.
size_t nBytesDirect
Mem allocated excluding descendant nodes.
std::string name
Tag name.
PrintSetting
Specify which parts of the report to print.
@ BOTH
Print both tree and call sites.
@ TREE
Print the full call tree.
@ CALLSITES
Print just the call sites > 0.1%.
PathNode root
Root node of the call-site hierarchy.
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.
Record of the bytes allocated under each different tag.
Node in the call tree structure.