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
28bool 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
44long 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
63TEST(SchedPolicy, set_sched_policy) {
64    if (!hasCapSysNice()) {
65        GTEST_LOG_(INFO) << "skipping test that requires CAP_SYS_NICE";
66        return;
67    }
68
69    // A measureable effect of scheduling policy is that the kernel has 800x
70    // greater slack time in waking up a sleeping background thread.
71    //
72    // Look for 100x difference in how long FB and BG threads actually sleep
73    // when trying to sleep for 1 ns.  This difference is large enough not
74    // to happen by chance, but small enough (compared to 800x) to keep inherent
75    // fuzziness in scheduler behavior from causing false negatives.
76    const unsigned int BG_FG_SLACK_FACTOR = 100;
77
78    ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
79    auto bgSleepTime = medianSleepTime();
80
81    ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
82    auto fgSleepTime = medianSleepTime();
83    ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR);
84}
85
86TEST(SchedPolicy, get_sched_policy) {
87    SchedPolicy policy;
88    ASSERT_EQ(0, get_sched_policy(0, &policy));
89
90    const char *policyName = get_sched_policy_name(policy);
91    EXPECT_NE(nullptr, policyName);
92    EXPECT_STRNE("error", policyName);
93
94    ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
95    SchedPolicy newPolicy;
96    ASSERT_EQ(0, get_sched_policy(0, &newPolicy));
97    EXPECT_EQ(SP_BACKGROUND, newPolicy);
98}
99