Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4666958pxv; Tue, 20 Jul 2021 08:55:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZUFjB9RvHAZjMh9gJiuLatq0rWlVnTKkcOv8aGqw8IGejJkdhsW4wO2XH89kEaQFckl/x X-Received: by 2002:a17:906:c20d:: with SMTP id d13mr32577654ejz.259.1626796518232; Tue, 20 Jul 2021 08:55:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626796518; cv=none; d=google.com; s=arc-20160816; b=nkIZKsthSk+FXKDeeangH8WlQ+I9a/+r9XqRa6tukGK/TRZjALyviccCI7PM1xXAIp vQldIFZv5khnDDabcm49uRvG6uB+AVoq9gFjnt9OI45jJbTPRXS529arBAROVLrZuPcy Imw52/o9IFa3DSRTfKeWBERZFkHSjkqjXRCcRRPKs45lkg/sMW2xxU4PgpRdR9webqIp DLHqXalYFzfArG3xh0xGjgX09NgNiP0bNrgiYIaAz7BYGAeG2fK3heuNg1DEoPihTJ0y i4xOChiLUOkNErzzwVbJr072kXiQn16+NY8Q8K5qTaq6vFpPV14IlhaN1RLyu33mlFB2 cO9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=bFJHJh4qsQU0eaATvIOQpsQFTAVzC/9mOhRaKTojlis=; b=yk0sygw74w0R5N1lOwPsMhsQ19Dn8/VazfBL5CKvB8ABbErOkeC3C+FR7Vx3MFJF3+ fcN9hIcSHlMbeKt3IddKxXu6LIw3GjgBbYGhfFGGvBJ4YTWuMbintNkm3CYiWlaeqybo ww2cAqyqFT9joDMqhiHMiLaz0zb9134a61RTWrpERg/A77mRV6sZ40hhUG6d+n+eDKN8 j3CJ2aKeVY5N9xrDC8PPjuItPPS0K2hT3o5pWYPKSfNEQGKFghLDGMZbElLLWPZwB6Hy kOhh4tUdWpnSYmeKxkA2RzmVVKDe4zGnyMKB5iHztAZmhvinji4nFzWubGvPhMEVQB4p e+uQ== 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=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gh10si21279992ejc.319.2021.07.20.08.54.54; Tue, 20 Jul 2021 08:55:18 -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=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241867AbhGTPKz (ORCPT + 99 others); Tue, 20 Jul 2021 11:10:55 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3438 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239809AbhGTOed (ORCPT ); Tue, 20 Jul 2021 10:34:33 -0400 Received: from fraeml707-chm.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GThmp1tPJz6J7Cw; Tue, 20 Jul 2021 23:03:38 +0800 (CST) Received: from lhreml724-chm.china.huawei.com (10.201.108.75) by fraeml707-chm.china.huawei.com (10.206.15.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 20 Jul 2021 17:15:06 +0200 Received: from localhost.localdomain (10.69.192.58) by lhreml724-chm.china.huawei.com (10.201.108.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 20 Jul 2021 16:15:02 +0100 From: John Garry To: , , , , , , , , , CC: , , "John Garry" Subject: [PATCH] perf pmu: Fix alias matching Date: Tue, 20 Jul 2021 23:10:19 +0800 Message-ID: <1626793819-79090-1-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.69.192.58] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To lhreml724-chm.china.huawei.com (10.201.108.75) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit c47a5599eda32 ("perf tools: Fix pattern matching for same substring in different PMU type"), may have fixed some alias matching, but has broken some others. Firstly it cannot handle the simple scenario of PMU name in form pmu_name{digits} - it can only handle pmu_name_{digits}. Secondly it cannot handle more complex matching in the case where we have multiple tokens. In this scenario, the code failed to realise that we may examine multiple substrings in the PMU name. Fix in two ways: - Change perf_pmu__valid_suffix() to accept a PMU name without '_' in the suffix - Only pay attention to perf_pmu__valid_suffix() for the final token Also add const qualifiers as necessary to avoid casting. Fixes: c47a5599eda3 ("perf tools: Fix pattern matching for same substring in different PMU type") Signed-off-by: John Garry --- @Jin Yao, please test for your scenarios Note: About any effect in perf_pmu__match() -> perf_pmu__valid_suffix() callchain, this seems to be called for wildcard in PMU names in metric expressions. We don't have any metrics for arm64 which use feature. However, I hacked an existing metric to use a wildcard and it looks ok. Also the "DRAM_BW_Use" metric on my broadwell uses this feature, and it looks ok. diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index a1bd7007a8b4..fc683bc41715 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -742,9 +742,13 @@ struct pmu_events_map *__weak pmu_events_map__find(void) return perf_pmu__find_map(NULL); } -static bool perf_pmu__valid_suffix(char *pmu_name, char *tok) +/* + * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name + * to be valid. + */ +static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok) { - char *p; + const char *p; if (strncmp(pmu_name, tok, strlen(tok))) return false; @@ -753,12 +757,16 @@ static bool perf_pmu__valid_suffix(char *pmu_name, char *tok) if (*p == 0) return true; - if (*p != '_') - return false; + if (*p == '_') + ++p; - ++p; - if (*p == 0 || !isdigit(*p)) - return false; + /* Ensure we end in a number */ + while (1) { + if (!isdigit(*p)) + return false; + if (*(++p) == 0) + break; + } return true; } @@ -789,12 +797,19 @@ bool pmu_uncore_alias_match(const char *pmu_name, const char *name) * match "socket" in "socketX_pmunameY" and then "pmuname" in * "pmunameY". */ - for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { + while (1) { + char *next_tok = strtok_r(NULL, ",", &tmp); + name = strstr(name, tok); - if (!name || !perf_pmu__valid_suffix((char *)name, tok)) { + if (!name || + (!next_tok && !perf_pmu__valid_suffix(name, tok))) { res = false; goto out; } + if (!next_tok) + break; + tok = next_tok; + name += strlen(tok); } res = true; -- 2.26.2