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", RES_SCHED_SERVICE_SO,
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", RES_SCHED_SERVICE_SO,
58 "ERR_TYPE", "plugin failure",
59 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
60 dlclose(handle);
61 return;
62 }
63 }
64
LoadUtilsExtra()65 void ResSchedUtils::LoadUtilsExtra()
66 {
67 auto handle = dlopen(RES_SCHED_CG_EXT_SO.c_str(), RTLD_NOW);
68 if (!handle) {
69 CGS_LOGD("%{public}s load %{public}s failed! errno:%{public}d", __func__, RES_SCHED_CG_EXT_SO.c_str(), errno);
70 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
71 "COMPONENT_NAME", RES_SCHED_CG_EXT_SO,
72 "ERR_TYPE", "plugin failure",
73 "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_CG_EXT_SO + " failed!");
74 return;
75 }
76
77 reportArbitrationResultFunc_ =
78 reinterpret_cast<ReportArbitrationResultFunc>(dlsym(handle, "ReportArbitrationResult"));
79 if (!reportArbitrationResultFunc_) {
80 CGS_LOGD("%{public}s load function:ReportArbitrationResult failed! errno:%{public}d", __func__, errno);
81 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
82 "COMPONENT_NAME", RES_SCHED_CG_EXT_SO,
83 "ERR_TYPE", "plugin failure",
84 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_CG_EXT_SO + "!");
85 dlclose(handle);
86 return;
87 }
88
89 dispatchResourceExtFunc_ = reinterpret_cast<DispatchResourceExtFunc>(dlsym(handle, "DispatchResourceExt"));
90 if (!dispatchResourceExtFunc_) {
91 CGS_LOGD("%{public}s load function:DispatchResourceExt failed! errno:%{public}d", __func__, errno);
92 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
93 "COMPONENT_NAME", RES_SCHED_CG_EXT_SO,
94 "ERR_TYPE", "plugin failure",
95 "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_CG_EXT_SO + "!");
96 dlclose(handle);
97 return;
98 }
99
100 reportSysEventFunc_ = reinterpret_cast<ReportSysEventFunc>(dlsym(handle, "ReportSysEvent"));
101 if (!reportSysEventFunc_) {
102 CGS_LOGD("%{public}s load function:ReportSysEvent failed! errno:%{public}d", __func__, errno);
103 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
104 "COMPONENT_NAME", RES_SCHED_CG_EXT_SO,
105 "ERR_TYPE", "plugin failure",
106 "ERR_MSG", "ResSchedUtils don't found ReportSysEvent in " + RES_SCHED_CG_EXT_SO + "!");
107 dlclose(handle);
108 return;
109 }
110
111 subscribeResourceExtFunc_ = reinterpret_cast<SubscribeResourceExtFunc>(dlsym(handle, "SubscribeResourceExt"));
112 if (!subscribeResourceExtFunc_) {
113 CGS_LOGD("%{public}s load function:SubscribeResourceExtFunc failed! errno:%{public}d", __func__, errno);
114 dlclose(handle);
115 return;
116 }
117 }
118
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)119 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
120 {
121 if (!reportFunc_) {
122 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
123 return;
124 }
125 reportFunc_(resType, value, payload);
126 }
127
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)128 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
129 {
130 if (!reportArbitrationResultFunc_) {
131 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
132 return;
133 }
134 reportArbitrationResultFunc_(app, pr, source);
135 }
136
ReportSysEvent(Application & app,ProcessRecord & pr,uint32_t resType,int32_t state)137 void ResSchedUtils::ReportSysEvent(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state)
138 {
139 if (!reportSysEventFunc_) {
140 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
141 return;
142 }
143 reportSysEventFunc_(app, pr, resType, state);
144 }
145
GetProcessFilePath(int32_t uid,std::string bundleName,int32_t pid)146 std::string ResSchedUtils::GetProcessFilePath(int32_t uid, std::string bundleName, int32_t pid)
147 {
148 if (uid < 0 || pid < 0) {
149 CGS_LOGE("%{public}s Parameter Error: UID: %{public}d, PID: %{public}d", __func__, uid, pid);
150 return "";
151 }
152 int32_t userId = uid / UID_TRANSFORM_DIVISOR;
153 std::string path;
154 path.append("/dev/pids/")
155 .append(std::to_string(userId))
156 .append("/")
157 .append(bundleName)
158 .append("/app_")
159 .append(std::to_string(pid))
160 .append("/cgroup.procs");
161 char absolutePath[PATH_MAX] = {0};
162 if (!realpath(path.c_str(), absolutePath)) {
163 CGS_LOGD("%{public}s Get Proc File Path Error.", __func__);
164 return "";
165 }
166 return std::string(absolutePath);
167 }
168
DispatchResourceExt(uint32_t resType,int64_t value,const nlohmann::json & payload)169 void ResSchedUtils::DispatchResourceExt(uint32_t resType, int64_t value, const nlohmann::json& payload)
170 {
171 if (!dispatchResourceExtFunc_) {
172 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
173 return;
174 }
175 dispatchResourceExtFunc_(resType, value, payload);
176 }
177
CheckTidIsInPid(int32_t pid,int32_t tid)178 bool ResSchedUtils::CheckTidIsInPid(int32_t pid, int32_t tid)
179 {
180 nlohmann::json payload;
181 payload["pid"] = pid;
182 payload["tid"] = tid;
183 nlohmann::json reply;
184 ResourceSchedule::ResSchedExeClient::GetInstance().SendRequestSync(
185 ResExeType::RES_TYPE_CGROUP_PROC_TASK_SYNC_EVENT, 0, payload, reply);
186 std::string resStr{"res"};
187 if (!reply.contains(resStr) || !reply[resStr].is_boolean()) {
188 return false;
189 }
190 return reply[resStr];
191 }
192
RssExeSendRequestSync(uint32_t resType,int64_t value,const nlohmann::json & context,nlohmann::json & reply)193 int32_t ResSchedUtils::RssExeSendRequestSync(uint32_t resType, int64_t value,
194 const nlohmann::json& context, nlohmann::json& reply)
195 {
196 return ResourceSchedule::ResSchedExeClient::GetInstance().SendRequestSync(
197 resType, value, context, reply);
198 }
199
RssExeSendRequestAsync(uint32_t resType,int64_t value,const nlohmann::json & context)200 void ResSchedUtils::RssExeSendRequestAsync(uint32_t resType, int64_t value,
201 const nlohmann::json& context)
202 {
203 ResSchedExeClient::GetInstance().SendRequestAsync(resType, value, context);
204 }
205
SubscribeResourceExt()206 void ResSchedUtils::SubscribeResourceExt()
207 {
208 if (!subscribeResourceExtFunc_) {
209 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
210 return;
211 }
212 subscribeResourceExtFunc_();
213 }
214 } // namespace ResourceSchedule
215 } // namespace OHOS