Skip to contentSkip to conversation

trace-cmd: Update for the new trace_print_event() logic

PATCH Superseded Steven Rostedt 2 participants 18 messages Sep 18, 2019 – Sep 20, 2019

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Update for the new trace_print_event() logic

Porting over Tzvetomir's trace_print_event() logic to trace-cmd, I found that it required some restructuring of the code. This was a quick work to get it working. Please take a look at it as I probably screwed something up (while jetlag and also doing this along with other things in parallel).

Steven Rostedt (VMware) (4):
trace-cmd: Move extract trace_clock into trace-input.c
trace-cmd: Separate out time diff code in trace_show_data()
trace-cmd: Add check for trace_clock using usecs into tracecmd_parse_trace_clock() tools/lib/traceevent: Round up in tep_print_event() time precision

Tzvetomir Stoyanov (1):
libtraceevent, perf tools: Changes in tep_print_event_* APIs

----

 include/trace-cmd/trace-cmd.h      |   4 +-
 include/traceevent/event-parse.h   |  29 ++--
 lib/trace-cmd/trace-input.c        |  41 ++++-
 lib/trace-cmd/trace-util.c         |  27 ----
 lib/traceevent/event-parse-api.c   |  40 -----
 lib/traceevent/event-parse-local.h |   4 -
 lib/traceevent/event-parse.c       | 324 +++++++++++++++++++++----------------
 tracecmd/trace-read.c              |  85 ++++++----
 8 files changed, 298 insertions(+), 256 deletions(-)
#7 lore raw

PATCH 2/5 trace-cmd: Separate out time diff code in trace_show_data()

From: "Steven Rostedt (VMware)" <[email protected]>

To make it easier to convert to the new tep_print_event() format, use the open coded version of the tracing data regardless if a different timestamp is to be used.

---
 tracecmd/trace-read.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tracecmd/trace-read.c +9 -8
index c14e87f76a32..01d904964edf 100644
@@ -757,6 +757,7 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 {
 	tracecmd_show_data_func func = tracecmd_get_show_data_func(handle);
 	struct tep_handle *pevent;
+	struct tep_event *event;
 	struct trace_seq s;
 	int cpu = record->cpu;
 	bool use_trace_clock;
@@ -793,14 +794,13 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 		}
 	}
 	use_trace_clock = tracecmd_get_use_trace_clock(handle);
+	event = tep_find_event_by_record(pevent, record);
+	tep_print_event_task(pevent, &s, event, record);
+	tep_print_event_time(pevent, &s, event, record,
+			     use_trace_clock);
 	if (tsdiff) {
 		unsigned long long rec_ts = record->ts;
-		struct tep_event *event;
 
-		event = tep_find_event_by_record(pevent, record);
-		tep_print_event_task(pevent, &s, event, record);
-		tep_print_event_time(pevent, &s, event, record,
-					use_trace_clock);
 		buf[0] = 0;
 		if (use_trace_clock && !tep_test_flag(pevent, TEP_NSEC_OUTPUT))
 			rec_ts = (rec_ts + 500) / 1000;
@@ -811,9 +811,10 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 		}
 		last_ts = rec_ts;
 		trace_seq_printf(&s, " %-8s", buf);
-		tep_print_event_data(pevent, &s, event, record);
-	} else
-		tep_print_event(pevent, &s, record, use_trace_clock);
+	}
+
+	tep_print_event_data(pevent, &s, event, record);
+
 	if (s.len && *(s.buffer + s.len - 1) == '\n')
 		s.len--;
 	if (tracecmd_get_debug()) {
Signature · 1 line
2.20.1
#9 lore raw

PATCH 3/5 trace-cmd: Add check for trace_clock using usecs into tracecmd_parse_trace_clock()

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

In preparation for using the new tep_print_event() format, move the logic of checking if the trace_clock used should be default as printed in usecs into tracecmd_parse_trace_clock() when it parses the trace_clock.

---
 include/trace-cmd/trace-cmd.h | 1 +
 lib/trace-cmd/trace-input.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
include/trace-cmd/trace-cmd.h +1 -0
index eb36a1f20c30..7c3bc2229461 100644
@@ -87,6 +87,7 @@ enum {
 	TRACECMD_FL_IGNORE_DATE		= (1 << 0),
 	TRACECMD_FL_BUFFER_INSTANCE	= (1 << 1),
 	TRACECMD_FL_LATENCY		= (1 << 2),
+	TRACECMD_FL_IN_USECS		= (1 << 3),
 };
 
 struct tracecmd_ftrace {
lib/trace-cmd/trace-input.c +8 -0
index 5533d42fd7c2..73f27a4e50d0 100644
@@ -2582,6 +2582,14 @@ static void extract_trace_clock(struct tracecmd_input *handle, char *line)
 	sscanf(data, "%ms", &clock);
 	/* TODO: report if it fails to allocate */
 	handle->trace_clock = clock;
+
+	if (!clock)
+		return;
+
+	if (!strcmp(clock, "local") || !strcmp(clock, "global")
+	    || !strcmp(clock, "uptime") || !strcmp(clock, "perf")
+	    || !strncmp(clock, "mono", 4))
+		handle->flags |= TRACECMD_FL_IN_USECS;
 }
 
 void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
Signature · 1 line
2.20.1
#16 lore raw

PATCH 5/5 tools/lib/traceevent: Round up in tep_print_event() time precision

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

When testing the output of the old trace-cmd compared to the one that uses the updated tep_print_event() logic, it was different in that the time stamp precision in the old format would round up to the nearest precision, where as the new logic truncates. Bring back the old method of rounding up.

---
 lib/traceevent/event-parse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/traceevent/event-parse.c +3 -1
index d1085aab9c43..7fbbbccab78a 100644
@@ -5527,8 +5527,10 @@ static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
 	if (divstr && isdigit(*(divstr + 1)))
 		div = atoi(divstr + 1);
 	time = record->ts;
-	if (div)
+	if (div) {
+		time += div / 2;
 		time /= div;
+	}
 	pr = prec;
 	while (pr--)
 		p10 *= 10;
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/5 trace-cmd: Move extract trace_clock into trace-input.c

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Move the extracting of the trace_clock logic into trace-input.c from trace-util.c, and store it locally in the tracecmd_input handler. This will allow us to remove the trace_clock logic from event_parse.c as it doesn't belong there.

---
 include/trace-cmd/trace-cmd.h |  3 ++-
 lib/trace-cmd/trace-input.c   | 33 +++++++++++++++++++++++++++++++--
 lib/trace-cmd/trace-util.c    | 27 ---------------------------
 3 files changed, 33 insertions(+), 30 deletions(-)
include/trace-cmd/trace-cmd.h +2 -1
index 092bd896fb09..eb36a1f20c30 100644
@@ -18,7 +18,6 @@
 #define TRACECMD_PTR2ERR(ptr)	((unisgned long)(ptr) & ~TRACECMD_ERR_MSK)
 
 void tracecmd_parse_cmdlines(struct tep_handle *pevent, char *file, int size);
-void tracecmd_parse_trace_clock(struct tep_handle *pevent, char *file, int size);
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent, char *file, unsigned int size);
 void tracecmd_parse_ftrace_printk(struct tep_handle *pevent, char *file, unsigned int size);
 
