RavEngine
Loading...
Searching...
No Matches
PxProfileEventBuffer.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_EVENT_BUFFER_H
30#define PX_PROFILE_EVENT_BUFFER_H
31
32#include "PxProfileEvents.h"
33#include "PxProfileEventSerialization.h"
34#include "PxProfileDataBuffer.h"
35#include "PxProfileContextProvider.h"
36
37#include "foundation/PxTime.h"
38
39namespace physx { namespace profile {
40
104 template<typename TContextProvider,
105 typename TMutex,
106 typename TScopedLock,
107 typename TEventFilter>
108 class EventBuffer : public DataBuffer<TMutex, TScopedLock>
109 {
110 public:
112 typedef TContextProvider TContextProviderType;
113 typedef TEventFilter TEventFilterType;
114 typedef typename TBaseType::TMutexType TMutexType;
115 typedef typename TBaseType::TScopedLockType TScopedLockType;
119
120 private:
121 EventContextInformation mEventContextInformation;
122 uint64_t mLastTimestamp;
123 TContextProvider mContextProvider;
124 TEventFilterType mEventFilter;
125
126 public:
127 EventBuffer(PxAllocatorCallback* inFoundation
128 , uint32_t inBufferFullAmount
129 , const TContextProvider& inProvider
130 , TMutexType* inBufferMutex
131 , const TEventFilterType& inEventFilter )
132 : TBaseType( inFoundation, inBufferFullAmount, inBufferMutex, "struct physx::profile::ProfileEvent" )
133 , mLastTimestamp( 0 )
134 , mContextProvider( inProvider )
135 , mEventFilter( inEventFilter )
136 {
137 memset(&mEventContextInformation,0,sizeof(EventContextInformation));
138 }
139
140 TContextProvider& getContextProvider() { return mContextProvider; }
141
142 PX_FORCE_INLINE void startEvent(uint16_t inId, uint32_t threadId, uint64_t contextId, uint8_t cpuId, uint8_t threadPriority, uint64_t inTimestamp)
143 {
144 TScopedLockType lock(TBaseType::mBufferMutex);
145 if ( mEventFilter.isEventEnabled( inId ) )
146 {
147 StartEvent theEvent;
148 theEvent.init( threadId, contextId, cpuId, threadPriority, inTimestamp );
149 doAddProfileEvent( inId, theEvent );
150 }
151 }
152
153 PX_FORCE_INLINE void startEvent(uint16_t inId, uint64_t contextId)
154 {
155 PxProfileEventExecutionContext ctx( mContextProvider.getExecutionContext() );
156 startEvent( inId, ctx.mThreadId, contextId, ctx.mCpuId, static_cast<uint8_t>(ctx.mThreadPriority), PxTime::getCurrentCounterValue() );
157 }
158
159 PX_FORCE_INLINE void startEvent(uint16_t inId, uint64_t contextId, uint32_t threadId)
160 {
161 startEvent( inId, threadId, contextId, 0, 0, PxTime::getCurrentCounterValue() );
162 }
163
164 PX_FORCE_INLINE void stopEvent(uint16_t inId, uint32_t threadId, uint64_t contextId, uint8_t cpuId, uint8_t threadPriority, uint64_t inTimestamp)
165 {
166 TScopedLockType lock(TBaseType::mBufferMutex);
167 if ( mEventFilter.isEventEnabled( inId ) )
168 {
169 StopEvent theEvent;
170 theEvent.init( threadId, contextId, cpuId, threadPriority, inTimestamp );
171 doAddProfileEvent( inId, theEvent );
172 }
173 }
174
175 PX_FORCE_INLINE void stopEvent(uint16_t inId, uint64_t contextId)
176 {
177 PxProfileEventExecutionContext ctx( mContextProvider.getExecutionContext() );
178 stopEvent( inId, ctx.mThreadId, contextId, ctx.mCpuId, static_cast<uint8_t>(ctx.mThreadPriority), PxTime::getCurrentCounterValue() );
179 }
180
181 PX_FORCE_INLINE void stopEvent(uint16_t inId, uint64_t contextId, uint32_t threadId)
182 {
183 stopEvent( inId, threadId, contextId, 0, 0, PxTime::getCurrentCounterValue() );
184 }
185
186 inline void eventValue( uint16_t inId, uint64_t contextId, int64_t inValue )
187 {
188 eventValue( inId, mContextProvider.getThreadId(), contextId, inValue );
189 }
190
191 inline void eventValue( uint16_t inId, uint32_t threadId, uint64_t contextId, int64_t inValue )
192 {
193 TScopedLockType lock( TBaseType::mBufferMutex );
194 EventValue theEvent;
195 theEvent.init( inValue, contextId, threadId );
196 EventHeader theHeader( static_cast<uint8_t>( getEventType<EventValue>() ), inId );
197 //set the header relative timestamp;
198 EventValue& theType( theEvent );
199 theType.setupHeader( theHeader );
200 sendEvent( theHeader, theType );
201 }
202
203 void flushProfileEvents()
204 {
205 TBaseType::flushEvents();
206 }
207
208 void release()
209 {
210 PX_PROFILE_DELETE( TBaseType::mWrapper.mUserFoundation, this );
211 }
212 protected:
213 //Clears the cache meaning event compression
214 //starts over again.
215 //only called when the buffer mutex is held
216 void clearCachedData()
217 {
218 mEventContextInformation.setToDefault();
219 mLastTimestamp = 0;
220 }
221
222 template<typename TProfileEventType>
223 PX_FORCE_INLINE void doAddProfileEvent(uint16_t eventId, const TProfileEventType& inType)
224 {
225 TScopedLockType lock(TBaseType::mBufferMutex);
226 if (mEventContextInformation == inType.mContextInformation)
227 doAddEvent(static_cast<uint8_t>(inType.getRelativeEventType()), eventId, inType.getRelativeEvent());
228 else
229 {
230 mEventContextInformation = inType.mContextInformation;
231 doAddEvent( static_cast<uint8_t>( getEventType<TProfileEventType>() ), eventId, inType );
232 }
233 }
234
235 template<typename TDataType>
236 PX_FORCE_INLINE void doAddEvent(uint8_t inEventType, uint16_t eventId, const TDataType& inType)
237 {
238 EventHeader theHeader( inEventType, eventId );
239 //set the header relative timestamp;
240 TDataType& theType( const_cast<TDataType&>( inType ) );
241 uint64_t currentTs = inType.getTimestamp();
242 theType.setupHeader(theHeader, mLastTimestamp);
243 mLastTimestamp = currentTs;
244 sendEvent( theHeader, theType );
245 }
246
247 template<typename TDataType>
248 PX_FORCE_INLINE void sendEvent( EventHeader& inHeader, TDataType& inType )
249 {
250 uint32_t sizeToWrite = sizeof(inHeader) + inType.getEventSize(inHeader);
251 PX_UNUSED(sizeToWrite);
252
253 uint32_t writtenSize = inHeader.streamify( TBaseType::mSerializer );
254 writtenSize += inType.streamify(TBaseType::mSerializer, inHeader);
255
256 PX_ASSERT(writtenSize == sizeToWrite);
257
258 if ( TBaseType::mDataArray.size() >= TBaseType::mBufferFullAmount )
259 flushProfileEvents();
260
261 }
262
263 };
264}}
265#endif
266
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
Definition PxProfileDataBuffer.h:41
Definition PxProfileEventBuffer.h:109
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxProfileEvents.h:322
Definition PxProfileEvents.h:169
Definition PxProfileEvents.h:491
Definition PxProfileContextProvider.h:35
Helper class to encapsulate the named allocator.
Definition PxProfileAllocatorWrapper.h:115
Definition PxProfileEvents.h:457
Definition PxProfileEvents.h:475