Use environment information for extra fields
[babeltrace.git] / formats / ctf / ctf.c
index 03312f59303bb4ced8691bec3f1c2fd82bac1acc..ab4945dddcff18b8c41c6df91798096b4940695b 100644 (file)
 #define min(a, b)      (((a) < (b)) ? (a) : (b))
 #endif
 
+#define NSEC_PER_SEC 1000000000ULL
+
+int opt_clock_raw,
+       opt_clock_seconds,
+       opt_clock_date,
+       opt_clock_gmt;
+
+uint64_t opt_clock_offset;
+
 extern int yydebug;
 
 static
@@ -121,9 +130,77 @@ void ctf_update_timestamp(struct ctf_stream *stream,
        updateval = stream->timestamp;
        updateval &= ~((1ULL << integer_declaration->len) - 1);
        updateval += newval;
+       stream->prev_timestamp = stream->timestamp;
        stream->timestamp = updateval;
 }
 
+void ctf_print_timestamp(FILE *fp,
+                       struct ctf_stream *stream,
+                       uint64_t timestamp)
+{
+       uint64_t ts_sec = 0, ts_nsec;
+       struct ctf_trace *trace = stream->stream_class->trace;
+       struct trace_collection *tc = trace->collection;
+       uint64_t tc_offset = tc->single_clock_offset_avg;
+
+       ts_nsec = timestamp;
+
+       /* Add offsets */
+       if (!opt_clock_raw) {
+               ts_nsec += tc_offset;
+       }
+       ts_sec += opt_clock_offset;
+
+       ts_sec += ts_nsec / NSEC_PER_SEC;
+       ts_nsec = ts_nsec % NSEC_PER_SEC;
+
+       if (!opt_clock_seconds) {
+               struct tm tm;
+               time_t time_s = (time_t) ts_sec;
+
+               if (!opt_clock_gmt) {
+                       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) {
+                       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(fp, "%s", timestr);
+               }
+               /* Print time in HH:MM:SS.ns */
+               fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
+                       tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
+               goto end;
+       }
+seconds:
+       fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
+               ts_sec, ts_nsec);
+
+end:
+       return;
+}
+
 static
 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
 {
@@ -392,20 +469,61 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
        read_next_packet:
                switch (whence) {
                case SEEK_CUR:
-                       if (pos->offset == EOF)
+               {
+                       uint32_t events_discarded_diff;
+
+                       if (pos->offset == EOF) {
                                return;
+                       }
+                       /* For printing discarded event count */
+                       index = &g_array_index(pos->packet_index,
+                                       struct packet_index, pos->cur_index);
+                       events_discarded_diff = index->events_discarded;
+                       file_stream->parent.prev_timestamp_end =
+                                               index->timestamp_end;
+                       if (pos->cur_index > 0) {
+                               index = &g_array_index(pos->packet_index,
+                                               struct packet_index,
+                                               pos->cur_index - 1);
+                               events_discarded_diff -= index->events_discarded;
+                       }
+                       file_stream->parent.events_discarded = events_discarded_diff;
+                       file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
                        /* The reader will expect us to skip padding */
                        assert(pos->offset + offset == pos->content_size);
                        ++pos->cur_index;
                        break;
+               }
                case SEEK_SET:
                        assert(offset == 0);    /* only seek supported for now */
                        pos->cur_index = 0;
+                       file_stream->parent.prev_timestamp = 0;
+                       file_stream->parent.prev_timestamp_end = 0;
                        break;
                default:
                        assert(0);
                }
                if (pos->cur_index >= pos->packet_index->len) {
+                       /*
+                        * When a stream reaches the end of the
+                        * file, we need to show the number of
+                        * events discarded ourselves, because
+                        * there is no next event scheduled to
+                        * be printed in the output.
+                        */
+                       if (file_stream->parent.events_discarded) {
+                               fflush(stdout);
+                               fprintf(stderr, "[warning] Tracer discarded %d events at end of stream between [",
+                                       file_stream->parent.events_discarded);
+                               ctf_print_timestamp(stderr, &file_stream->parent,
+                                       file_stream->parent.prev_timestamp);
+                               fprintf(stderr, "] and [");
+                               ctf_print_timestamp(stderr, &file_stream->parent,
+                                       file_stream->parent.prev_timestamp_end);
+                               fprintf(stderr, "]. You should consider increasing the buffer size.\n");
+                               fflush(stderr);
+                               file_stream->parent.events_discarded = 0;
+                       }
                        pos->offset = EOF;
                        return;
                }
@@ -904,6 +1022,7 @@ int create_stream_packet_index(struct ctf_trace *td,
                packet_index.packet_size = 0;
                packet_index.timestamp_begin = 0;
                packet_index.timestamp_end = 0;
+               packet_index.events_discarded = 0;
 
                /* read and check header, set stream id (and check) */
                if (file_stream->parent.trace_packet_header) {
@@ -1031,6 +1150,15 @@ int create_stream_packet_index(struct ctf_trace *td,
                                field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
                                packet_index.timestamp_end = get_unsigned_int(field);
                        }
+
+                       /* read events discarded from header */
+                       len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
+                       if (len_index >= 0) {
+                               struct definition *field;
+
+                               field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+                               packet_index.events_discarded = get_unsigned_int(field);
+                       }
                } else {
                        /* Use file size for packet size */
                        packet_index.content_size = filestats.st_size * CHAR_BIT;
This page took 0.025621 seconds and 4 git commands to generate.