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