Skip to contentSkip to conversation

add support for self monitoring

PATCH Superseded Tomasz Duszynski 14 participants 222 messages Nov 11, 2022 – Jun 10, 2025 Page 3 of 4

Subsystems (2)
lore
Share & tools
lore
#121 lore raw

PATCH v11 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  7 +++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 139 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index c257638e8b..e0220e3c59 100644
@@ -24,6 +24,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +7 -0
index a4160b494e..e857681137 100644
@@ -11,3 +11,10 @@ includes = [global_inc]
 
 sources = files('rte_pmu.c')
 headers = files('rte_pmu.h')
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index 6b664c3336..bcc8e3b22d 100644
@@ -26,6 +26,10 @@ extern "C" {
 #include <rte_compat.h>
 #include <rte_spinlock.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..10648f0c5f
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#122 lore raw

PATCH v11 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index e0220e3c59..bed7101a5d 100644
@@ -26,6 +26,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index e857681137..5b92e5c4e3 100644
@@ -13,6 +13,7 @@ sources = files('rte_pmu.c')
 headers = files('rte_pmu.h')
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index bcc8e3b22d..b1d1c17bc5 100644
@@ -28,6 +28,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..7b67466960
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#123 lore raw

PATCH v11 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis latern on. Since trace library supports storing data in a CTF format lets take adventage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++++
 lib/eal/common/eal_common_trace.c        | 13 ++++-
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/include/rte_eal_trace.h          | 13 +++++
 lib/eal/meson.build                      |  3 ++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 61 ++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 14 ++++++
 lib/pmu/version.map                      |  1 +
 11 files changed, 157 insertions(+), 1 deletion(-)
app/test/test_trace_perf.c +10 -0
index 46ae7d8074..f1929f2734 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_EXEC_ENV_LINUX
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_EXEC_ENV_LINUX
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_EXEC_ENV_LINUX
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 89e38cd301..c4dfe85c3b 100644
@@ -19,6 +19,11 @@ 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()``.
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index 3e0ea5835c..9c81936e35 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -137,6 +138,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +12 -1
index 75162b722d..8796052d0c 100644
@@ -11,6 +11,9 @@
 #include <rte_errno.h>
 #include <rte_lcore.h>
 #include <rte_per_lcore.h>
+#ifdef RTE_EXEC_ENV_LINUX
+#include <rte_pmu.h>
+#endif
 #include <rte_string_fns.h>
 
 #include "eal_trace.h"
@@ -71,8 +74,13 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+#ifdef RTE_EXEC_ENV_LINUX
+		if (rte_pmu_init() == 0)
+			rte_pmu_add_events_by_pattern(arg->val);
+#endif
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -88,6 +96,9 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+#ifdef RTE_EXEC_ENV_LINUX
+	rte_pmu_fini();
+#endif
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_points.c +5 -0
index 051f89809c..9d6faa19ed 100644
@@ -77,3 +77,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/include/rte_eal_trace.h +13 -0
index 6f5c022558..c7da83c480 100644
@@ -17,6 +17,9 @@ extern "C" {
 
 #include <rte_alarm.h>
 #include <rte_interrupts.h>
+#ifdef RTE_EXEC_ENV_LINUX
+#include <rte_pmu.h>
+#endif
 #include <rte_trace_point.h>
 
 #include "eal_interrupts.h"
@@ -285,6 +288,16 @@ RTE_TRACE_POINT(
 	rte_trace_point_emit_string(cpuset);
 )
 
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val;
+	val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/meson.build +3 -0
index 056beb9461..f5865dbcd9 100644
@@ -26,6 +26,9 @@ deps += ['kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if is_linux
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/eal/version.map +1 -0
index 2ae57ee78a..01e7a099d2 100644
@@ -441,6 +441,7 @@ EXPERIMENTAL {
 	rte_thread_join;
 
 	# added in 23.03
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 	rte_lcore_register_usage_cb;
 	rte_thread_create_control;
 	rte_thread_set_name;
lib/pmu/rte_pmu.c +61 -0

67 lines not on this page · raw message

lib/pmu/rte_pmu.h +14 -0
index b1d1c17bc5..e1c3bb5e56 100644
@@ -176,6 +176,20 @@ __rte_experimental
 int
 rte_pmu_add_event(const char *name);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/pmu/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
lib/pmu/version.map +1 -0
index 39a4f279c1..e16b3ff009 100644
@@ -9,6 +9,7 @@ EXPERIMENTAL {
 	per_lcore__event_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
Reviewed-by on the v11 series
#124 lore raw
In reply to Tomasz Duszynski
Quoted 86 lines

-----Original Message-----
From: Tomasz Duszynski <[email protected]>
Sent: Friday, February 17, 2023 1:55 AM
To: [email protected]
Cc: [email protected]; Ruifeng Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tomasz Duszynski <[email protected]>
Subject: [PATCH v11 0/4] add support for self monitoring

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   2 +
 app/test/test_pmu.c                      |  68 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  17 +
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_23_03.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |  13 +-
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/include/rte_eal_trace.h          |  13 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   1 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  21 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 521 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 232 ++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 ++
 lib/pmu/version.map                      |  16 +
 23 files changed, 1149 insertions(+), 2 deletions(-)  create mode 100644
Quoted 8 lines

app/test/test_pmu.c create mode 100644 lib/pmu/meson.build create mode 100644 lib/pmu/pmu_arm64.c 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/rte_pmu_pmc_arm64.h create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h create mode 100644 lib/pmu/version.map

--
2.34.1

For the series,

#125 lore raw
In reply to Tomasz Duszynski

Hello Tomasz,

Quoted 18 lines · Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

Quoted 8 lines

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

There are still some pending comments on this series and it can't be merged until they get sorted out.

I noted two points :
- Konstantin asked for better explanations in the implementation.
- He also pointed out at using this feature with non EAL lcores.

Could you work on this so we can consider this series for v23.07?

Thank you.

Signature · 1 line
David Marchand
#126 lore raw

Ping for update
What is the status of this feature?

04/05/2023 10:02, David Marchand:

Quoted 38 lines

Hello Tomasz,

Quoted 18 lines · Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

Quoted 8 lines

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

There are still some pending comments on this series and it can't be merged until they get sorted out.

I noted two points :
- Konstantin asked for better explanations in the implementation.
- He also pointed out at using this feature with non EAL lcores.

Could you work on this so we can consider this series for v23.07?

#127 lore raw
Quoted 12 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Monday, July 31, 2023 2:33 PM
To: Tomasz Duszynski <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob Kollanukkaran <[email protected]>; [email protected]; [email protected]; [email protected]; Konstantin Ananyev <[email protected]>; David Marchand <[email protected]>
Subject: [EXT] Re: [PATCH v11 0/4] add support for self monitoring

Quoted 7 lines

External Email

----------------------------------------------------------------------
Ping for update
What is the status of this feature?

Hi Thomas,

I'll re-spin the series soon.

Quoted 43 lines

04/05/2023 10:02, David Marchand:

Quoted 38 lines

Hello Tomasz,

Quoted 18 lines · Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

Quoted 8 lines

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

There are still some pending comments on this series and it can't be merged until they get sorted out.

I noted two points :
- Konstantin asked for better explanations in the implementation.
- He also pointed out at using this feature with non EAL lcores.

Could you work on this so we can consider this series for v23.07?

#128 lore raw

Hello,

Quoted 4 lines · Tomasz Duszynski
Quoted 2 lines

Ping for update
What is the status of this feature?

I'll re-spin the series soon.

-rc1 is getting closer.
Any update?

Signature · 1 line
David Marchand
#129 lore raw
Quoted 7 lines

From: David Marchand [mailto:[email protected]]
Sent: Thursday, 21 September 2023 10.27

Hello,

On Mon, Aug 7, 2023 at 10:12 AM Tomasz Duszynski <[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Ping for update
What is the status of this feature?

I'll re-spin the series soon.

-rc1 is getting closer.
Any update?

@Thomasz, @Jerin,

I expect this feature to be useful for profiling of systems deployed in production, and would like to see it go into DPDK.

What is its status?

-Morten

#130 lore raw

[...]

Quoted 16 lines
Quoted 8 lines

From: David Marchand [mailto:[email protected]]
Sent: Thursday, 21 September 2023 10.27

Hello,

On Mon, Aug 7, 2023 at 10:12 AM Tomasz Duszynski
<[email protected]>
wrote:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Ping for update
What is the status of this feature?

I'll re-spin the series soon.

-rc1 is getting closer.
Any update?

Quoted 8 lines

@Thomasz, @Jerin,

I expect this feature to be useful for profiling of systems deployed in production, and would like to see it go into DPDK.

What is its status?

-Morten

Hi Morten,

I was buried in other things and did not work on that so to speak. If there's still need for this feature, as you say, I'll respin this series for good. Hopefully new version will be available this month.

#131 lore raw

v12 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  68 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  31 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_24_11.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |  13 +-
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/include/rte_eal_trace.h          |  13 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   3 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  24 ++
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 526 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 226 ++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 ++
 lib/pmu/version.map                      |  15 +
 23 files changed, 1165 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#132 lore raw

PATCH v12 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  62 ++++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  26 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  15 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 465 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 206 +++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 837 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
MAINTAINERS +5 -0
index c5a703b5c0..80bf5968de 100644
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index e29258e6ec..45f56d8aae 100644
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +62 -0

62 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index f9f0300126..805efc6520 100644
@@ -237,7 +237,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),
doc/api/doxy-api.conf.in +1 -0
index a8823c046f..658490b6a2 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +26 -0
index a6b5fb4d5e..ecb90a0d94 100644
@@ -7,6 +7,32 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index 52841d8d56..bafa24f11a 100644
@@ -74,6 +74,13 @@ New Features
   * Added SR-IOV VF support.
   * Added recent 1400/14000 and 15000 models to the supported list.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/meson.build +1 -0
index 162287753f..cc7a4bd535 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +15 -0
new file mode 100644
index 0000000000..3502c73551
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+includes = [global_inc]
+
+sources = files('rte_pmu.c')
+headers = files('rte_pmu.h')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..b9f8c1ddc8
@@ -0,0 +1,32 @@
+/* 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +465 -0

465 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +206 -0

206 lines not on this page · raw message

lib/pmu/version.map new file +14 -0
new file mode 100644
index 0000000000..d7c80ce4ce
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	per_lcore__event_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
Acked-by on v12 1/4
#133 lore raw

RE: [PATCH v12 1/4] lib: add generic support for reading PMU events

Quoted 14 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Saturday, 28 September 2024 00.06

lib/pmu/rte_pmu.c new file +6 -0
+int
+rte_pmu_init(void)
+{
+	int ret;
+
+	/* Allow calling init from multiple contexts within a single

thread. This simplifies

(quoted diff) +1 -0
+	 * resource management a bit e.g in case fast-path tracepoint has

already been enabled

(quoted diff) +1 -0
+	 * via command line but application doesn't care enough and

performs init/fini again.

lib/pmu/rte_pmu.c line 416

Quoted 3 lines
lib/pmu/rte_pmu.c new file +3 -0
+	 */
+	if (rte_pmu.initialized != 0) {
+		rte_pmu.initialized++;

This increment is not multithread safe, does it need to be?

Same about the decrement in rte_pmu_fini().

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return 0;
+	}

With the above fixed, or if it doesn't need fixing,

For the series,

Thank you for resuming work on this. :-)

#134 lore raw

RE: [PATCH v12 1/4] lib: add generic support for reading PMU events

[...]

Quoted 18 lines
Quoted 12 lines
lib/pmu/rte_pmu.c new file +6 -0
+int
+rte_pmu_init(void)
+{
+	int ret;
+
+	/* Allow calling init from multiple contexts within a single

thread. This simplifies

(quoted diff) +1 -0
+	 * resource management a bit e.g in case fast-path tracepoint has

already been enabled

(quoted diff) +1 -0
+	 * via command line but application doesn't care enough and

performs init/fini again.

lib/pmu/rte_pmu.c line 416

Quoted 3 lines
lib/pmu/rte_pmu.c new file +3 -0
+	 */
+	if (rte_pmu.initialized != 0) {
+		rte_pmu.initialized++;

This increment is not multithread safe, does it need to be?

This was introduced *solely* to allow running fast-test (pmu_autotest) along with trace_perf_autotest. The thing is if you execute dpdk-test and have fast tracepoint option enabled then EAL will initialize pmu by itself. OTOH pmu_autotest is more for testing API calls than anything else so it initializes pmu too.

In order to avoid this situation where pmu gets initialized twice we have that counter.

If someone calls this API concurrently then we indeed face synchronization issues as you rightfully pointed out. I don't know to what extent calling init/fini from different context proves useful but I can harden this part just in case. Thanks for pointing this context out.

Quoted 11 lines

Same about the decrement in rte_pmu_fini().

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return 0;
+	}

With the above fixed, or if it doesn't need fixing,

For the series,

Thank you for resuming work on this. :-)

#135 lore raw

Re: [PATCH v12 1/4] lib: add generic support for reading PMU events

In reply to Tomasz Duszynski
Quoted 65 lines · Tomasz Duszynski

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  62 ++++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  26 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  15 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 465 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 206 +++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 837 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
MAINTAINERS +5 -0
index c5a703b5c0..80bf5968de 100644
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index e29258e6ec..45f56d8aae 100644
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +0 -0
new file mode 100644
index 0000000000..5792cf1963

app/test/test_pmu.c line 2

Quoted 2 lines
app/test/test_pmu.c new file +2 -0
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell International Ltd.

2023 -> 2024 across the patches.

app/test/test_pmu.c line 7

Quoted 5 lines
app/test/test_pmu.c new file +5 -0
+ */
+
+#include "test.h"
+
+#ifndef RTE_EXEC_ENV_LINUX

There is a lot of condition compilation across the series. I think, we can remove all that by returning -ENOSUP for rte_pmu_init() and friends for Non Linux Operating systems and adjusting the documentation, so the caller is aware of the contract. or add rte_pmd_feature_is_enabled() or so.

Quoted 82 lines
app/test/test_pmu.c new file +0 -0
 - **misc**:
   [EAL config](@ref rte_eal.h),
doc/api/doxy-api.conf.in +1 -0
index a8823c046f..658490b6a2 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +26 -0
index a6b5fb4d5e..ecb90a0d94 100644
@@ -7,6 +7,32 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 

new file mode 100644
index 0000000000..abf32da967

lib/pmu/rte_pmu.h new file +26 -0
@@ -0,0 +1,206 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_common.h>
+#include <rte_compat.h>

lib/pmu/rte_pmu.h line 36

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+#include <rte_spinlock.h>
+
+/** Maximum number of events in a group */
+#define RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+       /**< array of user pages */

Remove < as comment is before the symbol. Please check generated documentation to tally all documentation.

Quoted 6 lines
lib/pmu/rte_pmu.h new file +6 -0
+       struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+       int fds[RTE_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 */
+};
+
#136 lore raw

RE: [EXTERNAL] Re: [PATCH v12 1/4] lib: add generic support for reading PMU events

Quoted 59 lines · Tomasz Duszynski
Quoted 58 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  62 ++++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  26 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  15 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 465 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 206 +++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 837 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

MAINTAINERS index c5a703b5c0..80bf5968de renamed from MAINTAINERS +0 -0

100644

MAINTAINERS +5 -0
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build index renamed from app/test/meson.build +0 -0

e29258e6ec..45f56d8aae 100644

app/test/meson.build +1 -0
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'], diff --git

a/app/test/test_pmu.c b/app/test/test_pmu.c new file mode 100644 index

Quoted 9 lines

app/test/test_pmu.c line 2

Quoted 6 lines

0000000000..5792cf1963

app/test/test_pmu.c new file +2 -0
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell International Ltd.

2023 -> 2024 across the patches.

Okay. Indeed missed that in a few files.

Quoted 13 lines

app/test/test_pmu.c line 7

Quoted 6 lines
app/test/test_pmu.c new file +5 -0
+ */
+
+#include "test.h"
+
+#ifndef RTE_EXEC_ENV_LINUX

There is a lot of condition compilation across the series.
I think, we can remove all that by returning -ENOSUP for
rte_pmu_init() and friends for Non Linux Operating systems and adjusting the documentation, so the caller is aware of the contract.
or add rte_pmd_feature_is_enabled() or so.

Fine. I removed conditional compilation completely in v13 though the downside is libpmu needs to be always enabled and we have some data in version.map which might not necessarily by true on systems other than Linux. Maybe that's a not high price to pay for a cleaner code though.

Quoted 106 lines
Quoted 27 lines
app/test/test_pmu.c new file +0 -0
 - **misc**:
   [EAL config](@ref rte_eal.h),
doc/api/doxy-api.conf.in index renamed from doc/api/doxy-api.conf.in +0 -0

a8823c046f..658490b6a2 100644

doc/api/doxy-api.conf.in +1 -0
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \ diff --git

a/doc/guides/prog_guide/profile_app.rst
b/doc/guides/prog_guide/profile_app.rst
index a6b5fb4d5e..ecb90a0d94 100644

doc/guides/prog_guide/profile_app.rst +0 -0
@@ -7,6 +7,32 @@ Profile Your Application  The following sections

describe methods of profiling DPDK applications on different architectures.

doc/guides/prog_guide/profile_app.rst +1 -0
+Performance counter based profiling

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 10 lines
doc/guides/prog_guide/profile_app.rst +10 -0
+-----------------------------------
+
+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 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()``.

doc/guides/prog_guide/profile_app.rst

Quoted 6 lines
doc/guides/prog_guide/profile_app.rst +6 -0
+
+By default, userspace applications are not allowed to access PMU
+internals. That can be changed by setting
+``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default
+value anyway) and adding ``CAP_PERFMON`` capability to DPDK
+application. Please refer to ``Documentation/admin-guide/perf-security.rst`` under Linux sources

for more information. Fairly recent kernel, i.e >= 5.9, is advised too.

Quoted 42 lines
doc/guides/prog_guide/profile_app.rst +9 -0
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+

new file mode 100644
index 0000000000..abf32da967

lib/pmu/rte_pmu.h new file +26 -0
@@ -0,0 +1,206 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_common.h>
+#include <rte_compat.h>

lib/pmu/rte_pmu.h line 36

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+#include <rte_spinlock.h>
+
+/** Maximum number of events in a group */ #define
+RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+       /**< array of user pages */

Remove < as comment is before the symbol. Please check generated documentation to tally all documentation.

Quoted 5 lines
lib/pmu/rte_pmu.h new file +5 -0
+       struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+       int fds[RTE_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 */ };
+
#137 lore raw

PATCH v12 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 5792cf1963..e77e0927f3 100644
@@ -24,6 +24,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index 3502c73551..2958c59cba 100644
@@ -12,4 +12,12 @@ includes = [global_inc]
 sources = files('rte_pmu.c')
 headers = files('rte_pmu.h')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index abf32da967..346b8a98f2 100644
@@ -26,6 +26,10 @@ extern "C" {
 #include <rte_compat.h>
 #include <rte_spinlock.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..39165bbc20
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#138 lore raw

PATCH v12 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index e77e0927f3..4e734a8ee4 100644
@@ -26,6 +26,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 2958c59cba..0e4b0cbdb9 100644
@@ -14,6 +14,7 @@ headers = files('rte_pmu.h')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 346b8a98f2..a40f69fb5d 100644
@@ -28,6 +28,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..5eb8f73c11
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#139 lore raw

PATCH v12 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++++
 lib/eal/common/eal_common_trace.c        | 13 ++++-
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/include/rte_eal_trace.h          | 13 +++++
 lib/eal/meson.build                      |  3 ++
 lib/eal/version.map                      |  3 ++
 lib/pmu/rte_pmu.c                        | 61 ++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 14 ++++++
 lib/pmu/version.map                      |  1 +
 11 files changed, 159 insertions(+), 1 deletion(-)
app/test/test_trace_perf.c +10 -0
index 8257cc02be..f1f2e74136 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_EXEC_ENV_LINUX
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_EXEC_ENV_LINUX
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_EXEC_ENV_LINUX
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index ecb90a0d94..d73d919521 100644
@@ -33,6 +33,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..378abccd72 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +12 -1
index 918f49bf4f..5373dbb717 100644
@@ -12,6 +12,9 @@
 #include <rte_errno.h>
 #include <rte_lcore.h>
 #include <rte_per_lcore.h>
+#ifdef RTE_EXEC_ENV_LINUX
+#include <rte_pmu.h>
+#endif
 #include <rte_string_fns.h>
 
 #include "eal_trace.h"
@@ -72,8 +75,13 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+#ifdef RTE_EXEC_ENV_LINUX
+		if (rte_pmu_init() == 0)
+			rte_pmu_add_events_by_pattern(arg->val);
+#endif
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +97,9 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+#ifdef RTE_EXEC_ENV_LINUX
+	rte_pmu_fini();
+#endif
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..05f7ed83d5 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/include/rte_eal_trace.h +13 -0
index c3d15bbe5e..b7e7f5b085 100644
@@ -15,6 +15,9 @@
 extern "C" {
 #endif
 
+#ifdef RTE_EXEC_ENV_LINUX
+#include <rte_pmu.h>
+#endif
 #include <rte_trace_point.h>
 
 /* Generic */
@@ -127,6 +130,16 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val;
+	val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/meson.build +3 -0
index e1d6c4cf17..802b66afee 100644
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if is_linux
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/eal/version.map +3 -0
index e3ff412683..17c56f6bf7 100644
@@ -396,6 +396,9 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_vfio_get_device_info; # WINDOWS_NO_EXPORT
+
+	# added in 24.11
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +61 -0

67 lines not on this page · raw message

lib/pmu/rte_pmu.h +14 -0
index a40f69fb5d..78fa6bba19 100644
@@ -170,6 +170,20 @@ __rte_experimental
 int
 rte_pmu_add_event(const char *name);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/pmu/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
lib/pmu/version.map +1 -0
index d7c80ce4ce..0c98209b4a 100644
@@ -6,6 +6,7 @@ EXPERIMENTAL {
 	per_lcore__event_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#140 lore raw
In reply to Tomasz Duszynski

Hello Tomasz,

Quoted 16 lines · Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

Quoted 7 lines

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v12:
- rebase old series

It seems the series was rebased on next-net (I see a reference to some RN entry in next-net).
Can you rebase on main so CI can test it?

Thanks.

Signature · 1 line
David Marchand
#141 lore raw
Quoted 24 lines

Hello Tomasz,

Quoted 21 lines · Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

Quoted 7 lines
Quoted 2 lines

v12:
- rebase old series

It seems the series was rebased on next-net (I see a reference to some RN entry in next-net). Can you rebase on main so CI can test it?

Thanks.

Missed your comment prior sending v13. I'll resend v14 rebased on main.

Quoted 3 lines

--
David Marchand

#142 lore raw

v13 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  59 +++
 app/test/test_trace_perf.c               |   4 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  34 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_24_11.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   7 +-
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/include/rte_eal_trace.h          |   9 +
 lib/eal/meson.build                      |   2 +-
 lib/eal/version.map                      |   3 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  21 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 523 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 ++
 lib/pmu/version.map                      |  15 +
 23 files changed, 1160 insertions(+), 3 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#143 lore raw

PATCH v13 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  53 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  12 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 462 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 227 ++++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 846 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
MAINTAINERS +5 -0
index c5a703b5c0..80bf5968de 100644
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index e29258e6ec..45f56d8aae 100644
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +53 -0

53 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index f9f0300126..805efc6520 100644
@@ -237,7 +237,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),
doc/api/doxy-api.conf.in +1 -0
index a8823c046f..658490b6a2 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +29 -0
index a6b5fb4d5e..854c515a61 100644
@@ -7,6 +7,35 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index 52841d8d56..bafa24f11a 100644
@@ -74,6 +74,13 @@ New Features
   * Added SR-IOV VF support.
   * Added recent 1400/14000 and 15000 models to the supported list.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/meson.build +1 -0
index 162287753f..cc7a4bd535 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +12 -0
new file mode 100644
index 0000000000..386232e5c7
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+headers = files('rte_pmu.h')
+
+if not is_linux
+    subdir_done()
+endif
+
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +462 -0

462 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +227 -0

227 lines not on this page · raw message

lib/pmu/version.map new file +14 -0
new file mode 100644
index 0000000000..d7c80ce4ce
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	per_lcore__event_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
#144 lore raw

PATCH v13 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 79376ea2e8..e0809a0f93 100644
@@ -13,6 +13,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index 386232e5c7..0d9270eaca 100644
@@ -9,4 +9,12 @@ endif
 
 sources = files('rte_pmu.c')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index 09238ee33d..771fad31d0 100644
@@ -31,6 +31,10 @@ extern "C" {
 #include <rte_branch_prediction.h>
 #include <rte_spinlock.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..39165bbc20
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#145 lore raw

PATCH v13 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index e0809a0f93..2d185574ca 100644
@@ -15,6 +15,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 0d9270eaca..c2e99384e5 100644
@@ -11,6 +11,7 @@ sources = files('rte_pmu.c')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 771fad31d0..248bb55024 100644
@@ -33,6 +33,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..5eb8f73c11
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#146 lore raw

PATCH v13 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               |  4 ++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++++
 lib/eal/common/eal_common_trace.c        |  7 ++-
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/include/rte_eal_trace.h          |  9 ++++
 lib/eal/meson.build                      |  2 +-
 lib/eal/version.map                      |  3 ++
 lib/pmu/rte_pmu.c                        | 61 ++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 18 +++++++
 lib/pmu/version.map                      |  1 +
 11 files changed, 145 insertions(+), 2 deletions(-)
app/test/test_trace_perf.c +4 -0
index 8257cc02be..37af2037b4 100644
@@ -114,6 +114,8 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +124,7 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+WORKER_DEFINE(READ_PMU)
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +177,7 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 854c515a61..1ab6fb9eaa 100644
@@ -36,6 +36,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..378abccd72 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +6 -1
index 918f49bf4f..ffeb822fb7 100644
@@ -12,6 +12,7 @@
 #include <rte_errno.h>
 #include <rte_lcore.h>
 #include <rte_per_lcore.h>
+#include <rte_pmu.h>
 #include <rte_string_fns.h>
 
 #include "eal_trace.h"
@@ -72,8 +73,11 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		if (rte_pmu_init() == 0)
+			rte_pmu_add_events_by_pattern(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +93,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	rte_pmu_fini();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..05f7ed83d5 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/include/rte_eal_trace.h +9 -0
index c3d15bbe5e..27433525aa 100644
@@ -15,6 +15,7 @@
 extern "C" {
 #endif
 
+#include <rte_pmu.h>
 #include <rte_trace_point.h>
 
 /* Generic */
@@ -127,6 +128,14 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val;
+	val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/meson.build +1 -1
index e1d6c4cf17..2b1a1eb283 100644
@@ -14,7 +14,7 @@ subdir(exec_env)
 
 subdir(arch_subdir)
 
-deps += ['log', 'kvargs']
+deps += ['log', 'kvargs', 'pmu']
 if not is_windows
     deps += ['telemetry']
 endif
lib/eal/version.map +3 -0
index e3ff412683..17c56f6bf7 100644
@@ -396,6 +396,9 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_vfio_get_device_info; # WINDOWS_NO_EXPORT
+
+	# added in 24.11
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +61 -0

67 lines not on this page · raw message

lib/pmu/rte_pmu.h +18 -0
index 248bb55024..26264b3b8f 100644
@@ -175,6 +175,20 @@ __rte_experimental
 int
 rte_pmu_add_event(const char *name);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/pmu/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
@@ -221,6 +235,10 @@ static inline void rte_pmu_fini(void) { }
 __rte_experimental
 static inline int rte_pmu_add_event(const char *name __rte_unused) { return -ENOTSUP; }
 
+__rte_experimental
+static inline int
+rte_pmu_add_events_by_pattern(const char *pattern __rte_unused) { return -ENOTSUP; }
+
 __rte_experimental
 static inline uint64_t rte_pmu_read(unsigned int index __rte_unused) { return UINT64_MAX; }
 
lib/pmu/version.map +1 -0
index d7c80ce4ce..0c98209b4a 100644
@@ -6,6 +6,7 @@ EXPERIMENTAL {
 	per_lcore__event_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#147 lore raw

RE: [PATCH v13 4/4] eal: add PMU support to tracing library

Quoted 72 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Wednesday, 9 October 2024 13.23

doc/guides/prog_guide/trace_lib.rst +4 -0
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from

hardware

doc/guides/prog_guide/trace_lib.rst +3 -0
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both

expression

doc/guides/prog_guide/trace_lib.rst +6 -0
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under

``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory

doc/guides/prog_guide/trace_lib.rst +3 -0
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra

variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.

doc/guides/prog_guide/trace_lib.rst +9 -0
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the

``enable_trace_fp``

lib/pmu/rte_pmu.c +12 -0
+option for meson build.
+
 
 
+int
+rte_pmu_add_events_by_pattern(const char *pattern)
+{
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..]

pattern */

lib/pmu/rte_pmu.c +16 -0
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret)
+		return -EINVAL;
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 465

Quoted 10 lines
lib/pmu/rte_pmu.c +10 -0
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
+	}
+
+	regfree(&reg);
+
+	return ret;
+}

This --trace parameter takes a regex, but the --log-level parameter takes a globbing pattern (and a regex, for backwards compatibility, I assume).

This --trace parameter should behave like the --log-level parameter, or if not able to parse both, use globbing pattern, not regex.

Check the --trace parameter parser here:
https://elixir.bootlin.com/dpdk/v24.07/source/lib/eal/common/eal_common_options.c#L1409

#148 lore raw

Re: [PATCH v13 4/4] eal: add PMU support to tracing library

Quoted 85 lines · On Wed, 9 Oct 2024 14:50:02 +0200 Morten Brørup <[email protected]>…
Quoted 72 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Wednesday, 9 October 2024 13.23

doc/guides/prog_guide/trace_lib.rst +4 -0
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from

hardware

doc/guides/prog_guide/trace_lib.rst +3 -0
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both

expression

doc/guides/prog_guide/trace_lib.rst +6 -0
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under

``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory

doc/guides/prog_guide/trace_lib.rst +3 -0
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra

variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.

doc/guides/prog_guide/trace_lib.rst +9 -0
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the

``enable_trace_fp``

lib/pmu/rte_pmu.c +12 -0
+option for meson build.
+  
 
 
+int
+rte_pmu_add_events_by_pattern(const char *pattern)
+{
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..]

pattern */

lib/pmu/rte_pmu.c +16 -0
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret)
+		return -EINVAL;
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 465

Quoted 10 lines
lib/pmu/rte_pmu.c +10 -0
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
+	}
+
+	regfree(&reg);
+
+	return ret;
+}  

This --trace parameter takes a regex, but the --log-level parameter takes a globbing pattern (and a regex, for backwards compatibility, I assume).

Quoted 4 lines

This --trace parameter should behave like the --log-level parameter, or if not able to parse both, use globbing pattern, not regex.

Check the --trace parameter parser here:
https://elixir.bootlin.com/dpdk/v24.07/source/lib/eal/common/eal_common_options.c#L1409

The log level has two API's one takes regex and one takes file glob pattern.
The original one was the regex, but everybody (including the documentation) got regex wrong when the log level format had periods in it. The glob match was more intuitive, and that is what is recommended.

For tracing if you want to use regex then function should be named with _regexp( For extra bonus points, having both a regex and glob version (with pattern) would be great.

#149 lore raw

RE: [EXTERNAL] Re: [PATCH v13 4/4] eal: add PMU support to tracing library

Quoted 88 lines
Quoted 77 lines · On Wed, 9 Oct 2024 14:50:02 +0200 Morten Brørup <[email protected]>…
Quoted 77 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Wednesday, 9 October 2024 13.23

doc/guides/prog_guide/trace_lib.rst +4 -0
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from

hardware

doc/guides/prog_guide/trace_lib.rst +3 -0
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both

expression

doc/guides/prog_guide/trace_lib.rst +6 -0
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and
+expression ``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under

``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory

doc/guides/prog_guide/trace_lib.rst +3 -0
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra

variables

(quoted diff) +2 -0
+added to source files. Instead, caller passes index which follows
+the

order of

(quoted diff) +2 -0
+events specified via ``--trace`` parameter. In the following
+example

index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.

doc/guides/prog_guide/trace_lib.rst +9 -0
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the

``enable_trace_fp``

lib/pmu/rte_pmu.c +11 -0
+option for meson build.
+
 
 
+int
+rte_pmu_add_events_by_pattern(const char *pattern) {
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..]

pattern */

lib/pmu/rte_pmu.c +20 -0
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret)
+		return -EINVAL;
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
Quoted 8 lines

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 465

Quoted 6 lines
lib/pmu/rte_pmu.c +6 -0
+	}
+
+	regfree(&reg);
+
+	return ret;
+}

This --trace parameter takes a regex, but the --log-level parameter takes a globbing pattern (and

Quoted 8 lines

a regex, for backwards compatibility, I assume).

Quoted 2 lines

This --trace parameter should behave like the --log-level parameter, or if not able to parse

both, use globbing pattern, not regex.

Quoted 4 lines

Check the --trace parameter parser here:
https://urldefense.proofpoint.com/v2/url?u=https-3A__elixir.bootlin.co
m_dpdk_v24.07_source_lib_eal_common_eal-5Fcommon-5Foptions.c-23L1409

Just in case, I'll stress that --trace parameter is inherited from tracing library and that itself takes a regex. Checked both documentation and sources for any discrepancies but there are none.

That means there has never been any compatibility with --log-level behavior and I don't think that patch should straight things out.

Maybe addressing that separately would be more sensible.

Quoted 6 lines

The log level has two API's one takes regex and one takes file glob pattern.
The original one was the regex, but everybody (including the documentation) got regex wrong when the log level format had periods in it. The glob match was more intuitive, and that is what is recommended.

Thanks for providing more context.

Quoted 2 lines

For tracing if you want to use regex then function should be named with _regexp( For extra bonus points, having both a regex and glob version (with pattern) would be great.

Adding extra suffix is fine. glob match lacks some syntax used for specifying events to read, which is \| (OR). Checked the link from Morten and seems regex matching is turned on whenever log-level contains a coma. Unfortunately I treat coma as event delimiter so presumably syntax would need to be changed.

Maybe something like '.*pmu.read:cpu_cycles;l1d_cache,*some_other_unrelated tp*' would be more appropriate if making --trace similar to --log-level is the way to go.

#150 lore raw

RE: [EXTERNAL] Re: [PATCH v13 4/4] eal: add PMU support to tracing library

Quoted 139 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Thursday, 10 October 2024 09.24

Quoted 15 lines
Quoted 12 lines · On Wed, 9 Oct 2024 14:50:02 +0200 Morten Brørup <[email protected]>…
Quoted 12 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Wednesday, 9 October 2024 13.23

doc/guides/prog_guide/trace_lib.rst +4 -0
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from

hardware

doc/guides/prog_guide/trace_lib.rst +3 -0
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both

expression

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
doc/guides/prog_guide/trace_lib.rst +6 -0
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and
+expression ``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under

``/sys/bus/event_source/devices/PMU/events`` +directory, where ``PMU`` is a placeholder for either a ``cpu`` or

a

Quoted 57 lines
Quoted 57 lines
Quoted 57 lines

directory

doc/guides/prog_guide/trace_lib.rst +3 -0
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra

variables

(quoted diff) +2 -0
+added to source files. Instead, caller passes index which follows
+the

order of

(quoted diff) +2 -0
+events specified via ``--trace`` parameter. In the following
+example

index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.

doc/guides/prog_guide/trace_lib.rst +9 -0
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the

``enable_trace_fp``

lib/pmu/rte_pmu.c +11 -0
+option for meson build.
+
 
 
+int
+rte_pmu_add_events_by_pattern(const char *pattern) {
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..]

pattern */

lib/pmu/rte_pmu.c +20 -0
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret)
+		return -EINVAL;
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
Quoted 8 lines
Quoted 8 lines

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 465

Quoted 6 lines
lib/pmu/rte_pmu.c +6 -0
+	}
+
+	regfree(&reg);
+
+	return ret;
+}

