Skip to contentSkip to conversation

tracing: Add support to record and display tgid of tasks

RFC Superseded Joel Fernandes 1 participant 6 messages Jun 4, 2017

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#2 lore raw

RFC v2 2/4 tracing: Add support for recording tgid of tasks

In reply to Joel Fernandes

Inorder to support recording of tgid, the following changes are made:

- Introduce a new API for optionally recording the tgid along with the task's comm which replaces the existing '*cmdline*' APIs.
- reuse the existing sched_switch and sched_wakeup probes
- replace all uses of the old API
- add a new option 'record-tgid' to enable recording of tgid

This will have no memory or runtime overhead if record-tgid option isn't enabled.

---
 include/linux/trace_events.h         | 10 ++++-
 kernel/trace/blktrace.c              |  2 +-
 kernel/trace/trace.c                 | 79 ++++++++++++++++++++++++++++------
 kernel/trace/trace.h                 |  9 +++-
 kernel/trace/trace_events.c          | 83 ++++++++++++++++++++++++++++++------
 kernel/trace/trace_functions.c       |  5 ++-
 kernel/trace/trace_functions_graph.c |  4 +-
 kernel/trace/trace_sched_switch.c    | 67 +++++++++++++++++++++--------
 kernel/trace/trace_selftest.c        |  2 +-
 9 files changed, 208 insertions(+), 53 deletions(-)
include/linux/trace_events.h +8 -2
index a556805eff8a..bc54f1469971 100644
@@ -151,7 +151,11 @@ trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
 				int type, unsigned long len,
 				unsigned long flags, int pc);
 
-void tracing_record_cmdline(struct task_struct *tsk);
+void tracing_record_taskinfo(struct task_struct **tasks, int len, bool cmd,
+			     bool tgid);
+
+void tracing_record_taskinfo_single(struct task_struct *task, bool cmd,
+				    bool tgid);
 
 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
 
