1 /*
2 * Copyright (c) 2022 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 "nlohmann/json.hpp"
20
21 namespace OHOS {
22 namespace ResourceSchedule {
23 namespace {
24 #ifdef __aarch64__
25 const std::string RES_SCHED_SERVICE_SO = "/system/lib64/libresschedsvc.z.so";
26 const std::string RES_SCHED_CG_EXT_SO = "/system/lib64/libcgroup_sched_ext.z.so";
27 #else
28 const std::string RES_SCHED_SERVICE_SO = "/system/lib/libresschedsvc.z.so";
29 const std::string RES_SCHED_CG_EXT_SO = "/system/lib/libcgroup_sched_ext.z.so";
30 #endif
31 constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_RMS, "ResSchedUtils"};
32 }
33
GetInstance()34 ResSchedUtils& ResSchedUtils::GetInstance()
35 {
36 static ResSchedUtils instance;
37 return instance;
38 }
39
LoadUtils()40 void ResSchedUtils::LoadUtils()
41 {
42 auto handle = dlopen(RES_SCHED_SERVICE_SO.c_str(), RTLD_NOW);
43 if (!handle) {
44 CGS_LOGW("%{public}s load %{public}s failed!", __func__, RES_SCHED_SERVICE_SO.c_str());
45 return;
46 }
47
48 reportFunc_ = reinterpret_cast<ReportDataFunc>(dlsym(handle, "ReportDataInProcess"));
49 if (!reportFunc_) {
50 CGS_LOGW("%{public}s load function:ReportDataInProcess failed!", __func__);
51 dlclose(handle);
52 return;
53 }
54 }
55
LoadUtilsExtra()56 void ResSchedUtils::LoadUtilsExtra()
57 {
58 auto handle = dlopen(RES_SCHED_CG_EXT_SO.c_str(), RTLD_NOW);
59 if (!handle) {
60 CGS_LOGD("%{public}s load %{public}s failed! errno:%{public}d", __func__, RES_SCHED_CG_EXT_SO.c_str(), errno);
61 return;
62 }
63
64 reportArbitrationResultFunc_ =
65 reinterpret_cast<ReportArbitrationResultFunc>(dlsym(handle, "ReportArbitrationResult"));
66 if (!reportArbitrationResultFunc_) {
67 CGS_LOGD("%{public}s load function:ReportArbitrationResult failed! errno:%{public}d", __func__, errno);
68 dlclose(handle);
69 return;
70 }
71 }
72
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)73 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
74 {
75 if (!reportFunc_) {
76 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
77 return;
78 }
79 reportFunc_(resType, value, payload);
80 }
81
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)82 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
83 {
84 if (!reportArbitrationResultFunc_) {
85 CGS_LOGD("%{public}s failed, function nullptr.", __func__);
86 return;
87 }
88 reportArbitrationResultFunc_(app, pr, source);
89 }
90 } // namespace ResourceSchedule
91 } // namespace OHOS
92