Todd Poynor | 3948f80 | 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 | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 22 | #include <sched.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 23 | #include <signal.h> |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 27 | #include <sys/cdefs.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> |
| 29 | #include <sys/eventfd.h> |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 30 | #include <sys/mman.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 32 | #include <sys/sysinfo.h> |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 33 | #include <sys/types.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 38 | #include <lmkd.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 40 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 41 | /* |
| 42 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces |
| 43 | * to profile and correlate with OOM kills |
| 44 | */ |
| 45 | #ifdef LMKD_TRACE_KILLS |
| 46 | |
| 47 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
| 48 | #include <cutils/trace.h> |
| 49 | |
| 50 | #define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid); |
| 51 | #define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0); |
| 52 | |
| 53 | #else /* LMKD_TRACE_KILLS */ |
| 54 | |
| 55 | #define TRACE_KILL_START(pid) |
| 56 | #define TRACE_KILL_END() |
| 57 | |
| 58 | #endif /* LMKD_TRACE_KILLS */ |
| 59 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 60 | #ifndef __unused |
| 61 | #define __unused __attribute__((__unused__)) |
| 62 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 63 | |
| 64 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 65 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" |
| 66 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 67 | #define ZONEINFO_PATH "/proc/zoneinfo" |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 68 | #define MEMINFO_PATH "/proc/meminfo" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 69 | #define LINE_MAX 128 |
| 70 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 71 | /* gid containing AID_SYSTEM required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 72 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 73 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 74 | |
| 75 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 76 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 77 | |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 78 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ |
| 79 | #define SYSTEM_ADJ (-900) |
| 80 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 81 | /* default to old in-kernel interface if no memory pressure events */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 82 | static bool use_inkernel_interface = true; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 83 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 84 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 85 | /* memory pressure levels */ |
| 86 | enum vmpressure_level { |
| 87 | VMPRESS_LEVEL_LOW = 0, |
| 88 | VMPRESS_LEVEL_MEDIUM, |
| 89 | VMPRESS_LEVEL_CRITICAL, |
| 90 | VMPRESS_LEVEL_COUNT |
| 91 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 92 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 93 | static const char *level_name[] = { |
| 94 | "low", |
| 95 | "medium", |
| 96 | "critical" |
| 97 | }; |
| 98 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 99 | struct { |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 100 | int64_t min_nr_free_pages; /* recorded but not used yet */ |
| 101 | int64_t max_nr_free_pages; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 102 | } low_pressure_mem = { -1, -1 }; |
| 103 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 104 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 105 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 106 | static bool debug_process_killing; |
| 107 | static bool enable_pressure_upgrade; |
| 108 | static int64_t upgrade_pressure; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 109 | static int64_t downgrade_pressure; |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 110 | static bool low_ram_device; |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 111 | static bool kill_heaviest_task; |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 112 | static unsigned long kill_timeout_ms; |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 113 | static bool use_minfree_levels; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 114 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 115 | /* data required to handle events */ |
| 116 | struct event_handler_info { |
| 117 | int data; |
| 118 | void (*handler)(int data, uint32_t events); |
| 119 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 120 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 121 | /* data required to handle socket events */ |
| 122 | struct sock_event_handler_info { |
| 123 | int sock; |
| 124 | struct event_handler_info handler_info; |
| 125 | }; |
| 126 | |
| 127 | /* max supported number of data connections */ |
| 128 | #define MAX_DATA_CONN 2 |
| 129 | |
| 130 | /* socket event handler data */ |
| 131 | static struct sock_event_handler_info ctrl_sock; |
| 132 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; |
| 133 | |
| 134 | /* vmpressure event handler data */ |
| 135 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; |
| 136 | |
| 137 | /* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */ |
| 138 | #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 139 | static int epollfd; |
| 140 | static int maxevents; |
| 141 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 142 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 143 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 144 | #define OOM_SCORE_ADJ_MAX 1000 |
| 145 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 146 | static int lowmem_adj[MAX_TARGETS]; |
| 147 | static int lowmem_minfree[MAX_TARGETS]; |
| 148 | static int lowmem_targets_size; |
| 149 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 150 | /* Fields to parse in /proc/zoneinfo */ |
| 151 | enum zoneinfo_field { |
| 152 | ZI_NR_FREE_PAGES = 0, |
| 153 | ZI_NR_FILE_PAGES, |
| 154 | ZI_NR_SHMEM, |
| 155 | ZI_NR_UNEVICTABLE, |
| 156 | ZI_WORKINGSET_REFAULT, |
| 157 | ZI_HIGH, |
| 158 | ZI_FIELD_COUNT |
| 159 | }; |
| 160 | |
| 161 | static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = { |
| 162 | "nr_free_pages", |
| 163 | "nr_file_pages", |
| 164 | "nr_shmem", |
| 165 | "nr_unevictable", |
| 166 | "workingset_refault", |
| 167 | "high", |
| 168 | }; |
| 169 | |
| 170 | union zoneinfo { |
| 171 | struct { |
| 172 | int64_t nr_free_pages; |
| 173 | int64_t nr_file_pages; |
| 174 | int64_t nr_shmem; |
| 175 | int64_t nr_unevictable; |
| 176 | int64_t workingset_refault; |
| 177 | int64_t high; |
| 178 | /* fields below are calculated rather than read from the file */ |
| 179 | int64_t totalreserve_pages; |
| 180 | } field; |
| 181 | int64_t arr[ZI_FIELD_COUNT]; |
| 182 | }; |
| 183 | |
| 184 | /* Fields to parse in /proc/meminfo */ |
| 185 | enum meminfo_field { |
| 186 | MI_NR_FREE_PAGES = 0, |
| 187 | MI_CACHED, |
| 188 | MI_SWAP_CACHED, |
| 189 | MI_BUFFERS, |
| 190 | MI_SHMEM, |
| 191 | MI_UNEVICTABLE, |
| 192 | MI_FREE_SWAP, |
| 193 | MI_DIRTY, |
| 194 | MI_FIELD_COUNT |
| 195 | }; |
| 196 | |
| 197 | static const char* const meminfo_field_names[MI_FIELD_COUNT] = { |
| 198 | "MemFree:", |
| 199 | "Cached:", |
| 200 | "SwapCached:", |
| 201 | "Buffers:", |
| 202 | "Shmem:", |
| 203 | "Unevictable:", |
| 204 | "SwapFree:", |
| 205 | "Dirty:", |
| 206 | }; |
| 207 | |
| 208 | union meminfo { |
| 209 | struct { |
| 210 | int64_t nr_free_pages; |
| 211 | int64_t cached; |
| 212 | int64_t swap_cached; |
| 213 | int64_t buffers; |
| 214 | int64_t shmem; |
| 215 | int64_t unevictable; |
| 216 | int64_t free_swap; |
| 217 | int64_t dirty; |
| 218 | /* fields below are calculated rather than read from the file */ |
| 219 | int64_t nr_file_pages; |
| 220 | } field; |
| 221 | int64_t arr[MI_FIELD_COUNT]; |
| 222 | }; |
| 223 | |
| 224 | enum field_match_result { |
| 225 | NO_MATCH, |
| 226 | PARSE_FAIL, |
| 227 | PARSE_SUCCESS |
| 228 | }; |
| 229 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 230 | struct adjslot_list { |
| 231 | struct adjslot_list *next; |
| 232 | struct adjslot_list *prev; |
| 233 | }; |
| 234 | |
| 235 | struct proc { |
| 236 | struct adjslot_list asl; |
| 237 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 238 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 239 | int oomadj; |
| 240 | struct proc *pidhash_next; |
| 241 | }; |
| 242 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 243 | struct reread_data { |
| 244 | const char* const filename; |
| 245 | int fd; |
| 246 | }; |
| 247 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 248 | #define PIDHASH_SZ 1024 |
| 249 | static struct proc *pidhash[PIDHASH_SZ]; |
| 250 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 251 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 252 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 253 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 254 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 255 | /* PAGE_SIZE / 1024 */ |
| 256 | static long page_k; |
| 257 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 258 | static bool parse_int64(const char* str, int64_t* ret) { |
| 259 | char* endptr; |
| 260 | long long val = strtoll(str, &endptr, 10); |
| 261 | if (str == endptr || val > INT64_MAX) { |
| 262 | return false; |
| 263 | } |
| 264 | *ret = (int64_t)val; |
| 265 | return true; |
| 266 | } |
| 267 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 268 | static enum field_match_result match_field(const char* cp, const char* ap, |
| 269 | const char* const field_names[], |
| 270 | int field_count, int64_t* field, |
| 271 | int *field_idx) { |
| 272 | int64_t val; |
| 273 | int i; |
| 274 | |
| 275 | for (i = 0; i < field_count; i++) { |
| 276 | if (!strcmp(cp, field_names[i])) { |
| 277 | *field_idx = i; |
| 278 | return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL; |
| 279 | } |
| 280 | } |
| 281 | return NO_MATCH; |
| 282 | } |
| 283 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 284 | /* |
| 285 | * Read file content from the beginning up to max_len bytes or EOF |
| 286 | * whichever happens first. |
| 287 | */ |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 288 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 289 | { |
| 290 | ssize_t ret = 0; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 291 | off_t offset = 0; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 292 | |
| 293 | while (max_len > 0) { |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 294 | ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 295 | if (r == 0) { |
| 296 | break; |
| 297 | } |
| 298 | if (r == -1) { |
| 299 | return -1; |
| 300 | } |
| 301 | ret += r; |
| 302 | buf += r; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 303 | offset += r; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 304 | max_len -= r; |
| 305 | } |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 310 | /* |
| 311 | * Read a new or already opened file from the beginning. |
| 312 | * If the file has not been opened yet data->fd should be set to -1. |
| 313 | * To be used with files which are read often and possibly during high |
| 314 | * memory pressure to minimize file opening which by itself requires kernel |
| 315 | * memory allocation and might result in a stall on memory stressed system. |
| 316 | */ |
| 317 | static int reread_file(struct reread_data *data, char *buf, size_t buf_size) { |
| 318 | ssize_t size; |
| 319 | |
| 320 | if (data->fd == -1) { |
| 321 | data->fd = open(data->filename, O_RDONLY | O_CLOEXEC); |
| 322 | if (data->fd == -1) { |
| 323 | ALOGE("%s open: %s", data->filename, strerror(errno)); |
| 324 | return -1; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | size = read_all(data->fd, buf, buf_size - 1); |
| 329 | if (size < 0) { |
| 330 | ALOGE("%s read: %s", data->filename, strerror(errno)); |
| 331 | close(data->fd); |
| 332 | data->fd = -1; |
| 333 | return -1; |
| 334 | } |
| 335 | ALOG_ASSERT((size_t)size < buf_size - 1, data->filename " too large"); |
| 336 | buf[size] = 0; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 341 | static struct proc *pid_lookup(int pid) { |
| 342 | struct proc *procp; |
| 343 | |
| 344 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 345 | procp = procp->pidhash_next) |
| 346 | ; |
| 347 | |
| 348 | return procp; |
| 349 | } |
| 350 | |
| 351 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 352 | { |
| 353 | struct adjslot_list *next = head->next; |
| 354 | new->prev = head; |
| 355 | new->next = next; |
| 356 | next->prev = new; |
| 357 | head->next = new; |
| 358 | } |
| 359 | |
| 360 | static void adjslot_remove(struct adjslot_list *old) |
| 361 | { |
| 362 | struct adjslot_list *prev = old->prev; |
| 363 | struct adjslot_list *next = old->next; |
| 364 | next->prev = prev; |
| 365 | prev->next = next; |
| 366 | } |
| 367 | |
| 368 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 369 | struct adjslot_list *asl = head->prev; |
| 370 | |
| 371 | return asl == head ? NULL : asl; |
| 372 | } |
| 373 | |
| 374 | static void proc_slot(struct proc *procp) { |
| 375 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 376 | |
| 377 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 378 | } |
| 379 | |
| 380 | static void proc_unslot(struct proc *procp) { |
| 381 | adjslot_remove(&procp->asl); |
| 382 | } |
| 383 | |
| 384 | static void proc_insert(struct proc *procp) { |
| 385 | int hval = pid_hashfn(procp->pid); |
| 386 | |
| 387 | procp->pidhash_next = pidhash[hval]; |
| 388 | pidhash[hval] = procp; |
| 389 | proc_slot(procp); |
| 390 | } |
| 391 | |
| 392 | static int pid_remove(int pid) { |
| 393 | int hval = pid_hashfn(pid); |
| 394 | struct proc *procp; |
| 395 | struct proc *prevp; |
| 396 | |
| 397 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 398 | procp = procp->pidhash_next) |
| 399 | prevp = procp; |
| 400 | |
| 401 | if (!procp) |
| 402 | return -1; |
| 403 | |
| 404 | if (!prevp) |
| 405 | pidhash[hval] = procp->pidhash_next; |
| 406 | else |
| 407 | prevp->pidhash_next = procp->pidhash_next; |
| 408 | |
| 409 | proc_unslot(procp); |
| 410 | free(procp); |
| 411 | return 0; |
| 412 | } |
| 413 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 414 | /* |
| 415 | * Write a string to a file. |
| 416 | * Returns false if the file does not exist. |
| 417 | */ |
| 418 | static bool writefilestring(const char *path, const char *s, |
| 419 | bool err_if_missing) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 420 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 421 | ssize_t len = strlen(s); |
| 422 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 423 | |
| 424 | if (fd < 0) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 425 | if (err_if_missing) { |
| 426 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 427 | } |
| 428 | return false; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 431 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 432 | if (ret < 0) { |
| 433 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 434 | } else if (ret < len) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 435 | ALOGE("Short write on %s; length=%zd", path, ret); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | close(fd); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 439 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 442 | static void cmd_procprio(LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 443 | struct proc *procp; |
| 444 | char path[80]; |
| 445 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 446 | int soft_limit_mult; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 447 | struct lmk_procprio params; |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 448 | bool is_system_server; |
| 449 | struct passwd *pwdrec; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 450 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 451 | lmkd_pack_get_procprio(packet, ¶ms); |
| 452 | |
| 453 | if (params.oomadj < OOM_SCORE_ADJ_MIN || |
| 454 | params.oomadj > OOM_SCORE_ADJ_MAX) { |
| 455 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 459 | /* gid containing AID_READPROC required */ |
| 460 | /* CAP_SYS_RESOURCE required */ |
| 461 | /* CAP_DAC_OVERRIDE required */ |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 462 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); |
| 463 | snprintf(val, sizeof(val), "%d", params.oomadj); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 464 | if (!writefilestring(path, val, false)) { |
| 465 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", |
| 466 | path, errno, params.pid); |
| 467 | /* If this file does not exist the process is dead. */ |
| 468 | return; |
| 469 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 470 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 471 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 472 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 473 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 474 | |
Suren Baghdasaryan | fe26dfb | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 475 | if (low_ram_device) { |
| 476 | if (params.oomadj >= 900) { |
| 477 | soft_limit_mult = 0; |
| 478 | } else if (params.oomadj >= 800) { |
| 479 | soft_limit_mult = 0; |
| 480 | } else if (params.oomadj >= 700) { |
| 481 | soft_limit_mult = 0; |
| 482 | } else if (params.oomadj >= 600) { |
| 483 | // Launcher should be perceptible, don't kill it. |
| 484 | params.oomadj = 200; |
| 485 | soft_limit_mult = 1; |
| 486 | } else if (params.oomadj >= 500) { |
| 487 | soft_limit_mult = 0; |
| 488 | } else if (params.oomadj >= 400) { |
| 489 | soft_limit_mult = 0; |
| 490 | } else if (params.oomadj >= 300) { |
| 491 | soft_limit_mult = 1; |
| 492 | } else if (params.oomadj >= 200) { |
| 493 | soft_limit_mult = 2; |
| 494 | } else if (params.oomadj >= 100) { |
| 495 | soft_limit_mult = 10; |
| 496 | } else if (params.oomadj >= 0) { |
| 497 | soft_limit_mult = 20; |
| 498 | } else { |
| 499 | // Persistent processes will have a large |
| 500 | // soft limit 512MB. |
| 501 | soft_limit_mult = 64; |
| 502 | } |
| 503 | |
| 504 | snprintf(path, sizeof(path), MEMCG_SYSFS_PATH |
| 505 | "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", |
| 506 | params.uid, params.pid); |
| 507 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
| 508 | |
| 509 | /* |
| 510 | * system_server process has no memcg under /dev/memcg/apps but should be |
| 511 | * registered with lmkd. This is the best way so far to identify it. |
| 512 | */ |
| 513 | is_system_server = (params.oomadj == SYSTEM_ADJ && |
| 514 | (pwdrec = getpwnam("system")) != NULL && |
| 515 | params.uid == pwdrec->pw_uid); |
| 516 | writefilestring(path, val, !is_system_server); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 519 | procp = pid_lookup(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 520 | if (!procp) { |
| 521 | procp = malloc(sizeof(struct proc)); |
| 522 | if (!procp) { |
| 523 | // Oh, the irony. May need to rebuild our state. |
| 524 | return; |
| 525 | } |
| 526 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 527 | procp->pid = params.pid; |
| 528 | procp->uid = params.uid; |
| 529 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 530 | proc_insert(procp); |
| 531 | } else { |
| 532 | proc_unslot(procp); |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 533 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 534 | proc_slot(procp); |
| 535 | } |
| 536 | } |
| 537 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 538 | static void cmd_procremove(LMKD_CTRL_PACKET packet) { |
| 539 | struct lmk_procremove params; |
| 540 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 541 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 542 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 543 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 544 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 545 | lmkd_pack_get_procremove(packet, ¶ms); |
| 546 | pid_remove(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 549 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 550 | int i; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 551 | struct lmk_target target; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 552 | |
| 553 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 554 | return; |
| 555 | |
| 556 | for (i = 0; i < ntargets; i++) { |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 557 | lmkd_pack_get_target(packet, i, &target); |
| 558 | lowmem_minfree[i] = target.minfree; |
| 559 | lowmem_adj[i] = target.oom_adj_score; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | lowmem_targets_size = ntargets; |
| 563 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 564 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 565 | char minfreestr[128]; |
| 566 | char killpriostr[128]; |
| 567 | |
| 568 | minfreestr[0] = '\0'; |
| 569 | killpriostr[0] = '\0'; |
| 570 | |
| 571 | for (i = 0; i < lowmem_targets_size; i++) { |
| 572 | char val[40]; |
| 573 | |
| 574 | if (i) { |
| 575 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 576 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 577 | } |
| 578 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 579 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 580 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 581 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 582 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 583 | } |
| 584 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 585 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); |
| 586 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 590 | static void ctrl_data_close(int dsock_idx) { |
| 591 | struct epoll_event epev; |
| 592 | |
| 593 | ALOGI("closing lmkd data connection"); |
| 594 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { |
| 595 | // Log a warning and keep going |
| 596 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 597 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 598 | maxevents--; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 599 | |
| 600 | close(data_sock[dsock_idx].sock); |
| 601 | data_sock[dsock_idx].sock = -1; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 604 | static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 605 | int ret = 0; |
| 606 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 607 | ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 608 | |
| 609 | if (ret == -1) { |
| 610 | ALOGE("control data socket read failed; errno=%d", errno); |
| 611 | } else if (ret == 0) { |
| 612 | ALOGE("Got EOF on control data socket"); |
| 613 | ret = -1; |
| 614 | } |
| 615 | |
| 616 | return ret; |
| 617 | } |
| 618 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 619 | static void ctrl_command_handler(int dsock_idx) { |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 620 | LMKD_CTRL_PACKET packet; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 621 | int len; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 622 | enum lmk_cmd cmd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 623 | int nargs; |
| 624 | int targets; |
| 625 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 626 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 627 | if (len <= 0) |
| 628 | return; |
| 629 | |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 630 | if (len < (int)sizeof(int)) { |
| 631 | ALOGE("Wrong control socket read length len=%d", len); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | cmd = lmkd_pack_get_cmd(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 636 | nargs = len / sizeof(int) - 1; |
| 637 | if (nargs < 0) |
| 638 | goto wronglen; |
| 639 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 640 | switch(cmd) { |
| 641 | case LMK_TARGET: |
| 642 | targets = nargs / 2; |
| 643 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 644 | goto wronglen; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 645 | cmd_target(targets, packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 646 | break; |
| 647 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 648 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 649 | goto wronglen; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 650 | cmd_procprio(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 651 | break; |
| 652 | case LMK_PROCREMOVE: |
| 653 | if (nargs != 1) |
| 654 | goto wronglen; |
Suren Baghdasaryan | a92de71 | 2018-03-07 12:27:50 -0800 | [diff] [blame] | 655 | cmd_procremove(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 656 | break; |
| 657 | default: |
| 658 | ALOGE("Received unknown command code %d", cmd); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | return; |
| 663 | |
| 664 | wronglen: |
| 665 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 666 | } |
| 667 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 668 | static void ctrl_data_handler(int data, uint32_t events) { |
| 669 | if (events & EPOLLIN) { |
| 670 | ctrl_command_handler(data); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 674 | static int get_free_dsock() { |
| 675 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 676 | if (data_sock[i].sock < 0) { |
| 677 | return i; |
| 678 | } |
| 679 | } |
| 680 | return -1; |
| 681 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 682 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 683 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused) { |
| 684 | struct epoll_event epev; |
| 685 | int free_dscock_idx = get_free_dsock(); |
| 686 | |
| 687 | if (free_dscock_idx < 0) { |
| 688 | /* |
| 689 | * Number of data connections exceeded max supported. This should not |
| 690 | * happen but if it does we drop all existing connections and accept |
| 691 | * the new one. This prevents inactive connections from monopolizing |
| 692 | * data socket and if we drop ActivityManager connection it will |
| 693 | * immediately reconnect. |
| 694 | */ |
| 695 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 696 | ctrl_data_close(i); |
| 697 | } |
| 698 | free_dscock_idx = 0; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 701 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); |
| 702 | if (data_sock[free_dscock_idx].sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 703 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 704 | return; |
| 705 | } |
| 706 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 707 | ALOGI("lmkd data connection established"); |
| 708 | /* use data to store data connection idx */ |
| 709 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; |
| 710 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 711 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 712 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); |
| 713 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 714 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 715 | ctrl_data_close(free_dscock_idx); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 716 | return; |
| 717 | } |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 718 | maxevents++; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 721 | /* /prop/zoneinfo parsing routines */ |
| 722 | static int64_t zoneinfo_parse_protection(char *cp) { |
| 723 | int64_t max = 0; |
| 724 | long long zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 725 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 726 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 727 | for (cp = strtok_r(cp, "(), ", &save_ptr); cp; |
| 728 | cp = strtok_r(NULL, "), ", &save_ptr)) { |
| 729 | zoneval = strtoll(cp, &cp, 0); |
| 730 | if (zoneval > max) { |
| 731 | max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval; |
| 732 | } |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 733 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 734 | |
| 735 | return max; |
| 736 | } |
| 737 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 738 | static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 739 | char *cp = line; |
| 740 | char *ap; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 741 | char *save_ptr; |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 742 | int64_t val; |
| 743 | int field_idx; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 744 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 745 | cp = strtok_r(line, " ", &save_ptr); |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 746 | if (!cp) { |
| 747 | return true; |
| 748 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 749 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 750 | if (!strcmp(cp, "protection:")) { |
| 751 | ap = strtok_r(NULL, ")", &save_ptr); |
| 752 | } else { |
| 753 | ap = strtok_r(NULL, " ", &save_ptr); |
| 754 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 755 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 756 | if (!ap) { |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | switch (match_field(cp, ap, zoneinfo_field_names, |
| 761 | ZI_FIELD_COUNT, &val, &field_idx)) { |
| 762 | case (PARSE_SUCCESS): |
| 763 | zi->arr[field_idx] += val; |
| 764 | break; |
| 765 | case (NO_MATCH): |
| 766 | if (!strcmp(cp, "protection:")) { |
| 767 | zi->field.totalreserve_pages += |
| 768 | zoneinfo_parse_protection(ap); |
| 769 | } |
| 770 | break; |
| 771 | case (PARSE_FAIL): |
| 772 | default: |
| 773 | return false; |
| 774 | } |
| 775 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 778 | static int zoneinfo_parse(union zoneinfo *zi) { |
| 779 | static struct reread_data file_data = { |
| 780 | .filename = ZONEINFO_PATH, |
| 781 | .fd = -1, |
| 782 | }; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 783 | char buf[PAGE_SIZE]; |
| 784 | char *save_ptr; |
| 785 | char *line; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 786 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 787 | memset(zi, 0, sizeof(union zoneinfo)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 788 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 789 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 790 | return -1; |
| 791 | } |
| 792 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 793 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 794 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 795 | if (!zoneinfo_parse_line(line, zi)) { |
| 796 | ALOGE("%s parse error", file_data.filename); |
| 797 | return -1; |
| 798 | } |
| 799 | } |
| 800 | zi->field.totalreserve_pages += zi->field.high; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* /prop/meminfo parsing routines */ |
| 806 | static bool meminfo_parse_line(char *line, union meminfo *mi) { |
| 807 | char *cp = line; |
| 808 | char *ap; |
| 809 | char *save_ptr; |
| 810 | int64_t val; |
| 811 | int field_idx; |
| 812 | enum field_match_result match_res; |
| 813 | |
| 814 | cp = strtok_r(line, " ", &save_ptr); |
| 815 | if (!cp) { |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | ap = strtok_r(NULL, " ", &save_ptr); |
| 820 | if (!ap) { |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT, |
| 825 | &val, &field_idx); |
| 826 | if (match_res == PARSE_SUCCESS) { |
| 827 | mi->arr[field_idx] = val / page_k; |
| 828 | } |
| 829 | return (match_res != PARSE_FAIL); |
| 830 | } |
| 831 | |
| 832 | static int meminfo_parse(union meminfo *mi) { |
| 833 | static struct reread_data file_data = { |
| 834 | .filename = MEMINFO_PATH, |
| 835 | .fd = -1, |
| 836 | }; |
| 837 | char buf[PAGE_SIZE]; |
| 838 | char *save_ptr; |
| 839 | char *line; |
| 840 | |
| 841 | memset(mi, 0, sizeof(union meminfo)); |
| 842 | |
| 843 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 844 | return -1; |
| 845 | } |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 846 | |
Suren Baghdasaryan | ceffb41 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 847 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 848 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 849 | if (!meminfo_parse_line(line, mi)) { |
| 850 | ALOGE("%s parse error", file_data.filename); |
| 851 | return -1; |
| 852 | } |
| 853 | } |
| 854 | mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + |
| 855 | mi->field.buffers; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 856 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static int proc_get_size(int pid) { |
| 861 | char path[PATH_MAX]; |
| 862 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 863 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 864 | int rss = 0; |
| 865 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 866 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 867 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 868 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 869 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 870 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 871 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 872 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 873 | |
| 874 | ret = read_all(fd, line, sizeof(line) - 1); |
| 875 | if (ret < 0) { |
| 876 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 881 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 882 | return rss; |
| 883 | } |
| 884 | |
| 885 | static char *proc_get_name(int pid) { |
| 886 | char path[PATH_MAX]; |
| 887 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 888 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 889 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 890 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 891 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 892 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 893 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 894 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 895 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 896 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 897 | ret = read_all(fd, line, sizeof(line) - 1); |
| 898 | close(fd); |
| 899 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 900 | return NULL; |
| 901 | } |
| 902 | |
| 903 | cp = strchr(line, ' '); |
| 904 | if (cp) |
| 905 | *cp = '\0'; |
| 906 | |
| 907 | return line; |
| 908 | } |
| 909 | |
| 910 | static struct proc *proc_adj_lru(int oomadj) { |
| 911 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 912 | } |
| 913 | |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 914 | static struct proc *proc_get_heaviest(int oomadj) { |
| 915 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 916 | struct adjslot_list *curr = head->next; |
| 917 | struct proc *maxprocp = NULL; |
| 918 | int maxsize = 0; |
| 919 | while (curr != head) { |
| 920 | int pid = ((struct proc *)curr)->pid; |
| 921 | int tasksize = proc_get_size(pid); |
| 922 | if (tasksize <= 0) { |
| 923 | struct adjslot_list *next = curr->next; |
| 924 | pid_remove(pid); |
| 925 | curr = next; |
| 926 | } else { |
| 927 | if (tasksize > maxsize) { |
| 928 | maxsize = tasksize; |
| 929 | maxprocp = (struct proc *)curr; |
| 930 | } |
| 931 | curr = curr->next; |
| 932 | } |
| 933 | } |
| 934 | return maxprocp; |
| 935 | } |
| 936 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 937 | /* Kill one process specified by procp. Returns the size of the process killed */ |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 938 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 939 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 940 | int pid = procp->pid; |
| 941 | uid_t uid = procp->uid; |
| 942 | char *taskname; |
| 943 | int tasksize; |
| 944 | int r; |
| 945 | |
| 946 | taskname = proc_get_name(pid); |
| 947 | if (!taskname) { |
| 948 | pid_remove(pid); |
| 949 | return -1; |
| 950 | } |
| 951 | |
| 952 | tasksize = proc_get_size(pid); |
| 953 | if (tasksize <= 0) { |
| 954 | pid_remove(pid); |
| 955 | return -1; |
| 956 | } |
| 957 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 958 | TRACE_KILL_START(pid); |
| 959 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 960 | /* CAP_KILL required */ |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 961 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 962 | ALOGI( |
| 963 | "Killing '%s' (%d), uid %d, adj %d\n" |
Suren Baghdasaryan | d07a94f | 2018-03-20 16:25:54 -0700 | [diff] [blame] | 964 | " to free %ldkB because system is under %s memory pressure (min_oom_adj=%d)\n", |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 965 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 966 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 967 | pid_remove(pid); |
| 968 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 969 | TRACE_KILL_END(); |
| 970 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 971 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 972 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 973 | return -1; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 974 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 975 | |
| 976 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 980 | * Find processes to kill to free required number of pages. |
| 981 | * If pages_to_free is set to 0 only one process will be killed. |
| 982 | * Returns the size of the killed processes. |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 983 | */ |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 984 | static int find_and_kill_processes(enum vmpressure_level level, |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 985 | int min_score_adj, int pages_to_free) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 986 | int i; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 987 | int killed_size; |
| 988 | int pages_freed = 0; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 989 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 990 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 991 | struct proc *procp; |
| 992 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 993 | while (true) { |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 994 | procp = kill_heaviest_task ? |
| 995 | proc_get_heaviest(i) : proc_adj_lru(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 996 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 997 | if (!procp) |
| 998 | break; |
| 999 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1000 | killed_size = kill_one_process(procp, min_score_adj, level); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1001 | if (killed_size >= 0) { |
| 1002 | pages_freed += killed_size; |
| 1003 | if (pages_freed >= pages_to_free) { |
| 1004 | return pages_freed; |
| 1005 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1010 | return pages_freed; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1013 | static int64_t get_memory_usage(struct reread_data *file_data) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1014 | int ret; |
| 1015 | int64_t mem_usage; |
| 1016 | char buf[32]; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (reread_file(file_data, buf, sizeof(buf)) < 0) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1019 | return -1; |
| 1020 | } |
| 1021 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1022 | if (!parse_int64(buf, &mem_usage)) { |
| 1023 | ALOGE("%s parse error", file_data->filename); |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1024 | return -1; |
| 1025 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1026 | if (mem_usage == 0) { |
| 1027 | ALOGE("No memory!"); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | return mem_usage; |
| 1031 | } |
| 1032 | |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1033 | void record_low_pressure_levels(union meminfo *mi) { |
| 1034 | if (low_pressure_mem.min_nr_free_pages == -1 || |
| 1035 | low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1036 | if (debug_process_killing) { |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1037 | ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64, |
| 1038 | low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1039 | } |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1040 | low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1041 | } |
| 1042 | /* |
| 1043 | * Free memory at low vmpressure events occasionally gets spikes, |
| 1044 | * possibly a stale low vmpressure event with memory already |
| 1045 | * freed up (no memory pressure should have been reported). |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1046 | * Ignore large jumps in max_nr_free_pages that would mess up our stats. |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1047 | */ |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1048 | if (low_pressure_mem.max_nr_free_pages == -1 || |
| 1049 | (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages && |
| 1050 | mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages < |
| 1051 | low_pressure_mem.max_nr_free_pages * 0.1)) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1052 | if (debug_process_killing) { |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1053 | ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64, |
| 1054 | low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1055 | } |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1056 | low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1060 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 1061 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 1062 | level + 1 : level); |
| 1063 | } |
| 1064 | |
| 1065 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 1066 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 1067 | level - 1 : level); |
| 1068 | } |
| 1069 | |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1070 | static inline unsigned long get_time_diff_ms(struct timeval *from, |
| 1071 | struct timeval *to) { |
| 1072 | return (to->tv_sec - from->tv_sec) * 1000 + |
| 1073 | (to->tv_usec - from->tv_usec) / 1000; |
| 1074 | } |
| 1075 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1076 | static void mp_event_common(int data, uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1077 | int ret; |
| 1078 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1079 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1080 | int64_t mem_pressure; |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1081 | enum vmpressure_level lvl; |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1082 | union meminfo mi; |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1083 | union zoneinfo zi; |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1084 | static struct timeval last_report_tm; |
| 1085 | static unsigned long skip_count = 0; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1086 | enum vmpressure_level level = (enum vmpressure_level)data; |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1087 | long other_free = 0, other_file = 0; |
| 1088 | int min_score_adj; |
| 1089 | int pages_to_free = 0; |
| 1090 | int minfree = 0; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1091 | static struct reread_data mem_usage_file_data = { |
| 1092 | .filename = MEMCG_MEMORY_USAGE, |
| 1093 | .fd = -1, |
| 1094 | }; |
| 1095 | static struct reread_data memsw_usage_file_data = { |
| 1096 | .filename = MEMCG_MEMORYSW_USAGE, |
| 1097 | .fd = -1, |
| 1098 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1099 | |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1100 | /* |
| 1101 | * Check all event counters from low to critical |
| 1102 | * and upgrade to the highest priority one. By reading |
| 1103 | * eventfd we also reset the event counters. |
| 1104 | */ |
| 1105 | for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { |
| 1106 | if (mpevfd[lvl] != -1 && |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1107 | TEMP_FAILURE_RETRY(read(mpevfd[lvl], |
| 1108 | &evcount, sizeof(evcount))) > 0 && |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1109 | evcount > 0 && lvl > level) { |
| 1110 | level = lvl; |
| 1111 | } |
| 1112 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1113 | |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1114 | if (kill_timeout_ms) { |
| 1115 | struct timeval curr_tm; |
| 1116 | gettimeofday(&curr_tm, NULL); |
| 1117 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) { |
| 1118 | skip_count++; |
| 1119 | return; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | if (skip_count > 0) { |
| 1124 | if (debug_process_killing) { |
| 1125 | ALOGI("%lu memory pressure events were skipped after a kill!", |
| 1126 | skip_count); |
| 1127 | } |
| 1128 | skip_count = 0; |
| 1129 | } |
| 1130 | |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1131 | if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1132 | ALOGE("Failed to get free memory!"); |
| 1133 | return; |
| 1134 | } |
| 1135 | |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1136 | if (use_minfree_levels) { |
| 1137 | int i; |
| 1138 | |
| 1139 | other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages; |
| 1140 | if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) { |
| 1141 | other_file = (mi.field.nr_file_pages - mi.field.shmem - |
| 1142 | mi.field.unevictable - mi.field.swap_cached); |
| 1143 | } else { |
| 1144 | other_file = 0; |
| 1145 | } |
| 1146 | |
| 1147 | min_score_adj = OOM_SCORE_ADJ_MAX + 1; |
| 1148 | for (i = 0; i < lowmem_targets_size; i++) { |
| 1149 | minfree = lowmem_minfree[i]; |
| 1150 | if (other_free < minfree && other_file < minfree) { |
| 1151 | min_score_adj = lowmem_adj[i]; |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | |
Suren Baghdasaryan | fe26dfb | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1156 | if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { |
| 1157 | if (debug_process_killing) { |
| 1158 | ALOGI("Ignore %s memory pressure event " |
| 1159 | "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)", |
| 1160 | level_name[level], other_free * page_k, other_file * page_k, |
| 1161 | (long)lowmem_minfree[lowmem_targets_size - 1] * page_k); |
| 1162 | } |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1163 | return; |
Suren Baghdasaryan | fe26dfb | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1164 | } |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1165 | |
| 1166 | /* Free up enough pages to push over the highest minfree level */ |
| 1167 | pages_to_free = lowmem_minfree[lowmem_targets_size - 1] - |
| 1168 | ((other_free < other_file) ? other_free : other_file); |
| 1169 | goto do_kill; |
| 1170 | } |
| 1171 | |
Suren Baghdasaryan | 45c9e0b | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1172 | if (level == VMPRESS_LEVEL_LOW) { |
| 1173 | record_low_pressure_levels(&mi); |
| 1174 | } |
| 1175 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1176 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 1177 | /* Do not monitor this pressure level */ |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1181 | if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) { |
| 1182 | goto do_kill; |
| 1183 | } |
| 1184 | if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1185 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1186 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1187 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1188 | // Calculate percent for swappinness. |
| 1189 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 1190 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1191 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1192 | // We are swapping too much. |
| 1193 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1194 | level = upgrade_level(level); |
| 1195 | if (debug_process_killing) { |
| 1196 | ALOGI("Event upgraded to %s", level_name[level]); |
| 1197 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1201 | // If the pressure is larger than downgrade_pressure lmk will not |
| 1202 | // kill any process, since enough memory is available. |
| 1203 | if (mem_pressure > downgrade_pressure) { |
| 1204 | if (debug_process_killing) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1205 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1206 | } |
| 1207 | return; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1208 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 1209 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1210 | if (debug_process_killing) { |
| 1211 | ALOGI("Downgrade critical memory pressure"); |
| 1212 | } |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1213 | // Downgrade event, since enough memory available. |
| 1214 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1217 | do_kill: |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1218 | if (low_ram_device) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1219 | /* For Go devices kill only one task */ |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1220 | if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1221 | if (debug_process_killing) { |
| 1222 | ALOGI("Nothing to kill"); |
| 1223 | } |
| 1224 | } |
| 1225 | } else { |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1226 | int pages_freed; |
| 1227 | |
| 1228 | if (!use_minfree_levels) { |
| 1229 | /* If pressure level is less than critical and enough free swap then ignore */ |
| 1230 | if (level < VMPRESS_LEVEL_CRITICAL && |
| 1231 | mi.field.free_swap > low_pressure_mem.max_nr_free_pages) { |
| 1232 | if (debug_process_killing) { |
| 1233 | ALOGI("Ignoring pressure since %" PRId64 |
| 1234 | " swap pages are available ", |
| 1235 | mi.field.free_swap); |
| 1236 | } |
| 1237 | return; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1238 | } |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1239 | /* Free up enough memory to downgrate the memory pressure to low level */ |
| 1240 | if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) { |
| 1241 | pages_to_free = low_pressure_mem.max_nr_free_pages - |
| 1242 | mi.field.nr_free_pages; |
| 1243 | } else { |
| 1244 | if (debug_process_killing) { |
| 1245 | ALOGI("Ignoring pressure since more memory is " |
| 1246 | "available (%" PRId64 ") than watermark (%" PRId64 ")", |
| 1247 | mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages); |
| 1248 | } |
| 1249 | return; |
| 1250 | } |
| 1251 | min_score_adj = level_oomadj[level]; |
| 1252 | } else { |
| 1253 | if (debug_process_killing) { |
| 1254 | ALOGI("Killing because cache %ldkB is below " |
| 1255 | "limit %ldkB for oom_adj %d\n" |
| 1256 | " Free memory is %ldkB %s reserved", |
| 1257 | other_file * page_k, minfree * page_k, min_score_adj, |
| 1258 | other_free * page_k, other_free >= 0 ? "above" : "below"); |
| 1259 | } |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1262 | if (debug_process_killing) { |
| 1263 | ALOGI("Trying to free %d pages", pages_to_free); |
| 1264 | } |
| 1265 | pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free); |
| 1266 | if (pages_freed < pages_to_free) { |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1267 | if (debug_process_killing) { |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1268 | ALOGI("Unable to free enough memory (pages freed=%d)", pages_freed); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1269 | } |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1270 | } else { |
| 1271 | gettimeofday(&last_report_tm, NULL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1272 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 1273 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1276 | static bool init_mp_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1277 | int mpfd; |
| 1278 | int evfd; |
| 1279 | int evctlfd; |
| 1280 | char buf[256]; |
| 1281 | struct epoll_event epev; |
| 1282 | int ret; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1283 | int level_idx = (int)level; |
| 1284 | const char *levelstr = level_name[level_idx]; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1285 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1286 | /* gid containing AID_SYSTEM required */ |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1287 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1288 | if (mpfd < 0) { |
| 1289 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 1290 | goto err_open_mpfd; |
| 1291 | } |
| 1292 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1293 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1294 | if (evctlfd < 0) { |
| 1295 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 1296 | goto err_open_evctlfd; |
| 1297 | } |
| 1298 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1299 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1300 | if (evfd < 0) { |
| 1301 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 1302 | goto err_eventfd; |
| 1303 | } |
| 1304 | |
| 1305 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 1306 | if (ret >= (ssize_t)sizeof(buf)) { |
| 1307 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 1308 | goto err; |
| 1309 | } |
| 1310 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1311 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1312 | if (ret == -1) { |
| 1313 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 1314 | levelstr, errno); |
| 1315 | goto err; |
| 1316 | } |
| 1317 | |
| 1318 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1319 | /* use data to store event level */ |
| 1320 | vmpressure_hinfo[level_idx].data = level_idx; |
| 1321 | vmpressure_hinfo[level_idx].handler = mp_event_common; |
| 1322 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1323 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 1324 | if (ret == -1) { |
| 1325 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 1326 | goto err; |
| 1327 | } |
| 1328 | maxevents++; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1329 | mpevfd[level] = evfd; |
Suren Baghdasaryan | ab05d67 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 1330 | close(evctlfd); |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1331 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1332 | |
| 1333 | err: |
| 1334 | close(evfd); |
| 1335 | err_eventfd: |
| 1336 | close(evctlfd); |
| 1337 | err_open_evctlfd: |
| 1338 | close(mpfd); |
| 1339 | err_open_mpfd: |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1340 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1343 | static int init(void) { |
| 1344 | struct epoll_event epev; |
| 1345 | int i; |
| 1346 | int ret; |
| 1347 | |
| 1348 | page_k = sysconf(_SC_PAGESIZE); |
| 1349 | if (page_k == -1) |
| 1350 | page_k = PAGE_SIZE; |
| 1351 | page_k /= 1024; |
| 1352 | |
| 1353 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 1354 | if (epollfd == -1) { |
| 1355 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1359 | // mark data connections as not connected |
| 1360 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1361 | data_sock[i].sock = -1; |
| 1362 | } |
| 1363 | |
| 1364 | ctrl_sock.sock = android_get_control_socket("lmkd"); |
| 1365 | if (ctrl_sock.sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1366 | ALOGE("get lmkd control socket failed"); |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1370 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1371 | if (ret < 0) { |
| 1372 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1377 | ctrl_sock.handler_info.handler = ctrl_connect_handler; |
| 1378 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); |
| 1379 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1380 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 1381 | return -1; |
| 1382 | } |
| 1383 | maxevents++; |
| 1384 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1385 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
Suren Baghdasaryan | 2088f6a | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 1386 | use_inkernel_interface = has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1387 | |
| 1388 | if (use_inkernel_interface) { |
| 1389 | ALOGI("Using in-kernel low memory killer interface"); |
| 1390 | } else { |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1391 | if (!init_mp_common(VMPRESS_LEVEL_LOW) || |
| 1392 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || |
| 1393 | !init_mp_common(VMPRESS_LEVEL_CRITICAL)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1394 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1395 | return -1; |
| 1396 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1399 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1400 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 1401 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 1402 | } |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static void mainloop(void) { |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1408 | struct event_handler_info* handler_info; |
| 1409 | struct epoll_event *evt; |
| 1410 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1411 | while (1) { |
| 1412 | struct epoll_event events[maxevents]; |
| 1413 | int nevents; |
| 1414 | int i; |
| 1415 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1416 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 1417 | |
| 1418 | if (nevents == -1) { |
| 1419 | if (errno == EINTR) |
| 1420 | continue; |
| 1421 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 1422 | continue; |
| 1423 | } |
| 1424 | |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1425 | /* |
| 1426 | * First pass to see if any data socket connections were dropped. |
| 1427 | * Dropped connection should be handled before any other events |
| 1428 | * to deallocate data connection and correctly handle cases when |
| 1429 | * connection gets dropped and reestablished in the same epoll cycle. |
| 1430 | * In such cases it's essential to handle connection closures first. |
| 1431 | */ |
| 1432 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1433 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { |
| 1434 | ALOGI("lmkd data connection dropped"); |
| 1435 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1436 | ctrl_data_close(handler_info->data); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /* Second pass to handle all other events */ |
| 1441 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1442 | if (evt->events & EPOLLERR) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1443 | ALOGD("EPOLLERR on event #%d", i); |
Suren Baghdasaryan | 06f7570 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1444 | if (evt->events & EPOLLHUP) { |
| 1445 | /* This case was handled in the first pass */ |
| 1446 | continue; |
| 1447 | } |
| 1448 | if (evt->data.ptr) { |
| 1449 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1450 | handler_info->handler(handler_info->data, evt->events); |
| 1451 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 1456 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1457 | struct sched_param param = { |
| 1458 | .sched_priority = 1, |
| 1459 | }; |
| 1460 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1461 | /* By default disable low level vmpressure events */ |
| 1462 | level_oomadj[VMPRESS_LEVEL_LOW] = |
| 1463 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); |
| 1464 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
| 1465 | property_get_int32("ro.lmk.medium", 800); |
| 1466 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
| 1467 | property_get_int32("ro.lmk.critical", 0); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1468 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | e5c96431 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 1469 | |
| 1470 | /* By default disable upgrade/downgrade logic */ |
| 1471 | enable_pressure_upgrade = |
| 1472 | property_get_bool("ro.lmk.critical_upgrade", false); |
| 1473 | upgrade_pressure = |
| 1474 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); |
| 1475 | downgrade_pressure = |
| 1476 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1477 | kill_heaviest_task = |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1478 | property_get_bool("ro.lmk.kill_heaviest_task", false); |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1479 | low_ram_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1480 | kill_timeout_ms = |
| 1481 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); |
Suren Baghdasaryan | c09b53d | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1482 | use_minfree_levels = |
| 1483 | property_get_bool("ro.lmk.use_minfree_levels", false); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 1484 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1485 | if (!init()) { |
| 1486 | if (!use_inkernel_interface) { |
| 1487 | /* |
| 1488 | * MCL_ONFAULT pins pages as they fault instead of loading |
| 1489 | * everything immediately all at once. (Which would be bad, |
| 1490 | * because as of this writing, we have a lot of mapped pages we |
| 1491 | * never use.) Old kernels will see MCL_ONFAULT and fail with |
| 1492 | * EINVAL; we ignore this failure. |
| 1493 | * |
| 1494 | * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 1495 | * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 1496 | * in pages. |
| 1497 | */ |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1498 | /* CAP_IPC_LOCK required */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1499 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { |
| 1500 | ALOGW("mlockall failed %s", strerror(errno)); |
| 1501 | } |
Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 1502 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1503 | /* CAP_NICE required */ |
| 1504 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 1505 | ALOGW("set SCHED_FIFO failed %s", strerror(errno)); |
| 1506 | } |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1509 | mainloop(); |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1510 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1511 | |
| 1512 | ALOGI("exiting"); |
| 1513 | return 0; |
| 1514 | } |