sched_policy.cpp 7.0 KB

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