perf session: Register the idle thread in perf_session__process_events

No need for all tools to register it and then immediately call
perf_session__process_events.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
LKML-Reference: <1260741029-4430-3-git-send-email-acme@infradead.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 43e3bb3..93d765a 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -464,14 +464,11 @@
 {
 	struct perf_session *session = perf_session__new(input_name, O_RDONLY,
 							 force);
-	struct thread *idle;
 	int ret;
 
 	if (session == NULL)
 		return -ENOMEM;
 
-	idle = register_idle_thread();
-
 	ret = perf_session__process_events(session, &event_ops, 0,
 					   &event__cwdlen, &event__cwd);
 	if (ret)
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 6f74bd8..37a8499 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -372,7 +372,6 @@
 	if (session == NULL)
 		return -ENOMEM;
 
-	register_idle_thread();
 	err = perf_session__process_events(session, &event_ops, 0,
 					   &event__cwdlen, &event__cwd);
 	perf_session__delete(session);
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index c203eaf..4b37ac4 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -761,7 +761,6 @@
 
 static int __cmd_report(void)
 {
-	struct thread *idle;
 	int ret;
 	struct perf_session *session;
 
@@ -769,9 +768,6 @@
 	if (session == NULL)
 		return -ENOMEM;
 
-	idle = register_idle_thread();
-	thread__comm_adjust(idle);
-
 	if (show_threads)
 		perf_read_values_init(&show_threads_values);
 
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index b5b4472..847ed51 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1670,8 +1670,6 @@
 	if (session == NULL)
 		return -ENOMEM;
 
-	register_idle_thread();
-
 	err = perf_session__process_events(session, &event_ops, 0,
 					   &event__cwdlen, &event__cwd);
 	perf_session__delete(session);
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index c404dec..40cb896 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -125,7 +125,6 @@
 
 static int __cmd_trace(struct perf_session *session)
 {
-	register_idle_thread();
 	return perf_session__process_events(session, &event_ops, 0,
 					    &event__cwdlen, &event__cwd);
 }
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
index ba5bcfa..36e3bfe 100644
--- a/tools/perf/util/data_map.c
+++ b/tools/perf/util/data_map.c
@@ -1,6 +1,7 @@
 #include "symbol.h"
 #include "util.h"
 #include "debug.h"
+#include "thread.h"
 #include "session.h"
 
 static unsigned long	mmap_window = 32;
@@ -127,6 +128,18 @@
 	return err;
 }
 
+static struct thread *perf_session__register_idle_thread(struct perf_session *self __used)
+{
+	struct thread *thread = threads__findnew(0);
+
+	if (!thread || thread__set_comm(thread, "swapper")) {
+		pr_err("problem inserting idle task.\n");
+		thread = NULL;
+	}
+
+	return thread;
+}
+
 int perf_session__process_events(struct perf_session *self,
 				 struct perf_event_ops *ops,
 				 int full_paths, int *cwdlen, char **cwd)
@@ -140,6 +153,9 @@
 	uint32_t size;
 	char *buf;
 
+	if (perf_session__register_idle_thread(self) == NULL)
+		return -ENOMEM;
+
 	perf_event_ops__fill_defaults(ops);
 
 	page_size = getpagesize();
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index b68a00e..5c0ab14 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -161,18 +161,6 @@
 	return th;
 }
 
-struct thread *register_idle_thread(void)
-{
-	struct thread *thread = threads__findnew(0);
-
-	if (!thread || thread__set_comm(thread, "swapper")) {
-		fprintf(stderr, "problem inserting idle task.\n");
-		exit(-1);
-	}
-
-	return thread;
-}
-
 static void map_groups__remove_overlappings(struct map_groups *self,
 					    struct map *map)
 {
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 1751802..2e35e1f 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -24,7 +24,6 @@
 int thread__set_comm(struct thread *self, const char *comm);
 int thread__comm_len(struct thread *self);
 struct thread *threads__findnew(pid_t pid);
-struct thread *register_idle_thread(void);
 void thread__insert_map(struct thread *self, struct map *map);
 int thread__fork(struct thread *self, struct thread *parent);
 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp);