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