Skip to contentSkip to conversation

tracing: add tgid into common field

PATCH In review Yafang Shao 2 participants 3 messages Oct 13, 2020 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: add tgid into common field

Sometimes we want to trace a specific mutil-threaded process, which may create threads dynamically. Currently it is not easy to trace all its threads, because we can only filter these threads out by using common_pid.

This patch adds the tgid into the common field as well, with which we can easily filter this mutil-threaded process out. E.g.

$ cd /sys/kernel/debug/tracing
$ echo 'common_tgid == 4054' > events/sched/sched_wakeup/filter
$ cat trace_pipe
          python-4057    [005] d... 48003.898560: sched_wakeup: comm=python pid=4054 prio=120 target_cpu=002
          python-4054    [002] dNs. 48003.932906: sched_wakeup: comm=kworker/2:2 pid=130 prio=120 target_cpu=002
          python-4054    [002] dNH. 48003.932907: sched_wakeup: comm=cat pid=4084 prio=120 target_cpu=004
          python-4055    [003] d... 48004.816596: sched_wakeup: comm=python pid=4054 prio=120 target_cpu=002

With record-tgid set into trace_options, we can show the tgid,

$ echo record-tgid > trace_options
$ cat trace_pipe
          python-4054    (   4054) [002] d... 48166.611771: sched_wakeup: comm=python pid=4055 prio=120 target_cpu=004
          python-4057    (   4054) [005] d... 48166.611776: sched_wakeup: comm=python pid=4054 prio=120 target_cpu=002
          python-4055    (   4054) [004] d... 48166.611848: sched_wakeup: comm=python pid=4054 prio=120 target_cpu=002

After that change, tgid_map is only used by saved_tgid, which may be used by some user tools, so I just keep it as-is.

---
 include/linux/trace_events.h | 1 +
 kernel/trace/trace.c         | 1 +
 kernel/trace/trace_events.c  | 1 +
 kernel/trace/trace_output.c  | 2 +-
 4 files changed, 4 insertions(+), 1 deletion(-)
include/linux/trace_events.h +1 -0
index 5c6943354049..3725c05f0b01 100644
@@ -67,6 +67,7 @@ struct trace_entry {
 	unsigned char		flags;
 	unsigned char		preempt_count;
 	int			pid;
+	int			tgid;
 };
 
 #define TRACE_EVENT_TYPE_MAX						\
kernel/trace/trace.c +1 -0
index d3e5de717df2..c2423efaac2c 100644
@@ -2445,6 +2445,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
 
 	entry->preempt_count		= pc & 0xff;
 	entry->pid			= (tsk) ? tsk->pid : 0;
+	entry->tgid			= (tsk) ? tsk->tgid : 0;
 	entry->type			= type;
 	entry->flags =
 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
kernel/trace/trace_events.c +1 -0
index a85effb2373b..9a5adcecf245 100644
@@ -182,6 +182,7 @@ static int trace_define_common_fields(void)
 	__common_field(unsigned char, flags);
 	__common_field(unsigned char, preempt_count);
 	__common_field(int, pid);
+	__common_field(int, tgid);
 
 	return ret;
 }
kernel/trace/trace_output.c +1 -1
index 000e9dc224c6..e04dd45267c7 100644
@@ -591,7 +591,7 @@ int trace_print_context(struct trace_iterator *iter)
 	trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
 
 	if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
-		unsigned int tgid = trace_find_tgid(entry->pid);
+		unsigned int tgid = entry->tgid;
 
 		if (!tgid)
 			trace_seq_printf(s, "(-------) ");
Signature · 1 line
2.17.1
maintainer
#1 lore raw

include/linux/trace_events.h struct trace_entry { · line 72

Quoted 9 lines · On Tue, 13 Oct 2020 13:54:54 +0800 Yafang Shao <[email protected]> wrote:
include/linux/trace_events.h +1 -0
@@ -67,6 +67,7 @@ struct trace_entry {
 	unsigned char		flags;
 	unsigned char		preempt_count;
 	int			pid;
+	int			tgid;
 };
 

Sorry, this adds another 4 bytes to *every* event, if you want it to or not. I'd rather have some ways to remove fields from this header, and not add more to it.

I can't take this patch.

-- Steve

#2 lore raw
Quoted 18 lines · Steven Rostedt

include/linux/trace_events.h struct trace_entry { · line 72

Quoted 9 lines · On Tue, 13 Oct 2020 13:54:54 +0800 Yafang Shao <[email protected]> wrote:
include/linux/trace_events.h +1 -0
@@ -67,6 +67,7 @@ struct trace_entry {
      unsigned char           flags;
      unsigned char           preempt_count;
      int                     pid;
+     int                     tgid;
 };
 

Sorry, this adds another 4 bytes to *every* event, if you want it to or not. I'd rather have some ways to remove fields from this header, and not add more to it.

Quoted 2 lines

I can't take this patch.

Right, that is an overhead. I will think about some ways to avoid adding it.

Signature · 2 lines
Thanks
Yafang