@@ -124,6 +123,8 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
 
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle, char *file, int size);
+
 int tracecmd_make_pipe(struct tracecmd_input *handle, int cpu, int fd, int cpus);
 
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +31 -2
index 8cceb31cefef..5533d42fd7c2 100644
@@ -95,6 +95,7 @@ struct tracecmd_input {
 	char *			cpustats;
 	char *			uname;
 	char *			version;
+	char *			trace_clock;
 	struct input_buffer_instance	*buffers;
 	int			parsing_failures;
 
@@ -2571,6 +2572,33 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
 	return 0;
 }
 
+static void extract_trace_clock(struct tracecmd_input *handle, char *line)
+{
+	char *data;
+	char *clock;
+	char *next = NULL;
+
+	data = strtok_r(line, "[]", &next);
+	sscanf(data, "%ms", &clock);
+	/* TODO: report if it fails to allocate */
+	handle->trace_clock = clock;
+}
+
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
+				char *file, int size __maybe_unused)
+{
+	char *line;
+	char *next = NULL;
+
+	line = strtok_r(file, " ", &next);
+	while (line) {
+		/* current trace_clock is shown as "[local]". */
+		if (*line == '[')
+			return extract_trace_clock(handle, line);
+		line = strtok_r(NULL, " ", &next);
+	}
+}
+
 static int read_and_parse_trace_clock(struct tracecmd_input *handle,
 							struct tep_handle *pevent)
 {
@@ -2580,7 +2608,7 @@ static int read_and_parse_trace_clock(struct tracecmd_input *handle,
 	if (read_data_and_size(handle, &trace_clock, &size) < 0)
 		return -1;
 	trace_clock[size] = 0;
-	tracecmd_parse_trace_clock(pevent, trace_clock, size);
+	tracecmd_parse_trace_clock(handle, trace_clock, size);
 	free(trace_clock);
 	return 0;
 }
@@ -2618,7 +2646,7 @@ int tracecmd_init_data(struct tracecmd_input *handle)
 		if (read_and_parse_trace_clock(handle, pevent) < 0) {
 			char clock[] = "[local]";
 			warning("File has trace_clock bug, using local clock");
-			tracecmd_parse_trace_clock(pevent, clock, 8);
+			tracecmd_parse_trace_clock(handle, clock, 8);
 		}
 	}
 
@@ -3011,6 +3039,7 @@ void tracecmd_close(struct tracecmd_input *handle)
 	free(handle->cpustats);
 	free(handle->cpu_data);
 	free(handle->uname);
+	free(handle->trace_clock);
 	close(handle->fd);
 
 	tracecmd_free_hooks(handle->hooks);
lib/trace-cmd/trace-util.c +0 -27
index b08e377c61ba..38584c200fa1 100644
@@ -70,33 +70,6 @@ void tracecmd_parse_cmdlines(struct tep_handle *pevent,
 	}
 }
 
-static void extract_trace_clock(struct tep_handle *pevent, char *line)
-{
-	char *data;
-	char *clock;
-	char *next = NULL;
-
-	data = strtok_r(line, "[]", &next);
-	sscanf(data, "%ms", &clock);
-	tep_register_trace_clock(pevent, clock);
-	free(clock);
-}
-
-void tracecmd_parse_trace_clock(struct tep_handle *pevent,
-				char *file, int size __maybe_unused)
-{
-	char *line;
-	char *next = NULL;
-
-	line = strtok_r(file, " ", &next);
-	while (line) {
-		/* current trace_clock is shown as "[local]". */
-		if (*line == '[')
-			return extract_trace_clock(pevent, line);
-		line = strtok_r(NULL, " ", &next);
-	}
-}
-
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent,
 			 char *file, unsigned int size __maybe_unused)
 {
Signature · 1 line
2.20.1
#11 lore raw

PATCH 4/5 libtraceevent, perf tools: Changes in tep_print_event_* APIs

In reply to Steven Rostedt

From: Tzvetomir Stoyanov <[email protected]>

Libtraceevent APIs for printing various trace events information are complicated, there are complex extra parameters. To control the way event information is printed, the user should call a set of functions in a specific sequence.

These APIs are reimplemented to provide a more simple interface for printing event information.

Removed APIs:

tep_print_event_task()

	tep_print_event_time()
	tep_print_event_data()
	tep_event_info()
	tep_is_latency_format()
	tep_set_latency_format()
	tep_data_latency_format()
	tep_set_print_raw()

A new API for printing event information is introduced:
void tep_print_event(struct tep_handle *tep, struct trace_seq *s,

		        struct tep_record *record, const char *fmt, ...);

where "fmt" is a printf-like format string, followed by the event fields to be printed. Supported fields:
TEP_PRINT_PID, "%d" - event PID
TEP_PRINT_CPU, "%d" - event CPU
TEP_PRINT_COMM, "%s" - event command string TEP_PRINT_NAME, "%s" - event name
TEP_PRINT_LATENCY, "%s" - event latency
TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
can be specified as part of this format string:
"%precision.divisord". Example:
"%3.1000d" - divide the time by 1000 and print the first 3 digits before the dot. Thus, the time stamp "123456000" will be printed as "123.456"
TEP_PRINT_INFO, "%s" - event information. TEP_PRINT_INFO_RAW, "%s" - event information, in raw format.

Example:
tep_print_event(tep, s, record, "%16s-%-5d [%03d] %s %6.1000d %s %s",

		  TEP_PRINT_COMM, TEP_PRINT_PID, TEP_PRINT_CPU,
		  TEP_PRINT_LATENCY, TEP_PRINT_TIME, TEP_PRINT_NAME, TEP_PRINT_INFO);
Output:
	ls-11314 [005] d.h. 185207.366383 function __wake_up
---
 include/traceevent/event-parse.h   |  29 +--
 lib/traceevent/event-parse-api.c   |  40 ----
 lib/traceevent/event-parse-local.h |   4 -
 lib/traceevent/event-parse.c       | 322 +++++++++++++++++------------
 tracecmd/trace-read.c              |  78 ++++---
 5 files changed, 250 insertions(+), 223 deletions(-)
include/traceevent/event-parse.h +11 -18
index 99da5ef89c83..5dff952d1ea1 100644
@@ -436,18 +436,18 @@ int tep_register_print_string(struct tep_handle *tep, const char *fmt,
 			      unsigned long long addr);
 bool tep_is_pid_registered(struct tep_handle *tep, int pid);
 
-void tep_print_event_task(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record);
-void tep_print_event_time(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record,
-			  bool use_trace_clock);
-void tep_print_event_data(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record);
+#define TEP_PRINT_INFO		"INFO"
+#define TEP_PRINT_INFO_RAW	"INFO_RAW"
+#define TEP_PRINT_COMM		"COMM"
+#define TEP_PRINT_LATENCY	"LATENCY"
+#define TEP_PRINT_NAME		"NAME"
+#define TEP_PRINT_PID		1U
+#define TEP_PRINT_TIME		2U
+#define TEP_PRINT_CPU		3U
+
 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
-		     struct tep_record *record, bool use_trace_clock);
+		     struct tep_record *record, const char *fmt, ...)
+	__attribute__ ((format (printf, 4, 5)));
 
 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
 			  int long_size);
