From fa1930773f2178fa6a6f646bae23ba5b4c57d43c Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Wed, 13 May 2015 22:26:51 +0200 Subject: [PATCH] Rewrite the frame profiler --- profiler/profiler.cpp | 62 ++++++++++++++++++++++++++++++++++++------- profiler/profiler.h | 24 ++++++++--------- 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/profiler/profiler.cpp b/profiler/profiler.cpp index 9ec8b89149..4bb6ab3f8a 100644 --- a/profiler/profiler.cpp +++ b/profiler/profiler.cpp @@ -19,14 +19,22 @@ #define UNIFIED_CONST_STR #endif +struct Category { + const char *name; + uint32_t color; +}; + struct CategoryFrame { + CategoryFrame() { + memset(time_taken, 0, sizeof(time_taken)); + } float time_taken[MAX_CATEGORIES]; }; struct Profiler { int frameCount; int historyPos; - double eventStart; + double eventStart[MAX_CATEGORIES]; double curFrameStart; }; @@ -35,50 +43,84 @@ static Category categories[MAX_CATEGORIES]; static CategoryFrame *history; void internal_profiler_init() { + memset(&profiler, 0, sizeof(profiler)); history = new CategoryFrame[HISTORY_SIZE]; for (int i = 0; i < MAX_CATEGORIES; i++) { - categories[i].color = 0x358236 * i; + categories[i].color = 0xFF000000 | (0x358236 * (i+4)); } } -int internal_profiler_enter(const char *section_name) { - for (int i = 0; i < MAX_CATEGORIES; i++) { +int internal_profiler_enter(const char *category_name) { + int i; + for (i = 0; i < MAX_CATEGORIES; i++) { const char *catname = categories[i].name; if (!catname) break; #ifdef UNIFIED_CONST_STR - if (catname == section_name) { + if (catname == category_name) { #else - if (!strcmp(catname, section_name)) { + if (!strcmp(catname, category_name)) { #endif - profiler.eventStart = time_now_d(); + if (profiler.eventStart[i] == 0.0f) { + profiler.eventStart[i] = real_time_now(); + } return i; } } + + if (i < MAX_CATEGORIES && category_name) { + categories[i].name = category_name; + if (profiler.eventStart[i] == 0.0f) { + profiler.eventStart[i] = real_time_now(); + } + return i; + } + return -1; } void internal_profiler_leave(int category) { if (category < 0) return; - double diff = time_now_d() - profiler.eventStart; + double diff = real_time_now() - profiler.eventStart[category]; history[profiler.historyPos].time_taken[category] += (float)diff; + profiler.eventStart[category] = 0.0; } void internal_profiler_end_frame() { profiler.curFrameStart = real_time_now(); profiler.historyPos++; profiler.historyPos &= ~HISTORY_SIZE; + memset(&history[profiler.historyPos], 0, sizeof(history[profiler.historyPos])); } -const char *GetSectionName(int i) { +const char *Profiler_GetCategoryName(int i) { return i >= 0 ? categories[i].name : "N/A"; } -int GetNumSections() { +uint32_t Profiler_GetCategoryColor(int i) { + return i >= 0 ? categories[i].color : 0xFFFF00FF; +} + +int Profiler_GetHistoryLength() { + return HISTORY_SIZE; +} + +int Profiler_GetNumCategories() { for (int i = 0; i < MAX_CATEGORIES; i++) { if (!categories[i].name) return i; } return 0; } + +void Profiler_GetHistory(int category, float *data, int count) { + for (int i = 0; i < HISTORY_SIZE; i++) { + int x = i - count + profiler.historyPos; + if (x < 0) + x += HISTORY_SIZE; + if (x >= HISTORY_SIZE) + x -= HISTORY_SIZE; + data[i] = history[x].time_taken[category]; + } +} diff --git a/profiler/profiler.h b/profiler/profiler.h index e61ffbe194..86baf7956e 100644 --- a/profiler/profiler.h +++ b/profiler/profiler.h @@ -1,25 +1,25 @@ #pragma once -#define USE_PROFILER +#include + +// #define USE_PROFILER #ifdef USE_PROFILER class DrawBuffer; -struct Category { - const char *name; - uint32_t color; -}; - void internal_profiler_init(); void internal_profiler_end_frame(); -int internal_profiler_enter(const char *section); // Returns the category number. +int internal_profiler_enter(const char *category_name); // Returns the category number. void internal_profiler_leave(int category); -float internal_profiler_gethistory(const char *section, float *data, int count); -const char *GetSectionName(int i); -int GetNumSections(); + +const char *Profiler_GetCategoryName(int i); +uint32_t Profiler_GetCategoryColor(int i); +int Profiler_GetNumCategories(); +int Profiler_GetHistoryLength(); +void Profiler_GetHistory(int i, float *data, int count); class ProfileThis { public: @@ -36,13 +36,11 @@ private: #define PROFILE_INIT() internal_profiler_init(); #define PROFILE_THIS_SCOPE(cat) ProfileThis _profile_scoped(cat); #define PROFILE_END_FRAME() internal_profiler_end_frame(); -#define PROFILE_GET_HISTORY(section, data, count) internal_profiler_gethistory(section, data, count); #else #define PROFILE_INIT() -#define PROFILE_THIS_SCOPE(cat) ProfileThis _profile_scoped(cat); +#define PROFILE_THIS_SCOPE(cat) #define PROFILE_END_FRAME() -#define PROFILE_GET_HISTORY(section, data, count) internal_profiler_gethistory(section, data, count); #endif