George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "battery-mitigation" |
| 18 | |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 19 | #include <android/binder_process.h> |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 20 | #include <battery_mitigation/BatteryMitigation.h> |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 21 | #include <battery_mitigation/BatteryMitigationService.h> |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 22 | #include <sys/resource.h> |
| 23 | #include <system/thread_defs.h> |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 24 | |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 25 | #define COUNT_LIMIT 10 |
| 26 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 27 | using android::hardware::google::pixel::BatteryMitigation; |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 28 | using android::hardware::google::pixel::BatteryMitigationService; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 29 | using android::hardware::google::pixel::MitigationConfig; |
| 30 | |
| 31 | android::sp<BatteryMitigation> bmSp; |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 32 | android::sp<BatteryMitigationService> batteryMitigationService; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 33 | |
| 34 | const struct MitigationConfig::Config cfg = { |
| 35 | .SystemPath = { |
| 36 | "/dev/thermal/tz-by-name/batoilo/temp", |
| 37 | "/dev/thermal/tz-by-name/smpl_gm/temp", |
| 38 | "/dev/thermal/tz-by-name/soc/temp", |
| 39 | "/dev/thermal/tz-by-name/vdroop1/temp", |
| 40 | "/dev/thermal/tz-by-name/vdroop2/temp", |
| 41 | "/dev/thermal/tz-by-name/ocp_gpu/temp", |
| 42 | "/dev/thermal/tz-by-name/ocp_tpu/temp", |
| 43 | "/dev/thermal/tz-by-name/soft_ocp_cpu2/temp", |
| 44 | "/dev/thermal/tz-by-name/soft_ocp_cpu1/temp", |
| 45 | "/dev/thermal/tz-by-name/battery/temp", |
| 46 | "/dev/thermal/tz-by-name/battery_cycle/temp", |
| 47 | "/sys/bus/iio/devices/iio:device0/lpf_power", |
| 48 | "/sys/bus/iio/devices/iio:device1/lpf_power", |
| 49 | "/dev/thermal/cdev-by-name/thermal-cpufreq-2/cur_state", |
| 50 | "/dev/thermal/cdev-by-name/thermal-cpufreq-1/cur_state", |
| 51 | "/dev/thermal/cdev-by-name/thermal-gpufreq-0/cur_state", |
| 52 | "/dev/thermal/cdev-by-name/tpu_cooling/cur_state", |
| 53 | "/dev/thermal/cdev-by-name/CAM/cur_state", |
| 54 | "/dev/thermal/cdev-by-name/DISP/cur_state", |
| 55 | "/dev/thermal/cdev-by-name/gxp-cooling/cur_state", |
| 56 | "/sys/class/power_supply/battery/voltage_now", |
| 57 | "/sys/class/power_supply/battery/current_now", |
| 58 | }, |
| 59 | .FilteredZones = { |
| 60 | "batoilo", |
| 61 | "vdroop1", |
| 62 | "vdroop2", |
| 63 | "smpl_gm", |
| 64 | }, |
| 65 | .SystemName = { |
| 66 | "batoilo", "smpl_gm", "soc", "vdroop1", "vdroop2", "ocp_gpu", |
| 67 | "ocp_tpu", "soft_ocp_cpu2", "soft_ocp_cpu1", "battery", "battery_cycle", |
| 68 | "main", "sub", "CPU2", "CPU1", "GPU", "TPU", "CAM", "DISP", "NPU", |
| 69 | "voltage_now", "current_now", |
| 70 | }, |
| 71 | .LogFilePath = "/data/vendor/mitigation/thismeal.txt", |
| 72 | .TimestampFormat = "%Y-%m-%d %H:%M:%S", |
| 73 | }; |
| 74 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 75 | const struct MitigationConfig::EventThreadConfig eventThreadCfg = { |
| 76 | .NumericSysfsStatPaths = { |
| 77 | {"cpu0_freq", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"}, |
| 78 | {"cpu1_freq", "/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"}, |
| 79 | {"cpu2_freq", "/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"}, |
| 80 | {"gpu_freq", "/sys/devices/platform/1f000000.mali/cur_freq"}, |
| 81 | {"battery_temp", "/dev/thermal/tz-by-name/battery/temp"}, |
| 82 | {"battery_cycle", "/dev/thermal/tz-by-name/battery_cycle/temp"}, |
| 83 | {"voltage_now", "/sys/class/power_supply/battery/voltage_now"}, |
| 84 | {"current_now", "/sys/class/power_supply/battery/current_now"}, |
| 85 | }, |
| 86 | .NumericSysfsStatDirs = { |
| 87 | {"last_triggered_mode", "/sys/devices/virtual/pmic/mitigation/last_triggered_mode/"}, |
| 88 | }, |
| 89 | .TriggeredIdxPath = "/sys/devices/virtual/pmic/mitigation/br_stats/triggered_idx", |
| 90 | .BrownoutStatsPath = "/sys/devices/virtual/pmic/mitigation/br_stats/stats", |
| 91 | .StoringPath = "/data/vendor/mitigation/thismeal.bin", |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 92 | .ParsedThismealPath = "/data/vendor/mitigation/thismeal.txt", |
| 93 | .ParsedLastmealPath = "/data/vendor/mitigation/lastmeal.txt", |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 94 | .FvpStatsPath = "/sys/devices/platform/acpm_stats/fvp_stats", |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 95 | .PmicCommon = { |
| 96 | /* Main Pmic */ |
| 97 | { |
| 98 | .OdpmDir = "/sys/bus/iio/devices/iio:device0", |
| 99 | .OdpmEnabledRailsPath = "/sys/bus/iio/devices/iio:device0/enabled_rails", |
| 100 | .PmicNamePath = "/sys/bus/iio/devices/iio:device0/name", |
| 101 | }, |
| 102 | /* Sub Pmic */ |
| 103 | { |
| 104 | .OdpmDir = "/sys/bus/iio/devices/iio:device1", |
| 105 | .OdpmEnabledRailsPath = "/sys/bus/iio/devices/iio:device1/enabled_rails", |
| 106 | .PmicNamePath = "/sys/bus/iio/devices/iio:device1/name", |
| 107 | }, |
| 108 | }, |
| 109 | .PlatformSpecific = { |
| 110 | /* MIN_SUPPORTED_PLATFORM */ |
| 111 | { |
| 112 | .MainPmicName = "s2mpg10-odpm\n", |
| 113 | .SubPmicName = "s2mpg11-odpm\n", |
| 114 | .PcieModemPath = "/sys/devices/platform/11920000.pcie/power_stats", |
| 115 | .PcieWifiPath = "/sys/devices/platform/14520000.pcie/power_stats", |
| 116 | }, |
| 117 | { |
| 118 | .MainPmicName = "s2mpg12-odpm\n", |
| 119 | .SubPmicName = "s2mpg13-odpm\n", |
| 120 | .PcieModemPath = "/sys/devices/platform/11920000.pcie/power_stats", |
| 121 | .PcieWifiPath = "/sys/devices/platform/14520000.pcie/power_stats", |
| 122 | }, |
| 123 | { |
| 124 | .MainPmicName = "s2mpg14-odpm\n", |
| 125 | .SubPmicName = "s2mpg15-odpm\n", |
| 126 | .PcieModemPath = "/sys/devices/platform/12100000.pcie/power_stats", |
| 127 | .PcieWifiPath = "/sys/devices/platform/13120000.pcie/power_stats", |
| 128 | }, |
| 129 | /* MAX_SUPPORTED_PLATFORM */ |
| 130 | { |
| 131 | .MainPmicName = "s2mpg14-odpm\n", |
| 132 | .SubPmicName = "s2mpg15-odpm\n", |
| 133 | .PcieModemPath = "/sys/devices/platform/12100000.pcie/power_stats", |
| 134 | .PcieWifiPath = "/sys/devices/platform/13120000.pcie/power_stats", |
| 135 | }, |
| 136 | |
| 137 | }, |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 140 | const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready"; |
| 141 | const char kReadyProperty[] = "vendor.brownout.mitigation.ready"; |
| 142 | const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt"; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 143 | const char kBRRequestedProperty[] = "vendor.brownout_reason"; |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 144 | const char kLastMealProperty[] = "vendor.brownout.br.feasible"; |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 145 | const char kCDTProperty[] = "ro.boot.cdt_hwid"; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 146 | const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$"); |
| 147 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 148 | std::string GetSystemProperty(std::string property) { |
| 149 | char value[PROP_VALUE_MAX]; |
| 150 | __system_property_get(property.c_str(), value); |
| 151 | return std::string(value); |
| 152 | } |
| 153 | |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 154 | int main(int argc, char **argv) { |
| 155 | std::string cdt = GetSystemProperty(kCDTProperty); |
| 156 | int platformNum = atoi(cdt.substr(5, 1).c_str()); |
| 157 | batteryMitigationService = new BatteryMitigationService(eventThreadCfg, |
| 158 | platformNum); |
| 159 | if (!batteryMitigationService) { |
| 160 | return 0; |
| 161 | } |
| 162 | bool platformSupported = batteryMitigationService->isPlatformSupported(); |
| 163 | bool brownoutStatsBinarySupported = batteryMitigationService->isBrownoutStatsBinarySupported(); |
| 164 | if (argc == 2) { |
| 165 | if(strcmp(argv[1], "-d") == 0 && |
| 166 | brownoutStatsBinarySupported && |
| 167 | platformSupported) { |
| 168 | /* Create thismeal.txt from thismeal.bin */ |
| 169 | batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedThismealPath); |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 174 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 175 | auto batteryMitigationStartTime = std::chrono::system_clock::now(); |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 176 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 177 | ABinderProcess_startThreadPool(); |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 178 | |
| 179 | bool mitigationLogTimeValid; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 180 | std::string reason = android::base::GetProperty(kBRRequestedProperty, ""); |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 181 | if (brownoutStatsBinarySupported) { |
| 182 | /* Create lastmeal.txt if the dump time in thismeal.bin are valid */ |
| 183 | mitigationLogTimeValid = batteryMitigationService->isTimeValid(eventThreadCfg.StoringPath, |
| 184 | batteryMitigationStartTime); |
| 185 | if (!reason.empty() && mitigationLogTimeValid && |
| 186 | batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedLastmealPath)) { |
| 187 | android::base::SetProperty(kLastMealProperty, "1"); |
| 188 | } |
| 189 | /* Start BrownoutEventThread to poll brownout event from kernel */ |
| 190 | batteryMitigationService->startBrownoutEventThread(); |
| 191 | } else{ |
| 192 | bmSp = new BatteryMitigation(cfg); |
| 193 | if (!bmSp) { |
| 194 | return 0; |
| 195 | } |
| 196 | mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime, |
| 197 | cfg.LogFilePath, |
| 198 | cfg.TimestampFormat, |
| 199 | kTimestampRegex); |
| 200 | if (!reason.empty() && mitigationLogTimeValid) { |
| 201 | std::ifstream src(cfg.LogFilePath, std::ios::in); |
| 202 | std::ofstream dst(kLastMealPath, std::ios::out); |
| 203 | dst << src.rdbuf(); |
| 204 | android::base::SetProperty(kLastMealProperty, "1"); |
| 205 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 206 | } |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame^] | 207 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 208 | bool isBatteryMitigationReady = false; |
| 209 | std::string ready_str; |
| 210 | int val = 0; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 211 | for (int i = 0; i < COUNT_LIMIT; i++) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 212 | if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) { |
| 213 | continue; |
| 214 | } |
| 215 | ready_str = android::base::Trim(ready_str); |
| 216 | if (!android::base::ParseInt(ready_str, &val)) { |
| 217 | continue; |
| 218 | } |
| 219 | if (val == 1) { |
| 220 | isBatteryMitigationReady = true; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 221 | break; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 224 | if (isBatteryMitigationReady) { |
| 225 | android::base::SetProperty(kReadyProperty, "1"); |
| 226 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 227 | while (true) { |
| 228 | pause(); |
| 229 | } |
| 230 | return 0; |
| 231 | } |