This --trace parameter takes a regex, but the --log-level parameter

takes a globbing pattern (and

Quoted 3 lines

a regex, for backwards compatibility, I assume).

Quoted 2 lines

This --trace parameter should behave like the --log-level parameter,

or if not able to parse

Quoted 4 lines

both, use globbing pattern, not regex.

Quoted 3 lines

Check the --trace parameter parser here:
https://urldefense.proofpoint.com/v2/url?u=https-

3A__elixir.bootlin.co

Quoted 1 line
Quoted 1 line

m_dpdk_v24.07_source_lib_eal_common_eal-5Fcommon-5Foptions.c-23L1409

Just in case, I'll stress that --trace parameter is inherited from tracing library and that itself takes a regex.
Checked both documentation and sources for any discrepancies but there are none.

That means there has never been any compatibility with --log-level behavior and I don't think that patch
should straight things out.

Maybe addressing that separately would be more sensible.

Quoted 2 lines

The log level has two API's one takes regex and one takes file glob

pattern.

Quoted 1 line

The original one was the regex, but everybody (including the

documentation) got regex wrong when

Quoted 1 line

the log level format had periods in it. The glob match was more

intuitive, and that is what is

Quoted 2 lines

recommended.

Thanks for providing more context.

Quoted 1 line

For tracing if you want to use regex then function should be named

with _regexp( For extra bonus

Quoted 1 line

points, having both a regex and glob version (with pattern) would be

great.

Adding extra suffix is fine. glob match lacks some syntax used for specifying events to read, which is \| (OR). Checked the link from Morten
and seems regex matching is turned on whenever log-level contains a coma. Unfortunately I treat coma as event delimiter so presumably syntax would need to be changed.

Quoted 4 lines

Maybe something like '.*pmu.read:cpu_cycles;l1d_cache,*some_other_unrelated tp*' would be more appropriate if making --trace similar to --log-level is the way to go.

<rant>
It's unfortunate that the parameter formats passed though various means (command line, telemetry, ...) have evolved by piling on more and more, rather than making a proper design, like the other APIs. </rant>

Since this patch is related to trace, you should let trace conventions take precedence over log conventions.

Please ignore my feedback about using log conventions.

Sorry about the noise. ;-)

#151 lore raw

v14 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  59 +++
 app/test/test_trace_perf.c               |   4 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  34 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_24_11.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   7 +-
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/include/rte_eal_trace.h          |   9 +
 lib/eal/meson.build                      |   2 +-
 lib/eal/version.map                      |   3 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  21 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 524 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 ++
 lib/pmu/version.map                      |  15 +
 23 files changed, 1161 insertions(+), 3 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#152 lore raw

PATCH v14 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  53 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  12 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 463 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 227 ++++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 847 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
MAINTAINERS +5 -0
index 7d171e3d45..fbd46905b6 100644
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index e29258e6ec..45f56d8aae 100644
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +53 -0

53 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index f9f0300126..805efc6520 100644
@@ -237,7 +237,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),
doc/api/doxy-api.conf.in +1 -0
index a8823c046f..658490b6a2 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +29 -0
index a6b5fb4d5e..854c515a61 100644
@@ -7,6 +7,35 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index 53d8661365..06fa294787 100644
@@ -150,6 +150,13 @@ New Features
 
   * Added independent enqueue feature.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/meson.build +1 -0
index 162287753f..cc7a4bd535 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +12 -0
new file mode 100644
index 0000000000..386232e5c7
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+headers = files('rte_pmu.h')
+
+if not is_linux
+    subdir_done()
+endif
+
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +463 -0

463 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +227 -0

227 lines not on this page · raw message

lib/pmu/version.map new file +14 -0
new file mode 100644
index 0000000000..d7c80ce4ce
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	per_lcore__event_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
#153 lore raw

RE: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 167 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  53 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  12 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 463 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 227 ++++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 847 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
MAINTAINERS +5 -0
index 7d171e3d45..fbd46905b6 100644
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index e29258e6ec..45f56d8aae 100644
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +53 -0
new file mode 100644
index 0000000000..79376ea2e8
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_pmu.h>
+
+#include "test.h"
+
+static int
+test_pmu_read(void)
+{
+	const char *name = NULL;
+	int tries = 10, event;
+	uint64_t val = 0;
+
+	if (name == NULL) {
+		printf("PMU not supported on this arch\n");
+		return TEST_SKIPPED;
+	}
+
+	if (rte_pmu_init() == -ENOTSUP) {
+		printf("pmu_autotest only supported on Linux, skipping test\n");
+		return TEST_SKIPPED;
+	}
+	if (rte_pmu_init() < 0)
+		return TEST_SKIPPED;
+
+	event = rte_pmu_add_event(name);
+	while (tries--)
+		val += rte_pmu_read(event);
+
+	rte_pmu_fini();
+
+	return val ? TEST_SUCCESS : TEST_FAILED;
+}
+
+static struct unit_test_suite pmu_tests = {
+	.suite_name = "pmu autotest",
+	.setup = NULL,
+	.teardown = NULL,
+	.unit_test_cases = {
+		TEST_CASE(test_pmu_read),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_pmu(void)
+{
+	return unit_test_suite_runner(&pmu_tests);
+}
+
+REGISTER_FAST_TEST(pmu_autotest, true, true, test_pmu);
doc/api/doxy-api-index.md +2 -1
index f9f0300126..805efc6520 100644
@@ -237,7 +237,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),
doc/api/doxy-api.conf.in +1 -0
index a8823c046f..658490b6a2 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +14 -0
index a6b5fb4d5e..854c515a61 100644
@@ -7,6 +7,35 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 33

Quoted 10 lines
doc/guides/prog_guide/profile_app.rst +10 -0
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported

Not that I am against that limitation - it is quite common within DPDK... but reading through the code, not sure where it comes from?

Quoted 118 lines
doc/guides/prog_guide/profile_app.rst +5 -0
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index 53d8661365..06fa294787 100644
@@ -150,6 +150,13 @@ New Features
 
   * Added independent enqueue feature.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/meson.build +1 -0
index 162287753f..cc7a4bd535 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +12 -0
new file mode 100644
index 0000000000..386232e5c7
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+headers = files('rte_pmu.h')
+
+if not is_linux
+    subdir_done()
+endif
+
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +18 -0
new file mode 100644
index 0000000000..3a861bca71
@@ -0,0 +1,463 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <regex.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/queue.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <rte_atomic.h>
+#include <rte_lcore.h>

lib/pmu/rte_pmu.c line 28

Quoted 10 lines
lib/pmu/rte_pmu.c new file +10 -0
+#include <rte_per_lcore.h>
+#include <rte_pmu.h>
+#include <rte_spinlock.h>
+#include <rte_tailq.h>
+
+#include "pmu_private.h"
+
+#define EVENT_SOURCE_DEVICES_PATH "/sys/bus/event_source/devices"
+
+#define GENMASK_ULL(h, l) ((~0ULL - (1ULL << (l)) + 1) & (~0ULL >> ((64 - 1 - (h)))))

Would RTE_GENMASK64(high, low) work for you here?

Quoted 259 lines
lib/pmu/rte_pmu.c new file +259 -0
+#define FIELD_PREP(m, v) (((uint64_t)(v) << (__builtin_ffsll(m) - 1)) & (m))
+
+/* A structure describing an event */
+struct rte_pmu_event {
+	char *name;
+	unsigned int index;
+	TAILQ_ENTRY(rte_pmu_event) next;
+};
+
+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 should override them if
+ * necessary.
+ */
+
+int
+__rte_weak pmu_arch_init(void)
+{
+	return 0;
+}
+
+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 path[PATH_MAX];
+	char *config = NULL;
+	int high, low, ret;
+	FILE *fp;
+
+	*num = *mask = 0;
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/format/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	errno = 0;
+	ret = fscanf(fp, "%m[^:]:%d-%d", &config, &low, &high);
+	if (ret < 2) {
+		ret = -ENODATA;
+		goto out;
+	}
+	if (errno) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ret == 2)
+		high = low;
+
+	*mask = GENMASK_ULL(high, low);
+	/* Last digit should be [012]. If last digit is missing 0 is implied. */
+	*num = config[strlen(config) - 1];
+	*num = isdigit(*num) ? *num - '0' : 0;
+
+	ret = 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] = config[1] = config[2] = 0;
+
+	token = strtok(buf, ",");
+	while (token) {
+		errno = 0;
+		/* <term>=<value> */
+		ret = sscanf(token, "%m[^=]=%i", &term, &val);
+		if (ret < 1)
+			return -ENODATA;
+		if (errno)
+			return -errno;
+		if (ret == 1)
+			val = 1;
+
+		ret = get_term_format(term, &num, &mask);
+		free(term);
+		if (ret)
+			return ret;
+
+		config[num] |= FIELD_PREP(mask, val);
+		token = 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/events/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	ret = fread(buf, 1, sizeof(buf), fp);
+	if (ret == 0) {
+		fclose(fp);
+
+		return -EINVAL;
+	}
+	fclose(fp);
+	buf[ret] = '\0';
+
+	return parse_event(buf, config);
+}
+
+static int
+do_perf_event_open(uint64_t config[3], int group_fd)
+{
+	struct perf_event_attr attr = {
+		.size = sizeof(struct perf_event_attr),
+		.type = PERF_TYPE_RAW,
+		.exclude_kernel = 1,
+		.exclude_hv = 1,
+		.disabled = 1,
+		.pinned = group_fd == -1,
+	};
+
+	pmu_arch_fixup_config(config);
+
+	attr.config = config[0];
+	attr.config1 = config[1];
+	attr.config2 = 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 = 0, ret;
+
+	/* group leader gets created first, with fd = -1 */
+	group->fds[0] = -1;
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		ret = get_event_config(event->name, config);
+		if (ret)
+			continue;
+
+		ret = do_perf_event_open(config, group->fds[0]);
+		if (ret == -1) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->fds[event->index] = ret;
+		num++;
+	}
+
+	return 0;
+out:
+	for (--num; num >= 0; num--) {
+		close(group->fds[num]);
+		group->fds[num] = -1;
+	}
+
+
+	return ret;
+}
+
+static int
+mmap_events(struct rte_pmu_event_group *group)
+{
+	long page_size = sysconf(_SC_PAGE_SIZE);
+	unsigned int i;
+	void *addr;
+	int ret;
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		addr = mmap(0, page_size, PROT_READ, MAP_SHARED, group->fds[i], 0);
+		if (addr == MAP_FAILED) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->mmap_pages[i] = addr;
+	}
+
+	return 0;
+out:
+	for (; i; i--) {
+		munmap(group->mmap_pages[i - 1], page_size);
+		group->mmap_pages[i - 1] = NULL;
+	}
+
+	return ret;
+}
+
+static void
+cleanup_events(struct rte_pmu_event_group *group)
+{
+	unsigned int i;
+
+	if (group->fds[0] != -1)
+		ioctl(group->fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		if (group->mmap_pages[i]) {
+			munmap(group->mmap_pages[i], sysconf(_SC_PAGE_SIZE));
+			group->mmap_pages[i] = NULL;
+		}
+
+		if (group->fds[i] != -1) {
+			close(group->fds[i]);
+			group->fds[i] = -1;
+		}
+	}
+
+	group->enabled = false;
+}
+
+int
+__rte_pmu_enable_group(void)
+{
+	struct rte_pmu_event_group *group = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;

lib/pmu/rte_pmu.c line 297

Quoted 10 lines
lib/pmu/rte_pmu.c new file +10 -0
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -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);

I thought that after previous round of reviews, we got a consensus
that it is a bad idea to insert pointer of TLS variable into the global list:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
As I said before - I believe it is error prone, and needs to be addressed. There are plenty of other ways to have one object per lcore (if that's really necessary):
array of RTE_MAX_LCORE indexed by lcore_id, or new rte_lcore_var.h API introduced by Mattias recently, or even simple TLS pointer with malloc() behind would be better.

Quoted 109 lines
lib/pmu/rte_pmu.c new file +109 -0
+	group->enabled = 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 = opendir(EVENT_SOURCE_DEVICES_PATH);
+	if (dirp == NULL)
+		return -errno;
+
+	while ((dent = readdir(dirp))) {
+		name = dent->d_name;
+		if (name[0] == '.')
+			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) == 0)
+			break;
+	}
+
+	if (dent) {
+		rte_pmu.name = strdup(name);
+		if (rte_pmu.name == 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 = calloc(1, sizeof(*event));
+	if (event == NULL)
+		goto out;
+
+	event->name = strdup(name);
+	if (event->name == NULL) {
+		free(event);
+		event = 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 == NULL)
+		return -ENODEV;
+
+	if (rte_pmu.num_group_events + 1 >= RTE_MAX_NUM_GROUP_EVENTS)
+		return -ENOSPC;
+
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/events/%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 = new_event(name);
+	if (event == NULL)
+		return -ENOMEM;
+
+	event->index = rte_pmu.num_group_events++;
+	TAILQ_INSERT_TAIL(&rte_pmu.event_list, event, next);
+
+	return event->index;
+}
+
+int

lib/pmu/rte_pmu.c line 416

Quoted 10 lines
lib/pmu/rte_pmu.c new file +10 -0
+rte_pmu_init(void)
+{
+	int ret;
+
+	/* Allow calling init from multiple contexts within a single thread. 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 performs init/fini again.
+	 */
+	if (rte_atomic_fetch_add_explicit(&rte_pmu.initialized, 1, rte_memory_order_seq_cst) != 0)
+		return 0;

Not sure I understand the comment above: rte_pmu_init() supposed to be MT safe or not? If yes, then that function probably need to wait till initialization finishes.

Quoted 126 lines
lib/pmu/rte_pmu.c new file +47 -0
+
+	ret = scan_pmus();
+	if (ret)
+		goto out;
+
+	ret = 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);
+
+	return 0;
+out:
+	free(rte_pmu.name);
+	rte_pmu.name = 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_atomic_fetch_sub_explicit(&rte_pmu.initialized, 1,
+					  rte_memory_order_seq_cst) - 1 != 0)
+		return;
+
+	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event) {
+		TAILQ_REMOVE(&rte_pmu.event_list, event, next);
+		free_event(event);
+	}
+
+	RTE_TAILQ_FOREACH_SAFE(group, &rte_pmu.event_group_list, next, tmp_group) {
+		TAILQ_REMOVE(&rte_pmu.event_group_list, group, next);
+		cleanup_events(group);
+	}
+
+	pmu_arch_fini();
+	free(rte_pmu.name);
+	rte_pmu.name = NULL;
+	rte_pmu.num_group_events = 0;
+}
lib/pmu/rte_pmu.h new file +73 -0
new file mode 100644
index 0000000000..09238ee33d
@@ -0,0 +1,227 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 <errno.h>
+
+#include <rte_common.h>
+#include <rte_compat.h>
+
+#ifdef RTE_EXEC_ENV_LINUX
+
+#include <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_spinlock.h>
+
+/** Maximum number of events in a group */
+#define RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+	/** array of user pages */
+	struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+	int fds[RTE_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 */
+};
+
+/**
+ * A PMU state container.
+ */
+struct rte_pmu {
+	char *name; /**< name of core PMU listed under /sys/bus/event_source/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 events */
+	unsigned int initialized; /**< initialization counter */
+};
+
+/** 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) ({ (void)(index); 0; })
+#endif
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice

lib/pmu/rte_pmu.h line 83

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+ *
+ * Read PMU counter.
+ *
+ * @warning This should be not called directly.
+ *
+ * @param pc
+ *   Pointer to the mmapped user page.
+ * @return
+ *   Counter value read from hardware.
+ */

As I already asked you several times in previous review rounds:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
please add a detailed comment here:
i.e. that function (and rte_pmu_read()) to work properly supposed to be used only on a thread that *always* binded to just one cpu core.

Quoted 45 lines
lib/pmu/rte_pmu.h new file +45 -0
+__rte_experimental
+static __rte_always_inline uint64_t
+__rte_pmu_read_userpage(struct perf_event_mmap_page *pc)
+{
+#define __RTE_PMU_READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
+	uint64_t width, offset;
+	uint32_t seq, index;
+	int64_t pmc;
+
+	for (;;) {
+		seq = __RTE_PMU_READ_ONCE(pc->lock);
+		rte_compiler_barrier();
+		index = __RTE_PMU_READ_ONCE(pc->index);
+		offset = __RTE_PMU_READ_ONCE(pc->offset);
+		width = __RTE_PMU_READ_ONCE(pc->pmc_width);
+
+		/* index set to 0 means that particular counter cannot be used */
+		if (likely(pc->cap_user_rdpmc && index)) {
+			pmc = rte_pmu_pmc_read(index - 1);
+			pmc <<= 64 - width;
+			pmc >>= 64 - width;
+			offset += pmc;
+		}
+
+		rte_compiler_barrier();
+
+		if (likely(__RTE_PMU_READ_ONCE(pc->lock) == seq))
+			return offset;
+	}
+
+	return 0;
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable group of events on the calling lcore.
+ *
+ * @warning This should be not called directly.
+ *
+ * @return
+ *   0 in case of success, negative value otherwise.
+ */
+__rte_experimental

lib/pmu/rte_pmu.h line 138

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+int
+__rte_pmu_enable_group(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Initialize PMU library.
+ *
+ * @warning This should be not called directly.

Same questions as before: if it is internal, then why:
a) it is located in public API .h file
b) why it is called directly by user-code in app/test/test_pmu.c ?

Quoted 7 lines
lib/pmu/rte_pmu.h new file +7 -0
+ *
+ * @return
+ *   0 in case of success, negative value otherwise.
+ */
+__rte_experimental
+int
+rte_pmu_init(void);

lib/pmu/rte_pmu.h line 155

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+
+/**
+ * @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);

Hmm..., why _fini_() is allowed to be called directly while _init_() doesn't?

Quoted 93 lines
lib/pmu/rte_pmu.h new file +71 -0
+/**
+ * @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/events.
+ * @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 support
+ *   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 = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (unlikely(!rte_pmu.initialized))
+		return 0;
+
+	if (unlikely(!group->enabled)) {
+		ret = __rte_pmu_enable_group();
+		if (ret)
+			return 0;
+	}
+
+	if (unlikely(index >= rte_pmu.num_group_events))
+		return 0;
+
+	return __rte_pmu_read_userpage(group->mmap_pages[index]);
+}
+
+#else /* !RTE_EXEC_ENV_LINUX */
+
+__rte_experimental
+static inline int rte_pmu_init(void) { return -ENOTSUP; }
+
+__rte_experimental
+static inline void rte_pmu_fini(void) { }
+
+__rte_experimental
+static inline int rte_pmu_add_event(const char *name __rte_unused) { return -ENOTSUP; }
+
+__rte_experimental
+static inline uint64_t rte_pmu_read(unsigned int index __rte_unused) { return UINT64_MAX; }
+
+#endif /* RTE_EXEC_ENV_LINUX */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMU_H_ */
lib/pmu/version.map new file +14 -1
new file mode 100644
index 0000000000..d7c80ce4ce
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	per_lcore__event_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
--

2.34.1

#154 lore raw

Re: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 11 lines · On Fri, 11 Oct 2024 11:56:04 +0000 Konstantin Ananyev…
Quoted 11 lines

+ *
+ * @return
+ * 0 in case of success, negative value otherwise.
+ */

lib/pmu/rte_pmu.h new file +7 -0
+__rte_experimental
+int
+rte_pmu_init(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
Quoted 8 lines

lib/pmu/rte_pmu.h line 155

Quoted 6 lines

+ *
+ * Finalize PMU library. This should be called after PMU counters are no longer being read.
+ */

lib/pmu/rte_pmu.h new file +3 -0
+__rte_experimental
+void
+rte_pmu_fini(void);  

Hmm..., why _fini_() is allowed to be called directly while _init_() doesn't?

Is this handled by destructor, called from rte_eal_cleanup() and/or testpmd?

#155 lore raw

RE: [EXTERNAL] Re: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 38 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, October 11, 2024 4:19 PM
To: Konstantin Ananyev <[email protected]>
Cc: Tomasz Duszynski <[email protected]>; Thomas Monjalon <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v14 1/4] lib: add generic support for reading PMU events

On Fri, 11 Oct 2024 11: 56: 04 +0000 Konstantin Ananyev <konstantin. ananyev@ huawei. com> wrote: >

Quoted 1 line

+ * > > + * @return > > + * 0 in case of success, negative value otherwise. > > + */ > >

+__rte_experimental

Quoted 11 lines · On Fri, 11 Oct 2024 11:56:04 +0000 Konstantin Ananyev…
Quoted 11 lines

+ *
+ * @return
+ * 0 in case of success, negative value otherwise.
+ */

lib/pmu/rte_pmu.h new file +7 -0
+__rte_experimental
+int
+rte_pmu_init(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
Quoted 8 lines

lib/pmu/rte_pmu.h line 155

Quoted 6 lines

+ *
+ * Finalize PMU library. This should be called after PMU counters are no longer being read.
+ */

lib/pmu/rte_pmu.h new file +3 -0
+__rte_experimental
+void
+rte_pmu_fini(void);

Hmm..., why _fini_() is allowed to be called directly while _init_() doesn't?

Quoted 1 line

Is this handled by destructor, called from rte_eal_cleanup() and/or testpmd?

If fast tracepoint is enabled then this is called by eal_trace_fini() which is called by eal_trace_cleanup().

If fast tracepoint is disabled user should call it himself. Though, that counter reading works best with fast trace point because everything is handled automatically.

#156 lore raw

RE: [PATCH v14 1/4] lib: add generic support for reading PMU events

In reply to Konstantin Ananyev
Quoted 166 lines
Quoted 148 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  53 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  12 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 463 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 227 ++++++++++++
 lib/pmu/version.map                    |  14 +
 13 files changed, 847 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

MAINTAINERS index 7d171e3d45..fbd46905b6 renamed from MAINTAINERS +0 -0

100644

MAINTAINERS +5 -0
@@ -1811,6 +1811,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build index renamed from app/test/meson.build +0 -0

e29258e6ec..45f56d8aae 100644

app/test/meson.build +1 -0
@@ -139,6 +139,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'], diff --git

a/app/test/test_pmu.c b/app/test/test_pmu.c new file mode 100644 index 0000000000..79376ea2e8

app/test/test_pmu.c new file +52 -0
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_pmu.h>
+
+#include "test.h"
+
+static int
+test_pmu_read(void)
+{
+	const char *name = NULL;
+	int tries = 10, event;
+	uint64_t val = 0;
+
+	if (name == NULL) {
+		printf("PMU not supported on this arch\n");
+		return TEST_SKIPPED;
+	}
+
+	if (rte_pmu_init() == -ENOTSUP) {
+		printf("pmu_autotest only supported on Linux, skipping test\n");
+		return TEST_SKIPPED;
+	}
+	if (rte_pmu_init() < 0)
+		return TEST_SKIPPED;
+
+	event = rte_pmu_add_event(name);
+	while (tries--)
+		val += rte_pmu_read(event);
+
+	rte_pmu_fini();
+
+	return val ? TEST_SUCCESS : TEST_FAILED; }
+
+static struct unit_test_suite pmu_tests = {
+	.suite_name = "pmu autotest",
+	.setup = NULL,
+	.teardown = NULL,
+	.unit_test_cases = {
+		TEST_CASE(test_pmu_read),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_pmu(void)
+{
+	return unit_test_suite_runner(&pmu_tests);
+}
+
+REGISTER_FAST_TEST(pmu_autotest, true, true, test_pmu);
doc/api/doxy-api-index.md +1 -1
index f9f0300126..805efc6520 100644
@@ -237,7 +237,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),
doc/api/doxy-api.conf.in index renamed from doc/api/doxy-api.conf.in +0 -0

a8823c046f..658490b6a2 100644

doc/api/doxy-api.conf.in +1 -0
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \ diff --git

a/doc/guides/prog_guide/profile_app.rst
b/doc/guides/prog_guide/profile_app.rst
index a6b5fb4d5e..854c515a61 100644

doc/guides/prog_guide/profile_app.rst +0 -0
@@ -7,6 +7,35 @@ Profile Your Application  The following sections

describe methods of profiling DPDK applications on different architectures.

doc/guides/prog_guide/profile_app.rst +1 -0
+Performance counter based profiling

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 10 lines
doc/guides/prog_guide/profile_app.rst +10 -0
+-----------------------------------
+
+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 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()``.

doc/guides/prog_guide/profile_app.rst

Quoted 6 lines
doc/guides/prog_guide/profile_app.rst +6 -0
+
+By default, userspace applications are not allowed to access PMU
+internals. That can be changed by setting
+``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default
+value anyway) and adding ``CAP_PERFMON`` capability to DPDK
+application. Please refer to ``Documentation/admin-guide/perf-security.rst`` under Linux sources

for more information. Fairly recent kernel, i.e >= 5.9, is advised too.

Quoted 12 lines

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 33

Quoted 8 lines
doc/guides/prog_guide/profile_app.rst +8 -0
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will
+return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported

Not that I am against that limitation - it is quite common within DPDK... but reading through the code, not sure where it comes from?

It was tested only on lcores (look TLS for example). I don't know how other constructs behave and what are their specific requirements.

This library has some room for improvements so I think future changes will address that limitations.

Quoted 122 lines
Quoted 14 lines
doc/guides/prog_guide/profile_app.rst +5 -0
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +0 -0
index 53d8661365..06fa294787 100644

doc/guides/rel_notes/release_24_11.rst New Features · line 156

Quoted 7 lines
doc/guides/rel_notes/release_24_11.rst +4 -0
@@ -150,6 +150,13 @@ New Features
 
   * Added independent enqueue feature.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows
+ applications  to perform self monitoring activities without depending on external utilities

like perf.

Quoted 100 lines
doc/guides/rel_notes/release_24_11.rst +3 -0
+  After integration with :doc:`../prog_guide/trace_lib` data gathered
+ from hardware counters  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/meson.build index renamed from lib/meson.build +0 -0

162287753f..cc7a4bd535 100644

lib/meson.build +1 -0
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file mode renamed from lib/pmu/meson.build +0 -0

100644 index 0000000000..386232e5c7

lib/pmu/meson.build new file +12 -0
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(C) 2024 Marvell
+International Ltd.
+
+headers = files('rte_pmu.h')
+
+if not is_linux
+    subdir_done()
+endif
+
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file renamed from lib/pmu/pmu_private.h +0 -0

mode 100644 index 0000000000..d2b15615bf

lib/pmu/pmu_private.h new file +32 -0
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file mode renamed from lib/pmu/rte_pmu.c +0 -0

100644 index 0000000000..3a861bca71

lib/pmu/rte_pmu.c new file +23 -0
@@ -0,0 +1,463 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <regex.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/queue.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <rte_atomic.h>
+#include <rte_lcore.h>
+#include <rte_per_lcore.h>
+#include <rte_pmu.h>
+#include <rte_spinlock.h>
+#include <rte_tailq.h>
+
Quoted 8 lines

lib/pmu/rte_pmu.c

Quoted 6 lines
lib/pmu/rte_pmu.c new file +6 -0
+#include "pmu_private.h"
+
+#define EVENT_SOURCE_DEVICES_PATH "/sys/bus/event_source/devices"
+
+#define GENMASK_ULL(h, l) ((~0ULL - (1ULL << (l)) + 1) & (~0ULL >>
+((64 - 1 - (h)))))

