| 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 |  | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 19 | #include <dirent.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 20 | #include <errno.h> | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 21 | #include <inttypes.h> | 
| Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 22 | #include <pwd.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 23 | #include <sched.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 24 | #include <signal.h> | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 25 | #include <stdbool.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 28 | #include <sys/cdefs.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 29 | #include <sys/epoll.h> | 
|  | 30 | #include <sys/eventfd.h> | 
| Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 31 | #include <sys/mman.h> | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 32 | #include <sys/resource.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 33 | #include <sys/socket.h> | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 34 | #include <sys/sysinfo.h> | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 35 | #include <sys/time.h> | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 36 | #include <sys/types.h> | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 37 | #include <time.h> | 
| Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 38 | #include <unistd.h> | 
|  | 39 |  | 
| Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 40 | #include <cutils/properties.h> | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 41 | #include <cutils/sched_policy.h> | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 42 | #include <cutils/sockets.h> | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 43 | #include <lmkd.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 44 | #include <log/log.h> | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 45 | #include <log/log_event_list.h> | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 46 | #include <log/log_time.h> | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 47 | #include <psi/psi.h> | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 48 | #include <system/thread_defs.h> | 
| Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 49 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 50 | #ifdef LMKD_LOG_STATS | 
| Yao Chen | 389aee1 | 2018-05-02 11:19:27 -0700 | [diff] [blame] | 51 | #include "statslog.h" | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 52 | #endif | 
|  | 53 |  | 
| Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 54 | /* | 
|  | 55 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces | 
|  | 56 | * to profile and correlate with OOM kills | 
|  | 57 | */ | 
|  | 58 | #ifdef LMKD_TRACE_KILLS | 
|  | 59 |  | 
|  | 60 | #define ATRACE_TAG ATRACE_TAG_ALWAYS | 
|  | 61 | #include <cutils/trace.h> | 
|  | 62 |  | 
|  | 63 | #define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid); | 
|  | 64 | #define TRACE_KILL_END()      ATRACE_INT(__FUNCTION__, 0); | 
|  | 65 |  | 
|  | 66 | #else /* LMKD_TRACE_KILLS */ | 
|  | 67 |  | 
| Daniel Colascione | 347f6b4 | 2018-02-12 11:24:47 -0800 | [diff] [blame] | 68 | #define TRACE_KILL_START(pid) ((void)(pid)) | 
|  | 69 | #define TRACE_KILL_END() ((void)0) | 
| Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | #endif /* LMKD_TRACE_KILLS */ | 
|  | 72 |  | 
| Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 73 | #ifndef __unused | 
|  | 74 | #define __unused __attribute__((__unused__)) | 
|  | 75 | #endif | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 76 |  | 
|  | 77 | #define MEMCG_SYSFS_PATH "/dev/memcg/" | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 78 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" | 
|  | 79 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 80 | #define ZONEINFO_PATH "/proc/zoneinfo" | 
|  | 81 | #define MEMINFO_PATH "/proc/meminfo" | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 82 | #define PROC_STATUS_TGID_FIELD "Tgid:" | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 83 | #define LINE_MAX 128 | 
|  | 84 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 85 | /* Android Logger event logtags (see event.logtags) */ | 
|  | 86 | #define MEMINFO_LOG_TAG 10195355 | 
|  | 87 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 88 | /* gid containing AID_SYSTEM required */ | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 89 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" | 
|  | 90 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" | 
|  | 91 |  | 
|  | 92 | #define ARRAY_SIZE(x)   (sizeof(x) / sizeof(*(x))) | 
| Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 93 | #define EIGHT_MEGA (1 << 23) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 94 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 95 | #define TARGET_UPDATE_MIN_INTERVAL_MS 1000 | 
|  | 96 |  | 
|  | 97 | #define NS_PER_MS (NS_PER_SEC / MS_PER_SEC) | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 98 | #define US_PER_MS (US_PER_SEC / MS_PER_SEC) | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 99 |  | 
| Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 100 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ | 
|  | 101 | #define SYSTEM_ADJ (-900) | 
|  | 102 |  | 
| Greg Kaiser | f0da9b0 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 103 | #define STRINGIFY(x) STRINGIFY_INTERNAL(x) | 
|  | 104 | #define STRINGIFY_INTERNAL(x) #x | 
|  | 105 |  | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 106 | /* | 
|  | 107 | * PSI monitor tracking window size. | 
|  | 108 | * PSI monitor generates events at most once per window, | 
|  | 109 | * therefore we poll memory state for the duration of | 
|  | 110 | * PSI_WINDOW_SIZE_MS after the event happens. | 
|  | 111 | */ | 
|  | 112 | #define PSI_WINDOW_SIZE_MS 1000 | 
|  | 113 | /* Polling period after initial PSI signal */ | 
| Suren Baghdasaryan | 5db6a84 | 2019-03-26 13:21:45 -0700 | [diff] [blame] | 114 | #define PSI_POLL_PERIOD_MS 10 | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 115 | /* Poll for the duration of one window after initial PSI signal */ | 
|  | 116 | #define PSI_POLL_COUNT (PSI_WINDOW_SIZE_MS / PSI_POLL_PERIOD_MS) | 
|  | 117 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 118 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | 
|  | 119 |  | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 120 | #define FAIL_REPORT_RLIMIT_MS 1000 | 
|  | 121 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 122 | /* default to old in-kernel interface if no memory pressure events */ | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 123 | static bool use_inkernel_interface = true; | 
| Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 124 | static bool has_inkernel_module; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 125 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 126 | /* memory pressure levels */ | 
|  | 127 | enum vmpressure_level { | 
|  | 128 | VMPRESS_LEVEL_LOW = 0, | 
|  | 129 | VMPRESS_LEVEL_MEDIUM, | 
|  | 130 | VMPRESS_LEVEL_CRITICAL, | 
|  | 131 | VMPRESS_LEVEL_COUNT | 
|  | 132 | }; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 133 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 134 | static const char *level_name[] = { | 
|  | 135 | "low", | 
|  | 136 | "medium", | 
|  | 137 | "critical" | 
|  | 138 | }; | 
|  | 139 |  | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 140 | struct { | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 141 | int64_t min_nr_free_pages; /* recorded but not used yet */ | 
|  | 142 | int64_t max_nr_free_pages; | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 143 | } low_pressure_mem = { -1, -1 }; | 
|  | 144 |  | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 145 | struct psi_threshold { | 
|  | 146 | enum psi_stall_type stall_type; | 
|  | 147 | int threshold_ms; | 
|  | 148 | }; | 
|  | 149 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 150 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; | 
| Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 151 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 152 | static bool debug_process_killing; | 
|  | 153 | static bool enable_pressure_upgrade; | 
|  | 154 | static int64_t upgrade_pressure; | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 155 | static int64_t downgrade_pressure; | 
| Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 156 | static bool low_ram_device; | 
| Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 157 | static bool kill_heaviest_task; | 
| Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 158 | static unsigned long kill_timeout_ms; | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 159 | static bool use_minfree_levels; | 
| Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 160 | static bool per_app_memcg; | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 161 | static int swap_free_low_percentage; | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 162 | static bool use_psi_monitors = false; | 
|  | 163 | static struct psi_threshold psi_thresholds[VMPRESS_LEVEL_COUNT] = { | 
|  | 164 | { PSI_SOME, 70 },    /* 70ms out of 1sec for partial stall */ | 
|  | 165 | { PSI_SOME, 100 },   /* 100ms out of 1sec for partial stall */ | 
|  | 166 | { PSI_FULL, 70 },    /* 70ms out of 1sec for complete stall */ | 
|  | 167 | }; | 
| Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 168 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 169 | static android_log_context ctx; | 
|  | 170 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 171 | /* data required to handle events */ | 
|  | 172 | struct event_handler_info { | 
|  | 173 | int data; | 
|  | 174 | void (*handler)(int data, uint32_t events); | 
|  | 175 | }; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 176 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 177 | /* data required to handle socket events */ | 
|  | 178 | struct sock_event_handler_info { | 
|  | 179 | int sock; | 
|  | 180 | struct event_handler_info handler_info; | 
|  | 181 | }; | 
|  | 182 |  | 
|  | 183 | /* max supported number of data connections */ | 
|  | 184 | #define MAX_DATA_CONN 2 | 
|  | 185 |  | 
|  | 186 | /* socket event handler data */ | 
|  | 187 | static struct sock_event_handler_info ctrl_sock; | 
|  | 188 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; | 
|  | 189 |  | 
|  | 190 | /* vmpressure event handler data */ | 
|  | 191 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; | 
|  | 192 |  | 
| Jim Blackler | 3947c93 | 2019-04-26 11:18:29 +0100 | [diff] [blame] | 193 | /* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket, 1 lmk events */ | 
|  | 194 | #define MAX_EPOLL_EVENTS (2 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 195 | static int epollfd; | 
|  | 196 | static int maxevents; | 
|  | 197 |  | 
| Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 198 | /* OOM score values used by both kernel and framework */ | 
| Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 199 | #define OOM_SCORE_ADJ_MIN       (-1000) | 
|  | 200 | #define OOM_SCORE_ADJ_MAX       1000 | 
|  | 201 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 202 | static int lowmem_adj[MAX_TARGETS]; | 
|  | 203 | static int lowmem_minfree[MAX_TARGETS]; | 
|  | 204 | static int lowmem_targets_size; | 
|  | 205 |  | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 206 | /* Fields to parse in /proc/zoneinfo */ | 
|  | 207 | enum zoneinfo_field { | 
|  | 208 | ZI_NR_FREE_PAGES = 0, | 
|  | 209 | ZI_NR_FILE_PAGES, | 
|  | 210 | ZI_NR_SHMEM, | 
|  | 211 | ZI_NR_UNEVICTABLE, | 
|  | 212 | ZI_WORKINGSET_REFAULT, | 
|  | 213 | ZI_HIGH, | 
|  | 214 | ZI_FIELD_COUNT | 
|  | 215 | }; | 
|  | 216 |  | 
|  | 217 | static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = { | 
|  | 218 | "nr_free_pages", | 
|  | 219 | "nr_file_pages", | 
|  | 220 | "nr_shmem", | 
|  | 221 | "nr_unevictable", | 
|  | 222 | "workingset_refault", | 
|  | 223 | "high", | 
|  | 224 | }; | 
|  | 225 |  | 
|  | 226 | union zoneinfo { | 
|  | 227 | struct { | 
|  | 228 | int64_t nr_free_pages; | 
|  | 229 | int64_t nr_file_pages; | 
|  | 230 | int64_t nr_shmem; | 
|  | 231 | int64_t nr_unevictable; | 
|  | 232 | int64_t workingset_refault; | 
|  | 233 | int64_t high; | 
|  | 234 | /* fields below are calculated rather than read from the file */ | 
|  | 235 | int64_t totalreserve_pages; | 
|  | 236 | } field; | 
|  | 237 | int64_t arr[ZI_FIELD_COUNT]; | 
|  | 238 | }; | 
|  | 239 |  | 
|  | 240 | /* Fields to parse in /proc/meminfo */ | 
|  | 241 | enum meminfo_field { | 
|  | 242 | MI_NR_FREE_PAGES = 0, | 
|  | 243 | MI_CACHED, | 
|  | 244 | MI_SWAP_CACHED, | 
|  | 245 | MI_BUFFERS, | 
|  | 246 | MI_SHMEM, | 
|  | 247 | MI_UNEVICTABLE, | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 248 | MI_TOTAL_SWAP, | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 249 | MI_FREE_SWAP, | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 250 | MI_ACTIVE_ANON, | 
|  | 251 | MI_INACTIVE_ANON, | 
|  | 252 | MI_ACTIVE_FILE, | 
|  | 253 | MI_INACTIVE_FILE, | 
|  | 254 | MI_SRECLAIMABLE, | 
|  | 255 | MI_SUNRECLAIM, | 
|  | 256 | MI_KERNEL_STACK, | 
|  | 257 | MI_PAGE_TABLES, | 
|  | 258 | MI_ION_HELP, | 
|  | 259 | MI_ION_HELP_POOL, | 
|  | 260 | MI_CMA_FREE, | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 261 | MI_FIELD_COUNT | 
|  | 262 | }; | 
|  | 263 |  | 
|  | 264 | static const char* const meminfo_field_names[MI_FIELD_COUNT] = { | 
|  | 265 | "MemFree:", | 
|  | 266 | "Cached:", | 
|  | 267 | "SwapCached:", | 
|  | 268 | "Buffers:", | 
|  | 269 | "Shmem:", | 
|  | 270 | "Unevictable:", | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 271 | "SwapTotal:", | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 272 | "SwapFree:", | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 273 | "Active(anon):", | 
|  | 274 | "Inactive(anon):", | 
|  | 275 | "Active(file):", | 
|  | 276 | "Inactive(file):", | 
|  | 277 | "SReclaimable:", | 
|  | 278 | "SUnreclaim:", | 
|  | 279 | "KernelStack:", | 
|  | 280 | "PageTables:", | 
|  | 281 | "ION_heap:", | 
|  | 282 | "ION_heap_pool:", | 
|  | 283 | "CmaFree:", | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 284 | }; | 
|  | 285 |  | 
|  | 286 | union meminfo { | 
|  | 287 | struct { | 
|  | 288 | int64_t nr_free_pages; | 
|  | 289 | int64_t cached; | 
|  | 290 | int64_t swap_cached; | 
|  | 291 | int64_t buffers; | 
|  | 292 | int64_t shmem; | 
|  | 293 | int64_t unevictable; | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 294 | int64_t total_swap; | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 295 | int64_t free_swap; | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 296 | int64_t active_anon; | 
|  | 297 | int64_t inactive_anon; | 
|  | 298 | int64_t active_file; | 
|  | 299 | int64_t inactive_file; | 
|  | 300 | int64_t sreclaimable; | 
|  | 301 | int64_t sunreclaimable; | 
|  | 302 | int64_t kernel_stack; | 
|  | 303 | int64_t page_tables; | 
|  | 304 | int64_t ion_heap; | 
|  | 305 | int64_t ion_heap_pool; | 
|  | 306 | int64_t cma_free; | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 307 | /* fields below are calculated rather than read from the file */ | 
|  | 308 | int64_t nr_file_pages; | 
|  | 309 | } field; | 
|  | 310 | int64_t arr[MI_FIELD_COUNT]; | 
|  | 311 | }; | 
|  | 312 |  | 
|  | 313 | enum field_match_result { | 
|  | 314 | NO_MATCH, | 
|  | 315 | PARSE_FAIL, | 
|  | 316 | PARSE_SUCCESS | 
|  | 317 | }; | 
|  | 318 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 319 | struct adjslot_list { | 
|  | 320 | struct adjslot_list *next; | 
|  | 321 | struct adjslot_list *prev; | 
|  | 322 | }; | 
|  | 323 |  | 
|  | 324 | struct proc { | 
|  | 325 | struct adjslot_list asl; | 
|  | 326 | int pid; | 
| Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 327 | uid_t uid; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 328 | int oomadj; | 
|  | 329 | struct proc *pidhash_next; | 
|  | 330 | }; | 
|  | 331 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 332 | struct reread_data { | 
|  | 333 | const char* const filename; | 
|  | 334 | int fd; | 
|  | 335 | }; | 
|  | 336 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 337 | #ifdef LMKD_LOG_STATS | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 338 | static bool enable_stats_log; | 
|  | 339 | static android_log_context log_ctx; | 
|  | 340 | #endif | 
|  | 341 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 342 | #define PIDHASH_SZ 1024 | 
|  | 343 | static struct proc *pidhash[PIDHASH_SZ]; | 
|  | 344 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) | 
|  | 345 |  | 
| Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 346 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 347 | #define ADJTOSLOT_COUNT (ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1) | 
|  | 348 | static struct adjslot_list procadjslot_list[ADJTOSLOT_COUNT]; | 
|  | 349 |  | 
|  | 350 | #define MAX_DISTINCT_OOM_ADJ 32 | 
|  | 351 | #define KILLCNT_INVALID_IDX 0xFF | 
|  | 352 | /* | 
|  | 353 | * Because killcnt array is sparse a two-level indirection is used | 
|  | 354 | * to keep the size small. killcnt_idx stores index of the element in | 
|  | 355 | * killcnt array. Index KILLCNT_INVALID_IDX indicates an unused slot. | 
|  | 356 | */ | 
|  | 357 | static uint8_t killcnt_idx[ADJTOSLOT_COUNT]; | 
|  | 358 | static uint16_t killcnt[MAX_DISTINCT_OOM_ADJ]; | 
|  | 359 | static int killcnt_free_idx = 0; | 
|  | 360 | static uint32_t killcnt_total = 0; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 361 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 362 | /* PAGE_SIZE / 1024 */ | 
|  | 363 | static long page_k; | 
|  | 364 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 365 | static bool parse_int64(const char* str, int64_t* ret) { | 
|  | 366 | char* endptr; | 
|  | 367 | long long val = strtoll(str, &endptr, 10); | 
|  | 368 | if (str == endptr || val > INT64_MAX) { | 
|  | 369 | return false; | 
|  | 370 | } | 
|  | 371 | *ret = (int64_t)val; | 
|  | 372 | return true; | 
|  | 373 | } | 
|  | 374 |  | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 375 | static enum field_match_result match_field(const char* cp, const char* ap, | 
|  | 376 | const char* const field_names[], | 
|  | 377 | int field_count, int64_t* field, | 
|  | 378 | int *field_idx) { | 
|  | 379 | int64_t val; | 
|  | 380 | int i; | 
|  | 381 |  | 
|  | 382 | for (i = 0; i < field_count; i++) { | 
|  | 383 | if (!strcmp(cp, field_names[i])) { | 
|  | 384 | *field_idx = i; | 
|  | 385 | return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL; | 
|  | 386 | } | 
|  | 387 | } | 
|  | 388 | return NO_MATCH; | 
|  | 389 | } | 
|  | 390 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 391 | /* | 
|  | 392 | * Read file content from the beginning up to max_len bytes or EOF | 
|  | 393 | * whichever happens first. | 
|  | 394 | */ | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 395 | static ssize_t read_all(int fd, char *buf, size_t max_len) | 
|  | 396 | { | 
|  | 397 | ssize_t ret = 0; | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 398 | off_t offset = 0; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 399 |  | 
|  | 400 | while (max_len > 0) { | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 401 | ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset)); | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 402 | if (r == 0) { | 
|  | 403 | break; | 
|  | 404 | } | 
|  | 405 | if (r == -1) { | 
|  | 406 | return -1; | 
|  | 407 | } | 
|  | 408 | ret += r; | 
|  | 409 | buf += r; | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 410 | offset += r; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 411 | max_len -= r; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | return ret; | 
|  | 415 | } | 
|  | 416 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 417 | /* | 
|  | 418 | * Read a new or already opened file from the beginning. | 
|  | 419 | * If the file has not been opened yet data->fd should be set to -1. | 
|  | 420 | * To be used with files which are read often and possibly during high | 
|  | 421 | * memory pressure to minimize file opening which by itself requires kernel | 
|  | 422 | * memory allocation and might result in a stall on memory stressed system. | 
|  | 423 | */ | 
|  | 424 | static int reread_file(struct reread_data *data, char *buf, size_t buf_size) { | 
|  | 425 | ssize_t size; | 
|  | 426 |  | 
|  | 427 | if (data->fd == -1) { | 
|  | 428 | data->fd = open(data->filename, O_RDONLY | O_CLOEXEC); | 
|  | 429 | if (data->fd == -1) { | 
|  | 430 | ALOGE("%s open: %s", data->filename, strerror(errno)); | 
|  | 431 | return -1; | 
|  | 432 | } | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | size = read_all(data->fd, buf, buf_size - 1); | 
|  | 436 | if (size < 0) { | 
|  | 437 | ALOGE("%s read: %s", data->filename, strerror(errno)); | 
|  | 438 | close(data->fd); | 
|  | 439 | data->fd = -1; | 
|  | 440 | return -1; | 
|  | 441 | } | 
| Chih-Hung Hsieh | 62b0ef5 | 2018-07-25 12:15:25 -0700 | [diff] [blame] | 442 | 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] | 443 | buf[size] = 0; | 
|  | 444 |  | 
|  | 445 | return 0; | 
|  | 446 | } | 
|  | 447 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 448 | static struct proc *pid_lookup(int pid) { | 
|  | 449 | struct proc *procp; | 
|  | 450 |  | 
|  | 451 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; | 
|  | 452 | procp = procp->pidhash_next) | 
|  | 453 | ; | 
|  | 454 |  | 
|  | 455 | return procp; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) | 
|  | 459 | { | 
|  | 460 | struct adjslot_list *next = head->next; | 
|  | 461 | new->prev = head; | 
|  | 462 | new->next = next; | 
|  | 463 | next->prev = new; | 
|  | 464 | head->next = new; | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | static void adjslot_remove(struct adjslot_list *old) | 
|  | 468 | { | 
|  | 469 | struct adjslot_list *prev = old->prev; | 
|  | 470 | struct adjslot_list *next = old->next; | 
|  | 471 | next->prev = prev; | 
|  | 472 | prev->next = next; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { | 
|  | 476 | struct adjslot_list *asl = head->prev; | 
|  | 477 |  | 
|  | 478 | return asl == head ? NULL : asl; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | static void proc_slot(struct proc *procp) { | 
|  | 482 | int adjslot = ADJTOSLOT(procp->oomadj); | 
|  | 483 |  | 
|  | 484 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | static void proc_unslot(struct proc *procp) { | 
|  | 488 | adjslot_remove(&procp->asl); | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | static void proc_insert(struct proc *procp) { | 
|  | 492 | int hval = pid_hashfn(procp->pid); | 
|  | 493 |  | 
|  | 494 | procp->pidhash_next = pidhash[hval]; | 
|  | 495 | pidhash[hval] = procp; | 
|  | 496 | proc_slot(procp); | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | static int pid_remove(int pid) { | 
|  | 500 | int hval = pid_hashfn(pid); | 
|  | 501 | struct proc *procp; | 
|  | 502 | struct proc *prevp; | 
|  | 503 |  | 
|  | 504 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; | 
|  | 505 | procp = procp->pidhash_next) | 
|  | 506 | prevp = procp; | 
|  | 507 |  | 
|  | 508 | if (!procp) | 
|  | 509 | return -1; | 
|  | 510 |  | 
|  | 511 | if (!prevp) | 
|  | 512 | pidhash[hval] = procp->pidhash_next; | 
|  | 513 | else | 
|  | 514 | prevp->pidhash_next = procp->pidhash_next; | 
|  | 515 |  | 
|  | 516 | proc_unslot(procp); | 
|  | 517 | free(procp); | 
|  | 518 | return 0; | 
|  | 519 | } | 
|  | 520 |  | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 521 | /* | 
|  | 522 | * Write a string to a file. | 
|  | 523 | * Returns false if the file does not exist. | 
|  | 524 | */ | 
|  | 525 | static bool writefilestring(const char *path, const char *s, | 
|  | 526 | bool err_if_missing) { | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 527 | int fd = open(path, O_WRONLY | O_CLOEXEC); | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 528 | ssize_t len = strlen(s); | 
|  | 529 | ssize_t ret; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 530 |  | 
|  | 531 | if (fd < 0) { | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 532 | if (err_if_missing) { | 
|  | 533 | ALOGE("Error opening %s; errno=%d", path, errno); | 
|  | 534 | } | 
|  | 535 | return false; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 536 | } | 
|  | 537 |  | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 538 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 539 | if (ret < 0) { | 
|  | 540 | ALOGE("Error writing %s; errno=%d", path, errno); | 
|  | 541 | } else if (ret < len) { | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 542 | ALOGE("Short write on %s; length=%zd", path, ret); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
|  | 545 | close(fd); | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 546 | return true; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 547 | } | 
|  | 548 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 549 | static inline long get_time_diff_ms(struct timespec *from, | 
|  | 550 | struct timespec *to) { | 
|  | 551 | return (to->tv_sec - from->tv_sec) * (long)MS_PER_SEC + | 
|  | 552 | (to->tv_nsec - from->tv_nsec) / (long)NS_PER_MS; | 
|  | 553 | } | 
|  | 554 |  | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 555 | static int proc_get_tgid(int pid) { | 
|  | 556 | char path[PATH_MAX]; | 
|  | 557 | char buf[PAGE_SIZE]; | 
|  | 558 | int fd; | 
|  | 559 | ssize_t size; | 
|  | 560 | char *pos; | 
|  | 561 | int64_t tgid = -1; | 
|  | 562 |  | 
|  | 563 | snprintf(path, PATH_MAX, "/proc/%d/status", pid); | 
|  | 564 | fd = open(path, O_RDONLY | O_CLOEXEC); | 
|  | 565 | if (fd < 0) { | 
|  | 566 | return -1; | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | size = read_all(fd, buf, sizeof(buf) - 1); | 
|  | 570 | if (size < 0) { | 
|  | 571 | goto out; | 
|  | 572 | } | 
|  | 573 | buf[size] = 0; | 
|  | 574 |  | 
|  | 575 | pos = buf; | 
|  | 576 | while (true) { | 
|  | 577 | pos = strstr(pos, PROC_STATUS_TGID_FIELD); | 
|  | 578 | /* Stop if TGID tag not found or found at the line beginning */ | 
|  | 579 | if (pos == NULL || pos == buf || pos[-1] == '\n') { | 
|  | 580 | break; | 
|  | 581 | } | 
|  | 582 | pos++; | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | if (pos == NULL) { | 
|  | 586 | goto out; | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | pos += strlen(PROC_STATUS_TGID_FIELD); | 
|  | 590 | while (*pos == ' ') pos++; | 
|  | 591 | parse_int64(pos, &tgid); | 
|  | 592 |  | 
|  | 593 | out: | 
|  | 594 | close(fd); | 
|  | 595 | return (int)tgid; | 
|  | 596 | } | 
|  | 597 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 598 | static void cmd_procprio(LMKD_CTRL_PACKET packet) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 599 | struct proc *procp; | 
|  | 600 | char path[80]; | 
|  | 601 | char val[20]; | 
| Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 602 | int soft_limit_mult; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 603 | struct lmk_procprio params; | 
| Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 604 | bool is_system_server; | 
|  | 605 | struct passwd *pwdrec; | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 606 | int tgid; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 607 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 608 | lmkd_pack_get_procprio(packet, ¶ms); | 
|  | 609 |  | 
|  | 610 | if (params.oomadj < OOM_SCORE_ADJ_MIN || | 
|  | 611 | params.oomadj > OOM_SCORE_ADJ_MAX) { | 
|  | 612 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 613 | return; | 
|  | 614 | } | 
|  | 615 |  | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 616 | /* Check if registered process is a thread group leader */ | 
|  | 617 | tgid = proc_get_tgid(params.pid); | 
|  | 618 | if (tgid >= 0 && tgid != params.pid) { | 
|  | 619 | ALOGE("Attempt to register a task that is not a thread group leader (tid %d, tgid %d)", | 
|  | 620 | params.pid, tgid); | 
|  | 621 | return; | 
|  | 622 | } | 
|  | 623 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 624 | /* gid containing AID_READPROC required */ | 
|  | 625 | /* CAP_SYS_RESOURCE required */ | 
|  | 626 | /* CAP_DAC_OVERRIDE required */ | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 627 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); | 
|  | 628 | snprintf(val, sizeof(val), "%d", params.oomadj); | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 629 | if (!writefilestring(path, val, false)) { | 
|  | 630 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", | 
|  | 631 | path, errno, params.pid); | 
|  | 632 | /* If this file does not exist the process is dead. */ | 
|  | 633 | return; | 
|  | 634 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 635 |  | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 636 | if (use_inkernel_interface) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 637 | return; | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 638 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 639 |  | 
| Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 640 | if (per_app_memcg) { | 
| Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 641 | if (params.oomadj >= 900) { | 
|  | 642 | soft_limit_mult = 0; | 
|  | 643 | } else if (params.oomadj >= 800) { | 
|  | 644 | soft_limit_mult = 0; | 
|  | 645 | } else if (params.oomadj >= 700) { | 
|  | 646 | soft_limit_mult = 0; | 
|  | 647 | } else if (params.oomadj >= 600) { | 
|  | 648 | // Launcher should be perceptible, don't kill it. | 
|  | 649 | params.oomadj = 200; | 
|  | 650 | soft_limit_mult = 1; | 
|  | 651 | } else if (params.oomadj >= 500) { | 
|  | 652 | soft_limit_mult = 0; | 
|  | 653 | } else if (params.oomadj >= 400) { | 
|  | 654 | soft_limit_mult = 0; | 
|  | 655 | } else if (params.oomadj >= 300) { | 
|  | 656 | soft_limit_mult = 1; | 
|  | 657 | } else if (params.oomadj >= 200) { | 
| Srinivas Paladugu | 3eb20bc | 2018-10-09 14:21:10 -0700 | [diff] [blame] | 658 | soft_limit_mult = 8; | 
| Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 659 | } else if (params.oomadj >= 100) { | 
|  | 660 | soft_limit_mult = 10; | 
|  | 661 | } else if (params.oomadj >=   0) { | 
|  | 662 | soft_limit_mult = 20; | 
|  | 663 | } else { | 
|  | 664 | // Persistent processes will have a large | 
|  | 665 | // soft limit 512MB. | 
|  | 666 | soft_limit_mult = 64; | 
|  | 667 | } | 
| Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 668 |  | 
| Suren Baghdasaryan | 3862dd3 | 2018-05-21 19:48:47 -0700 | [diff] [blame] | 669 | snprintf(path, sizeof(path), MEMCG_SYSFS_PATH | 
|  | 670 | "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", | 
|  | 671 | params.uid, params.pid); | 
| Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 672 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); | 
| Suren Baghdasaryan | 3862dd3 | 2018-05-21 19:48:47 -0700 | [diff] [blame] | 673 |  | 
|  | 674 | /* | 
|  | 675 | * system_server process has no memcg under /dev/memcg/apps but should be | 
|  | 676 | * registered with lmkd. This is the best way so far to identify it. | 
|  | 677 | */ | 
|  | 678 | is_system_server = (params.oomadj == SYSTEM_ADJ && | 
|  | 679 | (pwdrec = getpwnam("system")) != NULL && | 
|  | 680 | params.uid == pwdrec->pw_uid); | 
|  | 681 | writefilestring(path, val, !is_system_server); | 
| Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 682 | } | 
|  | 683 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 684 | procp = pid_lookup(params.pid); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 685 | if (!procp) { | 
|  | 686 | procp = malloc(sizeof(struct proc)); | 
|  | 687 | if (!procp) { | 
|  | 688 | // Oh, the irony.  May need to rebuild our state. | 
|  | 689 | return; | 
|  | 690 | } | 
|  | 691 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 692 | procp->pid = params.pid; | 
|  | 693 | procp->uid = params.uid; | 
|  | 694 | procp->oomadj = params.oomadj; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 695 | proc_insert(procp); | 
|  | 696 | } else { | 
|  | 697 | proc_unslot(procp); | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 698 | procp->oomadj = params.oomadj; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 699 | proc_slot(procp); | 
|  | 700 | } | 
|  | 701 | } | 
|  | 702 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 703 | static void cmd_procremove(LMKD_CTRL_PACKET packet) { | 
|  | 704 | struct lmk_procremove params; | 
|  | 705 |  | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 706 | if (use_inkernel_interface) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 707 | return; | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 708 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 709 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 710 | lmkd_pack_get_procremove(packet, ¶ms); | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 711 | /* | 
|  | 712 | * WARNING: After pid_remove() procp is freed and can't be used! | 
|  | 713 | * Therefore placed at the end of the function. | 
|  | 714 | */ | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 715 | pid_remove(params.pid); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 716 | } | 
|  | 717 |  | 
| Suren Baghdasaryan | e3b6047 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 718 | static void cmd_procpurge() { | 
|  | 719 | int i; | 
|  | 720 | struct proc *procp; | 
|  | 721 | struct proc *next; | 
|  | 722 |  | 
|  | 723 | if (use_inkernel_interface) { | 
|  | 724 | return; | 
|  | 725 | } | 
|  | 726 |  | 
|  | 727 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { | 
|  | 728 | procadjslot_list[i].next = &procadjslot_list[i]; | 
|  | 729 | procadjslot_list[i].prev = &procadjslot_list[i]; | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | for (i = 0; i < PIDHASH_SZ; i++) { | 
|  | 733 | procp = pidhash[i]; | 
|  | 734 | while (procp) { | 
|  | 735 | next = procp->pidhash_next; | 
|  | 736 | free(procp); | 
|  | 737 | procp = next; | 
|  | 738 | } | 
|  | 739 | } | 
|  | 740 | memset(&pidhash[0], 0, sizeof(pidhash)); | 
|  | 741 | } | 
|  | 742 |  | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 743 | static void inc_killcnt(int oomadj) { | 
|  | 744 | int slot = ADJTOSLOT(oomadj); | 
|  | 745 | uint8_t idx = killcnt_idx[slot]; | 
|  | 746 |  | 
|  | 747 | if (idx == KILLCNT_INVALID_IDX) { | 
|  | 748 | /* index is not assigned for this oomadj */ | 
|  | 749 | if (killcnt_free_idx < MAX_DISTINCT_OOM_ADJ) { | 
|  | 750 | killcnt_idx[slot] = killcnt_free_idx; | 
|  | 751 | killcnt[killcnt_free_idx] = 1; | 
|  | 752 | killcnt_free_idx++; | 
|  | 753 | } else { | 
|  | 754 | ALOGW("Number of distinct oomadj levels exceeds %d", | 
|  | 755 | MAX_DISTINCT_OOM_ADJ); | 
|  | 756 | } | 
|  | 757 | } else { | 
|  | 758 | /* | 
|  | 759 | * wraparound is highly unlikely and is detectable using total | 
|  | 760 | * counter because it has to be equal to the sum of all counters | 
|  | 761 | */ | 
|  | 762 | killcnt[idx]++; | 
|  | 763 | } | 
|  | 764 | /* increment total kill counter */ | 
|  | 765 | killcnt_total++; | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | static int get_killcnt(int min_oomadj, int max_oomadj) { | 
|  | 769 | int slot; | 
|  | 770 | int count = 0; | 
|  | 771 |  | 
|  | 772 | if (min_oomadj > max_oomadj) | 
|  | 773 | return 0; | 
|  | 774 |  | 
|  | 775 | /* special case to get total kill count */ | 
|  | 776 | if (min_oomadj > OOM_SCORE_ADJ_MAX) | 
|  | 777 | return killcnt_total; | 
|  | 778 |  | 
|  | 779 | while (min_oomadj <= max_oomadj && | 
|  | 780 | (slot = ADJTOSLOT(min_oomadj)) < ADJTOSLOT_COUNT) { | 
|  | 781 | uint8_t idx = killcnt_idx[slot]; | 
|  | 782 | if (idx != KILLCNT_INVALID_IDX) { | 
|  | 783 | count += killcnt[idx]; | 
|  | 784 | } | 
|  | 785 | min_oomadj++; | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | return count; | 
|  | 789 | } | 
|  | 790 |  | 
|  | 791 | static int cmd_getkillcnt(LMKD_CTRL_PACKET packet) { | 
|  | 792 | struct lmk_getkillcnt params; | 
|  | 793 |  | 
|  | 794 | if (use_inkernel_interface) { | 
|  | 795 | /* kernel driver does not expose this information */ | 
|  | 796 | return 0; | 
|  | 797 | } | 
|  | 798 |  | 
|  | 799 | lmkd_pack_get_getkillcnt(packet, ¶ms); | 
|  | 800 |  | 
|  | 801 | return get_killcnt(params.min_oomadj, params.max_oomadj); | 
|  | 802 | } | 
|  | 803 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 804 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 805 | int i; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 806 | struct lmk_target target; | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 807 | char minfree_str[PROPERTY_VALUE_MAX]; | 
|  | 808 | char *pstr = minfree_str; | 
|  | 809 | char *pend = minfree_str + sizeof(minfree_str); | 
|  | 810 | static struct timespec last_req_tm; | 
|  | 811 | struct timespec curr_tm; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 812 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 813 | if (ntargets < 1 || ntargets > (int)ARRAY_SIZE(lowmem_adj)) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 814 | return; | 
|  | 815 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 816 | /* | 
|  | 817 | * Ratelimit minfree updates to once per TARGET_UPDATE_MIN_INTERVAL_MS | 
|  | 818 | * to prevent DoS attacks | 
|  | 819 | */ | 
|  | 820 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { | 
|  | 821 | ALOGE("Failed to get current time"); | 
|  | 822 | return; | 
|  | 823 | } | 
|  | 824 |  | 
|  | 825 | if (get_time_diff_ms(&last_req_tm, &curr_tm) < | 
|  | 826 | TARGET_UPDATE_MIN_INTERVAL_MS) { | 
|  | 827 | ALOGE("Ignoring frequent updated to lmkd limits"); | 
|  | 828 | return; | 
|  | 829 | } | 
|  | 830 |  | 
|  | 831 | last_req_tm = curr_tm; | 
|  | 832 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 833 | for (i = 0; i < ntargets; i++) { | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 834 | lmkd_pack_get_target(packet, i, &target); | 
|  | 835 | lowmem_minfree[i] = target.minfree; | 
|  | 836 | lowmem_adj[i] = target.oom_adj_score; | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 837 |  | 
|  | 838 | pstr += snprintf(pstr, pend - pstr, "%d:%d,", target.minfree, | 
|  | 839 | target.oom_adj_score); | 
|  | 840 | if (pstr >= pend) { | 
|  | 841 | /* if no more space in the buffer then terminate the loop */ | 
|  | 842 | pstr = pend; | 
|  | 843 | break; | 
|  | 844 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 845 | } | 
|  | 846 |  | 
|  | 847 | lowmem_targets_size = ntargets; | 
|  | 848 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 849 | /* Override the last extra comma */ | 
|  | 850 | pstr[-1] = '\0'; | 
|  | 851 | property_set("sys.lmk.minfree_levels", minfree_str); | 
|  | 852 |  | 
| Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 853 | if (has_inkernel_module) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 854 | char minfreestr[128]; | 
|  | 855 | char killpriostr[128]; | 
|  | 856 |  | 
|  | 857 | minfreestr[0] = '\0'; | 
|  | 858 | killpriostr[0] = '\0'; | 
|  | 859 |  | 
|  | 860 | for (i = 0; i < lowmem_targets_size; i++) { | 
|  | 861 | char val[40]; | 
|  | 862 |  | 
|  | 863 | if (i) { | 
|  | 864 | strlcat(minfreestr, ",", sizeof(minfreestr)); | 
|  | 865 | strlcat(killpriostr, ",", sizeof(killpriostr)); | 
|  | 866 | } | 
|  | 867 |  | 
| Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 868 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 869 | strlcat(minfreestr, val, sizeof(minfreestr)); | 
| Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 870 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 871 | strlcat(killpriostr, val, sizeof(killpriostr)); | 
|  | 872 | } | 
|  | 873 |  | 
| Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 874 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); | 
|  | 875 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 876 | } | 
|  | 877 | } | 
|  | 878 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 879 | static void ctrl_data_close(int dsock_idx) { | 
|  | 880 | struct epoll_event epev; | 
|  | 881 |  | 
|  | 882 | ALOGI("closing lmkd data connection"); | 
|  | 883 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { | 
|  | 884 | // Log a warning and keep going | 
|  | 885 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); | 
|  | 886 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 887 | maxevents--; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 888 |  | 
|  | 889 | close(data_sock[dsock_idx].sock); | 
|  | 890 | data_sock[dsock_idx].sock = -1; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 891 | } | 
|  | 892 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 893 | 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] | 894 | int ret = 0; | 
|  | 895 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 896 | ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz)); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 897 |  | 
|  | 898 | if (ret == -1) { | 
|  | 899 | ALOGE("control data socket read failed; errno=%d", errno); | 
|  | 900 | } else if (ret == 0) { | 
|  | 901 | ALOGE("Got EOF on control data socket"); | 
|  | 902 | ret = -1; | 
|  | 903 | } | 
|  | 904 |  | 
|  | 905 | return ret; | 
|  | 906 | } | 
|  | 907 |  | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 908 | static int ctrl_data_write(int dsock_idx, char *buf, size_t bufsz) { | 
|  | 909 | int ret = 0; | 
|  | 910 |  | 
|  | 911 | ret = TEMP_FAILURE_RETRY(write(data_sock[dsock_idx].sock, buf, bufsz)); | 
|  | 912 |  | 
|  | 913 | if (ret == -1) { | 
|  | 914 | ALOGE("control data socket write failed; errno=%d", errno); | 
|  | 915 | } else if (ret == 0) { | 
|  | 916 | ALOGE("Got EOF on control data socket"); | 
|  | 917 | ret = -1; | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | return ret; | 
|  | 921 | } | 
|  | 922 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 923 | static void ctrl_command_handler(int dsock_idx) { | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 924 | LMKD_CTRL_PACKET packet; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 925 | int len; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 926 | enum lmk_cmd cmd; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 927 | int nargs; | 
|  | 928 | int targets; | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 929 | int kill_cnt; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 930 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 931 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 932 | if (len <= 0) | 
|  | 933 | return; | 
|  | 934 |  | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 935 | if (len < (int)sizeof(int)) { | 
|  | 936 | ALOGE("Wrong control socket read length len=%d", len); | 
|  | 937 | return; | 
|  | 938 | } | 
|  | 939 |  | 
|  | 940 | cmd = lmkd_pack_get_cmd(packet); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 941 | nargs = len / sizeof(int) - 1; | 
|  | 942 | if (nargs < 0) | 
|  | 943 | goto wronglen; | 
|  | 944 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 945 | switch(cmd) { | 
|  | 946 | case LMK_TARGET: | 
|  | 947 | targets = nargs / 2; | 
|  | 948 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) | 
|  | 949 | goto wronglen; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 950 | cmd_target(targets, packet); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 951 | break; | 
|  | 952 | case LMK_PROCPRIO: | 
| Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 953 | if (nargs != 3) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 954 | goto wronglen; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 955 | cmd_procprio(packet); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 956 | break; | 
|  | 957 | case LMK_PROCREMOVE: | 
|  | 958 | if (nargs != 1) | 
|  | 959 | goto wronglen; | 
| Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 960 | cmd_procremove(packet); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 961 | break; | 
| Suren Baghdasaryan | e3b6047 | 2018-10-10 14:17:17 -0700 | [diff] [blame] | 962 | case LMK_PROCPURGE: | 
|  | 963 | if (nargs != 0) | 
|  | 964 | goto wronglen; | 
|  | 965 | cmd_procpurge(); | 
|  | 966 | break; | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 967 | case LMK_GETKILLCNT: | 
|  | 968 | if (nargs != 2) | 
|  | 969 | goto wronglen; | 
|  | 970 | kill_cnt = cmd_getkillcnt(packet); | 
|  | 971 | len = lmkd_pack_set_getkillcnt_repl(packet, kill_cnt); | 
|  | 972 | if (ctrl_data_write(dsock_idx, (char *)packet, len) != len) | 
|  | 973 | return; | 
|  | 974 | break; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 975 | default: | 
|  | 976 | ALOGE("Received unknown command code %d", cmd); | 
|  | 977 | return; | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | return; | 
|  | 981 |  | 
|  | 982 | wronglen: | 
|  | 983 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); | 
|  | 984 | } | 
|  | 985 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 986 | static void ctrl_data_handler(int data, uint32_t events) { | 
|  | 987 | if (events & EPOLLIN) { | 
|  | 988 | ctrl_command_handler(data); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 989 | } | 
|  | 990 | } | 
|  | 991 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 992 | static int get_free_dsock() { | 
|  | 993 | for (int i = 0; i < MAX_DATA_CONN; i++) { | 
|  | 994 | if (data_sock[i].sock < 0) { | 
|  | 995 | return i; | 
|  | 996 | } | 
|  | 997 | } | 
|  | 998 | return -1; | 
|  | 999 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1000 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1001 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused) { | 
|  | 1002 | struct epoll_event epev; | 
|  | 1003 | int free_dscock_idx = get_free_dsock(); | 
|  | 1004 |  | 
|  | 1005 | if (free_dscock_idx < 0) { | 
|  | 1006 | /* | 
|  | 1007 | * Number of data connections exceeded max supported. This should not | 
|  | 1008 | * happen but if it does we drop all existing connections and accept | 
|  | 1009 | * the new one. This prevents inactive connections from monopolizing | 
|  | 1010 | * data socket and if we drop ActivityManager connection it will | 
|  | 1011 | * immediately reconnect. | 
|  | 1012 | */ | 
|  | 1013 | for (int i = 0; i < MAX_DATA_CONN; i++) { | 
|  | 1014 | ctrl_data_close(i); | 
|  | 1015 | } | 
|  | 1016 | free_dscock_idx = 0; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1017 | } | 
|  | 1018 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1019 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); | 
|  | 1020 | if (data_sock[free_dscock_idx].sock < 0) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1021 | ALOGE("lmkd control socket accept failed; errno=%d", errno); | 
|  | 1022 | return; | 
|  | 1023 | } | 
|  | 1024 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1025 | ALOGI("lmkd data connection established"); | 
|  | 1026 | /* use data to store data connection idx */ | 
|  | 1027 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; | 
|  | 1028 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1029 | epev.events = EPOLLIN; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1030 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); | 
|  | 1031 | 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] | 1032 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1033 | ctrl_data_close(free_dscock_idx); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1034 | return; | 
|  | 1035 | } | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1036 | maxevents++; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1037 | } | 
|  | 1038 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1039 | #ifdef LMKD_LOG_STATS | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1040 | 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] | 1041 | char key[LINE_MAX + 1]; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1042 | int64_t value; | 
|  | 1043 |  | 
| Greg Kaiser | f0da9b0 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 1044 | sscanf(line, "%" STRINGIFY(LINE_MAX) "s  %" SCNd64 "", key, &value); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1045 |  | 
|  | 1046 | if (strcmp(key, "total_") < 0) { | 
|  | 1047 | return; | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | if (!strcmp(key, "total_pgfault")) | 
|  | 1051 | mem_st->pgfault = value; | 
|  | 1052 | else if (!strcmp(key, "total_pgmajfault")) | 
|  | 1053 | mem_st->pgmajfault = value; | 
|  | 1054 | else if (!strcmp(key, "total_rss")) | 
|  | 1055 | mem_st->rss_in_bytes = value; | 
|  | 1056 | else if (!strcmp(key, "total_cache")) | 
|  | 1057 | mem_st->cache_in_bytes = value; | 
|  | 1058 | else if (!strcmp(key, "total_swap")) | 
|  | 1059 | mem_st->swap_in_bytes = value; | 
|  | 1060 | } | 
|  | 1061 |  | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1062 | static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid) { | 
| Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1063 | FILE *fp; | 
|  | 1064 | char buf[PATH_MAX]; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1065 |  | 
| Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1066 | snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1067 |  | 
| Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1068 | fp = fopen(buf, "r"); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1069 |  | 
| Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1070 | if (fp == NULL) { | 
|  | 1071 | ALOGE("%s open failed: %s", buf, strerror(errno)); | 
|  | 1072 | return -1; | 
|  | 1073 | } | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1074 |  | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1075 | while (fgets(buf, PAGE_SIZE, fp) != NULL) { | 
| Suren Baghdasaryan | 1d1c002 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 1076 | memory_stat_parse_line(buf, mem_st); | 
|  | 1077 | } | 
|  | 1078 | fclose(fp); | 
|  | 1079 |  | 
|  | 1080 | return 0; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1081 | } | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1082 |  | 
|  | 1083 | static int memory_stat_from_procfs(struct memory_stat* mem_st, int pid) { | 
|  | 1084 | char path[PATH_MAX]; | 
|  | 1085 | char buffer[PROC_STAT_BUFFER_SIZE]; | 
|  | 1086 | int fd, ret; | 
|  | 1087 |  | 
|  | 1088 | snprintf(path, sizeof(path), PROC_STAT_FILE_PATH, pid); | 
|  | 1089 | if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0) { | 
|  | 1090 | ALOGE("%s open failed: %s", path, strerror(errno)); | 
|  | 1091 | return -1; | 
|  | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | ret = read(fd, buffer, sizeof(buffer)); | 
|  | 1095 | if (ret < 0) { | 
|  | 1096 | ALOGE("%s read failed: %s", path, strerror(errno)); | 
|  | 1097 | close(fd); | 
|  | 1098 | return -1; | 
|  | 1099 | } | 
|  | 1100 | close(fd); | 
|  | 1101 |  | 
|  | 1102 | // field 10 is pgfault | 
|  | 1103 | // field 12 is pgmajfault | 
| Jim Blackler | 1417cdb | 2018-11-21 16:22:36 +0000 | [diff] [blame] | 1104 | // field 22 is starttime | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1105 | // field 24 is rss_in_pages | 
| Jim Blackler | 1417cdb | 2018-11-21 16:22:36 +0000 | [diff] [blame] | 1106 | int64_t pgfault = 0, pgmajfault = 0, starttime = 0, rss_in_pages = 0; | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1107 | if (sscanf(buffer, | 
|  | 1108 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %" SCNd64 " %*d " | 
|  | 1109 | "%" SCNd64 " %*d %*u %*u %*d %*d %*d %*d %*d %*d " | 
| Jim Blackler | 1417cdb | 2018-11-21 16:22:36 +0000 | [diff] [blame] | 1110 | "%" SCNd64 " %*d %" SCNd64 "", | 
|  | 1111 | &pgfault, &pgmajfault, &starttime, &rss_in_pages) != 4) { | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1112 | return -1; | 
|  | 1113 | } | 
|  | 1114 | mem_st->pgfault = pgfault; | 
|  | 1115 | mem_st->pgmajfault = pgmajfault; | 
|  | 1116 | mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE); | 
| Jim Blackler | 1417cdb | 2018-11-21 16:22:36 +0000 | [diff] [blame] | 1117 | mem_st->process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK)); | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1118 | return 0; | 
|  | 1119 | } | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1120 | #endif | 
|  | 1121 |  | 
| Suren Baghdasaryan | 8b9deaf | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 1122 | /* /prop/zoneinfo parsing routines */ | 
|  | 1123 | static int64_t zoneinfo_parse_protection(char *cp) { | 
|  | 1124 | int64_t max = 0; | 
|  | 1125 | long long zoneval; | 
|  | 1126 | char *save_ptr; | 
|  | 1127 |  | 
|  | 1128 | for (cp = strtok_r(cp, "(), ", &save_ptr); cp; | 
|  | 1129 | cp = strtok_r(NULL, "), ", &save_ptr)) { | 
|  | 1130 | zoneval = strtoll(cp, &cp, 0); | 
|  | 1131 | if (zoneval > max) { | 
|  | 1132 | max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval; | 
|  | 1133 | } | 
|  | 1134 | } | 
|  | 1135 |  | 
|  | 1136 | return max; | 
|  | 1137 | } | 
|  | 1138 |  | 
|  | 1139 | static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) { | 
|  | 1140 | char *cp = line; | 
|  | 1141 | char *ap; | 
|  | 1142 | char *save_ptr; | 
|  | 1143 | int64_t val; | 
|  | 1144 | int field_idx; | 
|  | 1145 |  | 
|  | 1146 | cp = strtok_r(line, " ", &save_ptr); | 
|  | 1147 | if (!cp) { | 
|  | 1148 | return true; | 
|  | 1149 | } | 
|  | 1150 |  | 
|  | 1151 | if (!strcmp(cp, "protection:")) { | 
|  | 1152 | ap = strtok_r(NULL, ")", &save_ptr); | 
|  | 1153 | } else { | 
|  | 1154 | ap = strtok_r(NULL, " ", &save_ptr); | 
|  | 1155 | } | 
|  | 1156 |  | 
|  | 1157 | if (!ap) { | 
|  | 1158 | return true; | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | switch (match_field(cp, ap, zoneinfo_field_names, | 
|  | 1162 | ZI_FIELD_COUNT, &val, &field_idx)) { | 
|  | 1163 | case (PARSE_SUCCESS): | 
|  | 1164 | zi->arr[field_idx] += val; | 
|  | 1165 | break; | 
|  | 1166 | case (NO_MATCH): | 
|  | 1167 | if (!strcmp(cp, "protection:")) { | 
|  | 1168 | zi->field.totalreserve_pages += | 
|  | 1169 | zoneinfo_parse_protection(ap); | 
|  | 1170 | } | 
|  | 1171 | break; | 
|  | 1172 | case (PARSE_FAIL): | 
|  | 1173 | default: | 
|  | 1174 | return false; | 
|  | 1175 | } | 
|  | 1176 | return true; | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | static int zoneinfo_parse(union zoneinfo *zi) { | 
|  | 1180 | static struct reread_data file_data = { | 
|  | 1181 | .filename = ZONEINFO_PATH, | 
|  | 1182 | .fd = -1, | 
|  | 1183 | }; | 
|  | 1184 | char buf[PAGE_SIZE]; | 
|  | 1185 | char *save_ptr; | 
|  | 1186 | char *line; | 
|  | 1187 |  | 
|  | 1188 | memset(zi, 0, sizeof(union zoneinfo)); | 
|  | 1189 |  | 
|  | 1190 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { | 
|  | 1191 | return -1; | 
|  | 1192 | } | 
|  | 1193 |  | 
|  | 1194 | for (line = strtok_r(buf, "\n", &save_ptr); line; | 
|  | 1195 | line = strtok_r(NULL, "\n", &save_ptr)) { | 
|  | 1196 | if (!zoneinfo_parse_line(line, zi)) { | 
|  | 1197 | ALOGE("%s parse error", file_data.filename); | 
|  | 1198 | return -1; | 
|  | 1199 | } | 
|  | 1200 | } | 
|  | 1201 | zi->field.totalreserve_pages += zi->field.high; | 
|  | 1202 |  | 
|  | 1203 | return 0; | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | /* /prop/meminfo parsing routines */ | 
|  | 1207 | static bool meminfo_parse_line(char *line, union meminfo *mi) { | 
|  | 1208 | char *cp = line; | 
|  | 1209 | char *ap; | 
|  | 1210 | char *save_ptr; | 
|  | 1211 | int64_t val; | 
|  | 1212 | int field_idx; | 
|  | 1213 | enum field_match_result match_res; | 
|  | 1214 |  | 
|  | 1215 | cp = strtok_r(line, " ", &save_ptr); | 
|  | 1216 | if (!cp) { | 
|  | 1217 | return false; | 
|  | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | ap = strtok_r(NULL, " ", &save_ptr); | 
|  | 1221 | if (!ap) { | 
|  | 1222 | return false; | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT, | 
|  | 1226 | &val, &field_idx); | 
|  | 1227 | if (match_res == PARSE_SUCCESS) { | 
|  | 1228 | mi->arr[field_idx] = val / page_k; | 
|  | 1229 | } | 
|  | 1230 | return (match_res != PARSE_FAIL); | 
|  | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | static int meminfo_parse(union meminfo *mi) { | 
|  | 1234 | static struct reread_data file_data = { | 
|  | 1235 | .filename = MEMINFO_PATH, | 
|  | 1236 | .fd = -1, | 
|  | 1237 | }; | 
|  | 1238 | char buf[PAGE_SIZE]; | 
|  | 1239 | char *save_ptr; | 
|  | 1240 | char *line; | 
|  | 1241 |  | 
|  | 1242 | memset(mi, 0, sizeof(union meminfo)); | 
|  | 1243 |  | 
|  | 1244 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { | 
|  | 1245 | return -1; | 
|  | 1246 | } | 
|  | 1247 |  | 
|  | 1248 | for (line = strtok_r(buf, "\n", &save_ptr); line; | 
|  | 1249 | line = strtok_r(NULL, "\n", &save_ptr)) { | 
|  | 1250 | if (!meminfo_parse_line(line, mi)) { | 
|  | 1251 | ALOGE("%s parse error", file_data.filename); | 
|  | 1252 | return -1; | 
|  | 1253 | } | 
|  | 1254 | } | 
|  | 1255 | mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + | 
|  | 1256 | mi->field.buffers; | 
|  | 1257 |  | 
|  | 1258 | return 0; | 
|  | 1259 | } | 
|  | 1260 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 1261 | static void meminfo_log(union meminfo *mi) { | 
|  | 1262 | for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) { | 
|  | 1263 | android_log_write_int32(ctx, (int32_t)min(mi->arr[field_idx] * page_k, INT32_MAX)); | 
|  | 1264 | } | 
|  | 1265 |  | 
|  | 1266 | android_log_write_list(ctx, LOG_ID_EVENTS); | 
|  | 1267 | android_log_reset(ctx); | 
|  | 1268 | } | 
|  | 1269 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1270 | static int proc_get_size(int pid) { | 
|  | 1271 | char path[PATH_MAX]; | 
|  | 1272 | char line[LINE_MAX]; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1273 | int fd; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1274 | int rss = 0; | 
|  | 1275 | int total; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1276 | ssize_t ret; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1277 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1278 | /* gid containing AID_READPROC required */ | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1279 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1280 | fd = open(path, O_RDONLY | O_CLOEXEC); | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1281 | if (fd == -1) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1282 | return -1; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1283 |  | 
|  | 1284 | ret = read_all(fd, line, sizeof(line) - 1); | 
|  | 1285 | if (ret < 0) { | 
|  | 1286 | close(fd); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1287 | return -1; | 
|  | 1288 | } | 
|  | 1289 |  | 
|  | 1290 | sscanf(line, "%d %d ", &total, &rss); | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1291 | close(fd); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1292 | return rss; | 
|  | 1293 | } | 
|  | 1294 |  | 
|  | 1295 | static char *proc_get_name(int pid) { | 
|  | 1296 | char path[PATH_MAX]; | 
|  | 1297 | static char line[LINE_MAX]; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1298 | int fd; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1299 | char *cp; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1300 | ssize_t ret; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1301 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1302 | /* gid containing AID_READPROC required */ | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1303 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1304 | fd = open(path, O_RDONLY | O_CLOEXEC); | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1305 | if (fd == -1) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1306 | return NULL; | 
| Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 1307 | ret = read_all(fd, line, sizeof(line) - 1); | 
|  | 1308 | close(fd); | 
|  | 1309 | if (ret < 0) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1310 | return NULL; | 
|  | 1311 | } | 
|  | 1312 |  | 
|  | 1313 | cp = strchr(line, ' '); | 
|  | 1314 | if (cp) | 
|  | 1315 | *cp = '\0'; | 
|  | 1316 |  | 
|  | 1317 | return line; | 
|  | 1318 | } | 
|  | 1319 |  | 
|  | 1320 | static struct proc *proc_adj_lru(int oomadj) { | 
|  | 1321 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); | 
|  | 1322 | } | 
|  | 1323 |  | 
| Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1324 | static struct proc *proc_get_heaviest(int oomadj) { | 
|  | 1325 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; | 
|  | 1326 | struct adjslot_list *curr = head->next; | 
|  | 1327 | struct proc *maxprocp = NULL; | 
|  | 1328 | int maxsize = 0; | 
|  | 1329 | while (curr != head) { | 
|  | 1330 | int pid = ((struct proc *)curr)->pid; | 
|  | 1331 | int tasksize = proc_get_size(pid); | 
|  | 1332 | if (tasksize <= 0) { | 
|  | 1333 | struct adjslot_list *next = curr->next; | 
|  | 1334 | pid_remove(pid); | 
|  | 1335 | curr = next; | 
|  | 1336 | } else { | 
|  | 1337 | if (tasksize > maxsize) { | 
|  | 1338 | maxsize = tasksize; | 
|  | 1339 | maxprocp = (struct proc *)curr; | 
|  | 1340 | } | 
|  | 1341 | curr = curr->next; | 
|  | 1342 | } | 
|  | 1343 | } | 
|  | 1344 | return maxprocp; | 
|  | 1345 | } | 
|  | 1346 |  | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 1347 | static void set_process_group_and_prio(int pid, SchedPolicy sp, int prio) { | 
|  | 1348 | DIR* d; | 
|  | 1349 | char proc_path[PATH_MAX]; | 
|  | 1350 | struct dirent* de; | 
|  | 1351 |  | 
|  | 1352 | snprintf(proc_path, sizeof(proc_path), "/proc/%d/task", pid); | 
|  | 1353 | if (!(d = opendir(proc_path))) { | 
|  | 1354 | ALOGW("Failed to open %s; errno=%d: process pid(%d) might have died", proc_path, errno, | 
|  | 1355 | pid); | 
|  | 1356 | return; | 
|  | 1357 | } | 
|  | 1358 |  | 
|  | 1359 | while ((de = readdir(d))) { | 
|  | 1360 | int t_pid; | 
|  | 1361 |  | 
|  | 1362 | if (de->d_name[0] == '.') continue; | 
|  | 1363 | t_pid = atoi(de->d_name); | 
|  | 1364 |  | 
|  | 1365 | if (!t_pid) { | 
|  | 1366 | ALOGW("Failed to get t_pid for '%s' of pid(%d)", de->d_name, pid); | 
|  | 1367 | continue; | 
|  | 1368 | } | 
|  | 1369 |  | 
|  | 1370 | if (setpriority(PRIO_PROCESS, t_pid, prio) && errno != ESRCH) { | 
|  | 1371 | ALOGW("Unable to raise priority of killing t_pid (%d): errno=%d", t_pid, errno); | 
|  | 1372 | } | 
|  | 1373 |  | 
|  | 1374 | if (set_cpuset_policy(t_pid, sp)) { | 
|  | 1375 | ALOGW("Failed to set_cpuset_policy on pid(%d) t_pid(%d) to %d", pid, t_pid, (int)sp); | 
|  | 1376 | continue; | 
|  | 1377 | } | 
|  | 1378 | } | 
|  | 1379 | closedir(d); | 
|  | 1380 | } | 
|  | 1381 |  | 
| Tim Murray | e7853f6 | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 1382 | static int last_killed_pid = -1; | 
|  | 1383 |  | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1384 | /* Kill one process specified by procp.  Returns the size of the process killed */ | 
| Suren Baghdasaryan | ec5e4c6 | 2019-03-04 11:07:39 -0800 | [diff] [blame] | 1385 | static int kill_one_process(struct proc* procp, int min_oom_score) { | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1386 | int pid = procp->pid; | 
|  | 1387 | uid_t uid = procp->uid; | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1388 | int tgid; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1389 | char *taskname; | 
|  | 1390 | int tasksize; | 
|  | 1391 | int r; | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1392 | int result = -1; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1393 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1394 | #ifdef LMKD_LOG_STATS | 
| Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 1395 | struct memory_stat mem_st = {}; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1396 | int memory_stat_parse_result = -1; | 
| Suren Baghdasaryan | ec5e4c6 | 2019-03-04 11:07:39 -0800 | [diff] [blame] | 1397 | #else | 
|  | 1398 | /* To prevent unused parameter warning */ | 
|  | 1399 | (void)(min_oom_score); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1400 | #endif | 
|  | 1401 |  | 
| Suren Baghdasaryan | 0082ef1 | 2019-07-02 15:52:07 -0700 | [diff] [blame] | 1402 | tgid = proc_get_tgid(pid); | 
|  | 1403 | if (tgid >= 0 && tgid != pid) { | 
|  | 1404 | ALOGE("Possible pid reuse detected (pid %d, tgid %d)!", pid, tgid); | 
|  | 1405 | goto out; | 
|  | 1406 | } | 
|  | 1407 |  | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1408 | taskname = proc_get_name(pid); | 
|  | 1409 | if (!taskname) { | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1410 | goto out; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | tasksize = proc_get_size(pid); | 
|  | 1414 | if (tasksize <= 0) { | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1415 | goto out; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1416 | } | 
|  | 1417 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1418 | #ifdef LMKD_LOG_STATS | 
|  | 1419 | if (enable_stats_log) { | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1420 | if (per_app_memcg) { | 
|  | 1421 | memory_stat_parse_result = memory_stat_from_cgroup(&mem_st, pid, uid); | 
|  | 1422 | } else { | 
|  | 1423 | memory_stat_parse_result = memory_stat_from_procfs(&mem_st, pid); | 
|  | 1424 | } | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1425 | } | 
|  | 1426 | #endif | 
|  | 1427 |  | 
| Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1428 | TRACE_KILL_START(pid); | 
|  | 1429 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1430 | /* CAP_KILL required */ | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1431 | r = kill(pid, SIGKILL); | 
| Wei Wang | 2d95c10 | 2018-11-21 00:11:44 -0800 | [diff] [blame] | 1432 |  | 
|  | 1433 | set_process_group_and_prio(pid, SP_FOREGROUND, ANDROID_PRIORITY_HIGHEST); | 
|  | 1434 |  | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 1435 | inc_killcnt(procp->oomadj); | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1436 | ALOGI("Kill '%s' (%d), uid %d, oom_adj %d to free %ldkB", | 
|  | 1437 | taskname, pid, uid, procp->oomadj, tasksize * page_k); | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1438 |  | 
| Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1439 | TRACE_KILL_END(); | 
|  | 1440 |  | 
| Tim Murray | e7853f6 | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 1441 | last_killed_pid = pid; | 
|  | 1442 |  | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1443 | if (r) { | 
| Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1444 | ALOGE("kill(%d): errno=%d", pid, errno); | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1445 | goto out; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1446 | } else { | 
|  | 1447 | #ifdef LMKD_LOG_STATS | 
|  | 1448 | if (memory_stat_parse_result == 0) { | 
|  | 1449 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, | 
|  | 1450 | procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes, | 
| Suren Baghdasaryan | ec5e4c6 | 2019-03-04 11:07:39 -0800 | [diff] [blame] | 1451 | mem_st.cache_in_bytes, mem_st.swap_in_bytes, mem_st.process_start_time_ns, | 
|  | 1452 | min_oom_score); | 
| Rajeev Kumar | 4dbc24d | 2018-10-05 12:34:59 -0700 | [diff] [blame] | 1453 | } else if (enable_stats_log) { | 
|  | 1454 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, procp->oomadj, | 
| Suren Baghdasaryan | ec5e4c6 | 2019-03-04 11:07:39 -0800 | [diff] [blame] | 1455 | -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1, -1, | 
|  | 1456 | min_oom_score); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1457 | } | 
|  | 1458 | #endif | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1459 | result = tasksize; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1460 | } | 
| Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1461 |  | 
| Suren Baghdasaryan | 0106327 | 2018-10-12 11:28:33 -0700 | [diff] [blame] | 1462 | out: | 
|  | 1463 | /* | 
|  | 1464 | * WARNING: After pid_remove() procp is freed and can't be used! | 
|  | 1465 | * Therefore placed at the end of the function. | 
|  | 1466 | */ | 
|  | 1467 | pid_remove(pid); | 
|  | 1468 | return result; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1469 | } | 
|  | 1470 |  | 
|  | 1471 | /* | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1472 | * Find one process to kill at or above the given oom_adj level. | 
|  | 1473 | * Returns size of the killed process. | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1474 | */ | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1475 | static int find_and_kill_process(int min_score_adj) { | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1476 | int i; | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1477 | int killed_size = 0; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1478 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1479 | #ifdef LMKD_LOG_STATS | 
| Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1480 | bool lmk_state_change_start = false; | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1481 | #endif | 
|  | 1482 |  | 
| Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1483 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1484 | struct proc *procp; | 
|  | 1485 |  | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1486 | while (true) { | 
| Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1487 | procp = kill_heaviest_task ? | 
|  | 1488 | proc_get_heaviest(i) : proc_adj_lru(i); | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1489 |  | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1490 | if (!procp) | 
|  | 1491 | break; | 
|  | 1492 |  | 
| Suren Baghdasaryan | ec5e4c6 | 2019-03-04 11:07:39 -0800 | [diff] [blame] | 1493 | killed_size = kill_one_process(procp, min_score_adj); | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1494 | if (killed_size >= 0) { | 
| Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1495 | #ifdef LMKD_LOG_STATS | 
|  | 1496 | if (enable_stats_log && !lmk_state_change_start) { | 
|  | 1497 | lmk_state_change_start = true; | 
|  | 1498 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, | 
|  | 1499 | LMK_STATE_CHANGE_START); | 
|  | 1500 | } | 
|  | 1501 | #endif | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1502 | break; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1503 | } | 
|  | 1504 | } | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1505 | if (killed_size) { | 
|  | 1506 | break; | 
|  | 1507 | } | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1508 | } | 
|  | 1509 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1510 | #ifdef LMKD_LOG_STATS | 
| Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1511 | if (enable_stats_log && lmk_state_change_start) { | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1512 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP); | 
|  | 1513 | } | 
|  | 1514 | #endif | 
|  | 1515 |  | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1516 | return killed_size; | 
| Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1517 | } | 
|  | 1518 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1519 | static int64_t get_memory_usage(struct reread_data *file_data) { | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1520 | int ret; | 
|  | 1521 | int64_t mem_usage; | 
|  | 1522 | char buf[32]; | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1523 |  | 
|  | 1524 | if (reread_file(file_data, buf, sizeof(buf)) < 0) { | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1525 | return -1; | 
|  | 1526 | } | 
|  | 1527 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1528 | if (!parse_int64(buf, &mem_usage)) { | 
|  | 1529 | ALOGE("%s parse error", file_data->filename); | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1530 | return -1; | 
|  | 1531 | } | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1532 | if (mem_usage == 0) { | 
|  | 1533 | ALOGE("No memory!"); | 
|  | 1534 | return -1; | 
|  | 1535 | } | 
|  | 1536 | return mem_usage; | 
|  | 1537 | } | 
|  | 1538 |  | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1539 | void record_low_pressure_levels(union meminfo *mi) { | 
|  | 1540 | if (low_pressure_mem.min_nr_free_pages == -1 || | 
|  | 1541 | low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) { | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1542 | if (debug_process_killing) { | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1543 | ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64, | 
|  | 1544 | low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages); | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1545 | } | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1546 | low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages; | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1547 | } | 
|  | 1548 | /* | 
|  | 1549 | * Free memory at low vmpressure events occasionally gets spikes, | 
|  | 1550 | * possibly a stale low vmpressure event with memory already | 
|  | 1551 | * freed up (no memory pressure should have been reported). | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1552 | * 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] | 1553 | */ | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1554 | if (low_pressure_mem.max_nr_free_pages == -1 || | 
|  | 1555 | (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages && | 
|  | 1556 | mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages < | 
|  | 1557 | low_pressure_mem.max_nr_free_pages * 0.1)) { | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1558 | if (debug_process_killing) { | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1559 | ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64, | 
|  | 1560 | low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages); | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1561 | } | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1562 | low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages; | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1563 | } | 
|  | 1564 | } | 
|  | 1565 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1566 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { | 
|  | 1567 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? | 
|  | 1568 | level + 1 : level); | 
|  | 1569 | } | 
|  | 1570 |  | 
|  | 1571 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { | 
|  | 1572 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? | 
|  | 1573 | level - 1 : level); | 
|  | 1574 | } | 
|  | 1575 |  | 
| Tim Murray | e7853f6 | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 1576 | static bool is_kill_pending(void) { | 
|  | 1577 | char buf[24]; | 
|  | 1578 |  | 
|  | 1579 | if (last_killed_pid < 0) { | 
|  | 1580 | return false; | 
|  | 1581 | } | 
|  | 1582 |  | 
|  | 1583 | snprintf(buf, sizeof(buf), "/proc/%d/", last_killed_pid); | 
|  | 1584 | if (access(buf, F_OK) == 0) { | 
|  | 1585 | return true; | 
|  | 1586 | } | 
|  | 1587 |  | 
|  | 1588 | // reset last killed PID because there's nothing pending | 
|  | 1589 | last_killed_pid = -1; | 
|  | 1590 | return false; | 
|  | 1591 | } | 
|  | 1592 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1593 | static void mp_event_common(int data, uint32_t events __unused) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1594 | int ret; | 
|  | 1595 | unsigned long long evcount; | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1596 | int64_t mem_usage, memsw_usage; | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1597 | int64_t mem_pressure; | 
| Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1598 | enum vmpressure_level lvl; | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1599 | union meminfo mi; | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1600 | union zoneinfo zi; | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1601 | struct timespec curr_tm; | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1602 | static struct timespec last_kill_tm; | 
|  | 1603 | static unsigned long kill_skip_count = 0; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1604 | enum vmpressure_level level = (enum vmpressure_level)data; | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1605 | long other_free = 0, other_file = 0; | 
|  | 1606 | int min_score_adj; | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1607 | int minfree = 0; | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1608 | static struct reread_data mem_usage_file_data = { | 
|  | 1609 | .filename = MEMCG_MEMORY_USAGE, | 
|  | 1610 | .fd = -1, | 
|  | 1611 | }; | 
|  | 1612 | static struct reread_data memsw_usage_file_data = { | 
|  | 1613 | .filename = MEMCG_MEMORYSW_USAGE, | 
|  | 1614 | .fd = -1, | 
|  | 1615 | }; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1616 |  | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 1617 | if (debug_process_killing) { | 
|  | 1618 | ALOGI("%s memory pressure event is triggered", level_name[level]); | 
|  | 1619 | } | 
|  | 1620 |  | 
|  | 1621 | if (!use_psi_monitors) { | 
|  | 1622 | /* | 
|  | 1623 | * Check all event counters from low to critical | 
|  | 1624 | * and upgrade to the highest priority one. By reading | 
|  | 1625 | * eventfd we also reset the event counters. | 
|  | 1626 | */ | 
|  | 1627 | for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { | 
|  | 1628 | if (mpevfd[lvl] != -1 && | 
|  | 1629 | TEMP_FAILURE_RETRY(read(mpevfd[lvl], | 
|  | 1630 | &evcount, sizeof(evcount))) > 0 && | 
|  | 1631 | evcount > 0 && lvl > level) { | 
|  | 1632 | level = lvl; | 
|  | 1633 | } | 
| Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1634 | } | 
|  | 1635 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1636 |  | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1637 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { | 
|  | 1638 | ALOGE("Failed to get current time"); | 
|  | 1639 | return; | 
|  | 1640 | } | 
|  | 1641 |  | 
| Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1642 | if (kill_timeout_ms) { | 
| Tim Murray | e7853f6 | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 1643 | // If we're within the timeout, see if there's pending reclaim work | 
|  | 1644 | // from the last killed process. If there is (as evidenced by | 
|  | 1645 | // /proc/<pid> continuing to exist), skip killing for now. | 
|  | 1646 | if ((get_time_diff_ms(&last_kill_tm, &curr_tm) < kill_timeout_ms) && | 
|  | 1647 | (low_ram_device || is_kill_pending())) { | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1648 | kill_skip_count++; | 
| Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1649 | return; | 
|  | 1650 | } | 
|  | 1651 | } | 
|  | 1652 |  | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1653 | if (kill_skip_count > 0) { | 
| Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1654 | ALOGI("%lu memory pressure events were skipped after a kill!", | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1655 | kill_skip_count); | 
|  | 1656 | kill_skip_count = 0; | 
| Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1657 | } | 
|  | 1658 |  | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1659 | if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) { | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1660 | ALOGE("Failed to get free memory!"); | 
|  | 1661 | return; | 
|  | 1662 | } | 
|  | 1663 |  | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1664 | if (use_minfree_levels) { | 
|  | 1665 | int i; | 
|  | 1666 |  | 
|  | 1667 | other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages; | 
|  | 1668 | if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) { | 
|  | 1669 | other_file = (mi.field.nr_file_pages - mi.field.shmem - | 
|  | 1670 | mi.field.unevictable - mi.field.swap_cached); | 
|  | 1671 | } else { | 
|  | 1672 | other_file = 0; | 
|  | 1673 | } | 
|  | 1674 |  | 
|  | 1675 | min_score_adj = OOM_SCORE_ADJ_MAX + 1; | 
|  | 1676 | for (i = 0; i < lowmem_targets_size; i++) { | 
|  | 1677 | minfree = lowmem_minfree[i]; | 
|  | 1678 | if (other_free < minfree && other_file < minfree) { | 
|  | 1679 | min_score_adj = lowmem_adj[i]; | 
|  | 1680 | break; | 
|  | 1681 | } | 
|  | 1682 | } | 
|  | 1683 |  | 
| Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1684 | if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { | 
|  | 1685 | if (debug_process_killing) { | 
|  | 1686 | ALOGI("Ignore %s memory pressure event " | 
|  | 1687 | "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)", | 
|  | 1688 | level_name[level], other_free * page_k, other_file * page_k, | 
|  | 1689 | (long)lowmem_minfree[lowmem_targets_size - 1] * page_k); | 
|  | 1690 | } | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1691 | return; | 
| Suren Baghdasaryan | 20686f0 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1692 | } | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1693 |  | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1694 | goto do_kill; | 
|  | 1695 | } | 
|  | 1696 |  | 
| Suren Baghdasaryan | 9926e57 | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1697 | if (level == VMPRESS_LEVEL_LOW) { | 
|  | 1698 | record_low_pressure_levels(&mi); | 
|  | 1699 | } | 
|  | 1700 |  | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1701 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { | 
|  | 1702 | /* Do not monitor this pressure level */ | 
|  | 1703 | return; | 
|  | 1704 | } | 
|  | 1705 |  | 
| Suren Baghdasaryan | 6499e5e | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1706 | if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) { | 
|  | 1707 | goto do_kill; | 
|  | 1708 | } | 
|  | 1709 | if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) { | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1710 | goto do_kill; | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1711 | } | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1712 |  | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1713 | // Calculate percent for swappinness. | 
|  | 1714 | mem_pressure = (mem_usage * 100) / memsw_usage; | 
|  | 1715 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1716 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1717 | // We are swapping too much. | 
|  | 1718 | if (mem_pressure < upgrade_pressure) { | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1719 | level = upgrade_level(level); | 
|  | 1720 | if (debug_process_killing) { | 
|  | 1721 | ALOGI("Event upgraded to %s", level_name[level]); | 
|  | 1722 | } | 
| Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1723 | } | 
|  | 1724 | } | 
|  | 1725 |  | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 1726 | // If we still have enough swap space available, check if we want to | 
|  | 1727 | // ignore/downgrade pressure events. | 
|  | 1728 | if (mi.field.free_swap >= | 
|  | 1729 | mi.field.total_swap * swap_free_low_percentage / 100) { | 
|  | 1730 | // If the pressure is larger than downgrade_pressure lmk will not | 
|  | 1731 | // kill any process, since enough memory is available. | 
|  | 1732 | if (mem_pressure > downgrade_pressure) { | 
|  | 1733 | if (debug_process_killing) { | 
|  | 1734 | ALOGI("Ignore %s memory pressure", level_name[level]); | 
|  | 1735 | } | 
|  | 1736 | return; | 
|  | 1737 | } else if (level == VMPRESS_LEVEL_CRITICAL && mem_pressure > upgrade_pressure) { | 
|  | 1738 | if (debug_process_killing) { | 
|  | 1739 | ALOGI("Downgrade critical memory pressure"); | 
|  | 1740 | } | 
|  | 1741 | // Downgrade event, since enough memory available. | 
|  | 1742 | level = downgrade_level(level); | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1743 | } | 
| Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1744 | } | 
|  | 1745 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1746 | do_kill: | 
| Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1747 | if (low_ram_device) { | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1748 | /* For Go devices kill only one task */ | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1749 | if (find_and_kill_process(level_oomadj[level]) == 0) { | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1750 | if (debug_process_killing) { | 
|  | 1751 | ALOGI("Nothing to kill"); | 
|  | 1752 | } | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 1753 | } else { | 
|  | 1754 | meminfo_log(&mi); | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1755 | } | 
|  | 1756 | } else { | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1757 | int pages_freed; | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1758 | static struct timespec last_report_tm; | 
|  | 1759 | static unsigned long report_skip_count = 0; | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1760 |  | 
|  | 1761 | if (!use_minfree_levels) { | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1762 | /* Free up enough memory to downgrate the memory pressure to low level */ | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1763 | if (mi.field.nr_free_pages >= low_pressure_mem.max_nr_free_pages) { | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1764 | if (debug_process_killing) { | 
|  | 1765 | ALOGI("Ignoring pressure since more memory is " | 
|  | 1766 | "available (%" PRId64 ") than watermark (%" PRId64 ")", | 
|  | 1767 | mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages); | 
|  | 1768 | } | 
|  | 1769 | return; | 
|  | 1770 | } | 
|  | 1771 | min_score_adj = level_oomadj[level]; | 
| Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1772 | } | 
|  | 1773 |  | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1774 | pages_freed = find_and_kill_process(min_score_adj); | 
| Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1775 |  | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1776 | if (pages_freed == 0) { | 
|  | 1777 | /* Rate limit kill reports when nothing was reclaimed */ | 
|  | 1778 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < FAIL_REPORT_RLIMIT_MS) { | 
|  | 1779 | report_skip_count++; | 
| Suren Baghdasaryan | 314a505 | 2018-07-24 17:13:06 -0700 | [diff] [blame] | 1780 | return; | 
|  | 1781 | } | 
| Tim Murray | e7853f6 | 2018-10-25 17:05:41 -0700 | [diff] [blame] | 1782 | } else { | 
|  | 1783 | /* If we killed anything, update the last killed timestamp. */ | 
|  | 1784 | last_kill_tm = curr_tm; | 
| Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1785 | } | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1786 |  | 
|  | 1787 | /* Log meminfo whenever we kill or when report rate limit allows */ | 
|  | 1788 | meminfo_log(&mi); | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1789 |  | 
|  | 1790 | if (use_minfree_levels) { | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1791 | ALOGI("Reclaimed %ldkB, cache(%ldkB) and " | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1792 | "free(%" PRId64 "kB)-reserved(%" PRId64 "kB) below min(%ldkB) for oom_adj %d", | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1793 | pages_freed * page_k, | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1794 | other_file * page_k, mi.field.nr_free_pages * page_k, | 
|  | 1795 | zi.field.totalreserve_pages * page_k, | 
|  | 1796 | minfree * page_k, min_score_adj); | 
|  | 1797 | } else { | 
| Suren Baghdasaryan | f81b5f4 | 2018-10-26 11:32:15 -0700 | [diff] [blame] | 1798 | ALOGI("Reclaimed %ldkB at oom_adj %d", | 
|  | 1799 | pages_freed * page_k, min_score_adj); | 
| Suren Baghdasaryan | 3693441 | 2018-09-05 15:46:32 -0700 | [diff] [blame] | 1800 | } | 
|  | 1801 |  | 
|  | 1802 | if (report_skip_count > 0) { | 
|  | 1803 | ALOGI("Suppressed %lu failed kill reports", report_skip_count); | 
|  | 1804 | report_skip_count = 0; | 
|  | 1805 | } | 
|  | 1806 |  | 
|  | 1807 | last_report_tm = curr_tm; | 
| Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 1808 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1809 | } | 
|  | 1810 |  | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 1811 | static bool init_mp_psi(enum vmpressure_level level) { | 
|  | 1812 | int fd = init_psi_monitor(psi_thresholds[level].stall_type, | 
|  | 1813 | psi_thresholds[level].threshold_ms * US_PER_MS, | 
|  | 1814 | PSI_WINDOW_SIZE_MS * US_PER_MS); | 
|  | 1815 |  | 
|  | 1816 | if (fd < 0) { | 
|  | 1817 | return false; | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | vmpressure_hinfo[level].handler = mp_event_common; | 
|  | 1821 | vmpressure_hinfo[level].data = level; | 
|  | 1822 | if (register_psi_monitor(epollfd, fd, &vmpressure_hinfo[level]) < 0) { | 
|  | 1823 | destroy_psi_monitor(fd); | 
|  | 1824 | return false; | 
|  | 1825 | } | 
|  | 1826 | maxevents++; | 
|  | 1827 | mpevfd[level] = fd; | 
|  | 1828 |  | 
|  | 1829 | return true; | 
|  | 1830 | } | 
|  | 1831 |  | 
|  | 1832 | static void destroy_mp_psi(enum vmpressure_level level) { | 
|  | 1833 | int fd = mpevfd[level]; | 
|  | 1834 |  | 
|  | 1835 | if (unregister_psi_monitor(epollfd, fd) < 0) { | 
|  | 1836 | ALOGE("Failed to unregister psi monitor for %s memory pressure; errno=%d", | 
|  | 1837 | level_name[level], errno); | 
|  | 1838 | } | 
|  | 1839 | destroy_psi_monitor(fd); | 
|  | 1840 | mpevfd[level] = -1; | 
|  | 1841 | } | 
|  | 1842 |  | 
|  | 1843 | static bool init_psi_monitors() { | 
|  | 1844 | if (!init_mp_psi(VMPRESS_LEVEL_LOW)) { | 
|  | 1845 | return false; | 
|  | 1846 | } | 
|  | 1847 | if (!init_mp_psi(VMPRESS_LEVEL_MEDIUM)) { | 
|  | 1848 | destroy_mp_psi(VMPRESS_LEVEL_LOW); | 
|  | 1849 | return false; | 
|  | 1850 | } | 
|  | 1851 | if (!init_mp_psi(VMPRESS_LEVEL_CRITICAL)) { | 
|  | 1852 | destroy_mp_psi(VMPRESS_LEVEL_MEDIUM); | 
|  | 1853 | destroy_mp_psi(VMPRESS_LEVEL_LOW); | 
|  | 1854 | return false; | 
|  | 1855 | } | 
|  | 1856 | return true; | 
|  | 1857 | } | 
|  | 1858 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1859 | static bool init_mp_common(enum vmpressure_level level) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1860 | int mpfd; | 
|  | 1861 | int evfd; | 
|  | 1862 | int evctlfd; | 
|  | 1863 | char buf[256]; | 
|  | 1864 | struct epoll_event epev; | 
|  | 1865 | int ret; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1866 | int level_idx = (int)level; | 
|  | 1867 | const char *levelstr = level_name[level_idx]; | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1868 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1869 | /* gid containing AID_SYSTEM required */ | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1870 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1871 | if (mpfd < 0) { | 
|  | 1872 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); | 
|  | 1873 | goto err_open_mpfd; | 
|  | 1874 | } | 
|  | 1875 |  | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1876 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1877 | if (evctlfd < 0) { | 
|  | 1878 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); | 
|  | 1879 | goto err_open_evctlfd; | 
|  | 1880 | } | 
|  | 1881 |  | 
| Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1882 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1883 | if (evfd < 0) { | 
|  | 1884 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); | 
|  | 1885 | goto err_eventfd; | 
|  | 1886 | } | 
|  | 1887 |  | 
|  | 1888 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); | 
|  | 1889 | if (ret >= (ssize_t)sizeof(buf)) { | 
|  | 1890 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); | 
|  | 1891 | goto err; | 
|  | 1892 | } | 
|  | 1893 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1894 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1895 | if (ret == -1) { | 
|  | 1896 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", | 
|  | 1897 | levelstr, errno); | 
|  | 1898 | goto err; | 
|  | 1899 | } | 
|  | 1900 |  | 
|  | 1901 | epev.events = EPOLLIN; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1902 | /* use data to store event level */ | 
|  | 1903 | vmpressure_hinfo[level_idx].data = level_idx; | 
|  | 1904 | vmpressure_hinfo[level_idx].handler = mp_event_common; | 
|  | 1905 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1906 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); | 
|  | 1907 | if (ret == -1) { | 
|  | 1908 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); | 
|  | 1909 | goto err; | 
|  | 1910 | } | 
|  | 1911 | maxevents++; | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1912 | mpevfd[level] = evfd; | 
| Suren Baghdasaryan | 1bd2fc4 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 1913 | close(evctlfd); | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1914 | return true; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1915 |  | 
|  | 1916 | err: | 
|  | 1917 | close(evfd); | 
|  | 1918 | err_eventfd: | 
|  | 1919 | close(evctlfd); | 
|  | 1920 | err_open_evctlfd: | 
|  | 1921 | close(mpfd); | 
|  | 1922 | err_open_mpfd: | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1923 | return false; | 
| Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1924 | } | 
|  | 1925 |  | 
| Jim Blackler | 3947c93 | 2019-04-26 11:18:29 +0100 | [diff] [blame] | 1926 | #ifdef LMKD_LOG_STATS | 
|  | 1927 | static int kernel_poll_fd = -1; | 
|  | 1928 |  | 
|  | 1929 | static void poll_kernel() { | 
|  | 1930 | if (kernel_poll_fd == -1) { | 
|  | 1931 | // not waiting | 
|  | 1932 | return; | 
|  | 1933 | } | 
|  | 1934 |  | 
|  | 1935 | while (1) { | 
|  | 1936 | char rd_buf[256]; | 
|  | 1937 | int bytes_read = | 
|  | 1938 | TEMP_FAILURE_RETRY(pread(kernel_poll_fd, (void*)rd_buf, sizeof(rd_buf), 0)); | 
|  | 1939 | if (bytes_read <= 0) break; | 
|  | 1940 | rd_buf[bytes_read] = '\0'; | 
|  | 1941 |  | 
|  | 1942 | int64_t pid; | 
|  | 1943 | int64_t uid; | 
|  | 1944 | int64_t group_leader_pid; | 
|  | 1945 | int64_t min_flt; | 
|  | 1946 | int64_t maj_flt; | 
|  | 1947 | int64_t rss_in_pages; | 
|  | 1948 | int16_t oom_score_adj; | 
|  | 1949 | int16_t min_score_adj; | 
|  | 1950 | int64_t starttime; | 
|  | 1951 | char* taskname = 0; | 
|  | 1952 | int fields_read = sscanf(rd_buf, | 
|  | 1953 | "%" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 | 
|  | 1954 | " %" SCNd64 " %" SCNd16 " %" SCNd16 " %" SCNd64 "\n%m[^\n]", | 
|  | 1955 | &pid, &uid, &group_leader_pid, &min_flt, &maj_flt, &rss_in_pages, | 
|  | 1956 | &oom_score_adj, &min_score_adj, &starttime, &taskname); | 
|  | 1957 |  | 
|  | 1958 | /* only the death of the group leader process is logged */ | 
|  | 1959 | if (fields_read == 10 && group_leader_pid == pid) { | 
|  | 1960 | int64_t process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK)); | 
|  | 1961 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, oom_score_adj, | 
|  | 1962 | min_flt, maj_flt, rss_in_pages * PAGE_SIZE, 0, 0, | 
|  | 1963 | process_start_time_ns, min_score_adj); | 
|  | 1964 | } | 
|  | 1965 |  | 
|  | 1966 | free(taskname); | 
|  | 1967 | } | 
|  | 1968 | } | 
|  | 1969 |  | 
|  | 1970 | static struct event_handler_info kernel_poll_hinfo = {0, poll_kernel}; | 
|  | 1971 |  | 
|  | 1972 | static void init_poll_kernel() { | 
|  | 1973 | struct epoll_event epev; | 
|  | 1974 | kernel_poll_fd = | 
|  | 1975 | TEMP_FAILURE_RETRY(open("/proc/lowmemorykiller", O_RDONLY | O_NONBLOCK | O_CLOEXEC)); | 
|  | 1976 |  | 
|  | 1977 | if (kernel_poll_fd < 0) { | 
|  | 1978 | ALOGE("kernel lmk event file could not be opened; errno=%d", kernel_poll_fd); | 
|  | 1979 | return; | 
|  | 1980 | } | 
|  | 1981 |  | 
|  | 1982 | epev.events = EPOLLIN; | 
|  | 1983 | epev.data.ptr = (void*)&kernel_poll_hinfo; | 
|  | 1984 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, kernel_poll_fd, &epev) != 0) { | 
|  | 1985 | ALOGE("epoll_ctl for lmk events failed; errno=%d", errno); | 
|  | 1986 | close(kernel_poll_fd); | 
|  | 1987 | kernel_poll_fd = -1; | 
|  | 1988 | } else { | 
|  | 1989 | maxevents++; | 
|  | 1990 | } | 
|  | 1991 | } | 
|  | 1992 | #endif | 
|  | 1993 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1994 | static int init(void) { | 
|  | 1995 | struct epoll_event epev; | 
|  | 1996 | int i; | 
|  | 1997 | int ret; | 
|  | 1998 |  | 
|  | 1999 | page_k = sysconf(_SC_PAGESIZE); | 
|  | 2000 | if (page_k == -1) | 
|  | 2001 | page_k = PAGE_SIZE; | 
|  | 2002 | page_k /= 1024; | 
|  | 2003 |  | 
|  | 2004 | epollfd = epoll_create(MAX_EPOLL_EVENTS); | 
|  | 2005 | if (epollfd == -1) { | 
|  | 2006 | ALOGE("epoll_create failed (errno=%d)", errno); | 
|  | 2007 | return -1; | 
|  | 2008 | } | 
|  | 2009 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2010 | // mark data connections as not connected | 
|  | 2011 | for (int i = 0; i < MAX_DATA_CONN; i++) { | 
|  | 2012 | data_sock[i].sock = -1; | 
|  | 2013 | } | 
|  | 2014 |  | 
|  | 2015 | ctrl_sock.sock = android_get_control_socket("lmkd"); | 
|  | 2016 | if (ctrl_sock.sock < 0) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2017 | ALOGE("get lmkd control socket failed"); | 
|  | 2018 | return -1; | 
|  | 2019 | } | 
|  | 2020 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2021 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2022 | if (ret < 0) { | 
|  | 2023 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); | 
|  | 2024 | return -1; | 
|  | 2025 | } | 
|  | 2026 |  | 
|  | 2027 | epev.events = EPOLLIN; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2028 | ctrl_sock.handler_info.handler = ctrl_connect_handler; | 
|  | 2029 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); | 
|  | 2030 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2031 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); | 
|  | 2032 | return -1; | 
|  | 2033 | } | 
|  | 2034 | maxevents++; | 
|  | 2035 |  | 
| Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 2036 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); | 
| Suren Baghdasaryan | 979591b | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 2037 | use_inkernel_interface = has_inkernel_module; | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2038 |  | 
|  | 2039 | if (use_inkernel_interface) { | 
|  | 2040 | ALOGI("Using in-kernel low memory killer interface"); | 
| Jim Blackler | 3947c93 | 2019-04-26 11:18:29 +0100 | [diff] [blame] | 2041 | #ifdef LMKD_LOG_STATS | 
|  | 2042 | if (enable_stats_log) { | 
|  | 2043 | init_poll_kernel(); | 
|  | 2044 | } | 
|  | 2045 | #endif | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2046 | } else { | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2047 | /* Try to use psi monitor first if kernel has it */ | 
|  | 2048 | use_psi_monitors = property_get_bool("ro.lmk.use_psi", true) && | 
|  | 2049 | init_psi_monitors(); | 
|  | 2050 | /* Fall back to vmpressure */ | 
|  | 2051 | if (!use_psi_monitors && | 
|  | 2052 | (!init_mp_common(VMPRESS_LEVEL_LOW) || | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2053 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2054 | !init_mp_common(VMPRESS_LEVEL_CRITICAL))) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2055 | 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] | 2056 | return -1; | 
|  | 2057 | } | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2058 | if (use_psi_monitors) { | 
|  | 2059 | ALOGI("Using psi monitors for memory pressure detection"); | 
|  | 2060 | } else { | 
|  | 2061 | ALOGI("Using vmpressure for memory pressure detection"); | 
|  | 2062 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2063 | } | 
|  | 2064 |  | 
| Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 2065 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2066 | procadjslot_list[i].next = &procadjslot_list[i]; | 
|  | 2067 | procadjslot_list[i].prev = &procadjslot_list[i]; | 
|  | 2068 | } | 
|  | 2069 |  | 
| Suren Baghdasaryan | d4a2990 | 2018-10-12 11:07:40 -0700 | [diff] [blame] | 2070 | memset(killcnt_idx, KILLCNT_INVALID_IDX, sizeof(killcnt_idx)); | 
|  | 2071 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2072 | return 0; | 
|  | 2073 | } | 
|  | 2074 |  | 
|  | 2075 | static void mainloop(void) { | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2076 | struct event_handler_info* handler_info; | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2077 | struct event_handler_info* poll_handler = NULL; | 
|  | 2078 | struct timespec last_report_tm, curr_tm; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2079 | struct epoll_event *evt; | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2080 | long delay = -1; | 
|  | 2081 | int polling = 0; | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2082 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2083 | while (1) { | 
|  | 2084 | struct epoll_event events[maxevents]; | 
|  | 2085 | int nevents; | 
|  | 2086 | int i; | 
|  | 2087 |  | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2088 | if (polling) { | 
|  | 2089 | /* Calculate next timeout */ | 
|  | 2090 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); | 
|  | 2091 | delay = get_time_diff_ms(&last_report_tm, &curr_tm); | 
|  | 2092 | delay = (delay < PSI_POLL_PERIOD_MS) ? | 
|  | 2093 | PSI_POLL_PERIOD_MS - delay : PSI_POLL_PERIOD_MS; | 
|  | 2094 |  | 
|  | 2095 | /* Wait for events until the next polling timeout */ | 
|  | 2096 | nevents = epoll_wait(epollfd, events, maxevents, delay); | 
|  | 2097 |  | 
|  | 2098 | clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm); | 
|  | 2099 | if (get_time_diff_ms(&last_report_tm, &curr_tm) >= PSI_POLL_PERIOD_MS) { | 
| Greg Kaiser | a523fd6 | 2019-02-04 08:50:10 -0800 | [diff] [blame] | 2100 | polling--; | 
|  | 2101 | poll_handler->handler(poll_handler->data, 0); | 
|  | 2102 | last_report_tm = curr_tm; | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2103 | } | 
|  | 2104 | } else { | 
|  | 2105 | /* Wait for events with no timeout */ | 
|  | 2106 | nevents = epoll_wait(epollfd, events, maxevents, -1); | 
|  | 2107 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2108 |  | 
|  | 2109 | if (nevents == -1) { | 
|  | 2110 | if (errno == EINTR) | 
|  | 2111 | continue; | 
|  | 2112 | ALOGE("epoll_wait failed (errno=%d)", errno); | 
|  | 2113 | continue; | 
|  | 2114 | } | 
|  | 2115 |  | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2116 | /* | 
|  | 2117 | * First pass to see if any data socket connections were dropped. | 
|  | 2118 | * Dropped connection should be handled before any other events | 
|  | 2119 | * to deallocate data connection and correctly handle cases when | 
|  | 2120 | * connection gets dropped and reestablished in the same epoll cycle. | 
|  | 2121 | * In such cases it's essential to handle connection closures first. | 
|  | 2122 | */ | 
|  | 2123 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { | 
|  | 2124 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { | 
|  | 2125 | ALOGI("lmkd data connection dropped"); | 
|  | 2126 | handler_info = (struct event_handler_info*)evt->data.ptr; | 
|  | 2127 | ctrl_data_close(handler_info->data); | 
|  | 2128 | } | 
|  | 2129 | } | 
|  | 2130 |  | 
|  | 2131 | /* Second pass to handle all other events */ | 
|  | 2132 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { | 
|  | 2133 | if (evt->events & EPOLLERR) | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2134 | ALOGD("EPOLLERR on event #%d", i); | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2135 | if (evt->events & EPOLLHUP) { | 
|  | 2136 | /* This case was handled in the first pass */ | 
|  | 2137 | continue; | 
|  | 2138 | } | 
|  | 2139 | if (evt->data.ptr) { | 
|  | 2140 | handler_info = (struct event_handler_info*)evt->data.ptr; | 
|  | 2141 | handler_info->handler(handler_info->data, evt->events); | 
| Suren Baghdasaryan | 77122e5 | 2019-01-08 12:54:48 -0800 | [diff] [blame] | 2142 |  | 
|  | 2143 | if (use_psi_monitors && handler_info->handler == mp_event_common) { | 
|  | 2144 | /* | 
|  | 2145 | * Poll for the duration of PSI_WINDOW_SIZE_MS after the | 
|  | 2146 | * initial PSI event because psi events are rate-limited | 
|  | 2147 | * at one per sec. | 
|  | 2148 | */ | 
|  | 2149 | polling = PSI_POLL_COUNT; | 
|  | 2150 | poll_handler = handler_info; | 
|  | 2151 | clock_gettime(CLOCK_MONOTONIC_COARSE, &last_report_tm); | 
|  | 2152 | } | 
| Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 2153 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2154 | } | 
|  | 2155 | } | 
|  | 2156 | } | 
|  | 2157 |  | 
| Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 2158 | int main(int argc __unused, char **argv __unused) { | 
| Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 2159 | struct sched_param param = { | 
|  | 2160 | .sched_priority = 1, | 
|  | 2161 | }; | 
|  | 2162 |  | 
| Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 2163 | /* By default disable low level vmpressure events */ | 
|  | 2164 | level_oomadj[VMPRESS_LEVEL_LOW] = | 
|  | 2165 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); | 
|  | 2166 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = | 
|  | 2167 | property_get_int32("ro.lmk.medium", 800); | 
|  | 2168 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = | 
|  | 2169 | property_get_int32("ro.lmk.critical", 0); | 
| Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 2170 | debug_process_killing = property_get_bool("ro.lmk.debug", false); | 
| Suren Baghdasaryan | ad2fd91 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 2171 |  | 
|  | 2172 | /* By default disable upgrade/downgrade logic */ | 
|  | 2173 | enable_pressure_upgrade = | 
|  | 2174 | property_get_bool("ro.lmk.critical_upgrade", false); | 
|  | 2175 | upgrade_pressure = | 
|  | 2176 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); | 
|  | 2177 | downgrade_pressure = | 
|  | 2178 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); | 
| Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 2179 | kill_heaviest_task = | 
| Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 2180 | property_get_bool("ro.lmk.kill_heaviest_task", false); | 
| Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 2181 | low_ram_device = property_get_bool("ro.config.low_ram", false); | 
| Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 2182 | kill_timeout_ms = | 
|  | 2183 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); | 
| Suren Baghdasaryan | ffdc4dd | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 2184 | use_minfree_levels = | 
|  | 2185 | property_get_bool("ro.lmk.use_minfree_levels", false); | 
| Suren Baghdasaryan | ce13cb5 | 2018-06-19 18:38:12 -0700 | [diff] [blame] | 2186 | per_app_memcg = | 
|  | 2187 | property_get_bool("ro.config.per_app_memcg", low_ram_device); | 
| Vic Yang | 360a113 | 2018-08-07 10:18:22 -0700 | [diff] [blame] | 2188 | swap_free_low_percentage = | 
|  | 2189 | property_get_int32("ro.lmk.swap_free_low_percentage", 10); | 
| Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 2190 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 2191 | ctx = create_android_logger(MEMINFO_LOG_TAG); | 
|  | 2192 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2193 | #ifdef LMKD_LOG_STATS | 
| Rajeev Kumar | 1c669f7 | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 2194 | statslog_init(&log_ctx, &enable_stats_log); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2195 | #endif | 
|  | 2196 |  | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 2197 | if (!init()) { | 
|  | 2198 | if (!use_inkernel_interface) { | 
|  | 2199 | /* | 
|  | 2200 | * MCL_ONFAULT pins pages as they fault instead of loading | 
|  | 2201 | * everything immediately all at once. (Which would be bad, | 
|  | 2202 | * because as of this writing, we have a lot of mapped pages we | 
|  | 2203 | * never use.) Old kernels will see MCL_ONFAULT and fail with | 
|  | 2204 | * EINVAL; we ignore this failure. | 
|  | 2205 | * | 
|  | 2206 | * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT | 
|  | 2207 | * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault | 
|  | 2208 | * in pages. | 
|  | 2209 | */ | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 2210 | /* CAP_IPC_LOCK required */ | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 2211 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { | 
|  | 2212 | ALOGW("mlockall failed %s", strerror(errno)); | 
|  | 2213 | } | 
| Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 2214 |  | 
| Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 2215 | /* CAP_NICE required */ | 
|  | 2216 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | 
|  | 2217 | ALOGW("set SCHED_FIFO failed %s", strerror(errno)); | 
|  | 2218 | } | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 2219 | } | 
|  | 2220 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2221 | mainloop(); | 
| Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 2222 | } | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2223 |  | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2224 | #ifdef LMKD_LOG_STATS | 
| Rajeev Kumar | 1c669f7 | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 2225 | statslog_destroy(&log_ctx); | 
| Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 2226 | #endif | 
|  | 2227 |  | 
| Suren Baghdasaryan | 282ad1a | 2018-07-26 16:34:27 -0700 | [diff] [blame] | 2228 | android_log_destroy(&ctx); | 
|  | 2229 |  | 
| Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 2230 | ALOGI("exiting"); | 
|  | 2231 | return 0; | 
|  | 2232 | } |