[40/54] tracing: Add a clock attribute for hist triggers
diff mbox series

Message ID TY1PR01MB16921E957B15E4CFB454B2A496090@TY1PR01MB1692.jpnprd01.prod.outlook.com
State New
Headers show
Series
  • [01/54] tracing: Remove redundant unread variable ret
Related show

Commit Message

Motai.Hirotaka@aj.MitsubishiElectric.co.jp Aug. 29, 2018, 12:17 p.m. UTC
The default clock if timestamps are used in a histogram is "global".
If timestamps aren't used, the clock is irrelevant.

Use the "clock=" param only if you want to override the default
"global" clock for a histogram with timestamps.

Link: http://lkml.kernel.org/r/427bed1389c5d22aa40c3e0683e30cc3d151e260.1516069914.git.tom.zanussi@linux.intel.com

Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com>
Signed-off-by: Rajvi Jingar <rajvi.jingar@intel.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
(cherry picked from commit a4072fe85ba3671720cab0788291af953db27318)
Signed-off-by: Hirotaka MOTAI <Motai.Hirotaka@aj.MitsubishiElectric.co.jp>
---
 Documentation/trace/histogram.txt | 11 +++++++-
 kernel/trace/trace_events_hist.c  | 42 ++++++++++++++++++++++++++++---
 2 files changed, 49 insertions(+), 4 deletions(-)

Patch
diff mbox series

diff --git a/Documentation/trace/histogram.txt b/Documentation/trace/histogram.txt
index df08882d..6e05510a 100644
--- a/Documentation/trace/histogram.txt
+++ b/Documentation/trace/histogram.txt
@@ -1666,17 +1666,26 @@  features have been added to the hist trigger support:
     underlying ftrace ring buffer.  This timestamp is now exposed as a
     a synthetic field named 'common_timestamp' which can be used in
     histograms as if it were any other event field; it isn't an actual
     field in the trace format but rather is a synthesized value that
     nonetheless can be used as if it were an actual field.  By default
     it is in units of nanoseconds; appending '.usecs' to a
     common_timestamp field changes the units to microseconds.
 
-These features are decribed in more detail in the following sections.
+A note on inter-event timestamps: If common_timestamp is used in a
+histogram, the trace buffer is automatically switched over to using
+absolute timestamps and the "global" trace clock, in order to avoid
+bogus timestamp differences with other clocks that aren't coherent
+across CPUs.  This can be overridden by specifying one of the other
+trace clocks instead, using the "clock=XXX" hist trigger attribute,
+where XXX is any of the clocks listed in the tracing/trace_clock
+pseudo-file.
+
+These features are described in more detail in the following sections.
 
 2.2.1 Histogram Variables
 -------------------------
 
 Variables are simply named locations used for saving and retrieving
 values between matching events.  A 'matching' event is defined as an
 event that has a matching key - if a variable is saved for a histogram
 entry corresponding to that key, any subsequent event with a matching
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 8719b0ea..f7d0da20 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -237,16 +237,17 @@  struct var_defs {
 	char		*expr[TRACING_MAP_VARS_MAX];
 };
 
 struct hist_trigger_attrs {
 	char		*keys_str;
 	char		*vals_str;
 	char		*sort_key_str;
 	char		*name;
+	char		*clock;
 	bool		pause;
 	bool		cont;
 	bool		clear;
 	bool		ts_in_usecs;
 	unsigned int	map_bits;
 
 	char		*assignment_str[TRACING_MAP_VARS_MAX];
 	unsigned int	n_assignments;
@@ -1771,16 +1772,17 @@  static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
 
 	for (i = 0; i < attrs->n_actions; i++)
 		kfree(attrs->action_str[i]);
 
 	kfree(attrs->name);
 	kfree(attrs->sort_key_str);
 	kfree(attrs->keys_str);
 	kfree(attrs->vals_str);
+	kfree(attrs->clock);
 	kfree(attrs);
 }
 
 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
 {
 	int ret = -EINVAL;
 
 	if (attrs->n_actions >= HIST_ACTIONS_MAX)
@@ -1826,16 +1828,29 @@  static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
 			goto out;
 		}
 	} else if (strncmp(str, "name=", strlen("name=")) == 0) {
 		attrs->name = kstrdup(str, GFP_KERNEL);
 		if (!attrs->name) {
 			ret = -ENOMEM;
 			goto out;
 		}
+	} else if (strncmp(str, "clock=", strlen("clock=")) == 0) {
+		strsep(&str, "=");
+		if (!str) {
+			ret = -EINVAL;
+			goto out;
+		}
+
+		str = strstrip(str);
+		attrs->clock = kstrdup(str, GFP_KERNEL);
+		if (!attrs->clock) {
+			ret = -ENOMEM;
+			goto out;
+		}
 	} else if (strncmp(str, "size=", strlen("size=")) == 0) {
 		int map_bits = parse_map_size(str);
 
 		if (map_bits < 0) {
 			ret = map_bits;
 			goto out;
 		}
 		attrs->map_bits = map_bits;
@@ -1890,16 +1905,24 @@  static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
 		}
 	}
 
 	if (!attrs->keys_str) {
 		ret = -EINVAL;
 		goto free;
 	}
 
+	if (!attrs->clock) {
+		attrs->clock = kstrdup("global", GFP_KERNEL);
+		if (!attrs->clock) {
+			ret = -ENOMEM;
+			goto free;
+		}
+	}
+
 	return attrs;
  free:
 	destroy_hist_trigger_attrs(attrs);
 
 	return ERR_PTR(ret);
 }
 
 static inline void save_comm(char *comm, struct task_struct *task)
@@ -4929,16 +4952,18 @@  static int event_hist_trigger_print(struct seq_file *m,
 				idx += hist_data->n_vars;
 			hist_field_print(m, hist_data->fields[idx]);
 		}
 
 		if (sort_key->descending)
 			seq_puts(m, ".descending");
 	}
 	seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
+	if (hist_data->enable_timestamps)
+		seq_printf(m, ":clock=%s", hist_data->attrs->clock);
 
 	print_actions_spec(m, hist_data);
 
 	if (data->filter_str)
 		seq_printf(m, " if %s", data->filter_str);
 
 	if (data->paused)
 		seq_puts(m, " [paused]");
@@ -5196,32 +5221,43 @@  static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
 		ret = -ENOENT;
 		goto out;
 	}
 
 	if (hist_data->attrs->pause)
 		data->paused = true;
 
 	if (named_data) {
-		destroy_hist_data(data->private_data);
 		data->private_data = named_data->private_data;
 		set_named_trigger_data(data, named_data);
 		data->ops = &event_hist_trigger_named_ops;
 	}
 
 	if (data->ops->init) {
 		ret = data->ops->init(data->ops, data);
 		if (ret < 0)
 			goto out;
 	}
 
-	ret++;
+	if (hist_data->enable_timestamps) {
+		char *clock = hist_data->attrs->clock;
+
+		ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
+		if (ret) {
+			hist_err("Couldn't set trace_clock: ", clock);
+			goto out;
+		}
 
-	if (hist_data->enable_timestamps)
 		tracing_set_time_stamp_abs(file->tr, true);
+	}
+
+	if (named_data)
+		destroy_hist_data(hist_data);
+
+	ret++;
  out:
 	return ret;
 }
 
 static int hist_trigger_enable(struct event_trigger_data *data,
 			       struct trace_event_file *file)
 {
 	int ret = 0;