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> |
| 21 | #include <signal.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 26 | #include <sys/epoll.h> |
| 27 | #include <sys/eventfd.h> |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/types.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 33 | #include <cutils/sockets.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 34 | #include <log/log.h> |
Colin Cross | fef9522 | 2014-06-11 14:53:41 -0700 | [diff] [blame] | 35 | #include <processgroup/processgroup.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 36 | |
| 37 | #ifndef __unused |
| 38 | #define __unused __attribute__((__unused__)) |
| 39 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 40 | |
| 41 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
| 42 | #define MEMPRESSURE_WATCH_LEVEL "medium" |
| 43 | #define ZONEINFO_PATH "/proc/zoneinfo" |
| 44 | #define LINE_MAX 128 |
| 45 | |
| 46 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 47 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 48 | |
| 49 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
| 50 | |
| 51 | enum lmk_cmd { |
| 52 | LMK_TARGET, |
| 53 | LMK_PROCPRIO, |
| 54 | LMK_PROCREMOVE, |
| 55 | }; |
| 56 | |
| 57 | #define MAX_TARGETS 6 |
| 58 | /* |
| 59 | * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio |
| 60 | * values |
| 61 | */ |
| 62 | #define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1)) |
| 63 | |
| 64 | /* default to old in-kernel interface if no memory pressure events */ |
| 65 | static int use_inkernel_interface = 1; |
| 66 | |
| 67 | /* memory pressure level medium event */ |
| 68 | static int mpevfd; |
| 69 | |
| 70 | /* control socket listen and data */ |
| 71 | static int ctrl_lfd; |
| 72 | static int ctrl_dfd = -1; |
| 73 | static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */ |
| 74 | |
| 75 | /* 1 memory pressure level, 1 ctrl listen socket, 1 ctrl data socket */ |
| 76 | #define MAX_EPOLL_EVENTS 3 |
| 77 | static int epollfd; |
| 78 | static int maxevents; |
| 79 | |
| 80 | #define OOM_DISABLE (-17) |
| 81 | /* inclusive */ |
| 82 | #define OOM_ADJUST_MIN (-16) |
| 83 | #define OOM_ADJUST_MAX 15 |
| 84 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 85 | /* kernel OOM score values */ |
| 86 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 87 | #define OOM_SCORE_ADJ_MAX 1000 |
| 88 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 89 | static int lowmem_adj[MAX_TARGETS]; |
| 90 | static int lowmem_minfree[MAX_TARGETS]; |
| 91 | static int lowmem_targets_size; |
| 92 | |
| 93 | struct sysmeminfo { |
| 94 | int nr_free_pages; |
| 95 | int nr_file_pages; |
| 96 | int nr_shmem; |
| 97 | int totalreserve_pages; |
| 98 | }; |
| 99 | |
| 100 | struct adjslot_list { |
| 101 | struct adjslot_list *next; |
| 102 | struct adjslot_list *prev; |
| 103 | }; |
| 104 | |
| 105 | struct proc { |
| 106 | struct adjslot_list asl; |
| 107 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 108 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 109 | int oomadj; |
| 110 | struct proc *pidhash_next; |
| 111 | }; |
| 112 | |
| 113 | #define PIDHASH_SZ 1024 |
| 114 | static struct proc *pidhash[PIDHASH_SZ]; |
| 115 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 116 | |
| 117 | #define ADJTOSLOT(adj) (adj + -OOM_ADJUST_MIN) |
| 118 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_ADJUST_MAX) + 1]; |
| 119 | |
| 120 | /* |
| 121 | * Wait 1-2 seconds for the death report of a killed process prior to |
| 122 | * considering killing more processes. |
| 123 | */ |
| 124 | #define KILL_TIMEOUT 2 |
| 125 | /* Time of last process kill we initiated, stop me before I kill again */ |
| 126 | static time_t kill_lasttime; |
| 127 | |
| 128 | /* PAGE_SIZE / 1024 */ |
| 129 | static long page_k; |
| 130 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 131 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 132 | { |
| 133 | ssize_t ret = 0; |
| 134 | |
| 135 | while (max_len > 0) { |
| 136 | ssize_t r = read(fd, buf, max_len); |
| 137 | if (r == 0) { |
| 138 | break; |
| 139 | } |
| 140 | if (r == -1) { |
| 141 | return -1; |
| 142 | } |
| 143 | ret += r; |
| 144 | buf += r; |
| 145 | max_len -= r; |
| 146 | } |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 151 | static int lowmem_oom_adj_to_oom_score_adj(int oom_adj) |
| 152 | { |
| 153 | if (oom_adj == OOM_ADJUST_MAX) |
| 154 | return OOM_SCORE_ADJ_MAX; |
| 155 | else |
| 156 | return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; |
| 157 | } |
| 158 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 159 | static struct proc *pid_lookup(int pid) { |
| 160 | struct proc *procp; |
| 161 | |
| 162 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 163 | procp = procp->pidhash_next) |
| 164 | ; |
| 165 | |
| 166 | return procp; |
| 167 | } |
| 168 | |
| 169 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 170 | { |
| 171 | struct adjslot_list *next = head->next; |
| 172 | new->prev = head; |
| 173 | new->next = next; |
| 174 | next->prev = new; |
| 175 | head->next = new; |
| 176 | } |
| 177 | |
| 178 | static void adjslot_remove(struct adjslot_list *old) |
| 179 | { |
| 180 | struct adjslot_list *prev = old->prev; |
| 181 | struct adjslot_list *next = old->next; |
| 182 | next->prev = prev; |
| 183 | prev->next = next; |
| 184 | } |
| 185 | |
| 186 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 187 | struct adjslot_list *asl = head->prev; |
| 188 | |
| 189 | return asl == head ? NULL : asl; |
| 190 | } |
| 191 | |
| 192 | static void proc_slot(struct proc *procp) { |
| 193 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 194 | |
| 195 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 196 | } |
| 197 | |
| 198 | static void proc_unslot(struct proc *procp) { |
| 199 | adjslot_remove(&procp->asl); |
| 200 | } |
| 201 | |
| 202 | static void proc_insert(struct proc *procp) { |
| 203 | int hval = pid_hashfn(procp->pid); |
| 204 | |
| 205 | procp->pidhash_next = pidhash[hval]; |
| 206 | pidhash[hval] = procp; |
| 207 | proc_slot(procp); |
| 208 | } |
| 209 | |
| 210 | static int pid_remove(int pid) { |
| 211 | int hval = pid_hashfn(pid); |
| 212 | struct proc *procp; |
| 213 | struct proc *prevp; |
| 214 | |
| 215 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 216 | procp = procp->pidhash_next) |
| 217 | prevp = procp; |
| 218 | |
| 219 | if (!procp) |
| 220 | return -1; |
| 221 | |
| 222 | if (!prevp) |
| 223 | pidhash[hval] = procp->pidhash_next; |
| 224 | else |
| 225 | prevp->pidhash_next = procp->pidhash_next; |
| 226 | |
| 227 | proc_unslot(procp); |
| 228 | free(procp); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static void writefilestring(char *path, char *s) { |
| 233 | int fd = open(path, O_WRONLY); |
| 234 | int len = strlen(s); |
| 235 | int ret; |
| 236 | |
| 237 | if (fd < 0) { |
| 238 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | ret = write(fd, s, len); |
| 243 | if (ret < 0) { |
| 244 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 245 | } else if (ret < len) { |
| 246 | ALOGE("Short write on %s; length=%d", path, ret); |
| 247 | } |
| 248 | |
| 249 | close(fd); |
| 250 | } |
| 251 | |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 252 | static void cmd_procprio(int pid, int uid, int oomadj) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 253 | struct proc *procp; |
| 254 | char path[80]; |
| 255 | char val[20]; |
| 256 | |
| 257 | if (oomadj < OOM_DISABLE || oomadj > OOM_ADJUST_MAX) { |
| 258 | ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj); |
| 259 | return; |
| 260 | } |
| 261 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 262 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid); |
| 263 | snprintf(val, sizeof(val), "%d", lowmem_oom_adj_to_oom_score_adj(oomadj)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 264 | writefilestring(path, val); |
| 265 | |
| 266 | if (use_inkernel_interface) |
| 267 | return; |
| 268 | |
| 269 | procp = pid_lookup(pid); |
| 270 | if (!procp) { |
| 271 | procp = malloc(sizeof(struct proc)); |
| 272 | if (!procp) { |
| 273 | // Oh, the irony. May need to rebuild our state. |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | procp->pid = pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 278 | procp->uid = uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 279 | procp->oomadj = oomadj; |
| 280 | proc_insert(procp); |
| 281 | } else { |
| 282 | proc_unslot(procp); |
| 283 | procp->oomadj = oomadj; |
| 284 | proc_slot(procp); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static void cmd_procremove(int pid) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 289 | if (use_inkernel_interface) |
| 290 | return; |
| 291 | |
| 292 | pid_remove(pid); |
| 293 | kill_lasttime = 0; |
| 294 | } |
| 295 | |
| 296 | static void cmd_target(int ntargets, int *params) { |
| 297 | int i; |
| 298 | |
| 299 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 300 | return; |
| 301 | |
| 302 | for (i = 0; i < ntargets; i++) { |
| 303 | lowmem_minfree[i] = ntohl(*params++); |
| 304 | lowmem_adj[i] = ntohl(*params++); |
| 305 | } |
| 306 | |
| 307 | lowmem_targets_size = ntargets; |
| 308 | |
| 309 | if (use_inkernel_interface) { |
| 310 | char minfreestr[128]; |
| 311 | char killpriostr[128]; |
| 312 | |
| 313 | minfreestr[0] = '\0'; |
| 314 | killpriostr[0] = '\0'; |
| 315 | |
| 316 | for (i = 0; i < lowmem_targets_size; i++) { |
| 317 | char val[40]; |
| 318 | |
| 319 | if (i) { |
| 320 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 321 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 322 | } |
| 323 | |
| 324 | snprintf(val, sizeof(val), "%d", lowmem_minfree[i]); |
| 325 | strlcat(minfreestr, val, sizeof(minfreestr)); |
| 326 | snprintf(val, sizeof(val), "%d", lowmem_adj[i]); |
| 327 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 328 | } |
| 329 | |
| 330 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr); |
| 331 | writefilestring(INKERNEL_ADJ_PATH, killpriostr); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static void ctrl_data_close(void) { |
| 336 | ALOGI("Closing Activity Manager data connection"); |
| 337 | close(ctrl_dfd); |
| 338 | ctrl_dfd = -1; |
| 339 | maxevents--; |
| 340 | } |
| 341 | |
| 342 | static int ctrl_data_read(char *buf, size_t bufsz) { |
| 343 | int ret = 0; |
| 344 | |
| 345 | ret = read(ctrl_dfd, buf, bufsz); |
| 346 | |
| 347 | if (ret == -1) { |
| 348 | ALOGE("control data socket read failed; errno=%d", errno); |
| 349 | } else if (ret == 0) { |
| 350 | ALOGE("Got EOF on control data socket"); |
| 351 | ret = -1; |
| 352 | } |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static void ctrl_command_handler(void) { |
| 358 | int ibuf[CTRL_PACKET_MAX / sizeof(int)]; |
| 359 | int len; |
| 360 | int cmd = -1; |
| 361 | int nargs; |
| 362 | int targets; |
| 363 | |
| 364 | len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX); |
| 365 | if (len <= 0) |
| 366 | return; |
| 367 | |
| 368 | nargs = len / sizeof(int) - 1; |
| 369 | if (nargs < 0) |
| 370 | goto wronglen; |
| 371 | |
| 372 | cmd = ntohl(ibuf[0]); |
| 373 | |
| 374 | switch(cmd) { |
| 375 | case LMK_TARGET: |
| 376 | targets = nargs / 2; |
| 377 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 378 | goto wronglen; |
| 379 | cmd_target(targets, &ibuf[1]); |
| 380 | break; |
| 381 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 382 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 383 | goto wronglen; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 384 | cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3])); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 385 | break; |
| 386 | case LMK_PROCREMOVE: |
| 387 | if (nargs != 1) |
| 388 | goto wronglen; |
| 389 | cmd_procremove(ntohl(ibuf[1])); |
| 390 | break; |
| 391 | default: |
| 392 | ALOGE("Received unknown command code %d", cmd); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | return; |
| 397 | |
| 398 | wronglen: |
| 399 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 400 | } |
| 401 | |
| 402 | static void ctrl_data_handler(uint32_t events) { |
| 403 | if (events & EPOLLHUP) { |
| 404 | ALOGI("ActivityManager disconnected"); |
| 405 | if (!ctrl_dfd_reopened) |
| 406 | ctrl_data_close(); |
| 407 | } else if (events & EPOLLIN) { |
| 408 | ctrl_command_handler(); |
| 409 | } |
| 410 | } |
| 411 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 412 | static void ctrl_connect_handler(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 413 | struct sockaddr addr; |
| 414 | socklen_t alen; |
| 415 | struct epoll_event epev; |
| 416 | |
| 417 | if (ctrl_dfd >= 0) { |
| 418 | ctrl_data_close(); |
| 419 | ctrl_dfd_reopened = 1; |
| 420 | } |
| 421 | |
| 422 | alen = sizeof(addr); |
| 423 | ctrl_dfd = accept(ctrl_lfd, &addr, &alen); |
| 424 | |
| 425 | if (ctrl_dfd < 0) { |
| 426 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | ALOGI("ActivityManager connected"); |
| 431 | maxevents++; |
| 432 | epev.events = EPOLLIN; |
| 433 | epev.data.ptr = (void *)ctrl_data_handler; |
| 434 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) { |
| 435 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 436 | ctrl_data_close(); |
| 437 | return; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static int zoneinfo_parse_protection(char *cp) { |
| 442 | int max = 0; |
| 443 | int zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 444 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 445 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 446 | 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] | 447 | zoneval = strtol(cp, &cp, 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 448 | if (zoneval > max) |
| 449 | max = zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 450 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 451 | |
| 452 | return max; |
| 453 | } |
| 454 | |
| 455 | static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) { |
| 456 | char *cp = line; |
| 457 | char *ap; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 458 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 459 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 460 | cp = strtok_r(line, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 461 | if (!cp) |
| 462 | return; |
| 463 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 464 | ap = strtok_r(NULL, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 465 | if (!ap) |
| 466 | return; |
| 467 | |
| 468 | if (!strcmp(cp, "nr_free_pages")) |
| 469 | mip->nr_free_pages += strtol(ap, NULL, 0); |
| 470 | else if (!strcmp(cp, "nr_file_pages")) |
| 471 | mip->nr_file_pages += strtol(ap, NULL, 0); |
| 472 | else if (!strcmp(cp, "nr_shmem")) |
| 473 | mip->nr_shmem += strtol(ap, NULL, 0); |
| 474 | else if (!strcmp(cp, "high")) |
| 475 | mip->totalreserve_pages += strtol(ap, NULL, 0); |
| 476 | else if (!strcmp(cp, "protection:")) |
| 477 | mip->totalreserve_pages += zoneinfo_parse_protection(ap); |
| 478 | } |
| 479 | |
| 480 | static int zoneinfo_parse(struct sysmeminfo *mip) { |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 481 | int fd; |
| 482 | ssize_t size; |
| 483 | char buf[PAGE_SIZE]; |
| 484 | char *save_ptr; |
| 485 | char *line; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 486 | |
| 487 | memset(mip, 0, sizeof(struct sysmeminfo)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 488 | |
| 489 | fd = open(ZONEINFO_PATH, O_RDONLY); |
| 490 | if (fd == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 491 | ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno); |
| 492 | return -1; |
| 493 | } |
| 494 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 495 | size = read_all(fd, buf, sizeof(buf) - 1); |
| 496 | if (size < 0) { |
| 497 | ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno); |
| 498 | close(fd); |
| 499 | return -1; |
| 500 | } |
| 501 | ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large"); |
| 502 | buf[size] = 0; |
| 503 | |
| 504 | 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] | 505 | zoneinfo_parse_line(line, mip); |
| 506 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 507 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int proc_get_size(int pid) { |
| 512 | char path[PATH_MAX]; |
| 513 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 514 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 515 | int rss = 0; |
| 516 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 517 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 518 | |
| 519 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 520 | fd = open(path, O_RDONLY); |
| 521 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 522 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 523 | |
| 524 | ret = read_all(fd, line, sizeof(line) - 1); |
| 525 | if (ret < 0) { |
| 526 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 531 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 532 | return rss; |
| 533 | } |
| 534 | |
| 535 | static char *proc_get_name(int pid) { |
| 536 | char path[PATH_MAX]; |
| 537 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 538 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 539 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 540 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 541 | |
| 542 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 543 | fd = open(path, O_RDONLY); |
| 544 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 545 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 546 | ret = read_all(fd, line, sizeof(line) - 1); |
| 547 | close(fd); |
| 548 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | cp = strchr(line, ' '); |
| 553 | if (cp) |
| 554 | *cp = '\0'; |
| 555 | |
| 556 | return line; |
| 557 | } |
| 558 | |
| 559 | static struct proc *proc_adj_lru(int oomadj) { |
| 560 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 561 | } |
| 562 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 563 | /* Kill one process specified by procp. Returns the size of the process killed */ |
| 564 | static int kill_one_process(struct proc *procp, int other_free, int other_file, |
| 565 | int minfree, int min_score_adj, bool first) |
| 566 | { |
| 567 | int pid = procp->pid; |
| 568 | uid_t uid = procp->uid; |
| 569 | char *taskname; |
| 570 | int tasksize; |
| 571 | int r; |
| 572 | |
| 573 | taskname = proc_get_name(pid); |
| 574 | if (!taskname) { |
| 575 | pid_remove(pid); |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | tasksize = proc_get_size(pid); |
| 580 | if (tasksize <= 0) { |
| 581 | pid_remove(pid); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | ALOGI("Killing '%s' (%d), uid %d, adj %d\n" |
| 586 | " to free %ldkB because cache %s%ldkB is below limit %ldkB for oom_adj %d\n" |
| 587 | " Free memory is %s%ldkB %s reserved", |
| 588 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 589 | first ? "" : "~", other_file * page_k, minfree * page_k, min_score_adj, |
| 590 | first ? "" : "~", other_free * page_k, other_free >= 0 ? "above" : "below"); |
| 591 | r = kill(pid, SIGKILL); |
| 592 | killProcessGroup(uid, pid, SIGKILL); |
| 593 | pid_remove(pid); |
| 594 | |
| 595 | if (r) { |
| 596 | ALOGE("kill(%d): errno=%d", procp->pid, errno); |
| 597 | return -1; |
| 598 | } else { |
| 599 | return tasksize; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Find a process to kill based on the current (possibly estimated) free memory |
| 605 | * and cached memory sizes. Returns the size of the killed processes. |
| 606 | */ |
| 607 | static int find_and_kill_process(int other_free, int other_file, bool first) |
| 608 | { |
| 609 | int i; |
| 610 | int r; |
| 611 | int min_score_adj = OOM_ADJUST_MAX + 1; |
| 612 | int minfree = 0; |
| 613 | int killed_size = 0; |
| 614 | |
| 615 | for (i = 0; i < lowmem_targets_size; i++) { |
| 616 | minfree = lowmem_minfree[i]; |
| 617 | if (other_free < minfree && other_file < minfree) { |
| 618 | min_score_adj = lowmem_adj[i]; |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (min_score_adj == OOM_ADJUST_MAX + 1) |
| 624 | return 0; |
| 625 | |
| 626 | for (i = OOM_ADJUST_MAX; i >= min_score_adj; i--) { |
| 627 | struct proc *procp; |
| 628 | |
| 629 | retry: |
| 630 | procp = proc_adj_lru(i); |
| 631 | |
| 632 | if (procp) { |
| 633 | killed_size = kill_one_process(procp, other_free, other_file, minfree, min_score_adj, first); |
| 634 | if (killed_size < 0) { |
| 635 | goto retry; |
| 636 | } else { |
| 637 | return killed_size; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 645 | static void mp_event(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 646 | int i; |
| 647 | int ret; |
| 648 | unsigned long long evcount; |
| 649 | struct sysmeminfo mi; |
| 650 | int other_free; |
| 651 | int other_file; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 652 | int killed_size; |
| 653 | bool first = true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 654 | |
| 655 | ret = read(mpevfd, &evcount, sizeof(evcount)); |
| 656 | if (ret < 0) |
| 657 | ALOGE("Error reading memory pressure event fd; errno=%d", |
| 658 | errno); |
| 659 | |
| 660 | if (time(NULL) - kill_lasttime < KILL_TIMEOUT) |
| 661 | return; |
| 662 | |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 663 | while (zoneinfo_parse(&mi) < 0) { |
| 664 | // Failed to read /proc/zoneinfo, assume ENOMEM and kill something |
| 665 | find_and_kill_process(0, 0, true); |
| 666 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 667 | |
| 668 | other_free = mi.nr_free_pages - mi.totalreserve_pages; |
| 669 | other_file = mi.nr_file_pages - mi.nr_shmem; |
| 670 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 671 | do { |
| 672 | killed_size = find_and_kill_process(other_free, other_file, first); |
| 673 | if (killed_size > 0) { |
| 674 | first = false; |
| 675 | other_free += killed_size; |
| 676 | other_file += killed_size; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 677 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 678 | } while (killed_size > 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static int init_mp(char *levelstr, void *event_handler) |
| 682 | { |
| 683 | int mpfd; |
| 684 | int evfd; |
| 685 | int evctlfd; |
| 686 | char buf[256]; |
| 687 | struct epoll_event epev; |
| 688 | int ret; |
| 689 | |
| 690 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY); |
| 691 | if (mpfd < 0) { |
| 692 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 693 | goto err_open_mpfd; |
| 694 | } |
| 695 | |
| 696 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY); |
| 697 | if (evctlfd < 0) { |
| 698 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 699 | goto err_open_evctlfd; |
| 700 | } |
| 701 | |
| 702 | evfd = eventfd(0, EFD_NONBLOCK); |
| 703 | if (evfd < 0) { |
| 704 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 705 | goto err_eventfd; |
| 706 | } |
| 707 | |
| 708 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 709 | if (ret >= (ssize_t)sizeof(buf)) { |
| 710 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | ret = write(evctlfd, buf, strlen(buf) + 1); |
| 715 | if (ret == -1) { |
| 716 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 717 | levelstr, errno); |
| 718 | goto err; |
| 719 | } |
| 720 | |
| 721 | epev.events = EPOLLIN; |
| 722 | epev.data.ptr = event_handler; |
| 723 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 724 | if (ret == -1) { |
| 725 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 726 | goto err; |
| 727 | } |
| 728 | maxevents++; |
| 729 | mpevfd = evfd; |
| 730 | return 0; |
| 731 | |
| 732 | err: |
| 733 | close(evfd); |
| 734 | err_eventfd: |
| 735 | close(evctlfd); |
| 736 | err_open_evctlfd: |
| 737 | close(mpfd); |
| 738 | err_open_mpfd: |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | static int init(void) { |
| 743 | struct epoll_event epev; |
| 744 | int i; |
| 745 | int ret; |
| 746 | |
| 747 | page_k = sysconf(_SC_PAGESIZE); |
| 748 | if (page_k == -1) |
| 749 | page_k = PAGE_SIZE; |
| 750 | page_k /= 1024; |
| 751 | |
| 752 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 753 | if (epollfd == -1) { |
| 754 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | ctrl_lfd = android_get_control_socket("lmkd"); |
| 759 | if (ctrl_lfd < 0) { |
| 760 | ALOGE("get lmkd control socket failed"); |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | ret = listen(ctrl_lfd, 1); |
| 765 | if (ret < 0) { |
| 766 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 767 | return -1; |
| 768 | } |
| 769 | |
| 770 | epev.events = EPOLLIN; |
| 771 | epev.data.ptr = (void *)ctrl_connect_handler; |
| 772 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) { |
| 773 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 774 | return -1; |
| 775 | } |
| 776 | maxevents++; |
| 777 | |
| 778 | use_inkernel_interface = !access(INKERNEL_MINFREE_PATH, W_OK); |
| 779 | |
| 780 | if (use_inkernel_interface) { |
| 781 | ALOGI("Using in-kernel low memory killer interface"); |
| 782 | } else { |
| 783 | ret = init_mp(MEMPRESSURE_WATCH_LEVEL, (void *)&mp_event); |
| 784 | if (ret) |
| 785 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
| 786 | } |
| 787 | |
| 788 | for (i = 0; i <= ADJTOSLOT(OOM_ADJUST_MAX); i++) { |
| 789 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 790 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 791 | } |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static void mainloop(void) { |
| 797 | while (1) { |
| 798 | struct epoll_event events[maxevents]; |
| 799 | int nevents; |
| 800 | int i; |
| 801 | |
| 802 | ctrl_dfd_reopened = 0; |
| 803 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 804 | |
| 805 | if (nevents == -1) { |
| 806 | if (errno == EINTR) |
| 807 | continue; |
| 808 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | for (i = 0; i < nevents; ++i) { |
| 813 | if (events[i].events & EPOLLERR) |
| 814 | ALOGD("EPOLLERR on event #%d", i); |
| 815 | if (events[i].data.ptr) |
| 816 | (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 821 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 822 | struct sched_param param = { |
| 823 | .sched_priority = 1, |
| 824 | }; |
| 825 | |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 826 | mlockall(MCL_FUTURE); |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 827 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 828 | if (!init()) |
| 829 | mainloop(); |
| 830 | |
| 831 | ALOGI("exiting"); |
| 832 | return 0; |
| 833 | } |