Would RTE_GENMASK64(high, low) work for you here?

If that RTE_GENMASK64 generates continuous mask of 1s then it would work. I didn't realize DPDK has it.

Quoted 262 lines
Quoted 251 lines
lib/pmu/rte_pmu.c new file +250 -0
+#define FIELD_PREP(m, v) (((uint64_t)(v) << (__builtin_ffsll(m) - 1))
+& (m))
+
+/* A structure describing an event */ struct rte_pmu_event {
+	char *name;
+	unsigned int index;
+	TAILQ_ENTRY(rte_pmu_event) next;
+};
+
+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 should override them if
+ * necessary.
+ */
+
+int
+__rte_weak pmu_arch_init(void)
+{
+	return 0;
+}
+
+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 path[PATH_MAX];
+	char *config = NULL;
+	int high, low, ret;
+	FILE *fp;
+
+	*num = *mask = 0;
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/format/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	errno = 0;
+	ret = fscanf(fp, "%m[^:]:%d-%d", &config, &low, &high);
+	if (ret < 2) {
+		ret = -ENODATA;
+		goto out;
+	}
+	if (errno) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ret == 2)
+		high = low;
+
+	*mask = GENMASK_ULL(high, low);
+	/* Last digit should be [012]. If last digit is missing 0 is implied. */
+	*num = config[strlen(config) - 1];
+	*num = isdigit(*num) ? *num - '0' : 0;
+
+	ret = 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] = config[1] = config[2] = 0;
+
+	token = strtok(buf, ",");
+	while (token) {
+		errno = 0;
+		/* <term>=<value> */
+		ret = sscanf(token, "%m[^=]=%i", &term, &val);
+		if (ret < 1)
+			return -ENODATA;
+		if (errno)
+			return -errno;
+		if (ret == 1)
+			val = 1;
+
+		ret = get_term_format(term, &num, &mask);
+		free(term);
+		if (ret)
+			return ret;
+
+		config[num] |= FIELD_PREP(mask, val);
+		token = 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/events/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	ret = fread(buf, 1, sizeof(buf), fp);
+	if (ret == 0) {
+		fclose(fp);
+
+		return -EINVAL;
+	}
+	fclose(fp);
+	buf[ret] = '\0';
+
+	return parse_event(buf, config);
+}
+
+static int
+do_perf_event_open(uint64_t config[3], int group_fd) {
+	struct perf_event_attr attr = {
+		.size = sizeof(struct perf_event_attr),
+		.type = PERF_TYPE_RAW,
+		.exclude_kernel = 1,
+		.exclude_hv = 1,
+		.disabled = 1,
+		.pinned = group_fd == -1,
+	};
+
+	pmu_arch_fixup_config(config);
+
+	attr.config = config[0];
+	attr.config1 = config[1];
+	attr.config2 = 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 = 0, ret;
+
+	/* group leader gets created first, with fd = -1 */
+	group->fds[0] = -1;
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		ret = get_event_config(event->name, config);
+		if (ret)
+			continue;
+
+		ret = do_perf_event_open(config, group->fds[0]);
+		if (ret == -1) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->fds[event->index] = ret;
+		num++;
+	}
+
+	return 0;
+out:
+	for (--num; num >= 0; num--) {
+		close(group->fds[num]);
+		group->fds[num] = -1;
+	}
+
+
+	return ret;
+}
+
+static int
+mmap_events(struct rte_pmu_event_group *group) {
+	long page_size = sysconf(_SC_PAGE_SIZE);
+	unsigned int i;
+	void *addr;
+	int ret;
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		addr = mmap(0, page_size, PROT_READ, MAP_SHARED, group->fds[i], 0);
+		if (addr == MAP_FAILED) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->mmap_pages[i] = addr;
+	}
+
+	return 0;
+out:
+	for (; i; i--) {
+		munmap(group->mmap_pages[i - 1], page_size);
+		group->mmap_pages[i - 1] = NULL;
+	}
+
+	return ret;
+}
+
+static void
+cleanup_events(struct rte_pmu_event_group *group) {
+	unsigned int i;
+
+	if (group->fds[0] != -1)
+		ioctl(group->fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		if (group->mmap_pages[i]) {
+			munmap(group->mmap_pages[i], sysconf(_SC_PAGE_SIZE));
+			group->mmap_pages[i] = NULL;
+		}
+
+		if (group->fds[i] != -1) {
+			close(group->fds[i]);
+			group->fds[i] = -1;
+		}
+	}
+
+	group->enabled = false;
+}
+
+int
+__rte_pmu_enable_group(void)
+{
+	struct rte_pmu_event_group *group = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;

lib/pmu/rte_pmu.c line 297

Quoted 10 lines
lib/pmu/rte_pmu.c new file +10 -0
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -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);
Quoted 8 lines

I thought that after previous round of reviews, we got a consensus that it is a bad idea to insert pointer of TLS variable into the global list:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__patchwork.dpdk.org_project_dpdk_patch_20230216175502.3164820-2D2-2Dtduszynski- 40marvell.com_&d=DwIFAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5ce22YI6Is&m=oJ - eSnmJoK0r1zVFhKrkWMnfelOkxqpjtX2fCrXaG2RdWagOqAQ7vcFCJ0dOWrTt&s=TvGxqQmUz_U3xLOroMxOmsCiaxdqbNLi6GZ pHIefniw&e=

I don't think there was any consensus. It was rather your point of view solely. So I still believe that given currently that only runs on lcores and lcores do not terminate before the main one hence it's safe to access TLS from a main.

Once support for running on anything other than lcore gets added current mechanics is likely to change.

And as you pointed out, there are plenty of options available.

Quoted 118 lines

As I said before - I believe it is error prone, and needs to be addressed. There are plenty of other ways to have one object per lcore (if that's really necessary):
array of RTE_MAX_LCORE indexed by lcore_id, or new rte_lcore_var.h API introduced by Mattias recently, or even simple TLS pointer with malloc() behind would be better.

Quoted 113 lines
lib/pmu/rte_pmu.c new file +113 -0
+	group->enabled = 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 = opendir(EVENT_SOURCE_DEVICES_PATH);
+	if (dirp == NULL)
+		return -errno;
+
+	while ((dent = readdir(dirp))) {
+		name = dent->d_name;
+		if (name[0] == '.')
+			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) == 0)
+			break;
+	}
+
+	if (dent) {
+		rte_pmu.name = strdup(name);
+		if (rte_pmu.name == 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 = calloc(1, sizeof(*event));
+	if (event == NULL)
+		goto out;
+
+	event->name = strdup(name);
+	if (event->name == NULL) {
+		free(event);
+		event = 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 == NULL)
+		return -ENODEV;
+
+	if (rte_pmu.num_group_events + 1 >= RTE_MAX_NUM_GROUP_EVENTS)
+		return -ENOSPC;
+
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/events/%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 = new_event(name);
+	if (event == NULL)
+		return -ENOMEM;
+
+	event->index = 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 thread. This simplifies
Quoted 9 lines

lib/pmu/rte_pmu.c line 416

Quoted 5 lines
lib/pmu/rte_pmu.c new file +5 -0
+	 * 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 performs init/fini again.
+	 */
+	if (rte_atomic_fetch_add_explicit(&rte_pmu.initialized, 1, rte_memory_order_seq_cst) != 0)
+		return 0;

Not sure I understand the comment above: rte_pmu_init() supposed to be MT safe or not? If yes, then that function probably need to wait till initialization finishes.

It's supposed to be MT safe. Generally there are two options here.

# one you pointed out i.e spinning in a loop until everything is up and running 
# checking if tracing is initialized during event read-out

Initially second option was chosen and rte_pmu_read() performs check prior to doing anything. Still, I see a problem there so I'm glad you raised that.

Quoted 135 lines
Quoted 122 lines
lib/pmu/rte_pmu.c new file +47 -0
+
+	ret = scan_pmus();
+	if (ret)
+		goto out;
+
+	ret = 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);
+
+	return 0;
+out:
+	free(rte_pmu.name);
+	rte_pmu.name = 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_atomic_fetch_sub_explicit(&rte_pmu.initialized, 1,
+					  rte_memory_order_seq_cst) - 1 != 0)
+		return;
+
+	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event) {
+		TAILQ_REMOVE(&rte_pmu.event_list, event, next);
+		free_event(event);
+	}
+
+	RTE_TAILQ_FOREACH_SAFE(group, &rte_pmu.event_group_list, next, tmp_group) {
+		TAILQ_REMOVE(&rte_pmu.event_group_list, group, next);
+		cleanup_events(group);
+	}
+
+	pmu_arch_fini();
+	free(rte_pmu.name);
+	rte_pmu.name = NULL;
+	rte_pmu.num_group_events = 0;
+}
lib/pmu/rte_pmu.h new file mode renamed from lib/pmu/rte_pmu.h +0 -0

100644 index 0000000000..09238ee33d

lib/pmu/rte_pmu.h new file +70 -0
@@ -0,0 +1,227 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 <errno.h>
+
+#include <rte_common.h>
+#include <rte_compat.h>
+
+#ifdef RTE_EXEC_ENV_LINUX
+
+#include <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_spinlock.h>
+
+/** Maximum number of events in a group */ #define
+RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+	/** array of user pages */
+	struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+	int fds[RTE_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 */ };
+
+/**
+ * A PMU state container.
+ */
+struct rte_pmu {
+	char *name; /**< name of core PMU listed under /sys/bus/event_source/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 events */
+	unsigned int initialized; /**< initialization counter */ };
+
+/** 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) ({
+(void)(index); 0; }) #endif
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice

lib/pmu/rte_pmu.h line 80

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+ *
+ * Read PMU counter.
+ *
+ * @warning This should be not called directly.
+ *
+ * @param pc
+ *   Pointer to the mmapped user page.
+ * @return
+ *   Counter value read from hardware.
+ */

As I already asked you several times in previous review rounds:
https://urldefense.proofpoint.com/v2/url?u=https-

Quoted 9 lines

3A__patchwork.dpdk.org_project_dpdk_patch_20230216175502.3164820-2D2-2Dtduszynski- 40marvell.com_&d=DwIFAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5ce22YI6Is&m=oJ - eSnmJoK0r1zVFhKrkWMnfelOkxqpjtX2fCrXaG2RdWagOqAQ7vcFCJ0dOWrTt&s=TvGxqQmUz_U3xLOroMxOmsCiaxdqbNLi6GZ pHIefniw&e=
please add a detailed comment here:
i.e. that function (and rte_pmu_read()) to work properly supposed to be used only on a thread that *always* binded to just one cpu core.

Okay I will add comment under limitations that we want one lcore per cpu core.

Quoted 50 lines
Quoted 50 lines
lib/pmu/rte_pmu.h new file +50 -0
+__rte_experimental
+static __rte_always_inline uint64_t
+__rte_pmu_read_userpage(struct perf_event_mmap_page *pc) { #define
+__RTE_PMU_READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
+	uint64_t width, offset;
+	uint32_t seq, index;
+	int64_t pmc;
+
+	for (;;) {
+		seq = __RTE_PMU_READ_ONCE(pc->lock);
+		rte_compiler_barrier();
+		index = __RTE_PMU_READ_ONCE(pc->index);
+		offset = __RTE_PMU_READ_ONCE(pc->offset);
+		width = __RTE_PMU_READ_ONCE(pc->pmc_width);
+
+		/* index set to 0 means that particular counter cannot be used */
+		if (likely(pc->cap_user_rdpmc && index)) {
+			pmc = rte_pmu_pmc_read(index - 1);
+			pmc <<= 64 - width;
+			pmc >>= 64 - width;
+			offset += pmc;
+		}
+
+		rte_compiler_barrier();
+
+		if (likely(__RTE_PMU_READ_ONCE(pc->lock) == seq))
+			return offset;
+	}
+
+	return 0;
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable group of events on the calling lcore.
+ *
+ * @warning This should be not called directly.
+ *
+ * @return
+ *   0 in case of success, negative value otherwise.
+ */
+__rte_experimental
+int
+__rte_pmu_enable_group(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
Quoted 9 lines

lib/pmu/rte_pmu.h line 138

Quoted 4 lines
lib/pmu/rte_pmu.h new file +4 -0
+ *
+ * Initialize PMU library.
+ *
+ * @warning This should be not called directly.

Same questions as before: if it is internal, then why:
a) it is located in public API .h file
b) why it is called directly by user-code in app/test/test_pmu.c ?

Some functions are located in public API file out of convenience. They are either macros or inline functions. Documentation points out which are safe to be called directly which are not so user-space knows the rules.

Quoted 11 lines
Quoted 11 lines
lib/pmu/rte_pmu.h new file +11 -0
+ *
+ * @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
Quoted 9 lines

lib/pmu/rte_pmu.h line 155

Quoted 6 lines
lib/pmu/rte_pmu.h new file +6 -0
+ *
+ * Finalize PMU library. This should be called after PMU counters are no longer being read.
+ */
+__rte_experimental
+void
+rte_pmu_fini(void);

Hmm..., why _fini_() is allowed to be called directly while _init_() doesn't?

Typo. Functions that should not be called directly are prefixed with double underscore. I'll scan through header file once again and remove invalid comments. Thanks for catching this.

Quoted 94 lines
Quoted 94 lines
lib/pmu/rte_pmu.h new file +73 -0
+/**
+ * @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/events.
+ * @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 support
+ *   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 = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (unlikely(!rte_pmu.initialized))
+		return 0;
+
+	if (unlikely(!group->enabled)) {
+		ret = __rte_pmu_enable_group();
+		if (ret)
+			return 0;
+	}
+
+	if (unlikely(index >= rte_pmu.num_group_events))
+		return 0;
+
+	return __rte_pmu_read_userpage(group->mmap_pages[index]);
+}
+
+#else /* !RTE_EXEC_ENV_LINUX */
+
+__rte_experimental
+static inline int rte_pmu_init(void) { return -ENOTSUP; }
+
+__rte_experimental
+static inline void rte_pmu_fini(void) { }
+
+__rte_experimental
+static inline int rte_pmu_add_event(const char *name __rte_unused) {
+return -ENOTSUP; }
+
+__rte_experimental
+static inline uint64_t rte_pmu_read(unsigned int index __rte_unused)
+{ return UINT64_MAX; }
+
+#endif /* RTE_EXEC_ENV_LINUX */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMU_H_ */
lib/pmu/version.map new file mode renamed from lib/pmu/version.map +0 -0

100644 index 0000000000..d7c80ce4ce

lib/pmu/version.map new file +14 -1
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	per_lcore__event_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
--

2.34.1

#157 lore raw

RE: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 42 lines
Quoted 33 lines
Quoted 22 lines
lib/pmu/rte_pmu.c new file +20 -0
+int
+__rte_pmu_enable_group(void)
+{
+	struct rte_pmu_event_group *group = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;

lib/pmu/rte_pmu.c line 297

Quoted 10 lines
lib/pmu/rte_pmu.c new file +10 -0
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -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);
Quoted 8 lines

I thought that after previous round of reviews, we got a consensus that it is a bad idea to insert pointer of TLS variable into the global list:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__patchwork.dpdk.org_project_dpdk_patch_20230216175502.3164820-2D2-2Dtduszynski- 40marvell.com_&d=DwIFAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5ce22YI6Is&m=oJ - eSnmJoK0r1zVFhKrkWMnfelOkxqpjtX2fCrXaG2RdWagOqAQ7vcFCJ0dOWrTt&s=TvGxqQmUz_U3xLOroMxOmsCiaxdqbNLi6GZ pHIefniw&e=

Quoted 1 line

I don't think there was any consensus. It was rather your point of view solely.

Here is a mail where I highlighted the problem:
https://inbox.dpdk.org/dev/[email protected]/

Here is a mail where Morten agreed that it needs to be addressed:
https://inbox.dpdk.org/dev/[email protected]/

Here is a mail from David, where he summarizes the remaining work required for these series:
https://inbox.dpdk.org/dev/DM4PR18MB43684B889C50F20DDD1B0A29D20CA@DM4PR18MB4368.namprd18.prod.outlook.com/
"...
- Konstantin asked for better explanations in the implementation.
- He also pointed out at using this feature with non EAL lcores.
..."

Here is your reply:
https://inbox.dpdk.org/dev/DM4PR18MB43684B889C50F20DDD1B0A29D20CA@DM4PR18MB4368.namprd18.prod.outlook.com/

You didn't object, so I interpreted that (probably wrongly) that we had a consensus here.

Quoted 3 lines

So I still believe
that given currently that only runs on lcores and lcores do not terminate before the main one hence it's safe to access TLS from a main.

