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, ¤t_policy));
66 EXPECT_EQ(expected_policy, current_policy);
67 }
68
TEST(SchedPolicy,set_sched_policy)69 TEST(SchedPolicy, set_sched_policy) {
70 ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
71 ASSERT_EQ(0, set_cpuset_policy(0, SP_BACKGROUND));
72 AssertPolicy(SP_BACKGROUND);
73
74 ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
75 ASSERT_EQ(0, set_cpuset_policy(0, SP_FOREGROUND));
76 AssertPolicy(SP_FOREGROUND);
77 }
78
TEST(SchedPolicy,set_sched_policy_timerslack)79 TEST(SchedPolicy, set_sched_policy_timerslack) {
80 if (!hasCapSysNice()) {
81 GTEST_LOG_(INFO) << "skipping test that requires CAP_SYS_NICE";
82 return;
83 }
84
85 // A measureable effect of scheduling policy is that the kernel has 800x
86 // greater slack time in waking up a sleeping background thread.
87 //
88 // Look for 10x difference in how long FB and BG threads actually sleep
89 // when trying to sleep for 1 ns. This difference is large enough not
90 // to happen by chance, but small enough (compared to 800x) to keep inherent
91 // fuzziness in scheduler behavior from causing false negatives.
92 const unsigned int BG_FG_SLACK_FACTOR = 10;
93
94 ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
95 auto bgSleepTime = medianSleepTime();
96
97 ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
98 auto fgSleepTime = medianSleepTime();
99
100 ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR);
101 }
102
TEST(SchedPolicy,get_sched_policy_name)103 TEST(SchedPolicy, get_sched_policy_name) {
104 EXPECT_STREQ("bg", get_sched_policy_name(SP_BACKGROUND));
105 EXPECT_EQ(nullptr, get_sched_policy_name(SchedPolicy(-2)));
106 EXPECT_EQ(nullptr, get_sched_policy_name(SP_CNT));
107 }
108
TEST(SchedPolicy,get_cpuset_policy_profile_name)109 TEST(SchedPolicy, get_cpuset_policy_profile_name) {
110 EXPECT_STREQ("CPUSET_SP_BACKGROUND", get_cpuset_policy_profile_name(SP_BACKGROUND));
111 EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SchedPolicy(-2)));
112 EXPECT_EQ(nullptr, get_cpuset_policy_profile_name(SP_CNT));
113 }
114
TEST(SchedPolicy,get_sched_policy_profile_name)115 TEST(SchedPolicy, get_sched_policy_profile_name) {
116 EXPECT_STREQ("SCHED_SP_BACKGROUND", get_sched_policy_profile_name(SP_BACKGROUND));
117 EXPECT_EQ(nullptr, get_sched_policy_profile_name(SchedPolicy(-2)));
118 EXPECT_EQ(nullptr, get_sched_policy_profile_name(SP_CNT));
119 }
120