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 #pragma once 18 19 #include <inttypes.h> 20 21 #include <array> 22 #include <chrono> 23 #include <deque> 24 #include <random> 25 #include <vector> 26 27 #include "AdaptiveCpuConfig.h" 28 #include "CpuFrequencyReader.h" 29 #include "Device.h" 30 #include "ThrottleDecision.h" 31 #include "WorkDurationProcessor.h" 32 33 namespace aidl { 34 namespace google { 35 namespace hardware { 36 namespace power { 37 namespace impl { 38 namespace pixel { 39 40 // Currently Adaptive CPU is targeted to only raven/oriole, so we can hardcode the CPU architecture. 41 // If we extend to other architectures, this will have to vary per-device or be dynamically loaded. 42 constexpr uint32_t NUM_CPU_CORES = 8; 43 constexpr uint32_t NUM_CPU_POLICIES = 3; 44 constexpr std::array<uint32_t, NUM_CPU_POLICIES> CPU_POLICY_INDICES{0, 4, 6}; 45 46 struct ModelInput { 47 std::array<double, NUM_CPU_POLICIES> cpuPolicyAverageFrequencyHz; 48 std::array<double, NUM_CPU_CORES> cpuCoreIdleTimesPercentage; 49 WorkDurationFeatures workDurationFeatures; 50 ThrottleDecision previousThrottleDecision; 51 Device device; 52 53 bool SetCpuFreqiencies( 54 const std::vector<CpuPolicyAverageFrequency> &cpuPolicyAverageFrequencies); 55 56 void LogToAtrace() const; 57 58 bool operator==(const ModelInput &other) const { 59 return cpuPolicyAverageFrequencyHz == other.cpuPolicyAverageFrequencyHz && 60 cpuCoreIdleTimesPercentage == other.cpuCoreIdleTimesPercentage && 61 workDurationFeatures == other.workDurationFeatures && 62 previousThrottleDecision == other.previousThrottleDecision; 63 } 64 }; 65 66 class Model { 67 public: Model()68 Model() 69 : mShouldRandomThrottleDistribution(0, 1), 70 mRandomThrottleDistribution(static_cast<uint32_t>(ThrottleDecision::FIRST), 71 static_cast<uint32_t>(ThrottleDecision::LAST)) {} 72 ThrottleDecision Run(const std::deque<ModelInput> &modelInputs, 73 const AdaptiveCpuConfig &config); 74 75 private: 76 std::default_random_engine mGenerator; 77 std::uniform_real_distribution<double> mShouldRandomThrottleDistribution; 78 std::uniform_int_distribution<uint32_t> mRandomThrottleDistribution; 79 80 ThrottleDecision RunDecisionTree(const std::deque<ModelInput> &modelInputs); 81 }; 82 83 } // namespace pixel 84 } // namespace impl 85 } // namespace power 86 } // namespace hardware 87 } // namespace google 88 } // namespace aidl 89