• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
23 
24 #include <android-base/logging.h>
25 #include <android-base/threads.h>
26 #include <cgroup_map.h>
27 #include <processgroup/processgroup.h>
28 
29 using android::base::GetThreadId;
30 
31 /* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
32  * Call this any place a SchedPolicy is used as an input parameter.
33  * Returns the possibly re-mapped policy.
34  */
_policy(SchedPolicy p)35 static inline SchedPolicy _policy(SchedPolicy p) {
36     return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
37 }
38 
39 #if defined(__ANDROID__)
40 
set_cpuset_policy(int tid,SchedPolicy policy)41 int set_cpuset_policy(int tid, SchedPolicy policy) {
42     if (tid == 0) {
43         tid = GetThreadId();
44     }
45     policy = _policy(policy);
46 
47     switch (policy) {
48         case SP_BACKGROUND:
49             return SetTaskProfiles(tid,
50                                    {"HighEnergySaving", "ProcessCapacityLow", "LowIoPriority",
51                                     "TimerSlackHigh"},
52                                    true)
53                            ? 0
54                            : -1;
55         case SP_FOREGROUND:
56         case SP_AUDIO_APP:
57         case SP_AUDIO_SYS:
58             return SetTaskProfiles(tid,
59                                    {"HighPerformance", "ProcessCapacityHigh", "HighIoPriority",
60                                     "TimerSlackNormal"},
61                                    true)
62                            ? 0
63                            : -1;
64         case SP_TOP_APP:
65             return SetTaskProfiles(tid,
66                                    {"MaxPerformance", "ProcessCapacityMax", "MaxIoPriority",
67                                     "TimerSlackNormal"},
68                                    true)
69                            ? 0
70                            : -1;
71         case SP_SYSTEM:
72             return SetTaskProfiles(tid, {"ServiceCapacityLow", "TimerSlackNormal"}, true) ? 0 : -1;
73         case SP_RESTRICTED:
74             return SetTaskProfiles(tid, {"ServiceCapacityRestricted", "TimerSlackNormal"}, true)
75                            ? 0
76                            : -1;
77         default:
78             break;
79     }
80 
81     return 0;
82 }
83 
set_sched_policy(int tid,SchedPolicy policy)84 int set_sched_policy(int tid, SchedPolicy policy) {
85     if (tid == 0) {
86         tid = GetThreadId();
87     }
88     policy = _policy(policy);
89 
90 #if POLICY_DEBUG
91     char statfile[64];
92     char statline[1024];
93     char thread_name[255];
94 
95     snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
96     memset(thread_name, 0, sizeof(thread_name));
97 
98     unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
99     if (fd >= 0) {
100         int rc = read(fd, statline, 1023);
101         statline[rc] = 0;
102         char* p = statline;
103         char* q;
104 
105         for (p = statline; *p != '('; p++)
106             ;
107         p++;
108         for (q = p; *q != ')'; q++)
109             ;
110 
111         strncpy(thread_name, p, (q - p));
112     }
113     switch (policy) {
114         case SP_BACKGROUND:
115             SLOGD("vvv tid %d (%s)", tid, thread_name);
116             break;
117         case SP_FOREGROUND:
118         case SP_AUDIO_APP:
119         case SP_AUDIO_SYS:
120         case SP_TOP_APP:
121             SLOGD("^^^ tid %d (%s)", tid, thread_name);
122             break;
123         case SP_SYSTEM:
124             SLOGD("/// tid %d (%s)", tid, thread_name);
125             break;
126         case SP_RT_APP:
127             SLOGD("RT  tid %d (%s)", tid, thread_name);
128             break;
129         default:
130             SLOGD("??? tid %d (%s)", tid, thread_name);
131             break;
132     }
133 #endif
134 
135     switch (policy) {
136         case SP_BACKGROUND:
137             return SetTaskProfiles(tid, {"HighEnergySaving", "TimerSlackHigh"}, true) ? 0 : -1;
138         case SP_FOREGROUND:
139         case SP_AUDIO_APP:
140         case SP_AUDIO_SYS:
141             return SetTaskProfiles(tid, {"HighPerformance", "TimerSlackNormal"}, true) ? 0 : -1;
142         case SP_TOP_APP:
143             return SetTaskProfiles(tid, {"MaxPerformance", "TimerSlackNormal"}, true) ? 0 : -1;
144         case SP_RT_APP:
145             return SetTaskProfiles(tid, {"RealtimePerformance", "TimerSlackNormal"}, true) ? 0 : -1;
146         default:
147             return SetTaskProfiles(tid, {"TimerSlackNormal"}, true) ? 0 : -1;
148     }
149 
150     return 0;
151 }
152 
cpusets_enabled()153 bool cpusets_enabled() {
154     static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").IsUsable());
155     return enabled;
156 }
157 
schedboost_enabled()158 bool schedboost_enabled() {
159     static bool enabled = (CgroupMap::GetInstance().FindController("schedtune").IsUsable());
160     return enabled;
161 }
162 
getCGroupSubsys(int tid,const char * subsys,std::string & subgroup)163 static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
164     auto controller = CgroupMap::GetInstance().FindController(subsys);
165 
166     if (!controller.IsUsable()) return -1;
167 
168     if (!controller.GetTaskGroup(tid, &subgroup)) {
169         LOG(ERROR) << "Failed to find cgroup for tid " << tid;
170         return -1;
171     }
172     return 0;
173 }
174 
get_sched_policy(int tid,SchedPolicy * policy)175 int get_sched_policy(int tid, SchedPolicy* policy) {
176     if (tid == 0) {
177         tid = GetThreadId();
178     }
179 
180     std::string group;
181     if (schedboost_enabled()) {
182         if (getCGroupSubsys(tid, "schedtune", group) < 0) return -1;
183     }
184     if (group.empty() && cpusets_enabled()) {
185         if (getCGroupSubsys(tid, "cpuset", group) < 0) return -1;
186     }
187 
188     // TODO: replace hardcoded directories
189     if (group.empty()) {
190         *policy = SP_FOREGROUND;
191     } else if (group == "foreground") {
192         *policy = SP_FOREGROUND;
193     } else if (group == "system-background") {
194         *policy = SP_SYSTEM;
195     } else if (group == "background") {
196         *policy = SP_BACKGROUND;
197     } else if (group == "top-app") {
198         *policy = SP_TOP_APP;
199     } else if (group == "restricted") {
200         *policy = SP_RESTRICTED;
201     } else {
202         errno = ERANGE;
203         return -1;
204     }
205     return 0;
206 }
207 
208 #else
209 
210 /* Stubs for non-Android targets. */
211 
set_sched_policy(int,SchedPolicy)212 int set_sched_policy(int, SchedPolicy) {
213     return 0;
214 }
215 
get_sched_policy(int,SchedPolicy * policy)216 int get_sched_policy(int, SchedPolicy* policy) {
217     *policy = SP_SYSTEM_DEFAULT;
218     return 0;
219 }
220 
221 #endif
222 
get_sched_policy_name(SchedPolicy policy)223 const char* get_sched_policy_name(SchedPolicy policy) {
224     policy = _policy(policy);
225     static const char* const kSchedPolicyNames[] = {
226             [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = "  ",
227             [SP_AUDIO_APP] = "aa",  [SP_AUDIO_SYS] = "as",  [SP_TOP_APP] = "ta",
228             [SP_RT_APP] = "rt",     [SP_RESTRICTED] = "rs",
229     };
230     static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
231     if (policy < SP_BACKGROUND || policy >= SP_CNT) {
232         return "error";
233     }
234     return kSchedPolicyNames[policy];
235 }
236