@@ -520,8 +520,6 @@ tep_find_event_by_name(struct tep_handle *tep, const char *sys, const char *name
 struct tep_event *
 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record);
 
-void tep_data_latency_format(struct tep_handle *tep,
-			     struct trace_seq *s, struct tep_record *record);
 int tep_data_type(struct tep_handle *tep, struct tep_record *rec);
 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec);
 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec);
@@ -536,8 +534,6 @@ void tep_print_field(struct trace_seq *s, void *data,
 		     struct tep_format_field *field);
 void tep_print_fields(struct trace_seq *s, void *data,
 		      int size __maybe_unused, struct tep_event *event);
-void tep_event_info(struct trace_seq *s, struct tep_event *event,
-		    struct tep_record *record);
 int tep_strerror(struct tep_handle *tep, enum tep_errno errnum,
 		 char *buf, size_t buflen);
 
@@ -562,12 +558,9 @@ bool tep_is_file_bigendian(struct tep_handle *tep);
 void tep_set_file_bigendian(struct tep_handle *tep, enum tep_endian endian);
 bool tep_is_local_bigendian(struct tep_handle *tep);
 void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian);
-bool tep_is_latency_format(struct tep_handle *tep);
-void tep_set_latency_format(struct tep_handle *tep, int lat);
 int tep_get_header_page_size(struct tep_handle *tep);
 int tep_get_header_timestamp_size(struct tep_handle *tep);
 bool tep_is_old_format(struct tep_handle *tep);
-void tep_set_print_raw(struct tep_handle *tep, int print_raw);
 void tep_set_test_filters(struct tep_handle *tep, int test_filters);
 
 struct tep_handle *tep_alloc(void);
lib/traceevent/event-parse-api.c +0 -40
index 988587840c80..4faf52a65791 100644
@@ -302,33 +302,6 @@ void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian)
 		tep->host_bigendian = endian;
 }
 
