Merge "Set default timestamp to elapsedRealtimeNanos"

This commit is contained in:
Ruchir Rastogi 2019-11-02 00:02:58 +00:00 committed by Android (Google) Code Review
commit 65567ee266
3 changed files with 16 additions and 6 deletions

View file

@ -22,7 +22,7 @@ cc_library {
srcs: [
"stats_event_list.c",
"statsd_writer.c",
"stats_event.c",
"stats_event.c",
],
host_supported: true,
cflags: [

View file

@ -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 @@ 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;
}

View file

@ -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 @@ 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