sched_policy: add get_cpuset/sched_policy_profile_name

Expose API to return name of task profile for cpuset/sched policy so
that libprocessgroup clients using SetTaskProfiles directly don't have
to maintain the mapping. This reduces the risk of inconsistency and saves
memory.

Bug: 139521784
Test: atest libcutils_test:SchedPolicy
Change-Id: I414312a038613913fb6a827bdcefceb3dec21264
This commit is contained in:
Wei Wang 2019-10-11 15:09:09 -07:00
parent 5cd5da9e54
commit ee2f260223
3 changed files with 65 additions and 4 deletions

View File

@ -107,6 +107,18 @@ TEST(SchedPolicy, set_sched_policy_timerslack) {
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));
EXPECT_EQ(nullptr, get_sched_policy_name(SchedPolicy(-2)));
EXPECT_EQ(nullptr, get_sched_policy_name(SP_CNT));
}
TEST(SchedPolicy, get_cpuset_policy_profile_name) {
EXPECT_STREQ("CPUSET_SP_BACKGROUND", get_cpuset_policy_profile_name(SP_BACKGROUND));
EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SchedPolicy(-2)));
EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SP_CNT));
}
TEST(SchedPolicy, get_sched_policy_profile_name) {
EXPECT_STREQ("SCHED_SP_BACKGROUND", get_sched_policy_profile_name(SP_BACKGROUND));
EXPECT_EQ(nullptr, get_sched_policy_profile_name(SchedPolicy(-2)));
EXPECT_EQ(nullptr, get_sched_policy_profile_name(SP_CNT));
}

View File

@ -70,11 +70,22 @@ extern int set_sched_policy(int tid, SchedPolicy policy);
extern int get_sched_policy(int tid, SchedPolicy* policy);
/* Return a displayable string corresponding to policy.
* Return value: non-NULL NUL-terminated name of unspecified length;
* Return value: NUL-terminated name of unspecified length, nullptr if invalid;
* the caller is responsible for displaying the useful part of the string.
*/
extern const char* get_sched_policy_name(SchedPolicy policy);
/* Return the aggregated task profile name corresponding to cpuset policy.
* Return value: NUL-terminated name of unspecified length, nullptr if invalid;
* the caller could use it to call SetTaskProfiles.
*/
extern const char* get_cpuset_policy_profile_name(SchedPolicy policy);
/* Return the aggregated task profile name corresponding to sched policy.
* Return value: NUL-terminated name of unspecified length, nullptr if invalid;
* the caller could use it to call SetTaskProfiles.
*/
extern const char* get_sched_policy_profile_name(SchedPolicy policy);
#ifdef __cplusplus
}
#endif

View File

@ -212,7 +212,45 @@ const char* get_sched_policy_name(SchedPolicy policy) {
};
static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
if (policy < SP_BACKGROUND || policy >= SP_CNT) {
return "error";
return nullptr;
}
return kSchedPolicyNames[policy];
}
const char* get_cpuset_policy_profile_name(SchedPolicy policy) {
/*
* cpuset profile array for:
* SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
* SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
* SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
* index is policy + 1
* this need keep in sync with SchedPolicy enum
*/
static constexpr const char* kCpusetProfiles[SP_CNT + 1] = {
"CPUSET_SP_DEFAULT", "CPUSET_SP_BACKGROUND", "CPUSET_SP_FOREGROUND",
"CPUSET_SP_SYSTEM", "CPUSET_SP_FOREGROUND", "CPUSET_SP_FOREGROUND",
"CPUSET_SP_TOP_APP", "CPUSET_SP_DEFAULT", "CPUSET_SP_RESTRICTED"};
if (policy < SP_DEFAULT || policy >= SP_CNT) {
return nullptr;
}
return kCpusetProfiles[policy + 1];
}
const char* get_sched_policy_profile_name(SchedPolicy policy) {
/*
* sched profile array for:
* SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
* SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
* SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
* index is policy + 1
* this need keep in sync with SchedPolicy enum
*/
static constexpr const char* kSchedProfiles[SP_CNT + 1] = {
"SCHED_SP_DEFAULT", "SCHED_SP_BACKGROUND", "SCHED_SP_FOREGROUND",
"SCHED_SP_DEFAULT", "SCHED_SP_FOREGROUND", "SCHED_SP_FOREGROUND",
"SCHED_SP_TOP_APP", "SCHED_SP_RT_APP", "SCHED_SP_DEFAULT"};
if (policy < SP_DEFAULT || policy >= SP_CNT) {
return nullptr;
}
return kSchedProfiles[policy + 1];
}