los_cpup.h 14.1 KB
Newer Older
L
l00278955 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
Y
yinjiaming 已提交
3
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
L
l00278955 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @defgroup los_cpup CPU usage
 * @ingroup kernel
 */

#ifndef _LOS_CPUP_H
#define _LOS_CPUP_H

L
likailong 已提交
40
#include "los_interrupt.h"
L
l00278955 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#include "los_task.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */


/**
* @ingroup los_cpup
* CPU usage error code: The request for memory fails.
*
* Value: 0x02001e00
*
* Solution: Decrease the maximum number of tasks.
*/
#define LOS_ERRNO_CPUP_NO_MEMORY             LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x00)

/**
* @ingroup los_cpup
* CPU usage error code: The pointer to an input parameter is NULL.
*
* Value: 0x02001e01
*
* Solution: Check whether the pointer to the input parameter is usable.
*/
#define LOS_ERRNO_CPUP_TASK_PTR_NULL         LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x01)

/**
* @ingroup los_cpup
* CPU usage error code: The CPU usage is not initialized.
*
* Value: 0x02001e02
*
* Solution: Check whether the CPU usage is initialized.
*/
#define LOS_ERRNO_CPUP_NO_INIT               LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x02)

/**
* @ingroup los_cpup
* CPU usage error code: The number of threads is invalid.
*
* Value: 0x02001e03
*
* Solution: Check whether the number of threads is applicable for the current operation.
*/
#define LOS_ERRNO_CPUP_MAXNUM_INVALID        LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x03)

/**
* @ingroup los_cpup
* CPU usage error code: The target thread is not created.
*
* Value: 0x02001e04
*
* Solution: Check whether the target thread is created.
*/
#define LOS_ERRNO_CPUP_THREAD_NO_CREATED     LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x04)

/**
* @ingroup los_cpup
* CPU usage error code: The target task ID is invalid.
*
* Value: 0x02001e05
*
* Solution: Check whether the target task ID is applicable for the current operation.
*/
#define LOS_ERRNO_CPUP_TSK_ID_INVALID        LOS_ERRNO_OS_ERROR(LOS_MOD_CPUP, 0x05)

/**
* @ingroup los_cpup
* Sum of cpup with all tasks. It means the value of cpup is a permillage.
*/
#define LOS_CPUP_PRECISION                   1000

/**
* @ingroup los_cpup
* Multiple of current cpup precision change to percent.
*/
#define LOS_CPUP_PRECISION_MULT              (LOS_CPUP_PRECISION / 100)

/**
 * @ingroup los_cpup
L
likailong 已提交
124
 * Number of historical running time records
L
l00278955 已提交
125
 */
L
likailong 已提交
126
#define OS_CPUP_HISTORY_RECORD_NUM   10
L
l00278955 已提交
127 128 129

/**
 * @ingroup los_cpup
L
likailong 已提交
130
 * Count the CPU usage structures of a task.
L
l00278955 已提交
131
 */
L
likailong 已提交
132 133 134 135 136 137 138
typedef struct {
    UINT32 cpupID;                                        /**< Task ID */
    UINT16 status;                                        /**< Task status */
    UINT64 allTime;                                       /**< Total running time */
    UINT64 startTime;                                     /**< Time before a task is invoked */
    UINT64 historyTime[OS_CPUP_HISTORY_RECORD_NUM];       /**< Historical running time */
} OsCpupCB;
L
l00278955 已提交
139

L
likailong 已提交
140
extern OsCpupCB    *g_cpup;
L
l00278955 已提交
141

W
wangchen 已提交
142 143 144 145 146 147 148 149 150 151 152 153
#if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
typedef struct {
    UINT32 cpupID;                                        /**< Irq ID */
    UINT16 status;                                        /**< Irq status */
    UINT64 allTime;                                       /**< Total running time */
    UINT64 startTime;                                     /**< Time before a task is invoked */
    UINT64 timeMax;                                       /**< Irq samples count */
    UINT64 count;                                         /**< Irq samples count */
    UINT64 historyTime[OS_CPUP_HISTORY_RECORD_NUM];       /**< Historical running time */
} OsIrqCpupCB;
#endif

L
l00278955 已提交
154 155
/**
 * @ingroup los_cpup
L
likailong 已提交
156
 * @brief Initialization cpup.
L
l00278955 已提交
157 158
 *
 * @par Description:
L
likailong 已提交
159
 * This API is used to initialization cpup.
L
l00278955 已提交
160 161
 * @attention
 * <ul>
L
likailong 已提交
162
 * <li>None.</li>
L
l00278955 已提交
163 164
 * </ul>
 *
L
likailong 已提交
165
 * @param None.
L
l00278955 已提交
166
 *
L
likailong 已提交
167
 * @retval UINT32   Initialization result.
L
l00278955 已提交
168 169
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
L
likailong 已提交
170
 * @see None.
L
l00278955 已提交
171
 */
