blob: 10bf17e97002673c588e34dcbb7797f2400e1c83 [file] [log] [blame]
Benjamin Schwartzc798cb32021-03-18 17:55:40 -07001/*
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
Benjamin Schwartze04673c2021-04-05 22:26:29 -070019#include <dataproviders/DisplayStateResidencyDataProvider.h>
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070020#include <dataproviders/GenericStateResidencyDataProvider.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070021#include <dataproviders/PowerStatsEnergyConsumer.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070022#include <Gs101CommonDataProviders.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070023#include <PowerStatsAidl.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070024
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>
chungkaie593aaa2022-04-06 10:11:12 +000030#include <sys/stat.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070031
Benjamin Schwartze04673c2021-04-05 22:26:29 -070032using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
33using aidl::android::hardware::power::stats::EnergyConsumerType;
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070034using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
Benjamin Schwartze04673c2021-04-05 22:26:29 -070035using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
36
samouf1820db2021-07-29 07:38:05 +000037const char kBootRevision[] = "ro.boot.revision";
38std::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
Benjamin Schwartze04673c2021-04-05 22:26:29 -070044void 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
samouf1820db2021-07-29 07:38:05 +000062 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
Benjamin Schwartze04673c2021-04-05 22:26:29 -070071 // 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,
samouf1820db2021-07-29 07:38:05 +000077 EnergyConsumerType::DISPLAY, "display", {channelName}, "Display",
Benjamin Schwartze04673c2021-04-05 22:26:29 -070078 {{"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
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070088void 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
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700120int main() {
chungkaie593aaa2022-04-06 10:11:12 +0000121 struct stat buffer;
122
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700123 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);
Benjamin Schwartze04673c2021-04-05 22:26:29 -0700131 addDisplay(p);
chungkaie593aaa2022-04-06 10:11:12 +0000132 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 }
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700145 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}