@@ -290,6 +294,7 @@ struct trace_subsystem_dir;
 enum {
 	EVENT_FILE_FL_ENABLED_BIT,
 	EVENT_FILE_FL_RECORDED_CMD_BIT,
+	EVENT_FILE_FL_RECORDED_TGID_BIT,
 	EVENT_FILE_FL_FILTERED_BIT,
 	EVENT_FILE_FL_NO_SET_FILTER_BIT,
 	EVENT_FILE_FL_SOFT_MODE_BIT,
@@ -315,6 +320,7 @@ enum {
 enum {
 	EVENT_FILE_FL_ENABLED		= (1 << EVENT_FILE_FL_ENABLED_BIT),
 	EVENT_FILE_FL_RECORDED_CMD	= (1 << EVENT_FILE_FL_RECORDED_CMD_BIT),
+	EVENT_FILE_FL_RECORDED_TGID	= (1 << EVENT_FILE_FL_RECORDED_TGID_BIT),
 	EVENT_FILE_FL_FILTERED		= (1 << EVENT_FILE_FL_FILTERED_BIT),
 	EVENT_FILE_FL_NO_SET_FILTER	= (1 << EVENT_FILE_FL_NO_SET_FILTER_BIT),
 	EVENT_FILE_FL_SOFT_MODE		= (1 << EVENT_FILE_FL_SOFT_MODE_BIT),
@@ -463,7 +469,7 @@ int trace_set_clr_event(const char *system, const char *event, int set);
 #define event_trace_printk(ip, fmt, args...)				\
 do {									\
 	__trace_printk_check_format(fmt, ##args);			\
-	tracing_record_cmdline(current);				\
+	tracing_record_taskinfo_single(current, true, false);		\
 	if (__builtin_constant_p(fmt)) {				\
 		static const char *trace_printk_fmt			\
 		  __attribute__((section("__trace_printk_fmt"))) =	\
kernel/trace/blktrace.c +1 -1
index 193c5f5e3f79..d7394cdf899e 100644
@@ -236,7 +236,7 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
 	cpu = raw_smp_processor_id();
 
 	if (blk_tracer) {
-		tracing_record_cmdline(current);
+		tracing_record_taskinfo_single(current, true, false);
 
 		buffer = blk_tr->trace_buffer.buffer;
 		pc = preempt_count();
kernel/trace/trace.c +65 -14

151 lines not on this page · raw message

kernel/trace/trace.h +7 -2
index 39fd77330aab..17a66d459ee3 100644
@@ -635,8 +635,9 @@ void trace_graph_return(struct ftrace_graph_ret *trace);
 int trace_graph_entry(struct ftrace_graph_ent *trace);
 void set_graph_array(struct trace_array *tr);
 
-void tracing_start_cmdline_record(void);
-void tracing_stop_cmdline_record(void);
+void tracing_alloc_tgid_map(void);
+void tracing_start_taskinfo_record(bool cmd, bool tgid);
+void tracing_stop_taskinfo_record(bool cmd, bool tgid);
 int register_tracer(struct tracer *type);
 int is_tracing_stopped(void);
 
@@ -697,6 +698,7 @@ static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
 extern u64 ftrace_now(int cpu);
 
 extern void trace_find_cmdline(int pid, char comm[]);
+extern unsigned int trace_find_tgid(unsigned int pid);
 extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
 
 #ifdef CONFIG_DYNAMIC_FTRACE
@@ -1107,6 +1109,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 		C(CONTEXT_INFO,		"context-info"),   /* Print pid/cpu/time */ \
 		C(LATENCY_FMT,		"latency-format"),	\
 		C(RECORD_CMD,		"record-cmd"),		\
+		C(RECORD_TGID,		"record-tgid"),		\
 		C(OVERWRITE,		"overwrite"),		\
 		C(STOP_ON_FREE,		"disable_on_free"),	\
 		C(IRQ_INFO,		"irq-info"),		\
@@ -1423,6 +1426,8 @@ struct ftrace_event_field *
 trace_find_event_field(struct trace_event_call *call, char *name);
 
 extern void trace_event_enable_cmd_record(bool enable);
+extern void trace_event_enable_tgid_record(bool enable);
+
 extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
 extern int event_trace_del_tracer(struct trace_array *tr);
 
kernel/trace/trace_events.c +71 -12

132 lines not on this page · raw message

kernel/trace/trace_functions.c +3 -2
index a3bddbfd0874..05da1e7513f6 100644
@@ -103,7 +103,8 @@ static int function_trace_init(struct trace_array *tr)
 	tr->trace_buffer.cpu = get_cpu();
 	put_cpu();
 
-	tracing_start_cmdline_record();
+	tracing_start_taskinfo_record(true, false);
+
 	tracing_start_function_trace(tr);
 	return 0;
 }
@@ -111,7 +112,7 @@ static int function_trace_init(struct trace_array *tr)
 static void function_trace_reset(struct trace_array *tr)
 {
 	tracing_stop_function_trace(tr);
-	tracing_stop_cmdline_record();
+	tracing_stop_taskinfo_record(true, false);
 	ftrace_reset_array_ops(tr);
 }
 
kernel/trace/trace_functions_graph.c +2 -2
index d56123cdcc89..3691198b4778 100644
@@ -524,14 +524,14 @@ static int graph_trace_init(struct trace_array *tr)
 					    &trace_graph_entry);
 	if (ret)
 		return ret;
-	tracing_start_cmdline_record();
+	tracing_start_taskinfo_record(true, false);
 
 	return 0;
 }
 
 static void graph_trace_reset(struct trace_array *tr)
 {
-	tracing_stop_cmdline_record();
+	tracing_stop_taskinfo_record(true, false);
 	unregister_ftrace_graph();
 }
 
kernel/trace/trace_sched_switch.c +50 -17

105 lines not on this page · raw message

kernel/trace/trace_selftest.c +1 -1
index cb917cebae29..83029d94d643 100644
@@ -770,7 +770,7 @@ trace_selftest_startup_function_graph(struct tracer *trace,
 		warn_failed_init_tracer(trace, ret);
 		goto out;
 	}
-	tracing_start_cmdline_record();
+	tracing_start_taskinfo_record(true, false);
 
 	/* Sleep for a 1/10 of a second */
 	msleep(100);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#0 lore raw

v2 cover tracing: Add support to record and display tgid of tasks

Hi Steven, Ingo,

Here is a rework of the last RFC [1] I posted. Its a a complete rewrite from scratch to be more upstream friendly.

Android systrace viewer heavily depends on the tgid to group tasks. tgid is also useful for analyzing traces and generating stats for groups of tasks.

Basically in this approach I try to make sure there is no overhead in terms of runtime or space usage for existing users who don't need tgid. Only if the trace option is enabled will the tgid table which does the mapping be allocated and used.

I tested these and they are showing good results so far, looking forward to getting your feedback and thoughts on them. Thank you.

[1] https://patchwork.kernel.org/patch/9728283/

Joel Fernandes (4):
tracing: Remove unused declaration of trace_stop_cmdline_recording
tracing: Add support for recording tgid of tasks
tracing: Add support for display of tgid in trace output tracing/ftrace: Add support to record and display tgid

 include/linux/trace_events.h         |  10 ++-
 kernel/trace/blktrace.c              |   2 +-
 kernel/trace/trace.c                 | 141 +++++++++++++++++++++++++++--------
 kernel/trace/trace.h                 |   9 ++-
 kernel/trace/trace_events.c          |  84 ++++++++++++++++++---
 kernel/trace/trace_functions.c       |  24 +++++-
 kernel/trace/trace_functions_graph.c |   4 +-
 kernel/trace/trace_output.c          |   9 +++
 kernel/trace/trace_sched_switch.c    |  67 ++++++++++++-----
 kernel/trace/trace_selftest.c        |   2 +-
 10 files changed, 283 insertions(+), 69 deletions(-)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#1 lore raw

RFC v2 1/4 tracing: Remove unused declaration of trace_stop_cmdline_recording

trace_stop_cmdline_recording declaration isn't in use, remove it.

---
 kernel/trace/trace.c | 2 --
 1 file changed, 2 deletions(-)
kernel/trace/trace.c +0 -2
index 1122f151466f..63deff9cdf2c 100644
@@ -1910,8 +1910,6 @@ static void tracing_stop_tr(struct trace_array *tr)
 	raw_spin_unlock_irqrestore(&tr->start_lock, flags);
 }
 
-void trace_stop_cmdline_recording(void);
-
 static int trace_save_cmdline(struct task_struct *tsk)
 {
 	unsigned pid, idx;
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#4 lore raw

RFC v2 3/4 tracing: Add support for display of tgid in trace output

In reply to Joel Fernandes

Earlier patches introduced ability to record the tgid using the 'record-tgid' option. Here we read the tgid and output it if the option is enabled.

---
 kernel/trace/trace.c        | 47 ++++++++++++++++++++++++++++++++-------------
 kernel/trace/trace_output.c |  9 +++++++++
 2 files changed, 43 insertions(+), 13 deletions(-)
kernel/trace/trace.c +34 -13
index 7be21ae4f0a8..5883498754a1 100644
@@ -3294,23 +3294,42 @@ static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
 	seq_puts(m, "#\n");
 }
 
-static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
+static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
+				   unsigned int flags)
 {
 	print_event_info(buf, m);
-	seq_puts(m, "#           TASK-PID   CPU#      TIMESTAMP  FUNCTION\n"
-		    "#              | |       |          |         |\n");
+	if (!(flags & TRACE_ITER_RECORD_TGID)) {
+		seq_puts(m, "#           TASK-PID   CPU#      TIMESTAMP  FUNCTION\n"
+			    "#              | |       |          |         |\n");
+		return;
+	}
+
+	seq_puts(m, "#           TASK-PID   CPU#   TGID       TIMESTAMP  FUNCTION\n"
+		    "#              | |       |      |           |         |\n");
 }
 
-static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
+static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
+				       unsigned int flags)
 {
 	print_event_info(buf, m);
-	seq_puts(m, "#                              _-----=> irqs-off\n"
-		    "#                             / _----=> need-resched\n"
-		    "#                            | / _---=> hardirq/softirq\n"
-		    "#                            || / _--=> preempt-depth\n"
-		    "#                            ||| /     delay\n"
-		    "#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION\n"
-		    "#              | |       |   ||||       |         |\n");
+	if (!(flags & TRACE_ITER_RECORD_TGID)) {
+		seq_puts(m, "#                              _-----=> irqs-off\n"
+			    "#                             / _----=> need-resched\n"
+			    "#                            | / _---=> hardirq/softirq\n"
+			    "#                            || / _--=> preempt-depth\n"
+			    "#                            ||| /     delay\n"
+			    "#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION\n"
+			    "#              | |       |   ||||       |         |\n");
+		return;
+	}
+
+	seq_puts(m, "#                                      _-----=> irqs-off\n"
+		    "#                                     / _----=> need-resched\n"
+		    "#                                    | / _---=> hardirq/softirq\n"
+		    "#                                    || / _--=> preempt-depth\n"
+		    "#                                    ||| /     delay\n"
+		    "#           TASK-PID   CPU#   TGID   ||||    TIMESTAMP  FUNCTION\n"
+		    "#              | |       |      |    ||||       |         |\n");
 }
 
 void
@@ -3626,9 +3645,11 @@ void trace_default_header(struct seq_file *m)
 	} else {
 		if (!(trace_flags & TRACE_ITER_VERBOSE)) {
 			if (trace_flags & TRACE_ITER_IRQ_INFO)
-				print_func_help_header_irq(iter->trace_buffer, m);
+				print_func_help_header_irq(iter->trace_buffer,
+							   m, trace_flags);
 			else
-				print_func_help_header(iter->trace_buffer, m);
+				print_func_help_header(iter->trace_buffer, m,
+						       trace_flags);
 		}
 	}
 }
