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