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
This commit is contained in:
parent
419ba9e2aa
commit
9f49508f36
|
@ -164,6 +164,7 @@ cc_library {
|
|||
|
||||
shared_libs: ["liblog"],
|
||||
header_libs: [
|
||||
"libbase_headers",
|
||||
"libcutils_headers",
|
||||
"libutils_headers",
|
||||
],
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <android-base/macros.h>
|
||||
#include <log/log.h>
|
||||
|
||||
/* 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];
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue