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