• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <algorithm>
18 #include <chrono>
19 #include <thread>
20 #include <vector>
21 
22 #include <sys/capability.h>
23 
24 #include <cutils/sched_policy.h>
25 
26 #include <gtest/gtest.h>
27 
hasCapSysNice()28 bool hasCapSysNice() {
29     __user_cap_header_struct header;
30     memset(&header, 0, sizeof(header));
31     header.version = _LINUX_CAPABILITY_VERSION_3;
32 
33     __user_cap_data_struct caps[_LINUX_CAPABILITY_U32S_3];
34     if (capget(&header, &caps[0])) {
35         GTEST_LOG_(WARNING) << "failed to get process capabilities";
36         return false;
37     }
38 
39     auto nice_idx = CAP_TO_INDEX(CAP_SYS_NICE);
40     auto nice_mask = CAP_TO_MASK(CAP_SYS_NICE);
41     return caps[nice_idx].effective & nice_mask;
42 }
43 
medianSleepTime()44 long long medianSleepTime() {
45     std::vector<long long> sleepTimes;
46     constexpr size_t numSamples = 100;
47 
48     for (size_t i = 0; i < numSamples; i++) {
49         auto start = std::chrono::steady_clock::now();
50         std::this_thread::sleep_for(std::chrono::nanoseconds(1));
51         auto end = std::chrono::steady_clock::now();
52 
53         auto diff = end - start;
54         sleepTimes.push_back(diff.count());
55     }
56 
57     constexpr auto median = numSamples / 2;
58     std::nth_element(sleepTimes.begin(), sleepTimes.begin() + median,
59             sleepTimes.end());
60     return sleepTimes[median];
61 }
62 
AssertPolicy(SchedPolicy expected_policy)63 static void AssertPolicy(SchedPolicy expected_policy) {
64     SchedPolicy current_policy;
65     ASSERT_EQ(0, get_sched_policy(0, &current_policy));
66     EXPECT_EQ(expected_policy, current_policy);
67 }
68 
TEST(SchedPolicy,set_sched_policy)69 TEST(SchedPolicy, set_sched_policy) {
70     if (!schedboost_enabled()) {
71         // schedboost_enabled() (i.e. CONFIG_CGROUP_SCHEDTUNE) is optional;
72         // it's only needed on devices using energy-aware scheduler.
73         GTEST_LOG_(INFO) << "skipping test that requires CONFIG_CGROUP_SCHEDTUNE";
74         return;
75     }
76 
77     ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
78     AssertPolicy(SP_BACKGROUND);
79 
80     ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
81     AssertPolicy(SP_FOREGROUND);
82 }
83 
TEST(SchedPolicy,set_sched_policy_timerslack)84 TEST(SchedPolicy, set_sched_policy_timerslack) {
85     if (!hasCapSysNice()) {
86         GTEST_LOG_(INFO) << "skipping test that requires CAP_SYS_NICE";
87         return;
88     }
89 
90     // A measureable effect of scheduling policy is that the kernel has 800x
91     // greater slack time in waking up a sleeping background thread.
92     //
93     // Look for 10x difference in how long FB and BG threads actually sleep
94     // when trying to sleep for 1 ns.  This difference is large enough not
95     // to happen by chance, but small enough (compared to 800x) to keep inherent
96     // fuzziness in scheduler behavior from causing false negatives.
97     const unsigned int BG_FG_SLACK_FACTOR = 10;
98 
99     ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
100     auto bgSleepTime = medianSleepTime();
101 
102     ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
103     auto fgSleepTime = medianSleepTime();
104 
105     ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR);
106 }
107 
TEST(SchedPolicy,get_sched_policy_name)108 TEST(SchedPolicy, get_sched_policy_name) {
109     EXPECT_STREQ("bg", get_sched_policy_name(SP_BACKGROUND));
110     EXPECT_EQ(nullptr, get_sched_policy_name(SchedPolicy(-2)));
111     EXPECT_EQ(nullptr, get_sched_policy_name(SP_CNT));
112 }
113 
TEST(SchedPolicy,get_cpuset_policy_profile_name)114 TEST(SchedPolicy, get_cpuset_policy_profile_name) {
115     EXPECT_STREQ("CPUSET_SP_BACKGROUND", get_cpuset_policy_profile_name(SP_BACKGROUND));
116     EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SchedPolicy(-2)));
117     EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SP_CNT));
118 }
119 
TEST(SchedPolicy,get_sched_policy_profile_name)120 TEST(SchedPolicy, get_sched_policy_profile_name) {
121     EXPECT_STREQ("SCHED_SP_BACKGROUND", get_sched_policy_profile_name(SP_BACKGROUND));
122     EXPECT_EQ(nullptr, get_sched_policy_profile_name(SchedPolicy(-2)));
123     EXPECT_EQ(nullptr, get_sched_policy_profile_name(SP_CNT));
124 }
125