• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CPUP
2
3
4## Basic Concepts
5
6The central processing unit percent (CPUP) includes the system CPUP and task CPUP.
7
8**System CPUP**
9
10The system CPUP is the CPU usage of the system within a period of time. It reflects the CPU load and the system running status (idle or busy) in the given period of time. The CPUP ranges from 0 to 100, in percentage. The value **100** indicates that the system runs with full load.
11
12With the system CPUP, you can determine whether the current system load exceeds the designed specifications.
13
14**Task CPUP**
15
16Task CPUP refers to the CPU usage of a single task. It reflects the task status, busy or idle, in a period of time. The task CPUP ranges from 0 to 100, in percentage. The value **100** indicates that the task is being executed for the given period of time.
17
18With the CPUP of each task, you can determine whether the CPU usage of each task meets expectations of the design.
19
20**Interrupt CPUP**
21
22In addition, you can enable the interrupt usage statistics function after the CPUP function is enabled.
23
24Interrupt CPUP indicates the CPU usage of a single interrupt out of the total interrupt duration. The interrupt CPUP ranges from 0 to 100. The value **100** indicates that only the interrupt is triggered within a period of time.
25
26
27## Working Principles
28
29The OpenHarmony LiteOS-M CPUP records the system CPU usage on a task basis. When task switching occurs, the task start time and task switch-out or exit time are recorded. Each time when a task exits, the system accumulates the CPU time used by the task.
30
31You can configure this function in **target_config.h**.
32
33The OpenHarmony LiteOS-M provides the following types of CPUP information:
34
35- System CPUP
36- Task CPUP
37
38In addition, the system provides the capability of querying the interrupt CPUP (the CPUP and timer must be enabled).
39
40The CPUP is calculated as follows:
41
42System CPUP = Total running time of all tasks except idle tasks/Total running time of the system
43
44Task CPUP = Total running time of the task/Total running time of the system
45
46Interrupt CPUP = Running time of a single interrupt/Total running time of all interrupts
47
48
49## Available APIs
50
51  **Table 1** APIs for CPUP
52
53| Category| Description|
54| -------- | -------- |
55| Obtaining the system CPUP| **LOS_SysCpuUsage**: obtains the current system CPUP.<br>**LOS_HistorySysCpuUsage**: obtains the historical CPUP of the system.|
56| Obtaining the task CPUP| **LOS_TaskCpuUsage**: obtains the CPUP of a task.<br>**LOS_HistoryTaskCpuUsage**: obtains the historical CPUP of a task.<br>**LOS_AllTaskCpuUsage**: obtains the CPUP of all tasks.|
57| Outputting the task CPUP| **LOS_CpupUsageMonitor**: outputs the historical CPUP of a task.|
58| Obtaining the interrupt CPUP| **LOS_GetAllIrqCpuUsage**: obtains the CPUP of all interrupts.|
59
60## How to Develop
61
62In the **kernel/liteos_m** directory, run the **make menuconfig** command and choose **Kernel > Enable Cpup** to enable CPUP.
63
64Choose **Enable Cpup include irq** to enable interrupt CPUP.
65
66The typical CPUP development process is as follows:
67
681. Call **LOS_SysCpuUsage** to obtain the system CPUP.
69
702. Call **LOS_HistorySysCpuUsage** to obtain the historical CPUP of the system.
71
723. Call **LOS_TaskCpuUsage** to obtain the CPUP of a task.
73   - If the task has been created, disable interrupt, obtain the CPUP, and then enable interrupt.
74   - If the task is not created, return an error code.
75
764. Call **LOS_HistoryTaskCpuUsage** to obtain the historical CPUP of a task.
77   - If the task has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt.
78   - If the task is not created, return an error code.
79
805. Call **LOS_AllCpuUsage** to obtain the CPUP of all tasks.
81   - If CPUP has been initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt.
82   - If CPUP is not initialized or has invalid input parameters, return an error code.
83
84
85## Development Example
86
87
88### Example Description
89
90This example implements the following:
91
921. Create a task for the CPUP test.
93
942. Obtain the CPUP of the current system.
95
963. Obtain the historical system CPUP in different modes.
97
984. Obtain the CPUP of the created task.
99
1005. Obtain the CPUP of the created task in different modes.
101
102
103### Sample Code
104
105**Prerequisites**
106
107CPUP is enabled.<br>To enable CPUP, run **make menuconfig** in the **kernel/liteos_m** directory and choose **Kernel->Enable Cpup** to enable CPUP.
108
109The sample code is as follows:
110
111The sample code can be compiled and verified in **./kernel/liteos_m/testsuites/src/osTest.c**. The **ExampleCpup** function is called in **TestTaskEntry**.
112
113
114```
115#include "los_task.h"
116#include "los_cpup.h"
117
118#define TEST_TASK_PRIO  5
119#define TASK_DELAY_TIME 100
120VOID CpupTask(VOID)
121{
122    printf("entry cpup test example\n");
123    usleep(TASK_DELAY_TIME);
124    usleep(TASK_DELAY_TIME);
125    printf("exit cpup test example\n");
126}
127
128UINT32 ExampleCpup(VOID)
129{
130    UINT32 ret;
131    UINT32 cpupUse;
132    UINT32 taskID;
133    TSK_INIT_PARAM_S cpupTestTask = { 0 };
134
135    cpupTestTask.pfnTaskEntry = (TSK_ENTRY_FUNC)CpupTask;
136    cpupTestTask.pcName       = "TestCpupTsk";
137    cpupTestTask.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
138    cpupTestTask.usTaskPrio   = TEST_TASK_PRIO;
139    ret = LOS_TaskCreate(&taskID, &cpupTestTask);
140    if(ret != LOS_OK) {
141        printf("cpupTestTask create failed .\n");
142        return LOS_NOK;
143    }
144
145    usleep(TASK_DELAY_TIME);
146
147    /* Obtain the current system CPUP. */
148    cpupUse = LOS_SysCpuUsage();
149    printf("the current system cpu usage is: %u.%u\n",
150            cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);
151
152    /* Obtain the historical CPUP of the system. */
153    cpupUse = LOS_HistorySysCpuUsage(CPUP_LESS_THAN_1S);
154    printf("the history system cpu usage in all time: %u.%u\n",
155           cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);
156
157    /* Obtain the CPUP of a specified task. */
158    cpupUse = LOS_TaskCpuUsage(taskID);
159    printf("cpu usage of the cpupTestTask:\n TaskID: %d\n usage: %u.%u\n",
160           taskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);
161
162    /* Obtain the CPUP of a specified task since the system starts. */
163    cpupUse = LOS_HistoryTaskCpuUsage(taskID, CPUP_LESS_THAN_1S);
164    printf("cpu usage of the cpupTestTask in all time:\n TaskID: %d\n usage: %u.%u\n",
165           taskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);
166
167    return LOS_OK;
168}
169```
170
171
172### Verification
173
174  The development is successful if the return result is as follows:
175
176```
177entry cpup test example
178the current system cpu usage is: 8.2
179the history system cpu usage in all time: 8.9
180cpu usage of the cpupTestTask:
181 TaskID: 5
182 usage: 0.5
183cpu usage of the cpupTestTask in all time:
184 TaskID: 5
185 usage: 0.5
186
187exit cpup test example
188
189The preceding data may vary depending on the running environment.
190```
191