blob: e26b2271a3c9a46f12fcc39e4af74faa92ddedc3 [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;
Robert Benea164baeb2017-09-11 16:53:28 -070073static bool has_inkernel_module;
Todd Poynor3948f802013-07-09 19:35:14 -070074
75/* memory pressure level medium event */
Robert Benea673e2762017-06-01 16:32:31 -070076static int mpevfd[2];
77#define CRITICAL_INDEX 1
78#define MEDIUM_INDEX 0
Todd Poynor3948f802013-07-09 19:35:14 -070079
Robert Benea58891d52017-07-31 17:15:20 -070080static int medium_oomadj;
81static int critical_oomadj;
Robert Beneac47f2992017-08-21 15:18:31 -070082static bool debug_process_killing;
83static bool enable_pressure_upgrade;
84static int64_t upgrade_pressure;
Robert Benea164baeb2017-09-11 16:53:28 -070085static bool is_go_device;
Robert Benea58891d52017-07-31 17:15:20 -070086
Todd Poynor3948f802013-07-09 19:35:14 -070087/* control socket listen and data */
88static int ctrl_lfd;
89static int ctrl_dfd = -1;
90static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
91
Robert Benea673e2762017-06-01 16:32:31 -070092/* 2 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
93#define MAX_EPOLL_EVENTS 4
Todd Poynor3948f802013-07-09 19:35:14 -070094static int epollfd;
95static int maxevents;
96
Chong Zhang0a4acdf2015-10-14 16:19:53 -070097/* OOM score values used by both kernel and framework */
Todd Poynor16b60992013-09-16 19:26:47 -070098#define OOM_SCORE_ADJ_MIN (-1000)
99#define OOM_SCORE_ADJ_MAX 1000
100
Todd Poynor3948f802013-07-09 19:35:14 -0700101static int lowmem_adj[MAX_TARGETS];
102static int lowmem_minfree[MAX_TARGETS];
103static int lowmem_targets_size;
104
105struct sysmeminfo {
106 int nr_free_pages;
107 int nr_file_pages;
108 int nr_shmem;
109 int totalreserve_pages;
110};
111
112struct adjslot_list {
113 struct adjslot_list *next;
114 struct adjslot_list *prev;
115};
116
117struct proc {
118 struct adjslot_list asl;
119 int pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700120 uid_t uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700121 int oomadj;
122 struct proc *pidhash_next;
123};
124
125#define PIDHASH_SZ 1024
126static struct proc *pidhash[PIDHASH_SZ];
127#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
128
Chih-Hung Hsiehdaa13ea2016-05-19 16:02:22 -0700129#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700130static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
Todd Poynor3948f802013-07-09 19:35:14 -0700131
Todd Poynor3948f802013-07-09 19:35:14 -0700132/* PAGE_SIZE / 1024 */
133static long page_k;
134
Colin Crossce85d952014-07-11 17:53:27 -0700135static ssize_t read_all(int fd, char *buf, size_t max_len)
136{
137 ssize_t ret = 0;
138
139 while (max_len > 0) {
140 ssize_t r = read(fd, buf, max_len);
141 if (r == 0) {
142 break;
143 }
144 if (r == -1) {
145 return -1;
146 }
147 ret += r;
148 buf += r;
149 max_len -= r;
150 }
151
152 return ret;
153}
154
Todd Poynor3948f802013-07-09 19:35:14 -0700155static struct proc *pid_lookup(int pid) {
156 struct proc *procp;
157
158 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
159 procp = procp->pidhash_next)
160 ;
161
162 return procp;
163}
164
165static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new)
166{
167 struct adjslot_list *next = head->next;
168 new->prev = head;
169 new->next = next;
170 next->prev = new;
171 head->next = new;
172}
173
174static void adjslot_remove(struct adjslot_list *old)
175{
176 struct adjslot_list *prev = old->prev;
177 struct adjslot_list *next = old->next;
178 next->prev = prev;
179 prev->next = next;
180}
181
182static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
183 struct adjslot_list *asl = head->prev;
184
185 return asl == head ? NULL : asl;
186}
187
188static void proc_slot(struct proc *procp) {
189 int adjslot = ADJTOSLOT(procp->oomadj);
190
191 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
192}
193
194static void proc_unslot(struct proc *procp) {
195 adjslot_remove(&procp->asl);
196}
197
198static void proc_insert(struct proc *procp) {
199 int hval = pid_hashfn(procp->pid);
200
201 procp->pidhash_next = pidhash[hval];
202 pidhash[hval] = procp;
203 proc_slot(procp);
204}
205
206static int pid_remove(int pid) {
207 int hval = pid_hashfn(pid);
208 struct proc *procp;
209 struct proc *prevp;
210
211 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
212 procp = procp->pidhash_next)
213 prevp = procp;
214
215 if (!procp)
216 return -1;
217
218 if (!prevp)
219 pidhash[hval] = procp->pidhash_next;
220 else
221 prevp->pidhash_next = procp->pidhash_next;
222
223 proc_unslot(procp);
224 free(procp);
225 return 0;
226}
227
228static void writefilestring(char *path, char *s) {
Nick Kralevichc68c8862015-12-18 20:52:37 -0800229 int fd = open(path, O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700230 int len = strlen(s);
231 int ret;
232
233 if (fd < 0) {
234 ALOGE("Error opening %s; errno=%d", path, errno);
235 return;
236 }
237
238 ret = write(fd, s, len);
239 if (ret < 0) {
240 ALOGE("Error writing %s; errno=%d", path, errno);
241 } else if (ret < len) {
242 ALOGE("Short write on %s; length=%d", path, ret);
243 }
244
245 close(fd);
246}
247
Colin Crossfbb78c62014-06-13 14:52:43 -0700248static void cmd_procprio(int pid, int uid, int oomadj) {
Todd Poynor3948f802013-07-09 19:35:14 -0700249 struct proc *procp;
250 char path[80];
251 char val[20];
Robert Benea673e2762017-06-01 16:32:31 -0700252 int soft_limit_mult;
Todd Poynor3948f802013-07-09 19:35:14 -0700253
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700254 if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
Todd Poynor3948f802013-07-09 19:35:14 -0700255 ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
256 return;
257 }
258
Todd Poynor16b60992013-09-16 19:26:47 -0700259 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700260 snprintf(val, sizeof(val), "%d", oomadj);
Todd Poynor3948f802013-07-09 19:35:14 -0700261 writefilestring(path, val);
262
263 if (use_inkernel_interface)
264 return;
265
Robert Benea673e2762017-06-01 16:32:31 -0700266 if (oomadj >= 900) {
267 soft_limit_mult = 0;
268 } else if (oomadj >= 800) {
269 soft_limit_mult = 0;
270 } else if (oomadj >= 700) {
271 soft_limit_mult = 0;
272 } else if (oomadj >= 600) {
Robert Beneacaeaa652017-08-11 16:03:20 -0700273 // Launcher should be perceptible, don't kill it.
274 oomadj = 200;
275 soft_limit_mult = 1;
Robert Benea673e2762017-06-01 16:32:31 -0700276 } else if (oomadj >= 500) {
277 soft_limit_mult = 0;
278 } else if (oomadj >= 400) {
279 soft_limit_mult = 0;
280 } else if (oomadj >= 300) {
281 soft_limit_mult = 1;
282 } else if (oomadj >= 200) {
283 soft_limit_mult = 2;
284 } else if (oomadj >= 100) {
285 soft_limit_mult = 10;
286 } else if (oomadj >= 0) {
287 soft_limit_mult = 20;
288 } else {
289 // Persistent processes will have a large
290 // soft limit 512MB.
291 soft_limit_mult = 64;
292 }
293
294 snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
295 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
296 writefilestring(path, val);
297
Todd Poynor3948f802013-07-09 19:35:14 -0700298 procp = pid_lookup(pid);
299 if (!procp) {
300 procp = malloc(sizeof(struct proc));
301 if (!procp) {
302 // Oh, the irony. May need to rebuild our state.
303 return;
304 }
305
306 procp->pid = pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700307 procp->uid = uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700308 procp->oomadj = oomadj;
309 proc_insert(procp);
310 } else {
311 proc_unslot(procp);
312 procp->oomadj = oomadj;
313 proc_slot(procp);
314 }
315}
316
317static void cmd_procremove(int pid) {
Todd Poynor3948f802013-07-09 19:35:14 -0700318 if (use_inkernel_interface)
319 return;
320
321 pid_remove(pid);
Todd Poynor3948f802013-07-09 19:35:14 -0700322}
323
324static void cmd_target(int ntargets, int *params) {
325 int i;
326
327 if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
328 return;
329
330 for (i = 0; i < ntargets; i++) {
331 lowmem_minfree[i] = ntohl(*params++);
332 lowmem_adj[i] = ntohl(*params++);
333 }
334
335 lowmem_targets_size = ntargets;
336
Robert Benea164baeb2017-09-11 16:53:28 -0700337 if (has_inkernel_module) {
Todd Poynor3948f802013-07-09 19:35:14 -0700338 char minfreestr[128];
339 char killpriostr[128];
340
341 minfreestr[0] = '\0';
342 killpriostr[0] = '\0';
343
344 for (i = 0; i < lowmem_targets_size; i++) {
345 char val[40];
346
347 if (i) {
348 strlcat(minfreestr, ",", sizeof(minfreestr));
349 strlcat(killpriostr, ",", sizeof(killpriostr));
350 }
351
Robert Benea164baeb2017-09-11 16:53:28 -0700352 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700353 strlcat(minfreestr, val, sizeof(minfreestr));
Robert Benea164baeb2017-09-11 16:53:28 -0700354 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700355 strlcat(killpriostr, val, sizeof(killpriostr));
356 }
357
358 writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
359 writefilestring(INKERNEL_ADJ_PATH, killpriostr);
360 }
361}
362
363static void ctrl_data_close(void) {
364 ALOGI("Closing Activity Manager data connection");
365 close(ctrl_dfd);
366 ctrl_dfd = -1;
367 maxevents--;
368}
369
370static int ctrl_data_read(char *buf, size_t bufsz) {
371 int ret = 0;
372
373 ret = read(ctrl_dfd, buf, bufsz);
374
375 if (ret == -1) {
376 ALOGE("control data socket read failed; errno=%d", errno);
377 } else if (ret == 0) {
378 ALOGE("Got EOF on control data socket");
379 ret = -1;
380 }
381
382 return ret;
383}
384
385static void ctrl_command_handler(void) {
386 int ibuf[CTRL_PACKET_MAX / sizeof(int)];
387 int len;
388 int cmd = -1;
389 int nargs;
390 int targets;
391
392 len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
393 if (len <= 0)
394 return;
395
396 nargs = len / sizeof(int) - 1;
397 if (nargs < 0)
398 goto wronglen;
399
400 cmd = ntohl(ibuf[0]);
401
402 switch(cmd) {
403 case LMK_TARGET:
404 targets = nargs / 2;
405 if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
406 goto wronglen;
407 cmd_target(targets, &ibuf[1]);
408 break;
409 case LMK_PROCPRIO:
Colin Crossfbb78c62014-06-13 14:52:43 -0700410 if (nargs != 3)
Todd Poynor3948f802013-07-09 19:35:14 -0700411 goto wronglen;
Colin Crossfbb78c62014-06-13 14:52:43 -0700412 cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
Todd Poynor3948f802013-07-09 19:35:14 -0700413 break;
414 case LMK_PROCREMOVE:
415 if (nargs != 1)
416 goto wronglen;
417 cmd_procremove(ntohl(ibuf[1]));
418 break;
419 default:
420 ALOGE("Received unknown command code %d", cmd);
421 return;
422 }
423
424 return;
425
426wronglen:
427 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
428}
429
430static void ctrl_data_handler(uint32_t events) {
431 if (events & EPOLLHUP) {
432 ALOGI("ActivityManager disconnected");
433 if (!ctrl_dfd_reopened)
434 ctrl_data_close();
435 } else if (events & EPOLLIN) {
436 ctrl_command_handler();
437 }
438}
439
Mark Salyzyne6ed68b2014-04-30 13:36:35 -0700440static void ctrl_connect_handler(uint32_t events __unused) {
Todd Poynor3948f802013-07-09 19:35:14 -0700441 struct epoll_event epev;
442
443 if (ctrl_dfd >= 0) {
444 ctrl_data_close();
445 ctrl_dfd_reopened = 1;
446 }
447
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700448 ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
Todd Poynor3948f802013-07-09 19:35:14 -0700449
450 if (ctrl_dfd < 0) {
451 ALOGE("lmkd control socket accept failed; errno=%d", errno);
452 return;
453 }
454
455 ALOGI("ActivityManager connected");
456 maxevents++;
457 epev.events = EPOLLIN;
458 epev.data.ptr = (void *)ctrl_data_handler;
459 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
460 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
461 ctrl_data_close();
462 return;
463 }
464}
465
466static int zoneinfo_parse_protection(char *cp) {
467 int max = 0;
468 int zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700469 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700470
Colin Crossce85d952014-07-11 17:53:27 -0700471 for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
Todd Poynor3948f802013-07-09 19:35:14 -0700472 zoneval = strtol(cp, &cp, 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700473 if (zoneval > max)
474 max = zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700475 }
Todd Poynor3948f802013-07-09 19:35:14 -0700476
477 return max;
478}
479
480static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
481 char *cp = line;
482 char *ap;
Colin Crossce85d952014-07-11 17:53:27 -0700483 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700484
Colin Crossce85d952014-07-11 17:53:27 -0700485 cp = strtok_r(line, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700486 if (!cp)
487 return;
488
Colin Crossce85d952014-07-11 17:53:27 -0700489 ap = strtok_r(NULL, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700490 if (!ap)
491 return;
492
493 if (!strcmp(cp, "nr_free_pages"))
494 mip->nr_free_pages += strtol(ap, NULL, 0);
495 else if (!strcmp(cp, "nr_file_pages"))
496 mip->nr_file_pages += strtol(ap, NULL, 0);
497 else if (!strcmp(cp, "nr_shmem"))
498 mip->nr_shmem += strtol(ap, NULL, 0);
499 else if (!strcmp(cp, "high"))
500 mip->totalreserve_pages += strtol(ap, NULL, 0);
501 else if (!strcmp(cp, "protection:"))
502 mip->totalreserve_pages += zoneinfo_parse_protection(ap);
503}
504
505static int zoneinfo_parse(struct sysmeminfo *mip) {
Colin Crossce85d952014-07-11 17:53:27 -0700506 int fd;
507 ssize_t size;
508 char buf[PAGE_SIZE];
509 char *save_ptr;
510 char *line;
Todd Poynor3948f802013-07-09 19:35:14 -0700511
512 memset(mip, 0, sizeof(struct sysmeminfo));
Colin Crossce85d952014-07-11 17:53:27 -0700513
Nick Kralevichc68c8862015-12-18 20:52:37 -0800514 fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700515 if (fd == -1) {
Todd Poynor3948f802013-07-09 19:35:14 -0700516 ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
517 return -1;
518 }
519
Colin Crossce85d952014-07-11 17:53:27 -0700520 size = read_all(fd, buf, sizeof(buf) - 1);
521 if (size < 0) {
522 ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
523 close(fd);
524 return -1;
525 }
526 ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
527 buf[size] = 0;
528
529 for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
Todd Poynor3948f802013-07-09 19:35:14 -0700530 zoneinfo_parse_line(line, mip);
531
Colin Crossce85d952014-07-11 17:53:27 -0700532 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700533 return 0;
534}
535
536static int proc_get_size(int pid) {
537 char path[PATH_MAX];
538 char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700539 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700540 int rss = 0;
541 int total;
Colin Crossce85d952014-07-11 17:53:27 -0700542 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700543
544 snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800545 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700546 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700547 return -1;
Colin Crossce85d952014-07-11 17:53:27 -0700548
549 ret = read_all(fd, line, sizeof(line) - 1);
550 if (ret < 0) {
551 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700552 return -1;
553 }
554
555 sscanf(line, "%d %d ", &total, &rss);
Colin Crossce85d952014-07-11 17:53:27 -0700556 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700557 return rss;
558}
559
560static char *proc_get_name(int pid) {
561 char path[PATH_MAX];
562 static char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700563 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700564 char *cp;
Colin Crossce85d952014-07-11 17:53:27 -0700565 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700566
567 snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800568 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700569 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700570 return NULL;
Colin Crossce85d952014-07-11 17:53:27 -0700571 ret = read_all(fd, line, sizeof(line) - 1);
572 close(fd);
573 if (ret < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700574 return NULL;
575 }
576
577 cp = strchr(line, ' ');
578 if (cp)
579 *cp = '\0';
580
581 return line;
582}
583
584static struct proc *proc_adj_lru(int oomadj) {
585 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
586}
587
Colin Cross16b09462014-07-14 12:39:56 -0700588/* Kill one process specified by procp. Returns the size of the process killed */
Robert Beneacaeaa652017-08-11 16:03:20 -0700589static int kill_one_process(struct proc* procp, int min_score_adj, bool is_critical) {
Colin Cross16b09462014-07-14 12:39:56 -0700590 int pid = procp->pid;
591 uid_t uid = procp->uid;
592 char *taskname;
593 int tasksize;
594 int r;
595
596 taskname = proc_get_name(pid);
597 if (!taskname) {
598 pid_remove(pid);
599 return -1;
600 }
601
602 tasksize = proc_get_size(pid);
603 if (tasksize <= 0) {
604 pid_remove(pid);
605 return -1;
606 }
607
Robert Beneacaeaa652017-08-11 16:03:20 -0700608 ALOGI(
609 "Killing '%s' (%d), uid %d, adj %d\n"
610 " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
611 taskname, pid, uid, procp->oomadj, tasksize * page_k, is_critical ? "critical" : "medium",
612 min_score_adj);
Colin Cross16b09462014-07-14 12:39:56 -0700613 r = kill(pid, SIGKILL);
Colin Cross16b09462014-07-14 12:39:56 -0700614 pid_remove(pid);
615
616 if (r) {
617 ALOGE("kill(%d): errno=%d", procp->pid, errno);
618 return -1;
619 } else {
620 return tasksize;
621 }
622}
623
624/*
625 * Find a process to kill based on the current (possibly estimated) free memory
626 * and cached memory sizes. Returns the size of the killed processes.
627 */
Robert Beneacaeaa652017-08-11 16:03:20 -0700628static int find_and_kill_process(int min_score_adj, bool is_critical) {
Colin Cross16b09462014-07-14 12:39:56 -0700629 int i;
Colin Cross16b09462014-07-14 12:39:56 -0700630 int killed_size = 0;
631
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700632 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
Colin Cross16b09462014-07-14 12:39:56 -0700633 struct proc *procp;
634
635retry:
636 procp = proc_adj_lru(i);
637
638 if (procp) {
Robert Beneacaeaa652017-08-11 16:03:20 -0700639 killed_size = kill_one_process(procp, min_score_adj, is_critical);
Colin Cross16b09462014-07-14 12:39:56 -0700640 if (killed_size < 0) {
641 goto retry;
642 } else {
643 return killed_size;
644 }
645 }
646 }
647
648 return 0;
649}
650
Robert Beneac47f2992017-08-21 15:18:31 -0700651static int64_t get_memory_usage(const char* path) {
652 int ret;
653 int64_t mem_usage;
654 char buf[32];
655 int fd = open(path, O_RDONLY | O_CLOEXEC);
656 if (fd == -1) {
657 ALOGE("%s open: errno=%d", path, errno);
658 return -1;
659 }
660
661 ret = read_all(fd, buf, sizeof(buf) - 1);
662 close(fd);
663 if (ret < 0) {
664 ALOGE("%s error: errno=%d", path, errno);
665 return -1;
666 }
667 sscanf(buf, "%" SCNd64, &mem_usage);
668 if (mem_usage == 0) {
669 ALOGE("No memory!");
670 return -1;
671 }
672 return mem_usage;
673}
674
Robert Benea673e2762017-06-01 16:32:31 -0700675static void mp_event_common(bool is_critical) {
Todd Poynor3948f802013-07-09 19:35:14 -0700676 int ret;
677 unsigned long long evcount;
Robert Benea58891d52017-07-31 17:15:20 -0700678 int min_adj_score = is_critical ? critical_oomadj : medium_oomadj;
Robert Benea673e2762017-06-01 16:32:31 -0700679 int index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
Robert Beneac47f2992017-08-21 15:18:31 -0700680 int64_t mem_usage, memsw_usage;
Todd Poynor3948f802013-07-09 19:35:14 -0700681
Robert Benea673e2762017-06-01 16:32:31 -0700682 ret = read(mpevfd[index], &evcount, sizeof(evcount));
Todd Poynor3948f802013-07-09 19:35:14 -0700683 if (ret < 0)
684 ALOGE("Error reading memory pressure event fd; errno=%d",
685 errno);
686
Robert Beneac47f2992017-08-21 15:18:31 -0700687 if (enable_pressure_upgrade && !is_critical) {
688 mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
689 memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
690 if (memsw_usage < 0 || mem_usage < 0) {
691 find_and_kill_process(min_adj_score, is_critical);
692 return;
693 }
694
695 // We are swapping too much, calculate percent for swappinness.
696 if (((mem_usage * 100) / memsw_usage) < upgrade_pressure) {
697 ALOGI("Event upgraded to critical.");
698 min_adj_score = critical_oomadj;
699 is_critical = true;
700 }
701 }
702
Robert Beneacaeaa652017-08-11 16:03:20 -0700703 if (find_and_kill_process(min_adj_score, is_critical) == 0) {
704 if (debug_process_killing) {
705 ALOGI("Nothing to kill");
706 }
Colin Crossf8857cc2014-07-11 17:16:56 -0700707 }
Todd Poynor3948f802013-07-09 19:35:14 -0700708}
709
Robert Benea673e2762017-06-01 16:32:31 -0700710static void mp_event(uint32_t events __unused) {
711 mp_event_common(false);
712}
713
714static void mp_event_critical(uint32_t events __unused) {
Robert Benea673e2762017-06-01 16:32:31 -0700715 mp_event_common(true);
716}
717
718static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
Todd Poynor3948f802013-07-09 19:35:14 -0700719{
720 int mpfd;
721 int evfd;
722 int evctlfd;
723 char buf[256];
724 struct epoll_event epev;
725 int ret;
Robert Benea673e2762017-06-01 16:32:31 -0700726 int mpevfd_index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
Todd Poynor3948f802013-07-09 19:35:14 -0700727
Nick Kralevichc68c8862015-12-18 20:52:37 -0800728 mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700729 if (mpfd < 0) {
730 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
731 goto err_open_mpfd;
732 }
733
Nick Kralevichc68c8862015-12-18 20:52:37 -0800734 evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700735 if (evctlfd < 0) {
736 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
737 goto err_open_evctlfd;
738 }
739
Nick Kralevichc68c8862015-12-18 20:52:37 -0800740 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700741 if (evfd < 0) {
742 ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
743 goto err_eventfd;
744 }
745
746 ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
747 if (ret >= (ssize_t)sizeof(buf)) {
748 ALOGE("cgroup.event_control line overflow for level %s", levelstr);
749 goto err;
750 }
751
752 ret = write(evctlfd, buf, strlen(buf) + 1);
753 if (ret == -1) {
754 ALOGE("cgroup.event_control write failed for level %s; errno=%d",
755 levelstr, errno);
756 goto err;
757 }
758
759 epev.events = EPOLLIN;
760 epev.data.ptr = event_handler;
761 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
762 if (ret == -1) {
763 ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
764 goto err;
765 }
766 maxevents++;
Robert Benea673e2762017-06-01 16:32:31 -0700767 mpevfd[mpevfd_index] = evfd;
Todd Poynor3948f802013-07-09 19:35:14 -0700768 return 0;
769
770err:
771 close(evfd);
772err_eventfd:
773 close(evctlfd);
774err_open_evctlfd:
775 close(mpfd);
776err_open_mpfd:
777 return -1;
778}
779
Robert Benea673e2762017-06-01 16:32:31 -0700780static int init_mp_medium()
781{
782 return init_mp_common(MEMPRESSURE_WATCH_MEDIUM_LEVEL, (void *)&mp_event, false);
783}
784
785static int init_mp_critical()
786{
787 return init_mp_common(MEMPRESSURE_WATCH_CRITICAL_LEVEL, (void *)&mp_event_critical, true);
788}
789
Todd Poynor3948f802013-07-09 19:35:14 -0700790static int init(void) {
791 struct epoll_event epev;
792 int i;
793 int ret;
794
795 page_k = sysconf(_SC_PAGESIZE);
796 if (page_k == -1)
797 page_k = PAGE_SIZE;
798 page_k /= 1024;
799
800 epollfd = epoll_create(MAX_EPOLL_EVENTS);
801 if (epollfd == -1) {
802 ALOGE("epoll_create failed (errno=%d)", errno);
803 return -1;
804 }
805
806 ctrl_lfd = android_get_control_socket("lmkd");
807 if (ctrl_lfd < 0) {
808 ALOGE("get lmkd control socket failed");
809 return -1;
810 }
811
812 ret = listen(ctrl_lfd, 1);
813 if (ret < 0) {
814 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
815 return -1;
816 }
817
818 epev.events = EPOLLIN;
819 epev.data.ptr = (void *)ctrl_connect_handler;
820 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
821 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
822 return -1;
823 }
824 maxevents++;
825
Robert Benea164baeb2017-09-11 16:53:28 -0700826 has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
827 use_inkernel_interface = has_inkernel_module && !is_go_device;
Todd Poynor3948f802013-07-09 19:35:14 -0700828
829 if (use_inkernel_interface) {
830 ALOGI("Using in-kernel low memory killer interface");
831 } else {
Robert Benea673e2762017-06-01 16:32:31 -0700832 ret = init_mp_medium();
833 ret |= init_mp_critical();
Todd Poynor3948f802013-07-09 19:35:14 -0700834 if (ret)
835 ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
836 }
837
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700838 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
Todd Poynor3948f802013-07-09 19:35:14 -0700839 procadjslot_list[i].next = &procadjslot_list[i];
840 procadjslot_list[i].prev = &procadjslot_list[i];
841 }
842
843 return 0;
844}
845
846static void mainloop(void) {
847 while (1) {
848 struct epoll_event events[maxevents];
849 int nevents;
850 int i;
851
852 ctrl_dfd_reopened = 0;
853 nevents = epoll_wait(epollfd, events, maxevents, -1);
854
855 if (nevents == -1) {
856 if (errno == EINTR)
857 continue;
858 ALOGE("epoll_wait failed (errno=%d)", errno);
859 continue;
860 }
861
862 for (i = 0; i < nevents; ++i) {
863 if (events[i].events & EPOLLERR)
864 ALOGD("EPOLLERR on event #%d", i);
865 if (events[i].data.ptr)
866 (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
867 }
868 }
869}
870
Mark Salyzyne6ed68b2014-04-30 13:36:35 -0700871int main(int argc __unused, char **argv __unused) {
Colin Cross1a0d9be2014-07-14 14:31:15 -0700872 struct sched_param param = {
873 .sched_priority = 1,
874 };
875
Robert Benea58891d52017-07-31 17:15:20 -0700876 medium_oomadj = property_get_int32("ro.lmk.medium", 800);
877 critical_oomadj = property_get_int32("ro.lmk.critical", 0);
Robert Beneacaeaa652017-08-11 16:03:20 -0700878 debug_process_killing = property_get_bool("ro.lmk.debug", false);
Robert Beneac47f2992017-08-21 15:18:31 -0700879 enable_pressure_upgrade = property_get_bool("ro.lmk.critical_upgrade", false);
880 upgrade_pressure = (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 50);
Robert Benea164baeb2017-09-11 16:53:28 -0700881 is_go_device = property_get_bool("ro.config.low_ram", false);
Robert Benea58891d52017-07-31 17:15:20 -0700882
Colin Crossb28ff912014-07-11 17:15:44 -0700883 mlockall(MCL_FUTURE);
Colin Cross1a0d9be2014-07-14 14:31:15 -0700884 sched_setscheduler(0, SCHED_FIFO, &param);
Todd Poynor3948f802013-07-09 19:35:14 -0700885 if (!init())
886 mainloop();
887
888 ALOGI("exiting");
889 return 0;
890}