-/**
- * tep_is_latency_format - get if the latency output format is configured
- * @tep: a handle to the tep_handle
- *
- * This returns true if the latency output format is configured
- * If @tep is NULL, false is returned.
- */
-bool tep_is_latency_format(struct tep_handle *tep)
-{
-	if (tep)
-		return (tep->latency_format);
-	return false;
-}
-
-/**
- * tep_set_latency_format - set the latency output format
- * @tep: a handle to the tep_handle
- * @lat: non zero for latency output format
- *
- * This sets the latency output format
-  */
-void tep_set_latency_format(struct tep_handle *tep, int lat)
-{
-	if (tep)
-		tep->latency_format = lat;
-}
-
 /**
  * tep_is_old_format - get if an old kernel is used
  * @tep: a handle to the tep_handle
@@ -344,19 +317,6 @@ bool tep_is_old_format(struct tep_handle *tep)
 	return false;
 }
 
-/**
- * tep_set_print_raw - set a flag to force print in raw format
- * @tep: a handle to the tep_handle
- * @print_raw: the new value of the print_raw flag
- *
- * This sets a flag to force print in raw format
- */
-void tep_set_print_raw(struct tep_handle *tep, int print_raw)
-{
-	if (tep)
-		tep->print_raw = print_raw;
-}
-
 /**
  * tep_set_test_filters - set a flag to test a filter string
  * @tep: a handle to the tep_handle
lib/traceevent/event-parse-local.h +0 -4
index 09aa142f7fdd..6e58ee1fe7c8 100644
@@ -28,8 +28,6 @@ struct tep_handle {
 	enum tep_endian file_bigendian;
 	enum tep_endian host_bigendian;
 
-	int latency_format;
-
 	int old_format;
 
 	int cpus;
@@ -70,8 +68,6 @@ struct tep_handle {
 	int ld_offset;
 	int ld_size;
 
-	int print_raw;
-
 	int test_filters;
 
 	int flags;
lib/traceevent/event-parse.c +187 -135

418 lines not on this page · raw message

tracecmd/trace-read.c +52 -26

194 lines not on this page · raw message

Signature · 1 line
2.20.1
#12 lore raw

Re: [PATCH 4/5] libtraceevent, perf tools: Changes in tep_print_event_* APIs

Quoted 47 lines · On Tue, 17 Sep 2019 22:03:38 -0400 Steven Rostedt <[email protected]> wrote:

From: Tzvetomir Stoyanov <[email protected]>

Libtraceevent APIs for printing various trace events information are complicated, there are complex extra parameters. To control the way event information is printed, the user should call a set of functions in a specific sequence.

These APIs are reimplemented to provide a more simple interface for printing event information.

Removed APIs:

tep_print_event_task()

	tep_print_event_time()
	tep_print_event_data()
	tep_event_info()
	tep_is_latency_format()
	tep_set_latency_format()
	tep_data_latency_format()
	tep_set_print_raw()

A new API for printing event information is introduced:
void tep_print_event(struct tep_handle *tep, struct trace_seq *s,

		        struct tep_record *record, const char *fmt, ...);

where "fmt" is a printf-like format string, followed by the event fields to be printed. Supported fields:
TEP_PRINT_PID, "%d" - event PID
TEP_PRINT_CPU, "%d" - event CPU
TEP_PRINT_COMM, "%s" - event command string TEP_PRINT_NAME, "%s" - event name
TEP_PRINT_LATENCY, "%s" - event latency
TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
can be specified as part of this format string:
"%precision.divisord". Example:
"%3.1000d" - divide the time by 1000 and print the first 3 digits before the dot. Thus, the time stamp "123456000" will be printed as "123.456"
TEP_PRINT_INFO, "%s" - event information. TEP_PRINT_INFO_RAW, "%s" - event information, in raw format.

Example:
tep_print_event(tep, s, record, "%16s-%-5d [%03d] %s %6.1000d %s %s",

		  TEP_PRINT_COMM, TEP_PRINT_PID, TEP_PRINT_CPU,
		  TEP_PRINT_LATENCY, TEP_PRINT_TIME, TEP_PRINT_NAME, TEP_PRINT_INFO);
Output:
	ls-11314 [005] d.h. 185207.366383 function __wake_up
Quoted 11 lines

I needed to modify this with the following changes:

kernel-shark/src/libkshark.c +2 -2
index 47ec9c21..4207ae6f 100644
@@ -1103,7 +1103,7 @@ static const char *kshark_get_latency(struct tep_handle *pe,
 		return NULL;
 
 	trace_seq_reset(&seq);
-	tep_data_latency_format(pe, &seq, record);
+	tep_print_event(pe, &seq, record, "%s", TEP_PRINT_LATENCY);
 	return seq.buffer;
 }
 
@@ -1117,7 +1117,7 @@ static const char *kshark_get_info(struct tep_handle *pe,
 		return NULL;
 
 	trace_seq_reset(&seq);
-	tep_event_info(&seq, event, record);
+	tep_print_event(pe, &seq, record, "%s", TEP_PRINT_INFO);
 
 	/*
 	 * The event info string contains a trailing newline.

-- Steve

#15 lore raw

Re: [PATCH 4/5] libtraceevent, perf tools: Changes in tep_print_event_* APIs

In reply to Steven Rostedt
Quoted 6 lines · On Tue, 17 Sep 2019 22:03:38 -0400 Steven Rostedt <[email protected]> wrote:
Cc Andrew Morton <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Patrick McLean <[email protected]>

Sorry folks for the spam. This was a backport of the kernel's libtraceevent into trace-cmd's version. I used quilt send mail to post the series, and since I had the Cc's on this email, you were spammed with this version.

Please ignore, as the patch is already in the Linux kernel git repo.

-- Steve

Quoted 7 lines

---

#13 lore raw

PATCH v2 4/5 libtraceevent, perf tools: Changes in tep_print_event_* APIs

In reply to Steven Rostedt

From: Tzvetomir Stoyanov <[email protected]>

Libtraceevent APIs for printing various trace events information are complicated, there are complex extra parameters. To control the way event information is printed, the user should call a set of functions in a specific sequence.

These APIs are reimplemented to provide a more simple interface for printing event information.

Removed APIs:

tep_print_event_task()

	tep_print_event_time()
	tep_print_event_data()
	tep_event_info()
	tep_is_latency_format()
	tep_set_latency_format()
	tep_data_latency_format()
	tep_set_print_raw()

A new API for printing event information is introduced:
void tep_print_event(struct tep_handle *tep, struct trace_seq *s,

		        struct tep_record *record, const char *fmt, ...);

where "fmt" is a printf-like format string, followed by the event fields to be printed. Supported fields:
TEP_PRINT_PID, "%d" - event PID
TEP_PRINT_CPU, "%d" - event CPU
TEP_PRINT_COMM, "%s" - event command string TEP_PRINT_NAME, "%s" - event name
TEP_PRINT_LATENCY, "%s" - event latency
TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
can be specified as part of this format string:
"%precision.divisord". Example:
"%3.1000d" - divide the time by 1000 and print the first 3 digits before the dot. Thus, the time stamp "123456000" will be printed as "123.456"
TEP_PRINT_INFO, "%s" - event information. TEP_PRINT_INFO_RAW, "%s" - event information, in raw format.

Example:
tep_print_event(tep, s, record, "%16s-%-5d [%03d] %s %6.1000d %s %s",

		  TEP_PRINT_COMM, TEP_PRINT_PID, TEP_PRINT_CPU,
		  TEP_PRINT_LATENCY, TEP_PRINT_TIME, TEP_PRINT_NAME, TEP_PRINT_INFO);
Output:
	ls-11314 [005] d.h. 185207.366383 function __wake_up
---
 include/traceevent/event-parse.h   |  29 +--
 kernel-shark/src/libkshark.c       |   4 +-
 lib/traceevent/event-parse-api.c   |  40 ----
 lib/traceevent/event-parse-local.h |   4 -
 lib/traceevent/event-parse.c       | 322 +++++++++++++++++------------
 tracecmd/trace-read.c              |  78 ++++---
 6 files changed, 252 insertions(+), 225 deletions(-)
include/traceevent/event-parse.h +11 -18
index 99da5ef8..5dff952d 100644
@@ -436,18 +436,18 @@ int tep_register_print_string(struct tep_handle *tep, const char *fmt,
 			      unsigned long long addr);
 bool tep_is_pid_registered(struct tep_handle *tep, int pid);
 
-void tep_print_event_task(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record);
-void tep_print_event_time(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record,
-			  bool use_trace_clock);
-void tep_print_event_data(struct tep_handle *tep, struct trace_seq *s,
-			  struct tep_event *event,
-			  struct tep_record *record);
+#define TEP_PRINT_INFO		"INFO"
+#define TEP_PRINT_INFO_RAW	"INFO_RAW"
+#define TEP_PRINT_COMM		"COMM"
+#define TEP_PRINT_LATENCY	"LATENCY"
+#define TEP_PRINT_NAME		"NAME"
+#define TEP_PRINT_PID		1U
+#define TEP_PRINT_TIME		2U
+#define TEP_PRINT_CPU		3U
+
 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
-		     struct tep_record *record, bool use_trace_clock);
+		     struct tep_record *record, const char *fmt, ...)
+	__attribute__ ((format (printf, 4, 5)));
 
 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
 			  int long_size);
@@ -520,8 +520,6 @@ tep_find_event_by_name(struct tep_handle *tep, const char *sys, const char *name
 struct tep_event *
 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record);
 
-void tep_data_latency_format(struct tep_handle *tep,
-			     struct trace_seq *s, struct tep_record *record);
 int tep_data_type(struct tep_handle *tep, struct tep_record *rec);
 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec);
 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec);
@@ -536,8 +534,6 @@ void tep_print_field(struct trace_seq *s, void *data,
 		     struct tep_format_field *field);
 void tep_print_fields(struct trace_seq *s, void *data,
 		      int size __maybe_unused, struct tep_event *event);
-void tep_event_info(struct trace_seq *s, struct tep_event *event,
-		    struct tep_record *record);
 int tep_strerror(struct tep_handle *tep, enum tep_errno errnum,
 		 char *buf, size_t buflen);
 
@@ -562,12 +558,9 @@ bool tep_is_file_bigendian(struct tep_handle *tep);
 void tep_set_file_bigendian(struct tep_handle *tep, enum tep_endian endian);
 bool tep_is_local_bigendian(struct tep_handle *tep);
 void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian);
-bool tep_is_latency_format(struct tep_handle *tep);
-void tep_set_latency_format(struct tep_handle *tep, int lat);
 int tep_get_header_page_size(struct tep_handle *tep);
 int tep_get_header_timestamp_size(struct tep_handle *tep);
 bool tep_is_old_format(struct tep_handle *tep);
-void tep_set_print_raw(struct tep_handle *tep, int print_raw);
 void tep_set_test_filters(struct tep_handle *tep, int test_filters);
 
 struct tep_handle *tep_alloc(void);
kernel-shark/src/libkshark.c +2 -2
index 47ec9c21..4207ae6f 100644
@@ -1103,7 +1103,7 @@ static const char *kshark_get_latency(struct tep_handle *pe,
 		return NULL;
 
 	trace_seq_reset(&seq);
-	tep_data_latency_format(pe, &seq, record);
+	tep_print_event(pe, &seq, record, "%s", TEP_PRINT_LATENCY);
 	return seq.buffer;
 }
 
@@ -1117,7 +1117,7 @@ static const char *kshark_get_info(struct tep_handle *pe,
 		return NULL;
 
 	trace_seq_reset(&seq);
-	tep_event_info(&seq, event, record);
+	tep_print_event(pe, &seq, record, "%s", TEP_PRINT_INFO);
 
 	/*
 	 * The event info string contains a trailing newline.
lib/traceevent/event-parse-api.c +0 -40
index 98858784..4faf52a6 100644
@@ -302,33 +302,6 @@ void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian)
 		tep->host_bigendian = endian;
 }
 
-/**
- * tep_is_latency_format - get if the latency output format is configured
- * @tep: a handle to the tep_handle
- *
- * This returns true if the latency output format is configured
- * If @tep is NULL, false is returned.
- */
-bool tep_is_latency_format(struct tep_handle *tep)
-{
-	if (tep)
-		return (tep->latency_format);
-	return false;
-}
-
-/**
- * tep_set_latency_format - set the latency output format
- * @tep: a handle to the tep_handle
- * @lat: non zero for latency output format
- *
- * This sets the latency output format
-  */
-void tep_set_latency_format(struct tep_handle *tep, int lat)
-{
-	if (tep)
-		tep->latency_format = lat;
-}
-
 /**
  * tep_is_old_format - get if an old kernel is used
  * @tep: a handle to the tep_handle
@@ -344,19 +317,6 @@ bool tep_is_old_format(struct tep_handle *tep)
 	return false;
 }
 
-/**
- * tep_set_print_raw - set a flag to force print in raw format
- * @tep: a handle to the tep_handle
- * @print_raw: the new value of the print_raw flag
- *
- * This sets a flag to force print in raw format
- */
-void tep_set_print_raw(struct tep_handle *tep, int print_raw)
-{
-	if (tep)
-		tep->print_raw = print_raw;
-}
-
 /**
  * tep_set_test_filters - set a flag to test a filter string
  * @tep: a handle to the tep_handle
lib/traceevent/event-parse-local.h +0 -4
index 09aa142f..6e58ee1f 100644
@@ -28,8 +28,6 @@ struct tep_handle {
 	enum tep_endian file_bigendian;
 	enum tep_endian host_bigendian;
 
-	int latency_format;
-
 	int old_format;
 
 	int cpus;
@@ -70,8 +68,6 @@ struct tep_handle {
 	int ld_offset;
 	int ld_size;
 
-	int print_raw;
-
 	int test_filters;
 
 	int flags;
lib/traceevent/event-parse.c +187 -135

418 lines not on this page · raw message

tracecmd/trace-read.c +52 -26

194 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/5] trace-cmd: Move extract trace_clock into trace-input.c

In reply to Steven Rostedt
Quoted 49 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Move the extracting of the trace_clock logic into trace-input.c from trace-util.c, and store it locally in the tracecmd_input handler. This will allow us to remove the trace_clock logic from event_parse.c as it doesn't belong there.

---
 include/trace-cmd/trace-cmd.h |  3 ++-
 lib/trace-cmd/trace-input.c   | 33 +++++++++++++++++++++++++++++++--
 lib/trace-cmd/trace-util.c    | 27 ---------------------------
 3 files changed, 33 insertions(+), 30 deletions(-)
include/trace-cmd/trace-cmd.h +2 -1
index 092bd896fb09..eb36a1f20c30 100644
@@ -18,7 +18,6 @@
 #define TRACECMD_PTR2ERR(ptr)  ((unisgned long)(ptr) & ~TRACECMD_ERR_MSK)
 
 void tracecmd_parse_cmdlines(struct tep_handle *pevent, char *file, int size);
-void tracecmd_parse_trace_clock(struct tep_handle *pevent, char *file, int size);
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent, char *file, unsigned int size);
 void tracecmd_parse_ftrace_printk(struct tep_handle *pevent, char *file, unsigned int size);
 
@@ -124,6 +123,8 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
 
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle, char *file, int size);
+
 int tracecmd_make_pipe(struct tracecmd_input *handle, int cpu, int fd, int cpus);
 
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +1 -0
index 8cceb31cefef..5533d42fd7c2 100644
@@ -95,6 +95,7 @@ struct tracecmd_input {
        char *                  cpustats;
        char *                  uname;
        char *                  version;
+       char *                  trace_clock;
        struct input_buffer_instance    *buffers;
        int                     parsing_failures;
 

lib/trace-cmd/trace-input.c static int read_and_parse_cmdlines(struct tracecmd_input *handle) · line 2578

Quoted 7 lines
lib/trace-cmd/trace-input.c +4 -0
@@ -2571,6 +2572,33 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
        return 0;
 }
 
+static void extract_trace_clock(struct tracecmd_input *handle, char *line)
+{
+       char *data;
+       char *clock;

I would recommend to initialize clock with NULL, so in case sscanf fails due to broken line or mem alloc, handle->trace_clock will not point to random memory.

Quoted 94 lines
lib/trace-cmd/trace-input.c +26 -2
+       char *next = NULL;
+
+       data = strtok_r(line, "[]", &next);
+       sscanf(data, "%ms", &clock);
+       /* TODO: report if it fails to allocate */
+       handle->trace_clock = clock;
+}
+
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
+                               char *file, int size __maybe_unused)
+{
+       char *line;
+       char *next = NULL;
+
+       line = strtok_r(file, " ", &next);
+       while (line) {
+               /* current trace_clock is shown as "[local]". */
+               if (*line == '[')
+                       return extract_trace_clock(handle, line);
+               line = strtok_r(NULL, " ", &next);
+       }
+}
+
 static int read_and_parse_trace_clock(struct tracecmd_input *handle,
                                                        struct tep_handle *pevent)
 {
@@ -2580,7 +2608,7 @@ static int read_and_parse_trace_clock(struct tracecmd_input *handle,
        if (read_data_and_size(handle, &trace_clock, &size) < 0)
                return -1;
        trace_clock[size] = 0;
-       tracecmd_parse_trace_clock(pevent, trace_clock, size);
+       tracecmd_parse_trace_clock(handle, trace_clock, size);
        free(trace_clock);
        return 0;
 }
@@ -2618,7 +2646,7 @@ int tracecmd_init_data(struct tracecmd_input *handle)
                if (read_and_parse_trace_clock(handle, pevent) < 0) {
                        char clock[] = "[local]";
                        warning("File has trace_clock bug, using local clock");
-                       tracecmd_parse_trace_clock(pevent, clock, 8);
+                       tracecmd_parse_trace_clock(handle, clock, 8);
                }
        }
 
