Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1444900rdb; Fri, 16 Feb 2024 17:04:57 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVstrGbhrJVLU66rCmOEOzC/12YERQ5mc6vt4J5LPkw1g4wzfuLJ6v6jt5Gr6SuVLmcl1IC6F9OVJSQod5O1/kQ9AS3kDFi45/GS7putw== X-Google-Smtp-Source: AGHT+IERiI7pJCuUTRlnLw1BcFU/tL0vhvCgwDb05HjP2f11GdlRQdd+dyk78Im04yGp7UpEcWcK X-Received: by 2002:a05:6808:23c6:b0:3c1:3759:5166 with SMTP id bq6-20020a05680823c600b003c137595166mr7318781oib.7.1708131896875; Fri, 16 Feb 2024 17:04:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708131896; cv=pass; d=google.com; s=arc-20160816; b=wvC+R7jaxICGR6CAokDn/95mg1UyJPtVCS6lFAh9KKSW7dGc/tDMWUOBT4Qjc68+OV X2YiF4C/Hz1Y4noaUd3JP313RGXWKE28N+2JYyfYFLDWz/c7v/qHlimgcBo2Ow7fiRap Ta6/CWHutUn5kdpiI4CK/1WcK5lEcACYXeRUXrFKUeIQLPijvNiBkZADZTSlnOqjdqZj +S5ftTPBroBHNTZjPaHfcUhm5KhR6SBvnVohtxTMfqqolRfaxhwBuG55pKPo0ISgNCyH FFJpYZ96xC4lyBQ4I0VV9IL5g5Moe7msuqKUoe6Ikn3urtUpz9yI9/RiEMuqgMy3wjyu d2Qg== 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=EtMupTUIOKSWIiS3+KSauzncMWOa0iz/fT9+70O+xec=; fh=VCn6lt55QOKjzv0Hbz5VO3vbW0BxeXx0oGBGk4MoX4I=; b=ognDsP0f+4f2QUxlj30T3RXWRSGksIdI0fgsvWxObCU4PU+g7Nf4yvePxyKYyetY0D iyqYTB445cYdgm/nyCxf2nWaE9Fj5KJ22G2idr0ogquthcMYHQ7Lvi/BRONDbAN/oGnp odKLhxOnTntK+keotb6dH2R6ZUVMwtpI4I4jK4kmPEB8unwIzU/ld2tsO6+bU/RdmDuZ iiN8UksMLFWGqusnb3ZUiVKRiTQzzy7QgJ6rFst55cxFRL//CAjm2zhQvVk9FGGxyy8v dN1GlI5NF1nQZBYKh+Jf5y22MpOl69VEXsipYQEmnVJ9ruPLBH7MS618F9KfHGT8kEVr cUzQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=doTD5fw0; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-69612-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69612-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id p6-20020a05621421e600b0068ef771a5e2si1028681qvj.94.2024.02.16.17.04.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 17:04:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69612-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=doTD5fw0; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-69612-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69612-linux.lists.archive=gmail.com@vger.kernel.org" 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 7325B1C21398 for ; Sat, 17 Feb 2024 01:04:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A549B33080; Sat, 17 Feb 2024 00:58:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="doTD5fw0" Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 600622D610 for ; Sat, 17 Feb 2024 00:58:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708131538; cv=none; b=Tb9RTf+PtdrOGuWLOhQ9JiShDplHry9/9LP0x3Tbu4/Pdchv2C1exev2OoV019pXIETBV4qpatoXVyP4k9DUidrkbnRLvrDqZF/AgkABxc9sWV7ZkcGPziyhI1uAVegmghdAdrvsqcMsylnVEOPD8Ap5erMWgVM+l7CgGUL8J8I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708131538; c=relaxed/simple; bh=p7r/Fef2L33IhIiGMHD6kN+7Sih7HgKDJIIBtiTNBeQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A+27JQtD2/kr5ZlWLv27+OauU83btM2OY3vLp5z53PYYQ8Z4vto1z4GPT+OYQs5xvpcPQNW2pc1N3iw0UeD8NCFSK6+/yNWlEmaYRWpdUtG+XUrrUvbSj4Q3ZKXaT+VLpi7Sg+NoafAM3G94lMI4V45b2F1z215+6Ue9dKrjBVg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=doTD5fw0; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-59fb5b09cc2so17023eaf.0 for ; Fri, 16 Feb 2024 16:58:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1708131535; x=1708736335; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EtMupTUIOKSWIiS3+KSauzncMWOa0iz/fT9+70O+xec=; b=doTD5fw0eUiJjyCEadVvwUOLzW3kKyOdzzx0vyjifPicJdim+nu66pwzOsg8vw8cwA hgOQyaR8NcW9Skug2tuvO9UtB/fQkYdztiSZDf1nvPltEy4rYtvh5Utex5YWpUQOZ/HR t0KeoTeA5nSCOvLaYekDrk7EMt+9IFAAurCkTlBKc4VLhH3NAZ/6hKBWtxGVZm2LIxBD zTSE+PqeaUeVWxKMAz0aQHPaZf1IRz1hGUNreI62BsylD5698bPXMBkz4j/9T6V4JyF0 WJXp8ZqScG5T6z6H8PiLZ9vBWRsuP8TBjOun1+/4iGgf3G63w9cuhl6TJgoTf+5mrDW+ y0Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708131535; x=1708736335; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EtMupTUIOKSWIiS3+KSauzncMWOa0iz/fT9+70O+xec=; b=l1efx7YubZ42XKMLWz0RLPUfqJ53ejFS8hZQTCLnCZwd0/R4jsiY/uE8HhDb3AtkAW 2w6N51p5m2sijN9Yotn6V9OSuaw4chxKARvAYDBkUUdpzkFWPNNXO/GwvQQ8NU1KMozn ZwaeoWI9+1TsPEbVPw531/Gu9D8mU/MVMqAii1NAW+dVtEY9CjbNKdiF/h/PgpjOVmx/ bCezcnDGN8hjP9gqUgSKQUdK6rVgY03GfQjhvjwNuAhRjCA7R7V2SI7Z+0SJtpq2aoWN ZP5KQA2mKBekR5SyhEa1VK9JK78dSO1oqefBcsoT20QC2/2/J3k/3Qkgg5RLTsgqYvaK nG1g== X-Gm-Message-State: AOJu0Yx6EHqH3K8dXXgezz1n7s1Myih+wP49373IBziF1T+WweYXUWJc wT6bt6D5vBO42b50aZObs8jqn7V3gdi7grfeAo7de9tFurAFyACu7p+Agw9hXFrNxo3VVphIbvs j X-Received: by 2002:a05:6358:2913:b0:177:afce:b12 with SMTP id y19-20020a056358291300b00177afce0b12mr6399019rwb.31.1708131533554; Fri, 16 Feb 2024 16:58:53 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d188-20020a6336c5000000b005dc89957e06sm487655pga.71.2024.02.16.16.58.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:58:53 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Adrian Hunter , Alexander Shishkin , Alexandre Ghiti , Andrew Jones , Anup Patel , Arnaldo Carvalho de Melo , Atish Patra , Christian Brauner , =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , Conor Dooley , devicetree@vger.kernel.org, Evan Green , Guo Ren , Heiko Stuebner , Ian Rogers , Ingo Molnar , James Clark , Jing Zhang , Jiri Olsa , Ji Sheng Teoh , John Garry , Jonathan Corbet , Kan Liang , Krzysztof Kozlowski , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, Ley Foon Tan , linux-doc@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Namhyung Kim , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rob Herring , Samuel Holland , Weilin Wang , Will Deacon , kaiwenxue1@gmail.com, Yang Jihong Subject: [PATCH RFC 15/20] tools/perf: Add arch hooks to override perf standard events Date: Fri, 16 Feb 2024 16:57:33 -0800 Message-Id: <20240217005738.3744121-16-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240217005738.3744121-1-atishp@rivosinc.com> References: <20240217005738.3744121-1-atishp@rivosinc.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 RISC-V doesn't have any standard event encoding defined in the ISA. Cycle/instruction event is defined in the ISA but lack of event encoding allow vendors to choose their own encoding scheme. These events directly map to perf cycle/instruction events which gets decoded as per perf definitions. An arch hooks allows the RISC-V implementation to override the encodings if a vendor has specified the encodings via Json file at runtime. The alternate solution would be define vendor specific encodings in the driver similar to other architectures. However, these will grow over time to become unmaintainable as the number of vendors in RISC-V can be huge. Signed-off-by: Atish Patra --- tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/evlist.c | 59 +++++++++++++++++++ tools/perf/builtin-record.c | 3 + tools/perf/builtin-stat.c | 2 + tools/perf/builtin-top.c | 3 + .../pmu-events/arch/riscv/arch-standard.json | 10 ++++ tools/perf/pmu-events/jevents.py | 6 ++ tools/perf/util/evlist.c | 6 ++ tools/perf/util/evlist.h | 6 ++ 9 files changed, 96 insertions(+) create mode 100644 tools/perf/arch/riscv/util/evlist.c create mode 100644 tools/perf/pmu-events/arch/riscv/arch-standard.json diff --git a/tools/perf/arch/riscv/util/Build b/tools/perf/arch/riscv/util/Build index 603dbb5ae4dc..b581fb3d8677 100644 --- a/tools/perf/arch/riscv/util/Build +++ b/tools/perf/arch/riscv/util/Build @@ -1,5 +1,6 @@ perf-y += perf_regs.o perf-y += header.o +perf-y += evlist.o perf-$(CONFIG_DWARF) += dwarf-regs.o perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o diff --git a/tools/perf/arch/riscv/util/evlist.c b/tools/perf/arch/riscv/util/evlist.c new file mode 100644 index 000000000000..9ad287c6f396 --- /dev/null +++ b/tools/perf/arch/riscv/util/evlist.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "util/pmu.h" +#include "util/pmus.h" +#include "util/evlist.h" +#include "util/parse-events.h" +#include "util/event.h" +#include "evsel.h" + +static int pmu_update_cpu_stdevents_callback(const struct pmu_event *pe, + const struct pmu_events_table *table __maybe_unused, + void *vdata) +{ + struct evsel *evsel = vdata; + struct parse_events_terms terms; + int err; + struct perf_pmu *pmu = perf_pmus__find("cpu"); + + if (pe->event) { + parse_events_terms__init(&terms); + err = parse_events_terms(&terms, pe->event, NULL); + if (err) + goto out_free; + err = perf_pmu__config_terms(pmu, &evsel->core.attr, &terms, + /*zero=*/true, /*err=*/NULL); + if (err) + goto out_free; + } + +out_free: + parse_events_terms__exit(&terms); + return 0; +} + +int arch_evlist__override_default_attrs(struct evlist *evlist, const char *pmu_name) +{ + struct evsel *evsel; + struct perf_pmu *pmu = perf_pmus__find(pmu_name); + static const char *const overriden_event_arr[] = {"cycles", "instructions", + "dTLB-load-misses", "dTLB-store-misses", + "iTLB-load-misses"}; + unsigned int i, len = sizeof(overriden_event_arr) / sizeof(char *); + + if (!pmu) + return 0; + + for (i = 0; i < len; i++) { + if (perf_pmus__have_event(pmu_name, overriden_event_arr[i])) { + evsel = evlist__find_evsel_by_str(evlist, overriden_event_arr[i]); + if (!evsel) + continue; + pmu_events_table__find_event(pmu->events_table, pmu, + overriden_event_arr[i], + pmu_update_cpu_stdevents_callback, evsel); + } + } + + return 0; +} diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 86c910125172..305c2c030208 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -4152,6 +4152,9 @@ int cmd_record(int argc, const char **argv) goto out; } + if (arch_evlist__override_default_attrs(rec->evlist, "cpu")) + goto out; + if (rec->opts.target.tid && !rec->opts.no_inherit_set) rec->opts.no_inherit = true; diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 5fe9abc6a524..a0feafc5be2c 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2713,6 +2713,8 @@ int cmd_stat(int argc, const char **argv) if (add_default_attributes()) goto out; + if (arch_evlist__override_default_attrs(evsel_list, "cpu")) + goto out; if (stat_config.cgroup_list) { if (nr_cgroups > 0) { diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 5301d1badd43..7e268f239df0 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1672,6 +1672,9 @@ int cmd_top(int argc, const char **argv) goto out_delete_evlist; } + if (arch_evlist__override_default_attrs(top.evlist, "cpu")) + goto out_delete_evlist; + status = evswitch__init(&top.evswitch, top.evlist, stderr); if (status) goto out_delete_evlist; diff --git a/tools/perf/pmu-events/arch/riscv/arch-standard.json b/tools/perf/pmu-events/arch/riscv/arch-standard.json new file mode 100644 index 000000000000..96e21f088558 --- /dev/null +++ b/tools/perf/pmu-events/arch/riscv/arch-standard.json @@ -0,0 +1,10 @@ +[ + { + "EventName": "cycles", + "BriefDescription": "cycle executed" + }, + { + "EventName": "instructions", + "BriefDescription": "instruction retired" + } +] diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py index 81e465a43c75..30934a490109 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -7,6 +7,7 @@ from functools import lru_cache import json import metric import os +import re import sys from typing import (Callable, Dict, Optional, Sequence, Set, Tuple) import collections @@ -388,6 +389,11 @@ class JsonEvent: if arch_std: if arch_std.lower() in _arch_std_events: event = _arch_std_events[arch_std.lower()].event + if eventcode: + event = re.sub(r'event=\d+', f'event={llx(eventcode)}', event) + if configcode: + event = re.sub(r'config=\d+', f'event={llx(configcode)}', event) + # Copy from the architecture standard event to self for undefined fields. for attr, value in _arch_std_events[arch_std.lower()].__dict__.items(): if hasattr(self, attr) and not getattr(self, attr): diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 55a300a0977b..f8a5640cf4fa 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -357,6 +357,12 @@ __weak int arch_evlist__add_default_attrs(struct evlist *evlist, return __evlist__add_default_attrs(evlist, attrs, nr_attrs); } +__weak int arch_evlist__override_default_attrs(struct evlist *evlist __maybe_unused, + const char *pmu_name __maybe_unused) +{ + return 0; +} + struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) { struct evsel *evsel; diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index cb91dc9117a2..705b6643b558 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -109,9 +109,15 @@ int arch_evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs); + #define evlist__add_default_attrs(evlist, array) \ arch_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array)) +int arch_evlist__override_default_attrs(struct evlist *evlist, const char *pmu_name); + +#define evlist__override_default_attrs(evlist, pmu_name) \ + arch_evlist__override_default_attrs(evlist, pmu_name) + int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs); int evlist__add_dummy(struct evlist *evlist); -- 2.34.1