[automerger skipped] lmkd: Set default kill timeout to limit waits for uninterruptible processes am: dd8c2c181b am: 504a7cafc5 -s ours
am skip reason: Change-Id Ia280dc095df9ca8494278e0a75b976ed93fc04ae with SHA-1 dd8c2c181b is in history
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/memory/lmkd/+/12245883
Change-Id: I3cbc899106a37001e4e09c6b0223086a43c96e00
diff --git a/event.logtags b/event.logtags
index 452f411..fcbb960 100644
--- a/event.logtags
+++ b/event.logtags
@@ -35,4 +35,4 @@
# TODO: generate ".java" and ".h" files with integer constants from this file.
# for killinfo logs
-10195355 killinfo (Pid|1|5),(Uid|1|5),(OomAdj|1),(MinOomAdj|1),(TaskSize|1),(enum kill_reasons|1|5),(MemFree|1),(Cached|1),(SwapCached|1),(Buffers|1),(Shmem|1),(Unevictable|1),(SwapTotal|1),(SwapFree|1),(ActiveAnon|1),(InactiveAnon|1),(ActiveFile|1),(InactiveFile|1),(SReclaimable|1),(SUnreclaim|1),(KernelStack|1),(PageTables|1),(IonHeap|1),(IonHeapPool|1),(CmaFree|1)
+10195355 killinfo (Pid|1|5),(Uid|1|5),(OomAdj|1),(MinOomAdj|1),(TaskSize|1),(enum kill_reasons|1|5),(MemFree|1),(Cached|1),(SwapCached|1),(Buffers|1),(Shmem|1),(Unevictable|1),(SwapTotal|1),(SwapFree|1),(ActiveAnon|1),(InactiveAnon|1),(ActiveFile|1),(InactiveFile|1),(SReclaimable|1),(SUnreclaim|1),(KernelStack|1),(PageTables|1),(IonHeap|1),(IonHeapPool|1),(CmaFree|1),(MsSinceEvent|1),(MsSincePrevWakeup|1),(WakeupsSinceEvent|1),(SkippedWakeups|1)
diff --git a/libpsi/psi.cpp b/libpsi/psi.cpp
index f4d5d18..950dbc1 100644
--- a/libpsi/psi.cpp
+++ b/libpsi/psi.cpp
@@ -17,8 +17,11 @@
#define LOG_TAG "libpsi"
#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
+#include <unistd.h>
#include <log/log.h>
#include "psi/psi.h"
diff --git a/lmkd.cpp b/lmkd.cpp
index 7d06807..ddf0e6a 100644
--- a/lmkd.cpp
+++ b/lmkd.cpp
@@ -202,6 +202,7 @@
static int psi_complete_stall_ms;
static int thrashing_limit_pct;
static int thrashing_limit_decay_pct;
+static int swap_util_max;
static bool use_psi_monitors = false;
static int kpoll_fd;
static struct psi_threshold psi_thresholds[VMPRESS_LEVEL_COUNT] = {
@@ -1818,8 +1819,44 @@
return 0;
}
+enum wakeup_reason {
+ Event,
+ Polling
+};
+
+struct wakeup_info {
+ struct timespec wakeup_tm;
+ struct timespec prev_wakeup_tm;
+ struct timespec last_event_tm;
+ int wakeups_since_event;
+ int skipped_wakeups;
+};
+
+/*
+ * After the initial memory pressure event is received lmkd schedules periodic wakeups to check
+ * the memory conditions and kill if needed (polling). This is done because pressure events are
+ * rate-limited and memory conditions can change in between events. Therefore after the initial
+ * event there might be multiple wakeups. This function records the wakeup information such as the
+ * timestamps of the last event and the last wakeup, the number of wakeups since the last event
+ * and how many of those wakeups were skipped (some wakeups are skipped if previously killed
+ * process is still freeing its memory).
+ */
+static void record_wakeup_time(struct timespec *tm, enum wakeup_reason reason,
+ struct wakeup_info *wi) {
+ wi->prev_wakeup_tm = wi->wakeup_tm;
+ wi->wakeup_tm = *tm;
+ if (reason == Event) {
+ wi->last_event_tm = *tm;
+ wi->wakeups_since_event = 0;
+ wi->skipped_wakeups = 0;
+ } else {
+ wi->wakeups_since_event++;
+ }
+}
+
static void killinfo_log(struct proc* procp, int min_oom_score, int tasksize,
- int kill_reason, union meminfo *mi) {
+ int kill_reason, union meminfo *mi,
+ struct wakeup_info *wi, struct timespec *tm) {
/* log process information */
android_log_write_int32(ctx, procp->pid);
android_log_write_int32(ctx, procp->uid);
@@ -1833,6 +1870,12 @@
android_log_write_int32(ctx, (int32_t)min(mi->arr[field_idx] * page_k, INT32_MAX));
}
+ /* log lmkd wakeup information */
+ android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->last_event_tm, tm));
+ android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->prev_wakeup_tm, tm));
+ android_log_write_int32(ctx, wi->wakeups_since_event);
+ android_log_write_int32(ctx, wi->skipped_wakeups);
+
android_log_write_list(ctx, LOG_ID_EVENTS);
android_log_reset(ctx);
}
@@ -1999,7 +2042,8 @@
/* Kill one process specified by procp. Returns the size of the process killed */
static int kill_one_process(struct proc* procp, int min_oom_score, int kill_reason,
- const char *kill_desc, union meminfo *mi, struct timespec *tm) {
+ const char *kill_desc, union meminfo *mi, struct wakeup_info *wi,
+ struct timespec *tm) {
int pid = procp->pid;
int pidfd = procp->pidfd;
uid_t uid = procp->uid;
@@ -2055,7 +2099,7 @@
inc_killcnt(procp->oomadj);
- killinfo_log(procp, min_oom_score, tasksize, kill_reason, mi);
+ killinfo_log(procp, min_oom_score, tasksize, kill_reason, mi, wi, tm);
if (kill_desc) {
ALOGI("Kill '%s' (%d), uid %d, oom_adj %d to free %ldkB; reason: %s", taskname, pid,
@@ -2085,7 +2129,7 @@
* Returns size of the killed process.
*/
static int find_and_kill_process(int min_score_adj, int kill_reason, const char *kill_desc,
- union meminfo *mi, struct timespec *tm) {
+ union meminfo *mi, struct wakeup_info *wi, struct timespec *tm) {
int i;
int killed_size = 0;
bool lmk_state_change_start = false;
@@ -2100,7 +2144,8 @@
if (!procp)
break;
- killed_size = kill_one_process(procp, min_score_adj, kill_reason, kill_desc, mi, tm);
+ killed_size = kill_one_process(procp, min_score_adj, kill_reason, kill_desc,
+ mi, wi, tm);
if (killed_size >= 0) {
if (!lmk_state_change_start) {
lmk_state_change_start = true;
@@ -2230,6 +2275,13 @@
}
}
+static int calc_swap_utilization(union meminfo *mi) {
+ int64_t swap_used = mi->field.total_swap - mi->field.free_swap;
+ int64_t total_swappable = mi->field.active_anon + mi->field.inactive_anon +
+ mi->field.shmem + swap_used;
+ return total_swappable > 0 ? (swap_used * 100) / total_swappable : 0;
+}
+
static void mp_event_psi(int data, uint32_t events, struct polling_params *poll_params) {
enum kill_reasons {
NONE = -1, /* To denote no kill condition */
@@ -2239,6 +2291,7 @@
LOW_MEM_AND_SWAP,
LOW_MEM_AND_THRASHING,
DIRECT_RECL_AND_THRASHING,
+ LOW_MEM_AND_SWAP_UTIL,
KILL_REASON_COUNT
};
enum reclaim_state {
@@ -2256,6 +2309,7 @@
static bool in_reclaim;
static struct zone_watermarks watermarks;
static struct timespec wmark_update_tm;
+ static struct wakeup_info wi;
union meminfo mi;
union vmstat vs;
@@ -2270,16 +2324,20 @@
char kill_desc[LINE_MAX];
bool cut_thrashing_limit = false;
int min_score_adj = 0;
+ int swap_util = 0;
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))) {
/* Skip while still killing a process */
+ wi.skipped_wakeups++;
goto no_kill;
}
/*
@@ -2400,6 +2458,16 @@
if (wmark > WMARK_MIN) {
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) {
+ /*
+ * Too much anon memory is swapped out but swap is not low.
+ * Non-swappable allocations created memory pressure.
+ */
+ 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) {
/* Page cache is thrashing while memory is low */
kill_reason = LOW_MEM_AND_THRASHING;
@@ -2421,7 +2489,7 @@
/* Kill a process if necessary */
if (kill_reason != NONE) {
int pages_freed = find_and_kill_process(min_score_adj, kill_reason, kill_desc, &mi,
- &curr_tm);
+ &wi, &curr_tm);
if (pages_freed > 0) {
killing = true;
if (cut_thrashing_limit) {
@@ -2482,6 +2550,7 @@
.filename = MEMCG_MEMORYSW_USAGE,
.fd = -1,
};
+ static struct wakeup_info wi;
if (debug_process_killing) {
ALOGI("%s memory pressure event is triggered", level_name[level]);
@@ -2517,6 +2586,8 @@
return;
}
+ record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
+
if (kill_timeout_ms &&
get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms)) {
/*
@@ -2525,6 +2596,7 @@
*/
if (is_kill_pending()) {
kill_skip_count++;
+ wi.skipped_wakeups++;
return;
}
/*
@@ -2636,7 +2708,7 @@
do_kill:
if (low_ram_device) {
/* For Go devices kill only one task */
- if (find_and_kill_process(level_oomadj[level], -1, NULL, &mi, &curr_tm) == 0) {
+ if (find_and_kill_process(level_oomadj[level], -1, NULL, &mi, &wi, &curr_tm) == 0) {
if (debug_process_killing) {
ALOGI("Nothing to kill");
}
@@ -2659,7 +2731,7 @@
min_score_adj = level_oomadj[level];
}
- pages_freed = find_and_kill_process(min_score_adj, -1, NULL, &mi, &curr_tm);
+ pages_freed = find_and_kill_process(min_score_adj, -1, NULL, &mi, &wi, &curr_tm);
if (pages_freed == 0) {
/* Rate limit kill reports when nothing was reclaimed */
@@ -3211,6 +3283,7 @@
low_ram_device ? DEF_THRASHING_LOWRAM : DEF_THRASHING));
thrashing_limit_decay_pct = clamp(0, 100, property_get_int32("ro.lmk.thrashing_limit_decay",
low_ram_device ? DEF_THRASHING_DECAY_LOWRAM : DEF_THRASHING_DECAY));
+ swap_util_max = clamp(0, 100, property_get_int32("ro.lmk.swap_util_max", 100));
}
int main(int argc, char **argv) {
diff --git a/statslog.cpp b/statslog.cpp
index 8fb441c..8205a55 100644
--- a/statslog.cpp
+++ b/statslog.cpp
@@ -16,6 +16,7 @@
#include <assert.h>
#include <errno.h>
+#include <fcntl.h>
#include <log/log.h>
#include <log/log_id.h>
#include <statslog.h>
@@ -25,6 +26,7 @@
#include <string.h>
#include <sys/uio.h>
#include <time.h>
+#include <unistd.h>
#ifdef LMKD_LOG_STATS
diff --git a/statslog.h b/statslog.h
index 9cba6b2..5992a49 100644
--- a/statslog.h
+++ b/statslog.h
@@ -39,7 +39,7 @@
#ifdef LMKD_LOG_STATS
-#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
+#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%d/memory.stat"
#define PROC_STAT_FILE_PATH "/proc/%d/stat"
#define PROC_STAT_BUFFER_SIZE 1024
#define BYTES_IN_KILOBYTE 1024
diff --git a/tests/lmkd_test.cpp b/tests/lmkd_test.cpp
index 5dbf6db..b1957bc 100644
--- a/tests/lmkd_test.cpp
+++ b/tests/lmkd_test.cpp
@@ -251,6 +251,7 @@
params.pid = pid;
params.uid = uid;
params.oomadj = data->oomadj;
+ params.ptype = PROC_TYPE_APP;
ASSERT_FALSE(lmkd_register_proc(sock, ¶ms) < 0)
<< "Failed to communicate with lmkd, err=" << strerror(errno);
// signal the child it can proceed