blob: 08eff090154e3c13984c44ae94ec27b495172ad5 [file] [log] [blame]
Todd Poynor3948f802013-07-09 19:35:14 -07001/*
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 Salyzyne6ed68b2014-04-30 13:36:35 -070019#include <arpa/inet.h>
Todd Poynor3948f802013-07-09 19:35:14 -070020#include <errno.h>
Robert Beneac47f2992017-08-21 15:18:31 -070021#include <inttypes.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070022#include <sched.h>
Todd Poynor3948f802013-07-09 19:35:14 -070023#include <signal.h>
Todd Poynor3948f802013-07-09 19:35:14 -070024#include <stdlib.h>
25#include <string.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070026#include <sys/cdefs.h>
Todd Poynor3948f802013-07-09 19:35:14 -070027#include <sys/epoll.h>
28#include <sys/eventfd.h>
Colin Crossb28ff912014-07-11 17:15:44 -070029#include <sys/mman.h>
Todd Poynor3948f802013-07-09 19:35:14 -070030#include <sys/socket.h>
31#include <sys/types.h>
Robert Beneac47f2992017-08-21 15:18:31 -070032#include <time.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070033#include <unistd.h>
34
Robert Benea58891d52017-07-31 17:15:20 -070035#include <cutils/properties.h>
Todd Poynor3948f802013-07-09 19:35:14 -070036#include <cutils/sockets.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080037#include <log/log.h>
Colin Crossfef95222014-06-11 14:53:41 -070038#include <processgroup/processgroup.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070039
40#ifndef __unused
41#define __unused __attribute__((__unused__))
42#endif
Todd Poynor3948f802013-07-09 19:35:14 -070043
44#define MEMCG_SYSFS_PATH "/dev/memcg/"
Robert Beneac47f2992017-08-21 15:18:31 -070045#define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
46#define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
Robert Benea673e2762017-06-01 16:32:31 -070047#define MEMPRESSURE_WATCH_MEDIUM_LEVEL "medium"
48#define MEMPRESSURE_WATCH_CRITICAL_LEVEL "critical"
Todd Poynor3948f802013-07-09 19:35:14 -070049#define ZONEINFO_PATH "/proc/zoneinfo"
50#define LINE_MAX 128
51
52#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
53#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
54
55#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Robert Benea673e2762017-06-01 16:32:31 -070056#define EIGHT_MEGA (1 << 23)
Todd Poynor3948f802013-07-09 19:35:14 -070057
58enum lmk_cmd {
59 LMK_TARGET,
60 LMK_PROCPRIO,
61 LMK_PROCREMOVE,
62};
63
64#define MAX_TARGETS 6
65/*
66 * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio
67 * values
68 */
69#define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1))
70
71/* default to old in-kernel interface if no memory pressure events */
72static int use_inkernel_interface = 1;
73
74/* memory pressure level medium event */
Robert Benea673e2762017-06-01 16:32:31 -070075static int mpevfd[2];
76#define CRITICAL_INDEX 1
77#define MEDIUM_INDEX 0
Todd Poynor3948f802013-07-09 19:35:14 -070078
Robert Benea58891d52017-07-31 17:15:20 -070079static int medium_oomadj;
80static int critical_oomadj;
Robert Beneac47f2992017-08-21 15:18:31 -070081static bool debug_process_killing;
82static bool enable_pressure_upgrade;
83static int64_t upgrade_pressure;
Robert Benea58891d52017-07-31 17:15:20 -070084
Todd Poynor3948f802013-07-09 19:35:14 -070085/* control socket listen and data */
86static int ctrl_lfd;
87static int ctrl_dfd = -1;
88static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
89
Robert Benea673e2762017-06-01 16:32:31 -070090/* 2 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
91#define MAX_EPOLL_EVENTS 4
Todd Poynor3948f802013-07-09 19:35:14 -070092static int epollfd;
93static int maxevents;
94
Chong Zhang0a4acdf2015-10-14 16:19:53 -070095/* OOM score values used by both kernel and framework */
Todd Poynor16b60992013-09-16 19:26:47 -070096#define OOM_SCORE_ADJ_MIN (-1000)
97#define OOM_SCORE_ADJ_MAX 1000
98
Todd Poynor3948f802013-07-09 19:35:14 -070099static int lowmem_adj[MAX_TARGETS];
100static int lowmem_minfree[MAX_TARGETS];
101static int lowmem_targets_size;
102
103struct sysmeminfo {
104 int nr_free_pages;
105 int nr_file_pages;
106 int nr_shmem;
107 int totalreserve_pages;
108};
109
110struct adjslot_list {
111 struct adjslot_list *next;
112 struct adjslot_list *prev;
113};
114
115struct proc {
116 struct adjslot_list asl;
117 int pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700118 uid_t uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700119 int oomadj;
120 struct proc *pidhash_next;
121};
122
123#define PIDHASH_SZ 1024
124static struct proc *pidhash[PIDHASH_SZ];
125#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
126
Chih-Hung Hsiehdaa13ea2016-05-19 16:02:22 -0700127#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700128static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
Todd Poynor3948f802013-07-09 19:35:14 -0700129
Todd Poynor3948f802013-07-09 19:35:14 -0700130/* PAGE_SIZE / 1024 */
131static long page_k;
132
Colin Crossce85d952014-07-11 17:53:27 -0700133static ssize_t read_all(int fd, char *buf, size_t max_len)
134{
135 ssize_t ret = 0;
136
137 while (max_len > 0) {
138 ssize_t r = read(fd, buf, max_len);
139 if (r == 0) {
140 break;
141 }
142 if (r == -1) {
143 return -1;
144 }
145 ret += r;
146 buf += r;
147 max_len -= r;
148 }
149
150 return ret;
151}
152
Todd Poynor3948f802013-07-09 19:35:14 -0700153static struct proc *pid_lookup(int pid) {
154 struct proc *procp;
155
156 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
157 procp = procp->pidhash_next)
158 ;
159
160 return procp;
161}
162
163static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new)
164{
165 struct adjslot_list *next = head->next;
166 new->prev = head;
167 new->next = next;
168 next->prev = new;
169 head->next = new;
170}
171
172static void adjslot_remove(struct adjslot_list *old)
173{
174 struct adjslot_list *prev = old->prev;
175 struct adjslot_list *next = old->next;
176 next->prev = prev;
177 prev->next = next;
178}
179
180static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
181 struct adjslot_list *asl = head->prev;
182
183 return asl == head ? NULL : asl;
184}
185
186static void proc_slot(struct proc *procp) {
187 int adjslot = ADJTOSLOT(procp->oomadj);
188
189 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
190}
191
192static void proc_unslot(struct proc *procp) {
193 adjslot_remove(&procp->asl);
194}
195
196static void proc_insert(struct proc *procp) {
197 int hval = pid_hashfn(procp->pid);
198
199 procp->pidhash_next = pidhash[hval];
200 pidhash[hval] = procp;
201 proc_slot(procp);
202}
203
204static int pid_remove(int pid) {
205 int hval = pid_hashfn(pid);
206 struct proc *procp;
207 struct proc *prevp;
208
209 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
210 procp = procp->pidhash_next)
211 prevp = procp;
212
213 if (!procp)
214 return -1;
215
216 if (!prevp)
217 pidhash[hval] = procp->pidhash_next;
218 else
219 prevp->pidhash_next = procp->pidhash_next;
220
221 proc_unslot(procp);
222 free(procp);
223 return 0;
224}
225
226static void writefilestring(char *path, char *s) {
Nick Kralevichc68c8862015-12-18 20:52:37 -0800227 int fd = open(path, O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700228 int len = strlen(s);
229 int ret;
230
231 if (fd < 0) {
232 ALOGE("Error opening %s; errno=%d", path, errno);
233 return;
234 }
235
236 ret = write(fd, s, len);
237 if (ret < 0) {
238 ALOGE("Error writing %s; errno=%d", path, errno);
239 } else if (ret < len) {
240 ALOGE("Short write on %s; length=%d", path, ret);
241 }
242
243 close(fd);
244}
245
Colin Crossfbb78c62014-06-13 14:52:43 -0700246static void cmd_procprio(int pid, int uid, int oomadj) {
Todd Poynor3948f802013-07-09 19:35:14 -0700247 struct proc *procp;
248 char path[80];
249 char val[20];
Robert Benea673e2762017-06-01 16:32:31 -0700250 int soft_limit_mult;
Todd Poynor3948f802013-07-09 19:35:14 -0700251
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700252 if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
Todd Poynor3948f802013-07-09 19:35:14 -0700253 ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
254 return;
255 }
256
Todd Poynor16b60992013-09-16 19:26:47 -0700257 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700258 snprintf(val, sizeof(val), "%d", oomadj);
Todd Poynor3948f802013-07-09 19:35:14 -0700259 writefilestring(path, val);
260
261 if (use_inkernel_interface)
262 return;
263
Robert Benea673e2762017-06-01 16:32:31 -0700264 if (oomadj >= 900) {
265 soft_limit_mult = 0;
266 } else if (oomadj >= 800) {
267 soft_limit_mult = 0;
268 } else if (oomadj >= 700) {
269 soft_limit_mult = 0;
270 } else if (oomadj >= 600) {
Robert Beneacaeaa652017-08-11 16:03:20 -0700271 // Launcher should be perceptible, don't kill it.
272 oomadj = 200;
273 soft_limit_mult = 1;
Robert Benea673e2762017-06-01 16:32:31 -0700274 } else if (oomadj >= 500) {
275 soft_limit_mult = 0;
276 } else if (oomadj >= 400) {
277 soft_limit_mult = 0;
278 } else if (oomadj >= 300) {
279 soft_limit_mult = 1;
280 } else if (oomadj >= 200) {
281 soft_limit_mult = 2;
282 } else if (oomadj >= 100) {
283 soft_limit_mult = 10;
284 } else if (oomadj >= 0) {
285 soft_limit_mult = 20;
286 } else {
287 // Persistent processes will have a large
288 // soft limit 512MB.
289 soft_limit_mult = 64;
290 }
291
292 snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
293 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
294 writefilestring(path, val);
295
Todd Poynor3948f802013-07-09 19:35:14 -0700296 procp = pid_lookup(pid);
297 if (!procp) {
298 procp = malloc(sizeof(struct proc));
299 if (!procp) {
300 // Oh, the irony. May need to rebuild our state.
301 return;
302 }
303
304 procp->pid = pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700305 procp->uid = uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700306 procp->oomadj = oomadj;
307 proc_insert(procp);
308 } else {
309 proc_unslot(procp);
310 procp->oomadj = oomadj;
311 proc_slot(procp);
312 }
313}
314
315static void cmd_procremove(int pid) {
Todd Poynor3948f802013-07-09 19:35:14 -0700316 if (use_inkernel_interface)
317 return;
318
319 pid_remove(pid);
Todd Poynor3948f802013-07-09 19:35:14 -0700320}
321
322static void cmd_target(int ntargets, int *params) {
323 int i;
324
325 if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
326 return;
327
328 for (i = 0; i < ntargets; i++) {
329 lowmem_minfree[i] = ntohl(*params++);
330 lowmem_adj[i] = ntohl(*params++);
331 }
332
333 lowmem_targets_size = ntargets;
334
335 if (use_inkernel_interface) {
336 char minfreestr[128];
337 char killpriostr[128];
338
339 minfreestr[0] = '\0';
340 killpriostr[0] = '\0';
341
342 for (i = 0; i < lowmem_targets_size; i++) {
343 char val[40];
344
345 if (i) {
346 strlcat(minfreestr, ",", sizeof(minfreestr));
347 strlcat(killpriostr, ",", sizeof(killpriostr));
348 }
349
350 snprintf(val, sizeof(val), "%d", lowmem_minfree[i]);
351 strlcat(minfreestr, val, sizeof(minfreestr));
352 snprintf(val, sizeof(val), "%d", lowmem_adj[i]);
353 strlcat(killpriostr, val, sizeof(killpriostr));
354 }
355
356 writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
357 writefilestring(INKERNEL_ADJ_PATH, killpriostr);
358 }
359}
360
361static void ctrl_data_close(void) {
362 ALOGI("Closing Activity Manager data connection");
363 close(ctrl_dfd);
364 ctrl_dfd = -1;
365 maxevents--;
366}
367
368static int ctrl_data_read(char *buf, size_t bufsz) {
369 int ret = 0;
370
371 ret = read(ctrl_dfd, buf, bufsz);
372
373 if (ret == -1) {
374 ALOGE("control data socket read failed; errno=%d", errno);
375 } else if (ret == 0) {
376 ALOGE("Got EOF on control data socket");
377 ret = -1;
378 }
379
380 return ret;
381}
382
383static void ctrl_command_handler(void) {
384 int ibuf[CTRL_PACKET_MAX / sizeof(int)];
385 int len;
386 int cmd = -1;
387 int nargs;
388 int targets;
389
390 len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
391 if (len <= 0)
392 return;
393
394 nargs = len / sizeof(int) - 1;
395 if (nargs < 0)
396 goto wronglen;
397
398 cmd = ntohl(ibuf[0]);
399
400 switch(cmd) {
401 case LMK_TARGET:
402 targets = nargs / 2;
403 if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
404 goto wronglen;
405 cmd_target(targets, &ibuf[1]);
406 break;
407 case LMK_PROCPRIO:
Colin Crossfbb78c62014-06-13 14:52:43 -0700408 if (nargs != 3)
Todd Poynor3948f802013-07-09 19:35:14 -0700409 goto wronglen;
Colin Crossfbb78c62014-06-13 14:52:43 -0700410 cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
Todd Poynor3948f802013-07-09 19:35:14 -0700411 break;
412 case LMK_PROCREMOVE:
413 if (nargs != 1)
414 goto wronglen;
415 cmd_procremove(ntohl(ibuf[1]));
416 break;
417 default:
418 ALOGE("Received unknown command code %d", cmd);
419 return;
420 }
421
422 return;
423
424wronglen:
425 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
426}
427
428static void ctrl_data_handler(uint32_t events) {
429 if (events & EPOLLHUP) {
430 ALOGI("ActivityManager disconnected");
431 if (!ctrl_dfd_reopened)
432 ctrl_data_close();
433 } else if (events & EPOLLIN) {
434 ctrl_command_handler();
435 }
436}
437
Mark Salyzyne6ed68b2014-04-30 13:36:35 -0700438static void ctrl_connect_handler(uint32_t events __unused) {
Todd Poynor3948f802013-07-09 19:35:14 -0700439 struct epoll_event epev;
440
441 if (ctrl_dfd >= 0) {
442 ctrl_data_close();
443 ctrl_dfd_reopened = 1;
444 }
445
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700446 ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
Todd Poynor3948f802013-07-09 19:35:14 -0700447
448 if (ctrl_dfd < 0) {
449 ALOGE("lmkd control socket accept failed; errno=%d", errno);
450 return;
451 }
452
453 ALOGI("ActivityManager connected");
454 maxevents++;
455 epev.events = EPOLLIN;
456 epev.data.ptr = (void *)ctrl_data_handler;
457 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
458 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
459 ctrl_data_close();
460 return;
461 }
462}
463
464static int zoneinfo_parse_protection(char *cp) {
465 int max = 0;
466 int zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700467 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700468
Colin Crossce85d952014-07-11 17:53:27 -0700469 for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
Todd Poynor3948f802013-07-09 19:35:14 -0700470 zoneval = strtol(cp, &cp, 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700471 if (zoneval > max)
472 max = zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700473 }
Todd Poynor3948f802013-07-09 19:35:14 -0700474
475 return max;
476}
477
478static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
479 char *cp = line;
480 char *ap;
Colin Crossce85d952014-07-11 17:53:27 -0700481 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700482
Colin Crossce85d952014-07-11 17:53:27 -0700483 cp = strtok_r(line, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700484 if (!cp)
485 return;
486
Colin Crossce85d952014-07-11 17:53:27 -0700487 ap = strtok_r(NULL, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700488 if (!ap)
489 return;
490
491 if (!strcmp(cp, "nr_free_pages"))
492 mip->nr_free_pages += strtol(ap, NULL, 0);
493 else if (!strcmp(cp, "nr_file_pages"))
494 mip->nr_file_pages += strtol(ap, NULL, 0);
495 else if (!strcmp(cp, "nr_shmem"))
496 mip->nr_shmem += strtol(ap, NULL, 0);
497 else if (!strcmp(cp, "high"))
498 mip->totalreserve_pages += strtol(ap, NULL, 0);
499 else if (!strcmp(cp, "protection:"))
500 mip->totalreserve_pages += zoneinfo_parse_protection(ap);
501}
502
503static int zoneinfo_parse(struct sysmeminfo *mip) {
Colin Crossce85d952014-07-11 17:53:27 -0700504 int fd;
505 ssize_t size;
506 char buf[PAGE_SIZE];
507 char *save_ptr;
508 char *line;
Todd Poynor3948f802013-07-09 19:35:14 -0700509
510 memset(mip, 0, sizeof(struct sysmeminfo));
Colin Crossce85d952014-07-11 17:53:27 -0700511
Nick Kralevichc68c8862015-12-18 20:52:37 -0800512 fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700513 if (fd == -1) {
Todd Poynor3948f802013-07-09 19:35:14 -0700514 ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
515 return -1;
516 }
517
Colin Crossce85d952014-07-11 17:53:27 -0700518 size = read_all(fd, buf, sizeof(buf) - 1);
519 if (size < 0) {
520 ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
521 close(fd);
522 return -1;
523 }
524 ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
525 buf[size] = 0;
526
527 for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
Todd Poynor3948f802013-07-09 19:35:14 -0700528 zoneinfo_parse_line(line, mip);
529
Colin Crossce85d952014-07-11 17:53:27 -0700530 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700531 return 0;
532}
533
534static int proc_get_size(int pid) {
535 char path[PATH_MAX];
536 char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700537 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700538 int rss = 0;
539 int total;
Colin Crossce85d952014-07-11 17:53:27 -0700540 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700541
542 snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800543 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700544 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700545 return -1;
Colin Crossce85d952014-07-11 17:53:27 -0700546
547 ret = read_all(fd, line, sizeof(line) - 1);
548 if (ret < 0) {
549 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700550 return -1;
551 }
552
553 sscanf(line, "%d %d ", &total, &rss);
Colin Crossce85d952014-07-11 17:53:27 -0700554 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700555 return rss;
556}
557
558static char *proc_get_name(int pid) {
559 char path[PATH_MAX];
560 static char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700561 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700562 char *cp;
Colin Crossce85d952014-07-11 17:53:27 -0700563 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700564
565 snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800566 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700567 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700568 return NULL;
Colin Crossce85d952014-07-11 17:53:27 -0700569 ret = read_all(fd, line, sizeof(line) - 1);
570 close(fd);
571 if (ret < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700572 return NULL;
573 }
574
575 cp = strchr(line, ' ');
576 if (cp)
577 *cp = '\0';
578
579 return line;
580}
581
582static struct proc *proc_adj_lru(int oomadj) {
583 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
584}
585
Colin Cross16b09462014-07-14 12:39:56 -0700586/* Kill one process specified by procp. Returns the size of the process killed */
Robert Beneacaeaa652017-08-11 16:03:20 -0700587static int kill_one_process(struct proc* procp, int min_score_adj, bool is_critical) {
Colin Cross16b09462014-07-14 12:39:56 -0700588 int pid = procp->pid;
589 uid_t uid = procp->uid;
590 char *taskname;
591 int tasksize;
592 int r;
593
594 taskname = proc_get_name(pid);
595 if (!taskname) {
596 pid_remove(pid);
597 return -1;
598 }
599
600 tasksize = proc_get_size(pid);
601 if (tasksize <= 0) {
602 pid_remove(pid);
603 return -1;
604 }
605
Robert Beneacaeaa652017-08-11 16:03:20 -0700606 ALOGI(
607 "Killing '%s' (%d), uid %d, adj %d\n"
608 " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
609 taskname, pid, uid, procp->oomadj, tasksize * page_k, is_critical ? "critical" : "medium",
610 min_score_adj);
Colin Cross16b09462014-07-14 12:39:56 -0700611 r = kill(pid, SIGKILL);
Colin Cross16b09462014-07-14 12:39:56 -0700612 pid_remove(pid);
613
614 if (r) {
615 ALOGE("kill(%d): errno=%d", procp->pid, errno);
616 return -1;
617 } else {
618 return tasksize;
619 }
620}
621
622/*
623 * Find a process to kill based on the current (possibly estimated) free memory
624 * and cached memory sizes. Returns the size of the killed processes.
625 */
Robert Beneacaeaa652017-08-11 16:03:20 -0700626static int find_and_kill_process(int min_score_adj, bool is_critical) {
Colin Cross16b09462014-07-14 12:39:56 -0700627 int i;
Colin Cross16b09462014-07-14 12:39:56 -0700628 int killed_size = 0;
629
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700630 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
Colin Cross16b09462014-07-14 12:39:56 -0700631 struct proc *procp;
632
633retry:
634 procp = proc_adj_lru(i);
635
636 if (procp) {
Robert Beneacaeaa652017-08-11 16:03:20 -0700637 killed_size = kill_one_process(procp, min_score_adj, is_critical);
Colin Cross16b09462014-07-14 12:39:56 -0700638 if (killed_size < 0) {
639 goto retry;
640 } else {
641 return killed_size;
642 }
643 }
644 }
645
646 return 0;
647}
648
Robert Beneac47f2992017-08-21 15:18:31 -0700649static int64_t get_memory_usage(const char* path) {
650 int ret;
651 int64_t mem_usage;
652 char buf[32];
653 int fd = open(path, O_RDONLY | O_CLOEXEC);
654 if (fd == -1) {
655 ALOGE("%s open: errno=%d", path, errno);
656 return -1;
657 }
658
659 ret = read_all(fd, buf, sizeof(buf) - 1);
660 close(fd);
661 if (ret < 0) {
662 ALOGE("%s error: errno=%d", path, errno);
663 return -1;
664 }
665 sscanf(buf, "%" SCNd64, &mem_usage);
666 if (mem_usage == 0) {
667 ALOGE("No memory!");
668 return -1;
669 }
670 return mem_usage;
671}
672
Robert Benea673e2762017-06-01 16:32:31 -0700673static void mp_event_common(bool is_critical) {
Todd Poynor3948f802013-07-09 19:35:14 -0700674 int ret;
675 unsigned long long evcount;
Robert Benea58891d52017-07-31 17:15:20 -0700676 int min_adj_score = is_critical ? critical_oomadj : medium_oomadj;
Robert Benea673e2762017-06-01 16:32:31 -0700677 int index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
Robert Beneac47f2992017-08-21 15:18:31 -0700678 int64_t mem_usage, memsw_usage;
Todd Poynor3948f802013-07-09 19:35:14 -0700679
Robert Benea673e2762017-06-01 16:32:31 -0700680 ret = read(mpevfd[index], &evcount, sizeof(evcount));
Todd Poynor3948f802013-07-09 19:35:14 -0700681 if (ret < 0)
682 ALOGE("Error reading memory pressure event fd; errno=%d",
683 errno);
684
Robert Beneac47f2992017-08-21 15:18:31 -0700685 if (enable_pressure_upgrade && !is_critical) {
686 mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
687 memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
688 if (memsw_usage < 0 || mem_usage < 0) {
689 find_and_kill_process(min_adj_score, is_critical);
690 return;
691 }
692
693 // We are swapping too much, calculate percent for swappinness.
694 if (((mem_usage * 100) / memsw_usage) < upgrade_pressure) {
695 ALOGI("Event upgraded to critical.");
696 min_adj_score = critical_oomadj;
697 is_critical = true;
698 }
699 }
700
Robert Beneacaeaa652017-08-11 16:03:20 -0700701 if (find_and_kill_process(min_adj_score, is_critical) == 0) {
702 if (debug_process_killing) {
703 ALOGI("Nothing to kill");
704 }
Colin Crossf8857cc2014-07-11 17:16:56 -0700705 }
Todd Poynor3948f802013-07-09 19:35:14 -0700706}
707
Robert Benea673e2762017-06-01 16:32:31 -0700708static void mp_event(uint32_t events __unused) {
709 mp_event_common(false);
710}
711
712static void mp_event_critical(uint32_t events __unused) {
Robert Benea673e2762017-06-01 16:32:31 -0700713 mp_event_common(true);
714}
715
716static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
Todd Poynor3948f802013-07-09 19:35:14 -0700717{
718 int mpfd;
719 int evfd;
720 int evctlfd;
721 char buf[256];
722 struct epoll_event epev;
723 int ret;
Robert Benea673e2762017-06-01 16:32:31 -0700724 int mpevfd_index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
Todd Poynor3948f802013-07-09 19:35:14 -0700725
Nick Kralevichc68c8862015-12-18 20:52:37 -0800726 mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700727 if (mpfd < 0) {
728 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
729 goto err_open_mpfd;
730 }
731
Nick Kralevichc68c8862015-12-18 20:52:37 -0800732 evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700733 if (evctlfd < 0) {
734 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
735 goto err_open_evctlfd;
736 }
737
Nick Kralevichc68c8862015-12-18 20:52:37 -0800738 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700739 if (evfd < 0) {
740 ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
741 goto err_eventfd;
742 }
743
744 ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
745 if (ret >= (ssize_t)sizeof(buf)) {
746 ALOGE("cgroup.event_control line overflow for level %s", levelstr);
747 goto err;
748 }
749
750 ret = write(evctlfd, buf, strlen(buf) + 1);
751 if (ret == -1) {
752 ALOGE("cgroup.event_control write failed for level %s; errno=%d",
753 levelstr, errno);
754 goto err;
755 }
756
757 epev.events = EPOLLIN;
758 epev.data.ptr = event_handler;
759 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
760 if (ret == -1) {
761 ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
762 goto err;
763 }
764 maxevents++;
Robert Benea673e2762017-06-01 16:32:31 -0700765 mpevfd[mpevfd_index] = evfd;
Todd Poynor3948f802013-07-09 19:35:14 -0700766 return 0;
767
768err:
769 close(evfd);
770err_eventfd:
771 close(evctlfd);
772err_open_evctlfd:
773 close(mpfd);
774err_open_mpfd:
775 return -1;
776}
777
Robert Benea673e2762017-06-01 16:32:31 -0700778static int init_mp_medium()
779{
780 return init_mp_common(MEMPRESSURE_WATCH_MEDIUM_LEVEL, (void *)&mp_event, false);
781}
782
783static int init_mp_critical()
784{
785 return init_mp_common(MEMPRESSURE_WATCH_CRITICAL_LEVEL, (void *)&mp_event_critical, true);
786}
787
Todd Poynor3948f802013-07-09 19:35:14 -0700788static int init(void) {
789 struct epoll_event epev;
790 int i;
791 int ret;
792
793 page_k = sysconf(_SC_PAGESIZE);
794 if (page_k == -1)
795 page_k = PAGE_SIZE;
796 page_k /= 1024;
797
798 epollfd = epoll_create(MAX_EPOLL_EVENTS);
799 if (epollfd == -1) {
800 ALOGE("epoll_create failed (errno=%d)", errno);
801 return -1;
802 }
803
804 ctrl_lfd = android_get_control_socket("lmkd");
805 if (ctrl_lfd < 0) {
806 ALOGE("get lmkd control socket failed");
807 return -1;
808 }
809
810 ret = listen(ctrl_lfd, 1);
811 if (ret < 0) {
812 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
813 return -1;
814 }
815
816 epev.events = EPOLLIN;
817 epev.data.ptr = (void *)ctrl_connect_handler;
818 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
819 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
820 return -1;
821 }
822 maxevents++;
823
824 use_inkernel_interface = !access(INKERNEL_MINFREE_PATH, W_OK);
825
826 if (use_inkernel_interface) {
827 ALOGI("Using in-kernel low memory killer interface");
828 } else {
Robert Benea673e2762017-06-01 16:32:31 -0700829 ret = init_mp_medium();
830 ret |= init_mp_critical();
Todd Poynor3948f802013-07-09 19:35:14 -0700831 if (ret)
832 ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
833 }
834
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700835 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
Todd Poynor3948f802013-07-09 19:35:14 -0700836 procadjslot_list[i].next = &procadjslot_list[i];
837 procadjslot_list[i].prev = &procadjslot_list[i];
838 }
839
840 return 0;
841}
842
843static void mainloop(void) {
844 while (1) {
845 struct epoll_event events[maxevents];
846 int nevents;
847 int i;
848
849 ctrl_dfd_reopened = 0;
850 nevents = epoll_wait(epollfd, events, maxevents, -1);
851
852 if (nevents == -1) {
853 if (errno == EINTR)
854 continue;
855 ALOGE("epoll_wait failed (errno=%d)", errno);
856 continue;
857 }
858
859 for (i = 0; i < nevents; ++i) {
860 if (events[i].events & EPOLLERR)
861 ALOGD("EPOLLERR on event #%d", i);
862 if (events[i].data.ptr)
863 (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
864 }
865 }
866}
867
Mark Salyzyne6ed68b2014-04-30 13:36:35 -0700868int main(int argc __unused, char **argv __unused) {
Colin Cross1a0d9be2014-07-14 14:31:15 -0700869 struct sched_param param = {
870 .sched_priority = 1,
871 };
872
Robert Benea58891d52017-07-31 17:15:20 -0700873 medium_oomadj = property_get_int32("ro.lmk.medium", 800);
874 critical_oomadj = property_get_int32("ro.lmk.critical", 0);
Robert Beneacaeaa652017-08-11 16:03:20 -0700875 debug_process_killing = property_get_bool("ro.lmk.debug", false);
Robert Beneac47f2992017-08-21 15:18:31 -0700876 enable_pressure_upgrade = property_get_bool("ro.lmk.critical_upgrade", false);
877 upgrade_pressure = (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 50);
Robert Benea58891d52017-07-31 17:15:20 -0700878
Colin Crossb28ff912014-07-11 17:15:44 -0700879 mlockall(MCL_FUTURE);
Colin Cross1a0d9be2014-07-14 14:31:15 -0700880 sched_setscheduler(0, SCHED_FIFO, &param);
Todd Poynor3948f802013-07-09 19:35:14 -0700881 if (!init())
882 mainloop();
883
884 ALOGI("exiting");
885 return 0;
886}