blob: 338e5fa20d16bb1be1469a4138b06531218e042e [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>
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -080032#include <sys/sysinfo.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>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070038
Suren Baghdasaryane1217c02018-01-04 10:43:58 -080039/*
40 * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
41 * to profile and correlate with OOM kills
42 */
43#ifdef LMKD_TRACE_KILLS
44
45#define ATRACE_TAG ATRACE_TAG_ALWAYS
46#include <cutils/trace.h>
47
48#define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid);
49#define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0);
50
51#else /* LMKD_TRACE_KILLS */
52
53#define TRACE_KILL_START(pid)
54#define TRACE_KILL_END()
55
56#endif /* LMKD_TRACE_KILLS */
57
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070058#ifndef __unused
59#define __unused __attribute__((__unused__))
60#endif
Todd Poynor3948f802013-07-09 19:35:14 -070061
62#define MEMCG_SYSFS_PATH "/dev/memcg/"
Robert Beneac47f2992017-08-21 15:18:31 -070063#define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
64#define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
Todd Poynor3948f802013-07-09 19:35:14 -070065#define ZONEINFO_PATH "/proc/zoneinfo"
66#define LINE_MAX 128
67
68#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
69#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
70
71#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Robert Benea673e2762017-06-01 16:32:31 -070072#define EIGHT_MEGA (1 << 23)
Todd Poynor3948f802013-07-09 19:35:14 -070073
74enum lmk_cmd {
75 LMK_TARGET,
76 LMK_PROCPRIO,
77 LMK_PROCREMOVE,
78};
79
80#define MAX_TARGETS 6
81/*
82 * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio
83 * values
84 */
85#define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1))
86
87/* default to old in-kernel interface if no memory pressure events */
88static int use_inkernel_interface = 1;
Robert Benea164baeb2017-09-11 16:53:28 -070089static bool has_inkernel_module;
Todd Poynor3948f802013-07-09 19:35:14 -070090
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -080091/* memory pressure levels */
92enum vmpressure_level {
93 VMPRESS_LEVEL_LOW = 0,
94 VMPRESS_LEVEL_MEDIUM,
95 VMPRESS_LEVEL_CRITICAL,
96 VMPRESS_LEVEL_COUNT
97};
Todd Poynor3948f802013-07-09 19:35:14 -070098
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -080099static const char *level_name[] = {
100 "low",
101 "medium",
102 "critical"
103};
104
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800105struct mem_size {
106 int free_mem;
107 int free_swap;
108};
109
110struct {
111 int min_free; /* recorded but not used yet */
112 int max_free;
113} low_pressure_mem = { -1, -1 };
114
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800115static int level_oomadj[VMPRESS_LEVEL_COUNT];
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -0800116static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 };
Robert Beneac47f2992017-08-21 15:18:31 -0700117static bool debug_process_killing;
118static bool enable_pressure_upgrade;
119static int64_t upgrade_pressure;
Robert Benea6e8e7102017-09-13 15:20:30 -0700120static int64_t downgrade_pressure;
Robert Benea164baeb2017-09-11 16:53:28 -0700121static bool is_go_device;
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -0800122static bool kill_heaviest_task;
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800123static unsigned long kill_timeout_ms;
Robert Benea58891d52017-07-31 17:15:20 -0700124
Todd Poynor3948f802013-07-09 19:35:14 -0700125/* control socket listen and data */
126static int ctrl_lfd;
127static int ctrl_dfd = -1;
128static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
129
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800130/* 3 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
131#define MAX_EPOLL_EVENTS 5
Todd Poynor3948f802013-07-09 19:35:14 -0700132static int epollfd;
133static int maxevents;
134
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700135/* OOM score values used by both kernel and framework */
Todd Poynor16b60992013-09-16 19:26:47 -0700136#define OOM_SCORE_ADJ_MIN (-1000)
137#define OOM_SCORE_ADJ_MAX 1000
138
Todd Poynor3948f802013-07-09 19:35:14 -0700139static int lowmem_adj[MAX_TARGETS];
140static int lowmem_minfree[MAX_TARGETS];
141static int lowmem_targets_size;
142
143struct sysmeminfo {
144 int nr_free_pages;
145 int nr_file_pages;
146 int nr_shmem;
147 int totalreserve_pages;
148};
149
150struct adjslot_list {
151 struct adjslot_list *next;
152 struct adjslot_list *prev;
153};
154
155struct proc {
156 struct adjslot_list asl;
157 int pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700158 uid_t uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700159 int oomadj;
160 struct proc *pidhash_next;
161};
162
163#define PIDHASH_SZ 1024
164static struct proc *pidhash[PIDHASH_SZ];
165#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
166
Chih-Hung Hsiehdaa13ea2016-05-19 16:02:22 -0700167#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700168static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
Todd Poynor3948f802013-07-09 19:35:14 -0700169
Todd Poynor3948f802013-07-09 19:35:14 -0700170/* PAGE_SIZE / 1024 */
171static long page_k;
172
Colin Crossce85d952014-07-11 17:53:27 -0700173static ssize_t read_all(int fd, char *buf, size_t max_len)
174{
175 ssize_t ret = 0;
176
177 while (max_len > 0) {
178 ssize_t r = read(fd, buf, max_len);
179 if (r == 0) {
180 break;
181 }
182 if (r == -1) {
183 return -1;
184 }
185 ret += r;
186 buf += r;
187 max_len -= r;
188 }
189
190 return ret;
191}
192
Todd Poynor3948f802013-07-09 19:35:14 -0700193static struct proc *pid_lookup(int pid) {
194 struct proc *procp;
195
196 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
197 procp = procp->pidhash_next)
198 ;
199
200 return procp;
201}
202
203static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new)
204{
205 struct adjslot_list *next = head->next;
206 new->prev = head;
207 new->next = next;
208 next->prev = new;
209 head->next = new;
210}
211
212static void adjslot_remove(struct adjslot_list *old)
213{
214 struct adjslot_list *prev = old->prev;
215 struct adjslot_list *next = old->next;
216 next->prev = prev;
217 prev->next = next;
218}
219
220static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
221 struct adjslot_list *asl = head->prev;
222
223 return asl == head ? NULL : asl;
224}
225
226static void proc_slot(struct proc *procp) {
227 int adjslot = ADJTOSLOT(procp->oomadj);
228
229 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
230}
231
232static void proc_unslot(struct proc *procp) {
233 adjslot_remove(&procp->asl);
234}
235
236static void proc_insert(struct proc *procp) {
237 int hval = pid_hashfn(procp->pid);
238
239 procp->pidhash_next = pidhash[hval];
240 pidhash[hval] = procp;
241 proc_slot(procp);
242}
243
244static int pid_remove(int pid) {
245 int hval = pid_hashfn(pid);
246 struct proc *procp;
247 struct proc *prevp;
248
249 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
250 procp = procp->pidhash_next)
251 prevp = procp;
252
253 if (!procp)
254 return -1;
255
256 if (!prevp)
257 pidhash[hval] = procp->pidhash_next;
258 else
259 prevp->pidhash_next = procp->pidhash_next;
260
261 proc_unslot(procp);
262 free(procp);
263 return 0;
264}
265
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800266static void writefilestring(const char *path, char *s) {
Nick Kralevichc68c8862015-12-18 20:52:37 -0800267 int fd = open(path, O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700268 int len = strlen(s);
269 int ret;
270
271 if (fd < 0) {
272 ALOGE("Error opening %s; errno=%d", path, errno);
273 return;
274 }
275
276 ret = write(fd, s, len);
277 if (ret < 0) {
278 ALOGE("Error writing %s; errno=%d", path, errno);
279 } else if (ret < len) {
280 ALOGE("Short write on %s; length=%d", path, ret);
281 }
282
283 close(fd);
284}
285
Colin Crossfbb78c62014-06-13 14:52:43 -0700286static void cmd_procprio(int pid, int uid, int oomadj) {
Todd Poynor3948f802013-07-09 19:35:14 -0700287 struct proc *procp;
288 char path[80];
289 char val[20];
Robert Benea673e2762017-06-01 16:32:31 -0700290 int soft_limit_mult;
Todd Poynor3948f802013-07-09 19:35:14 -0700291
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700292 if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
Todd Poynor3948f802013-07-09 19:35:14 -0700293 ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
294 return;
295 }
296
Todd Poynor16b60992013-09-16 19:26:47 -0700297 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700298 snprintf(val, sizeof(val), "%d", oomadj);
Todd Poynor3948f802013-07-09 19:35:14 -0700299 writefilestring(path, val);
300
301 if (use_inkernel_interface)
302 return;
303
Robert Benea673e2762017-06-01 16:32:31 -0700304 if (oomadj >= 900) {
305 soft_limit_mult = 0;
306 } else if (oomadj >= 800) {
307 soft_limit_mult = 0;
308 } else if (oomadj >= 700) {
309 soft_limit_mult = 0;
310 } else if (oomadj >= 600) {
Robert Beneacaeaa652017-08-11 16:03:20 -0700311 // Launcher should be perceptible, don't kill it.
312 oomadj = 200;
313 soft_limit_mult = 1;
Robert Benea673e2762017-06-01 16:32:31 -0700314 } else if (oomadj >= 500) {
315 soft_limit_mult = 0;
316 } else if (oomadj >= 400) {
317 soft_limit_mult = 0;
318 } else if (oomadj >= 300) {
319 soft_limit_mult = 1;
320 } else if (oomadj >= 200) {
321 soft_limit_mult = 2;
322 } else if (oomadj >= 100) {
323 soft_limit_mult = 10;
324 } else if (oomadj >= 0) {
325 soft_limit_mult = 20;
326 } else {
327 // Persistent processes will have a large
328 // soft limit 512MB.
329 soft_limit_mult = 64;
330 }
331
332 snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
333 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
334 writefilestring(path, val);
335
Todd Poynor3948f802013-07-09 19:35:14 -0700336 procp = pid_lookup(pid);
337 if (!procp) {
338 procp = malloc(sizeof(struct proc));
339 if (!procp) {
340 // Oh, the irony. May need to rebuild our state.
341 return;
342 }
343
344 procp->pid = pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700345 procp->uid = uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700346 procp->oomadj = oomadj;
347 proc_insert(procp);
348 } else {
349 proc_unslot(procp);
350 procp->oomadj = oomadj;
351 proc_slot(procp);
352 }
353}
354
355static void cmd_procremove(int pid) {
Todd Poynor3948f802013-07-09 19:35:14 -0700356 if (use_inkernel_interface)
357 return;
358
359 pid_remove(pid);
Todd Poynor3948f802013-07-09 19:35:14 -0700360}
361
362static void cmd_target(int ntargets, int *params) {
363 int i;
364
365 if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
366 return;
367
368 for (i = 0; i < ntargets; i++) {
369 lowmem_minfree[i] = ntohl(*params++);
370 lowmem_adj[i] = ntohl(*params++);
371 }
372
373 lowmem_targets_size = ntargets;
374
Robert Benea164baeb2017-09-11 16:53:28 -0700375 if (has_inkernel_module) {
Todd Poynor3948f802013-07-09 19:35:14 -0700376 char minfreestr[128];
377 char killpriostr[128];
378
379 minfreestr[0] = '\0';
380 killpriostr[0] = '\0';
381
382 for (i = 0; i < lowmem_targets_size; i++) {
383 char val[40];
384
385 if (i) {
386 strlcat(minfreestr, ",", sizeof(minfreestr));
387 strlcat(killpriostr, ",", sizeof(killpriostr));
388 }
389
Robert Benea164baeb2017-09-11 16:53:28 -0700390 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700391 strlcat(minfreestr, val, sizeof(minfreestr));
Robert Benea164baeb2017-09-11 16:53:28 -0700392 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700393 strlcat(killpriostr, val, sizeof(killpriostr));
394 }
395
396 writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
397 writefilestring(INKERNEL_ADJ_PATH, killpriostr);
398 }
399}
400
401static void ctrl_data_close(void) {
402 ALOGI("Closing Activity Manager data connection");
403 close(ctrl_dfd);
404 ctrl_dfd = -1;
405 maxevents--;
406}
407
408static int ctrl_data_read(char *buf, size_t bufsz) {
409 int ret = 0;
410
411 ret = read(ctrl_dfd, buf, bufsz);
412
413 if (ret == -1) {
414 ALOGE("control data socket read failed; errno=%d", errno);
415 } else if (ret == 0) {
416 ALOGE("Got EOF on control data socket");
417 ret = -1;
418 }
419
420 return ret;
421}
422
423static void ctrl_command_handler(void) {
424 int ibuf[CTRL_PACKET_MAX / sizeof(int)];
425 int len;
426 int cmd = -1;
427 int nargs;
428 int targets;
429
430 len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
431 if (len <= 0)
432 return;
433
434 nargs = len / sizeof(int) - 1;
435 if (nargs < 0)
436 goto wronglen;
437
438 cmd = ntohl(ibuf[0]);
439
440 switch(cmd) {
441 case LMK_TARGET:
442 targets = nargs / 2;
443 if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
444 goto wronglen;
445 cmd_target(targets, &ibuf[1]);
446 break;
447 case LMK_PROCPRIO:
Colin Crossfbb78c62014-06-13 14:52:43 -0700448 if (nargs != 3)
Todd Poynor3948f802013-07-09 19:35:14 -0700449 goto wronglen;
Colin Crossfbb78c62014-06-13 14:52:43 -0700450 cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
Todd Poynor3948f802013-07-09 19:35:14 -0700451 break;
452 case LMK_PROCREMOVE:
453 if (nargs != 1)
454 goto wronglen;
455 cmd_procremove(ntohl(ibuf[1]));
456 break;
457 default:
458 ALOGE("Received unknown command code %d", cmd);
459 return;
460 }
461
462 return;
463
464wronglen:
465 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
466}
467
468static void ctrl_data_handler(uint32_t events) {
469 if (events & EPOLLHUP) {
470 ALOGI("ActivityManager disconnected");
471 if (!ctrl_dfd_reopened)
472 ctrl_data_close();
473 } else if (events & EPOLLIN) {
474 ctrl_command_handler();
475 }
476}
477
Mark Salyzyne6ed68b2014-04-30 13:36:35 -0700478static void ctrl_connect_handler(uint32_t events __unused) {
Todd Poynor3948f802013-07-09 19:35:14 -0700479 struct epoll_event epev;
480
481 if (ctrl_dfd >= 0) {
482 ctrl_data_close();
483 ctrl_dfd_reopened = 1;
484 }
485
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700486 ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
Todd Poynor3948f802013-07-09 19:35:14 -0700487
488 if (ctrl_dfd < 0) {
489 ALOGE("lmkd control socket accept failed; errno=%d", errno);
490 return;
491 }
492
493 ALOGI("ActivityManager connected");
494 maxevents++;
495 epev.events = EPOLLIN;
496 epev.data.ptr = (void *)ctrl_data_handler;
497 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
498 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
499 ctrl_data_close();
500 return;
501 }
502}
503
504static int zoneinfo_parse_protection(char *cp) {
505 int max = 0;
506 int zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700507 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700508
Colin Crossce85d952014-07-11 17:53:27 -0700509 for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
Todd Poynor3948f802013-07-09 19:35:14 -0700510 zoneval = strtol(cp, &cp, 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700511 if (zoneval > max)
512 max = zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700513 }
Todd Poynor3948f802013-07-09 19:35:14 -0700514
515 return max;
516}
517
518static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
519 char *cp = line;
520 char *ap;
Colin Crossce85d952014-07-11 17:53:27 -0700521 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700522
Colin Crossce85d952014-07-11 17:53:27 -0700523 cp = strtok_r(line, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700524 if (!cp)
525 return;
526
Colin Crossce85d952014-07-11 17:53:27 -0700527 ap = strtok_r(NULL, " ", &save_ptr);
Todd Poynor3948f802013-07-09 19:35:14 -0700528 if (!ap)
529 return;
530
531 if (!strcmp(cp, "nr_free_pages"))
532 mip->nr_free_pages += strtol(ap, NULL, 0);
533 else if (!strcmp(cp, "nr_file_pages"))
534 mip->nr_file_pages += strtol(ap, NULL, 0);
535 else if (!strcmp(cp, "nr_shmem"))
536 mip->nr_shmem += strtol(ap, NULL, 0);
537 else if (!strcmp(cp, "high"))
538 mip->totalreserve_pages += strtol(ap, NULL, 0);
539 else if (!strcmp(cp, "protection:"))
540 mip->totalreserve_pages += zoneinfo_parse_protection(ap);
541}
542
543static int zoneinfo_parse(struct sysmeminfo *mip) {
Colin Crossce85d952014-07-11 17:53:27 -0700544 int fd;
545 ssize_t size;
546 char buf[PAGE_SIZE];
547 char *save_ptr;
548 char *line;
Todd Poynor3948f802013-07-09 19:35:14 -0700549
550 memset(mip, 0, sizeof(struct sysmeminfo));
Colin Crossce85d952014-07-11 17:53:27 -0700551
Nick Kralevichc68c8862015-12-18 20:52:37 -0800552 fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700553 if (fd == -1) {
Todd Poynor3948f802013-07-09 19:35:14 -0700554 ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
555 return -1;
556 }
557
Colin Crossce85d952014-07-11 17:53:27 -0700558 size = read_all(fd, buf, sizeof(buf) - 1);
559 if (size < 0) {
560 ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
561 close(fd);
562 return -1;
563 }
564 ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
565 buf[size] = 0;
566
567 for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
Todd Poynor3948f802013-07-09 19:35:14 -0700568 zoneinfo_parse_line(line, mip);
569
Colin Crossce85d952014-07-11 17:53:27 -0700570 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700571 return 0;
572}
573
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800574static int get_free_memory(struct mem_size *ms) {
575 struct sysinfo si;
576
577 if (sysinfo(&si) < 0)
578 return -1;
579
580 ms->free_mem = (int)(si.freeram * si.mem_unit / PAGE_SIZE);
581 ms->free_swap = (int)(si.freeswap * si.mem_unit / PAGE_SIZE);
582
583 return 0;
584}
585
Todd Poynor3948f802013-07-09 19:35:14 -0700586static int proc_get_size(int pid) {
587 char path[PATH_MAX];
588 char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700589 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700590 int rss = 0;
591 int total;
Colin Crossce85d952014-07-11 17:53:27 -0700592 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700593
594 snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800595 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700596 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700597 return -1;
Colin Crossce85d952014-07-11 17:53:27 -0700598
599 ret = read_all(fd, line, sizeof(line) - 1);
600 if (ret < 0) {
601 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700602 return -1;
603 }
604
605 sscanf(line, "%d %d ", &total, &rss);
Colin Crossce85d952014-07-11 17:53:27 -0700606 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700607 return rss;
608}
609
610static char *proc_get_name(int pid) {
611 char path[PATH_MAX];
612 static char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700613 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700614 char *cp;
Colin Crossce85d952014-07-11 17:53:27 -0700615 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700616
617 snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800618 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700619 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700620 return NULL;
Colin Crossce85d952014-07-11 17:53:27 -0700621 ret = read_all(fd, line, sizeof(line) - 1);
622 close(fd);
623 if (ret < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700624 return NULL;
625 }
626
627 cp = strchr(line, ' ');
628 if (cp)
629 *cp = '\0';
630
631 return line;
632}
633
634static struct proc *proc_adj_lru(int oomadj) {
635 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
636}
637
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -0800638static struct proc *proc_get_heaviest(int oomadj) {
639 struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)];
640 struct adjslot_list *curr = head->next;
641 struct proc *maxprocp = NULL;
642 int maxsize = 0;
643 while (curr != head) {
644 int pid = ((struct proc *)curr)->pid;
645 int tasksize = proc_get_size(pid);
646 if (tasksize <= 0) {
647 struct adjslot_list *next = curr->next;
648 pid_remove(pid);
649 curr = next;
650 } else {
651 if (tasksize > maxsize) {
652 maxsize = tasksize;
653 maxprocp = (struct proc *)curr;
654 }
655 curr = curr->next;
656 }
657 }
658 return maxprocp;
659}
660
Colin Cross16b09462014-07-14 12:39:56 -0700661/* Kill one process specified by procp. Returns the size of the process killed */
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800662static int kill_one_process(struct proc* procp, int min_score_adj,
663 enum vmpressure_level level) {
Colin Cross16b09462014-07-14 12:39:56 -0700664 int pid = procp->pid;
665 uid_t uid = procp->uid;
666 char *taskname;
667 int tasksize;
668 int r;
669
670 taskname = proc_get_name(pid);
671 if (!taskname) {
672 pid_remove(pid);
673 return -1;
674 }
675
676 tasksize = proc_get_size(pid);
677 if (tasksize <= 0) {
678 pid_remove(pid);
679 return -1;
680 }
681
Suren Baghdasaryane1217c02018-01-04 10:43:58 -0800682 TRACE_KILL_START(pid);
683
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800684 r = kill(pid, SIGKILL);
Robert Beneacaeaa652017-08-11 16:03:20 -0700685 ALOGI(
686 "Killing '%s' (%d), uid %d, adj %d\n"
687 " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800688 taskname, pid, uid, procp->oomadj, tasksize * page_k,
689 level_name[level], min_score_adj);
Colin Cross16b09462014-07-14 12:39:56 -0700690 pid_remove(pid);
691
Suren Baghdasaryane1217c02018-01-04 10:43:58 -0800692 TRACE_KILL_END();
693
Colin Cross16b09462014-07-14 12:39:56 -0700694 if (r) {
Mark Salyzyn919f5382018-02-04 15:27:23 -0800695 ALOGE("kill(%d): errno=%d", pid, errno);
Colin Cross16b09462014-07-14 12:39:56 -0700696 return -1;
Colin Cross16b09462014-07-14 12:39:56 -0700697 }
Mark Salyzyn919f5382018-02-04 15:27:23 -0800698
699 return tasksize;
Colin Cross16b09462014-07-14 12:39:56 -0700700}
701
702/*
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800703 * Find processes to kill to free required number of pages.
704 * If pages_to_free is set to 0 only one process will be killed.
705 * Returns the size of the killed processes.
Colin Cross16b09462014-07-14 12:39:56 -0700706 */
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800707static int find_and_kill_processes(enum vmpressure_level level,
708 int pages_to_free) {
Colin Cross16b09462014-07-14 12:39:56 -0700709 int i;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800710 int killed_size;
711 int pages_freed = 0;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800712 int min_score_adj = level_oomadj[level];
Colin Cross16b09462014-07-14 12:39:56 -0700713
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700714 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
Colin Cross16b09462014-07-14 12:39:56 -0700715 struct proc *procp;
716
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800717 while (true) {
718 if (is_go_device)
719 procp = proc_adj_lru(i);
720 else
721 procp = proc_get_heaviest(i);
Colin Cross16b09462014-07-14 12:39:56 -0700722
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800723 if (!procp)
724 break;
725
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800726 killed_size = kill_one_process(procp, min_score_adj, level);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800727 if (killed_size >= 0) {
728 pages_freed += killed_size;
729 if (pages_freed >= pages_to_free) {
730 return pages_freed;
731 }
Colin Cross16b09462014-07-14 12:39:56 -0700732 }
733 }
734 }
735
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800736 return pages_freed;
Colin Cross16b09462014-07-14 12:39:56 -0700737}
738
Robert Beneac47f2992017-08-21 15:18:31 -0700739static int64_t get_memory_usage(const char* path) {
740 int ret;
741 int64_t mem_usage;
742 char buf[32];
743 int fd = open(path, O_RDONLY | O_CLOEXEC);
744 if (fd == -1) {
745 ALOGE("%s open: errno=%d", path, errno);
746 return -1;
747 }
748
749 ret = read_all(fd, buf, sizeof(buf) - 1);
750 close(fd);
751 if (ret < 0) {
752 ALOGE("%s error: errno=%d", path, errno);
753 return -1;
754 }
755 sscanf(buf, "%" SCNd64, &mem_usage);
756 if (mem_usage == 0) {
757 ALOGE("No memory!");
758 return -1;
759 }
760 return mem_usage;
761}
762
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800763void record_low_pressure_levels(struct mem_size *free_mem) {
764 if (low_pressure_mem.min_free == -1 ||
765 low_pressure_mem.min_free > free_mem->free_mem) {
766 if (debug_process_killing) {
767 ALOGI("Low pressure min memory update from %d to %d",
768 low_pressure_mem.min_free, free_mem->free_mem);
769 }
770 low_pressure_mem.min_free = free_mem->free_mem;
771 }
772 /*
773 * Free memory at low vmpressure events occasionally gets spikes,
774 * possibly a stale low vmpressure event with memory already
775 * freed up (no memory pressure should have been reported).
776 * Ignore large jumps in max_free that would mess up our stats.
777 */
778 if (low_pressure_mem.max_free == -1 ||
779 (low_pressure_mem.max_free < free_mem->free_mem &&
780 free_mem->free_mem - low_pressure_mem.max_free < low_pressure_mem.max_free * 0.1)) {
781 if (debug_process_killing) {
782 ALOGI("Low pressure max memory update from %d to %d",
783 low_pressure_mem.max_free, free_mem->free_mem);
784 }
785 low_pressure_mem.max_free = free_mem->free_mem;
786 }
787}
788
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800789enum vmpressure_level upgrade_level(enum vmpressure_level level) {
790 return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ?
791 level + 1 : level);
792}
793
794enum vmpressure_level downgrade_level(enum vmpressure_level level) {
795 return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ?
796 level - 1 : level);
797}
798
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800799static inline unsigned long get_time_diff_ms(struct timeval *from,
800 struct timeval *to) {
801 return (to->tv_sec - from->tv_sec) * 1000 +
802 (to->tv_usec - from->tv_usec) / 1000;
803}
804
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800805static void mp_event_common(enum vmpressure_level level) {
Todd Poynor3948f802013-07-09 19:35:14 -0700806 int ret;
807 unsigned long long evcount;
Robert Beneac47f2992017-08-21 15:18:31 -0700808 int64_t mem_usage, memsw_usage;
Robert Benea6e8e7102017-09-13 15:20:30 -0700809 int64_t mem_pressure;
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -0800810 enum vmpressure_level lvl;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800811 struct mem_size free_mem;
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800812 static struct timeval last_report_tm;
813 static unsigned long skip_count = 0;
Todd Poynor3948f802013-07-09 19:35:14 -0700814
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -0800815 /*
816 * Check all event counters from low to critical
817 * and upgrade to the highest priority one. By reading
818 * eventfd we also reset the event counters.
819 */
820 for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
821 if (mpevfd[lvl] != -1 &&
822 read(mpevfd[lvl], &evcount, sizeof(evcount)) > 0 &&
823 evcount > 0 && lvl > level) {
824 level = lvl;
825 }
826 }
Todd Poynor3948f802013-07-09 19:35:14 -0700827
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800828 if (kill_timeout_ms) {
829 struct timeval curr_tm;
830 gettimeofday(&curr_tm, NULL);
831 if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) {
832 skip_count++;
833 return;
834 }
835 }
836
837 if (skip_count > 0) {
838 if (debug_process_killing) {
839 ALOGI("%lu memory pressure events were skipped after a kill!",
840 skip_count);
841 }
842 skip_count = 0;
843 }
844
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800845 if (get_free_memory(&free_mem) == 0) {
846 if (level == VMPRESS_LEVEL_LOW) {
847 record_low_pressure_levels(&free_mem);
848 }
849 } else {
850 ALOGE("Failed to get free memory!");
851 return;
852 }
853
854 if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
855 /* Do not monitor this pressure level */
856 return;
857 }
858
Robert Benea6e8e7102017-09-13 15:20:30 -0700859 mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
860 memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
861 if (memsw_usage < 0 || mem_usage < 0) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800862 goto do_kill;
Robert Benea6e8e7102017-09-13 15:20:30 -0700863 }
Robert Beneac47f2992017-08-21 15:18:31 -0700864
Robert Benea6e8e7102017-09-13 15:20:30 -0700865 // Calculate percent for swappinness.
866 mem_pressure = (mem_usage * 100) / memsw_usage;
867
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800868 if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
Robert Benea6e8e7102017-09-13 15:20:30 -0700869 // We are swapping too much.
870 if (mem_pressure < upgrade_pressure) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800871 level = upgrade_level(level);
872 if (debug_process_killing) {
873 ALOGI("Event upgraded to %s", level_name[level]);
874 }
Robert Beneac47f2992017-08-21 15:18:31 -0700875 }
876 }
877
Robert Benea6e8e7102017-09-13 15:20:30 -0700878 // If the pressure is larger than downgrade_pressure lmk will not
879 // kill any process, since enough memory is available.
880 if (mem_pressure > downgrade_pressure) {
881 if (debug_process_killing) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800882 ALOGI("Ignore %s memory pressure", level_name[level]);
Robert Benea6e8e7102017-09-13 15:20:30 -0700883 }
884 return;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800885 } else if (level == VMPRESS_LEVEL_CRITICAL &&
886 mem_pressure > upgrade_pressure) {
Robert Benea6e8e7102017-09-13 15:20:30 -0700887 if (debug_process_killing) {
888 ALOGI("Downgrade critical memory pressure");
889 }
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800890 // Downgrade event, since enough memory available.
891 level = downgrade_level(level);
Robert Benea6e8e7102017-09-13 15:20:30 -0700892 }
893
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800894do_kill:
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800895 if (is_go_device) {
896 /* For Go devices kill only one task */
897 if (find_and_kill_processes(level, 0) == 0) {
898 if (debug_process_killing) {
899 ALOGI("Nothing to kill");
900 }
901 }
902 } else {
903 /* If pressure level is less than critical and enough free swap then ignore */
904 if (level < VMPRESS_LEVEL_CRITICAL && free_mem.free_swap > low_pressure_mem.max_free) {
905 if (debug_process_killing) {
906 ALOGI("Ignoring pressure since %d swap pages are available ", free_mem.free_swap);
907 }
908 return;
909 }
910
911 /* Free up enough memory to downgrate the memory pressure to low level */
912 if (free_mem.free_mem < low_pressure_mem.max_free) {
913 int pages_to_free = low_pressure_mem.max_free - free_mem.free_mem;
914 if (debug_process_killing) {
915 ALOGI("Trying to free %d pages", pages_to_free);
916 }
917 int pages_freed = find_and_kill_processes(level, pages_to_free);
918 if (pages_freed < pages_to_free) {
919 if (debug_process_killing) {
920 ALOGI("Unable to free enough memory (pages freed=%d)",
921 pages_freed);
922 }
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800923 } else {
924 gettimeofday(&last_report_tm, NULL);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800925 }
Robert Beneacaeaa652017-08-11 16:03:20 -0700926 }
Colin Crossf8857cc2014-07-11 17:16:56 -0700927 }
Todd Poynor3948f802013-07-09 19:35:14 -0700928}
929
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800930static void mp_event_low(uint32_t events __unused) {
931 mp_event_common(VMPRESS_LEVEL_LOW);
932}
933
934static void mp_event_medium(uint32_t events __unused) {
935 mp_event_common(VMPRESS_LEVEL_MEDIUM);
Robert Benea673e2762017-06-01 16:32:31 -0700936}
937
938static void mp_event_critical(uint32_t events __unused) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800939 mp_event_common(VMPRESS_LEVEL_CRITICAL);
Robert Benea673e2762017-06-01 16:32:31 -0700940}
941
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800942static bool init_mp_common(void *event_handler, enum vmpressure_level level) {
Todd Poynor3948f802013-07-09 19:35:14 -0700943 int mpfd;
944 int evfd;
945 int evctlfd;
946 char buf[256];
947 struct epoll_event epev;
948 int ret;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800949 const char *levelstr = level_name[level];
950
Nick Kralevichc68c8862015-12-18 20:52:37 -0800951 mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700952 if (mpfd < 0) {
953 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
954 goto err_open_mpfd;
955 }
956
Nick Kralevichc68c8862015-12-18 20:52:37 -0800957 evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700958 if (evctlfd < 0) {
959 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
960 goto err_open_evctlfd;
961 }
962
Nick Kralevichc68c8862015-12-18 20:52:37 -0800963 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -0700964 if (evfd < 0) {
965 ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
966 goto err_eventfd;
967 }
968
969 ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
970 if (ret >= (ssize_t)sizeof(buf)) {
971 ALOGE("cgroup.event_control line overflow for level %s", levelstr);
972 goto err;
973 }
974
975 ret = write(evctlfd, buf, strlen(buf) + 1);
976 if (ret == -1) {
977 ALOGE("cgroup.event_control write failed for level %s; errno=%d",
978 levelstr, errno);
979 goto err;
980 }
981
982 epev.events = EPOLLIN;
983 epev.data.ptr = event_handler;
984 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
985 if (ret == -1) {
986 ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
987 goto err;
988 }
989 maxevents++;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800990 mpevfd[level] = evfd;
Suren Baghdasaryanab05d672018-01-04 08:54:53 -0800991 close(evctlfd);
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800992 return true;
Todd Poynor3948f802013-07-09 19:35:14 -0700993
994err:
995 close(evfd);
996err_eventfd:
997 close(evctlfd);
998err_open_evctlfd:
999 close(mpfd);
1000err_open_mpfd:
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001001 return false;
Robert Benea673e2762017-06-01 16:32:31 -07001002}
1003
Todd Poynor3948f802013-07-09 19:35:14 -07001004static int init(void) {
1005 struct epoll_event epev;
1006 int i;
1007 int ret;
1008
1009 page_k = sysconf(_SC_PAGESIZE);
1010 if (page_k == -1)
1011 page_k = PAGE_SIZE;
1012 page_k /= 1024;
1013
1014 epollfd = epoll_create(MAX_EPOLL_EVENTS);
1015 if (epollfd == -1) {
1016 ALOGE("epoll_create failed (errno=%d)", errno);
1017 return -1;
1018 }
1019
1020 ctrl_lfd = android_get_control_socket("lmkd");
1021 if (ctrl_lfd < 0) {
1022 ALOGE("get lmkd control socket failed");
1023 return -1;
1024 }
1025
1026 ret = listen(ctrl_lfd, 1);
1027 if (ret < 0) {
1028 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
1029 return -1;
1030 }
1031
1032 epev.events = EPOLLIN;
1033 epev.data.ptr = (void *)ctrl_connect_handler;
1034 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
1035 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
1036 return -1;
1037 }
1038 maxevents++;
1039
Robert Benea164baeb2017-09-11 16:53:28 -07001040 has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
Suren Baghdasaryan2088f6a2018-01-18 17:27:30 -08001041 use_inkernel_interface = has_inkernel_module;
Todd Poynor3948f802013-07-09 19:35:14 -07001042
1043 if (use_inkernel_interface) {
1044 ALOGI("Using in-kernel low memory killer interface");
1045 } else {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001046 if (!init_mp_common((void *)&mp_event_low, VMPRESS_LEVEL_LOW) ||
1047 !init_mp_common((void *)&mp_event_medium, VMPRESS_LEVEL_MEDIUM) ||
1048 !init_mp_common((void *)&mp_event_critical,
1049 VMPRESS_LEVEL_CRITICAL)) {
Todd Poynor3948f802013-07-09 19:35:14 -07001050 ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001051 return -1;
1052 }
Todd Poynor3948f802013-07-09 19:35:14 -07001053 }
1054
Chong Zhang0a4acdf2015-10-14 16:19:53 -07001055 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
Todd Poynor3948f802013-07-09 19:35:14 -07001056 procadjslot_list[i].next = &procadjslot_list[i];
1057 procadjslot_list[i].prev = &procadjslot_list[i];
1058 }
1059
1060 return 0;
1061}
1062
1063static void mainloop(void) {
1064 while (1) {
1065 struct epoll_event events[maxevents];
1066 int nevents;
1067 int i;
1068
1069 ctrl_dfd_reopened = 0;
1070 nevents = epoll_wait(epollfd, events, maxevents, -1);
1071
1072 if (nevents == -1) {
1073 if (errno == EINTR)
1074 continue;
1075 ALOGE("epoll_wait failed (errno=%d)", errno);
1076 continue;
1077 }
1078
1079 for (i = 0; i < nevents; ++i) {
1080 if (events[i].events & EPOLLERR)
1081 ALOGD("EPOLLERR on event #%d", i);
1082 if (events[i].data.ptr)
1083 (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
1084 }
1085 }
1086}
1087
Mark Salyzyne6ed68b2014-04-30 13:36:35 -07001088int main(int argc __unused, char **argv __unused) {
Colin Cross1a0d9be2014-07-14 14:31:15 -07001089 struct sched_param param = {
1090 .sched_priority = 1,
1091 };
1092
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001093 /* By default disable low level vmpressure events */
1094 level_oomadj[VMPRESS_LEVEL_LOW] =
1095 property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1);
1096 level_oomadj[VMPRESS_LEVEL_MEDIUM] =
1097 property_get_int32("ro.lmk.medium", 800);
1098 level_oomadj[VMPRESS_LEVEL_CRITICAL] =
1099 property_get_int32("ro.lmk.critical", 0);
Robert Beneacaeaa652017-08-11 16:03:20 -07001100 debug_process_killing = property_get_bool("ro.lmk.debug", false);
Suren Baghdasaryane5c964312017-12-08 13:08:41 -08001101
1102 /* By default disable upgrade/downgrade logic */
1103 enable_pressure_upgrade =
1104 property_get_bool("ro.lmk.critical_upgrade", false);
1105 upgrade_pressure =
1106 (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100);
1107 downgrade_pressure =
1108 (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100);
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -08001109 kill_heaviest_task =
1110 property_get_bool("ro.lmk.kill_heaviest_task", true);
Robert Benea164baeb2017-09-11 16:53:28 -07001111 is_go_device = property_get_bool("ro.config.low_ram", false);
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -08001112 kill_timeout_ms =
1113 (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0);
Robert Benea58891d52017-07-31 17:15:20 -07001114
Daniel Colascioned39adf22018-01-05 14:59:55 -08001115 // MCL_ONFAULT pins pages as they fault instead of loading
1116 // everything immediately all at once. (Which would be bad,
1117 // because as of this writing, we have a lot of mapped pages we
1118 // never use.) Old kernels will see MCL_ONFAULT and fail with
1119 // EINVAL; we ignore this failure.
1120 //
1121 // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
1122 // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
1123 // in pages.
1124 if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && errno != EINVAL)
Daniel Colascione4dd5d002018-01-03 12:01:02 -08001125 ALOGW("mlockall failed: errno=%d", errno);
1126
Colin Cross1a0d9be2014-07-14 14:31:15 -07001127 sched_setscheduler(0, SCHED_FIFO, &param);
Todd Poynor3948f802013-07-09 19:35:14 -07001128 if (!init())
1129 mainloop();
1130
1131 ALOGI("exiting");
1132 return 0;
1133}