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> |
| 21 | #include <sys/resource.h> |
| 22 | #include <system/thread_defs.h> |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 23 | |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 24 | #define COUNT_LIMIT 10 |
| 25 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 26 | using android::hardware::google::pixel::BatteryMitigation; |
| 27 | using android::hardware::google::pixel::MitigationConfig; |
| 28 | |
| 29 | android::sp<BatteryMitigation> bmSp; |
| 30 | |
| 31 | const struct MitigationConfig::Config cfg = { |
| 32 | .SystemPath = { |
| 33 | "/dev/thermal/tz-by-name/batoilo/temp", |
| 34 | "/dev/thermal/tz-by-name/smpl_gm/temp", |
| 35 | "/dev/thermal/tz-by-name/soc/temp", |
| 36 | "/dev/thermal/tz-by-name/vdroop1/temp", |
| 37 | "/dev/thermal/tz-by-name/vdroop2/temp", |
| 38 | "/dev/thermal/tz-by-name/ocp_gpu/temp", |
| 39 | "/dev/thermal/tz-by-name/ocp_tpu/temp", |
| 40 | "/dev/thermal/tz-by-name/soft_ocp_cpu2/temp", |
| 41 | "/dev/thermal/tz-by-name/soft_ocp_cpu1/temp", |
| 42 | "/dev/thermal/tz-by-name/battery/temp", |
| 43 | "/dev/thermal/tz-by-name/battery_cycle/temp", |
| 44 | "/sys/bus/iio/devices/iio:device0/lpf_power", |
| 45 | "/sys/bus/iio/devices/iio:device1/lpf_power", |
| 46 | "/dev/thermal/cdev-by-name/thermal-cpufreq-2/cur_state", |
| 47 | "/dev/thermal/cdev-by-name/thermal-cpufreq-1/cur_state", |
| 48 | "/dev/thermal/cdev-by-name/thermal-gpufreq-0/cur_state", |
| 49 | "/dev/thermal/cdev-by-name/tpu_cooling/cur_state", |
| 50 | "/dev/thermal/cdev-by-name/CAM/cur_state", |
| 51 | "/dev/thermal/cdev-by-name/DISP/cur_state", |
| 52 | "/dev/thermal/cdev-by-name/gxp-cooling/cur_state", |
| 53 | "/sys/class/power_supply/battery/voltage_now", |
| 54 | "/sys/class/power_supply/battery/current_now", |
| 55 | }, |
| 56 | .FilteredZones = { |
| 57 | "batoilo", |
| 58 | "vdroop1", |
| 59 | "vdroop2", |
| 60 | "smpl_gm", |
| 61 | }, |
| 62 | .SystemName = { |
| 63 | "batoilo", "smpl_gm", "soc", "vdroop1", "vdroop2", "ocp_gpu", |
| 64 | "ocp_tpu", "soft_ocp_cpu2", "soft_ocp_cpu1", "battery", "battery_cycle", |
| 65 | "main", "sub", "CPU2", "CPU1", "GPU", "TPU", "CAM", "DISP", "NPU", |
| 66 | "voltage_now", "current_now", |
| 67 | }, |
| 68 | .LogFilePath = "/data/vendor/mitigation/thismeal.txt", |
| 69 | .TimestampFormat = "%Y-%m-%d %H:%M:%S", |
| 70 | }; |
| 71 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 72 | const struct MitigationConfig::EventThreadConfig eventThreadCfg = { |
| 73 | .NumericSysfsStatPaths = { |
| 74 | {"cpu0_freq", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"}, |
| 75 | {"cpu1_freq", "/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"}, |
| 76 | {"cpu2_freq", "/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"}, |
| 77 | {"gpu_freq", "/sys/devices/platform/1f000000.mali/cur_freq"}, |
| 78 | {"battery_temp", "/dev/thermal/tz-by-name/battery/temp"}, |
| 79 | {"battery_cycle", "/dev/thermal/tz-by-name/battery_cycle/temp"}, |
| 80 | {"voltage_now", "/sys/class/power_supply/battery/voltage_now"}, |
| 81 | {"current_now", "/sys/class/power_supply/battery/current_now"}, |
| 82 | }, |
| 83 | .NumericSysfsStatDirs = { |
| 84 | {"last_triggered_mode", "/sys/devices/virtual/pmic/mitigation/last_triggered_mode/"}, |
| 85 | }, |
| 86 | .TriggeredIdxPath = "/sys/devices/virtual/pmic/mitigation/br_stats/triggered_idx", |
| 87 | .BrownoutStatsPath = "/sys/devices/virtual/pmic/mitigation/br_stats/stats", |
| 88 | .StoringPath = "/data/vendor/mitigation/thismeal.bin", |
| 89 | .BackupPath = "/data/vendor/mitigation/lastmeal.bin", |
| 90 | .FvpStatsPath = "/sys/devices/platform/acpm_stats/fvp_stats", |
| 91 | .PcieModemPath = "/sys/devices/platform/12100000.pcie/power_stats", |
| 92 | .PcieWifiPath = "/sys/devices/platform/13120000.pcie/power_stats", |
| 93 | }; |
| 94 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 95 | const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready"; |
| 96 | const char kReadyProperty[] = "vendor.brownout.mitigation.ready"; |
| 97 | const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt"; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 98 | const char kBRRequestedProperty[] = "vendor.brownout_reason"; |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 99 | const char kLastMealProperty[] = "vendor.brownout.br.feasible"; |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 100 | const char kCDTProperty[] = "ro.boot.cdt_hwid"; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 101 | const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$"); |
| 102 | |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 103 | std::string GetSystemProperty(std::string property) { |
| 104 | char value[PROP_VALUE_MAX]; |
| 105 | __system_property_get(property.c_str(), value); |
| 106 | return std::string(value); |
| 107 | } |
| 108 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 109 | int main(int /*argc*/, char ** /*argv*/) { |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 110 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 111 | auto batteryMitigationStartTime = std::chrono::system_clock::now(); |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 112 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 113 | ABinderProcess_startThreadPool(); |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 114 | bmSp = new BatteryMitigation(cfg, eventThreadCfg); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 115 | if (!bmSp) { |
| 116 | return 0; |
| 117 | } |
samou | 440a92f | 2023-09-06 06:24:52 +0000 | [diff] [blame^] | 118 | std::string cdt = GetSystemProperty(kCDTProperty); |
| 119 | int platform_num = atoi(cdt.substr(5, 1).c_str()); |
| 120 | if (platform_num >= MIN_SUPPORTED_PLATFORM) { |
| 121 | bmSp->startBrownoutEventThread(); |
| 122 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 123 | bool mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime, |
| 124 | cfg.LogFilePath, |
| 125 | cfg.TimestampFormat, |
| 126 | kTimestampRegex); |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 127 | std::string reason = android::base::GetProperty(kBRRequestedProperty, ""); |
| 128 | if (!reason.empty() && mitigationLogTimeValid) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 129 | std::ifstream src(cfg.LogFilePath, std::ios::in); |
| 130 | std::ofstream dst(kLastMealPath, std::ios::out); |
| 131 | dst << src.rdbuf(); |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 132 | android::base::SetProperty(kLastMealProperty, "1"); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 133 | } |
| 134 | bool isBatteryMitigationReady = false; |
| 135 | std::string ready_str; |
| 136 | int val = 0; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 137 | for (int i = 0; i < COUNT_LIMIT; i++) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 138 | if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) { |
| 139 | continue; |
| 140 | } |
| 141 | ready_str = android::base::Trim(ready_str); |
| 142 | if (!android::base::ParseInt(ready_str, &val)) { |
| 143 | continue; |
| 144 | } |
| 145 | if (val == 1) { |
| 146 | isBatteryMitigationReady = true; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 147 | break; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame] | 150 | if (isBatteryMitigationReady) { |
| 151 | android::base::SetProperty(kReadyProperty, "1"); |
| 152 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 153 | while (true) { |
| 154 | pause(); |
| 155 | } |
| 156 | return 0; |
| 157 | } |