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 #define LOG_TAG "android.hardware.power.stats@1.0-service.pixel"
18
19 #include <android-base/properties.h>
20 #include <android/log.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 #include <hidl/HidlTransportSupport.h>
25
26 #include <pixelpowerstats/AidlStateResidencyDataProvider.h>
27 #include <pixelpowerstats/GenericStateResidencyDataProvider.h>
28 #include <pixelpowerstats/PowerStats.h>
29 #include <pixelpowerstats/WlanStateResidencyDataProvider.h>
30 #include <pixelpowerstats/DisplayStateResidencyDataProvider.h>
31
32 #include "RailDataProvider.h"
33
34 using android::OK;
35 using android::sp;
36 using android::status_t;
37
38 // libhwbinder:
39 using android::hardware::configureRpcThreadpool;
40 using android::hardware::joinRpcThreadpool;
41
42 // Generated HIDL files
43 using android::hardware::power::stats::V1_0::IPowerStats;
44 using android::hardware::power::stats::V1_0::PowerEntityInfo;
45 using android::hardware::power::stats::V1_0::PowerEntityStateSpace;
46 using android::hardware::power::stats::V1_0::PowerEntityType;
47 using android::hardware::power::stats::V1_0::implementation::PowerStats;
48
49 // Pixel specific
50 using android::hardware::google::pixel::powerstats::AidlStateResidencyDataProvider;
51 using android::hardware::google::pixel::powerstats::GenericStateResidencyDataProvider;
52 using android::hardware::google::pixel::powerstats::PowerEntityConfig;
53 using android::hardware::google::pixel::powerstats::StateResidencyConfig;
54 using android::hardware::google::pixel::powerstats::RailDataProvider;
55 using android::hardware::google::pixel::powerstats::WlanStateResidencyDataProvider;
56 using android::hardware::google::pixel::powerstats::DisplayStateResidencyDataProvider;
57
main(int,char **)58 int main(int /* argc */, char ** /* argv */) {
59 ALOGI("power.stats service 1.0 is starting.");
60
61 bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false);
62
63 PowerStats *service = new PowerStats();
64
65 // Add rail data provider
66 service->setRailDataProvider(std::make_unique<RailDataProvider>());
67
68 // Add power entities related to rpmh
69 const uint64_t RPM_CLK = 19200; // RPM runs at 19.2Mhz. Divide by 19200 for msec
70 std::function<uint64_t(uint64_t)> rpmConvertToMs = [](uint64_t a) { return a / RPM_CLK; };
71 std::vector<StateResidencyConfig> rpmStateResidencyConfigs = {
72 {.name = "Sleep",
73 .entryCountSupported = true,
74 .entryCountPrefix = "Sleep Count:",
75 .totalTimeSupported = true,
76 .totalTimePrefix = "Sleep Accumulated Duration:",
77 .totalTimeTransform = rpmConvertToMs,
78 .lastEntrySupported = true,
79 .lastEntryPrefix = "Sleep Last Entered At:",
80 .lastEntryTransform = rpmConvertToMs}};
81
82 auto rpmSdp = sp<GenericStateResidencyDataProvider>::make("/sys/power/rpmh_stats/master_stats");
83
84 uint32_t apssId = service->addPowerEntity("APSS", PowerEntityType::SUBSYSTEM);
85 rpmSdp->addEntity(apssId, PowerEntityConfig("APSS", rpmStateResidencyConfigs));
86
87 uint32_t mpssId = service->addPowerEntity("MPSS", PowerEntityType::SUBSYSTEM);
88 rpmSdp->addEntity(mpssId, PowerEntityConfig("MPSS", rpmStateResidencyConfigs));
89
90 uint32_t adspId = service->addPowerEntity("ADSP", PowerEntityType::SUBSYSTEM);
91 rpmSdp->addEntity(adspId, PowerEntityConfig("ADSP", rpmStateResidencyConfigs));
92
93 uint32_t adspIslandId = service->addPowerEntity("ADSP_ISLAND", PowerEntityType::SUBSYSTEM);
94 rpmSdp->addEntity(adspIslandId, PowerEntityConfig("ADSP_ISLAND", rpmStateResidencyConfigs));
95
96 uint32_t cdspId = service->addPowerEntity("CDSP", PowerEntityType::SUBSYSTEM);
97 rpmSdp->addEntity(cdspId, PowerEntityConfig("CDSP", rpmStateResidencyConfigs));
98
99 service->addStateResidencyDataProvider(std::move(rpmSdp));
100
101 // Add SoC power entity
102 std::vector<StateResidencyConfig> socStateResidencyConfigs = {
103 {.name = "AOSD",
104 .header = "RPM Mode:aosd",
105 .entryCountSupported = true,
106 .entryCountPrefix = "count:",
107 .totalTimeSupported = true,
108 .totalTimePrefix = "actual last sleep(msec):",
109 .lastEntrySupported = false},
110 {.name = "CXSD",
111 .header = "RPM Mode:cxsd",
112 .entryCountSupported = true,
113 .entryCountPrefix = "count:",
114 .totalTimeSupported = true,
115 .totalTimePrefix = "actual last sleep(msec):",
116 .lastEntrySupported = false}};
117
118 auto socSdp = sp<GenericStateResidencyDataProvider>::make("/sys/power/system_sleep/stats");
119
120 uint32_t socId = service->addPowerEntity("SoC", PowerEntityType::POWER_DOMAIN);
121 socSdp->addEntity(socId, PowerEntityConfig(socStateResidencyConfigs));
122
123 service->addStateResidencyDataProvider(socSdp);
124
125 if (isDebuggable) {
126 // Add WLAN power entity
127 uint32_t wlanId = service->addPowerEntity("WLAN", PowerEntityType::SUBSYSTEM);
128 auto wlanSdp = sp<WlanStateResidencyDataProvider>::make(wlanId,
129 "/sys/kernel/wifi/power_stats");
130 service->addStateResidencyDataProvider(wlanSdp);
131 }
132
133 uint32_t displayId = service->addPowerEntity("Display", PowerEntityType::SUBSYSTEM);
134 auto displaySdp =
135 sp<DisplayStateResidencyDataProvider>::make(displayId,
136 "/sys/class/backlight/panel0-backlight/state",
137 std::vector<std::string>{"Off", "LP", "1080x2340@60", "1080x2340@90"});
138 service->addStateResidencyDataProvider(displaySdp);
139
140 // Add NFC power entity
141 StateResidencyConfig nfcStateConfig = {
142 .entryCountSupported = true,
143 .entryCountPrefix = "Cumulative count:",
144 .totalTimeSupported = true,
145 .totalTimePrefix = "Cumulative duration msec:",
146 .lastEntrySupported = true,
147 .lastEntryPrefix = "Last entry timestamp msec:"
148 };
149 std::vector<std::pair<std::string, std::string>> nfcStateHeaders = {
150 std::make_pair("Idle", "Idle mode:"),
151 std::make_pair("Active", "Active mode:"),
152 std::make_pair("Active-RW", "Active Reader/Writer mode:"),
153 };
154
155 sp<GenericStateResidencyDataProvider> nfcSdp =
156 new GenericStateResidencyDataProvider("/sys/class/misc/st21nfc/device/power_stats");
157
158 uint32_t nfcId = service->addPowerEntity("NFC", PowerEntityType::SUBSYSTEM);
159 nfcSdp->addEntity(nfcId,
160 PowerEntityConfig(generateGenericStateResidencyConfigs(nfcStateConfig, nfcStateHeaders)));
161
162 service->addStateResidencyDataProvider(nfcSdp);
163
164 // Add Power Entities that require the Aidl data provider
165 auto aidlSdp = sp<AidlStateResidencyDataProvider>::make();
166 uint32_t citadelId = service->addPowerEntity("Citadel", PowerEntityType::SUBSYSTEM);
167 aidlSdp->addEntity(citadelId, "Citadel", {"Last-Reset", "Active", "Deep-Sleep"});
168
169 auto serviceStatus = android::defaultServiceManager()->addService(
170 android::String16("power.stats-vendor"), aidlSdp);
171 if (serviceStatus != android::OK) {
172 ALOGE("Unable to register power.stats-vendor service %d", serviceStatus);
173 return 1;
174 }
175 sp<android::ProcessState> ps{android::ProcessState::self()}; // Create non-HW binder threadpool
176 ps->startThreadPool();
177
178 service->addStateResidencyDataProvider(aidlSdp);
179
180 // Configure the threadpool
181 configureRpcThreadpool(1, true /*callerWillJoin*/);
182
183 status_t status = service->registerAsService();
184 if (status != OK) {
185 ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
186 return 1;
187 }
188
189 ALOGI("power.stats service is ready");
190 joinRpcThreadpool();
191
192 // In normal operation, we don't expect the thread pool to exit
193 ALOGE("power.stats service is shutting down");
194 return 1;
195 }
196