Set default timestamp to elapsedRealtimeNanos

Test: m -j libstatssocket compiles
Change-Id: I52b0cb8e1e916413db3dc0fe1b9a7e922833bdf7
Merged-In: I62f4c6bb14c3914124a6af25ff46fb0a980204e7
diff --git a/libstats/Android.bp b/libstats/Android.bp
index c5d05ec..0440087 100644
--- a/libstats/Android.bp
+++ b/libstats/Android.bp
@@ -22,7 +22,7 @@
     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 e2f247a..5bccd9b 100644
--- a/libstats/stats_event.c
+++ b/libstats/stats_event.c
@@ -17,6 +17,7 @@
 #include "stats_event.h"
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include "include/stats_event_list.h"
 
 #define byte unsigned char
@@ -66,6 +67,13 @@
     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 @@
     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 @@
     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 16a04f8..ee88628 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_<type>() and add_<type>_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_<type>_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 @@
 #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 @@
 
 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