Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +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 | |
| 17 | #include "AocStateResidencyDataProvider.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
Craig Dooley | 9f4ac2c | 2022-05-25 21:21:04 +0000 | [diff] [blame^] | 21 | #include <aoc.h> |
| 22 | |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 23 | namespace aidl { |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace power { |
| 27 | namespace stats { |
| 28 | |
| 29 | AocStateResidencyDataProvider::AocStateResidencyDataProvider(std::vector<std::pair<std::string, |
Kelly Rossmoyer | 20b0391 | 2022-04-24 23:22:49 +0000 | [diff] [blame] | 30 | std::string>> ids, std::vector<std::pair<std::string, std::string>> states) { |
Craig Dooley | 9f4ac2c | 2022-05-25 21:21:04 +0000 | [diff] [blame^] | 31 | // AoC stats are reported in ticks |
| 32 | std::function<uint64_t(uint64_t)> aocTickToMs = [](uint64_t a) { return aoc_ticks_to_nanoseconds(a) / 1000000; }; |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 33 | GenericStateResidencyDataProvider::StateResidencyConfig config = { |
| 34 | .entryCountSupported = true, |
| 35 | .entryCountPrefix = "Counter:", |
| 36 | .totalTimeSupported = true, |
| 37 | .totalTimePrefix = "Cumulative time:", |
| 38 | .totalTimeTransform = aocTickToMs, |
| 39 | .lastEntrySupported = true, |
| 40 | .lastEntryPrefix = "Time last entered:", |
| 41 | .lastEntryTransform = aocTickToMs, |
| 42 | }; |
| 43 | for (const auto &id : ids) { |
| 44 | for (const auto &state : states) { |
| 45 | std::vector<std::pair<std::string, std::string>> aocStateHeaders = { |
| 46 | std::make_pair(state.first, ""), |
| 47 | }; |
| 48 | std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs; |
| 49 | cfgs.emplace_back(generateGenericStateResidencyConfigs(config, aocStateHeaders), |
| 50 | id.first, ""); |
| 51 | std::unique_ptr<GenericStateResidencyDataProvider> sdp( |
| 52 | new GenericStateResidencyDataProvider(id.second + state.second, cfgs)); |
| 53 | mProviders[id.first].push_back(std::move(sdp)); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Kelly Rossmoyer | 20b0391 | 2022-04-24 23:22:49 +0000 | [diff] [blame] | 58 | bool AocStateResidencyDataProvider::getStateResidencies( |
| 59 | std::unordered_map<std::string, std::vector<StateResidency>> *residencies) { |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 60 | // States from the same power entity are merged. |
Kelly Rossmoyer | 20b0391 | 2022-04-24 23:22:49 +0000 | [diff] [blame] | 61 | bool ret = true; |
| 62 | for (const auto &providerList : mProviders) { |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 63 | int32_t stateId = 0; |
| 64 | std::string curEntity = providerList.first; |
| 65 | std::vector<StateResidency> stateResidencies; |
| 66 | |
| 67 | // Iterate over each provider in the providerList, appending each of the states |
| 68 | for (const auto &provider : providerList.second) { |
| 69 | std::unordered_map<std::string, std::vector<StateResidency>> residency; |
Kelly Rossmoyer | 20b0391 | 2022-04-24 23:22:49 +0000 | [diff] [blame] | 70 | ret &= provider->getStateResidencies(&residency); |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 71 | |
| 72 | // Each provider should only return data for curEntity but checking anyway |
| 73 | if (residency.find(curEntity) != residency.end()) { |
| 74 | for (auto &r : residency.at(curEntity)) { |
| 75 | /* |
| 76 | * Modifying stateId here because we are stitching together infos from |
| 77 | * multiple GenericStateResidencyDataProviders. stateId must be modified |
| 78 | * to maintain uniqueness for a given entity |
| 79 | */ |
| 80 | r.id = stateId++; |
| 81 | stateResidencies.push_back(r); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Kelly Rossmoyer | 20b0391 | 2022-04-24 23:22:49 +0000 | [diff] [blame] | 86 | residencies->emplace(curEntity, stateResidencies); |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 87 | } |
Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | std::unordered_map<std::string, std::vector<State>> AocStateResidencyDataProvider::getInfo() { |
| 92 | // States from the same power entity are merged |
| 93 | std::unordered_map<std::string, std::vector<State>> infos; |
| 94 | for (const auto &providerList : mProviders) { |
| 95 | int32_t stateId = 0; |
| 96 | std::string curEntity = providerList.first; |
| 97 | std::vector<State> stateInfos; |
| 98 | |
| 99 | // Iterate over each provider in the providerList, appending each of the states |
| 100 | for (const auto &provider : providerList.second) { |
| 101 | std::unordered_map<std::string, std::vector<State>> info = provider->getInfo(); |
| 102 | |
| 103 | // Each provider should only return data for curEntity but checking anyway |
| 104 | if (info.find(curEntity) != info.end()) { |
| 105 | for (auto &i : info.at(curEntity)) { |
| 106 | /* |
| 107 | * Modifying stateId because we are stitching together infos from |
| 108 | * multiple GenericStateResidencyDataProviders. stateId must be modified |
| 109 | * to maintain uniqueness for a given entity |
| 110 | */ |
| 111 | i.id = stateId++; |
| 112 | stateInfos.push_back(i); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | infos.emplace(curEntity, stateInfos); |
| 118 | } |
| 119 | |
| 120 | return infos; |
| 121 | } |
| 122 | |
| 123 | } // namespace stats |
| 124 | } // namespace power |
| 125 | } // namespace hardware |
| 126 | } // namespace android |
| 127 | } // namespace aidl |