1# Development Guidelines<a name="EN-US_TOPIC_0000001079036450"></a> 2 3- [Available APIs](#section158501652121514) 4- [How to Develop](#section783435801510) 5- [Development Example](#section460018317164) 6 - [Example Description](#section51413507517) 7 - [Sample Code](#section17617965523) 8 - [Verification](#section1968771515188) 9 10 11## Available APIs<a name="section158501652121514"></a> 12 13**Table 1** Functions 14 15<a name="table18293928155615"></a> 16<table><thead align="left"><tr id="row129362875613"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="p19444103765618"><a name="p19444103765618"></a><a name="p19444103765618"></a>Category</p> 17</th> 18<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="p944473716569"><a name="p944473716569"></a><a name="p944473716569"></a>API</p> 19</th> 20<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="p144445378565"><a name="p144445378565"></a><a name="p144445378565"></a>Description</p> 21</th> 22</tr> 23</thead> 24<tbody><tr id="row1143613475615"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p942993405610"><a name="p942993405610"></a><a name="p942993405610"></a>Obtaining the system CPU usage</p> 25</td> 26<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p242973419563"><a name="p242973419563"></a><a name="p242973419563"></a>LOS_SysCpuUsage</p> 27</td> 28<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p7429163416565"><a name="p7429163416565"></a><a name="p7429163416565"></a>Obtains the current system CPUP.</p> 29</td> 30</tr> 31<tr id="row15436163435611"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p20429183410563"><a name="p20429183410563"></a><a name="p20429183410563"></a>LOS_HistorySysCpuUsage</p> 32</td> 33<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p542953465617"><a name="p542953465617"></a><a name="p542953465617"></a>Obtains the historical CPUP of the system.</p> 34</td> 35</tr> 36<tr id="row143610342562"><td class="cellrowborder" rowspan="3" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p174295347568"><a name="p174295347568"></a><a name="p174295347568"></a>Obtaining the task CPUP</p> 37</td> 38<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p124291734155614"><a name="p124291734155614"></a><a name="p124291734155614"></a>LOS_TaskCpuUsage</p> 39</td> 40<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p1042963410568"><a name="p1042963410568"></a><a name="p1042963410568"></a>Obtains the CPUP of a specified task.</p> 41</td> 42</tr> 43<tr id="row12436143414561"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p6429834185613"><a name="p6429834185613"></a><a name="p6429834185613"></a>LOS_HistoryTaskCpuUsage</p> 44</td> 45<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p74302034175614"><a name="p74302034175614"></a><a name="p74302034175614"></a>Obtains the historical CPUP of a specified task.</p> 46</td> 47</tr> 48<tr id="row2435834135618"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p843073420563"><a name="p843073420563"></a><a name="p843073420563"></a>LOS_AllCpuUsage</p> 49</td> 50<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p4430134185614"><a name="p4430134185614"></a><a name="p4430134185614"></a>Obtains the CPUP of all tasks.</p> 51</td> 52</tr> 53<tr id="row15435934155618"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p1543033435615"><a name="p1543033435615"></a><a name="p1543033435615"></a>Outputting the task CPUP</p> 54</td> 55<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p1643016342562"><a name="p1643016342562"></a><a name="p1643016342562"></a>LOS_CpupUsageMonitor</p> 56</td> 57<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p84301234115617"><a name="p84301234115617"></a><a name="p84301234115617"></a>Outputs the historical CPUP of a task.</p> 58</td> 59</tr> 60</tbody> 61</table> 62 63## How to Develop<a name="section783435801510"></a> 64 65The typical CPUP development process is as follows. 66 671. Call **LOS\_SysCpuUsage** to obtain the system CPUP. 682. Call **LOS\_HistorySysCpuUsage** to obtain the historical CPUP of the system. 693. Call **LOS\_TaskCpuUsage** to obtain the CPUP of a specified task. 70 - If the task has been created, disable interrupt, obtain the CPUP, and then enable interrupt. 71 - If the task is not created, return an error code. 72 734. Call **LOS\_HistoryTaskCpuUsage** to obtain the historical CPUP of a specified task. 74 - If the task has been created, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 75 - If the task is not created, return an error code. 76 775. Call **LOS\_AllCpuUsage** to obtain the CPUP of all tasks. 78 - If the CPUP is initialized, disable interrupt, obtain the CPUP in different modes, and then enable interrupt. 79 - If CPUP is not initialized or has invalid input parameters, return an error code. 80 81 82## Development Example<a name="section460018317164"></a> 83 84### Example Description<a name="section51413507517"></a> 85 86This example implements the following: 87 881. Create a task for the CPUP test. 892. Obtain the CPUP of the current system. 903. Obtain the historical system CPUP in different modes. 914. Obtain the CPUP of the created test task. 925. Obtain the CPUP of the created test task in different modes. 93 94### Sample Code<a name="section17617965523"></a> 95 96Prerequisites 97 98In **target\_config.h**, the **LOSCFG\_BASE\_CORE\_CPUP** parameter is enabled. 99 100The sample code is as follows: 101 102``` 103#include "los_task.h" 104#include "los_cpup.h" 105#define MODE 4 106UINT32 g_cpuTestTaskID; 107VOID ExampleCpup(VOID) 108{ 109 printf("entry cpup test example\n"); 110 while(1) { 111 usleep(100); 112 } 113} 114UINT32 ItCpupTest(VOID) 115{ 116 UINT32 ret; 117 UINT32 cpupUse; 118 TSK_INIT_PARAM_S cpupTestTask = { 0 }; 119 memset(&cpupTestTask, 0, sizeof(TSK_INIT_PARAM_S)); 120 cpupTestTask.pfnTaskEntry = (TSK_ENTRY_FUNC)ExampleCpup; 121 cpupTestTask.pcName = "TestCpupTsk"; 122 cpupTestTask.uwStackSize = 0x800; 123 cpupTestTask.usTaskPrio = 5; 124 ret = LOS_TaskCreate(&g_cpuTestTaskID, &cpupTestTask); 125 if(ret != LOS_OK) { 126 printf("cpupTestTask create failed .\n"); 127 return LOS_NOK; 128 } 129 130 usleep(100); 131 132 /* Obtain the current CPUP of the system. */ 133 cpupUse = LOS_SysCpuUsage(); 134 printf("the current system cpu usage is: %u.%u\n", 135 cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 136 137 cpupUse = LOS_HistorySysCpuUsage(CPU_LESS_THAN_1S); 138 /* Obtain the CPUP of the specified task (cpupTestTask in this example).*/ 139 printf("the history system CPUP in all time: %u.%u\n", 140 cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 141 cpupUse = LOS_TaskCpuUsage(g_cpuTestTaskID); 142 /* Obtain the CPUP of the specified historical task (cpupTestTask in this example) since the system startup. */ 143 printf("cpu usage of the cpupTestTask:\n TaskID: %d\n usage: %u.%u\n", 144 g_cpuTestTaskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 145 cpupUse = LOS_HistoryTaskCpuUsage(g_cpuTestTaskID, CPU_LESS_THAN_1S); 146 printf("cpu usage of the cpupTestTask in all time:\n TaskID: %d\n usage: %u.%u\n", 147 g_cpuTestTaskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT); 148 return LOS_OK; 149} 150``` 151 152### Verification<a name="section1968771515188"></a> 153 154The development is successful if the return result is as follows: 155 156``` 157entry cpup test example 158the current system cpu usage is : 1.5 159 the history system cpu usage in all time: 3.0 160 cpu usage of the cpupTestTask: TaskID:10 usage: 0.0 161 cpu usage of the cpupTestTask in all time: TaskID:10 usage: 0.0 162``` 163 164