1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| static void mp_event_psi(int data, uint32_t events, struct polling_params *poll_params) { enum reclaim_state { NO_RECLAIM = 0, KSWAPD_RECLAIM, DIRECT_RECLAIM, }; ... if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) { ALOGE("Failed to get current time"); return; }
record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
bool kill_pending = is_kill_pending(); if (kill_pending && (kill_timeout_ms == 0 || get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms))) { wi.skipped_wakeups++; goto no_kill; }
stop_wait_for_proc_kill(!kill_pending);
if (vmstat_parse(&vs) < 0) { ALOGE("Failed to parse vmstat!"); return; } workingset_refault_file = vs.field.workingset_refault ? : vs.field.workingset_refault_file;
if (meminfo_parse(&mi) < 0) { ALOGE("Failed to parse meminfo!"); return; }
if (killing) { killing = false; cycle_after_kill = true; base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file; init_ws_refault = workingset_refault_file; thrashing_reset_tm = curr_tm; prev_thrash_growth = 0; }
if (swap_free_low_percentage) { if (!swap_low_threshold) { swap_low_threshold = mi.field.total_swap * swap_free_low_percentage / 100; } swap_is_low = mi.field.free_swap < swap_low_threshold; }
if (vs.field.pgscan_direct > init_pgscan_direct) { init_pgscan_direct = vs.field.pgscan_direct; init_pgscan_kswapd = vs.field.pgscan_kswapd; reclaim = DIRECT_RECLAIM; } else if (vs.field.pgscan_kswapd > init_pgscan_kswapd) { init_pgscan_kswapd = vs.field.pgscan_kswapd; reclaim = KSWAPD_RECLAIM; } else if (workingset_refault_file == prev_workingset_refault) {
goto no_kill; }
prev_workingset_refault = workingset_refault_file;
since_thrashing_reset_ms = get_time_diff_ms(&thrashing_reset_tm, &curr_tm); if (since_thrashing_reset_ms > THRASHING_RESET_INTERVAL_MS) { long windows_passed; prev_thrash_growth = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1); windows_passed = (since_thrashing_reset_ms / THRASHING_RESET_INTERVAL_MS);
if (windows_passed > 1 || prev_thrash_growth < thrashing_limit) { prev_thrash_growth >>= windows_passed; }
base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file; init_ws_refault = workingset_refault_file; thrashing_reset_tm = curr_tm; thrashing_limit = thrashing_limit_pct; } else { thrashing = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1); } thrashing += prev_thrash_growth; if (max_thrashing < thrashing) { max_thrashing = thrashing; }
if (watermarks.high_wmark == 0 || get_time_diff_ms(&wmark_update_tm, &curr_tm) > 60000) { struct zoneinfo zi;
if (zoneinfo_parse(&zi) < 0) { ALOGE("Failed to parse zoneinfo!"); return; }
calc_zone_watermarks(&zi, &watermarks); wmark_update_tm = curr_tm; }
wmark = get_lowest_watermark(&mi, &watermarks);
if (!psi_parse_mem(&psi_data)) { critical_stall = psi_data.mem_stats[PSI_FULL].avg10 > (float)stall_limit_critical; }
if (cycle_after_kill && wmark < WMARK_LOW) {
kill_reason = PRESSURE_AFTER_KILL; strncpy(kill_desc, "min watermark is breached even after kill", sizeof(kill_desc)); } else if (level == VMPRESS_LEVEL_CRITICAL && events != 0) {
kill_reason = NOT_RESPONDING; strncpy(kill_desc, "device is not responding", sizeof(kill_desc)); } else if (swap_is_low && thrashing > thrashing_limit_pct) { kill_reason = LOW_SWAP_AND_THRASHING; snprintf(kill_desc, sizeof(kill_desc), "device is low on swap (%" PRId64 "kB < %" PRId64 "kB) and thrashing (%" PRId64 "%%)", mi.field.free_swap * page_k, swap_low_threshold * page_k, thrashing); if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) { min_score_adj = PERCEPTIBLE_APP_ADJ + 1; } check_filecache = true; } else if (swap_is_low && wmark < WMARK_HIGH) { kill_reason = LOW_MEM_AND_SWAP; snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap is low (%" PRId64 "kB < %" PRId64 "kB)", wmark < WMARK_LOW ? "min" : "low", mi.field.free_swap * page_k, swap_low_threshold * page_k); if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) { min_score_adj = PERCEPTIBLE_APP_ADJ + 1; } } else if (wmark < WMARK_HIGH && swap_util_max < 100 && (swap_util = calc_swap_utilization(&mi)) > swap_util_max) {
kill_reason = LOW_MEM_AND_SWAP_UTIL; snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap utilization" " is high (%d%% > %d%%)", wmark < WMARK_LOW ? "min" : "low", swap_util, swap_util_max); } else if (wmark < WMARK_HIGH && thrashing > thrashing_limit) { kill_reason = LOW_MEM_AND_THRASHING; snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and thrashing (%" PRId64 "%%)", wmark < WMARK_LOW ? "min" : "low", thrashing); cut_thrashing_limit = true; if (thrashing < thrashing_critical_pct) { min_score_adj = PERCEPTIBLE_APP_ADJ + 1; } check_filecache = true; } else if (reclaim == DIRECT_RECLAIM && thrashing > thrashing_limit) { kill_reason = DIRECT_RECL_AND_THRASHING; snprintf(kill_desc, sizeof(kill_desc), "device is in direct reclaim and thrashing (%" PRId64 "%%)", thrashing); cut_thrashing_limit = true; if (thrashing < thrashing_critical_pct) { min_score_adj = PERCEPTIBLE_APP_ADJ + 1; } check_filecache = true; } else if (check_filecache) { int64_t file_lru_kb = (vs.field.nr_inactive_file + vs.field.nr_active_file) * page_k;
if (file_lru_kb < filecache_min_kb) { kill_reason = LOW_FILECACHE_AFTER_THRASHING; snprintf(kill_desc, sizeof(kill_desc), "filecache is low (%" PRId64 "kB < %" PRId64 "kB) after thrashing", file_lru_kb, filecache_min_kb); min_score_adj = PERCEPTIBLE_APP_ADJ + 1; } else { check_filecache = false; } }
if (kill_reason != NONE) { struct kill_info ki = { .kill_reason = kill_reason, .kill_desc = kill_desc, .thrashing = (int)thrashing, .max_thrashing = max_thrashing, };
if (critical_stall) { min_score_adj = 0; } psi_parse_io(&psi_data); psi_parse_cpu(&psi_data); int pages_freed = find_and_kill_process(min_score_adj, &ki, &mi, &wi, &curr_tm, &psi_data); if (pages_freed > 0) { killing = true; max_thrashing = 0; if (cut_thrashing_limit) {
thrashing_limit = (thrashing_limit * (100 - thrashing_limit_decay_pct)) / 100; } } } ... }
|