blob: a09b8a4c658a9c9427826a1969611f76c5e762f8 [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>
21#include <sys/resource.h>
22#include <system/thread_defs.h>
George Leea1bea3e2022-10-24 19:21:53 -070023
George Lee6c25d5a2023-03-24 17:19:14 -070024#define COUNT_LIMIT 10
25
George Leea1bea3e2022-10-24 19:21:53 -070026using android::hardware::google::pixel::BatteryMitigation;
27using android::hardware::google::pixel::MitigationConfig;
28
29android::sp<BatteryMitigation> bmSp;
30
31const 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
samou440a92f2023-09-06 06:24:52 +000072const 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 Leea1bea3e2022-10-24 19:21:53 -070095const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready";
96const char kReadyProperty[] = "vendor.brownout.mitigation.ready";
97const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt";
George Leefd2395f2022-12-02 00:09:30 +000098const char kBRRequestedProperty[] = "vendor.brownout_reason";
George Lee930ea072023-01-27 15:34:38 -080099const char kLastMealProperty[] = "vendor.brownout.br.feasible";
samou440a92f2023-09-06 06:24:52 +0000100const char kCDTProperty[] = "ro.boot.cdt_hwid";
George Leea1bea3e2022-10-24 19:21:53 -0700101const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$");
102
samou440a92f2023-09-06 06:24:52 +0000103std::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 Leea1bea3e2022-10-24 19:21:53 -0700109int main(int /*argc*/, char ** /*argv*/) {
samou440a92f2023-09-06 06:24:52 +0000110 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
George Leea1bea3e2022-10-24 19:21:53 -0700111 auto batteryMitigationStartTime = std::chrono::system_clock::now();
Xiang Wangf060fe32023-02-21 15:14:42 -0800112 ABinderProcess_setThreadPoolMaxThreadCount(1);
113 ABinderProcess_startThreadPool();
samou440a92f2023-09-06 06:24:52 +0000114 bmSp = new BatteryMitigation(cfg, eventThreadCfg);
George Leea1bea3e2022-10-24 19:21:53 -0700115 if (!bmSp) {
116 return 0;
117 }
samou440a92f2023-09-06 06:24:52 +0000118 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 Leea1bea3e2022-10-24 19:21:53 -0700123 bool mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime,
124 cfg.LogFilePath,
125 cfg.TimestampFormat,
126 kTimestampRegex);
George Leefd2395f2022-12-02 00:09:30 +0000127 std::string reason = android::base::GetProperty(kBRRequestedProperty, "");
128 if (!reason.empty() && mitigationLogTimeValid) {
George Leea1bea3e2022-10-24 19:21:53 -0700129 std::ifstream src(cfg.LogFilePath, std::ios::in);
130 std::ofstream dst(kLastMealPath, std::ios::out);
131 dst << src.rdbuf();
George Lee930ea072023-01-27 15:34:38 -0800132 android::base::SetProperty(kLastMealProperty, "1");
George Leea1bea3e2022-10-24 19:21:53 -0700133 }
134 bool isBatteryMitigationReady = false;
135 std::string ready_str;
136 int val = 0;
George Lee6c25d5a2023-03-24 17:19:14 -0700137 for (int i = 0; i < COUNT_LIMIT; i++) {
George Leea1bea3e2022-10-24 19:21:53 -0700138 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 Lee6c25d5a2023-03-24 17:19:14 -0700147 break;
George Leea1bea3e2022-10-24 19:21:53 -0700148 }
149 }
George Lee6c25d5a2023-03-24 17:19:14 -0700150 if (isBatteryMitigationReady) {
151 android::base::SetProperty(kReadyProperty, "1");
152 }
George Leea1bea3e2022-10-24 19:21:53 -0700153 while (true) {
154 pause();
155 }
156 return 0;
157}