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 <DevfreqStateResidencyDataProvider.h>
23 #include <Gs201CommonDataProviders.h>
24 #include <PowerStatsAidl.h>
25 
26 #include <android-base/logging.h>
27 #include <android-base/properties.h>
28 #include <android/binder_manager.h>
29 #include <android/binder_process.h>
30 #include <log/log.h>
31 #include <sys/stat.h>
32 
33 using aidl::android::hardware::power::stats::DevfreqStateResidencyDataProvider;
34 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
35 using aidl::android::hardware::power::stats::EnergyConsumerType;
36 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
37 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
38 
addDisplay(std::shared_ptr<PowerStats> p)39 void addDisplay(std::shared_ptr<PowerStats> p) {
40     // Add display residency stats
41     struct stat buffer;
42     if (!stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) {
43         // time_in_state exists
44         addDisplayMrr(p);
45     } else {
46         // time_in_state doesn't exist
47         std::vector<std::string> states = {
48             "Off",
49             "On: 1600x2560@60",
50             "HBM: 1600x2560@60"};
51 
52         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
53                 "/sys/class/backlight/panel0-backlight/state",
54                 states));
55     }
56 
57     // Add display energy consumer
58     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(
59             p,
60             EnergyConsumerType::DISPLAY,
61             "Display",
62             {"VSYS_PWR_DISPLAY"}));
63 }
64 
addGPUGs202(std::shared_ptr<PowerStats> p)65 void addGPUGs202(std::shared_ptr<PowerStats> p) {
66     std::map<std::string, int32_t> stateCoeffs;
67 
68     // Add GPU state residency
69     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
70             "GPU",
71             "/sys/devices/platform/28000000.mali"));
72 
73     // Add GPU energy consumer
74     stateCoeffs = {
75         {"202000",  890},
76         {"251000", 1102},
77         {"302000", 1308},
78         {"351000", 1522},
79         {"400000", 1772},
80         {"434000", 1931},
81         {"471000", 2105},
82         {"510000", 2292},
83         {"572000", 2528},
84         {"633000", 2811},
85         {"701000", 3127},
86         {"762000", 3452},
87         {"848000", 4044}};
88 
89     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(
90             p,
91             EnergyConsumerType::OTHER,
92             "GPU",
93             {"S2S_VDD_G3D", "S8S_VDD_G3D_L2"},
94             {{UID_TIME_IN_STATE, "/sys/devices/platform/28000000.mali/uid_time_in_state"}},
95             stateCoeffs));
96 }
97 
addUwb(std::shared_ptr<PowerStats> p)98 void addUwb(std::shared_ptr<PowerStats> p) {
99     // A constant to represent the number of nanoseconds in one millisecond.
100     const int NS_TO_MS = 1000000;
101 
102     // ACPM stats are reported in nanoseconds. The transform function
103     // converts nanoseconds to milliseconds.
104     std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
105     const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
106             .entryCountSupported = true,
107             .entryCountPrefix = "count:",
108             .totalTimeSupported = true,
109             .totalTimePrefix = "dur ns:",
110             .totalTimeTransform = uwbNsToMs,
111             .lastEntrySupported = false,
112     };
113 
114     const std::vector<std::pair<std::string, std::string>> stateHeaders = {
115             std::make_pair("Off", "Off state:"),
116             std::make_pair("Deep sleep", "Deep sleep state:"),
117             std::make_pair("Run", "Run state:"),
118             std::make_pair("Idle", "Idle state:"),
119             std::make_pair("Tx", "Tx state:"),
120             std::make_pair("Rx", "Rx state:"),
121     };
122 
123     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
124     cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders),
125             "UWB", "");
126 
127     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
128             "/sys/devices/platform/10db0000.spi/spi_master/spi16/spi16.0/uwb/power_stats", cfgs));
129 }
130 
main()131 int main() {
132     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
133 
134     // single thread
135     ABinderProcess_setThreadPoolMaxThreadCount(0);
136 
137     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
138 
139     setEnergyMeter(p);
140     addAoC(p);
141     addPixelStateResidencyDataProvider(p);
142     addCPUclusters(p);
143     addDisplay(p);
144     addSoC(p);
145     addWifi(p);
146     addTPU(p);
147     addUfs(p);
148     addUwb(p);
149     addPowerDomains(p);
150     addDevfreq(p);
151     addGPUGs202(p);
152     addDvfsStats(p);
153 
154     const std::string instance = std::string() + PowerStats::descriptor + "/default";
155     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
156     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
157 
158     ABinderProcess_joinThreadPool();
159     return EXIT_FAILURE;  // should not reach
160 }
161