Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp680547rdb; Thu, 8 Feb 2024 19:16:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVh3dz2nJgOaA0Kck3jJws52YImwpdfrx6I9OWNth4XdCR51RnmKM2z1TSSm7Qt+rTFPsZbP0/j4lbqa6eio8TMVMzNdeLUgLEvAZ1t8A== X-Google-Smtp-Source: AGHT+IHMLM1TKw61HDHhRcc5NJvbs/Ygo2FRQ2ber4KGkGABH+cnwTE5iNjsu0YVhw2kt2cuDLcA X-Received: by 2002:a17:903:48e:b0:1d9:5038:f115 with SMTP id jj14-20020a170903048e00b001d95038f115mr418391plb.4.1707448581427; Thu, 08 Feb 2024 19:16:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707448581; cv=pass; d=google.com; s=arc-20160816; b=j//im30xbbwlkVOGmb/KImmjowIX4KRRVVRl4cytS2O+ucT+100NYUYPVZLVEnSHbI JfJFvshJp6fFrc06h+3jPxQ86pn436Tzl1JRAMuRQsYccVnXHf6UL0ltDmE/ZxoS5Wxb FaMdmjXi4U7I6+aJpkL3m3oz3EU31FgmNywA58YSP5rR+jCSscpzk2jPZcerHa3JMs6/ Hd/P3Wh2LFc0iV+DPQdWlzWs8oKV0OcaEpngG4nN3GAq43QORDTIdcpFdcOCcx4UBygl eoRBAabRxnmvpAfyVG4TSDAPeHunIcwoEJuaJsg9LuqmpPM2ttrh/7mnSqZ4HzqSfIjl hXQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=GCyjOvfA4YRBXobswdIgkZQFMEOT56eCvfyLjzSS9jc=; fh=KSXTu4niUF4RwwK1eztcfCz6WvNE54qwAYSN2oMlT3Y=; b=YXzkTErg01f47jivPhf6NH9Xoa+7I/HpI/TXTUMAiV9Fx8OYbtAyR8IFhsQcLQtfCf UqVoJfsWl7cudcyWBm3xC9lVvYGkPNRrgzlQ/4+SL5HDoOBwqFGQbARkC5gTtWkln97h 9E/IRMkwh/p3pFma+z7OUpOGPsmuxee5CmFH+T7QiVy9V+v+HCvVg9Hw/LvTlpChJZ5k l3BW3De5HTEC2MHZpPT1lNfD7Xp8X1bKKM2Kc4e6HMCTKkCQCS/e1NlPkDDMuVvaeAkI fddpASrxNoOvd7aCEPdPh9J8f3peG2UsoCtNg+BmKWDT3SOw5/8hKbn78GYY3HZTXFcv i1nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="WrddKb/4"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-58972-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58972-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCUPaKtM+SEK9X8w7SN4VtK/CvtpzE01aXPz3wiiVoynIzgJ5mCY0hTJFa/lPi2NoRBbRtmhmT5Bf3j0iblF0C8LrRDoYocaUfX90dwPQA== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h6-20020a170902eec600b001d957283cb4si805445plb.318.2024.02.08.19.16.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 19:16:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58972-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="WrddKb/4"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-58972-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58972-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 15A4E284E2F for ; Fri, 9 Feb 2024 03:16:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D06C16410; Fri, 9 Feb 2024 03:14:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WrddKb/4" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6810E611E; Fri, 9 Feb 2024 03:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707448494; cv=none; b=UtAeKyECutJ/PpqFDGxWn7LRPhuVJ6fPJvN08UEEfqYYpFRPEwkXQNkRe+qbioYzmkqAdlt5wEtylPTgO6IszVU4gx64Q++v03VnEcmm/HX/33fckmDpfO8hEYQQy5bGctGPc9q1C8lKxHnOqGIMADavfd8xW/6ZO8MEF86sOCw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707448494; c=relaxed/simple; bh=Ptn1JpRP3StyuOfIWKSzj6sKJoeJjAtlLJUNbm0+pS8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sVe/Sbr5XQjX4IrJSmot5k/NJw1EH3JT3uPagMZGzNilidYRYIesyCSs4BsCLUQqy9Pm4iw3pCUrQaTFv/HGiRa8TnnAF1f3TejANvzD2N6n2XW/QSmiMKZe1QBoYk70nqDx2yShoaoz5G9hEV5W+9obCH5PB0L+0DDzlGoLE6g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WrddKb/4; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707448493; x=1738984493; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ptn1JpRP3StyuOfIWKSzj6sKJoeJjAtlLJUNbm0+pS8=; b=WrddKb/4rSojKTkCLJ5XBYuTuEUd2qohsIjSPd31BdD5Poais91vEMKy eA6EsC52iI2mvlEJNcNyG/kJdAgrtk7af8DJTHz+qMabCZNTH5rp8YGxh 5GNuMsRMvjvOvuvcR0Do00HyMfO5e3yUoC7gi/3E9WJIVoEP8MoHCg3gm LQ9T6W89Vc5Nati46u4zKM/9XT0iqWqrNYPGJiCW5Q4Ey9i6BVg6F767R /eTJOkeUd8cT/iF32zNL2B34CdYzeSD3zBOjF/MhwcySMrKssBeeUCyZ0 MiQhBKWLwUoS0MO2VfWp5y2zkQ1T6ANoa8zm0oUP7Wj2NNNdxml9lsJdP Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10978"; a="1257909" X-IronPort-AV: E=Sophos;i="6.05,255,1701158400"; d="scan'208";a="1257909" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2024 19:14:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,255,1701158400"; d="scan'208";a="32631428" Received: from b49691a74b80.jf.intel.com ([10.165.54.183]) by orviesa002.jf.intel.com with ESMTP; 08 Feb 2024 19:14:47 -0800 From: weilin.wang@intel.com To: weilin.wang@intel.com, Ian Rogers , Kan Liang , Namhyung Kim , Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Alexander Shishkin , Jiri Olsa , Adrian Hunter Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Perry Taylor , Samantha Alt , Caleb Biggers , Mark Rutland Subject: [RFC PATCH v4 06/15] perf stat: Add functions to get counter info Date: Thu, 8 Feb 2024 19:14:32 -0800 Message-ID: <20240209031441.943012-7-weilin.wang@intel.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240209031441.943012-1-weilin.wang@intel.com> References: <20240209031441.943012-1-weilin.wang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Weilin Wang Add data structure metricgroup__pmu_counters to represent hardware counters available in the system. Add functions to parse pmu-events and create the list of pmu_info_list to hold the counter information of the system. Add functions to free pmu_info_list and event_info_list before exit grouping for hardware-grouping method This method would fall back to normal grouping when event json files do not support hardware aware grouping. Signed-off-by: Weilin Wang --- tools/perf/util/metricgroup.c | 101 ++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 2a917220fb34..9061ed4ca015 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -179,6 +179,20 @@ struct metricgroup__event_info { DECLARE_BITMAP(counters, NR_COUNTERS); }; +/** + * A node is the counter availability of a pmu. + * This info is built up at the beginning from JSON file and + * used as a reference in metric grouping process. + */ +struct metricgroup__pmu_counters { + struct list_head nd; + /** The name of the pmu the event collected on. */ + const char *name; + /** The number of gp counters in the pmu. */ + size_t num_counters; + size_t num_fixed_counters; +}; + /** * Each group is one node in the group string list. */ @@ -1530,6 +1544,27 @@ static int parse_counter(const char *counter, return 0; } +static void metricgroup__free_event_info(struct list_head + *event_info_list) +{ + struct metricgroup__event_info *e, *tmp; + + list_for_each_entry_safe(e, tmp, event_info_list, nd) { + list_del_init(&e->nd); + free(e); + } +} + +static void metricgroup__free_pmu_info(struct list_head *pmu_info_list) +{ + struct metricgroup__pmu_counters *p, *tmp; + + list_for_each_entry_safe(p, tmp, pmu_info_list, nd) { + list_del_init(&p->nd); + free(p); + } +} + static struct metricgroup__event_info *event_info__new(const char *name, const char *pmu_name, const char *counter, @@ -1548,7 +1583,7 @@ static struct metricgroup__event_info *event_info__new(const char *name, e->name = name; e->free_counter = free_counter; - e->pmu_name = strdup(pmu_name); + e->pmu_name = pmu_name; if (free_counter) { ret = set_counter_bitmap(0, e->counters); if (ret) @@ -1583,7 +1618,9 @@ static int metricgroup__add_metric_event_callback(const struct pmu_event *pe, struct metricgroup__add_metric_event_data *d = data; if (!strcasecmp(pe->name, d->event_name)) { - event = event_info__new(d->event_id, pe->pmu, pe->counter, /*free_counter=*/false); + if (!pe->counters) + return -EINVAL; + event = event_info__new(d->event_id, pe->pmu, pe->counters, /*free_counter=*/false); if (!event) return -ENOMEM; list_add(&event->nd, d->list); @@ -1622,7 +1659,7 @@ static int get_metricgroup_events(const char *full_id, .event_name = id, .event_id = full_id, }; - ret = pmu_events_table_for_each_event(table, + ret = pmu_events_table__for_each_event(table, /*pmu=*/NULL, metricgroup__add_metric_event_callback, &data); } @@ -1631,6 +1668,57 @@ static int get_metricgroup_events(const char *full_id, return ret; } +static struct metricgroup__pmu_counters *pmu_layout__new(const struct pmu_layout *pl) +{ + struct metricgroup__pmu_counters *l; + + l = zalloc(sizeof(*l)); + + if (!l) + return NULL; + + l->name = pl->pmu; + l->num_counters = pl->num_counters; + l->num_fixed_counters = pl->num_fixed_counters; + pr_debug("create new pmu_layout: [pmu]=%s, [gp_size]=%ld, [fixed_size]=%ld\n", + l->name, l->num_counters, l->num_fixed_counters); + return l; +} + +static int metricgroup__add_pmu_layout_callback(const struct pmu_layout *pl, + void *data) +{ + struct metricgroup__pmu_counters *pmu; + struct list_head *d = data; + int ret = 0; + + pmu = pmu_layout__new(pl); + if (!pmu) + return -ENOMEM; + list_add(&pmu->nd, d); + return ret; +} + +/** + * get_pmu_counter_layouts - Find counter info of the architecture from + * the pmu_layouts table + * @pmu_info_list: the list that the new counter info of a pmu is added to. + * @table: pmu_layouts table that is searched for counter info. + */ +static int get_pmu_counter_layouts(struct list_head *pmu_info_list, + const struct pmu_layouts_table + *table) +{ + LIST_HEAD(list); + int ret; + + ret = pmu_layouts_table__for_each_layout(table, + metricgroup__add_pmu_layout_callback, &list); + + list_splice(&list, pmu_info_list); + return ret; +} + /** * hw_aware_build_grouping - Build event groupings by reading counter * requirement of the events and counter available on the system from @@ -1649,6 +1737,7 @@ static int hw_aware_build_grouping(struct expr_parse_ctx *ctx __maybe_unused, LIST_HEAD(event_info_list); size_t bkt; const struct pmu_events_table *etable = perf_pmu__find_events_table(NULL); + const struct pmu_layouts_table *ltable = perf_pmu__find_layouts_table(NULL); #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0) hashmap__for_each_entry(ctx->ids, cur, bkt) { @@ -1658,9 +1747,13 @@ static int hw_aware_build_grouping(struct expr_parse_ctx *ctx __maybe_unused, ret = get_metricgroup_events(id, etable, &event_info_list); if (ret) - return ret; + goto err_out; } + ret = get_pmu_counter_layouts(&pmu_info_list, ltable); +err_out: + metricgroup__free_event_info(&event_info_list); + metricgroup__free_pmu_info(&pmu_info_list); return ret; #undef RETURN_IF_NON_ZERO } -- 2.42.0