The principle and use of CPU utilization (cpuusage) of the HaaS100 development and debugging series

1. What is cpuusage

cpuusage (that is, CPU utilization, this article uses cpuusage to refer to CPU utilization) usually refers to the proportion of time the CPU is engaged in any work. For example: 90% cpuusage means that the CPU is 90% busy and 10% idle. When the CPU is idle, it does nothing. On the embedded real-time operating system RTOS, it enters the idle state. The idle itself is also a task. It just waits for interrupts and consumes the CPU.

On RTOS, the CPU is used in time slices. For example: Task A runs for 10ms, then switches to task B, B runs for 30ms, then idles for 60ms (that is, enters the idle task to run for 60ms), and then switches back to task A to run in a cycle. If this is the case for a period of time, then the cpuusage during this period is 40% (where the cpuusage of task A is 10%, and the cpuusage of task B is 30%).

The cpuusage can reflect the current busyness of the cpu. The higher the cpuusage, the more programs are running on the device, and vice versa. If the cpuusage has been too high for a period of time, it may be that high-priority tasks have been occupying the CPU to run, causing low-priority tasks to always be unable to get the CPU to run. This design may have problems. Therefore, cpuusage statistics can help us optimize applications.

 

2. How is cpuusage counted on AliOS Things

2.1, cpuusage statistics principle

Task cpuusage: The execution time of the task in the statistical period divided by the statistical period is the task's cpuusage.

 

Total cpuusage: Except idle tasks, the cumulative execution time/statistical period of all tasks in the statistical period. At this time, it can be calculated by 100%-idle task cpuusage.

 

for example:

image.png

Taking the above execution sequence as an example, in the statistical period, task1 is scheduled to be executed once, task2 is scheduled to be executed twice, and the rest of the time is idle task running.

 

 

 

2.2. Accumulation of task running time

After the single statistical time of a single task is determined, the running time of any task can be determined within a period of time—just accumulate the running time of this task during this period.

image.png

Take the task switching in the above figure as an example:

Assume that the time that Task1 has been running at t1 is total_task1, and the time that Task2 has been running at t1 is total_task2;

Then at t2, total_task1 is updated to

total_task1 += t2 - t1;

At t3, total_task2 is updated to

total_task2 += t3 - t2;

With this accumulation, you can get the total running time of tasks that need to be counted within a period of time.

 

3. How to use cpuusage on HaaS100

3.1, open platform configuration

Configure the following macros in the k_config.h file of the corresponding platform:

  1. RHINO_CONFIG_SYS_STATS is configured to 1, to enable the statistics function;
  2. RHINO_CONFIG_HW_COUNT is configured to 1, using a hardware timer (this timer needs to be adapted).
#ifndef RHINO_CONFIG_SYS_STATS
#define RHINO_CONFIG_SYS_STATS               1
#endif

#ifndef RHINO_CONFIG_HW_COUNT
#define RHINO_CONFIG_HW_COUNT                1
#endif

3.2, adapt hardware timer

The time statistics of cpuusage are realized through the high-precision hardware timer that comes with the board. The main frequency of this hardware timer is generally a few MHz to tens of MHz.

The acquisition of the timer value depends on the bsp interface provided by the hardware manufacturer. In order to shield the differences in the underlying hardware, AliOS Things provides a unified interface to acquire the timer value, as follows:

hr_timer_t soc_hr_hw_cnt_get(void);

On the HaaS100 development board, if a developer is interested in the details of the timing benchmark, you can refer to the implementation details of the timer acquisition interface. The code is located at:

platform/board/haas100/config/board.c

AliOS Things code download and description

Note:  We have turned on the platform configuration switch on HaaS100, and adapted the hardware timer.

In other words, the function of cpuusage on HaaS100 is turned on by default and can be used directly!

 

3.3. How to use-through cli command cpuusage

cpuusage is a command that comes with the kernel and does not depend on any app. That is to say, as long as you add the cli component when compiling and execute a command cpuusage under the cli shell, you can start to count the cpuusage of all tasks in the system.

Note: For how to open and use the cli component, please refer to another article-see the link "Portal" at the end of the article "An Easy Start to HaaS100 Diagnostic and Debugging System".

3.3.1, command instructions

cpuusage [-d n] [-t m] 命令启动CPU利用率统计
其中:-d选项用于指定统计周期,单位为ms,默认为1 s;
      -t选项用于指定统计时长,单位为ms,默认为连续运行。
      
举例说明:
cpuusage                   -- 启动一个cpuusage任务,该任务默认每隔1s执行一次统计;
cpuusage -d 3000           -- 启动一个cpuusage任务,该任务默认每隔3s(3000ms)执行一次统计;
cpuusage -d 2000 -t 10000  -- 启动一个cpuusage任务,该任务默认每隔2s(2000ms)执行一次统计,
                              统计到10s(10000ms)后停止;
                              
ctrl+c 结束统计                

3.3.2, command run screenshot

image.png

It can be seen that after the cpuusage command is executed, the CPU utilization of all tasks in the current system is printed out once per second. There is no application running in the current system, and the running time of idle_task occupies 99.99%.

I hope that developers can also try, create a few tasks, and observe what changes in cpuusage.

 

4. Portal

Easy entry into HaaS100 diagnostic and debugging system in one article

 

5. Developer technical support

If you need more technical support, you can join the Dingding Developer Group

For more technology and solution introduction, please visit the Aliyun AIoT homepage https://iot.aliyun.com/

Guess you like

Origin blog.csdn.net/HaaSTech/article/details/111504073