1 /*
2 * Copyright (c) 2022-2024 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 "res_exe_type.h"
21 #include "res_sched_exe_client.h"
22 #include "nlohmann/json.hpp"
23
24 #undef LOG_TAG
25 #define LOG_TAG "ResSchedUtils"
26
27 namespace OHOS {
28 namespace ResourceSchedule {
29 namespace {
30 const std::string RES_SCHED_SERVICE_SO = "libresschedsvc.z.so";
31 const std::string RES_SCHED_CG_EXT_SO = "libcgroup_sched_ext.z.so";
32 const int32_t UID_TRANSFORM_DIVISOR = 200000;
33 }
34
GetInstance()35 ResSchedUtils& ResSchedUtils::GetInstance()
36 {
37 static ResSchedUtils instance;
38 return instance;
39 }
40
LoadUtils()41 void ResSchedUtils::LoadUtils()
42 {
43 auto handle = dlopen(RES_SCHED_SERVICE_SO.c_str(), RTLD_NOW);
44 if (!handle) {
45 CGS_LOGW("%{public}s load %{public}s failed!", __func__, RES_SCHED_SERVICE_SO.c_str());
46 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
47 "COMPONENT_NAME", "MAIN",
48 "ERR_TYPE", "plugin failure",
49 "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_SERVICE_SO + " failed!");
50 return;
51 }
52
53 reportFunc_ = reinterpret_cast<ReportDataFunc>(dlsym(handle, "ReportDataInProcess"));
54 if (!reportFunc_) {
55 CGS_LOGW("%{public}s load function:ReportDataInProcess failed!", __func__);
56 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
57 "COMPONENT_NAME", "MAIN",
58 "ERR_TYPE", "plugin failure",
59 "ERR_MSG", "ResSchedUtils dlsym 'ReportDataInProcess' in " +
60 RES_SCHED_SERVICE_SO + " failed!");
61 dlclose(handle);
62 return;
63 }
64
65 reportAppStateFunc_ = reinterpret_cast<ReportAppStateFunc>(dlsym(handle, "ReportAppStateInProcess"));
66 if (!reportAppStateFunc_) {
67 CGS_LOGW("%{public}s load function:ReportAppStateInProcess failed! Due to %{public}s", __func__, dlerror());
68 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
69 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
70 "ERR_TYPE", "plugin failure",
71 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
72 }
73 }
74
LoadUtilsExtra()75 void ResSchedUtils::LoadUtilsExtra()
76 {
77 auto handle = dlopen(RES_SCHED_CG_EXT_SO.c_str(), RTLD_NOW);
78 if (!handle) {
79 CGS_LOGD("%{public}s load %{public}s failed! errno:%{public}d", __func__, RES_SCHED_CG_EXT_SO.c_str(), errno);
80 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
81 "COMPONENT_NAME", "MAIN",
82 "ERR_TYPE", "plugin failure",
83 "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_CG_EXT_SO + " failed!");
84 return;
85 }
86
87 reportArbitrationResultFunc_ =
88 reinterpret_cast<ReportArbitrationResultFunc>(dlsym(handle, "ReportArbitrationResult"));
89 if (!reportArbitrationResultFunc_) {
90 CGS_LOGD("%{public}s load function:ReportArbitrationResult failed! errno:%{public}d", __func__, errno);
91 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
92 "COMPONENT_NAME", "MAIN",
93 "ERR_TYPE", "plugin failure",
94 "ERR_MSG", "ResSchedUtils dlsym 'ReportArbitrationResult' in " + RES_SCHED_CG_EXT_SO + "!");
95 dlclose(handle);
96 return;
97 }
98
99 dispatchResourceExtFunc_ = reinterpret_cast<DispatchResourceExtFunc>(dlsym(handle, "DispatchResourceExt"));
100 if (!dispatchResourceExtFunc_) {
101 CGS_LOGD("%{public}s load function:DispatchResourceExt failed! errno:%{public}d", __func__, errno);
102 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
103 "COMPONENT_NAME", "MAIN",
104 "ERR_TYPE", "plugin failure",
105 "ERR_MSG", "ResSchedUtils dlsym 'DispatchResourceExt' in " + RES_SCHED_CG_EXT_SO + "!");
106 dlclose(handle);
107 return;
108 }
109
110 reportSysEventFunc_ = reinterpret_cast<ReportSysEventFunc>(dlsym(handle, "ReportSysEvent"));
111 if (!reportSysEventFunc_) {
112 CGS_LOGD("%{public}s load function:ReportSysEvent failed! errno:%{public}d", __func__, errno);
113 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
114 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
115 "ERR_TYPE", "plugin failure",
116 "ERR_MSG", "ResSchedUtils don't found ReportSysEvent in " + RES_SCHED_CG_EXT_SO + "!");
117 dlclose(handle);
118 return;
119 }
120
121 subscribeResourceExtFunc_ = reinterpret_cast<SubscribeResourceExtFunc>(dlsym(handle, "SubscribeResourceExt"));
122 if (!subscribeResourceExtFunc_) {
123 CGS_LOGD("%{public}s load function:SubscribeResourceExtFunc failed! errno:%{public}d", __func__, errno);
124 dlclose(handle);
125 return;
126 }
127 }
128
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)129 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
130 {
131 if (!reportFunc_) {
132 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
133 return;
134 }
135 reportFunc_(resType, value, payload);
136 }
137
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)138 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
139 {
140 if (!reportArbitrationResultFunc_) {
141 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
142 return;
143 }
144 reportArbitrationResultFunc_(app, pr, source);
145 }
146
ReportSysEvent(Application & app,ProcessRecord & pr,uint32_t resType,int32_t state)147 void ResSchedUtils::ReportSysEvent(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state)
148 {
149 if (!reportSysEventFunc_) {
150 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
151 return;
152 }
153 reportSysEventFunc_(app, pr, resType, state);
154 }
155
CheckTidIsInPid(int32_t pid,int32_t tid)156 bool ResSchedUtils::CheckTidIsInPid(int32_t pid, int32_t tid)
157 {
158 nlohmann::json payload;
159 payload["pid"] = pid;
160 payload["tid"] = tid;
161 nlohmann::json reply;
162 ResourceSchedule::ResSchedExeClient::GetInstance().SendRequestSync(
163 ResExeType::RES_TYPE_CGROUP_PROC_TASK_SYNC_EVENT, 0, payload, reply);
164 std::string resStr{"res"};
165 if (!reply.contains(resStr) || !reply[resStr].is_boolean()) {
166 return false;
167 }
168 return reply[resStr];
169 }
170
GetProcessFilePath(int32_t uid,std::string bundleName,int32_t pid)171 std::string ResSchedUtils::GetProcessFilePath(int32_t uid, std::string bundleName, int32_t pid)
172 {
173 if (uid < 0 || pid < 0) {
174 CGS_LOGE("%{public}s Parameter Error: UID: %{public}d, PID: %{public}d", __func__, uid, pid);
175 return "";
176 }
177 int32_t userId = uid / UID_TRANSFORM_DIVISOR;
178 std::string path;
179 path.append("/dev/pids/")
180 .append(std::to_string(userId))
181 .append("/")
182 .append(bundleName)
183 .append("/app_")
184 .append(std::to_string(pid))
185 .append("/cgroup.procs");
186 char absolutePath[PATH_MAX] = {0};
187 if (!realpath(path.c_str(), absolutePath)) {
188 CGS_LOGD("%{public}s Get Proc File Path Error.", __func__);
189 return "";
190 }
191 return std::string(absolutePath);
192 }
193
DispatchResourceExt(uint32_t resType,int64_t value,const nlohmann::json & payload)194 void ResSchedUtils::DispatchResourceExt(uint32_t resType, int64_t value, const nlohmann::json& payload)
195 {
196 if (!dispatchResourceExtFunc_) {
197 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
198 return;
199 }
200 dispatchResourceExtFunc_(resType, value, payload);
201 }
202
ReportAppStateInProcess(int32_t state,int32_t pid)203 void ResSchedUtils::ReportAppStateInProcess(int32_t state, int32_t pid)
204 {
205 if (!reportAppStateFunc_) {
206 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
207 return;
208 }
209 reportAppStateFunc_(state, pid);
210 }
211
SubscribeResourceExt()212 void ResSchedUtils::SubscribeResourceExt()
213 {
214 if (!subscribeResourceExtFunc_) {
215 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
216 return;
217 }
218 subscribeResourceExtFunc_();
219 }
220 } // namespace ResourceSchedule
221 } // namespace OHOS