Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp314153pxa; Wed, 19 Aug 2020 01:56:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrvA2ULvghWjVVVwBHLrUNvD1m+PBFSQfwWJHmIxFcqwHHJTFbsLSqUIFRdgH6BrEoLA4b X-Received: by 2002:a05:6402:16:: with SMTP id d22mr24435432edu.175.1597827382673; Wed, 19 Aug 2020 01:56:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597827382; cv=none; d=google.com; s=arc-20160816; b=hoTTPg9dLyFnA5dQuC6tnS92ELdVIWZVwH/MVj0bS58oTNVqJw/MokvbNtaL/bemgT 4a8SDTOa8cNPjmaJFXasRKaVwW+cMzX0rpQ9IJL+kG5oLcC4RoqQFy7hfvF51ifFe+gC W7aSM3Bdr3wLpTGVP9OxcAJcStX7XbqSbfk+uyVD4rSad2HCbDl8aVDwdUFpdhtqkL1N fZ2GcNHSMgglQZ/aQ5aaU/Yisnps2ATmVy2APRTTI4NH82E7FdoxOnsyROrIaWT2TU44 pEOODVbzQGOriMKo1FcbDLdNug12P60UB3Zm9lvdFrZEZ4uiNhy1q1NfVheR39gILdHn IJog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:dkim-signature :dkim-signature:date; bh=VEmp1dCGS2iHgtJ4L7r0DUbF4zfDD8HiadE5pe2z+lU=; b=AdbDZeKhI0Mt0uGuffsv7Ui1fu/pvxfda76CJ7U0haNOB/4LGcwM6NlY6+JSaOWhV7 d7M0qYUqDJ2PE7pj2nIqWdCgm8BDHYixSY3jIQeJ4ByAuc0/MR2FDxF3Z7rNR8YdxjPg IzbPkjBjAWIn1pinqT1tRMhJsfQpuYMXD1RdxUYygtgmWItk7rvSrdY5gNNSsofov/aa 23BPOVaPDgYTNnp5SdTOooNa3MmaI1AqbatWJnW7If5EBZNsMt1/D1nwIbyRQbwUiqvg T7kYUhN/0N/sOTbkOfMU6JODu6GEVhI+UyFMpKD6v4SmcAbdpl4KNgTdTcrK1IUsg8Zl zLRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linutronix.de header.s=2020 header.b=Gu4lCLZ8; dkim=neutral (no key) header.i=@vger.kernel.org header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e16si19002351ejl.531.2020.08.19.01.55.58; Wed, 19 Aug 2020 01:56:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@linutronix.de header.s=2020 header.b=Gu4lCLZ8; dkim=neutral (no key) header.i=@vger.kernel.org header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727849AbgHSIzW (ORCPT + 99 others); Wed, 19 Aug 2020 04:55:22 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:37138 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727018AbgHSIwL (ORCPT ); Wed, 19 Aug 2020 04:52:11 -0400 Date: Wed, 19 Aug 2020 08:52:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1597827128; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VEmp1dCGS2iHgtJ4L7r0DUbF4zfDD8HiadE5pe2z+lU=; b=Gu4lCLZ8FzapLSKi62pzCUFUNcn317YXv87JkOuPE8bC67ZE/6EfdwM9l4waZtHS2gEZWw X1G/iViEqtSGcg5pWSJBGSvKIS0ZdczDBmRrf69V/eUDsC3uYeAARbOveNUBweGb7dyi7o P4Am9iRNlnvO3Fwv8Dg9TZgpltPPcli2OEyBkMvNLRHWiKzanRxapyrlv0epCzReMLFjIG wwRBKm1Qbz1UvsJrTyKsY5vLteqhsY7svwgQXqrKD2Ez0wwK3M2Zc0vzNqtgQaNMskejf3 DnIt/PeiVmqRQw2yydONDG/rGxV7u1c2Kbb5NuojUIK9oSO1QtIoVznsvd+IMA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1597827128; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VEmp1dCGS2iHgtJ4L7r0DUbF4zfDD8HiadE5pe2z+lU=; b=diOnhVLoxRjTpvJ/6j4NV0fTw/rG72kmoXSkMDf8aCh+fpm3N/V7j56WS+pWB0NaTUF8yQ z3emW0B3SWrQIyBA== From: "tip-bot2 for Kan Liang" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/core] perf/x86/intel: Use switch in intel_pmu_disable/enable_event Cc: Kan Liang , "Peter Zijlstra (Intel)" , x86 , LKML In-Reply-To: <20200723171117.9918-7-kan.liang@linux.intel.com> References: <20200723171117.9918-7-kan.liang@linux.intel.com> MIME-Version: 1.0 Message-ID: <159782712833.3192.4230490784691479050.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the perf/core branch of tip: Commit-ID: 58da7dbe6f036fefe504a4bb452afbd39bba73f7 Gitweb: https://git.kernel.org/tip/58da7dbe6f036fefe504a4bb452afbd39bba73f7 Author: Kan Liang AuthorDate: Thu, 23 Jul 2020 10:11:09 -07:00 Committer: Peter Zijlstra CommitterDate: Tue, 18 Aug 2020 16:34:36 +02:00 perf/x86/intel: Use switch in intel_pmu_disable/enable_event Currently, the if-else is used in the intel_pmu_disable/enable_event to check the type of an event. It works well, but with more and more types added later, e.g., perf metrics, compared to the switch statement, the if-else may impair the readability of the code. There is no harm to use the switch statement to replace the if-else here. Also, some optimizing compilers may compile a switch statement into a jump-table which is more efficient than if-else for a large number of cases. The performance gain may not be observed for now, because the number of cases is only 5, but the benefits may be observed with more and more types added in the future. Use switch to replace the if-else in the intel_pmu_disable/enable_event. If the idx is invalid, print a warning. For the case INTEL_PMC_IDX_FIXED_BTS in intel_pmu_disable_event, don't need to check the event->attr.precise_ip. Use return for the case. Signed-off-by: Kan Liang Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20200723171117.9918-7-kan.liang@linux.intel.com --- arch/x86/events/intel/core.c | 36 +++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index ac1408f..76eab81 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2180,17 +2180,28 @@ static void intel_pmu_disable_event(struct perf_event *event) struct hw_perf_event *hwc = &event->hw; int idx = hwc->idx; - if (idx < INTEL_PMC_IDX_FIXED) { + switch (idx) { + case 0 ... INTEL_PMC_IDX_FIXED - 1: intel_clear_masks(event, idx); x86_pmu_disable_event(event); - } else if (idx < INTEL_PMC_IDX_FIXED_BTS) { + break; + case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1: intel_clear_masks(event, idx); intel_pmu_disable_fixed(event); - } else if (idx == INTEL_PMC_IDX_FIXED_BTS) { + break; + case INTEL_PMC_IDX_FIXED_BTS: intel_pmu_disable_bts(); intel_pmu_drain_bts_buffer(); - } else if (idx == INTEL_PMC_IDX_FIXED_VLBR) + return; + case INTEL_PMC_IDX_FIXED_VLBR: intel_clear_masks(event, idx); + break; + default: + intel_clear_masks(event, idx); + pr_warn("Failed to disable the event with invalid index %d\n", + idx); + return; + } /* * Needs to be called after x86_pmu_disable_event, @@ -2262,18 +2273,27 @@ static void intel_pmu_enable_event(struct perf_event *event) if (unlikely(event->attr.precise_ip)) intel_pmu_pebs_enable(event); - if (idx < INTEL_PMC_IDX_FIXED) { + switch (idx) { + case 0 ... INTEL_PMC_IDX_FIXED - 1: intel_set_masks(event, idx); __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); - } else if (idx < INTEL_PMC_IDX_FIXED_BTS) { + break; + case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1: intel_set_masks(event, idx); intel_pmu_enable_fixed(event); - } else if (idx == INTEL_PMC_IDX_FIXED_BTS) { + break; + case INTEL_PMC_IDX_FIXED_BTS: if (!__this_cpu_read(cpu_hw_events.enabled)) return; intel_pmu_enable_bts(hwc->config); - } else if (idx == INTEL_PMC_IDX_FIXED_VLBR) + break; + case INTEL_PMC_IDX_FIXED_VLBR: intel_set_masks(event, idx); + break; + default: + pr_warn("Failed to enable the event with invalid index %d\n", + idx); + } } static void intel_pmu_add_event(struct perf_event *event)