| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
JPH_SUPPRESS_WARNINGS_STD_BEGIN |
| 8 |
#include <mutex> |
| 9 |
#include <chrono> |
| 10 |
JPH_SUPPRESS_WARNINGS_STD_END |
| 11 |
|
| 12 |
#include <Jolt/Core/NonCopyable.h> |
| 13 |
#include <Jolt/Core/TickCounter.h> |
| 14 |
#include <Jolt/Core/UnorderedMap.h> |
| 15 |
|
| 16 |
#if defined(JPH_EXTERNAL_PROFILE) |
| 17 |
|
| 18 |
JPH_NAMESPACE_BEGIN |
| 19 |
|
| 20 |
#ifdef JPH_SHARED_LIBRARY |
| 21 |
|
| 22 |
using ProfileStartMeasurementFunction = void (*)(const char *inName, uint32 inColor, uint8 *ioUserData); |
| 23 |
using ProfileEndMeasurementFunction = void (*)(uint8 *ioUserData); |
| 24 |
|
| 25 |
JPH_EXPORT extern ProfileStartMeasurementFunction ProfileStartMeasurement; |
| 26 |
JPH_EXPORT extern ProfileEndMeasurementFunction ProfileEndMeasurement; |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class alignas(16) ExternalProfileMeasurement : public NonCopyable |
| 35 |
{ |
| 36 |
public: |
| 37 |
|
| 38 |
#ifdef JPH_SHARED_LIBRARY |
| 39 |
JPH_INLINE ExternalProfileMeasurement(const char *inName, uint32 inColor = 0) { ProfileStartMeasurement(inName, inColor, mUserData); } |
| 40 |
JPH_INLINE ~ExternalProfileMeasurement() { ProfileEndMeasurement(mUserData); } |
| 41 |
#else |
| 42 |
ExternalProfileMeasurement(const char *inName, uint32 inColor = 0); |
| 43 |
~ExternalProfileMeasurement(); |
| 44 |
#endif |
| 45 |
|
| 46 |
private: |
| 47 |
uint8 mUserData[64]; |
| 48 |
}; |
| 49 |
|
| 50 |
JPH_NAMESPACE_END |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
JPH_SUPPRESS_WARNING_PUSH |
| 57 |
JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") |
| 58 |
|
| 59 |
|
| 60 |
#define JPH_PROFILE_START(name) |
| 61 |
#define JPH_PROFILE_END() |
| 62 |
#define JPH_PROFILE_THREAD_START(name) |
| 63 |
#define JPH_PROFILE_THREAD_END() |
| 64 |
#define JPH_PROFILE_NEXTFRAME() |
| 65 |
#define JPH_PROFILE_DUMP(...) |
| 66 |
|
| 67 |
|
| 68 |
#define JPH_PROFILE_TAG2(line) profile##line |
| 69 |
#define JPH_PROFILE_TAG(line) JPH_PROFILE_TAG2(line) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#define JPH_PROFILE(...) ExternalProfileMeasurement JPH_PROFILE_TAG(__LINE__)(__VA_ARGS__) |
| 81 |
|
| 82 |
|
| 83 |
#define JPH_PROFILE_FUNCTION() JPH_PROFILE(JPH_FUNCTION_NAME) |
| 84 |
|
| 85 |
JPH_SUPPRESS_WARNING_POP |
| 86 |
|
| 87 |
#elif defined(JPH_PROFILE_ENABLED) |
| 88 |
|
| 89 |
JPH_NAMESPACE_BEGIN |
| 90 |
|
| 91 |
class ProfileSample; |
| 92 |
class ProfileThread; |
| 93 |
|
| 94 |
|
| 95 |
class JPH_EXPORT Profiler : public NonCopyable |
| 96 |
{ |
| 97 |
public: |
| 98 |
JPH_OVERRIDE_NEW_DELETE |
| 99 |
|
| 100 |
|
| 101 |
Profiler() { UpdateReferenceTime(); } |
| 102 |
|
| 103 |
|
| 104 |
void NextFrame(); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
void Dump(const string_view &inTag = string_view()); |
| 109 |
|
| 110 |
|
| 111 |
void AddThread(ProfileThread *inThread); |
| 112 |
|
| 113 |
|
| 114 |
void RemoveThread(ProfileThread *inThread); |
| 115 |
|
| 116 |
|
| 117 |
static Profiler * sInstance; |
| 118 |
|
| 119 |
private: |
| 120 |
|
| 121 |
struct ThreadSamples |
| 122 |
{ |
| 123 |
String mThreadName; |
| 124 |
ProfileSample * mSamplesBegin; |
| 125 |
ProfileSample * mSamplesEnd; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
class Aggregator |
| 130 |
{ |
| 131 |
public: |
| 132 |
|
| 133 |
Aggregator(const char *inName) : mName(inName) { } |
| 134 |
|
| 135 |
|
| 136 |
void AccumulateMeasurement(uint64 inCyclesInCallWithChildren) |
| 137 |
{ |
| 138 |
mCallCounter++; |
| 139 |
mTotalCyclesInCallWithChildren += inCyclesInCallWithChildren; |
| 140 |
mMinCyclesInCallWithChildren = min(inCyclesInCallWithChildren, mMinCyclesInCallWithChildren); |
| 141 |
mMaxCyclesInCallWithChildren = max(inCyclesInCallWithChildren, mMaxCyclesInCallWithChildren); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
bool operator < (const Aggregator &inRHS) const |
| 146 |
{ |
| 147 |
return mTotalCyclesInCallWithChildren > inRHS.mTotalCyclesInCallWithChildren; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
const char * mName; |
| 152 |
|
| 153 |
|
| 154 |
uint32 mCallCounter = 0; |
| 155 |
uint64 mTotalCyclesInCallWithChildren = 0; |
| 156 |
uint64 mMinCyclesInCallWithChildren = 0xffffffffffffffffUL; |
| 157 |
uint64 mMaxCyclesInCallWithChildren = 0; |
| 158 |
}; |
| 159 |
|
| 160 |
using Threads = Array<ThreadSamples>; |
| 161 |
using Aggregators = Array<Aggregator>; |
| 162 |
using KeyToAggregator = UnorderedMap<const char *, size_t>; |
| 163 |
|
| 164 |
|
| 165 |
static void sAggregate(int inDepth, uint32 inColor, ProfileSample *&ioSample, const ProfileSample *inEnd, Aggregators &ioAggregators, KeyToAggregator &ioKeyToAggregator); |
| 166 |
|
| 167 |
|
| 168 |
void UpdateReferenceTime(); |
| 169 |
|
| 170 |
|
| 171 |
uint64 GetProcessorTicksPerSecond() const; |
| 172 |
|
| 173 |
|
| 174 |
void DumpInternal(); |
| 175 |
void DumpChart(const char *inTag, const Threads &inThreads, const KeyToAggregator &inKeyToAggregators, const Aggregators &inAggregators); |
| 176 |
|
| 177 |
std::mutex mLock; |
| 178 |
uint64 mReferenceTick; |
| 179 |
std::chrono::high_resolution_clock::time_point mReferenceTime; |
| 180 |
Array<ProfileThread *> mThreads; |
| 181 |
bool mDump = false; |
| 182 |
String mDumpTag; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
class alignas(16) JPH_EXPORT_GCC_BUG_WORKAROUND ProfileSample : public NonCopyable |
| 187 |
{ |
| 188 |
public: |
| 189 |
JPH_OVERRIDE_NEW_DELETE |
| 190 |
|
| 191 |
const char * mName; |
| 192 |
uint32 mColor; |
| 193 |
uint8 mDepth; |
| 194 |
uint8 mUnused[3]; |
| 195 |
uint64 mStartCycle; |
| 196 |
uint64 mEndCycle; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
class ProfileThread : public NonCopyable |
| 201 |
{ |
| 202 |
public: |
| 203 |
JPH_OVERRIDE_NEW_DELETE |
| 204 |
|
| 205 |
|
| 206 |
inline ProfileThread(const string_view &inThreadName); |
| 207 |
inline ~ProfileThread(); |
| 208 |
|
| 209 |
static const uint cMaxSamples = 65536; |
| 210 |
|
| 211 |
String mThreadName; |
| 212 |
ProfileSample mSamples[cMaxSamples]; |
| 213 |
uint mCurrentSample = 0; |
| 214 |
|
| 215 |
#ifdef JPH_SHARED_LIBRARY |
| 216 |
JPH_EXPORT static void sSetInstance(ProfileThread *inInstance); |
| 217 |
JPH_EXPORT static ProfileThread *sGetInstance(); |
| 218 |
#else |
| 219 |
static inline void sSetInstance(ProfileThread *inInstance) { sInstance = inInstance; } |
| 220 |
static inline ProfileThread *sGetInstance() { return sInstance; } |
| 221 |
|
| 222 |
private: |
| 223 |
static thread_local ProfileThread *sInstance; |
| 224 |
#endif |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
class JPH_EXPORT ProfileMeasurement : public NonCopyable |
| 229 |
{ |
| 230 |
public: |
| 231 |
|
| 232 |
inline ProfileMeasurement(const char *inName, uint32 inColor = 0); |
| 233 |
inline ~ProfileMeasurement(); |
| 234 |
|
| 235 |
private: |
| 236 |
ProfileSample * mSample; |
| 237 |
ProfileSample mTemp; |
| 238 |
|
| 239 |
static bool sOutOfSamplesReported; |
| 240 |
}; |
| 241 |
|
| 242 |
JPH_NAMESPACE_END |
| 243 |
|
| 244 |
#include "Profiler.inl" |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
JPH_SUPPRESS_WARNING_PUSH |
| 251 |
JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") |
| 252 |
|
| 253 |
|
| 254 |
#define JPH_PROFILE_START(name) do { Profiler::sInstance = new Profiler; JPH_PROFILE_THREAD_START(name); } while (false) |
| 255 |
|
| 256 |
|
| 257 |
#define JPH_PROFILE_END() do { JPH_PROFILE_THREAD_END(); delete Profiler::sInstance; Profiler::sInstance = nullptr; } while (false) |
| 258 |
|
| 259 |
|
| 260 |
#define JPH_PROFILE_THREAD_START(name) do { if (Profiler::sInstance) ProfileThread::sSetInstance(new ProfileThread(name)); } while (false) |
| 261 |
|
| 262 |
|
| 263 |
#define JPH_PROFILE_THREAD_END() do { delete ProfileThread::sGetInstance(); ProfileThread::sSetInstance(nullptr); } while (false) |
| 264 |
|
| 265 |
|
| 266 |
#define JPH_PROFILE_TAG2(line) profile##line |
| 267 |
#define JPH_PROFILE_TAG(line) JPH_PROFILE_TAG2(line) |
| 268 |
#define JPH_PROFILE(...) ProfileMeasurement JPH_PROFILE_TAG(__LINE__)(__VA_ARGS__) |
| 269 |
|
| 270 |
|
| 271 |
#define JPH_PROFILE_FUNCTION() JPH_PROFILE(JPH_FUNCTION_NAME) |
| 272 |
|
| 273 |
|
| 274 |
#define JPH_PROFILE_NEXTFRAME() Profiler::sInstance->NextFrame() |
| 275 |
|
| 276 |
|
| 277 |
#define JPH_PROFILE_DUMP(...) Profiler::sInstance->Dump(__VA_ARGS__) |
| 278 |
|
| 279 |
JPH_SUPPRESS_WARNING_POP |
| 280 |
|
| 281 |
#else |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
JPH_SUPPRESS_WARNING_PUSH |
| 288 |
JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") |
| 289 |
|
| 290 |
#define JPH_PROFILE_START(name) |
| 291 |
#define JPH_PROFILE_END() |
| 292 |
#define JPH_PROFILE_THREAD_START(name) |
| 293 |
#define JPH_PROFILE_THREAD_END() |
| 294 |
#define JPH_PROFILE(...) |
| 295 |
#define JPH_PROFILE_FUNCTION() |
| 296 |
#define JPH_PROFILE_NEXTFRAME() |
| 297 |
#define JPH_PROFILE_DUMP(...) |
| 298 |
|
| 299 |
JPH_SUPPRESS_WARNING_POP |
| 300 |
|
| 301 |
#endif |