@@ -3011,6 +3039,7 @@ void tracecmd_close(struct tracecmd_input *handle)
        free(handle->cpustats);
        free(handle->cpu_data);
        free(handle->uname);
+       free(handle->trace_clock);
        close(handle->fd);
 
        tracecmd_free_hooks(handle->hooks);
lib/trace-cmd/trace-util.c +0 -28
index b08e377c61ba..38584c200fa1 100644
@@ -70,33 +70,6 @@ void tracecmd_parse_cmdlines(struct tep_handle *pevent,
        }
 }
 
-static void extract_trace_clock(struct tep_handle *pevent, char *line)
-{
-       char *data;
-       char *clock;
-       char *next = NULL;
-
-       data = strtok_r(line, "[]", &next);
-       sscanf(data, "%ms", &clock);
-       tep_register_trace_clock(pevent, clock);
-       free(clock);
-}
-
-void tracecmd_parse_trace_clock(struct tep_handle *pevent,
-                               char *file, int size __maybe_unused)
-{
-       char *line;
-       char *next = NULL;
-
-       line = strtok_r(file, " ", &next);
-       while (line) {
-               /* current trace_clock is shown as "[local]". */
-               if (*line == '[')
-                       return extract_trace_clock(pevent, line);
-               line = strtok_r(NULL, " ", &next);
-       }
-}
-
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent,
                         char *file, unsigned int size __maybe_unused)
 {
--

2.20.1

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#3 lore raw

Re: [PATCH 1/5] trace-cmd: Move extract trace_clock into trace-input.c

Quoted 8 lines · On Fri, 20 Sep 2019 14:55:21 +0000 Tzvetomir Stoyanov <[email protected]>…

lib/trace-cmd/trace-input.c static int read_and_parse_cmdlines(struct tracecmd_input *handle) · line 2578

Quoted 5 lines
lib/trace-cmd/trace-input.c +4 -0
+static void extract_trace_clock(struct tracecmd_input *handle, char *line)
+{
+       char *data;
+       char *clock;  

I would recommend to initialize clock with NULL, so in case sscanf fails due to broken line or mem alloc, handle->trace_clock will not point to random memory.

Good catch! Thanks, I'll update.

-- Steve

Quoted 9 lines
Quoted 9 lines
lib/trace-cmd/trace-input.c +8 -0
+       char *next = NULL;
+
+       data = strtok_r(line, "[]", &next);
+       sscanf(data, "%ms", &clock);
+       /* TODO: report if it fails to allocate */
+       handle->trace_clock = clock;
+}
+
Reviewed-by Tzvetomir Stoyanov (VMware) on v1 2/5
#8 lore raw

Re: [PATCH 2/5] trace-cmd: Separate out time diff code in trace_show_data()

In reply to Steven Rostedt
Quoted 58 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

To make it easier to convert to the new tep_print_event() format, use the open coded version of the tracing data regardless if a different timestamp is to be used.

---
 tracecmd/trace-read.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tracecmd/trace-read.c +9 -9
index c14e87f76a32..01d904964edf 100644
@@ -757,6 +757,7 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 {
        tracecmd_show_data_func func = tracecmd_get_show_data_func(handle);
        struct tep_handle *pevent;
+       struct tep_event *event;
        struct trace_seq s;
        int cpu = record->cpu;
        bool use_trace_clock;
@@ -793,14 +794,13 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
                }
        }
        use_trace_clock = tracecmd_get_use_trace_clock(handle);
