Class Profiler

Nested Relationships

Nested Types

Class Documentation

class Profiler

An object that stores information about the time it takes to run code. User can add an item to the profiler by pushing a profile to the profiler, running their function, then popping the profile id from the profiler after the function has finished.

The PROFILE macro allows you to interact with the profiler without using the profiler’s functions directly. For example, the PROFILE can be used as follows:

void foo()
{
}

void bar()
{
     PROFILE(profiler, foo, ());
}

void baz()
{
     PROFILE(profiler, bar, ());
}

In the example above, profiler is a pointer to a valid Profiler class. If you call baz, it will add the profile “bar” and “foo” to the profiler.

The profiler is limited in size to MAX_PROFILED_ELEMENTS. Once a element is registered with the profiler, the profiler will keep track of the min, max, and rolling average time (in microseconds) it takes to run the code associated with the profile. The profiled elements can then be inspected using a debugger, or in the future can be accessed via the terminal serial (not yet implemented).

One known limitation is this profiler doesn’t handle recursion well. For example, consider the following:

void foo(int i)
{
     if (i == 0) return;
     PROFILE(profiler, foo, (i - 1));
}

In this example, the PROFILE macro will overwrite the entry in the profiler in a way that makes the profiler information not useful for the foo function, so it is recommended that you do not use the PROFILE macro for a recursive call.

Public Functions

Profiler(tap::Drivers *drivers)
std::size_t push(const char *profile)

“Push” a profile to the profiler. Can be a new profile that the profiler has never seen or a profile that is already in the profiler. Starts the stopwatch timer associated with the profile.

Parameters:

profile[in] The name of the profile. Should be a unique const char * (i.e. string comparison is not used, instead raw pointer comparison is used).

Returns:

A “key” that then must be passed to the pop function to stop the stopwatch timer and update the profiles associated ProfilerData.

void pop(std::size_t key)

“Pops” a profile data, to stop the stopwatch that is timing how long some code takes to run.

Parameters:

key[in] The key that was returned by push associated with the profile that you would like to stop timing.

inline ProfilerData getData(std::size_t key)
Returns:

The data associated with some particular key.

inline void reset(std::size_t key)

Reset the ProfilerData associated with some particular key.

Public Static Attributes

static std::size_t MAX_PROFILED_ELEMENTS = 128

Max number of profiles that the profiler can store information about.

static float AVG_LOW_PASS_ALPHA = 0.01f

Low pass alpha to be used when averaging time it takes for some code to run.

struct ProfilerData

Stores profile information.

Public Functions

inline ProfilerData()
inline explicit ProfilerData(const char *name)
inline void reset()

Resets the long term profile storage information.

Public Members

const char *name = nullptr

Name of the profile.

uint32_t min = UINT32_MAX

Min value, in microseconds, ever recorded by the profiler.

uint32_t max = 0

Max value, in microseconds, ever recorded by the profiler.

float avg = 0

Average value, in microseconds, averaged using a low pass filter.

uint32_t prevPushedTime = 0

Value used to measure a “dt” between pushing and popping the profile from the profiler.