From 419ba9e2aadbfed3862d95265ce141d016c7ca69 Mon Sep 17 00:00:00 2001 From: Tim Murray Date: Fri, 13 Apr 2018 10:15:49 -0700 Subject: [PATCH 1/2] cutils: add restricted cpuset (cherrypick of 658ae90f699bd21ec8d79b18a34765ed5993c828 without the init.rc change) Bug: 78197570 Test: CTS Change-Id: I6df972950b75a839caa463ae282ad000b959e8ae --- libcutils/include/cutils/sched_policy.h | 15 ++++++++------- libcutils/sched_policy.cpp | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/libcutils/include/cutils/sched_policy.h b/libcutils/include/cutils/sched_policy.h index 4c1113be3..cf91b76f4 100644 --- a/libcutils/include/cutils/sched_policy.h +++ b/libcutils/include/cutils/sched_policy.h @@ -40,16 +40,17 @@ extern bool schedboost_enabled(); /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ typedef enum { - SP_DEFAULT = -1, + SP_DEFAULT = -1, SP_BACKGROUND = 0, SP_FOREGROUND = 1, - SP_SYSTEM = 2, // can't be used with set_sched_policy() - SP_AUDIO_APP = 3, - SP_AUDIO_SYS = 4, - SP_TOP_APP = 5, - SP_RT_APP = 6, + SP_SYSTEM = 2, // can't be used with set_sched_policy() + SP_AUDIO_APP = 3, + SP_AUDIO_SYS = 4, + SP_TOP_APP = 5, + SP_RT_APP = 6, + SP_RESTRICTED = 7, SP_CNT, - SP_MAX = SP_CNT - 1, + SP_MAX = SP_CNT - 1, SP_SYSTEM_DEFAULT = SP_FOREGROUND, } SchedPolicy; diff --git a/libcutils/sched_policy.cpp b/libcutils/sched_policy.cpp index f5ce82fea..f72ec52d6 100644 --- a/libcutils/sched_policy.cpp +++ b/libcutils/sched_policy.cpp @@ -57,6 +57,7 @@ static int system_bg_cpuset_fd = -1; static int bg_cpuset_fd = -1; static int fg_cpuset_fd = -1; static int ta_cpuset_fd = -1; // special cpuset for top app +static int rs_cpuset_fd = -1; // special cpuset for screen off restrictions // File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error static int bg_schedboost_fd = -1; @@ -151,6 +152,8 @@ static void __initialize() { system_bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); filename = "/dev/cpuset/top-app/tasks"; ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); + filename = "/dev/cpuset/restricted/tasks"; + rs_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); if (schedboost_enabled()) { filename = "/dev/stune/top-app/tasks"; @@ -308,6 +311,9 @@ int set_cpuset_policy(int tid, SchedPolicy policy) case SP_SYSTEM: fd = system_bg_cpuset_fd; break; + case SP_RESTRICTED: + fd = rs_cpuset_fd; + break; default: boost_fd = fd = -1; break; @@ -457,14 +463,10 @@ int get_sched_policy(int /*tid*/, SchedPolicy* policy) { const char *get_sched_policy_name(SchedPolicy policy) { policy = _policy(policy); - static const char * const strings[SP_CNT] = { - [SP_BACKGROUND] = "bg", - [SP_FOREGROUND] = "fg", - [SP_SYSTEM] = " ", - [SP_AUDIO_APP] = "aa", - [SP_AUDIO_SYS] = "as", - [SP_TOP_APP] = "ta", - [SP_RT_APP] = "rt", + static const char* const strings[SP_CNT] = { + [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ", + [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta", + [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs", }; if ((policy < SP_CNT) && (strings[policy] != NULL)) return strings[policy]; From 9f49508f368204657414ec0f2852f2a7abb5b4c5 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 25 Apr 2018 14:52:50 -0700 Subject: [PATCH 2/2] Rewrite get_sched_policy_name for safety. This way you'll get a build time error if you make the usual mistake of adding to the enum but not adding an entry to the array. Also improve the unit tests, and fix get_sched_policy_name's incorrect behavior on invalid inputs. Bug: N/A Test: ran tests Change-Id: Iefcb1ec9ef66267837da7a576c8be3d0cfb16cd0 --- libcutils/Android.bp | 1 + libcutils/sched_policy.cpp | 13 +++++++------ libcutils/tests/sched_policy_test.cpp | 24 ++++++++++++------------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/libcutils/Android.bp b/libcutils/Android.bp index 6d00dc6ee..dd46750fc 100644 --- a/libcutils/Android.bp +++ b/libcutils/Android.bp @@ -164,6 +164,7 @@ cc_library { shared_libs: ["liblog"], header_libs: [ + "libbase_headers", "libcutils_headers", "libutils_headers", ], diff --git a/libcutils/sched_policy.cpp b/libcutils/sched_policy.cpp index f72ec52d6..3fa548f78 100644 --- a/libcutils/sched_policy.cpp +++ b/libcutils/sched_policy.cpp @@ -25,6 +25,7 @@ #include #include +#include #include /* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged. @@ -460,16 +461,16 @@ int get_sched_policy(int /*tid*/, SchedPolicy* policy) { #endif -const char *get_sched_policy_name(SchedPolicy policy) -{ +const char* get_sched_policy_name(SchedPolicy policy) { policy = _policy(policy); - static const char* const strings[SP_CNT] = { + static const char* const kSchedPolicyNames[] = { [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ", [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta", [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs", }; - if ((policy < SP_CNT) && (strings[policy] != NULL)) - return strings[policy]; - else + static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name"); + if (policy < SP_BACKGROUND || policy >= SP_CNT) { return "error"; + } + return kSchedPolicyNames[policy]; } diff --git a/libcutils/tests/sched_policy_test.cpp b/libcutils/tests/sched_policy_test.cpp index 173174a1a..5942ee5b6 100644 --- a/libcutils/tests/sched_policy_test.cpp +++ b/libcutils/tests/sched_policy_test.cpp @@ -60,6 +60,12 @@ long long medianSleepTime() { return sleepTimes[median]; } +static void AssertPolicy(SchedPolicy expected_policy) { + SchedPolicy current_policy; + ASSERT_EQ(0, get_sched_policy(0, ¤t_policy)); + EXPECT_EQ(expected_policy, current_policy); +} + TEST(SchedPolicy, set_sched_policy) { if (!hasCapSysNice()) { GTEST_LOG_(INFO) << "skipping test that requires CAP_SYS_NICE"; @@ -76,23 +82,17 @@ TEST(SchedPolicy, set_sched_policy) { const unsigned int BG_FG_SLACK_FACTOR = 100; ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND)); + AssertPolicy(SP_BACKGROUND); auto bgSleepTime = medianSleepTime(); ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND)); + AssertPolicy(SP_FOREGROUND); auto fgSleepTime = medianSleepTime(); ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR); } -TEST(SchedPolicy, get_sched_policy) { - SchedPolicy policy; - ASSERT_EQ(0, get_sched_policy(0, &policy)); - - const char *policyName = get_sched_policy_name(policy); - EXPECT_NE(nullptr, policyName); - EXPECT_STRNE("error", policyName); - - ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND)); - SchedPolicy newPolicy; - ASSERT_EQ(0, get_sched_policy(0, &newPolicy)); - EXPECT_EQ(SP_BACKGROUND, newPolicy); +TEST(SchedPolicy, get_sched_policy_name) { + EXPECT_STREQ("bg", get_sched_policy_name(SP_BACKGROUND)); + EXPECT_STREQ("error", get_sched_policy_name(SchedPolicy(-2))); + EXPECT_STREQ("error", get_sched_policy_name(SP_CNT)); }