linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Borislav Petkov <bp@suse.de>, David Ahern <dsahern@gmail.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>, Kan Liang <kan.liang@intel.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Stephane Eranian <eranian@google.com>,
	Wang Nan <wangnan0@huawei.com>
Subject: [PATCH 21/27] perf env: Introduce read_cpu_topology_map() method
Date: Mon, 14 Sep 2015 13:38:24 -0300	[thread overview]
Message-ID: <1442248710-2339-22-git-send-email-acme@kernel.org> (raw)
In-Reply-To: <1442248710-2339-1-git-send-email-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Out of the code to write the cpu topology map in the perf.data file
header.

Now if one needs the CPU topology map for the running machine, one needs
to call perf_env__read_cpu_topology_map(perf_env) and the info will be
stored in perf_env.cpu.

For now we're using a global perf_env variable, that will have its
contents freed after we run a builtin.

v2: Check perf_env__read_cpu_topology_map() return in
    write_cpu_topology() (Kan Liang)

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1441828225-667-5-git-send-email-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/perf.c        |  2 ++
 tools/perf/util/env.c    | 28 ++++++++++++++++++++++++++++
 tools/perf/util/env.h    |  2 ++
 tools/perf/util/header.c | 28 ++++++++++++----------------
 4 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index f2fc019b3671..1fded922bcc8 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -8,6 +8,7 @@
  */
 #include "builtin.h"
 
+#include "util/env.h"
 #include "util/exec_cmd.h"
 #include "util/cache.h"
 #include "util/quote.h"
@@ -369,6 +370,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 	status = p->fn(argc, argv, prefix);
 	exit_browser(status);
+	perf_env__exit(&perf_env);
 
 	if (status)
 		return status & 0xff;
diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index ca1e33a2203e..6af4f7c36820 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -1,3 +1,4 @@
+#include "cpumap.h"
 #include "env.h"
 #include "util.h"
 
@@ -56,3 +57,30 @@ out_free:
 out_enomem:
 	return -ENOMEM;
 }
+
+int perf_env__read_cpu_topology_map(struct perf_env *env)
+{
+	int cpu, nr_cpus;
+
+	if (env->cpu != NULL)
+		return 0;
+
+	if (env->nr_cpus_avail == 0)
+		env->nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
+
+	nr_cpus = env->nr_cpus_avail;
+	if (nr_cpus == -1)
+		return -EINVAL;
+
+	env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
+	if (env->cpu == NULL)
+		return -ENOMEM;
+
+	for (cpu = 0; cpu < nr_cpus; ++cpu) {
+		env->cpu[cpu].core_id	= cpu_map__get_core_id(cpu);
+		env->cpu[cpu].socket_id	= cpu_map__get_socket_id(cpu);
+	}
+
+	env->nr_cpus_avail = nr_cpus;
+	return 0;
+}
diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
index d0d1a9681531..0132b9557c02 100644
--- a/tools/perf/util/env.h
+++ b/tools/perf/util/env.h
@@ -39,4 +39,6 @@ void perf_env__exit(struct perf_env *env);
 
 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
 
+int perf_env__read_cpu_topology_map(struct perf_env *env);
+
 #endif /* __PERF_ENV_H */
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index f307b17aa45e..46ec6c5ca47f 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -415,8 +415,6 @@ struct cpu_topo {
 	u32 thread_sib;
 	char **core_siblings;
 	char **thread_siblings;
-	int *core_id;
-	int *phy_pkg_id;
 };
 
 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
@@ -479,9 +477,6 @@ try_threads:
 	}
 	ret = 0;
 done:
-	tp->core_id[cpu] = cpu_map__get_core_id(cpu);
-	tp->phy_pkg_id[cpu] = cpu_map__get_socket_id(cpu);
-
 	if(fp)
 		fclose(fp);
 	free(buf);
@@ -509,7 +504,7 @@ static struct cpu_topo *build_cpu_topology(void)
 	struct cpu_topo *tp;
 	void *addr;
 	u32 nr, i;
-	size_t sz, sz_id;
+	size_t sz;
 	long ncpus;
 	int ret = -1;
 
@@ -520,9 +515,8 @@ static struct cpu_topo *build_cpu_topology(void)
 	nr = (u32)(ncpus & UINT_MAX);
 
 	sz = nr * sizeof(char *);
