diff --git a/Android.bp b/Android.bp index 354f521..8ce5a84 100644 --- a/Android.bp +++ b/Android.bp @@ -22,6 +22,7 @@ cc_binary { ], static_libs: [ "libstatslogc", + "libstatslog_lmkd", "libstatssocket", ], local_include_dirs: ["include"], @@ -42,7 +43,38 @@ cc_library_static { shared_libs: [ "liblog", ], - static_libs: ["libstatssocket",], + static_libs: [ + "libstatslog_lmkd", + ], +} + +genrule { + name: "statslog_lmkd.h", + tools: ["stats-log-api-gen"], + cmd: "$(location stats-log-api-gen) --header $(genDir)/statslog_lmkd.h --module lmkd --namespace android,lmkd,stats", + out: [ + "statslog_lmkd.h", + ], +} + +genrule { + name: "statslog_lmkd.cpp", + tools: ["stats-log-api-gen"], + cmd: "$(location stats-log-api-gen) --cpp $(genDir)/statslog_lmkd.cpp --module lmkd --namespace android,lmkd,stats --importHeader statslog_lmkd.h", + out: [ + "statslog_lmkd.cpp", + ], +} + +cc_library_static { + name: "libstatslog_lmkd", + generated_sources: ["statslog_lmkd.cpp"], + generated_headers: ["statslog_lmkd.h"], + export_generated_headers: ["statslog_lmkd.h"], + static_libs: [ + "libcutils", + "libstatssocket", + ], } cc_library_static { diff --git a/lmkd.cpp b/lmkd.cpp index f7c631d..23bb5c3 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -790,7 +791,7 @@ static void poll_kernel(int poll_fd) { ctrl_data_write_lmk_kill_occurred((pid_t)pid, (uid_t)uid); mem_st.process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK)); mem_st.rss_in_bytes = rss_in_pages * PAGE_SIZE; - stats_write_lmk_kill_occurred_pid(LMK_KILL_OCCURRED, uid, pid, oom_score_adj, + stats_write_lmk_kill_occurred_pid(uid, pid, oom_score_adj, min_score_adj, 0, &mem_st); } @@ -2035,8 +2036,7 @@ static int kill_one_process(struct proc* procp, int min_oom_score, int kill_reas uid, procp->oomadj, tasksize * page_k); } - stats_write_lmk_kill_occurred(LMK_KILL_OCCURRED, uid, taskname, - procp->oomadj, min_oom_score, tasksize, mem_st); + stats_write_lmk_kill_occurred(uid, taskname, procp->oomadj, min_oom_score, tasksize, mem_st); ctrl_data_write_lmk_kill_occurred((pid_t)pid, uid); @@ -2075,8 +2075,8 @@ static int find_and_kill_process(int min_score_adj, int kill_reason, const char if (killed_size >= 0) { if (!lmk_state_change_start) { lmk_state_change_start = true; - stats_write_lmk_state_changed(LMK_STATE_CHANGED, - LMK_STATE_CHANGE_START); + stats_write_lmk_state_changed( + android::lmkd::stats::LMK_STATE_CHANGED__STATE__START); } break; } @@ -2087,7 +2087,7 @@ static int find_and_kill_process(int min_score_adj, int kill_reason, const char } if (lmk_state_change_start) { - stats_write_lmk_state_changed(LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP); + stats_write_lmk_state_changed(android::lmkd::stats::LMK_STATE_CHANGED__STATE__STOP); } return killed_size; @@ -3085,8 +3085,6 @@ int main(int argc __unused, char **argv __unused) { ctx = create_android_logger(KILLINFO_LOG_TAG); - statslog_init(); - if (!init()) { if (!use_inkernel_interface) { /* @@ -3114,8 +3112,6 @@ int main(int argc __unused, char **argv __unused) { mainloop(); } - statslog_destroy(); - android_log_destroy(&ctx); ALOGI("exiting"); diff --git a/statslog.cpp b/statslog.cpp index f46900f..8fb441c 100644 --- a/statslog.cpp +++ b/statslog.cpp @@ -16,13 +16,14 @@ #include #include +#include #include -#include #include #include #include #include #include +#include #include #ifdef LMKD_LOG_STATS @@ -31,8 +32,7 @@ #define STRINGIFY(x) STRINGIFY_INTERNAL(x) #define STRINGIFY_INTERNAL(x) #x -static bool enable_stats_log; -static android_log_context log_ctx; +static bool enable_stats_log = property_get_bool("ro.lmk.log_stats", false); struct proc { int pid; @@ -44,60 +44,18 @@ struct proc { static struct proc** pidhash = NULL; #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) -static int64_t getElapsedRealTimeNs() { - struct timespec t; - t.tv_sec = t.tv_nsec = 0; - clock_gettime(CLOCK_BOOTTIME, &t); - return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec; -} - -void statslog_init() { - enable_stats_log = property_get_bool("ro.lmk.log_stats", false); - - if (enable_stats_log) { - log_ctx = create_android_logger(kStatsEventTag); - } -} - -void statslog_destroy() { - if (log_ctx) { - android_log_destroy(&log_ctx); - } -} - /** * 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(int32_t code, int32_t state) { - int ret = -EINVAL; - - if (!enable_stats_log) { - return ret; +stats_write_lmk_state_changed(int32_t state) { + if (enable_stats_log) { + return android::lmkd::stats::stats_write(android::lmkd::stats::LMK_STATE_CHANGED, state); + } else { + return -EINVAL; } - - assert(log_ctx != NULL); - if (!log_ctx) { - return ret; - } - - reset_log_context(log_ctx); - - if ((ret = android_log_write_int64(log_ctx, getElapsedRealTimeNs())) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, code)) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, state)) < 0) { - return ret; - } - - return write_to_logger(log_ctx, LOG_ID_STATS); } static struct proc* pid_lookup(int pid) { @@ -116,78 +74,35 @@ static struct proc* pid_lookup(int pid) { * Code: LMK_KILL_OCCURRED = 51 */ int -stats_write_lmk_kill_occurred(int32_t code, int32_t uid, char const* process_name, +stats_write_lmk_kill_occurred(int32_t uid, char const* process_name, int32_t oom_score, int32_t min_oom_score, int tasksize, struct memory_stat *mem_st) { - int ret = -EINVAL; - if (!enable_stats_log) { - return ret; + if (enable_stats_log) { + return android::lmkd::stats::stats_write( + android::lmkd::stats::LMK_KILL_OCCURRED, + uid, + process_name, + oom_score, + mem_st ? mem_st->pgfault : -1, + mem_st ? mem_st->pgmajfault : -1, + mem_st ? mem_st->rss_in_bytes : tasksize * BYTES_IN_KILOBYTE, + mem_st ? mem_st->cache_in_bytes : -1, + mem_st ? mem_st->swap_in_bytes : -1, + mem_st ? mem_st->process_start_time_ns : -1, + min_oom_score + ); + } else { + return -EINVAL; } - if (!log_ctx) { - return ret; - } - reset_log_context(log_ctx); - - if ((ret = android_log_write_int64(log_ctx, getElapsedRealTimeNs())) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, code)) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, uid)) < 0) { - return ret; - } - - if ((ret = android_log_write_string8(log_ctx, (process_name == NULL) ? "" : process_name)) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, oom_score)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->pgfault : -1)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->pgmajfault : -1)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->rss_in_bytes - : tasksize * BYTES_IN_KILOBYTE)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->cache_in_bytes : -1)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->swap_in_bytes : -1)) < 0) { - return ret; - } - - if ((ret = android_log_write_int64(log_ctx, mem_st ? mem_st->process_start_time_ns - : -1)) < 0) { - return ret; - } - - if ((ret = android_log_write_int32(log_ctx, min_oom_score)) < 0) { - return ret; - } - - return write_to_logger(log_ctx, LOG_ID_STATS); } -int stats_write_lmk_kill_occurred_pid(int32_t code, int32_t uid, int pid, int32_t oom_score, +int stats_write_lmk_kill_occurred_pid(int32_t uid, int pid, int32_t oom_score, int32_t min_oom_score, int tasksize, struct memory_stat* mem_st) { struct proc* proc = pid_lookup(pid); if (!proc) return -EINVAL; - return stats_write_lmk_kill_occurred(code, uid, proc->taskname, oom_score, min_oom_score, + return stats_write_lmk_kill_occurred(uid, proc->taskname, oom_score, min_oom_score, tasksize, mem_st); } diff --git a/statslog.h b/statslog.h index e6f121f..9cba6b2 100644 --- a/statslog.h +++ b/statslog.h @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include @@ -37,27 +37,8 @@ struct memory_stat { int64_t process_start_time_ns; }; -/* - * These are defined in - * http://cs/android/frameworks/base/cmds/statsd/src/atoms.proto - */ -#define LMK_KILL_OCCURRED 51 -#define LMK_STATE_CHANGED 54 -#define LMK_STATE_CHANGE_START 1 -#define LMK_STATE_CHANGE_STOP 2 - #ifdef LMKD_LOG_STATS -/* - * The single event tag id for all stats logs. - * Keep this in sync with system/core/logcat/event.logtags - */ -const static int kStatsEventTag = 1937006964; - -void statslog_init(); - -void statslog_destroy(); - #define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat" #define PROC_STAT_FILE_PATH "/proc/%d/stat" #define PROC_STAT_BUFFER_SIZE 1024 @@ -69,22 +50,22 @@ void statslog_destroy(); * Code: LMK_STATE_CHANGED = 54 */ int -stats_write_lmk_state_changed(int32_t code, int32_t state); +stats_write_lmk_state_changed(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(int32_t code, int32_t uid, - char const* process_name, int32_t oom_score, int32_t min_oom_score, +stats_write_lmk_kill_occurred(int32_t uid, char const* process_name, + int32_t oom_score, int32_t min_oom_score, int tasksize, struct memory_stat *mem_st); /** * Logs the event when LMKD kills a process to reduce memory pressure. * Code: LMK_KILL_OCCURRED = 51 */ -int stats_write_lmk_kill_occurred_pid(int32_t code, int32_t uid, int pid, int32_t oom_score, +int stats_write_lmk_kill_occurred_pid(int32_t uid, int pid, int32_t oom_score, int32_t min_oom_score, int tasksize, struct memory_stat* mem_st); @@ -107,19 +88,16 @@ void stats_remove_taskname(int pid); #else /* LMKD_LOG_STATS */ -static inline void statslog_init() {} -static inline void statslog_destroy() {} +static inline int +stats_write_lmk_state_changed(int32_t state __unused) { return -EINVAL; } static inline int -stats_write_lmk_state_changed(int32_t code __unused, int32_t state __unused) { return -EINVAL; } - -static inline int -stats_write_lmk_kill_occurred(int32_t code __unused, int32_t uid __unused, +stats_write_lmk_kill_occurred(int32_t uid __unused, char const* process_name __unused, int32_t oom_score __unused, int32_t min_oom_score __unused, int tasksize __unused, struct memory_stat *mem_st __unused) { return -EINVAL; } -static inline int stats_write_lmk_kill_occurred_pid(int32_t code __unused, int32_t uid __unused, +static inline int stats_write_lmk_kill_occurred_pid(int32_t uid __unused, int pid __unused, int32_t oom_score __unused, int32_t min_oom_score __unused, int tasksize __unused,