1 /*
2  * Copyright (C) 2019 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 "pwrstats_util"
17 
18 #include "PowerStatsCollector.h"
19 
20 #include <android-base/logging.h>
21 
addDataProvider(std::unique_ptr<IPowerStatProvider> p)22 void PowerStatsCollector::addDataProvider(std::unique_ptr<IPowerStatProvider> p) {
23     mStatProviders.emplace(p->typeOf(), std::move(p));
24 }
25 
get(std::vector<PowerStatistic> * stats) const26 int PowerStatsCollector::get(std::vector<PowerStatistic>* stats) const {
27     if (!stats) {
28         LOG(ERROR) << __func__ << ": bad args; stat is null";
29         return 1;
30     }
31 
32     stats->clear();
33     for (auto&& provider : mStatProviders) {
34         PowerStatistic stat;
35         if (provider.second->get(&stat) != 0) {
36             LOG(ERROR) << __func__ << ": a data provider failed";
37             stats->clear();
38             return 1;
39         }
40 
41         stats->emplace_back(stat);
42     }
43     return 0;
44 }
45 
get(const std::vector<PowerStatistic> & start,std::vector<PowerStatistic> * interval) const46 int PowerStatsCollector::get(const std::vector<PowerStatistic>& start,
47                              std::vector<PowerStatistic>* interval) const {
48     if (!interval) {
49         LOG(ERROR) << __func__ << ": bad args; interval is null";
50         return 1;
51     }
52 
53     interval->clear();
54     for (auto const& curStat : start) {
55         auto provider = mStatProviders.find(curStat.power_stat_case());
56         if (provider == mStatProviders.end()) {
57             LOG(ERROR) << __func__ << ": a provider is missing";
58             interval->clear();
59             return 1;
60         }
61 
62         PowerStatistic curInterval;
63         if (provider->second->get(curStat, &curInterval) != 0) {
64             LOG(ERROR) << __func__ << ": a data provider failed";
65             interval->clear();
66             return 1;
67         }
68         interval->emplace_back(curInterval);
69     }
70     return 0;
71 }
72 
dump(const std::vector<PowerStatistic> & stats,std::ostream * output) const73 void PowerStatsCollector::dump(const std::vector<PowerStatistic>& stats,
74                                std::ostream* output) const {
75     if (!output) {
76         LOG(ERROR) << __func__ << ": bad args; output is null";
77         return;
78     }
79 
80     for (auto const& stat : stats) {
81         auto provider = mStatProviders.find(stat.power_stat_case());
82         if (provider == mStatProviders.end()) {
83             LOG(ERROR) << __func__ << ": a provider is missing";
84             return;
85         }
86 
87         provider->second->dump(stat, output);
88     }
89 }
90 
get(PowerStatistic * stat) const91 int IPowerStatProvider::get(PowerStatistic* stat) const {
92     if (!stat) {
93         LOG(ERROR) << __func__ << ": bad args; stat is null";
94         return 1;
95     }
96 
97     return getImpl(stat);
98 }
99 
get(const PowerStatistic & start,PowerStatistic * interval) const100 int IPowerStatProvider::get(const PowerStatistic& start, PowerStatistic* interval) const {
101     if (!interval) {
102         LOG(ERROR) << __func__ << ": bad args; interval is null";
103         return 1;
104     }
105 
106     if (typeOf() != start.power_stat_case()) {
107         LOG(ERROR) << __func__ << ": bad args; start is incorrect type";
108         return 1;
109     }
110 
111     if (0 != getImpl(interval)) {
112         LOG(ERROR) << __func__ << ": unable to retrieve stats";
113         return 1;
114     }
115 
116     return getImpl(start, interval);
117 }
118 
dump(const PowerStatistic & stat,std::ostream * output) const119 void IPowerStatProvider::dump(const PowerStatistic& stat, std::ostream* output) const {
120     if (!output) {
121         LOG(ERROR) << __func__ << ": bad args; output is null";
122         return;
123     }
124 
125     if (typeOf() != stat.power_stat_case()) {
126         LOG(ERROR) << __func__ << ": bad args; stat is incorrect type";
127         return;
128     }
129 
130     dumpImpl(stat, output);
131 }
132