Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1955624lqb; Mon, 27 May 2024 03:15:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVRGPGQcGtBVB5Vyz7PlTgQBbUEzvdz8x0LIJus+aiKgljP1QUGNJq1AEFgW2A2WS0ilmXSgL9hZlCxqgXaeYKjST0SHTMYWiJ6v8YntA== X-Google-Smtp-Source: AGHT+IGSrp6k0g9HHE1lUCvZGLrCDclt7S+5D0ZRbL2YxI1VFVnXwa7rCbderpkf0CQT3tkWinD7 X-Received: by 2002:a05:6358:7f99:b0:192:3627:cc89 with SMTP id e5c5f4694b2df-197e50f4986mr1250544955d.10.1716804943101; Mon, 27 May 2024 03:15:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716804943; cv=pass; d=google.com; s=arc-20160816; b=s57oXV+WOPMdAKh7+vT4p7n3t6s6P9I6GntkVs2osaW9FJtChFt4YAYtgcQeSxdRgR kuV3buMgfI4ixB6CspYXdhkashwS/AHhg6OKor1Dvg1Xo4deb8bItu05TJT13GM0PGKr EtXcxR3k7OYRQYms+NrLUcb72qV/sc35j1zsDF+zdTUWE2t4aAGL9pyQ+eMQpJqKwa9H Nvi8pHv+GVx5uwbw+lg+t/Il2NjZ+gnFdveIUXh3xOp5KJ5tf4zjLTA4IfCiH1ieWCkO 8tFEfibqQHa684XrXjidg3XvJMjF9wn+A31yvR/AmEluH1GN8k9atU0oiMVglply5hBr Vudw== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=R8cvAHQEMV5i/B574wxhIOQP308p8fXzk2BaEibPcEQ=; fh=SnOhaGeUZe+n459Qg4ho+eTg90zgEbLfmbvQe4j7gnk=; b=JYgX9oNQclLOy6z4HqIflh6hGK6202n3/7Wxx88sOoYJua2V5E5OHfkt3NaI00/9kA jK+9Mth/uTzGb+QREgG6C0q926MVKOpb9M4El2UoWr0tyFg6Z70yWW71PyYDH53CAAZB aRjoE0R2TCxmrUskzXLpbrkCOMneEVenulVhZd32/AjcbICz19xZ0HoSt8A2B2qWPuB8 qIW98usEYdCuMPklEHu2N1itBRZrlaiJDmocVKOvxGovQJc0YhVRSPpisKsPyV/no7XW eSVetkbFl7Rlh6hWWBEzffpGEXJv1sqVHg/pOBHEYiVVR3Ve4rL6ftqiolWzO1bcPFGe U6/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="YD6IT/8U"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-190410-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190410-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6822a7a3389si6243564a12.739.2024.05.27.03.15.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 03:15:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190410-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=@redhat.com header.s=mimecast20190719 header.b="YD6IT/8U"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-190410-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190410-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 A8E00282DF3 for ; Mon, 27 May 2024 10:15:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE44013AD2F; Mon, 27 May 2024 10:15:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YD6IT/8U" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 B5BA422EE8 for ; Mon, 27 May 2024 10:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716804934; cv=none; b=O07HeePEnmezNrPBPOGU9Vxk9pV/aMEMDXOAWK4leDU/QP6vkoSA+mZMySzq/Zp7GLGhCmEGqAeaf4cA+zy/TjrtnyzQ5raEB2Ovxs7eYOe7xCWDNJ3lOfjhQEc43N+q8/WQUWbmLSLZ1OhfCTSMRYlN4BfJP6EGvojXfDYehnw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716804934; c=relaxed/simple; bh=SaTHtM3+CzAX3Nqlt1UrAv706o4OTbTRxvuPgYz9kG4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=gJcjPLQGSuuOrYnFi0zKOE8Rn9lqVMyuA4RB8hzcvgUP2Ln9vSBmCw5xjWmG52Rwd05PmpNEcCooIcjlJqD8BUkawJdSLPzLO1QEycZt8vqFZ9eVFGmkGfIY+3StW+5i+7QeDSVp5+jbSUszolnsXlYpWWgpIgnTFFZ+ZkAiXx0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YD6IT/8U; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716804931; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=R8cvAHQEMV5i/B574wxhIOQP308p8fXzk2BaEibPcEQ=; b=YD6IT/8UVE4yl3sgE++qzos82Rtc9l14P/7e1IG9F5lLZDfyH168HPSb2eJkehVHWqO4Xk kb/jQlbtwHsOcmnnSl4cxSOT+RSAEWGP8uikBA958Pk2GX2l6PO4WyX27umwn8rQmqWHJK 7lIuUM31SEjaa6OkzeadEXKJyG46xAE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-288-3Ud599KLPy-lM_MM7PTFyQ-1; Mon, 27 May 2024 06:15:25 -0400 X-MC-Unique: 3Ud599KLPy-lM_MM7PTFyQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EB81F85A58C; Mon, 27 May 2024 10:15:24 +0000 (UTC) Received: from alecto.usersys.redhat.com (unknown [10.45.225.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id D40741000A0E; Mon, 27 May 2024 10:15:21 +0000 (UTC) From: Artem Savkov To: linux-perf-users@vger.kernel.org Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , linux-kernel@vger.kernel.org, Artem Savkov Subject: [PATCH] perf record: add a shortcut for metrics Date: Mon, 27 May 2024 12:15:19 +0200 Message-ID: <20240527101519.356342-1-asavkov@redhat.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 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 Add -M/--metrics option to perf-record providing a shortcut to record metrics and metricgroups. This option mirrors the one in perf-stat. Suggested-by: Arnaldo Carvalho de Melo Signed-off-by: Artem Savkov --- tools/perf/Documentation/perf-record.txt | 7 +++- tools/perf/builtin-record.c | 43 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt index 6015fdd08fb63..ebb560d137e62 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -18,7 +18,6 @@ from it, into perf.data - without displaying anything. This file can then be inspected later on, using 'perf report'. - OPTIONS ------- ...:: @@ -216,6 +215,12 @@ OPTIONS na, by_data, by_addr (for mem_blk) hops0, hops1, hops2, hops3 (for mem_hops) +-M:: +--metrics:: +Record metrics or metricgroups specified in a comma separated list. +For a group all metrics from the group are added. +See perf list output for the possible metrics and metricgroups. + --exclude-perf:: Don't record events issued by perf itself. This option should follow an event selector (-e) which selects tracepoint event(s). It adds a diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 66a3de8ac6618..5828051ff2736 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -40,6 +40,7 @@ #include "util/trigger.h" #include "util/perf-hooks.h" #include "util/cpu-set-sched.h" +#include "util/metricgroup.h" #include "util/synthetic-events.h" #include "util/time-utils.h" #include "util/units.h" @@ -188,6 +189,7 @@ static volatile int done; static volatile int auxtrace_record__snapshot_started; static DEFINE_TRIGGER(auxtrace_snapshot_trigger); static DEFINE_TRIGGER(switch_output_trigger); +static char *metrics; static const char *affinity_tags[PERF_AFFINITY_MAX] = { "SYS", "NODE", "CPU" @@ -200,6 +202,25 @@ static inline pid_t gettid(void) } #endif +static int append_metric_groups(const struct option *opt __maybe_unused, + const char *str, + int unset __maybe_unused) +{ + if (metrics) { + char *tmp; + + if (asprintf(&tmp, "%s,%s", metrics, str) < 0) + return -ENOMEM; + free(metrics); + metrics = tmp; + } else { + metrics = strdup(str); + if (!metrics) + return -ENOMEM; + } + return 0; +} + static int record__threads_enabled(struct record *rec) { return rec->opts.threads_spec; @@ -3382,6 +3403,9 @@ static struct option __record_options[] = { parse_events_option), OPT_CALLBACK(0, "filter", &record.evlist, "filter", "event filter", parse_filter), + OPT_CALLBACK('M', "metrics", &record.evlist, "metric/metric group list", + "monitor specified metrics or metric groups (separated by ,)", + append_metric_groups), OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, NULL, "don't record events from perf itself", exclude_perf), @@ -3984,6 +4008,7 @@ int cmd_record(int argc, const char **argv) int err; struct record *rec = &record; char errbuf[BUFSIZ]; + struct rblist mevents; setlocale(LC_ALL, ""); @@ -4153,6 +4178,23 @@ int cmd_record(int argc, const char **argv) if (record.opts.overwrite) record.opts.tail_synthesize = true; + if (metrics) { + const char *pmu = parse_events_option_args.pmu_filter ?: "all"; + int ret = metricgroup__parse_groups(rec->evlist, pmu, metrics, + false, /* metric_no_group */ + false, /* metric_no_merge */ + false, /* metric_no_threshold */ + rec->opts.target.cpu_list, + rec->opts.target.system_wide, + false, /* hardware_aware_grouping */ + &mevents); + if (ret) { + err = ret; + goto out; + } + zfree(&metrics); + } + if (rec->evlist->core.nr_entries == 0) { bool can_profile_kernel = perf_event_paranoid_check(1); @@ -4264,6 +4306,7 @@ int cmd_record(int argc, const char **argv) out_opts: record__free_thread_masks(rec, rec->nr_threads); rec->nr_threads = 0; + metricgroup__rblist_exit(&mevents); evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); return err; } -- 2.45.1