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