• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @defgroup los_cpup CPU usage
34  * @ingroup kernel
35  */
36 
37 #ifndef _LOS_CPUP_H
38 #define _LOS_CPUP_H
39 
40 #include "los_interrupt.h"
41 #include "los_task.h"
42 
43 #ifdef __cplusplus
44 #if __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
47 #endif /* __cplusplus */
48 
49 
50 /**
51 * @ingroup los_cpup
52 * CPU usage error code: The request for memory fails.
53 *
54 * Value: 0x02001e00
55 *
56 * Solution: Decrease the maximum number of tasks.
57 */
58 #define LOS_ERRNO_CPUP_NO_MEMORY             LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x00)
59 
60 /**
61 * @ingroup los_cpup
62 * CPU usage error code: The pointer to an input parameter is NULL.
63 *
64 * Value: 0x02001e01
65 *
66 * Solution: Check whether the pointer to the input parameter is usable.
67 */
68 #define LOS_ERRNO_CPUP_TASK_PTR_NULL         LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x01)
69 
70 /**
71 * @ingroup los_cpup
72 * CPU usage error code: The CPU usage is not initialized.
73 *
74 * Value: 0x02001e02
75 *
76 * Solution: Check whether the CPU usage is initialized.
77 */
78 #define LOS_ERRNO_CPUP_NO_INIT               LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x02)
79 
80 /**
81 * @ingroup los_cpup
82 * CPU usage error code: The number of threads is invalid.
83 *
84 * Value: 0x02001e03
85 *
86 * Solution: Check whether the number of threads is applicable for the current operation.
87 */
88 #define LOS_ERRNO_CPUP_MAXNUM_INVALID        LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x03)
89 
90 /**
91 * @ingroup los_cpup
92 * CPU usage error code: The target thread is not created.
93 *
94 * Value: 0x02001e04
95 *
96 * Solution: Check whether the target thread is created.
97 */
98 #define LOS_ERRNO_CPUP_THREAD_NO_CREATED     LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x04)
99 
100 /**
101 * @ingroup los_cpup
102 * CPU usage error code: The target task ID is invalid.
103 *
104 * Value: 0x02001e05
105 *
106 * Solution: Check whether the target task ID is applicable for the current operation.
107 */
108 #define LOS_ERRNO_CPUP_TSK_ID_INVALID        LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x05)
109 
110 /**
111 * @ingroup los_cpup
112 * Sum of cpup with all tasks. It means the value of cpup is a permillage.
113 */
114 #define LOS_CPUP_PRECISION                   1000
115 
116 /**
117 * @ingroup los_cpup
118 * Multiple of current cpup precision change to percent.
119 */
120 #define LOS_CPUP_PRECISION_MULT              (LOS_CPUP_PRECISION / 100)
121 
122 /**
123  * @ingroup los_cpup
124  * Number of historical running time records
125  */
126 #define OS_CPUP_HISTORY_RECORD_NUM   10
127 
128 /**
129  * @ingroup los_cpup
130  * Count the CPU usage structures of a task.
131  */
132 typedef struct {
133     UINT32 cpupID;                                        /**< Task ID */
134     UINT16 status;                                        /**< Task status */
135     UINT64 allTime;                                       /**< Total running time */
136     UINT64 startTime;                                     /**< Time before a task is invoked */
137     UINT64 historyTime[OS_CPUP_HISTORY_RECORD_NUM];       /**< Historical running time */
138 } OsCpupCB;
139 
140 extern OsCpupCB    *g_cpup;
141 
142 /**
143  * @ingroup los_cpup
144  * @brief Initialization cpup.
145  *
146  * @par Description:
147  * This API is used to initialization cpup.
148  * @attention
149  * <ul>
150  * <li>None.</li>
151  * </ul>
152  *
153  * @param None.
154  *
155  * @retval UINT32   Initialization result.
156  * @par Dependency:
157  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
158  * @see None.
159  */
160 extern UINT32 OsCpupInit(VOID);
161 
162 /**
163  * @ingroup los_cpup
164  * @brief Start task to get cycles count in current task ending.
165  *
166  * @par Description:
167  * This API is used to start task to get cycles count in current task ending.
168  * @attention
169  * <ul>
170  * <li>None.</li>
171  * </ul>
172  *
173  * @param None.
174  *
175  * @retval None.
176  * @par Dependency:
177  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
178  * @see None.
179  */
180 extern VOID OsTskCycleEndStart(VOID);
181 
182 /**
183  * @ingroup los_cpup
184  * Count the CPU usage structures of all tasks.
185  */
186 typedef struct tagCpupInfo {
187     UINT16 usStatus;            /**< save the cur task status     */
188     UINT32 uwUsage;             /**< Usage. The value range is [0,1000].   */
189 } CPUP_INFO_S;
190 
191 /**
192  * @ingroup los_monitor
193  * Type of the CPU usage query.
194  */
195 typedef enum {
196     SYS_CPU_USAGE = 0,   /* system cpu occupancy rate */
197     TASK_CPU_USAGE,      /* task cpu occupancy rate */
198 } CPUP_TYPE_E;
199 
200 /**
201  * @ingroup los_monitor
202  * Mode of the CPU usage query.
203  */
204 typedef enum {
205     CPUP_IN_10S = 0,     /* cpu occupancy rate in 10s */
206     CPUP_IN_1S,          /* cpu occupancy rate in 1s */
207     CPUP_LESS_THAN_1S,   /* cpu occupancy rate less than 1s, if the input mode is none of them, it will be this. */
208 } CPUP_MODE_E;
209 
210 /**
211  * @ingroup los_cpup
212  * @brief Obtain the current CPU usage.
213  *
214  * @par Description:
215  * This API is used to obtain the current CPU usage.
216  * @attention
217  * <ul>
218  * <li>This API can be called only after the CPU usage is initialized. Otherwise, error codes will be returned.</li>
219  * <li> The precision of the CPU usage can be adjusted by changing the value of the CPUP_PRECISION macro.</li>
220  * </ul>
221  *
222  * @param None.
223  *
224  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
225  * @retval #cpup                            [0,1000], current CPU usage, of which the precision is adjustable.
226  * @par Dependency:
227  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
228  * @see LOS_SysCpuUsage
229  */
230 extern UINT32 LOS_SysCpuUsage(VOID);
231 
232 /**
233  * @ingroup los_cpup
234  * @brief Obtain the historical CPU usage.
235  *
236  * @par Description:
237  * This API is used to obtain the historical CPU usage.
238  * @attention
239  * <ul>
240  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails to be obtained.</li>
241  * </ul>
242  *
243  * @param  mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s will be
244  * obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained. Other values
245  * indicate that the CPU usage in the period that is less than 1s will be obtained.
246  *
247  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
248  * @retval #cpup                            [0,1000], historical CPU usage, of which the precision is adjustable.
249  * @par Dependency:
250  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
251  * @see LOS_HistoryTaskCpuUsage
252  */
253 extern UINT32 LOS_HistorySysCpuUsage(UINT16 mode);
254 
255 /**
256  * @ingroup los_cpup
257  * @brief Obtain the CPU usage of a specified task.
258  *
259  * @par Description:
260  * This API is used to obtain the CPU usage of a task specified by a passed-in task ID.
261  * @attention
262  * <ul>
263  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails to be obtained.</li>
264  * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
265  * the CPU usage fails to be obtained.</li>
266  * </ul>
267  *
268  * @param taskID   [IN] UINT32. Task ID.
269  *
270  * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
271  * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
272  * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
273  * @retval #cpup                              [0,1000], CPU usage of the specified task.
274  * @par Dependency:
275  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
276  * @see LOS_HistoryTaskCpuUsage
277  */
278 extern UINT32 LOS_TaskCpuUsage(UINT32 taskID);
279 
280 /**
281  * @ingroup los_cpup
282  * @brief  Obtain the historical CPU usage of a specified task.
283  *
284  * @par Description:
285  * This API is used to obtain the historical CPU usage of a task specified by a passed-in task ID.
286  * @attention
287  * <ul>
288  * <li>This API can be called only after the CPU usage is initialized. Otherwise,
289  * the CPU usage fails to be obtained.</li>
290  * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
291  * the CPU usage fails to be obtained.</li>
292  * </ul>
293  *
294  * @param taskID   [IN] UINT32. Task ID.
295  * @param mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
296  * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
297  * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
298  *
299  * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
300  * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
301  * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
302  * @retval #cpup                              [0,1000], CPU usage of the specified task.
303  * @par Dependency:
304  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
305  * @see LOS_HistorySysCpuUsage
306  */
307 extern UINT32 LOS_HistoryTaskCpuUsage(UINT32 taskID, UINT16 mode);
308 
309 /**
310  * @ingroup los_cpup
311  * @brief Obtain the CPU usage of all tasks.
312  *
313  * @par Description:
314  * This API is used to obtain the CPU usage of all tasks according to maximum number of threads.
315  * @attention
316  * <ul>
317  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails to be obtained.</li>
318  * <li>The input parameter pointer must not be NULL, Otherwise, the CPU usage fails to be obtained.</li>
319  * </ul>
320  *
321  * @param cpupInfo    [OUT]Type.   CPUP_INFO_S* Pointer to the task CPUP information structure to be obtained.
322  * @param mode        [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
323  * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
324  * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
325  *
326  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
327  * @retval #OS_ERRNO_CPUP_TASK_PTR_NULL     0x02001e01: The input parameter pointer is NULL.
328  * @retval #LOS_OK                          0x00000000: The CPU usage of all tasks is successfully obtained.
329  * @par Dependency:
330  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
331  * @see LOS_SysCpuUsage
332  */
333 extern UINT32 LOS_AllTaskCpuUsage(CPUP_INFO_S *cpupInfo, UINT16 mode);
334 
335 /**
336  * @ingroup los_monitor
337  * @brief Obtain CPU usage history of certain task.
338  *
339  * @par Description:
340  * This API is used to obtain CPU usage history of certain task.
341  * @attention
342  * <ul>
343  * <li>This API can be called only after the CPU usage is initialized. Otherwise, -1 will be returned.</li>
344  * <li> Only in SYS_CPU_USAGE type, uwTaskID is invalid.</li>
345  * </ul>
346  *
347  * @param type        [IN] cpup type, SYS_CPU_USAGE and TASK_CPU_USAGE
348  * @param mode        [IN] mode,CPUP_IN_10S = usage in 10s,CPUP_IN_1S = usage in last 1s,
349  * CPUP_LESS_THAN_1S = less than 1s, if the input mode is none of them, it will be as CPUP_LESS_THAN_1S.
350  * @param taskID      [IN] task ID, Only in SYS_CPU_USAGE type, taskID is invalid
351  *
352  * @retval #OS_ERROR           -1:CPU usage info obtain failed.
353  * @retval #LOS_OK              0:CPU usage info is successfully obtained.
354  * @par Dependency:
355  * <ul><li>los_monitor.h: the header file that contains the API declaration.</li></ul>
356  * @see LOS_CpupUsageMonitor
357  */
358 extern UINT32 LOS_CpupUsageMonitor(CPUP_TYPE_E type, CPUP_MODE_E mode, UINT32 taskID);
359 
360 #ifdef __cplusplus
361 #if __cplusplus
362 }
363 #endif /* __cplusplus */
364 #endif /* __cplusplus */
365 
366 #endif /* _LOS_CPUP_H */
367