1 #pragma once 2 3 /* 4 * Copyright (C) 2021 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include <chrono> 20 #include <map> 21 #include <ostream> 22 #include <set> 23 #include <vector> 24 25 #include "IFilesystem.h" 26 #include "RealFilesystem.h" 27 28 namespace aidl { 29 namespace google { 30 namespace hardware { 31 namespace power { 32 namespace impl { 33 namespace pixel { 34 35 struct CpuPolicyAverageFrequency { 36 const uint32_t policyId; 37 const uint64_t averageFrequencyHz; 38 39 bool operator==(const CpuPolicyAverageFrequency &other) const { 40 return policyId == other.policyId && averageFrequencyHz == other.averageFrequencyHz; 41 } 42 }; 43 44 class CpuFrequencyReader { 45 public: CpuFrequencyReader()46 CpuFrequencyReader() : mFilesystem(std::make_unique<RealFilesystem>()) {} CpuFrequencyReader(std::unique_ptr<IFilesystem> filesystem)47 CpuFrequencyReader(std::unique_ptr<IFilesystem> filesystem) 48 : mFilesystem(std::move(filesystem)) {} 49 50 // Initialize reading, must be done before calling other methods. 51 // Work is not done in constructor as it accesses files. 52 // Returns true on success. 53 bool Init(); 54 55 // Gets the average frequency each CPU policy was using, since this method was last called. 56 // Results are returned sorted by policyId. 57 // Returns true on success. 58 bool GetRecentCpuPolicyFrequencies(std::vector<CpuPolicyAverageFrequency> *result); 59 60 // The most recently read frequencies for each CPU policy. See readCpuPolicyFrequencies for type 61 // explanation. Used for dumping to bug reports. 62 std::map<uint32_t, std::map<uint64_t, std::chrono::milliseconds>> 63 GetPreviousCpuPolicyFrequencies() const; 64 65 private: 66 // CPU policy IDs read from /sys. Initialized in #init(). Sorted ascending. 67 std::vector<uint32_t> mCpuPolicyIds; 68 // The CPU frequencies when #getRecentCpuPolicyFrequencies was last called (or #init if it has 69 // not been called yet). 70 // See readCpuPolicyFrequencies for explanation of type. 71 std::map<uint32_t, std::map<uint64_t, std::chrono::milliseconds>> mPreviousCpuPolicyFrequencies; 72 const std::unique_ptr<IFilesystem> mFilesystem; 73 74 // Reads, from the /sys filesystem, the CPU frequencies used by each policy. 75 // - The outer map's key is the CPU policy ID. 76 // - The inner map's key is the CPU frequency in Hz. 77 // - The inner map's value is the time the policy has been running at that frequency, aggregated 78 // since boot. 79 // Returns true on success. 80 bool ReadCpuPolicyFrequencies( 81 std::map<uint32_t, std::map<uint64_t, std::chrono::milliseconds>> *result); 82 83 bool ReadCpuPolicyIds(std::vector<uint32_t> *result) const; 84 }; 85 86 } // namespace pixel 87 } // namespace impl 88 } // namespace power 89 } // namespace hardware 90 } // namespace google 91 } // namespace aidl 92