From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACC30C433FE for ; Fri, 22 Apr 2022 09:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446177AbiDVJrG (ORCPT ); Fri, 22 Apr 2022 05:47:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445893AbiDVJrB (ORCPT ); Fri, 22 Apr 2022 05:47:01 -0400 Received: from esa3.hc1455-7.c3s2.iphmx.com (esa3.hc1455-7.c3s2.iphmx.com [207.54.90.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E31D853B54; Fri, 22 Apr 2022 02:44:07 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="70825376" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="70825376" Received: from unknown (HELO oym-r1.gw.nic.fujitsu.com) ([210.162.30.89]) by esa3.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:05 +0900 Received: from oym-m3.gw.nic.fujitsu.com (oym-nat-oym-m3.gw.nic.fujitsu.com [192.168.87.60]) by oym-r1.gw.nic.fujitsu.com (Postfix) with ESMTP id 65790ED4C0; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by oym-m3.gw.nic.fujitsu.com (Postfix) with ESMTP id 430FED947E; Fri, 22 Apr 2022 18:44:03 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 046B940487FBB; Fri, 22 Apr 2022 18:44:02 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 1/7] libperf: Move 'open_flags' from tools/perf to evsel::open_flags Date: Fri, 22 Apr 2022 18:38:27 +0900 Message-Id: <20220422093833.340873-2-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move evsel::open_flags to perf_evsel::open_flags, so we can move the open_flags interface to libperf. Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/include/internal/evsel.h | 2 ++ tools/perf/util/evsel.c | 16 +++++++++------- tools/perf/util/evsel.h | 1 - 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/inclu= de/internal/evsel.h index cfc9ebd7968e..37a99cf261b3 100644 --- a/tools/lib/perf/include/internal/evsel.h +++ b/tools/lib/perf/include/internal/evsel.h @@ -51,6 +51,8 @@ struct perf_evsel { int nr_members; bool system_wide; int idx; + + unsigned long open_flags; }; =20 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *at= tr, diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 2a1729e7aee4..92f7a76ca9d5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1794,9 +1794,9 @@ static int __evsel__prepare_open(struct evsel *evsel,= struct perf_cpu_map *cpus, perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) = < 0) return -ENOMEM; =20 - evsel->open_flags =3D PERF_FLAG_FD_CLOEXEC; + evsel->core.open_flags =3D PERF_FLAG_FD_CLOEXEC; if (evsel->cgrp) - evsel->open_flags |=3D PERF_FLAG_PID_CGROUP; + evsel->core.open_flags |=3D PERF_FLAG_PID_CGROUP; =20 return 0; } @@ -1814,7 +1814,7 @@ static void evsel__disable_missing_features(struct ev= sel *evsel) evsel->core.attr.clockid =3D 0; } if (perf_missing_features.cloexec) - evsel->open_flags &=3D ~(unsigned long)PERF_FLAG_FD_CLOEXEC; + evsel->core.open_flags &=3D ~(unsigned long)PERF_FLAG_FD_CLOEXEC; if (perf_missing_features.mmap2) evsel->core.attr.mmap2 =3D 0; if (evsel->pmu && evsel->pmu->missing_features.exclude_guest) @@ -1902,7 +1902,8 @@ bool evsel__detect_missing_features(struct evsel *evs= el) perf_missing_features.clockid =3D true; pr_debug2_peo("switching off use_clockid\n"); return true; - } else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FL= AG_FD_CLOEXEC)) { + } else if (!perf_missing_features.cloexec && + (evsel->core.open_flags & PERF_FLAG_FD_CLOEXEC)) { perf_missing_features.cloexec =3D true; pr_debug2_peo("switching off cloexec flag\n"); return true; @@ -2029,11 +2030,12 @@ static int evsel__open_cpu(struct evsel *evsel, str= uct perf_cpu_map *cpus, test_attr__ready(); =20 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags = %#lx", - pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags); + pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, + evsel->core.open_flags); =20 fd =3D sys_perf_event_open(&evsel->core.attr, pid, perf_cpu_map__cpu(cpus, idx).cpu, - group_fd, evsel->open_flags); + group_fd, evsel->core.open_flags); =20 FD(evsel, idx, thread) =3D fd; =20 @@ -2050,7 +2052,7 @@ static int evsel__open_cpu(struct evsel *evsel, struc= t perf_cpu_map *cpus, if (unlikely(test_attr__enabled)) { test_attr__open(&evsel->core.attr, pid, perf_cpu_map__cpu(cpus, idx), - fd, group_fd, evsel->open_flags); + fd, group_fd, evsel->core.open_flags); } =20 pr_debug2_peo(" =3D %d\n", fd); diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 041b42d33bf5..8a545954eec7 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -152,7 +152,6 @@ struct evsel { struct bperf_leader_bpf *leader_skel; struct bperf_follower_bpf *follower_skel; }; - unsigned long open_flags; int precise_ip_original; =20 /* for missing_features */ --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39C39C433FE for ; Fri, 22 Apr 2022 09:44:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446164AbiDVJrB (ORCPT ); Fri, 22 Apr 2022 05:47:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445933AbiDVJrB (ORCPT ); Fri, 22 Apr 2022 05:47:01 -0400 Received: from esa5.hc1455-7.c3s2.iphmx.com (esa5.hc1455-7.c3s2.iphmx.com [68.232.139.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3C2B53B56; Fri, 22 Apr 2022 02:44:07 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="70383055" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="70383055" Received: from unknown (HELO oym-r4.gw.nic.fujitsu.com) ([210.162.30.92]) by esa5.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:05 +0900 Received: from oym-m4.gw.nic.fujitsu.com (oym-nat-oym-m4.gw.nic.fujitsu.com [192.168.87.61]) by oym-r4.gw.nic.fujitsu.com (Postfix) with ESMTP id 5CE94E07EA; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by oym-m4.gw.nic.fujitsu.com (Postfix) with ESMTP id 842A7110AC6; Fri, 22 Apr 2022 18:44:03 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 4414B40487FBC; Fri, 22 Apr 2022 18:44:03 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 2/7] libperf: Introduce perf_{evsel, evlist}__open_opt with extensible struct opts Date: Fri, 22 Apr 2022 18:38:28 +0900 Message-Id: <20220422093833.340873-3-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce perf_{evsel, evlist}__open_opt with extensible structure opts. The mechanism of the extensible structure opts imitates tools/lib/bpf/libbpf.h. Currently, only open_flags is supported for the opts structure. Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/Documentation/libperf.txt | 10 ++++++ tools/lib/perf/evlist.c | 20 +++++++++++ tools/lib/perf/evsel.c | 25 +++++++++++++- tools/lib/perf/include/perf/evlist.h | 3 ++ tools/lib/perf/include/perf/evsel.h | 22 ++++++++++++ tools/lib/perf/internal.h | 44 ++++++++++++++++++++++++ tools/lib/perf/libperf.map | 2 ++ 7 files changed, 125 insertions(+), 1 deletion(-) diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Docu= mentation/libperf.txt index a8f1a237931b..6ca91ca94e01 100644 --- a/tools/lib/perf/Documentation/libperf.txt +++ b/tools/lib/perf/Documentation/libperf.txt @@ -132,6 +132,16 @@ SYNOPSIS }; }; =20 + struct perf_evsel_open_opts { + /* size of this struct, for forward/backward compatibility */ + size_t sz; + + unsigned long open_flags; /* perf_event_open flags */ + }; + #define perf_evsel_open_opts__last_field open_flags + + #define LIBPERF_OPTS(TYPE, NAME, ...) + struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr); void perf_evsel__delete(struct perf_evsel *evsel); int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus, diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index a09315538a30..bf9343acb799 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -665,3 +665,23 @@ void perf_evlist__set_leader(struct perf_evlist *evlis= t) __perf_evlist__set_leader(&evlist->entries, first); } } + +int perf_evlist__open_opts(struct perf_evlist *evlist, + struct perf_evsel_open_opts *opts) +{ + struct perf_evsel *evsel; + int err; + + perf_evlist__for_each_entry(evlist, evsel) { + err =3D perf_evsel__open_opts(evsel, evsel->cpus, + evsel->threads, opts); + if (err < 0) + goto out_err; + } + + return 0; + +out_err: + perf_evlist__close(evlist); + return err; +} diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 210ea7c06ce8..00c0cea43b52 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -16,8 +16,12 @@ #include #include #include +#include +#include +#include #include #include +#include "internal.h" =20 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *at= tr, int idx) @@ -26,6 +30,7 @@ void perf_evsel__init(struct perf_evsel *evsel, struct pe= rf_event_attr *attr, evsel->attr =3D *attr; evsel->idx =3D idx; evsel->leader =3D evsel; + evsel->open_flags =3D 0; } =20 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr) @@ -158,7 +163,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct p= erf_cpu_map *cpus, =20 fd =3D sys_perf_event_open(&evsel->attr, threads->map[thread].pid, - cpu, group_fd, 0); + cpu, group_fd, evsel->open_flags); =20 if (fd < 0) return -errno; @@ -454,3 +459,21 @@ void perf_counts_values__scale(struct perf_counts_valu= es *count, if (pscaled) *pscaled =3D scaled; } + +int perf_evsel__open_opts(struct perf_evsel *evsel, struct perf_cpu_map *c= pus, + struct perf_thread_map *threads, + struct perf_evsel_open_opts *opts) +{ + int err =3D 0; + + if (!OPTS_VALID(opts, perf_evsel_open_opts)) { + err =3D -EINVAL; + return err; + } + + evsel->open_flags =3D OPTS_GET(opts, open_flags, 0); + + err =3D perf_evsel__open(evsel, cpus, threads); + + return err; +} diff --git a/tools/lib/perf/include/perf/evlist.h b/tools/lib/perf/include/= perf/evlist.h index 9ca399d49bb4..6eff1e9b3481 100644 --- a/tools/lib/perf/include/perf/evlist.h +++ b/tools/lib/perf/include/perf/evlist.h @@ -9,6 +9,7 @@ struct perf_evlist; struct perf_evsel; struct perf_cpu_map; struct perf_thread_map; +struct perf_evsel_open_opts; =20 LIBPERF_API void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *evsel); @@ -47,4 +48,6 @@ LIBPERF_API struct perf_mmap *perf_evlist__next_mmap(stru= ct perf_evlist *evlist, (pos) =3D perf_evlist__next_mmap((evlist), (pos), overwrite)) =20 LIBPERF_API void perf_evlist__set_leader(struct perf_evlist *evlist); +LIBPERF_API int perf_evlist__open_opts(struct perf_evlist *evlist, + struct perf_evsel_open_opts *opts); #endif /* __LIBPERF_EVLIST_H */ diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/p= erf/evsel.h index 2a9516b42d15..1140df4d2578 100644 --- a/tools/lib/perf/include/perf/evsel.h +++ b/tools/lib/perf/include/perf/evsel.h @@ -5,6 +5,7 @@ #include #include #include +#include #include =20 struct perf_evsel; @@ -23,6 +24,23 @@ struct perf_counts_values { }; }; =20 +struct perf_evsel_open_opts { + /* size of this struct, for forward/backward compatibility */ + size_t sz; + + unsigned long open_flags; /* perf_event_open flags */ +}; +#define perf_evsel_open_opts__last_field open_flags + +#define LIBPERF_OPTS(TYPE, NAME, ...) \ + struct TYPE NAME =3D ({ \ + memset(&NAME, 0, sizeof(struct TYPE)); \ + (struct TYPE) { \ + .sz =3D sizeof(struct TYPE), \ + __VA_ARGS__ \ + }; \ + }) + LIBPERF_API struct perf_evsel *perf_evsel__new(struct perf_event_attr *att= r); LIBPERF_API void perf_evsel__delete(struct perf_evsel *evsel); LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu= _map *cpus, @@ -43,5 +61,9 @@ LIBPERF_API struct perf_thread_map *perf_evsel__threads(s= truct perf_evsel *evsel LIBPERF_API struct perf_event_attr *perf_evsel__attr(struct perf_evsel *ev= sel); LIBPERF_API void perf_counts_values__scale(struct perf_counts_values *coun= t, bool scale, __s8 *pscaled); +LIBPERF_API int perf_evsel__open_opts(struct perf_evsel *evsel, + struct perf_cpu_map *cpus, + struct perf_thread_map *threads, + struct perf_evsel_open_opts *opts); =20 #endif /* __LIBPERF_EVSEL_H */ diff --git a/tools/lib/perf/internal.h b/tools/lib/perf/internal.h index 2c27e158de6b..4b91a087ed62 100644 --- a/tools/lib/perf/internal.h +++ b/tools/lib/perf/internal.h @@ -20,4 +20,48 @@ do { \ #define pr_debug2(fmt, ...) __pr(LIBPERF_DEBUG2, fmt, ##__VA_ARGS__) #define pr_debug3(fmt, ...) __pr(LIBPERF_DEBUG3, fmt, ##__VA_ARGS__) =20 +static inline bool libperf_is_mem_zeroed(const char *p, ssize_t len) +{ + while (len > 0) { + if (*p) + return false; + p++; + len--; + } + return true; +} + +static inline bool libperf_validate_opts(const char *opts, + size_t opts_sz, size_t user_sz, + const char *type_name) +{ + if (user_sz < sizeof(size_t)) { + pr_warning("%s size (%zu) is too small\n", type_name, user_sz); + return false; + } + if (!libperf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) { + pr_warning("%s has non-zero extra bytes\n", type_name); + return false; + } + return true; +} + +#define offsetofend(TYPE, FIELD) \ + (offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD)) + +#define OPTS_VALID(opts, type) \ + (!(opts) || libperf_validate_opts((const char *)opts, \ + offsetofend(struct type, \ + type##__last_field), \ + (opts)->sz, #type)) +#define OPTS_HAS(opts, field) \ + ((opts) && opts->sz >=3D offsetofend(typeof(*(opts)), field)) +#define OPTS_GET(opts, field, fallback_value) \ + (OPTS_HAS(opts, field) ? (opts)->field : fallback_value) +#define OPTS_SET(opts, field, value) \ + do { \ + if (OPTS_HAS(opts, field)) \ + (opts)->field =3D value; \ + } while (0) + #endif /* __LIBPERF_INTERNAL_H */ diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map index 190b56ae923a..eeeb3075e092 100644 --- a/tools/lib/perf/libperf.map +++ b/tools/lib/perf/libperf.map @@ -25,6 +25,7 @@ LIBPERF_0.0.1 { perf_evsel__enable; perf_evsel__disable; perf_evsel__open; + perf_evsel__open_opts; perf_evsel__close; perf_evsel__mmap; perf_evsel__munmap; @@ -36,6 +37,7 @@ LIBPERF_0.0.1 { perf_evlist__new; perf_evlist__delete; perf_evlist__open; + perf_evlist__open_opts; perf_evlist__close; perf_evlist__enable; perf_evlist__disable; --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4576C433F5 for ; Fri, 22 Apr 2022 09:45:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446201AbiDVJsY (ORCPT ); Fri, 22 Apr 2022 05:48:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446223AbiDVJsL (ORCPT ); Fri, 22 Apr 2022 05:48:11 -0400 Received: from esa8.hc1455-7.c3s2.iphmx.com (esa8.hc1455-7.c3s2.iphmx.com [139.138.61.253]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93BACDFBB for ; Fri, 22 Apr 2022 02:45:16 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="58751383" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="58751383" Received: from unknown (HELO yto-r3.gw.nic.fujitsu.com) ([218.44.52.219]) by esa8.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:06 +0900 Received: from yto-m2.gw.nic.fujitsu.com (yto-nat-yto-m2.gw.nic.fujitsu.com [192.168.83.65]) by yto-r3.gw.nic.fujitsu.com (Postfix) with ESMTP id E8F6BD5027; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by yto-m2.gw.nic.fujitsu.com (Postfix) with ESMTP id D4BD6E6743; Fri, 22 Apr 2022 18:44:03 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 8D0D240487FBD; Fri, 22 Apr 2022 18:44:03 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 3/7] libperf: Add support for overflow handling of sampling events Date: Fri, 22 Apr 2022 18:38:29 +0900 Message-Id: <20220422093833.340873-4-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extend the fields of the opts structure to set up overflow handling for sampling events. Also, add processing to set signal handlers in perf_evsel__open_opts. Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/Documentation/libperf.txt | 6 +- tools/lib/perf/Makefile | 1 + tools/lib/perf/evsel.c | 79 ++++++++++++++++++++++++ tools/lib/perf/include/perf/evsel.h | 6 +- tools/lib/perf/tests/test-evlist.c | 1 - 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Docu= mentation/libperf.txt index 6ca91ca94e01..ec93b1c75ebe 100644 --- a/tools/lib/perf/Documentation/libperf.txt +++ b/tools/lib/perf/Documentation/libperf.txt @@ -137,8 +137,12 @@ SYNOPSIS size_t sz; =20 unsigned long open_flags; /* perf_event_open flags */ + int flags; /* fcntl flags */ + unsigned int signal; + int owner_type; + struct sigaction *sig; }; - #define perf_evsel_open_opts__last_field open_flags + #define perf_evsel_open_opts__last_field sig =20 #define LIBPERF_OPTS(TYPE, NAME, ...) =20 diff --git a/tools/lib/perf/Makefile b/tools/lib/perf/Makefile index 21df023a2103..4c8fae193cf5 100644 --- a/tools/lib/perf/Makefile +++ b/tools/lib/perf/Makefile @@ -75,6 +75,7 @@ override CFLAGS +=3D -Werror -Wall override CFLAGS +=3D -fPIC override CFLAGS +=3D $(INCLUDES) override CFLAGS +=3D -fvisibility=3Dhidden +override CFLAGS +=3D -D_GNU_SOURCE =20 all: =20 diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 00c0cea43b52..a289f6c44d7c 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -460,6 +460,79 @@ void perf_counts_values__scale(struct perf_counts_valu= es *count, *pscaled =3D scaled; } =20 +static int perf_evsel__run_fcntl(struct perf_evsel *evsel, + unsigned int cmd, unsigned long arg, + int cpu_map_idx) +{ + int thread; + + for (thread =3D 0; thread < xyarray__max_y(evsel->fd); thread++) { + int err; + int *fd =3D FD(evsel, cpu_map_idx, thread); + + if (!fd || *fd < 0) + return -1; + + err =3D fcntl(*fd, cmd, arg); + if (err) + return err; + } + + return 0; +} + +static int perf_evsel__set_signal_handler(struct perf_evsel *evsel, + struct perf_evsel_open_opts *opts) +{ + unsigned int flags; + unsigned int signal; + struct f_owner_ex owner; + struct sigaction *sig; + int cpu_map_idx; + int err =3D 0; + + flags =3D OPTS_GET(opts, flags, (O_RDWR | O_NONBLOCK | O_ASYNC)); + signal =3D OPTS_GET(opts, signal, SIGIO); + owner.type =3D OPTS_GET(opts, owner_type, F_OWNER_PID); + sig =3D OPTS_GET(opts, sig, NULL); + + if (flags =3D=3D 0 && signal =3D=3D 0 && !owner.type =3D=3D 0 && sig =3D= =3D 0) + return err; + + err =3D sigaction(signal, sig, NULL); + if (err) + return err; + + switch (owner.type) { + case F_OWNER_PID: + owner.pid =3D getpid(); + break; + case F_OWNER_TID: + owner.pid =3D syscall(SYS_gettid); + break; + case F_OWNER_PGRP: + default: + return -1; + } + + for (cpu_map_idx =3D 0; cpu_map_idx < xyarray__max_x(evsel->fd); cpu_map_= idx++) { + err =3D perf_evsel__run_fcntl(evsel, F_SETFL, flags, cpu_map_idx); + if (err) + return err; + + err =3D perf_evsel__run_fcntl(evsel, F_SETSIG, signal, cpu_map_idx); + if (err) + return err; + + err =3D perf_evsel__run_fcntl(evsel, F_SETOWN_EX, + (unsigned long)&owner, cpu_map_idx); + if (err) + return err; + } + + return err; +} + int perf_evsel__open_opts(struct perf_evsel *evsel, struct perf_cpu_map *c= pus, struct perf_thread_map *threads, struct perf_evsel_open_opts *opts) @@ -474,6 +547,12 @@ int perf_evsel__open_opts(struct perf_evsel *evsel, st= ruct perf_cpu_map *cpus, evsel->open_flags =3D OPTS_GET(opts, open_flags, 0); =20 err =3D perf_evsel__open(evsel, cpus, threads); + if (err) + return err; + + err =3D perf_evsel__set_signal_handler(evsel, opts); + if (err) + return err; =20 return err; } diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/p= erf/evsel.h index 1140df4d2578..50662babfe97 100644 --- a/tools/lib/perf/include/perf/evsel.h +++ b/tools/lib/perf/include/perf/evsel.h @@ -29,8 +29,12 @@ struct perf_evsel_open_opts { size_t sz; =20 unsigned long open_flags; /* perf_event_open flags */ + int flags; /* fcntl flags */ + unsigned int signal; + int owner_type; + struct sigaction *sig; }; -#define perf_evsel_open_opts__last_field open_flags +#define perf_evsel_open_opts__last_field sig =20 #define LIBPERF_OPTS(TYPE, NAME, ...) \ struct TYPE NAME =3D ({ \ diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test= -evlist.c index ed616fc19b4f..d8f9493cd4d1 100644 --- a/tools/lib/perf/tests/test-evlist.c +++ b/tools/lib/perf/tests/test-evlist.c @@ -1,5 +1,4 @@ // SPDX-License-Identifier: GPL-2.0 -#define _GNU_SOURCE // needed for sched.h to get sched_[gs]etaffinity and = CPU_(ZERO,SET) #include #include #include --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D57FC433F5 for ; Fri, 22 Apr 2022 09:44:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446183AbiDVJrI (ORCPT ); Fri, 22 Apr 2022 05:47:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446166AbiDVJrC (ORCPT ); Fri, 22 Apr 2022 05:47:02 -0400 Received: from esa4.hc1455-7.c3s2.iphmx.com (esa4.hc1455-7.c3s2.iphmx.com [68.232.139.117]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EFA853B5D; Fri, 22 Apr 2022 02:44:09 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="70646198" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="70646198" Received: from unknown (HELO yto-r2.gw.nic.fujitsu.com) ([218.44.52.218]) by esa4.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:07 +0900 Received: from yto-m2.gw.nic.fujitsu.com (yto-nat-yto-m2.gw.nic.fujitsu.com [192.168.83.65]) by yto-r2.gw.nic.fujitsu.com (Postfix) with ESMTP id 06E2DC68B8; Fri, 22 Apr 2022 18:44:05 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by yto-m2.gw.nic.fujitsu.com (Postfix) with ESMTP id 1A32EE6747; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id C9C7F40483741; Fri, 22 Apr 2022 18:44:03 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 4/7] libperf: Add perf_evsel__has_fd() functions Date: Fri, 22 Apr 2022 18:38:30 +0900 Message-Id: <20220422093833.340873-5-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the following functions: perf_evsel__has_fd to check for perf events with the file descriptor specified in the argument. This function can be used in signal handlers to check overflow. Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/Documentation/libperf.txt | 1 + tools/lib/perf/evsel.c | 18 ++++++++++++++++++ tools/lib/perf/include/perf/evsel.h | 1 + tools/lib/perf/libperf.map | 1 + 4 files changed, 21 insertions(+) diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Docu= mentation/libperf.txt index ec93b1c75ebe..bc7881348c76 100644 --- a/tools/lib/perf/Documentation/libperf.txt +++ b/tools/lib/perf/Documentation/libperf.txt @@ -161,6 +161,7 @@ SYNOPSIS int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx); int perf_evsel__disable(struct perf_evsel *evsel); int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx); + bool perf_evsel__has_fd(struct perf_evsel *evsel, int fd); struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel); struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel); struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel); diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index a289f6c44d7c..446934c0e5e5 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -556,3 +556,21 @@ int perf_evsel__open_opts(struct perf_evsel *evsel, st= ruct perf_cpu_map *cpus, =20 return err; } + +bool perf_evsel__has_fd(struct perf_evsel *evsel, int fd) +{ + int cpu_map_idx; + int thread; + int *evsel_fd; + + for (cpu_map_idx =3D 0; cpu_map_idx < xyarray__max_x(evsel->fd); ++cpu_ma= p_idx) { + for (thread =3D 0; thread < xyarray__max_y(evsel->fd); ++thread) { + evsel_fd =3D FD(evsel, cpu_map_idx, thread); + + if (fd =3D=3D *evsel_fd) + return true; + } + } + + return false; +} diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/p= erf/evsel.h index 50662babfe97..19a7993d9021 100644 --- a/tools/lib/perf/include/perf/evsel.h +++ b/tools/lib/perf/include/perf/evsel.h @@ -69,5 +69,6 @@ LIBPERF_API int perf_evsel__open_opts(struct perf_evsel *= evsel, struct perf_cpu_map *cpus, struct perf_thread_map *threads, struct perf_evsel_open_opts *opts); +LIBPERF_API bool perf_evsel__has_fd(struct perf_evsel *evsel, int fd); =20 #endif /* __LIBPERF_EVSEL_H */ diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map index eeeb3075e092..b2ace16bbc32 100644 --- a/tools/lib/perf/libperf.map +++ b/tools/lib/perf/libperf.map @@ -34,6 +34,7 @@ LIBPERF_0.0.1 { perf_evsel__cpus; perf_evsel__threads; perf_evsel__attr; + perf_evsel__has_fd; perf_evlist__new; perf_evlist__delete; perf_evlist__open; --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AB0DC433EF for ; Fri, 22 Apr 2022 09:45:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446207AbiDVJrU (ORCPT ); Fri, 22 Apr 2022 05:47:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446171AbiDVJrD (ORCPT ); Fri, 22 Apr 2022 05:47:03 -0400 Received: from esa4.hc1455-7.c3s2.iphmx.com (esa4.hc1455-7.c3s2.iphmx.com [68.232.139.117]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A973253B54; Fri, 22 Apr 2022 02:44:10 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="70646202" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="70646202" Received: from unknown (HELO yto-r2.gw.nic.fujitsu.com) ([218.44.52.218]) by esa4.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:07 +0900 Received: from yto-m4.gw.nic.fujitsu.com (yto-nat-yto-m4.gw.nic.fujitsu.com [192.168.83.67]) by yto-r2.gw.nic.fujitsu.com (Postfix) with ESMTP id 306CDC68AC; Fri, 22 Apr 2022 18:44:05 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by yto-m4.gw.nic.fujitsu.com (Postfix) with ESMTP id 5AEF03014A; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 1020C40487FBB; Fri, 22 Apr 2022 18:44:04 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 5/7] libperf: Add perf_evsel__{refresh, period}() functions Date: Fri, 22 Apr 2022 18:38:31 +0900 Message-Id: <20220422093833.340873-6-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the following functions: perf_evsel__refresh() perf_evsel__period() to set the over flow limit and period. Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/Documentation/libperf.txt | 2 ++ tools/lib/perf/evsel.c | 44 ++++++++++++++++++++---- tools/lib/perf/include/perf/evsel.h | 2 ++ tools/lib/perf/libperf.map | 2 ++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Docu= mentation/libperf.txt index bc7881348c76..773224a96bd3 100644 --- a/tools/lib/perf/Documentation/libperf.txt +++ b/tools/lib/perf/Documentation/libperf.txt @@ -162,6 +162,8 @@ SYNOPSIS int perf_evsel__disable(struct perf_evsel *evsel); int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx); bool perf_evsel__has_fd(struct perf_evsel *evsel, int fd); + int perf_evsel__refresh(struct perf_evsel *evsel, int refresh); + int perf_evsel__period(struct perf_evsel *evsel, int period); struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel); struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel); struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel); diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 446934c0e5e5..578ae0050036 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -334,7 +334,7 @@ int perf_evsel__read(struct perf_evsel *evsel, int cpu_= map_idx, int thread, } =20 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, - int ioc, void *arg, + int ioc, unsigned long arg, int cpu_map_idx) { int thread; @@ -357,7 +357,7 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evs= el, =20 int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx) { - return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu_map_= idx); + return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, cpu_map_idx= ); } =20 int perf_evsel__enable(struct perf_evsel *evsel) @@ -366,13 +366,13 @@ int perf_evsel__enable(struct perf_evsel *evsel) int err =3D 0; =20 for (i =3D 0; i < xyarray__max_x(evsel->fd) && !err; i++) - err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i); + err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, i); return err; } =20 int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx) { - return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu_map= _idx); + return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, cpu_map_id= x); } =20 int perf_evsel__disable(struct perf_evsel *evsel) @@ -381,7 +381,39 @@ int perf_evsel__disable(struct perf_evsel *evsel) int err =3D 0; =20 for (i =3D 0; i < xyarray__max_x(evsel->fd) && !err; i++) - err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i); + err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, i); + return err; +} + +int perf_evsel__refresh(struct perf_evsel *evsel, int refresh) +{ + int i; + int err =3D 0; + + for (i =3D 0; i < xyarray__max_x(evsel->fd) && !err; i++) + err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_REFRESH, refresh, i); + return err; +} + +int perf_evsel__period(struct perf_evsel *evsel, __u64 period) +{ + struct perf_event_attr *attr; + int i; + int err =3D 0; + + attr =3D perf_evsel__attr(evsel); + if (!attr) + return -EINVAL; + + for (i =3D 0; i < xyarray__max_x(evsel->fd); i++) { + err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_PERIOD, + (unsigned long)&period, i); + if (err) + return err; + } + + attr->sample_period =3D period; + return err; } =20 @@ -392,7 +424,7 @@ int perf_evsel__apply_filter(struct perf_evsel *evsel, = const char *filter) for (i =3D 0; i < perf_cpu_map__nr(evsel->cpus) && !err; i++) err =3D perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_SET_FILTER, - (void *)filter, i); + (unsigned long)filter, i); return err; } =20 diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/p= erf/evsel.h index 19a7993d9021..2c5e52c17d28 100644 --- a/tools/lib/perf/include/perf/evsel.h +++ b/tools/lib/perf/include/perf/evsel.h @@ -60,6 +60,8 @@ LIBPERF_API int perf_evsel__enable(struct perf_evsel *evs= el); LIBPERF_API int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_m= ap_idx); LIBPERF_API int perf_evsel__disable(struct perf_evsel *evsel); LIBPERF_API int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_= map_idx); +LIBPERF_API int perf_evsel__refresh(struct perf_evsel *evsel, int refresh); +LIBPERF_API int perf_evsel__period(struct perf_evsel *evsel, __u64 period); LIBPERF_API struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel= ); LIBPERF_API struct perf_thread_map *perf_evsel__threads(struct perf_evsel = *evsel); LIBPERF_API struct perf_event_attr *perf_evsel__attr(struct perf_evsel *ev= sel); diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map index b2ace16bbc32..d97e208c5be8 100644 --- a/tools/lib/perf/libperf.map +++ b/tools/lib/perf/libperf.map @@ -31,6 +31,8 @@ LIBPERF_0.0.1 { perf_evsel__munmap; perf_evsel__mmap_base; perf_evsel__read; + perf_evsel__refresh; + perf_evsel__period; perf_evsel__cpus; perf_evsel__threads; perf_evsel__attr; --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96B76C433EF for ; Fri, 22 Apr 2022 09:45:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446195AbiDVJsV (ORCPT ); Fri, 22 Apr 2022 05:48:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381631AbiDVJsL (ORCPT ); Fri, 22 Apr 2022 05:48:11 -0400 X-Greylist: delayed 66 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 22 Apr 2022 02:45:14 PDT Received: from esa8.hc1455-7.c3s2.iphmx.com (esa8.hc1455-7.c3s2.iphmx.com [139.138.61.253]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D42153731 for ; Fri, 22 Apr 2022 02:45:13 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="58751382" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="58751382" Received: from unknown (HELO yto-r1.gw.nic.fujitsu.com) ([218.44.52.217]) by esa8.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:06 +0900 Received: from yto-m2.gw.nic.fujitsu.com (yto-nat-yto-m2.gw.nic.fujitsu.com [192.168.83.65]) by yto-r1.gw.nic.fujitsu.com (Postfix) with ESMTP id 7A44CCA260; Fri, 22 Apr 2022 18:44:05 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by yto-m2.gw.nic.fujitsu.com (Postfix) with ESMTP id 95504E6771; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 502B840487FBC; Fri, 22 Apr 2022 18:44:04 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 6/7] libperf test: Add test_stat_overflow() Date: Fri, 22 Apr 2022 18:38:32 +0900 Message-Id: <20220422093833.340873-7-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Added overflow test using refresh and period. Confirmation - That the overflow occurs the number of times specified by perf_evse__refresh() - That the period can be updated by perf_evsel__period() Committer testing: $ sudo make tests -C ./tools/lib/perf V=3D1 make: Entering directory '/home/nakamura/build_work/build_kernel/linux-ke= rnel/linux/tools/lib/perf' make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D. obj=3Dlibperf make -C /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/l= ib/api/ O=3D libapi.a make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./fd obj=3Dlibapi make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./fs obj=3Dlibapi make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D. obj=3Dtests make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./tests obj=3Dtests running static: - running tests/test-cpumap.c...OK - running tests/test-threadmap.c...OK - running tests/test-evlist.c... OK - running tests/test-evsel.c... period =3D 1000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 2000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 1000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 2000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 OK running dynamic: - running tests/test-cpumap.c...OK - running tests/test-threadmap.c...OK - running tests/test-evlist.c... OK - running tests/test-evsel.c... period =3D 1000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 2000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 1000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 period =3D 2000000 overflow limit =3D 3, overflow count =3D 3, POLL_IN =3D 2, POLL_U= P =3D 1, other signal event =3D 0 OK make: Leaving directory '/home/nakamura/build_work/build_kernel/linux-ker= nel/linux/tools/lib/perf' Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/tests/test-evsel.c | 108 ++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tools/lib/perf/tests/test-evsel.c b/tools/lib/perf/tests/test-= evsel.c index 89be89afb24d..4de5d5554836 100644 --- a/tools/lib/perf/tests/test-evsel.c +++ b/tools/lib/perf/tests/test-evsel.c @@ -1,6 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include #include +#include +#include +#include +#include #include #include #include @@ -8,6 +13,15 @@ #include #include "tests.h" =20 +#define WAIT_COUNT 10000000UL +static struct signal_counts { + int in; + int hup; + int others; + int overflow; +} sig_count; +static struct perf_evsel *s_evsel; + static int libperf_print(enum libperf_print_level level, const char *fmt, va_list ap) { @@ -189,6 +203,98 @@ static int test_stat_user_read(int event) return 0; } =20 +static void sig_handler(int signo, siginfo_t *info, void *uc) +{ + switch (info->si_code) { + case POLL_IN: + sig_count.in++; + break; + case POLL_HUP: + sig_count.hup++; + break; + default: + sig_count.others++; + } + + sig_count.overflow++; +} + +static int test_stat_overflow(int owner) +{ + static struct sigaction sig; + u64 period =3D 1000000; + int overflow_limit =3D 3; + + struct perf_thread_map *threads; + struct perf_event_attr attr =3D { + .type =3D PERF_TYPE_SOFTWARE, + .config =3D PERF_COUNT_SW_TASK_CLOCK, + .sample_type =3D PERF_SAMPLE_PERIOD, + .sample_period =3D period, + .disabled =3D 1, + }; + struct perf_event_attr *tmp_attr; + int err =3D 0, i; + + LIBPERF_OPTS(perf_evsel_open_opts, opts, + .open_flags =3D PERF_FLAG_FD_CLOEXEC, + .flags =3D (O_RDWR | O_NONBLOCK | O_ASYNC), + .signal =3D SIGRTMIN + 1, + .owner_type =3D owner, + .sig =3D &sig); + + /* setup signal handler */ + memset(&sig, 0, sizeof(struct sigaction)); + sig.sa_sigaction =3D (void *)sig_handler; + sig.sa_flags =3D SA_SIGINFO; + + threads =3D perf_thread_map__new_dummy(); + __T("failed to create threads", threads); + + perf_thread_map__set_pid(threads, 0, 0); + + s_evsel =3D perf_evsel__new(&attr); + __T("failed to create evsel", s_evsel); + + err =3D perf_evsel__open_opts(s_evsel, NULL, threads, &opts); + __T("failed to open evsel", err =3D=3D 0); + + for (i =3D 0; i < 2; i++) { + volatile unsigned int wait_count =3D WAIT_COUNT; + + sig_count.in =3D 0; + sig_count.hup =3D 0; + sig_count.others =3D 0; + sig_count.overflow =3D 0; + + period =3D period << i; + err =3D perf_evsel__period(s_evsel, period); + __T("failed to period evsel", err =3D=3D 0); + + tmp_attr =3D perf_evsel__attr(s_evsel); + __T_VERBOSE("\tperiod =3D %llu\n", tmp_attr->sample_period); + + err =3D perf_evsel__refresh(s_evsel, overflow_limit); + __T("failed to refresh evsel", err =3D=3D 0); + + while (wait_count--) + ; + + __T_VERBOSE("\toverflow limit =3D %d, overflow count =3D %d, ", + overflow_limit, sig_count.overflow); + __T_VERBOSE("POLL_IN =3D %d, POLL_HUP =3D %d, other signal event =3D %d\= n", + sig_count.in, sig_count.hup, sig_count.others); + + __T("failed to overflow count", overflow_limit =3D=3D sig_count.overflow= ); + } + + perf_evsel__close(s_evsel); + perf_evsel__delete(s_evsel); + perf_thread_map__put(threads); + + return 0; +} + int test_evsel(int argc, char **argv) { __T_START; @@ -200,6 +306,8 @@ int test_evsel(int argc, char **argv) test_stat_thread_enable(); test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS); test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES); + test_stat_overflow(F_OWNER_PID); + test_stat_overflow(F_OWNER_TID); =20 __T_END; return tests_failed =3D=3D 0 ? 0 : -1; --=20 2.25.1 From nobody Sun May 10 22:40:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA801C433F5 for ; Fri, 22 Apr 2022 09:44:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446189AbiDVJrP (ORCPT ); Fri, 22 Apr 2022 05:47:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446167AbiDVJrC (ORCPT ); Fri, 22 Apr 2022 05:47:02 -0400 Received: from esa10.hc1455-7.c3s2.iphmx.com (esa10.hc1455-7.c3s2.iphmx.com [139.138.36.225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5976453B5F; Fri, 22 Apr 2022 02:44:09 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="58614522" X-IronPort-AV: E=Sophos;i="5.90,281,1643641200"; d="scan'208";a="58614522" Received: from unknown (HELO oym-r3.gw.nic.fujitsu.com) ([210.162.30.91]) by esa10.hc1455-7.c3s2.iphmx.com with ESMTP; 22 Apr 2022 18:44:06 +0900 Received: from oym-m3.gw.nic.fujitsu.com (oym-nat-oym-m3.gw.nic.fujitsu.com [192.168.87.60]) by oym-r3.gw.nic.fujitsu.com (Postfix) with ESMTP id BF644D6478; Fri, 22 Apr 2022 18:44:05 +0900 (JST) Received: from oym-om1.fujitsu.com (oym-om1.o.css.fujitsu.com [10.85.58.161]) by oym-m3.gw.nic.fujitsu.com (Postfix) with ESMTP id CBC60D9462; Fri, 22 Apr 2022 18:44:04 +0900 (JST) Received: from localhost.localdomain (bakeccha.fct.css.fujitsu.com [10.126.195.136]) by oym-om1.fujitsu.com (Postfix) with ESMTP id 8B9E040483741; Fri, 22 Apr 2022 18:44:04 +0900 (JST) From: Shunsuke Nakamura To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 7/7] libperf test: Add test_stat_overflow_event() Date: Fri, 22 Apr 2022 18:38:33 +0900 Message-Id: <20220422093833.340873-8-nakamura.shun@fujitsu.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422093833.340873-1-nakamura.shun@fujitsu.com> References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a test to check overflowed events. Committer testing: $ sudo make tests -C ./tools/lib/perf V=3D1 make: Entering directory '/home/nakamura/build_work/build_kernel/linux-ke= rnel/linux/tools/lib/perf' make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D. obj=3Dlibperf make -C /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/l= ib/api/ O=3D libapi.a make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./fd obj=3Dlibapi make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./fs obj=3Dlibapi make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D. obj=3Dtests make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/b= uild/Makefile.build dir=3D./tests obj=3Dtests running static: - running tests/test-cpumap.c...OK - running tests/test-threadmap.c...OK - running tests/test-evlist.c... Event 0 -- overflow flag =3D 0x1, POLL_UP =3D 1, other signal event =3D 0 Event 1 -- overflow flag =3D 0x2, POLL_UP =3D 1, other signal event =3D 0 Event 2 -- overflow flag =3D 0x4, POLL_UP =3D 1, other signal event =3D 0 Event 3 -- overflow flag =3D 0x8, POLL_UP =3D 1, other signal event =3D 0 OK - running tests/test-evsel.c... OK running dynamic: - running tests/test-cpumap.c...OK - running tests/test-threadmap.c...OK - running tests/test-evlist.c... Event 0 -- overflow flag =3D 0x1, POLL_UP =3D 1, other signal event =3D 0 Event 1 -- overflow flag =3D 0x2, POLL_UP =3D 1, other signal event =3D 0 Event 2 -- overflow flag =3D 0x4, POLL_UP =3D 1, other signal event =3D 0 Event 3 -- overflow flag =3D 0x8, POLL_UP =3D 1, other signal event =3D 0 OK - running tests/test-evsel.c... OK make: Leaving directory '/home/nakamura/build_work/build_kernel/linux-ker= nel/linux/tools/lib/perf' Signed-off-by: Shunsuke Nakamura --- tools/lib/perf/tests/test-evlist.c | 111 +++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test= -evlist.c index d8f9493cd4d1..587364851432 100644 --- a/tools/lib/perf/tests/test-evlist.c +++ b/tools/lib/perf/tests/test-evlist.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -24,6 +26,13 @@ #define EVENT_NUM 15 #define WAIT_COUNT 100000000UL =20 +static unsigned int overflow_flag; +static struct signal_counts { + int hup; + int others; +} sig_count; +static struct perf_evlist *s_evlist; + static int libperf_print(enum libperf_print_level level, const char *fmt, va_list ap) { @@ -570,6 +579,107 @@ static int test_stat_multiplexing(void) return 0; } =20 +static void sig_handler(int signo, siginfo_t *info, void *uc) +{ + struct perf_evsel *evsel; + int i =3D 0; + + perf_evlist__for_each_evsel(s_evlist, evsel) { + if (perf_evsel__has_fd(evsel, info->si_fd)) { + if (info->si_code =3D=3D POLL_HUP) + sig_count.hup++; + else + sig_count.others++; + + overflow_flag =3D (1U << i); + return; + } + i++; + } + + __T_VERBOSE("Failed to get fd of overflowed event"); +} + +static int test_stat_overflow_event(void) +{ + static struct sigaction sig; + + struct perf_thread_map *threads; + struct perf_evsel *evsel; + struct perf_event_attr attr =3D { + .type =3D PERF_TYPE_SOFTWARE, + .config =3D PERF_COUNT_SW_CPU_CLOCK, + .sample_type =3D PERF_SAMPLE_PERIOD, + .sample_period =3D 100000, + .disabled =3D 1, + }; + int err, i, event_num =3D 4; + + LIBPERF_OPTS(perf_evsel_open_opts, opts, + .open_flags =3D PERF_FLAG_FD_CLOEXEC, + .flags =3D (O_RDWR | O_NONBLOCK | O_ASYNC), + .signal =3D SIGRTMIN + 1, + .owner_type =3D F_OWNER_PID, + .sig =3D &sig); + + /* setup signal handler */ + memset(&sig, 0, sizeof(struct sigaction)); + sig.sa_sigaction =3D (void *)sig_handler; + sig.sa_flags =3D SA_SIGINFO; + + threads =3D perf_thread_map__new_dummy(); + __T("failed to create threads", threads); + + perf_thread_map__set_pid(threads, 0, 0); + + s_evlist =3D perf_evlist__new(); + __T("failed to create evlist", s_evlist); + + for (i =3D 0; i < event_num; i++) { + evsel =3D perf_evsel__new(&attr); + __T("failed to create evsel", evsel); + + perf_evlist__add(s_evlist, evsel); + } + + perf_evlist__set_maps(s_evlist, NULL, threads); + + err =3D perf_evlist__open_opts(s_evlist, &opts); + __T("failed to open evlist", err =3D=3D 0); + + i =3D 0; + perf_evlist__for_each_evsel(s_evlist, evsel) { + volatile unsigned int wait_count =3D WAIT_COUNT; + + overflow_flag =3D 0; + sig_count.hup =3D 0; + sig_count.others =3D 0; + + err =3D perf_evsel__refresh(evsel, 1); + __T("failed to refresh evsel", err =3D=3D 0); + + while (wait_count--) + ; + + __T_VERBOSE("Event %2d -- overflow flag =3D %#x, ", + i, overflow_flag); + __T_VERBOSE("POLL_HUP =3D %d, other signal event =3D %d\n", + sig_count.hup, sig_count.others); + + __T("unexpected event overflow detected", overflow_flag && (1U << i)); + __T("unexpected signal event detected", + sig_count.hup =3D=3D 1 && sig_count.others =3D=3D 0); + + i++; + } + + perf_evlist__close(s_evlist); + perf_evlist__delete(s_evlist); + perf_thread_map__put(threads); + + return 0; +} + int test_evlist(int argc, char **argv) { __T_START; @@ -582,6 +692,7 @@ int test_evlist(int argc, char **argv) test_mmap_thread(); test_mmap_cpus(); test_stat_multiplexing(); + test_stat_overflow_event(); =20 __T_END; return tests_failed =3D=3D 0 ? 0 : -1; --=20 2.25.1