yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
28758e0e4
master
1#include "slang-performance-profiler.h" 2 3#include "slang-dictionary.h" 4 5namespace Slang 6{ 7class PerformanceProfilerImpl :public PerformanceProfiler 8{ 9public : 10OrderedDictionary < const char * ,FuncProfileInfo > data ; 11 12virtual FuncProfileContext enterFunction (const char * funcName )override 13 { 14auto entry = data .tryGetValue (funcName ); 15if (!entry ) 16 { 17data .add (funcName ,FuncProfileInfo ()); 18entry = data .tryGetValue (funcName ); 19 } 20entry -> invocationCount ++ ; 21FuncProfileContext ctx ; 22ctx .funcName = funcName ; 23ctx .startTime = std::chrono::high_resolution_clock::now (); 24return ctx ; 25 } 26virtual void exitFunction (FuncProfileContext ctx )override 27 { 28auto endTime = std::chrono::high_resolution_clock::now (); 29auto duration = endTime - ctx .startTime ; 30auto entry = data .tryGetValue (ctx .funcName ); 31entry -> duration += duration ; 32 } 33virtual void getResult (StringBuilder & out )override 34 { 35char buffer [512 ]; 36for (const auto & func :data ) 37 { 38auto microseconds = 39 std::chrono::duration_cast < std::chrono::microseconds > (func .value .duration ); 40double milliseconds = microseconds .count () /1000.0 ; 41snprintf ( 42buffer , 43sizeof (buffer ), 44"[*] %30s \t%d \t%8.2fms\n" , 45func .key , 46func .value .invocationCount , 47milliseconds ); 48 49out <<buffer ; 50 } 51 } 52 53 54virtual void clear ()override {data .clear (); } 55virtual void dispose ()override {data = decltype(data )(); } 56}; 57 58PerformanceProfiler * Slang ::PerformanceProfiler ::getProfiler () 59{ 60 thread_localstatic PerformanceProfilerImpl profiler = PerformanceProfilerImpl (); 61return & profiler ; 62} 63 64SlangProfiler ::SlangProfiler (PerformanceProfiler * profiler ) 65{ 66PerformanceProfilerImpl * profilerImpl = static_cast < PerformanceProfilerImpl *> (profiler ); 67size_t entryCount = profilerImpl -> data .getCount (); 68 69m_profilEntries .reserve (entryCount ); 70 71int index = 0 ; 72for (auto func :profilerImpl -> data ) 73 { 74ProfileInfo profileEntry {}; 75size_t strSize = std::min (sizeof (profileEntry .funcName )- 1 ,strlen (func .key )); 76 77if (strSize > 0 ) 78 { 79memcpy (profileEntry .funcName ,func .key ,strSize ); 80 } 81profileEntry .invocationCount = func .value .invocationCount ; 82profileEntry .duration = func .value .duration ; 83 84m_profilEntries .insert (index ,profileEntry ); 85index ++ ; 86 } 87} 88 89ISlangUnknown * SlangProfiler ::getInterface (const Guid & guid ) 90{ 91if (guid == SlangProfiler ::getTypeGuid ()) 92return static_cast < ISlangUnknown *> (this ); 93else 94return nullptr ; 95} 96 97size_t SlangProfiler ::getEntryCount () 98{ 99return m_profilEntries .getCount (); 100} 101 102const char * SlangProfiler ::getEntryName (uint32_t index ) 103{ 104if (index >= (uint32_t )m_profilEntries .getCount ()) 105return nullptr ; 106 107return m_profilEntries [index ].funcName ; 108} 109 110long SlangProfiler ::getEntryTimeMS (uint32_t index ) 111{ 112if (index >= (uint32_t )m_profilEntries .getCount ()) 113return 0 ; 114 115auto milliseconds = 116 std::chrono::duration_cast < std::chrono::milliseconds > (m_profilEntries [index ].duration ); 117return (long )milliseconds .count (); 118} 119 120uint32_t SlangProfiler ::getEntryInvocationTimes (uint32_t index ) 121{ 122if (index >= (uint32_t )m_profilEntries .getCount ()) 123return 0 ; 124 125return m_profilEntries [index ].invocationCount ; 126} 127}// namespace Slang