Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nRF52840 CPU Usage Accuracy and Explanation

Hello,

I'm a beginner using the nRF52840 DK, sdk v16.0, and Keil on Windows. I'm currently working on the provided pwr_mgmt example. My first goal was to display the CPU Usage percentage as a float number, which I accomplished by editing the nrf_pwr_mgmt.c file (inside the pwr_mgmt_cpu_usage_monitor_update function), such that:

 __STATIC_INLINE void pwr_mgmt_cpu_usage_monitor_update(void)
    {
        uint32_t delta;
        uint32_t ticks;
        //uint8_t  cpu_usage; <- ORIGINAL

		float cpu_usage; // <- ME
		
		
        ticks = app_timer_cnt_get();
        delta = app_timer_cnt_diff_compute(ticks, m_ticks_last);
			
        cpu_usage = (float)(delta - m_ticks_sleeping) / (delta) * 100; // <- ME
			
		//NRF_LOG_INFO("CPU Usage: %u%%", cpu_usage); <- ORIGINAL
		NRF_LOG_INFO("CPU Usage: " NRF_LOG_FLOAT_MARKER "%%", NRF_LOG_FLOAT(cpu_usage)); // <- ME
		
        if (m_max_cpu_usage < cpu_usage)
        {
            m_max_cpu_usage = cpu_usage;
        }

        m_ticks_last        = ticks;
        m_ticks_sleeping    = 0;
    }

I was wondering if this was an accurate way of doing so?

Additionally, I edited the main.c file , such that I included my own function to test the CPU Usage:

// ME
static void ME_addition() {
	int i;
	for (i = 0; i < 1000000; i++){
		uint32_t x = i;
		uint32_t y = i+1;
		uint64_t z = x + y;
	}
	return;
}

I call this inside the main() function and receive ~12.335% CPU Usage. For simple addition, I expected the CPU usage to be somewhat lower for the chip...so, I'm wondering if this is accurate or if I'm potentially missing something in my thinking?

In summary:

1.) What's the best way to display the CPU usage as a percentage with 3 decimal places?

2.) Why am I seeing ~12% usage for addition arithmetic?

Thanks.

Parents Reply Children
  • Hi Alan,

    I tried adding the ME_addition() to the pwr_mgmt example and call it from the while loop just as you have done, and I'm not able to replicate the same on my development kit. My development kit shows 0% after adding this, have you done any other changes to your project? Are you able to replicate this on SDK v17.1.0? 

    regards

    Jared

  • Hi Jared,

    I deleted everything I had done previously and tried my algorithm with the original pwr_mgmt example code. Below is how I implemented it:

    After compiling and flashing to the dev kit, I see the following on my terminal:

    The only changes I made to this project were the addition function implementation and the function call in the main while loop. I'm confused now why the CPU Usage is even higher than before? I tried this code with the dev kit on SDK v16.0.0, but it's my understanding that the results should be the same across SDK versions? Is it possible that you are optimizing for time when you're compiling in the target options?

    For reference, I ran this pwr_mgmt project without the addition function and I saw 0% CPU usage.

    Regards,

    Alan

  • Hi Alan,

    Can you download SES v5.42a and build the example with that IDE and see if it results in the same? 

    Also, use SDK v17.1.0,

    This is what I used when I tested it last time and got 0% with the addition,

    regards

    Jared 

Related