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> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <time.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> |
| 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 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 131 | static int lowmem_oom_adj_to_oom_score_adj(int oom_adj) |
| 132 | { |
| 133 | if (oom_adj == OOM_ADJUST_MAX) |
| 134 | return OOM_SCORE_ADJ_MAX; |
| 135 | else |
| 136 | return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; |
| 137 | } |
| 138 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 139 | static struct proc *pid_lookup(int pid) { |
| 140 | struct proc *procp; |
| 141 | |
| 142 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 143 | procp = procp->pidhash_next) |
| 144 | ; |
| 145 | |
| 146 | return procp; |
| 147 | } |
| 148 | |
| 149 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 150 | { |
| 151 | struct adjslot_list *next = head->next; |
| 152 | new->prev = head; |
| 153 | new->next = next; |
| 154 | next->prev = new; |
| 155 | head->next = new; |
| 156 | } |
| 157 | |
| 158 | static void adjslot_remove(struct adjslot_list *old) |
| 159 | { |
| 160 | struct adjslot_list *prev = old->prev; |
| 161 | struct adjslot_list *next = old->next; |
| 162 | next->prev = prev; |
| 163 | prev->next = next; |
| 164 | } |
| 165 | |
| 166 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 167 | struct adjslot_list *asl = head->prev; |
| 168 | |
| 169 | return asl == head ? NULL : asl; |
| 170 | } |
| 171 | |
| 172 | static void proc_slot(struct proc *procp) { |
| 173 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 174 | |
| 175 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 176 | } |
| 177 | |
| 178 | static void proc_unslot(struct proc *procp) { |
| 179 | adjslot_remove(&procp->asl); |
| 180 | } |
| 181 | |
| 182 | static void proc_insert(struct proc *procp) { |
| 183 | int hval = pid_hashfn(procp->pid); |
| 184 | |
| 185 | procp->pidhash_next = pidhash[hval]; |
| 186 | pidhash[hval] = procp; |
| 187 | proc_slot(procp); |
| 188 | } |
| 189 | |
| 190 | static int pid_remove(int pid) { |
| 191 | int hval = pid_hashfn(pid); |
| 192 | struct proc *procp; |
| 193 | struct proc *prevp; |
| 194 | |
| 195 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 196 | procp = procp->pidhash_next) |
| 197 | prevp = procp; |
| 198 | |
| 199 | if (!procp) |
| 200 | return -1; |
| 201 | |
| 202 | if (!prevp) |
| 203 | pidhash[hval] = procp->pidhash_next; |
| 204 | else |
| 205 | prevp->pidhash_next = procp->pidhash_next; |
| 206 | |
| 207 | proc_unslot(procp); |
| 208 | free(procp); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void writefilestring(char *path, char *s) { |
| 213 | int fd = open(path, O_WRONLY); |
| 214 | int len = strlen(s); |
| 215 | int ret; |
| 216 | |
| 217 | if (fd < 0) { |
| 218 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | ret = write(fd, s, len); |
| 223 | if (ret < 0) { |
| 224 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 225 | } else if (ret < len) { |
| 226 | ALOGE("Short write on %s; length=%d", path, ret); |
| 227 | } |
| 228 | |
| 229 | close(fd); |
| 230 | } |
| 231 | |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 232 | static void cmd_procprio(int pid, int uid, int oomadj) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 233 | struct proc *procp; |
| 234 | char path[80]; |
| 235 | char val[20]; |
| 236 | |
| 237 | if (oomadj < OOM_DISABLE || oomadj > OOM_ADJUST_MAX) { |
| 238 | ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj); |
| 239 | return; |
| 240 | } |
| 241 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 242 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid); |
| 243 | 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] | 244 | writefilestring(path, val); |
| 245 | |
| 246 | if (use_inkernel_interface) |
| 247 | return; |
| 248 | |
| 249 | procp = pid_lookup(pid); |
| 250 | if (!procp) { |
| 251 | procp = malloc(sizeof(struct proc)); |
| 252 | if (!procp) { |
| 253 | // Oh, the irony. May need to rebuild our state. |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | procp->pid = pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 258 | procp->uid = uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 259 | procp->oomadj = oomadj; |
| 260 | proc_insert(procp); |
| 261 | } else { |
| 262 | proc_unslot(procp); |
| 263 | procp->oomadj = oomadj; |
| 264 | proc_slot(procp); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void cmd_procremove(int pid) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 269 | if (use_inkernel_interface) |
| 270 | return; |
| 271 | |
| 272 | pid_remove(pid); |
| 273 | kill_lasttime = 0; |
| 274 | } |
| 275 | |
| 276 | static void cmd_target(int ntargets, int *params) { |
| 277 | int i; |
| 278 | |
| 279 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 280 | return; |
| 281 | |
| 282 | for (i = 0; i < ntargets; i++) { |
| 283 | lowmem_minfree[i] = ntohl(*params++); |
| 284 | lowmem_adj[i] = ntohl(*params++); |
| 285 | } |
| 286 | |
| 287 | lowmem_targets_size = ntargets; |
| 288 | |
| 289 | if (use_inkernel_interface) { |
| 290 | char minfreestr[128]; |
| 291 | char killpriostr[128]; |
| 292 | |
| 293 | minfreestr[0] = '\0'; |
| 294 | killpriostr[0] = '\0'; |
| 295 | |
| 296 | for (i = 0; i < lowmem_targets_size; i++) { |
| 297 | char val[40]; |
| 298 | |
| 299 | if (i) { |
| 300 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 301 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 302 | } |
| 303 | |
| 304 | snprintf(val, sizeof(val), "%d", lowmem_minfree[i]); |
| 305 | strlcat(minfreestr, val, sizeof(minfreestr)); |
| 306 | snprintf(val, sizeof(val), "%d", lowmem_adj[i]); |
| 307 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 308 | } |
| 309 | |
| 310 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr); |
| 311 | writefilestring(INKERNEL_ADJ_PATH, killpriostr); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void ctrl_data_close(void) { |
| 316 | ALOGI("Closing Activity Manager data connection"); |
| 317 | close(ctrl_dfd); |
| 318 | ctrl_dfd = -1; |
| 319 | maxevents--; |
| 320 | } |
| 321 | |
| 322 | static int ctrl_data_read(char *buf, size_t bufsz) { |
| 323 | int ret = 0; |
| 324 | |
| 325 | ret = read(ctrl_dfd, buf, bufsz); |
| 326 | |
| 327 | if (ret == -1) { |
| 328 | ALOGE("control data socket read failed; errno=%d", errno); |
| 329 | } else if (ret == 0) { |
| 330 | ALOGE("Got EOF on control data socket"); |
| 331 | ret = -1; |
| 332 | } |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static void ctrl_command_handler(void) { |
| 338 | int ibuf[CTRL_PACKET_MAX / sizeof(int)]; |
| 339 | int len; |
| 340 | int cmd = -1; |
| 341 | int nargs; |
| 342 | int targets; |
| 343 | |
| 344 | len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX); |
| 345 | if (len <= 0) |
| 346 | return; |
| 347 | |
| 348 | nargs = len / sizeof(int) - 1; |
| 349 | if (nargs < 0) |
| 350 | goto wronglen; |
| 351 | |
| 352 | cmd = ntohl(ibuf[0]); |
| 353 | |
| 354 | switch(cmd) { |
| 355 | case LMK_TARGET: |
| 356 | targets = nargs / 2; |
| 357 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 358 | goto wronglen; |
| 359 | cmd_target(targets, &ibuf[1]); |
| 360 | break; |
| 361 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 362 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 363 | goto wronglen; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 364 | cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3])); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 365 | break; |
| 366 | case LMK_PROCREMOVE: |
| 367 | if (nargs != 1) |
| 368 | goto wronglen; |
| 369 | cmd_procremove(ntohl(ibuf[1])); |
| 370 | break; |
| 371 | default: |
| 372 | ALOGE("Received unknown command code %d", cmd); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | return; |
| 377 | |
| 378 | wronglen: |
| 379 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 380 | } |
| 381 | |
| 382 | static void ctrl_data_handler(uint32_t events) { |
| 383 | if (events & EPOLLHUP) { |
| 384 | ALOGI("ActivityManager disconnected"); |
| 385 | if (!ctrl_dfd_reopened) |
| 386 | ctrl_data_close(); |
| 387 | } else if (events & EPOLLIN) { |
| 388 | ctrl_command_handler(); |
| 389 | } |
| 390 | } |
| 391 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 392 | static void ctrl_connect_handler(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 393 | struct sockaddr addr; |
| 394 | socklen_t alen; |
| 395 | struct epoll_event epev; |
| 396 | |
| 397 | if (ctrl_dfd >= 0) { |
| 398 | ctrl_data_close(); |
| 399 | ctrl_dfd_reopened = 1; |
| 400 | } |
| 401 | |
| 402 | alen = sizeof(addr); |
| 403 | ctrl_dfd = accept(ctrl_lfd, &addr, &alen); |
| 404 | |
| 405 | if (ctrl_dfd < 0) { |
| 406 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | ALOGI("ActivityManager connected"); |
| 411 | maxevents++; |
| 412 | epev.events = EPOLLIN; |
| 413 | epev.data.ptr = (void *)ctrl_data_handler; |
| 414 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) { |
| 415 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 416 | ctrl_data_close(); |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static int zoneinfo_parse_protection(char *cp) { |
| 422 | int max = 0; |
| 423 | int zoneval; |
| 424 | |
| 425 | if (*cp++ != '(') |
| 426 | return 0; |
| 427 | |
| 428 | do { |
| 429 | zoneval = strtol(cp, &cp, 0); |
| 430 | if ((*cp != ',') && (*cp != ')')) |
| 431 | return 0; |
| 432 | if (zoneval > max) |
| 433 | max = zoneval; |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 434 | } while ((cp = strtok(NULL, " "))); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 435 | |
| 436 | return max; |
| 437 | } |
| 438 | |
| 439 | static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) { |
| 440 | char *cp = line; |
| 441 | char *ap; |
| 442 | |
| 443 | cp = strtok(line, " "); |
| 444 | if (!cp) |
| 445 | return; |
| 446 | |
| 447 | ap = strtok(NULL, " "); |
| 448 | if (!ap) |
| 449 | return; |
| 450 | |
| 451 | if (!strcmp(cp, "nr_free_pages")) |
| 452 | mip->nr_free_pages += strtol(ap, NULL, 0); |
| 453 | else if (!strcmp(cp, "nr_file_pages")) |
| 454 | mip->nr_file_pages += strtol(ap, NULL, 0); |
| 455 | else if (!strcmp(cp, "nr_shmem")) |
| 456 | mip->nr_shmem += strtol(ap, NULL, 0); |
| 457 | else if (!strcmp(cp, "high")) |
| 458 | mip->totalreserve_pages += strtol(ap, NULL, 0); |
| 459 | else if (!strcmp(cp, "protection:")) |
| 460 | mip->totalreserve_pages += zoneinfo_parse_protection(ap); |
| 461 | } |
| 462 | |
| 463 | static int zoneinfo_parse(struct sysmeminfo *mip) { |
| 464 | FILE *f; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 465 | char line[LINE_MAX]; |
| 466 | |
| 467 | memset(mip, 0, sizeof(struct sysmeminfo)); |
| 468 | f = fopen(ZONEINFO_PATH, "r"); |
| 469 | if (!f) { |
| 470 | ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno); |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | while (fgets(line, LINE_MAX, f)) |
| 475 | zoneinfo_parse_line(line, mip); |
| 476 | |
| 477 | fclose(f); |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int proc_get_size(int pid) { |
| 482 | char path[PATH_MAX]; |
| 483 | char line[LINE_MAX]; |
| 484 | FILE *f; |
| 485 | int rss = 0; |
| 486 | int total; |
| 487 | |
| 488 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
| 489 | f = fopen(path, "r"); |
| 490 | if (!f) |
| 491 | return -1; |
| 492 | if (!fgets(line, LINE_MAX, f)) { |
| 493 | fclose(f); |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | sscanf(line, "%d %d ", &total, &rss); |
| 498 | fclose(f); |
| 499 | return rss; |
| 500 | } |
| 501 | |
| 502 | static char *proc_get_name(int pid) { |
| 503 | char path[PATH_MAX]; |
| 504 | static char line[LINE_MAX]; |
| 505 | FILE *f; |
| 506 | char *cp; |
| 507 | |
| 508 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
| 509 | f = fopen(path, "r"); |
| 510 | if (!f) |
| 511 | return NULL; |
| 512 | if (!fgets(line, LINE_MAX, f)) { |
| 513 | fclose(f); |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | cp = strchr(line, ' '); |
| 518 | if (cp) |
| 519 | *cp = '\0'; |
| 520 | |
| 521 | return line; |
| 522 | } |
| 523 | |
| 524 | static struct proc *proc_adj_lru(int oomadj) { |
| 525 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 526 | } |
| 527 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 528 | static void mp_event(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 529 | int i; |
| 530 | int ret; |
| 531 | unsigned long long evcount; |
| 532 | struct sysmeminfo mi; |
| 533 | int other_free; |
| 534 | int other_file; |
| 535 | int minfree = 0; |
| 536 | int min_score_adj = OOM_ADJUST_MAX + 1; |
| 537 | |
| 538 | ret = read(mpevfd, &evcount, sizeof(evcount)); |
| 539 | if (ret < 0) |
| 540 | ALOGE("Error reading memory pressure event fd; errno=%d", |
| 541 | errno); |
| 542 | |
| 543 | if (time(NULL) - kill_lasttime < KILL_TIMEOUT) |
| 544 | return; |
| 545 | |
| 546 | if (zoneinfo_parse(&mi) < 0) |
| 547 | return; |
| 548 | |
| 549 | other_free = mi.nr_free_pages - mi.totalreserve_pages; |
| 550 | other_file = mi.nr_file_pages - mi.nr_shmem; |
| 551 | |
| 552 | for (i = 0; i < lowmem_targets_size; i++) { |
| 553 | minfree = lowmem_minfree[i]; |
| 554 | if (other_free < minfree && other_file < minfree) { |
| 555 | min_score_adj = lowmem_adj[i]; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (min_score_adj == OOM_ADJUST_MAX + 1) |
| 561 | return; |
| 562 | |
| 563 | for (i = OOM_ADJUST_MAX; i >= min_score_adj; i--) { |
| 564 | struct proc *procp; |
| 565 | |
| 566 | retry: |
| 567 | procp = proc_adj_lru(i); |
| 568 | |
| 569 | if (procp) { |
| 570 | int pid = procp->pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 571 | uid_t uid = procp->uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 572 | char *taskname; |
| 573 | int tasksize; |
| 574 | int r; |
| 575 | |
| 576 | taskname = proc_get_name(pid); |
| 577 | if (!taskname) { |
| 578 | pid_remove(pid); |
| 579 | goto retry; |
| 580 | } |
| 581 | |
| 582 | tasksize = proc_get_size(pid); |
| 583 | if (tasksize < 0) { |
| 584 | pid_remove(pid); |
| 585 | goto retry; |
| 586 | } |
| 587 | |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 588 | ALOGI("Killing '%s' (%d), uid %d, adj %d\n" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 589 | " to free %ldkB because cache %ldkB is below limit %ldkB for oom_adj %d\n" |
| 590 | " Free memory is %ldkB %s reserved", |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 591 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 592 | other_file * page_k, minfree * page_k, min_score_adj, |
| 593 | other_free * page_k, other_free >= 0 ? "above" : "below"); |
| 594 | r = kill(pid, SIGKILL); |
Colin Cross | fef9522 | 2014-06-11 14:53:41 -0700 | [diff] [blame^] | 595 | killProcessGroup(uid, pid, SIGKILL); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 596 | pid_remove(pid); |
| 597 | |
| 598 | if (r) { |
| 599 | ALOGE("kill(%d): errno=%d", procp->pid, errno); |
| 600 | goto retry; |
| 601 | } else { |
| 602 | time(&kill_lasttime); |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static int init_mp(char *levelstr, void *event_handler) |
| 610 | { |
| 611 | int mpfd; |
| 612 | int evfd; |
| 613 | int evctlfd; |
| 614 | char buf[256]; |
| 615 | struct epoll_event epev; |
| 616 | int ret; |
| 617 | |
| 618 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY); |
| 619 | if (mpfd < 0) { |
| 620 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 621 | goto err_open_mpfd; |
| 622 | } |
| 623 | |
| 624 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY); |
| 625 | if (evctlfd < 0) { |
| 626 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 627 | goto err_open_evctlfd; |
| 628 | } |
| 629 | |
| 630 | evfd = eventfd(0, EFD_NONBLOCK); |
| 631 | if (evfd < 0) { |
| 632 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 633 | goto err_eventfd; |
| 634 | } |
| 635 | |
| 636 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 637 | if (ret >= (ssize_t)sizeof(buf)) { |
| 638 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 639 | goto err; |
| 640 | } |
| 641 | |
| 642 | ret = write(evctlfd, buf, strlen(buf) + 1); |
| 643 | if (ret == -1) { |
| 644 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 645 | levelstr, errno); |
| 646 | goto err; |
| 647 | } |
| 648 | |
| 649 | epev.events = EPOLLIN; |
| 650 | epev.data.ptr = event_handler; |
| 651 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 652 | if (ret == -1) { |
| 653 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 654 | goto err; |
| 655 | } |
| 656 | maxevents++; |
| 657 | mpevfd = evfd; |
| 658 | return 0; |
| 659 | |
| 660 | err: |
| 661 | close(evfd); |
| 662 | err_eventfd: |
| 663 | close(evctlfd); |
| 664 | err_open_evctlfd: |
| 665 | close(mpfd); |
| 666 | err_open_mpfd: |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | static int init(void) { |
| 671 | struct epoll_event epev; |
| 672 | int i; |
| 673 | int ret; |
| 674 | |
| 675 | page_k = sysconf(_SC_PAGESIZE); |
| 676 | if (page_k == -1) |
| 677 | page_k = PAGE_SIZE; |
| 678 | page_k /= 1024; |
| 679 | |
| 680 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 681 | if (epollfd == -1) { |
| 682 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | ctrl_lfd = android_get_control_socket("lmkd"); |
| 687 | if (ctrl_lfd < 0) { |
| 688 | ALOGE("get lmkd control socket failed"); |
| 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | ret = listen(ctrl_lfd, 1); |
| 693 | if (ret < 0) { |
| 694 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | epev.events = EPOLLIN; |
| 699 | epev.data.ptr = (void *)ctrl_connect_handler; |
| 700 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) { |
| 701 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 702 | return -1; |
| 703 | } |
| 704 | maxevents++; |
| 705 | |
| 706 | use_inkernel_interface = !access(INKERNEL_MINFREE_PATH, W_OK); |
| 707 | |
| 708 | if (use_inkernel_interface) { |
| 709 | ALOGI("Using in-kernel low memory killer interface"); |
| 710 | } else { |
| 711 | ret = init_mp(MEMPRESSURE_WATCH_LEVEL, (void *)&mp_event); |
| 712 | if (ret) |
| 713 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
| 714 | } |
| 715 | |
| 716 | for (i = 0; i <= ADJTOSLOT(OOM_ADJUST_MAX); i++) { |
| 717 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 718 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static void mainloop(void) { |
| 725 | while (1) { |
| 726 | struct epoll_event events[maxevents]; |
| 727 | int nevents; |
| 728 | int i; |
| 729 | |
| 730 | ctrl_dfd_reopened = 0; |
| 731 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 732 | |
| 733 | if (nevents == -1) { |
| 734 | if (errno == EINTR) |
| 735 | continue; |
| 736 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | for (i = 0; i < nevents; ++i) { |
| 741 | if (events[i].events & EPOLLERR) |
| 742 | ALOGD("EPOLLERR on event #%d", i); |
| 743 | if (events[i].data.ptr) |
| 744 | (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 749 | int main(int argc __unused, char **argv __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 750 | if (!init()) |
| 751 | mainloop(); |
| 752 | |
| 753 | ALOGI("exiting"); |
| 754 | return 0; |
| 755 | } |