perf report: Add --num-thread option to control number of thread

The --num-thread is to control number of thread to process events when
--multi-thread option is enabled.  Default value is the number of cpu
on the system that runs perf report.  The config variable
'report.num-thread' is also added to set a different default value.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index 3917710e..c605049 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -356,6 +356,11 @@
 --multi-thread::
 	Speed up report by parallelizing sample processing using multi-thread.
 
+--num-thread::
+	Number of thread to process sample events.  Should be greater than or
+	equals to 2.  Default is number of cpus on the system unless
+	report.num-thread config variable is set.
+
 include::callchain-overhead-calculation.txt[]
 
 SEE ALSO
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 4d08e5f..71ab710 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -54,6 +54,7 @@
 	bool			header;
 	bool			header_only;
 	bool			multi_thread;
+	int			nr_thread;
 	int			max_stack;
 	struct perf_read_values	show_threads_values;
 	const char		*pretty_printing_style;
@@ -89,6 +90,10 @@
 		rep->multi_thread = perf_config_bool(var, value);
 		return 0;
 	}
+	if (!strcmp(var, "report.num-thread")) {
+		rep->nr_thread = perf_config_int(var, value);
+		return 0;
+	}
 
 	return perf_default_config(var, value, cb);
 }
@@ -522,7 +527,8 @@
 
 	if (rep->multi_thread) {
 		rep->tool.sample = process_sample_event_mt;
-		ret = perf_session__process_events_mt(session, rep);
+		ret = perf_session__process_events_mt(session, rep->nr_thread,
+						      rep);
 	} else {
 		ret = perf_session__process_events(session);
 	}
@@ -664,6 +670,7 @@
 		},
 		.max_stack		 = PERF_MAX_STACK_DEPTH,
 		.pretty_printing_style	 = "normal",
+		.nr_thread		 = -1,
 	};
 	const struct option options[] = {
 	OPT_STRING('i', "input", &input_name, "file",
@@ -774,6 +781,8 @@
 			    itrace_parse_synth_opts),
 	OPT_BOOLEAN(0, "multi-thread", &report.multi_thread,
 		    "Speed up sample processing using multi-thead"),
+	OPT_INTEGER(0, "num-thread", &report.nr_thread,
+		    "Number of thread to process samples (>= 2)"),
 	OPT_END()
 	};
 	struct perf_data_file file = {
@@ -820,9 +829,21 @@
 
 	session->itrace_synth_opts = &itrace_synth_opts;
 
-	if (report.multi_thread && !perf_has_index) {
-		pr_debug("fallback to single thread for normal data file.\n");
-		report.multi_thread = false;
+	if (report.multi_thread) {
+		if (!perf_has_index) {
+			pr_debug("fallback to single thread for normal data file.\n");
+			report.multi_thread = false;
+		} else {
+			if (report.nr_thread == -1)
+				report.nr_thread = sysconf(_SC_NPROCESSORS_ONLN);
+			else if (report.nr_thread < 2) {
+				pr_err("invalid number of thread: %d\n",
+				       report.nr_thread);
+				parse_options_usage(report_usage, options,
+						    "num-thread", 0);
+				goto error;
+			}
+		}
 	}
 
 	report.session = session;
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 5f6c319..747d638 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1726,7 +1726,8 @@
 	return arg;
 }
 
-int perf_session__process_events_mt(struct perf_session *session, void *arg)
+int perf_session__process_events_mt(struct perf_session *session,
+				    int nr_thread, void *arg)
 {
 	struct perf_data_file *file = session->file;
 	struct perf_evlist *evlist = session->evlist;
@@ -1742,7 +1743,6 @@
 	int err, i, k;
 	int nr_index = session->header.nr_index;
 	u64 size = perf_data_file__size(file);
-	int nr_thread = sysconf(_SC_NPROCESSORS_ONLN);
 
 	if (perf_data_file__is_pipe(file) || !session->header.index) {
 		pr_err("data file doesn't contain the index table\n");
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index fde658d..a033e10 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -56,7 +56,8 @@
 			     struct perf_sample *sample);
 
 int perf_session__process_events(struct perf_session *session);
-int perf_session__process_events_mt(struct perf_session *session, void *arg);
+int perf_session__process_events_mt(struct perf_session *session,
+				    int nr_thread, void *arg);
 
 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
 			      struct perf_sample *sample, u64 file_offset);