public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
To: andi@firstfloor.org
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Jiri Olsa <jolsa@redhat.com>,
	Michael Ellerman <michaele@au1.ibm.com>,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 2/2] powerpc/perf: Implement get_cpu_str()
Date: Thu, 24 Jul 2014 00:47:49 -0700	[thread overview]
Message-ID: <20140724074749.GC18829@us.ibm.com> (raw)
In-Reply-To: <20140724074645.GA18829@us.ibm.com>


[RFC PATCH 2/2] powerpc/perf: Implement get_cpu_str()

get_cpu_str() returns a string identifying the CPU type on the system.
This string is then used to locate a cached JSON file which defines
the list of PMU events supported by the CPU.

Eg: if get_cpu_str() returns "power8", the perf tool would refer to the
PMU events defined in ~/.cache/pmu-events/power8.json.

Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
---
 tools/perf/arch/powerpc/util/header.c |   69 +++++++++++++++++++++++++++++++++
 tools/perf/perf.c                     |   11 ++++++
 tools/perf/perf.h                     |    2 +
 3 files changed, 82 insertions(+)

diff --git a/tools/perf/arch/powerpc/util/header.c b/tools/perf/arch/powerpc/util/header.c
index 6c1b8a7..4d82593 100644
--- a/tools/perf/arch/powerpc/util/header.c
+++ b/tools/perf/arch/powerpc/util/header.c
@@ -3,9 +3,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <elf.h>
+#include <link.h>
 
 #include "../../util/header.h"
 #include "../../util/util.h"
+#include "../../util/jevents.h"
 
 #define mfspr(rn)       ({unsigned long rval; \
 			 asm volatile("mfspr %0," __stringify(rn) \
@@ -15,6 +18,8 @@
 #define PVR_VER(pvr)    (((pvr) >>  16) & 0xFFFF) /* Version field */
 #define PVR_REV(pvr)    (((pvr) >>   0) & 0xFFFF) /* Revison field */
 
+static char *cached_cpu_str;
+
 int
 get_cpuid(char *buffer, size_t sz)
 {
@@ -32,3 +37,67 @@ get_cpuid(char *buffer, size_t sz)
 	}
 	return -1;
 }
+
+static void dup_platform(char *platform)
+{
+	char *bp;
+
+	bp = cached_cpu_str = malloc(128);
+	if (!bp)
+		return;
+
+	/*
+	 * Platform could be POWER8 or POWER8E. Exclude any suffixes
+	 * after the digit(s)
+	 */
+	while (isalpha(*platform))
+		*bp++ = tolower(*platform++);
+
+	while (isdigit(*platform))
+		*bp++ = *platform++;
+	*bp = '\0';
+}
+
+static void *find_auxv(void)
+{
+	char **envp;
+
+	/*
+	 * Exec() copies the AUX Variables after the environment variables.
+	 */
+	envp = environ;
+	while (*envp)
+		envp++;
+	envp++;
+
+	return envp;
+}
+
+void cache_cpu_str(void)
+{
+	ElfW(auxv_t) * auxv;
+
+	auxv = (ElfW(auxv_t)*)find_auxv();
+
+	while (auxv->a_type != AT_NULL) {
+		if (auxv->a_type == AT_BASE_PLATFORM) {
+			dup_platform((char *)auxv->a_un.a_val);
+			return;
+		}
+		auxv++;
+	}
+}
+
+/*
+ * Return an identier string for the CPU on this system.
+ *
+ * On Non-NULL return, assume that the caller will free the
+ * returned string buffer.
+ */
+char *get_cpu_str(void)
+{
+	if (cached_cpu_str)
+		return strdup(cached_cpu_str);
+
+	return NULL;
+}
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 95c58fc..fb9beb0 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -452,6 +452,10 @@ void pthread__unblock_sigwinch(void)
 	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
 }
 
+__attribute__((weak)) void cache_cpu_str(void)
+{
+}
+
 int main(int argc, const char **argv)
 {
 	const char *cmd;
@@ -460,6 +464,13 @@ int main(int argc, const char **argv)
 	page_size = sysconf(_SC_PAGE_SIZE);
 	cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
 
+	/*
+	 * Architectures that rely on AUXV variables to determine
+	 * CPU type must cache the cpu type before setenv() calls
+	 * So do that early.
+	 */
+	cache_cpu_str();
+
 	cmd = perf_extract_argv0_path(argv[0]);
 	if (!cmd)
 		cmd = "perf-help";
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index 510c65f..406fd5c 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -65,4 +65,6 @@ struct record_opts {
 	unsigned     initial_delay;
 };
 
+extern void cache_cpu_str(void);
+
 #endif
-- 
1.7.9.5


  parent reply	other threads:[~2014-07-24  7:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-24  7:46 [RFC PATCH 0/2] powerpc/perf: Implement get_cpu_str() Sukadev Bhattiprolu
2014-07-24  7:47 ` [RFC PATCH 1/2] powerpc/perf: include util/util.h and remove stringify macros Sukadev Bhattiprolu
2014-07-24 14:02   ` Arnaldo Carvalho de Melo
2014-07-28  8:26   ` [tip:perf/core] perf powerpc: Include util/ util.h " tip-bot for Sukadev Bhattiprolu
2014-07-24  7:47 ` Sukadev Bhattiprolu [this message]
2014-07-25  2:55   ` [RFC PATCH 2/2] powerpc/perf: Implement get_cpu_str() Michael Ellerman

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=20140724074749.GC18829@us.ibm.com \
    --to=sukadev@linux.vnet.ibm.com \
    --cc=acme@kernel.org \
    --cc=andi@firstfloor.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michaele@au1.ibm.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