• 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_hwi.h"
41 #include "los_base.h"
42 #include "los_sys.h"
43 
44 #ifdef __cplusplus
45 #if __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 #endif /* __cplusplus */
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 processes.
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 error.
63  *
64  * Value: 0x02001e01
65  *
66  * Solution: Check whether input parameter is valid.
67  */
68 #define LOS_ERRNO_CPUP_PTR_ERR       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 target cpup is not created.
83  *
84  * Value: 0x02001e03
85  *
86  * Solution: Check whether the target cpup is created.
87  */
88 #define LOS_ERRNO_CPUP_NO_CREATED    LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x03)
89 
90 /**
91  * @ingroup los_cpup
92  * CPU usage error code: The target cpup ID is invalid.
93  *
94  * Value: 0x02001e04
95  *
96  * Solution: Check whether the target cpup ID is applicable for the current operation.
97  */
98 #define LOS_ERRNO_CPUP_ID_INVALID    LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x04)
99 
100 /**
101  * @ingroup los_cpup
102  * Sum of single core cpup with all processes and tasks. It means the value of cpup is a permillage.
103  */
104 #define LOS_CPUP_SINGLE_CORE_PRECISION       10000
105 
106 /**
107  * @ingroup los_cpup
108  * Multiple of current cpup precision change to percent.
109  */
110 #define LOS_CPUP_PRECISION_MULT              (LOS_CPUP_SINGLE_CORE_PRECISION / 100)
111 
112 /**
113  * @ingroup los_cpup
114  * Sum of all core cpup with all processes. It means the value of cpup is a permillage.
115  */
116 #define LOS_CPUP_PRECISION                   (LOS_CPUP_SINGLE_CORE_PRECISION * LOSCFG_KERNEL_CORE_NUM)
117 
118 /**
119  * @ingroup los_cpup
120  * Count the CPU usage structures of all cpup.
121  */
122 typedef struct tagCpupInfo {
123     UINT16 status; /**< Save the cur cpup status     */
124     UINT32 usage;  /**< Usage. The value range is [0, LOS_CPUP_SINGLE_CORE_PRECISION].   */
125 } CPUP_INFO_S;
126 
127 /**
128  * @ingroup los_cpup
129  * Query the CPU usage of the system.
130  */
131 enum {
132     CPUP_LAST_TEN_SECONDS = 0, /**< Display CPU usage in the last ten seconds. */
133     CPUP_LAST_ONE_SECONDS = 1, /**< Display CPU usage in the last one seconds. */
134     CPUP_ALL_TIME = 0xffff     /**< Display CPU usage from system startup to now. */
135 };
136 
137 /**
138  * @ingroup los_cpup
139  * @brief Obtain the historical CPU usage.
140  *
141  * @par Description:
142  * This API is used to obtain the historical CPU usage.
143  * @attention
144  * <ul>
145  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails to be
146  * obtained.</li>
147  * </ul>
148  *
149  * @param  mode     [IN] UINT16. process mode. The parameter value 0 indicates that the CPU usage within 10s will be
150  *                               obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will
151  *                               be obtained. Other values indicate that the CPU usage in all time will be obtained.
152  *
153  * @retval #LOS_ERRNO_CPUP_NO_INIT           The CPU usage is not initialized.
154  * @retval #UINT32                           [0, LOS_CPUP_SINGLE_CORE_PRECISION], historical CPU usage,
155  *                                           of which the precision is adjustable.
156  * @par Dependency:
157  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
158  * @see
159  */
160 extern UINT32 LOS_HistorySysCpuUsage(UINT16 mode);
161 
162 /**
163  * @ingroup los_cpup
164  * @brief  Obtain the historical CPU usage of a specified process.
165  *
166  * @par Description:
167  * This API is used to obtain the historical CPU usage of a process specified by a passed-in process ID.
168  * @attention
169  * <ul>
170  * <li>This API can be called only after the CPU usage is initialized. Otherwise,
171  * the CPU usage fails to be obtained.</li>
172  * <li>The passed-in process ID must be valid and the process specified by the process ID must be created. Otherwise,
173  * the CPU usage fails to be obtained.</li>
174  * </ul>
175  *
176  * @param pid      [IN] UINT32. process ID.
177  * @param mode     [IN] UINT16. cpup mode. The parameter value 0 indicates that the CPU usage within 10s will be
178  *                              obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will
179  *                              be obtained. Other values indicate that the CPU usage in the period that is less than
180  *                              1s will be obtained.
181  *
182  * @retval #LOS_ERRNO_CPUP_NO_INIT          The CPU usage is not initialized.
183  * @retval #LOS_ERRNO_CPUP_ID_INVALID       The target process ID is invalid.
184  * @retval #LOS_ERRNO_CPUP_NO_CREATED       The target process is not created.
185  * @retval #UINT32                          [0, LOS_CPUP_PRECISION], CPU usage of the specified process.
186  * @par Dependency:
187  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
188  * @see
189  */
190 extern UINT32 LOS_HistoryProcessCpuUsage(UINT32 pid, UINT16 mode);
191 
192 /**
193  * @ingroup los_cpup
194  * @brief  Obtain the historical CPU usage of a specified task.
195  *
196  * @par Description:
197  * This API is used to obtain the historical CPU usage of a task specified by a passed-in task ID.
198  * @attention
199  * <ul>
200  * <li>This API can be called only after the CPU usage is initialized. Otherwise,
201  * the CPU usage fails to be obtained.</li>
202  * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
203  * the CPU usage fails to be obtained.</li>
204  * </ul>
205  *
206  * @param tid      [IN] UINT32. task ID.
207  * @param mode     [IN] UINT16. cpup mode. The parameter value 0 indicates that the CPU usage within 10s will be
208  *                              obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will
209  *                              be obtained. Other values indicate that the CPU usage in the period that is less than
210  *                              1s will be obtained.
211  *
212  * @retval #LOS_ERRNO_CPUP_NO_INIT          The CPU usage is not initialized.
213  * @retval #LOS_ERRNO_CPUP_ID_INVALID       The target task ID is invalid.
214  * @retval #LOS_ERRNO_CPUP_NO_CREATED       The target task is not created.
215  * @retval #UINT32                          [0, LOS_CPUP_SINGLE_CORE_PRECISION], CPU usage of the specified process.
216  * @par Dependency:
217  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
218  * @see
219  */
220 extern UINT32 LOS_HistoryTaskCpuUsage(UINT32 tid, UINT16 mode);
221 
222 /**
223  * @ingroup los_cpup
224  * @brief Obtain the CPU usage of processes.
225  *
226  * @par Description:
227  * This API is used to obtain the CPU usage of processes.
228  * @attention
229  * <ul>
230  * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails to be
231  * obtained.</li>
232  * <li>The input parameter pointer must not be NULL, Otherwise, the CPU usage fails to be obtained.</li>
233  * <li>The input parameter pointer should point to the structure array whose size be greater than
234  * (LOS_GetSystemProcessMaximum() * sizeof (CPUP_INFO_S)).</li>
235  * </ul>
236  *
237  * @param mode       [IN] UINT16. Time mode. The parameter value 0 indicates that the CPU usage within 10s will be
238  *                                obtained, and the parameter value 1 indicates that the CPU usage in the former 1s
239  *                                will be obtained.Other values indicate that the CPU usage in all time will be
240  *                                obtained.
241  * @param cpupInfo   [OUT]Type.   CPUP_INFO_S* Pointer to the CPUP information structure to be obtained.
242  * @param len        [IN] UINT32. The Maximum length of processes.
243  *
244  * @retval #LOS_ERRNO_CPUP_NO_INIT                  The CPU usage is not initialized.
245  * @retval #LOS_ERRNO_CPUP_PROCESS_PTR_ERR          The input parameter pointer is NULL or
246  *                                                  len less than LOS_GetSystemProcessMaximum() * sizeof (CPUP_INFO_S).
247  * @retval #LOS_OK                                  The CPU usage of all processes is successfully obtained.
248  * @par Dependency:
249  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
250  * @see
251  */
252 extern UINT32 LOS_GetAllProcessCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len);
253 
254 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
255 /**
256  * @ingroup los_cpup
257  * @brief Obtain the CPU usage of hwi.
258  *
259  * @par Description:
260  * This API is used to obtain the CPU usage of hwi.
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
264  * obtained.</li>
265  * <li>The input parameter pointer must not be NULL, Otherwise, the CPU usage fails to be obtained.</li>
266  * <li>The input parameter pointer should point to the structure array whose size be greater than
267  * (LOS_GetSystemHwiMaximum() * sizeof (CPUP_INFO_S)).</li>
268  * </ul>
269  *
270  * @param mode       [IN] UINT16. Time mode. The parameter value 0 indicates that the CPU usage within 10s will be
271  *                                obtained, and the parameter value 1 indicates that the CPU usage in the former 1s
272  *                                will be obtained.Other values indicate that the CPU usage in all time will be
273  *                                obtained.
274  * @param cpupInfo   [OUT]Type.   CPUP_INFO_S* Pointer to the CPUP information structure to be obtained.
275  * @param len        [IN] UINT32. The Maximum length of hwis.
276  *
277  * @retval #LOS_ERRNO_CPUP_NO_INIT                  The CPU usage is not initialized.
278  * @retval #LOS_ERRNO_CPUP_PROCESS_PTR_ERR          The input parameter pointer is NULL or
279  *                                                  len less than LOS_GetSystemHwiMaximum() * sizeof (CPUP_INFO_S).
280  * @retval #LOS_OK                                  The CPU usage of all hwis is successfully obtained.
281  * @par Dependency:
282  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
283  * @see
284  */
285 extern UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len);
286 #endif
287 
288 /**
289  * @ingroup los_cpup
290  * @brief Reset the data of CPU usage.
291  *
292  * @par Description:
293  * This API is used to reset the data of CPU usage.
294  * @attention
295  * <ul>
296  * <li>None.</li>
297  * </ul>
298  *
299  * @param None.
300  *
301  * @retval #None.
302  *
303  * @par Dependency:
304  * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
305  * @see
306  */
307 extern VOID LOS_CpupReset(VOID);
308 
309 #ifdef __cplusplus
310 #if __cplusplus
311 }
312 #endif /* __cplusplus */
313 #endif /* __cplusplus */
314 #endif
315