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 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 19 | #include <arpa/inet.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 21 | #include <inttypes.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> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 27 | #include <sys/epoll.h> |
| 28 | #include <sys/eventfd.h> |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 29 | #include <sys/mman.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 30 | #include <sys/socket.h> |
| 31 | #include <sys/types.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 35 | #include <cutils/sockets.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 36 | #include <log/log.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 37 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces |
| 40 | * to profile and correlate with OOM kills |
| 41 | */ |
| 42 | #ifdef LMKD_TRACE_KILLS |
| 43 | |
| 44 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
| 45 | #include <cutils/trace.h> |
| 46 | |
| 47 | #define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid); |
| 48 | #define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0); |
| 49 | |
| 50 | #else /* LMKD_TRACE_KILLS */ |
| 51 | |
| 52 | #define TRACE_KILL_START(pid) |
| 53 | #define TRACE_KILL_END() |
| 54 | |
| 55 | #endif /* LMKD_TRACE_KILLS */ |
| 56 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 57 | #ifndef __unused |
| 58 | #define __unused __attribute__((__unused__)) |
| 59 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 60 | |
| 61 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 62 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" |
| 63 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 64 | #define ZONEINFO_PATH "/proc/zoneinfo" |
| 65 | #define LINE_MAX 128 |
| 66 | |
| 67 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 68 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 69 | |
| 70 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 71 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 72 | |
| 73 | enum lmk_cmd { |
| 74 | LMK_TARGET, |
| 75 | LMK_PROCPRIO, |
| 76 | LMK_PROCREMOVE, |
| 77 | }; |
| 78 | |
| 79 | #define MAX_TARGETS 6 |
| 80 | /* |
| 81 | * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio |
| 82 | * values |
| 83 | */ |
| 84 | #define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1)) |
| 85 | |
| 86 | /* default to old in-kernel interface if no memory pressure events */ |
| 87 | static int use_inkernel_interface = 1; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 88 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 89 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 90 | /* memory pressure levels */ |
| 91 | enum vmpressure_level { |
| 92 | VMPRESS_LEVEL_LOW = 0, |
| 93 | VMPRESS_LEVEL_MEDIUM, |
| 94 | VMPRESS_LEVEL_CRITICAL, |
| 95 | VMPRESS_LEVEL_COUNT |
| 96 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 97 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 98 | static const char *level_name[] = { |
| 99 | "low", |
| 100 | "medium", |
| 101 | "critical" |
| 102 | }; |
| 103 | |
| 104 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
| 105 | static int mpevfd[VMPRESS_LEVEL_COUNT]; |
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; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 110 | static bool is_go_device; |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 111 | static bool kill_heaviest_task; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 112 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 113 | /* control socket listen and data */ |
| 114 | static int ctrl_lfd; |
| 115 | static int ctrl_dfd = -1; |
| 116 | static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */ |
| 117 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 118 | /* 3 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */ |
| 119 | #define MAX_EPOLL_EVENTS 5 |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 120 | static int epollfd; |
| 121 | static int maxevents; |
| 122 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 123 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 124 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 125 | #define OOM_SCORE_ADJ_MAX 1000 |
| 126 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 127 | static int lowmem_adj[MAX_TARGETS]; |
| 128 | static int lowmem_minfree[MAX_TARGETS]; |
| 129 | static int lowmem_targets_size; |
| 130 | |
| 131 | struct sysmeminfo { |
| 132 | int nr_free_pages; |
| 133 | int nr_file_pages; |
| 134 | int nr_shmem; |
| 135 | int totalreserve_pages; |
| 136 | }; |
| 137 | |
| 138 | struct adjslot_list { |
| 139 | struct adjslot_list *next; |
| 140 | struct adjslot_list *prev; |
| 141 | }; |
| 142 | |
| 143 | struct proc { |
| 144 | struct adjslot_list asl; |
| 145 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 146 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 147 | int oomadj; |
| 148 | struct proc *pidhash_next; |
| 149 | }; |
| 150 | |
| 151 | #define PIDHASH_SZ 1024 |
| 152 | static struct proc *pidhash[PIDHASH_SZ]; |
| 153 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 154 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 155 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 156 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 157 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 158 | /* PAGE_SIZE / 1024 */ |
| 159 | static long page_k; |
| 160 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 161 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 162 | { |
| 163 | ssize_t ret = 0; |
| 164 | |
| 165 | while (max_len > 0) { |
| 166 | ssize_t r = read(fd, buf, max_len); |
| 167 | if (r == 0) { |
| 168 | break; |
| 169 | } |
| 170 | if (r == -1) { |
| 171 | return -1; |
| 172 | } |
| 173 | ret += r; |
| 174 | buf += r; |
| 175 | max_len -= r; |
| 176 | } |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 181 | static struct proc *pid_lookup(int pid) { |
| 182 | struct proc *procp; |
| 183 | |
| 184 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 185 | procp = procp->pidhash_next) |
| 186 | ; |
| 187 | |
| 188 | return procp; |
| 189 | } |
| 190 | |
| 191 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 192 | { |
| 193 | struct adjslot_list *next = head->next; |
| 194 | new->prev = head; |
| 195 | new->next = next; |
| 196 | next->prev = new; |
| 197 | head->next = new; |
| 198 | } |
| 199 | |
| 200 | static void adjslot_remove(struct adjslot_list *old) |
| 201 | { |
| 202 | struct adjslot_list *prev = old->prev; |
| 203 | struct adjslot_list *next = old->next; |
| 204 | next->prev = prev; |
| 205 | prev->next = next; |
| 206 | } |
| 207 | |
| 208 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 209 | struct adjslot_list *asl = head->prev; |
| 210 | |
| 211 | return asl == head ? NULL : asl; |
| 212 | } |
| 213 | |
| 214 | static void proc_slot(struct proc *procp) { |
| 215 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 216 | |
| 217 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 218 | } |
| 219 | |
| 220 | static void proc_unslot(struct proc *procp) { |
| 221 | adjslot_remove(&procp->asl); |
| 222 | } |
| 223 | |
| 224 | static void proc_insert(struct proc *procp) { |
| 225 | int hval = pid_hashfn(procp->pid); |
| 226 | |
| 227 | procp->pidhash_next = pidhash[hval]; |
| 228 | pidhash[hval] = procp; |
| 229 | proc_slot(procp); |
| 230 | } |
| 231 | |
| 232 | static int pid_remove(int pid) { |
| 233 | int hval = pid_hashfn(pid); |
| 234 | struct proc *procp; |
| 235 | struct proc *prevp; |
| 236 | |
| 237 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 238 | procp = procp->pidhash_next) |
| 239 | prevp = procp; |
| 240 | |
| 241 | if (!procp) |
| 242 | return -1; |
| 243 | |
| 244 | if (!prevp) |
| 245 | pidhash[hval] = procp->pidhash_next; |
| 246 | else |
| 247 | prevp->pidhash_next = procp->pidhash_next; |
| 248 | |
| 249 | proc_unslot(procp); |
| 250 | free(procp); |
| 251 | return 0; |
| 252 | } |
| 253 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 254 | static void writefilestring(const char *path, char *s) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 255 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 256 | int len = strlen(s); |
| 257 | int ret; |
| 258 | |
| 259 | if (fd < 0) { |
| 260 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | ret = write(fd, s, len); |
| 265 | if (ret < 0) { |
| 266 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 267 | } else if (ret < len) { |
| 268 | ALOGE("Short write on %s; length=%d", path, ret); |
| 269 | } |
| 270 | |
| 271 | close(fd); |
| 272 | } |
| 273 | |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 274 | static void cmd_procprio(int pid, int uid, int oomadj) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 275 | struct proc *procp; |
| 276 | char path[80]; |
| 277 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 278 | int soft_limit_mult; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 279 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 280 | if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 281 | ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj); |
| 282 | return; |
| 283 | } |
| 284 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 285 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid); |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 286 | snprintf(val, sizeof(val), "%d", oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 287 | writefilestring(path, val); |
| 288 | |
| 289 | if (use_inkernel_interface) |
| 290 | return; |
| 291 | |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 292 | if (oomadj >= 900) { |
| 293 | soft_limit_mult = 0; |
| 294 | } else if (oomadj >= 800) { |
| 295 | soft_limit_mult = 0; |
| 296 | } else if (oomadj >= 700) { |
| 297 | soft_limit_mult = 0; |
| 298 | } else if (oomadj >= 600) { |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 299 | // Launcher should be perceptible, don't kill it. |
| 300 | oomadj = 200; |
| 301 | soft_limit_mult = 1; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 302 | } else if (oomadj >= 500) { |
| 303 | soft_limit_mult = 0; |
| 304 | } else if (oomadj >= 400) { |
| 305 | soft_limit_mult = 0; |
| 306 | } else if (oomadj >= 300) { |
| 307 | soft_limit_mult = 1; |
| 308 | } else if (oomadj >= 200) { |
| 309 | soft_limit_mult = 2; |
| 310 | } else if (oomadj >= 100) { |
| 311 | soft_limit_mult = 10; |
| 312 | } else if (oomadj >= 0) { |
| 313 | soft_limit_mult = 20; |
| 314 | } else { |
| 315 | // Persistent processes will have a large |
| 316 | // soft limit 512MB. |
| 317 | soft_limit_mult = 64; |
| 318 | } |
| 319 | |
| 320 | snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid); |
| 321 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
| 322 | writefilestring(path, val); |
| 323 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 324 | procp = pid_lookup(pid); |
| 325 | if (!procp) { |
| 326 | procp = malloc(sizeof(struct proc)); |
| 327 | if (!procp) { |
| 328 | // Oh, the irony. May need to rebuild our state. |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | procp->pid = pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 333 | procp->uid = uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 334 | procp->oomadj = oomadj; |
| 335 | proc_insert(procp); |
| 336 | } else { |
| 337 | proc_unslot(procp); |
| 338 | procp->oomadj = oomadj; |
| 339 | proc_slot(procp); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static void cmd_procremove(int pid) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 344 | if (use_inkernel_interface) |
| 345 | return; |
| 346 | |
| 347 | pid_remove(pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void cmd_target(int ntargets, int *params) { |
| 351 | int i; |
| 352 | |
| 353 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 354 | return; |
| 355 | |
| 356 | for (i = 0; i < ntargets; i++) { |
| 357 | lowmem_minfree[i] = ntohl(*params++); |
| 358 | lowmem_adj[i] = ntohl(*params++); |
| 359 | } |
| 360 | |
| 361 | lowmem_targets_size = ntargets; |
| 362 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 363 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 364 | char minfreestr[128]; |
| 365 | char killpriostr[128]; |
| 366 | |
| 367 | minfreestr[0] = '\0'; |
| 368 | killpriostr[0] = '\0'; |
| 369 | |
| 370 | for (i = 0; i < lowmem_targets_size; i++) { |
| 371 | char val[40]; |
| 372 | |
| 373 | if (i) { |
| 374 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 375 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 376 | } |
| 377 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 378 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 379 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 380 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 381 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 382 | } |
| 383 | |
| 384 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr); |
| 385 | writefilestring(INKERNEL_ADJ_PATH, killpriostr); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void ctrl_data_close(void) { |
| 390 | ALOGI("Closing Activity Manager data connection"); |
| 391 | close(ctrl_dfd); |
| 392 | ctrl_dfd = -1; |
| 393 | maxevents--; |
| 394 | } |
| 395 | |
| 396 | static int ctrl_data_read(char *buf, size_t bufsz) { |
| 397 | int ret = 0; |
| 398 | |
| 399 | ret = read(ctrl_dfd, buf, bufsz); |
| 400 | |
| 401 | if (ret == -1) { |
| 402 | ALOGE("control data socket read failed; errno=%d", errno); |
| 403 | } else if (ret == 0) { |
| 404 | ALOGE("Got EOF on control data socket"); |
| 405 | ret = -1; |
| 406 | } |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static void ctrl_command_handler(void) { |
| 412 | int ibuf[CTRL_PACKET_MAX / sizeof(int)]; |
| 413 | int len; |
| 414 | int cmd = -1; |
| 415 | int nargs; |
| 416 | int targets; |
| 417 | |
| 418 | len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX); |
| 419 | if (len <= 0) |
| 420 | return; |
| 421 | |
| 422 | nargs = len / sizeof(int) - 1; |
| 423 | if (nargs < 0) |
| 424 | goto wronglen; |
| 425 | |
| 426 | cmd = ntohl(ibuf[0]); |
| 427 | |
| 428 | switch(cmd) { |
| 429 | case LMK_TARGET: |
| 430 | targets = nargs / 2; |
| 431 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 432 | goto wronglen; |
| 433 | cmd_target(targets, &ibuf[1]); |
| 434 | break; |
| 435 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 436 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 437 | goto wronglen; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 438 | cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3])); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 439 | break; |
| 440 | case LMK_PROCREMOVE: |
| 441 | if (nargs != 1) |
| 442 | goto wronglen; |
| 443 | cmd_procremove(ntohl(ibuf[1])); |
| 444 | break; |
| 445 | default: |
| 446 | ALOGE("Received unknown command code %d", cmd); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | return; |
| 451 | |
| 452 | wronglen: |
| 453 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 454 | } |
| 455 | |
| 456 | static void ctrl_data_handler(uint32_t events) { |
| 457 | if (events & EPOLLHUP) { |
| 458 | ALOGI("ActivityManager disconnected"); |
| 459 | if (!ctrl_dfd_reopened) |
| 460 | ctrl_data_close(); |
| 461 | } else if (events & EPOLLIN) { |
| 462 | ctrl_command_handler(); |
| 463 | } |
| 464 | } |
| 465 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 466 | static void ctrl_connect_handler(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 467 | struct epoll_event epev; |
| 468 | |
| 469 | if (ctrl_dfd >= 0) { |
| 470 | ctrl_data_close(); |
| 471 | ctrl_dfd_reopened = 1; |
| 472 | } |
| 473 | |
Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 474 | ctrl_dfd = accept(ctrl_lfd, NULL, NULL); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 475 | |
| 476 | if (ctrl_dfd < 0) { |
| 477 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | ALOGI("ActivityManager connected"); |
| 482 | maxevents++; |
| 483 | epev.events = EPOLLIN; |
| 484 | epev.data.ptr = (void *)ctrl_data_handler; |
| 485 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) { |
| 486 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 487 | ctrl_data_close(); |
| 488 | return; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static int zoneinfo_parse_protection(char *cp) { |
| 493 | int max = 0; |
| 494 | int zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 495 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 496 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 497 | for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 498 | zoneval = strtol(cp, &cp, 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 499 | if (zoneval > max) |
| 500 | max = zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 501 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 502 | |
| 503 | return max; |
| 504 | } |
| 505 | |
| 506 | static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) { |
| 507 | char *cp = line; |
| 508 | char *ap; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 509 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 510 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 511 | cp = strtok_r(line, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 512 | if (!cp) |
| 513 | return; |
| 514 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 515 | ap = strtok_r(NULL, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 516 | if (!ap) |
| 517 | return; |
| 518 | |
| 519 | if (!strcmp(cp, "nr_free_pages")) |
| 520 | mip->nr_free_pages += strtol(ap, NULL, 0); |
| 521 | else if (!strcmp(cp, "nr_file_pages")) |
| 522 | mip->nr_file_pages += strtol(ap, NULL, 0); |
| 523 | else if (!strcmp(cp, "nr_shmem")) |
| 524 | mip->nr_shmem += strtol(ap, NULL, 0); |
| 525 | else if (!strcmp(cp, "high")) |
| 526 | mip->totalreserve_pages += strtol(ap, NULL, 0); |
| 527 | else if (!strcmp(cp, "protection:")) |
| 528 | mip->totalreserve_pages += zoneinfo_parse_protection(ap); |
| 529 | } |
| 530 | |
| 531 | static int zoneinfo_parse(struct sysmeminfo *mip) { |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 532 | int fd; |
| 533 | ssize_t size; |
| 534 | char buf[PAGE_SIZE]; |
| 535 | char *save_ptr; |
| 536 | char *line; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 537 | |
| 538 | memset(mip, 0, sizeof(struct sysmeminfo)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 539 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 540 | fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 541 | if (fd == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 542 | ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno); |
| 543 | return -1; |
| 544 | } |
| 545 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 546 | size = read_all(fd, buf, sizeof(buf) - 1); |
| 547 | if (size < 0) { |
| 548 | ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno); |
| 549 | close(fd); |
| 550 | return -1; |
| 551 | } |
| 552 | ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large"); |
| 553 | buf[size] = 0; |
| 554 | |
| 555 | for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr)) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 556 | zoneinfo_parse_line(line, mip); |
| 557 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 558 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int proc_get_size(int pid) { |
| 563 | char path[PATH_MAX]; |
| 564 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 565 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 566 | int rss = 0; |
| 567 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 568 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 569 | |
| 570 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 571 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 572 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 573 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 574 | |
| 575 | ret = read_all(fd, line, sizeof(line) - 1); |
| 576 | if (ret < 0) { |
| 577 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 582 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 583 | return rss; |
| 584 | } |
| 585 | |
| 586 | static char *proc_get_name(int pid) { |
| 587 | char path[PATH_MAX]; |
| 588 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 589 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 590 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 591 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 592 | |
| 593 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 594 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 595 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 596 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 597 | ret = read_all(fd, line, sizeof(line) - 1); |
| 598 | close(fd); |
| 599 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | cp = strchr(line, ' '); |
| 604 | if (cp) |
| 605 | *cp = '\0'; |
| 606 | |
| 607 | return line; |
| 608 | } |
| 609 | |
| 610 | static struct proc *proc_adj_lru(int oomadj) { |
| 611 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 612 | } |
| 613 | |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 614 | static struct proc *proc_get_heaviest(int oomadj) { |
| 615 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 616 | struct adjslot_list *curr = head->next; |
| 617 | struct proc *maxprocp = NULL; |
| 618 | int maxsize = 0; |
| 619 | while (curr != head) { |
| 620 | int pid = ((struct proc *)curr)->pid; |
| 621 | int tasksize = proc_get_size(pid); |
| 622 | if (tasksize <= 0) { |
| 623 | struct adjslot_list *next = curr->next; |
| 624 | pid_remove(pid); |
| 625 | curr = next; |
| 626 | } else { |
| 627 | if (tasksize > maxsize) { |
| 628 | maxsize = tasksize; |
| 629 | maxprocp = (struct proc *)curr; |
| 630 | } |
| 631 | curr = curr->next; |
| 632 | } |
| 633 | } |
| 634 | return maxprocp; |
| 635 | } |
| 636 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 637 | /* 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] | 638 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 639 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 640 | int pid = procp->pid; |
| 641 | uid_t uid = procp->uid; |
| 642 | char *taskname; |
| 643 | int tasksize; |
| 644 | int r; |
| 645 | |
| 646 | taskname = proc_get_name(pid); |
| 647 | if (!taskname) { |
| 648 | pid_remove(pid); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | tasksize = proc_get_size(pid); |
| 653 | if (tasksize <= 0) { |
| 654 | pid_remove(pid); |
| 655 | return -1; |
| 656 | } |
| 657 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 658 | TRACE_KILL_START(pid); |
| 659 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 660 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 661 | ALOGI( |
| 662 | "Killing '%s' (%d), uid %d, adj %d\n" |
| 663 | " to free %ldkB because system is under %s memory pressure oom_adj %d\n", |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 664 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 665 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 666 | pid_remove(pid); |
| 667 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 668 | TRACE_KILL_END(); |
| 669 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 670 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 671 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 672 | return -1; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 673 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 674 | |
| 675 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Find a process to kill based on the current (possibly estimated) free memory |
| 680 | * and cached memory sizes. Returns the size of the killed processes. |
| 681 | */ |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 682 | static int find_and_kill_process(enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 683 | int i; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 684 | int killed_size = 0; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 685 | int min_score_adj = level_oomadj[level]; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 686 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 687 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 688 | struct proc *procp; |
| 689 | |
| 690 | retry: |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 691 | if (kill_heaviest_task) |
| 692 | procp = proc_get_heaviest(i); |
| 693 | else |
| 694 | procp = proc_adj_lru(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 695 | |
| 696 | if (procp) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 697 | killed_size = kill_one_process(procp, min_score_adj, level); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 698 | if (killed_size < 0) { |
| 699 | goto retry; |
| 700 | } else { |
| 701 | return killed_size; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 709 | static int64_t get_memory_usage(const char* path) { |
| 710 | int ret; |
| 711 | int64_t mem_usage; |
| 712 | char buf[32]; |
| 713 | int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 714 | if (fd == -1) { |
| 715 | ALOGE("%s open: errno=%d", path, errno); |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | ret = read_all(fd, buf, sizeof(buf) - 1); |
| 720 | close(fd); |
| 721 | if (ret < 0) { |
| 722 | ALOGE("%s error: errno=%d", path, errno); |
| 723 | return -1; |
| 724 | } |
| 725 | sscanf(buf, "%" SCNd64, &mem_usage); |
| 726 | if (mem_usage == 0) { |
| 727 | ALOGE("No memory!"); |
| 728 | return -1; |
| 729 | } |
| 730 | return mem_usage; |
| 731 | } |
| 732 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 733 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 734 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 735 | level + 1 : level); |
| 736 | } |
| 737 | |
| 738 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 739 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 740 | level - 1 : level); |
| 741 | } |
| 742 | |
| 743 | static void mp_event_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 744 | int ret; |
| 745 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 746 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 747 | int64_t mem_pressure; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 748 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 749 | ret = read(mpevfd[level], &evcount, sizeof(evcount)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 750 | if (ret < 0) |
| 751 | ALOGE("Error reading memory pressure event fd; errno=%d", |
| 752 | errno); |
| 753 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 754 | mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE); |
| 755 | memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE); |
| 756 | if (memsw_usage < 0 || mem_usage < 0) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 757 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 758 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 759 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 760 | // Calculate percent for swappinness. |
| 761 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 762 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 763 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 764 | // We are swapping too much. |
| 765 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 766 | level = upgrade_level(level); |
| 767 | if (debug_process_killing) { |
| 768 | ALOGI("Event upgraded to %s", level_name[level]); |
| 769 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 773 | // If the pressure is larger than downgrade_pressure lmk will not |
| 774 | // kill any process, since enough memory is available. |
| 775 | if (mem_pressure > downgrade_pressure) { |
| 776 | if (debug_process_killing) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 777 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 778 | } |
| 779 | return; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 780 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 781 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 782 | if (debug_process_killing) { |
| 783 | ALOGI("Downgrade critical memory pressure"); |
| 784 | } |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 785 | // Downgrade event, since enough memory available. |
| 786 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 789 | do_kill: |
| 790 | if (find_and_kill_process(level) == 0) { |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 791 | if (debug_process_killing) { |
| 792 | ALOGI("Nothing to kill"); |
| 793 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 794 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 797 | static void mp_event_low(uint32_t events __unused) { |
| 798 | mp_event_common(VMPRESS_LEVEL_LOW); |
| 799 | } |
| 800 | |
| 801 | static void mp_event_medium(uint32_t events __unused) { |
| 802 | mp_event_common(VMPRESS_LEVEL_MEDIUM); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static void mp_event_critical(uint32_t events __unused) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 806 | mp_event_common(VMPRESS_LEVEL_CRITICAL); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 809 | static bool init_mp_common(void *event_handler, enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 810 | int mpfd; |
| 811 | int evfd; |
| 812 | int evctlfd; |
| 813 | char buf[256]; |
| 814 | struct epoll_event epev; |
| 815 | int ret; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 816 | const char *levelstr = level_name[level]; |
| 817 | |
| 818 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 819 | ALOGI("%s pressure events are disabled", levelstr); |
| 820 | return true; |
| 821 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 822 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 823 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 824 | if (mpfd < 0) { |
| 825 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 826 | goto err_open_mpfd; |
| 827 | } |
| 828 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 829 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 830 | if (evctlfd < 0) { |
| 831 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 832 | goto err_open_evctlfd; |
| 833 | } |
| 834 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 835 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 836 | if (evfd < 0) { |
| 837 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 838 | goto err_eventfd; |
| 839 | } |
| 840 | |
| 841 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 842 | if (ret >= (ssize_t)sizeof(buf)) { |
| 843 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 844 | goto err; |
| 845 | } |
| 846 | |
| 847 | ret = write(evctlfd, buf, strlen(buf) + 1); |
| 848 | if (ret == -1) { |
| 849 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 850 | levelstr, errno); |
| 851 | goto err; |
| 852 | } |
| 853 | |
| 854 | epev.events = EPOLLIN; |
| 855 | epev.data.ptr = event_handler; |
| 856 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 857 | if (ret == -1) { |
| 858 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 859 | goto err; |
| 860 | } |
| 861 | maxevents++; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 862 | mpevfd[level] = evfd; |
Suren Baghdasaryan | ab05d67 | 2018-01-04 08:54:53 -0800 | [diff] [blame^] | 863 | close(evctlfd); |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 864 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 865 | |
| 866 | err: |
| 867 | close(evfd); |
| 868 | err_eventfd: |
| 869 | close(evctlfd); |
| 870 | err_open_evctlfd: |
| 871 | close(mpfd); |
| 872 | err_open_mpfd: |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 873 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 876 | static int init(void) { |
| 877 | struct epoll_event epev; |
| 878 | int i; |
| 879 | int ret; |
| 880 | |
| 881 | page_k = sysconf(_SC_PAGESIZE); |
| 882 | if (page_k == -1) |
| 883 | page_k = PAGE_SIZE; |
| 884 | page_k /= 1024; |
| 885 | |
| 886 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 887 | if (epollfd == -1) { |
| 888 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 889 | return -1; |
| 890 | } |
| 891 | |
| 892 | ctrl_lfd = android_get_control_socket("lmkd"); |
| 893 | if (ctrl_lfd < 0) { |
| 894 | ALOGE("get lmkd control socket failed"); |
| 895 | return -1; |
| 896 | } |
| 897 | |
| 898 | ret = listen(ctrl_lfd, 1); |
| 899 | if (ret < 0) { |
| 900 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | epev.events = EPOLLIN; |
| 905 | epev.data.ptr = (void *)ctrl_connect_handler; |
| 906 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) { |
| 907 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 908 | return -1; |
| 909 | } |
| 910 | maxevents++; |
| 911 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 912 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
| 913 | use_inkernel_interface = has_inkernel_module && !is_go_device; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 914 | |
| 915 | if (use_inkernel_interface) { |
| 916 | ALOGI("Using in-kernel low memory killer interface"); |
| 917 | } else { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 918 | if (!init_mp_common((void *)&mp_event_low, VMPRESS_LEVEL_LOW) || |
| 919 | !init_mp_common((void *)&mp_event_medium, VMPRESS_LEVEL_MEDIUM) || |
| 920 | !init_mp_common((void *)&mp_event_critical, |
| 921 | VMPRESS_LEVEL_CRITICAL)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 922 | 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] | 923 | return -1; |
| 924 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 927 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 928 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 929 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 930 | } |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static void mainloop(void) { |
| 936 | while (1) { |
| 937 | struct epoll_event events[maxevents]; |
| 938 | int nevents; |
| 939 | int i; |
| 940 | |
| 941 | ctrl_dfd_reopened = 0; |
| 942 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 943 | |
| 944 | if (nevents == -1) { |
| 945 | if (errno == EINTR) |
| 946 | continue; |
| 947 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 948 | continue; |
| 949 | } |
| 950 | |
| 951 | for (i = 0; i < nevents; ++i) { |
| 952 | if (events[i].events & EPOLLERR) |
| 953 | ALOGD("EPOLLERR on event #%d", i); |
| 954 | if (events[i].data.ptr) |
| 955 | (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 960 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 961 | struct sched_param param = { |
| 962 | .sched_priority = 1, |
| 963 | }; |
| 964 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 965 | /* By default disable low level vmpressure events */ |
| 966 | level_oomadj[VMPRESS_LEVEL_LOW] = |
| 967 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); |
| 968 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
| 969 | property_get_int32("ro.lmk.medium", 800); |
| 970 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
| 971 | property_get_int32("ro.lmk.critical", 0); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 972 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | e5c96431 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 973 | |
| 974 | /* By default disable upgrade/downgrade logic */ |
| 975 | enable_pressure_upgrade = |
| 976 | property_get_bool("ro.lmk.critical_upgrade", false); |
| 977 | upgrade_pressure = |
| 978 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); |
| 979 | downgrade_pressure = |
| 980 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 981 | kill_heaviest_task = |
| 982 | property_get_bool("ro.lmk.kill_heaviest_task", true); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 983 | is_go_device = property_get_bool("ro.config.low_ram", false); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 984 | |
Daniel Colascione | d39adf2 | 2018-01-05 14:59:55 -0800 | [diff] [blame] | 985 | // MCL_ONFAULT pins pages as they fault instead of loading |
| 986 | // everything immediately all at once. (Which would be bad, |
| 987 | // because as of this writing, we have a lot of mapped pages we |
| 988 | // never use.) Old kernels will see MCL_ONFAULT and fail with |
| 989 | // EINVAL; we ignore this failure. |
| 990 | // |
| 991 | // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 992 | // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 993 | // in pages. |
| 994 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && errno != EINVAL) |
Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 995 | ALOGW("mlockall failed: errno=%d", errno); |
| 996 | |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 997 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 998 | if (!init()) |
| 999 | mainloop(); |
| 1000 | |
| 1001 | ALOGI("exiting"); |
| 1002 | return 0; |
| 1003 | } |