• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <PowerStatsAidl.h>
18 #include <Gs201CommonDataProviders.h>
19 #include <AdaptiveDvfsStateResidencyDataProvider.h>
20 #include <AocTimedStateResidencyDataProvider.h>
21 #include <DevfreqStateResidencyDataProvider.h>
22 #include <DvfsStateResidencyDataProvider.h>
23 #include <UfsStateResidencyDataProvider.h>
24 #include <dataproviders/GenericStateResidencyDataProvider.h>
25 #include <dataproviders/IioEnergyMeterDataProvider.h>
26 #include <dataproviders/PowerStatsEnergyConsumer.h>
27 #include <dataproviders/PowerStatsEnergyAttribution.h>
28 #include <dataproviders/PixelStateResidencyDataProvider.h>
29 #include <dataproviders/WlanStateResidencyDataProvider.h>
30 
31 #include <android-base/logging.h>
32 #include <android-base/properties.h>
33 #include <android/binder_manager.h>
34 #include <android/binder_process.h>
35 #include <log/log.h>
36 
37 using aidl::android::hardware::power::stats::AdaptiveDvfsStateResidencyDataProvider;
38 using aidl::android::hardware::power::stats::AocTimedStateResidencyDataProvider;
39 using aidl::android::hardware::power::stats::DevfreqStateResidencyDataProvider;
40 using aidl::android::hardware::power::stats::DvfsStateResidencyDataProvider;
41 using aidl::android::hardware::power::stats::UfsStateResidencyDataProvider;
42 using aidl::android::hardware::power::stats::EnergyConsumerType;
43 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
44 using aidl::android::hardware::power::stats::IioEnergyMeterDataProvider;
45 using aidl::android::hardware::power::stats::PixelStateResidencyDataProvider;
46 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
47 using aidl::android::hardware::power::stats::WlanStateResidencyDataProvider;
48 
49 // TODO (b/181070764) (b/182941084):
50 // Remove this when Wifi/BT energy consumption models are available or revert before ship
51 using aidl::android::hardware::power::stats::EnergyConsumerResult;
52 using aidl::android::hardware::power::stats::Channel;
53 using aidl::android::hardware::power::stats::EnergyMeasurement;
54 class PlaceholderEnergyConsumer : public PowerStats::IEnergyConsumer {
55   public:
PlaceholderEnergyConsumer(std::shared_ptr<PowerStats> p,EnergyConsumerType type,std::string name)56     PlaceholderEnergyConsumer(std::shared_ptr<PowerStats> p, EnergyConsumerType type,
57             std::string name) : kType(type), kName(name), mPowerStats(p), mChannelId(-1) {
58         std::vector<Channel> channels;
59         mPowerStats->getEnergyMeterInfo(&channels);
60 
61         for (const auto &c : channels) {
62             if (c.name == "VSYS_PWR_WLAN_BT") {
63                 mChannelId = c.id;
64                 break;
65             }
66         }
67     }
getInfo()68     std::pair<EnergyConsumerType, std::string> getInfo() override { return {kType, kName}; }
69 
getEnergyConsumed()70     std::optional<EnergyConsumerResult> getEnergyConsumed() override {
71         int64_t totalEnergyUWs = 0;
72         int64_t timestampMs = 0;
73         if (mChannelId != -1) {
74             std::vector<EnergyMeasurement> measurements;
75             if (mPowerStats->readEnergyMeter({mChannelId}, &measurements).isOk()) {
76                 for (const auto &m : measurements) {
77                     totalEnergyUWs += m.energyUWs;
78                     timestampMs = m.timestampMs;
79                 }
80             } else {
81                 LOG(ERROR) << "Failed to read energy meter";
82                 return {};
83             }
84         }
85 
86         return EnergyConsumerResult{.timestampMs = timestampMs,
87                                 .energyUWs = totalEnergyUWs>>1};
88     }
89 
getConsumerName()90     std::string getConsumerName() override {
91         return kName;
92     };
93 
94   private:
95     const EnergyConsumerType kType;
96     const std::string kName;
97     std::shared_ptr<PowerStats> mPowerStats;
98     int32_t mChannelId;
99 };
100 
addPlaceholderEnergyConsumers(std::shared_ptr<PowerStats> p)101 void addPlaceholderEnergyConsumers(std::shared_ptr<PowerStats> p) {
102     p->addEnergyConsumer(
103             std::make_unique<PlaceholderEnergyConsumer>(p, EnergyConsumerType::WIFI, "Wifi"));
104     p->addEnergyConsumer(
105             std::make_unique<PlaceholderEnergyConsumer>(p, EnergyConsumerType::BLUETOOTH, "BT"));
106 }
107 
addAoC(std::shared_ptr<PowerStats> p)108 void addAoC(std::shared_ptr<PowerStats> p) {
109     // When the given timeout is 0, the timeout will be replaced with "120ms * statesCount".
110     static const uint64_t TIMEOUT_MILLIS = 0;
111     // AoC clock is synced from "libaoc.c"
112     static const uint64_t AOC_CLOCK = 24576;
113     std::string prefix = "/sys/devices/platform/19000000.aoc/control/";
114 
115     // Add AoC cores (a32, ff1, hf0, and hf1)
116     std::vector<std::pair<std::string, std::string>> coreIds = {
117             {"AoC-A32", prefix + "a32_"},
118             {"AoC-FF1", prefix + "ff1_"},
119             {"AoC-HF1", prefix + "hf1_"},
120             {"AoC-HF0", prefix + "hf0_"},
121     };
122     std::vector<std::pair<std::string, std::string>> coreStates = {
123             {"DWN", "off"}, {"RET", "retention"}, {"WFI", "wfi"}};
124     p->addStateResidencyDataProvider(std::make_unique<AocTimedStateResidencyDataProvider>(coreIds,
125             coreStates, TIMEOUT_MILLIS, AOC_CLOCK));
126 
127     // Add AoC voltage stats
128     std::vector<std::pair<std::string, std::string>> voltageIds = {
129             {"AoC-Voltage", prefix + "voltage_"},
130     };
131     std::vector<std::pair<std::string, std::string>> voltageStates = {{"NOM", "nominal"},
132                                                                       {"SUD", "super_underdrive"},
133                                                                       {"UUD", "ultra_underdrive"},
134                                                                       {"UD", "underdrive"}};
135     p->addStateResidencyDataProvider(
136             std::make_unique<AocTimedStateResidencyDataProvider>(voltageIds, voltageStates,
137                     TIMEOUT_MILLIS, AOC_CLOCK));
138 
139     // Add AoC monitor mode
140     std::vector<std::pair<std::string, std::string>> monitorIds = {
141             {"AoC", prefix + "monitor_"},
142     };
143     std::vector<std::pair<std::string, std::string>> monitorStates = {
144             {"MON", "mode"},
145     };
146     p->addStateResidencyDataProvider(
147             std::make_unique<AocTimedStateResidencyDataProvider>(monitorIds, monitorStates,
148                     TIMEOUT_MILLIS, AOC_CLOCK));
149 
150     // Add AoC restart count
151     const GenericStateResidencyDataProvider::StateResidencyConfig restartCountConfig = {
152             .entryCountSupported = true,
153             .entryCountPrefix = "",
154             .totalTimeSupported = false,
155             .lastEntrySupported = false,
156     };
157     const std::vector<std::pair<std::string, std::string>> restartCountHeaders = {
158             std::make_pair("RESTART", ""),
159     };
160     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
161     cfgs.emplace_back(
162             generateGenericStateResidencyConfigs(restartCountConfig, restartCountHeaders),
163             "AoC-Count", "");
164     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
165             "/sys/devices/platform/19000000.aoc/restart_count", cfgs));
166 }
167 
addDvfsStats(std::shared_ptr<PowerStats> p)168 void addDvfsStats(std::shared_ptr<PowerStats> p) {
169     // A constant to represent the number of nanoseconds in one millisecond
170     const int NS_TO_MS = 1000000;
171     std::string path = "/sys/devices/platform/acpm_stats/fvp_stats";
172 
173     std::vector<std::pair<std::string, std::string>> adpCfgs = {
174         std::make_pair("CL0", "/sys/devices/system/cpu/cpufreq/policy0/stats"),
175         std::make_pair("CL1", "/sys/devices/system/cpu/cpufreq/policy4/stats"),
176         std::make_pair("CL2", "/sys/devices/system/cpu/cpufreq/policy6/stats")
177     };
178     p->addStateResidencyDataProvider(std::make_unique<AdaptiveDvfsStateResidencyDataProvider>(
179             path, NS_TO_MS, adpCfgs));
180 
181     std::vector<DvfsStateResidencyDataProvider::Config> cfgs;
182 
183     cfgs.push_back({"TPU", {
184         std::make_pair("1066MHz", "1066000"),
185         std::make_pair("845MHz", "845000"),
186         std::make_pair("627MHz", "627000"),
187         std::make_pair("401MHz", "401000"),
188         std::make_pair("226MHz", "226000"),
189         std::make_pair("0MHz", "0"),
190     }});
191 
192     cfgs.push_back({"AUR", {
193         std::make_pair("1160MHz", "1160000"),
194         std::make_pair("750MHz", "750000"),
195         std::make_pair("373MHz", "373000"),
196         std::make_pair("178MHz", "178000"),
197         std::make_pair("0MHz", "0"),
198     }});
199 
200     p->addStateResidencyDataProvider(std::make_unique<DvfsStateResidencyDataProvider>(
201             path, NS_TO_MS, cfgs));
202 }
203 
addSoC(std::shared_ptr<PowerStats> p)204 void addSoC(std::shared_ptr<PowerStats> p) {
205     // A constant to represent the number of nanoseconds in one millisecond.
206     const int NS_TO_MS = 1000000;
207 
208     // ACPM stats are reported in nanoseconds. The transform function
209     // converts nanoseconds to milliseconds.
210     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
211     const GenericStateResidencyDataProvider::StateResidencyConfig lpmStateConfig = {
212             .entryCountSupported = true,
213             .entryCountPrefix = "success_count:",
214             .totalTimeSupported = true,
215             .totalTimePrefix = "total_time_ns:",
216             .totalTimeTransform = acpmNsToMs,
217             .lastEntrySupported = true,
218             .lastEntryPrefix = "last_entry_time_ns:",
219             .lastEntryTransform = acpmNsToMs,
220     };
221     const GenericStateResidencyDataProvider::StateResidencyConfig downStateConfig = {
222             .entryCountSupported = true,
223             .entryCountPrefix = "down_count:",
224             .totalTimeSupported = true,
225             .totalTimePrefix = "total_down_time_ns:",
226             .totalTimeTransform = acpmNsToMs,
227             .lastEntrySupported = true,
228             .lastEntryPrefix = "last_down_time_ns:",
229             .lastEntryTransform = acpmNsToMs,
230     };
231     const GenericStateResidencyDataProvider::StateResidencyConfig reqStateConfig = {
232             .entryCountSupported = true,
233             .entryCountPrefix = "req_up_count:",
234             .totalTimeSupported = true,
235             .totalTimePrefix = "total_req_up_time_ns:",
236             .totalTimeTransform = acpmNsToMs,
237             .lastEntrySupported = true,
238             .lastEntryPrefix = "last_req_up_time_ns:",
239             .lastEntryTransform = acpmNsToMs,
240 
241     };
242     const std::vector<std::pair<std::string, std::string>> powerStateHeaders = {
243             std::make_pair("SICD", "SICD"),
244             std::make_pair("SLEEP", "SLEEP"),
245             std::make_pair("SLEEP_SLCMON", "SLEEP_SLCMON"),
246             std::make_pair("SLEEP_HSI1ON", "SLEEP_HSI1ON"),
247             std::make_pair("STOP", "STOP"),
248     };
249     const std::vector<std::pair<std::string, std::string>> mifReqStateHeaders = {
250             std::make_pair("AOC", "AOC"),
251             std::make_pair("GSA", "GSA"),
252             std::make_pair("TPU", "TPU"),
253     };
254     const std::vector<std::pair<std::string, std::string>> slcReqStateHeaders = {
255             std::make_pair("AOC", "AOC"),
256     };
257 
258     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
259     cfgs.emplace_back(generateGenericStateResidencyConfigs(lpmStateConfig, powerStateHeaders),
260             "LPM", "LPM:");
261     cfgs.emplace_back(generateGenericStateResidencyConfigs(downStateConfig, powerStateHeaders),
262             "MIF", "MIF:");
263     cfgs.emplace_back(generateGenericStateResidencyConfigs(reqStateConfig, mifReqStateHeaders),
264             "MIF-REQ", "MIF_REQ:");
265     cfgs.emplace_back(generateGenericStateResidencyConfigs(downStateConfig, powerStateHeaders),
266             "SLC", "SLC:");
267     cfgs.emplace_back(generateGenericStateResidencyConfigs(reqStateConfig, slcReqStateHeaders),
268             "SLC-REQ", "SLC_REQ:");
269 
270     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
271             "/sys/devices/platform/acpm_stats/soc_stats", cfgs));
272 }
273 
setEnergyMeter(std::shared_ptr<PowerStats> p)274 void setEnergyMeter(std::shared_ptr<PowerStats> p) {
275     std::vector<const std::string> deviceNames { "s2mpg12-odpm", "s2mpg13-odpm" };
276     p->setEnergyMeterDataProvider(std::make_unique<IioEnergyMeterDataProvider>(deviceNames, true));
277 }
278 
addCPUclusters(std::shared_ptr<PowerStats> p)279 void addCPUclusters(std::shared_ptr<PowerStats> p) {
280     // A constant to represent the number of nanoseconds in one millisecond.
281     const int NS_TO_MS = 1000000;
282 
283     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
284     const GenericStateResidencyDataProvider::StateResidencyConfig cpuStateConfig = {
285             .entryCountSupported = true,
286             .entryCountPrefix = "down_count:",
287             .totalTimeSupported = true,
288             .totalTimePrefix = "total_down_time_ns:",
289             .totalTimeTransform = acpmNsToMs,
290             .lastEntrySupported = true,
291             .lastEntryPrefix = "last_down_time_ns:",
292             .lastEntryTransform = acpmNsToMs,
293     };
294 
295     const std::vector<std::pair<std::string, std::string>> cpuStateHeaders = {
296             std::make_pair("DOWN", ""),
297     };
298 
299     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
300     for (std::string name : {"CORE00", "CORE01", "CORE02", "CORE03", "CORE10", "CORE11",
301                                 "CORE20", "CORE21", "CLUSTER0", "CLUSTER1", "CLUSTER2"}) {
302         cfgs.emplace_back(generateGenericStateResidencyConfigs(cpuStateConfig, cpuStateHeaders),
303             name, name);
304     }
305 
306     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
307             "/sys/devices/platform/acpm_stats/core_stats", cfgs));
308 
309     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
310             EnergyConsumerType::CPU_CLUSTER, "CPUCL0", {"S4M_VDD_CPUCL0"}));
311     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
312             EnergyConsumerType::CPU_CLUSTER, "CPUCL1", {"S3M_VDD_CPUCL1"}));
313     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
314             EnergyConsumerType::CPU_CLUSTER, "CPUCL2", {"S2M_VDD_CPUCL2"}));
315 }
316 
addGPU(std::shared_ptr<PowerStats> p)317 void addGPU(std::shared_ptr<PowerStats> p) {
318     // Add gpu energy consumer
319     std::map<std::string, int32_t> stateCoeffs;
320 
321     // TODO (b/197721618): Measuring the GPU power numbers
322     stateCoeffs = {
323         {"202000",  890},
324         {"251000", 1102},
325         {"302000", 1308},
326         {"351000", 1522},
327         {"400000", 1772},
328         {"471000", 2105},
329         {"510000", 2292},
330         {"572000", 2528},
331         {"701000", 3127},
332         {"762000", 3452},
333         {"848000", 4044}};
334 
335     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(p,
336             EnergyConsumerType::OTHER, "GPU", {"S8S_VDD_G3D_L2", "S2S_VDD_G3D"},
337             {{UID_TIME_IN_STATE, "/sys/devices/platform/28000000.mali/uid_time_in_state"}},
338             stateCoeffs));
339 
340     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>("GPU",
341             "/sys/devices/platform/28000000.mali"));
342 }
343 
addMobileRadio(std::shared_ptr<PowerStats> p)344 void addMobileRadio(std::shared_ptr<PowerStats> p)
345 {
346     // A constant to represent the number of microseconds in one millisecond.
347     const int US_TO_MS = 1000;
348 
349     // modem power_stats are reported in microseconds. The transform function
350     // converts microseconds to milliseconds.
351     std::function<uint64_t(uint64_t)> modemUsToMs = [](uint64_t a) { return a / US_TO_MS; };
352     const GenericStateResidencyDataProvider::StateResidencyConfig powerStateConfig = {
353             .entryCountSupported = true,
354             .entryCountPrefix = "count:",
355             .totalTimeSupported = true,
356             .totalTimePrefix = "duration_usec:",
357             .totalTimeTransform = modemUsToMs,
358             .lastEntrySupported = true,
359             .lastEntryPrefix = "last_entry_timestamp_usec:",
360             .lastEntryTransform = modemUsToMs,
361     };
362     const std::vector<std::pair<std::string, std::string>> powerStateHeaders = {
363             std::make_pair("SLEEP", "SLEEP:"),
364     };
365 
366     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
367     cfgs.emplace_back(generateGenericStateResidencyConfigs(powerStateConfig, powerStateHeaders),
368             "MODEM", "");
369 
370     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
371             "/sys/devices/platform/cpif/modem/power_stats", cfgs));
372 
373     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
374             EnergyConsumerType::MOBILE_RADIO, "MODEM",
375             {"VSYS_PWR_MODEM", "VSYS_PWR_RFFE", "VSYS_PWR_MMWAVE"}));
376 }
377 
addGNSS(std::shared_ptr<PowerStats> p)378 void addGNSS(std::shared_ptr<PowerStats> p)
379 {
380     // A constant to represent the number of microseconds in one millisecond.
381     const int US_TO_MS = 1000;
382 
383     // gnss power_stats are reported in microseconds. The transform function
384     // converts microseconds to milliseconds.
385     std::function<uint64_t(uint64_t)> gnssUsToMs = [](uint64_t a) { return a / US_TO_MS; };
386 
387     const GenericStateResidencyDataProvider::StateResidencyConfig gnssStateConfig = {
388         .entryCountSupported = true,
389         .entryCountPrefix = "count:",
390         .totalTimeSupported = true,
391         .totalTimePrefix = "duration_usec:",
392         .totalTimeTransform = gnssUsToMs,
393         .lastEntrySupported = true,
394         .lastEntryPrefix = "last_entry_timestamp_usec:",
395         .lastEntryTransform = gnssUsToMs,
396     };
397 
398     const std::vector<std::pair<std::string, std::string>> gnssStateHeaders = {
399         std::make_pair("ON", "GPS_ON:"),
400         std::make_pair("OFF", "GPS_OFF:"),
401     };
402 
403     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
404     cfgs.emplace_back(generateGenericStateResidencyConfigs(gnssStateConfig, gnssStateHeaders),
405             "GPS", "");
406 
407     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
408             "/dev/bbd_pwrstat", cfgs));
409 
410     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
411             EnergyConsumerType::GNSS, "GPS", {"L9S_GNSS_CORE"}));
412 }
413 
addPCIe(std::shared_ptr<PowerStats> p)414 void addPCIe(std::shared_ptr<PowerStats> p) {
415     // Add PCIe power entities for Modem and WiFi
416     const GenericStateResidencyDataProvider::StateResidencyConfig pcieStateConfig = {
417         .entryCountSupported = true,
418         .entryCountPrefix = "Cumulative count:",
419         .totalTimeSupported = true,
420         .totalTimePrefix = "Cumulative duration msec:",
421         .lastEntrySupported = true,
422         .lastEntryPrefix = "Last entry timestamp msec:",
423     };
424     const std::vector<std::pair<std::string, std::string>> pcieStateHeaders = {
425         std::make_pair("UP", "Link up:"),
426         std::make_pair("DOWN", "Link down:"),
427     };
428 
429     // Add PCIe - Modem
430     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> pcieModemCfgs = {
431         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders), "PCIe-Modem",
432                 "Version: 1"}
433     };
434 
435     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
436             "/sys/devices/platform/11920000.pcie/power_stats", pcieModemCfgs));
437 
438     // Add PCIe - WiFi
439     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> pcieWifiCfgs = {
440         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders),
441             "PCIe-WiFi", "Version: 1"}
442     };
443 
444     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
445             "/sys/devices/platform/14520000.pcie/power_stats", pcieWifiCfgs));
446 }
447 
addWifi(std::shared_ptr<PowerStats> p)448 void addWifi(std::shared_ptr<PowerStats> p) {
449     // The transform function converts microseconds to milliseconds.
450     std::function<uint64_t(uint64_t)> usecToMs = [](uint64_t a) { return a / 1000; };
451     const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
452         .entryCountSupported = true,
453         .entryCountPrefix = "count:",
454         .totalTimeSupported = true,
455         .totalTimePrefix = "duration_usec:",
456         .totalTimeTransform = usecToMs,
457         .lastEntrySupported = true,
458         .lastEntryPrefix = "last_entry_timestamp_usec:",
459         .lastEntryTransform = usecToMs,
460     };
461     const GenericStateResidencyDataProvider::StateResidencyConfig pcieStateConfig = {
462         .entryCountSupported = true,
463         .entryCountPrefix = "count:",
464         .totalTimeSupported = true,
465         .totalTimePrefix = "duration_usec:",
466         .totalTimeTransform = usecToMs,
467         .lastEntrySupported = false,
468     };
469 
470     const std::vector<std::pair<std::string, std::string>> stateHeaders = {
471         std::make_pair("AWAKE", "AWAKE:"),
472         std::make_pair("ASLEEP", "ASLEEP:"),
473 
474     };
475     const std::vector<std::pair<std::string, std::string>> pcieStateHeaders = {
476         std::make_pair("L0", "L0:"),
477         std::make_pair("L1", "L1:"),
478         std::make_pair("L1_1", "L1_1:"),
479         std::make_pair("L1_2", "L1_2:"),
480         std::make_pair("L2", "L2:"),
481     };
482 
483     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs = {
484         {generateGenericStateResidencyConfigs(stateConfig, stateHeaders), "WIFI", "WIFI"},
485         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders), "WIFI-PCIE",
486                 "WIFI-PCIE"}
487     };
488 
489     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>("/sys/wifi/power_stats",
490             cfgs));
491 }
492 
addWlan(std::shared_ptr<PowerStats> p)493 void addWlan(std::shared_ptr<PowerStats> p) {
494     p->addStateResidencyDataProvider(std::make_unique<WlanStateResidencyDataProvider>(
495             "WLAN",
496             "/sys/kernel/wifi/power_stats"));
497 }
498 
addUfs(std::shared_ptr<PowerStats> p)499 void addUfs(std::shared_ptr<PowerStats> p) {
500     p->addStateResidencyDataProvider(std::make_unique<UfsStateResidencyDataProvider>("/sys/bus/platform/devices/14700000.ufs/ufs_stats/"));
501 }
502 
addPowerDomains(std::shared_ptr<PowerStats> p)503 void addPowerDomains(std::shared_ptr<PowerStats> p) {
504     // A constant to represent the number of nanoseconds in one millisecond.
505     const int NS_TO_MS = 1000000;
506 
507     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
508     const GenericStateResidencyDataProvider::StateResidencyConfig cpuStateConfig = {
509             .entryCountSupported = true,
510             .entryCountPrefix = "on_count:",
511             .totalTimeSupported = true,
512             .totalTimePrefix = "total_on_time_ns:",
513             .totalTimeTransform = acpmNsToMs,
514             .lastEntrySupported = true,
515             .lastEntryPrefix = "last_on_time_ns:",
516             .lastEntryTransform = acpmNsToMs,
517     };
518 
519     const std::vector<std::pair<std::string, std::string>> cpuStateHeaders = {
520             std::make_pair("ON", ""),
521     };
522 
523     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
524     for (std::string name : {"pd-aur", "pd-tpu", "pd-bo", "pd-tnr", "pd-gdc", "pd-mcsc", "pd-itp",
525                                 "pd-ipp", "pd-g3aa", "pd-dns", "pd-pdp", "pd-csis",
526                                 "pd-mfc", "pd-g2d", "pd-disp", "pd-dpu", "pd-hsi0",
527                                 "pd-g3d", "pd-embedded_g3d", "pd-eh"}) {
528         cfgs.emplace_back(generateGenericStateResidencyConfigs(cpuStateConfig, cpuStateHeaders),
529             name, name + ":");
530     }
531 
532     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
533             "/sys/devices/platform/acpm_stats/pd_stats", cfgs));
534 }
535 
addDevfreq(std::shared_ptr<PowerStats> p)536 void addDevfreq(std::shared_ptr<PowerStats> p) {
537     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
538             "MIF",
539             "/sys/devices/platform/17000010.devfreq_mif/devfreq/17000010.devfreq_mif"));
540 
541     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
542             "INT",
543             "/sys/devices/platform/17000020.devfreq_int/devfreq/17000020.devfreq_int"));
544 
545     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
546             "INTCAM",
547             "/sys/devices/platform/17000030.devfreq_intcam/devfreq/17000030.devfreq_intcam"));
548 
549     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
550             "DISP",
551             "/sys/devices/platform/17000040.devfreq_disp/devfreq/17000040.devfreq_disp"));
552 
553     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
554             "CAM",
555             "/sys/devices/platform/17000050.devfreq_cam/devfreq/17000050.devfreq_cam"));
556 
557     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
558             "TNR",
559             "/sys/devices/platform/17000060.devfreq_tnr/devfreq/17000060.devfreq_tnr"));
560 
561     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
562             "MFC",
563             "/sys/devices/platform/17000070.devfreq_mfc/devfreq/17000070.devfreq_mfc"));
564 
565     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
566             "BO",
567             "/sys/devices/platform/17000080.devfreq_bo/devfreq/17000080.devfreq_bo"));
568 }
569 
addTPU(std::shared_ptr<PowerStats> p)570 void addTPU(std::shared_ptr<PowerStats> p) {
571     std::map<std::string, int32_t> stateCoeffs;
572 
573     stateCoeffs = {
574         // TODO (b/197721618): Measuring the TPU power numbers
575         {"226000",  10},
576         {"627000",  20},
577         {"845000",  30},
578         {"1066000", 40}};
579 
580     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(p,
581             EnergyConsumerType::OTHER, "TPU", {"S10M_VDD_TPU"},
582             {{UID_TIME_IN_STATE, "/sys/class/edgetpu/edgetpu-soc/device/tpu_usage"}},
583             stateCoeffs));
584 }
585 
586 /**
587  * Unlike other data providers, which source power entity state residency data from the kernel,
588  * this data provider acts as a general-purpose channel for state residency data providers
589  * that live in user space. Entities are defined here and user space clients of this provider's
590  * vendor service register callbacks to provide state residency data for their given pwoer entity.
591  */
addPixelStateResidencyDataProvider(std::shared_ptr<PowerStats> p)592 void addPixelStateResidencyDataProvider(std::shared_ptr<PowerStats> p) {
593 
594     auto pixelSdp = std::make_unique<PixelStateResidencyDataProvider>();
595 
596     pixelSdp->addEntity("Bluetooth", {{0, "Idle"}, {1, "Active"}, {2, "Tx"}, {3, "Rx"}});
597 
598     pixelSdp->start();
599 
600     p->addStateResidencyDataProvider(std::move(pixelSdp));
601 }
602 
addCamera(std::shared_ptr<PowerStats> p)603 void addCamera(std::shared_ptr<PowerStats> p) {
604     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(
605             p,
606             EnergyConsumerType::CAMERA,
607             "CAMERA",
608             {"VSYS_PWR_CAM"}));
609 }
610 
addGs201CommonDataProviders(std::shared_ptr<PowerStats> p)611 void addGs201CommonDataProviders(std::shared_ptr<PowerStats> p) {
612     setEnergyMeter(p);
613 
614     addPixelStateResidencyDataProvider(p);
615     addAoC(p);
616     addDvfsStats(p);
617     addSoC(p);
618     addCPUclusters(p);
619     addGPU(p);
620     addMobileRadio(p);
621     addGNSS(p);
622     addPCIe(p);
623     addWifi(p);
624     addUfs(p);
625     addPowerDomains(p);
626     addDevfreq(p);
627     addTPU(p);
628     addCamera(p);
629 }
630 
addNFC(std::shared_ptr<PowerStats> p,const std::string & path)631 void addNFC(std::shared_ptr<PowerStats> p, const std::string& path) {
632     const GenericStateResidencyDataProvider::StateResidencyConfig nfcStateConfig = {
633         .entryCountSupported = true,
634         .entryCountPrefix = "Cumulative count:",
635         .totalTimeSupported = true,
636         .totalTimePrefix = "Cumulative duration msec:",
637         .lastEntrySupported = true,
638         .lastEntryPrefix = "Last entry timestamp msec:",
639     };
640     const std::vector<std::pair<std::string, std::string>> nfcStateHeaders = {
641         std::make_pair("IDLE", "Idle mode:"),
642         std::make_pair("ACTIVE", "Active mode:"),
643         std::make_pair("ACTIVE-RW", "Active Reader/Writer mode:"),
644     };
645 
646     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
647     cfgs.emplace_back(generateGenericStateResidencyConfigs(nfcStateConfig, nfcStateHeaders),
648             "NFC", "NFC subsystem");
649 
650     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
651             path, cfgs));
652 }
653