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