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