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 = { |
samou | a86cc5e | 2023-12-06 07:23:42 +0000 | [diff] [blame] | 76 | .BrownoutStatsEnablePath = "/sys/devices/virtual/pmic/mitigation/br_stats/enable_br_stats", |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 77 | .NumericSysfsStatPaths = { |
| 78 | {"cpu0_freq", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"}, |
| 79 | {"cpu1_freq", "/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"}, |
| 80 | {"cpu2_freq", "/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"}, |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 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 | }, |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 86 | .TriggeredIdxPath = "/sys/devices/virtual/pmic/mitigation/br_stats/triggered_idx", |
George Lee | 1201db7 | 2023-11-09 01:17:09 +0000 | [diff] [blame] | 87 | .triggeredStatePath[android::hardware::google::pixel::UVLO1] = |
| 88 | "/sys/devices/virtual/pmic/mitigation/triggered_state/uvlo1_triggered", |
| 89 | .triggeredStatePath[android::hardware::google::pixel::UVLO2] = |
| 90 | "/sys/devices/virtual/pmic/mitigation/triggered_state/uvlo2_triggered", |
| 91 | .triggeredStatePath[android::hardware::google::pixel::OILO1] = |
| 92 | "/sys/devices/virtual/pmic/mitigation/triggered_state/oilo1_triggered", |
| 93 | .triggeredStatePath[android::hardware::google::pixel::OILO2] = |
| 94 | "/sys/devices/virtual/pmic/mitigation/triggered_state/oilo2_triggered", |
| 95 | .triggeredStatePath[android::hardware::google::pixel::SMPL] = |
| 96 | "/sys/devices/virtual/pmic/mitigation/triggered_state/smpl_triggered", |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 97 | .BrownoutStatsPath = "/sys/devices/virtual/pmic/mitigation/br_stats/stats", |
| 98 | .StoringPath = "/data/vendor/mitigation/thismeal.bin", |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 99 | .ParsedThismealPath = "/data/vendor/mitigation/thismeal.txt", |
| 100 | .ParsedLastmealPath = "/data/vendor/mitigation/lastmeal.txt", |
samou | fface22 | 2023-11-08 02:00:15 +0000 | [diff] [blame] | 101 | .ParsedLastmealCSVPath = "/data/vendor/mitigation/lastmeal.csv", |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 102 | .FvpStatsPath = "/sys/devices/platform/acpm_stats/fvp_stats", |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 103 | .PmicCommon = { |
| 104 | /* Main Pmic */ |
| 105 | { |
| 106 | .OdpmDir = "/sys/bus/iio/devices/iio:device0", |
| 107 | .OdpmEnabledRailsPath = "/sys/bus/iio/devices/iio:device0/enabled_rails", |
| 108 | .PmicNamePath = "/sys/bus/iio/devices/iio:device0/name", |
| 109 | }, |
| 110 | /* Sub Pmic */ |
| 111 | { |
| 112 | .OdpmDir = "/sys/bus/iio/devices/iio:device1", |
| 113 | .OdpmEnabledRailsPath = "/sys/bus/iio/devices/iio:device1/enabled_rails", |
| 114 | .PmicNamePath = "/sys/bus/iio/devices/iio:device1/name", |
| 115 | }, |
| 116 | }, |
| 117 | .PlatformSpecific = { |
samou | b4de8aa | 2023-12-02 06:10:26 +0000 | [diff] [blame] | 118 | .NumericSysfsStatPaths = { |
| 119 | { |
| 120 | .name = "battery_soc", |
| 121 | .paths = { |
| 122 | "/sys/class/power_supply/max77759fg/capacity", |
| 123 | "/sys/class/power_supply/max77779fg/capacity", |
| 124 | }, |
samou | 0a00bc7 | 2023-11-22 06:42:35 +0000 | [diff] [blame] | 125 | }, |
samou | b4de8aa | 2023-12-02 06:10:26 +0000 | [diff] [blame] | 126 | { |
| 127 | .name = "gpu_freq", |
| 128 | .paths = { |
| 129 | "/sys/devices/platform/1c500000.mali/cur_freq", |
| 130 | "/sys/devices/platform/28000000.mali/cur_freq", |
| 131 | "/sys/devices/platform/1f000000.mali/cur_freq", |
| 132 | }, |
samou | 0a00bc7 | 2023-11-22 06:42:35 +0000 | [diff] [blame] | 133 | }, |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 134 | |
samou | b4de8aa | 2023-12-02 06:10:26 +0000 | [diff] [blame] | 135 | }, |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 136 | }, |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 139 | const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready"; |
| 140 | const char kReadyProperty[] = "vendor.brownout.mitigation.ready"; |
| 141 | const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt"; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 142 | const char kBRRequestedProperty[] = "vendor.brownout_reason"; |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 143 | const char kLastMealProperty[] = "vendor.brownout.br.feasible"; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 144 | const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$"); |
| 145 | |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 146 | int main(int argc, char **argv) { |
samou | b4de8aa | 2023-12-02 06:10:26 +0000 | [diff] [blame] | 147 | batteryMitigationService = new BatteryMitigationService(eventThreadCfg); |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 148 | if (!batteryMitigationService) { |
| 149 | return 0; |
| 150 | } |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 151 | bool brownoutStatsBinarySupported = batteryMitigationService->isBrownoutStatsBinarySupported(); |
| 152 | if (argc == 2) { |
| 153 | if(strcmp(argv[1], "-d") == 0 && |
samou | b4de8aa | 2023-12-02 06:10:26 +0000 | [diff] [blame] | 154 | brownoutStatsBinarySupported) { |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 155 | /* Create thismeal.txt from thismeal.bin */ |
| 156 | batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedThismealPath); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame] | 161 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 162 | auto batteryMitigationStartTime = std::chrono::system_clock::now(); |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 163 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 164 | ABinderProcess_startThreadPool(); |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 165 | |
| 166 | bool mitigationLogTimeValid; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 167 | std::string reason = android::base::GetProperty(kBRRequestedProperty, ""); |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 168 | if (brownoutStatsBinarySupported) { |
| 169 | /* Create lastmeal.txt if the dump time in thismeal.bin are valid */ |
| 170 | mitigationLogTimeValid = batteryMitigationService->isTimeValid(eventThreadCfg.StoringPath, |
| 171 | batteryMitigationStartTime); |
| 172 | if (!reason.empty() && mitigationLogTimeValid && |
samou | fface22 | 2023-11-08 02:00:15 +0000 | [diff] [blame] | 173 | batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedLastmealPath) && |
| 174 | batteryMitigationService->genLastmealCSV(eventThreadCfg.ParsedLastmealCSVPath)) { |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 175 | android::base::SetProperty(kLastMealProperty, "1"); |
| 176 | } |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 177 | } else{ |
| 178 | bmSp = new BatteryMitigation(cfg); |
| 179 | if (!bmSp) { |
| 180 | return 0; |
| 181 | } |
| 182 | mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime, |
| 183 | cfg.LogFilePath, |
| 184 | cfg.TimestampFormat, |
| 185 | kTimestampRegex); |
| 186 | if (!reason.empty() && mitigationLogTimeValid) { |
| 187 | std::ifstream src(cfg.LogFilePath, std::ios::in); |
| 188 | std::ofstream dst(kLastMealPath, std::ios::out); |
| 189 | dst << src.rdbuf(); |
| 190 | android::base::SetProperty(kLastMealProperty, "1"); |
| 191 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 192 | } |
samou | e153fc3 | 2023-10-31 16:39:26 +0000 | [diff] [blame] | 193 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 194 | bool isBatteryMitigationReady = false; |
| 195 | std::string ready_str; |
| 196 | int val = 0; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 197 | for (int i = 0; i < COUNT_LIMIT; i++) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 198 | if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) { |
| 199 | continue; |
| 200 | } |
| 201 | ready_str = android::base::Trim(ready_str); |
| 202 | if (!android::base::ParseInt(ready_str, &val)) { |
| 203 | continue; |
| 204 | } |
| 205 | if (val == 1) { |
| 206 | isBatteryMitigationReady = true; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 207 | break; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 210 | if (isBatteryMitigationReady) { |
| 211 | android::base::SetProperty(kReadyProperty, "1"); |
| 212 | } |
George Lee | 1201db7 | 2023-11-09 01:17:09 +0000 | [diff] [blame] | 213 | if (isBatteryMitigationReady && brownoutStatsBinarySupported) { |
| 214 | /* Start BrownoutEventThread to poll brownout event from kernel */ |
| 215 | batteryMitigationService->startBrownoutEventThread(); |
| 216 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 217 | while (true) { |
| 218 | pause(); |
| 219 | } |
| 220 | return 0; |
| 221 | } |