Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #define LOG_TAG "libpixelpowerstats" |
| 17 | |
| 18 | #include "AocStateResidencyDataProvider.h" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace google { |
| 27 | namespace pixel { |
| 28 | namespace powerstats { |
| 29 | |
| 30 | AocStateResidencyDataProvider::AocStateResidencyDataProvider( |
| 31 | std::vector<std::pair<uint32_t, std::string>> ids, |
| 32 | std::vector<std::pair<std::string, std::string>> states) { |
| 33 | // AoC stats are reported in ticks of 244.140625ns. The transform |
| 34 | // function converts ticks to milliseconds. |
| 35 | // 1000000 / 244.140625 = 4096. |
| 36 | static const uint64_t AOC_CLK = 4096; |
| 37 | std::function<uint64_t(uint64_t)> aocTickToMs = [](uint64_t a) { return a / AOC_CLK; }; |
| 38 | StateResidencyConfig config = { |
| 39 | .entryCountSupported = true, |
| 40 | .entryCountPrefix = "Counter:", |
| 41 | .totalTimeSupported = true, |
| 42 | .totalTimePrefix = "Cumulative time:", |
| 43 | .totalTimeTransform = aocTickToMs, |
| 44 | .lastEntrySupported = true, |
| 45 | .lastEntryPrefix = "Time last entered:", |
| 46 | .lastEntryTransform = aocTickToMs, |
| 47 | }; |
| 48 | uint32_t state_id; |
| 49 | for (auto &id : ids) { |
| 50 | state_id = 1; |
| 51 | for (auto &state : states) { |
| 52 | std::vector<std::pair<std::string, std::string>> aocStateHeaders = { |
| 53 | std::make_pair(state.first, ""), |
| 54 | }; |
| 55 | std::unique_ptr<GenericStateResidencyDataProvider> sdp( |
| 56 | new GenericStateResidencyDataProvider(id.second + state.second)); |
| 57 | sdp->addEntity(id.first, PowerEntityConfig(state_id++, "", |
| 58 | generateGenericStateResidencyConfigs( |
| 59 | config, aocStateHeaders))); |
| 60 | mProviders.push_back(std::move(sdp)); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool AocStateResidencyDataProvider::getResults( |
| 66 | std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) { |
| 67 | for (auto &provider : mProviders) { |
| 68 | provider->getResults(results); |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | std::vector<PowerEntityStateSpace> AocStateResidencyDataProvider::getStateSpaces() { |
| 74 | // Return state spaces based on all configured providers. |
| 75 | // States from the same power entity are merged. |
| 76 | std::map<uint32_t, PowerEntityStateSpace> stateSpaces; |
| 77 | for (auto &provider : mProviders) { |
| 78 | for (auto &stateSpace : provider->getStateSpaces()) { |
| 79 | auto it = stateSpaces.find(stateSpace.powerEntityId); |
| 80 | if (it != stateSpaces.end()) { |
| 81 | auto &states = it->second.states; |
| 82 | auto size = states.size(); |
| 83 | states.resize(size + stateSpace.states.size()); |
| 84 | for (uint32_t i = 0; i < stateSpace.states.size(); i++) { |
| 85 | states[size + i] = stateSpace.states[i]; |
| 86 | } |
| 87 | } else { |
| 88 | stateSpaces.insert(std::pair<uint32_t, PowerEntityStateSpace>( |
| 89 | stateSpace.powerEntityId, stateSpace)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | std::vector<PowerEntityStateSpace> ret; |
| 95 | for (auto &stateSpace : stateSpaces) { |
| 96 | ret.push_back(stateSpace.second); |
| 97 | } |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | } // namespace powerstats |
| 102 | } // namespace pixel |
| 103 | } // namespace google |
| 104 | } // namespace hardware |
| 105 | } // namespace android |