blob: 18bb63d98c67814365ab5a5b057c009491f68732 [file] [log] [blame]
George Leea1bea3e2022-10-24 19:21:53 -07001/*
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 Wangf060fe32023-02-21 15:14:42 -080019#include <android/binder_process.h>
samou440a92f2023-09-06 06:24:52 +000020#include <battery_mitigation/BatteryMitigation.h>
samoue153fc32023-10-31 16:39:26 +000021#include <battery_mitigation/BatteryMitigationService.h>
samou440a92f2023-09-06 06:24:52 +000022#include <sys/resource.h>
23#include <system/thread_defs.h>
George Leea1bea3e2022-10-24 19:21:53 -070024
George Lee6c25d5a2023-03-24 17:19:14 -070025#define COUNT_LIMIT 10
26
George Leea1bea3e2022-10-24 19:21:53 -070027using android::hardware::google::pixel::BatteryMitigation;
samoue153fc32023-10-31 16:39:26 +000028using android::hardware::google::pixel::BatteryMitigationService;
George Leea1bea3e2022-10-24 19:21:53 -070029using android::hardware::google::pixel::MitigationConfig;
30
31android::sp<BatteryMitigation> bmSp;
samoue153fc32023-10-31 16:39:26 +000032android::sp<BatteryMitigationService> batteryMitigationService;
George Leea1bea3e2022-10-24 19:21:53 -070033
34const 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
samou440a92f2023-09-06 06:24:52 +000075const 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 },
samou440a92f2023-09-06 06:24:52 +000086 .TriggeredIdxPath = "/sys/devices/virtual/pmic/mitigation/br_stats/triggered_idx",
George Lee1201db72023-11-09 01:17:09 +000087 .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",
samou440a92f2023-09-06 06:24:52 +000097 .BrownoutStatsPath = "/sys/devices/virtual/pmic/mitigation/br_stats/stats",
98 .StoringPath = "/data/vendor/mitigation/thismeal.bin",
samoue153fc32023-10-31 16:39:26 +000099 .ParsedThismealPath = "/data/vendor/mitigation/thismeal.txt",
100 .ParsedLastmealPath = "/data/vendor/mitigation/lastmeal.txt",
samoufface222023-11-08 02:00:15 +0000101 .ParsedLastmealCSVPath = "/data/vendor/mitigation/lastmeal.csv",
samou440a92f2023-09-06 06:24:52 +0000102 .FvpStatsPath = "/sys/devices/platform/acpm_stats/fvp_stats",
samoue153fc32023-10-31 16:39:26 +0000103 .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 = {
118 /* MIN_SUPPORTED_PLATFORM */
119 {
120 .MainPmicName = "s2mpg10-odpm\n",
121 .SubPmicName = "s2mpg11-odpm\n",
samou0a00bc72023-11-22 06:42:35 +0000122 .NumericSysfsStatPaths = {
123 {"battery_soc", "/sys/class/power_supply/max77759fg/capacity"},
124 },
samoue153fc32023-10-31 16:39:26 +0000125 },
126 {
127 .MainPmicName = "s2mpg12-odpm\n",
128 .SubPmicName = "s2mpg13-odpm\n",
samou0a00bc72023-11-22 06:42:35 +0000129 .NumericSysfsStatPaths = {
130 {"battery_soc", "/sys/class/power_supply/max77759fg/capacity"},
131 },
samoue153fc32023-10-31 16:39:26 +0000132 },
133 {
134 .MainPmicName = "s2mpg14-odpm\n",
135 .SubPmicName = "s2mpg15-odpm\n",
samou0a00bc72023-11-22 06:42:35 +0000136 .NumericSysfsStatPaths = {
137 {"battery_soc", "/sys/class/power_supply/max77779fg/capacity"},
138 },
samoue153fc32023-10-31 16:39:26 +0000139 },
140 /* MAX_SUPPORTED_PLATFORM */
141 {
142 .MainPmicName = "s2mpg14-odpm\n",
143 .SubPmicName = "s2mpg15-odpm\n",
samou0a00bc72023-11-22 06:42:35 +0000144 .NumericSysfsStatPaths = {
145 {"battery_soc", "/sys/class/power_supply/max77779fg/capacity"},
146 },
samoue153fc32023-10-31 16:39:26 +0000147 },
148
149 },
samou440a92f2023-09-06 06:24:52 +0000150};
151
George Leea1bea3e2022-10-24 19:21:53 -0700152const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready";
153const char kReadyProperty[] = "vendor.brownout.mitigation.ready";
154const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt";
George Leefd2395f2022-12-02 00:09:30 +0000155const char kBRRequestedProperty[] = "vendor.brownout_reason";
George Lee930ea072023-01-27 15:34:38 -0800156const char kLastMealProperty[] = "vendor.brownout.br.feasible";
samou440a92f2023-09-06 06:24:52 +0000157const char kCDTProperty[] = "ro.boot.cdt_hwid";
George Leea1bea3e2022-10-24 19:21:53 -0700158const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$");
159
samou440a92f2023-09-06 06:24:52 +0000160std::string GetSystemProperty(std::string property) {
161 char value[PROP_VALUE_MAX];
162 __system_property_get(property.c_str(), value);
163 return std::string(value);
164}
165
samoue153fc32023-10-31 16:39:26 +0000166int main(int argc, char **argv) {
167 std::string cdt = GetSystemProperty(kCDTProperty);
168 int platformNum = atoi(cdt.substr(5, 1).c_str());
169 batteryMitigationService = new BatteryMitigationService(eventThreadCfg,
170 platformNum);
171 if (!batteryMitigationService) {
172 return 0;
173 }
174 bool platformSupported = batteryMitigationService->isPlatformSupported();
175 bool brownoutStatsBinarySupported = batteryMitigationService->isBrownoutStatsBinarySupported();
176 if (argc == 2) {
177 if(strcmp(argv[1], "-d") == 0 &&
178 brownoutStatsBinarySupported &&
179 platformSupported) {
180 /* Create thismeal.txt from thismeal.bin */
181 batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedThismealPath);
182 }
183 return 0;
184 }
185
samou440a92f2023-09-06 06:24:52 +0000186 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
George Leea1bea3e2022-10-24 19:21:53 -0700187 auto batteryMitigationStartTime = std::chrono::system_clock::now();
Xiang Wangf060fe32023-02-21 15:14:42 -0800188 ABinderProcess_setThreadPoolMaxThreadCount(1);
189 ABinderProcess_startThreadPool();
samoue153fc32023-10-31 16:39:26 +0000190
191 bool mitigationLogTimeValid;
George Leefd2395f2022-12-02 00:09:30 +0000192 std::string reason = android::base::GetProperty(kBRRequestedProperty, "");
samoue153fc32023-10-31 16:39:26 +0000193 if (brownoutStatsBinarySupported) {
194 /* Create lastmeal.txt if the dump time in thismeal.bin are valid */
195 mitigationLogTimeValid = batteryMitigationService->isTimeValid(eventThreadCfg.StoringPath,
196 batteryMitigationStartTime);
197 if (!reason.empty() && mitigationLogTimeValid &&
samoufface222023-11-08 02:00:15 +0000198 batteryMitigationService->genParsedMeal(eventThreadCfg.ParsedLastmealPath) &&
199 batteryMitigationService->genLastmealCSV(eventThreadCfg.ParsedLastmealCSVPath)) {
samoue153fc32023-10-31 16:39:26 +0000200 android::base::SetProperty(kLastMealProperty, "1");
201 }
samoue153fc32023-10-31 16:39:26 +0000202 } else{
203 bmSp = new BatteryMitigation(cfg);
204 if (!bmSp) {
205 return 0;
206 }
207 mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime,
208 cfg.LogFilePath,
209 cfg.TimestampFormat,
210 kTimestampRegex);
211 if (!reason.empty() && mitigationLogTimeValid) {
212 std::ifstream src(cfg.LogFilePath, std::ios::in);
213 std::ofstream dst(kLastMealPath, std::ios::out);
214 dst << src.rdbuf();
215 android::base::SetProperty(kLastMealProperty, "1");
216 }
George Leea1bea3e2022-10-24 19:21:53 -0700217 }
samoue153fc32023-10-31 16:39:26 +0000218
George Leea1bea3e2022-10-24 19:21:53 -0700219 bool isBatteryMitigationReady = false;
220 std::string ready_str;
221 int val = 0;
George Lee6c25d5a2023-03-24 17:19:14 -0700222 for (int i = 0; i < COUNT_LIMIT; i++) {
George Leea1bea3e2022-10-24 19:21:53 -0700223 if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) {
224 continue;
225 }
226 ready_str = android::base::Trim(ready_str);
227 if (!android::base::ParseInt(ready_str, &val)) {
228 continue;
229 }
230 if (val == 1) {
231 isBatteryMitigationReady = true;
George Lee6c25d5a2023-03-24 17:19:14 -0700232 break;
George Leea1bea3e2022-10-24 19:21:53 -0700233 }
234 }
George Lee6c25d5a2023-03-24 17:19:14 -0700235 if (isBatteryMitigationReady) {
236 android::base::SetProperty(kReadyProperty, "1");
237 }
George Lee1201db72023-11-09 01:17:09 +0000238 if (isBatteryMitigationReady && brownoutStatsBinarySupported) {
239 /* Start BrownoutEventThread to poll brownout event from kernel */
240 batteryMitigationService->startBrownoutEventThread();
241 }
George Leea1bea3e2022-10-24 19:21:53 -0700242 while (true) {
243 pause();
244 }
245 return 0;
246}