Could you clarify what you means by 'lcores' here?
Threads with valid 'lcore_id'?
There is an API within DPDK that allows user
to assign vacant lcore_id ((rte_thread_register) to some new thread, run it for some time, then release lcore_id (rte_thread_unregister) and then terminate the thread.

Another thing - there is no checks within PMU API around does this thread satisfy expected criteria's or not. So if user will call PMU API from 'wrong' thread - it would succeed and for many cases will keep working as expected. But for other cases (thread lifetime is shorter then program lifetime) it might cause a crash or silent memory corruption.

Quoted 1 line

Once support for running on anything other than lcore gets added current mechanics is likely to change.

Honestly, using TLS variables within the global list here - is just asking for trouble. Please fix it, there are plenty of simple ways to avoid such issue once and for all. Without this problem addressed, my vote for that patch is NACK.

Quoted 6 lines

And as you pointed out, there are plenty of options available.

Quoted 4 lines

As I said before - I believe it is error prone, and needs to be addressed. There are plenty of other ways to have one object per lcore (if that's really necessary):
array of RTE_MAX_LCORE indexed by lcore_id, or new rte_lcore_var.h API introduced by Mattias recently, or even simple TLS pointer with malloc() behind would be better.

#158 lore raw

RE: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 108 lines

-----Original Message-----
From: Konstantin Ananyev <[email protected]>
Sent: Wednesday, October 16, 2024 10:50 AM
To: Tomasz Duszynski <[email protected]>; Thomas Monjalon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXTERNAL] RE: [PATCH v14 1/4] lib: add generic support for reading PMU events

Quoted 43 lines
Quoted 32 lines
Quoted 32 lines
lib/pmu/rte_pmu.c new file +29 -0
+int > >> +__rte_pmu_enable_group(void) > >> +{ > >> + struct
+rte_pmu_event_group *group = &RTE_PER_LCORE(_event_group); > >> +
+int ret; > >> + > >> + if (rte_pmu. num_group_events
 
 
 
+int
+__rte_pmu_enable_group(void)
+{
+	struct rte_pmu_event_group *group = &RTE_PER_LCORE(_event_group);
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
Quoted 8 lines

lib/pmu/rte_pmu.c line 297

Quoted 4 lines
lib/pmu/rte_pmu.c new file +4 -0
+
+	rte_spinlock_lock(&rte_pmu.lock);
+	TAILQ_INSERT_TAIL(&rte_pmu.event_group_list, group, next);
+	rte_spinlock_unlock(&rte_pmu.lock);

I thought that after previous round of reviews, we got a consensus that it is a bad idea to insert pointer of TLS variable into the global list:
https://urldefense.proofpoint.com/v2/url?u=https-

<https://urldefense.proofpoint.com/v2/url?u=https- >>

Quoted 1 line

3A__patchwork.dpdk.org_project_dpdk_patch_20230216175502.3164820-2D2-

2Dtduszynski-

Quoted 8 lines
Quoted 6 lines

40marvell.com_&d=DwIFAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYk xIxRndyEUwWU_ad5ce22YI6Is&m=oJ
- eSnmJoK0r1zVFhKrkWMnfelOkxqpjtX2fCrXaG2RdWagOqAQ7vcFCJ0dOWrTt&s=TvGxq QmUz_U3xLOroMxOmsCiaxdqbNLi6GZ
pHIefniw&e=

I don't think there was any consensus. It was rather your point of view solely.

Here is a mail where I highlighted the problem:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__inbox.dpdk.org_dev_6bf789b7ba4e4a8e847431a130372a4b- 40huawei.com_&d=DwIGaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5ce22YI6Is&m=wN1 qnauDKLK8D51b9VKif_iUKl3vHRCvB4El-B965rNZHUr9xDs6GiBAERI6lHSM&s=J0zulN5Omv_77822arGCtQhab6oa74zF- tBWcdU7ZUI&e=

Here is a mail where Morten agreed that it needs to be addressed:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__inbox.dpdk.org_dev_98CBD80474FA8B44BF855DF32C47DC35D87792- 40smartserver.smartshare.dk_&d=DwIGaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5 ce22YI6Is&m=wN1qnauDKLK8D51b9VKif_iUKl3vHRCvB4El- B965rNZHUr9xDs6GiBAERI6lHSM&s=aUb6eYuLukWkofeLkMzDbBiafIeQXlKbxnw1OTauaMU&e=

Here is a mail from David, where he summarizes the remaining work required for these series:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__inbox.dpdk.org_dev_DM4PR18MB43684B889C50F20DDD1B0A29D20CA- 40DM4PR18MB4368.namprd18.prod.outlook.com_&d=DwIGaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxI xRndyEUwWU_ad5ce22YI6Is&m=wN1qnauDKLK8D51b9VKif_iUKl3vHRCvB4El- B965rNZHUr9xDs6GiBAERI6lHSM&s=s8o4dWZJPBqtPTYSYB1jT7ufGGu-ADncO6u0ZRgOAuQ&e=
"...
- Konstantin asked for better explanations in the implementation.
- He also pointed out at using this feature with non EAL lcores.
..."

Here is your reply:
https://urldefense.proofpoint.com/v2/url?u=https-
3A__inbox.dpdk.org_dev_DM4PR18MB43684B889C50F20DDD1B0A29D20CA- 40DM4PR18MB4368.namprd18.prod.outlook.com_&d=DwIGaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxI xRndyEUwWU_ad5ce22YI6Is&m=wN1qnauDKLK8D51b9VKif_iUKl3vHRCvB4El- B965rNZHUr9xDs6GiBAERI6lHSM&s=s8o4dWZJPBqtPTYSYB1jT7ufGGu-ADncO6u0ZRgOAuQ&e=

You didn't object, so I interpreted that (probably wrongly) that we had a consensus here.

Quoted 3 lines

So I still believe
that given currently that only runs on lcores and lcores do not terminate before the main one hence it's safe to access TLS from a main.

Could you clarify what you means by 'lcores' here?
Threads with valid 'lcore_id'?
There is an API within DPDK that allows user to assign vacant lcore_id ((rte_thread_register) to some new thread, run it for some time, then release lcore_id (rte_thread_unregister) and then terminate the thread.

Quoted 6 lines

Another thing - there is no checks within PMU API around does this thread satisfy expected criteria's or not.
So if user will call PMU API from 'wrong' thread - it would succeed and for many cases will keep working as expected. But for other cases (thread lifetime is shorter then program lifetime) it might cause a crash or silent memory corruption.

There are plenty of ways user can shoot shot show own foot and the more checks gets added to APIs, especially the one that reads event, the more cycle hungry it gets. So there needs to be some trade-off.

Currently I'm not aiming at supporting everything, rather worker lcores which are spawned during eal init and say till the end.

As said once basic version gets merged I'll continue work on improving other areas.

Quoted 7 lines
Quoted 1 line

Once support for running on anything other than lcore gets added current mechanics is likely to

change.

Honestly, using TLS variables within the global list here - is just asking for trouble. Please fix it, there are plenty of simple ways to avoid such issue once and for all. Without this problem addressed, my vote for that patch is NACK.

Good then. I'll address it earlier than anticipated then.

Quoted 7 lines
Quoted 6 lines

And as you pointed out, there are plenty of options available.

Quoted 4 lines

As I said before - I believe it is error prone, and needs to be addressed. There are plenty of other ways to have one object per lcore (if that's really necessary):
array of RTE_MAX_LCORE indexed by lcore_id, or new rte_lcore_var.h
API introduced by Mattias recently, or even simple TLS pointer with malloc() behind would be

better.

#159 lore raw

PATCH v14 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 79376ea2e8..e0809a0f93 100644
@@ -13,6 +13,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index 386232e5c7..0d9270eaca 100644
@@ -9,4 +9,12 @@ endif
 
 sources = files('rte_pmu.c')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index 09238ee33d..771fad31d0 100644
@@ -31,6 +31,10 @@ extern "C" {
 #include <rte_branch_prediction.h>
 #include <rte_spinlock.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..39165bbc20
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#160 lore raw

PATCH v14 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index e0809a0f93..2d185574ca 100644
@@ -15,6 +15,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 0d9270eaca..c2e99384e5 100644
@@ -11,6 +11,7 @@ sources = files('rte_pmu.c')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 771fad31d0..248bb55024 100644
@@ -33,6 +33,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..5eb8f73c11
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#161 lore raw

PATCH v14 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               |  4 ++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++++
 lib/eal/common/eal_common_trace.c        |  7 ++-
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/include/rte_eal_trace.h          |  9 ++++
 lib/eal/meson.build                      |  2 +-
 lib/eal/version.map                      |  3 ++
 lib/pmu/rte_pmu.c                        | 61 ++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 18 +++++++
 lib/pmu/version.map                      |  1 +
 11 files changed, 145 insertions(+), 2 deletions(-)
app/test/test_trace_perf.c +4 -0
index 8257cc02be..37af2037b4 100644
@@ -114,6 +114,8 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +124,7 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+WORKER_DEFINE(READ_PMU)
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +177,7 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 854c515a61..1ab6fb9eaa 100644
@@ -36,6 +36,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..378abccd72 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +6 -1
index 918f49bf4f..ffeb822fb7 100644
@@ -12,6 +12,7 @@
 #include <rte_errno.h>
 #include <rte_lcore.h>
 #include <rte_per_lcore.h>
+#include <rte_pmu.h>
 #include <rte_string_fns.h>
 
 #include "eal_trace.h"
@@ -72,8 +73,11 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		if (rte_pmu_init() == 0)
+			rte_pmu_add_events_by_pattern(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +93,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	rte_pmu_fini();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..05f7ed83d5 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_EXEC_ENV_LINUX
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/include/rte_eal_trace.h +9 -0
index 9ad2112801..6c1ed8e8e3 100644
@@ -11,6 +11,7 @@
  * API for EAL trace support
  */
 
+#include <rte_pmu.h>
 #include <rte_trace_point.h>
 
 #ifdef __cplusplus
@@ -127,6 +128,14 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val;
+	val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/meson.build +1 -1
index e1d6c4cf17..2b1a1eb283 100644
@@ -14,7 +14,7 @@ subdir(exec_env)
 
 subdir(arch_subdir)
 
-deps += ['log', 'kvargs']
+deps += ['log', 'kvargs', 'pmu']
 if not is_windows
     deps += ['telemetry']
 endif
lib/eal/version.map +3 -0
index e3ff412683..17c56f6bf7 100644
@@ -396,6 +396,9 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_vfio_get_device_info; # WINDOWS_NO_EXPORT
+
+	# added in 24.11
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +61 -0

67 lines not on this page · raw message

lib/pmu/rte_pmu.h +18 -0
index 248bb55024..26264b3b8f 100644
@@ -175,6 +175,20 @@ __rte_experimental
 int
 rte_pmu_add_event(const char *name);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/pmu/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
@@ -221,6 +235,10 @@ static inline void rte_pmu_fini(void) { }
 __rte_experimental
 static inline int rte_pmu_add_event(const char *name __rte_unused) { return -ENOTSUP; }
 
+__rte_experimental
+static inline int
+rte_pmu_add_events_by_pattern(const char *pattern __rte_unused) { return -ENOTSUP; }
+
 __rte_experimental
 static inline uint64_t rte_pmu_read(unsigned int index __rte_unused) { return UINT64_MAX; }
 
lib/pmu/version.map +1 -0
index d7c80ce4ce..0c98209b4a 100644
@@ -6,6 +6,7 @@ EXPERIMENTAL {
 	per_lcore__event_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#162 lore raw

Re: [PATCH v14 4/4] eal: add PMU support to tracing library

lib/eal/meson.build subdir(exec_env) · line 17

Quoted 10 lines · Tomasz Duszynski
lib/eal/meson.build +1 -1
index e1d6c4cf17..2b1a1eb283 100644
@@ -14,7 +14,7 @@ subdir(exec_env)
 
 subdir(arch_subdir)
 
-deps += ['log', 'kvargs']
+deps += ['log', 'kvargs', 'pmu']

Quick comment as I saw a failure in CI.

An optional library should not be a required dependency for EAL. You need something like (untested):

if dpdk_conf.has('RTE_LIB_PMU')

    deps += ['pmu']

endif

Quoted 3 lines
lib/eal/meson.build +0 -0
 if not is_windows
     deps += ['telemetry']
 endif
Signature · 1 line
David Marchand
#163 lore raw

RE: [EXTERNAL] Re: [PATCH v14 4/4] eal: add PMU support to tracing library

Quoted 29 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Friday, October 11, 2024 3:29 PM
To: Tomasz Duszynski <[email protected]>
Cc: Jerin Jacob <[email protected]>; Sunil Kumar Kori <[email protected]>; Tyler Retzlaff <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v14 4/4] eal: add PMU support to tracing library

On Fri, Oct 11, 2024 at 11: 50 AM Tomasz Duszynski <tduszynski@ marvell. com> wrote: > diff --git a/lib/eal/meson. build b/lib/eal/meson. build > index e1d6c4cf17. . 2b1a1eb283 100644 > --- a/lib/eal/meson. build > +++ b/lib/eal/meson. build

lib/eal/meson.build subdir(exec_env) · line 17

Quoted 10 lines · Tomasz Duszynski
lib/eal/meson.build index renamed from lib/eal/meson.build +0 -0

e1d6c4cf17..2b1a1eb283 100644

lib/eal/meson.build +1 -1
@@ -14,7 +14,7 @@ subdir(exec_env)
 
 subdir(arch_subdir)
 
-deps += ['log', 'kvargs']
+deps += ['log', 'kvargs', 'pmu']

Quick comment as I saw a failure in CI.

Quoted 7 lines

An optional library should not be a required dependency for EAL. You need something like (untested):

if dpdk_conf.has('RTE_LIB_PMU')
deps += ['pmu']
endif

Okay, I'll double check. Thanks.

Quoted 7 lines

lib/eal/meson.build subdir(exec_env) · line 20

Quoted 3 lines
lib/eal/meson.build +0 -0
 if not is_windows
     deps += ['telemetry']
 endif

--
David Marchand

#164 lore raw

v15 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(), rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts rte_pmu_read() performance because more logic needs to be incorporated to handle all corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported
on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  55 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  34 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_24_11.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   5 +-
 lib/eal/common/eal_common_trace_pmu.c    |  38 ++
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/common/eal_trace.h               |   4 +
 lib/eal/common/meson.build               |   1 +
 lib/eal/include/rte_eal_trace.h          |  11 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   1 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 536 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 229 ++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
 lib/pmu/version.map                      |  14 +
 26 files changed, 1196 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#165 lore raw

PATCH v15 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  49 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/eal/meson.build                    |   3 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  13 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 473 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 205 +++++++++++
 lib/pmu/version.map                    |  13 +
 14 files changed, 834 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
MAINTAINERS +5 -0
index cd78bc7db1..077efe41cf 100644
@@ -1816,6 +1816,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index 0f7e11969a..5f1622ecab 100644
@@ -141,6 +141,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +49 -0

49 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index 266c8b90dc..3e6020f367 100644
@@ -240,7 +240,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),
doc/api/doxy-api.conf.in +1 -0
index c94f02d411..f4b382e073 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +29 -0
index a6b5fb4d5e..854c515a61 100644
@@ -7,6 +7,35 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index fa4822d928..d34ecb55e0 100644
@@ -247,6 +247,13 @@ New Features
   Added ability for node to advertise and update multiple xstat counters,
   that can be retrieved using ``rte_graph_cluster_stats_get``.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/eal/meson.build +3 -0
index e1d6c4cf17..1349624653 100644
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if dpdk_conf.has('RTE_LIB_PMU')
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/meson.build +1 -0
index ce92cb5537..968ad29e8d 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +13 -0
new file mode 100644
index 0000000000..f173b6f55c
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+headers = files('rte_pmu.h')
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +473 -0

473 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +205 -0

205 lines not on this page · raw message

lib/pmu/version.map new file +13 -0
new file mode 100644
index 0000000000..d0f907d13d
@@ -0,0 +1,13 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
#166 lore raw

RE: [PATCH v15 1/4] lib: add generic support for reading PMU events

Quoted 8 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

LGTM in general, just few questions, nits - majority about docs/comments.

Quoted 767 lines
---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  49 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/eal/meson.build                    |   3 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  13 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 473 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 205 +++++++++++
 lib/pmu/version.map                    |  13 +
 14 files changed, 834 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
MAINTAINERS +5 -0
index cd78bc7db1..077efe41cf 100644
@@ -1816,6 +1816,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index 0f7e11969a..5f1622ecab 100644
@@ -141,6 +141,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +49 -0
new file mode 100644
index 0000000000..464e5068ec
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_pmu.h>
+
+#include "test.h"
+
+static int
+test_pmu_read(void)
+{
+	const char *name = NULL;
+	int tries = 10, event;
+	uint64_t val = 0;
+
+	if (name == NULL) {
+		printf("PMU not supported on this arch\n");
+		return TEST_SKIPPED;
+	}
+
+	if (rte_pmu_init() < 0)
+		return TEST_FAILED;
+
+	event = rte_pmu_add_event(name);
+	while (tries--)
+		val += rte_pmu_read(event);
+
+	rte_pmu_fini();
+
+	return val ? TEST_SUCCESS : TEST_FAILED;
+}
+
+static struct unit_test_suite pmu_tests = {
+	.suite_name = "pmu autotest",
+	.setup = NULL,
+	.teardown = NULL,
+	.unit_test_cases = {
+		TEST_CASE(test_pmu_read),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_pmu(void)
+{
+	return unit_test_suite_runner(&pmu_tests);
+}
+
+REGISTER_FAST_TEST(pmu_autotest, true, true, test_pmu);
doc/api/doxy-api-index.md +2 -1
index 266c8b90dc..3e6020f367 100644
@@ -240,7 +240,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),
doc/api/doxy-api.conf.in +1 -0
index c94f02d411..f4b382e073 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +29 -0
index a6b5fb4d5e..854c515a61 100644
@@ -7,6 +7,35 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index fa4822d928..d34ecb55e0 100644
@@ -247,6 +247,13 @@ New Features
   Added ability for node to advertise and update multiple xstat counters,
   that can be retrieved using ``rte_graph_cluster_stats_get``.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/eal/meson.build +3 -0
index e1d6c4cf17..1349624653 100644
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if dpdk_conf.has('RTE_LIB_PMU')
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/meson.build +1 -0
index ce92cb5537..968ad29e8d 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +13 -0
new file mode 100644
index 0000000000..f173b6f55c
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+headers = files('rte_pmu.h')
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +473 -0
new file mode 100644
index 0000000000..dd57961627
@@ -0,0 +1,473 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <regex.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/queue.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <rte_atomic.h>
+#include <rte_bitops.h>
+#include <rte_lcore.h>
+#include <rte_log.h>
+#include <rte_per_lcore.h>
+#include <rte_pmu.h>
+#include <rte_tailq.h>
+
+#include "pmu_private.h"
+
+#define EVENT_SOURCE_DEVICES_PATH "/sys/bus/event_source/devices"
+
+#define FIELD_PREP(m, v) (((uint64_t)(v) << (__builtin_ffsll(m) - 1)) & (m))
+
+RTE_LOG_REGISTER_DEFAULT(rte_pmu_logtype, INFO)
+#define RTE_LOGTYPE_PMU rte_pmu_logtype
+
+#define PMU_LOG(level, ...) \
+	RTE_LOG_LINE(level, PMU, ## __VA_ARGS__)
+
+/* A structure describing an event */
+struct rte_pmu_event {
+	char *name;
+	unsigned int index;
+	TAILQ_ENTRY(rte_pmu_event) next;
+};
+
+struct rte_pmu rte_pmu;
+
+/*
+ * Following __rte_weak functions provide default no-op. Architectures should override them if
+ * necessary.
+ */
+
+int
+__rte_weak pmu_arch_init(void)
+{
+	return 0;
+}
+
+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 path[PATH_MAX];
+	char *config = NULL;
+	int high, low, ret;
+	FILE *fp;
+
+	*num = *mask = 0;
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/format/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	errno = 0;
+	ret = fscanf(fp, "%m[^:]:%d-%d", &config, &low, &high);
+	if (ret < 2) {
+		ret = -ENODATA;
+		goto out;
+	}
+	if (errno) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ret == 2)
+		high = low;
+
+	*mask = RTE_GENMASK64(high, low);
+	/* Last digit should be [012]. If last digit is missing 0 is implied. */
+	*num = config[strlen(config) - 1];
+	*num = isdigit(*num) ? *num - '0' : 0;
+
+	ret = 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] = config[1] = config[2] = 0;
+
+	token = strtok(buf, ",");
+	while (token) {
+		errno = 0;
+		/* <term>=<value> */
+		ret = sscanf(token, "%m[^=]=%i", &term, &val);
+		if (ret < 1)
+			return -ENODATA;
+		if (errno)
+			return -errno;
+		if (ret == 1)
+			val = 1;
+
+		ret = get_term_format(term, &num, &mask);
+		free(term);
+		if (ret)
+			return ret;
+
+		config[num] |= FIELD_PREP(mask, val);
+		token = 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/events/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	ret = fread(buf, 1, sizeof(buf), fp);
+	if (ret == 0) {
+		fclose(fp);
+
+		return -EINVAL;
+	}
+	fclose(fp);
+	buf[ret] = '\0';
+
+	return parse_event(buf, config);
+}
+
+static int
+do_perf_event_open(uint64_t config[3], int group_fd)
+{
+	struct perf_event_attr attr = {
+		.size = sizeof(struct perf_event_attr),
+		.type = PERF_TYPE_RAW,
+		.exclude_kernel = 1,
+		.exclude_hv = 1,
+		.disabled = 1,
+		.pinned = group_fd == -1,
+	};
+
+	pmu_arch_fixup_config(config);
+
+	attr.config = config[0];
+	attr.config1 = config[1];
+	attr.config2 = 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 = 0, ret;
+
+	/* group leader gets created first, with fd = -1 */
+	group->fds[0] = -1;
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		ret = get_event_config(event->name, config);
+		if (ret)
+			continue;
+
+		ret = do_perf_event_open(config, group->fds[0]);
+		if (ret == -1) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->fds[event->index] = ret;
+		num++;
+	}
+
+	return 0;
+out:
+	for (--num; num >= 0; num--) {
+		close(group->fds[num]);
+		group->fds[num] = -1;
+	}
+
+	return ret;
+}
+
+static int
+mmap_events(struct rte_pmu_event_group *group)
+{
+	long page_size = sysconf(_SC_PAGE_SIZE);
+	unsigned int i;
+	void *addr;
+	int ret;
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		addr = mmap(0, page_size, PROT_READ, MAP_SHARED, group->fds[i], 0);
+		if (addr == MAP_FAILED) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->mmap_pages[i] = addr;
+	}
+
+	return 0;
+out:
+	for (; i; i--) {
+		munmap(group->mmap_pages[i - 1], page_size);
+		group->mmap_pages[i - 1] = NULL;
+	}
+
+	return ret;
+}
+
+static void
+cleanup_events(struct rte_pmu_event_group *group)
+{
+	unsigned int i;
+
+	if (group->fds[0] != -1)
+		ioctl(group->fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		if (group->mmap_pages[i]) {
+			munmap(group->mmap_pages[i], sysconf(_SC_PAGE_SIZE));
+			group->mmap_pages[i] = NULL;
+		}
+
+		if (group->fds[i] != -1) {
+			close(group->fds[i]);
+			group->fds[i] = -1;
+		}
+	}
+
+	group->enabled = false;
+}
+
+int
+__rte_pmu_enable_group(struct rte_pmu_event_group *group)
+{
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
+
+	group->enabled = 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 = opendir(EVENT_SOURCE_DEVICES_PATH);
+	if (dirp == NULL)
+		return -errno;
+
+	while ((dent = readdir(dirp))) {
+		name = dent->d_name;
+		if (name[0] == '.')
+			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) == 0)
+			break;
+	}
+
+	if (dent) {
+		rte_pmu.name = strdup(name);
+		if (rte_pmu.name == 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 = calloc(1, sizeof(*event));
+	if (event == NULL)
+		goto out;
+
+	event->name = strdup(name);
+	if (event->name == NULL) {
+		free(event);
+		event = 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.initialized) {
+		PMU_LOG(ERR, "PMU is not initialized");
+		return -ENODEV;
+	}
+
+	if (rte_pmu.num_group_events + 1 >= RTE_MAX_NUM_GROUP_EVENTS) {
+		PMU_LOG(ERR, "Excessive number of events in a group (%d > %d)",
+			rte_pmu.num_group_events, RTE_MAX_NUM_GROUP_EVENTS);
+		return -ENOSPC;
+	}
+
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/events/%s", rte_pmu.name, name);
+	if (access(path, R_OK)) {
+		PMU_LOG(ERR, "Cannot access %s", path);
+		return -ENODEV;
+	}
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		if (strcmp(event->name, name))
+			continue;
+
+		return event->index;
+	}
+
+	event = new_event(name);
+	if (event == NULL) {
+		PMU_LOG(ERR, "Failed to create event %s", name);
+		return -ENOMEM;
+	}
+
+	event->index = rte_pmu.num_group_events++;
+	TAILQ_INSERT_TAIL(&rte_pmu.event_list, event, next);
+
+	return event->index;
+}
+
+int
+rte_pmu_init(void)
+{
+	int ret;
+
+	if (rte_pmu.initialized)
+		return 0;
+
+	ret = scan_pmus();
+	if (ret) {
+		PMU_LOG(ERR, "Failed to scan for event sources");
+		goto out;
+	}
+
+	ret = pmu_arch_init();
+	if (ret) {
+		PMU_LOG(ERR, "Failed to setup arch internals");
+		goto out;
+	}
+
+	TAILQ_INIT(&rte_pmu.event_list);
+	rte_pmu.initialized = 1;
+out:
+	if (ret) {
+		free(rte_pmu.name);
+		rte_pmu.name = NULL;
+	}
+
+	return ret;
+}
+
+void
+rte_pmu_fini(void)
+{
+	struct rte_pmu_event *event, *tmp_event;
+	struct rte_pmu_event_group *group;
+	unsigned int i;
+
+	if (!rte_pmu.initialized)
+		return;
+
+	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event) {
+		TAILQ_REMOVE(&rte_pmu.event_list, event, next);
+		free_event(event);
+	}
+
+	for (i = 0; i < RTE_DIM(rte_pmu.event_groups); i++) {
+		group = &rte_pmu.event_groups[i];
+		if (!group->enabled)
+			continue;
+
+		cleanup_events(group);
+	}
+
+	pmu_arch_fini();
+	free(rte_pmu.name);
+	rte_pmu.name = NULL;
+	rte_pmu.num_group_events = 0;
+}
lib/pmu/rte_pmu.h new file +8 -0
new file mode 100644
index 0000000000..7d10a0dc56
@@ -0,0 +1,205 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell
+ */
+
+#ifndef _RTE_PMU_H_
+#define _RTE_PMU_H_
+
+/**

lib/pmu/rte_pmu.h line 18

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+ * @file
+ *
+ * PMU event tracing operations
+ *
+ * This file defines generic API and types necessary to setup PMU and
+ * read selected counters in runtime. Exported APIs are generally not MT-safe.
+ * One exception is rte_pmu_read() which can be called concurrently once
+ * everything has been setup.
+ */
+

From reading the code - I can see ithat PMU API is not MT safe.
The only function that can run in parallel is rte_pmu_read(), correct? All other combinations: let say pmu_read() and add_event() are not possible, right?
If so, then it is probably worth to articulate that explicitly in the public API comments, after all extra comment doesn't hurt.

Quoted 150 lines
lib/pmu/rte_pmu.h new file +150 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <errno.h>
+#include <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_common.h>
+#include <rte_compat.h>
+#include <rte_lcore.h>
+
+/** Maximum number of events in a group */
+#define RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+	/** array of user pages */
+	struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+	int fds[RTE_MAX_NUM_GROUP_EVENTS]; /**< array of event descriptors */
+	TAILQ_ENTRY(rte_pmu_event_group) next; /**< list entry */
+	bool enabled; /**< true if group was enabled on particular lcore */
+};
+
+/**
+ * A PMU state container.
+ */
+struct rte_pmu {
+	struct rte_pmu_event_group event_groups[RTE_MAX_LCORE]; /**< event groups */
+	unsigned int num_group_events; /**< number of events in a group */
+	unsigned int initialized; /**< initialization counter */
+	char *name; /**< name of core PMU listed under /sys/bus/event_source/devices */
+	TAILQ_HEAD(, rte_pmu_event) event_list; /**< list of matching events */
+};
+
+/** 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) ({ (void)(index); 0; })
+#endif
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Read PMU counter.
+ *
+ * @warning This should not be called directly.
+ *
+ * @param pc
+ *   Pointer to the mmapped user page.
+ * @return
+ *   Counter value read from hardware.
+ */
+__rte_experimental
+static __rte_always_inline uint64_t
+__rte_pmu_read_userpage(struct perf_event_mmap_page *pc)
+{
+#define __RTE_PMU_READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
+	uint64_t width, offset;
+	uint32_t seq, index;
+	int64_t pmc;
+
+	for (;;) {
+		seq = __RTE_PMU_READ_ONCE(pc->lock);
+		rte_compiler_barrier();
+		index = __RTE_PMU_READ_ONCE(pc->index);
+		offset = __RTE_PMU_READ_ONCE(pc->offset);
+		width = __RTE_PMU_READ_ONCE(pc->pmc_width);
+
+		/* index set to 0 means that particular counter cannot be used */
+		if (likely(pc->cap_user_rdpmc && index)) {
+			pmc = rte_pmu_pmc_read(index - 1);
+			pmc <<= 64 - width;
+			pmc >>= 64 - width;
+			offset += pmc;
+		}
+
+		rte_compiler_barrier();
+
+		if (likely(__RTE_PMU_READ_ONCE(pc->lock) == seq))
+			return offset;
+	}
+
+	return 0;
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable group of events on the calling lcore.
+ *
+ * @warning This should not be called directly.
+ *
+ * @param group
+ *   Pointer to the group which will be enabled.
+ * @return
+ *   0 in case of success, negative value otherwise.
+ */
+__rte_experimental
+int
+__rte_pmu_enable_group(struct rte_pmu_event_group *group);
+
+/**
+ * @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.
+ */
+__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>/events.
+ * @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

lib/pmu/rte_pmu.h line 178

Quoted 10 lines
lib/pmu/rte_pmu.h new file +10 -0
+ *
+ * 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 support
+ *   0 is returned. In other words, stream of zeros in a trace file
+ *   indicates problem with reading particular PMU event register.
+ */

As I remember that function implies to be called from the thread bind to one particular cpu.
If that is still the case - please state it explicitly in the comments above.

lib/pmu/rte_pmu.h line 183

Quoted 5 lines
lib/pmu/rte_pmu.h new file +5 -0
+__rte_experimental
+static __rte_always_inline uint64_t
+rte_pmu_read(unsigned int index)
+{
+	struct rte_pmu_event_group *group = &rte_pmu.event_groups[rte_lcore_id()];

I think we better check here value returned by rte_lcore_id() before using it as an index.

Quoted 43 lines
lib/pmu/rte_pmu.h new file +22 -0
+	int ret;
+
+	if (unlikely(!rte_pmu.initialized))
+		return 0;
+
+	if (unlikely(!group->enabled)) {
+		ret = __rte_pmu_enable_group(group);
+		if (ret)
+			return 0;
+	}
+
+	if (unlikely(index >= rte_pmu.num_group_events))
+		return 0;
+
+	return __rte_pmu_read_userpage(group->mmap_pages[index]);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMU_H_ */
lib/pmu/version.map new file +13 -1
new file mode 100644
index 0000000000..d0f907d13d
@@ -0,0 +1,13 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
--

2.34.1

#167 lore raw

RE: [PATCH v15 1/4] lib: add generic support for reading PMU events

Quoted 787 lines

-----Original Message-----
From: Konstantin Ananyev <[email protected]>
Sent: Tuesday, November 5, 2024 11:58 AM
To: Tomasz Duszynski <[email protected]>; Thomas Monjalon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXTERNAL] RE: [PATCH v15 1/4] lib: add generic support for reading PMU events

Quoted 7 lines

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 > i. e run dedicated tasks. In such cases one cannot

Quoted 6 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

LGTM in general, just few questions, nits - majority about docs/comments.

Quoted 138 lines
---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  49 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  29 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/eal/meson.build                    |   3 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  13 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 473 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 205 +++++++++++
 lib/pmu/version.map                    |  13 +
 14 files changed, 834 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

MAINTAINERS index cd78bc7db1..077efe41cf renamed from MAINTAINERS +0 -0

100644

MAINTAINERS +5 -0
@@ -1816,6 +1816,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build index renamed from app/test/meson.build +0 -0

0f7e11969a..5f1622ecab 100644

app/test/meson.build +1 -0
@@ -141,6 +141,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'], diff --git

a/app/test/test_pmu.c b/app/test/test_pmu.c new file mode 100644 index 0000000000..464e5068ec

app/test/test_pmu.c new file +48 -0
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_pmu.h>
+
+#include "test.h"
+
+static int
+test_pmu_read(void)
+{
+	const char *name = NULL;
+	int tries = 10, event;
+	uint64_t val = 0;
+
+	if (name == NULL) {
+		printf("PMU not supported on this arch\n");
+		return TEST_SKIPPED;
+	}
+
+	if (rte_pmu_init() < 0)
+		return TEST_FAILED;
+
+	event = rte_pmu_add_event(name);
+	while (tries--)
+		val += rte_pmu_read(event);
+
+	rte_pmu_fini();
+
+	return val ? TEST_SUCCESS : TEST_FAILED; }
+
+static struct unit_test_suite pmu_tests = {
+	.suite_name = "pmu autotest",
+	.setup = NULL,
+	.teardown = NULL,
+	.unit_test_cases = {
+		TEST_CASE(test_pmu_read),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_pmu(void)
+{
+	return unit_test_suite_runner(&pmu_tests);
+}
+
+REGISTER_FAST_TEST(pmu_autotest, true, true, test_pmu);
doc/api/doxy-api-index.md +1 -1
index 266c8b90dc..3e6020f367 100644
@@ -240,7 +240,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),
doc/api/doxy-api.conf.in index renamed from doc/api/doxy-api.conf.in +0 -0

c94f02d411..f4b382e073 100644

doc/api/doxy-api.conf.in +1 -0
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \ diff --git

a/doc/guides/prog_guide/profile_app.rst
b/doc/guides/prog_guide/profile_app.rst
index a6b5fb4d5e..854c515a61 100644

doc/guides/prog_guide/profile_app.rst +0 -0
@@ -7,6 +7,35 @@ Profile Your Application  The following sections

describe methods of profiling DPDK applications on different architectures.

doc/guides/prog_guide/profile_app.rst +1 -0
+Performance counter based profiling

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 10 lines
doc/guides/prog_guide/profile_app.rst +10 -0
+-----------------------------------
+
+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 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()``.

doc/guides/prog_guide/profile_app.rst

Quoted 6 lines
doc/guides/prog_guide/profile_app.rst +6 -0
+
+By default, userspace applications are not allowed to access PMU
+internals. That can be changed by setting
+``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default
+value anyway) and adding ``CAP_PERFMON`` capability to DPDK
+application. Please refer to ``Documentation/admin-guide/perf-security.rst`` under Linux sources

for more information. Fairly recent kernel, i.e >= 5.9, is advised too.

Quoted 22 lines
doc/guides/prog_guide/profile_app.rst +13 -0
+
+As of now implementation imposes certain limitations:
+
+* Management APIs that normally return a non-negative value will
+return error (``-ENOTSUP``) while
+  ``rte_pmu_read()`` will return ``UINT64_MAX`` if running under unsupported operating system.
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +0 -0
index fa4822d928..d34ecb55e0 100644

doc/guides/rel_notes/release_24_11.rst New Features · line 253

Quoted 7 lines
doc/guides/rel_notes/release_24_11.rst +4 -0
@@ -247,6 +247,13 @@ New Features
   Added ability for node to advertise and update multiple xstat counters,
   that can be retrieved using ``rte_graph_cluster_stats_get``.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows
+ applications  to perform self monitoring activities without depending on external utilities

like perf.

Quoted 575 lines
doc/guides/rel_notes/release_24_11.rst +3 -0
+  After integration with :doc:`../prog_guide/trace_lib` data gathered
+ from hardware counters  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/eal/meson.build index renamed from lib/eal/meson.build +0 -0

e1d6c4cf17..1349624653 100644

lib/eal/meson.build +3 -0
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']  if not is_windows
     deps += ['telemetry']
 endif
+if dpdk_conf.has('RTE_LIB_PMU')
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/meson.build index renamed from lib/meson.build +0 -0

ce92cb5537..968ad29e8d 100644

lib/meson.build +1 -0
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file mode renamed from lib/pmu/meson.build +0 -0

100644 index 0000000000..f173b6f55c

lib/pmu/meson.build new file +13 -0
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(C) 2024 Marvell
+International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+headers = files('rte_pmu.h')
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file renamed from lib/pmu/pmu_private.h +0 -0

mode 100644 index 0000000000..d2b15615bf

lib/pmu/pmu_private.h new file +32 -0
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file mode renamed from lib/pmu/rte_pmu.c +0 -0

100644 index 0000000000..dd57961627

lib/pmu/rte_pmu.c new file +462 -0
@@ -0,0 +1,473 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <regex.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/queue.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <rte_atomic.h>
+#include <rte_bitops.h>
+#include <rte_lcore.h>
+#include <rte_log.h>
+#include <rte_per_lcore.h>
+#include <rte_pmu.h>
+#include <rte_tailq.h>
+
+#include "pmu_private.h"
+
+#define EVENT_SOURCE_DEVICES_PATH "/sys/bus/event_source/devices"
+
+#define FIELD_PREP(m, v) (((uint64_t)(v) << (__builtin_ffsll(m) - 1))
+& (m))
+
+RTE_LOG_REGISTER_DEFAULT(rte_pmu_logtype, INFO) #define
+RTE_LOGTYPE_PMU rte_pmu_logtype
+
+#define PMU_LOG(level, ...) \
+	RTE_LOG_LINE(level, PMU, ## __VA_ARGS__)
+
+/* A structure describing an event */ struct rte_pmu_event {
+	char *name;
+	unsigned int index;
+	TAILQ_ENTRY(rte_pmu_event) next;
+};
+
+struct rte_pmu rte_pmu;
+
+/*
+ * Following __rte_weak functions provide default no-op.
+Architectures should override them if
+ * necessary.
+ */
+
+int
+__rte_weak pmu_arch_init(void)
+{
+	return 0;
+}
+
+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 path[PATH_MAX];
+	char *config = NULL;
+	int high, low, ret;
+	FILE *fp;
+
+	*num = *mask = 0;
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/format/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	errno = 0;
+	ret = fscanf(fp, "%m[^:]:%d-%d", &config, &low, &high);
+	if (ret < 2) {
+		ret = -ENODATA;
+		goto out;
+	}
+	if (errno) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ret == 2)
+		high = low;
+
+	*mask = RTE_GENMASK64(high, low);
+	/* Last digit should be [012]. If last digit is missing 0 is implied. */
+	*num = config[strlen(config) - 1];
+	*num = isdigit(*num) ? *num - '0' : 0;
+
+	ret = 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] = config[1] = config[2] = 0;
+
+	token = strtok(buf, ",");
+	while (token) {
+		errno = 0;
+		/* <term>=<value> */
+		ret = sscanf(token, "%m[^=]=%i", &term, &val);
+		if (ret < 1)
+			return -ENODATA;
+		if (errno)
+			return -errno;
+		if (ret == 1)
+			val = 1;
+
+		ret = get_term_format(term, &num, &mask);
+		free(term);
+		if (ret)
+			return ret;
+
+		config[num] |= FIELD_PREP(mask, val);
+		token = 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/events/%s", rte_pmu.name, name);
+	fp = fopen(path, "r");
+	if (fp == NULL)
+		return -errno;
+
+	ret = fread(buf, 1, sizeof(buf), fp);
+	if (ret == 0) {
+		fclose(fp);
+
+		return -EINVAL;
+	}
+	fclose(fp);
+	buf[ret] = '\0';
+
+	return parse_event(buf, config);
+}
+
+static int
+do_perf_event_open(uint64_t config[3], int group_fd) {
+	struct perf_event_attr attr = {
+		.size = sizeof(struct perf_event_attr),
+		.type = PERF_TYPE_RAW,
+		.exclude_kernel = 1,
+		.exclude_hv = 1,
+		.disabled = 1,
+		.pinned = group_fd == -1,
+	};
+
+	pmu_arch_fixup_config(config);
+
+	attr.config = config[0];
+	attr.config1 = config[1];
+	attr.config2 = 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 = 0, ret;
+
+	/* group leader gets created first, with fd = -1 */
+	group->fds[0] = -1;
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		ret = get_event_config(event->name, config);
+		if (ret)
+			continue;
+
+		ret = do_perf_event_open(config, group->fds[0]);
+		if (ret == -1) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->fds[event->index] = ret;
+		num++;
+	}
+
+	return 0;
+out:
+	for (--num; num >= 0; num--) {
+		close(group->fds[num]);
+		group->fds[num] = -1;
+	}
+
+	return ret;
+}
+
+static int
+mmap_events(struct rte_pmu_event_group *group) {
+	long page_size = sysconf(_SC_PAGE_SIZE);
+	unsigned int i;
+	void *addr;
+	int ret;
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		addr = mmap(0, page_size, PROT_READ, MAP_SHARED, group->fds[i], 0);
+		if (addr == MAP_FAILED) {
+			ret = -errno;
+			goto out;
+		}
+
+		group->mmap_pages[i] = addr;
+	}
+
+	return 0;
+out:
+	for (; i; i--) {
+		munmap(group->mmap_pages[i - 1], page_size);
+		group->mmap_pages[i - 1] = NULL;
+	}
+
+	return ret;
+}
+
+static void
+cleanup_events(struct rte_pmu_event_group *group) {
+	unsigned int i;
+
+	if (group->fds[0] != -1)
+		ioctl(group->fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
+
+	for (i = 0; i < rte_pmu.num_group_events; i++) {
+		if (group->mmap_pages[i]) {
+			munmap(group->mmap_pages[i], sysconf(_SC_PAGE_SIZE));
+			group->mmap_pages[i] = NULL;
+		}
+
+		if (group->fds[i] != -1) {
+			close(group->fds[i]);
+			group->fds[i] = -1;
+		}
+	}
+
+	group->enabled = false;
+}
+
+int
+__rte_pmu_enable_group(struct rte_pmu_event_group *group) {
+	int ret;
+
+	if (rte_pmu.num_group_events == 0)
+		return -ENODEV;
+
+	ret = open_events(group);
+	if (ret)
+		goto out;
+
+	ret = mmap_events(group);
+	if (ret)
+		goto out;
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (ioctl(group->fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
+		ret = -errno;
+		goto out;
+	}
+
+	group->enabled = 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 = opendir(EVENT_SOURCE_DEVICES_PATH);
+	if (dirp == NULL)
+		return -errno;
+
+	while ((dent = readdir(dirp))) {
+		name = dent->d_name;
+		if (name[0] == '.')
+			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) == 0)
+			break;
+	}
+
+	if (dent) {
+		rte_pmu.name = strdup(name);
+		if (rte_pmu.name == 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 = calloc(1, sizeof(*event));
+	if (event == NULL)
+		goto out;
+
+	event->name = strdup(name);
+	if (event->name == NULL) {
+		free(event);
+		event = 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.initialized) {
+		PMU_LOG(ERR, "PMU is not initialized");
+		return -ENODEV;
+	}
+
+	if (rte_pmu.num_group_events + 1 >= RTE_MAX_NUM_GROUP_EVENTS) {
+		PMU_LOG(ERR, "Excessive number of events in a group (%d > %d)",
+			rte_pmu.num_group_events, RTE_MAX_NUM_GROUP_EVENTS);
+		return -ENOSPC;
+	}
+
+	snprintf(path, sizeof(path), EVENT_SOURCE_DEVICES_PATH "/%s/events/%s", rte_pmu.name, name);
+	if (access(path, R_OK)) {
+		PMU_LOG(ERR, "Cannot access %s", path);
+		return -ENODEV;
+	}
+
+	TAILQ_FOREACH(event, &rte_pmu.event_list, next) {
+		if (strcmp(event->name, name))
+			continue;
+
+		return event->index;
+	}
+
+	event = new_event(name);
+	if (event == NULL) {
+		PMU_LOG(ERR, "Failed to create event %s", name);
+		return -ENOMEM;
+	}
+
+	event->index = rte_pmu.num_group_events++;
+	TAILQ_INSERT_TAIL(&rte_pmu.event_list, event, next);
+
+	return event->index;
+}
+
+int
+rte_pmu_init(void)
+{
+	int ret;
+
+	if (rte_pmu.initialized)
+		return 0;
+
+	ret = scan_pmus();
+	if (ret) {
+		PMU_LOG(ERR, "Failed to scan for event sources");
+		goto out;
+	}
+
+	ret = pmu_arch_init();
+	if (ret) {
+		PMU_LOG(ERR, "Failed to setup arch internals");
+		goto out;
+	}
+
+	TAILQ_INIT(&rte_pmu.event_list);
+	rte_pmu.initialized = 1;
+out:
+	if (ret) {
+		free(rte_pmu.name);
+		rte_pmu.name = NULL;
+	}
+
+	return ret;
+}
+
+void
+rte_pmu_fini(void)
+{
+	struct rte_pmu_event *event, *tmp_event;
+	struct rte_pmu_event_group *group;
+	unsigned int i;
+
+	if (!rte_pmu.initialized)
+		return;
+
+	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event) {
+		TAILQ_REMOVE(&rte_pmu.event_list, event, next);
+		free_event(event);
+	}
+
+	for (i = 0; i < RTE_DIM(rte_pmu.event_groups); i++) {
+		group = &rte_pmu.event_groups[i];
+		if (!group->enabled)
+			continue;
+
+		cleanup_events(group);
+	}
+
+	pmu_arch_fini();
+	free(rte_pmu.name);
+	rte_pmu.name = NULL;
+	rte_pmu.num_group_events = 0;
+}
lib/pmu/rte_pmu.h new file mode renamed from lib/pmu/rte_pmu.h +0 -0

100644 index 0000000000..7d10a0dc56

lib/pmu/rte_pmu.h new file +17 -0
@@ -0,0 +1,205 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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. Exported APIs are generally not MT-safe.
+ * One exception is rte_pmu_read() which can be called concurrently
+once
+ * everything has been setup.
Quoted 9 lines

lib/pmu/rte_pmu.h line 19

Quoted 2 lines
lib/pmu/rte_pmu.h new file +2 -0
+ */
+

From reading the code - I can see ithat PMU API is not MT safe.
The only function that can run in parallel is rte_pmu_read(), correct?
All other combinations: let say pmu_read() and add_event() are not possible, right? If so, then it is probably worth to articulate that explicitly in the public API comments, after all extra comment doesn't hurt.

Yes, when it comes to this version pmu_read() is the only function that can be called concurrently on different lcores. Okay, I'll try to rewrite existing comment to make that more explicit.

Quoted 153 lines
Quoted 153 lines
lib/pmu/rte_pmu.h new file +153 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <errno.h>
+#include <linux/perf_event.h>
+
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_common.h>
+#include <rte_compat.h>
+#include <rte_lcore.h>
+
+/** Maximum number of events in a group */ #define
+RTE_MAX_NUM_GROUP_EVENTS 8
+
+/**
+ * A structure describing a group of events.
+ */
+struct __rte_cache_aligned rte_pmu_event_group {
+	/** array of user pages */
+	struct perf_event_mmap_page *mmap_pages[RTE_MAX_NUM_GROUP_EVENTS];
+	int fds[RTE_MAX_NUM_GROUP_EVENTS]; /**< array of event descriptors */
+	TAILQ_ENTRY(rte_pmu_event_group) next; /**< list entry */
+	bool enabled; /**< true if group was enabled on particular lcore */
+};
+
+/**
+ * A PMU state container.
+ */
+struct rte_pmu {
+	struct rte_pmu_event_group event_groups[RTE_MAX_LCORE]; /**< event groups */
+	unsigned int num_group_events; /**< number of events in a group */
+	unsigned int initialized; /**< initialization counter */
+	char *name; /**< name of core PMU listed under /sys/bus/event_source/devices */
+	TAILQ_HEAD(, rte_pmu_event) event_list; /**< list of matching events
+*/ };
+
+/** 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) ({
+(void)(index); 0; }) #endif
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Read PMU counter.
+ *
+ * @warning This should not be called directly.
+ *
+ * @param pc
+ *   Pointer to the mmapped user page.
+ * @return
+ *   Counter value read from hardware.
+ */
+__rte_experimental
+static __rte_always_inline uint64_t
+__rte_pmu_read_userpage(struct perf_event_mmap_page *pc) { #define
+__RTE_PMU_READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
+	uint64_t width, offset;
+	uint32_t seq, index;
+	int64_t pmc;
+
+	for (;;) {
+		seq = __RTE_PMU_READ_ONCE(pc->lock);
+		rte_compiler_barrier();
+		index = __RTE_PMU_READ_ONCE(pc->index);
+		offset = __RTE_PMU_READ_ONCE(pc->offset);
+		width = __RTE_PMU_READ_ONCE(pc->pmc_width);
+
+		/* index set to 0 means that particular counter cannot be used */
+		if (likely(pc->cap_user_rdpmc && index)) {
+			pmc = rte_pmu_pmc_read(index - 1);
+			pmc <<= 64 - width;
+			pmc >>= 64 - width;
+			offset += pmc;
+		}
+
+		rte_compiler_barrier();
+
+		if (likely(__RTE_PMU_READ_ONCE(pc->lock) == seq))
+			return offset;
+	}
+
+	return 0;
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable group of events on the calling lcore.
+ *
+ * @warning This should not be called directly.
+ *
+ * @param group
+ *   Pointer to the group which will be enabled.
+ * @return
+ *   0 in case of success, negative value otherwise.
+ */
+__rte_experimental
+int
+__rte_pmu_enable_group(struct rte_pmu_event_group *group);
+
+/**
+ * @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.
+ */
+__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>/events.
+ * @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.
Quoted 9 lines

lib/pmu/rte_pmu.h line 178

Quoted 5 lines
lib/pmu/rte_pmu.h new file +5 -0
+ * @return
+ *   Event value read from register. In case of errors or lack of support
+ *   0 is returned. In other words, stream of zeros in a trace file
+ *   indicates problem with reading particular PMU event register.
+ */

As I remember that function implies to be called from the thread bind to one particular cpu. If that is still the case - please state it explicitly in the comments above.

Yes, that still the case. Will mention that.

Quoted 9 lines

lib/pmu/rte_pmu.h

Quoted 6 lines
lib/pmu/rte_pmu.h new file +6 -0
+__rte_experimental
+static __rte_always_inline uint64_t
+rte_pmu_read(unsigned int index)
+{
+	struct rte_pmu_event_group *group =
+&rte_pmu.event_groups[rte_lcore_id()];

I think we better check here value returned by rte_lcore_id() before using it as an index.

I'll add this check. Hopefully it will disappear once this gets switched to per-lcore-variables (given performance won't deteriorate).

Quoted 42 lines
Quoted 42 lines
lib/pmu/rte_pmu.h new file +22 -0
+	int ret;
+
+	if (unlikely(!rte_pmu.initialized))
+		return 0;
+
+	if (unlikely(!group->enabled)) {
+		ret = __rte_pmu_enable_group(group);
+		if (ret)
+			return 0;
+	}
+
+	if (unlikely(index >= rte_pmu.num_group_events))
+		return 0;
+
+	return __rte_pmu_read_userpage(group->mmap_pages[index]);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMU_H_ */
lib/pmu/version.map new file mode renamed from lib/pmu/version.map +0 -0

100644 index 0000000000..d0f907d13d

lib/pmu/version.map new file +13 -1
@@ -0,0 +1,13 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
--

2.34.1

#168 lore raw

PATCH v15 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 464e5068ec..0dc33eeccc 100644
@@ -13,6 +13,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index f173b6f55c..94619c63c2 100644
@@ -10,4 +10,12 @@ endif
 headers = files('rte_pmu.h')
 sources = files('rte_pmu.c')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index 7d10a0dc56..5c8e8b9705 100644
@@ -29,6 +29,10 @@ extern "C" {
 #include <rte_compat.h>
 #include <rte_lcore.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..39165bbc20
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#169 lore raw

PATCH v15 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index 0dc33eeccc..c6e798f52d 100644
@@ -15,6 +15,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 94619c63c2..5230ca3e69 100644
@@ -12,6 +12,7 @@ sources = files('rte_pmu.c')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 5c8e8b9705..85f9127911 100644
@@ -31,6 +31,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..5eb8f73c11
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#170 lore raw

PATCH v15 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
app/test/test_trace_perf.c +10 -0
index 8257cc02be..8a0730943e 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_LIB_PMU
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_LIB_PMU
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_LIB_PMU
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 854c515a61..1ab6fb9eaa 100644
@@ -36,6 +36,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..378abccd72 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +4 -1
index 918f49bf4f..9be8724ec4 100644
@@ -72,8 +72,10 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		trace_pmu_args_apply(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +91,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	trace_pmu_args_free();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_pmu.c new file +38 -0
new file mode 100644
index 0000000000..b3ab41e8a2
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_common.h>
+
+#include "eal_trace.h"
+
+#ifdef RTE_LIB_PMU
+
+#include <rte_pmu.h>
+
+void
+trace_pmu_args_apply(const char *arg)
+{
+	static bool once;
+
+	if (!once) {
+		if (rte_pmu_init())
+			return;
+		once = true;
+	}
+
+	rte_pmu_add_events_by_pattern(arg);
+}
+
+void
+trace_pmu_args_free(void)
+{
+	rte_pmu_fini();
+}
+
+#else /* !RTE_LIB_PMU */
+
+void trace_pmu_args_apply(const char *arg __rte_unused) { return; }
+void trace_pmu_args_free(void) { return; }
+
+#endif /* RTE_LIB_PMU */
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/common/eal_trace.h +4 -0
index 55262677e0..58fa43472a 100644
@@ -104,6 +104,10 @@ int trace_epoch_time_save(void);
 void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
+/* PMU wrappers */
+void trace_pmu_args_apply(const char *arg);
+void trace_pmu_args_free(void);
+
 /* EAL interface */
 int eal_trace_init(void);
 void eal_trace_fini(void);
lib/eal/common/meson.build +1 -0
index c1bbf26654..59d5b15708 100644
@@ -27,6 +27,7 @@ sources += files(
         'eal_common_tailqs.c',
         'eal_common_thread.c',
         'eal_common_timer.c',
+        'eal_common_trace_pmu.c',
         'eal_common_trace_points.c',
         'eal_common_uuid.c',
         'malloc_elem.c',
lib/eal/include/rte_eal_trace.h +11 -0
index 9ad2112801..9c78f63ff5 100644
@@ -127,6 +127,17 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+#ifdef RTE_LIB_PMU
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/version.map +1 -0
index f493cd1ca7..3dc147f848 100644
@@ -399,6 +399,7 @@ EXPERIMENTAL {
 
 	# added in 24.11
 	rte_bitset_to_str;
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +65 -2

84 lines not on this page · raw message

lib/pmu/rte_pmu.h +21 -3
index 85f9127911..df571f0a2f 100644
@@ -135,7 +135,7 @@ __rte_pmu_enable_group(struct rte_pmu_event_group *group);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Initialize PMU library.
+ * Initialize PMU library. It's safe to call it multiple times.
  *
  * @return
  *   0 in case of success, negative value otherwise.
@@ -148,7 +148,9 @@ rte_pmu_init(void);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Finalize PMU library.
+ * Finalize PMU library. Number of calls must match number
+ * of times rte_pmu_init() was called. Otherwise memory
+ * won't be freed properly.
  */
 __rte_experimental
 void
@@ -173,7 +175,23 @@ 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.
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/<pmu>/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Read hardware counter configured to count occurrences of an event. This is called by an lcore
+ * binded exclusively to particular cpu and may not work as expected if gets migrated elsewhere.
+ * Reason being event group is pinned hence not supposed to be multiplexed with any other events.
  *
  * @param index
  *   Index of an event to be read.
lib/pmu/version.map +1 -0
index d0f907d13d..f14d498b54 100644
@@ -5,6 +5,7 @@ EXPERIMENTAL {
 	__rte_pmu_enable_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#171 lore raw

Re: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 7 lines · Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

--------------START-------------------------

Quoted 17 lines
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
        lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
        lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+       lib.eal.pmu.read)
+#endif

#define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)

lib/eal/common/eal_common_trace_points.c +1 -0
+#ifdef RTE_LIB_PMU

lib/eal/common/eal_common_trace_points.c

Quoted 10 lines
lib/eal/common/eal_common_trace_points.c +10 -0
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+       rte_eal_trace_pmu_read,
+       RTE_TRACE_POINT_ARGS(unsigned int index),
+       uint64_t val = rte_pmu_read(index);
+       rte_trace_point_emit_u64(val);
+)
+#endif
+

--------------END-------------------------

All of the above changes can go to lib/pmu. Right? Like ethdev is adding its trace points in ethdev library?
and make trace point name as rte_pmu_trace_read

#172 lore raw

RE: [EXTERNAL] Re: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 42 lines · Tomasz Duszynski
Quoted 7 lines

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

--------------START-------------------------

Quoted 19 lines
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
        lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
        lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+       lib.eal.pmu.read)
+#endif

#define RTE_EAL_TRACE_GENERIC_FUNC
rte_eal_trace_generic_func(__func__)

lib/eal/common/eal_common_trace_points.c +1 -0
+#ifdef RTE_LIB_PMU

lib/eal/common/eal_common_trace_points.c

Quoted 10 lines
lib/eal/common/eal_common_trace_points.c +10 -0
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+       rte_eal_trace_pmu_read,
+       RTE_TRACE_POINT_ARGS(unsigned int index),
+       uint64_t val = rte_pmu_read(index);
+       rte_trace_point_emit_u64(val);
+)
+#endif
+

--------------END-------------------------

Quoted 3 lines

All of the above changes can go to lib/pmu. Right? Like ethdev is adding its trace points in ethdev library?
and make trace point name as rte_pmu_trace_read

That's because libpmu is higher than eal in hierarchy of libraries so it won't see some symbols, for example __rte_trace_point_register(). So first tracing itself needs to be moved out to a separate library and then approach other libs take would become natural for libpmu too.

#173 lore raw

RE: [EXTERNAL] Re: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 49 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Monday, 28 October 2024 11.32

Quoted 1 line

On Fri, Oct 25, 2024 at 2:25 PM Tomasz Duszynski

Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that

and

Quoted 5 lines
Quoted 3 lines

add a dedicated PMU tracepoint.

Quoted 7 lines

--------------START-------------------------

lib/eal/common/eal_common_trace_points.c

Quoted 6 lines
lib/eal/common/eal_common_trace_points.c +0 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@

RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,

Quoted 25 lines
Quoted 13 lines
lib/eal/common/eal_common_trace_points.c +5 -0
        lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
        lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+       lib.eal.pmu.read)
+#endif

#define RTE_EAL_TRACE_GENERIC_FUNC
rte_eal_trace_generic_func(__func__)

lib/eal/common/eal_common_trace_points.c +1 -0
+#ifdef RTE_LIB_PMU

lib/eal/common/eal_common_trace_points.c

Quoted 10 lines
lib/eal/common/eal_common_trace_points.c +10 -0
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+       rte_eal_trace_pmu_read,
+       RTE_TRACE_POINT_ARGS(unsigned int index),
+       uint64_t val = rte_pmu_read(index);
+       rte_trace_point_emit_u64(val);
+)
+#endif
+

--------------END-------------------------

Quoted 1 line

All of the above changes can go to lib/pmu. Right? Like ethdev is

Quoted 8 lines

adding its trace points in ethdev

Quoted 2 lines

library?
and make trace point name as rte_pmu_trace_read

That's because libpmu is higher than eal in hierarchy of libraries so it won't see some symbols, for example __rte_trace_point_register(). So first tracing itself needs to be moved out to a separate library and then approach other libs take would become natural for libpmu too.

OK; I suppose the required change of tracing library is not going to happen right away, so Ack to keeping it here (for now).

Please rename trace point to rte_pmu_trace_read / lib.pmu.read as requested by Jerin. Although it is temporarily in EAL (due to the above trace library issue), it really is PMU library, not EAL library.

#174 lore raw

RE: [EXTERNAL] Re: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 58 lines
Quoted 49 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Monday, 28 October 2024 11.32

Quoted 1 line

On Fri, Oct 25, 2024 at 2:25 PM Tomasz Duszynski

Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that

and

Quoted 5 lines
Quoted 3 lines

add a dedicated PMU tracepoint.

Quoted 7 lines

--------------START-------------------------

lib/eal/common/eal_common_trace_points.c

Quoted 6 lines
lib/eal/common/eal_common_trace_points.c +0 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@

RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,

Quoted 25 lines
Quoted 13 lines
lib/eal/common/eal_common_trace_points.c +5 -0
        lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
        lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+       lib.eal.pmu.read)
