• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 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 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
143 typedef struct {
144     UINT32 cpupID;                                        /**< Irq ID */
145     UINT16 status;                                        /**< Irq status */
146     UINT64 allTime;                                       /**< Total running time */
147     UINT64 startTime;                                     /**< Time before a task is invoked */
148     UINT64 timeMax;                                       /**< Irq samples count */
149     UINT64 count;                                         /**< Irq samples count */
150     UINT64 historyTime[OS_CPUP_HISTORY_RECORD_NUM];       /**< Historical running time */
151 } OsIrqCpupCB;
152 #endif
153 
154 /**
155  * @ingroup los_cpup
156  * @brief Initialization cpup.
157  *
158  * @par Description:
159  * This API is used to initialization cpup.
160  * @attention
161  * <ul>
162  * <li>None.</li>
163  * </ul>
164  *
165  * @param None.
166  *
167  * @retval UINT32   Initialization result.
168  * @par Dependency:
169  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
170  * @see None.
171  */
172 extern UINT32 OsCpupInit(VOID);
173 
174 extern UINT32 OsCpupDaemonInit(VOID);
175 /**
176  * @ingroup los_cpup
177  * @brief Start task to get cycles count in current task ending.
178  *
179  * @par Description:
180  * This API is used to start task to get cycles count in current task ending.
181  * @attention
182  * <ul>
183  * <li>None.</li>
184  * </ul>
185  *
186  * @param None.
187  *
188  * @retval None.
189  * @par Dependency:
190  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
191  * @see None.
192  */
193 extern VOID OsTskCycleEndStart(VOID);
194 
195 /**
196  * @ingroup los_cpup
197  * Count the CPU usage structures of all tasks.
198  */
199 typedef struct tagCpupInfo {
200     UINT16 usStatus;            /**< save the cur task status     */
201     UINT32 uwUsage;             /**< Usage. The value range is [0,1000].   */
202 } CPUP_INFO_S;
203 
204 /**
205  * @ingroup los_monitor
206  * Type of the CPU usage query.
207  */
208 typedef enum {
209     SYS_CPU_USAGE = 0,   /* system cpu occupancy rate */
210     TASK_CPU_USAGE,      /* task cpu occupancy rate */
211 } CPUP_TYPE_E;
212 
213 /**
214  * @ingroup los_monitor
215  * Mode of the CPU usage query.
216  */
217 typedef enum {
218     CPUP_IN_10S = 0,     /* cpu occupancy rate in 10s */
219     CPUP_IN_1S,          /* cpu occupancy rate in 1s */
220     CPUP_LESS_THAN_1S,   /* cpu occupancy rate less than 1s, if the input mode is none of them, it will be this. */
221 } CPUP_MODE_E;
222 
223 /**
224  * @ingroup los_cpup
225  * @brief Obtain the current CPU usage.
226  *
227  * @par Description:
228  * This API is used to obtain the current CPU usage.
229  * @attention
230  * <ul>
231  * <li>This API can be called only after the CPU usage is initialized. Otherwise, error codes will be returned.</li>
232  * <li> The precision of the CPU usage can be adjusted by changing the value of the CPUP_PRECISION macro.</li>
233  * </ul>
234  *
235  * @param None.
236  *
237  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
238  * @retval #cpup                            [0,1000], current CPU usage, of which the precision is adjustable.
239  * @par Dependency:
240  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
241  * @see LOS_SysCpuUsage
242  */
243 extern UINT32 LOS_SysCpuUsage(VOID);
244 
245 /**
246  * @ingroup los_cpup
247  * @brief Obtain the historical CPU usage.
248  *
249  * @par Description:
250  * This API is used to obtain the historical CPU usage.
251  * @attention
252  * <ul>
253  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
254  * to be obtained.</li>
255  * </ul>
256  *
257  * @param  mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s will be
258  * obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained. Other values
259  * indicate that the CPU usage in the period that is less than 1s will be obtained.
260  *
261  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
262  * @retval #cpup                            [0,1000], historical CPU usage, of which the precision is adjustable.
263  * @par Dependency:
264  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
265  * @see LOS_HistoryTaskCpuUsage
266  */
267 extern UINT32 LOS_HistorySysCpuUsage(UINT16 mode);
268 
269 /**
270  * @ingroup los_cpup
271  * @brief Obtain the CPU usage of a specified task.
272  *
273  * @par Description:
274  * This API is used to obtain the CPU usage of a task specified by a passed-in task ID.
275  * @attention
276  * <ul>
277  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
278  * to be obtained.</li>
279  * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
280  * the CPU usage fails to be obtained.</li>
281  * </ul>
282  *
283  * @param taskID   [IN] UINT32. Task ID.
284  *
285  * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
286  * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
287  * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
288  * @retval #cpup                              [0,1000], CPU usage of the specified task.
289  * @par Dependency:
290  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
291  * @see LOS_HistoryTaskCpuUsage
292  */
293 extern UINT32 LOS_TaskCpuUsage(UINT32 taskID);
294 
295 /**
296  * @ingroup los_cpup
297  * @brief  Obtain the historical CPU usage of a specified task.
298  *
299  * @par Description:
300  * This API is used to obtain the historical CPU usage of a task specified by a passed-in task ID.
301  * @attention
302  * <ul>
303  * <li>This API can be called only after the CPU usage is initialized. Otherwise,
304  * the CPU usage fails to be obtained.</li>
305  * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
306  * the CPU usage fails to be obtained.</li>
307  * </ul>
308  *
309  * @param taskID   [IN] UINT32. Task ID.
310  * @param mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
311  * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
312  * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
313  *
314  * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
315  * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
316  * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
317  * @retval #cpup                              [0,1000], CPU usage of the specified task.
318  * @par Dependency:
319  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
320  * @see LOS_HistorySysCpuUsage
321  */
322 extern UINT32 LOS_HistoryTaskCpuUsage(UINT32 taskID, UINT16 mode);
323 
324 /**
325  * @ingroup los_cpup
326  * @brief Obtain the CPU usage of all tasks.
327  *
328  * @par Description:
329  * This API is used to obtain the CPU usage of all tasks according to maximum number of threads.
330  * @attention
331  * <ul>
332  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
333  * to be obtained.</li>
334  * <li>The input parameter pointer must not be NULL, Otherwise, the CPU usage fails to be obtained.</li>
335  * </ul>
336  *
337  * @param cpupInfo    [OUT]Type.   CPUP_INFO_S* Pointer to the task CPUP information structure to be obtained.
338  * @param mode        [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
339  * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
340  * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
341  *
342  * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
343  * @retval #OS_ERRNO_CPUP_TASK_PTR_NULL     0x02001e01: The input parameter pointer is NULL.
344  * @retval #LOS_OK                          0x00000000: The CPU usage of all tasks is successfully obtained.
345  * @par Dependency:
346  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
347  * @see LOS_SysCpuUsage
348  */
349 extern UINT32 LOS_AllTaskCpuUsage(CPUP_INFO_S *cpupInfo, UINT16 mode);
350 
351 /**
352  * @ingroup los_monitor
353  * @brief Obtain CPU usage history of certain task.
354  *
355  * @par Description:
356  * This API is used to obtain CPU usage history of certain task.
357  * @attention
358  * <ul>
359  * <li>This API can be called only after the CPU usage is initialized. Otherwise, -1 will be returned.</li>
360  * <li> Only in SYS_CPU_USAGE type, uwTaskID is invalid.</li>
361  * </ul>
362  *
363  * @param type        [IN] cpup type, SYS_CPU_USAGE and TASK_CPU_USAGE
364  * @param mode        [IN] mode,CPUP_IN_10S = usage in 10s,CPUP_IN_1S = usage in last 1s,
365  * CPUP_LESS_THAN_1S = less than 1s, if the input mode is none of them, it will be as CPUP_LESS_THAN_1S.
366  * @param taskID      [IN] task ID, Only in SYS_CPU_USAGE type, taskID is invalid
367  *
368  * @retval #OS_ERROR           -1:CPU usage info obtain failed.
369  * @retval #LOS_OK              0:CPU usage info is successfully obtained.
370  * @par Dependency:
371  * <ul><li>los_monitor.h: the header file that contains the API declaration.</li></ul>
372  * @see LOS_CpupUsageMonitor
373  */
374 extern UINT32 LOS_CpupUsageMonitor(CPUP_TYPE_E type, CPUP_MODE_E mode, UINT32 taskID);
375 
376 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
377 extern VOID OsCpupIrqStart(UINT32 intNum);
378 extern VOID OsCpupIrqEnd(UINT32 intNum);
379 extern OsIrqCpupCB *OsGetIrqCpupArrayBase(VOID);
380 extern UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo);
381 #endif
382 
383 #ifdef __cplusplus
384 #if __cplusplus
385 }
386 #endif /* __cplusplus */
387 #endif /* __cplusplus */
388 
389 #endif /* _LOS_CPUP_H */
390