Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751642AbaACFsk (ORCPT ); Fri, 3 Jan 2014 00:48:40 -0500 Received: from mga01.intel.com ([192.55.52.88]:23246 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751584AbaACFsi (ORCPT ); Fri, 3 Jan 2014 00:48:38 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.95,595,1384329600"; d="scan'208";a="459449413" From: "Yan, Zheng" To: linux-kernel@vger.kernel.org Cc: a.p.zijlstra@chello.nl, mingo@kernel.org, acme@infradead.org, eranian@google.com, andi@firstfloor.org, "Yan, Zheng" Subject: [PATCH 13/14] perf, x86: enable LBR callstack when recording callchain Date: Fri, 3 Jan 2014 13:48:10 +0800 Message-Id: <1388728091-18564-14-git-send-email-zheng.z.yan@intel.com> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1388728091-18564-1-git-send-email-zheng.z.yan@intel.com> References: <1388728091-18564-1-git-send-email-zheng.z.yan@intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4974 Lines: 168 Try enabling the LBR callstack facility if user requests recording user space callchain. Also adds a cpu pmu attribute to enable/disable this feature. This feature is disabled by default because it may contend for the LBR with other events that explicitly require branch stack Signed-off-by: Yan, Zheng --- arch/x86/kernel/cpu/perf_event.c | 99 ++++++++++++++++++++++++++++------------ arch/x86/kernel/cpu/perf_event.h | 7 +++ 2 files changed, 77 insertions(+), 29 deletions(-) diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c index 1509340..3ea184a 100644 --- a/arch/x86/kernel/cpu/perf_event.c +++ b/arch/x86/kernel/cpu/perf_event.c @@ -399,37 +399,49 @@ int x86_pmu_hw_config(struct perf_event *event) if (event->attr.precise_ip > precise) return -EOPNOTSUPP; + } + /* + * check that PEBS LBR correction does not conflict with + * whatever the user is asking with attr->branch_sample_type + */ + if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) { + u64 *br_type = &event->attr.branch_sample_type; + + if (has_branch_stack(event)) { + if (!precise_br_compat(event)) + return -EOPNOTSUPP; + + /* branch_sample_type is compatible */ + + } else { + /* + * user did not specify branch_sample_type + * + * For PEBS fixups, we capture all + * the branches at the priv level of the + * event. + */ + *br_type = PERF_SAMPLE_BRANCH_ANY; + + if (!event->attr.exclude_user) + *br_type |= PERF_SAMPLE_BRANCH_USER; + + if (!event->attr.exclude_kernel) + *br_type |= PERF_SAMPLE_BRANCH_KERNEL; + } + } else if ((event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) && + !has_branch_stack(event) && + x86_pmu.attr_lbr_callstack && + !event->attr.exclude_user && + (event->attach_state & PERF_ATTACH_TASK)) { /* - * check that PEBS LBR correction does not conflict with - * whatever the user is asking with attr->branch_sample_type + * user did not specify branch_sample_type, + * try using the LBR call stack facility to + * record call chains of user program. */ - if (event->attr.precise_ip > 1 && - x86_pmu.intel_cap.pebs_format < 2) { - u64 *br_type = &event->attr.branch_sample_type; - - if (has_branch_stack(event)) { - if (!precise_br_compat(event)) - return -EOPNOTSUPP; - - /* branch_sample_type is compatible */ - - } else { - /* - * user did not specify branch_sample_type - * - * For PEBS fixups, we capture all - * the branches at the priv level of the - * event. - */ - *br_type = PERF_SAMPLE_BRANCH_ANY; - - if (!event->attr.exclude_user) - *br_type |= PERF_SAMPLE_BRANCH_USER; - - if (!event->attr.exclude_kernel) - *br_type |= PERF_SAMPLE_BRANCH_KERNEL; - } - } + event->attr.branch_sample_type = + PERF_SAMPLE_BRANCH_USER | + PERF_SAMPLE_BRANCH_CALL_STACK; } /* @@ -1828,10 +1840,39 @@ static ssize_t set_attr_rdpmc(struct device *cdev, return count; } +static ssize_t get_attr_lbr_callstack(struct device *cdev, + struct device_attribute *attr, char *buf) +{ + return snprintf(buf, 40, "%d\n", x86_pmu.attr_lbr_callstack); +} + +static ssize_t set_attr_lbr_callstack(struct device *cdev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + unsigned long val; + ssize_t ret; + + ret = kstrtoul(buf, 0, &val); + if (ret) + return ret; + + if (!!val != !!x86_pmu.attr_lbr_callstack) { + if (val && !x86_pmu_has_lbr_callstack()) + return -EOPNOTSUPP; + x86_pmu.attr_lbr_callstack = !!val; + } + return count; +} + static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc); +static DEVICE_ATTR(lbr_callstack, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, + get_attr_lbr_callstack, set_attr_lbr_callstack); + static struct attribute *x86_pmu_attrs[] = { &dev_attr_rdpmc.attr, + &dev_attr_lbr_callstack.attr, NULL, }; diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h index 3ed9629..b45258c 100644 --- a/arch/x86/kernel/cpu/perf_event.h +++ b/arch/x86/kernel/cpu/perf_event.h @@ -400,6 +400,7 @@ struct x86_pmu { * sysfs attrs */ int attr_rdpmc; + int attr_lbr_callstack; struct attribute **format_attrs; struct attribute **event_attrs; @@ -504,6 +505,12 @@ static struct perf_pmu_events_attr event_attr_##v = { \ extern struct x86_pmu x86_pmu __read_mostly; +static inline bool x86_pmu_has_lbr_callstack(void) +{ + return x86_pmu.lbr_sel_map && + x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0; +} + DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); int x86_perf_event_set_period(struct perf_event *event); -- 1.8.4.2 -- 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/