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