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 ApplyProcessGroup(app, pr);
63 ResSchedUtils::GetInstance().ReportArbitrationResult(app, pr, source);
64 }
65
AdjustAllProcessGroup(Application & app,AdjustSource source)66 void CgroupAdjuster::AdjustAllProcessGroup(Application &app, AdjustSource source)
67 {
68 auto pidsMap = app.GetPidsMap();
69 for (auto iter = pidsMap.begin(); iter != pidsMap.end(); iter++) {
70 auto pr = iter->second;
71 if (pr.get()) {
72 AdjustProcessGroup(app, *(pr.get()), source);
73 }
74 }
75 }
76
AdjustSelfProcessGroup()77 inline void CgroupAdjuster::AdjustSelfProcessGroup()
78 {
79 int pid = getpid();
80 int group = SP_FOREGROUND;
81 int ret = CgroupSetting::SetThreadGroupSchedPolicy(pid, group);
82 if (ret != 0) {
83 CGS_LOGE("%{public}s set %{public}d to group %{public}d failed, ret=%{public}d!", __func__, pid, group, ret);
84 }
85 }
86
ComputeProcessGroup(Application & app,ProcessRecord & pr,AdjustSource source)87 void CgroupAdjuster::ComputeProcessGroup(Application &app, ProcessRecord &pr, AdjustSource source)
88 {
89 SchedPolicy group = SP_DEFAULT;
90
91 {
92 ChronoScope cs("ComputeProcessGroup");
93 if (source == AdjustSource::ADJS_PROCESS_CREATE) {
94 group = SP_DEFAULT;
95 } else if (app.focusedProcess_) {
96 group = SP_TOP_APP;
97 } else {
98 if (pr.abilities_.size() == 0) {
99 group = SP_DEFAULT;
100 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
101 group = SP_BACKGROUND;
102 }
103 } else if (pr.IsVisible()) {
104 group = SP_FOREGROUND;
105 } else if (pr.HasServiceExtension()) {
106 group = SP_DEFAULT;
107 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
108 group = SP_BACKGROUND;
109 }
110 } else {
111 if (app.state_ == (int32_t)ApplicationState::APP_STATE_BACKGROUND) {
112 group = SP_BACKGROUND;
113 } else if (app.state_ == (int32_t)ApplicationState::APP_STATE_FOREGROUND) {
114 group = SP_FOREGROUND;
115 } else {
116 group = SP_DEFAULT;
117 }
118 }
119 }
120 pr.setSchedGroup_ = group;
121 } // end ChronoScope
122 }
123
ApplyProcessGroup(Application & app,ProcessRecord & pr)124 void CgroupAdjuster::ApplyProcessGroup(Application &app, ProcessRecord &pr)
125 {
126 ChronoScope cs("ApplyProcessGroup");
127 if (pr.curSchedGroup_ != pr.setSchedGroup_) {
128 pid_t pid = pr.GetPid();
129 int ret = CgroupSetting::SetThreadGroupSchedPolicy(pid, (int)pr.setSchedGroup_);
130 if (ret != 0) {
131 CGS_LOGE("%{public}s set %{public}d to group %{public}d failed, ret=%{public}d!",
132 __func__, pid, pr.setSchedGroup_, ret);
133 return;
134 }
135
136 pr.lastSchedGroup_ = pr.curSchedGroup_;
137 pr.curSchedGroup_ = pr.setSchedGroup_;
138 CGS_LOGI("%{public}s Set %{public}d's cgroup from %{public}d to %{public}d.",
139 __func__, pr.GetPid(), pr.lastSchedGroup_, pr.curSchedGroup_);
140
141 std::string traceStr(__func__);
142 traceStr.append(" for ").append(std::to_string(pid)).append(", group change from ")
143 .append(std::to_string((int32_t)(pr.lastSchedGroup_))).append(" to ")
144 .append(std::to_string((int32_t)(pr.curSchedGroup_)));
145 StartTrace(HITRACE_TAG_OHOS, traceStr);
146
147 nlohmann::json payload;
148 payload["pid"] = std::to_string(pr.GetPid());
149 payload["uid"] = std::to_string(pr.GetUid());
150 payload["name"] = app.name_;
151 payload["oldGroup"] = std::to_string((int32_t)(pr.lastSchedGroup_));
152 payload["newGroup"] = std::to_string((int32_t)(pr.curSchedGroup_));
153 ResSchedUtils::GetInstance().ReportDataInProcess(ResType::RES_TYPE_CGROUP_ADJUSTER, 0, payload);
154
155 FinishTrace(HITRACE_TAG_OHOS);
156 }
157 }
158 } // namespace ResourceSchedule
159 } // namespace OHOS
160