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