Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp261464pxv; Wed, 30 Jun 2021 05:11:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwF3LVqeoGogKYKKjtD6Sr6oEuArsPDNYyqI050DW4KjC4QGIa/up/oMM++1mbYEN/HSV0O X-Received: by 2002:a92:b004:: with SMTP id x4mr26518400ilh.121.1625055090846; Wed, 30 Jun 2021 05:11:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625055090; cv=none; d=google.com; s=arc-20160816; b=0ptWj0+n1RQUOtYZEsbIMk9ko+mbNZjCPAXbLFr6juJmitC5sgN1AQZdszCb3tmAB2 vhbljb/LAp/A4eIf0wZ8CYjmHISBDK+K5YA3YxZuXnneycs1lv/n1O4PXX8fpigRmYfY qi+vMKWprroY4e3BFwBffWxu4c3RUDwKF0Ul5Cvsc8bpK8vziBk4qYwW6we5Lppgim7C DGd2U3sExtO+MIvwBePmm45nCOG4SUjiQOVqa0urm+EQA/GhuAOo2e8DNCkiJPNwJuua d9AFJJB3Ql2A+4XXztR61DrMdKWNGUUlPTq0WYtOWPRCDOx6GzKw0BRKRqvjXk3OVD6k j0og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=kMIOdBaAL049BvVNyWt5yKFxEf0o6jVtLUeLp6+BVbQ=; b=V9bkXKsxPblUIXOQAGZp25VVa8gDITqtTIrGGzczUMAKeZfZKMvtdccvBAGnQnz2VD ksb3MU37d8U4AT+jlBrx6cjwkH974Nz6KOBL+B+vzyCwoqBEEB/DzOfkdw0inAqIq6sX uC9B9bDJfKi8LuoM9/ySsTjStSY2QhZhpdgnhziv2vRyGJim1KylKJib9N+NU9j4dKPg YyGidDZUIzBrYf/kyT2IBAp/TtOTiycRQ2NV38gr+MlK1kep+ETLgA9NQ7lkym2U2WS1 vXA5yA+JALoMzBhRg/52duicrFPyXOIHkl4yIz7q2c9n1/KYeC30ffY4hq6aFYB4IxBc kl9g== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t6si19545000jam.67.2021.06.30.05.11.19; Wed, 30 Jun 2021 05:11:30 -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; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234449AbhF3MNQ (ORCPT + 99 others); Wed, 30 Jun 2021 08:13:16 -0400 Received: from mga03.intel.com ([134.134.136.65]:52431 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234309AbhF3MNP (ORCPT ); Wed, 30 Jun 2021 08:13:15 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10030"; a="208375724" X-IronPort-AV: E=Sophos;i="5.83,311,1616482800"; d="scan'208";a="208375724" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jun 2021 05:10:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,311,1616482800"; d="scan'208";a="455235656" Received: from kbl-ppc.sh.intel.com ([10.239.159.163]) by orsmga008.jf.intel.com with ESMTP; 30 Jun 2021 05:10:30 -0700 From: Jin Yao To: acme@kernel.org, jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com Cc: Linux-kernel@vger.kernel.org, ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com, Jin Yao Subject: [PATCH v2 2/2] perf tools: Fix pattern matching for same substring in different pmu type Date: Wed, 30 Jun 2021 20:09:12 +0800 Message-Id: <20210630120912.6998-2-yao.jin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210630120912.6998-1-yao.jin@linux.intel.com> References: <20210630120912.6998-1-yao.jin@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some different pmu types may have same substring. For example, on Icelake server, we have pmu types "uncore_imc" and "uncore_imc_free_running". Both pmu types have substring "uncore_imc". But the parser would wrongly think they are the same pmu type. We enable an imc event, perf stat -e uncore_imc/event=0xe3/ -a -- sleep 1 Perf actually expands the event to: uncore_imc_0/event=0xe3/ uncore_imc_1/event=0xe3/ uncore_imc_2/event=0xe3/ uncore_imc_3/event=0xe3/ uncore_imc_4/event=0xe3/ uncore_imc_5/event=0xe3/ uncore_imc_6/event=0xe3/ uncore_imc_7/event=0xe3/ uncore_imc_free_running_0/event=0xe3/ uncore_imc_free_running_1/event=0xe3/ uncore_imc_free_running_3/event=0xe3/ uncore_imc_free_running_4/event=0xe3/ That's because the "uncore_imc_free_running" matches the pattern "uncore_imc*". Now we check that the last characters of pmu name is '_'. For pattern "uncore_imc*", "uncore_imc_0" is parsed ok, but "uncore_imc_free_running_0" is failed. Fixes: b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in dynamic pmu events") Signed-off-by: Jin Yao --- tools/perf/util/pmu.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 96f5ff9b5440..9ee123d77e6d 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -741,6 +742,28 @@ struct pmu_events_map *__weak pmu_events_map__find(void) return perf_pmu__find_map(NULL); } +static bool perf_pmu__valid_suffix(char *tok, char *pmu_name) +{ + char *p; + + /* + * The pmu_name has substring tok. If the format of + * pmu_name is tok or tok_digit, return true. + */ + p = pmu_name + strlen(tok); + if (*p == 0) + return true; + + if (*p != '_') + return false; + + ++p; + if (*p == 0 || !isdigit(*p)) + return false; + + return true; +} + bool pmu_uncore_alias_match(const char *pmu_name, const char *name) { char *tmp = NULL, *tok, *str; @@ -769,7 +792,7 @@ bool pmu_uncore_alias_match(const char *pmu_name, const char *name) */ for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { name = strstr(name, tok); - if (!name) { + if (!name || !perf_pmu__valid_suffix(tok, (char *)name)) { res = false; goto out; } @@ -1886,5 +1909,8 @@ int perf_pmu__pattern_match(struct perf_pmu *pmu, char *pattern, char *tok) if (fnmatch(pattern, name, 0)) return -1; + if (!perf_pmu__valid_suffix(tok, name)) + return -1; + return 0; } -- 2.17.1