1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <processgroup/sched_policy.h>
18 
19 #define LOG_TAG "SchedPolicy"
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 
25 #include <android-base/logging.h>
26 #include <android-base/threads.h>
27 #include <cgroup_map.h>
28 #include <processgroup/processgroup.h>
29 
30 using android::base::GetThreadId;
31 
32 /* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
33  * Call this any place a SchedPolicy is used as an input parameter.
34  * Returns the possibly re-mapped policy.
35  */
_policy(SchedPolicy p)36 static inline SchedPolicy _policy(SchedPolicy p) {
37     return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
38 }
39 
40 #if defined(__ANDROID__)
41 
set_cpuset_policy(pid_t tid,SchedPolicy policy)42 int set_cpuset_policy(pid_t tid, SchedPolicy policy) {
43     if (tid == 0) {
44         tid = GetThreadId();
45     }
46     policy = _policy(policy);
47 
48     switch (policy) {
49         case SP_BACKGROUND:
50             return SetTaskProfiles(tid, {"CPUSET_SP_BACKGROUND"}, true) ? 0 : -1;
51         case SP_FOREGROUND:
52         case SP_AUDIO_APP:
53         case SP_AUDIO_SYS:
54             return SetTaskProfiles(tid, {"CPUSET_SP_FOREGROUND"}, true) ? 0 : -1;
55         case SP_TOP_APP:
56             return SetTaskProfiles(tid, {"CPUSET_SP_TOP_APP"}, true) ? 0 : -1;
57         case SP_SYSTEM:
58             return SetTaskProfiles(tid, {"CPUSET_SP_SYSTEM"}, true) ? 0 : -1;
59         case SP_RESTRICTED:
60             return SetTaskProfiles(tid, {"CPUSET_SP_RESTRICTED"}, true) ? 0 : -1;
61         default:
62             break;
63     }
64 
65     return 0;
66 }
67 
set_sched_policy(pid_t tid,SchedPolicy policy)68 int set_sched_policy(pid_t tid, SchedPolicy policy) {
69     if (tid == 0) {
70         tid = GetThreadId();
71     }
72     policy = _policy(policy);
73 
74 #if POLICY_DEBUG
75     char statfile[64];
76     char statline[1024];
77     char thread_name[255];
78 
79     snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
80     memset(thread_name, 0, sizeof(thread_name));
81 
82     unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
83     if (fd >= 0) {
84         int rc = read(fd, statline, 1023);
85         statline[rc] = 0;
86         char* p = statline;
87         char* q;
88 
89         for (p = statline; *p != '('; p++)
90             ;
91         p++;
92         for (q = p; *q != ')'; q++)
93             ;
94 
95         strncpy(thread_name, p, (q - p));
96     }
97     switch (policy) {
98         case SP_BACKGROUND:
99             SLOGD("vvv tid %d (%s)", tid, thread_name);
100             break;
101         case SP_FOREGROUND:
102         case SP_AUDIO_APP:
103         case SP_AUDIO_SYS:
104         case SP_TOP_APP:
105             SLOGD("^^^ tid %d (%s)", tid, thread_name);
106             break;
107         case SP_SYSTEM:
108             SLOGD("/// tid %d (%s)", tid, thread_name);
109             break;
110         case SP_RT_APP:
111             SLOGD("RT  tid %d (%s)", tid, thread_name);
112             break;
113         default:
114             SLOGD("??? tid %d (%s)", tid, thread_name);
115             break;
116     }
117 #endif
118 
119     switch (policy) {
120         case SP_BACKGROUND:
121             return SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1;
122         case SP_FOREGROUND:
123         case SP_AUDIO_APP:
124         case SP_AUDIO_SYS:
125             return SetTaskProfiles(tid, {"SCHED_SP_FOREGROUND"}, true) ? 0 : -1;
126         case SP_TOP_APP:
127             return SetTaskProfiles(tid, {"SCHED_SP_TOP_APP"}, true) ? 0 : -1;
128         case SP_SYSTEM:
129             return SetTaskProfiles(tid, {"SCHED_SP_SYSTEM"}, true) ? 0 : -1;
130         case SP_RT_APP:
131             return SetTaskProfiles(tid, {"SCHED_SP_RT_APP"}, true) ? 0 : -1;
132         default:
133             return SetTaskProfiles(tid, {"SCHED_SP_DEFAULT"}, true) ? 0 : -1;
134     }
135 
136     return 0;
137 }
138 
cpusets_enabled()139 bool cpusets_enabled() {
140     static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").IsUsable());
141     return enabled;
142 }
143 
schedtune_enabled()144 static bool schedtune_enabled() {
145     return (CgroupMap::GetInstance().FindController("schedtune").IsUsable());
146 }
147 
cpuctl_enabled()148 static bool cpuctl_enabled() {
149     return (CgroupMap::GetInstance().FindController("cpu").IsUsable());
150 }
151 
schedboost_enabled()152 bool schedboost_enabled() {
153     static bool enabled = schedtune_enabled() || cpuctl_enabled();
154 
155     return enabled;
156 }
157 
getCGroupSubsys(pid_t tid,const char * subsys,std::string & subgroup)158 static int getCGroupSubsys(pid_t tid, const char* subsys, std::string& subgroup) {
159     auto controller = CgroupMap::GetInstance().FindController(subsys);
160 
161     if (!controller.IsUsable()) return -1;
162 
163     if (!controller.GetTaskGroup(tid, &subgroup))
164         return -1;
165 
166     return 0;
167 }
168 
get_sched_policy_from_group(const std::string & group,SchedPolicy * policy)169 static int get_sched_policy_from_group(const std::string& group, SchedPolicy* policy) {
170     if (group.empty()) {
171         *policy = SP_FOREGROUND;
172     } else if (group == "foreground") {
173         *policy = SP_FOREGROUND;
174     } else if (group == "system-background") {
175         *policy = SP_SYSTEM;
176     } else if (group == "background") {
177         *policy = SP_BACKGROUND;
178     } else if (group == "top-app") {
179         *policy = SP_TOP_APP;
180     } else if (group == "restricted") {
181         *policy = SP_RESTRICTED;
182     } else {
183         errno = ERANGE;
184         return -1;
185     }
186     return 0;
187 }
188 
get_sched_policy(pid_t tid,SchedPolicy * policy)189 int get_sched_policy(pid_t tid, SchedPolicy* policy) {
190     if (tid == 0) {
191         tid = GetThreadId();
192     }
193 
194     std::string group;
195     if (schedboost_enabled()) {
196         if ((getCGroupSubsys(tid, "schedtune", group) < 0) &&
197             (getCGroupSubsys(tid, "cpu", group) < 0)) {
198             LOG(ERROR) << "Failed to find cpu cgroup for tid " << tid;
199             return -1;
200         }
201         // Wipe invalid group to fallback to cpuset
202         if (!group.empty()) {
203             if (get_sched_policy_from_group(group, policy) < 0) {
204                 group.clear();
205             } else {
206                 return 0;
207             }
208         }
209     }
210 
211     if (cpusets_enabled() && getCGroupSubsys(tid, "cpuset", group) < 0) {
212         LOG(ERROR) << "Failed to find cpuset cgroup for tid " << tid;
213         return -1;
214     }
215     return get_sched_policy_from_group(group, policy);
216 }
217 
218 #else
219 
220 /* Stubs for non-Android targets. */
221 
set_sched_policy(int,SchedPolicy)222 int set_sched_policy(int, SchedPolicy) {
223     return 0;
224 }
225 
get_sched_policy(int,SchedPolicy * policy)226 int get_sched_policy(int, SchedPolicy* policy) {
227     *policy = SP_SYSTEM_DEFAULT;
228     return 0;
229 }
230 
231 #endif
232 
get_sched_policy_name(SchedPolicy policy)233 const char* get_sched_policy_name(SchedPolicy policy) {
234     policy = _policy(policy);
235     static const char* const kSchedPolicyNames[] = {
236             [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = "  ",
237             [SP_AUDIO_APP] = "aa",  [SP_AUDIO_SYS] = "as",  [SP_TOP_APP] = "ta",
238             [SP_RT_APP] = "rt",     [SP_RESTRICTED] = "rs",
239     };
240     static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
241     if (policy < SP_BACKGROUND || policy >= SP_CNT) {
242         return nullptr;
243     }
244     return kSchedPolicyNames[policy];
245 }
246 
get_cpuset_policy_profile_name(SchedPolicy policy)247 const char* get_cpuset_policy_profile_name(SchedPolicy policy) {
248     /*
249      *  cpuset profile array for:
250      *  SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
251      *  SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
252      *  SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
253      *  index is policy + 1
254      *  this need keep in sync with SchedPolicy enum
255      */
256     static constexpr const char* kCpusetProfiles[SP_CNT + 1] = {
257             "CPUSET_SP_DEFAULT", "CPUSET_SP_BACKGROUND", "CPUSET_SP_FOREGROUND",
258             "CPUSET_SP_SYSTEM",  "CPUSET_SP_FOREGROUND", "CPUSET_SP_FOREGROUND",
259             "CPUSET_SP_TOP_APP", "CPUSET_SP_DEFAULT",    "CPUSET_SP_RESTRICTED"};
260     if (policy < SP_DEFAULT || policy >= SP_CNT) {
261         return nullptr;
262     }
263     return kCpusetProfiles[policy + 1];
264 }
265 
get_sched_policy_profile_name(SchedPolicy policy)266 const char* get_sched_policy_profile_name(SchedPolicy policy) {
267     /*
268      *  sched profile array for:
269      *  SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
270      *  SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
271      *  SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
272      *  index is policy + 1
273      *  this need keep in sync with SchedPolicy enum
274      */
275     static constexpr const char* kSchedProfiles[SP_CNT + 1] = {
276             "SCHED_SP_DEFAULT", "SCHED_SP_BACKGROUND", "SCHED_SP_FOREGROUND",
277             "SCHED_SP_SYSTEM",  "SCHED_SP_FOREGROUND", "SCHED_SP_FOREGROUND",
278             "SCHED_SP_TOP_APP", "SCHED_SP_RT_APP",     "SCHED_SP_DEFAULT"};
279     if (policy < SP_DEFAULT || policy >= SP_CNT) {
280         return nullptr;
281     }
282     return kSchedProfiles[policy + 1];
283 }
284