+#endif

#define RTE_EAL_TRACE_GENERIC_FUNC
rte_eal_trace_generic_func(__func__)

lib/eal/common/eal_common_trace_points.c +1 -0
+#ifdef RTE_LIB_PMU

lib/eal/common/eal_common_trace_points.c

Quoted 10 lines
lib/eal/common/eal_common_trace_points.c +10 -0
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+       rte_eal_trace_pmu_read,
+       RTE_TRACE_POINT_ARGS(unsigned int index),
+       uint64_t val = rte_pmu_read(index);
+       rte_trace_point_emit_u64(val);
+)
+#endif
+

--------------END-------------------------

Quoted 1 line

All of the above changes can go to lib/pmu. Right? Like ethdev is

Quoted 8 lines

adding its trace points in ethdev

Quoted 2 lines

library?
and make trace point name as rte_pmu_trace_read

That's because libpmu is higher than eal in hierarchy of libraries so it won't see some symbols, for example __rte_trace_point_register(). So first tracing itself needs to be moved out to a separate library and then approach other libs take would become natural for libpmu too.

Quoted 7 lines

OK; I suppose the required change of tracing library is not going to happen right away, so Ack to keeping it here (for now).

Please rename trace point to rte_pmu_trace_read / lib.pmu.read as requested by Jerin. Although it is temporarily in EAL (due to the above trace library issue), it really is PMU library, not EAL library.

Sure, no problem.

#175 lore raw

RE: [PATCH v15 4/4] eal: add PMU support to tracing library

In reply to Tomasz Duszynski
Quoted 335 lines

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
app/test/test_trace_perf.c +10 -0
index 8257cc02be..8a0730943e 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_LIB_PMU
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_eal_trace_pmu_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_LIB_PMU
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_LIB_PMU
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 854c515a61..1ab6fb9eaa 100644
@@ -36,6 +36,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..378abccd72 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +4 -1
index 918f49bf4f..9be8724ec4 100644
@@ -72,8 +72,10 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		trace_pmu_args_apply(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +91,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	trace_pmu_args_free();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_pmu.c new file +38 -0
new file mode 100644
index 0000000000..b3ab41e8a2
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_common.h>
+
+#include "eal_trace.h"
+
+#ifdef RTE_LIB_PMU
+
+#include <rte_pmu.h>
+
+void
+trace_pmu_args_apply(const char *arg)
+{
+	static bool once;
+
+	if (!once) {
+		if (rte_pmu_init())
+			return;
+		once = true;
+	}
+
+	rte_pmu_add_events_by_pattern(arg);
+}
+
+void
+trace_pmu_args_free(void)
+{
+	rte_pmu_fini();
+}
+
+#else /* !RTE_LIB_PMU */
+
+void trace_pmu_args_apply(const char *arg __rte_unused) { return; }
+void trace_pmu_args_free(void) { return; }
+
+#endif /* RTE_LIB_PMU */
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/common/eal_trace.h +4 -0
index 55262677e0..58fa43472a 100644
@@ -104,6 +104,10 @@ int trace_epoch_time_save(void);
 void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
+/* PMU wrappers */
+void trace_pmu_args_apply(const char *arg);
+void trace_pmu_args_free(void);
+
 /* EAL interface */
 int eal_trace_init(void);
 void eal_trace_fini(void);
lib/eal/common/meson.build +1 -0
index c1bbf26654..59d5b15708 100644
@@ -27,6 +27,7 @@ sources += files(
         'eal_common_tailqs.c',
         'eal_common_thread.c',
         'eal_common_timer.c',
+        'eal_common_trace_pmu.c',
         'eal_common_trace_points.c',
         'eal_common_uuid.c',
         'malloc_elem.c',
lib/eal/include/rte_eal_trace.h +11 -0
index 9ad2112801..9c78f63ff5 100644
@@ -127,6 +127,17 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+#ifdef RTE_LIB_PMU
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/version.map +1 -0
index f493cd1ca7..3dc147f848 100644
@@ -399,6 +399,7 @@ EXPERIMENTAL {
 
 	# added in 24.11
 	rte_bitset_to_str;
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +60 -0
index dd57961627..b65f08c75b 100644
@@ -412,12 +412,75 @@ rte_pmu_add_event(const char *name)
 	return event->index;
 }
 
+static int
+add_events(const char *pattern)
+{
+	char *token, *copy;
+	int ret = 0;
+
+	copy = strdup(pattern);
+	if (copy == NULL)
+		return -ENOMEM;
+
+	token = strtok(copy, ",");
+	while (token) {
+		ret = rte_pmu_add_event(token);
+		if (ret < 0)
+			break;
+
+		token = strtok(NULL, ",");
+	}
+
+	free(copy);
+
+	return ret >= 0 ? 0 : ret;
+}
+
+int
+rte_pmu_add_events_by_pattern(const char *pattern)
+{
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..] pattern */
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret) {
+		PMU_LOG(ERR, "Failed to compile event matching regexp");
+		return -EINVAL;
+	}
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
+	}
+
+	regfree(&reg);
+

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 483

Quoted 10 lines
lib/pmu/rte_pmu.c +4 -1
+	return ret;
+}
+
 int
 rte_pmu_init(void)
 {
 	int ret;
 
-	if (rte_pmu.initialized)
+	if (rte_pmu.initialized && ++rte_pmu.initialized)

Stupid q, why not:
if (rte_pmu.initialized++ > 0) return 0?

Also increment/decrement counter implies that init()/fini() can be called multiple times, correct?
If so, there is still an implicit assumption that it always will happen in a sequential manner?

Quoted 75 lines
lib/pmu/rte_pmu.c +1 -1
 		return 0;
 
 	ret = scan_pmus();
@@ -450,7 +513,7 @@ rte_pmu_fini(void)
 	struct rte_pmu_event_group *group;
 	unsigned int i;
 
-	if (!rte_pmu.initialized)
+	if (!rte_pmu.initialized || --rte_pmu.initialized)
 		return;
 
 	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event) {
lib/pmu/rte_pmu.h +21 -3
index 85f9127911..df571f0a2f 100644
@@ -135,7 +135,7 @@ __rte_pmu_enable_group(struct rte_pmu_event_group *group);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Initialize PMU library.
+ * Initialize PMU library. It's safe to call it multiple times.
  *
  * @return
  *   0 in case of success, negative value otherwise.
@@ -148,7 +148,9 @@ rte_pmu_init(void);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Finalize PMU library.
+ * Finalize PMU library. Number of calls must match number
+ * of times rte_pmu_init() was called. Otherwise memory
+ * won't be freed properly.
  */
 __rte_experimental
 void
@@ -173,7 +175,23 @@ 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.
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/<pmu>/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Read hardware counter configured to count occurrences of an event. This is called by an lcore
+ * binded exclusively to particular cpu and may not work as expected if gets migrated elsewhere.
+ * Reason being event group is pinned hence not supposed to be multiplexed with any other events.
  *
  * @param index
  *   Index of an event to be read.
lib/pmu/version.map +1 -1
index d0f907d13d..f14d498b54 100644
@@ -5,6 +5,7 @@ EXPERIMENTAL {
 	__rte_pmu_enable_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
--

2.34.1

#176 lore raw

RE: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 348 lines
Quoted 79 lines

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)  create mode

100644 lib/eal/common/eal_common_trace_pmu.c

app/test/test_trace_perf.c +6 -0
index 8257cc02be..8a0730943e 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \  #define
 GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")  #define
 VOID_FP app_dpdk_test_fp()
+#ifdef RTE_LIB_PMU
+/* 0 corresponds first event passed via --trace= */ #define READ_PMU
+rte_eal_trace_pmu_read(0) #endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_LIB_PMU
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data,

size_t sz) @@ -174,6 +181,9 @@ test_trace_perf(void)

app/test/test_trace_perf.c +2 -0
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_LIB_PMU
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz); #endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 854c515a61..1ab6fb9eaa 100644
@@ -36,6 +36,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated
+tracepoint ``rte_eal_trace_pmu_event()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +0 -0
index d9b17abe90..378abccd72 100644

doc/guides/prog_guide/trace_lib.rst DPDK tracing library features · line 48

Quoted 3 lines
doc/guides/prog_guide/trace_lib.rst +0 -0
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://urldefense.proofpoint.com/v2/url?u=https-

