1 /* 2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved. 3 * 4 * UniProton is licensed under Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * http://license.coscl.org.cn/MulanPSL2 8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 11 * See the Mulan PSL v2 for more details. 12 * Create: 2009-12-22 13 * Description: 线程级CPU占用率模块的C文件 14 */ 15 #include "prt_cpup_thread_internal.h" 16 17 /* 当前cpu占用率统计时间窗起始时间 */ 18 OS_SEC_BSS U64 g_cpuWinStart; 19 /* 在计算线程级CPUP时,创建但已删除任务的CPU时间总大小 */ 20 OS_SEC_BSS U64 g_cpuTimeDelTask; 21 OS_SEC_BSS U16 g_cpupDelTask; 22 23 /* 硬中断、Tick钩子是否需要计算CPUP标识 */ 24 OS_SEC_BSS U32 g_cpupFlag; 25 OsCpupGet(void)26OS_SEC_L2_TEXT U32 OsCpupGet(void) 27 { 28 return (U32)(CPUP_USE_RATE - g_cpup[TSK_GET_INDEX(IDLE_TASK_ID)].usage); 29 } 30 31 /* 32 * 描述:有tick模式下的cpu占用率采样,在tick中断中被调用,每个tick调用一次,当到达采样间隔时间时计算cpu 33 * 占用率,本函数放在tick处理函数中被调用 34 */ OsCpupThreadTickTask(void)35OS_SEC_L2_TEXT void OsCpupThreadTickTask(void) 36 { 37 /* 采样间隔,单位tick */ 38 U32 ticksPerSample = g_ticksPerSample; 39 40 /* 每个tick调用一次,当tick数到达采样间隔时计算cpu占用率,全程关中断 */ 41 g_tickCount++; 42 43 if (g_tickCount >= ticksPerSample) { 44 OsCpupTickCal(); 45 46 #if defined(OS_OPTION_CPUP_WARN) 47 /* cpup告警检测 */ 48 if (g_cpupWarnCheck != NULL) { 49 g_cpupWarnCheck(); 50 } 51 #endif 52 53 g_tickCount -= ticksPerSample; 54 55 OsCpupTimeClear(); 56 } 57 } 58 59 /* 60 * 描述:参数检查接口,返回OS_OK成功,其他为错误码 61 */ OsCpupPreCheck(void)62OS_SEC_L2_TEXT U32 OsCpupPreCheck(void) 63 { 64 if (!OsCpupInitIsDone()) { 65 return OS_ERRNO_CPUP_NOT_INITED; 66 } 67 68 if ((UNI_FLAG & OS_FLG_BGD_ACTIVE) == 0) { 69 return OS_ERRNO_CPUP_OS_NOT_STARTED; 70 } 71 72 return OS_OK; 73 } 74