History log of /freebsd-current/lib/libpmc/libpmc_pmu_util.c
Revision Date Author Comments
# 6729e8a4 23-May-2024 Brooks Davis <brooks@FreeBSD.org>

libpmc: remove tautological assert

gcc13 whines about this assert than an unsigned integer is >= 0.

Reviewed by: luporl
Fixes: b48a2770d48b9 powerpc64: add Power8 and Power9 PMCs
Differential Revision: https://reviews.freebsd.org/D45232


# c190fb35 06-Jun-2023 Mitchell Horne <mhorne@FreeBSD.org>

pmc: better distinguish pmu-events allocation path

Background:

The pm_ev field of struct pmc_op_pmcallocate and struct pmc
traditionally contains the index of the chosen event, corresponding to
the __PMC_EVENTS array in pmc_events.h. This is a static list of events,
maintained by FreeBSD.

In the usual case, libpmc translates the user supplied event name
(string) into the pm_ev index, which is passed as an argument to the
allocation syscall. On the kernel side, the allocation method for the
relevant hwpmc class translates the given index into the event code that
will be written to an event selection register.

In 2018, a new source of performance event definitions was introduced:
the pmu-events json files, which are maintained by the Linux kernel. The
result was better coverage for newer Intel processors with a reduced
maintenance burden for libpmc/hwpmc. Intel and AMD CPUs were
unconditionally switched to allocate events from pmu-events instead of
the traditional scheme (959826ca1bb0a, 81eb4dcf9e0d).

Under the pmu-events scheme, the pm_ev field contains an index
corresponding to the selected event from the pmu-events table, something
which the kernel has no knowledge of. The configuration for the
performance counting registers is instead passed via class-dependent
fields (struct pmc_md_op_pmcallocate).

In 2021 I changed the allocation logic so that it would attempt to
pull from the pmu-events table first, and fall-back to the traditional
method (dfb4fb41166bc3). Later, pmu-events support for arm64 and power8
CPUs was added (28dd6730a5d6 and b48a2770d48b).

The problem that remains is that the pm_ev field is overloaded, without
a definitive way to determine whether the event allocation came from the
pmu-events table or FreeBSD's statically-defined PMC events. This
resulted in a recent fix, 21f7397a61f7.

Change:

To disambiguate these two supported but separate use-cases, add a new
flag, PMC_F_EV_PMU, to be set as part of the allocation, indicating that
the event index came from pmu-events.

This is useful in two ways:
1. On the kernel side, we can validate the syscall arguments better.
Some classes support only the traditional event scheme (e.g.
hwpmc_armv7), while others support only the pmu-events method (e.g.
hwpmc_core for Intel). We can now check for this. The hwpmc_arm64
class supports both methods, so the new flag supersedes the existing
MD flag, PM_MD_EVENT_RAW.

2. The flag will be tracked in struct pmc for the duration of its
lifetime, meaning it is communicated back to userspace. This allows
libpmc to perform the reverse index-to-event-name translation
without speculating about the meaning of the index value.

Adding the flag is a backwards-incompatible ABI change. We recently
bumped the major version of the hwpmc module, so this breakage is
acceptable.

Reviewed by: jkoshy
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D40753


# 45dcc17e 19-Jun-2023 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: make pmc_pmu_pmcallocate() machine-independent

Have it call the platform-dependent version. For better layering, move
the reset logic inside the new function. This is mainly to facilitate an
upcoming change.

Reviewed by: jkoshy
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D40752


# b3e76948 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: two-line .h pattern

Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/


# 4d846d26 10-May-2023 Warner Losh <imp@FreeBSD.org>

spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD

The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with: pfg
MFC After: 3 days
Sponsored by: Netflix


# 6f50b73e 06-Jul-2022 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: add armv8 pmu-event aliases

These are all "standard microarchitectural events", which in theory are
supported by every ARMv8 processor. In practice, it depends on the
pmu-event definitions being complete and accurate, which they are not
for every processor. Still, these aliases should be functional on the
majority of systems.

PR: 254532
Reported by: emaste
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D35550


# e144cd92 02-Jun-2022 Alexander Motin <mav@FreeBSD.org>

libpmc: Prefer fixed counters in Intel event aliases.

It is recommended by Intel to use fixed counters when possible to
leave programmable ones for different events.

This fixes `pmc stat`, which tries to count 6 events same time, while
many Intel CPUs have only 4 programmable counters.

MFC after: 1 month


# 037dd0a9 30-May-2022 Alexander Motin <mav@FreeBSD.org>

libpmc: Fix INVERT flag mapping for both Intel and AMD.

Looks like somebody's trivial copy/paste bug.

MFC after: 1 month


# 73b7b181 30-May-2022 Alexander Motin <mav@FreeBSD.org>

