1# CPUP<a name="EN-US_TOPIC_0000001123188477"></a> 2 3- [Basic Concepts](#section17683419227) 4- [Working Principles](#section593718536227) 5- [Development Guidelines](#section11284210152311) 6 - [Available APIs](#section3745151592312) 7 - [How to Develop](#section122901429182316) 8 - [Development Example](#section1765785212310) 9 10 11## Basic Concepts<a name="section17683419227"></a> 12 13The 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 the CPU usage of each task/process/interrupt meets expectations of the design. 14 15- System CPUP 16 17 The 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 a 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. 18 19- Process CPUP 20 21 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. 22 23- Task CPUP 24 25 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 a period of time. 26 27- Interrupt CPUP 28 29 Task 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. 30 31 32## Working Principles<a name="section593718536227"></a> 33 34The 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. 35 36The OpenHarmony provides the following types of CPUP information: 37 38- System CPUP 39- Process CPUP 40- Task CPUP 41- Interrupt CPUP 42 43You can calculate the CPUP as follows: 44 45System CPUP = Total running time of all tasks except idle tasks/Total running time of the system 46 47Process CPUP = Total running time of the process/Total running time of the system 48 49Task CPUP = Total running time of the task/Total running time of the system 50 51Interrupt CPUP = Total running time of the interrupt/Total running time of the system 52 53## Development Guidelines<a name="section11284210152311"></a> 54 55### Available APIs<a name="section3745151592312"></a> 56 57**Table 1** CPUP module APIs 58 59<a name="table147491853163018"></a> 60<table><thead align="left"><tr id="row10807205323013"><th class="cellrowborder" valign="top" width="28.3971602839716%" id="mcps1.2.4.1.1"><p id="p980714539304"><a name="p980714539304"></a><a name="p980714539304"></a>Category</p> 61</th> 62<th class="cellrowborder" valign="top" width="36.47635236476353%" id="mcps1.2.4.1.2"><p id="p1780715533305"><a name="p1780715533305"></a><a name="p1780715533305"></a>API</p> 63</th> 64<th class="cellrowborder" valign="top" width="35.12648735126487%" id="mcps1.2.4.1.3"><p id="p18807185316301"><a name="p18807185316301"></a><a name="p18807185316301"></a>Description</p> 65</th> 66</tr> 67</thead> 68<tbody><tr id="row3807145310300"><td class="cellrowborder" valign="top" width="28.3971602839716%" headers="mcps1.2.4.1.1 "><p id="p174011140141013"><a name="p174011140141013"></a><a name="p174011140141013"></a>System CPUP</p> 69</td> 70<td class="cellrowborder" valign="top" width="36.47635236476353%" headers="mcps1.2.4.1.2 "><p id="p158071153133013"><a name="p158071153133013"></a><a name="p158071153133013"></a>LOS_HistorySysCpuUsage</p> 71</td> 72<td class="cellrowborder" valign="top" width="35.12648735126487%" headers="mcps1.2.4.1.3 "><p id="p14808115353010"><a name="p14808115353010"></a><a name="p14808115353010"></a>Obtains the historical CPUP of the system.</p> 73</td> 74</tr> 75<tr id="row147461859201016"><td class="cellrowborder" rowspan="2" valign="top" width="28.3971602839716%" headers="mcps1.2.4.1.1 "><p id="p374720596102"><a name="p374720596102"></a><a name="p374720596102"></a>Process CPUP</p> 76</td> 77<td class="cellrowborder" valign="top" width="36.47635236476353%" headers="mcps1.2.4.1.2 "><p id="p3747859101010"><a name="p3747859101010"></a><a name="p3747859101010"></a>LOS_HistoryProcessCpuUsage</p> 78</td> 79<td class="cellrowborder" valign="top" width="35.12648735126487%" headers="mcps1.2.4.1.3 "><p id="p1747959131016"><a name="p1747959131016"></a><a name="p1747959131016"></a>Obtains the historical CPUP of a specified process.</p> 80</td> 81</tr> 82<tr id="row2075434812116"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p17541648171114"><a name="p17541648171114"></a><a name="p17541648171114"></a>LOS_GetAllProcessCpuUsage</p> 83</td> 84<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1575419483116"><a name="p1575419483116"></a><a name="p1575419483116"></a>Obtains the historical CPUP of all processes in the system.</p> 85</td> 86</tr> 87<tr id="row1480855311301"><td class="cellrowborder" valign="top" width="28.3971602839716%" headers="mcps1.2.4.1.1 "><p id="p1618792981018"><a name="p1618792981018"></a><a name="p1618792981018"></a>Task CPUP</p> 88</td> 89<td class="cellrowborder" valign="top" width="36.47635236476353%" headers="mcps1.2.4.1.2 "><p id="p9808185353016"><a name="p9808185353016"></a><a name="p9808185353016"></a>LOS_HistoryTaskCpuUsage</p> 90</td> 91<td class="cellrowborder" valign="top" width="35.12648735126487%" headers="mcps1.2.4.1.3 "><p id="p12808653183016"><a name="p12808653183016"></a><a name="p12808653183016"></a>Obtains the historical CPUP of a specified task.</p> 92</td> 93</tr> 94<tr id="row680812535306"><td class="cellrowborder" valign="top" width="28.3971602839716%" headers="mcps1.2.4.1.1 "><p id="p13808125314307"><a name="p13808125314307"></a><a name="p13808125314307"></a>Interrupt CPUP</p> 95</td> 96<td class="cellrowborder" valign="top" width="36.47635236476353%" headers="mcps1.2.4.1.2 "><p id="p1480835316303"><a name="p1480835316303"></a><a name="p1480835316303"></a>LOS_GetAllIrqCpuUsage</p> 97</td> 98<td class="cellrowborder" valign="top" width="35.12648735126487%" headers="mcps1.2.4.1.3 "><p id="p33741531163313"><a name="p33741531163313"></a><a name="p33741531163313"></a>Obtains the historical CPUP of all interrupts in the system.</p> 99</td> 100</tr> 101</tbody> 102</table> 103 104### How to Develop<a name="section122901429182316"></a> 105 106The typical CPUP development process is as follows. 107 1081. Call **LOS\_HistorySysCpuUsage** to obtain the historical CPUP of the system. 1092. Call **LOS\_HistoryProcessCpuUsage** to obtain the historical CPUP of a specified process. 110 - If the process has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 111 - If the process is not created, return an error code. 112 1133. Call **LOS\_GetAllProcessCpuUsage** to obtain the CPUP of all processes. 114 - If the CPUP is initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 115 - If CPUP is not initialized or has invalid input parameters, return an error code. 116 1174. Call **LOS\_HistoryTaskCpuUsage** to obtain the historical CPUP of a specified task. 118 - If the task has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 119 - If the task is not created, return an error code. 120 1215. Call **LOS\_GetAllIrqCpuUsage** to obtain the CPUP of all interrupts. 122 - If the CPUP is initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 123 - If CPUP is not initialized or has invalid input parameters, return an error code. 124 125 126### Development Example<a name="section1765785212310"></a> 127 128This example implements the following: 129 1301. Create a task for the CPUP test. 1312. Obtain the CPUP of the current system. 1323. Obtain the historical system CPUP in different modes. 1334. Obtain the CPUP of the created test task. 1345. Obtains the CPUP of the created test task in different modes. 135 136Prerequisites 137 138Enable the cpup control switch in the **menuconfig** configuration. 139 140**Sample Code** 141 142The sample code is as follows: 143 144``` 145#include "los_task.h" 146#include "los_cpup.h" 147#define MODE 4 148UINT32 g_cpuTestTaskID; 149VOID ExampleCpup(VOID) 150{ 151 printf("entry cpup test example\n"); 152 while(1) { 153 usleep(100); 154 } 155} 156UINT32 ItCpupTest(VOID) 157{ 158 UINT32 ret; 159 UINT32 cpupUse; 160 TSK_INIT_PARAM_S cpupTestTask = { 0 }; 161 memset(&cpupTestTask, 0, sizeof(TSK_INIT_PARAM_S)); 162 cpupTestTask.pfnTaskEntry = (TSK_ENTRY_FUNC)ExampleCpup; 163 cpupTestTask.pcName = "TestCpupTsk"; 164 cpupTestTask.uwStackSize = 0x800; 165 cpupTestTask.usTaskPrio = 5; 166 ret = LOS_TaskCreate(&g_cpuTestTaskID, &cpupTestTask); 167 if(ret != LOS_OK) { 168 printf("cpupTestTask create failed .\n"); 169 return LOS_NOK; 170 } 171 172 usleep(100); 173 174 /* Obtain the historical CPUP of the system. */ 175 cpupUse = LOS_HistorySysCpuUsage(CPU_LESS_THAN_1S); 176 printf("the history system cpu usage in all time: %u.%u\n", 177 cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 178 /* Obtain the CPUP of the specified task (cpupTestTask in this example).*/ 179 cpupUse = LOS_HistoryTaskCpuUsage(g_cpuTestTaskID, CPU_LESS_THAN_1S); 180 printf("cpu usage of the cpupTestTask in all time:\n TaskID: %d\n usage: %u.%u\n", 181 g_cpuTestTaskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 182 return LOS_OK; 183} 184``` 185 186**Verification** 187 188The development is successful if the return result is as follows: 189 190``` 191entry cpup test example 192the history system cpu usage in all time: 3.0 193cpu usage of the cpupTestTask in all time: TaskID:10 usage: 0.0 194``` 195 196