Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752980AbbH1REG (ORCPT ); Fri, 28 Aug 2015 13:04:06 -0400 Received: from mga02.intel.com ([134.134.136.20]:25548 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751787AbbH1REB (ORCPT ); Fri, 28 Aug 2015 13:04:01 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,425,1437462000"; d="scan'208";a="777614764" From: Kan Liang To: acme@kernel.org, jolsa@kernel.org Cc: a.p.zijlstra@chello.nl, luto@kernel.org, mingo@redhat.com, eranian@google.com, ak@linux.intel.com, mark.rutland@arm.com, adrian.hunter@intel.com, namhyung@kernel.org, linux-kernel@vger.kernel.org, Kan Liang Subject: [PATCH V7 1/8] perf,tools: introduce generic FEAT for CPU attributes Date: Fri, 28 Aug 2015 05:48:02 -0400 Message-Id: <1440755289-30939-2-git-send-email-kan.liang@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1440755289-30939-1-git-send-email-kan.liang@intel.com> References: <1440755289-30939-1-git-send-email-kan.liang@intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5456 Lines: 203 From: Kan Liang This patch introduces generic FEAT for CPU attributes. For the patch set, we only need cpu max frequency. But it can be easily extented to support more other CPU attributes. The cpu max frequency is from the first online cpu. Signed-off-by: Kan Liang --- tools/perf/util/cpumap.c | 32 ++++++++++++++++++++++++++ tools/perf/util/cpumap.h | 1 + tools/perf/util/header.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/header.h | 12 ++++++++++ 4 files changed, 104 insertions(+) diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 3667e21..ef7e57e 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -499,3 +499,35 @@ int cpu__setup_cpunode_map(void) closedir(dir1); return 0; } + +u64 get_cpu_max_freq(void) +{ + const char *mnt; + char path[PATH_MAX], tmp; + FILE *fp; + u64 freq; + int cpu = 0; + int ret; + + mnt = sysfs__mountpoint(); + if (!mnt) + return 0; + + snprintf(path, PATH_MAX, "%s/devices/system/cpu/online", mnt); + fp = fopen(path, "r"); + if (fp) { + ret = fscanf(fp, "%u%c", &cpu, &tmp); + fclose(fp); + if (ret < 1) + return 0; + } + + snprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", mnt, cpu); + fp = fopen(path, "r"); + if (!fp) + return 0; + ret = fscanf(fp, "%lu", &freq); + fclose(fp); + + return (ret == 1) ? freq : 0; +} diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h index 0af9cec..8fd2bd6 100644 --- a/tools/perf/util/cpumap.h +++ b/tools/perf/util/cpumap.h @@ -58,6 +58,7 @@ int max_node_num; int *cpunode_map; int cpu__setup_cpunode_map(void); +u64 get_cpu_max_freq(void); static inline int cpu__max_node(void) { diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 179b2bd..df4461a 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -862,6 +862,23 @@ write_it: return do_write_string(fd, buffer); } +static int write_cpu_attributes(int fd, struct perf_header *h __maybe_unused, + struct perf_evlist *evlist __maybe_unused) +{ + u32 tag_id; + u64 max_freq; + int ret; + + tag_id = PERF_HEADER_CPU_MAX_FREQ; + ret = do_write(fd, &tag_id, sizeof(tag_id)); + if (ret < 0) + return ret; + + max_freq = get_cpu_max_freq(); + + return do_write(fd, &max_freq, sizeof(max_freq)); +} + static int write_branch_stack(int fd __maybe_unused, struct perf_header *h __maybe_unused, struct perf_evlist *evlist __maybe_unused) @@ -1154,6 +1171,11 @@ static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp) fprintf(fp, "# cpuid : %s\n", ph->env.cpuid); } +static void print_cpu_attributes(struct perf_header *ph, int fd __maybe_unused, FILE *fp) +{ + fprintf(fp, "# CPU attributes: max frequency = %lu KHz\n", ph->env.cpu.max_freq); +} + static void print_branch_stack(struct perf_header *ph __maybe_unused, int fd __maybe_unused, FILE *fp) { @@ -1467,6 +1489,42 @@ static int process_cpuid(struct perf_file_section *section __maybe_unused, return ph->env.cpuid ? 0 : -ENOMEM; } +static int process_cpu_attributes(struct perf_file_section *section __maybe_unused, + struct perf_header *ph, int fd, + void *data __maybe_unused) +{ + ssize_t ret; + u32 i, tag_id; + u64 nr; + + for (i = 0; i < PERF_HEADER_CPU_ATTR_MAX; i++) { + + ret = readn(fd, &tag_id, sizeof(tag_id)); + if (ret != sizeof(tag_id)) + return -1; + + if (ph->needs_swap) + nr = bswap_32(tag_id); + + if (tag_id >= PERF_HEADER_CPU_ATTR_MAX) { + pr_debug("The number of cpu attributes is not expected. " + "You may need to upgrade the perf tool.\n"); + return -1; + } + + ret = readn(fd, &nr, sizeof(nr)); + if (ret != sizeof(nr)) + return -1; + + if (ph->needs_swap) + nr = bswap_64(nr); + + ph->env.cpu_attr[tag_id] = nr; + } + + return 0; +} + static int process_total_mem(struct perf_file_section *section __maybe_unused, struct perf_header *ph, int fd, void *data __maybe_unused) @@ -1899,6 +1957,7 @@ static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings), FEAT_OPP(HEADER_GROUP_DESC, group_desc), FEAT_OPP(HEADER_AUXTRACE, auxtrace), + FEAT_OPP(HEADER_CPU_ATTR, cpu_attributes), }; struct header_print_data { diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index 9b53b65..3e49ba6 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -31,6 +31,7 @@ enum { HEADER_PMU_MAPPINGS, HEADER_GROUP_DESC, HEADER_AUXTRACE, + HEADER_CPU_ATTR, HEADER_LAST_FEATURE, HEADER_FEAT_BITS = 256, }; @@ -66,6 +67,11 @@ struct perf_header; int perf_file_header__read(struct perf_file_header *header, struct perf_header *ph, int fd); +enum perf_header_cpu_attr { + PERF_HEADER_CPU_MAX_FREQ = 0, + PERF_HEADER_CPU_ATTR_MAX, +}; + struct perf_session_env { char *hostname; char *os_release; @@ -89,6 +95,12 @@ struct perf_session_env { char *sibling_threads; char *numa_nodes; char *pmu_mappings; + union { + u64 cpu_attr[PERF_HEADER_CPU_ATTR_MAX]; + struct { + u64 max_freq; + } cpu; + }; }; struct perf_header { -- 1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/