Skip to contentSkip to conversation

RFC [PATCH 0/5] tsc2nsec fixes

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 6 messages Apr 28, 2021

lore
Share & tools
lore
#0 lore raw

Removed the not so stable logic for tsc2nsec offset and replaced it with trace-cmd library APIs which can be used from the library users to achieve the same functionality.
These changes ignore the offset, part of the tsc2nsec data written in the trace file metadata. Is it save to completely remove the offset field and to break the format of this trace file option?

This is a RFC patchset, additional updates are required in case the changes are approved:
- Update trace-cmd documentation.
- Remove offset field from tsc2nsec structures.

Tzvetomir Stoyanov (VMware) (5):
trace-cmd: Remove ts offset from tsc2nsec conversion trace-cmd library: Remove useless check before applying ts offset trace-cmd library: Store the timestamp of the first event when reading
a trace file
trace-cmd library: New API for modifyning the timestamp offset trace-cmd report: New option --align-ts

 include/trace-cmd/trace-cmd.h |  2 ++
 lib/trace-cmd/trace-input.c   | 39 ++++++++++++++++++++--
 tracecmd/trace-read.c         | 25 ++++++++++++--
 tracecmd/trace-record.c       | 62 -----------------------------------
 tracecmd/trace-usage.c        |  1 +
 5 files changed, 61 insertions(+), 68 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/5 trace-cmd: Remove ts offset from tsc2nsec conversion

Do not get, store and use offset when converting TSC to nanoseconds. The logic for automatically detecting the TSC offset is not stable enough and could introduce errors, which leads to problems when reading trace files in some use cases. The offset is not mandatory for tsc2nsec conversion.

---
 lib/trace-cmd/trace-input.c |  1 -
 tracecmd/trace-record.c     | 62 -------------------------------------
 2 files changed, 63 deletions(-)
lib/trace-cmd/trace-input.c +0 -1
index a00fa982..e8a257db 100644
@@ -1319,7 +1319,6 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
 		ts *= handle->ts2secs;
 	} else if (handle->tsc_calc.mult) {
 		/* auto calculated TSC clock frequency */
-		ts -= handle->tsc_calc.offset;
 		ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
 	}
 
tracecmd/trace-record.c +0 -62

80 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/5 trace-cmd library: Remove useless check before applying ts offset

In reply to Tzvetomir Stoyanov (VMware)

Checking if offset is non-zero before adding to the timestamp is useless, as adding 0 will not change the timestamp.

---
 lib/trace-cmd/trace-input.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/trace-cmd/trace-input.c +1 -2
index e8a257db..bacf9ccf 100644
@@ -1323,8 +1323,7 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
 	}
 
 	/* User specified time offset with --ts-offset or --date options */
-	if (handle->ts_offset)
-		ts += handle->ts_offset;
+	ts += handle->ts_offset;
 
 	return ts;
 }
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/5 trace-cmd library: Store the timestamp of the first event when reading a trace file

In reply to Tzvetomir Stoyanov (VMware)

When reading a trace file, detect and store the timestamp of the first recorded event. A new API is introduced to get the first ts from an input handler:
tracecmd_get_first_ts()
The first ts can be used by the library users to align timestamps of the events, when displaying them.

---
 include/trace-cmd/trace-cmd.h |  1 +
 lib/trace-cmd/trace-input.c   | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
include/trace-cmd/trace-cmd.h +1 -0
index 162cd318..022720b0 100644
@@ -37,6 +37,7 @@ int tracecmd_get_guest_cpumap(struct tracecmd_input *handle,
 			      unsigned long long trace_id,
 			      const char **name,
 			      int *vcpu_count, const int **cpu_pid);
+unsigned long long tracecmd_get_first_ts(struct tracecmd_input *handle);
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
 const char *tracecmd_buffer_instance_name(struct tracecmd_input *handle, int indx);
 struct tracecmd_input *tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx);
lib/trace-cmd/trace-input.c +23 -0
index bacf9ccf..284f25e8 100644
@@ -61,6 +61,7 @@ struct cpu_data {
 	unsigned long long	offset;
 	unsigned long long	size;
 	unsigned long long	timestamp;
+	unsigned long long	first_ts;
 	struct list_head	page_maps;
 	struct page_map		*page_map;
 	struct page		**pages;
@@ -2273,6 +2274,7 @@ static int init_cpu(struct tracecmd_input *handle, int cpu)
 
 	if (update_page_info(handle, cpu))
 		goto fail;
+	cpu_data->first_ts = cpu_data->timestamp;
 
 	return 0;
  fail:
@@ -4072,6 +4074,27 @@ unsigned long long tracecmd_get_traceid(struct tracecmd_input *handle)
 	return handle->trace_id;
 }
 
