RavEngine
Loading...
Searching...
No Matches
PxProfileZoneImpl.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_ZONE_IMPL_H
30#define PX_PROFILE_ZONE_IMPL_H
31
32#include "PxPvdProfileZone.h"
33#include "PxProfileZoneManager.h"
34#include "PxProfileContextProviderImpl.h"
35#include "PxProfileScopedMutexLock.h"
36#include "PxProfileEventBufferAtomic.h"
37#include "foundation/PxMutex.h"
38
39namespace physx { namespace profile {
40
45 {
46 void setEventEnabled( const PxProfileEventId&, bool) { PX_ASSERT(false); }
47 bool isEventEnabled( const PxProfileEventId&) const { return true; }
48 };
49
53 //typedef EventBufferAtomic< PxDefaultContextProvider, TZoneMutexType, TZoneLockType, PxProfileNullEventFilter > TZoneEventBufferType;
54
55 template<typename TNameProvider>
56 class ZoneImpl : TZoneEventBufferType //private inheritance intended
57 , public PxProfileZone
59 {
62 //ensure we don't reuse event ids.
64 typedef TMutexType::ScopedLock TLockType;
65
66
67 const char* mName;
68 mutable TMutexType mMutex;
70 // to avoid locking, read-only and read-write map exist
71 TNameToEvtIndexMap mNameToEvtIndexMapR;
72 TNameToEvtIndexMap mNameToEvtIndexMapRW;
73 //ensure we don't reuse event ids.
74 TEvtIdToNameMap mEvtIdToNameMap;
75
76 PxProfileZoneManager* mProfileZoneManager;
77
79 volatile bool mEventsActive;
80
82 public:
83 ZoneImpl( PxAllocatorCallback* inAllocator, const char* inName, uint32_t bufferSize = 0x10000 /*64k*/, const TNameProvider& inProvider = TNameProvider() )
84 : TZoneEventBufferType( inAllocator, bufferSize, PxDefaultContextProvider(), NULL, PxProfileNullEventFilter() )
85 , mName( inName )
87 , mEventNames( mWrapper )
88 , mNameToEvtIndexMapR( mWrapper )
89 , mNameToEvtIndexMapRW( mWrapper )
90 , mEvtIdToNameMap( mWrapper )
91 , mProfileZoneManager( NULL )
92 , mZoneClients( mWrapper )
93 , mEventsActive( false )
94 {
95 TZoneEventBufferType::setBufferMutex( &mMutex );
96 //Initialize the event name structure with existing names from the name provider.
97 PxProfileNames theNames( inProvider.getProfileNames() );
98 for ( uint32_t idx = 0; idx < theNames.eventCount; ++idx )
99 {
100 const PxProfileEventName& theName (theNames.events[idx]);
101 doAddName( theName.name, theName.eventId.eventId, theName.eventId.compileTimeEnabled );
102 }
103 TZoneEventBufferType::addClient( *this );
104 }
105
106 virtual ~ZoneImpl() {
107 if ( mProfileZoneManager != NULL )
108 mProfileZoneManager->removeProfileZone( *this );
109 mProfileZoneManager = NULL;
110 TZoneEventBufferType::removeClient( *this );
111 }
112
113 void doAddName( const char* inName, uint16_t inEventId, bool inCompileTimeEnabled )
114 {
115 TLockType theLocker( mMutex );
116 mEvtIdToNameMap.insert( inEventId, inName );
117 uint32_t idx = static_cast<uint32_t>( mEventNames.size() );
118 mNameToEvtIndexMapRW.insert( inName, idx );
119 mEventNames.pushBack( PxProfileEventName( inName, PxProfileEventId( inEventId, inCompileTimeEnabled ) ) );
120 }
121
122 virtual void flushEventIdNameMap()
123 {
124 // copy the RW map into R map
125 if (mNameToEvtIndexMapRW.size())
126 {
127 for (TNameToEvtIndexMap::Iterator iter = mNameToEvtIndexMapRW.getIterator(); !iter.done(); ++iter)
128 {
129 mNameToEvtIndexMapR.insert(iter->first, iter->second);
130 }
131 mNameToEvtIndexMapRW.clear();
132 }
133 }
134
135 virtual uint16_t getEventIdForName( const char* inName )
136 {
137 return getEventIdsForNames( &inName, 1 );
138 }
139
140 virtual uint16_t getEventIdsForNames( const char** inNames, uint32_t inLen )
141 {
142 if ( inLen == 0 )
143 return 0;
144
145 // search the read-only map first
146 const TNameToEvtIndexMap::Entry* theEntry( mNameToEvtIndexMapR.find( inNames[0] ) );
147 if ( theEntry )
148 return mEventNames[theEntry->second].eventId;
149
150 TLockType theLocker(mMutex);
151
152 const TNameToEvtIndexMap::Entry* theReEntry(mNameToEvtIndexMapRW.find(inNames[0]));
153 if (theReEntry)
154 return mEventNames[theReEntry->second].eventId;
155
156 //Else git R dun.
157 uint16_t nameSize = static_cast<uint16_t>( mEventNames.size() );
158 //We don't allow 0 as an event id.
159 uint16_t eventId = nameSize;
160 //Find a contiguous set of unique event ids
161 bool foundAnEventId = false;
162 do
163 {
164 foundAnEventId = false;
165 ++eventId;
166 for ( uint16_t idx = 0; idx < inLen && foundAnEventId == false; ++idx )
167 foundAnEventId = mEvtIdToNameMap.find( uint16_t(eventId + idx) ) != NULL;
168 }
169 while( foundAnEventId );
170
171 uint32_t clientCount = mZoneClients.size();
172 for ( uint16_t nameIdx = 0; nameIdx < inLen; ++nameIdx )
173 {
174 uint16_t newId = uint16_t(eventId + nameIdx);
175 doAddName( inNames[nameIdx], newId, true );
176 for( uint32_t clientIdx =0; clientIdx < clientCount; ++clientIdx )
177 mZoneClients[clientIdx]->handleEventAdded( PxProfileEventName( inNames[nameIdx], PxProfileEventId( newId ) ) );
178 }
179
180 return eventId;
181 }
182
184 {
185 mProfileZoneManager = inMgr;
186 }
187
189 {
190 return mProfileZoneManager;
191 }
192
193
194
195 const char* getName() { return mName; }
196
197 PxProfileEventBufferClient* getEventBufferClient() { return this; }
198
199 //SDK implementation
200
202 {
203 TLockType lock( mMutex );
204 mZoneClients.pushBack( &inClient );
205 mEventsActive = true;
206 }
207
209 {
210 TLockType lock( mMutex );
211 for ( uint32_t idx =0; idx < mZoneClients.size(); ++idx )
212 {
213 if (mZoneClients[idx] == &inClient )
214 {
215 inClient.handleClientRemoved();
216 mZoneClients.replaceWithLast( idx );
217 break;
218 }
219 }
220 mEventsActive = mZoneClients.size() != 0;
221 }
222
223 virtual bool hasClients() const
224 {
225 return mEventsActive;
226 }
227
229 {
230 TLockType theLocker( mMutex );
231 const PxProfileEventName* theNames = mEventNames.begin();
232 uint32_t theEventCount = uint32_t(mEventNames.size());
233 return PxProfileNames( theEventCount, theNames );
234 }
235
236 virtual void release()
237 {
238 PX_PROFILE_DELETE( mWrapper.getAllocator(), this );
239 }
240
241 //Implementation chaining the buffer flush to our clients
242 virtual void handleBufferFlush( const uint8_t* inData, uint32_t inLength )
243 {
244 TLockType theLocker( mMutex );
245
246 uint32_t clientCount = mZoneClients.size();
247 for( uint32_t idx =0; idx < clientCount; ++idx )
248 mZoneClients[idx]->handleBufferFlush( inData, inLength );
249 }
250 //Happens if something removes all the clients from the manager.
251 virtual void handleClientRemoved() {}
252
253 //Send a profile event, optionally with a context. Events are sorted by thread
254 //and context in the client side.
255 virtual void startEvent( uint16_t inId, uint64_t contextId)
256 {
257 if( mEventsActive )
258 {
259 TZoneEventBufferType::startEvent( inId, contextId );
260 }
261 }
262 virtual void stopEvent( uint16_t inId, uint64_t contextId)
263 {
264 if( mEventsActive )
265 {
266 TZoneEventBufferType::stopEvent( inId, contextId );
267 }
268 }
269
270 virtual void startEvent( uint16_t inId, uint64_t contextId, uint32_t threadId)
271 {
272 if( mEventsActive )
273 {
274 TZoneEventBufferType::startEvent( inId, contextId, threadId );
275 }
276 }
277 virtual void stopEvent( uint16_t inId, uint64_t contextId, uint32_t threadId )
278 {
279 if( mEventsActive )
280 {
281 TZoneEventBufferType::stopEvent( inId, contextId, threadId );
282 }
283 }
284
285 virtual void atEvent(uint16_t inId, uint64_t contextId, uint32_t threadId, uint64_t start, uint64_t stop)
286 {
287 if (mEventsActive)
288 {
289 TZoneEventBufferType::startEvent(inId, threadId, contextId, 0, 0, start);
290 TZoneEventBufferType::stopEvent(inId, threadId, contextId, 0, 0, stop);
291 }
292 }
293
299 virtual void eventValue( uint16_t inId, uint64_t contextId, int64_t inValue )
300 {
301 if( mEventsActive )
302 {
303 TZoneEventBufferType::eventValue( inId, contextId, inValue );
304 }
305 }
306 virtual void flushProfileEvents()
307 {
308 TZoneEventBufferType::flushProfileEvents();
309 }
310 };
311
312}}
313#endif
314
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE ConstIterator begin() const
Definition PxArray.h:176
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE void replaceWithLast(uint32_t i)
Definition PxArray.h:345
Definition PxHashInternals.h:453
Definition PxMutex.h:89
Definition PxBasicTemplates.h:67
Definition PxProfileEventBuffer.h:109
Client handles the data when an event buffer flushes. This data can be parsed (PxProfileEventHandler....
Definition PxProfileEventBufferClient.h:39
virtual void handleClientRemoved()=0
Happens if something removes all the clients from the manager.
Helper class to encapsulate the reflection allocator.
Definition PxProfileAllocatorWrapper.h:71
Client handles new profile event add.
Definition PxProfileEventBufferClient.h:64
The profiling system was setup in the expectation that there would be several systems that each had i...
Definition PxProfileZoneManager.h:86
virtual void removeProfileZone(PxProfileZone &inSDK)=0
Removes profile zone from the manager.
The profiling system was setup in the expectation that there would be several systems that each had i...
Definition PxPvdProfileZone.h:67
Definition PxProfileZoneImpl.h:59
virtual PxProfileNames getProfileNames() const
Returns profile event names.
Definition PxProfileZoneImpl.h:228
virtual void flushEventIdNameMap()
Specifies that it is a safe point to flush read-write name map into read-only map....
Definition PxProfileZoneImpl.h:122
virtual void stopEvent(uint16_t inId, uint64_t contextId)
Send a stop profile event, optionally with a context. Events are sorted by thread and context in the ...
Definition PxProfileZoneImpl.h:262
virtual void startEvent(uint16_t inId, uint64_t contextId)
Send a start profile event, optionally with a context. Events are sorted by thread and context in the...
Definition PxProfileZoneImpl.h:255
void addClient(PxProfileZoneClient &inClient)
Adds new client.
Definition PxProfileZoneImpl.h:201
virtual void stopEvent(uint16_t inId, uint64_t contextId, uint32_t threadId)
Send a stop profile event, optionally with a context. Events are sorted by thread and context in the ...
Definition PxProfileZoneImpl.h:277
const char * getName()
Get profile zone name.
Definition PxProfileZoneImpl.h:195
virtual void handleClientRemoved()
Happens if something removes all the clients from the manager.
Definition PxProfileZoneImpl.h:251
virtual void startEvent(uint16_t inId, uint64_t contextId, uint32_t threadId)
Send a start profile event, optionally with a context. Events are sorted by thread and context in the...
Definition PxProfileZoneImpl.h:270
virtual void eventValue(uint16_t inId, uint64_t contextId, int64_t inValue)
Definition PxProfileZoneImpl.h:299
virtual uint16_t getEventIdsForNames(const char **inNames, uint32_t inLen)
Reserve a contiguous set of profile event ids for a set of names.
Definition PxProfileZoneImpl.h:140
void removeClient(PxProfileZoneClient &inClient)
Removes a client.
Definition PxProfileZoneImpl.h:208
virtual uint16_t getEventIdForName(const char *inName)
Get or create a new event id for a given name. If you pass in a previously defined event name (includ...
Definition PxProfileZoneImpl.h:135
virtual void release()
Release the profile zone.
Definition PxProfileZoneImpl.h:236
virtual bool hasClients() const
Check if manager has clients.
Definition PxProfileZoneImpl.h:223
virtual void flushProfileEvents()
Flush profile events. Sends the profile event buffer to hooked clients.
Definition PxProfileZoneImpl.h:306
virtual void setProfileZoneManager(PxProfileZoneManager *inMgr)
Set profile zone manager for the zone.
Definition PxProfileZoneImpl.h:183
virtual PxProfileZoneManager * getProfileZoneManager()
Get profile zone manager for the zone.
Definition PxProfileZoneImpl.h:188
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxProfileContextProviderImpl.h:37
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152
A event id structure. Optionally includes information about if the event was enabled at compile time.
Definition PxProfileEventId.h:38
Mapping from event id to name.
Definition PxProfileEventNames.h:38
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:171
Aggregator of event id -> name mappings.
Definition PxProfileEventNames.h:54
Simple event filter that enables all events.
Definition PxProfileZoneImpl.h:45
Definition PxProfileScopedMutexLock.h:41