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