Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "lowmemorykiller" |
| 18 | |
| 19 | #include <errno.h> |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Suren Baghdasaryan | bb7747b | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Mark Salyzyn | a1f5b86 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 22 | #include <sched.h> |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Mark Salyzyn | eb06274 | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 27 | #include <sys/epoll.h> |
| 28 | #include <sys/eventfd.h> |
Colin Cross | c405900 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 29 | #include <sys/mman.h> |
Josh Gao | 84623be | 2021-03-18 17:16:08 -0700 | [diff] [blame] | 30 | #include <sys/pidfd.h> |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 32 | #include <sys/syscall.h> |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 33 | #include <sys/sysinfo.h> |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 34 | #include <time.h> |
Mark Salyzyn | eb06274 | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 38 | #include <array> |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 39 | #include <shared_mutex> |
| 40 | |
Robert Benea | 57397dc | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 41 | #include <cutils/properties.h> |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 42 | #include <cutils/sockets.h> |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 43 | #include <liblmkd_utils.h> |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 44 | #include <lmkd.h> |
Mark Salyzyn | 6a63fde | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 45 | #include <log/log.h> |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 46 | #include <log/log_event_list.h> |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 47 | #include <log/log_time.h> |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 49 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 50 | #include <psi/psi.h> |
Mark Salyzyn | eb06274 | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 51 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 52 | #include "reaper.h" |
Yao Chen | 337606c | 2018-05-02 11:19:27 -0700 | [diff] [blame] | 53 | #include "statslog.h" |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 54 | #include "watchdog.h" |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 55 | |
Suren Baghdasaryan | 940e7cf | 2021-05-27 18:15:44 -0700 | [diff] [blame] | 56 | #define BPF_FD_JUST_USE_INT |
| 57 | #include "BpfSyscallWrappers.h" |
| 58 | |
Suren Baghdasaryan | 03e1987 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces |
| 61 | * to profile and correlate with OOM kills |
| 62 | */ |
| 63 | #ifdef LMKD_TRACE_KILLS |
| 64 | |
| 65 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
| 66 | #include <cutils/trace.h> |
| 67 | |
Suren Baghdasaryan | 34928bb | 2021-07-29 17:02:51 -0700 | [diff] [blame] | 68 | static inline void trace_kill_start(int pid, const char *desc) { |
| 69 | ATRACE_INT("kill_one_process", pid); |
| 70 | ATRACE_BEGIN(desc); |
| 71 | } |
| 72 | |
| 73 | static inline void trace_kill_end() { |
| 74 | ATRACE_END(); |
| 75 | ATRACE_INT("kill_one_process", 0); |
| 76 | } |
Suren Baghdasaryan | 03e1987 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 77 | |
| 78 | #else /* LMKD_TRACE_KILLS */ |
| 79 | |
Suren Baghdasaryan | 34928bb | 2021-07-29 17:02:51 -0700 | [diff] [blame] | 80 | static inline void trace_kill_start(int, const char *) {} |
| 81 | static inline void trace_kill_end() {} |
Suren Baghdasaryan | 03e1987 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 82 | |
| 83 | #endif /* LMKD_TRACE_KILLS */ |
| 84 | |
Mark Salyzyn | eb06274 | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 85 | #ifndef __unused |
| 86 | #define __unused __attribute__((__unused__)) |
| 87 | #endif |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 88 | |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 89 | #define ZONEINFO_PATH "/proc/zoneinfo" |
| 90 | #define MEMINFO_PATH "/proc/meminfo" |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 91 | #define VMSTAT_PATH "/proc/vmstat" |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 92 | #define PROC_STATUS_TGID_FIELD "Tgid:" |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 93 | #define PROC_STATUS_RSS_FIELD "VmRSS:" |
| 94 | #define PROC_STATUS_SWAP_FIELD "VmSwap:" |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 95 | #define LINE_MAX 128 |
| 96 | |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 97 | #define PERCEPTIBLE_APP_ADJ 200 |
| 98 | |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 99 | /* Android Logger event logtags (see event.logtags) */ |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 100 | #define KILLINFO_LOG_TAG 10195355 |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 101 | |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 102 | /* gid containing AID_SYSTEM required */ |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 103 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 104 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 105 | |
Robert Benea | 58d6a13 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 106 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 107 | |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 108 | #define TARGET_UPDATE_MIN_INTERVAL_MS 1000 |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 109 | #define THRASHING_RESET_INTERVAL_MS 1000 |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 110 | |
| 111 | #define NS_PER_MS (NS_PER_SEC / MS_PER_SEC) |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 112 | #define US_PER_MS (US_PER_SEC / MS_PER_SEC) |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 113 | |
Suren Baghdasaryan | bb7747b | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 114 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ |
| 115 | #define SYSTEM_ADJ (-900) |
| 116 | |
Greg Kaiser | f5b1d14 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 117 | #define STRINGIFY(x) STRINGIFY_INTERNAL(x) |
| 118 | #define STRINGIFY_INTERNAL(x) #x |
| 119 | |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 120 | /* |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 121 | * Read lmk property with persist.device_config.lmkd_native.<name> overriding ro.lmk.<name> |
| 122 | * persist.device_config.lmkd_native.* properties are being set by experiments. If a new property |
| 123 | * can be controlled by an experiment then use GET_LMK_PROPERTY instead of property_get_xxx and |
| 124 | * add "on property" triggers in lmkd.rc to react to the experiment flag changes. |
| 125 | */ |
| 126 | #define GET_LMK_PROPERTY(type, name, def) \ |
| 127 | property_get_##type("persist.device_config.lmkd_native." name, \ |
| 128 | property_get_##type("ro.lmk." name, def)) |
| 129 | |
| 130 | /* |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 131 | * PSI monitor tracking window size. |
| 132 | * PSI monitor generates events at most once per window, |
| 133 | * therefore we poll memory state for the duration of |
| 134 | * PSI_WINDOW_SIZE_MS after the event happens. |
| 135 | */ |
| 136 | #define PSI_WINDOW_SIZE_MS 1000 |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 137 | /* Polling period after PSI signal when pressure is high */ |
| 138 | #define PSI_POLL_PERIOD_SHORT_MS 10 |
| 139 | /* Polling period after PSI signal when pressure is low */ |
| 140 | #define PSI_POLL_PERIOD_LONG_MS 100 |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 141 | |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 142 | #define FAIL_REPORT_RLIMIT_MS 1000 |
| 143 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 144 | /* |
| 145 | * System property defaults |
| 146 | */ |
| 147 | /* ro.lmk.swap_free_low_percentage property defaults */ |
Suren Baghdasaryan | fb1f592 | 2020-05-19 13:07:23 -0700 | [diff] [blame] | 148 | #define DEF_LOW_SWAP 10 |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 149 | /* ro.lmk.thrashing_limit property defaults */ |
| 150 | #define DEF_THRASHING_LOWRAM 30 |
| 151 | #define DEF_THRASHING 100 |
| 152 | /* ro.lmk.thrashing_limit_decay property defaults */ |
| 153 | #define DEF_THRASHING_DECAY_LOWRAM 50 |
| 154 | #define DEF_THRASHING_DECAY 10 |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 155 | /* ro.lmk.psi_partial_stall_ms property defaults */ |
| 156 | #define DEF_PARTIAL_STALL_LOWRAM 200 |
| 157 | #define DEF_PARTIAL_STALL 70 |
| 158 | /* ro.lmk.psi_complete_stall_ms property defaults */ |
| 159 | #define DEF_COMPLETE_STALL 700 |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 160 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 161 | #define LMKD_REINIT_PROP "lmkd.reinit" |
| 162 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 163 | #define WATCHDOG_TIMEOUT_SEC 2 |
| 164 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 165 | /* default to old in-kernel interface if no memory pressure events */ |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 166 | static bool use_inkernel_interface = true; |
Robert Benea | 7878c9b | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 167 | static bool has_inkernel_module; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 168 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 169 | /* memory pressure levels */ |
| 170 | enum vmpressure_level { |
| 171 | VMPRESS_LEVEL_LOW = 0, |
| 172 | VMPRESS_LEVEL_MEDIUM, |
| 173 | VMPRESS_LEVEL_CRITICAL, |
| 174 | VMPRESS_LEVEL_COUNT |
| 175 | }; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 176 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 177 | static const char *level_name[] = { |
| 178 | "low", |
| 179 | "medium", |
| 180 | "critical" |
| 181 | }; |
| 182 | |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 183 | struct { |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 184 | int64_t min_nr_free_pages; /* recorded but not used yet */ |
| 185 | int64_t max_nr_free_pages; |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 186 | } low_pressure_mem = { -1, -1 }; |
| 187 | |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 188 | struct psi_threshold { |
| 189 | enum psi_stall_type stall_type; |
| 190 | int threshold_ms; |
| 191 | }; |
| 192 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 193 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
Suren Baghdasaryan | 3e1a849 | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 194 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 195 | static bool pidfd_supported; |
| 196 | static int last_kill_pid_or_fd = -1; |
| 197 | static struct timespec last_kill_tm; |
| 198 | |
| 199 | /* lmkd configurable parameters */ |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 200 | static bool debug_process_killing; |
| 201 | static bool enable_pressure_upgrade; |
| 202 | static int64_t upgrade_pressure; |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 203 | static int64_t downgrade_pressure; |
Suren Baghdasaryan | d1d59f8 | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 204 | static bool low_ram_device; |
Suren Baghdasaryan | eb7c549 | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 205 | static bool kill_heaviest_task; |
Suren Baghdasaryan | 30854e7 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 206 | static unsigned long kill_timeout_ms; |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 207 | static bool use_minfree_levels; |
Suren Baghdasaryan | 8389fdb | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 208 | static bool per_app_memcg; |
Vic Yang | 6568069 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 209 | static int swap_free_low_percentage; |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 210 | static int psi_partial_stall_ms; |
| 211 | static int psi_complete_stall_ms; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 212 | static int thrashing_limit_pct; |
| 213 | static int thrashing_limit_decay_pct; |
Suren Baghdasaryan | 0142b3c | 2021-03-03 11:11:09 -0800 | [diff] [blame] | 214 | static int thrashing_critical_pct; |
Suren Baghdasaryan | 51ee4c5 | 2020-04-21 12:27:01 -0700 | [diff] [blame] | 215 | static int swap_util_max; |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 216 | static int64_t filecache_min_kb; |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 217 | static int64_t stall_limit_critical; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 218 | static bool use_psi_monitors = false; |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 219 | static int kpoll_fd; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 220 | static struct psi_threshold psi_thresholds[VMPRESS_LEVEL_COUNT] = { |
| 221 | { PSI_SOME, 70 }, /* 70ms out of 1sec for partial stall */ |
| 222 | { PSI_SOME, 100 }, /* 100ms out of 1sec for partial stall */ |
| 223 | { PSI_FULL, 70 }, /* 70ms out of 1sec for complete stall */ |
| 224 | }; |
Robert Benea | 57397dc | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 225 | |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 226 | static android_log_context ctx; |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 227 | static Reaper reaper; |
| 228 | static int reaper_comm_fd[2]; |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 229 | |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 230 | enum polling_update { |
| 231 | POLLING_DO_NOT_CHANGE, |
| 232 | POLLING_START, |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 233 | POLLING_PAUSE, |
| 234 | POLLING_RESUME, |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Data used for periodic polling for the memory state of the device. |
| 239 | * Note that when system is not polling poll_handler is set to NULL, |
| 240 | * when polling starts poll_handler gets set and is reset back to |
| 241 | * NULL when polling stops. |
| 242 | */ |
| 243 | struct polling_params { |
| 244 | struct event_handler_info* poll_handler; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 245 | struct event_handler_info* paused_handler; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 246 | struct timespec poll_start_tm; |
| 247 | struct timespec last_poll_tm; |
| 248 | int polling_interval_ms; |
| 249 | enum polling_update update; |
| 250 | }; |
| 251 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 252 | /* data required to handle events */ |
| 253 | struct event_handler_info { |
| 254 | int data; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 255 | void (*handler)(int data, uint32_t events, struct polling_params *poll_params); |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 256 | }; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 257 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 258 | /* data required to handle socket events */ |
| 259 | struct sock_event_handler_info { |
| 260 | int sock; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 261 | pid_t pid; |
Suren Baghdasaryan | 36baf44 | 2019-12-23 11:37:34 -0800 | [diff] [blame] | 262 | uint32_t async_event_mask; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 263 | struct event_handler_info handler_info; |
| 264 | }; |
| 265 | |
Suren Baghdasaryan | f2cbefd | 2019-10-21 17:59:22 -0700 | [diff] [blame] | 266 | /* max supported number of data connections (AMS, init, tests) */ |
| 267 | #define MAX_DATA_CONN 3 |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 268 | |
| 269 | /* socket event handler data */ |
| 270 | static struct sock_event_handler_info ctrl_sock; |
| 271 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; |
| 272 | |
| 273 | /* vmpressure event handler data */ |
| 274 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; |
| 275 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 276 | /* |
Suren Baghdasaryan | f2cbefd | 2019-10-21 17:59:22 -0700 | [diff] [blame] | 277 | * 1 ctrl listen socket, 3 ctrl data socket, 3 memory pressure levels, |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 278 | * 1 lmk events + 1 fd to wait for process death + 1 fd to receive kill failure notifications |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 279 | */ |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 280 | #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT + 1 + 1 + 1) |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 281 | static int epollfd; |
| 282 | static int maxevents; |
| 283 | |
Chong Zhang | 1cd12b5 | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 284 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | a08c172 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 285 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 286 | #define OOM_SCORE_ADJ_MAX 1000 |
| 287 | |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 288 | static std::array<int, MAX_TARGETS> lowmem_adj; |
| 289 | static std::array<int, MAX_TARGETS> lowmem_minfree; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 290 | static int lowmem_targets_size; |
| 291 | |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 292 | /* Fields to parse in /proc/zoneinfo */ |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 293 | /* zoneinfo per-zone fields */ |
| 294 | enum zoneinfo_zone_field { |
| 295 | ZI_ZONE_NR_FREE_PAGES = 0, |
| 296 | ZI_ZONE_MIN, |
| 297 | ZI_ZONE_LOW, |
| 298 | ZI_ZONE_HIGH, |
| 299 | ZI_ZONE_PRESENT, |
| 300 | ZI_ZONE_NR_FREE_CMA, |
| 301 | ZI_ZONE_FIELD_COUNT |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 304 | static const char* const zoneinfo_zone_field_names[ZI_ZONE_FIELD_COUNT] = { |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 305 | "nr_free_pages", |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 306 | "min", |
| 307 | "low", |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 308 | "high", |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 309 | "present", |
| 310 | "nr_free_cma", |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 313 | /* zoneinfo per-zone special fields */ |
| 314 | enum zoneinfo_zone_spec_field { |
| 315 | ZI_ZONE_SPEC_PROTECTION = 0, |
| 316 | ZI_ZONE_SPEC_PAGESETS, |
| 317 | ZI_ZONE_SPEC_FIELD_COUNT, |
| 318 | }; |
| 319 | |
| 320 | static const char* const zoneinfo_zone_spec_field_names[ZI_ZONE_SPEC_FIELD_COUNT] = { |
| 321 | "protection:", |
| 322 | "pagesets", |
| 323 | }; |
| 324 | |
| 325 | /* see __MAX_NR_ZONES definition in kernel mmzone.h */ |
| 326 | #define MAX_NR_ZONES 6 |
| 327 | |
| 328 | union zoneinfo_zone_fields { |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 329 | struct { |
| 330 | int64_t nr_free_pages; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 331 | int64_t min; |
| 332 | int64_t low; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 333 | int64_t high; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 334 | int64_t present; |
| 335 | int64_t nr_free_cma; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 336 | } field; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 337 | int64_t arr[ZI_ZONE_FIELD_COUNT]; |
| 338 | }; |
| 339 | |
| 340 | struct zoneinfo_zone { |
| 341 | union zoneinfo_zone_fields fields; |
| 342 | int64_t protection[MAX_NR_ZONES]; |
| 343 | int64_t max_protection; |
| 344 | }; |
| 345 | |
| 346 | /* zoneinfo per-node fields */ |
| 347 | enum zoneinfo_node_field { |
| 348 | ZI_NODE_NR_INACTIVE_FILE = 0, |
| 349 | ZI_NODE_NR_ACTIVE_FILE, |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 350 | ZI_NODE_FIELD_COUNT |
| 351 | }; |
| 352 | |
| 353 | static const char* const zoneinfo_node_field_names[ZI_NODE_FIELD_COUNT] = { |
| 354 | "nr_inactive_file", |
| 355 | "nr_active_file", |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | union zoneinfo_node_fields { |
| 359 | struct { |
| 360 | int64_t nr_inactive_file; |
| 361 | int64_t nr_active_file; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 362 | } field; |
| 363 | int64_t arr[ZI_NODE_FIELD_COUNT]; |
| 364 | }; |
| 365 | |
| 366 | struct zoneinfo_node { |
| 367 | int id; |
| 368 | int zone_count; |
| 369 | struct zoneinfo_zone zones[MAX_NR_ZONES]; |
| 370 | union zoneinfo_node_fields fields; |
| 371 | }; |
| 372 | |
| 373 | /* for now two memory nodes is more than enough */ |
| 374 | #define MAX_NR_NODES 2 |
| 375 | |
| 376 | struct zoneinfo { |
| 377 | int node_count; |
| 378 | struct zoneinfo_node nodes[MAX_NR_NODES]; |
| 379 | int64_t totalreserve_pages; |
| 380 | int64_t total_inactive_file; |
| 381 | int64_t total_active_file; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | /* Fields to parse in /proc/meminfo */ |
| 385 | enum meminfo_field { |
| 386 | MI_NR_FREE_PAGES = 0, |
| 387 | MI_CACHED, |
| 388 | MI_SWAP_CACHED, |
| 389 | MI_BUFFERS, |
| 390 | MI_SHMEM, |
| 391 | MI_UNEVICTABLE, |
Vic Yang | 6568069 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 392 | MI_TOTAL_SWAP, |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 393 | MI_FREE_SWAP, |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 394 | MI_ACTIVE_ANON, |
| 395 | MI_INACTIVE_ANON, |
| 396 | MI_ACTIVE_FILE, |
| 397 | MI_INACTIVE_FILE, |
| 398 | MI_SRECLAIMABLE, |
| 399 | MI_SUNRECLAIM, |
| 400 | MI_KERNEL_STACK, |
| 401 | MI_PAGE_TABLES, |
| 402 | MI_ION_HELP, |
| 403 | MI_ION_HELP_POOL, |
| 404 | MI_CMA_FREE, |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 405 | MI_FIELD_COUNT |
| 406 | }; |
| 407 | |
| 408 | static const char* const meminfo_field_names[MI_FIELD_COUNT] = { |
| 409 | "MemFree:", |
| 410 | "Cached:", |
| 411 | "SwapCached:", |
| 412 | "Buffers:", |
| 413 | "Shmem:", |
| 414 | "Unevictable:", |
Vic Yang | 6568069 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 415 | "SwapTotal:", |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 416 | "SwapFree:", |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 417 | "Active(anon):", |
| 418 | "Inactive(anon):", |
| 419 | "Active(file):", |
| 420 | "Inactive(file):", |
| 421 | "SReclaimable:", |
| 422 | "SUnreclaim:", |
| 423 | "KernelStack:", |
| 424 | "PageTables:", |
| 425 | "ION_heap:", |
| 426 | "ION_heap_pool:", |
| 427 | "CmaFree:", |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | union meminfo { |
| 431 | struct { |
| 432 | int64_t nr_free_pages; |
| 433 | int64_t cached; |
| 434 | int64_t swap_cached; |
| 435 | int64_t buffers; |
| 436 | int64_t shmem; |
| 437 | int64_t unevictable; |
Vic Yang | 6568069 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 438 | int64_t total_swap; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 439 | int64_t free_swap; |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 440 | int64_t active_anon; |
| 441 | int64_t inactive_anon; |
| 442 | int64_t active_file; |
| 443 | int64_t inactive_file; |
| 444 | int64_t sreclaimable; |
| 445 | int64_t sunreclaimable; |
| 446 | int64_t kernel_stack; |
| 447 | int64_t page_tables; |
| 448 | int64_t ion_heap; |
| 449 | int64_t ion_heap_pool; |
| 450 | int64_t cma_free; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 451 | /* fields below are calculated rather than read from the file */ |
| 452 | int64_t nr_file_pages; |
Suren Baghdasaryan | 940e7cf | 2021-05-27 18:15:44 -0700 | [diff] [blame] | 453 | int64_t total_gpu_kb; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 454 | } field; |
| 455 | int64_t arr[MI_FIELD_COUNT]; |
| 456 | }; |
| 457 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 458 | /* Fields to parse in /proc/vmstat */ |
| 459 | enum vmstat_field { |
| 460 | VS_FREE_PAGES, |
| 461 | VS_INACTIVE_FILE, |
| 462 | VS_ACTIVE_FILE, |
| 463 | VS_WORKINGSET_REFAULT, |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 464 | VS_WORKINGSET_REFAULT_FILE, |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 465 | VS_PGSCAN_KSWAPD, |
| 466 | VS_PGSCAN_DIRECT, |
| 467 | VS_PGSCAN_DIRECT_THROTTLE, |
| 468 | VS_FIELD_COUNT |
| 469 | }; |
| 470 | |
| 471 | static const char* const vmstat_field_names[MI_FIELD_COUNT] = { |
| 472 | "nr_free_pages", |
| 473 | "nr_inactive_file", |
| 474 | "nr_active_file", |
| 475 | "workingset_refault", |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 476 | "workingset_refault_file", |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 477 | "pgscan_kswapd", |
| 478 | "pgscan_direct", |
| 479 | "pgscan_direct_throttle", |
| 480 | }; |
| 481 | |
| 482 | union vmstat { |
| 483 | struct { |
| 484 | int64_t nr_free_pages; |
| 485 | int64_t nr_inactive_file; |
| 486 | int64_t nr_active_file; |
| 487 | int64_t workingset_refault; |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 488 | int64_t workingset_refault_file; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 489 | int64_t pgscan_kswapd; |
| 490 | int64_t pgscan_direct; |
| 491 | int64_t pgscan_direct_throttle; |
| 492 | } field; |
| 493 | int64_t arr[VS_FIELD_COUNT]; |
| 494 | }; |
| 495 | |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 496 | enum field_match_result { |
| 497 | NO_MATCH, |
| 498 | PARSE_FAIL, |
| 499 | PARSE_SUCCESS |
| 500 | }; |
| 501 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 502 | struct adjslot_list { |
| 503 | struct adjslot_list *next; |
| 504 | struct adjslot_list *prev; |
| 505 | }; |
| 506 | |
| 507 | struct proc { |
| 508 | struct adjslot_list asl; |
| 509 | int pid; |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 510 | int pidfd; |
Colin Cross | 748d218 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 511 | uid_t uid; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 512 | int oomadj; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 513 | pid_t reg_pid; /* PID of the process that registered this record */ |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 514 | bool valid; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 515 | struct proc *pidhash_next; |
| 516 | }; |
| 517 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 518 | struct reread_data { |
| 519 | const char* const filename; |
| 520 | int fd; |
| 521 | }; |
| 522 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 523 | #define PIDHASH_SZ 1024 |
| 524 | static struct proc *pidhash[PIDHASH_SZ]; |
| 525 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 526 | |
Chih-Hung Hsieh | eefa246 | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 527 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 528 | #define ADJTOSLOT_COUNT (ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1) |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 529 | |
| 530 | // protects procadjslot_list from concurrent access |
| 531 | static std::shared_mutex adjslot_list_lock; |
| 532 | // procadjslot_list should be modified only from the main thread while exclusively holding |
| 533 | // adjslot_list_lock. Readers from non-main threads should hold adjslot_list_lock shared lock. |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 534 | static struct adjslot_list procadjslot_list[ADJTOSLOT_COUNT]; |
| 535 | |
| 536 | #define MAX_DISTINCT_OOM_ADJ 32 |
| 537 | #define KILLCNT_INVALID_IDX 0xFF |
| 538 | /* |
| 539 | * Because killcnt array is sparse a two-level indirection is used |
| 540 | * to keep the size small. killcnt_idx stores index of the element in |
| 541 | * killcnt array. Index KILLCNT_INVALID_IDX indicates an unused slot. |
| 542 | */ |
| 543 | static uint8_t killcnt_idx[ADJTOSLOT_COUNT]; |
| 544 | static uint16_t killcnt[MAX_DISTINCT_OOM_ADJ]; |
| 545 | static int killcnt_free_idx = 0; |
| 546 | static uint32_t killcnt_total = 0; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 547 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 548 | /* PAGE_SIZE / 1024 */ |
| 549 | static long page_k; |
| 550 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 551 | static void update_props(); |
| 552 | static bool init_monitors(); |
| 553 | static void destroy_monitors(); |
| 554 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 555 | static int clamp(int low, int high, int value) { |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 556 | return std::max(std::min(value, high), low); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 559 | static bool parse_int64(const char* str, int64_t* ret) { |
| 560 | char* endptr; |
| 561 | long long val = strtoll(str, &endptr, 10); |
| 562 | if (str == endptr || val > INT64_MAX) { |
| 563 | return false; |
| 564 | } |
| 565 | *ret = (int64_t)val; |
| 566 | return true; |
| 567 | } |
| 568 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 569 | static int find_field(const char* name, const char* const field_names[], int field_count) { |
| 570 | for (int i = 0; i < field_count; i++) { |
| 571 | if (!strcmp(name, field_names[i])) { |
| 572 | return i; |
| 573 | } |
| 574 | } |
| 575 | return -1; |
| 576 | } |
| 577 | |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 578 | static enum field_match_result match_field(const char* cp, const char* ap, |
| 579 | const char* const field_names[], |
| 580 | int field_count, int64_t* field, |
| 581 | int *field_idx) { |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 582 | int i = find_field(cp, field_names, field_count); |
| 583 | if (i < 0) { |
| 584 | return NO_MATCH; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 585 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 586 | *field_idx = i; |
| 587 | return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 590 | /* |
| 591 | * Read file content from the beginning up to max_len bytes or EOF |
| 592 | * whichever happens first. |
| 593 | */ |
Colin Cross | dba1cc6 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 594 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 595 | { |
| 596 | ssize_t ret = 0; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 597 | off_t offset = 0; |
Colin Cross | dba1cc6 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 598 | |
| 599 | while (max_len > 0) { |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 600 | ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset)); |
Colin Cross | dba1cc6 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 601 | if (r == 0) { |
| 602 | break; |
| 603 | } |
| 604 | if (r == -1) { |
| 605 | return -1; |
| 606 | } |
| 607 | ret += r; |
| 608 | buf += r; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 609 | offset += r; |
Colin Cross | dba1cc6 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 610 | max_len -= r; |
| 611 | } |
| 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Read a new or already opened file from the beginning. |
| 618 | * If the file has not been opened yet data->fd should be set to -1. |
| 619 | * To be used with files which are read often and possibly during high |
| 620 | * memory pressure to minimize file opening which by itself requires kernel |
| 621 | * memory allocation and might result in a stall on memory stressed system. |
| 622 | */ |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 623 | static char *reread_file(struct reread_data *data) { |
| 624 | /* start with page-size buffer and increase if needed */ |
| 625 | static ssize_t buf_size = PAGE_SIZE; |
| 626 | static char *new_buf, *buf = NULL; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 627 | ssize_t size; |
| 628 | |
| 629 | if (data->fd == -1) { |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 630 | /* First-time buffer initialization */ |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 631 | if (!buf && (buf = static_cast<char*>(malloc(buf_size))) == nullptr) { |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | data->fd = TEMP_FAILURE_RETRY(open(data->filename, O_RDONLY | O_CLOEXEC)); |
| 636 | if (data->fd < 0) { |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 637 | ALOGE("%s open: %s", data->filename, strerror(errno)); |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 638 | return NULL; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 642 | while (true) { |
| 643 | size = read_all(data->fd, buf, buf_size - 1); |
| 644 | if (size < 0) { |
| 645 | ALOGE("%s read: %s", data->filename, strerror(errno)); |
| 646 | close(data->fd); |
| 647 | data->fd = -1; |
| 648 | return NULL; |
| 649 | } |
| 650 | if (size < buf_size - 1) { |
| 651 | break; |
| 652 | } |
| 653 | /* |
| 654 | * Since we are reading /proc files we can't use fstat to find out |
| 655 | * the real size of the file. Double the buffer size and keep retrying. |
| 656 | */ |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 657 | if ((new_buf = static_cast<char*>(realloc(buf, buf_size * 2))) == nullptr) { |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 658 | errno = ENOMEM; |
| 659 | return NULL; |
| 660 | } |
| 661 | buf = new_buf; |
| 662 | buf_size *= 2; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 663 | } |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 664 | buf[size] = 0; |
| 665 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 666 | return buf; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 669 | static bool claim_record(struct proc* procp, pid_t pid) { |
| 670 | if (procp->reg_pid == pid) { |
| 671 | /* Record already belongs to the registrant */ |
| 672 | return true; |
| 673 | } |
| 674 | if (procp->reg_pid == 0) { |
| 675 | /* Old registrant is gone, claim the record */ |
| 676 | procp->reg_pid = pid; |
| 677 | return true; |
| 678 | } |
| 679 | /* The record is owned by another registrant */ |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | static void remove_claims(pid_t pid) { |
| 684 | int i; |
| 685 | |
| 686 | for (i = 0; i < PIDHASH_SZ; i++) { |
| 687 | struct proc* procp = pidhash[i]; |
| 688 | while (procp) { |
| 689 | if (procp->reg_pid == pid) { |
| 690 | procp->reg_pid = 0; |
| 691 | } |
| 692 | procp = procp->pidhash_next; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | static void ctrl_data_close(int dsock_idx) { |
| 698 | struct epoll_event epev; |
| 699 | |
| 700 | ALOGI("closing lmkd data connection"); |
| 701 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { |
| 702 | // Log a warning and keep going |
| 703 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 704 | } |
| 705 | maxevents--; |
| 706 | |
| 707 | close(data_sock[dsock_idx].sock); |
| 708 | data_sock[dsock_idx].sock = -1; |
| 709 | |
| 710 | /* Mark all records of the old registrant as unclaimed */ |
| 711 | remove_claims(data_sock[dsock_idx].pid); |
| 712 | } |
| 713 | |
| 714 | static ssize_t ctrl_data_read(int dsock_idx, char* buf, size_t bufsz, struct ucred* sender_cred) { |
| 715 | struct iovec iov = {buf, bufsz}; |
| 716 | char control[CMSG_SPACE(sizeof(struct ucred))]; |
| 717 | struct msghdr hdr = { |
| 718 | NULL, 0, &iov, 1, control, sizeof(control), 0, |
| 719 | }; |
| 720 | ssize_t ret; |
| 721 | ret = TEMP_FAILURE_RETRY(recvmsg(data_sock[dsock_idx].sock, &hdr, 0)); |
| 722 | if (ret == -1) { |
| 723 | ALOGE("control data socket read failed; %s", strerror(errno)); |
| 724 | return -1; |
| 725 | } |
| 726 | if (ret == 0) { |
| 727 | ALOGE("Got EOF on control data socket"); |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | struct ucred* cred = NULL; |
| 732 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr); |
| 733 | while (cmsg != NULL) { |
| 734 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 735 | cred = (struct ucred*)CMSG_DATA(cmsg); |
| 736 | break; |
| 737 | } |
| 738 | cmsg = CMSG_NXTHDR(&hdr, cmsg); |
| 739 | } |
| 740 | |
| 741 | if (cred == NULL) { |
| 742 | ALOGE("Failed to retrieve sender credentials"); |
| 743 | /* Close the connection */ |
| 744 | ctrl_data_close(dsock_idx); |
| 745 | return -1; |
| 746 | } |
| 747 | |
| 748 | memcpy(sender_cred, cred, sizeof(struct ucred)); |
| 749 | |
| 750 | /* Store PID of the peer */ |
| 751 | data_sock[dsock_idx].pid = cred->pid; |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | static int ctrl_data_write(int dsock_idx, char* buf, size_t bufsz) { |
| 757 | int ret = 0; |
| 758 | |
| 759 | ret = TEMP_FAILURE_RETRY(write(data_sock[dsock_idx].sock, buf, bufsz)); |
| 760 | |
| 761 | if (ret == -1) { |
| 762 | ALOGE("control data socket write failed; errno=%d", errno); |
| 763 | } else if (ret == 0) { |
| 764 | ALOGE("Got EOF on control data socket"); |
| 765 | ret = -1; |
| 766 | } |
| 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Write the pid/uid pair over the data socket, note: all active clients |
| 773 | * will receive this unsolicited notification. |
| 774 | */ |
| 775 | static void ctrl_data_write_lmk_kill_occurred(pid_t pid, uid_t uid) { |
| 776 | LMKD_CTRL_PACKET packet; |
| 777 | size_t len = lmkd_pack_set_prockills(packet, pid, uid); |
| 778 | |
| 779 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
Suren Baghdasaryan | 36baf44 | 2019-12-23 11:37:34 -0800 | [diff] [blame] | 780 | if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_KILL) { |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 781 | ctrl_data_write(i, (char*)packet, len); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Vova Sharaienko | a92b76b | 2021-04-24 00:30:06 +0000 | [diff] [blame] | 786 | /* |
| 787 | * Write the kill_stat/memory_stat over the data socket to be propagated via AMS to statsd |
| 788 | */ |
| 789 | static void stats_write_lmk_kill_occurred(struct kill_stat *kill_st, |
| 790 | struct memory_stat *mem_st) { |
| 791 | LMK_KILL_OCCURRED_PACKET packet; |
| 792 | const size_t len = lmkd_pack_set_kill_occurred(packet, kill_st, mem_st); |
| 793 | if (len == 0) { |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 798 | if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_STAT) { |
| 799 | ctrl_data_write(i, packet, len); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | } |
| 804 | |
| 805 | static void stats_write_lmk_kill_occurred_pid(int pid, struct kill_stat *kill_st, |
| 806 | struct memory_stat *mem_st) { |
| 807 | kill_st->taskname = stats_get_task_name(pid); |
| 808 | if (kill_st->taskname != NULL) { |
| 809 | stats_write_lmk_kill_occurred(kill_st, mem_st); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Write the state_changed over the data socket to be propagated via AMS to statsd |
| 815 | */ |
| 816 | static void stats_write_lmk_state_changed(enum lmk_state state) { |
| 817 | LMKD_CTRL_PACKET packet_state_changed; |
| 818 | const size_t len = lmkd_pack_set_state_changed(packet_state_changed, state); |
| 819 | if (len == 0) { |
| 820 | return; |
| 821 | } |
| 822 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 823 | if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_STAT) { |
| 824 | ctrl_data_write(i, (char*)packet_state_changed, len); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 829 | static void poll_kernel(int poll_fd) { |
| 830 | if (poll_fd == -1) { |
| 831 | // not waiting |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | while (1) { |
| 836 | char rd_buf[256]; |
Bart Van Assche | 5ebc4e8 | 2022-02-03 00:05:19 +0000 | [diff] [blame] | 837 | int bytes_read = TEMP_FAILURE_RETRY(pread(poll_fd, (void*)rd_buf, sizeof(rd_buf) - 1, 0)); |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 838 | if (bytes_read <= 0) break; |
| 839 | rd_buf[bytes_read] = '\0'; |
| 840 | |
| 841 | int64_t pid; |
| 842 | int64_t uid; |
| 843 | int64_t group_leader_pid; |
| 844 | int64_t rss_in_pages; |
| 845 | struct memory_stat mem_st = {}; |
| 846 | int16_t oom_score_adj; |
| 847 | int16_t min_score_adj; |
| 848 | int64_t starttime; |
| 849 | char* taskname = 0; |
| 850 | |
| 851 | int fields_read = |
| 852 | sscanf(rd_buf, |
| 853 | "%" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 |
| 854 | " %" SCNd16 " %" SCNd16 " %" SCNd64 "\n%m[^\n]", |
| 855 | &pid, &uid, &group_leader_pid, &mem_st.pgfault, &mem_st.pgmajfault, |
| 856 | &rss_in_pages, &oom_score_adj, &min_score_adj, &starttime, &taskname); |
| 857 | |
| 858 | /* only the death of the group leader process is logged */ |
| 859 | if (fields_read == 10 && group_leader_pid == pid) { |
| 860 | ctrl_data_write_lmk_kill_occurred((pid_t)pid, (uid_t)uid); |
| 861 | mem_st.process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK)); |
| 862 | mem_st.rss_in_bytes = rss_in_pages * PAGE_SIZE; |
Suren Baghdasaryan | 3cc1f13 | 2020-09-09 20:19:02 -0700 | [diff] [blame] | 863 | |
| 864 | struct kill_stat kill_st = { |
| 865 | .uid = static_cast<int32_t>(uid), |
| 866 | .kill_reason = NONE, |
| 867 | .oom_score = oom_score_adj, |
| 868 | .min_oom_score = min_score_adj, |
| 869 | .free_mem_kb = 0, |
| 870 | .free_swap_kb = 0, |
| 871 | }; |
| 872 | stats_write_lmk_kill_occurred_pid(pid, &kill_st, &mem_st); |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | free(taskname); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | static bool init_poll_kernel() { |
| 880 | kpoll_fd = TEMP_FAILURE_RETRY(open("/proc/lowmemorykiller", O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 881 | |
| 882 | if (kpoll_fd < 0) { |
| 883 | ALOGE("kernel lmk event file could not be opened; errno=%d", errno); |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | return true; |
| 888 | } |
| 889 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 890 | static struct proc *pid_lookup(int pid) { |
| 891 | struct proc *procp; |
| 892 | |
| 893 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 894 | procp = procp->pidhash_next) |
| 895 | ; |
| 896 | |
| 897 | return procp; |
| 898 | } |
| 899 | |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 900 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new_element) |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 901 | { |
| 902 | struct adjslot_list *next = head->next; |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 903 | new_element->prev = head; |
| 904 | new_element->next = next; |
| 905 | next->prev = new_element; |
| 906 | head->next = new_element; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void adjslot_remove(struct adjslot_list *old) |
| 910 | { |
| 911 | struct adjslot_list *prev = old->prev; |
| 912 | struct adjslot_list *next = old->next; |
| 913 | next->prev = prev; |
| 914 | prev->next = next; |
| 915 | } |
| 916 | |
| 917 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 918 | struct adjslot_list *asl = head->prev; |
| 919 | |
| 920 | return asl == head ? NULL : asl; |
| 921 | } |
| 922 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 923 | // Should be modified only from the main thread. |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 924 | static void proc_slot(struct proc *procp) { |
| 925 | int adjslot = ADJTOSLOT(procp->oomadj); |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 926 | std::scoped_lock lock(adjslot_list_lock); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 927 | |
| 928 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 929 | } |
| 930 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 931 | // Should be modified only from the main thread. |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 932 | static void proc_unslot(struct proc *procp) { |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 933 | std::scoped_lock lock(adjslot_list_lock); |
| 934 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 935 | adjslot_remove(&procp->asl); |
| 936 | } |
| 937 | |
| 938 | static void proc_insert(struct proc *procp) { |
| 939 | int hval = pid_hashfn(procp->pid); |
| 940 | |
| 941 | procp->pidhash_next = pidhash[hval]; |
| 942 | pidhash[hval] = procp; |
| 943 | proc_slot(procp); |
| 944 | } |
| 945 | |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 946 | // Can be called only from the main thread. |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 947 | static int pid_remove(int pid) { |
| 948 | int hval = pid_hashfn(pid); |
| 949 | struct proc *procp; |
| 950 | struct proc *prevp; |
| 951 | |
| 952 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 953 | procp = procp->pidhash_next) |
| 954 | prevp = procp; |
| 955 | |
| 956 | if (!procp) |
| 957 | return -1; |
| 958 | |
| 959 | if (!prevp) |
| 960 | pidhash[hval] = procp->pidhash_next; |
| 961 | else |
| 962 | prevp->pidhash_next = procp->pidhash_next; |
| 963 | |
| 964 | proc_unslot(procp); |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 965 | /* |
| 966 | * Close pidfd here if we are not waiting for corresponding process to die, |
| 967 | * in which case stop_wait_for_proc_kill() will close the pidfd later |
| 968 | */ |
| 969 | if (procp->pidfd >= 0 && procp->pidfd != last_kill_pid_or_fd) { |
| 970 | close(procp->pidfd); |
| 971 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 972 | free(procp); |
| 973 | return 0; |
| 974 | } |
| 975 | |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 976 | static void pid_invalidate(int pid) { |
| 977 | std::shared_lock lock(adjslot_list_lock); |
| 978 | struct proc *procp = pid_lookup(pid); |
| 979 | |
| 980 | if (procp) { |
| 981 | procp->valid = false; |
| 982 | } |
| 983 | } |
| 984 | |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 985 | /* |
| 986 | * Write a string to a file. |
| 987 | * Returns false if the file does not exist. |
| 988 | */ |
| 989 | static bool writefilestring(const char *path, const char *s, |
| 990 | bool err_if_missing) { |
Nick Kralevich | 148d8dd | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 991 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 992 | ssize_t len = strlen(s); |
| 993 | ssize_t ret; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 994 | |
| 995 | if (fd < 0) { |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 996 | if (err_if_missing) { |
| 997 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 998 | } |
| 999 | return false; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 1002 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1003 | if (ret < 0) { |
| 1004 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 1005 | } else if (ret < len) { |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 1006 | ALOGE("Short write on %s; length=%zd", path, ret); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | close(fd); |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 1010 | return true; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1013 | static inline long get_time_diff_ms(struct timespec *from, |
| 1014 | struct timespec *to) { |
| 1015 | return (to->tv_sec - from->tv_sec) * (long)MS_PER_SEC + |
| 1016 | (to->tv_nsec - from->tv_nsec) / (long)NS_PER_MS; |
| 1017 | } |
| 1018 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1019 | /* Reads /proc/pid/status into buf. */ |
| 1020 | static bool read_proc_status(int pid, char *buf, size_t buf_sz) { |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1021 | char path[PATH_MAX]; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1022 | int fd; |
| 1023 | ssize_t size; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1024 | |
| 1025 | snprintf(path, PATH_MAX, "/proc/%d/status", pid); |
| 1026 | fd = open(path, O_RDONLY | O_CLOEXEC); |
| 1027 | if (fd < 0) { |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1028 | return false; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1031 | size = read_all(fd, buf, buf_sz - 1); |
| 1032 | close(fd); |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1033 | if (size < 0) { |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1034 | return false; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1035 | } |
| 1036 | buf[size] = 0; |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1037 | return true; |
| 1038 | } |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1039 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1040 | /* Looks for tag in buf and parses the first integer */ |
| 1041 | static bool parse_status_tag(char *buf, const char *tag, int64_t *out) { |
| 1042 | char *pos = buf; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1043 | while (true) { |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1044 | pos = strstr(pos, tag); |
| 1045 | /* Stop if tag not found or found at the line beginning */ |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1046 | if (pos == NULL || pos == buf || pos[-1] == '\n') { |
| 1047 | break; |
| 1048 | } |
| 1049 | pos++; |
| 1050 | } |
| 1051 | |
| 1052 | if (pos == NULL) { |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1053 | return false; |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1056 | pos += strlen(tag); |
| 1057 | while (*pos == ' ') ++pos; |
| 1058 | return parse_int64(pos, out); |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1061 | static int proc_get_size(int pid) { |
| 1062 | char path[PATH_MAX]; |
| 1063 | char line[LINE_MAX]; |
| 1064 | int fd; |
| 1065 | int rss = 0; |
| 1066 | int total; |
| 1067 | ssize_t ret; |
| 1068 | |
| 1069 | /* gid containing AID_READPROC required */ |
| 1070 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
| 1071 | fd = open(path, O_RDONLY | O_CLOEXEC); |
| 1072 | if (fd == -1) |
| 1073 | return -1; |
| 1074 | |
| 1075 | ret = read_all(fd, line, sizeof(line) - 1); |
| 1076 | if (ret < 0) { |
| 1077 | close(fd); |
| 1078 | return -1; |
| 1079 | } |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1080 | line[ret] = '\0'; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1081 | |
| 1082 | sscanf(line, "%d %d ", &total, &rss); |
| 1083 | close(fd); |
| 1084 | return rss; |
| 1085 | } |
| 1086 | |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1087 | static char *proc_get_name(int pid, char *buf, size_t buf_size) { |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1088 | char path[PATH_MAX]; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1089 | int fd; |
| 1090 | char *cp; |
| 1091 | ssize_t ret; |
| 1092 | |
| 1093 | /* gid containing AID_READPROC required */ |
| 1094 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
| 1095 | fd = open(path, O_RDONLY | O_CLOEXEC); |
| 1096 | if (fd == -1) { |
| 1097 | return NULL; |
| 1098 | } |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1099 | ret = read_all(fd, buf, buf_size - 1); |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1100 | close(fd); |
| 1101 | if (ret < 0) { |
| 1102 | return NULL; |
| 1103 | } |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1104 | buf[ret] = '\0'; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1105 | |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1106 | cp = strchr(buf, ' '); |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1107 | if (cp) { |
| 1108 | *cp = '\0'; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1111 | return buf; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1114 | static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred *cred) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1115 | struct proc *procp; |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 1116 | char path[LINE_MAX]; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1117 | char val[20]; |
Robert Benea | 58d6a13 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1118 | int soft_limit_mult; |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1119 | struct lmk_procprio params; |
Suren Baghdasaryan | bb7747b | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 1120 | bool is_system_server; |
| 1121 | struct passwd *pwdrec; |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1122 | int64_t tgid; |
| 1123 | char buf[PAGE_SIZE]; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1124 | |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1125 | lmkd_pack_get_procprio(packet, field_count, ¶ms); |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1126 | |
| 1127 | if (params.oomadj < OOM_SCORE_ADJ_MIN || |
| 1128 | params.oomadj > OOM_SCORE_ADJ_MAX) { |
| 1129 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1133 | if (params.ptype < PROC_TYPE_FIRST || params.ptype >= PROC_TYPE_COUNT) { |
| 1134 | ALOGE("Invalid PROCPRIO process type argument %d", params.ptype); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1138 | /* Check if registered process is a thread group leader */ |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 1139 | if (read_proc_status(params.pid, buf, sizeof(buf))) { |
| 1140 | if (parse_status_tag(buf, PROC_STATUS_TGID_FIELD, &tgid) && tgid != params.pid) { |
| 1141 | ALOGE("Attempt to register a task that is not a thread group leader " |
| 1142 | "(tid %d, tgid %" PRId64 ")", params.pid, tgid); |
| 1143 | return; |
| 1144 | } |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1147 | /* gid containing AID_READPROC required */ |
| 1148 | /* CAP_SYS_RESOURCE required */ |
| 1149 | /* CAP_DAC_OVERRIDE required */ |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1150 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); |
| 1151 | snprintf(val, sizeof(val), "%d", params.oomadj); |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 1152 | if (!writefilestring(path, val, false)) { |
| 1153 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", |
| 1154 | path, errno, params.pid); |
| 1155 | /* If this file does not exist the process is dead. */ |
| 1156 | return; |
| 1157 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1158 | |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1159 | if (use_inkernel_interface) { |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 1160 | stats_store_taskname(params.pid, proc_get_name(params.pid, path, sizeof(path))); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1161 | return; |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1162 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1163 | |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1164 | /* lmkd should not change soft limits for services */ |
| 1165 | if (params.ptype == PROC_TYPE_APP && per_app_memcg) { |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1166 | if (params.oomadj >= 900) { |
| 1167 | soft_limit_mult = 0; |
| 1168 | } else if (params.oomadj >= 800) { |
| 1169 | soft_limit_mult = 0; |
| 1170 | } else if (params.oomadj >= 700) { |
| 1171 | soft_limit_mult = 0; |
| 1172 | } else if (params.oomadj >= 600) { |
| 1173 | // Launcher should be perceptible, don't kill it. |
| 1174 | params.oomadj = 200; |
| 1175 | soft_limit_mult = 1; |
| 1176 | } else if (params.oomadj >= 500) { |
| 1177 | soft_limit_mult = 0; |
| 1178 | } else if (params.oomadj >= 400) { |
| 1179 | soft_limit_mult = 0; |
| 1180 | } else if (params.oomadj >= 300) { |
| 1181 | soft_limit_mult = 1; |
| 1182 | } else if (params.oomadj >= 200) { |
Srinivas Paladugu | a453f0b | 2018-10-09 14:21:10 -0700 | [diff] [blame] | 1183 | soft_limit_mult = 8; |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1184 | } else if (params.oomadj >= 100) { |
| 1185 | soft_limit_mult = 10; |
| 1186 | } else if (params.oomadj >= 0) { |
| 1187 | soft_limit_mult = 20; |
| 1188 | } else { |
| 1189 | // Persistent processes will have a large |
| 1190 | // soft limit 512MB. |
| 1191 | soft_limit_mult = 64; |
| 1192 | } |
Robert Benea | 58d6a13 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1193 | |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 1194 | std::string path; |
| 1195 | if (!CgroupGetAttributePathForTask("MemSoftLimit", params.pid, &path)) { |
| 1196 | ALOGE("Querying MemSoftLimit path failed"); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1200 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
Suren Baghdasaryan | bf919ff | 2018-05-21 19:48:47 -0700 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * system_server process has no memcg under /dev/memcg/apps but should be |
| 1204 | * registered with lmkd. This is the best way so far to identify it. |
| 1205 | */ |
| 1206 | is_system_server = (params.oomadj == SYSTEM_ADJ && |
| 1207 | (pwdrec = getpwnam("system")) != NULL && |
| 1208 | params.uid == pwdrec->pw_uid); |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 1209 | writefilestring(path.c_str(), val, !is_system_server); |
Robert Benea | 58d6a13 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1212 | procp = pid_lookup(params.pid); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1213 | if (!procp) { |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1214 | int pidfd = -1; |
| 1215 | |
| 1216 | if (pidfd_supported) { |
Josh Gao | 84623be | 2021-03-18 17:16:08 -0700 | [diff] [blame] | 1217 | pidfd = TEMP_FAILURE_RETRY(pidfd_open(params.pid, 0)); |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1218 | if (pidfd < 0) { |
| 1219 | ALOGE("pidfd_open for pid %d failed; errno=%d", params.pid, errno); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1220 | return; |
| 1221 | } |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1222 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1223 | |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 1224 | procp = static_cast<struct proc*>(calloc(1, sizeof(struct proc))); |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1225 | if (!procp) { |
| 1226 | // Oh, the irony. May need to rebuild our state. |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | procp->pid = params.pid; |
| 1231 | procp->pidfd = pidfd; |
| 1232 | procp->uid = params.uid; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1233 | procp->reg_pid = cred->pid; |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1234 | procp->oomadj = params.oomadj; |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 1235 | procp->valid = true; |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 1236 | proc_insert(procp); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1237 | } else { |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1238 | if (!claim_record(procp, cred->pid)) { |
| 1239 | char buf[LINE_MAX]; |
Suren Baghdasaryan | 9f1be12 | 2021-04-23 13:39:37 -0700 | [diff] [blame] | 1240 | char *taskname = proc_get_name(cred->pid, buf, sizeof(buf)); |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1241 | /* Only registrant of the record can remove it */ |
| 1242 | ALOGE("%s (%d, %d) attempts to modify a process registered by another client", |
Suren Baghdasaryan | 9f1be12 | 2021-04-23 13:39:37 -0700 | [diff] [blame] | 1243 | taskname ? taskname : "A process ", cred->uid, cred->pid); |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1244 | return; |
| 1245 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1246 | proc_unslot(procp); |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1247 | procp->oomadj = params.oomadj; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1248 | proc_slot(procp); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1252 | static void cmd_procremove(LMKD_CTRL_PACKET packet, struct ucred *cred) { |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1253 | struct lmk_procremove params; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1254 | struct proc *procp; |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1255 | |
George Burgess IV | 3b36b90 | 2019-10-02 11:22:55 -0700 | [diff] [blame] | 1256 | lmkd_pack_get_procremove(packet, ¶ms); |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1257 | |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1258 | if (use_inkernel_interface) { |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 1259 | /* |
| 1260 | * Perform an extra check before the pid is removed, after which it |
| 1261 | * will be impossible for poll_kernel to get the taskname. poll_kernel() |
| 1262 | * is potentially a long-running blocking function; however this method |
| 1263 | * handles AMS requests but does not block AMS. |
| 1264 | */ |
| 1265 | poll_kernel(kpoll_fd); |
| 1266 | |
| 1267 | stats_remove_taskname(params.pid); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1268 | return; |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1269 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1270 | |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1271 | procp = pid_lookup(params.pid); |
| 1272 | if (!procp) { |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | if (!claim_record(procp, cred->pid)) { |
| 1277 | char buf[LINE_MAX]; |
Suren Baghdasaryan | 9f1be12 | 2021-04-23 13:39:37 -0700 | [diff] [blame] | 1278 | char *taskname = proc_get_name(cred->pid, buf, sizeof(buf)); |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1279 | /* Only registrant of the record can remove it */ |
| 1280 | ALOGE("%s (%d, %d) attempts to unregister a process registered by another client", |
Suren Baghdasaryan | 9f1be12 | 2021-04-23 13:39:37 -0700 | [diff] [blame] | 1281 | taskname ? taskname : "A process ", cred->uid, cred->pid); |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1282 | return; |
| 1283 | } |
| 1284 | |
Suren Baghdasaryan | 8b00c6d | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * WARNING: After pid_remove() procp is freed and can't be used! |
| 1287 | * Therefore placed at the end of the function. |
| 1288 | */ |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1289 | pid_remove(params.pid); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1292 | static void cmd_procpurge(struct ucred *cred) { |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1293 | int i; |
| 1294 | struct proc *procp; |
| 1295 | struct proc *next; |
| 1296 | |
| 1297 | if (use_inkernel_interface) { |
Jim Blackler | 90853b6 | 2019-09-10 15:30:05 +0100 | [diff] [blame] | 1298 | stats_purge_tasknames(); |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1302 | for (i = 0; i < PIDHASH_SZ; i++) { |
| 1303 | procp = pidhash[i]; |
| 1304 | while (procp) { |
| 1305 | next = procp->pidhash_next; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1306 | /* Purge only records created by the requestor */ |
| 1307 | if (claim_record(procp, cred->pid)) { |
| 1308 | pid_remove(procp->pid); |
| 1309 | } |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1310 | procp = next; |
| 1311 | } |
| 1312 | } |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Suren Baghdasaryan | 36baf44 | 2019-12-23 11:37:34 -0800 | [diff] [blame] | 1315 | static void cmd_subscribe(int dsock_idx, LMKD_CTRL_PACKET packet) { |
| 1316 | struct lmk_subscribe params; |
| 1317 | |
| 1318 | lmkd_pack_get_subscribe(packet, ¶ms); |
| 1319 | data_sock[dsock_idx].async_event_mask |= 1 << params.evt_type; |
| 1320 | } |
| 1321 | |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 1322 | static void inc_killcnt(int oomadj) { |
| 1323 | int slot = ADJTOSLOT(oomadj); |
| 1324 | uint8_t idx = killcnt_idx[slot]; |
| 1325 | |
| 1326 | if (idx == KILLCNT_INVALID_IDX) { |
| 1327 | /* index is not assigned for this oomadj */ |
| 1328 | if (killcnt_free_idx < MAX_DISTINCT_OOM_ADJ) { |
| 1329 | killcnt_idx[slot] = killcnt_free_idx; |
| 1330 | killcnt[killcnt_free_idx] = 1; |
| 1331 | killcnt_free_idx++; |
| 1332 | } else { |
| 1333 | ALOGW("Number of distinct oomadj levels exceeds %d", |
| 1334 | MAX_DISTINCT_OOM_ADJ); |
| 1335 | } |
| 1336 | } else { |
| 1337 | /* |
| 1338 | * wraparound is highly unlikely and is detectable using total |
| 1339 | * counter because it has to be equal to the sum of all counters |
| 1340 | */ |
| 1341 | killcnt[idx]++; |
| 1342 | } |
| 1343 | /* increment total kill counter */ |
| 1344 | killcnt_total++; |
| 1345 | } |
| 1346 | |
| 1347 | static int get_killcnt(int min_oomadj, int max_oomadj) { |
| 1348 | int slot; |
| 1349 | int count = 0; |
| 1350 | |
| 1351 | if (min_oomadj > max_oomadj) |
| 1352 | return 0; |
| 1353 | |
| 1354 | /* special case to get total kill count */ |
| 1355 | if (min_oomadj > OOM_SCORE_ADJ_MAX) |
| 1356 | return killcnt_total; |
| 1357 | |
| 1358 | while (min_oomadj <= max_oomadj && |
| 1359 | (slot = ADJTOSLOT(min_oomadj)) < ADJTOSLOT_COUNT) { |
| 1360 | uint8_t idx = killcnt_idx[slot]; |
| 1361 | if (idx != KILLCNT_INVALID_IDX) { |
| 1362 | count += killcnt[idx]; |
| 1363 | } |
| 1364 | min_oomadj++; |
| 1365 | } |
| 1366 | |
| 1367 | return count; |
| 1368 | } |
| 1369 | |
| 1370 | static int cmd_getkillcnt(LMKD_CTRL_PACKET packet) { |
| 1371 | struct lmk_getkillcnt params; |
| 1372 | |
| 1373 | if (use_inkernel_interface) { |
| 1374 | /* kernel driver does not expose this information */ |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | lmkd_pack_get_getkillcnt(packet, ¶ms); |
| 1379 | |
| 1380 | return get_killcnt(params.min_oomadj, params.max_oomadj); |
| 1381 | } |
| 1382 | |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1383 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1384 | int i; |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1385 | struct lmk_target target; |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1386 | char minfree_str[PROPERTY_VALUE_MAX]; |
| 1387 | char *pstr = minfree_str; |
| 1388 | char *pend = minfree_str + sizeof(minfree_str); |
| 1389 | static struct timespec last_req_tm; |
| 1390 | struct timespec curr_tm; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1391 | |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 1392 | if (ntargets < 1 || ntargets > (int)lowmem_adj.size()) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1393 | return; |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 1394 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1395 | |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * Ratelimit minfree updates to once per TARGET_UPDATE_MIN_INTERVAL_MS |
| 1398 | * to prevent DoS attacks |
| 1399 | */ |
| 1400 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { |
| 1401 | ALOGE("Failed to get current time"); |
| 1402 | return; |
| 1403 | } |
| 1404 | |
| 1405 | if (get_time_diff_ms(&last_req_tm, &curr_tm) < |
| 1406 | TARGET_UPDATE_MIN_INTERVAL_MS) { |
| 1407 | ALOGE("Ignoring frequent updated to lmkd limits"); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | last_req_tm = curr_tm; |
| 1412 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1413 | for (i = 0; i < ntargets; i++) { |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1414 | lmkd_pack_get_target(packet, i, &target); |
| 1415 | lowmem_minfree[i] = target.minfree; |
| 1416 | lowmem_adj[i] = target.oom_adj_score; |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1417 | |
| 1418 | pstr += snprintf(pstr, pend - pstr, "%d:%d,", target.minfree, |
| 1419 | target.oom_adj_score); |
| 1420 | if (pstr >= pend) { |
| 1421 | /* if no more space in the buffer then terminate the loop */ |
| 1422 | pstr = pend; |
| 1423 | break; |
| 1424 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | lowmem_targets_size = ntargets; |
| 1428 | |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1429 | /* Override the last extra comma */ |
| 1430 | pstr[-1] = '\0'; |
| 1431 | property_set("sys.lmk.minfree_levels", minfree_str); |
| 1432 | |
Robert Benea | 7878c9b | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1433 | if (has_inkernel_module) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1434 | char minfreestr[128]; |
| 1435 | char killpriostr[128]; |
| 1436 | |
| 1437 | minfreestr[0] = '\0'; |
| 1438 | killpriostr[0] = '\0'; |
| 1439 | |
| 1440 | for (i = 0; i < lowmem_targets_size; i++) { |
| 1441 | char val[40]; |
| 1442 | |
| 1443 | if (i) { |
| 1444 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 1445 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 1446 | } |
| 1447 | |
Robert Benea | 7878c9b | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1448 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1449 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 7878c9b | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1450 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1451 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 1452 | } |
| 1453 | |
Suren Baghdasaryan | f584fff | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 1454 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); |
| 1455 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1459 | static void ctrl_command_handler(int dsock_idx) { |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1460 | LMKD_CTRL_PACKET packet; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1461 | struct ucred cred; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1462 | int len; |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1463 | enum lmk_cmd cmd; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1464 | int nargs; |
| 1465 | int targets; |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 1466 | int kill_cnt; |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 1467 | int result; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1468 | |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1469 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE, &cred); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1470 | if (len <= 0) |
| 1471 | return; |
| 1472 | |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1473 | if (len < (int)sizeof(int)) { |
| 1474 | ALOGE("Wrong control socket read length len=%d", len); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | cmd = lmkd_pack_get_cmd(packet); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1479 | nargs = len / sizeof(int) - 1; |
| 1480 | if (nargs < 0) |
| 1481 | goto wronglen; |
| 1482 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1483 | switch(cmd) { |
| 1484 | case LMK_TARGET: |
| 1485 | targets = nargs / 2; |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 1486 | if (nargs & 0x1 || targets > (int)lowmem_adj.size()) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1487 | goto wronglen; |
Bart Van Assche | 5450679 | 2022-02-02 23:57:01 +0000 | [diff] [blame] | 1488 | } |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 1489 | cmd_target(targets, packet); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1490 | break; |
| 1491 | case LMK_PROCPRIO: |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1492 | /* process type field is optional for backward compatibility */ |
| 1493 | if (nargs < 3 || nargs > 4) |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1494 | goto wronglen; |
Suren Baghdasaryan | a93503e | 2019-10-22 17:12:01 -0700 | [diff] [blame] | 1495 | cmd_procprio(packet, nargs, &cred); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | case LMK_PROCREMOVE: |
| 1498 | if (nargs != 1) |
| 1499 | goto wronglen; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1500 | cmd_procremove(packet, &cred); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1501 | break; |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1502 | case LMK_PROCPURGE: |
| 1503 | if (nargs != 0) |
| 1504 | goto wronglen; |
Suren Baghdasaryan | 945658a | 2019-10-18 11:16:52 -0700 | [diff] [blame] | 1505 | cmd_procpurge(&cred); |
Suren Baghdasaryan | 83df008 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 1506 | break; |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 1507 | case LMK_GETKILLCNT: |
| 1508 | if (nargs != 2) |
| 1509 | goto wronglen; |
| 1510 | kill_cnt = cmd_getkillcnt(packet); |
| 1511 | len = lmkd_pack_set_getkillcnt_repl(packet, kill_cnt); |
| 1512 | if (ctrl_data_write(dsock_idx, (char *)packet, len) != len) |
| 1513 | return; |
| 1514 | break; |
Suren Baghdasaryan | 36baf44 | 2019-12-23 11:37:34 -0800 | [diff] [blame] | 1515 | case LMK_SUBSCRIBE: |
| 1516 | if (nargs != 1) |
| 1517 | goto wronglen; |
| 1518 | cmd_subscribe(dsock_idx, packet); |
| 1519 | break; |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 1520 | case LMK_PROCKILL: |
| 1521 | /* This command code is NOT expected at all */ |
| 1522 | ALOGE("Received unexpected command code %d", cmd); |
| 1523 | break; |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 1524 | case LMK_UPDATE_PROPS: |
| 1525 | if (nargs != 0) |
| 1526 | goto wronglen; |
| 1527 | update_props(); |
| 1528 | if (!use_inkernel_interface) { |
| 1529 | /* Reinitialize monitors to apply new settings */ |
| 1530 | destroy_monitors(); |
| 1531 | result = init_monitors() ? 0 : -1; |
| 1532 | } else { |
| 1533 | result = 0; |
| 1534 | } |
| 1535 | len = lmkd_pack_set_update_props_repl(packet, result); |
| 1536 | if (ctrl_data_write(dsock_idx, (char *)packet, len) != len) { |
| 1537 | ALOGE("Failed to report operation results"); |
| 1538 | } |
| 1539 | if (!result) { |
| 1540 | ALOGI("Properties reinitilized"); |
| 1541 | } else { |
| 1542 | /* New settings can't be supported, crash to be restarted */ |
| 1543 | ALOGE("New configuration is not supported. Exiting..."); |
| 1544 | exit(1); |
| 1545 | } |
| 1546 | break; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1547 | default: |
| 1548 | ALOGE("Received unknown command code %d", cmd); |
| 1549 | return; |
| 1550 | } |
| 1551 | |
| 1552 | return; |
| 1553 | |
| 1554 | wronglen: |
| 1555 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 1556 | } |
| 1557 | |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 1558 | static void ctrl_data_handler(int data, uint32_t events, |
| 1559 | struct polling_params *poll_params __unused) { |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1560 | if (events & EPOLLIN) { |
| 1561 | ctrl_command_handler(data); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1565 | static int get_free_dsock() { |
| 1566 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1567 | if (data_sock[i].sock < 0) { |
| 1568 | return i; |
| 1569 | } |
| 1570 | } |
| 1571 | return -1; |
| 1572 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1573 | |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 1574 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused, |
| 1575 | struct polling_params *poll_params __unused) { |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1576 | struct epoll_event epev; |
| 1577 | int free_dscock_idx = get_free_dsock(); |
| 1578 | |
| 1579 | if (free_dscock_idx < 0) { |
| 1580 | /* |
| 1581 | * Number of data connections exceeded max supported. This should not |
| 1582 | * happen but if it does we drop all existing connections and accept |
| 1583 | * the new one. This prevents inactive connections from monopolizing |
| 1584 | * data socket and if we drop ActivityManager connection it will |
| 1585 | * immediately reconnect. |
| 1586 | */ |
| 1587 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1588 | ctrl_data_close(i); |
| 1589 | } |
| 1590 | free_dscock_idx = 0; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1593 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); |
| 1594 | if (data_sock[free_dscock_idx].sock < 0) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1595 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 1596 | return; |
| 1597 | } |
| 1598 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1599 | ALOGI("lmkd data connection established"); |
| 1600 | /* use data to store data connection idx */ |
| 1601 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; |
| 1602 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; |
Suren Baghdasaryan | 36baf44 | 2019-12-23 11:37:34 -0800 | [diff] [blame] | 1603 | data_sock[free_dscock_idx].async_event_mask = 0; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1604 | epev.events = EPOLLIN; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1605 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); |
| 1606 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1607 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1608 | ctrl_data_close(free_dscock_idx); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1609 | return; |
| 1610 | } |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1611 | maxevents++; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1614 | /* |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 1615 | * /proc/zoneinfo parsing routines |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1616 | * Expected file format is: |
| 1617 | * |
| 1618 | * Node <node_id>, zone <zone_name> |
| 1619 | * ( |
| 1620 | * per-node stats |
| 1621 | * (<per-node field name> <value>)+ |
| 1622 | * )? |
| 1623 | * (pages free <value> |
| 1624 | * (<per-zone field name> <value>)+ |
| 1625 | * pagesets |
| 1626 | * (<unused fields>)* |
| 1627 | * )+ |
| 1628 | * ... |
| 1629 | */ |
| 1630 | static void zoneinfo_parse_protection(char *buf, struct zoneinfo_zone *zone) { |
| 1631 | int zone_idx; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1632 | int64_t max = 0; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1633 | char *save_ptr; |
| 1634 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1635 | for (buf = strtok_r(buf, "(), ", &save_ptr), zone_idx = 0; |
| 1636 | buf && zone_idx < MAX_NR_ZONES; |
| 1637 | buf = strtok_r(NULL, "), ", &save_ptr), zone_idx++) { |
| 1638 | long long zoneval = strtoll(buf, &buf, 0); |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1639 | if (zoneval > max) { |
| 1640 | max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval; |
| 1641 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1642 | zone->protection[zone_idx] = zoneval; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1643 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1644 | zone->max_protection = max; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1647 | static int zoneinfo_parse_zone(char **buf, struct zoneinfo_zone *zone) { |
| 1648 | for (char *line = strtok_r(NULL, "\n", buf); line; |
| 1649 | line = strtok_r(NULL, "\n", buf)) { |
| 1650 | char *cp; |
| 1651 | char *ap; |
| 1652 | char *save_ptr; |
| 1653 | int64_t val; |
| 1654 | int field_idx; |
| 1655 | enum field_match_result match_res; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1656 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1657 | cp = strtok_r(line, " ", &save_ptr); |
| 1658 | if (!cp) { |
| 1659 | return false; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1660 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1661 | |
| 1662 | field_idx = find_field(cp, zoneinfo_zone_spec_field_names, ZI_ZONE_SPEC_FIELD_COUNT); |
| 1663 | if (field_idx >= 0) { |
| 1664 | /* special field */ |
| 1665 | if (field_idx == ZI_ZONE_SPEC_PAGESETS) { |
| 1666 | /* no mode fields we are interested in */ |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
| 1670 | /* protection field */ |
| 1671 | ap = strtok_r(NULL, ")", &save_ptr); |
| 1672 | if (ap) { |
| 1673 | zoneinfo_parse_protection(ap, zone); |
| 1674 | } |
| 1675 | continue; |
| 1676 | } |
| 1677 | |
| 1678 | ap = strtok_r(NULL, " ", &save_ptr); |
| 1679 | if (!ap) { |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
| 1683 | match_res = match_field(cp, ap, zoneinfo_zone_field_names, ZI_ZONE_FIELD_COUNT, |
| 1684 | &val, &field_idx); |
| 1685 | if (match_res == PARSE_FAIL) { |
| 1686 | return false; |
| 1687 | } |
| 1688 | if (match_res == PARSE_SUCCESS) { |
| 1689 | zone->fields.arr[field_idx] = val; |
| 1690 | } |
| 1691 | if (field_idx == ZI_ZONE_PRESENT && val == 0) { |
| 1692 | /* zone is not populated, stop parsing it */ |
| 1693 | return true; |
| 1694 | } |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1695 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1696 | return false; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1699 | static int zoneinfo_parse_node(char **buf, struct zoneinfo_node *node) { |
| 1700 | int fields_to_match = ZI_NODE_FIELD_COUNT; |
| 1701 | |
| 1702 | for (char *line = strtok_r(NULL, "\n", buf); line; |
| 1703 | line = strtok_r(NULL, "\n", buf)) { |
| 1704 | char *cp; |
| 1705 | char *ap; |
| 1706 | char *save_ptr; |
| 1707 | int64_t val; |
| 1708 | int field_idx; |
| 1709 | enum field_match_result match_res; |
| 1710 | |
| 1711 | cp = strtok_r(line, " ", &save_ptr); |
| 1712 | if (!cp) { |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | ap = strtok_r(NULL, " ", &save_ptr); |
| 1717 | if (!ap) { |
| 1718 | return false; |
| 1719 | } |
| 1720 | |
| 1721 | match_res = match_field(cp, ap, zoneinfo_node_field_names, ZI_NODE_FIELD_COUNT, |
| 1722 | &val, &field_idx); |
| 1723 | if (match_res == PARSE_FAIL) { |
| 1724 | return false; |
| 1725 | } |
| 1726 | if (match_res == PARSE_SUCCESS) { |
| 1727 | node->fields.arr[field_idx] = val; |
| 1728 | fields_to_match--; |
| 1729 | if (!fields_to_match) { |
| 1730 | return true; |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | return false; |
| 1735 | } |
| 1736 | |
| 1737 | static int zoneinfo_parse(struct zoneinfo *zi) { |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1738 | static struct reread_data file_data = { |
| 1739 | .filename = ZONEINFO_PATH, |
| 1740 | .fd = -1, |
| 1741 | }; |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 1742 | char *buf; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1743 | char *save_ptr; |
| 1744 | char *line; |
Greg Kaiser | 259984f | 2019-10-02 07:07:32 -0700 | [diff] [blame] | 1745 | char zone_name[LINE_MAX + 1]; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1746 | struct zoneinfo_node *node = NULL; |
| 1747 | int node_idx = 0; |
| 1748 | int zone_idx = 0; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1749 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1750 | memset(zi, 0, sizeof(struct zoneinfo)); |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1751 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 1752 | if ((buf = reread_file(&file_data)) == NULL) { |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1753 | return -1; |
| 1754 | } |
| 1755 | |
| 1756 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 1757 | line = strtok_r(NULL, "\n", &save_ptr)) { |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1758 | int node_id; |
| 1759 | if (sscanf(line, "Node %d, zone %" STRINGIFY(LINE_MAX) "s", &node_id, zone_name) == 2) { |
| 1760 | if (!node || node->id != node_id) { |
| 1761 | /* new node is found */ |
| 1762 | if (node) { |
| 1763 | node->zone_count = zone_idx + 1; |
| 1764 | node_idx++; |
| 1765 | if (node_idx == MAX_NR_NODES) { |
| 1766 | /* max node count exceeded */ |
| 1767 | ALOGE("%s parse error", file_data.filename); |
| 1768 | return -1; |
| 1769 | } |
| 1770 | } |
| 1771 | node = &zi->nodes[node_idx]; |
| 1772 | node->id = node_id; |
| 1773 | zone_idx = 0; |
| 1774 | if (!zoneinfo_parse_node(&save_ptr, node)) { |
| 1775 | ALOGE("%s parse error", file_data.filename); |
| 1776 | return -1; |
| 1777 | } |
| 1778 | } else { |
| 1779 | /* new zone is found */ |
| 1780 | zone_idx++; |
| 1781 | } |
| 1782 | if (!zoneinfo_parse_zone(&save_ptr, &node->zones[zone_idx])) { |
| 1783 | ALOGE("%s parse error", file_data.filename); |
| 1784 | return -1; |
| 1785 | } |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1788 | if (!node) { |
| 1789 | ALOGE("%s parse error", file_data.filename); |
| 1790 | return -1; |
| 1791 | } |
| 1792 | node->zone_count = zone_idx + 1; |
| 1793 | zi->node_count = node_idx + 1; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1794 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1795 | /* calculate totals fields */ |
| 1796 | for (node_idx = 0; node_idx < zi->node_count; node_idx++) { |
| 1797 | node = &zi->nodes[node_idx]; |
| 1798 | for (zone_idx = 0; zone_idx < node->zone_count; zone_idx++) { |
| 1799 | struct zoneinfo_zone *zone = &zi->nodes[node_idx].zones[zone_idx]; |
| 1800 | zi->totalreserve_pages += zone->max_protection + zone->fields.field.high; |
| 1801 | } |
| 1802 | zi->total_inactive_file += node->fields.field.nr_inactive_file; |
| 1803 | zi->total_active_file += node->fields.field.nr_active_file; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 1804 | } |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1805 | return 0; |
| 1806 | } |
| 1807 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 1808 | /* /proc/meminfo parsing routines */ |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1809 | static bool meminfo_parse_line(char *line, union meminfo *mi) { |
| 1810 | char *cp = line; |
| 1811 | char *ap; |
| 1812 | char *save_ptr; |
| 1813 | int64_t val; |
| 1814 | int field_idx; |
| 1815 | enum field_match_result match_res; |
| 1816 | |
| 1817 | cp = strtok_r(line, " ", &save_ptr); |
| 1818 | if (!cp) { |
| 1819 | return false; |
| 1820 | } |
| 1821 | |
| 1822 | ap = strtok_r(NULL, " ", &save_ptr); |
| 1823 | if (!ap) { |
| 1824 | return false; |
| 1825 | } |
| 1826 | |
| 1827 | match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT, |
| 1828 | &val, &field_idx); |
| 1829 | if (match_res == PARSE_SUCCESS) { |
| 1830 | mi->arr[field_idx] = val / page_k; |
| 1831 | } |
| 1832 | return (match_res != PARSE_FAIL); |
| 1833 | } |
| 1834 | |
Suren Baghdasaryan | 940e7cf | 2021-05-27 18:15:44 -0700 | [diff] [blame] | 1835 | static int64_t read_gpu_total_kb() { |
| 1836 | static int fd = android::bpf::bpfFdGet( |
| 1837 | "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map", BPF_F_RDONLY); |
| 1838 | static constexpr uint64_t kBpfKeyGpuTotalUsage = 0; |
| 1839 | uint64_t value; |
| 1840 | |
| 1841 | if (fd < 0) { |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | return android::bpf::findMapEntry(fd, &kBpfKeyGpuTotalUsage, &value) |
| 1846 | ? 0 |
| 1847 | : (int32_t)(value / 1024); |
| 1848 | } |
| 1849 | |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1850 | static int meminfo_parse(union meminfo *mi) { |
| 1851 | static struct reread_data file_data = { |
| 1852 | .filename = MEMINFO_PATH, |
| 1853 | .fd = -1, |
| 1854 | }; |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 1855 | char *buf; |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1856 | char *save_ptr; |
| 1857 | char *line; |
| 1858 | |
| 1859 | memset(mi, 0, sizeof(union meminfo)); |
| 1860 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 1861 | if ((buf = reread_file(&file_data)) == NULL) { |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1862 | return -1; |
| 1863 | } |
| 1864 | |
| 1865 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 1866 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 1867 | if (!meminfo_parse_line(line, mi)) { |
| 1868 | ALOGE("%s parse error", file_data.filename); |
| 1869 | return -1; |
| 1870 | } |
| 1871 | } |
| 1872 | mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + |
| 1873 | mi->field.buffers; |
Suren Baghdasaryan | 940e7cf | 2021-05-27 18:15:44 -0700 | [diff] [blame] | 1874 | mi->field.total_gpu_kb = read_gpu_total_kb(); |
Suren Baghdasaryan | d28a973 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 1879 | /* /proc/vmstat parsing routines */ |
| 1880 | static bool vmstat_parse_line(char *line, union vmstat *vs) { |
| 1881 | char *cp; |
| 1882 | char *ap; |
| 1883 | char *save_ptr; |
| 1884 | int64_t val; |
| 1885 | int field_idx; |
| 1886 | enum field_match_result match_res; |
| 1887 | |
| 1888 | cp = strtok_r(line, " ", &save_ptr); |
| 1889 | if (!cp) { |
| 1890 | return false; |
| 1891 | } |
| 1892 | |
| 1893 | ap = strtok_r(NULL, " ", &save_ptr); |
| 1894 | if (!ap) { |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
| 1898 | match_res = match_field(cp, ap, vmstat_field_names, VS_FIELD_COUNT, |
| 1899 | &val, &field_idx); |
| 1900 | if (match_res == PARSE_SUCCESS) { |
| 1901 | vs->arr[field_idx] = val; |
| 1902 | } |
| 1903 | return (match_res != PARSE_FAIL); |
| 1904 | } |
| 1905 | |
| 1906 | static int vmstat_parse(union vmstat *vs) { |
| 1907 | static struct reread_data file_data = { |
| 1908 | .filename = VMSTAT_PATH, |
| 1909 | .fd = -1, |
| 1910 | }; |
| 1911 | char *buf; |
| 1912 | char *save_ptr; |
| 1913 | char *line; |
| 1914 | |
| 1915 | memset(vs, 0, sizeof(union vmstat)); |
| 1916 | |
| 1917 | if ((buf = reread_file(&file_data)) == NULL) { |
| 1918 | return -1; |
| 1919 | } |
| 1920 | |
| 1921 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 1922 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 1923 | if (!vmstat_parse_line(line, vs)) { |
| 1924 | ALOGE("%s parse error", file_data.filename); |
| 1925 | return -1; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 1932 | static int psi_parse(struct reread_data *file_data, struct psi_stats stats[], bool full) { |
| 1933 | char *buf; |
| 1934 | char *save_ptr; |
| 1935 | char *line; |
| 1936 | |
| 1937 | if ((buf = reread_file(file_data)) == NULL) { |
| 1938 | return -1; |
| 1939 | } |
| 1940 | |
| 1941 | line = strtok_r(buf, "\n", &save_ptr); |
| 1942 | if (parse_psi_line(line, PSI_SOME, stats)) { |
| 1943 | return -1; |
| 1944 | } |
| 1945 | if (full) { |
| 1946 | line = strtok_r(NULL, "\n", &save_ptr); |
| 1947 | if (parse_psi_line(line, PSI_FULL, stats)) { |
| 1948 | return -1; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | static int psi_parse_mem(struct psi_data *psi_data) { |
| 1956 | static struct reread_data file_data = { |
| 1957 | .filename = PSI_PATH_MEMORY, |
| 1958 | .fd = -1, |
| 1959 | }; |
| 1960 | return psi_parse(&file_data, psi_data->mem_stats, true); |
| 1961 | } |
| 1962 | |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 1963 | static int psi_parse_io(struct psi_data *psi_data) { |
| 1964 | static struct reread_data file_data = { |
| 1965 | .filename = PSI_PATH_IO, |
| 1966 | .fd = -1, |
| 1967 | }; |
| 1968 | return psi_parse(&file_data, psi_data->io_stats, true); |
| 1969 | } |
| 1970 | |
| 1971 | static int psi_parse_cpu(struct psi_data *psi_data) { |
| 1972 | static struct reread_data file_data = { |
| 1973 | .filename = PSI_PATH_CPU, |
| 1974 | .fd = -1, |
| 1975 | }; |
| 1976 | return psi_parse(&file_data, psi_data->cpu_stats, false); |
| 1977 | } |
| 1978 | |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 1979 | enum wakeup_reason { |
| 1980 | Event, |
| 1981 | Polling |
| 1982 | }; |
| 1983 | |
| 1984 | struct wakeup_info { |
| 1985 | struct timespec wakeup_tm; |
| 1986 | struct timespec prev_wakeup_tm; |
| 1987 | struct timespec last_event_tm; |
| 1988 | int wakeups_since_event; |
| 1989 | int skipped_wakeups; |
| 1990 | }; |
| 1991 | |
| 1992 | /* |
| 1993 | * After the initial memory pressure event is received lmkd schedules periodic wakeups to check |
| 1994 | * the memory conditions and kill if needed (polling). This is done because pressure events are |
| 1995 | * rate-limited and memory conditions can change in between events. Therefore after the initial |
| 1996 | * event there might be multiple wakeups. This function records the wakeup information such as the |
| 1997 | * timestamps of the last event and the last wakeup, the number of wakeups since the last event |
| 1998 | * and how many of those wakeups were skipped (some wakeups are skipped if previously killed |
| 1999 | * process is still freeing its memory). |
| 2000 | */ |
| 2001 | static void record_wakeup_time(struct timespec *tm, enum wakeup_reason reason, |
| 2002 | struct wakeup_info *wi) { |
| 2003 | wi->prev_wakeup_tm = wi->wakeup_tm; |
| 2004 | wi->wakeup_tm = *tm; |
| 2005 | if (reason == Event) { |
| 2006 | wi->last_event_tm = *tm; |
| 2007 | wi->wakeups_since_event = 0; |
| 2008 | wi->skipped_wakeups = 0; |
| 2009 | } else { |
| 2010 | wi->wakeups_since_event++; |
| 2011 | } |
| 2012 | } |
| 2013 | |
Suren Baghdasaryan | 39b5480 | 2021-08-09 15:10:25 -0700 | [diff] [blame] | 2014 | struct kill_info { |
| 2015 | enum kill_reasons kill_reason; |
| 2016 | const char *kill_desc; |
| 2017 | int thrashing; |
| 2018 | int max_thrashing; |
| 2019 | }; |
| 2020 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2021 | static void killinfo_log(struct proc* procp, int min_oom_score, int rss_kb, |
Suren Baghdasaryan | 39b5480 | 2021-08-09 15:10:25 -0700 | [diff] [blame] | 2022 | int swap_kb, struct kill_info *ki, union meminfo *mi, |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2023 | struct wakeup_info *wi, struct timespec *tm, struct psi_data *pd) { |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 2024 | /* log process information */ |
| 2025 | android_log_write_int32(ctx, procp->pid); |
| 2026 | android_log_write_int32(ctx, procp->uid); |
| 2027 | android_log_write_int32(ctx, procp->oomadj); |
| 2028 | android_log_write_int32(ctx, min_oom_score); |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 2029 | android_log_write_int32(ctx, std::min(rss_kb, (int)INT32_MAX)); |
Suren Baghdasaryan | 39b5480 | 2021-08-09 15:10:25 -0700 | [diff] [blame] | 2030 | android_log_write_int32(ctx, ki ? ki->kill_reason : NONE); |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 2031 | |
| 2032 | /* log meminfo fields */ |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 2033 | for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) { |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 2034 | android_log_write_int32(ctx, |
| 2035 | mi ? std::min(mi->arr[field_idx] * page_k, (int64_t)INT32_MAX) : 0); |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2038 | /* log lmkd wakeup information */ |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2039 | if (wi) { |
| 2040 | android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->last_event_tm, tm)); |
| 2041 | android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->prev_wakeup_tm, tm)); |
| 2042 | android_log_write_int32(ctx, wi->wakeups_since_event); |
| 2043 | android_log_write_int32(ctx, wi->skipped_wakeups); |
| 2044 | } else { |
| 2045 | android_log_write_int32(ctx, 0); |
| 2046 | android_log_write_int32(ctx, 0); |
| 2047 | android_log_write_int32(ctx, 0); |
| 2048 | android_log_write_int32(ctx, 0); |
| 2049 | } |
| 2050 | |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 2051 | android_log_write_int32(ctx, std::min(swap_kb, (int)INT32_MAX)); |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2052 | android_log_write_int32(ctx, mi ? (int32_t)mi->field.total_gpu_kb : 0); |
Suren Baghdasaryan | 39b5480 | 2021-08-09 15:10:25 -0700 | [diff] [blame] | 2053 | if (ki) { |
| 2054 | android_log_write_int32(ctx, ki->thrashing); |
| 2055 | android_log_write_int32(ctx, ki->max_thrashing); |
| 2056 | } else { |
| 2057 | android_log_write_int32(ctx, 0); |
| 2058 | android_log_write_int32(ctx, 0); |
| 2059 | } |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2060 | |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2061 | if (pd) { |
| 2062 | android_log_write_float32(ctx, pd->mem_stats[PSI_SOME].avg10); |
| 2063 | android_log_write_float32(ctx, pd->mem_stats[PSI_FULL].avg10); |
| 2064 | android_log_write_float32(ctx, pd->io_stats[PSI_SOME].avg10); |
| 2065 | android_log_write_float32(ctx, pd->io_stats[PSI_FULL].avg10); |
| 2066 | android_log_write_float32(ctx, pd->cpu_stats[PSI_SOME].avg10); |
| 2067 | } else { |
| 2068 | for (int i = 0; i < 5; i++) { |
| 2069 | android_log_write_float32(ctx, 0); |
| 2070 | } |
| 2071 | } |
| 2072 | |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 2073 | android_log_write_list(ctx, LOG_ID_EVENTS); |
| 2074 | android_log_reset(ctx); |
| 2075 | } |
| 2076 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2077 | // Note: returned entry is only an anchor and does not hold a valid process info. |
| 2078 | // When called from a non-main thread, adjslot_list_lock read lock should be taken. |
| 2079 | static struct proc *proc_adj_head(int oomadj) { |
| 2080 | return (struct proc *)&procadjslot_list[ADJTOSLOT(oomadj)]; |
| 2081 | } |
| 2082 | |
| 2083 | // When called from a non-main thread, adjslot_list_lock read lock should be taken. |
| 2084 | static struct proc *proc_adj_tail(int oomadj) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2085 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 2086 | } |
| 2087 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2088 | // When called from a non-main thread, adjslot_list_lock read lock should be taken. |
| 2089 | static struct proc *proc_adj_prev(int oomadj, int pid) { |
| 2090 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 2091 | struct adjslot_list *curr = adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 2092 | |
| 2093 | while (curr != head) { |
| 2094 | if (((struct proc *)curr)->pid == pid) { |
| 2095 | return (struct proc *)curr->prev; |
| 2096 | } |
| 2097 | curr = curr->prev; |
| 2098 | } |
| 2099 | |
| 2100 | return NULL; |
| 2101 | } |
| 2102 | |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 2103 | // Can be called only from the main thread. |
Suren Baghdasaryan | eb7c549 | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 2104 | static struct proc *proc_get_heaviest(int oomadj) { |
| 2105 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 2106 | struct adjslot_list *curr = head->next; |
| 2107 | struct proc *maxprocp = NULL; |
| 2108 | int maxsize = 0; |
| 2109 | while (curr != head) { |
| 2110 | int pid = ((struct proc *)curr)->pid; |
| 2111 | int tasksize = proc_get_size(pid); |
Suren Baghdasaryan | 5263aa7 | 2021-04-29 15:28:57 -0700 | [diff] [blame] | 2112 | if (tasksize < 0) { |
Suren Baghdasaryan | eb7c549 | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 2113 | struct adjslot_list *next = curr->next; |
| 2114 | pid_remove(pid); |
| 2115 | curr = next; |
| 2116 | } else { |
| 2117 | if (tasksize > maxsize) { |
| 2118 | maxsize = tasksize; |
| 2119 | maxprocp = (struct proc *)curr; |
| 2120 | } |
| 2121 | curr = curr->next; |
| 2122 | } |
| 2123 | } |
| 2124 | return maxprocp; |
| 2125 | } |
| 2126 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2127 | static bool find_victim(int oom_score, int prev_pid, struct proc &target_proc) { |
| 2128 | struct proc *procp; |
| 2129 | std::shared_lock lock(adjslot_list_lock); |
| 2130 | |
| 2131 | if (!prev_pid) { |
| 2132 | procp = proc_adj_tail(oom_score); |
| 2133 | } else { |
| 2134 | procp = proc_adj_prev(oom_score, prev_pid); |
| 2135 | if (!procp) { |
| 2136 | // pid was removed, restart at the tail |
| 2137 | procp = proc_adj_tail(oom_score); |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | // the list is empty at this oom_score or we looped through it |
| 2142 | if (!procp || procp == proc_adj_head(oom_score)) { |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
| 2146 | // make a copy because original might be destroyed after adjslot_list_lock is released |
| 2147 | target_proc = *procp; |
| 2148 | |
| 2149 | return true; |
| 2150 | } |
| 2151 | |
| 2152 | static void watchdog_callback() { |
| 2153 | int prev_pid = 0; |
| 2154 | |
| 2155 | ALOGW("lmkd watchdog timed out!"); |
| 2156 | for (int oom_score = OOM_SCORE_ADJ_MAX; oom_score >= 0;) { |
| 2157 | struct proc target; |
| 2158 | |
| 2159 | if (!find_victim(oom_score, prev_pid, target)) { |
| 2160 | oom_score--; |
| 2161 | prev_pid = 0; |
| 2162 | continue; |
| 2163 | } |
| 2164 | |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 2165 | if (target.valid && reaper.kill({ target.pidfd, target.pid, target.uid }, true) == 0) { |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2166 | ALOGW("lmkd watchdog killed process %d, oom_score_adj %d", target.pid, oom_score); |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2167 | killinfo_log(&target, 0, 0, 0, NULL, NULL, NULL, NULL, NULL); |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 2168 | // Can't call pid_remove() from non-main thread, therefore just invalidate the record |
| 2169 | pid_invalidate(target.pid); |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2170 | break; |
| 2171 | } |
| 2172 | prev_pid = target.pid; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | static Watchdog watchdog(WATCHDOG_TIMEOUT_SEC, watchdog_callback); |
| 2177 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2178 | static bool is_kill_pending(void) { |
| 2179 | char buf[24]; |
| 2180 | |
| 2181 | if (last_kill_pid_or_fd < 0) { |
| 2182 | return false; |
| 2183 | } |
| 2184 | |
| 2185 | if (pidfd_supported) { |
| 2186 | return true; |
| 2187 | } |
| 2188 | |
| 2189 | /* when pidfd is not supported base the decision on /proc/<pid> existence */ |
| 2190 | snprintf(buf, sizeof(buf), "/proc/%d/", last_kill_pid_or_fd); |
| 2191 | if (access(buf, F_OK) == 0) { |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | return false; |
| 2196 | } |
| 2197 | |
| 2198 | static bool is_waiting_for_kill(void) { |
| 2199 | return pidfd_supported && last_kill_pid_or_fd >= 0; |
| 2200 | } |
| 2201 | |
| 2202 | static void stop_wait_for_proc_kill(bool finished) { |
| 2203 | struct epoll_event epev; |
| 2204 | |
| 2205 | if (last_kill_pid_or_fd < 0) { |
| 2206 | return; |
| 2207 | } |
| 2208 | |
| 2209 | if (debug_process_killing) { |
| 2210 | struct timespec curr_tm; |
| 2211 | |
| 2212 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { |
| 2213 | /* |
| 2214 | * curr_tm is used here merely to report kill duration, so this failure is not fatal. |
| 2215 | * Log an error and continue. |
| 2216 | */ |
| 2217 | ALOGE("Failed to get current time"); |
| 2218 | } |
| 2219 | |
| 2220 | if (finished) { |
| 2221 | ALOGI("Process got killed in %ldms", |
| 2222 | get_time_diff_ms(&last_kill_tm, &curr_tm)); |
| 2223 | } else { |
| 2224 | ALOGI("Stop waiting for process kill after %ldms", |
| 2225 | get_time_diff_ms(&last_kill_tm, &curr_tm)); |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | if (pidfd_supported) { |
| 2230 | /* unregister fd */ |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 2231 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, last_kill_pid_or_fd, &epev)) { |
| 2232 | // Log an error and keep going |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2233 | ALOGE("epoll_ctl for last killed process failed; errno=%d", errno); |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2234 | } |
| 2235 | maxevents--; |
| 2236 | close(last_kill_pid_or_fd); |
| 2237 | } |
| 2238 | |
| 2239 | last_kill_pid_or_fd = -1; |
| 2240 | } |
| 2241 | |
| 2242 | static void kill_done_handler(int data __unused, uint32_t events __unused, |
| 2243 | struct polling_params *poll_params) { |
| 2244 | stop_wait_for_proc_kill(true); |
| 2245 | poll_params->update = POLLING_RESUME; |
| 2246 | } |
| 2247 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 2248 | static void kill_fail_handler(int data __unused, uint32_t events __unused, |
| 2249 | struct polling_params *poll_params) { |
| 2250 | int pid; |
| 2251 | |
| 2252 | // Extract pid from the communication pipe. Clearing the pipe this way allows further |
| 2253 | // epoll_wait calls to sleep until the next event. |
| 2254 | if (TEMP_FAILURE_RETRY(read(reaper_comm_fd[0], &pid, sizeof(pid))) != sizeof(pid)) { |
| 2255 | ALOGE("thread communication read failed: %s", strerror(errno)); |
| 2256 | } |
| 2257 | stop_wait_for_proc_kill(false); |
| 2258 | poll_params->update = POLLING_RESUME; |
| 2259 | } |
| 2260 | |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 2261 | static void start_wait_for_proc_kill(int pid_or_fd) { |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2262 | static struct event_handler_info kill_done_hinfo = { 0, kill_done_handler }; |
| 2263 | struct epoll_event epev; |
| 2264 | |
| 2265 | if (last_kill_pid_or_fd >= 0) { |
| 2266 | /* Should not happen but if it does we should stop previous wait */ |
| 2267 | ALOGE("Attempt to wait for a kill while another wait is in progress"); |
| 2268 | stop_wait_for_proc_kill(false); |
| 2269 | } |
| 2270 | |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 2271 | last_kill_pid_or_fd = pid_or_fd; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2272 | |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 2273 | if (!pidfd_supported) { |
| 2274 | /* If pidfd is not supported just store PID and exit */ |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2275 | return; |
| 2276 | } |
| 2277 | |
| 2278 | epev.events = EPOLLIN; |
| 2279 | epev.data.ptr = (void *)&kill_done_hinfo; |
| 2280 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, last_kill_pid_or_fd, &epev) != 0) { |
| 2281 | ALOGE("epoll_ctl for last kill failed; errno=%d", errno); |
| 2282 | close(last_kill_pid_or_fd); |
| 2283 | last_kill_pid_or_fd = -1; |
| 2284 | return; |
| 2285 | } |
| 2286 | maxevents++; |
| 2287 | } |
Tim Murray | a79ec0f | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 2288 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2289 | /* Kill one process specified by procp. Returns the size (in pages) of the process killed */ |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2290 | static int kill_one_process(struct proc* procp, int min_oom_score, struct kill_info *ki, |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2291 | union meminfo *mi, struct wakeup_info *wi, struct timespec *tm, |
| 2292 | struct psi_data *pd) { |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2293 | int pid = procp->pid; |
Suren Baghdasaryan | a10157c | 2019-07-19 10:55:39 -0700 | [diff] [blame] | 2294 | int pidfd = procp->pidfd; |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2295 | uid_t uid = procp->uid; |
| 2296 | char *taskname; |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 2297 | int kill_result; |
Suren Baghdasaryan | 8b00c6d | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 2298 | int result = -1; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 2299 | struct memory_stat *mem_st; |
Suren Baghdasaryan | 3cc1f13 | 2020-09-09 20:19:02 -0700 | [diff] [blame] | 2300 | struct kill_stat kill_st; |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2301 | int64_t tgid; |
| 2302 | int64_t rss_kb; |
| 2303 | int64_t swap_kb; |
| 2304 | char buf[PAGE_SIZE]; |
Suren Baghdasaryan | 34928bb | 2021-07-29 17:02:51 -0700 | [diff] [blame] | 2305 | char desc[LINE_MAX]; |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2306 | |
Suren Baghdasaryan | a3802f1 | 2022-09-27 14:30:34 -0700 | [diff] [blame] | 2307 | if (!procp->valid || !read_proc_status(pid, buf, sizeof(buf))) { |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2308 | goto out; |
| 2309 | } |
| 2310 | if (!parse_status_tag(buf, PROC_STATUS_TGID_FIELD, &tgid)) { |
| 2311 | ALOGE("Unable to parse tgid from /proc/%d/status", pid); |
| 2312 | goto out; |
| 2313 | } |
| 2314 | if (tgid != pid) { |
| 2315 | ALOGE("Possible pid reuse detected (pid %d, tgid %" PRId64 ")!", pid, tgid); |
| 2316 | goto out; |
| 2317 | } |
| 2318 | // Zombie processes will not have RSS / Swap fields. |
| 2319 | if (!parse_status_tag(buf, PROC_STATUS_RSS_FIELD, &rss_kb)) { |
| 2320 | goto out; |
| 2321 | } |
| 2322 | if (!parse_status_tag(buf, PROC_STATUS_SWAP_FIELD, &swap_kb)) { |
Suren Baghdasaryan | 3ee11d4 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 2323 | goto out; |
| 2324 | } |
| 2325 | |
Suren Baghdasaryan | 632f1c5 | 2019-10-04 16:06:55 -0700 | [diff] [blame] | 2326 | taskname = proc_get_name(pid, buf, sizeof(buf)); |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2327 | // taskname will point inside buf, do not reuse buf onwards. |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2328 | if (!taskname) { |
Suren Baghdasaryan | 8b00c6d | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 2329 | goto out; |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2332 | mem_st = stats_read_memory_stat(per_app_memcg, pid, uid, rss_kb * 1024, swap_kb * 1024); |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2333 | |
George Burgess IV | e849f14 | 2021-08-05 06:59:42 +0000 | [diff] [blame] | 2334 | snprintf(desc, sizeof(desc), "lmk,%d,%d,%d,%d,%d", pid, ki ? (int)ki->kill_reason : -1, |
| 2335 | procp->oomadj, min_oom_score, ki ? ki->max_thrashing : -1); |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 2336 | |
Suren Baghdasaryan | 34928bb | 2021-07-29 17:02:51 -0700 | [diff] [blame] | 2337 | trace_kill_start(pid, desc); |
Suren Baghdasaryan | 03e1987 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 2338 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 2339 | start_wait_for_proc_kill(pidfd < 0 ? pid : pidfd); |
Suren Baghdasaryan | 2bdf7f0 | 2022-01-20 18:48:52 -0800 | [diff] [blame] | 2340 | kill_result = reaper.kill({ pidfd, pid, uid }, false); |
Wei Wang | f1ee2e1 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 2341 | |
Suren Baghdasaryan | 34928bb | 2021-07-29 17:02:51 -0700 | [diff] [blame] | 2342 | trace_kill_end(); |
Suren Baghdasaryan | c2e05b6 | 2019-09-04 16:44:47 -0700 | [diff] [blame] | 2343 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 2344 | if (kill_result) { |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2345 | stop_wait_for_proc_kill(false); |
Suren Baghdasaryan | c2e05b6 | 2019-09-04 16:44:47 -0700 | [diff] [blame] | 2346 | ALOGE("kill(%d): errno=%d", pid, errno); |
| 2347 | /* Delete process record even when we fail to kill so that we don't get stuck on it */ |
| 2348 | goto out; |
| 2349 | } |
| 2350 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2351 | last_kill_tm = *tm; |
| 2352 | |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 2353 | inc_killcnt(procp->oomadj); |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 2354 | |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2355 | if (ki) { |
| 2356 | kill_st.kill_reason = ki->kill_reason; |
| 2357 | kill_st.thrashing = ki->thrashing; |
| 2358 | kill_st.max_thrashing = ki->max_thrashing; |
Ioannis Ilkos | 4884890 | 2021-02-18 19:53:33 +0000 | [diff] [blame] | 2359 | ALOGI("Kill '%s' (%d), uid %d, oom_score_adj %d to free %" PRId64 "kB rss, %" PRId64 |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2360 | "kB swap; reason: %s", taskname, pid, uid, procp->oomadj, rss_kb, swap_kb, |
| 2361 | ki->kill_desc); |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2362 | } else { |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2363 | kill_st.kill_reason = NONE; |
| 2364 | kill_st.thrashing = 0; |
| 2365 | kill_st.max_thrashing = 0; |
Ioannis Ilkos | 4884890 | 2021-02-18 19:53:33 +0000 | [diff] [blame] | 2366 | ALOGI("Kill '%s' (%d), uid %d, oom_score_adj %d to free %" PRId64 "kB rss, %" PRId64 |
| 2367 | "kb swap", taskname, pid, uid, procp->oomadj, rss_kb, swap_kb); |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2368 | } |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2369 | killinfo_log(procp, min_oom_score, rss_kb, swap_kb, ki, mi, wi, tm, pd); |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2370 | |
Suren Baghdasaryan | 3cc1f13 | 2020-09-09 20:19:02 -0700 | [diff] [blame] | 2371 | kill_st.uid = static_cast<int32_t>(uid); |
| 2372 | kill_st.taskname = taskname; |
Suren Baghdasaryan | 3cc1f13 | 2020-09-09 20:19:02 -0700 | [diff] [blame] | 2373 | kill_st.oom_score = procp->oomadj; |
| 2374 | kill_st.min_oom_score = min_oom_score; |
| 2375 | kill_st.free_mem_kb = mi->field.nr_free_pages * page_k; |
| 2376 | kill_st.free_swap_kb = mi->field.free_swap * page_k; |
| 2377 | stats_write_lmk_kill_occurred(&kill_st, mem_st); |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 2378 | |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 2379 | ctrl_data_write_lmk_kill_occurred((pid_t)pid, uid); |
| 2380 | |
Ioannis Ilkos | 279268a | 2020-08-01 10:50:40 +0100 | [diff] [blame] | 2381 | result = rss_kb / page_k; |
Mark Salyzyn | 1d5fdf3 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 2382 | |
Suren Baghdasaryan | 8b00c6d | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 2383 | out: |
| 2384 | /* |
| 2385 | * WARNING: After pid_remove() procp is freed and can't be used! |
| 2386 | * Therefore placed at the end of the function. |
| 2387 | */ |
| 2388 | pid_remove(pid); |
| 2389 | return result; |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | /* |
Chris Morin | 74b4df9 | 2021-02-26 00:00:35 -0800 | [diff] [blame] | 2393 | * Find one process to kill at or above the given oom_score_adj level. |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 2394 | * Returns size of the killed process. |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2395 | */ |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2396 | static int find_and_kill_process(int min_score_adj, struct kill_info *ki, union meminfo *mi, |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2397 | struct wakeup_info *wi, struct timespec *tm, |
| 2398 | struct psi_data *pd) { |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2399 | int i; |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 2400 | int killed_size = 0; |
Yang Lu | 5dfffbc | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 2401 | bool lmk_state_change_start = false; |
Suren Baghdasaryan | 858e8c6 | 2021-03-03 11:05:09 -0800 | [diff] [blame] | 2402 | bool choose_heaviest_task = kill_heaviest_task; |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2403 | |
Chong Zhang | 1cd12b5 | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 2404 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2405 | struct proc *procp; |
| 2406 | |
Suren Baghdasaryan | 858e8c6 | 2021-03-03 11:05:09 -0800 | [diff] [blame] | 2407 | if (!choose_heaviest_task && i <= PERCEPTIBLE_APP_ADJ) { |
| 2408 | /* |
| 2409 | * If we have to choose a perceptible process, choose the heaviest one to |
| 2410 | * hopefully minimize the number of victims. |
| 2411 | */ |
| 2412 | choose_heaviest_task = true; |
| 2413 | } |
| 2414 | |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2415 | while (true) { |
Suren Baghdasaryan | 858e8c6 | 2021-03-03 11:05:09 -0800 | [diff] [blame] | 2416 | procp = choose_heaviest_task ? |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 2417 | proc_get_heaviest(i) : proc_adj_tail(i); |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2418 | |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2419 | if (!procp) |
| 2420 | break; |
| 2421 | |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2422 | killed_size = kill_one_process(procp, min_score_adj, ki, mi, wi, tm, pd); |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2423 | if (killed_size >= 0) { |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 2424 | if (!lmk_state_change_start) { |
Yang Lu | 5dfffbc | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 2425 | lmk_state_change_start = true; |
Vova Sharaienko | a92b76b | 2021-04-24 00:30:06 +0000 | [diff] [blame] | 2426 | stats_write_lmk_state_changed(STATE_START); |
Yang Lu | 5dfffbc | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 2427 | } |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 2428 | break; |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2429 | } |
| 2430 | } |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 2431 | if (killed_size) { |
| 2432 | break; |
| 2433 | } |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 2436 | if (lmk_state_change_start) { |
Vova Sharaienko | a92b76b | 2021-04-24 00:30:06 +0000 | [diff] [blame] | 2437 | stats_write_lmk_state_changed(STATE_STOP); |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2438 | } |
Rajeev Kumar | 4aba915 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2439 | |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 2440 | return killed_size; |
Colin Cross | 3d57a51 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2443 | static int64_t get_memory_usage(struct reread_data *file_data) { |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 2444 | int64_t mem_usage; |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 2445 | char *buf; |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2446 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 2447 | if ((buf = reread_file(file_data)) == NULL) { |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 2448 | return -1; |
| 2449 | } |
| 2450 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2451 | if (!parse_int64(buf, &mem_usage)) { |
| 2452 | ALOGE("%s parse error", file_data->filename); |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 2453 | return -1; |
| 2454 | } |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 2455 | if (mem_usage == 0) { |
| 2456 | ALOGE("No memory!"); |
| 2457 | return -1; |
| 2458 | } |
| 2459 | return mem_usage; |
| 2460 | } |
| 2461 | |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2462 | void record_low_pressure_levels(union meminfo *mi) { |
| 2463 | if (low_pressure_mem.min_nr_free_pages == -1 || |
| 2464 | low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) { |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2465 | if (debug_process_killing) { |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2466 | ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64, |
| 2467 | low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2468 | } |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2469 | low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2470 | } |
| 2471 | /* |
| 2472 | * Free memory at low vmpressure events occasionally gets spikes, |
| 2473 | * possibly a stale low vmpressure event with memory already |
| 2474 | * freed up (no memory pressure should have been reported). |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2475 | * Ignore large jumps in max_nr_free_pages that would mess up our stats. |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2476 | */ |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2477 | if (low_pressure_mem.max_nr_free_pages == -1 || |
| 2478 | (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages && |
| 2479 | mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages < |
| 2480 | low_pressure_mem.max_nr_free_pages * 0.1)) { |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2481 | if (debug_process_killing) { |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2482 | ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64, |
| 2483 | low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2484 | } |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2485 | low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 2489 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 2490 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 2491 | level + 1 : level); |
| 2492 | } |
| 2493 | |
| 2494 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 2495 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 2496 | level - 1 : level); |
| 2497 | } |
| 2498 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2499 | enum zone_watermark { |
| 2500 | WMARK_MIN = 0, |
| 2501 | WMARK_LOW, |
| 2502 | WMARK_HIGH, |
| 2503 | WMARK_NONE |
| 2504 | }; |
| 2505 | |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2506 | struct zone_watermarks { |
| 2507 | long high_wmark; |
| 2508 | long low_wmark; |
| 2509 | long min_wmark; |
| 2510 | }; |
| 2511 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2512 | /* |
| 2513 | * Returns lowest breached watermark or WMARK_NONE. |
| 2514 | */ |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2515 | static enum zone_watermark get_lowest_watermark(union meminfo *mi, |
| 2516 | struct zone_watermarks *watermarks) |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2517 | { |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2518 | int64_t nr_free_pages = mi->field.nr_free_pages - mi->field.cma_free; |
| 2519 | |
| 2520 | if (nr_free_pages < watermarks->min_wmark) { |
| 2521 | return WMARK_MIN; |
| 2522 | } |
| 2523 | if (nr_free_pages < watermarks->low_wmark) { |
| 2524 | return WMARK_LOW; |
| 2525 | } |
| 2526 | if (nr_free_pages < watermarks->high_wmark) { |
| 2527 | return WMARK_HIGH; |
| 2528 | } |
| 2529 | return WMARK_NONE; |
| 2530 | } |
| 2531 | |
| 2532 | void calc_zone_watermarks(struct zoneinfo *zi, struct zone_watermarks *watermarks) { |
| 2533 | memset(watermarks, 0, sizeof(struct zone_watermarks)); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2534 | |
| 2535 | for (int node_idx = 0; node_idx < zi->node_count; node_idx++) { |
| 2536 | struct zoneinfo_node *node = &zi->nodes[node_idx]; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2537 | for (int zone_idx = 0; zone_idx < node->zone_count; zone_idx++) { |
| 2538 | struct zoneinfo_zone *zone = &node->zones[zone_idx]; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2539 | |
| 2540 | if (!zone->fields.field.present) { |
| 2541 | continue; |
| 2542 | } |
| 2543 | |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2544 | watermarks->high_wmark += zone->max_protection + zone->fields.field.high; |
| 2545 | watermarks->low_wmark += zone->max_protection + zone->fields.field.low; |
| 2546 | watermarks->min_wmark += zone->max_protection + zone->fields.field.min; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2547 | } |
| 2548 | } |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
Suren Baghdasaryan | 51ee4c5 | 2020-04-21 12:27:01 -0700 | [diff] [blame] | 2551 | static int calc_swap_utilization(union meminfo *mi) { |
| 2552 | int64_t swap_used = mi->field.total_swap - mi->field.free_swap; |
| 2553 | int64_t total_swappable = mi->field.active_anon + mi->field.inactive_anon + |
| 2554 | mi->field.shmem + swap_used; |
| 2555 | return total_swappable > 0 ? (swap_used * 100) / total_swappable : 0; |
| 2556 | } |
| 2557 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2558 | static void mp_event_psi(int data, uint32_t events, struct polling_params *poll_params) { |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2559 | enum reclaim_state { |
| 2560 | NO_RECLAIM = 0, |
| 2561 | KSWAPD_RECLAIM, |
| 2562 | DIRECT_RECLAIM, |
| 2563 | }; |
| 2564 | static int64_t init_ws_refault; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2565 | static int64_t prev_workingset_refault; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2566 | static int64_t base_file_lru; |
| 2567 | static int64_t init_pgscan_kswapd; |
| 2568 | static int64_t init_pgscan_direct; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2569 | static bool killing; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2570 | static int thrashing_limit = thrashing_limit_pct; |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2571 | static struct zone_watermarks watermarks; |
| 2572 | static struct timespec wmark_update_tm; |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2573 | static struct wakeup_info wi; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2574 | static struct timespec thrashing_reset_tm; |
| 2575 | static int64_t prev_thrash_growth = 0; |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2576 | static bool check_filecache = false; |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2577 | static int max_thrashing = 0; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2578 | |
| 2579 | union meminfo mi; |
| 2580 | union vmstat vs; |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 2581 | struct psi_data psi_data; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2582 | struct timespec curr_tm; |
| 2583 | int64_t thrashing = 0; |
| 2584 | bool swap_is_low = false; |
| 2585 | enum vmpressure_level level = (enum vmpressure_level)data; |
| 2586 | enum kill_reasons kill_reason = NONE; |
| 2587 | bool cycle_after_kill = false; |
| 2588 | enum reclaim_state reclaim = NO_RECLAIM; |
| 2589 | enum zone_watermark wmark = WMARK_NONE; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2590 | char kill_desc[LINE_MAX]; |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 2591 | bool cut_thrashing_limit = false; |
| 2592 | int min_score_adj = 0; |
Suren Baghdasaryan | 51ee4c5 | 2020-04-21 12:27:01 -0700 | [diff] [blame] | 2593 | int swap_util = 0; |
Suren Baghdasaryan | 6e6d14b | 2021-10-28 13:30:08 -0700 | [diff] [blame] | 2594 | int64_t swap_low_threshold; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2595 | long since_thrashing_reset_ms; |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2596 | int64_t workingset_refault_file; |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 2597 | bool critical_stall = false; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2598 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2599 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { |
| 2600 | ALOGE("Failed to get current time"); |
| 2601 | return; |
| 2602 | } |
| 2603 | |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2604 | record_wakeup_time(&curr_tm, events ? Event : Polling, &wi); |
| 2605 | |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 2606 | bool kill_pending = is_kill_pending(); |
Suren Baghdasaryan | ed715a3 | 2020-05-11 16:31:57 -0700 | [diff] [blame] | 2607 | if (kill_pending && (kill_timeout_ms == 0 || |
| 2608 | get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms))) { |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 2609 | /* Skip while still killing a process */ |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2610 | wi.skipped_wakeups++; |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 2611 | goto no_kill; |
| 2612 | } |
| 2613 | /* |
| 2614 | * Process is dead or kill timeout is over, stop waiting. This has no effect if pidfds are |
| 2615 | * supported and death notification already caused waiting to stop. |
| 2616 | */ |
| 2617 | stop_wait_for_proc_kill(!kill_pending); |
| 2618 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2619 | if (vmstat_parse(&vs) < 0) { |
| 2620 | ALOGE("Failed to parse vmstat!"); |
| 2621 | return; |
| 2622 | } |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2623 | /* Starting 5.9 kernel workingset_refault vmstat field was renamed workingset_refault_file */ |
| 2624 | workingset_refault_file = vs.field.workingset_refault ? : vs.field.workingset_refault_file; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2625 | |
| 2626 | if (meminfo_parse(&mi) < 0) { |
| 2627 | ALOGE("Failed to parse meminfo!"); |
| 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | /* Reset states after process got killed */ |
| 2632 | if (killing) { |
| 2633 | killing = false; |
| 2634 | cycle_after_kill = true; |
| 2635 | /* Reset file-backed pagecache size and refault amounts after a kill */ |
| 2636 | base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file; |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2637 | init_ws_refault = workingset_refault_file; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2638 | thrashing_reset_tm = curr_tm; |
| 2639 | prev_thrash_growth = 0; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | /* Check free swap levels */ |
| 2643 | if (swap_free_low_percentage) { |
Suren Baghdasaryan | 6e6d14b | 2021-10-28 13:30:08 -0700 | [diff] [blame] | 2644 | swap_low_threshold = mi.field.total_swap * swap_free_low_percentage / 100; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2645 | swap_is_low = mi.field.free_swap < swap_low_threshold; |
Suren Baghdasaryan | 6e6d14b | 2021-10-28 13:30:08 -0700 | [diff] [blame] | 2646 | } else { |
| 2647 | swap_low_threshold = 0; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | /* Identify reclaim state */ |
| 2651 | if (vs.field.pgscan_direct > init_pgscan_direct) { |
| 2652 | init_pgscan_direct = vs.field.pgscan_direct; |
| 2653 | init_pgscan_kswapd = vs.field.pgscan_kswapd; |
| 2654 | reclaim = DIRECT_RECLAIM; |
| 2655 | } else if (vs.field.pgscan_kswapd > init_pgscan_kswapd) { |
| 2656 | init_pgscan_kswapd = vs.field.pgscan_kswapd; |
| 2657 | reclaim = KSWAPD_RECLAIM; |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2658 | } else if (workingset_refault_file == prev_workingset_refault) { |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2659 | /* |
| 2660 | * Device is not thrashing and not reclaiming, bail out early until we see these stats |
| 2661 | * changing |
| 2662 | */ |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2663 | goto no_kill; |
| 2664 | } |
| 2665 | |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2666 | prev_workingset_refault = workingset_refault_file; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2667 | |
| 2668 | /* |
| 2669 | * It's possible we fail to find an eligible process to kill (ex. no process is |
| 2670 | * above oom_adj_min). When this happens, we should retry to find a new process |
| 2671 | * for a kill whenever a new eligible process is available. This is especially |
| 2672 | * important for a slow growing refault case. While retrying, we should keep |
| 2673 | * monitoring new thrashing counter as someone could release the memory to mitigate |
| 2674 | * the thrashing. Thus, when thrashing reset window comes, we decay the prev thrashing |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2675 | * counter by window counts. If the counter is still greater than thrashing limit, |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2676 | * we preserve the current prev_thrash counter so we will retry kill again. Otherwise, |
| 2677 | * we reset the prev_thrash counter so we will stop retrying. |
| 2678 | */ |
| 2679 | since_thrashing_reset_ms = get_time_diff_ms(&thrashing_reset_tm, &curr_tm); |
| 2680 | if (since_thrashing_reset_ms > THRASHING_RESET_INTERVAL_MS) { |
| 2681 | long windows_passed; |
| 2682 | /* Calculate prev_thrash_growth if we crossed THRASHING_RESET_INTERVAL_MS */ |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2683 | prev_thrash_growth = (workingset_refault_file - init_ws_refault) * 100 |
Martin Liu | c310841 | 2020-09-03 22:12:14 +0800 | [diff] [blame] | 2684 | / (base_file_lru + 1); |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2685 | windows_passed = (since_thrashing_reset_ms / THRASHING_RESET_INTERVAL_MS); |
| 2686 | /* |
| 2687 | * Decay prev_thrashing unless over-the-limit thrashing was registered in the window we |
| 2688 | * just crossed, which means there were no eligible processes to kill. We preserve the |
| 2689 | * counter in that case to ensure a kill if a new eligible process appears. |
| 2690 | */ |
| 2691 | if (windows_passed > 1 || prev_thrash_growth < thrashing_limit) { |
| 2692 | prev_thrash_growth >>= windows_passed; |
| 2693 | } |
| 2694 | |
| 2695 | /* Record file-backed pagecache size when crossing THRASHING_RESET_INTERVAL_MS */ |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2696 | base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file; |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2697 | init_ws_refault = workingset_refault_file; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2698 | thrashing_reset_tm = curr_tm; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2699 | thrashing_limit = thrashing_limit_pct; |
| 2700 | } else { |
| 2701 | /* Calculate what % of the file-backed pagecache refaulted so far */ |
Suren Baghdasaryan | dc60f97 | 2020-12-14 13:38:48 -0800 | [diff] [blame] | 2702 | thrashing = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2703 | } |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2704 | /* Add previous cycle's decayed thrashing amount */ |
| 2705 | thrashing += prev_thrash_growth; |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2706 | if (max_thrashing < thrashing) { |
| 2707 | max_thrashing = thrashing; |
| 2708 | } |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2709 | |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2710 | /* |
| 2711 | * Refresh watermarks once per min in case user updated one of the margins. |
| 2712 | * TODO: b/140521024 replace this periodic update with an API for AMS to notify LMKD |
| 2713 | * that zone watermarks were changed by the system software. |
| 2714 | */ |
| 2715 | if (watermarks.high_wmark == 0 || get_time_diff_ms(&wmark_update_tm, &curr_tm) > 60000) { |
| 2716 | struct zoneinfo zi; |
| 2717 | |
| 2718 | if (zoneinfo_parse(&zi) < 0) { |
| 2719 | ALOGE("Failed to parse zoneinfo!"); |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | calc_zone_watermarks(&zi, &watermarks); |
| 2724 | wmark_update_tm = curr_tm; |
Martin Liu | 1f72f5f | 2020-08-21 13:18:50 +0800 | [diff] [blame] | 2725 | } |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2726 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2727 | /* Find out which watermark is breached if any */ |
Suren Baghdasaryan | 81c75b2 | 2019-08-14 09:48:35 -0700 | [diff] [blame] | 2728 | wmark = get_lowest_watermark(&mi, &watermarks); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2729 | |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 2730 | if (!psi_parse_mem(&psi_data)) { |
| 2731 | critical_stall = psi_data.mem_stats[PSI_FULL].avg10 > (float)stall_limit_critical; |
| 2732 | } |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2733 | /* |
| 2734 | * TODO: move this logic into a separate function |
| 2735 | * Decide if killing a process is necessary and record the reason |
| 2736 | */ |
| 2737 | if (cycle_after_kill && wmark < WMARK_LOW) { |
| 2738 | /* |
| 2739 | * Prevent kills not freeing enough memory which might lead to OOM kill. |
| 2740 | * This might happen when a process is consuming memory faster than reclaim can |
| 2741 | * free even after a kill. Mostly happens when running memory stress tests. |
| 2742 | */ |
| 2743 | kill_reason = PRESSURE_AFTER_KILL; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2744 | strncpy(kill_desc, "min watermark is breached even after kill", sizeof(kill_desc)); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2745 | } else if (level == VMPRESS_LEVEL_CRITICAL && events != 0) { |
| 2746 | /* |
| 2747 | * Device is too busy reclaiming memory which might lead to ANR. |
| 2748 | * Critical level is triggered when PSI complete stall (all tasks are blocked because |
| 2749 | * of the memory congestion) breaches the configured threshold. |
| 2750 | */ |
| 2751 | kill_reason = NOT_RESPONDING; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2752 | strncpy(kill_desc, "device is not responding", sizeof(kill_desc)); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2753 | } else if (swap_is_low && thrashing > thrashing_limit_pct) { |
| 2754 | /* Page cache is thrashing while swap is low */ |
| 2755 | kill_reason = LOW_SWAP_AND_THRASHING; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2756 | snprintf(kill_desc, sizeof(kill_desc), "device is low on swap (%" PRId64 |
| 2757 | "kB < %" PRId64 "kB) and thrashing (%" PRId64 "%%)", |
| 2758 | mi.field.free_swap * page_k, swap_low_threshold * page_k, thrashing); |
Suren Baghdasaryan | 0142b3c | 2021-03-03 11:11:09 -0800 | [diff] [blame] | 2759 | /* Do not kill perceptible apps unless below min watermark or heavily thrashing */ |
| 2760 | if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) { |
Suren Baghdasaryan | 48135c4 | 2020-05-19 12:59:18 -0700 | [diff] [blame] | 2761 | min_score_adj = PERCEPTIBLE_APP_ADJ + 1; |
| 2762 | } |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2763 | check_filecache = true; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2764 | } else if (swap_is_low && wmark < WMARK_HIGH) { |
| 2765 | /* Both free memory and swap are low */ |
| 2766 | kill_reason = LOW_MEM_AND_SWAP; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2767 | snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap is low (%" |
Suren Baghdasaryan | 2367818 | 2021-03-02 18:33:09 -0800 | [diff] [blame] | 2768 | PRId64 "kB < %" PRId64 "kB)", wmark < WMARK_LOW ? "min" : "low", |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2769 | mi.field.free_swap * page_k, swap_low_threshold * page_k); |
Suren Baghdasaryan | 0142b3c | 2021-03-03 11:11:09 -0800 | [diff] [blame] | 2770 | /* Do not kill perceptible apps unless below min watermark or heavily thrashing */ |
| 2771 | if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) { |
Suren Baghdasaryan | 48135c4 | 2020-05-19 12:59:18 -0700 | [diff] [blame] | 2772 | min_score_adj = PERCEPTIBLE_APP_ADJ + 1; |
| 2773 | } |
Suren Baghdasaryan | 51ee4c5 | 2020-04-21 12:27:01 -0700 | [diff] [blame] | 2774 | } else if (wmark < WMARK_HIGH && swap_util_max < 100 && |
| 2775 | (swap_util = calc_swap_utilization(&mi)) > swap_util_max) { |
| 2776 | /* |
| 2777 | * Too much anon memory is swapped out but swap is not low. |
| 2778 | * Non-swappable allocations created memory pressure. |
| 2779 | */ |
| 2780 | kill_reason = LOW_MEM_AND_SWAP_UTIL; |
| 2781 | snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap utilization" |
Suren Baghdasaryan | 2367818 | 2021-03-02 18:33:09 -0800 | [diff] [blame] | 2782 | " is high (%d%% > %d%%)", wmark < WMARK_LOW ? "min" : "low", |
Suren Baghdasaryan | 51ee4c5 | 2020-04-21 12:27:01 -0700 | [diff] [blame] | 2783 | swap_util, swap_util_max); |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2784 | } else if (wmark < WMARK_HIGH && thrashing > thrashing_limit) { |
| 2785 | /* Page cache is thrashing while memory is low */ |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2786 | kill_reason = LOW_MEM_AND_THRASHING; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2787 | snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and thrashing (%" |
Suren Baghdasaryan | 2367818 | 2021-03-02 18:33:09 -0800 | [diff] [blame] | 2788 | PRId64 "%%)", wmark < WMARK_LOW ? "min" : "low", thrashing); |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 2789 | cut_thrashing_limit = true; |
Suren Baghdasaryan | 0142b3c | 2021-03-03 11:11:09 -0800 | [diff] [blame] | 2790 | /* Do not kill perceptible apps unless thrashing at critical levels */ |
| 2791 | if (thrashing < thrashing_critical_pct) { |
| 2792 | min_score_adj = PERCEPTIBLE_APP_ADJ + 1; |
| 2793 | } |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2794 | check_filecache = true; |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2795 | } else if (reclaim == DIRECT_RECLAIM && thrashing > thrashing_limit) { |
| 2796 | /* Page cache is thrashing while in direct reclaim (mostly happens on lowram devices) */ |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2797 | kill_reason = DIRECT_RECL_AND_THRASHING; |
Suren Baghdasaryan | 89454e6 | 2019-07-15 15:50:17 -0700 | [diff] [blame] | 2798 | snprintf(kill_desc, sizeof(kill_desc), "device is in direct reclaim and thrashing (%" |
| 2799 | PRId64 "%%)", thrashing); |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 2800 | cut_thrashing_limit = true; |
Suren Baghdasaryan | 0142b3c | 2021-03-03 11:11:09 -0800 | [diff] [blame] | 2801 | /* Do not kill perceptible apps unless thrashing at critical levels */ |
| 2802 | if (thrashing < thrashing_critical_pct) { |
| 2803 | min_score_adj = PERCEPTIBLE_APP_ADJ + 1; |
| 2804 | } |
Suren Baghdasaryan | 11221d4 | 2021-07-14 17:57:52 -0700 | [diff] [blame] | 2805 | check_filecache = true; |
| 2806 | } else if (check_filecache) { |
| 2807 | int64_t file_lru_kb = (vs.field.nr_inactive_file + vs.field.nr_active_file) * page_k; |
| 2808 | |
| 2809 | if (file_lru_kb < filecache_min_kb) { |
| 2810 | /* File cache is too low after thrashing, keep killing background processes */ |
| 2811 | kill_reason = LOW_FILECACHE_AFTER_THRASHING; |
| 2812 | snprintf(kill_desc, sizeof(kill_desc), |
| 2813 | "filecache is low (%" PRId64 "kB < %" PRId64 "kB) after thrashing", |
| 2814 | file_lru_kb, filecache_min_kb); |
| 2815 | min_score_adj = PERCEPTIBLE_APP_ADJ + 1; |
| 2816 | } else { |
| 2817 | /* File cache is big enough, stop checking */ |
| 2818 | check_filecache = false; |
| 2819 | } |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | /* Kill a process if necessary */ |
| 2823 | if (kill_reason != NONE) { |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2824 | struct kill_info ki = { |
| 2825 | .kill_reason = kill_reason, |
| 2826 | .kill_desc = kill_desc, |
| 2827 | .thrashing = (int)thrashing, |
| 2828 | .max_thrashing = max_thrashing, |
| 2829 | }; |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 2830 | |
| 2831 | /* Allow killing perceptible apps if the system is stalled */ |
| 2832 | if (critical_stall) { |
| 2833 | min_score_adj = 0; |
| 2834 | } |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 2835 | psi_parse_io(&psi_data); |
| 2836 | psi_parse_cpu(&psi_data); |
| 2837 | int pages_freed = find_and_kill_process(min_score_adj, &ki, &mi, &wi, &curr_tm, &psi_data); |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 2838 | if (pages_freed > 0) { |
| 2839 | killing = true; |
Suren Baghdasaryan | e160475 | 2021-07-22 16:21:21 -0700 | [diff] [blame] | 2840 | max_thrashing = 0; |
Suren Baghdasaryan | 970a26a | 2019-09-19 15:27:21 -0700 | [diff] [blame] | 2841 | if (cut_thrashing_limit) { |
| 2842 | /* |
| 2843 | * Cut thrasing limit by thrashing_limit_decay_pct percentage of the current |
| 2844 | * thrashing limit until the system stops thrashing. |
| 2845 | */ |
| 2846 | thrashing_limit = (thrashing_limit * (100 - thrashing_limit_decay_pct)) / 100; |
| 2847 | } |
| 2848 | } |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | no_kill: |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2852 | /* Do not poll if kernel supports pidfd waiting */ |
| 2853 | if (is_waiting_for_kill()) { |
| 2854 | /* Pause polling if we are waiting for process death notification */ |
| 2855 | poll_params->update = POLLING_PAUSE; |
| 2856 | return; |
| 2857 | } |
| 2858 | |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2859 | /* |
| 2860 | * Start polling after initial PSI event; |
| 2861 | * extend polling while device is in direct reclaim or process is being killed; |
| 2862 | * do not extend when kswapd reclaims because that might go on for a long time |
| 2863 | * without causing memory pressure |
| 2864 | */ |
| 2865 | if (events || killing || reclaim == DIRECT_RECLAIM) { |
| 2866 | poll_params->update = POLLING_START; |
| 2867 | } |
| 2868 | |
| 2869 | /* Decide the polling interval */ |
| 2870 | if (swap_is_low || killing) { |
| 2871 | /* Fast polling during and after a kill or when swap is low */ |
| 2872 | poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS; |
| 2873 | } else { |
| 2874 | /* By default use long intervals */ |
| 2875 | poll_params->polling_interval_ms = PSI_POLL_PERIOD_LONG_MS; |
| 2876 | } |
| 2877 | } |
| 2878 | |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 2879 | static std::string GetCgroupAttributePath(const char* attr) { |
| 2880 | std::string path; |
| 2881 | if (!CgroupGetAttributePath(attr, &path)) { |
| 2882 | ALOGE("Unknown cgroup attribute %s", attr); |
| 2883 | } |
| 2884 | return path; |
| 2885 | } |
| 2886 | |
| 2887 | // The implementation of this function relies on memcg statistics that are only available in the |
| 2888 | // v1 cgroup hierarchy. |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 2889 | static void mp_event_common(int data, uint32_t events, struct polling_params *poll_params) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2890 | unsigned long long evcount; |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 2891 | int64_t mem_usage, memsw_usage; |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 2892 | int64_t mem_pressure; |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 2893 | union meminfo mi; |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 2894 | struct zoneinfo zi; |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 2895 | struct timespec curr_tm; |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 2896 | static unsigned long kill_skip_count = 0; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2897 | enum vmpressure_level level = (enum vmpressure_level)data; |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2898 | long other_free = 0, other_file = 0; |
| 2899 | int min_score_adj; |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2900 | int minfree = 0; |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 2901 | static const std::string mem_usage_path = GetCgroupAttributePath("MemUsage"); |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2902 | static struct reread_data mem_usage_file_data = { |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 2903 | .filename = mem_usage_path.c_str(), |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2904 | .fd = -1, |
| 2905 | }; |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 2906 | static const std::string memsw_usage_path = GetCgroupAttributePath("MemAndSwapUsage"); |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2907 | static struct reread_data memsw_usage_file_data = { |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 2908 | .filename = memsw_usage_path.c_str(), |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 2909 | .fd = -1, |
| 2910 | }; |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2911 | static struct wakeup_info wi; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2912 | |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2913 | if (debug_process_killing) { |
| 2914 | ALOGI("%s memory pressure event is triggered", level_name[level]); |
| 2915 | } |
| 2916 | |
| 2917 | if (!use_psi_monitors) { |
| 2918 | /* |
| 2919 | * Check all event counters from low to critical |
| 2920 | * and upgrade to the highest priority one. By reading |
| 2921 | * eventfd we also reset the event counters. |
| 2922 | */ |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 2923 | for (int lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2924 | if (mpevfd[lvl] != -1 && |
| 2925 | TEMP_FAILURE_RETRY(read(mpevfd[lvl], |
| 2926 | &evcount, sizeof(evcount))) > 0 && |
| 2927 | evcount > 0 && lvl > level) { |
Tom Cherry | 43f3d2b | 2019-12-04 12:46:57 -0800 | [diff] [blame] | 2928 | level = static_cast<vmpressure_level>(lvl); |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2929 | } |
Suren Baghdasaryan | 3e1a849 | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 2930 | } |
| 2931 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2932 | |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 2933 | /* Start polling after initial PSI event */ |
| 2934 | if (use_psi_monitors && events) { |
| 2935 | /* Override polling params only if current event is more critical */ |
| 2936 | if (!poll_params->poll_handler || data > poll_params->poll_handler->data) { |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 2937 | poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 2938 | poll_params->update = POLLING_START; |
| 2939 | } |
| 2940 | } |
| 2941 | |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 2942 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { |
| 2943 | ALOGE("Failed to get current time"); |
| 2944 | return; |
| 2945 | } |
| 2946 | |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2947 | record_wakeup_time(&curr_tm, events ? Event : Polling, &wi); |
| 2948 | |
Suren Baghdasaryan | ed715a3 | 2020-05-11 16:31:57 -0700 | [diff] [blame] | 2949 | if (kill_timeout_ms && |
| 2950 | get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms)) { |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2951 | /* |
| 2952 | * If we're within the no-kill timeout, see if there's pending reclaim work |
| 2953 | * from the last killed process. If so, skip killing for now. |
| 2954 | */ |
| 2955 | if (is_kill_pending()) { |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 2956 | kill_skip_count++; |
Suren Baghdasaryan | d7b4fcb | 2020-07-23 18:00:43 -0700 | [diff] [blame] | 2957 | wi.skipped_wakeups++; |
Suren Baghdasaryan | 30854e7 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 2958 | return; |
| 2959 | } |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 2960 | /* |
| 2961 | * Process is dead, stop waiting. This has no effect if pidfds are supported and |
| 2962 | * death notification already caused waiting to stop. |
| 2963 | */ |
| 2964 | stop_wait_for_proc_kill(true); |
| 2965 | } else { |
| 2966 | /* |
| 2967 | * Killing took longer than no-kill timeout. Stop waiting for the last process |
| 2968 | * to die because we are ready to kill again. |
| 2969 | */ |
| 2970 | stop_wait_for_proc_kill(false); |
Suren Baghdasaryan | 30854e7 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 2971 | } |
| 2972 | |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 2973 | if (kill_skip_count > 0) { |
Suren Baghdasaryan | eff8233 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 2974 | ALOGI("%lu memory pressure events were skipped after a kill!", |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 2975 | kill_skip_count); |
| 2976 | kill_skip_count = 0; |
Suren Baghdasaryan | 30854e7 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 2977 | } |
| 2978 | |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2979 | if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) { |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 2980 | ALOGE("Failed to get free memory!"); |
| 2981 | return; |
| 2982 | } |
| 2983 | |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2984 | if (use_minfree_levels) { |
| 2985 | int i; |
| 2986 | |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 2987 | other_free = mi.field.nr_free_pages - zi.totalreserve_pages; |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2988 | if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) { |
| 2989 | other_file = (mi.field.nr_file_pages - mi.field.shmem - |
| 2990 | mi.field.unevictable - mi.field.swap_cached); |
| 2991 | } else { |
| 2992 | other_file = 0; |
| 2993 | } |
| 2994 | |
| 2995 | min_score_adj = OOM_SCORE_ADJ_MAX + 1; |
| 2996 | for (i = 0; i < lowmem_targets_size; i++) { |
| 2997 | minfree = lowmem_minfree[i]; |
| 2998 | if (other_free < minfree && other_file < minfree) { |
| 2999 | min_score_adj = lowmem_adj[i]; |
| 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 3004 | if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { |
liuhailong | cf8af50 | 2021-12-04 16:29:52 +0800 | [diff] [blame] | 3005 | if (debug_process_killing && lowmem_targets_size) { |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 3006 | ALOGI("Ignore %s memory pressure event " |
| 3007 | "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)", |
| 3008 | level_name[level], other_free * page_k, other_file * page_k, |
| 3009 | (long)lowmem_minfree[lowmem_targets_size - 1] * page_k); |
| 3010 | } |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3011 | return; |
Suren Baghdasaryan | b0bda55 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 3012 | } |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3013 | |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3014 | goto do_kill; |
| 3015 | } |
| 3016 | |
Suren Baghdasaryan | e0f3dd1 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 3017 | if (level == VMPRESS_LEVEL_LOW) { |
| 3018 | record_low_pressure_levels(&mi); |
| 3019 | } |
| 3020 | |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 3021 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 3022 | /* Do not monitor this pressure level */ |
| 3023 | return; |
| 3024 | } |
| 3025 | |
Suren Baghdasaryan | 8796674 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 3026 | if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) { |
| 3027 | goto do_kill; |
| 3028 | } |
| 3029 | if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) { |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3030 | goto do_kill; |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 3031 | } |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 3032 | |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 3033 | // Calculate percent for swappinness. |
| 3034 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 3035 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3036 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 3037 | // We are swapping too much. |
| 3038 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3039 | level = upgrade_level(level); |
| 3040 | if (debug_process_killing) { |
| 3041 | ALOGI("Event upgraded to %s", level_name[level]); |
| 3042 | } |
Robert Benea | c72b293 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
Vic Yang | 6568069 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 3046 | // If we still have enough swap space available, check if we want to |
| 3047 | // ignore/downgrade pressure events. |
| 3048 | if (mi.field.free_swap >= |
| 3049 | mi.field.total_swap * swap_free_low_percentage / 100) { |
| 3050 | // If the pressure is larger than downgrade_pressure lmk will not |
| 3051 | // kill any process, since enough memory is available. |
| 3052 | if (mem_pressure > downgrade_pressure) { |
| 3053 | if (debug_process_killing) { |
| 3054 | ALOGI("Ignore %s memory pressure", level_name[level]); |
| 3055 | } |
| 3056 | return; |
| 3057 | } else if (level == VMPRESS_LEVEL_CRITICAL && mem_pressure > upgrade_pressure) { |
| 3058 | if (debug_process_killing) { |
| 3059 | ALOGI("Downgrade critical memory pressure"); |
| 3060 | } |
| 3061 | // Downgrade event, since enough memory available. |
| 3062 | level = downgrade_level(level); |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 3063 | } |
Robert Benea | 3be1614 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3066 | do_kill: |
Suren Baghdasaryan | d1d59f8 | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 3067 | if (low_ram_device) { |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 3068 | /* For Go devices kill only one task */ |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 3069 | if (find_and_kill_process(level_oomadj[level], NULL, &mi, &wi, &curr_tm, NULL) == 0) { |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 3070 | if (debug_process_killing) { |
| 3071 | ALOGI("Nothing to kill"); |
| 3072 | } |
| 3073 | } |
| 3074 | } else { |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3075 | int pages_freed; |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3076 | static struct timespec last_report_tm; |
| 3077 | static unsigned long report_skip_count = 0; |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3078 | |
| 3079 | if (!use_minfree_levels) { |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3080 | /* Free up enough memory to downgrate the memory pressure to low level */ |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 3081 | if (mi.field.nr_free_pages >= low_pressure_mem.max_nr_free_pages) { |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3082 | if (debug_process_killing) { |
| 3083 | ALOGI("Ignoring pressure since more memory is " |
| 3084 | "available (%" PRId64 ") than watermark (%" PRId64 ")", |
| 3085 | mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages); |
| 3086 | } |
| 3087 | return; |
| 3088 | } |
| 3089 | min_score_adj = level_oomadj[level]; |
Suren Baghdasaryan | 94ccd72 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 3090 | } |
| 3091 | |
Suren Baghdasaryan | 014dd71 | 2022-02-18 12:51:15 -0800 | [diff] [blame] | 3092 | pages_freed = find_and_kill_process(min_score_adj, NULL, &mi, &wi, &curr_tm, NULL); |
Suren Baghdasaryan | eff8233 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 3093 | |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3094 | if (pages_freed == 0) { |
| 3095 | /* Rate limit kill reports when nothing was reclaimed */ |
| 3096 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < FAIL_REPORT_RLIMIT_MS) { |
| 3097 | report_skip_count++; |
Suren Baghdasaryan | 1ed0db1 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 3098 | return; |
| 3099 | } |
Robert Benea | 7f68a3f | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 3100 | } |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3101 | |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 3102 | /* Log whenever we kill or when report rate limit allows */ |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3103 | if (use_minfree_levels) { |
Chris Morin | 74b4df9 | 2021-02-26 00:00:35 -0800 | [diff] [blame] | 3104 | ALOGI("Reclaimed %ldkB, cache(%ldkB) and free(%" PRId64 "kB)-reserved(%" PRId64 "kB) " |
| 3105 | "below min(%ldkB) for oom_score_adj %d", |
Suren Baghdasaryan | 85c31b5 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 3106 | pages_freed * page_k, |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3107 | other_file * page_k, mi.field.nr_free_pages * page_k, |
Suren Baghdasaryan | 92d0eec | 2019-07-15 13:54:20 -0700 | [diff] [blame] | 3108 | zi.totalreserve_pages * page_k, |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3109 | minfree * page_k, min_score_adj); |
| 3110 | } else { |
Chris Morin | 74b4df9 | 2021-02-26 00:00:35 -0800 | [diff] [blame] | 3111 | ALOGI("Reclaimed %ldkB at oom_score_adj %d", pages_freed * page_k, min_score_adj); |
Suren Baghdasaryan | 53be36e | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | if (report_skip_count > 0) { |
| 3115 | ALOGI("Suppressed %lu failed kill reports", report_skip_count); |
| 3116 | report_skip_count = 0; |
| 3117 | } |
| 3118 | |
| 3119 | last_report_tm = curr_tm; |
Colin Cross | 01db271 | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 3120 | } |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3121 | if (is_waiting_for_kill()) { |
| 3122 | /* pause polling if we are waiting for process death notification */ |
| 3123 | poll_params->update = POLLING_PAUSE; |
| 3124 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3127 | static bool init_mp_psi(enum vmpressure_level level, bool use_new_strategy) { |
| 3128 | int fd; |
| 3129 | |
| 3130 | /* Do not register a handler if threshold_ms is not set */ |
| 3131 | if (!psi_thresholds[level].threshold_ms) { |
| 3132 | return true; |
| 3133 | } |
| 3134 | |
| 3135 | fd = init_psi_monitor(psi_thresholds[level].stall_type, |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3136 | psi_thresholds[level].threshold_ms * US_PER_MS, |
| 3137 | PSI_WINDOW_SIZE_MS * US_PER_MS); |
| 3138 | |
| 3139 | if (fd < 0) { |
| 3140 | return false; |
| 3141 | } |
| 3142 | |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3143 | vmpressure_hinfo[level].handler = use_new_strategy ? mp_event_psi : mp_event_common; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3144 | vmpressure_hinfo[level].data = level; |
| 3145 | if (register_psi_monitor(epollfd, fd, &vmpressure_hinfo[level]) < 0) { |
| 3146 | destroy_psi_monitor(fd); |
| 3147 | return false; |
| 3148 | } |
| 3149 | maxevents++; |
| 3150 | mpevfd[level] = fd; |
| 3151 | |
| 3152 | return true; |
| 3153 | } |
| 3154 | |
| 3155 | static void destroy_mp_psi(enum vmpressure_level level) { |
| 3156 | int fd = mpevfd[level]; |
| 3157 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3158 | if (fd < 0) { |
| 3159 | return; |
| 3160 | } |
| 3161 | |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3162 | if (unregister_psi_monitor(epollfd, fd) < 0) { |
| 3163 | ALOGE("Failed to unregister psi monitor for %s memory pressure; errno=%d", |
| 3164 | level_name[level], errno); |
| 3165 | } |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3166 | maxevents--; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3167 | destroy_psi_monitor(fd); |
| 3168 | mpevfd[level] = -1; |
| 3169 | } |
| 3170 | |
Bart Van Assche | 7599436 | 2022-02-15 23:45:04 +0000 | [diff] [blame] | 3171 | enum class MemcgVersion { |
| 3172 | kNotFound, |
| 3173 | kV1, |
| 3174 | kV2, |
| 3175 | }; |
| 3176 | |
| 3177 | static MemcgVersion __memcg_version() { |
| 3178 | std::string cgroupv2_path, memcg_path; |
| 3179 | |
| 3180 | if (!CgroupGetControllerPath("memory", &memcg_path)) { |
| 3181 | return MemcgVersion::kNotFound; |
| 3182 | } |
| 3183 | return CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroupv2_path) && |
| 3184 | cgroupv2_path == memcg_path |
| 3185 | ? MemcgVersion::kV2 |
| 3186 | : MemcgVersion::kV1; |
| 3187 | } |
| 3188 | |
| 3189 | static MemcgVersion memcg_version() { |
| 3190 | static MemcgVersion version = __memcg_version(); |
| 3191 | |
| 3192 | return version; |
| 3193 | } |
| 3194 | |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3195 | static bool init_psi_monitors() { |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3196 | /* |
| 3197 | * When PSI is used on low-ram devices or on high-end devices without memfree levels |
Bart Van Assche | 7599436 | 2022-02-15 23:45:04 +0000 | [diff] [blame] | 3198 | * use new kill strategy based on zone watermarks, free swap and thrashing stats. |
| 3199 | * Also use the new strategy if memcg has not been mounted in the v1 cgroups hiearchy since |
| 3200 | * the old strategy relies on memcg attributes that are available only in the v1 cgroups |
| 3201 | * hiearchy. |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3202 | */ |
| 3203 | bool use_new_strategy = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3204 | GET_LMK_PROPERTY(bool, "use_new_strategy", low_ram_device || !use_minfree_levels); |
Bart Van Assche | 7599436 | 2022-02-15 23:45:04 +0000 | [diff] [blame] | 3205 | if (!use_new_strategy && memcg_version() != MemcgVersion::kV1) { |
| 3206 | ALOGE("Old kill strategy can only be used with v1 cgroup hierarchy"); |
| 3207 | return false; |
| 3208 | } |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3209 | /* In default PSI mode override stall amounts using system properties */ |
| 3210 | if (use_new_strategy) { |
| 3211 | /* Do not use low pressure level */ |
| 3212 | psi_thresholds[VMPRESS_LEVEL_LOW].threshold_ms = 0; |
| 3213 | psi_thresholds[VMPRESS_LEVEL_MEDIUM].threshold_ms = psi_partial_stall_ms; |
| 3214 | psi_thresholds[VMPRESS_LEVEL_CRITICAL].threshold_ms = psi_complete_stall_ms; |
| 3215 | } |
| 3216 | |
| 3217 | if (!init_mp_psi(VMPRESS_LEVEL_LOW, use_new_strategy)) { |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3218 | return false; |
| 3219 | } |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3220 | if (!init_mp_psi(VMPRESS_LEVEL_MEDIUM, use_new_strategy)) { |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3221 | destroy_mp_psi(VMPRESS_LEVEL_LOW); |
| 3222 | return false; |
| 3223 | } |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3224 | if (!init_mp_psi(VMPRESS_LEVEL_CRITICAL, use_new_strategy)) { |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3225 | destroy_mp_psi(VMPRESS_LEVEL_MEDIUM); |
| 3226 | destroy_mp_psi(VMPRESS_LEVEL_LOW); |
| 3227 | return false; |
| 3228 | } |
| 3229 | return true; |
| 3230 | } |
| 3231 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3232 | static bool init_mp_common(enum vmpressure_level level) { |
Bart Van Assche | 7599436 | 2022-02-15 23:45:04 +0000 | [diff] [blame] | 3233 | // The implementation of this function relies on memcg statistics that are only available in the |
| 3234 | // v1 cgroup hierarchy. |
| 3235 | if (memcg_version() != MemcgVersion::kV1) { |
| 3236 | ALOGE("%s: global monitoring is only available for the v1 cgroup hierarchy", __func__); |
| 3237 | return false; |
| 3238 | } |
| 3239 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3240 | int mpfd; |
| 3241 | int evfd; |
| 3242 | int evctlfd; |
| 3243 | char buf[256]; |
| 3244 | struct epoll_event epev; |
| 3245 | int ret; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3246 | int level_idx = (int)level; |
| 3247 | const char *levelstr = level_name[level_idx]; |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3248 | |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 3249 | /* gid containing AID_SYSTEM required */ |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 3250 | mpfd = open(GetCgroupAttributePath("MemPressureLevel").c_str(), O_RDONLY | O_CLOEXEC); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3251 | if (mpfd < 0) { |
| 3252 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 3253 | goto err_open_mpfd; |
| 3254 | } |
| 3255 | |
Bart Van Assche | b4d26bb | 2022-02-17 21:31:51 +0000 | [diff] [blame] | 3256 | evctlfd = open(GetCgroupAttributePath("CgroupEventControl").c_str(), O_WRONLY | O_CLOEXEC); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3257 | if (evctlfd < 0) { |
| 3258 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 3259 | goto err_open_evctlfd; |
| 3260 | } |
| 3261 | |
Nick Kralevich | 148d8dd | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 3262 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3263 | if (evfd < 0) { |
| 3264 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 3265 | goto err_eventfd; |
| 3266 | } |
| 3267 | |
| 3268 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 3269 | if (ret >= (ssize_t)sizeof(buf)) { |
| 3270 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 3271 | goto err; |
| 3272 | } |
| 3273 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3274 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3275 | if (ret == -1) { |
| 3276 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 3277 | levelstr, errno); |
| 3278 | goto err; |
| 3279 | } |
| 3280 | |
| 3281 | epev.events = EPOLLIN; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3282 | /* use data to store event level */ |
| 3283 | vmpressure_hinfo[level_idx].data = level_idx; |
| 3284 | vmpressure_hinfo[level_idx].handler = mp_event_common; |
| 3285 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3286 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 3287 | if (ret == -1) { |
| 3288 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 3289 | goto err; |
| 3290 | } |
| 3291 | maxevents++; |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3292 | mpevfd[level] = evfd; |
Suren Baghdasaryan | ceffaf2 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 3293 | close(evctlfd); |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3294 | return true; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3295 | |
| 3296 | err: |
| 3297 | close(evfd); |
| 3298 | err_eventfd: |
| 3299 | close(evctlfd); |
| 3300 | err_open_evctlfd: |
| 3301 | close(mpfd); |
| 3302 | err_open_mpfd: |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3303 | return false; |
Robert Benea | 58d6a13 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3306 | static void destroy_mp_common(enum vmpressure_level level) { |
| 3307 | struct epoll_event epev; |
| 3308 | int fd = mpevfd[level]; |
| 3309 | |
| 3310 | if (fd < 0) { |
| 3311 | return; |
| 3312 | } |
| 3313 | |
| 3314 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &epev)) { |
| 3315 | // Log an error and keep going |
| 3316 | ALOGE("epoll_ctl for level %s failed; errno=%d", level_name[level], errno); |
| 3317 | } |
| 3318 | maxevents--; |
| 3319 | close(fd); |
| 3320 | mpevfd[level] = -1; |
| 3321 | } |
| 3322 | |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3323 | static void kernel_event_handler(int data __unused, uint32_t events __unused, |
| 3324 | struct polling_params *poll_params __unused) { |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3325 | poll_kernel(kpoll_fd); |
Jim Blackler | 700b719 | 2019-04-26 11:18:29 +0100 | [diff] [blame] | 3326 | } |
| 3327 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3328 | static bool init_monitors() { |
| 3329 | /* Try to use psi monitor first if kernel has it */ |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3330 | use_psi_monitors = GET_LMK_PROPERTY(bool, "use_psi", true) && |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3331 | init_psi_monitors(); |
| 3332 | /* Fall back to vmpressure */ |
| 3333 | if (!use_psi_monitors && |
| 3334 | (!init_mp_common(VMPRESS_LEVEL_LOW) || |
| 3335 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || |
| 3336 | !init_mp_common(VMPRESS_LEVEL_CRITICAL))) { |
| 3337 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
| 3338 | return false; |
| 3339 | } |
| 3340 | if (use_psi_monitors) { |
| 3341 | ALOGI("Using psi monitors for memory pressure detection"); |
| 3342 | } else { |
| 3343 | ALOGI("Using vmpressure for memory pressure detection"); |
| 3344 | } |
| 3345 | return true; |
| 3346 | } |
| 3347 | |
| 3348 | static void destroy_monitors() { |
| 3349 | if (use_psi_monitors) { |
| 3350 | destroy_mp_psi(VMPRESS_LEVEL_CRITICAL); |
| 3351 | destroy_mp_psi(VMPRESS_LEVEL_MEDIUM); |
| 3352 | destroy_mp_psi(VMPRESS_LEVEL_LOW); |
| 3353 | } else { |
| 3354 | destroy_mp_common(VMPRESS_LEVEL_CRITICAL); |
| 3355 | destroy_mp_common(VMPRESS_LEVEL_MEDIUM); |
| 3356 | destroy_mp_common(VMPRESS_LEVEL_LOW); |
| 3357 | } |
| 3358 | } |
| 3359 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 3360 | static void drop_reaper_comm() { |
| 3361 | close(reaper_comm_fd[0]); |
| 3362 | close(reaper_comm_fd[1]); |
| 3363 | } |
| 3364 | |
| 3365 | static bool setup_reaper_comm() { |
| 3366 | if (pipe(reaper_comm_fd)) { |
| 3367 | ALOGE("pipe failed: %s", strerror(errno)); |
| 3368 | return false; |
| 3369 | } |
| 3370 | |
| 3371 | // Ensure main thread never blocks on read |
| 3372 | int flags = fcntl(reaper_comm_fd[0], F_GETFL); |
| 3373 | if (fcntl(reaper_comm_fd[0], F_SETFL, flags | O_NONBLOCK)) { |
| 3374 | ALOGE("fcntl failed: %s", strerror(errno)); |
| 3375 | drop_reaper_comm(); |
| 3376 | return false; |
| 3377 | } |
| 3378 | |
| 3379 | return true; |
| 3380 | } |
| 3381 | |
| 3382 | static bool init_reaper() { |
| 3383 | if (!reaper.is_reaping_supported()) { |
| 3384 | ALOGI("Process reaping is not supported"); |
| 3385 | return false; |
| 3386 | } |
| 3387 | |
| 3388 | if (!setup_reaper_comm()) { |
| 3389 | ALOGE("Failed to create thread communication channel"); |
| 3390 | return false; |
| 3391 | } |
| 3392 | |
| 3393 | // Setup epoll handler |
| 3394 | struct epoll_event epev; |
| 3395 | static struct event_handler_info kill_failed_hinfo = { 0, kill_fail_handler }; |
| 3396 | epev.events = EPOLLIN; |
| 3397 | epev.data.ptr = (void *)&kill_failed_hinfo; |
| 3398 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, reaper_comm_fd[0], &epev)) { |
| 3399 | ALOGE("epoll_ctl failed: %s", strerror(errno)); |
| 3400 | drop_reaper_comm(); |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | if (!reaper.init(reaper_comm_fd[1])) { |
| 3405 | ALOGE("Failed to initialize reaper object"); |
| 3406 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, reaper_comm_fd[0], &epev)) { |
| 3407 | ALOGE("epoll_ctl failed: %s", strerror(errno)); |
| 3408 | } |
| 3409 | drop_reaper_comm(); |
| 3410 | return false; |
| 3411 | } |
| 3412 | maxevents++; |
| 3413 | |
| 3414 | return true; |
| 3415 | } |
| 3416 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3417 | static int init(void) { |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3418 | static struct event_handler_info kernel_poll_hinfo = { 0, kernel_event_handler }; |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 3419 | struct reread_data file_data = { |
| 3420 | .filename = ZONEINFO_PATH, |
| 3421 | .fd = -1, |
| 3422 | }; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3423 | struct epoll_event epev; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3424 | int pidfd; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3425 | int i; |
| 3426 | int ret; |
| 3427 | |
| 3428 | page_k = sysconf(_SC_PAGESIZE); |
| 3429 | if (page_k == -1) |
| 3430 | page_k = PAGE_SIZE; |
| 3431 | page_k /= 1024; |
| 3432 | |
| 3433 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 3434 | if (epollfd == -1) { |
| 3435 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 3436 | return -1; |
| 3437 | } |
| 3438 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3439 | // mark data connections as not connected |
| 3440 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 3441 | data_sock[i].sock = -1; |
| 3442 | } |
| 3443 | |
| 3444 | ctrl_sock.sock = android_get_control_socket("lmkd"); |
| 3445 | if (ctrl_sock.sock < 0) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3446 | ALOGE("get lmkd control socket failed"); |
| 3447 | return -1; |
| 3448 | } |
| 3449 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3450 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3451 | if (ret < 0) { |
| 3452 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 3453 | return -1; |
| 3454 | } |
| 3455 | |
| 3456 | epev.events = EPOLLIN; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3457 | ctrl_sock.handler_info.handler = ctrl_connect_handler; |
| 3458 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); |
| 3459 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3460 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 3461 | return -1; |
| 3462 | } |
| 3463 | maxevents++; |
| 3464 | |
Robert Benea | 7878c9b | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 3465 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
Suren Baghdasaryan | e6613ea | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 3466 | use_inkernel_interface = has_inkernel_module; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3467 | |
| 3468 | if (use_inkernel_interface) { |
| 3469 | ALOGI("Using in-kernel low memory killer interface"); |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3470 | if (init_poll_kernel()) { |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3471 | epev.events = EPOLLIN; |
| 3472 | epev.data.ptr = (void*)&kernel_poll_hinfo; |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3473 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, kpoll_fd, &epev) != 0) { |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3474 | ALOGE("epoll_ctl for lmk events failed (errno=%d)", errno); |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3475 | close(kpoll_fd); |
| 3476 | kpoll_fd = -1; |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3477 | } else { |
| 3478 | maxevents++; |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3479 | /* let the others know it does support reporting kills */ |
| 3480 | property_set("sys.lmk.reportkills", "1"); |
Suren Baghdasaryan | 8b016be | 2019-09-04 19:12:29 -0700 | [diff] [blame] | 3481 | } |
Jim Blackler | 700b719 | 2019-04-26 11:18:29 +0100 | [diff] [blame] | 3482 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3483 | } else { |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3484 | if (!init_monitors()) { |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3485 | return -1; |
| 3486 | } |
Jing Ji | 5c48096 | 2019-12-04 09:22:05 -0800 | [diff] [blame] | 3487 | /* let the others know it does support reporting kills */ |
| 3488 | property_set("sys.lmk.reportkills", "1"); |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
Chong Zhang | 1cd12b5 | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 3491 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3492 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 3493 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 3494 | } |
| 3495 | |
Suren Baghdasaryan | a7394ea | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 3496 | memset(killcnt_idx, KILLCNT_INVALID_IDX, sizeof(killcnt_idx)); |
| 3497 | |
Suren Baghdasaryan | 03cb836 | 2019-07-15 13:35:04 -0700 | [diff] [blame] | 3498 | /* |
| 3499 | * Read zoneinfo as the biggest file we read to create and size the initial |
| 3500 | * read buffer and avoid memory re-allocations during memory pressure |
| 3501 | */ |
| 3502 | if (reread_file(&file_data) == NULL) { |
| 3503 | ALOGE("Failed to read %s: %s", file_data.filename, strerror(errno)); |
| 3504 | } |
| 3505 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3506 | /* check if kernel supports pidfd_open syscall */ |
Josh Gao | 84623be | 2021-03-18 17:16:08 -0700 | [diff] [blame] | 3507 | pidfd = TEMP_FAILURE_RETRY(pidfd_open(getpid(), 0)); |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3508 | if (pidfd < 0) { |
| 3509 | pidfd_supported = (errno != ENOSYS); |
| 3510 | } else { |
| 3511 | pidfd_supported = true; |
| 3512 | close(pidfd); |
| 3513 | } |
| 3514 | ALOGI("Process polling is %s", pidfd_supported ? "supported" : "not supported" ); |
| 3515 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3516 | return 0; |
| 3517 | } |
| 3518 | |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 3519 | static bool polling_paused(struct polling_params *poll_params) { |
| 3520 | return poll_params->paused_handler != NULL; |
| 3521 | } |
| 3522 | |
| 3523 | static void resume_polling(struct polling_params *poll_params, struct timespec curr_tm) { |
| 3524 | poll_params->poll_start_tm = curr_tm; |
| 3525 | poll_params->poll_handler = poll_params->paused_handler; |
Martin Liu | 589b575 | 2020-09-02 23:15:18 +0800 | [diff] [blame] | 3526 | poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS; |
| 3527 | poll_params->paused_handler = NULL; |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 3528 | } |
| 3529 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3530 | static void call_handler(struct event_handler_info* handler_info, |
| 3531 | struct polling_params *poll_params, uint32_t events) { |
| 3532 | struct timespec curr_tm; |
| 3533 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 3534 | watchdog.start(); |
Suren Baghdasaryan | 9ca5334 | 2020-04-24 16:54:55 -0700 | [diff] [blame] | 3535 | poll_params->update = POLLING_DO_NOT_CHANGE; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3536 | handler_info->handler(handler_info->data, events, poll_params); |
| 3537 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); |
Suren Baghdasaryan | 9ca5334 | 2020-04-24 16:54:55 -0700 | [diff] [blame] | 3538 | if (poll_params->poll_handler == handler_info) { |
| 3539 | poll_params->last_poll_tm = curr_tm; |
| 3540 | } |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3541 | |
| 3542 | switch (poll_params->update) { |
| 3543 | case POLLING_START: |
| 3544 | /* |
| 3545 | * Poll for the duration of PSI_WINDOW_SIZE_MS after the |
| 3546 | * initial PSI event because psi events are rate-limited |
| 3547 | * at one per sec. |
| 3548 | */ |
| 3549 | poll_params->poll_start_tm = curr_tm; |
Greg Kaiser | 5e80ed5 | 2019-10-10 06:52:23 -0700 | [diff] [blame] | 3550 | poll_params->poll_handler = handler_info; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3551 | break; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3552 | case POLLING_PAUSE: |
| 3553 | poll_params->paused_handler = handler_info; |
| 3554 | poll_params->poll_handler = NULL; |
| 3555 | break; |
| 3556 | case POLLING_RESUME: |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 3557 | resume_polling(poll_params, curr_tm); |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3558 | break; |
| 3559 | case POLLING_DO_NOT_CHANGE: |
| 3560 | if (get_time_diff_ms(&poll_params->poll_start_tm, &curr_tm) > PSI_WINDOW_SIZE_MS) { |
| 3561 | /* Polled for the duration of PSI window, time to stop */ |
| 3562 | poll_params->poll_handler = NULL; |
| 3563 | } |
Suren Baghdasaryan | 9ca5334 | 2020-04-24 16:54:55 -0700 | [diff] [blame] | 3564 | break; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3565 | } |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 3566 | watchdog.stop(); |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3569 | static void mainloop(void) { |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3570 | struct event_handler_info* handler_info; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3571 | struct polling_params poll_params; |
| 3572 | struct timespec curr_tm; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3573 | struct epoll_event *evt; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3574 | long delay = -1; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3575 | |
| 3576 | poll_params.poll_handler = NULL; |
Suren Baghdasaryan | 9ca5334 | 2020-04-24 16:54:55 -0700 | [diff] [blame] | 3577 | poll_params.paused_handler = NULL; |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3578 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3579 | while (1) { |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3580 | struct epoll_event events[MAX_EPOLL_EVENTS]; |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3581 | int nevents; |
| 3582 | int i; |
| 3583 | |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3584 | if (poll_params.poll_handler) { |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3585 | bool poll_now; |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3586 | |
| 3587 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); |
Martin Liu | 589b575 | 2020-09-02 23:15:18 +0800 | [diff] [blame] | 3588 | if (poll_params.update == POLLING_RESUME) { |
| 3589 | /* Just transitioned into POLLING_RESUME, poll immediately. */ |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3590 | poll_now = true; |
| 3591 | nevents = 0; |
| 3592 | } else { |
| 3593 | /* Calculate next timeout */ |
| 3594 | delay = get_time_diff_ms(&poll_params.last_poll_tm, &curr_tm); |
| 3595 | delay = (delay < poll_params.polling_interval_ms) ? |
| 3596 | poll_params.polling_interval_ms - delay : poll_params.polling_interval_ms; |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3597 | |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3598 | /* Wait for events until the next polling timeout */ |
| 3599 | nevents = epoll_wait(epollfd, events, maxevents, delay); |
| 3600 | |
| 3601 | /* Update current time after wait */ |
| 3602 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); |
| 3603 | poll_now = (get_time_diff_ms(&poll_params.last_poll_tm, &curr_tm) >= |
| 3604 | poll_params.polling_interval_ms); |
| 3605 | } |
| 3606 | if (poll_now) { |
| 3607 | call_handler(poll_params.poll_handler, &poll_params, 0); |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3608 | } |
| 3609 | } else { |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 3610 | if (kill_timeout_ms && is_waiting_for_kill()) { |
| 3611 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); |
| 3612 | delay = kill_timeout_ms - get_time_diff_ms(&last_kill_tm, &curr_tm); |
| 3613 | /* Wait for pidfds notification or kill timeout to expire */ |
| 3614 | nevents = (delay > 0) ? epoll_wait(epollfd, events, maxevents, delay) : 0; |
| 3615 | if (nevents == 0) { |
| 3616 | /* Kill notification timed out */ |
| 3617 | stop_wait_for_proc_kill(false); |
| 3618 | if (polling_paused(&poll_params)) { |
| 3619 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); |
Martin Liu | 589b575 | 2020-09-02 23:15:18 +0800 | [diff] [blame] | 3620 | poll_params.update = POLLING_RESUME; |
Suren Baghdasaryan | 03dccf3 | 2020-04-28 16:02:13 -0700 | [diff] [blame] | 3621 | resume_polling(&poll_params, curr_tm); |
| 3622 | } |
| 3623 | } |
| 3624 | } else { |
| 3625 | /* Wait for events with no timeout */ |
| 3626 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 3627 | } |
Suren Baghdasaryan | 55e3150 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 3628 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3629 | |
| 3630 | if (nevents == -1) { |
| 3631 | if (errno == EINTR) |
| 3632 | continue; |
| 3633 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 3634 | continue; |
| 3635 | } |
| 3636 | |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3637 | /* |
| 3638 | * First pass to see if any data socket connections were dropped. |
| 3639 | * Dropped connection should be handled before any other events |
| 3640 | * to deallocate data connection and correctly handle cases when |
| 3641 | * connection gets dropped and reestablished in the same epoll cycle. |
| 3642 | * In such cases it's essential to handle connection closures first. |
| 3643 | */ |
| 3644 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 3645 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { |
| 3646 | ALOGI("lmkd data connection dropped"); |
| 3647 | handler_info = (struct event_handler_info*)evt->data.ptr; |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 3648 | watchdog.start(); |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3649 | ctrl_data_close(handler_info->data); |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 3650 | watchdog.stop(); |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | /* Second pass to handle all other events */ |
| 3655 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3656 | if (evt->events & EPOLLERR) { |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3657 | ALOGD("EPOLLERR on event #%d", i); |
Suren Baghdasaryan | e12a067 | 2019-07-15 14:50:49 -0700 | [diff] [blame] | 3658 | } |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3659 | if (evt->events & EPOLLHUP) { |
| 3660 | /* This case was handled in the first pass */ |
| 3661 | continue; |
| 3662 | } |
| 3663 | if (evt->data.ptr) { |
| 3664 | handler_info = (struct event_handler_info*)evt->data.ptr; |
Suren Baghdasaryan | bc99e1b | 2019-06-26 17:56:01 -0700 | [diff] [blame] | 3665 | call_handler(handler_info, &poll_params, evt->events); |
Suren Baghdasaryan | ef8e701 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 3666 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3671 | int issue_reinit() { |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3672 | int sock; |
Colin Cross | d5b510e | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 3673 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3674 | sock = lmkd_connect(); |
| 3675 | if (sock < 0) { |
| 3676 | ALOGE("failed to connect to lmkd: %s", strerror(errno)); |
| 3677 | return -1; |
| 3678 | } |
| 3679 | |
| 3680 | enum update_props_result res = lmkd_update_props(sock); |
| 3681 | switch (res) { |
| 3682 | case UPDATE_PROPS_SUCCESS: |
| 3683 | ALOGI("lmkd updated properties successfully"); |
| 3684 | break; |
| 3685 | case UPDATE_PROPS_SEND_ERR: |
| 3686 | ALOGE("failed to send lmkd request: %s", strerror(errno)); |
| 3687 | break; |
| 3688 | case UPDATE_PROPS_RECV_ERR: |
| 3689 | ALOGE("failed to receive lmkd reply: %s", strerror(errno)); |
| 3690 | break; |
| 3691 | case UPDATE_PROPS_FORMAT_ERR: |
| 3692 | ALOGE("lmkd reply is invalid"); |
| 3693 | break; |
| 3694 | case UPDATE_PROPS_FAIL: |
| 3695 | ALOGE("lmkd failed to update its properties"); |
| 3696 | break; |
| 3697 | } |
| 3698 | |
| 3699 | close(sock); |
| 3700 | return res == UPDATE_PROPS_SUCCESS ? 0 : -1; |
| 3701 | } |
| 3702 | |
| 3703 | static void update_props() { |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3704 | /* By default disable low level vmpressure events */ |
| 3705 | level_oomadj[VMPRESS_LEVEL_LOW] = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3706 | GET_LMK_PROPERTY(int32, "low", OOM_SCORE_ADJ_MAX + 1); |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3707 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3708 | GET_LMK_PROPERTY(int32, "medium", 800); |
Suren Baghdasaryan | b2e0060 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 3709 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3710 | GET_LMK_PROPERTY(int32, "critical", 0); |
| 3711 | debug_process_killing = GET_LMK_PROPERTY(bool, "debug", false); |
Suren Baghdasaryan | 3faa303 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 3712 | |
| 3713 | /* By default disable upgrade/downgrade logic */ |
| 3714 | enable_pressure_upgrade = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3715 | GET_LMK_PROPERTY(bool, "critical_upgrade", false); |
Suren Baghdasaryan | 3faa303 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 3716 | upgrade_pressure = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3717 | (int64_t)GET_LMK_PROPERTY(int32, "upgrade_pressure", 100); |
Suren Baghdasaryan | 3faa303 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 3718 | downgrade_pressure = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3719 | (int64_t)GET_LMK_PROPERTY(int32, "downgrade_pressure", 100); |
Suren Baghdasaryan | eb7c549 | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 3720 | kill_heaviest_task = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3721 | GET_LMK_PROPERTY(bool, "kill_heaviest_task", false); |
Suren Baghdasaryan | d1d59f8 | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 3722 | low_ram_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | 30854e7 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 3723 | kill_timeout_ms = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3724 | (unsigned long)GET_LMK_PROPERTY(int32, "kill_timeout_ms", 100); |
Suren Baghdasaryan | 2c4611a | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 3725 | use_minfree_levels = |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3726 | GET_LMK_PROPERTY(bool, "use_minfree_levels", false); |
Suren Baghdasaryan | 8389fdb | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 3727 | per_app_memcg = |
| 3728 | property_get_bool("ro.config.per_app_memcg", low_ram_device); |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3729 | swap_free_low_percentage = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_free_low_percentage", |
Suren Baghdasaryan | fb1f592 | 2020-05-19 13:07:23 -0700 | [diff] [blame] | 3730 | DEF_LOW_SWAP)); |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3731 | psi_partial_stall_ms = GET_LMK_PROPERTY(int32, "psi_partial_stall_ms", |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3732 | low_ram_device ? DEF_PARTIAL_STALL_LOWRAM : DEF_PARTIAL_STALL); |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3733 | psi_complete_stall_ms = GET_LMK_PROPERTY(int32, "psi_complete_stall_ms", |
Suren Baghdasaryan | 2f88e15 | 2019-07-15 15:42:09 -0700 | [diff] [blame] | 3734 | DEF_COMPLETE_STALL); |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 3735 | thrashing_limit_pct = |
| 3736 | std::max(0, GET_LMK_PROPERTY(int32, "thrashing_limit", |
| 3737 | low_ram_device ? DEF_THRASHING_LOWRAM : DEF_THRASHING)); |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3738 | thrashing_limit_decay_pct = clamp(0, 100, GET_LMK_PROPERTY(int32, "thrashing_limit_decay", |
Suren Baghdasaryan | af2be4c | 2019-07-15 15:35:44 -0700 | [diff] [blame] | 3739 | low_ram_device ? DEF_THRASHING_DECAY_LOWRAM : DEF_THRASHING_DECAY)); |
Bart Van Assche | 80a3dba | 2022-02-02 23:51:35 +0000 | [diff] [blame] | 3740 | thrashing_critical_pct = std::max( |
| 3741 | 0, GET_LMK_PROPERTY(int32, "thrashing_limit_critical", thrashing_limit_pct * 2)); |
Suren Baghdasaryan | d0a8004 | 2021-08-03 15:40:23 -0700 | [diff] [blame] | 3742 | swap_util_max = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_util_max", 100)); |
| 3743 | filecache_min_kb = GET_LMK_PROPERTY(int64, "filecache_min_kb", 0); |
Suren Baghdasaryan | 5ae47a9 | 2022-02-10 21:10:23 -0800 | [diff] [blame] | 3744 | stall_limit_critical = GET_LMK_PROPERTY(int64, "stall_limit_critical", 100); |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 3745 | |
| 3746 | reaper.enable_debug(debug_process_killing); |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | int main(int argc, char **argv) { |
| 3750 | if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { |
Suren Baghdasaryan | 0e64ead | 2021-09-01 00:49:51 -0700 | [diff] [blame] | 3751 | if (property_set(LMKD_REINIT_PROP, "")) { |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3752 | ALOGE("Failed to reset " LMKD_REINIT_PROP " property"); |
| 3753 | } |
| 3754 | return issue_reinit(); |
| 3755 | } |
| 3756 | |
| 3757 | update_props(); |
Robert Benea | 57397dc | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 3758 | |
Suren Baghdasaryan | 12cacae | 2019-09-16 12:06:30 -0700 | [diff] [blame] | 3759 | ctx = create_android_logger(KILLINFO_LOG_TAG); |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 3760 | |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 3761 | if (!init()) { |
| 3762 | if (!use_inkernel_interface) { |
| 3763 | /* |
| 3764 | * MCL_ONFAULT pins pages as they fault instead of loading |
| 3765 | * everything immediately all at once. (Which would be bad, |
| 3766 | * because as of this writing, we have a lot of mapped pages we |
| 3767 | * never use.) Old kernels will see MCL_ONFAULT and fail with |
| 3768 | * EINVAL; we ignore this failure. |
| 3769 | * |
| 3770 | * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 3771 | * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 3772 | * in pages. |
| 3773 | */ |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 3774 | /* CAP_IPC_LOCK required */ |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 3775 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { |
| 3776 | ALOGW("mlockall failed %s", strerror(errno)); |
| 3777 | } |
Daniel Colascione | 4664833 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 3778 | |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 3779 | /* CAP_NICE required */ |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 3780 | struct sched_param param = { |
| 3781 | .sched_priority = 1, |
| 3782 | }; |
Mark Salyzyn | a00ccd8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 3783 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 3784 | ALOGW("set SCHED_FIFO failed %s", strerror(errno)); |
| 3785 | } |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Suren Baghdasaryan | 7c3addb | 2021-06-11 12:12:56 -0700 | [diff] [blame] | 3788 | if (init_reaper()) { |
| 3789 | ALOGI("Process reaper initialized with %d threads in the pool", |
| 3790 | reaper.thread_cnt()); |
| 3791 | } |
| 3792 | |
Suren Baghdasaryan | af1b0e0 | 2021-11-16 11:50:29 -0800 | [diff] [blame] | 3793 | if (!watchdog.init()) { |
| 3794 | ALOGE("Failed to initialize the watchdog"); |
| 3795 | } |
| 3796 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3797 | mainloop(); |
Mark Salyzyn | 5cc80b3 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 3798 | } |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3799 | |
Suren Baghdasaryan | 08bfa98 | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 3800 | android_log_destroy(&ctx); |
| 3801 | |
Todd Poynor | c58c514 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 3802 | ALOGI("exiting"); |
| 3803 | return 0; |
| 3804 | } |