+/**
+ * tracecmd_get_first_ts - get the timestamp of the first recorded event
+ * @handle: input handle for the trace.dat file
+ *
+ * Returns the timestamp of the first recorded event
+ */
+unsigned long long tracecmd_get_first_ts(struct tracecmd_input *handle)
+{
+	unsigned long long ts = 0;
+	bool first = true;
+	int i;
+
+	for (i = 0; i < handle->cpus; i++) {
+		if (first || ts > handle->cpu_data[i].first_ts)
+			ts = handle->cpu_data[i].first_ts;
+		first = false;
+	}
+
+	return ts;
+}
+
 /**
  * tracecmd_get_guest_cpumap - get the mapping of guest VCPU to host process
  * @handle: input handle for the trace.dat file
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/5 trace-cmd library: New API for modifyning the timestamp offset

In reply to Tzvetomir Stoyanov (VMware)

New API is introduced, it can be used to add value to the offset which will be applied to the timestamps of all events from given trace file:
tracecmd_add_ts_offset()

---
 include/trace-cmd/trace-cmd.h |  1 +
 lib/trace-cmd/trace-input.c   | 12 ++++++++++++
 2 files changed, 13 insertions(+)
include/trace-cmd/trace-cmd.h +1 -0
index 022720b0..7305487c 100644
@@ -38,6 +38,7 @@ int tracecmd_get_guest_cpumap(struct tracecmd_input *handle,
 			      const char **name,
 			      int *vcpu_count, const int **cpu_pid);
 unsigned long long tracecmd_get_first_ts(struct tracecmd_input *handle);
+void tracecmd_add_ts_offset(struct tracecmd_input *handle, long long offset);
 int tracecmd_buffer_instances(struct tracecmd_input *handle);
 const char *tracecmd_buffer_instance_name(struct tracecmd_input *handle, int indx);
 struct tracecmd_input *tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx);
lib/trace-cmd/trace-input.c +12 -0
index 284f25e8..6adc8bfa 100644
@@ -2291,6 +2291,18 @@ void tracecmd_set_ts_offset(struct tracecmd_input *handle,
 	handle->ts_offset = offset;
 }
 
+/**
+ * tracecmd_add_ts_offset - Add value to the offset which will be applied to the timestamps of all
+ *			    events from given trace file
+ * @handle: input handle to the trace.dat file
+ * @offset: value, that will be added to the offset
+ */
+void tracecmd_add_ts_offset(struct tracecmd_input *handle,
+			    long long offset)
+{
+	handle->ts_offset += offset;
+}
+
 void tracecmd_set_ts2secs(struct tracecmd_input *handle,
 			 unsigned long long hz)
 {
Signature · 1 line
2.30.2
#5 lore raw

PATCH 5/5 trace-cmd report: New option --align-ts

In reply to Tzvetomir Stoyanov (VMware)

A new option is added:
trace-cmd report --align-ts
It shows timestamps aligned to the first recorded event.

---
 tracecmd/trace-read.c  | 25 ++++++++++++++++++++++---
 tracecmd/trace-usage.c |  1 +
 2 files changed, 23 insertions(+), 3 deletions(-)
tracecmd/trace-read.c +22 -3
index d962b671..4a7bdb9c 100644
@@ -1192,14 +1192,16 @@ enum output_type {
 };
 
 static void read_data_info(struct list_head *handle_list, enum output_type otype,
-			   int global)
+			   int global, int align_ts)
 {
+	unsigned long long ts, first_ts;
 	struct handle_list *handles;
 	struct handle_list *last_handle;
 	struct tep_record *record;
 	struct tep_record *last_record;
 	struct tep_handle *pevent;
 	struct tep_event *event;
+	int first = 1;
 	int ret;
 
 	list_for_each_entry(handles, handle_list, list) {
@@ -1218,7 +1220,12 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype
 		ret = tracecmd_init_data(handles->handle);
 		if (ret < 0)
 			die("failed to init data");
-
+		if (align_ts) {
+			ts = tracecmd_get_first_ts(handles->handle);
+			if (first || first_ts > ts)
+				first_ts = ts;
+			first = 0;
+		}
 		print_handle_file(handles);
 		printf("cpus=%d\n", cpus);
 
@@ -1286,6 +1293,12 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype
 	if (otype != OUTPUT_NORMAL)
 		return;
 
+	if (align_ts) {
+		list_for_each_entry(handles, handle_list, list) {
+			tracecmd_add_ts_offset(handles->handle, -first_ts);
+		}
+	}
+
 	do {
 		last_handle = NULL;
 		last_record = NULL;
@@ -1485,6 +1498,7 @@ static void add_hook(const char *arg)
 }
 
 enum {
+	OPT_align_ts	= 235,
 	OPT_raw_ts	= 236,
 	OPT_version	= 237,
 	OPT_tscheck	= 238,
@@ -1536,6 +1550,7 @@ void trace_report (int argc, char **argv)
 	int nanosec = 0;
 	int no_date = 0;
 	int raw_ts = 0;
+	int align_ts = 0;
 	int global = 0;
 	int neg = 0;
 	int ret = 0;
@@ -1578,6 +1593,7 @@ void trace_report (int argc, char **argv)
 			{"ts-diff", no_argument, NULL, OPT_tsdiff},
 			{"ts-check", no_argument, NULL, OPT_tscheck},
 			{"raw-ts", no_argument, NULL, OPT_raw_ts},
+			{"align-ts", no_argument, NULL, OPT_align_ts},
 			{"help", no_argument, NULL, '?'},
 			{NULL, 0, NULL, 0}
 		};
@@ -1753,6 +1769,9 @@ void trace_report (int argc, char **argv)
 		case OPT_raw_ts:
 			raw_ts = 1;
 			break;
+		case OPT_align_ts:
+			align_ts = 1;
+			break;
 		default:
 			usage(argv);
 		}
@@ -1883,7 +1902,7 @@ void trace_report (int argc, char **argv)
 	/* and version overrides uname! */
 	if (show_version)
 		otype = OUTPUT_VERSION_ONLY;
-	read_data_info(&handle_list, otype, global);
+	read_data_info(&handle_list, otype, global, align_ts);
 
 	list_for_each_entry(handles, &handle_list, list) {
 		tracecmd_close(handles->handle);
tracecmd/trace-usage.c +1 -0
index 98247074..97b3aec0 100644
@@ -234,6 +234,7 @@ static struct usage_help usage_help[] = {
 		"          --ts-diff Show the delta timestamp between events.\n"
 		"          --ts-check Check to make sure no time stamp on any CPU goes backwards.\n"
 		"          --raw-ts Display raw timestamps, without any corrections.\n"
+		"          --align-ts Display timestamps aligned to the first event.\n"
 	},
 	{
 		"stream",
Signature · 1 line
2.30.2