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> |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 32 | #include <sys/sysinfo.h> |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 33 | #include <sys/types.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 |
Yao Chen | 389aee1 | 2018-05-02 11:19:27 -0700 | [diff] [blame] | 42 | #include "statslog.h" |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 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" |
Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 71 | #define ZONEINFO_PATH "/proc/zoneinfo" |
| 72 | #define MEMINFO_PATH "/proc/meminfo" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 73 | #define LINE_MAX 128 |
| 74 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 75 | /* gid containing AID_SYSTEM required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 76 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 77 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 78 | |
| 79 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 80 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 81 | |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 82 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ |
| 83 | #define SYSTEM_ADJ (-900) |
| 84 | |
Greg Kaiser | f0da9b0 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 85 | #define STRINGIFY(x) STRINGIFY_INTERNAL(x) |
| 86 | #define STRINGIFY_INTERNAL(x) #x |
| 87 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 88 | /* default to old in-kernel interface if no memory pressure events */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 89 | static bool use_inkernel_interface = true; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 90 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 91 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 92 | /* memory pressure levels */ |
| 93 | enum vmpressure_level { |
| 94 | VMPRESS_LEVEL_LOW = 0, |
| 95 | VMPRESS_LEVEL_MEDIUM, |
| 96 | VMPRESS_LEVEL_CRITICAL, |
| 97 | VMPRESS_LEVEL_COUNT |
| 98 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 99 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 100 | static const char *level_name[] = { |
| 101 | "low", |
| 102 | "medium", |
| 103 | "critical" |
| 104 | }; |
| 105 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 106 | struct { |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 107 | int64_t min_nr_free_pages; /* recorded but not used yet */ |
| 108 | int64_t max_nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 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; |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 117 | static bool low_ram_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; |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 120 | static bool use_minfree_levels; |
Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 121 | static bool per_app_memcg; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 122 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 123 | /* data required to handle events */ |
| 124 | struct event_handler_info { |
| 125 | int data; |
| 126 | void (*handler)(int data, uint32_t events); |
| 127 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 128 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 129 | /* data required to handle socket events */ |
| 130 | struct sock_event_handler_info { |
| 131 | int sock; |
| 132 | struct event_handler_info handler_info; |
| 133 | }; |
| 134 | |
| 135 | /* max supported number of data connections */ |
| 136 | #define MAX_DATA_CONN 2 |
| 137 | |
| 138 | /* socket event handler data */ |
| 139 | static struct sock_event_handler_info ctrl_sock; |
| 140 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; |
| 141 | |
| 142 | /* vmpressure event handler data */ |
| 143 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; |
| 144 | |
| 145 | /* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */ |
| 146 | #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 147 | static int epollfd; |
| 148 | static int maxevents; |
| 149 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 150 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 151 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 152 | #define OOM_SCORE_ADJ_MAX 1000 |
| 153 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 154 | static int lowmem_adj[MAX_TARGETS]; |
| 155 | static int lowmem_minfree[MAX_TARGETS]; |
| 156 | static int lowmem_targets_size; |
| 157 | |
Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 158 | /* Fields to parse in /proc/zoneinfo */ |
| 159 | enum zoneinfo_field { |
| 160 | ZI_NR_FREE_PAGES = 0, |
| 161 | ZI_NR_FILE_PAGES, |
| 162 | ZI_NR_SHMEM, |
| 163 | ZI_NR_UNEVICTABLE, |
| 164 | ZI_WORKINGSET_REFAULT, |
| 165 | ZI_HIGH, |
| 166 | ZI_FIELD_COUNT |
| 167 | }; |
| 168 | |
| 169 | static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = { |
| 170 | "nr_free_pages", |
| 171 | "nr_file_pages", |
| 172 | "nr_shmem", |
| 173 | "nr_unevictable", |
| 174 | "workingset_refault", |
| 175 | "high", |
| 176 | }; |
| 177 | |
| 178 | union zoneinfo { |
| 179 | struct { |
| 180 | int64_t nr_free_pages; |
| 181 | int64_t nr_file_pages; |
| 182 | int64_t nr_shmem; |
| 183 | int64_t nr_unevictable; |
| 184 | int64_t workingset_refault; |
| 185 | int64_t high; |
| 186 | /* fields below are calculated rather than read from the file */ |
| 187 | int64_t totalreserve_pages; |
| 188 | } field; |
| 189 | int64_t arr[ZI_FIELD_COUNT]; |
| 190 | }; |
| 191 | |
| 192 | /* Fields to parse in /proc/meminfo */ |
| 193 | enum meminfo_field { |
| 194 | MI_NR_FREE_PAGES = 0, |
| 195 | MI_CACHED, |
| 196 | MI_SWAP_CACHED, |
| 197 | MI_BUFFERS, |
| 198 | MI_SHMEM, |
| 199 | MI_UNEVICTABLE, |
| 200 | MI_FREE_SWAP, |
| 201 | MI_DIRTY, |
| 202 | MI_FIELD_COUNT |
| 203 | }; |
| 204 | |
| 205 | static const char* const meminfo_field_names[MI_FIELD_COUNT] = { |
| 206 | "MemFree:", |
| 207 | "Cached:", |
| 208 | "SwapCached:", |
| 209 | "Buffers:", |
| 210 | "Shmem:", |
| 211 | "Unevictable:", |
| 212 | "SwapFree:", |
| 213 | "Dirty:", |
| 214 | }; |
| 215 | |
| 216 | union meminfo { |
| 217 | struct { |
| 218 | int64_t nr_free_pages; |
| 219 | int64_t cached; |
| 220 | int64_t swap_cached; |
| 221 | int64_t buffers; |
| 222 | int64_t shmem; |
| 223 | int64_t unevictable; |
| 224 | int64_t free_swap; |
| 225 | int64_t dirty; |
| 226 | /* fields below are calculated rather than read from the file */ |
| 227 | int64_t nr_file_pages; |
| 228 | } field; |
| 229 | int64_t arr[MI_FIELD_COUNT]; |
| 230 | }; |
| 231 | |
| 232 | enum field_match_result { |
| 233 | NO_MATCH, |
| 234 | PARSE_FAIL, |
| 235 | PARSE_SUCCESS |
| 236 | }; |
| 237 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 238 | struct adjslot_list { |
| 239 | struct adjslot_list *next; |
| 240 | struct adjslot_list *prev; |
| 241 | }; |
| 242 | |
| 243 | struct proc { |
| 244 | struct adjslot_list asl; |
| 245 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 246 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 247 | int oomadj; |
| 248 | struct proc *pidhash_next; |
| 249 | }; |
| 250 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 251 | struct reread_data { |
| 252 | const char* const filename; |
| 253 | int fd; |
| 254 | }; |
| 255 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 256 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 257 | static bool enable_stats_log; |
| 258 | static android_log_context log_ctx; |
| 259 | #endif |
| 260 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 261 | #define PIDHASH_SZ 1024 |
| 262 | static struct proc *pidhash[PIDHASH_SZ]; |
| 263 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 264 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 265 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 266 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 267 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 268 | /* PAGE_SIZE / 1024 */ |
| 269 | static long page_k; |
| 270 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 271 | static bool parse_int64(const char* str, int64_t* ret) { |
| 272 | char* endptr; |
| 273 | long long val = strtoll(str, &endptr, 10); |
| 274 | if (str == endptr || val > INT64_MAX) { |
| 275 | return false; |
| 276 | } |
| 277 | *ret = (int64_t)val; |
| 278 | return true; |
| 279 | } |
| 280 | |
Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 281 | static enum field_match_result match_field(const char* cp, const char* ap, |
| 282 | const char* const field_names[], |
| 283 | int field_count, int64_t* field, |
| 284 | int *field_idx) { |
| 285 | int64_t val; |
| 286 | int i; |
| 287 | |
| 288 | for (i = 0; i < field_count; i++) { |
| 289 | if (!strcmp(cp, field_names[i])) { |
| 290 | *field_idx = i; |
| 291 | return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL; |
| 292 | } |
| 293 | } |
| 294 | return NO_MATCH; |
| 295 | } |
| 296 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 297 | /* |
| 298 | * Read file content from the beginning up to max_len bytes or EOF |
| 299 | * whichever happens first. |
| 300 | */ |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 301 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 302 | { |
| 303 | ssize_t ret = 0; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 304 | off_t offset = 0; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 305 | |
| 306 | while (max_len > 0) { |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 307 | ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 308 | if (r == 0) { |
| 309 | break; |
| 310 | } |
| 311 | if (r == -1) { |
| 312 | return -1; |
| 313 | } |
| 314 | ret += r; |
| 315 | buf += r; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 316 | offset += r; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 317 | max_len -= r; |
| 318 | } |
| 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Read a new or already opened file from the beginning. |
| 325 | * If the file has not been opened yet data->fd should be set to -1. |
| 326 | * To be used with files which are read often and possibly during high |
| 327 | * memory pressure to minimize file opening which by itself requires kernel |
| 328 | * memory allocation and might result in a stall on memory stressed system. |
| 329 | */ |
| 330 | static int reread_file(struct reread_data *data, char *buf, size_t buf_size) { |
| 331 | ssize_t size; |
| 332 | |
| 333 | if (data->fd == -1) { |
| 334 | data->fd = open(data->filename, O_RDONLY | O_CLOEXEC); |
| 335 | if (data->fd == -1) { |
| 336 | ALOGE("%s open: %s", data->filename, strerror(errno)); |
| 337 | return -1; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | size = read_all(data->fd, buf, buf_size - 1); |
| 342 | if (size < 0) { |
| 343 | ALOGE("%s read: %s", data->filename, strerror(errno)); |
| 344 | close(data->fd); |
| 345 | data->fd = -1; |
| 346 | return -1; |
| 347 | } |
Chih-Hung Hsieh | 62b0ef5 | 2018-07-25 12:15:25 -0700 | [diff] [blame] | 348 | ALOG_ASSERT((size_t)size < buf_size - 1, "%s too large", data->filename); |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 349 | buf[size] = 0; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 354 | static struct proc *pid_lookup(int pid) { |
| 355 | struct proc *procp; |
| 356 | |
| 357 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 358 | procp = procp->pidhash_next) |
| 359 | ; |
| 360 | |
| 361 | return procp; |
| 362 | } |
| 363 | |
| 364 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 365 | { |
| 366 | struct adjslot_list *next = head->next; |
| 367 | new->prev = head; |
| 368 | new->next = next; |
| 369 | next->prev = new; |
| 370 | head->next = new; |
| 371 | } |
| 372 | |
| 373 | static void adjslot_remove(struct adjslot_list *old) |
| 374 | { |
| 375 | struct adjslot_list *prev = old->prev; |
| 376 | struct adjslot_list *next = old->next; |
| 377 | next->prev = prev; |
| 378 | prev->next = next; |
| 379 | } |
| 380 | |
| 381 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 382 | struct adjslot_list *asl = head->prev; |
| 383 | |
| 384 | return asl == head ? NULL : asl; |
| 385 | } |
| 386 | |
| 387 | static void proc_slot(struct proc *procp) { |
| 388 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 389 | |
| 390 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 391 | } |
| 392 | |
| 393 | static void proc_unslot(struct proc *procp) { |
| 394 | adjslot_remove(&procp->asl); |
| 395 | } |
| 396 | |
| 397 | static void proc_insert(struct proc *procp) { |
| 398 | int hval = pid_hashfn(procp->pid); |
| 399 | |
| 400 | procp->pidhash_next = pidhash[hval]; |
| 401 | pidhash[hval] = procp; |
| 402 | proc_slot(procp); |
| 403 | } |
| 404 | |
| 405 | static int pid_remove(int pid) { |
| 406 | int hval = pid_hashfn(pid); |
| 407 | struct proc *procp; |
| 408 | struct proc *prevp; |
| 409 | |
| 410 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 411 | procp = procp->pidhash_next) |
| 412 | prevp = procp; |
| 413 | |
| 414 | if (!procp) |
| 415 | return -1; |
| 416 | |
| 417 | if (!prevp) |
| 418 | pidhash[hval] = procp->pidhash_next; |
| 419 | else |
| 420 | prevp->pidhash_next = procp->pidhash_next; |
| 421 | |
| 422 | proc_unslot(procp); |
| 423 | free(procp); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 427 | /* |
| 428 | * Write a string to a file. |
| 429 | * Returns false if the file does not exist. |
| 430 | */ |
| 431 | static bool writefilestring(const char *path, const char *s, |
| 432 | bool err_if_missing) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 433 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 434 | ssize_t len = strlen(s); |
| 435 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 436 | |
| 437 | if (fd < 0) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 438 | if (err_if_missing) { |
| 439 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 440 | } |
| 441 | return false; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 444 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 445 | if (ret < 0) { |
| 446 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 447 | } else if (ret < len) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 448 | ALOGE("Short write on %s; length=%zd", path, ret); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | close(fd); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 452 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 455 | static void cmd_procprio(LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 456 | struct proc *procp; |
| 457 | char path[80]; |
| 458 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 459 | int soft_limit_mult; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 460 | struct lmk_procprio params; |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 461 | bool is_system_server; |
| 462 | struct passwd *pwdrec; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 463 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 464 | lmkd_pack_get_procprio(packet, ¶ms); |
| 465 | |
| 466 | if (params.oomadj < OOM_SCORE_ADJ_MIN || |
| 467 | params.oomadj > OOM_SCORE_ADJ_MAX) { |
| 468 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 472 | /* gid containing AID_READPROC required */ |
| 473 | /* CAP_SYS_RESOURCE required */ |
| 474 | /* CAP_DAC_OVERRIDE required */ |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 475 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); |
| 476 | snprintf(val, sizeof(val), "%d", params.oomadj); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 477 | if (!writefilestring(path, val, false)) { |
| 478 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", |
| 479 | path, errno, params.pid); |
| 480 | /* If this file does not exist the process is dead. */ |
| 481 | return; |
| 482 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 483 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 484 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 485 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 486 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 487 | |
Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 488 | if (per_app_memcg) { |
Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 489 | if (params.oomadj >= 900) { |
| 490 | soft_limit_mult = 0; |
| 491 | } else if (params.oomadj >= 800) { |
| 492 | soft_limit_mult = 0; |
| 493 | } else if (params.oomadj >= 700) { |
| 494 | soft_limit_mult = 0; |
| 495 | } else if (params.oomadj >= 600) { |
| 496 | // Launcher should be perceptible, don't kill it. |
| 497 | params.oomadj = 200; |
| 498 | soft_limit_mult = 1; |
| 499 | } else if (params.oomadj >= 500) { |
| 500 | soft_limit_mult = 0; |
| 501 | } else if (params.oomadj >= 400) { |
| 502 | soft_limit_mult = 0; |
| 503 | } else if (params.oomadj >= 300) { |
| 504 | soft_limit_mult = 1; |
| 505 | } else if (params.oomadj >= 200) { |
| 506 | soft_limit_mult = 2; |
| 507 | } else if (params.oomadj >= 100) { |
| 508 | soft_limit_mult = 10; |
| 509 | } else if (params.oomadj >= 0) { |
| 510 | soft_limit_mult = 20; |
| 511 | } else { |
| 512 | // Persistent processes will have a large |
| 513 | // soft limit 512MB. |
| 514 | soft_limit_mult = 64; |
| 515 | } |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 516 | |
Suren Baghdasaryan | 3862dd3 | 2018-05-21 19:48:47 -0700 | [diff] [blame] | 517 | snprintf(path, sizeof(path), MEMCG_SYSFS_PATH |
| 518 | "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", |
| 519 | params.uid, params.pid); |
Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 520 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
Suren Baghdasaryan | 3862dd3 | 2018-05-21 19:48:47 -0700 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * system_server process has no memcg under /dev/memcg/apps but should be |
| 524 | * registered with lmkd. This is the best way so far to identify it. |
| 525 | */ |
| 526 | is_system_server = (params.oomadj == SYSTEM_ADJ && |
| 527 | (pwdrec = getpwnam("system")) != NULL && |
| 528 | params.uid == pwdrec->pw_uid); |
| 529 | writefilestring(path, val, !is_system_server); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 532 | procp = pid_lookup(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 533 | if (!procp) { |
| 534 | procp = malloc(sizeof(struct proc)); |
| 535 | if (!procp) { |
| 536 | // Oh, the irony. May need to rebuild our state. |
| 537 | return; |
| 538 | } |
| 539 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 540 | procp->pid = params.pid; |
| 541 | procp->uid = params.uid; |
| 542 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 543 | proc_insert(procp); |
| 544 | } else { |
| 545 | proc_unslot(procp); |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 546 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 547 | proc_slot(procp); |
| 548 | } |
| 549 | } |
| 550 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 551 | static void cmd_procremove(LMKD_CTRL_PACKET packet) { |
| 552 | struct lmk_procremove params; |
| 553 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 554 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 555 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 556 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 557 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 558 | lmkd_pack_get_procremove(packet, ¶ms); |
| 559 | pid_remove(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 562 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 563 | int i; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 564 | struct lmk_target target; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 565 | |
| 566 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 567 | return; |
| 568 | |
| 569 | for (i = 0; i < ntargets; i++) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 570 | lmkd_pack_get_target(packet, i, &target); |
| 571 | lowmem_minfree[i] = target.minfree; |
| 572 | lowmem_adj[i] = target.oom_adj_score; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | lowmem_targets_size = ntargets; |
| 576 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 577 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 578 | char minfreestr[128]; |
| 579 | char killpriostr[128]; |
| 580 | |
| 581 | minfreestr[0] = '\0'; |
| 582 | killpriostr[0] = '\0'; |
| 583 | |
| 584 | for (i = 0; i < lowmem_targets_size; i++) { |
| 585 | char val[40]; |
| 586 | |
| 587 | if (i) { |
| 588 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 589 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 590 | } |
| 591 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 592 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 593 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 594 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 595 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 596 | } |
| 597 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 598 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); |
| 599 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 603 | static void ctrl_data_close(int dsock_idx) { |
| 604 | struct epoll_event epev; |
| 605 | |
| 606 | ALOGI("closing lmkd data connection"); |
| 607 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { |
| 608 | // Log a warning and keep going |
| 609 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 610 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 611 | maxevents--; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 612 | |
| 613 | close(data_sock[dsock_idx].sock); |
| 614 | data_sock[dsock_idx].sock = -1; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 617 | 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] | 618 | int ret = 0; |
| 619 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 620 | ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 621 | |
| 622 | if (ret == -1) { |
| 623 | ALOGE("control data socket read failed; errno=%d", errno); |
| 624 | } else if (ret == 0) { |
| 625 | ALOGE("Got EOF on control data socket"); |
| 626 | ret = -1; |
| 627 | } |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 632 | static void ctrl_command_handler(int dsock_idx) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 633 | LMKD_CTRL_PACKET packet; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 634 | int len; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 635 | enum lmk_cmd cmd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 636 | int nargs; |
| 637 | int targets; |
| 638 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 639 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 640 | if (len <= 0) |
| 641 | return; |
| 642 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 643 | if (len < (int)sizeof(int)) { |
| 644 | ALOGE("Wrong control socket read length len=%d", len); |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | cmd = lmkd_pack_get_cmd(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 649 | nargs = len / sizeof(int) - 1; |
| 650 | if (nargs < 0) |
| 651 | goto wronglen; |
| 652 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 653 | switch(cmd) { |
| 654 | case LMK_TARGET: |
| 655 | targets = nargs / 2; |
| 656 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 657 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 658 | cmd_target(targets, packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 659 | break; |
| 660 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 661 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 662 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 663 | cmd_procprio(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 664 | break; |
| 665 | case LMK_PROCREMOVE: |
| 666 | if (nargs != 1) |
| 667 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 668 | cmd_procremove(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 669 | break; |
| 670 | default: |
| 671 | ALOGE("Received unknown command code %d", cmd); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | return; |
| 676 | |
| 677 | wronglen: |
| 678 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 679 | } |
| 680 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 681 | static void ctrl_data_handler(int data, uint32_t events) { |
| 682 | if (events & EPOLLIN) { |
| 683 | ctrl_command_handler(data); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 687 | static int get_free_dsock() { |
| 688 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 689 | if (data_sock[i].sock < 0) { |
| 690 | return i; |
| 691 | } |
| 692 | } |
| 693 | return -1; |
| 694 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 695 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 696 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused) { |
| 697 | struct epoll_event epev; |
| 698 | int free_dscock_idx = get_free_dsock(); |
| 699 | |
| 700 | if (free_dscock_idx < 0) { |
| 701 | /* |
| 702 | * Number of data connections exceeded max supported. This should not |
| 703 | * happen but if it does we drop all existing connections and accept |
| 704 | * the new one. This prevents inactive connections from monopolizing |
| 705 | * data socket and if we drop ActivityManager connection it will |
| 706 | * immediately reconnect. |
| 707 | */ |
| 708 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 709 | ctrl_data_close(i); |
| 710 | } |
| 711 | free_dscock_idx = 0; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 714 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); |
| 715 | if (data_sock[free_dscock_idx].sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 716 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 717 | return; |
| 718 | } |
| 719 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 720 | ALOGI("lmkd data connection established"); |
| 721 | /* use data to store data connection idx */ |
| 722 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; |
| 723 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 724 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 725 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); |
| 726 | 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] | 727 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 728 | ctrl_data_close(free_dscock_idx); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 729 | return; |
| 730 | } |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 731 | maxevents++; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 734 | #ifdef LMKD_LOG_STATS |
| 735 | static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) { |
Greg Kaiser | f0da9b0 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 736 | char key[LINE_MAX + 1]; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 737 | int64_t value; |
| 738 | |
Greg Kaiser | f0da9b0 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 739 | sscanf(line, "%" STRINGIFY(LINE_MAX) "s %" SCNd64 "", key, &value); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 740 | |
| 741 | if (strcmp(key, "total_") < 0) { |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | if (!strcmp(key, "total_pgfault")) |
| 746 | mem_st->pgfault = value; |
| 747 | else if (!strcmp(key, "total_pgmajfault")) |
| 748 | mem_st->pgmajfault = value; |
| 749 | else if (!strcmp(key, "total_rss")) |
| 750 | mem_st->rss_in_bytes = value; |
| 751 | else if (!strcmp(key, "total_cache")) |
| 752 | mem_st->cache_in_bytes = value; |
| 753 | else if (!strcmp(key, "total_swap")) |
| 754 | mem_st->swap_in_bytes = value; |
| 755 | } |
| 756 | |
| 757 | static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) { |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 758 | FILE *fp; |
| 759 | char buf[PATH_MAX]; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 760 | |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 761 | /* |
| 762 | * Per-application memory.stat files are available only when |
| 763 | * per-application memcgs are enabled. |
| 764 | */ |
| 765 | if (!per_app_memcg) |
| 766 | return -1; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 767 | |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 768 | snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 769 | |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 770 | fp = fopen(buf, "r"); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 771 | |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 772 | if (fp == NULL) { |
| 773 | ALOGE("%s open failed: %s", buf, strerror(errno)); |
| 774 | return -1; |
| 775 | } |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 776 | |
Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame^] | 777 | while (fgets(buf, PAGE_SIZE, fp) != NULL ) { |
| 778 | memory_stat_parse_line(buf, mem_st); |
| 779 | } |
| 780 | fclose(fp); |
| 781 | |
| 782 | return 0; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 783 | } |
| 784 | #endif |
| 785 | |
Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 786 | /* /prop/zoneinfo parsing routines */ |
| 787 | static int64_t zoneinfo_parse_protection(char *cp) { |
| 788 | int64_t max = 0; |
| 789 | long long zoneval; |
| 790 | char *save_ptr; |
| 791 | |
| 792 | for (cp = strtok_r(cp, "(), ", &save_ptr); cp; |
| 793 | cp = strtok_r(NULL, "), ", &save_ptr)) { |
| 794 | zoneval = strtoll(cp, &cp, 0); |
| 795 | if (zoneval > max) { |
| 796 | max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | return max; |
| 801 | } |
| 802 | |
| 803 | static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) { |
| 804 | char *cp = line; |
| 805 | char *ap; |
| 806 | char *save_ptr; |
| 807 | int64_t val; |
| 808 | int field_idx; |
| 809 | |
| 810 | cp = strtok_r(line, " ", &save_ptr); |
| 811 | if (!cp) { |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | if (!strcmp(cp, "protection:")) { |
| 816 | ap = strtok_r(NULL, ")", &save_ptr); |
| 817 | } else { |
| 818 | ap = strtok_r(NULL, " ", &save_ptr); |
| 819 | } |
| 820 | |
| 821 | if (!ap) { |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | switch (match_field(cp, ap, zoneinfo_field_names, |
| 826 | ZI_FIELD_COUNT, &val, &field_idx)) { |
| 827 | case (PARSE_SUCCESS): |
| 828 | zi->arr[field_idx] += val; |
| 829 | break; |
| 830 | case (NO_MATCH): |
| 831 | if (!strcmp(cp, "protection:")) { |
| 832 | zi->field.totalreserve_pages += |
| 833 | zoneinfo_parse_protection(ap); |
| 834 | } |
| 835 | break; |
| 836 | case (PARSE_FAIL): |
| 837 | default: |
| 838 | return false; |
| 839 | } |
| 840 | return true; |
| 841 | } |
| 842 | |
| 843 | static int zoneinfo_parse(union zoneinfo *zi) { |
| 844 | static struct reread_data file_data = { |
| 845 | .filename = ZONEINFO_PATH, |
| 846 | .fd = -1, |
| 847 | }; |
| 848 | char buf[PAGE_SIZE]; |
| 849 | char *save_ptr; |
| 850 | char *line; |
| 851 | |
| 852 | memset(zi, 0, sizeof(union zoneinfo)); |
| 853 | |
| 854 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 859 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 860 | if (!zoneinfo_parse_line(line, zi)) { |
| 861 | ALOGE("%s parse error", file_data.filename); |
| 862 | return -1; |
| 863 | } |
| 864 | } |
| 865 | zi->field.totalreserve_pages += zi->field.high; |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* /prop/meminfo parsing routines */ |
| 871 | static bool meminfo_parse_line(char *line, union meminfo *mi) { |
| 872 | char *cp = line; |
| 873 | char *ap; |
| 874 | char *save_ptr; |
| 875 | int64_t val; |
| 876 | int field_idx; |
| 877 | enum field_match_result match_res; |
| 878 | |
| 879 | cp = strtok_r(line, " ", &save_ptr); |
| 880 | if (!cp) { |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | ap = strtok_r(NULL, " ", &save_ptr); |
| 885 | if (!ap) { |
| 886 | return false; |
| 887 | } |
| 888 | |
| 889 | match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT, |
| 890 | &val, &field_idx); |
| 891 | if (match_res == PARSE_SUCCESS) { |
| 892 | mi->arr[field_idx] = val / page_k; |
| 893 | } |
| 894 | return (match_res != PARSE_FAIL); |
| 895 | } |
| 896 | |
| 897 | static int meminfo_parse(union meminfo *mi) { |
| 898 | static struct reread_data file_data = { |
| 899 | .filename = MEMINFO_PATH, |
| 900 | .fd = -1, |
| 901 | }; |
| 902 | char buf[PAGE_SIZE]; |
| 903 | char *save_ptr; |
| 904 | char *line; |
| 905 | |
| 906 | memset(mi, 0, sizeof(union meminfo)); |
| 907 | |
| 908 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
| 909 | return -1; |
| 910 | } |
| 911 | |
| 912 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 913 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 914 | if (!meminfo_parse_line(line, mi)) { |
| 915 | ALOGE("%s parse error", file_data.filename); |
| 916 | return -1; |
| 917 | } |
| 918 | } |
| 919 | mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + |
| 920 | mi->field.buffers; |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 925 | static int proc_get_size(int pid) { |
| 926 | char path[PATH_MAX]; |
| 927 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 928 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 929 | int rss = 0; |
| 930 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 931 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 932 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 933 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 934 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 935 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 936 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 937 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 938 | |
| 939 | ret = read_all(fd, line, sizeof(line) - 1); |
| 940 | if (ret < 0) { |
| 941 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 946 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 947 | return rss; |
| 948 | } |
| 949 | |
| 950 | static char *proc_get_name(int pid) { |
| 951 | char path[PATH_MAX]; |
| 952 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 953 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 954 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 955 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 956 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 957 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 958 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 959 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 960 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 961 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 962 | ret = read_all(fd, line, sizeof(line) - 1); |
| 963 | close(fd); |
| 964 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 965 | return NULL; |
| 966 | } |
| 967 | |
| 968 | cp = strchr(line, ' '); |
| 969 | if (cp) |
| 970 | *cp = '\0'; |
| 971 | |
| 972 | return line; |
| 973 | } |
| 974 | |
| 975 | static struct proc *proc_adj_lru(int oomadj) { |
| 976 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 977 | } |
| 978 | |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 979 | static struct proc *proc_get_heaviest(int oomadj) { |
| 980 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 981 | struct adjslot_list *curr = head->next; |
| 982 | struct proc *maxprocp = NULL; |
| 983 | int maxsize = 0; |
| 984 | while (curr != head) { |
| 985 | int pid = ((struct proc *)curr)->pid; |
| 986 | int tasksize = proc_get_size(pid); |
| 987 | if (tasksize <= 0) { |
| 988 | struct adjslot_list *next = curr->next; |
| 989 | pid_remove(pid); |
| 990 | curr = next; |
| 991 | } else { |
| 992 | if (tasksize > maxsize) { |
| 993 | maxsize = tasksize; |
| 994 | maxprocp = (struct proc *)curr; |
| 995 | } |
| 996 | curr = curr->next; |
| 997 | } |
| 998 | } |
| 999 | return maxprocp; |
| 1000 | } |
| 1001 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1002 | /* 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] | 1003 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 1004 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1005 | int pid = procp->pid; |
| 1006 | uid_t uid = procp->uid; |
| 1007 | char *taskname; |
| 1008 | int tasksize; |
| 1009 | int r; |
| 1010 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1011 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 1012 | struct memory_stat mem_st = {}; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1013 | int memory_stat_parse_result = -1; |
| 1014 | #endif |
| 1015 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1016 | taskname = proc_get_name(pid); |
| 1017 | if (!taskname) { |
| 1018 | pid_remove(pid); |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | tasksize = proc_get_size(pid); |
| 1023 | if (tasksize <= 0) { |
| 1024 | pid_remove(pid); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1028 | #ifdef LMKD_LOG_STATS |
| 1029 | if (enable_stats_log) { |
| 1030 | memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid); |
| 1031 | } |
| 1032 | #endif |
| 1033 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1034 | TRACE_KILL_START(pid); |
| 1035 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1036 | /* CAP_KILL required */ |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1037 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1038 | ALOGI( |
| 1039 | "Killing '%s' (%d), uid %d, adj %d\n" |
Suren Baghdasaryan | d07a94f | 2018-03-20 16:25:54 -0700 | [diff] [blame] | 1040 | " 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] | 1041 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 1042 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1043 | pid_remove(pid); |
| 1044 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1045 | TRACE_KILL_END(); |
| 1046 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1047 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1048 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1049 | return -1; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1050 | } else { |
| 1051 | #ifdef LMKD_LOG_STATS |
| 1052 | if (memory_stat_parse_result == 0) { |
| 1053 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, |
| 1054 | procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes, |
| 1055 | mem_st.cache_in_bytes, mem_st.swap_in_bytes); |
| 1056 | } |
| 1057 | #endif |
| 1058 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1059 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1060 | |
| 1061 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1065 | * Find processes to kill to free required number of pages. |
| 1066 | * If pages_to_free is set to 0 only one process will be killed. |
| 1067 | * Returns the size of the killed processes. |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1068 | */ |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1069 | static int find_and_kill_processes(enum vmpressure_level level, |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1070 | int min_score_adj, int pages_to_free) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1071 | int i; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1072 | int killed_size; |
| 1073 | int pages_freed = 0; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1074 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1075 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1076 | bool lmk_state_change_start = false; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1077 | #endif |
| 1078 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1079 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1080 | struct proc *procp; |
| 1081 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1082 | while (true) { |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1083 | procp = kill_heaviest_task ? |
| 1084 | proc_get_heaviest(i) : proc_adj_lru(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1085 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1086 | if (!procp) |
| 1087 | break; |
| 1088 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1089 | killed_size = kill_one_process(procp, min_score_adj, level); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1090 | if (killed_size >= 0) { |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1091 | #ifdef LMKD_LOG_STATS |
| 1092 | if (enable_stats_log && !lmk_state_change_start) { |
| 1093 | lmk_state_change_start = true; |
| 1094 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, |
| 1095 | LMK_STATE_CHANGE_START); |
| 1096 | } |
| 1097 | #endif |
| 1098 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1099 | pages_freed += killed_size; |
| 1100 | if (pages_freed >= pages_to_free) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1101 | |
| 1102 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1103 | if (enable_stats_log && lmk_state_change_start) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1104 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, |
| 1105 | LMK_STATE_CHANGE_STOP); |
| 1106 | } |
| 1107 | #endif |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1108 | return pages_freed; |
| 1109 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1114 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1115 | if (enable_stats_log && lmk_state_change_start) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1116 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP); |
| 1117 | } |
| 1118 | #endif |
| 1119 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1120 | return pages_freed; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1123 | static int64_t get_memory_usage(struct reread_data *file_data) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1124 | int ret; |
| 1125 | int64_t mem_usage; |
| 1126 | char buf[32]; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (reread_file(file_data, buf, sizeof(buf)) < 0) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
| 1131 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1132 | if (!parse_int64(buf, &mem_usage)) { |
| 1133 | ALOGE("%s parse error", file_data->filename); |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1134 | return -1; |
| 1135 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1136 | if (mem_usage == 0) { |
| 1137 | ALOGE("No memory!"); |
| 1138 | return -1; |
| 1139 | } |
| 1140 | return mem_usage; |
| 1141 | } |
| 1142 | |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1143 | void record_low_pressure_levels(union meminfo *mi) { |
| 1144 | if (low_pressure_mem.min_nr_free_pages == -1 || |
| 1145 | low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1146 | if (debug_process_killing) { |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1147 | ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64, |
| 1148 | low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1149 | } |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1150 | low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1151 | } |
| 1152 | /* |
| 1153 | * Free memory at low vmpressure events occasionally gets spikes, |
| 1154 | * possibly a stale low vmpressure event with memory already |
| 1155 | * freed up (no memory pressure should have been reported). |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1156 | * Ignore large jumps in max_nr_free_pages that would mess up our stats. |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1157 | */ |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1158 | if (low_pressure_mem.max_nr_free_pages == -1 || |
| 1159 | (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages && |
| 1160 | mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages < |
| 1161 | low_pressure_mem.max_nr_free_pages * 0.1)) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1162 | if (debug_process_killing) { |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1163 | ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64, |
| 1164 | low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1165 | } |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1166 | low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1170 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 1171 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 1172 | level + 1 : level); |
| 1173 | } |
| 1174 | |
| 1175 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 1176 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 1177 | level - 1 : level); |
| 1178 | } |
| 1179 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1180 | static inline unsigned long get_time_diff_ms(struct timeval *from, |
| 1181 | struct timeval *to) { |
| 1182 | return (to->tv_sec - from->tv_sec) * 1000 + |
| 1183 | (to->tv_usec - from->tv_usec) / 1000; |
| 1184 | } |
| 1185 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1186 | static void mp_event_common(int data, uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1187 | int ret; |
| 1188 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1189 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1190 | int64_t mem_pressure; |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1191 | enum vmpressure_level lvl; |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1192 | union meminfo mi; |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1193 | union zoneinfo zi; |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1194 | static struct timeval last_report_tm; |
| 1195 | static unsigned long skip_count = 0; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1196 | enum vmpressure_level level = (enum vmpressure_level)data; |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1197 | long other_free = 0, other_file = 0; |
| 1198 | int min_score_adj; |
| 1199 | int pages_to_free = 0; |
| 1200 | int minfree = 0; |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1201 | static struct reread_data mem_usage_file_data = { |
| 1202 | .filename = MEMCG_MEMORY_USAGE, |
| 1203 | .fd = -1, |
| 1204 | }; |
| 1205 | static struct reread_data memsw_usage_file_data = { |
| 1206 | .filename = MEMCG_MEMORYSW_USAGE, |
| 1207 | .fd = -1, |
| 1208 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1209 | |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1210 | /* |
| 1211 | * Check all event counters from low to critical |
| 1212 | * and upgrade to the highest priority one. By reading |
| 1213 | * eventfd we also reset the event counters. |
| 1214 | */ |
| 1215 | for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { |
| 1216 | if (mpevfd[lvl] != -1 && |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1217 | TEMP_FAILURE_RETRY(read(mpevfd[lvl], |
| 1218 | &evcount, sizeof(evcount))) > 0 && |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1219 | evcount > 0 && lvl > level) { |
| 1220 | level = lvl; |
| 1221 | } |
| 1222 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1223 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1224 | if (kill_timeout_ms) { |
| 1225 | struct timeval curr_tm; |
| 1226 | gettimeofday(&curr_tm, NULL); |
| 1227 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) { |
| 1228 | skip_count++; |
| 1229 | return; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | if (skip_count > 0) { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1234 | ALOGI("%lu memory pressure events were skipped after a kill!", |
| 1235 | skip_count); |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1236 | skip_count = 0; |
| 1237 | } |
| 1238 | |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1239 | if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1240 | ALOGE("Failed to get free memory!"); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1244 | if (use_minfree_levels) { |
| 1245 | int i; |
| 1246 | |
| 1247 | other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages; |
| 1248 | if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) { |
| 1249 | other_file = (mi.field.nr_file_pages - mi.field.shmem - |
| 1250 | mi.field.unevictable - mi.field.swap_cached); |
| 1251 | } else { |
| 1252 | other_file = 0; |
| 1253 | } |
| 1254 | |
| 1255 | min_score_adj = OOM_SCORE_ADJ_MAX + 1; |
| 1256 | for (i = 0; i < lowmem_targets_size; i++) { |
| 1257 | minfree = lowmem_minfree[i]; |
| 1258 | if (other_free < minfree && other_file < minfree) { |
| 1259 | min_score_adj = lowmem_adj[i]; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | |
Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1264 | if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { |
| 1265 | if (debug_process_killing) { |
| 1266 | ALOGI("Ignore %s memory pressure event " |
| 1267 | "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)", |
| 1268 | level_name[level], other_free * page_k, other_file * page_k, |
| 1269 | (long)lowmem_minfree[lowmem_targets_size - 1] * page_k); |
| 1270 | } |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1271 | return; |
Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1272 | } |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* Free up enough pages to push over the highest minfree level */ |
| 1275 | pages_to_free = lowmem_minfree[lowmem_targets_size - 1] - |
| 1276 | ((other_free < other_file) ? other_free : other_file); |
| 1277 | goto do_kill; |
| 1278 | } |
| 1279 | |
Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1280 | if (level == VMPRESS_LEVEL_LOW) { |
| 1281 | record_low_pressure_levels(&mi); |
| 1282 | } |
| 1283 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1284 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 1285 | /* Do not monitor this pressure level */ |
| 1286 | return; |
| 1287 | } |
| 1288 | |
Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1289 | if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) { |
| 1290 | goto do_kill; |
| 1291 | } |
| 1292 | if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1293 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1294 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1295 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1296 | // Calculate percent for swappinness. |
| 1297 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 1298 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1299 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1300 | // We are swapping too much. |
| 1301 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1302 | level = upgrade_level(level); |
| 1303 | if (debug_process_killing) { |
| 1304 | ALOGI("Event upgraded to %s", level_name[level]); |
| 1305 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1309 | // If the pressure is larger than downgrade_pressure lmk will not |
| 1310 | // kill any process, since enough memory is available. |
| 1311 | if (mem_pressure > downgrade_pressure) { |
| 1312 | if (debug_process_killing) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1313 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1314 | } |
| 1315 | return; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1316 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 1317 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1318 | if (debug_process_killing) { |
| 1319 | ALOGI("Downgrade critical memory pressure"); |
| 1320 | } |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1321 | // Downgrade event, since enough memory available. |
| 1322 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1325 | do_kill: |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1326 | if (low_ram_device) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1327 | /* For Go devices kill only one task */ |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1328 | if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1329 | if (debug_process_killing) { |
| 1330 | ALOGI("Nothing to kill"); |
| 1331 | } |
| 1332 | } |
| 1333 | } else { |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1334 | int pages_freed; |
| 1335 | |
| 1336 | if (!use_minfree_levels) { |
| 1337 | /* If pressure level is less than critical and enough free swap then ignore */ |
| 1338 | if (level < VMPRESS_LEVEL_CRITICAL && |
| 1339 | mi.field.free_swap > low_pressure_mem.max_nr_free_pages) { |
| 1340 | if (debug_process_killing) { |
| 1341 | ALOGI("Ignoring pressure since %" PRId64 |
| 1342 | " swap pages are available ", |
| 1343 | mi.field.free_swap); |
| 1344 | } |
| 1345 | return; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1346 | } |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1347 | /* Free up enough memory to downgrate the memory pressure to low level */ |
| 1348 | if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) { |
| 1349 | pages_to_free = low_pressure_mem.max_nr_free_pages - |
| 1350 | mi.field.nr_free_pages; |
| 1351 | } else { |
| 1352 | if (debug_process_killing) { |
| 1353 | ALOGI("Ignoring pressure since more memory is " |
| 1354 | "available (%" PRId64 ") than watermark (%" PRId64 ")", |
| 1355 | mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages); |
| 1356 | } |
| 1357 | return; |
| 1358 | } |
| 1359 | min_score_adj = level_oomadj[level]; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1362 | pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free); |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1363 | |
| 1364 | if (use_minfree_levels) { |
| 1365 | ALOGI("Killing because cache %ldkB is below " |
| 1366 | "limit %ldkB for oom_adj %d\n" |
| 1367 | " Free memory is %ldkB %s reserved", |
| 1368 | other_file * page_k, minfree * page_k, min_score_adj, |
| 1369 | other_free * page_k, other_free >= 0 ? "above" : "below"); |
| 1370 | } |
| 1371 | |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1372 | if (pages_freed < pages_to_free) { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1373 | ALOGI("Unable to free enough memory (pages to free=%d, pages freed=%d)", |
| 1374 | pages_to_free, pages_freed); |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1375 | } else { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1376 | ALOGI("Reclaimed enough memory (pages to free=%d, pages freed=%d)", |
| 1377 | pages_to_free, pages_freed); |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1378 | gettimeofday(&last_report_tm, NULL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1379 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 1380 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1383 | static bool init_mp_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1384 | int mpfd; |
| 1385 | int evfd; |
| 1386 | int evctlfd; |
| 1387 | char buf[256]; |
| 1388 | struct epoll_event epev; |
| 1389 | int ret; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1390 | int level_idx = (int)level; |
| 1391 | const char *levelstr = level_name[level_idx]; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1392 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1393 | /* gid containing AID_SYSTEM required */ |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1394 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1395 | if (mpfd < 0) { |
| 1396 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 1397 | goto err_open_mpfd; |
| 1398 | } |
| 1399 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1400 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1401 | if (evctlfd < 0) { |
| 1402 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 1403 | goto err_open_evctlfd; |
| 1404 | } |
| 1405 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1406 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1407 | if (evfd < 0) { |
| 1408 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 1409 | goto err_eventfd; |
| 1410 | } |
| 1411 | |
| 1412 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 1413 | if (ret >= (ssize_t)sizeof(buf)) { |
| 1414 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 1415 | goto err; |
| 1416 | } |
| 1417 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1418 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1419 | if (ret == -1) { |
| 1420 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 1421 | levelstr, errno); |
| 1422 | goto err; |
| 1423 | } |
| 1424 | |
| 1425 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1426 | /* use data to store event level */ |
| 1427 | vmpressure_hinfo[level_idx].data = level_idx; |
| 1428 | vmpressure_hinfo[level_idx].handler = mp_event_common; |
| 1429 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1430 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 1431 | if (ret == -1) { |
| 1432 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 1433 | goto err; |
| 1434 | } |
| 1435 | maxevents++; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1436 | mpevfd[level] = evfd; |
Suren Baghdasaryan | 1bd2fc4 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 1437 | close(evctlfd); |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1438 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1439 | |
| 1440 | err: |
| 1441 | close(evfd); |
| 1442 | err_eventfd: |
| 1443 | close(evctlfd); |
| 1444 | err_open_evctlfd: |
| 1445 | close(mpfd); |
| 1446 | err_open_mpfd: |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1447 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1450 | static int init(void) { |
| 1451 | struct epoll_event epev; |
| 1452 | int i; |
| 1453 | int ret; |
| 1454 | |
| 1455 | page_k = sysconf(_SC_PAGESIZE); |
| 1456 | if (page_k == -1) |
| 1457 | page_k = PAGE_SIZE; |
| 1458 | page_k /= 1024; |
| 1459 | |
| 1460 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 1461 | if (epollfd == -1) { |
| 1462 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1466 | // mark data connections as not connected |
| 1467 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1468 | data_sock[i].sock = -1; |
| 1469 | } |
| 1470 | |
| 1471 | ctrl_sock.sock = android_get_control_socket("lmkd"); |
| 1472 | if (ctrl_sock.sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1473 | ALOGE("get lmkd control socket failed"); |
| 1474 | return -1; |
| 1475 | } |
| 1476 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1477 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1478 | if (ret < 0) { |
| 1479 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | |
| 1483 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1484 | ctrl_sock.handler_info.handler = ctrl_connect_handler; |
| 1485 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); |
| 1486 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1487 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | maxevents++; |
| 1491 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1492 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
Suren Baghdasaryan | 979591b | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 1493 | use_inkernel_interface = has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1494 | |
| 1495 | if (use_inkernel_interface) { |
| 1496 | ALOGI("Using in-kernel low memory killer interface"); |
| 1497 | } else { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1498 | if (!init_mp_common(VMPRESS_LEVEL_LOW) || |
| 1499 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || |
| 1500 | !init_mp_common(VMPRESS_LEVEL_CRITICAL)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1501 | 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] | 1502 | return -1; |
| 1503 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1506 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1507 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 1508 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 1509 | } |
| 1510 | |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static void mainloop(void) { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1515 | struct event_handler_info* handler_info; |
| 1516 | struct epoll_event *evt; |
| 1517 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1518 | while (1) { |
| 1519 | struct epoll_event events[maxevents]; |
| 1520 | int nevents; |
| 1521 | int i; |
| 1522 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1523 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 1524 | |
| 1525 | if (nevents == -1) { |
| 1526 | if (errno == EINTR) |
| 1527 | continue; |
| 1528 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 1529 | continue; |
| 1530 | } |
| 1531 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1532 | /* |
| 1533 | * First pass to see if any data socket connections were dropped. |
| 1534 | * Dropped connection should be handled before any other events |
| 1535 | * to deallocate data connection and correctly handle cases when |
| 1536 | * connection gets dropped and reestablished in the same epoll cycle. |
| 1537 | * In such cases it's essential to handle connection closures first. |
| 1538 | */ |
| 1539 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1540 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { |
| 1541 | ALOGI("lmkd data connection dropped"); |
| 1542 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1543 | ctrl_data_close(handler_info->data); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* Second pass to handle all other events */ |
| 1548 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1549 | if (evt->events & EPOLLERR) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1550 | ALOGD("EPOLLERR on event #%d", i); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1551 | if (evt->events & EPOLLHUP) { |
| 1552 | /* This case was handled in the first pass */ |
| 1553 | continue; |
| 1554 | } |
| 1555 | if (evt->data.ptr) { |
| 1556 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1557 | handler_info->handler(handler_info->data, evt->events); |
| 1558 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 1563 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1564 | struct sched_param param = { |
| 1565 | .sched_priority = 1, |
| 1566 | }; |
| 1567 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1568 | /* By default disable low level vmpressure events */ |
| 1569 | level_oomadj[VMPRESS_LEVEL_LOW] = |
| 1570 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); |
| 1571 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
| 1572 | property_get_int32("ro.lmk.medium", 800); |
| 1573 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
| 1574 | property_get_int32("ro.lmk.critical", 0); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1575 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | ad2fd91 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 1576 | |
| 1577 | /* By default disable upgrade/downgrade logic */ |
| 1578 | enable_pressure_upgrade = |
| 1579 | property_get_bool("ro.lmk.critical_upgrade", false); |
| 1580 | upgrade_pressure = |
| 1581 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); |
| 1582 | downgrade_pressure = |
| 1583 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1584 | kill_heaviest_task = |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1585 | property_get_bool("ro.lmk.kill_heaviest_task", false); |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1586 | low_ram_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1587 | kill_timeout_ms = |
| 1588 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); |
Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1589 | use_minfree_levels = |
| 1590 | property_get_bool("ro.lmk.use_minfree_levels", false); |
Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1591 | per_app_memcg = |
| 1592 | property_get_bool("ro.config.per_app_memcg", low_ram_device); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 1593 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1594 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 1c669f7 | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1595 | statslog_init(&log_ctx, &enable_stats_log); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1596 | #endif |
| 1597 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1598 | if (!init()) { |
| 1599 | if (!use_inkernel_interface) { |
| 1600 | /* |
| 1601 | * MCL_ONFAULT pins pages as they fault instead of loading |
| 1602 | * everything immediately all at once. (Which would be bad, |
| 1603 | * because as of this writing, we have a lot of mapped pages we |
| 1604 | * never use.) Old kernels will see MCL_ONFAULT and fail with |
| 1605 | * EINVAL; we ignore this failure. |
| 1606 | * |
| 1607 | * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 1608 | * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 1609 | * in pages. |
| 1610 | */ |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1611 | /* CAP_IPC_LOCK required */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1612 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { |
| 1613 | ALOGW("mlockall failed %s", strerror(errno)); |
| 1614 | } |
Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 1615 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1616 | /* CAP_NICE required */ |
| 1617 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 1618 | ALOGW("set SCHED_FIFO failed %s", strerror(errno)); |
| 1619 | } |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1622 | mainloop(); |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1623 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1624 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1625 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 1c669f7 | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1626 | statslog_destroy(&log_ctx); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1627 | #endif |
| 1628 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1629 | ALOGI("exiting"); |
| 1630 | return 0; |
| 1631 | } |