1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "[email protected]"
18
19 #include <android/log.h>
20 #include <binder/IPCThreadState.h>
21 #include <binder/IServiceManager.h>
22 #include <binder/ProcessState.h>
23 #include <hidl/HidlTransportSupport.h>
24
25 #include <pixelpowerstats/AidlStateResidencyDataProvider.h>
26 #include <pixelpowerstats/GenericStateResidencyDataProvider.h>
27 #include <pixelpowerstats/PowerStats.h>
28 #include <pixelpowerstats/WlanStateResidencyDataProvider.h>
29
30 #include "RailDataProvider.h"
31
32 using android::OK;
33 using android::sp;
34 using android::status_t;
35
36 // libhwbinder:
37 using android::hardware::configureRpcThreadpool;
38 using android::hardware::joinRpcThreadpool;
39
40 // Generated HIDL files
41 using android::hardware::power::stats::V1_0::IPowerStats;
42 using android::hardware::power::stats::V1_0::PowerEntityInfo;
43 using android::hardware::power::stats::V1_0::PowerEntityStateSpace;
44 using android::hardware::power::stats::V1_0::PowerEntityType;
45 using android::hardware::power::stats::V1_0::implementation::PowerStats;
46
47 // Pixel specific
48 using android::hardware::google::pixel::powerstats::AidlStateResidencyDataProvider;
49 using android::hardware::google::pixel::powerstats::generateGenericStateResidencyConfigs;
50 using android::hardware::google::pixel::powerstats::GenericStateResidencyDataProvider;
51 using android::hardware::google::pixel::powerstats::PowerEntityConfig;
52 using android::hardware::google::pixel::powerstats::StateResidencyConfig;
53 using android::hardware::google::pixel::powerstats::RailDataProvider;
54 using android::hardware::google::pixel::powerstats::WlanStateResidencyDataProvider;
55
main(int,char **)56 int main(int /* argc */, char ** /* argv */) {
57 ALOGE("power.stats service 1.0 is starting.");
58
59
60 PowerStats *service = new PowerStats();
61
62 // Add rail data provider
63 service->setRailDataProvider(std::make_unique<RailDataProvider>());
64
65 // Add power entities related to rpmh
66 const uint64_t RPM_CLK = 19200; // RPM runs at 19.2Mhz. Divide by 19200 for msec
67 std::function<uint64_t(uint64_t)> rpmConvertToMs = [](uint64_t a) { return a / RPM_CLK; };
68 std::vector<StateResidencyConfig> rpmStateResidencyConfigs = {
69 {.name = "Sleep",
70 .entryCountSupported = true,
71 .entryCountPrefix = "Sleep Count:",
72 .totalTimeSupported = true,
73 .totalTimePrefix = "Sleep Accumulated Duration:",
74 .totalTimeTransform = rpmConvertToMs,
75 .lastEntrySupported = true,
76 .lastEntryPrefix = "Sleep Last Entered At:",
77 .lastEntryTransform = rpmConvertToMs}};
78
79 sp<GenericStateResidencyDataProvider> rpmSdp =
80 new GenericStateResidencyDataProvider("/sys/power/rpmh_stats/master_stats");
81
82 uint32_t apssId = service->addPowerEntity("APSS", PowerEntityType::SUBSYSTEM);
83 rpmSdp->addEntity(apssId, PowerEntityConfig("APSS", rpmStateResidencyConfigs));
84
85 uint32_t mpssId = service->addPowerEntity("MPSS", PowerEntityType::SUBSYSTEM);
86 rpmSdp->addEntity(mpssId, PowerEntityConfig("MPSS", rpmStateResidencyConfigs));
87
88 uint32_t adspId = service->addPowerEntity("ADSP", PowerEntityType::SUBSYSTEM);
89 rpmSdp->addEntity(adspId, PowerEntityConfig("ADSP", rpmStateResidencyConfigs));
90
91 uint32_t cdspId = service->addPowerEntity("CDSP", PowerEntityType::SUBSYSTEM);
92 rpmSdp->addEntity(cdspId, PowerEntityConfig("CDSP", rpmStateResidencyConfigs));
93
94 service->addStateResidencyDataProvider(rpmSdp);
95
96 // Add SoC power entity
97 StateResidencyConfig socStateConfig = {
98 .entryCountSupported = true,
99 .entryCountPrefix = "count:",
100 .totalTimeSupported = true,
101 .totalTimePrefix = "actual last sleep(msec):",
102 .lastEntrySupported = false
103 };
104 std::vector<std::pair<std::string, std::string>> socStateHeaders = {
105 std::make_pair("AOSD", "RPM Mode:aosd"),
106 std::make_pair("CXSD", "RPM Mode:cxsd"),
107 std::make_pair("DDR", "RPM Mode:ddr"),
108 };
109
110 sp<GenericStateResidencyDataProvider> socSdp =
111 new GenericStateResidencyDataProvider("/sys/power/system_sleep/stats");
112
113 uint32_t socId = service->addPowerEntity("SoC", PowerEntityType::POWER_DOMAIN);
114 socSdp->addEntity(socId,
115 PowerEntityConfig(generateGenericStateResidencyConfigs(socStateConfig, socStateHeaders)));
116
117 service->addStateResidencyDataProvider(socSdp);
118
119 // Add WLAN power entity
120 uint32_t wlanId = service->addPowerEntity("WLAN", PowerEntityType::SUBSYSTEM);
121 sp<WlanStateResidencyDataProvider> wlanSdp =
122 new WlanStateResidencyDataProvider(wlanId, "/sys/kernel/wlan/power_stats");
123 service->addStateResidencyDataProvider(wlanSdp);
124
125 // Add Power Entities that require the Aidl data provider
126 sp<AidlStateResidencyDataProvider> aidlSdp = new AidlStateResidencyDataProvider();
127 uint32_t citadelId = service->addPowerEntity("Citadel", PowerEntityType::SUBSYSTEM);
128 aidlSdp->addEntity(citadelId, "Citadel", {"Last-Reset", "Active", "Deep-Sleep"});
129
130 auto serviceStatus = android::defaultServiceManager()->addService(
131 android::String16("power.stats-vendor"), aidlSdp);
132 if (serviceStatus != android::OK) {
133 ALOGE("Unable to register power.stats-vendor service %d", serviceStatus);
134 return 1;
135 }
136 sp<android::ProcessState> ps{android::ProcessState::self()}; // Create non-HW binder threadpool
137 ps->startThreadPool();
138
139 service->addStateResidencyDataProvider(aidlSdp);
140
141 // Configure the threadpool
142 configureRpcThreadpool(1, true /*callerWillJoin*/);
143
144 status_t status = service->registerAsService();
145 if (status != OK) {
146 ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
147 return 1;
148 }
149
150 ALOGI("power.stats service is ready");
151 joinRpcThreadpool();
152
153 // In normal operation, we don't expect the thread pool to exit
154 ALOGE("power.stats service is shutting down");
155 return 1;
156 }
157