Print timestamps in text plug-in
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 18 Nov 2016 21:42:16 +0000 (16:42 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 18:09:06 +0000 (14:09 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/ctf/ir/clock.c
formats/ctf/ir/event.c
include/babeltrace/ctf-ir/clock.h
include/babeltrace/ctf-ir/event-internal.h
include/babeltrace/ctf-ir/event.h
plugins/ctf/common/notif-iter/notif-iter.c
plugins/text/print.c
tests/lib/test_ctf_writer.c

index dee93c7a54634676f204ec4a0867cb74fcbad0d7..4eed39c718a42a6f67e37883d89929709e95814a 100644 (file)
@@ -501,7 +501,7 @@ end:
        return ret;
 }
 
-int bt_ctf_clock_value_ns_from_epoch(struct bt_ctf_clock_value *value,
+int bt_ctf_clock_value_get_value_ns_from_epoch(struct bt_ctf_clock_value *value,
                int64_t *ret_value_ns)
 {
        int ret = 0;
index beac9d489ca3f895938c977c730018471c81d3b8..4c1d24029d60a150a899f0f305c3cd86bb559f23 100644 (file)
@@ -156,7 +156,7 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
         */
        event->event_class = bt_get(event_class);
        event->clock_values = g_hash_table_new_full(g_direct_hash,
-               g_direct_equal, NULL, g_free);
+                       g_direct_equal, bt_put, bt_put);
        event_header =
                bt_ctf_field_create(validation_output.event_header_type);
        if (!event_header) {
@@ -282,39 +282,6 @@ end:
        return stream;
 }
 
-struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event)
-{
-       struct bt_ctf_clock *clock = NULL;
-       struct bt_ctf_event_class *event_class;
-       struct bt_ctf_stream_class *stream_class;
-
-       if (!event) {
-               goto end;
-       }
-
-       event_class = bt_ctf_event_get_class(event);
-       if (!event_class) {
-               goto end;
-       }
-
-       stream_class = bt_ctf_event_class_get_stream_class(event_class);
-       if (!stream_class) {
-               goto error_put_event_class;
-       }
-
-       clock = bt_ctf_stream_class_get_clock(stream_class);
-       if (!clock) {
-               goto error_put_stream_class;
-       }
-
-error_put_stream_class:
-       bt_put(stream_class);
-error_put_event_class:
-       bt_put(event_class);
-end:
-       return clock;
-}
-
 int bt_ctf_event_set_payload(struct bt_ctf_event *event,
                const char *name,
                struct bt_ctf_field *payload)
@@ -600,11 +567,10 @@ void bt_ctf_event_destroy(struct bt_object *obj)
        g_free(event);
 }
 
-uint64_t bt_ctf_event_get_clock_value(struct bt_ctf_event *event,
-               struct bt_ctf_clock *clock)
+struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
+               struct bt_ctf_event *event, struct bt_ctf_clock *clock)
 {
-       uint64_t ret = -1ULL;
-       uint64_t *clock_value;
+       struct bt_ctf_clock_value *clock_value = NULL;
 
        if (!event || !clock) {
                goto end;
@@ -615,8 +581,22 @@ uint64_t bt_ctf_event_get_clock_value(struct bt_ctf_event *event,
                goto end;
        }
 
-       ret = *clock_value;
+       bt_get(clock_value);
+end:
+       return clock_value;
+}
+
+int bt_ctf_event_set_clock_value(struct bt_ctf_event *event,
+               struct bt_ctf_clock *clock, struct bt_ctf_clock_value *value)
+{
+       int ret = 0;
+
+       if (!event || !clock || !value || event->frozen) {
+               ret = -1;
+               goto end;
+       }
 
+       g_hash_table_insert(event->clock_values, bt_get(clock), bt_get(value));
 end:
        return ret;
 }
index 2bd1924bfe771408d2002ab077cb7e2c842e9878..d9e3eb06567b969d813cb16f60fac7f016a60f7d 100644 (file)
@@ -257,7 +257,7 @@ extern int bt_ctf_clock_value_get_value(
                struct bt_ctf_clock_value *clock_value, uint64_t *raw_value);
 
 extern int bt_ctf_clock_value_get_value_ns_from_epoch(
-               struct bt_ctf_clock_value, int64_t *value_ns);
+               struct bt_ctf_clock_value *clock_value, int64_t *value_ns);
 
 #ifdef __cplusplus
 }
