|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <thread> |
| 4 | +#include <zest/time/timer.h> |
| 5 | +#include <zest/math/math.h> |
| 6 | +#include <zest/math/math_utils.h> |
| 7 | +#include <pico/sync.h> |
| 8 | + |
| 9 | +namespace Zest |
| 10 | +{ |
| 11 | + |
| 12 | +class PicoMutex { |
| 13 | +public: |
| 14 | + PicoMutex() |
| 15 | + { |
| 16 | + mutex_init(&_myMutex); |
| 17 | + } |
| 18 | + |
| 19 | + void lock() |
| 20 | + { |
| 21 | + mutex_enter_blocking(&_myMutex); |
| 22 | + } |
| 23 | + void unlock() |
| 24 | + { |
| 25 | + mutex_exit(&_myMutex); |
| 26 | + } |
| 27 | + |
| 28 | +private: |
| 29 | + mutex _myMutex; |
| 30 | +}; |
| 31 | + |
| 32 | +class PicoLockGuard { |
| 33 | +public: |
| 34 | + explicit PicoLockGuard(PicoMutex& mtx) : myMutex(mtx) { // construct and lock |
| 35 | + myMutex.lock(); |
| 36 | + } |
| 37 | + |
| 38 | + ~PicoLockGuard() noexcept { |
| 39 | + myMutex.unlock(); |
| 40 | + } |
| 41 | + |
| 42 | + PicoLockGuard(const PicoLockGuard&) = delete; |
| 43 | + PicoLockGuard& operator=(const PicoLockGuard&) = delete; |
| 44 | + |
| 45 | + PicoMutex& myMutex; |
| 46 | +}; |
| 47 | + |
| 48 | +namespace Profiler |
| 49 | +{ |
| 50 | + |
| 51 | +struct ProfilerEntry |
| 52 | +{ |
| 53 | + // static infos |
| 54 | + const char* szSection; |
| 55 | + const char* szFile; |
| 56 | + int line; |
| 57 | + unsigned int color; |
| 58 | + // infos used for rendering infos |
| 59 | + int level = 0; |
| 60 | + int64_t startTime; |
| 61 | + int64_t endTime; |
| 62 | + uint32_t parent; |
| 63 | +}; |
| 64 | + |
| 65 | +struct FrameThreadInfo |
| 66 | +{ |
| 67 | + uint32_t threadIndex; |
| 68 | + uint32_t activeEntry; |
| 69 | +}; |
| 70 | + |
| 71 | +struct Region |
| 72 | +{ |
| 73 | + std::string name; |
| 74 | + int64_t startTime; |
| 75 | + int64_t endTime; |
| 76 | +}; |
| 77 | + |
| 78 | +struct Frame : Region |
| 79 | +{ |
| 80 | + uint32_t frameThreadCount = 0; |
| 81 | + std::vector<FrameThreadInfo> frameThreads; |
| 82 | +}; |
| 83 | + |
| 84 | +struct ThreadData |
| 85 | +{ |
| 86 | + bool initialized; |
| 87 | + uint32_t callStackDepth = 0; |
| 88 | + uint32_t maxLevel = 0; |
| 89 | + int64_t minTime; |
| 90 | + int64_t maxTime; |
| 91 | + uint32_t currentEntry = 0; |
| 92 | + bool hidden = false; |
| 93 | + std::string name; |
| 94 | + std::vector<ProfilerEntry> entries; |
| 95 | + std::vector<uint32_t> entryStack; |
| 96 | +}; |
| 97 | + |
| 98 | +struct ProfileSettings |
| 99 | +{ |
| 100 | + uint32_t MaxThreads = 120; |
| 101 | + uint32_t MaxCallStack = 20; |
| 102 | + uint32_t MaxEntriesPerThread = 100000; |
| 103 | + uint32_t MaxFrames = 10000; |
| 104 | + uint32_t MaxRegions = 10000; |
| 105 | +}; |
| 106 | + |
| 107 | +void SetProfileSettings(const ProfileSettings& settings); |
| 108 | +void Init(); |
| 109 | +void NewFrame(); |
| 110 | +void NameThread(const char* pszName); |
| 111 | +void SetPaused(bool pause); |
| 112 | +void BeginRegion(); |
| 113 | +void EndRegion(); |
| 114 | +void SetRegionLimit(uint64_t maxTimeNs); |
| 115 | +void PushSectionBase(const char*, uint32_t, const char*, int); |
| 116 | +void PopSection(); |
| 117 | +void HideThread(); |
| 118 | +void Finish(); |
| 119 | + |
| 120 | +struct ProfileScope |
| 121 | +{ |
| 122 | + ProfileScope(const char* szSection, uint32_t color, const char* szFile, int line) |
| 123 | + { |
| 124 | + PushSectionBase(szSection, color, szFile, line); |
| 125 | + } |
| 126 | + ~ProfileScope() |
| 127 | + { |
| 128 | + PopSection(); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +struct RegionScope |
| 133 | +{ |
| 134 | + RegionScope() |
| 135 | + { |
| 136 | + BeginRegion(); |
| 137 | + } |
| 138 | + ~RegionScope() |
| 139 | + { |
| 140 | + EndRegion(); |
| 141 | + } |
| 142 | +}; |
| 143 | +#define PROFILE_COL_LOCK 0xFF0000FF |
| 144 | + |
| 145 | +template <class _Mutex> |
| 146 | +class profile_lock_guard { // class with destructor that unlocks a mutex |
| 147 | +public: |
| 148 | + using mutex_type = _Mutex; |
| 149 | + |
| 150 | + explicit profile_lock_guard(_Mutex& _Mtx, const char* name = "Mutex", const char* szFile = nullptr, int line = 0) : _MyMutex(_Mtx) { // construct and lock |
| 151 | + PushSectionBase(name, PROFILE_COL_LOCK, szFile, line); |
| 152 | + _MyMutex.lock(); |
| 153 | + PopSection(); |
| 154 | + } |
| 155 | + |
| 156 | + profile_lock_guard(_Mutex& _Mtx, std::adopt_lock_t) : _MyMutex(_Mtx) {} // construct but don't lock |
| 157 | + |
| 158 | + ~profile_lock_guard() noexcept { |
| 159 | + _MyMutex.unlock(); |
| 160 | + } |
| 161 | + |
| 162 | + profile_lock_guard(const profile_lock_guard&) = delete; |
| 163 | + profile_lock_guard& operator=(const profile_lock_guard&) = delete; |
| 164 | + |
| 165 | +private: |
| 166 | + _Mutex& _MyMutex; |
| 167 | +}; |
| 168 | + |
| 169 | +#define LOCK_GUARD(var, name) \ |
| 170 | +::Zest::Profiler::profile_lock_guard name##_lock(var, #name, __FILE__, __LINE__) |
| 171 | + |
| 172 | +const glm::vec4& ColorFromName(const char* pszName, const uint32_t len); |
| 173 | + |
| 174 | +} // namespace Profiler |
| 175 | +} // namespace Zest |
| 176 | + |
| 177 | +// PROFILE_SCOPE(MyNameWithoutQuotes) |
| 178 | +#define PROFILE_SCOPE(name) \ |
| 179 | +static const uint32_t name##_color = Zest::ToPackedARGB(Zest::Profiler::ColorFromName(#name, uint32_t(strlen(#name)))); \ |
| 180 | +Zest::Profiler::ProfileScope name##_scope(#name, name##_color, __FILE__, __LINE__); |
| 181 | + |
| 182 | +// PROFILE_SCOPE(char*, ImColor32 bit value) |
| 183 | +#define PROFILE_SCOPE_STR(str, col) \ |
| 184 | +Zest::Profiler::ProfileScope name##_scope(str, col, __FILE__, __LINE__); |
| 185 | + |
| 186 | +// Mark one extra region |
| 187 | +#define PROFILE_REGION(name) \ |
| 188 | +Zest::Profiler::RegionScope name##_region; |
| 189 | + |
| 190 | +// Give a thread a name. |
| 191 | +#define PROFILE_NAME_THREAD(name) \ |
| 192 | +Zest::Profiler::NameThread(#name); |
| 193 | + |
| 194 | +// Hide a thread. Not sure this is tested or works.... |
| 195 | +#define PROFILE_HIDE_THREAD() \ |
| 196 | +Zest::Profiler::HideThread(); |
| 197 | + |
0 commit comments