From 6c7cbc430e29c731cbc35d2b26a13b419ebcacba Mon Sep 17 00:00:00 2001 From: Ruchir Rastogi Date: Tue, 22 Oct 2019 10:56:58 -0700 Subject: [PATCH] Set default timestamp to elapsedRealtimeNanos Test: m -j libstatssocket compiles Change-Id: I62f4c6bb14c3914124a6af25ff46fb0a980204e7 --- libstats/Android.bp | 2 +- libstats/stats_event.c | 11 ++++++++++- libstats/stats_event.h | 9 +++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/libstats/Android.bp b/libstats/Android.bp index c5d05ec16..0440087b2 100644 --- a/libstats/Android.bp +++ b/libstats/Android.bp @@ -22,7 +22,7 @@ cc_library { srcs: [ "stats_event_list.c", "statsd_writer.c", - "stats_event.c", + "stats_event.c", ], host_supported: true, cflags: [ diff --git a/libstats/stats_event.c b/libstats/stats_event.c index e2f247a1b..5bccd9bb4 100644 --- a/libstats/stats_event.c +++ b/libstats/stats_event.c @@ -17,6 +17,7 @@ #include "stats_event.h" #include #include +#include #include "include/stats_event_list.h" #define byte unsigned char @@ -66,6 +67,13 @@ struct stats_event { uint32_t tag; }; +static int64_t get_elapsed_realtime_ns() { + 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; +} + struct stats_event* stats_event_obtain() { struct stats_event* event = malloc(sizeof(struct stats_event)); @@ -77,7 +85,7 @@ struct stats_event* stats_event_obtain() { event->size = 0; event->numElements = 0; event->atomId = 0; - event->timestampNs = 0; + event->timestampNs = get_elapsed_realtime_ns(); event->errors = 0; event->tag = STATS_EVENT_TAG; return event; @@ -87,6 +95,7 @@ void stats_event_release(struct stats_event* event) { free(event); // free is a no-op if event is NULL } +// Should only be used for testing void stats_event_set_timestamp_ns(struct stats_event* event, uint64_t timestampNs) { if (event) event->timestampNs = timestampNs; } diff --git a/libstats/stats_event.h b/libstats/stats_event.h index 16a04f8f8..ee886280c 100644 --- a/libstats/stats_event.h +++ b/libstats/stats_event.h @@ -28,7 +28,6 @@ * Usage: * struct stats_event* event = stats_event_obtain(); * - * stats_event_set_timestamp_ns(event, timestampNs); * stats_event_set_atom_id(event, atomId); * stats_event_write_int32(event, 24); * stats_event_add_bool_annotation(event, 1, true); // annotations apply to the previous field @@ -41,7 +40,7 @@ * Notes: * (a) write_() and add__annotation() should be called in the order that fields * and annotations are defined in the atom. - * (b) set_timestamp_ns() and set_atom_id() can be called anytime before stats_event_write(). + * (b) set_atom_id() can be called anytime before stats_event_write(). * (c) add__annotation() calls apply to the previous field. * (d) If errors occur, stats_event_write() will write a bitmask of the errors to the socket. * (e) Strings should be encoded using UTF8 and written using stats_event_write_string8(). @@ -60,13 +59,12 @@ struct stats_event; #define ERROR_TOO_MANY_ANNOTATIONS 0x80 #define ERROR_TOO_MANY_FIELDS 0x100 -/* System API */ +/* SYSTEM API */ struct stats_event* stats_event_obtain(); void stats_event_write(struct stats_event* event); void stats_event_release(struct stats_event* event); void stats_event_set_atom_id(struct stats_event* event, const uint32_t atomId); -void stats_event_set_timestamp_ns(struct stats_event* event, const uint64_t timestampNs); void stats_event_write_int32(struct stats_event* event, int32_t value); void stats_event_write_int64(struct stats_event* event, int64_t value); @@ -83,4 +81,7 @@ void stats_event_add_int32_annotation(struct stats_event* event, uint32_t annota uint32_t stats_event_get_errors(struct stats_event* event); +/* TESTING ONLY */ +void stats_event_set_timestamp_ns(struct stats_event* event, const uint64_t timestampNs); + #endif // ANDROID_STATS_LOG_STATS_EVENT_H