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 | |
| 19 | #include <errno.h> |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 21 | #include <pwd.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> |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 27 | #include <sys/cdefs.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> |
| 29 | #include <sys/eventfd.h> |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 30 | #include <sys/mman.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | #include <sys/types.h> |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 33 | #include <sys/sysinfo.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 38 | #include <lmkd.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 40 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 41 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 42 | #include <statslog.h> |
| 43 | #endif |
| 44 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces |
| 47 | * to profile and correlate with OOM kills |
| 48 | */ |
| 49 | #ifdef LMKD_TRACE_KILLS |
| 50 | |
| 51 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
| 52 | #include <cutils/trace.h> |
| 53 | |
| 54 | #define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid); |
| 55 | #define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0); |
| 56 | |
| 57 | #else /* LMKD_TRACE_KILLS */ |
| 58 | |
Daniel Colascione | 347f6b4 | 2018-02-12 11:24:47 -0800 | [diff] [blame] | 59 | #define TRACE_KILL_START(pid) ((void)(pid)) |
| 60 | #define TRACE_KILL_END() ((void)0) |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 61 | |
| 62 | #endif /* LMKD_TRACE_KILLS */ |
| 63 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 64 | #ifndef __unused |
| 65 | #define __unused __attribute__((__unused__)) |
| 66 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 67 | |
| 68 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 69 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" |
| 70 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 71 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 72 | #define LINE_MAX 128 |
| 73 | |
| 74 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 75 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 76 | |
| 77 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 78 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 79 | |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 80 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ |
| 81 | #define SYSTEM_ADJ (-900) |
| 82 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 83 | /* default to old in-kernel interface if no memory pressure events */ |
| 84 | static int use_inkernel_interface = 1; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 85 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 86 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 87 | /* memory pressure levels */ |
| 88 | enum vmpressure_level { |
| 89 | VMPRESS_LEVEL_LOW = 0, |
| 90 | VMPRESS_LEVEL_MEDIUM, |
| 91 | VMPRESS_LEVEL_CRITICAL, |
| 92 | VMPRESS_LEVEL_COUNT |
| 93 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 94 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 95 | static const char *level_name[] = { |
| 96 | "low", |
| 97 | "medium", |
| 98 | "critical" |
| 99 | }; |
| 100 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 101 | struct mem_size { |
| 102 | int free_mem; |
| 103 | int free_swap; |
| 104 | }; |
| 105 | |
| 106 | struct { |
| 107 | int min_free; /* recorded but not used yet */ |
| 108 | int max_free; |
| 109 | } low_pressure_mem = { -1, -1 }; |
| 110 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 111 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 112 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 113 | static bool debug_process_killing; |
| 114 | static bool enable_pressure_upgrade; |
| 115 | static int64_t upgrade_pressure; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 116 | static int64_t downgrade_pressure; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 117 | static bool is_go_device; |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 118 | static bool kill_heaviest_task; |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 119 | static unsigned long kill_timeout_ms; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 120 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 121 | /* data required to handle events */ |
| 122 | struct event_handler_info { |
| 123 | int data; |
| 124 | void (*handler)(int data, uint32_t events); |
| 125 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 126 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 127 | /* data required to handle socket events */ |
| 128 | struct sock_event_handler_info { |
| 129 | int sock; |
| 130 | struct event_handler_info handler_info; |
| 131 | }; |
| 132 | |
| 133 | /* max supported number of data connections */ |
| 134 | #define MAX_DATA_CONN 2 |
| 135 | |
| 136 | /* socket event handler data */ |
| 137 | static struct sock_event_handler_info ctrl_sock; |
| 138 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; |
| 139 | |
| 140 | /* vmpressure event handler data */ |
| 141 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; |
| 142 | |
| 143 | /* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */ |
| 144 | #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 145 | static int epollfd; |
| 146 | static int maxevents; |
| 147 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 148 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 149 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 150 | #define OOM_SCORE_ADJ_MAX 1000 |
| 151 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 152 | static int lowmem_adj[MAX_TARGETS]; |
| 153 | static int lowmem_minfree[MAX_TARGETS]; |
| 154 | static int lowmem_targets_size; |
| 155 | |
| 156 | struct sysmeminfo { |
| 157 | int nr_free_pages; |
| 158 | int nr_file_pages; |
| 159 | int nr_shmem; |
| 160 | int totalreserve_pages; |
| 161 | }; |
| 162 | |
| 163 | struct adjslot_list { |
| 164 | struct adjslot_list *next; |
| 165 | struct adjslot_list *prev; |
| 166 | }; |
| 167 | |
| 168 | struct proc { |
| 169 | struct adjslot_list asl; |
| 170 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 171 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 172 | int oomadj; |
| 173 | struct proc *pidhash_next; |
| 174 | }; |
| 175 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 176 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 177 | static bool enable_stats_log; |
| 178 | static android_log_context log_ctx; |
| 179 | #endif |
| 180 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 181 | #define PIDHASH_SZ 1024 |
| 182 | static struct proc *pidhash[PIDHASH_SZ]; |
| 183 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 184 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 185 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 186 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 187 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 188 | /* PAGE_SIZE / 1024 */ |
| 189 | static long page_k; |
| 190 | |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 191 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 192 | { |
| 193 | ssize_t ret = 0; |
| 194 | |
| 195 | while (max_len > 0) { |
| 196 | ssize_t r = read(fd, buf, max_len); |
| 197 | if (r == 0) { |
| 198 | break; |
| 199 | } |
| 200 | if (r == -1) { |
| 201 | return -1; |
| 202 | } |
| 203 | ret += r; |
| 204 | buf += r; |
| 205 | max_len -= r; |
| 206 | } |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 211 | static struct proc *pid_lookup(int pid) { |
| 212 | struct proc *procp; |
| 213 | |
| 214 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 215 | procp = procp->pidhash_next) |
| 216 | ; |
| 217 | |
| 218 | return procp; |
| 219 | } |
| 220 | |
| 221 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 222 | { |
| 223 | struct adjslot_list *next = head->next; |
| 224 | new->prev = head; |
| 225 | new->next = next; |
| 226 | next->prev = new; |
| 227 | head->next = new; |
| 228 | } |
| 229 | |
| 230 | static void adjslot_remove(struct adjslot_list *old) |
| 231 | { |
| 232 | struct adjslot_list *prev = old->prev; |
| 233 | struct adjslot_list *next = old->next; |
| 234 | next->prev = prev; |
| 235 | prev->next = next; |
| 236 | } |
| 237 | |
| 238 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 239 | struct adjslot_list *asl = head->prev; |
| 240 | |
| 241 | return asl == head ? NULL : asl; |
| 242 | } |
| 243 | |
| 244 | static void proc_slot(struct proc *procp) { |
| 245 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 246 | |
| 247 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 248 | } |
| 249 | |
| 250 | static void proc_unslot(struct proc *procp) { |
| 251 | adjslot_remove(&procp->asl); |
| 252 | } |
| 253 | |
| 254 | static void proc_insert(struct proc *procp) { |
| 255 | int hval = pid_hashfn(procp->pid); |
| 256 | |
| 257 | procp->pidhash_next = pidhash[hval]; |
| 258 | pidhash[hval] = procp; |
| 259 | proc_slot(procp); |
| 260 | } |
| 261 | |
| 262 | static int pid_remove(int pid) { |
| 263 | int hval = pid_hashfn(pid); |
| 264 | struct proc *procp; |
| 265 | struct proc *prevp; |
| 266 | |
| 267 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 268 | procp = procp->pidhash_next) |
| 269 | prevp = procp; |
| 270 | |
| 271 | if (!procp) |
| 272 | return -1; |
| 273 | |
| 274 | if (!prevp) |
| 275 | pidhash[hval] = procp->pidhash_next; |
| 276 | else |
| 277 | prevp->pidhash_next = procp->pidhash_next; |
| 278 | |
| 279 | proc_unslot(procp); |
| 280 | free(procp); |
| 281 | return 0; |
| 282 | } |
| 283 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 284 | /* |
| 285 | * Write a string to a file. |
| 286 | * Returns false if the file does not exist. |
| 287 | */ |
| 288 | static bool writefilestring(const char *path, const char *s, |
| 289 | bool err_if_missing) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 290 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 291 | ssize_t len = strlen(s); |
| 292 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 293 | |
| 294 | if (fd < 0) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 295 | if (err_if_missing) { |
| 296 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 297 | } |
| 298 | return false; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 301 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 302 | if (ret < 0) { |
| 303 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 304 | } else if (ret < len) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 305 | ALOGE("Short write on %s; length=%zd", path, ret); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | close(fd); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 309 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 312 | static void cmd_procprio(LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 313 | struct proc *procp; |
| 314 | char path[80]; |
| 315 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 316 | int soft_limit_mult; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 317 | struct lmk_procprio params; |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 318 | bool is_system_server; |
| 319 | struct passwd *pwdrec; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 320 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 321 | lmkd_pack_get_procprio(packet, ¶ms); |
| 322 | |
| 323 | if (params.oomadj < OOM_SCORE_ADJ_MIN || |
| 324 | params.oomadj > OOM_SCORE_ADJ_MAX) { |
| 325 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 329 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); |
| 330 | snprintf(val, sizeof(val), "%d", params.oomadj); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 331 | if (!writefilestring(path, val, false)) { |
| 332 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", |
| 333 | path, errno, params.pid); |
| 334 | /* If this file does not exist the process is dead. */ |
| 335 | return; |
| 336 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 337 | |
| 338 | if (use_inkernel_interface) |
| 339 | return; |
| 340 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 341 | if (params.oomadj >= 900) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 342 | soft_limit_mult = 0; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 343 | } else if (params.oomadj >= 800) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 344 | soft_limit_mult = 0; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 345 | } else if (params.oomadj >= 700) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 346 | soft_limit_mult = 0; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 347 | } else if (params.oomadj >= 600) { |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 348 | // Launcher should be perceptible, don't kill it. |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 349 | params.oomadj = 200; |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 350 | soft_limit_mult = 1; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 351 | } else if (params.oomadj >= 500) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 352 | soft_limit_mult = 0; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 353 | } else if (params.oomadj >= 400) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 354 | soft_limit_mult = 0; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 355 | } else if (params.oomadj >= 300) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 356 | soft_limit_mult = 1; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 357 | } else if (params.oomadj >= 200) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 358 | soft_limit_mult = 2; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 359 | } else if (params.oomadj >= 100) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 360 | soft_limit_mult = 10; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 361 | } else if (params.oomadj >= 0) { |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 362 | soft_limit_mult = 20; |
| 363 | } else { |
| 364 | // Persistent processes will have a large |
| 365 | // soft limit 512MB. |
| 366 | soft_limit_mult = 64; |
| 367 | } |
| 368 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 369 | snprintf(path, sizeof(path), |
| 370 | "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", |
| 371 | params.uid, params.pid); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 372 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * system_server process has no memcg under /dev/memcg/apps but should be |
| 376 | * registered with lmkd. This is the best way so far to identify it. |
| 377 | */ |
| 378 | is_system_server = (params.oomadj == SYSTEM_ADJ && |
| 379 | (pwdrec = getpwnam("system")) != NULL && |
| 380 | params.uid == pwdrec->pw_uid); |
| 381 | writefilestring(path, val, !is_system_server); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 382 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 383 | procp = pid_lookup(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 384 | if (!procp) { |
| 385 | procp = malloc(sizeof(struct proc)); |
| 386 | if (!procp) { |
| 387 | // Oh, the irony. May need to rebuild our state. |
| 388 | return; |
| 389 | } |
| 390 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 391 | procp->pid = params.pid; |
| 392 | procp->uid = params.uid; |
| 393 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 394 | proc_insert(procp); |
| 395 | } else { |
| 396 | proc_unslot(procp); |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 397 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 398 | proc_slot(procp); |
| 399 | } |
| 400 | } |
| 401 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 402 | static void cmd_procremove(LMKD_CTRL_PACKET packet) { |
| 403 | struct lmk_procremove params; |
| 404 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 405 | if (use_inkernel_interface) |
| 406 | return; |
| 407 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 408 | lmkd_pack_get_procremove(packet, ¶ms); |
| 409 | pid_remove(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 412 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 413 | int i; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 414 | struct lmk_target target; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 415 | |
| 416 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 417 | return; |
| 418 | |
| 419 | for (i = 0; i < ntargets; i++) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 420 | lmkd_pack_get_target(packet, i, &target); |
| 421 | lowmem_minfree[i] = target.minfree; |
| 422 | lowmem_adj[i] = target.oom_adj_score; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | lowmem_targets_size = ntargets; |
| 426 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 427 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 428 | char minfreestr[128]; |
| 429 | char killpriostr[128]; |
| 430 | |
| 431 | minfreestr[0] = '\0'; |
| 432 | killpriostr[0] = '\0'; |
| 433 | |
| 434 | for (i = 0; i < lowmem_targets_size; i++) { |
| 435 | char val[40]; |
| 436 | |
| 437 | if (i) { |
| 438 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 439 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 440 | } |
| 441 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 442 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 443 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 444 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 445 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 446 | } |
| 447 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 448 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); |
| 449 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 453 | static void ctrl_data_close(int dsock_idx) { |
| 454 | struct epoll_event epev; |
| 455 | |
| 456 | ALOGI("closing lmkd data connection"); |
| 457 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { |
| 458 | // Log a warning and keep going |
| 459 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 460 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 461 | maxevents--; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 462 | |
| 463 | close(data_sock[dsock_idx].sock); |
| 464 | data_sock[dsock_idx].sock = -1; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 467 | static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 468 | int ret = 0; |
| 469 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 470 | ret = read(data_sock[dsock_idx].sock, buf, bufsz); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 471 | |
| 472 | if (ret == -1) { |
| 473 | ALOGE("control data socket read failed; errno=%d", errno); |
| 474 | } else if (ret == 0) { |
| 475 | ALOGE("Got EOF on control data socket"); |
| 476 | ret = -1; |
| 477 | } |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 482 | static void ctrl_command_handler(int dsock_idx) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 483 | LMKD_CTRL_PACKET packet; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 484 | int len; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 485 | enum lmk_cmd cmd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 486 | int nargs; |
| 487 | int targets; |
| 488 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 489 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 490 | if (len <= 0) |
| 491 | return; |
| 492 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 493 | if (len < (int)sizeof(int)) { |
| 494 | ALOGE("Wrong control socket read length len=%d", len); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | cmd = lmkd_pack_get_cmd(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 499 | nargs = len / sizeof(int) - 1; |
| 500 | if (nargs < 0) |
| 501 | goto wronglen; |
| 502 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 503 | switch(cmd) { |
| 504 | case LMK_TARGET: |
| 505 | targets = nargs / 2; |
| 506 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 507 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 508 | cmd_target(targets, packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 509 | break; |
| 510 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 511 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 512 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 513 | cmd_procprio(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 514 | break; |
| 515 | case LMK_PROCREMOVE: |
| 516 | if (nargs != 1) |
| 517 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 518 | cmd_procremove(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 519 | break; |
| 520 | default: |
| 521 | ALOGE("Received unknown command code %d", cmd); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | return; |
| 526 | |
| 527 | wronglen: |
| 528 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 529 | } |
| 530 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 531 | static void ctrl_data_handler(int data, uint32_t events) { |
| 532 | if (events & EPOLLIN) { |
| 533 | ctrl_command_handler(data); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 537 | static int get_free_dsock() { |
| 538 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 539 | if (data_sock[i].sock < 0) { |
| 540 | return i; |
| 541 | } |
| 542 | } |
| 543 | return -1; |
| 544 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 545 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 546 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused) { |
| 547 | struct epoll_event epev; |
| 548 | int free_dscock_idx = get_free_dsock(); |
| 549 | |
| 550 | if (free_dscock_idx < 0) { |
| 551 | /* |
| 552 | * Number of data connections exceeded max supported. This should not |
| 553 | * happen but if it does we drop all existing connections and accept |
| 554 | * the new one. This prevents inactive connections from monopolizing |
| 555 | * data socket and if we drop ActivityManager connection it will |
| 556 | * immediately reconnect. |
| 557 | */ |
| 558 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 559 | ctrl_data_close(i); |
| 560 | } |
| 561 | free_dscock_idx = 0; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 564 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); |
| 565 | if (data_sock[free_dscock_idx].sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 566 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 567 | return; |
| 568 | } |
| 569 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 570 | ALOGI("lmkd data connection established"); |
| 571 | /* use data to store data connection idx */ |
| 572 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; |
| 573 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 574 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 575 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); |
| 576 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 577 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 578 | ctrl_data_close(free_dscock_idx); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 579 | return; |
| 580 | } |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 581 | maxevents++; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 584 | #ifdef LMKD_LOG_STATS |
| 585 | static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) { |
| 586 | char key[LINE_MAX]; |
| 587 | int64_t value; |
| 588 | |
| 589 | sscanf(line,"%s %" SCNd64 "", key, &value); |
| 590 | |
| 591 | if (strcmp(key, "total_") < 0) { |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | if (!strcmp(key, "total_pgfault")) |
| 596 | mem_st->pgfault = value; |
| 597 | else if (!strcmp(key, "total_pgmajfault")) |
| 598 | mem_st->pgmajfault = value; |
| 599 | else if (!strcmp(key, "total_rss")) |
| 600 | mem_st->rss_in_bytes = value; |
| 601 | else if (!strcmp(key, "total_cache")) |
| 602 | mem_st->cache_in_bytes = value; |
| 603 | else if (!strcmp(key, "total_swap")) |
| 604 | mem_st->swap_in_bytes = value; |
| 605 | } |
| 606 | |
| 607 | static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) { |
| 608 | FILE *fp; |
| 609 | char buf[PATH_MAX]; |
| 610 | |
| 611 | snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid); |
| 612 | |
| 613 | fp = fopen(buf, "r"); |
| 614 | |
| 615 | if (fp == NULL) { |
Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 616 | ALOGE("%s open failed: %s", buf, strerror(errno)); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | while (fgets(buf, PAGE_SIZE, fp) != NULL ) { |
| 621 | memory_stat_parse_line(buf, mem_st); |
| 622 | } |
| 623 | fclose(fp); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | #endif |
| 628 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 629 | static int get_free_memory(struct mem_size *ms) { |
| 630 | struct sysinfo si; |
| 631 | |
| 632 | if (sysinfo(&si) < 0) |
| 633 | return -1; |
| 634 | |
| 635 | ms->free_mem = (int)(si.freeram * si.mem_unit / PAGE_SIZE); |
| 636 | ms->free_swap = (int)(si.freeswap * si.mem_unit / PAGE_SIZE); |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 641 | static int proc_get_size(int pid) { |
| 642 | char path[PATH_MAX]; |
| 643 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 644 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 645 | int rss = 0; |
| 646 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 647 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 648 | |
| 649 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 650 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 651 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 652 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 653 | |
| 654 | ret = read_all(fd, line, sizeof(line) - 1); |
| 655 | if (ret < 0) { |
| 656 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 661 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 662 | return rss; |
| 663 | } |
| 664 | |
| 665 | static char *proc_get_name(int pid) { |
| 666 | char path[PATH_MAX]; |
| 667 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 668 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 669 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 670 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 671 | |
| 672 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 673 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 674 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 675 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 676 | ret = read_all(fd, line, sizeof(line) - 1); |
| 677 | close(fd); |
| 678 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | cp = strchr(line, ' '); |
| 683 | if (cp) |
| 684 | *cp = '\0'; |
| 685 | |
| 686 | return line; |
| 687 | } |
| 688 | |
| 689 | static struct proc *proc_adj_lru(int oomadj) { |
| 690 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 691 | } |
| 692 | |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 693 | static struct proc *proc_get_heaviest(int oomadj) { |
| 694 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 695 | struct adjslot_list *curr = head->next; |
| 696 | struct proc *maxprocp = NULL; |
| 697 | int maxsize = 0; |
| 698 | while (curr != head) { |
| 699 | int pid = ((struct proc *)curr)->pid; |
| 700 | int tasksize = proc_get_size(pid); |
| 701 | if (tasksize <= 0) { |
| 702 | struct adjslot_list *next = curr->next; |
| 703 | pid_remove(pid); |
| 704 | curr = next; |
| 705 | } else { |
| 706 | if (tasksize > maxsize) { |
| 707 | maxsize = tasksize; |
| 708 | maxprocp = (struct proc *)curr; |
| 709 | } |
| 710 | curr = curr->next; |
| 711 | } |
| 712 | } |
| 713 | return maxprocp; |
| 714 | } |
| 715 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 716 | /* Kill one process specified by procp. Returns the size of the process killed */ |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 717 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 718 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 719 | int pid = procp->pid; |
| 720 | uid_t uid = procp->uid; |
| 721 | char *taskname; |
| 722 | int tasksize; |
| 723 | int r; |
| 724 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 725 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 726 | struct memory_stat mem_st = {}; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 727 | int memory_stat_parse_result = -1; |
| 728 | #endif |
| 729 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 730 | taskname = proc_get_name(pid); |
| 731 | if (!taskname) { |
| 732 | pid_remove(pid); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | tasksize = proc_get_size(pid); |
| 737 | if (tasksize <= 0) { |
| 738 | pid_remove(pid); |
| 739 | return -1; |
| 740 | } |
| 741 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 742 | #ifdef LMKD_LOG_STATS |
| 743 | if (enable_stats_log) { |
| 744 | memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid); |
| 745 | } |
| 746 | #endif |
| 747 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 748 | TRACE_KILL_START(pid); |
| 749 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 750 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 751 | ALOGI( |
| 752 | "Killing '%s' (%d), uid %d, adj %d\n" |
Suren Baghdasaryan | d07a94f | 2018-03-20 16:25:54 -0700 | [diff] [blame] | 753 | " to free %ldkB because system is under %s memory pressure (min_oom_adj=%d)\n", |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 754 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 755 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 756 | pid_remove(pid); |
| 757 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 758 | TRACE_KILL_END(); |
| 759 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 760 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 761 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 762 | return -1; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 763 | } else { |
| 764 | #ifdef LMKD_LOG_STATS |
| 765 | if (memory_stat_parse_result == 0) { |
| 766 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, |
| 767 | procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes, |
| 768 | mem_st.cache_in_bytes, mem_st.swap_in_bytes); |
| 769 | } |
| 770 | #endif |
| 771 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 772 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 773 | |
| 774 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 778 | * Find processes to kill to free required number of pages. |
| 779 | * If pages_to_free is set to 0 only one process will be killed. |
| 780 | * Returns the size of the killed processes. |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 781 | */ |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 782 | static int find_and_kill_processes(enum vmpressure_level level, |
| 783 | int pages_to_free) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 784 | int i; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 785 | int killed_size; |
| 786 | int pages_freed = 0; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 787 | int min_score_adj = level_oomadj[level]; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 788 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 789 | #ifdef LMKD_LOG_STATS |
| 790 | if (enable_stats_log) { |
| 791 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_START); |
| 792 | } |
| 793 | #endif |
| 794 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 795 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 796 | struct proc *procp; |
| 797 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 798 | while (true) { |
| 799 | if (is_go_device) |
| 800 | procp = proc_adj_lru(i); |
| 801 | else |
| 802 | procp = proc_get_heaviest(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 803 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 804 | if (!procp) |
| 805 | break; |
| 806 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 807 | killed_size = kill_one_process(procp, min_score_adj, level); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 808 | if (killed_size >= 0) { |
| 809 | pages_freed += killed_size; |
| 810 | if (pages_freed >= pages_to_free) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 811 | |
| 812 | #ifdef LMKD_LOG_STATS |
| 813 | if (enable_stats_log) { |
| 814 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, |
| 815 | LMK_STATE_CHANGE_STOP); |
| 816 | } |
| 817 | #endif |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 818 | return pages_freed; |
| 819 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 824 | #ifdef LMKD_LOG_STATS |
| 825 | if (enable_stats_log) { |
| 826 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP); |
| 827 | } |
| 828 | #endif |
| 829 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 830 | return pages_freed; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 833 | static int64_t get_memory_usage(const char* path) { |
| 834 | int ret; |
| 835 | int64_t mem_usage; |
| 836 | char buf[32]; |
| 837 | int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 838 | if (fd == -1) { |
| 839 | ALOGE("%s open: errno=%d", path, errno); |
| 840 | return -1; |
| 841 | } |
| 842 | |
| 843 | ret = read_all(fd, buf, sizeof(buf) - 1); |
| 844 | close(fd); |
| 845 | if (ret < 0) { |
| 846 | ALOGE("%s error: errno=%d", path, errno); |
| 847 | return -1; |
| 848 | } |
| 849 | sscanf(buf, "%" SCNd64, &mem_usage); |
| 850 | if (mem_usage == 0) { |
| 851 | ALOGE("No memory!"); |
| 852 | return -1; |
| 853 | } |
| 854 | return mem_usage; |
| 855 | } |
| 856 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 857 | void record_low_pressure_levels(struct mem_size *free_mem) { |
| 858 | if (low_pressure_mem.min_free == -1 || |
| 859 | low_pressure_mem.min_free > free_mem->free_mem) { |
| 860 | if (debug_process_killing) { |
| 861 | ALOGI("Low pressure min memory update from %d to %d", |
| 862 | low_pressure_mem.min_free, free_mem->free_mem); |
| 863 | } |
| 864 | low_pressure_mem.min_free = free_mem->free_mem; |
| 865 | } |
| 866 | /* |
| 867 | * Free memory at low vmpressure events occasionally gets spikes, |
| 868 | * possibly a stale low vmpressure event with memory already |
| 869 | * freed up (no memory pressure should have been reported). |
| 870 | * Ignore large jumps in max_free that would mess up our stats. |
| 871 | */ |
| 872 | if (low_pressure_mem.max_free == -1 || |
| 873 | (low_pressure_mem.max_free < free_mem->free_mem && |
| 874 | free_mem->free_mem - low_pressure_mem.max_free < low_pressure_mem.max_free * 0.1)) { |
| 875 | if (debug_process_killing) { |
| 876 | ALOGI("Low pressure max memory update from %d to %d", |
| 877 | low_pressure_mem.max_free, free_mem->free_mem); |
| 878 | } |
| 879 | low_pressure_mem.max_free = free_mem->free_mem; |
| 880 | } |
| 881 | } |
| 882 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 883 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 884 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 885 | level + 1 : level); |
| 886 | } |
| 887 | |
| 888 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 889 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 890 | level - 1 : level); |
| 891 | } |
| 892 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 893 | static inline unsigned long get_time_diff_ms(struct timeval *from, |
| 894 | struct timeval *to) { |
| 895 | return (to->tv_sec - from->tv_sec) * 1000 + |
| 896 | (to->tv_usec - from->tv_usec) / 1000; |
| 897 | } |
| 898 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 899 | static void mp_event_common(int data, uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 900 | int ret; |
| 901 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 902 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 903 | int64_t mem_pressure; |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 904 | enum vmpressure_level lvl; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 905 | struct mem_size free_mem; |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 906 | static struct timeval last_report_tm; |
| 907 | static unsigned long skip_count = 0; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 908 | enum vmpressure_level level = (enum vmpressure_level)data; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 909 | |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 910 | /* |
| 911 | * Check all event counters from low to critical |
| 912 | * and upgrade to the highest priority one. By reading |
| 913 | * eventfd we also reset the event counters. |
| 914 | */ |
| 915 | for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { |
| 916 | if (mpevfd[lvl] != -1 && |
| 917 | read(mpevfd[lvl], &evcount, sizeof(evcount)) > 0 && |
| 918 | evcount > 0 && lvl > level) { |
| 919 | level = lvl; |
| 920 | } |
| 921 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 922 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 923 | if (kill_timeout_ms) { |
| 924 | struct timeval curr_tm; |
| 925 | gettimeofday(&curr_tm, NULL); |
| 926 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) { |
| 927 | skip_count++; |
| 928 | return; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | if (skip_count > 0) { |
| 933 | if (debug_process_killing) { |
| 934 | ALOGI("%lu memory pressure events were skipped after a kill!", |
| 935 | skip_count); |
| 936 | } |
| 937 | skip_count = 0; |
| 938 | } |
| 939 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 940 | if (get_free_memory(&free_mem) == 0) { |
| 941 | if (level == VMPRESS_LEVEL_LOW) { |
| 942 | record_low_pressure_levels(&free_mem); |
| 943 | } |
| 944 | } else { |
| 945 | ALOGE("Failed to get free memory!"); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 950 | /* Do not monitor this pressure level */ |
| 951 | return; |
| 952 | } |
| 953 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 954 | mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE); |
| 955 | memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE); |
| 956 | if (memsw_usage < 0 || mem_usage < 0) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 957 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 958 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 959 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 960 | // Calculate percent for swappinness. |
| 961 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 962 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 963 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 964 | // We are swapping too much. |
| 965 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 966 | level = upgrade_level(level); |
| 967 | if (debug_process_killing) { |
| 968 | ALOGI("Event upgraded to %s", level_name[level]); |
| 969 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 973 | // If the pressure is larger than downgrade_pressure lmk will not |
| 974 | // kill any process, since enough memory is available. |
| 975 | if (mem_pressure > downgrade_pressure) { |
| 976 | if (debug_process_killing) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 977 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 978 | } |
| 979 | return; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 980 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 981 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 982 | if (debug_process_killing) { |
| 983 | ALOGI("Downgrade critical memory pressure"); |
| 984 | } |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 985 | // Downgrade event, since enough memory available. |
| 986 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 989 | do_kill: |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 990 | if (is_go_device) { |
| 991 | /* For Go devices kill only one task */ |
| 992 | if (find_and_kill_processes(level, 0) == 0) { |
| 993 | if (debug_process_killing) { |
| 994 | ALOGI("Nothing to kill"); |
| 995 | } |
| 996 | } |
| 997 | } else { |
| 998 | /* If pressure level is less than critical and enough free swap then ignore */ |
| 999 | if (level < VMPRESS_LEVEL_CRITICAL && free_mem.free_swap > low_pressure_mem.max_free) { |
| 1000 | if (debug_process_killing) { |
| 1001 | ALOGI("Ignoring pressure since %d swap pages are available ", free_mem.free_swap); |
| 1002 | } |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | /* Free up enough memory to downgrate the memory pressure to low level */ |
| 1007 | if (free_mem.free_mem < low_pressure_mem.max_free) { |
| 1008 | int pages_to_free = low_pressure_mem.max_free - free_mem.free_mem; |
| 1009 | if (debug_process_killing) { |
| 1010 | ALOGI("Trying to free %d pages", pages_to_free); |
| 1011 | } |
| 1012 | int pages_freed = find_and_kill_processes(level, pages_to_free); |
| 1013 | if (pages_freed < pages_to_free) { |
| 1014 | if (debug_process_killing) { |
| 1015 | ALOGI("Unable to free enough memory (pages freed=%d)", |
| 1016 | pages_freed); |
| 1017 | } |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1018 | } else { |
| 1019 | gettimeofday(&last_report_tm, NULL); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1020 | } |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1021 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 1022 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1025 | static bool init_mp_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1026 | int mpfd; |
| 1027 | int evfd; |
| 1028 | int evctlfd; |
| 1029 | char buf[256]; |
| 1030 | struct epoll_event epev; |
| 1031 | int ret; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1032 | int level_idx = (int)level; |
| 1033 | const char *levelstr = level_name[level_idx]; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1034 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1035 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1036 | if (mpfd < 0) { |
| 1037 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 1038 | goto err_open_mpfd; |
| 1039 | } |
| 1040 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1041 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1042 | if (evctlfd < 0) { |
| 1043 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 1044 | goto err_open_evctlfd; |
| 1045 | } |
| 1046 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1047 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1048 | if (evfd < 0) { |
| 1049 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 1050 | goto err_eventfd; |
| 1051 | } |
| 1052 | |
| 1053 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 1054 | if (ret >= (ssize_t)sizeof(buf)) { |
| 1055 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 1056 | goto err; |
| 1057 | } |
| 1058 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1059 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1060 | if (ret == -1) { |
| 1061 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 1062 | levelstr, errno); |
| 1063 | goto err; |
| 1064 | } |
| 1065 | |
| 1066 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1067 | /* use data to store event level */ |
| 1068 | vmpressure_hinfo[level_idx].data = level_idx; |
| 1069 | vmpressure_hinfo[level_idx].handler = mp_event_common; |
| 1070 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1071 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 1072 | if (ret == -1) { |
| 1073 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 1074 | goto err; |
| 1075 | } |
| 1076 | maxevents++; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1077 | mpevfd[level] = evfd; |
Suren Baghdasaryan | 1bd2fc4 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 1078 | close(evctlfd); |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1079 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1080 | |
| 1081 | err: |
| 1082 | close(evfd); |
| 1083 | err_eventfd: |
| 1084 | close(evctlfd); |
| 1085 | err_open_evctlfd: |
| 1086 | close(mpfd); |
| 1087 | err_open_mpfd: |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1088 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1091 | static int init(void) { |
| 1092 | struct epoll_event epev; |
| 1093 | int i; |
| 1094 | int ret; |
| 1095 | |
| 1096 | page_k = sysconf(_SC_PAGESIZE); |
| 1097 | if (page_k == -1) |
| 1098 | page_k = PAGE_SIZE; |
| 1099 | page_k /= 1024; |
| 1100 | |
| 1101 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 1102 | if (epollfd == -1) { |
| 1103 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 1104 | return -1; |
| 1105 | } |
| 1106 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1107 | // mark data connections as not connected |
| 1108 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1109 | data_sock[i].sock = -1; |
| 1110 | } |
| 1111 | |
| 1112 | ctrl_sock.sock = android_get_control_socket("lmkd"); |
| 1113 | if (ctrl_sock.sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1114 | ALOGE("get lmkd control socket failed"); |
| 1115 | return -1; |
| 1116 | } |
| 1117 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1118 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1119 | if (ret < 0) { |
| 1120 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
| 1124 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1125 | ctrl_sock.handler_info.handler = ctrl_connect_handler; |
| 1126 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); |
| 1127 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1128 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 1129 | return -1; |
| 1130 | } |
| 1131 | maxevents++; |
| 1132 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1133 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
Suren Baghdasaryan | 979591b | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 1134 | use_inkernel_interface = has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1135 | |
| 1136 | if (use_inkernel_interface) { |
| 1137 | ALOGI("Using in-kernel low memory killer interface"); |
| 1138 | } else { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1139 | if (!init_mp_common(VMPRESS_LEVEL_LOW) || |
| 1140 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || |
| 1141 | !init_mp_common(VMPRESS_LEVEL_CRITICAL)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1142 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1143 | return -1; |
| 1144 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1147 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1148 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 1149 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 1150 | } |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static void mainloop(void) { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1156 | struct event_handler_info* handler_info; |
| 1157 | struct epoll_event *evt; |
| 1158 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1159 | while (1) { |
| 1160 | struct epoll_event events[maxevents]; |
| 1161 | int nevents; |
| 1162 | int i; |
| 1163 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1164 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 1165 | |
| 1166 | if (nevents == -1) { |
| 1167 | if (errno == EINTR) |
| 1168 | continue; |
| 1169 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 1170 | continue; |
| 1171 | } |
| 1172 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1173 | /* |
| 1174 | * First pass to see if any data socket connections were dropped. |
| 1175 | * Dropped connection should be handled before any other events |
| 1176 | * to deallocate data connection and correctly handle cases when |
| 1177 | * connection gets dropped and reestablished in the same epoll cycle. |
| 1178 | * In such cases it's essential to handle connection closures first. |
| 1179 | */ |
| 1180 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1181 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { |
| 1182 | ALOGI("lmkd data connection dropped"); |
| 1183 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1184 | ctrl_data_close(handler_info->data); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* Second pass to handle all other events */ |
| 1189 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1190 | if (evt->events & EPOLLERR) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1191 | ALOGD("EPOLLERR on event #%d", i); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1192 | if (evt->events & EPOLLHUP) { |
| 1193 | /* This case was handled in the first pass */ |
| 1194 | continue; |
| 1195 | } |
| 1196 | if (evt->data.ptr) { |
| 1197 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1198 | handler_info->handler(handler_info->data, evt->events); |
| 1199 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 1204 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1205 | struct sched_param param = { |
| 1206 | .sched_priority = 1, |
| 1207 | }; |
| 1208 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1209 | /* By default disable low level vmpressure events */ |
| 1210 | level_oomadj[VMPRESS_LEVEL_LOW] = |
| 1211 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); |
| 1212 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
| 1213 | property_get_int32("ro.lmk.medium", 800); |
| 1214 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
| 1215 | property_get_int32("ro.lmk.critical", 0); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1216 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | ad2fd91 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 1217 | |
| 1218 | /* By default disable upgrade/downgrade logic */ |
| 1219 | enable_pressure_upgrade = |
| 1220 | property_get_bool("ro.lmk.critical_upgrade", false); |
| 1221 | upgrade_pressure = |
| 1222 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); |
| 1223 | downgrade_pressure = |
| 1224 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1225 | kill_heaviest_task = |
| 1226 | property_get_bool("ro.lmk.kill_heaviest_task", true); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1227 | is_go_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1228 | kill_timeout_ms = |
| 1229 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 1230 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1231 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | fb25ddd | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1232 | statslog_init(&log_ctx, &enable_stats_log); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1233 | #endif |
| 1234 | |
Daniel Colascione | d39adf2 | 2018-01-05 14:59:55 -0800 | [diff] [blame] | 1235 | // MCL_ONFAULT pins pages as they fault instead of loading |
| 1236 | // everything immediately all at once. (Which would be bad, |
| 1237 | // because as of this writing, we have a lot of mapped pages we |
| 1238 | // never use.) Old kernels will see MCL_ONFAULT and fail with |
| 1239 | // EINVAL; we ignore this failure. |
| 1240 | // |
| 1241 | // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 1242 | // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 1243 | // in pages. |
| 1244 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && errno != EINVAL) |
Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 1245 | ALOGW("mlockall failed: errno=%d", errno); |
| 1246 | |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1247 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1248 | if (!init()) |
| 1249 | mainloop(); |
| 1250 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1251 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | fb25ddd | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1252 | statslog_destroy(&log_ctx); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1253 | #endif |
| 1254 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1255 | ALOGI("exiting"); |
| 1256 | return 0; |
| 1257 | } |