kernel/trace/trace_output.c +9 -0
index 08f9bab8089e..55c743295874 100644
@@ -587,6 +587,15 @@ int trace_print_context(struct trace_iterator *iter)
 	trace_seq_printf(s, "%16s-%-5d [%03d] ",
 			       comm, entry->pid, iter->cpu);
 
+	if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
+		unsigned int tgid = trace_find_tgid(entry->pid);
+
+		if (!tgid)
+			trace_seq_printf(s, "(-----) ");
+		else
+			trace_seq_printf(s, "(%5d) ", tgid);
+	}
+
 	if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
 		trace_print_lat_fmt(s, entry);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#5 lore raw

RFC v2 4/4 tracing/ftrace: Add support to record and display tgid

In reply to Joel Fernandes

Make function tracer able to record tgid if/when record-tgid is enabled.

---
 kernel/trace/trace_functions.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/trace/trace_functions.c +21 -2
index 05da1e7513f6..290a7a80abf2 100644
@@ -27,6 +27,7 @@ static void
 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
 			  struct ftrace_ops *op, struct pt_regs *pt_regs);
 static struct tracer_flags func_flags;
+static bool tgid_recorded;
 
 /* Our option */
 enum {
@@ -103,7 +104,8 @@ static int function_trace_init(struct trace_array *tr)
 	tr->trace_buffer.cpu = get_cpu();
 	put_cpu();
 
-	tracing_start_taskinfo_record(true, false);
+	tgid_recorded = tr->trace_flags & TRACE_ITER_RECORD_TGID;
+	tracing_start_taskinfo_record(true, tgid_recorded);
 
 	tracing_start_function_trace(tr);
 	return 0;
@@ -112,7 +114,8 @@ static int function_trace_init(struct trace_array *tr)
 static void function_trace_reset(struct trace_array *tr)
 {
 	tracing_stop_function_trace(tr);
-	tracing_stop_taskinfo_record(true, false);
+	tracing_stop_taskinfo_record(true, tgid_recorded);
+	tgid_recorded = false;
 	ftrace_reset_array_ops(tr);
 }
 
@@ -253,6 +256,21 @@ func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
 	return 0;
 }
 
