• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
23 namespace ResourceSchedule {
24 namespace {
25     const std::string RES_SCHED_SERVICE_SO = "libresschedsvc.z.so";
26     const std::string RES_SCHED_CG_EXT_SO = "libcgroup_sched_ext.z.so";
27     constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_RMS, "ResSchedUtils"};
28 }
29 
GetInstance()30 ResSchedUtils& ResSchedUtils::GetInstance()
31 {
32     static ResSchedUtils instance;
33     return instance;
34 }
35 
LoadUtils()36 void ResSchedUtils::LoadUtils()
37 {
38     auto handle = dlopen(RES_SCHED_SERVICE_SO.c_str(), RTLD_NOW);
39     if (!handle) {
40         CGS_LOGW("%{public}s load %{public}s failed!", __func__, RES_SCHED_SERVICE_SO.c_str());
41         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
42                         "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
43                         "ERR_TYPE", "plugin failure",
44                         "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_SERVICE_SO + " failed!");
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         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
52                         "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
53                         "ERR_TYPE", "plugin failure",
54                         "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
55         dlclose(handle);
56         return;
57     }
58 }
59 
LoadUtilsExtra()60 void ResSchedUtils::LoadUtilsExtra()
61 {
62     auto handle = dlopen(RES_SCHED_CG_EXT_SO.c_str(), RTLD_NOW);
63     if (!handle) {
64         CGS_LOGD("%{public}s load %{public}s failed! errno:%{public}d", __func__, RES_SCHED_CG_EXT_SO.c_str(), errno);
65         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
66                         "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
67                         "ERR_TYPE", "plugin failure",
68                         "ERR_MSG", "ResSchedUtils dlopen " + RES_SCHED_SERVICE_SO + " failed!");
69         return;
70     }
71 
72     reportArbitrationResultFunc_ =
73         reinterpret_cast<ReportArbitrationResultFunc>(dlsym(handle, "ReportArbitrationResult"));
74     if (!reportArbitrationResultFunc_) {
75         CGS_LOGD("%{public}s load function:ReportArbitrationResult failed! errno:%{public}d", __func__, errno);
76         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
77                         "COMPONENT_NAME", RES_SCHED_SERVICE_SO,
78                         "ERR_TYPE", "plugin failure",
79                         "ERR_MSG", "ResSchedUtils don't found dlsym " + RES_SCHED_SERVICE_SO + "!");
80         dlclose(handle);
81         return;
82     }
83 }
84 
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)85 void ResSchedUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
86 {
87     if (!reportFunc_) {
88         CGS_LOGD("%{public}s failed, function nullptr.", __func__);
89         return;
90     }
91     reportFunc_(resType, value, payload);
92 }
93 
ReportArbitrationResult(Application & app,ProcessRecord & pr,AdjustSource source)94 void ResSchedUtils::ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source)
95 {
96     if (!reportArbitrationResultFunc_) {
97         CGS_LOGD("%{public}s failed, function nullptr.", __func__);
98         return;
99     }
100     reportArbitrationResultFunc_(app, pr, source);
101 }
102 } // namespace ResourceSchedule
103 } // namespace OHOS
104