Functions to log stats log from LMKD.

Bug: 72177881
Test: Manual
Change-Id: I0f618013e242fabae785b62eb39f9bfbcaac16da
diff --git a/lmkd/Android.bp b/lmkd/Android.bp
index 76d308a..e16db65 100644
--- a/lmkd/Android.bp
+++ b/lmkd/Android.bp
@@ -18,3 +18,15 @@
         },
     },
 }
+
+cc_library_shared {
+    name: "libstatslogc",
+    srcs: ["statslog.c"],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+    shared_libs: [
+        "liblog",
+    ],
+}
diff --git a/lmkd/statslog.c b/lmkd/statslog.c
new file mode 100644
index 0000000..db7a76a
--- /dev/null
+++ b/lmkd/statslog.c
@@ -0,0 +1,100 @@
+/*
+ *  Copyright 2018 Google, Inc
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <log/log_event_list.h>
+#include <log/log_id.h>
+
+/**
+ * Logs the change in LMKD state which is used as start/stop boundaries for logging
+ * LMK_KILL_OCCURRED event.
+ * Code: LMK_STATE_CHANGED = 54
+ */
+int
+stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state) {
+    assert(ctx != NULL);
+    int ret = -EINVAL;
+    if (!ctx) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int32(ctx, code)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int32(ctx, state)) < 0) {
+        return ret;
+    }
+    return ret;
+}
+
+/**
+ * Logs the event when LMKD kills a process to reduce memory pressure.
+ * Code: LMK_KILL_OCCURRED = 51
+ */
+int
+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) {
+    assert(ctx != NULL);
+    int ret = -EINVAL;
+    if (!ctx) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int32(ctx, code)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int32(ctx, uid)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
+        return ret;
+    }
+
+    if ((ret = android_log_write_list(ctx, LOG_ID_STATS)) < 0) {
+        return ret;
+    }
+    return ret;
+}
diff --git a/lmkd/statslog.h b/lmkd/statslog.h
new file mode 100644
index 0000000..ea05fa6
--- /dev/null
+++ b/lmkd/statslog.h
@@ -0,0 +1,39 @@
+/*
+ *  Copyright 2018 Google, Inc
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#pragma once
+
+#include <sys/cdefs.h>
+__BEGIN_DECLS
+
+/**
+ * Logs the change in LMKD state which is used as start/stop boundaries for logging
+ * LMK_KILL_OCCURRED event.
+ * Code: LMK_STATE_CHANGED = 54
+ */
+int
+stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state);
+
+/**
+ * Logs the event when LMKD kills a process to reduce memory pressure.
+ * Code: LMK_KILL_OCCURRED = 51
+ */
+int
+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);
+__END_DECLS