• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_internal.h"
16 
17 OS_SEC_BSS struct TagOsCpupWarnInfo g_cpupWarnInfo;
18 
19 /*
20  * 描述:初始化CPUP告警模块
21  */
OsCpupWarnInit(void)22 OS_SEC_L4_TEXT void OsCpupWarnInit(void)
23 {
24     g_cpupWarnCheck = OsCpupWarn;
25 }
26 
27 /*
28  * 描述:CPUP告警,调用用户注册的钩子函数上报信息通知用户
29  */
OsCpupWarn(void)30 OS_SEC_L2_TEXT void OsCpupWarn(void)
31 {
32     struct CpupWarnInfo warnInfo;
33     U32 cpup;
34     static bool warn = FALSE;
35 
36     cpup = OsCpupGet();
37     if (cpup > g_cpupWarnInfo.warn) { /* 当超过阀值时,发出告警信息。 */
38         /* 当超过阈值时,发出告警信息。 */
39         if ((warn == FALSE) && (g_hookCb[OS_HOOK_CPUP_WARN].sigHook != NULL)) {
40             warn = TRUE;
41             warnInfo.type = CPUP_INFO_TYPE_OVERLOAD;
42             OS_SHOOK_ACTIVATE_PARA1(OS_HOOK_CPUP_WARN, &warnInfo);
43         }
44     } else if (cpup < g_cpupWarnInfo.resume) {
45         if (warn == TRUE) {
46             warn = FALSE;
47             if (g_hookCb[OS_HOOK_CPUP_WARN].sigHook != NULL) {
48                 warnInfo.type = CPUP_INFO_TYPE_RECONVERT;
49                 OS_SHOOK_ACTIVATE_PARA1(OS_HOOK_CPUP_WARN, &warnInfo);
50             }
51         }
52     }
53 }
54 
55 /*
56  * 描述:告警门限和告警恢复门限的设置
57  */
PRT_CpupSetWarnValue(U32 warn,U32 resume)58 OS_SEC_L4_TEXT U32 PRT_CpupSetWarnValue(U32 warn, U32 resume)
59 {
60     uintptr_t intSave;
61 
62     if (!OsCpupInitIsDone()) {
63         return OS_ERRNO_CPUP_NOT_INITED;
64     }
65 
66     if ((UNI_FLAG & OS_FLG_BGD_ACTIVE) == 0) {
67         return OS_ERRNO_CPUP_OS_NOT_STARTED;
68     }
69 
70     if (warn > CPUP_USE_RATE || warn == 0) {
71         return OS_ERRNO_CPUP_INTERVAL_NOT_SUITED;
72     }
73 
74     if (resume >= warn) {
75         return OS_ERRNO_CPUP_RESUME_NOT_SUITED;
76     }
77 
78     intSave = OsIntLock();
79 
80     g_cpupWarnInfo.warn = warn;
81     g_cpupWarnInfo.resume = resume;
82 
83     OsIntRestore(intSave);
84 
85     return OS_OK;
86 }
87 
88 /*
89  * 描述:查询告警阈值和告警恢复阈值
90  */
PRT_CpupGetWarnValue(U32 * warn,U32 * resume)91 OS_SEC_L4_TEXT U32 PRT_CpupGetWarnValue(U32 *warn, U32 *resume)
92 {
93     uintptr_t intSave;
94 
95     if (!OsCpupInitIsDone()) {
96         return OS_ERRNO_CPUP_NOT_INITED;
97     }
98 
99     if ((UNI_FLAG & OS_FLG_BGD_ACTIVE) == 0) {
100         return OS_ERRNO_CPUP_OS_NOT_STARTED;
101     }
102 
103     if (warn == NULL || resume == NULL) {
104         return OS_ERRNO_CPUP_PTR_NULL;
105     }
106 
107     intSave = OsIntLock();
108 
109     *warn = g_cpupWarnInfo.warn;
110     *resume = g_cpupWarnInfo.resume;
111 
112     OsIntRestore(intSave);
113 
114     return OS_OK;
115 }
116 
117 /*
118  * 描述:注册CPUP告警钩子函数
119  */
PRT_CpupRegWarnHook(CpupHookFunc hook)120 OS_SEC_L4_TEXT U32 PRT_CpupRegWarnHook(CpupHookFunc hook)
121 {
122     return OsShookReg(OS_HOOK_CPUP_WARN, (OsVoidFunc)hook);
123 }
124 
OsCpupWarnReg(struct CpupModInfo * modInfo)125 OS_SEC_L4_TEXT U32 OsCpupWarnReg(struct CpupModInfo *modInfo)
126 {
127     if (modInfo->warn == 0 || modInfo->warn > CPUP_USE_RATE) {
128         return OS_ERRNO_CPUP_INTERVAL_NOT_SUITED;
129     }
130 
131     if (modInfo->resume >= modInfo->warn) {
132         return OS_ERRNO_CPUP_RESUME_VALUE_ERROR;
133     }
134 
135     g_cpupWarnInfo.warn = modInfo->warn;
136     g_cpupWarnInfo.resume = modInfo->resume;
137 
138     return OS_OK;
139 }
140