3A__diamon.org_ctf_&d=DwIFAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=PZNXgrbjdlXxVEEGYkxIxRndyEUwWU_ad5ce22YI6Is &m=sUalf-TsyEi- hdbFm4eACDHmnQ9BrqmS_1Df11kPLuDK3_xxQZoVjxp2ZFNsHPnb&s=JntIuAUv6IrRBlS92bo8iPsQ66IVHHPouJ4S82GCNyI& e=>`_.

Quoted 19 lines
doc/guides/prog_guide/trace_lib.rst +1 -0
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be

enabled using the ``enable_trace_fp`` option for meson build.

doc/guides/prog_guide/trace_lib.rst +11 -0
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from
+hardware registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both
+expression matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and
+expression ``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'

doc/guides/prog_guide/trace_lib.rst

Quoted 10 lines
doc/guides/prog_guide/trace_lib.rst +10 -0
+
+Event names are available under
+``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a
+directory containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra
+variables added to source files. Instead, caller passes index which
+follows the order of events specified via ``--trace`` parameter. In
+the following example index ``0`` corresponds to ``cpu_cyclces`` while index ``1`` corresponds

to ``l1d_cache``.

Quoted 232 lines
doc/guides/prog_guide/trace_lib.rst +11 -0
+
+.. code-block:: c
+
+ ...
+ rte_eal_trace_pmu_read(0);
+ rte_eal_trace_pmu_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the
+``enable_trace_fp`` option for meson build.
+
 Event record mode
 -----------------
lib/eal/common/eal_common_trace.c +4 -1
index 918f49bf4f..9be8724ec4 100644
@@ -72,8 +72,10 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		trace_pmu_args_apply(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +91,7 @@ eal_trace_init(void)  void
 eal_trace_fini(void)
 {
+	trace_pmu_args_free();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_pmu.c new file +37 -0
new file mode 100644
index 0000000000..b3ab41e8a2
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_common.h>
+
+#include "eal_trace.h"
+
+#ifdef RTE_LIB_PMU
+
+#include <rte_pmu.h>
+
+void
+trace_pmu_args_apply(const char *arg) {
+	static bool once;
+
+	if (!once) {
+		if (rte_pmu_init())
+			return;
+		once = true;
+	}
+
+	rte_pmu_add_events_by_pattern(arg);
+}
+
+void
+trace_pmu_args_free(void)
+{
+	rte_pmu_fini();
+}
+
+#else /* !RTE_LIB_PMU */
+
+void trace_pmu_args_apply(const char *arg __rte_unused) { return; }
+void trace_pmu_args_free(void) { return; }
+
+#endif /* RTE_LIB_PMU */
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..c99eab92f4 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#ifdef RTE_LIB_PMU
+RTE_TRACE_POINT_REGISTER(rte_eal_trace_pmu_read,
+	lib.eal.pmu.read)
+#endif
lib/eal/common/eal_trace.h +4 -0
index 55262677e0..58fa43472a 100644
@@ -104,6 +104,10 @@ int trace_epoch_time_save(void);  void
 trace_mem_free(void);  void trace_mem_per_thread_free(void);
 
+/* PMU wrappers */
+void trace_pmu_args_apply(const char *arg); void
+trace_pmu_args_free(void);
+
 /* EAL interface */
 int eal_trace_init(void);
 void eal_trace_fini(void);
lib/eal/common/meson.build +1 -0
index c1bbf26654..59d5b15708 100644
@@ -27,6 +27,7 @@ sources += files(
         'eal_common_tailqs.c',
         'eal_common_thread.c',
         'eal_common_timer.c',
+        'eal_common_trace_pmu.c',
         'eal_common_trace_points.c',
         'eal_common_uuid.c',
         'malloc_elem.c',
(quoted diff) +0 -0

b/lib/eal/include/rte_eal_trace.h index 9ad2112801..9c78f63ff5 100644

lib/eal/include/rte_eal_trace.h +11 -0
@@ -127,6 +127,17 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC
 rte_eal_trace_generic_func(__func__)
 
+#ifdef RTE_LIB_PMU
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+	rte_eal_trace_pmu_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }

#endif

lib/eal/version.map index renamed from lib/eal/version.map +0 -0

f493cd1ca7..3dc147f848 100644

lib/eal/version.map +1 -0
@@ -399,6 +399,7 @@ EXPERIMENTAL {
 
 	# added in 24.11
 	rte_bitset_to_str;
+	__rte_eal_trace_pmu_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c index renamed from lib/pmu/rte_pmu.c +0 -0

dd57961627..b65f08c75b 100644

lib/pmu/rte_pmu.c +62 -0
@@ -412,12 +412,75 @@ rte_pmu_add_event(const char *name)
 	return event->index;
 }
 
+static int
+add_events(const char *pattern)
+{
+	char *token, *copy;
+	int ret = 0;
+
+	copy = strdup(pattern);
+	if (copy == NULL)
+		return -ENOMEM;
+
+	token = strtok(copy, ",");
+	while (token) {
+		ret = rte_pmu_add_event(token);
+		if (ret < 0)
+			break;
+
+		token = strtok(NULL, ",");
+	}
+
+	free(copy);
+
+	return ret >= 0 ? 0 : ret;
+}
+
+int
+rte_pmu_add_events_by_pattern(const char *pattern) {
+	regmatch_t rmatch;
+	char buf[BUFSIZ];
+	unsigned int num;
+	regex_t reg;
+	int ret;
+
+	/* events are matched against occurrences of e=ev1[,ev2,..] pattern */
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret) {
+		PMU_LOG(ERR, "Failed to compile event matching regexp");
+		return -EINVAL;
+	}
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+
+		num = rmatch.rm_eo - rmatch.rm_so;
+		if (num > sizeof(buf))
+			num = sizeof(buf);
+
+		/* skip e= pattern prefix */
+		memcpy(buf, pattern + rmatch.rm_so + 2, num - 2);
+		buf[num - 2] = '\0';
+		ret = add_events(buf);
+		if (ret)
+			break;
+
+		pattern += rmatch.rm_eo;
+	}
+
+	regfree(&reg);
+
+	return ret;
+}
+
 int
 rte_pmu_init(void)
 {
 	int ret;
 
Quoted 6 lines

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 483

Quoted 2 lines
lib/pmu/rte_pmu.c +1 -1
-	if (rte_pmu.initialized)
+	if (rte_pmu.initialized && ++rte_pmu.initialized)

Stupid q, why not:
if (rte_pmu.initialized++ > 0) return 0?

If you call rte_pmu_init() and it fails, then rte_pmu.initialized stays at 1. It means that libpmu is initialized but obviously that's not the case. On the other hand 1 means that there's one user, since this variable serves both as a flag and reference counter, which is true in fact. So only half of the story is true but we want the whole story to be true.

Besides, if first call fails then second one will return early hence caller won’t be able to initialize libpmu but this time due to a different reason.

And last thing, that expression resembles one used in rte_pmu_fini().

Quoted 3 lines

Also increment/decrement counter implies that init()/fini() can be called multiple times, correct? If so, there is still an implicit assumption that it always will happen in a sequential manner?

Yes, that should happen in sequential manner. Frankly, that counter should stay at 1 for the most of the time. The only exception is when you configure DPDK with -Denable_trace_fp=true and run functional test (pmu_autotest). Then that counter jumps to 2 during test and gets pulled down to 1 at the end.

Quoted 78 lines
Quoted 37 lines
lib/pmu/rte_pmu.c +1 -1
 		return 0;
 
 	ret = scan_pmus();
@@ -450,7 +513,7 @@ rte_pmu_fini(void)
 	struct rte_pmu_event_group *group;
 	unsigned int i;
 
-	if (!rte_pmu.initialized)
+	if (!rte_pmu.initialized || --rte_pmu.initialized)
 		return;
 
 	RTE_TAILQ_FOREACH_SAFE(event, &rte_pmu.event_list, next, tmp_event)

{ diff --git a/lib/pmu/rte_pmu.h b/lib/pmu/rte_pmu.h index 85f9127911..df571f0a2f 100644

lib/pmu/rte_pmu.h +4 -2
@@ -135,7 +135,7 @@ __rte_pmu_enable_group(struct rte_pmu_event_group *group);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Initialize PMU library.
+ * Initialize PMU library. It's safe to call it multiple times.
  *
  * @return
  *   0 in case of success, negative value otherwise.
@@ -148,7 +148,9 @@ rte_pmu_init(void);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Finalize PMU library.
+ * Finalize PMU library. Number of calls must match number
+ * of times rte_pmu_init() was called. Otherwise memory
+ * won't be freed properly.
  */
 __rte_experimental
 void

lib/pmu/rte_pmu.h rte_pmu_add_event(const char *name); · line 181

Quoted 8 lines
lib/pmu/rte_pmu.h +4 -1
@@ -173,7 +175,23 @@ 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.
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed

under

Quoted 4 lines
lib/pmu/rte_pmu.h +4 -0
+ *   /sys/bus/event_source/devices/<pmu>/events.
+ */
+__rte_experimental
+int

lib/pmu/rte_pmu.h rte_pmu_add_event(const char *name);

Quoted 10 lines
lib/pmu/rte_pmu.h +10 -0
+rte_pmu_add_events_by_pattern(const char *pattern);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Read hardware counter configured to count occurrences of an event.
+This is called by an lcore
+ * binded exclusively to particular cpu and may not work as expected if gets migrated elsewhere.
+ * Reason being event group is pinned hence not supposed to be multiplexed with any other

events.

Quoted 17 lines
lib/pmu/rte_pmu.h +0 -0
  *
  * @param index
  *   Index of an event to be read.
lib/pmu/version.map index renamed from lib/pmu/version.map +0 -0

d0f907d13d..f14d498b54 100644

lib/pmu/version.map +1 -1
@@ -5,6 +5,7 @@ EXPERIMENTAL {
 	__rte_pmu_enable_group;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
--

2.34.1

#177 lore raw

Re: [PATCH v15 4/4] eal: add PMU support to tracing library

In reply to Tomasz Duszynski
Quoted 6 lines · On Fri, 25 Oct 2024 10:54:14 +0200 Tomasz Duszynski <[email protected]>…

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

Quoted 16 lines
---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c

There is an issue with calling a rte_experimental function.

-------------------------------BEGIN LOGS----------------------------
####################################################################################
#### [Begin job log] "ubuntu-22.04-gcc-debug+doc+examples+tests" at step Build and test
####################################################################################
[3384/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o
FAILED: buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o
ccache gcc -Ibuildtools/chkincs/chkincs.p -Ibuildtools/chkincs -I../buildtools/chkincs -Iexamples/l3fwd -I../examples/l3fwd -I../examples/common -Idrivers/bus/vdev -I../drivers/bus/vdev -I. -I.. -Iconfig -I../config -Ilib/eal/include -I../lib/eal/include -Ilib/eal/linux/include -I../lib/eal/linux/include -Ilib/eal/x86/include -I../lib/eal/x86/include -Ilib/eal/common -I../lib/eal/common -Ilib/eal -I../lib/eal -Ilib/kvargs -I../lib/kvargs -Ilib/log -I../lib/log -Ilib/metrics -I../lib/metrics -Ilib/telemetry -I../lib/telemetry -Ilib/pmu -I../lib/pmu -Idrivers/bus/pci -I../drivers/bus/pci -I../drivers/bus/pci/linux -Ilib/pci -I../lib/pci -Idrivers/bus/vmbus -I../drivers/bus/vmbus -I../drivers/bus/vmbus/linux -Ilib/argparse -I../lib/argparse -Ilib/ptr_compress -I../lib/ptr_compress -Ilib/ring -I../lib/ring -Ilib/rcu -I../lib/rcu -Ilib/mempool -I../lib/mempool -Ilib/mbuf -I../lib/mbuf -Ilib/net -I../lib/net -Ilib/meter -I../lib/meter -Ilib/ethdev -I../lib/ethdev -Ilib/cmdline -I../lib/cmdline -Ilib/hash -I../lib/hash -Ilib/timer -I../lib/timer -Ilib/acl -I../lib/acl -Ilib/bbdev -I../lib/bbdev -Ilib/bitratestats -I../lib/bitratestats -Ilib/bpf -I../lib/bpf -Ilib/cfgfile -I../lib/cfgfile -Ilib/compressdev -I../lib/compressdev -Ilib/cryptodev -I../lib/cryptodev -Ilib/distributor -I../lib/distributor -Ilib/dmadev -I../lib/dmadev -Ilib/efd -I../lib/efd -Ilib/eventdev -I../lib/eventdev -Ilib/dispatcher -I../lib/dispatcher -Ilib/gpudev -I../lib/gpudev -Ilib/gro -I../lib/gro -Ilib/gso -I../lib/gso -Ilib/ip_frag -I../lib/ip_frag -Ilib/jobstats -I../lib/jobstats -Ilib/latencystats -I../lib/latencystats -Ilib/lpm -I../lib/lpm -Ilib/member -I../lib/member -Ilib/pcapng -I../lib/pcapng -Ilib/power -I../lib/power -Ilib/rawdev -I../lib/rawdev -Ilib/regexdev -I../lib/regexdev -Ilib/mldev -I../lib/mldev -Ilib/rib -I../lib/rib -Ilib/reorder -I../lib/reorder -Ilib/sched -I../lib/sched -Ilib/security -I../lib/security -Ilib/stack -I../lib/stack -Ilib/vhost -I../lib/vhost -Ilib/ipsec -I../lib/ipsec -Ilib/pdcp -I../lib/pdcp -Ilib/fib -I../lib/fib -Ilib/port -I../lib/port -Ilib/pdump -I../lib/pdump -Ilib/table -I../lib/table -Ilib/pipeline -I../lib/pipeline -Ilib/graph -I../lib/graph -Ilib/node -I../lib/node -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Werror -std=c11 -g -include rte_config.h -Wcast-qual -Wdeprecated -Wformat -Wformat-nonliteral -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wold-style-definition -Wpointer-arith -Wsign-compare -Wstrict-prototypes -Wundef -Wwrite-strings -Wno-address-of-packed-member -Wno-packed-not-aligned -Wno-missing-field-initializers -D_GNU_SOURCE -march=corei7 -mrtm -MD -MQ buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o -MF buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o.d -o buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o -c buildtools/chkincs/chkincs.p/rte_pmu.c In file included from buildtools/chkincs/chkincs.p/rte_pmu.c:1:
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h: In function ‘rte_pmu_read’:
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:214:17: error: ‘__rte_pmu_enable_group’ is deprecated: Symbol is not yet part of stable ABI [-Werror=deprecated-declarations]

  214 |                 ret = __rte_pmu_enable_group(group);
      |                 ^~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:132:1: note: declared here
  132 | __rte_pmu_enable_group(struct rte_pmu_event_group *group);
      | ^~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:222:9: error: ‘__rte_pmu_read_userpage’ is deprecated: Symbol is not yet part of stable ABI [-Werror=deprecated-declarations]
  222 |         return __rte_pmu_read_userpage(group->mmap_pages[index]);
      |         ^~~~~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:86:1: note: declared here
   86 | __rte_pmu_read_userpage(struct perf_event_mmap_page *pc)
      | ^~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
[3385/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_byteorder.c.o [3386/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_atomic.c.o [3387/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_rtm.c.o [3388/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_memcpy.c.o [3389/6468] Compiling C object app/dpdk-test.p/test_test_memcpy_perf.c.o
ninja: build stopped: subcommand failed.
##[error]Process completed with exit code 1.

#178 lore raw

RE: [EXTERNAL] Re: [PATCH v15 4/4] eal: add PMU support to tracing library

Quoted 96 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Wednesday, November 13, 2024 12:10 AM
To: Tomasz Duszynski <[email protected]>
Cc: Jerin Jacob <[email protected]>; Sunil Kumar Kori <[email protected]>; Tyler Retzlaff <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v15 4/4] eal: add PMU support to tracing library

On Fri, 25 Oct 2024 10: 54: 14 +0200 Tomasz Duszynski <tduszynski@ marvell. com> wrote: > In order to profile app one needs to store significant amount of samples > somewhere for an analysis later on. Since trace library supports >

Quoted 21 lines · On Fri, 25 Oct 2024 10:54:14 +0200 Tomasz Duszynski <[email protected]>…

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)  create mode
Quoted 1 line

100644 lib/eal/common/eal_common_trace_pmu.c

There is an issue with calling a rte_experimental function.

-------------------------------BEGIN LOGS----------------------------
####################################################################################
#### [Begin job log] "ubuntu-22.04-gcc-debug+doc+examples+tests" at step Build and test
####################################################################################
[3384/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o
FAILED: buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o
ccache gcc -Ibuildtools/chkincs/chkincs.p -Ibuildtools/chkincs -I../buildtools/chkincs - Iexamples/l3fwd -I../examples/l3fwd -I../examples/common -Idrivers/bus/vdev -I../drivers/bus/vdev - I. -I.. -Iconfig -I../config -Ilib/eal/include -I../lib/eal/include -Ilib/eal/linux/include - I../lib/eal/linux/include -Ilib/eal/x86/include -I../lib/eal/x86/include -Ilib/eal/common - I../lib/eal/common -Ilib/eal -I../lib/eal -Ilib/kvargs -I../lib/kvargs -Ilib/log -I../lib/log - Ilib/metrics -I../lib/metrics -Ilib/telemetry -I../lib/telemetry -Ilib/pmu -I../lib/pmu - Idrivers/bus/pci -I../drivers/bus/pci -I../drivers/bus/pci/linux -Ilib/pci -I../lib/pci - Idrivers/bus/vmbus -I../drivers/bus/vmbus -I../drivers/bus/vmbus/linux -Ilib/argparse - I../lib/argparse -Ilib/ptr_compress -I../lib/ptr_compress -Ilib/ring -I../lib/ring -Ilib/rcu - I../lib/rcu -Ilib/mempool -I../lib/mempool -Ilib/mbuf -I../lib/mbuf -Ilib/net -I../lib/net - Ilib/meter -I../lib/meter -Ilib/ethdev -I../lib/ethdev -Ilib/cmdline -I../lib/cmdline -Ilib/hash - I../lib/hash -Ilib/timer -I../lib/timer -Ilib/acl -I../lib/acl -Ilib/bbdev -I../lib/bbdev - Ilib/bitratestats -I../lib/bitratestats -Ilib/bpf -I../lib/bpf -Ilib/cfgfile -I../lib/cfgfile - Ilib/compressdev -I../lib/compressdev -Ilib/cryptodev -I../lib/cryptodev -Ilib/distributor - I../lib/distributor -Ilib/dmadev -I../lib/dmadev -Ilib/efd -I../lib/efd -Ilib/eventdev - I../lib/eventdev -Ilib/dispatcher -I../lib/dispatcher -Ilib/gpudev -I../lib/gpudev -Ilib/gro - I../lib/gro -Ilib/gso -I../lib/gso -Ilib/ip_frag -I../lib/ip_frag -Ilib/jobstats -I../lib/jobstats -Ilib/latencystats -I../lib/latencystats -Ilib/lpm -I../lib/lpm -Ilib/member -I../lib/member - Ilib/pcapng -I../lib/pcapng -Ilib/power -I../lib/power -Ilib/rawdev -I../lib/rawdev -Ilib/regexdev

(quoted diff) +0 -3
-I../lib/regexdev -Ilib/mldev -I../lib/mldev -Ilib/rib -I../lib/rib -Ilib/reorder -I../lib/reorder
-Ilib/sched -I../lib/sched -Ilib/security -I../lib/security -Ilib/stack -I../lib/stack -Ilib/vhost
-I../lib/vhost -Ilib/ipsec -I../lib/ipsec -Ilib/pdcp -I../lib/pdcp -Ilib/fib -I../lib/fib -

Ilib/port -I../lib/port -Ilib/pdump -I../lib/pdump -Ilib/table -I../lib/table -Ilib/pipeline - I../lib/pipeline -Ilib/graph -I../lib/graph -Ilib/node -I../lib/node -fdiagnostics-color=always - pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Werror -std=c11 -g -include rte_config.h - Wcast-qual -Wdeprecated -Wformat -Wformat-nonliteral -Wformat-security -Wmissing-declarations - Wmissing-prototypes -Wnested-externs -Wold-style-definition -Wpointer-arith -Wsign-compare - Wstrict-prototypes -Wundef -Wwrite-strings -Wno-address-of-packed-member -Wno-packed-not-aligned - Wno-missing-field-initializers -D_GNU_SOURCE -march=corei7 -mrtm -MD -MQ buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o -MF buildtools/chkincs/chkincs.p/meson- generated_rte_pmu.c.o.d -o buildtools/chkincs/chkincs.p/meson-generated_rte_pmu.c.o -c buildtools/chkincs/chkincs.p/rte_pmu.c
In file included from buildtools/chkincs/chkincs.p/rte_pmu.c:1:
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h: In function ‘rte_pmu_read’:
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:214:17: error: ‘__rte_pmu_enable_group’ is
deprecated: Symbol is not yet part of stable ABI [-Werror=deprecated-declarations]

 214 |                 ret = __rte_pmu_enable_group(group);
     |                 ^~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:132:1: note: declared here
 132 | __rte_pmu_enable_group(struct rte_pmu_event_group *group);
     | ^~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:222:9: error: ‘__rte_pmu_read_userpage’ is
deprecated: Symbol is not yet part of stable ABI [-Werror=deprecated-declarations]
 222 |         return __rte_pmu_read_userpage(group->mmap_pages[index]);
     |         ^~~~~~
/home/runner/work/dpdk/dpdk/lib/pmu/rte_pmu.h:86:1: note: declared here
  86 | __rte_pmu_read_userpage(struct perf_event_mmap_page *pc)
     | ^~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines

cc1: all warnings being treated as errors [3385/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_byteorder.c.o
[3386/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_atomic.c.o [3387/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_rtm.c.o [3388/6468] Compiling C object buildtools/chkincs/chkincs.p/meson-generated_rte_memcpy.c.o [3389/6468] Compiling C object app/dpdk-test.p/test_test_memcpy_perf.c.o
ninja: build stopped: subcommand failed.
##[error]Process completed with exit code 1.

Right, this indeed pops up with -Dcheck_includes=true. Will fix this in v16.

Thanks.

#179 lore raw
In reply to Tomasz Duszynski
Quoted 116 lines

-----Original Message-----
From: Tomasz Duszynski <[email protected]>
Sent: Friday, October 25, 2024 10:54 AM
To: Tomasz Duszynski <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v15 0/4] add support for self monitoring

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(), rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts rte_pmu_read() performance because more logic needs to be incorporated to handle all corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported
on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

MAINTAINERS                              |   5 +
app/test/meson.build                     |   1 +
app/test/test_pmu.c                      |  55 +++
app/test/test_trace_perf.c               |  10 +
doc/api/doxy-api-index.md                |   3 +-
doc/api/doxy-api.conf.in                 |   1 +
doc/guides/prog_guide/profile_app.rst    |  34 ++
doc/guides/prog_guide/trace_lib.rst      |  32 ++
doc/guides/rel_notes/release_24_11.rst   |   7 +
lib/eal/common/eal_common_trace.c        |   5 +-
lib/eal/common/eal_common_trace_pmu.c    |  38 ++
lib/eal/common/eal_common_trace_points.c |   5 +
lib/eal/common/eal_trace.h               |   4 +
lib/eal/common/meson.build               |   1 +
lib/eal/include/rte_eal_trace.h          |  11 +
lib/eal/meson.build                      |   3 +
lib/eal/version.map                      |   1 +
lib/meson.build                          |   1 +
lib/pmu/meson.build                      |  22 +
lib/pmu/pmu_arm64.c                      |  94 ++++
lib/pmu/pmu_private.h                    |  32 ++
lib/pmu/rte_pmu.c                        | 536 +++++++++++++++++++++++
lib/pmu/rte_pmu.h                        | 229 ++++++++++
lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
lib/pmu/version.map                      |  14 +
26 files changed, 1196 insertions(+), 2 deletions(-)  create mode 100644 app/test/test_pmu.c

create mode 100644 lib/eal/common/eal_common_trace_pmu.c
create mode 100644 lib/pmu/meson.build
create mode 100644 lib/pmu/pmu_arm64.c
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/rte_pmu_pmc_arm64.h create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h create mode 100644 lib/pmu/version.map

Quoted 2 lines

--
2.34.1

Hi,

Did anyone interested have a time to look into latest series?

#180 lore raw

v16 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v16:
- fix build issue related to ALLOW_EXPERIMENTAL_API not always defined
- rename tracepoints upfront to make it easier to decouple for tracing
- minor doc improvements
- make sure non EAL threads can't read PMU
v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(), rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts rte_pmu_read() performance because more logic needs to be incorporated to handle all corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported
on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  55 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/profile_app.rst    |  31 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_24_11.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   5 +-
 lib/eal/common/eal_common_trace_pmu.c    |  38 ++
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/common/eal_trace.h               |   4 +
 lib/eal/common/meson.build               |   1 +
 lib/eal/include/rte_eal_trace.h          |  11 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   1 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 536 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 250 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
 lib/pmu/version.map                      |  15 +
 26 files changed, 1215 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#181 lore raw

PATCH v16 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  49 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/profile_app.rst  |  26 ++
 doc/guides/rel_notes/release_24_11.rst |   7 +
 lib/eal/meson.build                    |   3 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  13 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 473 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 226 ++++++++++++
 lib/pmu/version.map                    |  14 +
 14 files changed, 853 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
MAINTAINERS +5 -0
index f84ca3ea68..940dce9940 100644
@@ -1854,6 +1854,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index 40f22a54d5..7533eadcdf 100644
@@ -143,6 +143,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +49 -0

49 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index f0193502bc..a0eb6fad73 100644
@@ -241,7 +241,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),
doc/api/doxy-api.conf.in +1 -0
index d23352d300..f26317d346 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/profile_app.rst +26 -0
index a6b5fb4d5e..ecb90a0d94 100644
@@ -7,6 +7,32 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+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 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +7 -0
index 5063badf39..1c299293e0 100644
@@ -324,6 +324,13 @@ New Features
   Added ability for node to advertise and update multiple xstat counters,
   that can be retrieved using ``rte_graph_cluster_stats_get``.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/eal/meson.build +3 -0
index e1d6c4cf17..1349624653 100644
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if dpdk_conf.has('RTE_LIB_PMU')
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/meson.build +1 -0
index ce92cb5537..968ad29e8d 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +13 -0
new file mode 100644
index 0000000000..f173b6f55c
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Marvell International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+headers = files('rte_pmu.h')
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..d2b15615bf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +473 -0

473 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +226 -0

226 lines not on this page · raw message

lib/pmu/version.map new file +14 -0
new file mode 100644
index 0000000000..566d653fcd
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 24.11
+	__rte_pmu_enable_group;
+	__rte_pmu_read_userpage;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
#182 lore raw

Re: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 7 lines · On Mon, 18 Nov 2024 08:37:03 +0100 Tomasz Duszynski <[email protected]>…
doc/guides/prog_guide/profile_app.rst +0 -0
index a6b5fb4d5e..ecb90a0d94 100644
@@ -7,6 +7,32 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 18

Quoted 10 lines
doc/guides/prog_guide/profile_app.rst +9 -0
 
+Performance counter based profiling
+-----------------------------------
+
+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 and run
+dedicated tasks interrupting those tasks with perf may be undesirable.

The data should be folded into telemetry rather than introducing yet another DPDK API for applications to deal with.

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 26

Quoted 8 lines
doc/guides/prog_guide/profile_app.rst +8 -0
+
+In such cases, an application can use the PMU library to read such events via ``rte_pmu_read()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.

What happens on older kernels?

doc/guides/rel_notes/release_24_11.rst

Quoted 16 lines
doc/guides/prog_guide/profile_app.rst +9 -0
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_24_11.rst +0 -0
index 5063badf39..1c299293e0 100644

Well 24.11 is released, so as a minimum will need rebase for 25.03

#183 lore raw

RE: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 19 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 3 December 2024 22.39

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines · On Mon, 18 Nov 2024 08:37:03 +0100 Tomasz Duszynski <[email protected]>…
doc/guides/prog_guide/profile_app.rst +4 -0
+Performance counter based profiling
+-----------------------------------
+
+Majority of architectures support some performance monitoring unit

(PMU).

Quoted 1 line

+Such unit provides programmable counters that monitor specific

events.

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines
doc/guides/prog_guide/profile_app.rst +4 -0
+
+Different tools gather that information, like for example perf.
+However, in some scenarios when CPU cores are isolated and run
+dedicated tasks interrupting those tasks with perf may be

undesirable.

Quoted 3 lines

The data should be folded into telemetry rather than introducing yet another
DPDK API for applications to deal with.

I strongly prefer the dedicated high-performance PMU API rather than using telemetry for this. Please keep the PMU API.

I expect to call the PMU API in our (proprietary) run-time profiling library, where reading PMU counters should be as lean as calling rte_rdtsc(). I sure don't want any superfluous overhead when profiling with a very high sampling rate.

For reference, many other libraries have dedicated APIs for reading the statistics structures of those libraries.

A wrapper around the PMU API can be added for Telemetry.

IMO, the Telemetry library should be made optional, like the Trace library recently was. For embedded systems, they are not only bloat, but potentially helpful for hackers trying to break in. And Security is one of the DPDK Governing Board's focus areas.

#184 lore raw

Re: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 26 lines · On Wed, 4 Dec 2024 00:49:58 +0100 Morten Brørup <[email protected]>…
Quoted 19 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 3 December 2024 22.39

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines · On Mon, 18 Nov 2024 08:37:03 +0100 Tomasz Duszynski <[email protected]>…
doc/guides/prog_guide/profile_app.rst +4 -0
+Performance counter based profiling
+-----------------------------------
+
+Majority of architectures support some performance monitoring unit  

(PMU).

Quoted 1 line

+Such unit provides programmable counters that monitor specific

events.

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines
doc/guides/prog_guide/profile_app.rst +4 -0
+
+Different tools gather that information, like for example perf.
+However, in some scenarios when CPU cores are isolated and run
+dedicated tasks interrupting those tasks with perf may be  

undesirable.

Quoted 3 lines

The data should be folded into telemetry rather than introducing yet another
DPDK API for applications to deal with.

I strongly prefer the dedicated high-performance PMU API rather than using telemetry for this. Please keep the PMU API.

Quoted 8 lines

I expect to call the PMU API in our (proprietary) run-time profiling library, where reading PMU counters should be as lean as calling rte_rdtsc(). I sure don't want any superfluous overhead when profiling with a very high sampling rate.

For reference, many other libraries have dedicated APIs for reading the statistics structures of those libraries.

A wrapper around the PMU API can be added for Telemetry.

IMO, the Telemetry library should be made optional, like the Trace library recently was. For embedded systems, they are not only bloat, but potentially helpful for hackers trying to break in. And Security is one of the DPDK Governing Board's focus areas.

Can this data go right into perf?
It is not clear why this is better than just using perf?
The one use case I can think of is a cloud provider with lots and lots of embedded systems. But in that case they already have much more detailed and integrated tools, the DPDK stuff is not needed.

#185 lore raw

RE: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 45 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 4 December 2024 01.55

Quoted 10 lines · On Wed, 4 Dec 2024 00:49:58 +0100 Morten Brørup <[email protected]>…
Quoted 10 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 3 December 2024 22.39

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines · On Mon, 18 Nov 2024 08:37:03 +0100 Tomasz Duszynski <[email protected]>…
doc/guides/prog_guide/profile_app.rst +4 -0
+Performance counter based profiling
+-----------------------------------
+
+Majority of architectures support some performance monitoring

unit

Quoted 10 lines
Quoted 10 lines

(PMU).

Quoted 1 line

+Such unit provides programmable counters that monitor specific

events.

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines
doc/guides/prog_guide/profile_app.rst +4 -0
+
+Different tools gather that information, like for example perf.
+However, in some scenarios when CPU cores are isolated and run
+dedicated tasks interrupting those tasks with perf may be

undesirable.

The data should be folded into telemetry rather than introducing

yet

Quoted 4 lines
Quoted 2 lines

another
DPDK API for applications to deal with.

I strongly prefer the dedicated high-performance PMU API rather than

using telemetry for this.

Quoted 3 lines

Please keep the PMU API.

I expect to call the PMU API in our (proprietary) run-time profiling

library, where reading PMU counters should be as lean as calling rte_rdtsc(). I sure don't want any superfluous overhead when profiling with a very high sampling rate.

Quoted 2 lines

For reference, many other libraries have dedicated APIs for reading

the statistics structures of those libraries.

Quoted 3 lines

A wrapper around the PMU API can be added for Telemetry.

Quoted 8 lines
Quoted 1 line

IMO, the Telemetry library should be made optional, like the Trace

library recently was. For embedded systems, they are not only bloat, but potentially helpful for hackers trying to break in. And Security is one of the DPDK Governing Board's focus areas.

Quoted 1 line

Can this data go right into perf?
It is not clear why this is better than just using perf?

I don't know enough about perf to provide a qualified answer to this.
Our data plane profiling library has very high resolution. Think of it gathering information about every single run of each pipeline stage, thus also providing detailed information about outliers.

Quoted 4 lines

The one use case I can think of is a cloud provider with lots and lots of embedded systems.
But in that case they already have much more detailed and integrated tools, the DPDK stuff is not needed.

#186 lore raw

Re: [PATCH v16 1/4] lib: add generic support for reading PMU events

In reply to Tomasz Duszynski

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 14

Quoted 5 lines · On Mon, 18 Nov 2024 08:37:03 +0100 Tomasz Duszynski <[email protected]>…
doc/guides/prog_guide/profile_app.rst +5 -0
+Performance counter based profiling
+-----------------------------------
+
+Majority of architectures support some performance monitoring unit (PMU).
+Such unit provides programmable counters that monitor specific events.

Sentence wording is awkward, maybe combine the two sentences and/or look to wikipedia or vendor for better definition.

Also add PMU to glossary in programmer's guide

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 23

Quoted 9 lines
doc/guides/prog_guide/profile_app.rst +9 -0
+
+Different tools gather that information, like for example perf.
+However, in some scenarios when CPU cores are isolated 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()``.
+
+By default, userspace applications are not allowed to access PMU internals. That can be changed
+by setting ``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default value anyway) and

Wrong path, should be /proc/sys/kernel/perf_event_paranoid The default in current kernels is 2.

doc/guides/prog_guide/profile_app.rst Profile Your Application · line 26

Quoted 3 lines
doc/guides/prog_guide/profile_app.rst +3 -0
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer to
+``Documentation/admin-guide/perf-security.rst`` under Linux sources for more information. Fairly
+recent kernel, i.e >= 5.9, is advised too.

Provide link to kernel.org page for that doc please.
https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html

Since multiple things (even selinux) can restrict this. Maybe best to just add a generic paragraph about what is needed, and leave the security description up to the kernel and distro to handle.

Quoted 9 lines
doc/guides/prog_guide/profile_app.rst +9 -0
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
#187 lore raw

RE: [EXTERNAL] Re: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 5 lines

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 4 lines
doc/guides/prog_guide/profile_app.rst +4 -0
+Performance counter based profiling
+-----------------------------------
+
+Majority of architectures support some performance monitoring unit

(PMU).

Quoted 7 lines
Quoted 1 line

+Such unit provides programmable counters that monitor specific events.

Sentence wording is awkward, maybe combine the two sentences and/or look to wikipedia or vendor for better definition.

Also add PMU to glossary in programmer's guide

Okay. I'll address that in the next revision.

Quoted 7 lines

doc/guides/prog_guide/profile_app.rst Profile Your Application

Quoted 6 lines
doc/guides/prog_guide/profile_app.rst +6 -0
+
+Different tools gather that information, like for example perf.
+However, in some scenarios when CPU cores are isolated 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()``.

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+By default, userspace applications are not allowed to access PMU
+internals. That can be changed by setting
+``/sys/kernel/perf_event_paranoid`` to 2 (that should be a default
+value anyway) and

Wrong path, should be /proc/sys/kernel/perf_event_paranoid

Thanks for catching this.

Quoted 2 lines

The default in current kernels is 2.

Right, that's what I meant. Though distros can set paranoia levels to higher values.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+adding ``CAP_PERFMON`` capability to DPDK application. Please refer
+to ``Documentation/admin-guide/perf-security.rst`` under Linux
+sources for more information. Fairly recent kernel, i.e >= 5.9, is advised too.

Provide link to kernel.org page for that doc please.

Okay

Quoted 5 lines

Since multiple things (even selinux) can restrict this. Maybe best to just add a generic paragraph about what is needed, and leave the security description up to the kernel and distro to handle.

I'll try to improve that in next release.

Quoted 9 lines
Quoted 9 lines
doc/guides/prog_guide/profile_app.rst +9 -0
+
+As of now implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
Acked-by on v16 1/4
#188 lore raw

RE: [PATCH v16 1/4] lib: add generic support for reading PMU events

In reply to Tomasz Duszynski
Quoted 6 lines

Add support for programming PMU counters and reading their values in runtime bypassing kernel completely.

Quoted 6 lines

This is especially useful in cases where CPU cores are isolated i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---

As future possible enhancements - I think it would be useful to make control-path API MT safe, plus probably try to hide some of the exposed internal structures (rte_pmu_event_group, etc.) inside .c (to minimize surface for possible ABI breakage).

Quoted 2 lines

--
2.34.1

#189 lore raw

RE: [PATCH v16 1/4] lib: add generic support for reading PMU events

Quoted 10 lines
Quoted 9 lines

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---

Quoted 7 lines

As future possible enhancements - I think it would be useful to make control- path API MT safe, plus probably try to hide some of the exposed internal structures (rte_pmu_event_group, etc.) inside .c (to minimize surface for possible ABI breakage).

Thanks. Yes sure, that series is not one time-addition. It will be improved over time.

Quoted 2 lines
Quoted 2 lines

--
2.34.1

#190 lore raw

PATCH v16 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 464e5068ec..0dc33eeccc 100644
@@ -13,6 +13,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index f173b6f55c..94619c63c2 100644
@@ -10,4 +10,12 @@ endif
 headers = files('rte_pmu.h')
 sources = files('rte_pmu.c')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index 06731c866f..6e073f11ea 100644
@@ -37,6 +37,10 @@ extern "C" {
 #include <rte_compat.h>
 #include <rte_lcore.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..39165bbc20
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#191 lore raw

PATCH v16 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index 0dc33eeccc..c6e798f52d 100644
@@ -15,6 +15,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 94619c63c2..5230ca3e69 100644
@@ -12,6 +12,7 @@ sources = files('rte_pmu.c')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 6e073f11ea..e96655e4d2 100644
@@ -39,6 +39,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..5eb8f73c11
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#192 lore raw

PATCH v16 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
app/test/test_trace_perf.c +10 -0
index 8257cc02be..28f908ce40 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_LIB_PMU
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_pmu_trace_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_LIB_PMU
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_LIB_PMU
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index ecb90a0d94..8e9cddb652 100644
@@ -33,6 +33,11 @@ As of now implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_pmu_trace_read()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..1cbfd42656 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_pmu_trace_read(0);
+ rte_pmu_trace_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +4 -1
index 918f49bf4f..9be8724ec4 100644
@@ -72,8 +72,10 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		trace_pmu_args_apply(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +91,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	trace_pmu_args_free();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_pmu.c new file +38 -0
new file mode 100644
index 0000000000..b3ab41e8a2
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2024 Marvell International Ltd.
+ */
+
+#include <rte_common.h>
+
+#include "eal_trace.h"
+
+#ifdef RTE_LIB_PMU
+
+#include <rte_pmu.h>
+
+void
+trace_pmu_args_apply(const char *arg)
+{
+	static bool once;
+
+	if (!once) {
+		if (rte_pmu_init())
+			return;
+		once = true;
+	}
+
+	rte_pmu_add_events_by_pattern(arg);
+}
+
+void
+trace_pmu_args_free(void)
+{
+	rte_pmu_fini();
+}
+
+#else /* !RTE_LIB_PMU */
+
+void trace_pmu_args_apply(const char *arg __rte_unused) { return; }
+void trace_pmu_args_free(void) { return; }
+
+#endif /* RTE_LIB_PMU */
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..25aeb439df 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#if defined(ALLOW_EXPERIMENTAL_API) && defined(RTE_LIB_PMU)
+RTE_TRACE_POINT_REGISTER(rte_pmu_trace_read,
+	lib.pmu.read)
+#endif
lib/eal/common/eal_trace.h +4 -0
index 55262677e0..58fa43472a 100644
@@ -104,6 +104,10 @@ int trace_epoch_time_save(void);
 void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
+/* PMU wrappers */
+void trace_pmu_args_apply(const char *arg);
+void trace_pmu_args_free(void);
+
 /* EAL interface */
 int eal_trace_init(void);
 void eal_trace_fini(void);
lib/eal/common/meson.build +1 -0
index e273745e93..239c111461 100644
@@ -28,6 +28,7 @@ sources += files(
         'eal_common_tailqs.c',
         'eal_common_thread.c',
         'eal_common_timer.c',
+        'eal_common_trace_pmu.c',
         'eal_common_trace_points.c',
         'eal_common_uuid.c',
         'malloc_elem.c',
lib/eal/include/rte_eal_trace.h +11 -0
index 9ad2112801..774ff9dcb2 100644
@@ -127,6 +127,17 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+#if defined(ALLOW_EXPERIMENTAL_API) && defined(RTE_LIB_PMU)
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+	rte_pmu_trace_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/version.map +1 -0
index a20c713eb1..945414c626 100644
@@ -398,6 +398,7 @@ EXPERIMENTAL {
 	# added in 24.11
 	rte_bitset_to_str;
 	rte_lcore_var_alloc;
+	__rte_pmu_trace_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +65 -2

84 lines not on this page · raw message

lib/pmu/rte_pmu.h +21 -3
index e96655e4d2..8c0d087874 100644
@@ -19,7 +19,9 @@
  * is required:
  *
  * rte_pmu_init()
- * rte_pmu_add_event()
+ * rte_pmu_add_event() [or rte_pmu_add_events_by_pattern()]
+ *
+ * Note that if -Denable_trace_fp=True was passed to meson rte_pmu_init() gets called automatically.
  *
  * Afterwards all threads can read events by calling rte_pmu_read().
  */
@@ -143,7 +145,7 @@ __rte_pmu_enable_group(struct rte_pmu_event_group *group);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Initialize PMU library.
+ * Initialize PMU library. It's safe to call it multiple times.
  *
  * @return
  *   0 in case of success, negative value otherwise.
@@ -156,7 +158,9 @@ rte_pmu_init(void);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Finalize PMU library.
+ * Finalize PMU library. Number of calls must match number
+ * of times rte_pmu_init() was called. Otherwise memory
+ * won't be freed properly.
  */
 __rte_experimental
 void
@@ -183,6 +187,20 @@ rte_pmu_add_event(const char *name);
 #define __rte_pmu_read_userpage(pc) ({ RTE_SET_USED(pc); 0; })
 #endif
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, where evX is a placeholder for an event listed under
+ *   /sys/bus/event_source/devices/<pmu>/events.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
lib/pmu/version.map +1 -0
index 566d653fcd..0bdb3e2f48 100644
@@ -6,6 +6,7 @@ EXPERIMENTAL {
 	__rte_pmu_read_userpage;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#193 lore raw

Re: [PATCH v16 4/4] eal: add PMU support to tracing library

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 458

Quoted 12 lines · On Mon, 18 Nov 2024 08:37:06 +0100 Tomasz Duszynski <[email protected]>…
lib/pmu/rte_pmu.c +12 -0
+
+	/* events are matched against occurrences of e=ev1[,ev2,..] pattern */
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret) {
+		PMU_LOG(ERR, "Failed to compile event matching regexp");
+		return -EINVAL;
+	}
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+

As with log parameters. Regex is harder to work with than a glob based syntax.

#194 lore raw

RE: [EXTERNAL] Re: [PATCH v16 4/4] eal: add PMU support to tracing library

Quoted 6 lines
Quoted 6 lines
lib/pmu/rte_pmu.c +6 -0
+
+	/* events are matched against occurrences of e=ev1[,ev2,..] pattern */
+	ret = regcomp(&reg, "e=([_[:alnum:]-],?)+", REG_EXTENDED);
+	if (ret) {
+		PMU_LOG(ERR, "Failed to compile event matching regexp");
+		return -EINVAL;
Quoted 8 lines

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 458

Quoted 6 lines
lib/pmu/rte_pmu.c +6 -0
+	}
+
+	for (;;) {
+		if (regexec(&reg, pattern, 1, &rmatch, 0))
+			break;
+

As with log parameters. Regex is harder to work with than a glob based syntax.

That regex is for --trace and --trace parameter is inherited from tracing library and that itself takes a regex. I don't that patch should tinker with existing behavior.

#195 lore raw

Re: [PATCH v16 4/4] eal: add PMU support to tracing library

In reply to Tomasz Duszynski

lib/pmu/rte_pmu.c rte_pmu_add_event(const char *name) · line 425

Quoted 11 lines · On Mon, 18 Nov 2024 08:37:06 +0100 Tomasz Duszynski <[email protected]>…
lib/pmu/rte_pmu.c +11 -0
+static int
+add_events(const char *pattern)
+{
+	char *token, *copy;
+	int ret = 0;
+
+	copy = strdup(pattern);
+	if (copy == NULL)
+		return -ENOMEM;
+
+	token = strtok(copy, ",");

Since strtok is not thread safe, either use strtok_r or another way to parse comma seperated list.

Maybe rte_strsplit could help?

#196 lore raw
In reply to Tomasz Duszynski
Quoted 121 lines

-----Original Message-----
From: Tomasz Duszynski <[email protected]>
Sent: Monday, November 18, 2024 8:37 AM
To: Tomasz Duszynski <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Jerin Jacob <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v16 0/4] add support for self monitoring

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v16:
- fix build issue related to ALLOW_EXPERIMENTAL_API not always defined
- rename tracepoints upfront to make it easier to decouple for tracing
- minor doc improvements
- make sure non EAL threads can't read PMU
v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(), rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts rte_pmu_read() performance because more logic needs to be incorporated to handle all corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported
on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

MAINTAINERS                              |   5 +
app/test/meson.build                     |   1 +
app/test/test_pmu.c                      |  55 +++
app/test/test_trace_perf.c               |  10 +
doc/api/doxy-api-index.md                |   3 +-
doc/api/doxy-api.conf.in                 |   1 +
doc/guides/prog_guide/profile_app.rst    |  31 ++
doc/guides/prog_guide/trace_lib.rst      |  32 ++
doc/guides/rel_notes/release_24_11.rst   |   7 +
lib/eal/common/eal_common_trace.c        |   5 +-
lib/eal/common/eal_common_trace_pmu.c    |  38 ++
lib/eal/common/eal_common_trace_points.c |   5 +
lib/eal/common/eal_trace.h               |   4 +
lib/eal/common/meson.build               |   1 +
lib/eal/include/rte_eal_trace.h          |  11 +
lib/eal/meson.build                      |   3 +
lib/eal/version.map                      |   1 +
lib/meson.build                          |   1 +
lib/pmu/meson.build                      |  22 +
lib/pmu/pmu_arm64.c                      |  94 ++++
lib/pmu/pmu_private.h                    |  32 ++
lib/pmu/rte_pmu.c                        | 536 +++++++++++++++++++++++
lib/pmu/rte_pmu.h                        | 250 +++++++++++
lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
lib/pmu/version.map                      |  15 +
26 files changed, 1215 insertions(+), 2 deletions(-)  create mode 100644 app/test/test_pmu.c

create mode 100644 lib/eal/common/eal_common_trace_pmu.c
create mode 100644 lib/pmu/meson.build
create mode 100644 lib/pmu/pmu_arm64.c
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/rte_pmu_pmc_arm64.h create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h create mode 100644 lib/pmu/version.map

Quoted 2 lines

--
2.34.1

Hi,

Any more comments on this?

#197 lore raw
In reply to Tomasz Duszynski
Quoted 12 lines · On Mon, 18 Nov 2024 08:37:02 +0100 Tomasz Duszynski <[email protected]>…

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Quoted 7 lines

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

It would be good to have a working useful example of what and how these could be used.

Given that most DPDK applications are poll mode, it is not clear how these could be useful.

There is a lot of work that perf does to go from the raw counters to useful data, and without that exposing the raw stuff doesn't seem that useful

#198 lore raw
Quoted 15 lines
Quoted 15 lines

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

Quoted 8 lines
Quoted 4 lines

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

It would be good to have a working useful example of what and how these could be used.

I added some tests which demonstrate how to use it. Unfortunately, I don't have any specific app to share this time.

Quoted 5 lines

Given that most DPDK applications are poll mode, it is not clear how these could be useful.

There is a lot of work that perf does to go from the raw counters to useful data, and without that exposing the raw stuff doesn't seem that useful

The problem with perf is that some systems simply does not have one by default and chances are you need to build it from sources. It's not a big deal but if you gather some specific statistics for later analysis it seems easier to use what you have at hand. Moreover, self-monitoring may give you some edge e.g. some custom sampling methodology tailored to specific task or predefined bundle of counters you always want to monitor. Other frameworks make use of that too, VPP has perfmon for example.

#199 lore raw

v17 cover add support for self monitoring

In reply to Tomasz Duszynski

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v17:
- improve docs
- use safer strtok_r()
v16:
- fix build issue related to ALLOW_EXPERIMENTAL_API not always defined
- rename tracepoints upfront to make it easier to decouple for tracing
- minor doc improvements
- make sure non EAL threads can't read PMU
v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(), rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts rte_pmu_read() performance because more logic needs to be incorporated to handle all corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported
on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  55 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/glossary.rst       |   3 +
 doc/guides/prog_guide/profile_app.rst    |  38 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_25_03.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   5 +-
 lib/eal/common/eal_common_trace_pmu.c    |  38 ++
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/common/eal_trace.h               |   4 +
 lib/eal/common/meson.build               |   1 +
 lib/eal/include/rte_eal_trace.h          |  11 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   1 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
 lib/pmu/version.map                      |  15 +
 27 files changed, 1227 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map

--
2.34.1

#200 lore raw

PATCH v17 1/4 lib: add generic support for reading PMU events

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 i.e run dedicated tasks. In such cases one cannot use standard perf utility without sacrificing latency and performance.

---
 MAINTAINERS                            |   5 +
 app/test/meson.build                   |   1 +
 app/test/test_pmu.c                    |  49 +++
 doc/api/doxy-api-index.md              |   3 +-
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/prog_guide/glossary.rst     |   3 +
 doc/guides/prog_guide/profile_app.rst  |  33 ++
 doc/guides/rel_notes/release_25_03.rst |   7 +
 lib/eal/meson.build                    |   3 +
 lib/meson.build                        |   1 +
 lib/pmu/meson.build                    |  13 +
 lib/pmu/pmu_private.h                  |  32 ++
 lib/pmu/rte_pmu.c                      | 474 +++++++++++++++++++++++++
 lib/pmu/rte_pmu.h                      | 227 ++++++++++++
 lib/pmu/version.map                    |  14 +
 15 files changed, 865 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
MAINTAINERS +5 -0
index b86cdd266b..226f41e36b 100644
@@ -1868,6 +1868,11 @@ M: Nithin Dabilpuram <[email protected]>
 M: Pavan Nikhilesh <[email protected]>
 F: lib/node/
 
+PMU - EXPERIMENTAL
+M: Tomasz Duszynski <[email protected]>
+F: lib/pmu/
+F: app/test/test_pmu*
+
 
 Test Applications
 -----------------
app/test/meson.build +1 -0
index 48cf77fda9..fb9fa9faf8 100644
@@ -142,6 +142,7 @@ source_file_deps = {
     'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps,
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
+    'test_pmu.c': ['pmu'],
     'test_power.c': ['power'],
     'test_power_cpufreq.c': ['power'],
     'test_power_intel_uncore.c': ['power'],
app/test/test_pmu.c new file +49 -0

49 lines not on this page · raw message

doc/api/doxy-api-index.md +2 -1
index b2fc24b3e4..adf5950899 100644
@@ -242,7 +242,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),
doc/api/doxy-api.conf.in +1 -0
index d23352d300..f26317d346 100644
@@ -69,6 +69,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/pdcp \
                           @TOPDIR@/lib/pdump \
                           @TOPDIR@/lib/pipeline \
+                          @TOPDIR@/lib/pmu \
                           @TOPDIR@/lib/port \
                           @TOPDIR@/lib/power \
                           @TOPDIR@/lib/ptr_compress \
doc/guides/prog_guide/glossary.rst +3 -0
index 8d6349701e..fd995373f2 100644
@@ -164,6 +164,9 @@ pktmbuf
 PMD
    Poll Mode Driver
 
+PMU
+   Performance Monitoring Unit
+
 QoS
    Quality of Service
 
doc/guides/prog_guide/profile_app.rst +33 -0
index a6b5fb4d5e..9a18aa0a26 100644
@@ -7,6 +7,39 @@ Profile Your Application
 The following sections describe methods of profiling DPDK applications on
 different architectures.
 
+Performance counter based profiling
+-----------------------------------
+
+Modern CPU architectures are equipped with Performance Monitoring Units (PMUs), which provide
+programmable counters to monitor specific hardware events, such as cache hits, instruction counts,
+and branch predictions.
+
+Tools like perf utilize PMUs to gather performance data. However, in scenarios where CPU cores are
+isolated, running dedicated tasks and performance of some specific regions of code must be analyzed
+extra overhead may be undesirable. In such cases, applications can directly access PMU data using
+the ``rte_pmu_read()`` function.
+
+Access requirements
+~~~~~~~~~~~~~~~~~~~
+
+Userspace applications may be restricted, due to various reasons, from accessing PMU internals.
+To enable access ``/proc/sys/kernel/perf_event_paranoid`` should be set to ``2`` and application
+should have ``CAP_PERFMON`` capability assigned.
+
+For comprehensive information on security implications and configuration, refer to
+`kernel documentation <https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html>`_.
+
+Limitations
+~~~~~~~~~~~
+
+Current implementation imposes certain limitations:
+
+* Only EAL lcores are supported
+
+* EAL lcores must not share a cpu
+
+* Each EAL lcore measures same group of events
+
 
 Profiling on x86
 ----------------
doc/guides/rel_notes/release_25_03.rst +7 -0
index 85986ffa61..546579a626 100644
@@ -63,6 +63,13 @@ New Features
   and even substantial part of its code.
   It can be viewed as an extension of rte_ring functionality.
 
+* **Added PMU library.**
+
+  Added a new performance monitoring unit (PMU) library which allows applications
+  to perform self monitoring activities without depending on external utilities like perf.
+  After integration with :doc:`../prog_guide/trace_lib` data gathered from hardware counters
+  can be stored in CTF format for further analysis.
+
 
 Removed Items
 -------------
lib/eal/meson.build +3 -0
index e1d6c4cf17..1349624653 100644
@@ -18,6 +18,9 @@ deps += ['log', 'kvargs']
 if not is_windows
     deps += ['telemetry']
 endif
+if dpdk_conf.has('RTE_LIB_PMU')
+    deps += ['pmu']
+endif
 if dpdk_conf.has('RTE_USE_LIBBSD')
     ext_deps += libbsd
 endif
lib/meson.build +1 -0
index ce92cb5537..968ad29e8d 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'argparse',
         'telemetry', # basic info querying
+        'pmu',
         'eal', # everything depends on eal
         'ptr_compress',
         'ring',
lib/pmu/meson.build new file +13 -0
new file mode 100644
index 0000000000..46eebda155
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2025 Marvell International Ltd.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+headers = files('rte_pmu.h')
+sources = files('rte_pmu.c')
+
+deps += ['log']
lib/pmu/pmu_private.h new file +32 -0
new file mode 100644
index 0000000000..388458850b
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 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 syscall.
+ *
+ * @param config
+ *   Architecture specific event configuration. Consult kernel sources for available options.
+ */
+void
+pmu_arch_fixup_config(uint64_t config[3]);
+
+#endif /* _PMU_PRIVATE_H_ */
lib/pmu/rte_pmu.c new file +474 -0

474 lines not on this page · raw message

lib/pmu/rte_pmu.h new file +227 -0

227 lines not on this page · raw message

lib/pmu/version.map new file +14 -0
new file mode 100644
index 0000000000..d21f7cf933
@@ -0,0 +1,14 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 25.03
+	__rte_pmu_enable_group;
+	__rte_pmu_read_userpage;
+	rte_pmu;
+	rte_pmu_add_event;
+	rte_pmu_fini;
+	rte_pmu_init;
+	rte_pmu_read;
+
+	local: *;
+};
Signature · 1 line
2.34.1
#201 lore raw

PATCH v17 2/4 pmu: support reading ARM PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading ARM PMU events in runtime.

---
 app/test/test_pmu.c         |  4 ++
 lib/pmu/meson.build         |  8 ++++
 lib/pmu/pmu_arm64.c         | 94 +++++++++++++++++++++++++++++++++++++
 lib/pmu/rte_pmu.h           |  4 ++
 lib/pmu/rte_pmu_pmc_arm64.h | 30 ++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 lib/pmu/pmu_arm64.c
 create mode 100644 lib/pmu/rte_pmu_pmc_arm64.h
app/test/test_pmu.c +4 -0
index 210190e583..e0c11e4898 100644
@@ -13,6 +13,10 @@ test_pmu_read(void)
 	int tries = 10, event;
 	uint64_t val = 0;
 
+#if defined(RTE_ARCH_ARM64)
+	name = "cpu_cycles";
+#endif
+
 	if (name == NULL) {
 		printf("PMU not supported on this arch\n");
 		return TEST_SKIPPED;
lib/pmu/meson.build +8 -0
index 46eebda155..467841e431 100644
@@ -10,4 +10,12 @@ endif
 headers = files('rte_pmu.h')
 sources = files('rte_pmu.c')
 
+indirect_headers += files(
+        'rte_pmu_pmc_arm64.h',
+)
+
+if dpdk_conf.has('RTE_ARCH_ARM64')
+    sources += files('pmu_arm64.c')
+endif
+
 deps += ['log']
lib/pmu/pmu_arm64.c new file +94 -0

94 lines not on this page · raw message

lib/pmu/rte_pmu.h +4 -0
index a158091115..171c52e692 100644
@@ -37,6 +37,10 @@ extern "C" {
 #include <rte_compat.h>
 #include <rte_lcore.h>
 
+#if defined(RTE_ARCH_ARM64)
+#include "rte_pmu_pmc_arm64.h"
+#endif
+
 /** Maximum number of events in a group */
 #define RTE_MAX_NUM_GROUP_EVENTS 8
 
lib/pmu/rte_pmu_pmc_arm64.h new file +30 -0
new file mode 100644
index 0000000000..2952f5d65e
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_ARM64_H_
+#define _RTE_PMU_PMC_ARM64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t val;
+
+	if (index == 31) {
+		/* CPU Cycles (0x11) must be read via pmccntr_el0 */
+		asm volatile("mrs %0, pmccntr_el0" : "=r" (val));
+	} else {
+		asm volatile(
+			"msr pmselr_el0, %x0\n"
+			"mrs %0, pmxevcntr_el0\n"
+			: "=r" (val)
+			: "rZ" (index)
+		);
+	}
+
+	return val;
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_ARM64_H_ */
Signature · 1 line
2.34.1
#202 lore raw

PATCH v17 3/4 pmu: support reading Intel x86_64 PMU events in runtime

In reply to Tomasz Duszynski

Add support for reading Intel x86_64 PMU events in runtime.

---
 app/test/test_pmu.c          |  2 ++
 lib/pmu/meson.build          |  1 +
 lib/pmu/rte_pmu.h            |  2 ++
 lib/pmu/rte_pmu_pmc_x86_64.h | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
app/test/test_pmu.c +2 -0
index e0c11e4898..addb078005 100644
@@ -15,6 +15,8 @@ test_pmu_read(void)
 
 #if defined(RTE_ARCH_ARM64)
 	name = "cpu_cycles";
+#elif defined(RTE_ARCH_X86_64)
+	name = "cpu-cycles";
 #endif
 
 	if (name == NULL) {
lib/pmu/meson.build +1 -0
index 467841e431..ad16ead11f 100644
@@ -12,6 +12,7 @@ sources = files('rte_pmu.c')
 
 indirect_headers += files(
         'rte_pmu_pmc_arm64.h',
+        'rte_pmu_pmc_x86_64.h',
 )
 
 if dpdk_conf.has('RTE_ARCH_ARM64')
lib/pmu/rte_pmu.h +2 -0
index 171c52e692..c66898d1d3 100644
@@ -39,6 +39,8 @@ extern "C" {
 
 #if defined(RTE_ARCH_ARM64)
 #include "rte_pmu_pmc_arm64.h"
+#elif defined(RTE_ARCH_X86_64)
+#include "rte_pmu_pmc_x86_64.h"
 #endif
 
 /** Maximum number of events in a group */
lib/pmu/rte_pmu_pmc_x86_64.h new file +24 -0
new file mode 100644
index 0000000000..9ba6e5bae8
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Marvell.
+ */
+#ifndef _RTE_PMU_PMC_X86_64_H_
+#define _RTE_PMU_PMC_X86_64_H_
+
+#include <rte_common.h>
+
+static __rte_always_inline uint64_t
+rte_pmu_pmc_read(int index)
+{
+	uint64_t low, high;
+
+	asm volatile(
+		"rdpmc\n"
+		: "=a" (low), "=d" (high)
+		: "c" (index)
+	);
+
+	return low | (high << 32);
+}
+#define rte_pmu_pmc_read rte_pmu_pmc_read
+
+#endif /* _RTE_PMU_PMC_X86_64_H_ */
Signature · 1 line
2.34.1
#203 lore raw

PATCH v17 4/4 eal: add PMU support to tracing library

In reply to Tomasz Duszynski

In order to profile app one needs to store significant amount of samples somewhere for an analysis later on. Since trace library supports storing data in a CTF format lets take advantage of that and add a dedicated PMU tracepoint.

---
 app/test/test_trace_perf.c               | 10 ++++
 doc/guides/prog_guide/profile_app.rst    |  5 ++
 doc/guides/prog_guide/trace_lib.rst      | 32 +++++++++++
 lib/eal/common/eal_common_trace.c        |  5 +-
 lib/eal/common/eal_common_trace_pmu.c    | 38 ++++++++++++++
 lib/eal/common/eal_common_trace_points.c |  5 ++
 lib/eal/common/eal_trace.h               |  4 ++
 lib/eal/common/meson.build               |  1 +
 lib/eal/include/rte_eal_trace.h          | 11 ++++
 lib/eal/version.map                      |  1 +
 lib/pmu/rte_pmu.c                        | 67 +++++++++++++++++++++++-
 lib/pmu/rte_pmu.h                        | 24 +++++++--
 lib/pmu/version.map                      |  1 +
 13 files changed, 198 insertions(+), 6 deletions(-)
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
app/test/test_trace_perf.c +10 -0
index 8257cc02be..28f908ce40 100644
@@ -114,6 +114,10 @@ worker_fn_##func(void *arg) \
 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
 #define VOID_FP app_dpdk_test_fp()
+#ifdef RTE_LIB_PMU
+/* 0 corresponds first event passed via --trace= */
+#define READ_PMU rte_pmu_trace_read(0)
+#endif
 
 WORKER_DEFINE(GENERIC_VOID)
 WORKER_DEFINE(GENERIC_U64)
@@ -122,6 +126,9 @@ WORKER_DEFINE(GENERIC_FLOAT)
 WORKER_DEFINE(GENERIC_DOUBLE)
 WORKER_DEFINE(GENERIC_STR)
 WORKER_DEFINE(VOID_FP)
+#ifdef RTE_LIB_PMU
+WORKER_DEFINE(READ_PMU)
+#endif
 
 static void
 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
@@ -174,6 +181,9 @@ test_trace_perf(void)
 	run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
 	run_test("string", worker_fn_GENERIC_STR, data, sz);
 	run_test("void_fp", worker_fn_VOID_FP, data, sz);
+#ifdef RTE_LIB_PMU
+	run_test("read_pmu", worker_fn_READ_PMU, data, sz);
+#endif
 
 	rte_free(data);
 	return TEST_SUCCESS;
doc/guides/prog_guide/profile_app.rst +5 -0
index 9a18aa0a26..525b5012ed 100644
@@ -40,6 +40,11 @@ Current implementation imposes certain limitations:
 
 * Each EAL lcore measures same group of events
 
+Alternatively tracing library can be used which offers dedicated tracepoint
+``rte_pmu_trace_read()``.
+
+Refer to :doc:`../prog_guide/trace_lib` for more details.
+
 
 Profiling on x86
 ----------------
doc/guides/prog_guide/trace_lib.rst +32 -0
index d9b17abe90..1cbfd42656 100644
@@ -46,6 +46,7 @@ DPDK tracing library features
   trace format and is compatible with ``LTTng``.
   For detailed information, refer to
   `Common Trace Format <https://diamon.org/ctf/>`_.