+       event = tep_find_event_by_record(pevent, record);
+       tep_print_event_task(pevent, &s, event, record);
+       tep_print_event_time(pevent, &s, event, record,
+                            use_trace_clock);
        if (tsdiff) {
                unsigned long long rec_ts = record->ts;
-               struct tep_event *event;
 
-               event = tep_find_event_by_record(pevent, record);
-               tep_print_event_task(pevent, &s, event, record);
-               tep_print_event_time(pevent, &s, event, record,
-                                       use_trace_clock);
                buf[0] = 0;
                if (use_trace_clock && !tep_test_flag(pevent, TEP_NSEC_OUTPUT))
                        rec_ts = (rec_ts + 500) / 1000;
@@ -811,9 +811,10 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
                }
                last_ts = rec_ts;
                trace_seq_printf(&s, " %-8s", buf);
-               tep_print_event_data(pevent, &s, event, record);
-       } else
-               tep_print_event(pevent, &s, record, use_trace_clock);
+       }
+
+       tep_print_event_data(pevent, &s, event, record);
+
        if (s.len && *(s.buffer + s.len - 1) == '\n')
                s.len--;
        if (tracecmd_get_debug()) {
--
Quoted 3 lines

2.20.1

Looks good to me.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#4 lore raw

PATCH v2 1/5 trace-cmd: Move extract trace_clock into trace-input.c

In reply to Tzvetomir Stoyanov

From: "Steven Rostedt (VMware)" <[email protected]>

Move the extracting of the trace_clock logic into trace-input.c from trace-util.c, and store it locally in the tracecmd_input handler. This will allow us to remove the trace_clock logic from event_parse.c as it doesn't belong there.

---
Changes since v1:

- Initialized clock to NULL in case scanf failed (Tzvetomir Stoyanov)

 include/trace-cmd/trace-cmd.h |  3 ++-
 lib/trace-cmd/trace-input.c   | 33 +++++++++++++++++++++++++++++++--
 lib/trace-cmd/trace-util.c    | 27 ---------------------------
 3 files changed, 33 insertions(+), 30 deletions(-)
include/trace-cmd/trace-cmd.h +2 -1
index 7a9fbc5f..2d4c6e8f 100644
@@ -18,7 +18,6 @@
 #define TRACECMD_PTR2ERR(ptr)	((unisgned long)(ptr) & ~TRACECMD_ERR_MSK)
 
 void tracecmd_parse_cmdlines(struct tep_handle *pevent, char *file, int size);
-void tracecmd_parse_trace_clock(struct tep_handle *pevent, char *file, int size);
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent, char *file, unsigned int size);
 void tracecmd_parse_ftrace_printk(struct tep_handle *pevent, char *file, unsigned int size);
 
@@ -127,6 +126,8 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
 
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle, char *file, int size);
+
 int tracecmd_make_pipe(struct tracecmd_input *handle, int cpu, int fd, int cpus);
 
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +31 -2
index 8cceb31c..f469118a 100644
@@ -95,6 +95,7 @@ struct tracecmd_input {
 	char *			cpustats;
 	char *			uname;
 	char *			version;
+	char *			trace_clock;
 	struct input_buffer_instance	*buffers;
 	int			parsing_failures;
 
@@ -2571,6 +2572,33 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
 	return 0;
 }
 
