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