From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 8F649130484 for ; Fri, 24 May 2024 20:52:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716583975; cv=none; b=fvWsNf8TcpwUhKN1xfNsnKgTUhLgCWBy+iHKraLbNemtLOvOfsUhTs7/03tK0Uu6t/fHNlKDH6x2ab+Ex0X3GaQsnwa40WSQDpvyc1Sgt0Z6jaRt+ZeLSQ9I2L0opifmEwmwgDSXyJYLvPFb1tCFB7oSgWVEGMulpXQa/4KHfYY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716583975; c=relaxed/simple; bh=T6StSA4MlJBF2S7oG9VD3Lo7lESuYYgHE4TziCebXc4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=s5y2t79J4ErNggZva+cktA5ZETFchZ/qn2KRL5AX8yI4Ztiax0KZRDzD++Cawm6heu1fVKS0k4psGqG7b8sv7dUQ8+pcLkOMd4YxbGhNIKJQlAPN2u8QhQ/GA6RaiTqWXTrrLLOY6j3gE7qL5dz3tdtZt7L4Xp2p4SaMAM4U7Oc= 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=iFeyMUOh; arc=none smtp.client-ip=209.85.128.202 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iFeyMUOh" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-627f43bec13so11050007b3.0 for ; Fri, 24 May 2024 13:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716583971; x=1717188771; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=y5S/1+TbdrNyqVdsVMC7pvcCEQxH3ljnz+UDUM5b9Uk=; b=iFeyMUOh1559/P6oc3LOo4brRks+a7ARCS5P+24feQSBxLhqgKIVjZmoT8yKb52EEp F4UiyZyZUZnesVk9tsmfup6MfN1azSUySaMy0tW9bdRzqFOZ7PtXlxDSnk7yr4boKUoX oUd102CjOsKPxns0XihfEu2NDoEeq8NCmC+4C6B5Qw96uA/qS64vbo5ogzH89pp9f3qT tXmaj6m3Bl9bPS4O+Ss+3QmRLgyJsqI+oaiGpRXZV7XcVpHfHzrLUpB275aO5hs+94rC +kTyVcC8SmfgXxp4TzlL4QLZaELyL709sS+FA76TolqcHlAgr+8ogMalSJkzzVuaXf4R 1WqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716583971; x=1717188771; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=y5S/1+TbdrNyqVdsVMC7pvcCEQxH3ljnz+UDUM5b9Uk=; b=VmXhnWzwoglvQJCFRit5wxNiCGSIoCHlMIoSO7ywAIf0mybTAnITsVGFykv9o3rbHL T8jk0XleHETYgjWlJXqpzdd80KslczUZvntVKakp3SVS6jUV5r0CwSNrgemjCIdENruS MOzJB0BsRmahUM+ReeC4XdyYLTH84iOXwSTahjLG9ae3Y157h5alxD48gt0MmVOXZhDX +66Y3Zpwxk299GrRsv+5q8IHuNxyvLwzDp0IxvO0yqUNbpXSLI6G+5gUZFBjtXe/Y9wi f/0WjMxIBI4fFyQ75z8E/0adWh4R9Dtx9EdappqdVYC7lf1xxXyS2HQ7bwl02pegruz5 VK7g== X-Forwarded-Encrypted: i=1; AJvYcCURYJBhAiVSyug9WCWdYVIV9jYEYdPVUlrW8QObjAtJSNBk+1QJcKeIO6ggm7ga9qXYX7CijmxqXBGSiU8NSvN938iu0aZIh3czFVMGlZDFtQ== X-Gm-Message-State: AOJu0YwXuULHAT29vsyBu15rsFVIcW1XAzU912S6Qa9V6dXmWEPdoWpt 0aFCEAVulaFzCRoJy0fKQec7Fpy32N/NJlHiIgOVaGkkEgPdXE1w9GvbVRosL+ENcI/n1IjNFqR IUeQoow== X-Google-Smtp-Source: AGHT+IEHCtPO9sX5jtzbEfu51zLGPCby+BaxuDoXVGuIQbLvlpbjy3tKFd1KXX2w/7yRvzQtPUu7PhBrqyTm X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:b0b5:95af:a29:375e]) (user=irogers job=sendgmr) by 2002:a05:6902:1007:b0:df4:90aa:f4ac with SMTP id 3f1490d57ef6-df77239babemr841669276.9.1716583971616; Fri, 24 May 2024 13:52:51 -0700 (PDT) Date: Fri, 24 May 2024 13:52:27 -0700 In-Reply-To: <20240524205227.244375-1-irogers@google.com> Message-Id: <20240524205227.244375-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524205227.244375-1-irogers@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Subject: [PATCH v3 3/3] perf top: Allow filters on events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Changbin Du , Yang Jihong , Andrii Nakryiko , John Fastabend , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Allow filters to be added to perf top events. One use is to workaround issues with: ``` $ perf top --uid="$(id -u)" ``` which tries to scan /proc find processes belonging to the uid and can fail in such a pid terminates between the scan and the perf_event_open reporting: ``` Error: The sys_perf_event_open() syscall returned with 3 (No such process) for event (cycles:P). /bin/dmesg | grep -i perf may provide additional information. ``` A similar filter: ``` $ perf top -e cycles:P --filter "uid == $(id -u)" ``` doesn't fail this way. Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-top.txt | 4 ++++ tools/perf/builtin-top.c | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt index a754875fa5bb..667e5102075e 100644 --- a/tools/perf/Documentation/perf-top.txt +++ b/tools/perf/Documentation/perf-top.txt @@ -43,6 +43,10 @@ Default is to monitor all CPUS. encoding with the layout of the event control registers as described by entries in /sys/bus/event_source/devices/cpu/format/*. +--filter=:: + Event filter. This option should follow an event selector (-e). For + syntax see linkperf:perf-record[1]. + -E :: --entries=:: Display this many functions. diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 1d6aef51c122..e8cbbf10d361 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1055,6 +1055,13 @@ static int perf_top__start_counters(struct perf_top *top) } } + if (evlist__apply_filters(evlist, &counter)) { + pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", + counter->filter ?: "BPF", evsel__name(counter), errno, + str_error_r(errno, msg, sizeof(msg))); + goto out_err; + } + if (evlist__mmap(evlist, opts->mmap_pages) < 0) { ui__error("Failed to mmap with %d (%s)\n", errno, str_error_r(errno, msg, sizeof(msg))); @@ -1462,6 +1469,8 @@ int cmd_top(int argc, const char **argv) OPT_CALLBACK('e', "event", &parse_events_option_args, "event", "event selector. use 'perf list' to list available events", parse_events_option), + OPT_CALLBACK(0, "filter", &top.evlist, "filter", + "event filter", parse_filter), OPT_U64('c', "count", &opts->user_interval, "event period to sample"), OPT_STRING('p', "pid", &target->pid, "pid", "profile events on existing process id"), -- 2.45.1.288.g0e0cd299f1-goog