Merge "Support stats log in lmkd." into pi-dev

am: dcd71294a4

Change-Id: I2607f3f94cc74e5bddccc664acbd8f139af85151
This commit is contained in:
Yao Chen
2018-05-04 13:35:07 -07:00
committed by android-build-merger
5 changed files with 142 additions and 89 deletions

View File

@@ -98,9 +98,23 @@ cc_library_shared {
name: "libstatslog",
generated_sources: ["statslog.cpp"],
generated_headers: ["statslog.h"],
cflags: [
"-Wall",
"-Werror",
],
export_generated_headers: ["statslog.h"],
shared_libs: [
"liblog",
"libutils",
],
static_libs: ["libstatssocket"],
}
cc_library_static {
name: "libstatssocket",
srcs: [
"stats_event_list.cpp",
"statsd_writer.cpp",
"stats_event_list.c",
"statsd_writer.c",
],
cflags: [
"-Wall",
@@ -109,10 +123,9 @@ cc_library_shared {
"-DWRITE_TO_STATSD=1",
"-DWRITE_TO_LOGD=0",
],
export_generated_headers: ["statslog.h"],
export_include_dirs: ["include"],
shared_libs: [
"liblog",
"libutils",
],
}

View File

@@ -19,14 +19,23 @@
#include <log/log_event_list.h>
namespace android {
namespace util {
#ifdef __cplusplus
extern "C" {
#endif
void reset_log_context(android_log_context ctx);
int write_to_logger(android_log_context context, log_id_t id);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
/**
* A copy of android_log_event_list class.
*
* android_log_event_list is going to be deprecated soon, so copy it here to avoid creating
* dependency on upstream code. TODO(b/78304629): Rewrite this code.
* android_log_event_list is going to be deprecated soon, so copy it here to
* avoid creating dependency on upstream code. TODO(b/78304629): Rewrite this
* code.
*/
class stats_event_list {
private:
@@ -36,8 +45,6 @@ private:
stats_event_list(const stats_event_list&) = delete;
void operator=(const stats_event_list&) = delete;
int write_to_logger(android_log_context context, log_id_t id);
public:
explicit stats_event_list(int tag) : ret(0) {
ctx = create_android_logger(static_cast<uint32_t>(tag));
@@ -46,99 +53,114 @@ public:
ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
log_msg.entry.len - sizeof(uint32_t));
}
~stats_event_list() {
android_log_destroy(&ctx);
}
~stats_event_list() { android_log_destroy(&ctx); }
int close() {
int retval = android_log_destroy(&ctx);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return retval;
}
/* To allow above C calls to use this class as parameter */
operator android_log_context() const {
return ctx;
}
operator android_log_context() const { return ctx; }
/* return errors or transmit status */
int status() const {
return ret;
}
int status() const { return ret; }
int begin() {
int retval = android_log_write_list_begin(ctx);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret;
}
int end() {
int retval = android_log_write_list_end(ctx);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret;
}
stats_event_list& operator<<(int32_t value) {
int retval = android_log_write_int32(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
stats_event_list& operator<<(uint32_t value) {
int retval = android_log_write_int32(ctx, static_cast<int32_t>(value));
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
stats_event_list& operator<<(bool value) {
int retval = android_log_write_int32(ctx, value ? 1 : 0);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
stats_event_list& operator<<(int64_t value) {
int retval = android_log_write_int64(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
stats_event_list& operator<<(uint64_t value) {
int retval = android_log_write_int64(ctx, static_cast<int64_t>(value));
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
stats_event_list& operator<<(const char* value) {
int retval = android_log_write_string8(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
#if defined(_USING_LIBCXX)
stats_event_list& operator<<(const std::string& value) {
int retval = android_log_write_string8_len(ctx, value.data(), value.length());
if (retval < 0) ret = retval;
int retval = android_log_write_string8_len(ctx, value.data(),
value.length());
if (retval < 0) {
ret = retval;
}
return *this;
}
#endif
stats_event_list& operator<<(float value) {
int retval = android_log_write_float32(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return *this;
}
int write(log_id_t id = LOG_ID_EVENTS) {
/* facilitate -EBUSY retry */
if ((ret == -EBUSY) || (ret > 0)) ret = 0;
if ((ret == -EBUSY) || (ret > 0)) {
ret = 0;
}
int retval = write_to_logger(ctx, id);
/* existing errors trump transmission errors */
if (!ret) ret = retval;
return ret;
}
int operator<<(log_id_t id) {
write(id);
android_log_destroy(&ctx);
if (!ret) {
ret = retval;
}
return ret;
}
@@ -151,45 +173,61 @@ public:
bool AppendInt(int32_t value) {
int retval = android_log_write_int32(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
bool AppendLong(int64_t value) {
int retval = android_log_write_int64(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
bool AppendString(const char* value) {
int retval = android_log_write_string8(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
bool AppendString(const char* value, size_t len) {
int retval = android_log_write_string8_len(ctx, value, len);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
#if defined(_USING_LIBCXX)
bool AppendString(const std::string& value) {
int retval = android_log_write_string8_len(ctx, value.data(), value.length());
if (retval < 0) ret = retval;
int retval = android_log_write_string8_len(ctx, value.data(),
value.length());
if (retval < 0) {
ret = retval;
}
return ret;
}
bool Append(const std::string& value) {
int retval = android_log_write_string8_len(ctx, value.data(), value.length());
if (retval < 0) ret = retval;
int retval = android_log_write_string8_len(ctx, value.data(),
value.length());
if (retval < 0) {
ret = retval;
}
return ret;
}
#endif
bool AppendFloat(float value) {
int retval = android_log_write_float32(ctx, value);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
@@ -201,19 +239,15 @@ public:
bool Append(const char* value, size_t len) {
int retval = android_log_write_string8_len(ctx, value, len);
if (retval < 0) ret = retval;
if (retval < 0) {
ret = retval;
}
return ret >= 0;
}
android_log_list_element read() {
return android_log_read_next(ctx);
}
android_log_list_element peek() {
return android_log_peek_next(ctx);
}
android_log_list_element read() { return android_log_read_next(ctx); }
android_log_list_element peek() { return android_log_peek_next(ctx); }
};
} // namespace util
} // namespace android
#endif
#endif // ANDROID_STATS_LOG_STATS_EVENT_LIST_H

View File

@@ -14,17 +14,12 @@
* limitations under the License.
*/
#include "stats_event_list.h"
#include "include/stats_event_list.h"
#include <string.h>
#include "statsd_writer.h"
namespace android {
namespace util {
enum ReadWriteFlag {
kAndroidLoggerRead = 1,
kAndroidLoggerWrite = 2,
};
#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
typedef struct {
uint32_t tag;
@@ -35,7 +30,10 @@ typedef struct {
unsigned len; /* Length or raw buffer. */
bool overflow;
bool list_stop; /* next call decrement list_nest_depth and issue a stop */
ReadWriteFlag read_write_flag;
enum {
kAndroidLoggerRead = 1,
kAndroidLoggerWrite = 2,
} read_write_flag;
uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
} android_log_context_internal;
@@ -45,6 +43,29 @@ static int __write_to_statsd_init(struct iovec* vec, size_t nr);
static int (*write_to_statsd)(struct iovec* vec,
size_t nr) = __write_to_statsd_init;
// Similar to create_android_logger(), but instead of allocation a new buffer,
// this function resets the buffer for resuse.
void reset_log_context(android_log_context ctx) {
if (!ctx) {
return;
}
android_log_context_internal* context =
(android_log_context_internal*)(ctx);
uint32_t tag = context->tag;
memset(context, 0, sizeof(android_log_context_internal));
context->tag = tag;
context->read_write_flag = kAndroidLoggerWrite;
size_t needed = sizeof(uint8_t) + sizeof(uint8_t);
if ((context->pos + needed) > MAX_EVENT_PAYLOAD) {
context->overflow = true;
}
/* Everything is a list */
context->storage[context->pos + 0] = EVENT_TYPE_LIST;
context->list[0] = context->pos + 1;
context->pos += needed;
}
int stats_write_list(android_log_context ctx) {
android_log_context_internal* context;
const char* msg;
@@ -80,7 +101,7 @@ int stats_write_list(android_log_context ctx) {
return write_to_statsd(vec, 2);
}
int stats_event_list::write_to_logger(android_log_context ctx, log_id_t id) {
int write_to_logger(android_log_context ctx, log_id_t id) {
int retValue = 0;
if (WRITE_TO_LOGD) {
@@ -89,9 +110,9 @@ int stats_event_list::write_to_logger(android_log_context ctx, log_id_t id) {
if (WRITE_TO_STATSD) {
// log_event_list's cast operator is overloaded.
int ret = stats_write_list(static_cast<android_log_context>(*this));
// In debugging phase, we may write to both logd and statsd. Prefer to return
// statsd socket write error code here.
int ret = stats_write_list(ctx);
// In debugging phase, we may write to both logd and statsd. Prefer to
// return statsd socket write error code here.
if (ret < 0) {
retValue = ret;
}
@@ -159,7 +180,4 @@ static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
ret = write_to_statsd(vec, nr);
errno = save_errno;
return ret;
}
} // namespace util
} // namespace android
}

View File

@@ -37,9 +37,6 @@
/* branchless on many architectures. */
#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
namespace android {
namespace util {
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
void statsd_writer_init_lock() {
@@ -65,14 +62,13 @@ static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
struct android_log_transport_write statsdLoggerWrite = {
.name = "statsd",
.sock = -EBADF,
.available = statsdAvailable,
.open = statsdOpen,
.close = statsdClose,
.write = statsdWrite,
};
std::atomic_int android_log_transport_write::sock(-EBADF);
/* log_init_lock assumed */
static int statsdOpen() {
int i, ret = 0;
@@ -267,6 +263,3 @@ static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
return ret;
}
} // namespace util
} // namespace android

View File

@@ -21,9 +21,6 @@
#include <stdatomic.h>
#include <sys/socket.h>
namespace android {
namespace util {
/**
* Internal lock should not be exposed. This is bad design.
* TODO: rewrite it in c++ code and encapsulate the functionality in a
@@ -35,7 +32,7 @@ void statsd_writer_init_unlock();
struct android_log_transport_write {
const char* name; /* human name to describe the transport */
static std::atomic_int sock;
atomic_int sock;
int (*available)(); /* Does not cause resources to be taken */
int (*open)(); /* can be called multiple times, reusing current resources */
void (*close)(); /* free up resources */
@@ -43,7 +40,5 @@ struct android_log_transport_write {
int (*write)(struct timespec* ts, struct iovec* vec, size_t nr);
};
} // namespace util
} // namespace android
#endif // ANDROID_STATS_LOG_STATS_WRITER_H