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> |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 32 | #include <sys/sysinfo.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> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 38 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 39 | /* |
| 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 Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 58 | #ifndef __unused |
| 59 | #define __unused __attribute__((__unused__)) |
| 60 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 61 | |
| 62 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 63 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" |
| 64 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 65 | #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 Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 72 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 73 | |
| 74 | enum 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 */ |
| 88 | static int use_inkernel_interface = 1; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 89 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 90 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 91 | /* memory pressure levels */ |
| 92 | enum vmpressure_level { |
| 93 | VMPRESS_LEVEL_LOW = 0, |
| 94 | VMPRESS_LEVEL_MEDIUM, |
| 95 | VMPRESS_LEVEL_CRITICAL, |
| 96 | VMPRESS_LEVEL_COUNT |
| 97 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 98 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 99 | static const char *level_name[] = { |
| 100 | "low", |
| 101 | "medium", |
| 102 | "critical" |
| 103 | }; |
| 104 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 105 | struct mem_size { |
| 106 | int free_mem; |
| 107 | int free_swap; |
| 108 | }; |
| 109 | |
| 110 | struct { |
| 111 | int min_free; /* recorded but not used yet */ |
| 112 | int max_free; |
| 113 | } low_pressure_mem = { -1, -1 }; |
| 114 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 115 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 116 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 117 | static bool debug_process_killing; |
| 118 | static bool enable_pressure_upgrade; |
| 119 | static int64_t upgrade_pressure; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 120 | static int64_t downgrade_pressure; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 121 | static bool is_go_device; |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 122 | static bool kill_heaviest_task; |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 123 | static unsigned long kill_timeout_ms; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 124 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 125 | /* control socket listen and data */ |
| 126 | static int ctrl_lfd; |
| 127 | static int ctrl_dfd = -1; |
| 128 | static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */ |
| 129 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 130 | /* 3 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */ |
| 131 | #define MAX_EPOLL_EVENTS 5 |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 132 | static int epollfd; |
| 133 | static int maxevents; |
| 134 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 135 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 136 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 137 | #define OOM_SCORE_ADJ_MAX 1000 |
| 138 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 139 | static int lowmem_adj[MAX_TARGETS]; |
| 140 | static int lowmem_minfree[MAX_TARGETS]; |
| 141 | static int lowmem_targets_size; |
| 142 | |
| 143 | struct sysmeminfo { |
| 144 | int nr_free_pages; |
| 145 | int nr_file_pages; |
| 146 | int nr_shmem; |
| 147 | int totalreserve_pages; |
| 148 | }; |
| 149 | |
| 150 | struct adjslot_list { |
| 151 | struct adjslot_list *next; |
| 152 | struct adjslot_list *prev; |
| 153 | }; |
| 154 | |
| 155 | struct proc { |
| 156 | struct adjslot_list asl; |
| 157 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 158 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 159 | int oomadj; |
| 160 | struct proc *pidhash_next; |
| 161 | }; |
| 162 | |
| 163 | #define PIDHASH_SZ 1024 |
| 164 | static struct proc *pidhash[PIDHASH_SZ]; |
| 165 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 166 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 167 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 168 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 169 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 170 | /* PAGE_SIZE / 1024 */ |
| 171 | static long page_k; |
| 172 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 173 | static 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 193 | static 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 | |
| 203 | static 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 | |
| 212 | static 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 | |
| 220 | static 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 | |
| 226 | static void proc_slot(struct proc *procp) { |
| 227 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 228 | |
| 229 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 230 | } |
| 231 | |
| 232 | static void proc_unslot(struct proc *procp) { |
| 233 | adjslot_remove(&procp->asl); |
| 234 | } |
| 235 | |
| 236 | static 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 | |
| 244 | static 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 266 | static void writefilestring(const char *path, char *s) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 267 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 268 | 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 Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 286 | static void cmd_procprio(int pid, int uid, int oomadj) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 287 | struct proc *procp; |
| 288 | char path[80]; |
| 289 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 290 | int soft_limit_mult; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 291 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 292 | if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 293 | ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj); |
| 294 | return; |
| 295 | } |
| 296 | |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 297 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid); |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 298 | snprintf(val, sizeof(val), "%d", oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 299 | writefilestring(path, val); |
| 300 | |
| 301 | if (use_inkernel_interface) |
| 302 | return; |
| 303 | |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 304 | 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 Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 311 | // Launcher should be perceptible, don't kill it. |
| 312 | oomadj = 200; |
| 313 | soft_limit_mult = 1; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 314 | } 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 336 | 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 Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 345 | procp->uid = uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 346 | procp->oomadj = oomadj; |
| 347 | proc_insert(procp); |
| 348 | } else { |
| 349 | proc_unslot(procp); |
| 350 | procp->oomadj = oomadj; |
| 351 | proc_slot(procp); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void cmd_procremove(int pid) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 356 | if (use_inkernel_interface) |
| 357 | return; |
| 358 | |
| 359 | pid_remove(pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static 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 Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 375 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 376 | 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 Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 390 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 391 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 392 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 393 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 394 | } |
| 395 | |
| 396 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr); |
| 397 | writefilestring(INKERNEL_ADJ_PATH, killpriostr); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void ctrl_data_close(void) { |
| 402 | ALOGI("Closing Activity Manager data connection"); |
| 403 | close(ctrl_dfd); |
| 404 | ctrl_dfd = -1; |
| 405 | maxevents--; |
| 406 | } |
| 407 | |
| 408 | static 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 | |
| 423 | static 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 Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 448 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 449 | goto wronglen; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 450 | cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3])); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 451 | 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 | |
| 464 | wronglen: |
| 465 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 466 | } |
| 467 | |
| 468 | static 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 Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 478 | static void ctrl_connect_handler(uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 479 | struct epoll_event epev; |
| 480 | |
| 481 | if (ctrl_dfd >= 0) { |
| 482 | ctrl_data_close(); |
| 483 | ctrl_dfd_reopened = 1; |
| 484 | } |
| 485 | |
Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 486 | ctrl_dfd = accept(ctrl_lfd, NULL, NULL); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 487 | |
| 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 | |
| 504 | static int zoneinfo_parse_protection(char *cp) { |
| 505 | int max = 0; |
| 506 | int zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 507 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 508 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 509 | 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] | 510 | zoneval = strtol(cp, &cp, 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 511 | if (zoneval > max) |
| 512 | max = zoneval; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 513 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 514 | |
| 515 | return max; |
| 516 | } |
| 517 | |
| 518 | static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) { |
| 519 | char *cp = line; |
| 520 | char *ap; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 521 | char *save_ptr; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 522 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 523 | cp = strtok_r(line, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 524 | if (!cp) |
| 525 | return; |
| 526 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 527 | ap = strtok_r(NULL, " ", &save_ptr); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 528 | 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 | |
| 543 | static int zoneinfo_parse(struct sysmeminfo *mip) { |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 544 | int fd; |
| 545 | ssize_t size; |
| 546 | char buf[PAGE_SIZE]; |
| 547 | char *save_ptr; |
| 548 | char *line; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 549 | |
| 550 | memset(mip, 0, sizeof(struct sysmeminfo)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 551 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 552 | fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 553 | if (fd == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 554 | ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno); |
| 555 | return -1; |
| 556 | } |
| 557 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 558 | 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 568 | zoneinfo_parse_line(line, mip); |
| 569 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 570 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 574 | static 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 586 | static int proc_get_size(int pid) { |
| 587 | char path[PATH_MAX]; |
| 588 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 589 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 590 | int rss = 0; |
| 591 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 592 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 593 | |
| 594 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 595 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 596 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 597 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 598 | |
| 599 | ret = read_all(fd, line, sizeof(line) - 1); |
| 600 | if (ret < 0) { |
| 601 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 602 | return -1; |
| 603 | } |
| 604 | |
| 605 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 606 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 607 | return rss; |
| 608 | } |
| 609 | |
| 610 | static char *proc_get_name(int pid) { |
| 611 | char path[PATH_MAX]; |
| 612 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 613 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 614 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 615 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 616 | |
| 617 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 618 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 619 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 620 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 621 | ret = read_all(fd, line, sizeof(line) - 1); |
| 622 | close(fd); |
| 623 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | cp = strchr(line, ' '); |
| 628 | if (cp) |
| 629 | *cp = '\0'; |
| 630 | |
| 631 | return line; |
| 632 | } |
| 633 | |
| 634 | static struct proc *proc_adj_lru(int oomadj) { |
| 635 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 636 | } |
| 637 | |
Suren Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 638 | static 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 Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 661 | /* Kill one process specified by procp. Returns the size of the process killed */ |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 662 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 663 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 664 | 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 Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 682 | TRACE_KILL_START(pid); |
| 683 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 684 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 685 | 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 688 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 689 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 690 | pid_remove(pid); |
| 691 | |
Suren Baghdasaryan | e1217c0 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 692 | TRACE_KILL_END(); |
| 693 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 694 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 695 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 696 | return -1; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 697 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 698 | |
| 699 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 703 | * 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 Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 706 | */ |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 707 | static int find_and_kill_processes(enum vmpressure_level level, |
| 708 | int pages_to_free) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 709 | int i; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 710 | int killed_size; |
| 711 | int pages_freed = 0; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 712 | int min_score_adj = level_oomadj[level]; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 713 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 714 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 715 | struct proc *procp; |
| 716 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 717 | while (true) { |
| 718 | if (is_go_device) |
| 719 | procp = proc_adj_lru(i); |
| 720 | else |
| 721 | procp = proc_get_heaviest(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 722 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 723 | if (!procp) |
| 724 | break; |
| 725 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 726 | killed_size = kill_one_process(procp, min_score_adj, level); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 727 | if (killed_size >= 0) { |
| 728 | pages_freed += killed_size; |
| 729 | if (pages_freed >= pages_to_free) { |
| 730 | return pages_freed; |
| 731 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 736 | return pages_freed; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 739 | static 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 Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 763 | void 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 789 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 790 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 791 | level + 1 : level); |
| 792 | } |
| 793 | |
| 794 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 795 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 796 | level - 1 : level); |
| 797 | } |
| 798 | |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 799 | static 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 805 | static void mp_event_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 806 | int ret; |
| 807 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 808 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 809 | int64_t mem_pressure; |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 810 | enum vmpressure_level lvl; |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 811 | struct mem_size free_mem; |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 812 | static struct timeval last_report_tm; |
| 813 | static unsigned long skip_count = 0; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 814 | |
Suren Baghdasaryan | 1a2589e | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 815 | /* |
| 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 827 | |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 828 | 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 Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 845 | 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 Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 859 | 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 862 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 863 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 864 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 865 | // Calculate percent for swappinness. |
| 866 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 867 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 868 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 869 | // We are swapping too much. |
| 870 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 871 | level = upgrade_level(level); |
| 872 | if (debug_process_killing) { |
| 873 | ALOGI("Event upgraded to %s", level_name[level]); |
| 874 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 878 | // 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 882 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 883 | } |
| 884 | return; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 885 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 886 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 887 | if (debug_process_killing) { |
| 888 | ALOGI("Downgrade critical memory pressure"); |
| 889 | } |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 890 | // Downgrade event, since enough memory available. |
| 891 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 894 | do_kill: |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 895 | 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 Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 923 | } else { |
| 924 | gettimeofday(&last_report_tm, NULL); |
Suren Baghdasaryan | aa73baf | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 925 | } |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 926 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 927 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 930 | static void mp_event_low(uint32_t events __unused) { |
| 931 | mp_event_common(VMPRESS_LEVEL_LOW); |
| 932 | } |
| 933 | |
| 934 | static void mp_event_medium(uint32_t events __unused) { |
| 935 | mp_event_common(VMPRESS_LEVEL_MEDIUM); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static void mp_event_critical(uint32_t events __unused) { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 939 | mp_event_common(VMPRESS_LEVEL_CRITICAL); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 942 | static bool init_mp_common(void *event_handler, enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 943 | int mpfd; |
| 944 | int evfd; |
| 945 | int evctlfd; |
| 946 | char buf[256]; |
| 947 | struct epoll_event epev; |
| 948 | int ret; |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 949 | const char *levelstr = level_name[level]; |
| 950 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 951 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 952 | if (mpfd < 0) { |
| 953 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 954 | goto err_open_mpfd; |
| 955 | } |
| 956 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 957 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 958 | if (evctlfd < 0) { |
| 959 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 960 | goto err_open_evctlfd; |
| 961 | } |
| 962 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 963 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 964 | 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 Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 990 | mpevfd[level] = evfd; |
Suren Baghdasaryan | ab05d67 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 991 | close(evctlfd); |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 992 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 993 | |
| 994 | err: |
| 995 | close(evfd); |
| 996 | err_eventfd: |
| 997 | close(evctlfd); |
| 998 | err_open_evctlfd: |
| 999 | close(mpfd); |
| 1000 | err_open_mpfd: |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1001 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1004 | static 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 Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1040 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
| 1041 | use_inkernel_interface = has_inkernel_module && !is_go_device; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1042 | |
| 1043 | if (use_inkernel_interface) { |
| 1044 | ALOGI("Using in-kernel low memory killer interface"); |
| 1045 | } else { |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1046 | 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 Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1050 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1055 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1056 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 1057 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 1058 | } |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static 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 Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 1088 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1089 | struct sched_param param = { |
| 1090 | .sched_priority = 1, |
| 1091 | }; |
| 1092 | |
Suren Baghdasaryan | cd7ad2f | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1093 | /* 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 Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1100 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | e5c96431 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 1101 | |
| 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 Baghdasaryan | b93764d | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1109 | kill_heaviest_task = |
| 1110 | property_get_bool("ro.lmk.kill_heaviest_task", true); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1111 | is_go_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | 63dadcf | 2018-01-17 17:28:01 -0800 | [diff] [blame^] | 1112 | kill_timeout_ms = |
| 1113 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 1114 | |
Daniel Colascione | d39adf2 | 2018-01-05 14:59:55 -0800 | [diff] [blame] | 1115 | // 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 Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 1125 | ALOGW("mlockall failed: errno=%d", errno); |
| 1126 | |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1127 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1128 | if (!init()) |
| 1129 | mainloop(); |
| 1130 | |
| 1131 | ALOGI("exiting"); |
| 1132 | return 0; |
| 1133 | } |