Skip to contentSkip to conversation

perf tools: Don't sort the task scan result from /proc

PATCH In review Namhyung Kim 2 participants 4 messages Jul 1, 2022 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf tools: Don't sort the task scan result from /proc

It should not sort the result as procfs already returns a proper ordering of tasks. Actually sorting the order caused problems that it doesn't guararantee to process the main thread first.

---
 tools/perf/util/synthetic-events.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/synthetic-events.c +2 -2
index 27acdc5e5723..a068f42833c3 100644
@@ -754,7 +754,7 @@ static int __event__synthesize_thread(union perf_event *comm_event,
 	snprintf(filename, sizeof(filename), "%s/proc/%d/task",
 		 machine->root_dir, pid);
 
-	n = scandir(filename, &dirent, filter_task, alphasort);
+	n = scandir(filename, &dirent, filter_task, NULL);
 	if (n < 0)
 		return n;
 
@@ -987,7 +987,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
 		return 0;
 
 	snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
-	n = scandir(proc_path, &dirent, filter_task, alphasort);
+	n = scandir(proc_path, &dirent, filter_task, NULL);
 	if (n < 0)
 		return err;
 
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
author maintainer
#1 lore raw

PATCH 2/2 perf tools: Ignore dead threads during event synthesis

When it synthesize various task events, it scans the list of task first and then accesses later. There's a window threads can die between the two and proc entries may not be available.

Instead of bailing out, we can ignore that thread and move on.

---
 tools/perf/util/synthetic-events.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/synthetic-events.c +3 -2
index a068f42833c3..84d17bd4efae 100644
@@ -767,11 +767,12 @@ static int __event__synthesize_thread(union perf_event *comm_event,
 		if (*end)
 			continue;
 
-		rc = -1;
+		/* some threads may exit just after scan, ignore it */
 		if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
 					     &tgid, &ppid, &kernel_thread) != 0)
-			break;
+			continue;
 
+		rc = -1;
 		if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
 						ppid, process, machine) < 0)
 			break;
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
reviewer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] perf tools: Ignore dead threads during event synthesis

Quoted 8 lines · Namhyung Kim

When it synthesize various task events, it scans the list of task first and then accesses later. There's a window threads can die between the two and proc entries may not be available.

Instead of bailing out, we can ignore that thread and move on.

Thanks,
Ian

Quoted 26 lines
---
 tools/perf/util/synthetic-events.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/synthetic-events.c +3 -3
index a068f42833c3..84d17bd4efae 100644
@@ -767,11 +767,12 @@ static int __event__synthesize_thread(union perf_event *comm_event,
                if (*end)
                        continue;
 
-               rc = -1;
+               /* some threads may exit just after scan, ignore it */
                if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
                                             &tgid, &ppid, &kernel_thread) != 0)
-                       break;
+                       continue;
 
+               rc = -1;
                if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
                                                ppid, process, machine) < 0)
                        break;
--

2.37.0.rc0.161.g10f37bed90-goog

reviewer Acked-by on v1 1/2
#3 lore raw
In reply to Namhyung Kim
Quoted 6 lines · Namhyung Kim

It should not sort the result as procfs already returns a proper ordering of tasks. Actually sorting the order caused problems that it doesn't guararantee to process the main thread first.

Thanks,
Ian

Quoted 29 lines
---
 tools/perf/util/synthetic-events.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/synthetic-events.c +2 -3
index 27acdc5e5723..a068f42833c3 100644
@@ -754,7 +754,7 @@ static int __event__synthesize_thread(union perf_event *comm_event,
        snprintf(filename, sizeof(filename), "%s/proc/%d/task",
                 machine->root_dir, pid);
 
-       n = scandir(filename, &dirent, filter_task, alphasort);
+       n = scandir(filename, &dirent, filter_task, NULL);
        if (n < 0)
                return n;
 
@@ -987,7 +987,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
                return 0;
 
        snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
-       n = scandir(proc_path, &dirent, filter_task, alphasort);
+       n = scandir(proc_path, &dirent, filter_task, NULL);
        if (n < 0)
                return err;
 
--

2.37.0.rc0.161.g10f37bed90-goog