Skip to contentSkip to conversation

trace-cmd report: keep a separator before long timestamps

PATCH New CaoRuichuang 1 participant 1 message Apr 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace-cmd report: keep a separator before long timestamps

trace-cmd report formats normal nanosecond timestamps with "%12d:". That relies on the field width to provide the separator before the timestamp.

With --raw-ts and a large --ts-offset, the timestamp can grow beyond 12 digits. Once that happens, the padding disappears and the output runs into the raw flags field, producing lines such as:

[003] dNh2.749686305812356:

Make the normal timestamp format include an explicit leading space so the separator remains present regardless of the timestamp width.

Add a unit test that exercises report --raw-ts with a large timestamp offset and checks both the fixed and the broken output forms.

---
 tracecmd/trace-read.c  |  2 +-
 utest/tracecmd-utest.c | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
tracecmd/trace-read.c +1 -1
index 355ee0f4..aef9b4c5 100644
@@ -170,7 +170,7 @@ static const char *time_format(struct tracecmd_input *handle, enum time_fmt tf)
 			else
 				return tf == TIME_FMT_NORMAL ? " %6.1000d:" : "%6.1000d";
 		} else
-			return tf == TIME_FMT_NORMAL ? "%12d:" : "%12d";
+			return tf == TIME_FMT_NORMAL ? " %12d:" : "%12d";
 	}
 }
 
utest/tracecmd-utest.c +24 -0
index c18fb2ea..cc487d13 100644
@@ -445,6 +445,28 @@ static void test_trace_convert6(void)
 	CU_TEST(ret == 0);
 }
 
+static void test_trace_report_long_timestamp_separator(void)
+{
+	struct stat st;
+	int ret;
+
+	/* If the trace data is already created, just use it, otherwise make it again */
+	if (stat(TRACECMD_FILE, &st) < 0) {
+		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
+		CU_TEST(ret == 0);
+	}
+
+	ret = grep_match("\\[[0-9][0-9][0-9]\\] [^ ]*\\. [0-9]\\{12,\\}:",
+			 "report", TRACECMD_IN, "--raw-ts",
+			 "--ts-offset", "100000000000000", NULL);
+	CU_TEST(ret == 0);
+
+	ret = grep_match("\\[[0-9][0-9][0-9]\\] [^ ]*\\.[0-9]\\{12,\\}:",
+			 "report", TRACECMD_IN, "--raw-ts",
+			 "--ts-offset", "100000000000000", NULL);
+	CU_TEST(ret == 1);
+}
+
 struct callback_data {
 	long			counter;
 	struct trace_seq	seq;
@@ -798,6 +820,8 @@ void test_tracecmd_lib(void)
 		    test_trace_sqlhist_hist);
 	CU_add_test(suite, "Test convert from v7 to v6",
 		    test_trace_convert6);
+	CU_add_test(suite, "Keep separator before long timestamps",
+		    test_trace_report_long_timestamp_separator);
 	CU_add_test(suite, "Use libraries to read file",
 		    test_trace_library_read);
 	CU_add_test(suite, "Use libraries to read file incremental",
Signature · 1 line
2.39.5 (Apple Git-154)