+- Support reading PMU events on ARM64 and x86-64 (Intel)
 
 How to add a tracepoint?
 ------------------------
@@ -139,6 +140,37 @@ the user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``.
 ``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using
 the ``enable_trace_fp`` option for meson build.
 
+PMU tracepoint
+--------------
+
+Performance monitoring unit (PMU) event values can be read from hardware
+registers using predefined ``rte_pmu_read`` tracepoint.
+
+Tracing is enabled via ``--trace`` EAL option by passing both expression
+matching PMU tracepoint name i.e ``lib.eal.pmu.read`` and expression
+``e=ev1[,ev2,...]`` matching particular events::
+
+    --trace='.*pmu.read\|e=cpu_cycles,l1d_cache'
+
+Event names are available under ``/sys/bus/event_source/devices/PMU/events``
+directory, where ``PMU`` is a placeholder for either a ``cpu`` or a directory
+containing ``cpus``.
+
+In contrary to other tracepoints this does not need any extra variables
+added to source files. Instead, caller passes index which follows the order of
+events specified via ``--trace`` parameter. In the following example index ``0``
+corresponds to ``cpu_cyclces`` while index ``1`` corresponds to ``l1d_cache``.
+
+.. code-block:: c
+
+ ...
+ rte_pmu_trace_read(0);
+ rte_pmu_trace_read(1);
+ ...
+
+PMU tracing support must be explicitly enabled using the ``enable_trace_fp``
+option for meson build.
+
 Event record mode
 -----------------
 
