Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ StereoCamTrigger stereoCamTrigger(60);

ConfigLayer config_layer;

#ifdef PROFILER
Profiler prof;
#endif

SensorManager sensor_manager;
EstimatorManager estimator_manager;
Expand Down
41 changes: 20 additions & 21 deletions src/utils/profiler.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
#include "profiler.hpp"
#include <cstring>

#ifdef PROFILE
#ifdef PROFILER
static DMAMEM Profiler::profiler_section_t sections[PROF_MAX_SECTIONS] = {0};
#endif

void Profiler::clear() {
#ifdef PROFILE
#ifdef PROFILER
for (auto &sec : sections)
sec = profiler_section_t();
#endif
}

void Profiler::begin(const char *name) {
#ifdef PROFILE
#ifdef PROFILER
// 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)) {
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);
sections[i].name[PROF_MAX_NAME] = '\0';// ensure null termination
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;
}
}
#endif
}

void Profiler::end(const char *name) {
#ifdef PROFILE
#ifdef PROFILER
// find section by name
for(uint32_t i = 0; i < PROF_MAX_SECTIONS; i++) {
if(strcmp(sections[i].name, name) == 0) {
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) {
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(sections[i].count == PROF_MAX_TIMES)
{
if (sections[i].count == PROF_MAX_TIMES) {
sections[i].count = 0;
sections[i].overflowed = 1;
}
Expand All @@ -53,11 +52,11 @@ void Profiler::end(const char *name) {
}

void Profiler::print(const char *name) {
#ifdef PROFILE
#ifdef PROFILER
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) {
Expand All @@ -67,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)
if (delta < min)
min = delta;
if(delta > max)
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,
(trueCount == 0 ? 0 : sum / trueCount));
Expand Down
8 changes: 4 additions & 4 deletions src/utils/profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#define PROFILER_H

// Use this flag to toggle profiling globally.
#define PROFILE
// #define PROFILER

#include <Arduino.h>

#define PROF_MAX_SECTIONS 8 // max number of active profiling sections
#define PROF_MAX_SECTIONS 4 // 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 (1000) // max number of start/end times per section

/// @brief Object for profiling sections of code.
struct Profiler {
Expand All @@ -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 + 1] = {0};// +1 extra space for null terminator
char name[PROF_MAX_NAME + 1] = {0}; // +1 extra space for null terminator
};

/// @brief clear all profiling sections
Expand Down