Skip to content
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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft

CPU utilization #208

wants to merge 1 commit into from

Conversation

atkwon
Copy link
Contributor

@atkwon atkwon commented Jul 22, 2024

Added method to measure and print cpu utilization

Copy link
Member

@jacobkoziej jacobkoziej left a 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) {
Copy link
Member

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)
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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 #defineing 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?
Copy link
Member

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
Copy link
Member

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;
Copy link
Member

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
Copy link
Member

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;
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants