RT-thread CPU usage Calculation

Source: Internet
Author: User

CPU usage is generally a matter of concern to us. Here we use the hook function of the idle thread to count the CPU usage and print it through the serial port.
First, we set the hook function in the initialization thread and add many "tasks" to the LED thread to increase the usage and facilitate statistics, then, the CPU usage is printed every one second. The CPU usage is reversed Based on the CPU idle rate.

#include <rtthread.h>#include <rthw.h>#define CPU_USAGE_CALC_TICK    10#define CPU_USAGE_LOOP        100static rt_uint8_t  cpu_usage_major = 0, cpu_usage_minor= 0;static rt_uint32_t total_count = 0;static void cpu_usage_idle_hook(void){    rt_tick_t tick;    rt_uint32_t count;    volatile rt_uint32_t loop;    if (total_count == 0)    {        /* get total count */        rt_enter_critical();        tick = rt_tick_get();        while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)        {            total_count ++;            loop = 0;            while (loop < CPU_USAGE_LOOP) loop ++;        }        rt_exit_critical();    }    count = 0;    /* get CPU usage */    tick = rt_tick_get();    while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)    {        count ++;        loop  = 0;        while (loop < CPU_USAGE_LOOP) loop ++;    }    /* calculate major and minor */    if (count < total_count)    {        count = total_count - count;        cpu_usage_major = (count * 100) / total_count;        cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;    }    else    {        total_count = count;        /* no CPU usage */        cpu_usage_major = 0;        cpu_usage_minor = 0;    }}void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor){    RT_ASSERT(major != RT_NULL);    RT_ASSERT(minor != RT_NULL);    *major = cpu_usage_major;    *minor = cpu_usage_minor;}void cpu_usage_init(void){    /* set idle thread hook */    rt_thread_idle_sethook(cpu_usage_idle_hook);}

As mentioned above, the system's heartbeat clock speed is too fast, which will increase the CPU burden. We can verify it here.
To change the system tick time to 1 ms, then you will find that the CPU usage has increased from 24% to 87% !!, For example:

RT-thread CPU usage Calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.