+static int
+func_tracer_flag_changed(struct trace_array *tr, unsigned int mask,
+			 int enabled)
+{
+	if (mask == TRACE_ITER_RECORD_TGID) {
+		tgid_recorded = !!enabled;
+		if (enabled)
+			tracing_start_taskinfo_record(false, true);
+		else
+			tracing_stop_taskinfo_record(false, true);
+	}
+
+	return 0;
+}
+
 static struct tracer function_trace __tracer_data =
 {
 	.name		= "function",
@@ -261,6 +279,7 @@ static struct tracer function_trace __tracer_data =
 	.start		= function_trace_start,
 	.flags		= &func_flags,
 	.set_flag	= func_set_flag,
+	.flag_changed	= func_tracer_flag_changed,
 	.allow_instances = true,
 #ifdef CONFIG_FTRACE_SELFTEST
 	.selftest	= trace_selftest_startup_function,
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#3 lore raw

Re: [RFC v2 2/4] tracing: Add support for recording tgid of tasks

In reply to Joel Fernandes

Some minor things that I will rework in next rev after spending some more time on it:

On Sat, Jun 3, 2017 at 9:03 PM, Joel Fernandes <[email protected]> wrote:
[..]

Quoted 14 lines
include/linux/trace_events.h +1 -1
@@ -463,7 +469,7 @@ int trace_set_clr_event(const char *system, const char *event, int set);
 #define event_trace_printk(ip, fmt, args...)                           \
 do {                                                                   \
        __trace_printk_check_format(fmt, ##args);                       \
-       tracing_record_cmdline(current);                                \
+       tracing_record_taskinfo_single(current, true, false);           \
        if (__builtin_constant_p(fmt)) {                                \
                static const char *trace_printk_fmt                     \
                  __attribute__((section("__trace_printk_fmt"))) =      \
kernel/trace/blktrace.c +0 -0
index 193c5f5e3f79..d7394cdf899e 100644

kernel/trace/blktrace.c static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, · line 239

Quoted 5 lines
kernel/trace/blktrace.c +1 -1
@@ -236,7 +236,7 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
        cpu = raw_smp_processor_id();
 
        if (blk_tracer) {
-               tracing_record_cmdline(current);
+               tracing_record_taskinfo_single(current, true, false);

I think I will try to preserve the existing API so that existing users aren't bothered much.

Quoted 26 lines
kernel/trace/blktrace.c +0 -0
                buffer = blk_tr->trace_buffer.buffer;
                pc = preempt_count();
kernel/trace/trace.c +2 -2
index 63deff9cdf2c..7be21ae4f0a8 100644
@@ -87,7 +87,7 @@ dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  * tracing is active, only save the comm when a trace event
  * occurred.
  */
-static DEFINE_PER_CPU(bool, trace_cmdline_save);
+static DEFINE_PER_CPU(bool, trace_taskinfo_save);
 
 /*
  * Kill all tracing for good (never come back).
@@ -790,7 +790,7 @@ EXPORT_SYMBOL_GPL(tracing_on);
 static __always_inline void
 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
 {
-       __this_cpu_write(trace_cmdline_save, true);
+       __this_cpu_write(trace_taskinfo_save, true);
 
        /* If this is the temp buffer, we need to commit fully */
        if (this_cpu_read(trace_buffered_event) == event) {

kernel/trace/trace.c void tracing_reset_all_online_cpus(void) · line 1718

Quoted 10 lines
kernel/trace/trace.c +7 -0
@@ -1709,6 +1709,15 @@ void tracing_reset_all_online_cpus(void)
        }
 }
 
+static unsigned int *tgid_map;
+
+void tracing_alloc_tgid_map(void)
+{
+       tgid_map = kzalloc((PID_MAX_DEFAULT + 1) * sizeof(*tgid_map),
+                          GFP_KERNEL);
+       WARN_ONCE(!tgid_map, "Allocation of tgid_map failed\n");

I should check if tgid_map is already allocated or there's a chance of re-allocating.

Looking forward to any other comments...

thanks,

-Joel