+static void extract_trace_clock(struct tracecmd_input *handle, char *line)
+{
+	char *clock = NULL;
+	char *next = NULL;
+	char *data;
+
+	data = strtok_r(line, "[]", &next);
+	sscanf(data, "%ms", &clock);
+	/* TODO: report if it fails to allocate */
+	handle->trace_clock = clock;
+}
+
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
+				char *file, int size __maybe_unused)
+{
+	char *line;
+	char *next = NULL;
+
+	line = strtok_r(file, " ", &next);
+	while (line) {
+		/* current trace_clock is shown as "[local]". */
+		if (*line == '[')
+			return extract_trace_clock(handle, line);
+		line = strtok_r(NULL, " ", &next);
+	}
+}
+
 static int read_and_parse_trace_clock(struct tracecmd_input *handle,
 							struct tep_handle *pevent)
 {
@@ -2580,7 +2608,7 @@ static int read_and_parse_trace_clock(struct tracecmd_input *handle,
 	if (read_data_and_size(handle, &trace_clock, &size) < 0)
 		return -1;
 	trace_clock[size] = 0;
-	tracecmd_parse_trace_clock(pevent, trace_clock, size);
+	tracecmd_parse_trace_clock(handle, trace_clock, size);
 	free(trace_clock);
 	return 0;
 }
@@ -2618,7 +2646,7 @@ int tracecmd_init_data(struct tracecmd_input *handle)
 		if (read_and_parse_trace_clock(handle, pevent) < 0) {
 			char clock[] = "[local]";
 			warning("File has trace_clock bug, using local clock");
-			tracecmd_parse_trace_clock(pevent, clock, 8);
+			tracecmd_parse_trace_clock(handle, clock, 8);
 		}
 	}
 
@@ -3011,6 +3039,7 @@ void tracecmd_close(struct tracecmd_input *handle)
 	free(handle->cpustats);
 	free(handle->cpu_data);
 	free(handle->uname);
+	free(handle->trace_clock);
 	close(handle->fd);
 
 	tracecmd_free_hooks(handle->hooks);
lib/trace-cmd/trace-util.c +0 -27
index c153e4b7..8aa3b6cd 100644
@@ -73,33 +73,6 @@ void tracecmd_parse_cmdlines(struct tep_handle *pevent,
 	}
 }
 
