blob: 6ab54dba5442a21e1f68c1df5c79407d295ff20d [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
Craig Dooley9f4ac2c2022-05-25 21:21:04 +000021#include <aoc.h>
22
Darren Hsu673d3b02021-12-28 21:31:24 +080023namespace aidl {
24namespace android {
25namespace hardware {
26namespace power {
27namespace stats {
28
29AocStateResidencyDataProvider::AocStateResidencyDataProvider(std::vector<std::pair<std::string,
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000030 std::string>> ids, std::vector<std::pair<std::string, std::string>> states) {
Craig Dooley9f4ac2c2022-05-25 21:21:04 +000031 // 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 Hsu673d3b02021-12-28 21:31:24 +080033 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 Rossmoyer20b03912022-04-24 23:22:49 +000058bool AocStateResidencyDataProvider::getStateResidencies(
59 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
Darren Hsu673d3b02021-12-28 21:31:24 +080060 // States from the same power entity are merged.
Kelly Rossmoyer20b03912022-04-24 23:22:49 +000061 bool ret = true;
62 for (const auto &providerList : mProviders) {
Darren Hsu673d3b02021-12-28 21:31:24 +080063 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 Rossmoyer20b03912022-04-24 23:22:49 +000070 ret &= provider->getStateResidencies(&residency);
Darren Hsu673d3b02021-12-28 21:31:24 +080071
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 Rossmoyer20b03912022-04-24 23:22:49 +000086 residencies->emplace(curEntity, stateResidencies);
Darren Hsu673d3b02021-12-28 21:31:24 +080087 }
Darren Hsu673d3b02021-12-28 21:31:24 +080088 return ret;
89}
90
91std::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