blob: 38cd85f957e07d07f948fef9034407c5531ab4d5 [file] [log] [blame]
Darren Hsu673d3b02021-12-28 21:31:24 +08001/*
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
21namespace aidl {
22namespace android {
23namespace hardware {
24namespace power {
25namespace stats {
26
Darren Hsud4cab2b2022-06-17 20:10:29 +080027AocStateResidencyDataProvider::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;
Sam Dubey33552442022-06-10 03:23:56 +000033 std::function<uint64_t(uint64_t)> aocTickToMs = [](uint64_t a) { return a / AOC_CLK; };
Darren Hsu673d3b02021-12-28 21:31:24 +080034 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
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000059bool AocStateResidencyDataProvider::getStateResidencies(
60 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
Darren Hsu673d3b02021-12-28 21:31:24 +080061 // States from the same power entity are merged.
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000062 bool ret = true;
63 for (const auto &providerList : mProviders) {
Darren Hsu673d3b02021-12-28 21:31:24 +080064 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;
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000071 ret &= provider->getStateResidencies(&residency);
Darren Hsu673d3b02021-12-28 21:31:24 +080072
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
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000087 residencies->emplace(curEntity, stateResidencies);
Darren Hsu673d3b02021-12-28 21:31:24 +080088 }
Darren Hsu673d3b02021-12-28 21:31:24 +080089 return ret;
90}
91
92std::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