RavEngine
Loading...
Searching...
No Matches
PxProfileMemoryEventBuffer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_PROFILE_MEMORY_EVENT_BUFFER_H
30#define PX_PROFILE_MEMORY_EVENT_BUFFER_H
31
32#include "PxProfileDataBuffer.h"
33#include "PxProfileMemoryEvents.h"
34#include "PxProfileMemory.h"
35#include "PxProfileScopedMutexLock.h"
36#include "PxProfileAllocatorWrapper.h"
37#include "PxProfileEventMutex.h"
38
39#include "foundation/PxHash.h"
40#include "foundation/PxHashMap.h"
41#include "foundation/PxUserAllocated.h"
42
43namespace physx { namespace profile {
44
45 template<typename TMutex,
46 typename TScopedLock>
47 class MemoryEventBuffer : public DataBuffer<TMutex, TScopedLock>
48 {
49 public:
51 typedef typename TBaseType::TMutexType TMutexType;
52 typedef typename TBaseType::TScopedLockType TScopedLockType;
57
58 protected:
59 TCharPtrToHandleMap mStringTable;
60
61 public:
62
64 , uint32_t inBufferFullAmount
65 , TMutexType* inBufferMutex )
66 : TBaseType( &cback, inBufferFullAmount, inBufferMutex, "struct physx::profile::MemoryEvent" )
67 , mStringTable( TU8AllocatorType( TBaseType::getWrapper(), "MemoryEventStringBuffer" ) )
68 {
69 }
70
71 uint32_t getHandle( const char* inData )
72 {
73 if ( inData == NULL ) inData = "";
74 const typename TCharPtrToHandleMap::Entry* result( mStringTable.find( inData ) );
75 if ( result )
76 return result->second;
77 uint32_t hdl = mStringTable.size() + 1;
78 mStringTable.insert( inData, hdl );
79 StringTableEvent theEvent;
80 theEvent.init( inData, hdl );
81 sendEvent( theEvent );
82 return hdl;
83 }
84
85 void onAllocation( size_t inSize, const char* inType, const char* inFile, uint32_t inLine, uint64_t addr )
86 {
87 if ( addr == 0 )
88 return;
89 uint32_t typeHdl( getHandle( inType ) );
90 uint32_t fileHdl( getHandle( inFile ) );
91 AllocationEvent theEvent;
92 theEvent.init( inSize, typeHdl, fileHdl, inLine, addr );
93 sendEvent( theEvent );
94 }
95
96 void onDeallocation( uint64_t addr )
97 {
98 if ( addr == 0 )
99 return;
100 DeallocationEvent theEvent;
101 theEvent.init( addr );
102 sendEvent( theEvent );
103 }
104
105 void flushProfileEvents()
106 {
107 TBaseType::flushEvents();
108 }
109
110 protected:
111
112 template<typename TDataType>
113 void sendEvent( TDataType inType )
114 {
115 MemoryEventHeader theHeader( getMemoryEventType<TDataType>() );
116 inType.setup( theHeader );
117 theHeader.streamify( TBaseType::mSerializer );
118 inType.streamify( TBaseType::mSerializer, theHeader );
119 if ( TBaseType::mDataArray.size() >= TBaseType::mBufferFullAmount )
120 flushProfileEvents();
121 }
122 };
123
126 {
128 TMemoryBufferType mBuffer;
129
130 public:
131 PxProfileMemoryEventBufferImpl( PxAllocatorCallback& alloc, uint32_t inBufferFullAmount )
132 : mBuffer( alloc, inBufferFullAmount, NULL )
133 {
134 }
135
136 virtual void onAllocation( size_t size, const char* typeName, const char* filename, int line, void* allocatedMemory )
137 {
138 mBuffer.onAllocation( size, typeName, filename, uint32_t(line), static_cast<uint64_t>(reinterpret_cast<size_t>(allocatedMemory)) );
139 }
140 virtual void onDeallocation( void* allocatedMemory )
141 {
142 mBuffer.onDeallocation(static_cast<uint64_t>(reinterpret_cast<size_t>(allocatedMemory)) );
143 }
144
145 virtual void addClient( PxProfileEventBufferClient& inClient ) { mBuffer.addClient( inClient ); }
146 virtual void removeClient( PxProfileEventBufferClient& inClient ) { mBuffer.removeClient( inClient ); }
147 virtual bool hasClients() const { return mBuffer.hasClients(); }
148
149 virtual void flushProfileEvents() { mBuffer.flushProfileEvents(); }
150
151 virtual void release(){ PX_PROFILE_DELETE( mBuffer.getWrapper().getAllocator(), this ); }
152 };
153}}
154
155#endif
156
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
Definition PxHashMap.h:78
Definition PxBasicTemplates.h:67
Definition PxUserAllocated.h:43
Definition PxProfileDataBuffer.h:41
Definition PxProfileMemoryEventBuffer.h:48
Client handles the data when an event buffer flushes. This data can be parsed (PxProfileEventHandler....
Definition PxProfileEventBufferClient.h:39
Definition PxProfileMemoryEventBuffer.h:126
virtual void onDeallocation(void *allocatedMemory)
callback when memory is deallocated.
Definition PxProfileMemoryEventBuffer.h:140
virtual bool hasClients() const
Check if manager has clients.
Definition PxProfileMemoryEventBuffer.h:147
virtual void onAllocation(size_t size, const char *typeName, const char *filename, int line, void *allocatedMemory)
callback when memory is allocated.
Definition PxProfileMemoryEventBuffer.h:136
virtual void removeClient(PxProfileEventBufferClient &inClient)
Removes a client.
Definition PxProfileMemoryEventBuffer.h:146
virtual void release()
Release the instance.
Definition PxProfileMemoryEventBuffer.h:151
virtual void flushProfileEvents()
Flush profile events. Sends the profile event buffer to hooked clients.
Definition PxProfileMemoryEventBuffer.h:149
virtual void addClient(PxProfileEventBufferClient &inClient)
Adds new client.
Definition PxProfileMemoryEventBuffer.h:145
Stores memory events into the memory buffer.
Definition PxProfileMemory.h:65
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxProfileMemoryEvents.h:242
Definition PxProfileMemoryEvents.h:335
Definition PxProfileMemoryEvents.h:110
Helper class to encapsulate the named allocator.
Definition PxProfileAllocatorWrapper.h:115
Definition PxProfileMemoryEvents.h:178