From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756916AbcIOW0T (ORCPT ); Thu, 15 Sep 2016 18:26:19 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:35846 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753255AbcIOWZp (ORCPT ); Thu, 15 Sep 2016 18:25:45 -0400 From: Sukadev Bhattiprolu To: Arnaldo Carvalho de Melo Cc: peterz@infradead.org, maddy@linux.vnet.ibm.com, linuxppc-dev@lists.ozlabs.org, Subject: [PATCH v21 03/19] perf, tools: Use pmu_events table to create aliases Date: Thu, 15 Sep 2016 15:24:40 -0700 X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1473978296-20712-1-git-send-email-sukadev@linux.vnet.ibm.com> References: <1473978296-20712-1-git-send-email-sukadev@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16091522-0012-0000-0000-000010ABE102 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00005767; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000185; SDB=6.00758095; UDB=6.00359730; IPR=6.00531733; BA=6.00004731; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00012685; XFM=3.00000011; UTC=2016-09-15 22:25:38 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16091522-0013-0000-0000-00004583EB21 Message-Id: <1473978296-20712-4-git-send-email-sukadev@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-09-15_10:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=2 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609020000 definitions=main-1609150298 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At run time (when 'perf' is starting up), locate the specific table of PMU events that corresponds to the current CPU. Using that table, create aliases for the each of the PMU events in the CPU. The use these aliases to parse the user specified perf event. In short this would allow the user to specify events using their aliases rather than raw event codes. Based on input and some earlier patches from Andi Kleen, Jiri Olsa. Signed-off-by: Sukadev Bhattiprolu Acked-by: Jiri Olsa Acked-by: Ingo Molnar --- Changelog[v4] - Split off unrelated code into separate patches. Changelog[v3] - [Jiri Olsa] Fix memory leak in cpuid Changelog[v2] - [Andi Kleen] Replace pmu_events_map->vfm with a generic "cpuid". --- tools/perf/util/header.h | 1 + tools/perf/util/pmu.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index d306ca1..d30109b 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -151,4 +151,5 @@ int write_padded(int fd, const void *bf, size_t count, size_t count_aligned); */ int get_cpuid(char *buffer, size_t sz); +char *get_cpuid_str(void); #endif /* __PERF_HEADER_H */ diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 2babcdf..c842886 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -12,6 +12,8 @@ #include "pmu.h" #include "parse-events.h" #include "cpumap.h" +#include "header.h" +#include "pmu-events/pmu-events.h" struct perf_pmu_format { char *name; @@ -473,6 +475,62 @@ static struct cpu_map *pmu_cpumask(const char *name) return cpus; } +/* + * Return the CPU id as a raw string. + * + * Each architecture should provide a more precise id string that + * can be use to match the architecture's "mapfile". + */ +char * __weak get_cpuid_str(void) +{ + return NULL; +} + +/* + * From the pmu_events_map, find the table of PMU events that corresponds + * to the current running CPU. Then, add all PMU events from that table + * as aliases. + */ +static int pmu_add_cpu_aliases(struct list_head *head) +{ + int i; + struct pmu_events_map *map; + struct pmu_event *pe; + char *cpuid; + + cpuid = get_cpuid_str(); + if (!cpuid) + return 0; + + i = 0; + while (1) { + map = &pmu_events_map[i++]; + if (!map->table) + goto out; + + if (!strcmp(map->cpuid, cpuid)) + break; + } + + /* + * Found a matching PMU events table. Create aliases + */ + i = 0; + while (1) { + pe = &map->table[i++]; + if (!pe->name) + break; + + /* need type casts to override 'const' */ + __perf_pmu__new_alias(head, NULL, (char *)pe->name, + (char *)pe->desc, (char *)pe->event); + } + +out: + free(cpuid); + return 0; +} + struct perf_event_attr * __weak perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) { @@ -497,6 +555,9 @@ static struct perf_pmu *pmu_lookup(const char *name) if (pmu_aliases(name, &aliases)) return NULL; + if (!strcmp(name, "cpu")) + (void)pmu_add_cpu_aliases(&aliases); + if (pmu_type(name, &type)) return NULL; -- 1.8.3.1