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
  • I'm calling ME_addition() inside the main() function's while loop (at line 30). I'll include a code snippet below:

    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("Power Management example");
    
        lfclk_config();
    
    		uint32_t err_code = app_timer_init(); // drv_rtc.c:108 "RTC: initialized."
        APP_ERROR_CHECK(err_code);
    
    #if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
        APP_SCHED_INIT(APP_SCHED_MAX_EVENT_SIZE, APP_SCHED_QUEUE_SIZE);
    #endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    	
        bsp_configuration();
    
    		ret_code_t ret_code = nrf_pwr_mgmt_init(); // nrf_pwr_mgmt.c:326 "Init"
        APP_ERROR_CHECK(ret_code);
    
        while (true)
        {
    			
    #if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
            app_sched_execute();
    #endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    
    			
    		ME_addition();
    			
            if (NRF_LOG_PROCESS() == false)
            {
                nrf_pwr_mgmt_run();
            }
        }
    }

    Thanks

  • Hi,

    What do you measure if you don't add the addition? What is the baseline CPU usage without adding line 30,

    regards

    Jared 

  • Baseline CPU usage without line 30 is 0% (or ~0.3% with floating point representation).

  • 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

Related