From 85b4d4cb736d78245a557d3924a0e8d0978fbe95 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sat, 30 Aug 2025 17:13:19 -0600 Subject: [PATCH 01/11] cleared out profiler --- src/utils/profiler.cpp | 83 ------------------------------------------ src/utils/profiler.hpp | 35 ------------------ 2 files changed, 118 deletions(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index a715a398..98597963 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -1,85 +1,2 @@ #include "profiler.hpp" -/// @brief Array of profiling sections. -#ifdef PROFILE -static DMAMEM Profiler::profiler_section_t sections[PROF_MAX_SECTIONS] = { 0 }; -#endif - - -void Profiler::clear() { -#ifdef PROFILE - for (auto& sec : sections) { - // initialize each section so the sections arent filled with DMAMEM junk - sec = profiler_section_t(); - } -#endif -} - -void Profiler::begin(const char* name) { -#ifdef PROFILE - // search for a section by name or an empty slot - for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if (strncmp(sections[i].name, name, PROF_MAX_NAME) == 0 || (sections[i].count == 0 && sections[i].overflow == 0)) { - // start this section - strncpy(sections[i].name, name, PROF_MAX_NAME); - sections[i].name[PROF_MAX_NAME] = '\0'; // ensure null termination - if (sections[i].count < PROF_MAX_TIMES) { - sections[i].start_times[sections[i].count] = micros(); - sections[i].started = 1; // set this section as "started" - } - return; - } - } -#endif -} - -void Profiler::end(const char* name) { -#ifdef PROFILE - // find the section by name and add an end time - for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if (strncmp(sections[i].name, name, PROF_MAX_NAME) == 0) { - uint32_t count = sections[i].count; - if (count < PROF_MAX_TIMES) { - sections[i].end_times[count] = micros(); - sections[i].count++; - sections[i].started = 0; // set this section as "finished" - // if this value just overflowed, or it has reached the max time count, - // mark it as overflowed and reset to 0 - if (sections[i].count == 0 || sections[i].count == PROF_MAX_TIMES) { - sections[i].overflow = 1; - sections[i].count = 0; - } - } - return; - } - } -#endif -} - -void Profiler::print(const char* name) { -#ifdef PROFILE - uint32_t min = UINT32_MAX; - uint32_t max = 0; - uint32_t sum = 0; - - for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if (strncmp(sections[i].name, name, PROF_MAX_NAME) != 0) continue; - // find actual count since this value can overflow/max out - uint32_t actual_count = sections[i].overflow ? PROF_MAX_TIMES : sections[i].count; - for (unsigned int j = 0; j < actual_count; j++) { - // if end() was not called when we decide to print, ignore the "bad" section reading - if (sections[i].started && j == sections[i].count) continue; - - uint32_t delta = sections[i].end_times[j] - sections[i].start_times[j]; - // sum all deltas to be averaged upon printing - sum += delta; - if (delta < min) min = delta; - if (delta > max) max = delta; - } - // print stats - Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", - name, min, max, sum / actual_count); - return; - } -#endif -} diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index d7857d4a..c3b872fc 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -12,41 +12,6 @@ /// @brief Object for profiling sections of code. struct Profiler { - /// @brief Constructor for the profiler. - Profiler() { - clear(); - } - - /// @brief Data structure for a profiling section. - struct profiler_section_t { - /// @brief Start time for each profiling section. - uint32_t start_times[PROF_MAX_TIMES] = { 0 }; - /// @brief End time for each profiling section. - uint32_t end_times[PROF_MAX_TIMES] = { 0 }; - /// @brief Number of start/end times recorded (how many deltas can be calculated). - uint16_t count = 0; - /// @brief Flag on whether count has overflowed or not - uint8_t overflow : 7; - /// @brief Flag on whether this section had begin() called on it - uint8_t started : 1; - /// @brief A unique name to identify the section. - char name[PROF_MAX_NAME + 1] = { 0 }; // extra for null terminator - }; - - /// @brief Clear all profiling sections. - void clear(); - - /// @brief Start a profiling section. - /// @param name A unique name to identify the section. - void begin(const char* name); - - /// @brief End a profiling section. - /// @param name The name of the section to end. - void end(const char* name); - - /// @brief Print stats for a particular profiling section. - /// @param name The name of the section to print statistics for. - void print(const char* name); }; extern Profiler prof; // Global profiler From 9b34d1bba3ffdb24fb518899cec0ec7f6f944d99 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sun, 31 Aug 2025 15:49:37 -0600 Subject: [PATCH 02/11] recreated profiler --- src/utils/profiler.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++ src/utils/profiler.hpp | 35 ++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index 98597963..d96224dd 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -1,2 +1,86 @@ #include "profiler.hpp" +#ifdef PROFILE +static DMAMEM Profiler::profiler_section_t sections[PROF_MAX_SECTIONS] = {0}; +#endif + +void Profiler::clear() { +#ifdef PROFILE + for(auto& sec : sections) + sec = profiler_section_t(); +#endif +} + +void Profiler::begin(const char* name) { +#ifdef PROFILE + //find section by name or first empty slot + for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if(strcmp(sections[i].name, name) == 0 || sections[i].count == 0 && sections[i].overflow == 0) { + //start section + sections[i].name = name; + if(sections[i].count < PROF_MAX_TIMES) + { + sections[i].start_times[sections[i].count] = micros(); + sections[i].started = 1;//label section as started + } + return; + } + } +#endif +} + +void Profiler::end(const char* name) { +#ifdef PROFILE + //find section by name + for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if(strcmp(sections[i].name, name) == 0) { + //end section + if(sections[i].count < PROF_MAX_TIMES) { + sections[i].end_times[count] = micros(); + sections[i].count++; + sections[i].started = 0;//label section as finished + + //if count is now at limit, reset count and label as overflowed + if(sections[i].count == PROF_MAX_TIMES) + { + sections[i].count = 0; + sections[i].overflowed = 1; + } + } + return; + } + } +#endif +} + +void Profiler::print(const char* name) { +#ifdef PROFILE + uint32_t sum = 0; + uint32_t min = UINT32_MAX; + uint32_t max = 0; + + //find section by name + for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if(strcmp(sections[i].name, name) == 0) { + //calculate values + uint32_t trueCount = sections[i].overflowed ? PROF_MAX_SECTIONS : sections[i].count; + for(uint32_t j = 0; j < trueCount; j++) { + //if the last run was started and not ended, ignore it + if(sections[i].started && j == sections[i].count) + continue; + + uint32_t delta = sections[i].end_times[j] - sections[i].end_times[j]; + sum += delta; + if(delta < min) + min = delta; + if(delta > max) + max = delta; + } + + //print values + Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us", name, min, max, sum/trueCount); + return; + } + } +#endif +} diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index c3b872fc..019acce1 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -12,6 +12,41 @@ /// @brief Object for profiling sections of code. struct Profiler { + /// @brief Constructor for the profiler. + Profiler() { + clear(); + } + + // @brief Data structure for a profiling section + struct profiler_section_t { + /// @brief Start time for each profiling section + uint32_t start_times[PROF_MAX_TIMES] = {0}; + /// @brief End time for each profiling section + uint32_t end_times[PROF_MAX_TIMES] = {0}; + /// @brief Number of start/end times recorded + uint16_t count = 0; + /// @brief Label on if count has overflowed + uint8_t overflowed = 0; + /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet + uint8_t started = 0; + /// @brief Name for each section + char name[PROF_MAX_NAME] = {0}; + }; + + /// @brief clear all profiling sections + void clear(); + + /// @brief Start a profiling section + /// @param name A unique name to identify the section + void begin(const char* name); + + /// @brief End a profiling section + /// @param name The name of the section to end + void end(const char* name); + + /// @brief Print stats for a profiling section + /// @param name The name of the section to print stats for + void print(const char* name); }; extern Profiler prof; // Global profiler From 6011214ada317e12519972ec8b949774f2080567 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sat, 6 Sep 2025 17:55:43 -0600 Subject: [PATCH 03/11] added profiler calls to main --- src/main.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e0fec56f..3d300c0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,8 +88,11 @@ int main() { Serial.begin(115200); // the serial monitor is actually always active (for debug use Serial.println & tycmd) debug.begin(SerialUSB1); - + + prof.begin("logo"); print_logo(); + prof.end("logo"); + prof.print("logo"); // check to see if there is a crash report, and if so, print it repeatedly over Serial // in the future, we'll send this directly over comms @@ -454,4 +457,4 @@ int main() { } return 0; -} \ No newline at end of file +} From 59c227de4f728df61413a94529e478b3b849abea Mon Sep 17 00:00:00 2001 From: Rocco Antoniou Date: Sat, 6 Sep 2025 18:05:50 -0600 Subject: [PATCH 04/11] error fixes, prints now --- src/utils/profiler.cpp | 9 +++++---- src/utils/profiler.hpp | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index d96224dd..34b9fc4b 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -1,4 +1,5 @@ #include "profiler.hpp" +#include #ifdef PROFILE static DMAMEM Profiler::profiler_section_t sections[PROF_MAX_SECTIONS] = {0}; @@ -15,9 +16,9 @@ void Profiler::begin(const char* name) { #ifdef PROFILE //find section by name or first empty slot for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if(strcmp(sections[i].name, name) == 0 || sections[i].count == 0 && sections[i].overflow == 0) { + if(strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { //start section - sections[i].name = name; + strncpy(sections[i].name, name, PROF_MAX_NAME-1); if(sections[i].count < PROF_MAX_TIMES) { sections[i].start_times[sections[i].count] = micros(); @@ -36,7 +37,7 @@ void Profiler::end(const char* name) { if(strcmp(sections[i].name, name) == 0) { //end section if(sections[i].count < PROF_MAX_TIMES) { - sections[i].end_times[count] = micros(); + sections[i].end_times[sections[i].count] = micros(); sections[i].count++; sections[i].started = 0;//label section as finished @@ -78,7 +79,7 @@ void Profiler::print(const char* name) { } //print values - Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us", name, min, max, sum/trueCount); + Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, sum/trueCount); return; } } diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index 019acce1..6a99256e 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -2,7 +2,7 @@ #define PROFILER_H // Use this flag to toggle profiling globally. -// #define PROFILE +#define PROFILE #include From 2301db9254611cba5b3435e6365dc995b7b45905 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sat, 6 Sep 2025 18:14:06 -0600 Subject: [PATCH 05/11] fixed delta calculation --- src/utils/profiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index 34b9fc4b..c167f7ed 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -70,7 +70,7 @@ void Profiler::print(const char* name) { if(sections[i].started && j == sections[i].count) continue; - uint32_t delta = sections[i].end_times[j] - sections[i].end_times[j]; + uint32_t delta = sections[i].end_times[j] - sections[i].start_times[j]; sum += delta; if(delta < min) min = delta; From afad5f2ed0a2ed23f173588d7e8c7dac93fc1cd5 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sat, 6 Sep 2025 18:27:55 -0600 Subject: [PATCH 06/11] more thorough prof testing --- src/main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3d300c0d..f9be042f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,10 +89,14 @@ int main() { Serial.begin(115200); // the serial monitor is actually always active (for debug use Serial.println & tycmd) debug.begin(SerialUSB1); - prof.begin("logo"); print_logo(); - prof.end("logo"); - prof.print("logo"); + + for(uint_32t i = 0; i < 5100; i++) { + prof.begin("tester"); + delayMicroseconds(random(150, 250)); + prof.end("tester"); + } + prof.print("tester"); // check to see if there is a crash report, and if so, print it repeatedly over Serial // in the future, we'll send this directly over comms From 1ed3a926cf50b6580151a4d69003fdd5de394bd2 Mon Sep 17 00:00:00 2001 From: Rocco Antoniou Date: Mon, 8 Sep 2025 11:48:23 -0600 Subject: [PATCH 07/11] formatted --- src/utils/profiler.cpp | 109 ++++++++++++++++++++--------------------- src/utils/profiler.hpp | 40 +++++++-------- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index c167f7ed..13832e84 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -7,81 +7,80 @@ static DMAMEM Profiler::profiler_section_t sections[PROF_MAX_SECTIONS] = {0}; void Profiler::clear() { #ifdef PROFILE - for(auto& sec : sections) + for (auto &sec : sections) sec = profiler_section_t(); #endif } -void Profiler::begin(const char* name) { +void Profiler::begin(const char *name) { #ifdef PROFILE - //find section by name or first empty slot - for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if(strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { - //start section - strncpy(sections[i].name, name, PROF_MAX_NAME-1); - if(sections[i].count < PROF_MAX_TIMES) - { - sections[i].start_times[sections[i].count] = micros(); - sections[i].started = 1;//label section as started - } - return; - } + // find section by name or first empty slot + for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if (strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { + // start section + strncpy(sections[i].name, name, PROF_MAX_NAME - 1); + if (sections[i].count < PROF_MAX_TIMES) { + sections[i].start_times[sections[i].count] = micros(); + sections[i].started = 1; // label section as started + } + return; + } } #endif } -void Profiler::end(const char* name) { +void Profiler::end(const char *name) { #ifdef PROFILE - //find section by name - for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if(strcmp(sections[i].name, name) == 0) { - //end section - if(sections[i].count < PROF_MAX_TIMES) { - sections[i].end_times[sections[i].count] = micros(); - sections[i].count++; - sections[i].started = 0;//label section as finished - - //if count is now at limit, reset count and label as overflowed - if(sections[i].count == PROF_MAX_TIMES) - { - sections[i].count = 0; - sections[i].overflowed = 1; - } - } - return; - } + // find section by name + for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if (strcmp(sections[i].name, name) == 0) { + // end section + if (sections[i].count < PROF_MAX_TIMES) { + sections[i].end_times[sections[i].count] = micros(); + sections[i].count++; + sections[i].started = 0; // label section as finished + + // if count is now at limit, reset count and label as overflowed + if (sections[i].count == PROF_MAX_TIMES) { + sections[i].count = 0; + sections[i].overflowed = 1; + } + } + return; + } } #endif } -void Profiler::print(const char* name) { +void Profiler::print(const char *name) { #ifdef PROFILE uint32_t sum = 0; uint32_t min = UINT32_MAX; uint32_t max = 0; - - //find section by name - for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if(strcmp(sections[i].name, name) == 0) { - //calculate values - uint32_t trueCount = sections[i].overflowed ? PROF_MAX_SECTIONS : sections[i].count; - for(uint32_t j = 0; j < trueCount; j++) { - //if the last run was started and not ended, ignore it - if(sections[i].started && j == sections[i].count) - continue; - uint32_t delta = sections[i].end_times[j] - sections[i].start_times[j]; - sum += delta; - if(delta < min) - min = delta; - if(delta > max) - max = delta; - } + // find section by name + for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if (strcmp(sections[i].name, name) == 0) { + // calculate values + uint32_t trueCount = sections[i].overflowed ? PROF_MAX_SECTIONS : sections[i].count; + for (uint32_t j = 0; j < trueCount; j++) { + // if the last run was started and not ended, ignore it + if (sections[i].started && j == sections[i].count) + continue; + + uint32_t delta = sections[i].end_times[j] - sections[i].start_times[j]; + sum += delta; + if (delta < min) + min = delta; + if (delta > max) + max = delta; + } - //print values - Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, sum/trueCount); - return; - } + // print values + Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, + sum / trueCount); + return; + } } #endif } diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index 6a99256e..f325f045 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -6,31 +6,29 @@ #include -#define PROF_MAX_SECTIONS 8 // max number of active profiling sections +#define PROF_MAX_SECTIONS 8 // max number of active profiling sections #define PROF_MAX_NAME 16 // max length of section name -#define PROF_MAX_TIMES (5000) // max number of start/end times per section +#define PROF_MAX_TIMES (5000) // max number of start/end times per section /// @brief Object for profiling sections of code. struct Profiler { /// @brief Constructor for the profiler. - Profiler() { - clear(); - } + Profiler() { clear(); } // @brief Data structure for a profiling section struct profiler_section_t { /// @brief Start time for each profiling section - uint32_t start_times[PROF_MAX_TIMES] = {0}; - /// @brief End time for each profiling section - uint32_t end_times[PROF_MAX_TIMES] = {0}; - /// @brief Number of start/end times recorded - uint16_t count = 0; - /// @brief Label on if count has overflowed - uint8_t overflowed = 0; - /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet - uint8_t started = 0; - /// @brief Name for each section - char name[PROF_MAX_NAME] = {0}; + uint32_t start_times[PROF_MAX_TIMES] = {0}; + /// @brief End time for each profiling section + uint32_t end_times[PROF_MAX_TIMES] = {0}; + /// @brief Number of start/end times recorded + uint16_t count = 0; + /// @brief Label on if count has overflowed + uint8_t overflowed = 0; + /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet + uint8_t started = 0; + /// @brief Name for each section + char name[PROF_MAX_NAME] = {0}; }; /// @brief clear all profiling sections @@ -38,17 +36,17 @@ struct Profiler { /// @brief Start a profiling section /// @param name A unique name to identify the section - void begin(const char* name); + void begin(const char *name); /// @brief End a profiling section /// @param name The name of the section to end - void end(const char* name); + void end(const char *name); /// @brief Print stats for a profiling section /// @param name The name of the section to print stats for - void print(const char* name); + void print(const char *name); }; -extern Profiler prof; // Global profiler +extern Profiler prof; // Global profiler -#endif // PROFILER_H +#endif // PROFILER_H From 9c945475fadf672ec098c3f47ad667af9b179d8a Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Tue, 9 Sep 2025 19:42:39 -0600 Subject: [PATCH 08/11] reduced load by only using one array --- src/utils/profiler.cpp | 69 +++++++++++++++++++++--------------------- src/utils/profiler.hpp | 24 +++++++-------- 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index 13832e84..1f0d2bea 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -14,40 +14,39 @@ void Profiler::clear() { void Profiler::begin(const char *name) { #ifdef PROFILE - // find section by name or first empty slot - for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if (strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { - // start section - strncpy(sections[i].name, name, PROF_MAX_NAME - 1); - if (sections[i].count < PROF_MAX_TIMES) { - sections[i].start_times[sections[i].count] = micros(); - sections[i].started = 1; // label section as started - } - return; - } + //find section by name or first empty slot + for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if(strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { + //start section + strncpy(sections[i].name, name, PROF_MAX_NAME-1); + sections[i].start_time = micros(); + sections[i].started = 1;//label section as started + return; + } } #endif } void Profiler::end(const char *name) { #ifdef PROFILE - // find section by name - for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if (strcmp(sections[i].name, name) == 0) { - // end section - if (sections[i].count < PROF_MAX_TIMES) { - sections[i].end_times[sections[i].count] = micros(); - sections[i].count++; - sections[i].started = 0; // label section as finished - - // if count is now at limit, reset count and label as overflowed - if (sections[i].count == PROF_MAX_TIMES) { - sections[i].count = 0; - sections[i].overflowed = 1; - } - } - return; - } + //find section by name + for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { + if(strcmp(sections[i].name, name) == 0) { + //end section + if(sections[i].count < PROF_MAX_TIMES) { + sections[i].time_lengths[sections[i].count] = micros() - sections[i].start_time; + sections[i].count++; + sections[i].started = 0;//label section as finished + + //if count is now at limit, reset count and label as overflowed + if(sections[i].count == PROF_MAX_TIMES) + { + sections[i].count = 0; + sections[i].overflowed = 1; + } + } + return; + } } #endif } @@ -68,13 +67,13 @@ void Profiler::print(const char *name) { if (sections[i].started && j == sections[i].count) continue; - uint32_t delta = sections[i].end_times[j] - sections[i].start_times[j]; - sum += delta; - if (delta < min) - min = delta; - if (delta > max) - max = delta; - } + uint32_t delta = sections[i].time_lengths[j]; + sum += delta; + if(delta < min) + min = delta; + if(delta > max) + max = delta; + } // print values Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index f325f045..f9c3178c 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -17,18 +17,18 @@ struct Profiler { // @brief Data structure for a profiling section struct profiler_section_t { - /// @brief Start time for each profiling section - uint32_t start_times[PROF_MAX_TIMES] = {0}; - /// @brief End time for each profiling section - uint32_t end_times[PROF_MAX_TIMES] = {0}; - /// @brief Number of start/end times recorded - uint16_t count = 0; - /// @brief Label on if count has overflowed - uint8_t overflowed = 0; - /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet - uint8_t started = 0; - /// @brief Name for each section - char name[PROF_MAX_NAME] = {0}; + /// @brief the time lengths of each profiling section + uint32_t time_lengths[PROF_MAX_TIMES] = {0}; + /// @brief Start time for the current profiling section + uint32_t start_time = 0; + /// @brief Number of start/end times recorded + uint16_t count = 0; + /// @brief Label on if count has overflowed + uint8_t overflowed = 0; + /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet + uint8_t started = 0; + /// @brief Name for each section + char name[PROF_MAX_NAME] = {0}; }; /// @brief clear all profiling sections From 5b2dedb6fc8e2acb18d97892540824403dd02ea3 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Tue, 9 Sep 2025 20:09:07 -0600 Subject: [PATCH 09/11] fixed vim formatting --- src/utils/profiler.cpp | 64 +++++++++++++++++++++--------------------- src/utils/profiler.hpp | 22 +++++++-------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index 1f0d2bea..4bda4114 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -17,12 +17,12 @@ void Profiler::begin(const char *name) { //find section by name or first empty slot for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { if(strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { - //start section - strncpy(sections[i].name, name, PROF_MAX_NAME-1); - sections[i].start_time = micros(); - sections[i].started = 1;//label section as started - return; - } + //start section + strncpy(sections[i].name, name, PROF_MAX_NAME-1); + sections[i].start_time = micros(); + sections[i].started = 1;//label section as started + return; + } } #endif } @@ -31,22 +31,22 @@ void Profiler::end(const char *name) { #ifdef PROFILE //find section by name for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { - if(strcmp(sections[i].name, name) == 0) { - //end section - if(sections[i].count < PROF_MAX_TIMES) { - sections[i].time_lengths[sections[i].count] = micros() - sections[i].start_time; - sections[i].count++; - sections[i].started = 0;//label section as finished - - //if count is now at limit, reset count and label as overflowed - if(sections[i].count == PROF_MAX_TIMES) - { - sections[i].count = 0; - sections[i].overflowed = 1; - } - } - return; - } + if(strcmp(sections[i].name, name) == 0) { + //end section + if(sections[i].count < PROF_MAX_TIMES) { + sections[i].time_lengths[sections[i].count] = micros() - sections[i].start_time; + sections[i].count++; + sections[i].started = 0;//label section as finished + + //if count is now at limit, reset count and label as overflowed + if(sections[i].count == PROF_MAX_TIMES) + { + sections[i].count = 0; + sections[i].overflowed = 1; + } + } + return; + } } #endif } @@ -56,7 +56,7 @@ void Profiler::print(const char *name) { uint32_t sum = 0; uint32_t min = UINT32_MAX; uint32_t max = 0; - + // find section by name for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { if (strcmp(sections[i].name, name) == 0) { @@ -66,15 +66,15 @@ void Profiler::print(const char *name) { // if the last run was started and not ended, ignore it if (sections[i].started && j == sections[i].count) continue; - - uint32_t delta = sections[i].time_lengths[j]; - sum += delta; - if(delta < min) - min = delta; - if(delta > max) - max = delta; - } - + + uint32_t delta = sections[i].time_lengths[j]; + sum += delta; + if(delta < min) + min = delta; + if(delta > max) + max = delta; + } + // print values Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, sum / trueCount); diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index f9c3178c..a34fb526 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -17,18 +17,18 @@ struct Profiler { // @brief Data structure for a profiling section struct profiler_section_t { - /// @brief the time lengths of each profiling section - uint32_t time_lengths[PROF_MAX_TIMES] = {0}; + /// @brief the time lengths of each profiling section + uint32_t time_lengths[PROF_MAX_TIMES] = {0}; /// @brief Start time for the current profiling section - uint32_t start_time = 0; - /// @brief Number of start/end times recorded - uint16_t count = 0; - /// @brief Label on if count has overflowed - uint8_t overflowed = 0; - /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet - uint8_t started = 0; - /// @brief Name for each section - char name[PROF_MAX_NAME] = {0}; + uint32_t start_time = 0; + /// @brief Number of start/end times recorded + uint16_t count = 0; + /// @brief Label on if count has overflowed + uint8_t overflowed = 0; + /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet + uint8_t started = 0; + /// @brief Name for each section + char name[PROF_MAX_NAME] = {0}; }; /// @brief clear all profiling sections From cd1bddfd8c352a4f8e51d40df71429bfd6b0bfe9 Mon Sep 17 00:00:00 2001 From: rcschwartz18-lgtm Date: Sat, 13 Sep 2025 17:19:11 -0600 Subject: [PATCH 10/11] fixed issues --- src/main.cpp | 7 ------- src/utils/profiler.cpp | 21 +++++++++++---------- src/utils/profiler.hpp | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ccc8af5d..46fdf79d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,13 +88,6 @@ int main() { debug.begin(SerialUSB1); print_logo(); - - for(uint_32t i = 0; i < 5100; i++) { - prof.begin("tester"); - delayMicroseconds(random(150, 250)); - prof.end("tester"); - } - prof.print("tester"); // check to see if there is a crash report, and if so, print it repeatedly over Serial // in the future, we'll send this directly over comms diff --git a/src/utils/profiler.cpp b/src/utils/profiler.cpp index 4bda4114..50328e3f 100644 --- a/src/utils/profiler.cpp +++ b/src/utils/profiler.cpp @@ -14,13 +14,14 @@ void Profiler::clear() { void Profiler::begin(const char *name) { #ifdef PROFILE - //find section by name or first empty slot + // find section by name or first empty slot for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { if(strcmp(sections[i].name, name) == 0 || (sections[i].count == 0 && sections[i].overflowed == 0)) { - //start section - strncpy(sections[i].name, name, PROF_MAX_NAME-1); + // start section + strncpy(sections[i].name, name, PROF_MAX_NAME); + sections[i].name[PROF_MAX_NAME] = '\0';// ensure null termination sections[i].start_time = micros(); - sections[i].started = 1;//label section as started + sections[i].started = 1;// label section as started return; } } @@ -29,16 +30,16 @@ void Profiler::begin(const char *name) { void Profiler::end(const char *name) { #ifdef PROFILE - //find section by name + // find section by name for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { if(strcmp(sections[i].name, name) == 0) { - //end section + // end section if(sections[i].count < PROF_MAX_TIMES) { sections[i].time_lengths[sections[i].count] = micros() - sections[i].start_time; sections[i].count++; - sections[i].started = 0;//label section as finished + sections[i].started = 0;// label section as finished - //if count is now at limit, reset count and label as overflowed + // if count is now at limit, reset count and label as overflowed if(sections[i].count == PROF_MAX_TIMES) { sections[i].count = 0; @@ -61,7 +62,7 @@ void Profiler::print(const char *name) { for (uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) { if (strcmp(sections[i].name, name) == 0) { // calculate values - uint32_t trueCount = sections[i].overflowed ? PROF_MAX_SECTIONS : sections[i].count; + uint32_t trueCount = sections[i].overflowed ? PROF_MAX_TIMES : sections[i].count; for (uint32_t j = 0; j < trueCount; j++) { // if the last run was started and not ended, ignore it if (sections[i].started && j == sections[i].count) @@ -77,7 +78,7 @@ void Profiler::print(const char *name) { // print values Serial.printf("Profiling for: %s\n Min: %u us\n Max: %u us\n Avg: %u us\n", name, min, max, - sum / trueCount); + (trueCount == 0 ? 0 : sum / trueCount)); return; } } diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index a34fb526..70c9352e 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -28,7 +28,7 @@ struct Profiler { /// @brief Label on if a begin() has been called and the corresponding end() hasn't yet uint8_t started = 0; /// @brief Name for each section - char name[PROF_MAX_NAME] = {0}; + char name[PROF_MAX_NAME + 1] = {0};// +1 extra space for null terminator }; /// @brief clear all profiling sections From c929f6e3398fe7d33188fddd043ae5318477c006 Mon Sep 17 00:00:00 2001 From: Rocco Antoniou Date: Tue, 16 Sep 2025 11:46:15 -0600 Subject: [PATCH 11/11] fix doxygen typo --- src/main.cpp | 2 +- src/utils/profiler.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e6e90e24..c29e3923 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,7 +96,7 @@ int main() { Serial.begin(115200); // the serial monitor is actually always active (for // debug use Serial.println & tycmd) debug.begin(SerialUSB1); - + print_logo(); // check to see if there is a crash report, and if so, print it repeatedly diff --git a/src/utils/profiler.hpp b/src/utils/profiler.hpp index 70c9352e..f987ae3c 100644 --- a/src/utils/profiler.hpp +++ b/src/utils/profiler.hpp @@ -15,7 +15,7 @@ struct Profiler { /// @brief Constructor for the profiler. Profiler() { clear(); } - // @brief Data structure for a profiling section + /// @brief Data structure for a profiling section struct profiler_section_t { /// @brief the time lengths of each profiling section uint32_t time_lengths[PROF_MAX_TIMES] = {0};