From 389aee111725cc14e57c2d863fd67dbeb625207e Mon Sep 17 00:00:00 2001 From: Yao Chen Date: Wed, 2 May 2018 11:19:27 -0700 Subject: [PATCH] Fix the stats log in lmkd 1. let logs be written to statsd directly like all other stats logs. + stats log should not write to logd anymore(b/78239479) 2. fixed the log format + need to embed the elapsed real time in the log 3. fixed the log context reuse problem +reset the log context buffer and internal state before reuse Bug: 78603347 Bug: 78239479 Test: tested with alloc_stress, and saw logs written to statsd performance measurement (memory & cpu): https://paste.googleplex.com/5508158646648832 Change-Id: I345f0eace8ba1687ff480fb88e9abba1d8533f76 --- lmkd/Android.bp | 2 ++ lmkd/lmkd.c | 2 +- lmkd/statslog.c | 29 +++++++++++++++++++++++------ lmkd/statslog.h | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lmkd/Android.bp b/lmkd/Android.bp index 58647f2ff..0474ff58a 100644 --- a/lmkd/Android.bp +++ b/lmkd/Android.bp @@ -8,6 +8,7 @@ cc_binary { ], static_libs: [ "libstatslogc", + "libstatssocket", ], local_include_dirs: ["include"], cflags: ["-Werror", "-DLMKD_TRACE_KILLS"], @@ -31,6 +32,7 @@ cc_library_static { shared_libs: [ "liblog", ], + static_libs: ["libstatssocket",], } cc_library_static { diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c index ec55f908b..7be5f82df 100644 --- a/lmkd/lmkd.c +++ b/lmkd/lmkd.c @@ -37,7 +37,7 @@ #include #ifdef LMKD_LOG_STATS -#include +#include "statslog.h" #endif /* diff --git a/lmkd/statslog.c b/lmkd/statslog.c index db7a76a22..66d11647b 100644 --- a/lmkd/statslog.c +++ b/lmkd/statslog.c @@ -16,8 +16,16 @@ #include #include -#include #include +#include +#include + +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; +} /** * Logs the change in LMKD state which is used as start/stop boundaries for logging @@ -32,6 +40,12 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta return ret; } + reset_log_context(ctx); + + if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { + return ret; + } + if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; } @@ -39,7 +53,8 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta if ((ret = android_log_write_int32(ctx, state)) < 0) { return ret; } - return ret; + + return write_to_logger(ctx, LOG_ID_STATS); } /** @@ -56,6 +71,11 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid if (!ctx) { return ret; } + reset_log_context(ctx); + + if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { + return ret; + } if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; @@ -93,8 +113,5 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid return ret; } - if ((ret = android_log_write_list(ctx, LOG_ID_STATS)) < 0) { - return ret; - } - return ret; + return write_to_logger(ctx, LOG_ID_STATS); } diff --git a/lmkd/statslog.h b/lmkd/statslog.h index 4cde840ea..edebb195b 100644 --- a/lmkd/statslog.h +++ b/lmkd/statslog.h @@ -18,11 +18,11 @@ #define _STATSLOG_H_ #include +#include #include #include #include -#include __BEGIN_DECLS