libpmc: Disable hardcoding of Intel fixed counters.

Intel json's now have pretty reasonable fixed counters representation
via event=0 and umask. Hardcoded values create more confusion than fix.

MFC after: 1 month


# a20c1089 30-Sep-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: add some AMD pmu counter aliases

Make it mostly compatible with what's defined for Intel. Except where
noted, these are defined for all of amdzen(1|2|3).

Reviewed by: emaste
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D32162


# 937539e0 30-Sep-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: fix the 'cycles' event alias on x86

Looking for "tsc-tsc" in the pmu tables will fail every time. Instead,
make this an alias for the static TSC event defined in pmc_events.h.
This fixes 'pmcstat -s cycles' on Intel and AMD.

Reviewed by: emaste
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D32197


# b48a2770 23-Jul-2021 Leandro Lupori <luporl@FreeBSD.org>

powerpc64: add Power8 and Power9 PMCs

Add support to allocate Power8 and 9 PMCs.

Submitted by: Leonardo Bianconi <leonardo.bianconi@eldorado.org.br>
Reviewed by: mhorne
Sponsored by: Instituto de Pesquisas Eldorado (eldorado.org.br)
Differential Revision: https://reviews.freebsd.org/D31109


# 27ea55fc 21-Jul-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc/hwpmc: fix issues with arm64 pmu-events support

Due to a mis-merge, the changes committed to libpmc never called
pmu_parse_event(), or set pm->pm_ev. However, this field shouldn't be
used to carry the actual pmc event code anyway, as it is expected to
contain the index into the pmu event array (otherwise, it breaks event
name lookup in pmclog_get_event()). Add a new MD field,
pm_md.pm_md_config, to pass the raw event code to arm64_allocate_pmc().

Additionally, the change made to pmc_md_op_pmcallocate was incorrect, as
this is a union, not a struct. Restore the proper padding size.

Reviewed by: luporl, ray, andrew
Fixes: 28dd6730a5d6 ("libpmc: enable pmu_utils on arm64")
Fixes: 8cc3815f02be ("hwpmc_arm64: accept raw event codes...")
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D31221


# 28dd6730 27-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: enable pmu_utils on arm64

This allows supported libpmc to query/select from the pmu-events table,
which may have a more complete set of events than what we define
manually. A future update to these definitions should greatly improve
this support. The alias table is empty for now, until this future import
is complete.

Add the Foundation's copyright for recent work on this file.

Reviewed by: ray (slightly earlier version)
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30603


# 0024f1aa 31-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: make libpmc_pmu_utils.c more amenable to porting

