• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sp<GenericStateResidencyDataProvider> rpmSdp =
83         new GenericStateResidencyDataProvider("/sys/power/rpmh_stats/master_stats");
84 
85     uint32_t apssId = service->addPowerEntity("APSS", PowerEntityType::SUBSYSTEM);
86     rpmSdp->addEntity(apssId, PowerEntityConfig("APSS", rpmStateResidencyConfigs));
87 
88     uint32_t mpssId = service->addPowerEntity("MPSS", PowerEntityType::SUBSYSTEM);
89     rpmSdp->addEntity(mpssId, PowerEntityConfig("MPSS", rpmStateResidencyConfigs));
90 
91     uint32_t adspId = service->addPowerEntity("ADSP", PowerEntityType::SUBSYSTEM);
92     rpmSdp->addEntity(adspId, PowerEntityConfig("ADSP", rpmStateResidencyConfigs));
93 
94     uint32_t adspIslandId = service->addPowerEntity("ADSP_ISLAND", PowerEntityType::SUBSYSTEM);
95     rpmSdp->addEntity(adspIslandId, PowerEntityConfig("ADSP_ISLAND", rpmStateResidencyConfigs));
96 
97     uint32_t cdspId = service->addPowerEntity("CDSP", PowerEntityType::SUBSYSTEM);
98     rpmSdp->addEntity(cdspId, PowerEntityConfig("CDSP", rpmStateResidencyConfigs));
99 
100     service->addStateResidencyDataProvider(std::move(rpmSdp));
101 
102     // Add SoC power entity
103     std::vector<StateResidencyConfig> socStateResidencyConfigs = {
104         {.name = "AOSD",
105          .header = "RPM Mode:aosd",
106          .entryCountSupported = true,
107          .entryCountPrefix = "count:",
108          .totalTimeSupported = true,
109          .totalTimePrefix = "actual last sleep(msec):",
110          .lastEntrySupported = false},
111         {.name = "CXSD",
112          .header = "RPM Mode:cxsd",
113          .entryCountSupported = true,
114          .entryCountPrefix = "count:",
115          .totalTimeSupported = true,
116          .totalTimePrefix = "actual last sleep(msec):",
117          .lastEntrySupported = false}};
118 
119     sp<GenericStateResidencyDataProvider> socSdp =
120         new GenericStateResidencyDataProvider("/sys/power/system_sleep/stats");
121 
122     uint32_t socId = service->addPowerEntity("SoC", PowerEntityType::POWER_DOMAIN);
123     socSdp->addEntity(socId, PowerEntityConfig(socStateResidencyConfigs));
124 
125     service->addStateResidencyDataProvider(socSdp);
126 
127     if (isDebuggable) {
128         // Add WLAN power entity
129         uint32_t wlanId = service->addPowerEntity("WLAN", PowerEntityType::SUBSYSTEM);
130         sp<WlanStateResidencyDataProvider> wlanSdp =
131             new WlanStateResidencyDataProvider(wlanId, "/sys/kernel/wifi/power_stats");
132         service->addStateResidencyDataProvider(wlanSdp);
133     }
134 
135     uint32_t displayId = service->addPowerEntity("Display", PowerEntityType::SUBSYSTEM);
136     sp<DisplayStateResidencyDataProvider> displaySdp =
137         new DisplayStateResidencyDataProvider(displayId,
138         "/sys/class/backlight/panel0-backlight/state", {"Off", "LP", "1080x2340@60", "1080x2340@90"});
139     service->addStateResidencyDataProvider(displaySdp);
140 
141     // Add Power Entities that require the Aidl data provider
142     sp<AidlStateResidencyDataProvider> aidlSdp = new AidlStateResidencyDataProvider();
143     uint32_t citadelId = service->addPowerEntity("Citadel", PowerEntityType::SUBSYSTEM);
144     aidlSdp->addEntity(citadelId, "Citadel", {"Last-Reset", "Active", "Deep-Sleep"});
145 
146     auto serviceStatus = android::defaultServiceManager()->addService(
147         android::String16("power.stats-vendor"), aidlSdp);
148     if (serviceStatus != android::OK) {
149         ALOGE("Unable to register power.stats-vendor service %d", serviceStatus);
150         return 1;
151     }
152     sp<android::ProcessState> ps{android::ProcessState::self()};  // Create non-HW binder threadpool
153     ps->startThreadPool();
154 
155     service->addStateResidencyDataProvider(aidlSdp);
156 
157     // Configure the threadpool
158     configureRpcThreadpool(1, true /*callerWillJoin*/);
159 
160     status_t status = service->registerAsService();
161     if (status != OK) {
162         ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
163         return 1;
164     }
165 
166     ALOGI("power.stats service is ready");
167     joinRpcThreadpool();
168 
169     // In normal operation, we don't expect the thread pool to exit
170     ALOGE("power.stats service is shutting down");
171     return 1;
172 }
173