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_ =
100 reinterpret_cast<DispatchResourceExtFunc>(dlsym(handle, "DispatchResourceExt"));
101 if (!dispatchResourceExtFunc_) {
102 CGS_LOGD("%{public}s load function:DispatchResourceExt failed! errno:%{public}d", __func__, errno);
103 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
104 "COMPONENT_NAME", "MAIN",
105 "ERR_TYPE", "plugin failure",
106 "ERR_MSG", "ResSchedUtils dlsym 'DispatchResourceExt' in " + RES_SCHED_CG_EXT_SO + "!");
107 dlclose(handle);
108 return;
109 }
110
111 reportSysEventFunc_ =
112 reinterpret_cast<ReportSysEventFunc>(dlsym(handle, "ReportSysEvent"));
113 if (!reportSysEventFunc_) {
114 CGS_LOGD("%{public}s load function:ReportSysEvent failed! errno:%{public}d", __func__, errno);
115 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
116 "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
117 "ERR_TYPE", "plugin failure",
118 "ERR_MSG", "ResSchedUtils don't found ReportSysEvent in " + RES_SCHED_CG_EXT_SO + "!");
119 dlclose(handle);
120 return;
121 }
122 }
123
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)124 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
125 {
126 if (!reportFunc_) {
127 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
128 return;
129 }
130 reportFunc_(resType, value, payload);
131 }
132
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)133 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
134 {
135 if (!reportArbitrationResultFunc_) {
136 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
137 return;
138 }
139 reportArbitrationResultFunc_(app, pr, source);
140 }
141
ReportSysEvent(Application & app,ProcessRecord & pr,uint32_t resType,int32_t state)142 void ResSchedUtils::ReportSysEvent(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state)
143 {
144 if (!reportSysEventFunc_) {
145 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
146 return;
147 }
148 reportSysEventFunc_(app, pr, resType, state);
149 }
150
CheckTidIsInPid(int32_t pid,int32_t tid)151 bool ResSchedUtils::CheckTidIsInPid(int32_t pid, int32_t tid)
152 {
153 nlohmann::json payload;
154 payload["pid"] = pid;
155 payload["tid"] = tid;
156 nlohmann::json reply;
157 ResourceSchedule::ResSchedExeClient::GetInstance().SendRequestSync(
158 ResExeType::RES_TYPE_CGROUP_PROC_TASK_SYNC_EVENT, 0, payload, reply);
159 std::string resStr{"res"};
160 if (!reply.contains(resStr) || !reply[resStr].is_boolean()) {
161 return false;
162 }
163 return reply[resStr];
164 }
165
GetProcessFilePath(int32_t uid,std::string bundleName,int32_t pid)166 std::string ResSchedUtils::GetProcessFilePath(int32_t uid, std::string bundleName, int32_t pid)
167 {
168 if (uid < 0 || pid < 0) {
169 CGS_LOGE("%{public}s Parameter Error: UID: %{public}d, PID: %{public}d", __func__, uid, pid);
170 return "";
171 }
172 int32_t userId = uid / UID_TRANSFORM_DIVISOR;
173 std::string path;
174 path.append("/dev/pids/")
175 .append(std::to_string(userId))
176 .append("/")
177 .append(bundleName)
178 .append("/app_")
179 .append(std::to_string(pid))
180 .append("/cgroup.procs");
181 char absolutePath[PATH_MAX] = {0};
182 if (!realpath(path.c_str(), absolutePath)) {
183 CGS_LOGD("%{public}s Get Proc File Path Error.", __func__);
184 return "";
185 }
186 return std::string(absolutePath);
187 }
188
DispatchResourceExt(uint32_t resType,int64_t value,const nlohmann::json & payload)189 void ResSchedUtils::DispatchResourceExt(uint32_t resType, int64_t value, const nlohmann::json& payload)
190 {
191 if (!dispatchResourceExtFunc_) {
192 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
193 return;
194 }
195 dispatchResourceExtFunc_(resType, value, payload);
196 }
197
ReportAppStateInProcess(int32_t state,int32_t pid)198 void ResSchedUtils::ReportAppStateInProcess(int32_t state, int32_t pid)
199 {
200 if (!reportAppStateFunc_) {
201 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
202 return;
203 }
204 reportAppStateFunc_(state, pid);
205 }
206 } // namespace ResourceSchedule
207 } // namespace OHOS