-static void extract_trace_clock(struct tep_handle *pevent, char *line)
-{
-	char *data;
-	char *clock;
-	char *next = NULL;
-
-	data = strtok_r(line, "[]", &next);
-	sscanf(data, "%ms", &clock);
-	tep_register_trace_clock(pevent, clock);
-	free(clock);
-}
-
-void tracecmd_parse_trace_clock(struct tep_handle *pevent,
-				char *file, int size __maybe_unused)
-{
-	char *line;
-	char *next = NULL;
-
-	line = strtok_r(file, " ", &next);
-	while (line) {
-		/* current trace_clock is shown as "[local]". */
-		if (*line == '[')
-			return extract_trace_clock(pevent, line);
-		line = strtok_r(NULL, " ", &next);
-	}
-}
-
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent,
 			 char *file, unsigned int size __maybe_unused)
 {
Signature · 1 line
2.20.1
Reviewed-by Tzvetomir Stoyanov (VMware) on v1 3/5
#10 lore raw

Re: [PATCH 3/5] trace-cmd: Add check for trace_clock using usecs into tracecmd_parse_trace_clock()

In reply to Steven Rostedt
Quoted 45 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

In preparation for using the new tep_print_event() format, move the logic of checking if the trace_clock used should be default as printed in usecs into tracecmd_parse_trace_clock() when it parses the trace_clock.

---
 include/trace-cmd/trace-cmd.h | 1 +
 lib/trace-cmd/trace-input.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
include/trace-cmd/trace-cmd.h +1 -0
index eb36a1f20c30..7c3bc2229461 100644
@@ -87,6 +87,7 @@ enum {
        TRACECMD_FL_IGNORE_DATE         = (1 << 0),
        TRACECMD_FL_BUFFER_INSTANCE     = (1 << 1),
        TRACECMD_FL_LATENCY             = (1 << 2),
+       TRACECMD_FL_IN_USECS            = (1 << 3),
 };
 
 struct tracecmd_ftrace {
lib/trace-cmd/trace-input.c +8 -1
index 5533d42fd7c2..73f27a4e50d0 100644
@@ -2582,6 +2582,14 @@ static void extract_trace_clock(struct tracecmd_input *handle, char *line)
        sscanf(data, "%ms", &clock);
        /* TODO: report if it fails to allocate */
        handle->trace_clock = clock;
+
+       if (!clock)
+               return;
+
+       if (!strcmp(clock, "local") || !strcmp(clock, "global")
+           || !strcmp(clock, "uptime") || !strcmp(clock, "perf")
+           || !strncmp(clock, "mono", 4))
+               handle->flags |= TRACECMD_FL_IN_USECS;
 }
 
 void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
--
Quoted 3 lines

2.20.1

Looks OK.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Reviewed-by Tzvetomir Stoyanov (VMware) on v1 5/5
#17 lore raw

Re: [PATCH 5/5] tools/lib/traceevent: Round up in tep_print_event() time precision

In reply to Steven Rostedt
Quoted 30 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

When testing the output of the old trace-cmd compared to the one that uses the updated tep_print_event() logic, it was different in that the time stamp precision in the old format would round up to the nearest precision, where as the new logic truncates. Bring back the old method of rounding up.

---
 lib/traceevent/event-parse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/traceevent/event-parse.c +3 -2
index d1085aab9c43..7fbbbccab78a 100644
@@ -5527,8 +5527,10 @@ static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
        if (divstr && isdigit(*(divstr + 1)))
                div = atoi(divstr + 1);
        time = record->ts;
-       if (div)
+       if (div) {
+               time += div / 2;
                time /= div;
+       }
        pr = prec;
        while (pr--)
                p10 *= 10;
--
Quoted 3 lines

2.20.1

Looks good to me.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Reviewed-by Tzvetomir Stoyanov (VMware)
#14 lore raw

Re: [PATCH 4/5] libtraceevent, perf tools: Changes in tep_print_event_* APIs

In reply to Steven Rostedt
Quoted 81 lines · Steven Rostedt
Quoted 47 lines · On Tue, 17 Sep 2019 22:03:38 -0400 Steven Rostedt <[email protected]> wrote:

From: Tzvetomir Stoyanov <[email protected]>

Libtraceevent APIs for printing various trace events information are complicated, there are complex extra parameters. To control the way event information is printed, the user should call a set of functions in a specific sequence.

These APIs are reimplemented to provide a more simple interface for printing event information.

Removed APIs:

      tep_print_event_task()
      tep_print_event_time()
      tep_print_event_data()
      tep_event_info()
      tep_is_latency_format()
      tep_set_latency_format()
      tep_data_latency_format()
      tep_set_print_raw()

A new API for printing event information is introduced:
void tep_print_event(struct tep_handle *tep, struct trace_seq *s,

                      struct tep_record *record, const char *fmt, ...);

where "fmt" is a printf-like format string, followed by the event fields to be printed. Supported fields:
TEP_PRINT_PID, "%d" - event PID
TEP_PRINT_CPU, "%d" - event CPU
TEP_PRINT_COMM, "%s" - event command string TEP_PRINT_NAME, "%s" - event name
TEP_PRINT_LATENCY, "%s" - event latency
TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
can be specified as part of this format string:
"%precision.divisord". Example:
"%3.1000d" - divide the time by 1000 and print the first 3 digits before the dot. Thus, the time stamp "123456000" will be printed as "123.456"
TEP_PRINT_INFO, "%s" - event information. TEP_PRINT_INFO_RAW, "%s" - event information, in raw format.

Example:
tep_print_event(tep, s, record, "%16s-%-5d [%03d] %s %6.1000d %s %s",
TEP_PRINT_COMM, TEP_PRINT_PID, TEP_PRINT_CPU, TEP_PRINT_LATENCY, TEP_PRINT_TIME, TEP_PRINT_NAME, TEP_PRINT_INFO);
Output:

      ls-11314 [005] d.h. 185207.366383 function __wake_up
Quoted 11 lines

I needed to modify this with the following changes:

kernel-shark/src/libkshark.c +1 -1
index 47ec9c21..4207ae6f 100644
@@ -1103,7 +1103,7 @@ static const char *kshark_get_latency(struct tep_handle *pe,
                return NULL;
 
        trace_seq_reset(&seq);
-       tep_data_latency_format(pe, &seq, record);
+       tep_print_event(pe, &seq, record, "%s", TEP_PRINT_LATENCY);
        return seq.buffer;
 }
 
@@ -1117,7 +1117,7 @@ static const char *kshark_get_info(struct tep_handle *pe,
                return NULL;

kernel-shark/src/libkshark.c static const char *kshark_get_info(struct tep_handle *pe,

Quoted 10 lines
kernel-shark/src/libkshark.c +1 -2
 
        trace_seq_reset(&seq);
-       tep_event_info(&seq, event, record);
+       tep_print_event(pe, &seq, record, "%s", TEP_PRINT_INFO);
 
        /*
         * The event info string contains a trailing newline.
 
 
-- Steve

Changes looks OK, thanks Steven.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Reviewed-by Tzvetomir Stoyanov (VMware) on v2 1/5
#5 lore raw

Re: [PATCH 1/5 v2] trace-cmd: Move extract trace_clock into trace-input.c

In reply to Steven Rostedt
Quoted 152 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Move the extracting of the trace_clock logic into trace-input.c from trace-util.c, and store it locally in the tracecmd_input handler. This will allow us to remove the trace_clock logic from event_parse.c as it doesn't belong there.

---
Changes since v1:

- Initialized clock to NULL in case scanf failed (Tzvetomir Stoyanov)

 include/trace-cmd/trace-cmd.h |  3 ++-
 lib/trace-cmd/trace-input.c   | 33 +++++++++++++++++++++++++++++++--
 lib/trace-cmd/trace-util.c    | 27 ---------------------------
 3 files changed, 33 insertions(+), 30 deletions(-)
include/trace-cmd/trace-cmd.h +2 -1
index 7a9fbc5f..2d4c6e8f 100644
@@ -18,7 +18,6 @@
 #define TRACECMD_PTR2ERR(ptr)  ((unisgned long)(ptr) & ~TRACECMD_ERR_MSK)
 
 void tracecmd_parse_cmdlines(struct tep_handle *pevent, char *file, int size);
-void tracecmd_parse_trace_clock(struct tep_handle *pevent, char *file, int size);
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent, char *file, unsigned int size);
 void tracecmd_parse_ftrace_printk(struct tep_handle *pevent, char *file, unsigned int size);
 
@@ -127,6 +126,8 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
 
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle, char *file, int size);
+
 int tracecmd_make_pipe(struct tracecmd_input *handle, int cpu, int fd, int cpus);
 
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +31 -2
index 8cceb31c..f469118a 100644
@@ -95,6 +95,7 @@ struct tracecmd_input {
        char *                  cpustats;
        char *                  uname;
        char *                  version;
+       char *                  trace_clock;
        struct input_buffer_instance    *buffers;
        int                     parsing_failures;
 
@@ -2571,6 +2572,33 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
        return 0;
 }
 
+static void extract_trace_clock(struct tracecmd_input *handle, char *line)
+{
+       char *clock = NULL;
+       char *next = NULL;
+       char *data;
+
+       data = strtok_r(line, "[]", &next);
+       sscanf(data, "%ms", &clock);
+       /* TODO: report if it fails to allocate */
+       handle->trace_clock = clock;
+}
+
+void tracecmd_parse_trace_clock(struct tracecmd_input *handle,
+                               char *file, int size __maybe_unused)
+{
+       char *line;
+       char *next = NULL;
+
+       line = strtok_r(file, " ", &next);
+       while (line) {
+               /* current trace_clock is shown as "[local]". */
+               if (*line == '[')
+                       return extract_trace_clock(handle, line);
+               line = strtok_r(NULL, " ", &next);
+       }
+}
+
 static int read_and_parse_trace_clock(struct tracecmd_input *handle,
                                                        struct tep_handle *pevent)
 {
@@ -2580,7 +2608,7 @@ static int read_and_parse_trace_clock(struct tracecmd_input *handle,
        if (read_data_and_size(handle, &trace_clock, &size) < 0)
                return -1;
        trace_clock[size] = 0;
-       tracecmd_parse_trace_clock(pevent, trace_clock, size);
+       tracecmd_parse_trace_clock(handle, trace_clock, size);
        free(trace_clock);
        return 0;
 }
@@ -2618,7 +2646,7 @@ int tracecmd_init_data(struct tracecmd_input *handle)
                if (read_and_parse_trace_clock(handle, pevent) < 0) {
                        char clock[] = "[local]";
                        warning("File has trace_clock bug, using local clock");
-                       tracecmd_parse_trace_clock(pevent, clock, 8);
+                       tracecmd_parse_trace_clock(handle, clock, 8);
                }
        }
 
@@ -3011,6 +3039,7 @@ void tracecmd_close(struct tracecmd_input *handle)
        free(handle->cpustats);
        free(handle->cpu_data);
        free(handle->uname);
+       free(handle->trace_clock);
        close(handle->fd);
 
        tracecmd_free_hooks(handle->hooks);
lib/trace-cmd/trace-util.c +0 -28
index c153e4b7..8aa3b6cd 100644
@@ -73,33 +73,6 @@ void tracecmd_parse_cmdlines(struct tep_handle *pevent,
        }
 }
 
-static void extract_trace_clock(struct tep_handle *pevent, char *line)
-{
-       char *data;
-       char *clock;
-       char *next = NULL;
-
-       data = strtok_r(line, "[]", &next);
-       sscanf(data, "%ms", &clock);
-       tep_register_trace_clock(pevent, clock);
-       free(clock);
-}
-
-void tracecmd_parse_trace_clock(struct tep_handle *pevent,
-                               char *file, int size __maybe_unused)
-{
-       char *line;
-       char *next = NULL;
-
-       line = strtok_r(file, " ", &next);
-       while (line) {
-               /* current trace_clock is shown as "[local]". */
-               if (*line == '[')
-                       return extract_trace_clock(pevent, line);
-               line = strtok_r(NULL, " ", &next);
-       }
-}
-
 void tracecmd_parse_proc_kallsyms(struct tep_handle *pevent,
                         char *file, unsigned int size __maybe_unused)
 {
--
Quoted 2 lines

2.20.1

Looks good, thanks Steven.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center