[lttng-dev] [PATCH babeltrace] Fix: lttng-live flush when output to file

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Tue Apr 19 22:42:06 UTC 2016


When the standard output is redirected to a file, we need to flush the
output between each packet, otherwise the stream buffering may keep us
from seeing events for a while in a low-throughput trace.

While we are there, document that lttng-live does now support the
--output option in babeltrace 1.x.

Fixes #1010

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
---
 doc/babeltrace.1                       |  3 ++-
 formats/lttng-live/lttng-live-comm.c   | 13 ++++++++++++-
 formats/lttng-live/lttng-live-plugin.c |  6 ++++++
 formats/lttng-live/lttng-live.h        |  7 +++++++
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/doc/babeltrace.1 b/doc/babeltrace.1
index f783105..cee9f7b 100644
--- a/doc/babeltrace.1
+++ b/doc/babeltrace.1
@@ -100,7 +100,8 @@ babeltrace-log(1), lttng(1), lttng-ust(3), lttng-sessiond(8)
 .SH "BUGS"
 
 .PP
-No known bugs at this point.
+The lttng-live plugin does not honor the the "--output" command line
+option.
 
 If you encounter any issues or usability problem, please report it on
 our mailing list <lttng-dev at lists.lttng.org> to help improve this
diff --git a/formats/lttng-live/lttng-live-comm.c b/formats/lttng-live/lttng-live-comm.c
index cea50ff..729df6f 100644
--- a/formats/lttng-live/lttng-live-comm.c
+++ b/formats/lttng-live/lttng-live-comm.c
@@ -246,7 +246,7 @@ void print_session_list(GPtrArray *session_list, const char *path)
 
 	for (i = 0; i < session_list->len; i++) {
 		relay_session = g_ptr_array_index(session_list, i);
-		fprintf(stdout, "%s/host/%s/%s (timer = %u, "
+		fprintf(LTTNG_LIVE_OUTPUT_FP, "%s/host/%s/%s (timer = %u, "
 				"%u stream(s), %u client(s) connected)\n",
 				path, relay_session->hostname,
 				relay_session->name, relay_session->timer,
@@ -649,6 +649,17 @@ retry:
 		ret = -1;
 		goto end;
 	}
+
+	/*
+	 * Flush the output between attempt to grab a packet, thus
+	 * ensuring we flush at least at the periodical timer period.
+	 * This is useful when the output is redirected to a file.
+	 */
+	if (fflush(LTTNG_LIVE_OUTPUT_FP) < 0) {
+		perror("fflush");
+		goto error;
+	}
+
 	cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
 	cmd.data_size = htobe64(sizeof(rq));
 	cmd.cmd_version = htobe32(0);
diff --git a/formats/lttng-live/lttng-live-plugin.c b/formats/lttng-live/lttng-live-plugin.c
index bf71b2b..771801c 100644
--- a/formats/lttng-live/lttng-live-plugin.c
+++ b/formats/lttng-live/lttng-live-plugin.c
@@ -292,6 +292,12 @@ struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
 	pos->parent.rw_table = NULL;
 	pos->parent.event_cb = NULL;
 	pos->parent.trace = &pos->trace_descriptor;
+	/*
+	 * Since we do *everything* in this function, we are skipping
+	 * the output plugin handling that is part of Babeltrace 1.x.
+	 * Therefore, don't expect the --output cmd line option to work.
+	 * This limits the output of lttng-live to stderr and stdout.
+	 */
 	if (lttng_live_open_trace_read(path) < 0) {
 		goto error;
 	}
diff --git a/formats/lttng-live/lttng-live.h b/formats/lttng-live/lttng-live.h
index 553cd34..e00a51e 100644
--- a/formats/lttng-live/lttng-live.h
+++ b/formats/lttng-live/lttng-live.h
@@ -32,6 +32,13 @@
 #define LTTNG_LIVE_MAJOR			2
 #define LTTNG_LIVE_MINOR			4
 
+/*
+ * The lttng-live output file pointer is currently hardcoded to stdout,
+ * and is expected to be hardcoded to this by fflush() performed between
+ * each packet.
+ */
+#define LTTNG_LIVE_OUTPUT_FP			stdout
+
 struct lttng_live_ctx {
 	char traced_hostname[MAXNAMLEN];
 	char session_name[MAXNAMLEN];
-- 
2.1.4



More information about the lttng-dev mailing list