-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CPU utilization #208
base: dev
Are you sure you want to change the base?
CPU utilization #208
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good first step, but stay vigilant of redundant comments. Usually we comment why not how as the code already does that for us 😁
// Can ccheck by creating vApplicationTickHook and comparing # tick and | ||
// # idle cycles << todo | ||
|
||
if (xPortGetCoreID() == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be cleaner to make the variable an array and index into it with the output of the function to increment the variable. Just make sure it always succeeds or else you need to add error checking.
// For ISR to handle (?) | ||
BaseType_t xHigherPriorityTaskWoken = pdFALSE; | ||
|
||
// Which core? (assumption 1 cycle == 1 tick) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FreeRTOS tick is triggered by some kind of SysTick timer (in our case I believe it's TIMER0
.
#define TIMER_DIVIDER 80 // Divide Base by 80 | ||
|
||
|
||
// Timer group 0, timer 0 configuration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See how these comments are redundant? Even the last field wouldn't need a comment if you use a descriptive constant name.
|
||
#include <stdio.h> | ||
|
||
#define TIMER_DIVIDER 80 // Divide Base by 80 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment is self-evident, would you mind #define
ing constants for an expression as to how you arrived to this value?
|
||
// Let run for 5 sec | ||
// Interrupts can be called here to run other tasks | ||
// What if interrupt task runs for more than 5 secs? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Watchdog should trip.
// Calculate total ticks/cycles in 1 sec | ||
uint64_t ticks_elapsed = end_time - start_time; | ||
|
||
float core0_utilization = 100.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should use ticks to calculate this over cpu cycles since that's much harder to keep track of. There is a FreeRTOS config #define
that sets the SysTick frequency in Hz. ESP-IDF is quirky and sets this in the sdkconfig
and I think it generates the config for their custom fork automatically. You can either look at the file manually or run the menuconfig
target to see this.
printf("Core 1 CPU Utilization: %.2f\n", core1_utilization); | ||
|
||
// Reset idle cycles | ||
core0_idle_cycles = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for discarding these? Ideally we keep a global running count and a sliding window which we would average so that we have current stats and stats for the duration of the power cycle.
.intr_type = TIMER_INTR_DISABLE, // No timer interrupts | ||
.counter_dir = TIMER_COUNT_UP, // Count up | ||
.auto_reload = TIMER_AUTORELOAD_EN, // Auto-reload | ||
.divider = TIMER_DIVIDER // Set divider for 1ms (assumption that base |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do expand this into a proper #define
you wouldn't need to make this assumption! FreeRTOS should have a config #define
that has the CPU frequency.
uint64_t ticks_elapsed = end_time - start_time; | ||
|
||
float core0_utilization = 100.0 | ||
- ((float) core0_idle_cycles / ticks_elapsed) * 100.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also try to keep the CPU utilization similar to how Linux reports it (as a float between 0 and 1 per core).
Added method to measure and print cpu utilization