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     ALOGE("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     service->addStateResidencyDataProvider(rpmSdp);
90 
91     // Add SoC power entity
92     std::vector<StateResidencyConfig> socStateResidencyConfigs = {
93         {.name = "AOSD",
94          .header = "RPM Mode:aosd",
95          .entryCountSupported = true,
96          .entryCountPrefix = "count:",
97          .totalTimeSupported = true,
98          .totalTimePrefix = "actual last sleep(msec):",
99          .lastEntrySupported = false},
100         {.name = "CXSD",
101          .header = "RPM Mode:cxsd",
102          .entryCountSupported = true,
103          .entryCountPrefix = "count:",
104          .totalTimeSupported = true,
105          .totalTimePrefix = "actual last sleep(msec):",
106          .lastEntrySupported = false}};
107 
108     sp<GenericStateResidencyDataProvider> socSdp =
109         new GenericStateResidencyDataProvider("/sys/power/system_sleep/stats");
110 
111     uint32_t socId = service->addPowerEntity("SoC", PowerEntityType::POWER_DOMAIN);
112     socSdp->addEntity(socId, PowerEntityConfig(socStateResidencyConfigs));
113 
114     service->addStateResidencyDataProvider(socSdp);
115 
116     if (isDebuggable) {
117         // Add WLAN power entity
118         uint32_t wlanId = service->addPowerEntity("WLAN", PowerEntityType::SUBSYSTEM);
119         sp<WlanStateResidencyDataProvider> wlanSdp =
120             new WlanStateResidencyDataProvider(wlanId, "/d/wlan0/power_stats");
121         service->addStateResidencyDataProvider(wlanSdp);
122     }
123 
124     // Add Power Entities that require the Aidl data provider
125     sp<AidlStateResidencyDataProvider> aidlSdp = new AidlStateResidencyDataProvider();
126     uint32_t citadelId = service->addPowerEntity("Citadel", PowerEntityType::SUBSYSTEM);
127     aidlSdp->addEntity(citadelId, "Citadel", {"Last-Reset", "Active", "Deep-Sleep"});
128 
129     auto serviceStatus = android::defaultServiceManager()->addService(
130         android::String16("power.stats-vendor"), aidlSdp);
131     if (serviceStatus != android::OK) {
132         ALOGE("Unable to register power.stats-vendor service %d", serviceStatus);
133         return 1;
134     }
135     sp<android::ProcessState> ps{android::ProcessState::self()};  // Create non-HW binder threadpool
136     ps->startThreadPool();
137 
138     service->addStateResidencyDataProvider(aidlSdp);
139 
140     // Configure the threadpool
141     configureRpcThreadpool(1, true /*callerWillJoin*/);
142 
143     status_t status = service->registerAsService();
144     if (status != OK) {
145         ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
146         return 1;
147     }
148 
149     ALOGI("power.stats service is ready");
150     joinRpcThreadpool();
151 
152     // In normal operation, we don't expect the thread pool to exit
153     ALOGE("power.stats service is shutting down");
154     return 1;
155 }
156