[automerger skipped] lmkd: Add lmkd wakeup information into killinfo logs am: fa667edb56 am: 2c509d9073 -s ours

am skip reason: Change-Id I0356c27515132ff0dd309b59a8bf907acbd67cd8 with SHA-1 fa667edb56 is in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/memory/lmkd/+/12245884

Change-Id: I9a089a64efd480fa9db148552dc89ef6d14c70cd
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 a2820b8..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] = {
@@ -2274,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 */
@@ -2283,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 {
@@ -2315,6 +2324,7 @@
     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");
@@ -2448,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;
@@ -3263,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, &params) < 0)
                 << "Failed to communicate with lmkd, err=" << strerror(errno);
             // signal the child it can proceed