Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1167229lqb; Sat, 25 May 2024 08:30:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVWVb+EcCedxvfuZd2bX9wwdIZx4JH/biqLrPYlJCwuugw++OvxrFTC0qmYjW+jExMRRIElRImiCGzvKgjttaQe2Sm7Zaj3HYtJYtA19g== X-Google-Smtp-Source: AGHT+IE5eMPAcD0aVN6bEg626/gRaD0tED33vP0Hz8tkeTMqBZmFfygbvIlRicldImI7zwWoACKW X-Received: by 2002:a50:999a:0:b0:578:5f43:c05c with SMTP id 4fb4d7f45d1cf-5785f43c0c5mr2995724a12.39.1716651010001; Sat, 25 May 2024 08:30:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716651009; cv=pass; d=google.com; s=arc-20160816; b=DgRaJvD5LfhrB9wUvrWkr76R6ibb10pJ0fquWMD1Q5u+sK7veOATGo4p4S5m1NO6x/ 7Uf1YB10bjjdYr/AhuUUJhf9+sZb6xydv1+2Rkcxgj7UfpBCm+MLZg3OLkGj27a0QK8C o9ClI/ut4NbykZyoBhMSzfMVCv3fFO2ObrW/xcXx44YsoBJPP1JrG/X8uMC2myRofwGO koqpTfjrB3Zlaf11j+JLvloYUR7kq4UTx9+iUSEP1NSCHcZJ+bfEDzEwPhn3hjRvGILb B9QG1uO+hB4VUcXvm208Er87rUw1aNA+gzoH+THWiwcf6uq4GdsnO8JaHuBP0Ht/KZ8H SFMw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:date:dkim-signature; bh=pWtjA5RKLficyvRTTH7or1mGMjQs5mR1l7sIJY8crh8=; fh=hjlIHd2jOMnFvWcEJI1jMrOKsZBydM6NuViDxFgU2jw=; b=jk7j474IXcT6mPqMfwYdbaNS0rHZTMiA+GUqD1UWrqzIesUjeazn4aglj4fUTPhlhX Pcum72jck++fwit/KiyYUXck9vH81E8qkrzd491Cdq21legyUG+QCZZ1EjdM35vxG23T Jtpcndgee5a13FVt1BB2ZTuc4Hv6JFlyim87WrJ8t/RnA/AUdelAPZoHji5QKZwl6DOj p5I8XSao8SGiDwxcQFyc7rLT6LD3tpsjWQTYLG6jYpu3+Gt6cbWeWthauXODJws9Zdmm ssFkMsbp2tnksirt2Rkr0Ix7fwrL2eD3kispITqWYrWPA+UTyTiXQDH80XSEwWOIObKV BlJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="O/5Nandb"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-189423-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189423-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-579c4a9fe57si155378a12.648.2024.05.25.08.30.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 May 2024 08:30:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189423-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="O/5Nandb"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-189423-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189423-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 93F1F1F215FD for ; Sat, 25 May 2024 15:30:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15FBA84FB1; Sat, 25 May 2024 15:30:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="O/5Nandb" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 9E96844C6F for ; Sat, 25 May 2024 15:30:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716651002; cv=none; b=uOBJNzNDmDMDuwvAKWOHFSiYDOCoVFJYVVR9DjXDQbE3HmhXAJopxYyeil4w6nTNbNgHFWHveFBHb9CmHwxRyfOVkTjkll8BwvcPcMXWNsPi7ZRt+vZxtUWrWNX3Emy8h8MjeeIgVFT/NREKpKde3dfkutP1loX4h3klAJPvFgw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716651002; c=relaxed/simple; bh=3I21YJLtt6e3c5sULT5fvIQ84OxV7DLMKCnQYsWdgDI=; h=Date:Message-Id:Mime-Version:Subject:From:To:Content-Type; b=HGuY+9yBKOQV4UJBnYyRSn28WCjjRma/kXObEKEfwlKLy6HLdelt/oMKs6INju7Jlki0TWTc4ny+/EkKcvNQ6MlOctBflU8PYIJUMK8+amfRtu4nkYdJoqVYvAYHk38QuFVPnN9Wha8J1W2AVUcVhHYowP96Cz5E+/GCD83zePc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=O/5Nandb; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-df771b4eacbso2149170276.2 for ; Sat, 25 May 2024 08:30:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716650999; x=1717255799; darn=vger.kernel.org; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=pWtjA5RKLficyvRTTH7or1mGMjQs5mR1l7sIJY8crh8=; b=O/5Nandblp6Paysd97Zgv1gQqq6xRYIdVoUpSm0qcjmd/El4FQvHGQ8HynxjhZqam7 rbr5lCS4da9W+2Z+ADSU5NSWVpv7qMwkxpO99DqKFxojn1AuBEu+F0otmR4duNI8og3h 42PtLKcSapPSZ7NbuFIPRFg0ktWHu2jdSqTetYljSAUwHHqOrhE4L64GIG2Gnrk2wO3G GW7epGyzRl+bNv5VYLIWK5ylgMRBSuzL3l7QgaKAQLdPW3EOYjuKsXcBRR5ruFNsqfrV 8IV2fQfL/kXlab7yamZwlTbKGmtsQFHrRiKbF7vkvw6w1FHGk7JNPGcLX0FyWSE+9+ZP DP5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716650999; x=1717255799; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=pWtjA5RKLficyvRTTH7or1mGMjQs5mR1l7sIJY8crh8=; b=vEia4uLwWBFdpMB8/K+3tzb9xQwkFkHz6mZgQsZcI6kpbCEvIAEPOSjaQKSPej7EXE juKRFDbYnZaHXkrsJjdxA+bKh74g4nJbr3oSC3VsOr4nnikGQx8DWHhsmMWFjCwppYZU pypF/nb5PmByr+i3sVWOLSRvcH0gWVmUQ8xzaMT1r5yralgb19FeDxRT1kzLurJUrgOw q01RNz0N8zpTJTDkET2Ej1JGiBJSSG/ffPOMdiGo4zRUHcqrh0qL3/CUk45E0PoHIhwU vOFM3D2wOiBnsvxB50dfCA9FVBydG3EnnatZgl7M4gG4GcVdYVAq0rFmR90bLl59wy+7 EQ0w== X-Forwarded-Encrypted: i=1; AJvYcCWcQAqWcaL2ELqX1kX56zXlgLkb7Xzmb/EhTqoSZaPzEHGD0c5OKSqlI33ZZnF1X0T7zc88X664L5hZ3Dy+uftbNtD1OR/wSH5Iy5au X-Gm-Message-State: AOJu0YxLzU8pyqJs4B8m42a0cOfoj+DCoyWwISzRJ0ZYsgqrPkRPZKZq SB8L0J4OoPOO4YQcPC2KYchbypNy6+2YKBZ0EkuabNHkXQw3LOuBHqMcWPnmuq+4ukUE1SdrUKW h/aLCKw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:8ae2:c404:aad0:5c4d]) (user=irogers job=sendgmr) by 2002:a05:6902:1509:b0:dc6:cd85:bcd7 with SMTP id 3f1490d57ef6-df7721c1a47mr1364616276.3.1716650999571; Sat, 25 May 2024 08:29:59 -0700 (PDT) Date: Sat, 25 May 2024 08:29:27 -0700 Message-Id: <20240525152927.665498-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Subject: [PATCH v1] perf evlist: Force adding default events only to core PMUs From: Ian Rogers To: Linus Torvalds , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Dominique Martinet , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" PMUs other than core PMUs may have a 'cycles' event. Default opening a non-core cycles event with perf record can lead to perf_event_open failure. Avoid this by only opening the default 'cycles' event on core PMUs. Closes: https://lore.kernel.org/lkml/CAHk-=wiWvtFyedDNpoV7a8Fq_FpbB+F5KmWK2xPY3QoYseOf_A@mail.gmail.com/ Fixes: 617824a7f0f7 ("perf parse-events: Prefer sysfs/JSON hardware events over legacy") Signed-off-by: Ian Rogers --- tools/perf/builtin-record.c | 6 ++---- tools/perf/builtin-top.c | 3 +-- tools/perf/util/evlist.c | 43 ++++++++++++++++++++++++++++++++++--- tools/perf/util/evlist.h | 1 + 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 66a3de8ac661..b968c3c2def6 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3198,7 +3198,7 @@ static int switch_output_setup(struct record *rec) unsigned long val; /* - * If we're using --switch-output-events, then we imply its + * If we're using --switch-output-events, then we imply its * --switch-output=signal, as we'll send a SIGUSR2 from the side band * thread to its parent. */ @@ -4154,9 +4154,7 @@ int cmd_record(int argc, const char **argv) record.opts.tail_synthesize = true; if (rec->evlist->core.nr_entries == 0) { - bool can_profile_kernel = perf_event_paranoid_check(1); - - err = parse_event(rec->evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu"); + err = evlist__add_default_events(rec->evlist); if (err) goto out; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 1d6aef51c122..90b97fc24edb 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1665,8 +1665,7 @@ int cmd_top(int argc, const char **argv) goto out_delete_evlist; if (!top.evlist->core.nr_entries) { - bool can_profile_kernel = perf_event_paranoid_check(1); - int err = parse_event(top.evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu"); + int err = evlist__add_default_events(top.evlist); if (err) goto out_delete_evlist; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 3a719edafc7a..ddca50cb049f 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -32,6 +32,7 @@ #include "util/sample.h" #include "util/bpf-filter.h" #include "util/stat.h" +#include "util/strbuf.h" #include "util/util.h" #include #include @@ -93,14 +94,12 @@ struct evlist *evlist__new(void) struct evlist *evlist__new_default(void) { struct evlist *evlist = evlist__new(); - bool can_profile_kernel; int err; if (!evlist) return NULL; - can_profile_kernel = perf_event_paranoid_check(1); - err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu"); + err = evlist__add_default_events(evlist); if (err) { evlist__delete(evlist); return NULL; @@ -187,6 +186,44 @@ void evlist__delete(struct evlist *evlist) free(evlist); } +int evlist__add_default_events(struct evlist *evlist) +{ + struct perf_pmu *pmu = NULL; + bool can_profile_kernel = perf_event_paranoid_check(1); + struct strbuf events; + bool first = true; + int err; + + err = strbuf_init(&events, /*hint=*/32); + if (err) + return err; + + while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { + if (!first) { + err = strbuf_addch(&events, ','); + if (err) + goto err_out; + } + err = strbuf_addstr(&events, pmu->name); + if (err < 0) + goto err_out; + + if (can_profile_kernel) + err = strbuf_addstr(&events, "/cycles/P"); + else + err = strbuf_addstr(&events, "/cycles/Pu"); + + if (err < 0) + goto err_out; + + first = false; + } + err = parse_event(evlist, events.buf); +err_out: + strbuf_release(&events); + return err; +} + void evlist__add(struct evlist *evlist, struct evsel *entry) { perf_evlist__add(&evlist->core, &entry->core); diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index cb91dc9117a2..269db02f7b45 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -97,6 +97,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, void evlist__exit(struct evlist *evlist); void evlist__delete(struct evlist *evlist); +int evlist__add_default_events(struct evlist *evlist); void evlist__add(struct evlist *evlist, struct evsel *entry); void evlist__remove(struct evlist *evlist, struct evsel *evsel); -- 2.45.1.288.g0e0cd299f1-goog