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