Add start time to LmkKillOccurred

This is to measure an application's behavior with respect to being LMKed
(the longer an app lives before being LMKed, the better).

Bug: 119854389
Test: Manual
Change-Id: I4ef6433391c8758626334731d2b5de038e4468ae
Merged-In: I4ef6433391c8758626334731d2b5de038e4468ae
(cherry picked from I4ef6433391c8758626334731d2b5de038e4468ae)
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index d6632a2..98b3aa1 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -1048,18 +1048,20 @@
 
     // field 10 is pgfault
     // field 12 is pgmajfault
+    // field 22 is starttime
     // field 24 is rss_in_pages
-    int64_t pgfault = 0, pgmajfault = 0, rss_in_pages = 0;
+    int64_t pgfault = 0, pgmajfault = 0, starttime = 0, rss_in_pages = 0;
     if (sscanf(buffer,
                "%*u %*s %*s %*d %*d %*d %*d %*d %*d %" SCNd64 " %*d "
                "%" SCNd64 " %*d %*u %*u %*d %*d %*d %*d %*d %*d "
-               "%*d %*d %" SCNd64 "",
-               &pgfault, &pgmajfault, &rss_in_pages) != 3) {
+               "%" SCNd64 " %*d %" SCNd64 "",
+               &pgfault, &pgmajfault, &starttime, &rss_in_pages) != 4) {
         return -1;
     }
     mem_st->pgfault = pgfault;
     mem_st->pgmajfault = pgmajfault;
     mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE);
+    mem_st->process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
     return 0;
 }
 #endif
@@ -1383,10 +1385,10 @@
         if (memory_stat_parse_result == 0) {
             stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname,
                     procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes,
-                    mem_st.cache_in_bytes, mem_st.swap_in_bytes);
+                    mem_st.cache_in_bytes, mem_st.swap_in_bytes, mem_st.process_start_time_ns);
         } else if (enable_stats_log) {
             stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, procp->oomadj,
-                                          -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1);
+                                          -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1, -1);
         }
 #endif
         result = tasksize;
diff --git a/lmkd/statslog.c b/lmkd/statslog.c
index 66d1164..689e8ae 100644
--- a/lmkd/statslog.c
+++ b/lmkd/statslog.c
@@ -65,7 +65,7 @@
 stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
                               char const* process_name, int32_t oom_score, int64_t pgfault,
                               int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
-                              int64_t swap_in_bytes) {
+                              int64_t swap_in_bytes, int64_t process_start_time_ns) {
     assert(ctx != NULL);
     int ret = -EINVAL;
     if (!ctx) {
@@ -113,5 +113,9 @@
         return ret;
     }
 
+    if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
+        return ret;
+    }
+
     return write_to_logger(ctx, LOG_ID_STATS);
 }
diff --git a/lmkd/statslog.h b/lmkd/statslog.h
index 8458480..f3abe11 100644
--- a/lmkd/statslog.h
+++ b/lmkd/statslog.h
@@ -64,6 +64,7 @@
     int64_t rss_in_bytes;
     int64_t cache_in_bytes;
     int64_t swap_in_bytes;
+    int64_t process_start_time_ns;
 };
 
 #define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
@@ -87,7 +88,7 @@
 stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
                               char const* process_name, int32_t oom_score, int64_t pgfault,
                               int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
-                              int64_t swap_in_bytes);
+                              int64_t swap_in_bytes, int64_t process_start_time_ns);
 
 __END_DECLS