From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1D5B341BEB; Mon, 6 Feb 2023 12:02:58 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D41BF42D1A; Mon, 6 Feb 2023 12:02:57 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id E9C9042BAC for ; Mon, 6 Feb 2023 12:02:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675681375; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hOiLkBr/9FIBpNciCLmdiCVJR+YqVGaFYZmLtCCcuHQ=; b=LbAWTgvvEjNaAni6NhzMFNEfwUFaFM2Zn7WOeISWoqkUC8w1aLapdch3h3//Qb+sAJbr8H Q80Eg1c/LCrBfG3fSZCGpBLkenopu/gN5caYwPabru8uO18q9S0c6KQ6WrEmBOdEvAGiYl PxLQDnvupJzXgELL/0nocLj8+FRjcBU= Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-499-36hVSkBGN16ZLtisgpjiNg-1; Mon, 06 Feb 2023 06:02:49 -0500 X-MC-Unique: 36hVSkBGN16ZLtisgpjiNg-1 Received: by mail-pl1-f199.google.com with SMTP id m12-20020a1709026bcc00b001963da9cc71so6241176plt.11 for ; Mon, 06 Feb 2023 03:02:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hOiLkBr/9FIBpNciCLmdiCVJR+YqVGaFYZmLtCCcuHQ=; b=7o+Dpxk+H2YIpYIu80o7ITgtOGS9f3LTUor3brhAHK2f3685NAoX/Y20g+f+IcgvV5 psZ7HtPPDp3eis4dKU+MR8JqXszwFrzZ1hPWLRppSx0AV+NwJLojh4q2V81Hn4ujB3ui lR7pwqvU6JDL7iZkH/6WPCw5HFl0UDfeZ05U0Ir9FRR9SNuDdb7Qndx9/M0PLcM+V6vW tIsmlTZo41R+rJcmfMs8hiD8hBZDHeI4JBAsTiW1hvfFOzR3y0/2JS8FKYeu++M7BHqC chPPlIcfSKSMRXC3BDspmWigtjLRx2XFyPocQoQRcY+8KwgqmWok1wEVKr7tN8b7Sv+y eN2A== X-Gm-Message-State: AO0yUKWJN1zdUhPLoLuVbX/CsrYijpu71k78Vc2BpgNDCbdrrHjpvQJr X11AY6C9rsSIhclFuyAyAiLpsZOC5Dy7rKwpZ101vmWWLM+PCuGnNHLAhr9LY3TYJ6wJRfcj82n ptQEhwxM1KT3+45pTh38= X-Received: by 2002:a05:6a00:1a8d:b0:590:6fcc:be6f with SMTP id e13-20020a056a001a8d00b005906fccbe6fmr4221229pfv.49.1675681368410; Mon, 06 Feb 2023 03:02:48 -0800 (PST) X-Google-Smtp-Source: AK7set9cab+9Rrzu8AEeHX37NM1RMLfm45AyULWlAGh7w9AutGwNqh9bQYmuOBa9peSyBJ+DTS03gN1h0BvptXjcG7c= X-Received: by 2002:a05:6a00:1a8d:b0:590:6fcc:be6f with SMTP id e13-20020a056a001a8d00b005906fccbe6fmr4221216pfv.49.1675681367746; Mon, 06 Feb 2023 03:02:47 -0800 (PST) MIME-Version: 1.0 References: <20230202094358.2838758-1-tduszynski@marvell.com> <20230202124951.2915770-1-tduszynski@marvell.com> <20230202124951.2915770-2-tduszynski@marvell.com> In-Reply-To: <20230202124951.2915770-2-tduszynski@marvell.com> From: David Marchand Date: Mon, 6 Feb 2023 12:02:36 +0100 Message-ID: Subject: Re: [PATCH v9 1/4] lib: add generic support for reading PMU events To: Tomasz Duszynski Cc: dev@dpdk.org, Thomas Monjalon , roretzla@linux.microsoft.com, Ruifeng.Wang@arm.com, bruce.richardson@intel.com, jerinj@marvell.com, mattias.ronnblom@ericsson.com, mb@smartsharesystems.com, zhoumin@loongson.cn X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Hello, On Thu, Feb 2, 2023 at 1:50 PM Tomasz Duszynski wr= ote: > > Add support for programming PMU counters and reading their values > in runtime bypassing kernel completely. > > This is especially useful in cases where CPU cores are isolated > (nohz_full) i.e run dedicated tasks. In such cases one cannot use > standard perf utility without sacrificing latency and performance. For my understanding, what OS capability/permission are required to use this library? > > Signed-off-by: Tomasz Duszynski > Acked-by: Morten Br=C3=B8rup > --- > MAINTAINERS | 5 + > app/test/meson.build | 1 + > app/test/test_pmu.c | 55 +++ > doc/api/doxy-api-index.md | 3 +- > doc/api/doxy-api.conf.in | 1 + > doc/guides/prog_guide/profile_app.rst | 8 + > doc/guides/rel_notes/release_23_03.rst | 9 + > lib/meson.build | 1 + > lib/pmu/meson.build | 13 + > lib/pmu/pmu_private.h | 29 ++ > lib/pmu/rte_pmu.c | 464 +++++++++++++++++++++++++ > lib/pmu/rte_pmu.h | 205 +++++++++++ > lib/pmu/version.map | 20 ++ > 13 files changed, 813 insertions(+), 1 deletion(-) > create mode 100644 app/test/test_pmu.c > create mode 100644 lib/pmu/meson.build > create mode 100644 lib/pmu/pmu_private.h > create mode 100644 lib/pmu/rte_pmu.c > create mode 100644 lib/pmu/rte_pmu.h > create mode 100644 lib/pmu/version.map > > diff --git a/MAINTAINERS b/MAINTAINERS > index 9a0f416d2e..9f13eafd95 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -1697,6 +1697,11 @@ M: Nithin Dabilpuram > M: Pavan Nikhilesh > F: lib/node/ > > +PMU - EXPERIMENTAL > +M: Tomasz Duszynski > +F: lib/pmu/ > +F: app/test/test_pmu* > + > > Test Applications > ----------------- > diff --git a/app/test/meson.build b/app/test/meson.build > index f34d19e3c3..7b6b69dcf1 100644 > --- a/app/test/meson.build > +++ b/app/test/meson.build > @@ -111,6 +111,7 @@ test_sources =3D files( > 'test_reciprocal_division_perf.c', > 'test_red.c', > 'test_pie.c', > + 'test_pmu.c', > 'test_reorder.c', > 'test_rib.c', > 'test_rib6.c', This code adds a new test. This test should be added to an existing testsuite, like fast-tests etc... > diff --git a/app/test/test_pmu.c b/app/test/test_pmu.c > new file mode 100644 > index 0000000000..a9bfb1a427 > --- /dev/null > +++ b/app/test/test_pmu.c > @@ -0,0 +1,55 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(C) 2023 Marvell International Ltd. > + */ > + > +#include "test.h" > + > +#ifndef RTE_EXEC_ENV_LINUX > + > +static int > +test_pmu(void) > +{ > + printf("pmu_autotest only supported on Linux, skipping test\n"); > + return TEST_SKIPPED; > +} > + > +#else > + > +#include > + > +static int > +test_pmu_read(void) > +{ > + int tries =3D 10, event =3D -1; > + uint64_t val =3D 0; > + > + if (rte_pmu_init() < 0) > + return TEST_FAILED; > + > + while (tries--) > + val +=3D rte_pmu_read(event); > + > + rte_pmu_fini(); > + > + return val ? TEST_SUCCESS : TEST_FAILED; > +} > + > +static struct unit_test_suite pmu_tests =3D { > + .suite_name =3D "pmu autotest", > + .setup =3D NULL, > + .teardown =3D NULL, > + .unit_test_cases =3D { > + TEST_CASE(test_pmu_read), > + TEST_CASES_END() > + } > +}; > + > +static int > +test_pmu(void) > +{ > + return unit_test_suite_runner(&pmu_tests); > +} > + > +#endif /* RTE_EXEC_ENV_LINUX */ > + > +REGISTER_TEST_COMMAND(pmu_autotest, test_pmu); > diff --git a/doc/api/doxy-api-index.md b/doc/api/doxy-api-index.md > index de488c7abf..7f1938f92f 100644 > --- a/doc/api/doxy-api-index.md > +++ b/doc/api/doxy-api-index.md > @@ -222,7 +222,8 @@ The public API headers are grouped by topics: > [log](@ref rte_log.h), > [errno](@ref rte_errno.h), > [trace](@ref rte_trace.h), > - [trace_point](@ref rte_trace_point.h) > + [trace_point](@ref rte_trace_point.h), > + [pmu](@ref rte_pmu.h) > > - **misc**: > [EAL config](@ref rte_eal.h), > diff --git a/doc/api/doxy-api.conf.in b/doc/api/doxy-api.conf.in > index f0886c3bd1..920e615996 100644 > --- a/doc/api/doxy-api.conf.in > +++ b/doc/api/doxy-api.conf.in > @@ -63,6 +63,7 @@ INPUT =3D @TOPDIR@/doc/api/doxy-api-i= ndex.md \ > @TOPDIR@/lib/pci \ > @TOPDIR@/lib/pdump \ > @TOPDIR@/lib/pipeline \ > + @TOPDIR@/lib/pmu \ > @TOPDIR@/lib/port \ > @TOPDIR@/lib/power \ > @TOPDIR@/lib/rawdev \ > diff --git a/doc/guides/prog_guide/profile_app.rst b/doc/guides/prog_guid= e/profile_app.rst > index 14292d4c25..a8b501fe0c 100644 > --- a/doc/guides/prog_guide/profile_app.rst > +++ b/doc/guides/prog_guide/profile_app.rst > @@ -7,6 +7,14 @@ Profile Your Application > The following sections describe methods of profiling DPDK applications o= n > different architectures. > > +Performance counter based profiling > +----------------------------------- > + > +Majority of architectures support some sort hardware measurement unit wh= ich provides a set of > +programmable counters that monitor specific events. There are different = tools which can gather > +that information, perf being an example here. Though in some scenarios, = eg. when CPU cores are > +isolated (nohz_full) and run dedicated tasks, using perf is less than id= eal. In such cases one can > +read specific events directly from application via ``rte_pmu_read()``. We need a common definition in the documentation of what PMU stands for and use it consistently. I am not sure this documentation is the best place, but at least, I'd prefer we go with "Performance Monitoring Unit" and stick to it. Plus, this block is a bit hard to read too, what do you think of: """ A majority of architectures support some performance monitoring unit (PMU). Such unit provides programmable counters that monitor specific events. Different tools gather that information, like for example perf. However, in some scenarios when CPU cores are isolated (nohz_full) and run dedicated tasks, interrupting those tasks with perf may be undesirable. In such cases, an application can use the PMU library to read such events via ``rte_pmu_read()``. """ And, a double newline is used between sections in this doc. > > Profiling on x86 > ---------------- > diff --git a/doc/guides/rel_notes/release_23_03.rst b/doc/guides/rel_note= s/release_23_03.rst > index 73f5d94e14..733541d56c 100644 > --- a/doc/guides/rel_notes/release_23_03.rst > +++ b/doc/guides/rel_notes/release_23_03.rst > @@ -55,10 +55,19 @@ New Features > Also, make sure to start the actual text at the margin. > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > +* **Added PMU library.** > + > + Added a new PMU (performance measurement unit) library which allows ap= plications Performance Monitoring Unit. > + to perform self monitoring activities without depending on external ut= ilities like perf. > + After integration with :doc:`../prog_guide/trace_lib` data gathered fr= om hardware counters > + can be stored in CTF format for further analysis. Afaiu, this integration comes later in the series. This part of the RN update should go with it. > + > * **Updated AMD axgbe driver.** > > * Added multi-process support. > > +* **Added multi-process support for axgbe PMD.** > + > * **Updated Corigine nfp driver.** > Unrelated rebase damage.. please pay attention to such detail. > * Added support for meter options. > diff --git a/lib/meson.build b/lib/meson.build > index a90fee31b7..7132131b5c 100644 > --- a/lib/meson.build > +++ b/lib/meson.build > @@ -11,6 +11,7 @@ > libraries =3D [ > 'kvargs', # eal depends on kvargs > 'telemetry', # basic info querying > + 'pmu', > 'eal', # everything depends on eal > 'ring', > 'rcu', # rcu depends on ring > diff --git a/lib/pmu/meson.build b/lib/pmu/meson.build > new file mode 100644 > index 0000000000..a4160b494e > --- /dev/null > +++ b/lib/pmu/meson.build > @@ -0,0 +1,13 @@ > +# SPDX-License-Identifier: BSD-3-Clause > +# Copyright(C) 2023 Marvell International Ltd. > + > +if not is_linux > + build =3D false > + reason =3D 'only supported on Linux' > + subdir_done() > +endif > + > +includes =3D [global_inc] > + > +sources =3D files('rte_pmu.c') > +headers =3D files('rte_pmu.h') > diff --git a/lib/pmu/pmu_private.h b/lib/pmu/pmu_private.h > new file mode 100644 > index 0000000000..849549b125 > --- /dev/null > +++ b/lib/pmu/pmu_private.h > @@ -0,0 +1,29 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2023 Marvell > + */ > + > +#ifndef _PMU_PRIVATE_H_ > +#define _PMU_PRIVATE_H_ > + > +/** > + * Architecture specific PMU init callback. > + * > + * @return > + * 0 in case of success, negative value otherwise. > + */ > +int > +pmu_arch_init(void); > + > +/** > + * Architecture specific PMU cleanup callback. > + */ > +void > +pmu_arch_fini(void); > + > +/** > + * Apply architecture specific settings to config before passing it to s= yscall. Please describe config[]. > + */ > +void > +pmu_arch_fixup_config(uint64_t config[3]); > + > +#endif /* _PMU_PRIVATE_H_ */ > diff --git a/lib/pmu/rte_pmu.c b/lib/pmu/rte_pmu.c > new file mode 100644 > index 0000000000..4cf3161155 > --- /dev/null > +++ b/lib/pmu/rte_pmu.c > @@ -0,0 +1,464 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(C) 2023 Marvell International Ltd. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include Asking to be sure because I did not check: do we need all those includes, or is this just copy/pasted from somewhere e= lse? > + > +#include > +#include > +#include > +#include > +#include > + > +#include "pmu_private.h" > + > +#define EVENT_SOURCE_DEVICES_PATH "/sys/bus/event_source/devices" > + > +#ifndef GENMASK_ULL This macro is copy/pasted all over the dpdk tree... This is worth a cleanup later, read: I am not asking for it as part of this series. However, here, there is no need for protecting against its definition. > +#define GENMASK_ULL(h, l) ((~0ULL - (1ULL << (l)) + 1) & (~0ULL >> ((64 = - 1 - (h))))) > +#endif > + > +#ifndef FIELD_PREP > +#define FIELD_PREP(m, v) (((uint64_t)(v) << (__builtin_ffsll(m) - 1)) & = (m)) > +#endif Idem. > + > +RTE_DEFINE_PER_LCORE(struct rte_pmu_event_group, _event_group); > +struct rte_pmu rte_pmu; > + > +/* > + * Following __rte_weak functions provide default no-op. Architectures s= hould override them if > + * necessary. > + */ Prefer using per architectures #ifdef. It is easier to get a simple link error than use weak symbols that make it look like it could work on some arch. > + > +int > +__rte_weak pmu_arch_init(void) > +{ > + return 0; Add a debug log message indicating that this arch does not support PMU. > +} > + > +void > +__rte_weak pmu_arch_fini(void) > +{ > +} > + > +void > +__rte_weak pmu_arch_fixup_config(uint64_t __rte_unused config[3]) > +{ > +} > + > +static int > +get_term_format(const char *name, int *num, uint64_t *mask) > +{ > + char *config =3D NULL; > + char path[PATH_MAX]; > + int high, low, ret; > + FILE *fp; Reverse xmas tree when possible. > + > + /* quiesce -Wmaybe-uninitialized warning */ This comment just seems to be a note for yourself. What was the issue exactly? > + *num =3D 0; > + *mask =3D 0; > + > + snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/forma= t/%s", rte_pmu.name, name); > + fp =3D fopen(path, "r"); > + if (fp =3D=3D NULL) > + return -errno; > + > + errno =3D 0; > + ret =3D fscanf(fp, "%m[^:]:%d-%d", &config, &low, &high); > + if (ret < 2) { > + ret =3D -ENODATA; > + goto out; > + } > + if (errno) { > + ret =3D -errno; > + goto out; > + } > + > + if (ret =3D=3D 2) > + high =3D low; > + > + *mask =3D GENMASK_ULL(high, low); > + /* Last digit should be [012]. If last digit is missing 0 is impl= ied. */ > + *num =3D config[strlen(config) - 1]; > + *num =3D isdigit(*num) ? *num - '0' : 0; > + > + ret =3D 0; > +out: > + free(config); > + fclose(fp); > + > + return ret; > +} > + > +static int > +parse_event(char *buf, uint64_t config[3]) > +{ > + char *token, *term; > + int num, ret, val; > + uint64_t mask; > + > + config[0] =3D config[1] =3D config[2] =3D 0; > + > + token =3D strtok(buf, ","); > + while (token) { > + errno =3D 0; > + /* =3D */ > + ret =3D sscanf(token, "%m[^=3D]=3D%i", &term, &val); > + if (ret < 1) > + return -ENODATA; > + if (errno) > + return -errno; > + if (ret =3D=3D 1) > + val =3D 1; > + > + ret =3D get_term_format(term, &num, &mask); > + free(term); > + if (ret) > + return ret; > + > + config[num] |=3D FIELD_PREP(mask, val); > + token =3D strtok(NULL, ","); > + } > + > + return 0; > +} > + > +static int > +get_event_config(const char *name, uint64_t config[3]) > +{ > + char path[PATH_MAX], buf[BUFSIZ]; > + FILE *fp; > + int ret; > + > + snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/event= s/%s", rte_pmu.name, name); > + fp =3D fopen(path, "r"); > + if (fp =3D=3D NULL) > + return -errno; > + > + ret =3D fread(buf, 1, sizeof(buf), fp); > + if (ret =3D=3D 0) { > + fclose(fp); > + > + return -EINVAL; > + } > + fclose(fp); > + buf[ret] =3D '\0'; > + > + return parse_event(buf, config); > +} > + > +static int > +do_perf_event_open(uint64_t config[3], int group_fd) > +{ > + struct perf_event_attr attr =3D { > + .size =3D sizeof(struct perf_event_attr), > + .type =3D PERF_TYPE_RAW, > + .exclude_kernel =3D 1, > + .exclude_hv =3D 1, > + .disabled =3D 1, > + }; > + > + pmu_arch_fixup_config(config); > + > + attr.config =3D config[0]; > + attr.config1 =3D config[1]; > + attr.config2 =3D config[2]; > + > + return syscall(SYS_perf_event_open, &attr, 0, -1, group_fd, 0); > +} > + > +static int > +open_events(struct rte_pmu_event_group *group) > +{ > + struct rte_pmu_event *event; > + uint64_t config[3]; > + int num =3D 0, ret; > + > + /* group leader gets created first, with fd =3D -1 */ > + group->fds[0] =3D -1; > + > + TAILQ_FOREACH(event, &rte_pmu.event_list, next) { > + ret =3D get_event_config(event->name, config); > + if (ret) > + continue; > + > + ret =3D do_perf_event_open(config, group->fds[0]); > + if (ret =3D=3D -1) { > + ret =3D -errno; > + goto out; > + } > + > + group->fds[event->index] =3D ret; > + num++; > + } > + > + return 0; > +out: > + for (--num; num >=3D 0; num--) { > + close(group->fds[num]); > + group->fds[num] =3D -1; > + } > + > + > + return ret; > +} > + > +static int > +mmap_events(struct rte_pmu_event_group *group) > +{ > + long page_size =3D sysconf(_SC_PAGE_SIZE); > + unsigned int i; > + void *addr; > + int ret; > + > + for (i =3D 0; i < rte_pmu.num_group_events; i++) { > + addr =3D mmap(0, page_size, PROT_READ, MAP_SHARED, group-= >fds[i], 0); > + if (addr =3D=3D MAP_FAILED) { > + ret =3D -errno; > + goto out; > + } > + > + group->mmap_pages[i] =3D addr; > + } > + > + return 0; > +out: > + for (; i; i--) { > + munmap(group->mmap_pages[i - 1], page_size); > + group->mmap_pages[i - 1] =3D NULL; > + } > + > + return ret; > +} > + > +static void > +cleanup_events(struct rte_pmu_event_group *group) > +{ > + unsigned int i; > + > + if (group->fds[0] !=3D -1) > + ioctl(group->fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLA= G_GROUP); > + > + for (i =3D 0; i < rte_pmu.num_group_events; i++) { > + if (group->mmap_pages[i]) { > + munmap(group->mmap_pages[i], sysconf(_SC_PAGE_SIZ= E)); > + group->mmap_pages[i] =3D NULL; > + } > + > + if (group->fds[i] !=3D -1) { > + close(group->fds[i]); > + group->fds[i] =3D -1; > + } > + } > + > + group->enabled =3D false; > +} > + > +int __rte_noinline This symbol is exported out of this library, no need for noinline. > +rte_pmu_enable_group(void) > +{ > + struct rte_pmu_event_group *group =3D &RTE_PER_LCORE(_event_group= ); > + int ret; > + > + if (rte_pmu.num_group_events =3D=3D 0) > + return -ENODEV; > + > + ret =3D open_events(group); > + if (ret) > + goto out; > + > + ret =3D mmap_events(group); > + if (ret) > + goto out; > + > + if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROU= P) =3D=3D -1) { > + ret =3D -errno; > + goto out; > + } > + > + if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GRO= UP) =3D=3D -1) { > + ret =3D -errno; > + goto out; > + } > + > + rte_spinlock_lock(&rte_pmu.lock); > + TAILQ_INSERT_TAIL(&rte_pmu.event_group_list, group, next); > + rte_spinlock_unlock(&rte_pmu.lock); > + group->enabled =3D true; > + > + return 0; > + > +out: > + cleanup_events(group); > + > + return ret; > +} > + > +static int > +scan_pmus(void) > +{ > + char path[PATH_MAX]; > + struct dirent *dent; > + const char *name; > + DIR *dirp; > + > + dirp =3D opendir(EVENT_SOURCE_DEVICES_PATH); > + if (dirp =3D=3D NULL) > + return -errno; > + > + while ((dent =3D readdir(dirp))) { > + name =3D dent->d_name; > + if (name[0] =3D=3D '.') > + continue; > + > + /* sysfs entry should either contain cpus or be a cpu */ > + if (!strcmp(name, "cpu")) > + break; > + > + snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/= %s/cpus", name); > + if (access(path, F_OK) =3D=3D 0) > + break; > + } > + > + if (dent) { > + rte_pmu.name =3D strdup(name); > + if (rte_pmu.name =3D=3D NULL) { > + closedir(dirp); > + > + return -ENOMEM; > + } > + } > + > + closedir(dirp); > + > + return rte_pmu.name ? 0 : -ENODEV; > +} > + > +static struct rte_pmu_event * > +new_event(const char *name) > +{ > + struct rte_pmu_event *event; > + > + event =3D calloc(1, sizeof(*event)); > + if (event =3D=3D NULL) > + goto out; > + > + event->name =3D strdup(name); > + if (event->name =3D=3D NULL) { > + free(event); > + event =3D NULL; > + } > + > +out: > + return event; > +} > + > +static void > +free_event(struct rte_pmu_event *event) > +{ > + free(event->name); > + free(event); > +} > + > +int > +rte_pmu_add_event(const char *name) > +{ > + struct rte_pmu_event *event; > + char path[PATH_MAX]; > + > + if (rte_pmu.name =3D=3D NULL) > + return -ENODEV; > + > + if (rte_pmu.num_group_events + 1 >=3D MAX_NUM_GROUP_EVENTS) > + return -ENOSPC; > + > + snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/event= s/%s", rte_pmu.name, name); > + if (access(path, R_OK)) > + return -ENODEV; > + > + TAILQ_FOREACH(event, &rte_pmu.event_list, next) { > + if (!strcmp(event->name, name)) > + return event->index; > + continue; > + } > + > + event =3D new_event(name); > + if (event =3D=3D NULL) > + return -ENOMEM; > + > + event->index =3D rte_pmu.num_group_events++; > + TAILQ_INSERT_TAIL(&rte_pmu.event_list, event, next); > + > + return event->index; > +} > + > +int > +rte_pmu_init(void) > +{ > + int ret; > + > + /* Allow calling init from multiple contexts within a single thre= ad. This simplifies > + * resource management a bit e.g in case fast-path tracepoint has= already been enabled > + * via command line but application doesn't care enough and perfo= rms init/fini again. > + */ > + if (rte_pmu.initialized) { This is an integer so check against 0 explicitly (there may be other cases in this patch, I did not recheck the whole patch). > + rte_pmu.initialized++; > + return 0; > + } > + > + ret =3D scan_pmus(); > + if (ret) > + goto out; > + > + ret =3D pmu_arch_init(); > + if (ret) > + goto out; > + > + TAILQ_INIT(&rte_pmu.event_list); > + TAILQ_INIT(&rte_pmu.event_group_list); > + rte_spinlock_init(&rte_pmu.lock); > + rte_pmu.initialized =3D 1; > + > + return 0; > +out: > + free(rte_pmu.name); > + rte_pmu.name =3D NULL; > + > + return ret; > +} > + > +void > +rte_pmu_fini(void) > +{ > + struct rte_pmu_event_group *group, *tmp_group; > + struct rte_pmu_event *event, *tmp_event; > + > + /* cleanup once init count drops to zero */ > + if (!rte_pmu.initialized || --rte_pmu.initialized) > + return; > + > + RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_even= t) { > + TAILQ_REMOVE(&rte_pmu.event_list, event, next); > + free_event(event); > + } > + > + RTE_TAILQ_FOREACH_SAFE(group, &rte_pmu.event_group_list, next, tm= p_group) { > + TAILQ_REMOVE(&rte_pmu.event_group_list, group, next); > + cleanup_events(group); > + } > + > + pmu_arch_fini(); > + free(rte_pmu.name); > + rte_pmu.name =3D NULL; > + rte_pmu.num_group_events =3D 0; > +} > diff --git a/lib/pmu/rte_pmu.h b/lib/pmu/rte_pmu.h > new file mode 100644 > index 0000000000..e360375a0c > --- /dev/null > +++ b/lib/pmu/rte_pmu.h > @@ -0,0 +1,205 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2023 Marvell > + */ > + > +#ifndef _RTE_PMU_H_ > +#define _RTE_PMU_H_ > + > +/** > + * @file > + * > + * PMU event tracing operations > + * > + * This file defines generic API and types necessary to setup PMU and > + * read selected counters in runtime. > + */ > + > +#ifdef __cplusplus > +extern "C" { > +#endif > + > +#include > + > +#include > +#include > +#include > +#include > +#include > + > +/** Maximum number of events in a group */ > +#define MAX_NUM_GROUP_EVENTS 8 > + > +/** > + * A structure describing a group of events. > + */ > +struct rte_pmu_event_group { > + struct perf_event_mmap_page *mmap_pages[MAX_NUM_GROUP_EVENTS]; /*= *< array of user pages */ > + int fds[MAX_NUM_GROUP_EVENTS]; /**< array of event descriptors */ > + bool enabled; /**< true if group was enabled on particular lcore = */ > + TAILQ_ENTRY(rte_pmu_event_group) next; /**< list entry */ > +} __rte_cache_aligned; One problem for the future is that we have a fixed size fd array. Do we need to expose this whole structure to the application? > + > +/** > + * A structure describing an event. > + */ > +struct rte_pmu_event { > + char *name; /**< name of an event */ > + unsigned int index; /**< event index into fds/mmap_pages */ This is an internal consideration. Do we need to expose this to the application? > + TAILQ_ENTRY(rte_pmu_event) next; /**< list entry */ > +}; > + > +/** > + * A PMU state container. > + */ > +struct rte_pmu { > + char *name; /**< name of core PMU listed under /sys/bus/event_sou= rce/devices */ > + rte_spinlock_t lock; /**< serialize access to event group list */ > + TAILQ_HEAD(, rte_pmu_event_group) event_group_list; /**< list of = event groups */ > + unsigned int num_group_events; /**< number of events in a group *= / > + TAILQ_HEAD(, rte_pmu_event) event_list; /**< list of matching eve= nts */ > + unsigned int initialized; /**< initialization counter */ > +}; Idem, do we need to expose this to the application? > + > +/** lcore event group */ > +RTE_DECLARE_PER_LCORE(struct rte_pmu_event_group, _event_group); > + > +/** PMU state container */ > +extern struct rte_pmu rte_pmu; > + > +/** Each architecture supporting PMU needs to provide its own version */ > +#ifndef rte_pmu_pmc_read > +#define rte_pmu_pmc_read(index) ({ 0; }) > +#endif > + > +/** > + * @internal > + * > + * Read PMU counter. > + * > + * @param pc > + * Pointer to the mmapped user page. > + * @return > + * Counter value read from hardware. > + */ > +__rte_internal > +static __rte_always_inline uint64_t > +rte_pmu_read_userpage(struct perf_event_mmap_page *pc) > +{ > + uint64_t width, offset; > + uint32_t seq, index; > + int64_t pmc; > + > + for (;;) { > + seq =3D pc->lock; > + rte_compiler_barrier(); > + index =3D pc->index; > + offset =3D pc->offset; > + width =3D pc->pmc_width; > + > + /* index set to 0 means that particular counter cannot be= used */ > + if (likely(pc->cap_user_rdpmc && index)) { > + pmc =3D rte_pmu_pmc_read(index - 1); > + pmc <<=3D 64 - width; > + pmc >>=3D 64 - width; > + offset +=3D pmc; > + } > + > + rte_compiler_barrier(); > + > + if (likely(pc->lock =3D=3D seq)) > + return offset; > + } > + > + return 0; > +} > + > +/** > + * @internal > + * > + * Enable group of events on the calling lcore. > + * > + * @return > + * 0 in case of success, negative value otherwise. > + */ > +__rte_internal Unless I missed something, this symbol is called from rte_pmu_read() so this makes rte_pmu_read() itself internal. So external applications won't be able to use the PMU API. This can probably be confirmed by adding some call to the PMU API in an examples/. > +int > +rte_pmu_enable_group(void); > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * Initialize PMU library. > + * > + * @return > + * 0 in case of success, negative value otherwise. > + */ > +__rte_experimental > +int > +rte_pmu_init(void); > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * Finalize PMU library. This should be called after PMU counters are no= longer being read. > + */ > +__rte_experimental > +void > +rte_pmu_fini(void); > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * Add event to the group of enabled events. > + * > + * @param name > + * Name of an event listed under /sys/bus/event_source/devices/pmu/eve= nts. > + * @return > + * Event index in case of success, negative value otherwise. > + */ > +__rte_experimental > +int > +rte_pmu_add_event(const char *name); > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * Read hardware counter configured to count occurrences of an event. > + * > + * @param index > + * Index of an event to be read. > + * @return > + * Event value read from register. In case of errors or lack of suppor= t > + * 0 is returned. In other words, stream of zeros in a trace file > + * indicates problem with reading particular PMU event register. > + */ > +__rte_experimental > +static __rte_always_inline uint64_t > +rte_pmu_read(unsigned int index) > +{ > + struct rte_pmu_event_group *group =3D &RTE_PER_LCORE(_event_group= ); > + int ret; > + > + if (unlikely(!rte_pmu.initialized)) > + return 0; > + > + if (unlikely(!group->enabled)) { > + ret =3D rte_pmu_enable_group(); > + if (ret) > + return 0; > + } > + > + if (unlikely(index >=3D rte_pmu.num_group_events)) > + return 0; > + > + return rte_pmu_read_userpage(group->mmap_pages[index]); > +} > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif /* _RTE_PMU_H_ */ > diff --git a/lib/pmu/version.map b/lib/pmu/version.map > new file mode 100644 > index 0000000000..50fb0f354e > --- /dev/null > +++ b/lib/pmu/version.map > @@ -0,0 +1,20 @@ > +DPDK_23 { > + local: *; > +}; > + > +EXPERIMENTAL { > + global: > + > + per_lcore__event_group; > + rte_pmu; > + rte_pmu_add_event; > + rte_pmu_fini; > + rte_pmu_init; > + rte_pmu_read; > +}; > + > +INTERNAL { > + global: > + > + rte_pmu_enable_group; > +}; > -- > 2.34.1 > --=20 David Marchand