• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <android-base/properties.h>
18 #include <gtest/gtest.h>
19 
20 #include "adaptivecpu/AdaptiveCpuConfig.h"
21 
22 namespace aidl {
23 namespace google {
24 namespace hardware {
25 namespace power {
26 namespace impl {
27 namespace pixel {
28 
29 using std::chrono_literals::operator""ms;
30 using std::chrono_literals::operator""min;
31 
32 class AdaptiveCpuConfigTest : public ::testing::Test {
33   protected:
SetUp()34     void SetUp() override {
35         android::base::SetProperty("debug.adaptivecpu.iteration_sleep_duration_ms", "");
36         android::base::SetProperty("debug.adaptivecpu.hint_timeout_ms", "");
37         android::base::SetProperty("debug.adaptivecpu.random_throttle_decision_percent", "");
38         android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "");
39         android::base::SetProperty("debug.adaptivecpu.enabled_hint_timeout_ms", "");
40     }
41 };
42 
TEST(AdaptiveCpuConfigTest,valid)43 TEST(AdaptiveCpuConfigTest, valid) {
44     android::base::SetProperty("debug.adaptivecpu.iteration_sleep_duration_ms", "25");
45     android::base::SetProperty("debug.adaptivecpu.hint_timeout_ms", "500");
46     android::base::SetProperty("debug.adaptivecpu.random_throttle_decision_percent", "25");
47     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,3,4");
48     android::base::SetProperty("debug.adaptivecpu.enabled_hint_timeout_ms", "1000");
49     const AdaptiveCpuConfig expectedConfig{
50             .iterationSleepDuration = 25ms,
51             .hintTimeout = 500ms,
52             .randomThrottleDecisionProbability = 0.25,
53             .enabledHintTimeout = 1000ms,
54             .randomThrottleOptions = {ThrottleDecision::NO_THROTTLE, ThrottleDecision::THROTTLE_70,
55                                       ThrottleDecision::THROTTLE_80},
56     };
57     AdaptiveCpuConfig actualConfig;
58     ASSERT_TRUE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
59     ASSERT_EQ(actualConfig, expectedConfig);
60 }
61 
TEST(AdaptiveCpuConfigTest,defaultConfig)62 TEST(AdaptiveCpuConfigTest, defaultConfig) {
63     android::base::SetProperty("debug.adaptivecpu.iteration_sleep_duration_ms", "");
64     android::base::SetProperty("debug.adaptivecpu.hint_timeout_ms", "");
65     android::base::SetProperty("debug.adaptivecpu.random_throttle_decision_percent", "");
66     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "");
67     android::base::SetProperty("debug.adaptivecpu.enabled_hint_timeout_ms", "");
68     AdaptiveCpuConfig actualConfig;
69     ASSERT_TRUE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
70     ASSERT_EQ(actualConfig, AdaptiveCpuConfig::DEFAULT);
71 }
72 
TEST(AdaptiveCpuConfigTest,iterationSleepDuration_belowMin)73 TEST(AdaptiveCpuConfigTest, iterationSleepDuration_belowMin) {
74     android::base::SetProperty("debug.adaptivecpu.iteration_sleep_duration_ms", "2");
75     AdaptiveCpuConfig actualConfig;
76     ASSERT_TRUE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
77     ASSERT_EQ(actualConfig.iterationSleepDuration, 20ms);
78 }
79 
TEST(AdaptiveCpuConfigTest,iterationSleepDuration_negative)80 TEST(AdaptiveCpuConfigTest, iterationSleepDuration_negative) {
81     android::base::SetProperty("debug.adaptivecpu.iteration_sleep_duration_ms", "-100");
82     AdaptiveCpuConfig actualConfig;
83     ASSERT_TRUE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
84     ASSERT_EQ(actualConfig.iterationSleepDuration, 1000ms);
85 }
86 
TEST(AdaptiveCpuConfigTest,randomThrottleDecisionProbability_float)87 TEST(AdaptiveCpuConfigTest, randomThrottleDecisionProbability_float) {
88     android::base::SetProperty("debug.adaptivecpu.random_throttle_decision_percent", "0.5");
89     AdaptiveCpuConfig actualConfig;
90     ASSERT_TRUE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
91     ASSERT_EQ(actualConfig.randomThrottleDecisionProbability, 0);
92 }
93 
TEST(AdaptiveCpuConfigTest,randomThrottleDecisionProbability_tooBig)94 TEST(AdaptiveCpuConfigTest, randomThrottleDecisionProbability_tooBig) {
95     android::base::SetProperty("debug.adaptivecpu.random_throttle_decision_percent", "101");
96     AdaptiveCpuConfig actualConfig;
97     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
98 }
99 
TEST(AdaptiveCpuConfigTest,randomThrottleOptions_invalidThrottle)100 TEST(AdaptiveCpuConfigTest, randomThrottleOptions_invalidThrottle) {
101     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,1,2,9");
102     AdaptiveCpuConfig actualConfig;
103     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
104 }
105 
TEST(AdaptiveCpuConfigTest,randomThrottleOptions_wrongDelim)106 TEST(AdaptiveCpuConfigTest, randomThrottleOptions_wrongDelim) {
107     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,1.2,3");
108     AdaptiveCpuConfig actualConfig;
109     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
110 }
111 
TEST(AdaptiveCpuConfigTest,randomThrottleOptions_wrongNumber)112 TEST(AdaptiveCpuConfigTest, randomThrottleOptions_wrongNumber) {
113     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,1,2a,3");
114     AdaptiveCpuConfig actualConfig;
115     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
116 }
117 
TEST(AdaptiveCpuConfigTest,randomThrottleOptions_straySpace)118 TEST(AdaptiveCpuConfigTest, randomThrottleOptions_straySpace) {
119     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,1 ,2,3");
120     AdaptiveCpuConfig actualConfig;
121     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
122     android::base::SetProperty("debug.adaptivecpu.random_throttle_options", "0,1,2,3 ");
123     ASSERT_FALSE(AdaptiveCpuConfig::ReadFromSystemProperties(&actualConfig));
124 }
125 
126 }  // namespace pixel
127 }  // namespace impl
128 }  // namespace power
129 }  // namespace hardware
130 }  // namespace google
131 }  // namespace aidl
132