+ stream->cycles_timestamp = updateval;
+
+ /* convert to real timestamp */
+ stream->real_timestamp = ctf_get_real_timestamp(stream,
+ stream->cycles_timestamp);
+}
+
+/*
+ * Print timestamp, rescaling clock frequency to nanoseconds and
+ * applying offsets as needed (unix time).
+ */
+static
+void ctf_print_timestamp_real(FILE *fp,
+ struct ctf_stream_definition *stream,
+ uint64_t timestamp)
+{
+ uint64_t ts_sec = 0, ts_nsec;
+
+ ts_nsec = timestamp;
+
+ /* Add command-line offset in ns*/
+ ts_nsec += opt_clock_offset_ns;
+
+ /* Add command-line 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;
+}
+
+/*
+ * Print timestamp, in cycles
+ */
+static
+void ctf_print_timestamp_cycles(FILE *fp,
+ struct ctf_stream_definition *stream,
+ uint64_t timestamp)
+{
+ fprintf(fp, "%020" PRIu64, timestamp);
+}
+
+void ctf_print_timestamp(FILE *fp,
+ struct ctf_stream_definition *stream,
+ uint64_t timestamp)
+{
+ if (opt_clock_cycles) {
+ ctf_print_timestamp_cycles(fp, stream, timestamp);
+ } else {
+ ctf_print_timestamp_real(fp, stream, timestamp);
+ }
+}
+
+static
+void print_uuid(FILE *fp, unsigned char *uuid)
+{
+ int i;
+
+ for (i = 0; i < BABELTRACE_UUID_LEN; i++)
+ fprintf(fp, "%x", (unsigned int) uuid[i]);
+}
+
+/*
+ * Discarded events can be either:
+ * - discarded after end of previous buffer due to buffer full:
+ * happened within range: [ prev_timestamp_end, timestamp_begin ]
+ * - discarded within current buffer due to either event too large or
+ * nested wrap-around:
+ * happened within range: [ timestamp_begin, timestamp_end ]
+ *
+ * Given we have discarded counters of those two types merged into the
+ * events_discarded counter, we need to use the union of those ranges:
+ * [ prev_timestamp_end, timestamp_end ]
+ *
+ * Lost packets occur if the tracer overwrote some subbuffer(s) before the
+ * consumer had time to extract them. We keep track of those gaps with the
+ * packet sequence number in each packet.
+ */
+void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
+{
+ if ((!stream->events_discarded && !stream->packets_lost) ||
+ !babeltrace_ctf_console_output) {
+ return;
+ }
+ fflush(stdout);
+ if (stream->events_discarded) {
+ fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events between [",
+ stream->events_discarded);
+ } else if (stream->packets_lost) {
+ fprintf(fp, "[warning] Tracer lost %" PRIu64 " trace packets between [",
+ stream->packets_lost);
+ }
+ if (opt_clock_cycles) {
+ ctf_print_timestamp(fp, stream,
+ stream->prev.cycles.end);
+ fprintf(fp, "] and [");
+ ctf_print_timestamp(fp, stream,
+ stream->current.cycles.end);
+ } else {
+ ctf_print_timestamp(fp, stream,
+ stream->prev.real.end);
+ fprintf(fp, "] and [");
+ ctf_print_timestamp(fp, stream,
+ stream->current.real.end);
+ }
+ fprintf(fp, "] in trace UUID ");
+ print_uuid(fp, stream->stream_class->trace->uuid);
+ if (stream->stream_class->trace->parent.path[0])
+ fprintf(fp, ", at path: \"%s\"",
+ stream->stream_class->trace->parent.path);
+
+ fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
+ if (stream->path[0])
+ fprintf(fp, ", at relative path: \"%s\"", stream->path);
+ fprintf(fp, ". ");
+ fprintf(fp, "You should consider recording a new trace with larger "
+ "buffers or with fewer events enabled.\n");
+ fflush(fp);