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