L
likailong 已提交
172
extern UINT32 OsCpupInit(VOID);
L
l00278955 已提交
173

W
wangchen 已提交
174
extern UINT32 OsCpupDaemonInit(VOID);
L
l00278955 已提交
175 176
/**
 * @ingroup los_cpup
L
likailong 已提交
177
 * @brief Start task to get cycles count in current task ending.
L
l00278955 已提交
178 179
 *
 * @par Description:
L
likailong 已提交
180
 * This API is used to start task to get cycles count in current task ending.
L
l00278955 已提交
181 182
 * @attention
 * <ul>
L
likailong 已提交
183
 * <li>None.</li>
L
l00278955 已提交
184 185
 * </ul>
 *
L
likailong 已提交
186
 * @param None.
L
l00278955 已提交
187
 *
L
likailong 已提交
188
 * @retval None.
L
l00278955 已提交
189 190
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
L
likailong 已提交
191
 * @see None.
L
l00278955 已提交
192
 */
L
likailong 已提交
193
extern VOID OsTskCycleEndStart(VOID);
L
l00278955 已提交
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/**
 * @ingroup los_cpup
 * Count the CPU usage structures of all tasks.
 */
typedef struct tagCpupInfo {
    UINT16 usStatus;            /**< save the cur task status     */
    UINT32 uwUsage;             /**< Usage. The value range is [0,1000].   */
} CPUP_INFO_S;

/**
 * @ingroup los_monitor
 * Type of the CPU usage query.
 */
typedef enum {
    SYS_CPU_USAGE = 0,   /* system cpu occupancy rate */
    TASK_CPU_USAGE,      /* task cpu occupancy rate */
} CPUP_TYPE_E;
L
l00278955 已提交
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/**
 * @ingroup los_monitor
 * Mode of the CPU usage query.
 */
typedef enum {
    CPUP_IN_10S = 0,     /* cpu occupancy rate in 10s */
    CPUP_IN_1S,          /* cpu occupancy rate in 1s */
    CPUP_LESS_THAN_1S,   /* cpu occupancy rate less than 1s, if the input mode is none of them, it will be this. */
} CPUP_MODE_E;

/**
 * @ingroup los_cpup
 * @brief Obtain the current CPU usage.
 *
 * @par Description:
 * This API is used to obtain the current CPU usage.
 * @attention
 * <ul>
 * <li>This API can be called only after the CPU usage is initialized. Otherwise, error codes will be returned.</li>
 * <li> The precision of the CPU usage can be adjusted by changing the value of the CPUP_PRECISION macro.</li>
 * </ul>
 *
 * @param None.
 *
 * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
zhushy_'s avatar
zhushy_ 已提交
238
 * @retval #cpup                            [0,1000], current CPU usage, of which the precision is adjustable.
239 240 241 242 243 244 245 246 247 248 249 250 251 252
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SysCpuUsage
 */
extern UINT32 LOS_SysCpuUsage(VOID);

/**
 * @ingroup los_cpup
 * @brief Obtain the historical CPU usage.
 *
 * @par Description:
 * This API is used to obtain the historical CPU usage.
 * @attention
 * <ul>
Y
yinjiaming 已提交
253 254
 * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
 * to be obtained.</li>
255 256 257 258 259 260 261
 * </ul>
 *
 * @param  mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s will be
 * obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained. Other values
 * indicate that the CPU usage in the period that is less than 1s will be obtained.
 *
 * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
zhushy_'s avatar
zhushy_ 已提交
262
 * @retval #cpup                            [0,1000], historical CPU usage, of which the precision is adjustable.
263 264 265 266 267 268 269 270 271 272 273 274 275 276
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_HistoryTaskCpuUsage
 */
extern UINT32 LOS_HistorySysCpuUsage(UINT16 mode);

/**
 * @ingroup los_cpup
 * @brief Obtain the CPU usage of a specified task.
 *
 * @par Description:
 * This API is used to obtain the CPU usage of a task specified by a passed-in task ID.
 * @attention
 * <ul>
Y
yinjiaming 已提交
277 278
 * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
 * to be obtained.</li>
279 280 281 282 283 284 285 286 287
 * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
 * the CPU usage fails to be obtained.</li>
 * </ul>
 *
 * @param taskID   [IN] UINT32. Task ID.
 *
 * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
 * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
 * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
zhushy_'s avatar
zhushy_ 已提交
288
 * @retval #cpup                              [0,1000], CPU usage of the specified task.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_HistoryTaskCpuUsage
 */