lib/eal/common/eal_common_trace.c +4 -1
index 918f49bf4f..9be8724ec4 100644
@@ -72,8 +72,10 @@ eal_trace_init(void)
 		goto free_meta;
 
 	/* Apply global configurations */
-	STAILQ_FOREACH(arg, &trace.args, next)
+	STAILQ_FOREACH(arg, &trace.args, next) {
 		trace_args_apply(arg->val);
+		trace_pmu_args_apply(arg->val);
+	}
 
 	rte_trace_mode_set(trace.mode);
 
@@ -89,6 +91,7 @@ eal_trace_init(void)
 void
 eal_trace_fini(void)
 {
+	trace_pmu_args_free();
 	trace_mem_free();
 	trace_metadata_destroy();
 	eal_trace_args_free();
lib/eal/common/eal_common_trace_pmu.c new file +38 -0
new file mode 100644
index 0000000000..3824904481
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2025 Marvell International Ltd.
+ */
+
+#include <rte_common.h>
+
+#include "eal_trace.h"
+
+#ifdef RTE_LIB_PMU
+
+#include <rte_pmu.h>
+
+void
+trace_pmu_args_apply(const char *arg)
+{
+	static bool once;
+
+	if (!once) {
+		if (rte_pmu_init())
+			return;
+		once = true;
+	}
+
+	rte_pmu_add_events_by_pattern(arg);
+}
+
+void
+trace_pmu_args_free(void)
+{
+	rte_pmu_fini();
+}
+
+#else /* !RTE_LIB_PMU */
+
+void trace_pmu_args_apply(const char *arg __rte_unused) { return; }
+void trace_pmu_args_free(void) { return; }
+
+#endif /* RTE_LIB_PMU */
lib/eal/common/eal_common_trace_points.c +5 -0
index 0f1240ea3a..25aeb439df 100644
@@ -100,3 +100,8 @@ RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_enable,
 	lib.eal.intr.enable)
 RTE_TRACE_POINT_REGISTER(rte_eal_trace_intr_disable,
 	lib.eal.intr.disable)
+
+#if defined(ALLOW_EXPERIMENTAL_API) && defined(RTE_LIB_PMU)
+RTE_TRACE_POINT_REGISTER(rte_pmu_trace_read,
+	lib.pmu.read)
+#endif
lib/eal/common/eal_trace.h +4 -0
index 55262677e0..58fa43472a 100644
@@ -104,6 +104,10 @@ int trace_epoch_time_save(void);
 void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
+/* PMU wrappers */
+void trace_pmu_args_apply(const char *arg);
+void trace_pmu_args_free(void);
+
 /* EAL interface */
 int eal_trace_init(void);
 void eal_trace_fini(void);
lib/eal/common/meson.build +1 -0
index e273745e93..239c111461 100644
@@ -28,6 +28,7 @@ sources += files(
         'eal_common_tailqs.c',
         'eal_common_thread.c',
         'eal_common_timer.c',
+        'eal_common_trace_pmu.c',
         'eal_common_trace_points.c',
         'eal_common_uuid.c',
         'malloc_elem.c',
lib/eal/include/rte_eal_trace.h +11 -0
index 9ad2112801..774ff9dcb2 100644
@@ -127,6 +127,17 @@ RTE_TRACE_POINT(
 
 #define RTE_EAL_TRACE_GENERIC_FUNC rte_eal_trace_generic_func(__func__)
 
+#if defined(ALLOW_EXPERIMENTAL_API) && defined(RTE_LIB_PMU)
+#include <rte_pmu.h>
+
+RTE_TRACE_POINT_FP(
+	rte_pmu_trace_read,
+	RTE_TRACE_POINT_ARGS(unsigned int index),
+	uint64_t val = rte_pmu_read(index);
+	rte_trace_point_emit_u64(val);
+)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/version.map +1 -0
index a20c713eb1..945414c626 100644
@@ -398,6 +398,7 @@ EXPERIMENTAL {
 	# added in 24.11
 	rte_bitset_to_str;
 	rte_lcore_var_alloc;
+	__rte_pmu_trace_read; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
lib/pmu/rte_pmu.c +65 -2

84 lines not on this page · raw message

lib/pmu/rte_pmu.h +21 -3
index c66898d1d3..093e62f040 100644
@@ -19,7 +19,9 @@
  * is required:
  *
  * rte_pmu_init()
- * rte_pmu_add_event()
+ * rte_pmu_add_event() [or rte_pmu_add_events_by_pattern()]
+ *
+ * Note that if -Denable_trace_fp=True was passed to meson rte_pmu_init() gets called automatically.
  *
  * Afterwards all threads can read events by calling rte_pmu_read().
  */
@@ -143,7 +145,7 @@ __rte_pmu_enable_group(struct rte_pmu_event_group *group);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Initialize PMU library.
+ * Initialize PMU library. It's safe to call it multiple times.
  *
  * @return
  *   0 in case of success, negative value otherwise.
@@ -156,7 +158,9 @@ rte_pmu_init(void);
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Finalize PMU library.
+ * Finalize PMU library. Number of calls must match number
+ * of times rte_pmu_init() was called. Otherwise memory
+ * won't be freed properly.
  */
 __rte_experimental
 void
@@ -184,6 +188,20 @@ rte_pmu_add_event(const char *name);
 #define __rte_pmu_read_userpage(pc) ({ RTE_SET_USED(pc); 0; })
 #endif
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add events matching pattern to the group of enabled events.
+ *
+ * @param pattern
+ *   Pattern e=ev1[,ev2,...] matching events, listed under /sys/bus/event_source/devices/pmu/events,
+ *   where evX and pmu are placeholders for respectively an event and an event source.
+ */
+__rte_experimental
+int
+rte_pmu_add_events_by_pattern(const char *pattern);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
lib/pmu/version.map +1 -0
index d21f7cf933..2502326cdf 100644
@@ -6,6 +6,7 @@ EXPERIMENTAL {
 	__rte_pmu_read_userpage;
 	rte_pmu;
 	rte_pmu_add_event;
+	rte_pmu_add_events_by_pattern;
 	rte_pmu_fini;
 	rte_pmu_init;
 	rte_pmu_read;
Signature · 1 line
2.34.1
#204 lore raw
In reply to Tomasz Duszynski

+CC David Christensen (IBM POWER maintainer) and Stanislaw Kardach (RISC-V maintainer) +CC Stanislaw Kardach (RISC-V maintainer)
+CC Wathsala Vithanage (ARM maintainer)
+CC Vipin Varghese (AMD x86 maintainer)

Quoted 131 lines

From: Tomasz Duszynski [mailto:[email protected]]
Sent: Friday, 17 January 2025 10.00

This series adds self monitoring support i.e allows to configure and read performance measurement unit (PMU) counters in runtime without using perf utility. This has certain advantages when application runs on
isolated cores running dedicated tasks.

Events can be read directly using rte_pmu_read() or using dedicated tracepoint rte_eal_trace_pmu_read(). The latter will cause events to be stored inside CTF file.

By design, all enabled events are grouped together and the same group is attached to lcores that use self monitoring funtionality.

Events are enabled by names, which need to be read from standard location under sysfs i.e

/sys/bus/event_source/devices/PMU/events

where PMU is a core pmu i.e one measuring cpu events. As of today raw events are not supported.

v17:
- improve docs
- use safer strtok_r()
v16:
- fix build issue related to ALLOW_EXPERIMENTAL_API not always defined
- rename tracepoints upfront to make it easier to decouple for tracing
- minor doc improvements
- make sure non EAL threads can't read PMU
v15:
- add some basic logs related to API failures
- get rid of thread-local-storage
- do not support MT-safety (which was buggy anyway) in management APIs (rte_pmu_init(),
rte_pmu_fini(), rte_pmu_add_{event,events_by_pattern}() as it impacts
rte_pmu_read()
performance because more logic needs to be incorporated to handle all
corner cases
- improve documentation slightly
- various other improvements here and there
v14:
- replace __atomic_xxx with rte_atomic_xxx
- rebase to dpdk/main since that's a new feature
v13:
- allow init/fini calling from multiple contexts
- get rid of conditional compilation and return erors in case APIs are used on unsupported OS
v12:
- rebase old series
- slightly refresh existing documentation
- make sure compiler won't optimize accesses to certain variables during
event readout
- drop previous Acked-by to respin a fresh review cycle
v11:
- skip fast test in case init fails
v10:
- check permissions before using counters
- do not use internal symbols in exported functions
- address review comments
v9:
- fix 'maybe-uninitialized' warning reported by CI
v8:
- just rebase series
v7:
- use per-lcore event group instead of global table index by lcore-id
- don't add pmu_autotest to fast tests because due to lack of suported on every arch
v6:
- move codebase to the separate library
- address review comments
v5:
- address review comments
- fix sign extension while reading pmu on x86
- fix regex mentioned in doc
- various minor changes/improvements here and there
v4:
- fix freeing mem detected by debug_autotest
v3:
- fix shared build
v2:
- fix problems reported by test build infra

Tomasz Duszynski (4):
lib: add generic support for reading PMU events
pmu: support reading ARM PMU events in runtime
pmu: support reading Intel x86_64 PMU events in runtime
eal: add PMU support to tracing library

 MAINTAINERS                              |   5 +
 app/test/meson.build                     |   1 +
 app/test/test_pmu.c                      |  55 +++
 app/test/test_trace_perf.c               |  10 +
 doc/api/doxy-api-index.md                |   3 +-
 doc/api/doxy-api.conf.in                 |   1 +
 doc/guides/prog_guide/glossary.rst       |   3 +
 doc/guides/prog_guide/profile_app.rst    |  38 ++
 doc/guides/prog_guide/trace_lib.rst      |  32 ++
 doc/guides/rel_notes/release_25_03.rst   |   7 +
 lib/eal/common/eal_common_trace.c        |   5 +-
 lib/eal/common/eal_common_trace_pmu.c    |  38 ++
 lib/eal/common/eal_common_trace_points.c |   5 +
 lib/eal/common/eal_trace.h               |   4 +
 lib/eal/common/meson.build               |   1 +
 lib/eal/include/rte_eal_trace.h          |  11 +
 lib/eal/meson.build                      |   3 +
 lib/eal/version.map                      |   1 +
 lib/meson.build                          |   1 +
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
 lib/pmu/version.map                      |  15 +
 27 files changed, 1227 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_pmu.c
 create mode 100644 lib/eal/common/eal_common_trace_pmu.c
 create mode 100644 lib/pmu/meson.build
 create mode 100644 lib/pmu/pmu_arm64.c
 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/rte_pmu_pmc_arm64.h
 create mode 100644 lib/pmu/rte_pmu_pmc_x86_64.h
 create mode 100644 lib/pmu/version.map
Quoted 2 lines

--
2.34.1

I think the PMU library will be useful, and hope it gets into the 25.11 LTS release. Let's see if it can go into 25.07 to begin with.

NB: Although this is Linux only, consider using rte_ffs64() instead of __builtin_ffsll() in the FIELD_PREP() macro.

#205 lore raw
In reply to Tomasz Duszynski

Hello,

17/01/2025 10:00, Tomasz Duszynski:

Quoted 7 lines
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +

Reading it again, I wonder why it is a separate library. In general we give access to the hardware in EAL. Why not having PMU in EAL?

#206 lore raw
Quoted 10 lines

From: Thomas Monjalon [mailto:[email protected]]
Sent: Tuesday, 3 June 2025 15.28

Hello,

17/01/2025 10:00, Tomasz Duszynski:

Quoted 4 lines
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537
Quoted 9 lines

+++++++++++++++++++++++

Quoted 3 lines
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +

Reading it again, I wonder why it is a separate library. In general we give access to the hardware in EAL. Why not having PMU in EAL?

IMO, the EAL should be a thin shim layer, providing abstractions to only the "must have" parts of any underlying hardware and O/S running the DPDK application, not auxiliary hardware or O/S features. The EAL is already bloated with stuff that is not absolutely required to run simple DPDK applications.

As an advocate for a slimmer EAL, having PMU as a separate library makes sense to me.

Not all hardware access is provided through the EAL.
E.g., access to hardware power management controls is provided through the Power library/driver combo.
The PMU feature could be considered a type of "library/driver" combo, like the Power library/driver combo; but that might come with unnecessary complexity and performance overhead from additional abstractions and indirect function calling, so I don't like this alternative.

-Morten

#207 lore raw
In reply to Thomas Monjalon

Hi Thomas,

Quoted 11 lines

Hello,

17/01/2025 10:00, Tomasz Duszynski:

Quoted 7 lines
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
Quoted 3 lines

Reading it again, I wonder why it is a separate library. In general we give access to the hardware in EAL. Why not having PMU in EAL?

I initially started PMU as something tightly coupled with EAL, but after feedback from other developers I decided to decouple it completely [1].

The Technical Board also discussed this and supported the idea of having PMU as a separate library [2].

The main goal is to keep EAL lean and focused only on core essentials, making the PMU code easier to manage separately. This separation aligns nicely with the broader effort to simplify EAL by moving optional features out.

[1] https://lore.kernel.org/dpdk-dev/20230111210547.GA2134@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net/
[2] https://mails.dpdk.org/archives/dev/2023-January/260035.html#:~:text=PMU%20Library%20,principle%20for%20inclusion%20in%20DPDK

#208 lore raw

06/06/2025 18:27, Tomasz Duszynski:

Quoted 23 lines

Hi Thomas,

Quoted 11 lines

Hello,

17/01/2025 10:00, Tomasz Duszynski:

Quoted 7 lines
 lib/pmu/meson.build                      |  22 +
 lib/pmu/pmu_arm64.c                      |  94 ++++
 lib/pmu/pmu_private.h                    |  32 ++
 lib/pmu/rte_pmu.c                        | 537 +++++++++++++++++++++++
 lib/pmu/rte_pmu.h                        | 251 +++++++++++
 lib/pmu/rte_pmu_pmc_arm64.h              |  30 ++
 lib/pmu/rte_pmu_pmc_x86_64.h             |  24 +
Quoted 3 lines

Reading it again, I wonder why it is a separate library. In general we give access to the hardware in EAL. Why not having PMU in EAL?

I initially started PMU as something tightly coupled with EAL, but after feedback from other developers I decided to decouple it completely [1].

The Technical Board also discussed this and supported the idea of having PMU as a separate library [2].

Quoted 7 lines

The main goal is to keep EAL lean and focused only on core essentials, making the PMU code easier to manage separately. This separation aligns nicely with the broader effort to simplify EAL by moving optional features out.

[1] https://lore.kernel.org/dpdk-dev/20230111210547.GA2134@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net/
[2] https://mails.dpdk.org/archives/dev/2023-January/260035.html#:~:text=PMU%20Library%20,principle%20for%20inclusion%20in%20DPDK

OK I forgot this history.
Let's go with a separate library.

I'm trying to merge it.
I'm moving to a new release notes.
I did a bit of cleanup.
I've replaced __builtin_ffsll with rte_ffs64.
I've exported symbols with the new macro RTE_EXPORT_EXPERIMENTAL_SYMBOL. Now I hit another kind of issue:
error: "__rte_weak" is deprecated
I fix this one with using flags like RTE_PMU_SUPPORTED. (note: it replaces RTE_LIB_PMU which was not defined)

And at the end I get this error for tracing:

rte_eal_trace.h:136:37: error: passing argument 1 of 'rte_pmu_read' makes integer from pointer without a cast [-Wint-conversion]

  136 |         uint64_t val = rte_pmu_read(index);
      |                                     ^~~~~
      |                                     |
      |                                     char * (*)(const char *, int)

I think integration in tracing is too much risky at this stage anyway, so I'll postpone it.

I'll post my rebased version of this last patch for follow-up.

#209 lore raw

Hi Thomas,

Thanks for taking the time to merge the code and for the detailed update. Cleanup and fixes you've done - appreciated.

I'm working on resolving remaining issues.

#210 lore raw

cover add platform bus

In reply to Tomasz Duszynski

Platform bus is a bus under Linux which manages devices that do not have any discovery-mechanism built in. Linux learns about platform devices directly from device-tree during boot-up phase.

Afterwards if userspace wants to use some particular device driver being usually a mixture of vdev/rawdev gets developed.

In order to simplify that introduce a DPDK platform bus which provides auto-probe experience and separates a bus logic from the driver itself.

Now only devices which are backed-by vfio-platform kernel driver are supported, though other options may be added if necessary.

Tomasz Duszynski (2):
lib: add helper to read strings from sysfs files
bus: add platform bus

 MAINTAINERS                                |   4 +
 app/test/test_eal_fs.c                     | 108 +++-
 doc/guides/rel_notes/release_23_03.rst     |   5 +
 drivers/bus/meson.build                    |   1 +
 drivers/bus/platform/bus_platform_driver.h | 174 ++++++
 drivers/bus/platform/meson.build           |  16 +
 drivers/bus/platform/platform.c            | 604 +++++++++++++++++++++
 drivers/bus/platform/platform_params.c     |  70 +++
 drivers/bus/platform/private.h             |  48 ++
 drivers/bus/platform/version.map           |  10 +
 lib/eal/common/eal_filesystem.h            |   6 +
 lib/eal/unix/eal_filesystem.c              |  24 +-
 lib/eal/version.map                        |   1 +
 13 files changed, 1053 insertions(+), 18 deletions(-)
 create mode 100644 drivers/bus/platform/bus_platform_driver.h
 create mode 100644 drivers/bus/platform/meson.build
 create mode 100644 drivers/bus/platform/platform.c
 create mode 100644 drivers/bus/platform/platform_params.c
 create mode 100644 drivers/bus/platform/private.h
 create mode 100644 drivers/bus/platform/version.map

--
2.34.1

#211 lore raw

PATCH 1/2 lib: add helper to read strings from sysfs files

Reading strings from sysfs files is a re-occurring pattern hence add helper for doing that.

---
 app/test/test_eal_fs.c          | 108 ++++++++++++++++++++++++++++----
 lib/eal/common/eal_filesystem.h |   6 ++
 lib/eal/unix/eal_filesystem.c   |  24 ++++---
 lib/eal/version.map             |   1 +
 4 files changed, 121 insertions(+), 18 deletions(-)
app/test/test_eal_fs.c +97 -11
index b3686edcb4..6c373fc7f1 100644
@@ -20,12 +20,33 @@ test_eal_fs(void)
 
 #else
 
+static int
+temp_create(char *filename, size_t len)
+{
+	char file_template[] = "/tmp/eal_test_XXXXXX";
+	char proc_path[PATH_MAX];
+	int fd;
+
+	fd = mkstemp(file_template);
+	if (fd == -1) {
+		perror("mkstemp() failure");
+		return -1;
+	}
+
+	snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
+	if (readlink(proc_path, filename, len) < 0) {
+		perror("readlink() failure");
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
 static int
 test_parse_sysfs_value(void)
 {
 	char filename[PATH_MAX] = "";
-	char proc_path[PATH_MAX];
-	char file_template[] = "/tmp/eal_test_XXXXXX";
 	int tmp_file_handle = -1;
 	FILE *fd = NULL;
 	unsigned valid_number;
@@ -40,16 +61,10 @@ test_parse_sysfs_value(void)
 
 	/* get a temporary filename to use for all tests - create temp file handle and then
 	 * use /proc to get the actual file that we can open */
-	tmp_file_handle = mkstemp(file_template);
-	if (tmp_file_handle == -1) {
-		perror("mkstemp() failure");
+	tmp_file_handle = temp_create(filename, sizeof(filename));
+	if (tmp_file_handle < 0)
 		goto error;
-	}
-	snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
-	if (readlink(proc_path, filename, sizeof(filename)) < 0) {
-		perror("readlink() failure");
-		goto error;
-	}
+
 	printf("Temporary file is: %s\n", filename);
 
 	/* test we get an error value if we use file before it's created */
@@ -175,11 +190,82 @@ test_parse_sysfs_value(void)
 	return -1;
 }
 
+static int
+test_parse_sysfs_string(void)
+{
+	const char *teststr = "the quick brown dog jumps over the lazy fox\n";
+	char filename[PATH_MAX] = "";
+	char buf[BUFSIZ] = { };
+	int tmp_file_handle;
+	FILE *fd = NULL;
+
+#ifdef RTE_EXEC_ENV_FREEBSD
+	/* BSD doesn't have /proc/pid/fd */
+	return 0;
+#endif
+	printf("Testing function eal_parse_sysfs_string()\n");
+
+	/* get a temporary filename to use for all tests - create temp file handle and then
+	 * use /proc to get the actual file that we can open
+	 */
+	tmp_file_handle = temp_create(filename, sizeof(filename));
+	if (tmp_file_handle < 0)
+		goto error;
+
+	printf("Temporary file is: %s\n", filename);
+
+	/* test we get an error value if we use file before it's created */
+	printf("Test reading a missing file ...\n");
+	if (eal_parse_sysfs_string("/dev/not-quite-null", buf, sizeof(buf)) == 0) {
+		printf("Error with eal_parse_sysfs_string() - returned success on reading empty file\n");
+		goto error;
+	}
+	printf("Confirmed return error when reading empty file\n");
+
+	/* test reading a string from file */
+	printf("Test reading string ...\n");
+	fd = fopen(filename, "w");
+	if (fd == NULL) {
+		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
+		goto error;
+	}
+	fprintf(fd, "%s", teststr);
+	fclose(fd);
+	fd = NULL;
+	if (eal_parse_sysfs_string(filename, buf, sizeof(buf) - 1) < 0) {
+		printf("eal_parse_sysfs_string() returned error - test failed\n");
+		goto error;
+	}
+	if (strcmp(teststr, buf)) {
+		printf("Invalid string read by eal_parse_sysfs_string() - test failed\n");
+		goto error;
+	}
+	/* don't print newline */
+	buf[strlen(buf) - 1] = '\0';
+	printf("Read '%s\\n' ok\n", buf);
+
+	close(tmp_file_handle);
+	unlink(filename);
+	printf("eal_parse_sysfs_string() - OK\n");
+	return 0;
+
+error:
+	if (fd)
+		fclose(fd);
+	if (tmp_file_handle > 0)
+		close(tmp_file_handle);
+	if (filename[0] != '\0')
+		unlink(filename);
+	return -1;
+}
+
 static int
 test_eal_fs(void)
 {
 	if (test_parse_sysfs_value() < 0)
 		return -1;
+	if (test_parse_sysfs_string() < 0)
+		return -1;
 	return 0;
 }
 
lib/eal/common/eal_filesystem.h +6 -0
index 5d21f07c20..ac6449f529 100644
@@ -104,4 +104,10 @@ eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id
  * Used to read information from files on /sys */
 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
 
+/** Function to read a string from a file on the filesystem.
+ * Used to read information for files in /sys
+ */
+__rte_internal
+int eal_parse_sysfs_string(const char *filename, char *str, size_t size);
+
 #endif /* EAL_FILESYSTEM_H */
lib/eal/unix/eal_filesystem.c +17 -7
index afbab9368a..8ed10094be 100644
@@ -76,12 +76,9 @@ int eal_create_runtime_dir(void)
 	return 0;
 }
 
-/* parse a sysfs (or other) file containing one integer value */
-int eal_parse_sysfs_value(const char *filename, unsigned long *val)
+int eal_parse_sysfs_string(const char *filename, char *str, size_t size)
 {
 	FILE *f;
-	char buf[BUFSIZ];
-	char *end = NULL;
 
 	if ((f = fopen(filename, "r")) == NULL) {
 		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
@@ -89,19 +86,32 @@ int eal_parse_sysfs_value(const char *filename, unsigned long *val)
 		return -1;
 	}
 
-	if (fgets(buf, sizeof(buf), f) == NULL) {
+	if (fgets(str, size, f) == NULL) {
 		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
 			__func__, filename);
 		fclose(f);
 		return -1;
 	}
+	fclose(f);
+	return 0;
+}
+
+/* parse a sysfs (or other) file containing one integer value */
+int eal_parse_sysfs_value(const char *filename, unsigned long *val)
+{
+	char buf[BUFSIZ];
+	char *end = NULL;
+	int ret;
+
+	ret = eal_parse_sysfs_string(filename, buf, sizeof(buf));
+	if (ret < 0)
+		return ret;
+
 	*val = strtoul(buf, &end, 0);
 	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
 		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
 				__func__, filename);
-		fclose(f);
 		return -1;
 	}
-	fclose(f);
 	return 0;
 }
lib/eal/version.map +1 -0
index 7ad12a7dc9..9118bb6228 100644
@@ -445,6 +445,7 @@ EXPERIMENTAL {
 INTERNAL {
 	global:
 
+	eal_parse_sysfs_string; # WINDOWS_NO_EXPORT
 	rte_bus_register;
 	rte_bus_unregister;
 	rte_eal_get_baseaddr;
Signature · 1 line
2.34.1
#212 lore raw

Re: [PATCH 1/2] lib: add helper to read strings from sysfs files

25/01/2023 11:33, Tomasz Duszynski:

Quoted 2 lines

Reading strings from sysfs files is a re-occurring pattern hence add helper for doing that.

In general it would be to nice to clean sysfs parsing in libs and drivers, so they all use some functions from EAL.

#213 lore raw

Re: [PATCH 1/2] lib: add helper to read strings from sysfs files

Quoted 6 lines · Thomas Monjalon

25/01/2023 11:33, Tomasz Duszynski:

Quoted 2 lines

Reading strings from sysfs files is a re-occurring pattern hence add helper for doing that.

In general it would be to nice to clean sysfs parsing in libs and drivers, so they all use some functions from EAL.

maybe there should be a general utility library for dealing with sysfs separate from the core EAL that drivers / platform specific libs can share?