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