The current version has every function stubbed out for !x86. Only two
functions (pmu_alias_get() and pmc_pmu_pmcallocate() are really platform
dependent, so reduce the width of the ifdefs and remove some of the
stubs.

Reviewed by: ray
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30532


# 0c915023 31-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: remove pe->alias

It has never been a part of upstream's struct pmu_event. The jevents
utility will not fill this field, so remove it.

Reviewed by: gnn
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30530


# ec66cc95 31-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: eliminate pmc_pmu_stat_mode()

There is a single consumer, the pmc utility, that clearly has knowledge
of which counters it is expecting. Remove this function and have it
use common counter aliases instead.

Reviewed by: gnn
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30528


# 0092642f 31-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: remove unused 'isfixed' variable

Reviewed by: gnn, emaste
MFC after: 5 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30529


# bc1a6a9d 31-May-2021 Mitchell Horne <mhorne@FreeBSD.org>

libpmc: fix "instructions" alias on Intel

The typo prevents the counter from being allocated.

This fixes e.g. pmcstat -s instructions sleep 5

Reviewed by: mizhka, gnn, ray, emaste
MFC after: 5 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30527


# 24e337be 27-Jul-2020 Ryan Moeller <freqlabs@FreeBSD.org>

libpmc: Use known pmc_cpuid buffer size

Use the existing PMC_CPUID_LEN to size pmc_cpuid in the kernel and various
buffers for reading it in libpmc. This avoids some extra syscalls and
malloc/frees.

While in here, use strlcpy to copy a user-provided cpuid string instead of
memcpy, to make sure we terminate the buffer.

Reviewed by: mav
MFC after: 1 week
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D25679


# 1791cad0 14-Jul-2020 Alexander Motin <mav@FreeBSD.org>

Add stepping to the kern.hwpmc.cpuid string on x86.

It follows the equivalent Linux change to be able to differentiate
skylakex and cascadelakex, sharing the same model but not stepping.

This fixes skylakex handling broken by r363144.

MFC after: 6 days


# 53071ed1 07-Feb-2020 Konstantin Belousov <kib@FreeBSD.org>

pmc: Add Hygon Dhyana support.

To make the PMC tool pmcstat working properly on Hygon platform, add
support for Hygon Dhyana family 18h by using the PMC initialization
code path of AMD family 17h.

Submitted by: Pu Wen <puwen@hygon.cn>
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D23562


# f3dbece8 08-Nov-2019 Emmanuel Vadot <manu@FreeBSD.org>

libpmc: Forgot regex.h

Reported by: ci
MFC after: 1 week
X-MFC-With: r354549


# a0ac5706 08-Nov-2019 Emmanuel Vadot <manu@FreeBSD.org>

libpmc: Match on the cpuid with a regex

The CPUID is, or can be, a regex to be matched.
Use regex from libc instead of strcmp

Tested-by: gallatin
MFC after: 1 week


# 86b5e013 15-Aug-2019 Matt Macy <mmacy@FreeBSD.org>

pmc: restore "unhalted-cycles" alias

Reported by: mav@


# dacc43df 04-Nov-2018 Matt Macy <mmacy@FreeBSD.org>

Add aditional counter descriptions to AMD 0x17

Submitted by: Somalapuram Amaranath
MFC after: 3 days
Differential Revision: https://reviews.freebsd.org/D17401


# 0204d85a 13-Sep-2018 Matt Macy <mmacy@FreeBSD.org>

hwpmc: set default rate if event description lacks one / filter rate against misuse

Not all event descriptions have a sample rate (such as inst_retired.any)
this will restore the legacy behavior of using 65536 in that case. It also
prevents accidental API misuse that could lead to panic.

PR: 230985
Reported by: markj
Reviewed by: markj
Approved by: re (gjb)
Sponsored by: Limelight Networks
Differential Revision: https://reviews.freebsd.org/D16958


# 81eb4dcf 13-Aug-2018 Matt Macy <mmacy@FreeBSD.org>

Add library and kernel support for AMD Family 17h counters

NB: lacks default sample rate for most counters


# 1b000b50 06-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: simplify lookup by idx


# b2ca2e50 05-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

hwpmc: add summary command and further metadata extensions

metadata changes:
- log pmc sample rate with pmcallocate
- log proc flags with thread / process logging
to identify user vs kernel threads

fixes:
- use log cpuid to translate event id to event name

Implement rudimentary summary command to track sample
counts by thread and process name within a pmc log.

% make -j4 buildkernel >& /dev/null &
% sudo pmcstat -S unhalted_core_cycles -S llc-misses -O foo sleep 15
% pmc summary foo
cpu_clk_unhalted.thread_p_any:
idle: 138108207162
clang-6.0: 105336158004
sh: 72340108510
make: 8642012963
kernel: 7754011631
longest_lat_cache.miss:
clang-6.0: 87502625
sh: 40901227
make: 5500165
kernel: 3300099
awk: 2000060

% pmc summary -f ~/foo
idx: 278 name: cpu_clk_unhalted.thread_p_any rate: 2000003
idle: 69054
clang-6.0: 52668
sh: 36170
make: 4321
kernel: 3877
hwpmc: proc(7445): 3319
awk: 1289
xargs: 357
rand_harvestq: 181
mtree: 102
intr: 53
zfskern: 31
usb: 7
pagedaemon: 4
ntpd: 3
syslogd: 1
acpi_thermal: 1
logger: 1
syncer: 1
snmptrapd: 1
sleep: 1
idx: 17 name: longest_lat_cache.miss rate: 100003
clang-6.0: 875
sh: 409
make: 55
kernel: 33
awk: 20
hwpmc: proc(7445): 14
xargs: 9
idle: 8
intr: 3
zfskern: 2


# ee62e0b4 04-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc / stat: use westmere supported counter


# ce6fc9d7 03-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

update pmc_pmu_idx_get_by_event signature for !x86


# bfb46e2b 03-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

pmc: add filter command

pmc filter allows the user to select event types, threads, and processes from
a sample.

% pmcstat -S unhalted_core_cycles -S llc-misses -S -S resource_stalls.any -O pmc.log
% pmc filter -e llc-misses pmc.log pmc-llc-misses.log
% pmc filter -e unhalted_core_cycles -t 100339 pmc.log pmc-core-cycles.log
etc...
% pmcstat -R pmc-core-cycles.log -G pmc-core-cycles.stacks


# 07d80fd8 03-Jun-2018 Matt Macy <mmacy@FreeBSD.org>

hwpmc: ABI fixes
- increase pmc cpuid field from 8 to 12 bits
- add cpuid version string to initialize entry in the log
so that filter can identify which counter index an
event name maps to
- GC unused config flags
- make fixed counter assignment more robust as well as the
changes needed to be properly identified for filter


# fbf962e6 31-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: allow substring for list and add function for printing event details


# 96cbd26a 31-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc/pmu: update aliases table


# 7d1c2b74 31-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc/pmu: enable for i386 as well


# 785dd70d 31-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: add pmu support for uncore events


# 8b20f975 29-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: bring pmu_util closer in line with style(9)


# 8bed125d 29-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: silence scan-build warnings


# c53c63f6 29-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: remove fixed counter diagnostic


# a191ed2d 28-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: add support for using fixed function counters


# 78beed2f 28-May-2018 Matt Macy <mmacy@FreeBSD.org>

libpmc: export names of counters for stat mode, make get_by_idx name consistent with others


# 959826ca 26-May-2018 Matt Macy <mmacy@FreeBSD.org>

pmc(3)/hwpmc(4): update supported Intel processors to rely fully on the
vendor provided pmu-events tables and sundry cleanups.

The vendor pmu-events tables provide counter descriptions, default
sample rates, event, umask, and flag values for all the counter
configuration permutations. Using this gives us:

- much simpler kernel code for the MD component
- helpful long and short event descriptions
- simpler user code
- sample rates that won't overload the system

Update man page with newer sample types and remove unused sample type.


# 5506ceb8 26-May-2018 Matt Macy <mmacy@FreeBSD.org>

Revert r334242 "pmc(3)/hwpmc(4): update supported Intel processors to rely fully on the"
because of squash commit messages


# 49281356 26-May-2018 Matt Macy <mmacy@FreeBSD.org>

pmc(3)/hwpmc(4): update supported Intel processors to rely fully on the
vendor provided pmu-events tables and sundry cleanups.

The vendor pmu-events tables provide counter descriptions, default
sample rates, event, umask, and flag values for all the counter
configuration permutations. Using this gives us:

- much simpler kernel code for the MD component
- helpful long and short event descriptions
- simpler user code
- sample rates that won't overload the system

Update man page with newer sample types and remove unused sample type.

Squashed commit of the following:

commit 4459d43eff815bec08ccc5533dbe5de846f03128
Author: Matt Macy <mmacy@mattmacy.io>
Date: Sat May 26 00:06:31 2018 -0700

libpmc: fix pmu function signatures for non amd64

commit a2cb8bbc586c65d41f9b291430a2261ec67b59fe
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 22:38:11 2018 -0700

pmcstat: fix indentation of usage

commit f686954b15ff56a833ac80404898977cb80a265b
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 22:19:49 2018 -0700

pmclog(3): add callchain and pmcallocatedyn, remove pcsample

commit 73e13a0d2e9498c81c150d14d022050cee7511bb
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 22:19:00 2018 -0700

pmclog.h: GC pcsample field

commit 3e93ffd65da641fa657539dad3c48e281f8b5798
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 22:05:57 2018 -0700

hwpmc: make Intel core CPUs use external event tables

commit 634f5fae1e1644ac324003136c66cd9c619d1c93
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 22:00:06 2018 -0700

pmclog: update log record types, bump PMC_MAJOR
- explicitly make log record types a multiple of 8 bytes
- hook in pmu event types for pmc_allocate records
- remove references to no longer PCSAMPLE record

commit 83d84fcd2d65bdf6ddcb2e155a22f0cfa2a9c225
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 21:52:10 2018 -0700

libpmc: add support for having vendor table driven pmc_allocate

commit 9e6ad63c40c2fce8404847ace5078ca6cb33a736
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 19:11:33 2018 -0700

hwpmc_core: add accessors for EVSEL & UMASK, make IAP_UMASK useful to user

commit 859dceb93daa6419a48c794db99b6758e5b041c9
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 19:09:45 2018 -0700

pmcstat: update usage and man page as well as make -L consistent with pmccontrol

commit 79c7d8597e28c2eb13f5f9113e65ec2792ca57b1
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 18:07:03 2018 -0700

pmu_util: add support for all current intel event keywords

commit d8089c7f6a6c8527f38324252b1ffb47004694c6
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 17:45:00 2018 -0700

add description for new arguments

commit 058336740bab53c62ec88a3a026ea848cf3878c6
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 17:38:15 2018 -0700

libpmc: move pmu_events table and pmu_utils out of libpmcstat so that they can be used by pmc_allocate

commit 049b66b382e2f833c3f47bc8df9e750cb265709f
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 16:12:41 2018 -0700

pmcstat: hook pmu_events counter description utility routines in

commit f5e01e7b37a691dc045e1aa16b3ebdd162515de8
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 16:11:59 2018 -0700

pmu_events: add utility routines for listing counters and their descriptions

commit cba4d4f8907f772279f86f18f915e0d74d33ac56
Author: Matt Macy <mmacy@mattmacy.io>
Date: Fri May 25 16:09:50 2018 -0700

pmu-events: expand out skylake regex to simplify string matches