-	sz_id = nr * sizeof(int);
 
-	addr = calloc(1, sizeof(*tp) + 2 * sz + 2 * sz_id);
+	addr = calloc(1, sizeof(*tp) + 2 * sz);
 	if (!addr)
 		return NULL;
 
@@ -532,10 +526,6 @@ static struct cpu_topo *build_cpu_topology(void)
 	tp->core_siblings = addr;
 	addr += sz;
 	tp->thread_siblings = addr;
-	addr += sz;
-	tp->core_id = addr;
-	addr += sz_id;
-	tp->phy_pkg_id = addr;
 
 	for (i = 0; i < nr; i++) {
 		ret = build_cpu_topo(tp, i);
@@ -554,7 +544,7 @@ static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
 {
 	struct cpu_topo *tp;
 	u32 i;
-	int ret;
+	int ret, j;
 
 	tp = build_cpu_topology();
 	if (!tp)
@@ -579,11 +569,17 @@ static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
 			break;
 	}
 
-	for (i = 0; i < tp->cpu_nr; i++) {
-		ret = do_write(fd, &tp->core_id[i], sizeof(int));
+	ret = perf_env__read_cpu_topology_map(&perf_env);
+	if (ret < 0)
+		goto done;
+
+	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
+		ret = do_write(fd, &perf_env.cpu[j].core_id,
+			       sizeof(perf_env.cpu[j].core_id));
 		if (ret < 0)
 			return ret;
-		ret = do_write(fd, &tp->phy_pkg_id[i], sizeof(int));
+		ret = do_write(fd, &perf_env.cpu[j].socket_id,
+			       sizeof(perf_env.cpu[j].socket_id));
 		if (ret < 0)
 			return ret;
 	}
-- 
2.1.0


  parent reply	other threads:[~2015-09-14 16:40 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-14 16:38 [GIT PULL 00/27] perf/core2 improvements and fixes Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 01/27] perf tests: Take into account address of each objdump line Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 02/27] perf tests: Make objdump disassemble zero blocks Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 03/27] perf tests: Stop reading if objdump output crossed sections Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 04/27] perf tests: Print objdump/dso buffers if they don't match Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 05/27] tools lib api fs: Make tracing_path_strerror_open message generic Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 06/27] tools lib api fs: Replace debugfs/tracefs objects interface with fs.c Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 07/27] tools lib api fs: Remove debugfs, tracefs and findfs objects Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 08/27] perf tools: Switch to tracing_path interface on appropriate places Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 09/27] perf test: Add entry to test cpu topology Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 10/27] perf tests: Introduce iterator function for tests Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 11/27] perf env: Move perf_env out of header.h and session.c into separate object Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 12/27] perf env: Rename some leftovers from rename to perf_env Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 13/27] perf env: Adopt perf_header__set_cmdline Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 14/27] perf hists browser: Fixup the "cpu" column width calculation Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 15/27] perf evsel: Remove forward declaration of 'struct perf_evlist' Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 16/27] perf tools: Add tools/include into tags directories Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 17/27] perf env: Read msr pmu type from header Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 18/27] tools lib api fs: Introduce sysfs__read_{int,ull}() Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 19/27] tools lib api cpu: Introduce cpu.[ch] to obtain cpu related information Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 20/27] perf cpu_map: Use sysfs__read_int in get_{core,socket}_id() Arnaldo Carvalho de Melo
2015-09-14 16:38 ` Arnaldo Carvalho de Melo [this message]
2015-09-14 16:38 ` [PATCH 22/27] perf machine: Add pointer to sample's environment Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 23/27] perf tools: Add processor socket info to hist_entry and addr_location Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 24/27] perf tools: Introduce new sort type "socket" for the processor socket Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 25/27] perf report: Introduce --socket-filter option Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 26/27] perf hists browser: Zoom in/out for processor socket Arnaldo Carvalho de Melo
2015-09-14 16:38 ` [PATCH 27/27] perf test: Add entry for hists socket filter Arnaldo Carvalho de Melo
2015-09-15  6:53 ` [GIT PULL 00/27] perf/core2 improvements and fixes Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1442248710-2339-22-git-send-email-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=bp@suse.de \
    --cc=dsahern@gmail.com \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=kan.liang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).