extern UINT32 LOS_TaskCpuUsage(UINT32 taskID);

/**
 * @ingroup los_cpup
 * @brief  Obtain the historical CPU usage of a specified task.
 *
 * @par Description:
 * This API is used to obtain the historical CPU usage of a task specified by a passed-in task ID.
 * @attention
 * <ul>
 * <li>This API can be called only after the CPU usage is initialized. Otherwise,
 * the CPU usage fails to be obtained.</li>
 * <li>The passed-in task ID must be valid and the task specified by the task ID must be created. Otherwise,
 * the CPU usage fails to be obtained.</li>
 * </ul>
 *
 * @param taskID   [IN] UINT32. Task ID.
 * @param mode     [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
 * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
 * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
 *
 * @retval #OS_ERRNO_CPUP_NO_INIT             0x02001e02: The CPU usage is not initialized.
 * @retval #OS_ERRNO_CPUP_TSK_ID_INVALID      0x02001e05: The target task ID is invalid.
 * @retval #OS_ERRNO_CPUP_THREAD_NO_CREATED   0x02001e04: The target thread is not created.
zhushy_'s avatar
zhushy_ 已提交
317
 * @retval #cpup                              [0,1000], CPU usage of the specified task.
318 319 320 321 322 323 324 325 326 327 328 329 330 331
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_HistorySysCpuUsage
 */
extern UINT32 LOS_HistoryTaskCpuUsage(UINT32 taskID, UINT16 mode);

/**
 * @ingroup los_cpup
 * @brief Obtain the CPU usage of all tasks.
 *
 * @par Description:
 * This API is used to obtain the CPU usage of all tasks according to maximum number of threads.
 * @attention
 * <ul>
Y
yinjiaming 已提交
332 333
 * <li>This API can be called only after the CPU usage is initialized. Otherwise, the CPU usage fails
 * to be obtained.</li>
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
 * <li>The input parameter pointer must not be NULL, Otherwise, the CPU usage fails to be obtained.</li>
 * </ul>
 *
 * @param cpupInfo    [OUT]Type.   CPUP_INFO_S* Pointer to the task CPUP information structure to be obtained.
 * @param mode        [IN] UINT16. Task mode. The parameter value 0 indicates that the CPU usage within 10s
 * will be obtained, and the parameter value 1 indicates that the CPU usage in the former 1s will be obtained.
 * Other values indicate that the CPU usage in the period that is less than 1s will be obtained.
 *
 * @retval #OS_ERRNO_CPUP_NO_INIT           0x02001e02: The CPU usage is not initialized.
 * @retval #OS_ERRNO_CPUP_TASK_PTR_NULL     0x02001e01: The input parameter pointer is NULL.
 * @retval #LOS_OK                          0x00000000: The CPU usage of all tasks is successfully obtained.
 * @par Dependency:
 * <ul><li>los_cpup.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SysCpuUsage
 */
extern UINT32 LOS_AllTaskCpuUsage(CPUP_INFO_S *cpupInfo, UINT16 mode);

/**
 * @ingroup los_monitor
 * @brief Obtain CPU usage history of certain task.
 *
 * @par Description:
 * This API is used to obtain CPU usage history of certain task.
 * @attention
 * <ul>
 * <li>This API can be called only after the CPU usage is initialized. Otherwise, -1 will be returned.</li>
 * <li> Only in SYS_CPU_USAGE type, uwTaskID is invalid.</li>
 * </ul>
 *
 * @param type        [IN] cpup type, SYS_CPU_USAGE and TASK_CPU_USAGE
 * @param mode        [IN] mode,CPUP_IN_10S = usage in 10s,CPUP_IN_1S = usage in last 1s,
zhushy_'s avatar
zhushy_ 已提交
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 367 368 369 370 371 372 373 374
 * @param taskID      [IN] task ID, Only in SYS_CPU_USAGE type, taskID is invalid
 *
 * @retval #OS_ERROR           -1:CPU usage info obtain failed.
 * @retval #LOS_OK              0:CPU usage info is successfully obtained.
 * @par Dependency:
 * <ul><li>los_monitor.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_CpupUsageMonitor
 */
extern UINT32 LOS_CpupUsageMonitor(CPUP_TYPE_E type, CPUP_MODE_E mode, UINT32 taskID);
L
l00278955 已提交
375

W
wangchen 已提交
376 377 378 379 380 381 382
#if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
extern VOID OsCpupIrqStart(UINT32 intNum);
extern VOID OsCpupIrqEnd(UINT32 intNum);
extern OsIrqCpupCB *OsGetIrqCpupArrayBase(VOID);
extern UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo);
#endif

L
l00278955 已提交
383 384 385 386 387 388 389
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _LOS_CPUP_H */