• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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-service.pixel"
18 
19 #include <dataproviders/DisplayStateResidencyDataProvider.h>
20 #include <dataproviders/GenericStateResidencyDataProvider.h>
21 #include <dataproviders/PowerStatsEnergyConsumer.h>
22 #include <Gs101CommonDataProviders.h>
23 #include <PowerStatsAidl.h>
24 
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_process.h>
29 #include <log/log.h>
30 #include <sys/stat.h>
31 
32 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
33 using aidl::android::hardware::power::stats::EnergyConsumerType;
34 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
35 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
36 
37 const char kBootRevision[] = "ro.boot.revision";
38 std::map<std::string, std::string> displayChannelNames = {
39     {"PROTO1.0", "PPVAR_VSYS_PWR_DISP"},
40     {"EVT1.0", "PPVAR_VSYS_PWR_DISP"},
41     {"EVT1.1", "VSYS_PWR_DISPLAY"},
42 };
43 
addDisplay(std::shared_ptr<PowerStats> p)44 void addDisplay(std::shared_ptr<PowerStats> p) {
45     // Add display residency stats
46     std::vector<std::string> states = {
47         "Off",
48         "LP: 1440x3120@10",
49         "LP: 1440x3120@30",
50         "On: 1440x3120@10",
51         "On: 1440x3120@30",
52         "On: 1440x3120@60",
53         "On: 1440x3120@90",
54         "On: 1440x3120@120",
55         "HBM: 1440x3120@60",
56     };
57 
58     p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
59             "/sys/class/backlight/panel0-backlight/state",
60             states));
61 
62     std::string rev = android::base::GetProperty(kBootRevision, "");
63 
64     std::string channelName;
65     if (displayChannelNames.find(rev) == displayChannelNames.end()) {
66         channelName = displayChannelNames["EVT1.1"];
67     } else {
68         channelName = displayChannelNames[rev];
69     }
70 
71     // Add display energy consumer
72     /*
73      * TODO(b/167216667): Add correct display power model here. Must read from display rail
74      * and include proper coefficients for display states.
75      */
76     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p,
77             EnergyConsumerType::DISPLAY, "display", {channelName}, "Display",
78             {{"LP: 1440x3120@10", 1},
79              {"LP: 1440x3120@30", 2},
80              {"On: 1440x3120@10", 3},
81              {"On: 1440x3120@30", 4},
82              {"On: 1440x3120@60", 5},
83              {"On: 1440x3120@90", 6},
84              {"On: 1440x3120@120", 7},
85              {"HBM: 1440x3120@60", 8}}));
86 }
87 
addUwb(std::shared_ptr<PowerStats> p)88 void addUwb(std::shared_ptr<PowerStats> p) {
89     // A constant to represent the number of nanoseconds in one millisecond.
90     const int NS_TO_MS = 1000000;
91 
92     // ACPM stats are reported in nanoseconds. The transform function
93     // converts nanoseconds to milliseconds.
94     std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
95     const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
96             .entryCountSupported = true,
97             .entryCountPrefix = "count:",
98             .totalTimeSupported = true,
99             .totalTimePrefix = "dur ns:",
100             .totalTimeTransform = uwbNsToMs,
101             .lastEntrySupported = false,
102     };
103 
104     const std::vector<std::pair<std::string, std::string>> stateHeaders = {
105             std::make_pair("Off", "Off state:"),
106             std::make_pair("Run", "Run state:"),
107             std::make_pair("Idle", "Idle state:"),
108             std::make_pair("Tx", "Tx state:"),
109             std::make_pair("Rx", "Rx state:"),
110     };
111 
112     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
113     cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders),
114             "UWB", "");
115 
116     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
117             "/sys/devices/platform/10d30000.spi/spi_master/spi10/spi10.0/uwb/power_stats", cfgs));
118 }
119 
main()120 int main() {
121     struct stat buffer;
122 
123     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
124 
125     // single thread
126     ABinderProcess_setThreadPoolMaxThreadCount(0);
127 
128     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
129 
130     addGs101CommonDataProviders(p);
131     addDisplay(p);
132     if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-3/i2c-st21nfc/power_stats", &buffer)) {
133         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-3/i2c-st21nfc/power_stats");
134     } else if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-4/i2c-st21nfc/power_stats", &buffer)) {
135         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-4/i2c-st21nfc/power_stats");
136     } else if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-5/i2c-st21nfc/power_stats", &buffer)) {
137         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-5/i2c-st21nfc/power_stats");
138     } else if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-6/i2c-st21nfc/power_stats", &buffer)) {
139         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-6/i2c-st21nfc/power_stats");
140     } else if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-7/i2c-st21nfc/power_stats", &buffer)) {
141         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-7/i2c-st21nfc/power_stats");
142     } else {
143         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-8/i2c-st21nfc/power_stats");
144     }
145     const std::string instance = std::string() + PowerStats::descriptor + "/default";
146     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
147     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
148 
149     ABinderProcess_joinThreadPool();
150     return EXIT_FAILURE;  // should not reach
151 }
152