diff --git a/examples/testbed/framework/json_profiler.cpp b/examples/testbed/framework/json_profiler.cpp index 1f4a38a..ac2415c 100644 --- a/examples/testbed/framework/json_profiler.cpp +++ b/examples/testbed/framework/json_profiler.cpp @@ -75,7 +75,7 @@ void JsonProfiler::EndEvents() m_file = nullptr; } -void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t) +void JsonProfiler::BeginEvent(const char* name, float64 t) { if (!m_writer) { @@ -87,8 +87,8 @@ void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t) float64 scale = 1000.0; m_writer->StartObject(); - m_writer->STRING("pid"); m_writer->Int(pid); - m_writer->STRING("tid"); m_writer->Int(tid); + m_writer->STRING("pid"); m_writer->Int(0); + m_writer->STRING("tid"); m_writer->Int(0); m_writer->STRING("ts"); m_writer->Int64((u64)(t * scale)); m_writer->STRING("ph"); m_writer->String(phase, 1); m_writer->STRING("cat"); m_writer->STRING("physics"); @@ -97,7 +97,7 @@ void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t) m_writer->EndObject(); } -void JsonProfiler::EndEvent(i32 tid, i32 pid, const char* name, float64 t) +void JsonProfiler::EndEvent(const char* name, float64 t) { if (!m_writer) { @@ -109,8 +109,8 @@ void JsonProfiler::EndEvent(i32 tid, i32 pid, const char* name, float64 t) float64 scale = 1000.0; m_writer->StartObject(); - m_writer->STRING("pid"); m_writer->Int(pid); - m_writer->STRING("tid"); m_writer->Int(tid); + m_writer->STRING("pid"); m_writer->Int(0); + m_writer->STRING("tid"); m_writer->Int(0); m_writer->STRING("ts"); m_writer->Int64((u64)(t * scale)); m_writer->STRING("ph"); m_writer->String(phase, 1); m_writer->STRING("cat"); m_writer->STRING("physics"); diff --git a/examples/testbed/framework/json_profiler.h b/examples/testbed/framework/json_profiler.h index 15bcc2c..dbca993 100644 --- a/examples/testbed/framework/json_profiler.h +++ b/examples/testbed/framework/json_profiler.h @@ -43,9 +43,9 @@ public: void EndEvents(); - void BeginEvent(i32 tid, i32 pid, const char* name, float64 time); + void BeginEvent(const char* name, float64 time); - void EndEvent(i32 tid, i32 pid, const char* name, float64 time); + void EndEvent(const char* name, float64 time); private: FILE * m_file; FileWriteStream* m_stream; diff --git a/examples/testbed/framework/main.cpp b/examples/testbed/framework/main.cpp index dad7c74..f47c194 100644 --- a/examples/testbed/framework/main.cpp +++ b/examples/testbed/framework/main.cpp @@ -124,8 +124,7 @@ static void Run() for (u32 i = 0; i < records.Count(); ++i) { const ProfilerRecord& r = records[i]; - - if (r.elapsed > 0.0) + if (r.call != 0) { g_draw->DrawString(b3Color_white, "%s %.4f (%.4f) [ms]", r.name, r.elapsed, r.maxElapsed); } @@ -140,7 +139,7 @@ static void Run() g_profiler->PopEvent(); - g_profiler->End(g_profilerListener); + g_profiler->End(); glfwSwapBuffers(g_window); glfwPollEvents(); diff --git a/examples/testbed/framework/profiler.cpp b/examples/testbed/framework/profiler.cpp index 81b6aec..296db7f 100644 --- a/examples/testbed/framework/profiler.cpp +++ b/examples/testbed/framework/profiler.cpp @@ -35,26 +35,24 @@ void Profiler::PushEvent(const char* name) m_time.Update(); ProfilerEvent e; - e.tid = -1; - e.pid = -1; e.t0 = m_time.GetCurrentMilis(); - e.t1 = 0.0; + e.t1 = -1.0; e.name = name; e.parent = m_top; - ProfilerEvent* back = m_events.Push(e); - B3_ASSERT(back); - m_top = back; + m_events.PushBack(e); + + m_top = &m_events.Back(); } void Profiler::PopEvent() { - B3_ASSERT(m_top); - B3_ASSERT(m_top->t1 == 0.0); - m_time.Update(); + m_top->t1 = m_time.GetCurrentMilis(); - B3_ASSERT(m_top->t1 != 0.0); + + B3_ASSERT(m_top->t1 > m_top->t0); + m_top = m_top->parent; } @@ -65,29 +63,31 @@ void Profiler::Begin() B3_ASSERT(m_top == NULL); } -void Profiler::End(ProfilerListener* listener) +void Profiler::End() { + ProfilerListener* listener = g_profilerListener; + if (listener) { listener->BeginEvents(); } - while (m_events.IsEmpty() == false) + for (u32 i = 0; i < m_events.Count(); ++i) { - ProfilerEvent e = m_events.Front(); - - m_events.Pop(); + ProfilerEvent e = m_events[i]; if (listener) { - listener->BeginEvent(e.tid, e.pid, e.name, e.t0); + listener->BeginEvent(e.name, e.t0); - listener->EndEvent(e.tid, e.pid, e.name, e.t1); + listener->EndEvent(e.name, e.t1); listener->Duration(e.name, e.t1 - e.t0); } } + m_events.Resize(0); + B3_ASSERT(m_events.IsEmpty()); B3_ASSERT(m_top == NULL); diff --git a/examples/testbed/framework/profiler.h b/examples/testbed/framework/profiler.h index 38165cd..aa18bba 100644 --- a/examples/testbed/framework/profiler.h +++ b/examples/testbed/framework/profiler.h @@ -24,17 +24,11 @@ #include #include -// This defines the maximum number of profiler events that can be -// queued per frame until the function Profiler::End is called. -#define MAX_PROFILER_EVENTS 2048 - class ProfilerListener; // A time-stamped profiler event. struct ProfilerEvent { - i32 tid; - i32 pid; const char* name; float64 t0; float64 t1; @@ -56,7 +50,7 @@ public: // The function will report all events in this profiler // to the given event listener in the correct calling order. // This function also flushes the profiler. - void End(ProfilerListener* listener); + void End(); // Add a profiler event to the queue. // You can control the maximum number of profiler events using @@ -67,7 +61,7 @@ public: void PopEvent(); private: b3Time m_time; - b3BoundedQueue m_events; + b3StackArray m_events; ProfilerEvent* m_top; }; @@ -86,19 +80,15 @@ public: virtual void EndEvents() { } // This function is called when a profiler event begins. - virtual void BeginEvent(i32 tid, i32 pid, const char* name, float64 time) + virtual void BeginEvent(const char* name, float64 time) { - B3_NOT_USED(tid); - B3_NOT_USED(pid); B3_NOT_USED(name); B3_NOT_USED(time); } // This function is called when a profiler event ends. - virtual void EndEvent(i32 tid, i32 pid, const char* name, float64 time) + virtual void EndEvent(const char* name, float64 time) { - B3_NOT_USED(tid); - B3_NOT_USED(pid); B3_NOT_USED(name); B3_NOT_USED(time); } diff --git a/examples/testbed/framework/recorder_profiler.cpp b/examples/testbed/framework/recorder_profiler.cpp index 3b21a1a..d597a49 100644 --- a/examples/testbed/framework/recorder_profiler.cpp +++ b/examples/testbed/framework/recorder_profiler.cpp @@ -22,7 +22,7 @@ RecorderProfiler* g_profilerRecorder = nullptr; void RecorderProfiler::BeginEvents() { - m_count = 0; + m_call = 0; for (u32 i = 0; i < m_records.Count(); ++i) { m_records[i].elapsed = 0.0; @@ -43,33 +43,45 @@ void RecorderProfiler::EndEvents() if (r2.call < r1.call) { b3Swap(r1, r2); - break; } } } } -void RecorderProfiler::Add(const char* name, float64 elapsedTime) +ProfilerRecord* RecorderProfiler::FindRecord(const char* name) { - m_count += 1; - for (u32 i = 0; i < m_records.Count(); ++i) { ProfilerRecord& r = m_records[i]; if (r.name == name) { - r.elapsed += elapsedTime; - r.maxElapsed = b3Max(r.maxElapsed, elapsedTime); - r.call = m_count; - return; + return &r; } } + return nullptr; +} + +void RecorderProfiler::Add(const char* name, float64 elapsedTime) +{ + B3_ASSERT(elapsedTime >= 0.0); + + ++m_call; + + ProfilerRecord* fr = FindRecord(name); + if (fr) + { + fr->elapsed += elapsedTime; + fr->maxElapsed = b3Max(fr->maxElapsed, elapsedTime); + fr->call = m_call; + + return; + } ProfilerRecord r; r.name = name; r.elapsed = elapsedTime; r.maxElapsed = elapsedTime; - r.call = m_count; + r.call = m_call; m_records.PushBack(r); } \ No newline at end of file diff --git a/examples/testbed/framework/recorder_profiler.h b/examples/testbed/framework/recorder_profiler.h index 49f2edf..c623851 100644 --- a/examples/testbed/framework/recorder_profiler.h +++ b/examples/testbed/framework/recorder_profiler.h @@ -25,9 +25,9 @@ // An event in the profiler event recorder. struct ProfilerRecord { + const char* name; float64 elapsed; float64 maxElapsed; - const char* name; u32 call; }; @@ -43,10 +43,12 @@ public: void Add(const char* name, float64 elapsedTime); + ProfilerRecord* FindRecord(const char* name); + const b3Array& GetRecords() const { return m_records; } private: - b3StackArray m_records; - u32 m_count; + b3StackArray m_records; // persistent records sorted by call order + u32 m_call; }; extern RecorderProfiler* g_profilerRecorder; diff --git a/examples/testbed/framework/testbed_listener.h b/examples/testbed/framework/testbed_listener.h index ee50dba..847b700 100644 --- a/examples/testbed/framework/testbed_listener.h +++ b/examples/testbed/framework/testbed_listener.h @@ -54,25 +54,24 @@ public: } - void BeginEvent(i32 tid, i32 pid, const char* name, float64 time) override + void BeginEvent(const char* name, float64 time) override { #if (PROFILE_JSON == 1) - m_jsonListener.BeginEvent(tid, pid, name, time); + m_jsonListener.BeginEvent(name, time); +#endif + } + + void EndEvent(const char* name, float64 time) override + { +#if (PROFILE_JSON == 1) + m_jsonListener.EndEvent(name, time); #endif } - void EndEvent(i32 tid, i32 pid, const char* name, float64 time) override + void Duration(const char* name, float64 duration) override { -#if (PROFILE_JSON == 1) - m_jsonListener.EndEvent(tid, pid, name, time); -#endif - - } - - void Duration(const char* name, float64 time) override - { - m_recorderProfiler.Add(name, time); + m_recorderProfiler.Add(name, duration); } RecorderProfiler m_recorderProfiler;