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 | |
| 19 | #include <battery_mitigation/BatteryMitigation.h> |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 20 | #include <android/binder_process.h> |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 21 | |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame^] | 22 | #define COUNT_LIMIT 10 |
| 23 | |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 24 | using android::hardware::google::pixel::BatteryMitigation; |
| 25 | using android::hardware::google::pixel::MitigationConfig; |
| 26 | |
| 27 | android::sp<BatteryMitigation> bmSp; |
| 28 | |
| 29 | const struct MitigationConfig::Config cfg = { |
| 30 | .SystemPath = { |
| 31 | "/dev/thermal/tz-by-name/batoilo/temp", |
| 32 | "/dev/thermal/tz-by-name/smpl_gm/temp", |
| 33 | "/dev/thermal/tz-by-name/soc/temp", |
| 34 | "/dev/thermal/tz-by-name/vdroop1/temp", |
| 35 | "/dev/thermal/tz-by-name/vdroop2/temp", |
| 36 | "/dev/thermal/tz-by-name/ocp_gpu/temp", |
| 37 | "/dev/thermal/tz-by-name/ocp_tpu/temp", |
| 38 | "/dev/thermal/tz-by-name/soft_ocp_cpu2/temp", |
| 39 | "/dev/thermal/tz-by-name/soft_ocp_cpu1/temp", |
| 40 | "/dev/thermal/tz-by-name/battery/temp", |
| 41 | "/dev/thermal/tz-by-name/battery_cycle/temp", |
| 42 | "/sys/bus/iio/devices/iio:device0/lpf_power", |
| 43 | "/sys/bus/iio/devices/iio:device1/lpf_power", |
| 44 | "/dev/thermal/cdev-by-name/thermal-cpufreq-2/cur_state", |
| 45 | "/dev/thermal/cdev-by-name/thermal-cpufreq-1/cur_state", |
| 46 | "/dev/thermal/cdev-by-name/thermal-gpufreq-0/cur_state", |
| 47 | "/dev/thermal/cdev-by-name/tpu_cooling/cur_state", |
| 48 | "/dev/thermal/cdev-by-name/CAM/cur_state", |
| 49 | "/dev/thermal/cdev-by-name/DISP/cur_state", |
| 50 | "/dev/thermal/cdev-by-name/gxp-cooling/cur_state", |
| 51 | "/sys/class/power_supply/battery/voltage_now", |
| 52 | "/sys/class/power_supply/battery/current_now", |
| 53 | }, |
| 54 | .FilteredZones = { |
| 55 | "batoilo", |
| 56 | "vdroop1", |
| 57 | "vdroop2", |
| 58 | "smpl_gm", |
| 59 | }, |
| 60 | .SystemName = { |
| 61 | "batoilo", "smpl_gm", "soc", "vdroop1", "vdroop2", "ocp_gpu", |
| 62 | "ocp_tpu", "soft_ocp_cpu2", "soft_ocp_cpu1", "battery", "battery_cycle", |
| 63 | "main", "sub", "CPU2", "CPU1", "GPU", "TPU", "CAM", "DISP", "NPU", |
| 64 | "voltage_now", "current_now", |
| 65 | }, |
| 66 | .LogFilePath = "/data/vendor/mitigation/thismeal.txt", |
| 67 | .TimestampFormat = "%Y-%m-%d %H:%M:%S", |
| 68 | }; |
| 69 | |
| 70 | const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready"; |
| 71 | const char kReadyProperty[] = "vendor.brownout.mitigation.ready"; |
| 72 | const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt"; |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 73 | const char kBRRequestedProperty[] = "vendor.brownout_reason"; |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 74 | const char kLastMealProperty[] = "vendor.brownout.br.feasible"; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 75 | const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$"); |
| 76 | |
| 77 | int main(int /*argc*/, char ** /*argv*/) { |
| 78 | auto batteryMitigationStartTime = std::chrono::system_clock::now(); |
Xiang Wang | f060fe3 | 2023-02-21 15:14:42 -0800 | [diff] [blame] | 79 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 80 | ABinderProcess_startThreadPool(); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 81 | bmSp = new BatteryMitigation(cfg); |
| 82 | if (!bmSp) { |
| 83 | return 0; |
| 84 | } |
| 85 | bool mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime, |
| 86 | cfg.LogFilePath, |
| 87 | cfg.TimestampFormat, |
| 88 | kTimestampRegex); |
George Lee | fd2395f | 2022-12-02 00:09:30 +0000 | [diff] [blame] | 89 | std::string reason = android::base::GetProperty(kBRRequestedProperty, ""); |
| 90 | if (!reason.empty() && mitigationLogTimeValid) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 91 | std::ifstream src(cfg.LogFilePath, std::ios::in); |
| 92 | std::ofstream dst(kLastMealPath, std::ios::out); |
| 93 | dst << src.rdbuf(); |
George Lee | 930ea07 | 2023-01-27 15:34:38 -0800 | [diff] [blame] | 94 | android::base::SetProperty(kLastMealProperty, "1"); |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 95 | } |
| 96 | bool isBatteryMitigationReady = false; |
| 97 | std::string ready_str; |
| 98 | int val = 0; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame^] | 99 | for (int i = 0; i < COUNT_LIMIT; i++) { |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 100 | if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) { |
| 101 | continue; |
| 102 | } |
| 103 | ready_str = android::base::Trim(ready_str); |
| 104 | if (!android::base::ParseInt(ready_str, &val)) { |
| 105 | continue; |
| 106 | } |
| 107 | if (val == 1) { |
| 108 | isBatteryMitigationReady = true; |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame^] | 109 | break; |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
George Lee | 6c25d5a | 2023-03-24 17:19:14 -0700 | [diff] [blame^] | 112 | if (isBatteryMitigationReady) { |
| 113 | android::base::SetProperty(kReadyProperty, "1"); |
| 114 | } |
George Lee | a1bea3e | 2022-10-24 19:21:53 -0700 | [diff] [blame] | 115 | while (true) { |
| 116 | pause(); |
| 117 | } |
| 118 | return 0; |
| 119 | } |