Skip to content

Commit defd8d3

Browse files
dsahernacmel
authored andcommitted
perf tools: Fix comm for processes with named threads
perf does not properly handle monitoring of processes with named threads. For example: $ ps -C myapp -L PID LWP TTY TIME CMD 25118 25118 ? 00:00:00 myapp 25118 25119 ? 00:00:00 myapp:worker perf record -e cs -c 1 -fo /tmp/perf.data -p 25118 -- sleep 10 perf report --stdio -i /tmp/perf.data 100.00% myapp:worker [kernel.kallsyms] [k] perf_event_task_sched_out The process name is set to the name of the last thread it finds for the process. The Problem: perf-top and perf-record both create a thread_map of threads to be monitored. That map is used in perf_event__synthesize_thread_map which loops over the entries in thread_map and calls __event__synthesize_thread to generate COMM and MMAP events. __event__synthesize_thread calls perf_event__synthesize_comm which opens /proc/pid/status, reads the name of the task and its thread group id. That's all fine. The problem is that it then reads /proc/pid/task and generates COMM events for each task it finds - but using the name found in /proc/pid/status where pid is the thread of interest. The end result (looping over thread_map + synthesizing comm events for each thread each time) means the name of the last thread processed sets the name for all threads in the process - which is not good for multithreaded processes with named threads. The Fix: perf_event__synthesize_comm has an input argument (full) that decides whether to process task entries for each pid it is passed. It currently never set to 0 (perf_event__synthesize_comm has a single caller and it always passes the value 1). Let's fix that. Add the full input argument to __event__synthesize_thread which passes it to perf_event__synthesize_comm. For thread/process monitoring set full to 0 which means COMM and MMAP events are only generated for the pid passed to it. For system wide monitoring set full to 1 so that COMM events are generated for all threads in a process. Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1324578603-12762-2-git-send-email-dsahern@gmail.com Signed-off-by: David Ahern <dsahern@gmail.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent fb2bace commit defd8d3

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

‎tools/perf/util/event.c‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,12 @@ int perf_event__synthesize_modules(struct perf_tool *tool,
261261

262262
static int __event__synthesize_thread(union perf_event *comm_event,
263263
union perf_event *mmap_event,
264-
pid_t pid, perf_event__handler_t process,
264+
pid_t pid, int full,
265+
perf_event__handler_t process,
265266
struct perf_tool *tool,
266267
struct machine *machine)
267268
{
268-
pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, 1,
269+
pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
269270
process, machine);
270271
if (tgid == -1)
271272
return -1;
@@ -279,7 +280,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
279280
struct machine *machine)
280281
{
281282
union perf_event *comm_event, *mmap_event;
282-
int err = -1, thread;
283+
int err = -1, thread, j;
283284

284285
comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
285286
if (comm_event == NULL)
@@ -292,11 +293,37 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
292293
err = 0;
293294
for (thread = 0; thread < threads->nr; ++thread) {
294295
if (__event__synthesize_thread(comm_event, mmap_event,
295-
threads->map[thread],
296+
threads->map[thread], 0,
296297
process, tool, machine)) {
297298
err = -1;
298299
break;
299300
}
301+
302+
/*
303+
* comm.pid is set to thread group id by
304+
* perf_event__synthesize_comm
305+
*/
306+
if ((int) comm_event->comm.pid != threads->map[thread]) {
307+
bool need_leader = true;
308+
309+
/* is thread group leader in thread_map? */
310+
for (j = 0; j < threads->nr; ++j) {
311+
if ((int) comm_event->comm.pid == threads->map[j]) {
312+
need_leader = false;
313+
break;
314+
}
315+
}
316+
317+
/* if not, generate events for it */
318+
if (need_leader &&
319+
__event__synthesize_thread(comm_event,
320+
mmap_event,
321+
comm_event->comm.pid, 0,
322+
process, tool, machine)) {
323+
err = -1;
324+
break;
325+
}
326+
}
300327
}
301328
free(mmap_event);
302329
out_free_comm:
@@ -333,7 +360,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
333360
if (*end) /* only interested in proper numerical dirents */
334361
continue;
335362

336-
__event__synthesize_thread(comm_event, mmap_event, pid,
363+
__event__synthesize_thread(comm_event, mmap_event, pid, 1,
337364
process, tool, machine);
338365
}
339366

0 commit comments

Comments
 (0)