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
16 #include "cgroup_adjuster.h"
17
18 #include <unistd.h>
19 #include "app_mgr_constants.h"
20 #include "cgroup_event_handler.h"
21 #include "cgroup_sched_common.h"
22 #include "cgroup_sched_log.h"
23 #include "hitrace_meter.h"
24 #include "sched_controller.h"
25 #include "ressched_utils.h"
26 #include "res_type.h"
27 #include "wm_common.h"
28
29 namespace OHOS {
30 namespace ResourceSchedule {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_RMS, "CgroupAdjuster"};
33 }
34
35 using OHOS::AppExecFwk::ApplicationState;
36 using OHOS::AppExecFwk::AbilityState;
37 using OHOS::AppExecFwk::ExtensionState;
38 using OHOS::Rosen::WindowType;
39
GetInstance()40 CgroupAdjuster& CgroupAdjuster::GetInstance()
41 {
42 static CgroupAdjuster instance;
43 return instance;
44 }
45
InitAdjuster()46 void CgroupAdjuster::InitAdjuster()
47 {
48 // Trigger load shared library
49 (void)ResSchedUtils::GetInstance();
50 auto handler = SchedController::GetInstance().GetCgroupEventHandler();
51 if (handler) {
52 handler->PostTask([this] {
53 this->AdjustSelfProcessGroup();
54 });
55 }
56 }
57
AdjustProcessGroup(Application & app,ProcessRecord & pr,AdjustSource source)58 void CgroupAdjuster::AdjustProcessGroup(Application &app, ProcessRecord &pr, AdjustSource source)
59 {
60 CGS_LOGI("%{public}s for %{public}d, source : %{public}d", __func__, pr.GetPid(), source);
61 ComputeProcessGroup(app, pr, source);
62 ResSchedUtils::GetInstance().ReportArbitrationResult(app, pr, source);
63 ApplyProcessGroup(app, pr);
64
65 auto mainProcRecord = app.GetMainProcessRecord();
66 if (!mainProcRecord) {
67 CGS_LOGI("%{public}s mainProcRecord is null %{public}s %{public}d", __func__,
68 app.GetName().c_str(), app.GetUid());
69 return;
70 }
71 if (mainProcRecord->GetPid() != pr.GetPid()) {
72 return;
73 }
74
75 /* Let the sched group of render process follow the sched group of main process */
76 for (const auto &iter : app.GetPidsMap()) {
77 const auto &procRecord = iter.second;
78 if (procRecord && procRecord->isRenderProcess_) {
79 CGS_LOGI("%{public}s for %{public}d, source : %{public}d", __func__, procRecord->GetPid(), source);
80 procRecord->setSchedGroup_ = mainProcRecord->curSchedGroup_;
81 ApplyProcessGroup(app, *procRecord);
82 }
83 }
84 }
85
AdjustAllProcessGroup(Application & app,AdjustSource source)86 void CgroupAdjuster::AdjustAllProcessGroup(Application &app, AdjustSource source)
87 {
88 for (auto &iter : app.GetPidsMap()) {
89 const auto &procRecord = iter.second;
90 if (procRecord && !procRecord->isRenderProcess_) {
91 AdjustProcessGroup(app, *procRecord, source);
92 }
93 }
94 }
95
AdjustSelfProcessGroup()96 inline void CgroupAdjuster::AdjustSelfProcessGroup()
97 {
98 int pid = getpid();
99 int group = SP_FOREGROUND;
100 int ret = CgroupSetting::SetThreadGroupSchedPolicy(pid, group);
101 if (ret != 0) {
102 CGS_LOGE("%{public}s set %{public}d to group %{public}d failed, ret=%{public}d!", __func__, pid, group, ret);
103 }
104 }
105
ComputeProcessGroup(Application & app,ProcessRecord & pr,AdjustSource source)106 void CgroupAdjuster::ComputeProcessGroup(Application &app, ProcessRecord &pr, AdjustSource source)
107 {
108 SchedPolicy group = SP_DEFAULT;
109
110 {
111 ChronoScope cs("ComputeProcessGroup");
112 if (pr.isRenderProcess_) {
113 auto mainProcRecord = app.GetMainProcessRecord();
114 group = mainProcRecord ? mainProcRecord->curSchedGroup_ : SP_DEFAULT;
115 } else if (source == AdjustSource::ADJS_PROCESS_CREATE) {
116 group = SP_DEFAULT;
117 } else if (app.focusedProcess_) {
118 group = SP_TOP_APP;
119 } else {
120 if (pr.abilities_.size() == 0) {
121 group = SP_DEFAULT;
122 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
123 group = SP_BACKGROUND;
124 }
125 } else if (pr.IsVisible()) {
126 group = SP_FOREGROUND;
127 } else if (pr.HasServiceExtension()) {
128 group = SP_DEFAULT;
129 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
130 group = SP_BACKGROUND;
131 }
132 } else {
133 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
134 group = SP_BACKGROUND;
135 } else if (app.state_ == (int32_t)ApplicationState::APP_STATE_FOREGROUND) {
136 group = SP_FOREGROUND;
137 } else {
138 group = SP_DEFAULT;
139 }
140 }
141 }
142 pr.setSchedGroup_ = group;
143 } // end ChronoScope
144 }
145
ApplyProcessGroup(Application & app,ProcessRecord & pr)146 void CgroupAdjuster::ApplyProcessGroup(Application &app, ProcessRecord &pr)
147 {
148 ChronoScope cs("ApplyProcessGroup");
149 if (pr.curSchedGroup_ != pr.setSchedGroup_) {
150 pid_t pid = pr.GetPid();
151 int ret = CgroupSetting::SetThreadGroupSchedPolicy(pid, (int)pr.setSchedGroup_);
152 if (ret != 0) {
153 CGS_LOGE("%{public}s set %{public}d to group %{public}d failed, ret=%{public}d!",
154 __func__, pid, pr.setSchedGroup_, ret);
155 return;
156 }
157
158 pr.lastSchedGroup_ = pr.curSchedGroup_;
159 pr.curSchedGroup_ = pr.setSchedGroup_;
160 CGS_LOGI("%{public}s Set %{public}d's cgroup from %{public}d to %{public}d.",
161 __func__, pr.GetPid(), pr.lastSchedGroup_, pr.curSchedGroup_);
162
163 std::string traceStr(__func__);
164 traceStr.append(" for ").append(std::to_string(pid)).append(", group change from ")
165 .append(std::to_string((int32_t)(pr.lastSchedGroup_))).append(" to ")
166 .append(std::to_string((int32_t)(pr.curSchedGroup_)));
167 StartTrace(HITRACE_TAG_OHOS, traceStr);
168
169 nlohmann::json payload;
170 payload["pid"] = std::to_string(pr.GetPid());
171 payload["uid"] = std::to_string(pr.GetUid());
172 payload["name"] = app.GetName();
173 payload["oldGroup"] = std::to_string((int32_t)(pr.lastSchedGroup_));
174 payload["newGroup"] = std::to_string((int32_t)(pr.curSchedGroup_));
175 ResSchedUtils::GetInstance().ReportDataInProcess(ResType::RES_TYPE_CGROUP_ADJUSTER, 0, payload);
176
177 FinishTrace(HITRACE_TAG_OHOS);
178 }
179 }
180 } // namespace ResourceSchedule
181 } // namespace OHOS
182