LIBPFM(3)
β¨ NAME β¨
pfm_get_event_encoding β get raw event encoding
π¦ SYNOPSIS π¦
#include <perfmon/pfmlib.h>
int pfm_get_event_encoding(const char *str,int dfl_plm, char **fstr, int *idx, uint64_t *code, int *count);
π DESCRIPTION π
okay bestie, so this little function is your girl for turning an event string into its raw event encoding. You hand it str and it hands you back the encoding, periodt.
Important vibe check tho: one event per call. That's it. So str can hold exactly one symbolic event name β no sneaking a second one in there. It can contain unit masks and modifiers though, so dress it up however you like π. The default privilege level mask goes in dfl_plm, and whether it actually gets used depends on the event, so don't @ it.
β οΈ This function is DEPRECATED. It's been superseded bypfm_get_os_event_encoding(), where you set the OS toPFM_OS_NONE. Encoding gets retrieved through thepfm_pmu_encode_arg_tstructure. We love a glow-up π
Here's the transition, so you can see the before and after. This:
int i, count = 0;
uint64_t *codes;
ret = pfm_get_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, NULL, &codes, &count);
if (ret != PFM_SUCCESS)
err(1", cannot get encoding %s", pfm_strerror(ret));
for(i=0; i < count; i++)
printf("count[%d]=0x%"PRIx64"\n", i, codes[i]);
is equivalent to:
pfm_pmu_encode_arg_t arg;
int i;
memset(&arg, 0, sizeof(arg));
arg.size = sizeof(arg);
ret = pfm_get_os_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, PFM_OS_NONE, &arg);
if (ret != PFM_SUCCESS)
err(1", cannot get encoding %s", pfm_strerror(ret));
for(i=0; i < arg.count; i++)
printf("count[%d]=0x%"PRIx64"\n", i, arg.codes[i]);
free(arg.codes);
See? Same energy, new outfit β¨
Now the tea on the encoding itself: it may take several 64-bit integers. The function can use the array you pass in code if the number of entries you passed in count is big enough. But here's the thing β if both *codes is NULL and count is 0, the function allocates the memory it needs to store the encoding all by itself, and it is up to you, the caller to eventually free that memory. No ghosting your mallocs, bestie π«Ά
The number of 64-bit entries in codes gets reflected in *count upon return, regardless of whether codes was allocated for you or used as-is. And if that number is greater than one, then the order in which each component comes back is PMU-model specific β so go read the PMU specific man page, iykyk.
The "raw encoding" means the encoding as mandated by the underlying PMU model. Lowkey it may not be directly suitable to pass to a kernel API π, so you might want to use API-specific library calls to make sure the correct encoding is what actually gets passed. Slay responsibly.
If fstr is not NULL, it will point to the fully qualified event string upon successful return. That string contains the event name, any umask set, and the value of all the modifiers. Basically it reflects what the encoding will actually measure β receipts, fr fr. The function allocates the memory for that string, so the caller must eventually free it too.
Here's an example of how this function could be used:
#include <inttypes.h>
#include <err.h>
#include <perfmon/pfmlib.h>
int main(int argc, char **argv)
{
uint64_t *codes 0;
int count = 0;
int ret;
ret = pfm_initialize();
if (ret != PFMLIB_SUCCESS)
err(1", cannot initialize library %s", pfm_strerror(ret));
ret = pfm_get_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, NULL, &codes, &count);
if (ret != PFM_SUCCESS)
err(1", cannot get encoding %s", pfm_strerror(ret));
for(i=0; i < count; i++)
printf("count[%d]=0x%"PRIx64"\n", i, codes[i]);
free(codes);
return 0;
}
π RETURN π
The function returns the encoding of the event in *codes, and the number of 64-bit integers needed to support that encoding in *count. Upon success, PFM_SUCCESS is returned β otherwise you get a specific error code back, and it's giving π
π ERRORS π
PFM_ERR_TOOSMALLβ thecodeargument is too small for the encoding. Not enough room, bestie, she's overflowing πPFM_ERR_INVALβ thecodeorcountargument isNULL, orstrcontains more than one symbolic event. One event per call, I said what I said πPFM_ERR_NOMEMβ not enough memory. The allocation said no βPFM_ERR_NOTFOUNDβ event not found. She's not in the building πPFM_ERR_ATTRβ invalid event attribute (unit mask or modifier)PFM_ERR_ATTR_VALβ invalid modifier valuePFM_ERR_ATTR_SETβ attribute already set, cannot be changed. Locked in, no take-backs πPFM_ERR_ATTR_UMASKβ missing unit maskPFM_ERR_ATTR_FEATCOMBβ unit masks or features cannot be combined into a single event. These two are not compatible, and that's final π
π AUTHOR π
Stephane Eranian <eranian@gmail.com>
πΈ SEE ALSO πΈ
and that's the whole page, babe β this one's from the Linux Programmer's Manual, LIBPFM(3), dated September, 2009 π¦ thanks for reading, ily, go free your memory π
π¦ where this page comes from
- arch rolling: libpfm 4.14.1-1 Β· MIT
- centos-stream 10: libpfm-devel 4.14.1-1.el10 Β· MIT
- centos-stream 9: libpfm-devel 4.14.1-1.el9 Β· MIT
- centos-stream 9: libpfm-devel 4.14.1-1.el9 Β· MIT
- debian bookworm: libpfm4-dev 4.13.0-1 Β· MIT
- debian trixie: libpfm4-dev 4.13.0+git99-gc5587f9-1 Β· MIT
- fedora 43: libpfm-devel 4.13.0-16.fc43 Β· MIT
- fedora 43: libpfm-devel 4.13.0-16.fc43 Β· MIT
- fedora 43: libpfm-devel 4.13.0-20.fc43
- fedora 43: libpfm-devel 4.13.0-20.fc43
- fedora 43: libpfm-devel 4.14.1-2.fc43 Β· MIT
- fedora 43: libpfm-devel 4.14.1-2.fc43 Β· MIT
- fedora 44: libpfm-devel 4.13.0-19.fc44 Β· MIT
- fedora 44: libpfm-devel 4.13.0-19.fc44 Β· MIT
- fedora 44: libpfm-devel 4.13.0-20.fc44
- fedora 44: libpfm-devel 4.13.0-20.fc44
- fedora 44: libpfm-devel 4.14.1-2.fc44 Β· MIT
- fedora 44: libpfm-devel 4.14.1-2.fc44 Β· MIT
- nixos 26.05: libpfm 4.13.0
- nixos 26.05: papi 7.2.0
- nixpkgs unstable: libpfm 4.14.1
- nixpkgs unstable: papi 7.2.0
- opensuse-leap 16.0: libpfm-devel 4.13.0-160000.3.1 Β· MIT
- opensuse-leap 16.0: libpfm-devel 4.13.0-160000.3.1 Β· MIT
- opensuse-leap 16.0: libpfm-devel 4.13.0-160000.3.1 Β· MIT
- opensuse-leap 16.0: libpfm-devel 4.13.0-160000.3.1 Β· MIT
- ubuntu jammy: libpfm4-dev 4.11.1+git32-gd0b85fb-1 Β· BSD-like
- ubuntu jammy: libpfm4-dev 4.11.1+git32-gd0b85fb-1ubuntu0.1 Β· BSD-like
- ubuntu noble: libpfm4-dev 4.13.0+git32-g0d4ed0e-1 Β· MIT
- ubuntu noble: libpfm4-dev 4.13.0+git32-g0d4ed0e-1ubuntu0.1 Β· MIT
- ubuntu questing: libpfm4-dev 4.13.0+git99-gc5587f9-1
- ubuntu resolute: libpfm4-dev 4.13.0+git106-g3e4031b-1 Β· MIT
- ubuntu resolute: libpfm4-dev 4.13.0+git106-g3e4031b-1ubuntu0.1 Β· MIT
- ubuntu stonking: libpfm4-dev 4.13.0+git106-g3e4031b-1ubuntu1 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms