1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ressched_utils.h"
16
17 #include <dlfcn.h>
18 #include "cgroup_sched_log.h"
19 #include "hisysevent.h"
20 #include "nlohmann/json.hpp"
21
22 #undef LOG_TAG
23 #define LOG_TAG "ResSchedUtils"
24
25 namespace OHOS {
26 namespace ResourceSchedule {
27 namespace {
28 const std::string RES_SCHED_SERVICE_SO = "libresschedsvc.z.so";
29 const std::string RES_SCHED_CG_EXT_SO = "libcgroup_sched_ext.z.so";
30 }
31
GetInstance()32 ResSchedUtils& ResSchedUtils::GetInstance()
33 {
34 static ResSchedUtils instance;
35 return instance;
36 }
37
LoadUtils()38 void ResSchedUtils::LoadUtils()
39 {
40 auto handle = dlopen(RES_SCHED_SERVICE_SO.c_str(), RTLD_NOW);
41 if (!handle) {
42 CGS_LOGW("%{public}s load %{public}s failed!", __func__, RES_SCHED_SERVICE_SO.c_str());
43 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
44 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
45 "ERR_TYPE", "plugin failure",
46 "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_SERVICE_SO + " failed!");
47 return;
48 }
49
50 reportFunc_ = reinterpret_cast<ReportDataFunc>(dlsym(handle, "ReportDataInProcess"));
51 if (!reportFunc_) {
52 CGS_LOGW("%{public}s load function:ReportDataInProcess failed!", __func__);
53 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
54 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
55 "ERR_TYPE", "plugin failure",
56 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
57 dlclose(handle);
58 return;
59 }
60 }
61
LoadUtilsExtra()62 void ResSchedUtils::LoadUtilsExtra()
63 {
64 auto handle = dlopen(RES_SCHED_CG_EXT_SO.c_str(), RTLD_NOW);
65 if (!handle) {
66 CGS_LOGD("%{public}s load %{public}s failed! errno:%{public}d", __func__, RES_SCHED_CG_EXT_SO.c_str(), errno);
67 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
68 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
69 "ERR_TYPE", "plugin failure",
70 "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_SERVICE_SO + " failed!");
71 return;
72 }
73
74 reportArbitrationResultFunc_ =
75 reinterpret_cast<ReportArbitrationResultFunc>(dlsym(handle, "ReportArbitrationResult"));
76 if (!reportArbitrationResultFunc_) {
77 CGS_LOGD("%{public}s load function:ReportArbitrationResult failed! errno:%{public}d", __func__, errno);
78 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
79 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
80 "ERR_TYPE", "plugin failure",
81 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
82 dlclose(handle);
83 return;
84 }
85
86 reportSysEventFunc_ =
87 reinterpret_cast<ReportSysEventFunc>(dlsym(handle, "ReportSysEvent"));
88 if (!reportSysEventFunc_) {
89 CGS_LOGD("%{public}s load function:ReportSysEvent failed! errno:%{public}d", __func__, errno);
90 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
91 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
92 "ERR_TYPE", "plugin failure",
93 "ERR_MSG", "ResSchedUtils don't found ReportSysEvent in " + RES_SCHED_SERVICE_SO + "!");
94 dlclose(handle);
95 return;
96 }
97 }
98
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)99 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
100 {
101 if (!reportFunc_) {
102 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
103 return;
104 }
105 reportFunc_(resType, value, payload);
106 }
107
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)108 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
109 {
110 if (!reportArbitrationResultFunc_) {
111 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
112 return;
113 }
114 reportArbitrationResultFunc_(app, pr, source);
115 }
116
ReportSysEvent(Application & app,ProcessRecord & pr,uint32_t resType,int32_t state)117 void ResSchedUtils::ReportSysEvent(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state)
118 {
119 if (!reportSysEventFunc_) {
120 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
121 return;
122 }
123 reportSysEventFunc_(app, pr, resType, state);
124 }
125 } // namespace ResourceSchedule
126 } // namespace OHOS
127