154568cb3Sopenharmony_ci/*
254568cb3Sopenharmony_ci * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
354568cb3Sopenharmony_ci *
454568cb3Sopenharmony_ci * UniProton is licensed under Mulan PSL v2.
554568cb3Sopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
654568cb3Sopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
754568cb3Sopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
854568cb3Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
954568cb3Sopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1054568cb3Sopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1154568cb3Sopenharmony_ci * See the Mulan PSL v2 for more details.
1254568cb3Sopenharmony_ci * Create: 2009-12-22
1354568cb3Sopenharmony_ci * Description: CPU占用率模块的C文件
1454568cb3Sopenharmony_ci */
1554568cb3Sopenharmony_ci#include "prt_cpup_internal.h"
1654568cb3Sopenharmony_ci#include "prt_cpup_thread_internal.h"
1754568cb3Sopenharmony_ci
1854568cb3Sopenharmony_ci/* 数据定义放到XXX.c中是为了解决SAI报Data Module */
1954568cb3Sopenharmony_ci#if defined(OS_OPTION_CPUP_WARN)
2054568cb3Sopenharmony_ci/* CPUP告警检测函数钩子 */
2154568cb3Sopenharmony_ciOS_SEC_BSS CpupWarnFunc g_cpupWarnCheck;
2254568cb3Sopenharmony_ci#endif
2354568cb3Sopenharmony_ci
2454568cb3Sopenharmony_ciOS_SEC_BSS CpupNowFunc g_cpupNow;
2554568cb3Sopenharmony_ci/* 系统级CPUP一个计算周期的的tick数 */
2654568cb3Sopenharmony_ciOS_SEC_BSS U32 g_ticksPerSample;
2754568cb3Sopenharmony_ci/* 当前系统级CPUP计数 */
2854568cb3Sopenharmony_ciOS_SEC_BSS U32 g_cpupIndex;
2954568cb3Sopenharmony_ci/* 线程级CPU占用率结构体指针 */
3054568cb3Sopenharmony_ciOS_SEC_BSS struct TagCpupThread *g_cpup;
3154568cb3Sopenharmony_ci
3254568cb3Sopenharmony_ci/* 一个采样周期内,当前Tick计数值 */
3354568cb3Sopenharmony_ciOS_SEC_BSS U32 g_tickCount;
3454568cb3Sopenharmony_ci/* CPU占用率采样周期值 */
3554568cb3Sopenharmony_ciOS_SEC_BSS U64 g_baseValue;
3654568cb3Sopenharmony_ci
3754568cb3Sopenharmony_ciOS_SEC_L4_TEXT U32 OsCpupRegister(struct CpupModInfo *modInfo)
3854568cb3Sopenharmony_ci{
3954568cb3Sopenharmony_ci    U32 ret;
4054568cb3Sopenharmony_ci
4154568cb3Sopenharmony_ci    ret = OsCpupReg(modInfo);
4254568cb3Sopenharmony_ci    if (ret != OS_OK) {
4354568cb3Sopenharmony_ci        return ret;
4454568cb3Sopenharmony_ci    }
4554568cb3Sopenharmony_ci
4654568cb3Sopenharmony_ci#if defined(OS_OPTION_CPUP_WARN)
4754568cb3Sopenharmony_ci    if (modInfo->cpupWarnFlag == TRUE) {
4854568cb3Sopenharmony_ci        ret = OsCpupWarnReg(modInfo);
4954568cb3Sopenharmony_ci        if (ret != OS_OK) {
5054568cb3Sopenharmony_ci            return ret;
5154568cb3Sopenharmony_ci        }
5254568cb3Sopenharmony_ci    }
5354568cb3Sopenharmony_ci#endif
5454568cb3Sopenharmony_ci
5554568cb3Sopenharmony_ci    return OS_OK;
5654568cb3Sopenharmony_ci}
5754568cb3Sopenharmony_ci
5854568cb3Sopenharmony_ci/*
5954568cb3Sopenharmony_ci * 描述:模块间接口,获取CPUP是否已经初始化。
6054568cb3Sopenharmony_ci */
6154568cb3Sopenharmony_ciOS_SEC_L4_TEXT bool OsCpupInitIsDone(void)
6254568cb3Sopenharmony_ci{
6354568cb3Sopenharmony_ci    if (g_cpupNow == NULL) {
6454568cb3Sopenharmony_ci        return FALSE;
6554568cb3Sopenharmony_ci    }
6654568cb3Sopenharmony_ci    return TRUE;
6754568cb3Sopenharmony_ci}
6854568cb3Sopenharmony_ci
6954568cb3Sopenharmony_ci/*
7054568cb3Sopenharmony_ci * 描述:根据g_cpupThreadInitFlag判断是否已经初始化CPUP模块如果没有,则进行初始化
7154568cb3Sopenharmony_ci */
7254568cb3Sopenharmony_ciOS_SEC_L2_TEXT U32 OsCpupLazyInit(void)
7354568cb3Sopenharmony_ci{
7454568cb3Sopenharmony_ci    if (g_cpupNow == NULL) {
7554568cb3Sopenharmony_ci        return OS_ERRNO_CPUP_NOT_INITED;
7654568cb3Sopenharmony_ci    }
7754568cb3Sopenharmony_ci    return OS_OK;
7854568cb3Sopenharmony_ci}
79