index ed09e962bf9bca9a3536c0c8ae3392dce129a7a7..2e5175cbb06ca56104ed1566c961fd7e2cc2658c 100644 (file)
@@ -46,7 +46,8 @@ struct bt_ctf_event {
        struct bt_ctf_field *stream_event_context;
        struct bt_ctf_field *context_payload;
        struct bt_ctf_field *fields_payload;
-       GHashTable *clock_values; /* Maps clock addresses to (uint64_t *) */
+       /* Maps clock classes to bt_ctf_clock_value. */
+       GHashTable *clock_values;
        int frozen;
 };
 
index 22ac84b17375651fa8e7ee0b08ffb08f14f07dee..63926ba8c54849b8eab8aafb90b5cb5d8510bfcc 100644 (file)
@@ -38,6 +38,8 @@
 extern "C" {
 #endif
 
+struct bt_ctf_clock;
+struct bt_ctf_clock_value;
 struct bt_ctf_event_class;
 struct bt_ctf_event;
 struct bt_ctf_field;
@@ -82,16 +84,6 @@ extern struct bt_ctf_event_class *bt_ctf_event_get_class(
 extern struct bt_ctf_stream *bt_ctf_event_get_stream(
                struct bt_ctf_event *event);
 
-/*
- * bt_ctf_event_get_clock: get an event's associated clock.
- *
- * @param event Event.
- *
- * Returns the event's clock, NULL on error.
- */
-extern struct bt_ctf_clock *bt_ctf_event_get_clock(
-               struct bt_ctf_event *event);
-
 /*
  * bt_ctf_event_get_payload_field: get an event's payload.
  *
@@ -248,8 +240,12 @@ extern struct bt_ctf_packet *bt_ctf_event_get_packet(
 extern int bt_ctf_event_set_packet(struct bt_ctf_event *event,
                struct bt_ctf_packet *packet);
 
-extern uint64_t bt_ctf_event_get_clock_value(struct bt_ctf_event *event,
-               struct bt_ctf_clock *clock);
+extern struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
+               struct bt_ctf_event *event, struct bt_ctf_clock *clock);
+
+extern int bt_ctf_event_set_clock_value(
+               struct bt_ctf_event *event, struct bt_ctf_clock *clock,
+               struct bt_ctf_clock_value *value);
 
 #ifdef __cplusplus
 }
index d4bfdb1b14409503fb03869ce998d07d60b47878..260d15ee21d4dcd76cd2cfcc37089c16ea922ca4 100644 (file)
@@ -1758,6 +1758,37 @@ end:
        return selected_field_type;
 }
 
+static
+int set_event_clocks(struct bt_ctf_event *event,
+               struct bt_ctf_notif_iter *notit)
+{
+       int ret;
+       GHashTableIter iter;
+       struct bt_ctf_clock *clock;
+       uint64_t *clock_state;
+
+       g_hash_table_iter_init(&iter, notit->clock_states);
+
+       while (g_hash_table_iter_next(&iter, (gpointer) &clock,
+                       (gpointer) &clock_state)) {
+               struct bt_ctf_clock_value *clock_value;
+
+               clock_value = bt_ctf_clock_value_create(clock, *clock_state);
+               if (!clock_value) {
+                       ret = -1;
+                       goto end;
+               }
+               ret = bt_ctf_event_set_clock_value(event, clock, clock_value);
+               bt_put(clock_value);
+               if (ret) {
+                       goto end;
+               }
+       }
+       ret = 0;
+end:
+       return ret;
+}
+
 static
 struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
 {
@@ -1795,6 +1826,11 @@ struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
                goto error;
        }
 
+       ret = set_event_clocks(event, notit);
+       if (ret) {
+               goto error;
+       }
+
        /* Associate with current packet. */
        assert(notit->packet);
        ret = bt_ctf_event_set_packet(event, notit->packet);
index 472c29c81a4fc8d9fbfaf10c8b50fe2790967e3f..a7b67409ffbc7165e01940289d76c04589d42483 100644 (file)
 #include <babeltrace/ctf-ir/clock.h>
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/fields.h>
+#include <babeltrace/ctf-ir/trace.h>
 #include <babeltrace/bitfield.h>
 #include <inttypes.h>
 #include "text.h"
 
+#define NSEC_PER_SEC 1000000000LL
+
 static inline
 const char *rem_(const char *str)
 {
@@ -61,7 +64,23 @@ void print_timestamp_cycles(struct text_component *text,
                struct bt_ctf_clock *clock,
                struct bt_ctf_event *event)
 {
-       fputs("00000000000000000000", text->out);
+       int ret;
+       struct bt_ctf_clock_value *clock_value;
+       uint64_t cycles;
+
+       clock_value = bt_ctf_event_get_clock_value(event, clock);
+       if (!clock_value) {
+               fputs("????????????????????", text->out);
+               return;
+       }
+
+       ret = bt_ctf_clock_value_get_value(clock_value, &cycles);
+       bt_put(clock_value);
+       if (ret) {
+               fprintf(text->out, "Error");
+               return;
+       }
+       fprintf(text->out, "%020" PRIu64, cycles);
 }
 
 static
@@ -69,17 +88,103 @@ void print_timestamp_wall(struct text_component *text,
                struct bt_ctf_clock *clock,
                struct bt_ctf_event *event)
 {
-       fputs("??:??:??.?????????", text->out);
-}
+       int ret;
+       struct bt_ctf_clock_value *clock_value;
+       int64_t ts_nsec = 0;    /* add configurable offset */
+       int64_t ts_sec = 0;     /* add configurable offset */
+       uint64_t ts_sec_abs, ts_nsec_abs;
+       bool is_negative;
+
+       clock_value = bt_ctf_event_get_clock_value(event, clock);
+       if (!clock_value) {
+               fputs("??:??:??.?????????", text->out);
+               return;
+       }
+
+       ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, &ts_nsec);
+       bt_put(clock_value);
+       if (ret) {
+               fprintf(text->out, "Error");
+               return;
+       }
+
+       ts_sec += ts_nsec / NSEC_PER_SEC;
+       ts_nsec = ts_nsec % NSEC_PER_SEC;
+       if (ts_sec >= 0 && ts_nsec >= 0) {
+               is_negative = false;
+               ts_sec_abs = ts_sec;
+               ts_nsec_abs = ts_nsec;
+       } else if (ts_sec > 0 && ts_nsec < 0) {
+               is_negative = false;
+               ts_sec_abs = ts_sec - 1;
+               ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
+       } else if (ts_sec == 0 && ts_nsec < 0) {
+               is_negative = true;
+               ts_sec_abs = ts_sec;
+               ts_nsec_abs = -ts_nsec;
+       } else if (ts_sec < 0 && ts_nsec > 0) {
+               is_negative = true;
+               ts_sec_abs = -(ts_sec + 1);
+               ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
+       } else if (ts_sec < 0 && ts_nsec == 0) {
+               is_negative = true;
+               ts_sec_abs = -ts_sec;
+               ts_nsec_abs = ts_nsec;
+       } else {        /* (ts_sec < 0 && ts_nsec < 0) */
+               is_negative = true;
+               ts_sec_abs = -ts_sec;
+               ts_nsec_abs = -ts_nsec;
+       }
+
+       if (/*!opt_clock_seconds*/true) {
+               struct tm tm;
+               time_t time_s = (time_t) ts_sec_abs;
+
+               if (is_negative) {
+                       fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
+                       goto seconds;
+               }
 
-static
-enum bt_component_status get_event_timestamp(struct bt_ctf_event *event)
-{
-/*     int ret;
-       uint64_t value, frequency;
-       int64_t offset_s, offset;
-*/
-       return BT_COMPONENT_STATUS_OK;
+               if (/*!opt_clock_gmt*/true) {
+                       struct tm *res;
+
+                       res = localtime_r(&time_s, &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to get localtime.\n");
+                               goto seconds;
+                       }
+               } else {
+                       struct tm *res;
+
+                       res = gmtime_r(&time_s, &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to get gmtime.\n");
+                               goto seconds;
+                       }
+               }
+               if (/*opt_clock_date*/false) {
+                       char timestr[26];
+                       size_t res;
+
+                       /* Print date and time */
+                       res = strftime(timestr, sizeof(timestr),
+                                       "%F ", &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to print ascii time.\n");
+                               goto seconds;
+                       }
+                       fprintf(text->out, "%s", timestr);
+               }
+               /* Print time in HH:MM:SS.ns */
+               fprintf(text->out, "%02d:%02d:%02d.%09" PRIu64,
+                               tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
+               goto end;
+       }
+seconds:
+       fprintf(text->out, "%s%" PRId64 ".%09" PRIu64,
+                       is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
+end:
+       return;
 }
 
 static
@@ -89,6 +194,8 @@ enum bt_component_status print_event_timestamp(struct text_component *text,
        bool print_names = text->options.print_header_field_names;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_stream *stream = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_trace *trace = NULL;
        struct bt_ctf_clock *clock = NULL;
        FILE *out = text->out;
        FILE *err = text->err;
@@ -100,12 +207,10 @@ enum bt_component_status print_event_timestamp(struct text_component *text,
                goto end;
        }
 
-       clock = bt_ctf_event_get_clock(event);
-       if (!clock) {
-               /* Stream has no timestamp. */
-               //puts("no_timestamp!");
-               //goto end;
-       }
+       /* FIXME - error checking */
+       stream_class = bt_ctf_stream_get_class(stream);
+       trace = bt_ctf_stream_class_get_trace(stream_class);
+       clock = bt_ctf_trace_get_clock(trace, 0);
 
        fputs(print_names ? "timestamp = " : "[", out);
        if (text->options.print_timestamp_cycles) {
@@ -121,6 +226,8 @@ enum bt_component_status print_event_timestamp(struct text_component *text,
 end:
        bt_put(stream);
        bt_put(clock);
+       bt_put(stream_class);
+       bt_put(trace);
        return ret;
 }
 
index c72e1d3aea720cae44f3448f5f69ed7b017ff182..04e9d996ba209300d6e39c96ef5f7a39070e60e9 100644 (file)
@@ -319,7 +319,6 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        double ret_double;
        int64_t ret_range_start_int64_t, ret_range_end_int64_t;
        uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
-       struct bt_ctf_clock *ret_clock;
        struct bt_ctf_event_class *ret_event_class;
        struct bt_ctf_field *packet_context;
        struct bt_ctf_field *packet_context_field;
@@ -560,13 +559,6 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        ok(simple_event,
                "Instantiate an event containing a single integer field");
 
-       ok(bt_ctf_event_get_clock(NULL) == NULL,
-               "bt_ctf_event_get_clock handles NULL correctly");
-       ret_clock = bt_ctf_event_get_clock(simple_event);
-       ok(ret_clock == clock,
-               "bt_ctf_event_get_clock returns a correct clock");
-       bt_put(clock);
-
        integer_field = bt_ctf_field_create(ep_integer_field_type);
        bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
        ok(bt_ctf_event_set_payload(simple_event, "integer_field",
This page took 0.033278 seconds and 4 git commands to generate.