1# Process Debugging 2 3 4## Basic Concepts 5 6The central processing unit percent (CPUP) includes the system CPUP, process CPUP, task CPUP, and interrupt CPUP. With the system CPUP, you can determine whether the current system load exceeds the designed specifications. With the CPUP of each task/process/interrupt, you can determine whether their CPU usage meets expectations of the design. 7 8- System CPUP 9 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 valid range of the system CPUP is 0 to 100 in percentage. The precision can be adjusted through configuration. The value **100** indicates that the system runs with full load. 10 11- Process CPUP 12 Process CPUP refers to the CPU usage of a single process. It reflects the process status, busy or idle, in a period of time. The valid range of the process CPUP is 0 to 100 in percentage. The precision can be adjusted through configuration. The value **100** indicates that the process is being executed for a period of time. 13 14- Task CPUP 15 Task CPUP refers to the CPU usage of a single task. It reflects the task status, busy or idle, in a period of time. The valid range of task CPUP is 0 to 100 in percentage. The precision can be adjusted through configuration. The value **100** indicates that the task is being executed for the given period of time. 16 17- Interrupt CPUP 18 Interrupt CPUP refers to the CPU usage of a single interrupt. It reflects the interrupt status, busy or idle, in a period of time. The valid range of the interrupt CPUP is 0 to 100 in percentage. The precision can be adjusted through configuration. The value **100** indicates that the interrupt is being executed for a period of time. 19 20 21## Working Principles 22 23The OpenHarmony LiteOS-A kernel CPUP module records the CPU usage by process, task, and interrupt. When a process or task is switched, the start time of the process or task is recorded. When the process or task is switched out or exits, the system accumulates the CPU time of the entire process or task. When an interrupt is executed, the system accumulates and records the execution time of each interrupt. 24 25OpenHarmony provides the following types of CPUP information: 26 27- System CPUP 28 29- Process CPUP 30 31- Task CPUP 32 33- Interrupt CPUP 34 35The CPUP is calculated as follows: 36 37System CPUP = Total running time of all tasks except idle tasks/Total running time of the system 38 39Process CPUP = Total running time of the process/Total running time of the system 40 41Task CPUP = Total running time of the task/Total running time of the system 42 43Interrupt CPUP = Total running time of the interrupt/Total running time of the system 44 45 46## Development Guidelines 47 48 49### Available APIs 50 51 **Table 1** CPUP module APIs 52 53| Category| API| Description| 54| -------- | -------- | -------- | 55| System CPUP| LOS_HistorySysCpuUsage | Obtains the historical CPUP of the system.| 56| Process CPUP| LOS_HistoryProcessCpuUsage | Obtains the historical CPUP of a specified process.| 57| Process CPUP| LOS_GetAllProcessCpuUsage | Obtains the historical CPUP of all processes in the system.| 58| Task CPUP| LOS_HistoryTaskCpuUsage | Obtains the historical CPUP of a specified task.| 59| Interrupt CPUP| LOS_GetAllIrqCpuUsage | Obtains the historical CPUP of all interrupts in the system.| 60| Reset| LOS_CpupReset | Resets CPUP data.| 61 62 63### How to Develop 64 65The typical CPUP development process is as follows: 66 671. Call **LOS_HistorySysCpuUsage** to obtain the historical CPUP of the system. 68 692. Call **LOS_HistoryProcessCpuUsage** to obtain the historical CPUP of a specified process. 70 - If the process has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 71 - If the process is not created, return an error code. 72 733. Call **LOS_GetAllProcessCpuUsage** to obtain the CPUP of all processes. 74 - If the CPUP is initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 75 - If CPUP is not initialized or has invalid input parameters, return an error code. 76 774. Call **LOS_HistoryTaskCpuUsage** to obtain the historical CPUP of a specified task. 78 - If the task has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 79 - If the task is not created, return an error code. 80 815. Call **LOS_GetAllIrqCpuUsage** to obtain the CPUP of all interrupts. 82 - If the CPUP has been initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 83 - If CPUP is not initialized or has invalid input parameters, return an error code. 84 85 86### Development Example 87 88This example implements the following: 89 901. Create a task for the CPUP test. 91 922. Obtain the CPUP of the current system. 93 943. Obtain the historical system CPUP in different modes. 95 964. Obtain the CPUP of the created test task. 97 985. Obtain the CPUP of the created test task in different modes. 99 100Prerequisites: 101 102The CPUP control is enabled in the **menuconfig** configuration. 103 104**Sample Code** 105 106You can compile and verify the sample code in **kernel/liteos_a/testsuites /kernel/src /osTest.c**. The **CpupTest** function is called in **TestTaskEntry**. 107The sample code is as follows: 108 109 110```c 111#include "los_task.h" 112#include "los_cpup.h" 113#define MODE 4 114UINT32 g_cpuTestTaskID; 115VOID ExampleCpup(VOID) 116{ 117 int i = 0; 118 dprintf("entry cpup test example\n"); 119 for (i = 0; i < 10; i++) { 120 usleep(100); // 100: delay for 100ms 121 } 122} 123UINT32 CpupTest(VOID) 124{ 125 UINT32 ret; 126 UINT32 cpupUse; 127 TSK_INIT_PARAM_S cpupTestTask = {0}; 128 memset(&cpupTestTask, 0, sizeof(TSK_INIT_PARAM_S)); 129 cpupTestTask.pfnTaskEntry = (TSK_ENTRY_FUNC)ExampleCpup; 130 cpupTestTask.pcName = "TestCpupTsk"; 131 cpupTestTask.uwStackSize = 0x800; // 0x800: cpup test task stack size 132 cpupTestTask.usTaskPrio = 5; // 5: cpup test task priority 133 ret = LOS_TaskCreate(&g_cpuTestTaskID, &cpupTestTask); 134 if (ret != LOS_OK) { 135 printf("cpupTestTask create failed .\n"); 136 return LOS_NOK; 137 } 138 139 usleep(100); // 100: delay for 100ms 140 141 /* Obtain the historical CPUP of the system. */ 142 cpupUse = LOS_HistorySysCpuUsage(CPUP_LAST_ONE_SECONDS); 143 dprintf("the history system cpu usage in all time: %u.%u\n", 144 cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 145 /* Obtain the CPUP of the specified task (cpupTestTask in this example). */ 146 cpupUse = LOS_HistoryTaskCpuUsage(g_cpuTestTaskID, CPUP_LAST_ONE_SECONDS); 147 dprintf("cpu usage of the cpupTestTask in all time:\n TaskID: %d\n usage: %u.%u\n", 148 g_cpuTestTaskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 149 return LOS_OK; 150} 151``` 152 153**Verification** 154 155The development is successful if the return result is as follows: 156 157 158``` 159entry cpup test example 160the history system cpu usage in all time: 3.0 161cpu usage of the cpupTestTask in all time: TaskID:10 usage: 0.0 162 163The print information may vary depending on the running environment. 164``` 165 166