PERFMON(4)

✨ PERFMON(4) ✨

okay bestie, this is a page from the i386 Kernel Interfaces Manual β€” yes, the i386 one, we're going vintage today πŸ’… β€” and it's all about perfmon, the CPU performance-monitoring interface. buckle up πŸŽ€


🌸 NAME 🌸

perfmon β€” the CPU performance-monitoring interface, i.e. the little door into your CPU's built-in "how hard am I working tho" hardware 🫢


✨ SYNOPSIS ✨

cpu I586_CPU
cpu I686_CPU
options PERFMON

like, you gotta have one of those CPUs and you gotta turn on that option. no option, no vibes. periodt.


πŸ’– DESCRIPTION πŸ’–

so the perfmon driver hands you the keys to the internal performance-monitoring capabilities of the Intel Pentium and Pentium Pro CPUs. these processors are lowkey flexing two internal counters that you can configure to measure a whole bunch of events, either as a count or as a duration (in CPU cycles, bestie, we're counting in cycles), plus there's a cycle counter that just counts clock cycles. and perfmon gives you a device-style interface to all of that. iconic. πŸ”₯

every single bit of access to the performance-monitoring counters goes through the special device file /dev/perfmon. this device supports a bunch of ioctl(2) requests, which are defined in <machine/perfmon.h> β€” and that header is also where the definitions of the various counters live, for both Pentium and Pentium Pro processors. πŸ¦‹

NOTA BENE: the set of available events differs from processor to processor. it is the responsibility of the programmer to make sure the event numbers used are the correct ones for the CPU type being measured. no cap, this one matters. ⚠️

the following ioctl(2) requests are defined:

  • PMIOSETUP β€” (struct pmc) set up a counter with the parameters and flags defined in the structure. the following fields are defined in struct pmc:
    • int pmc_num β€” the number of the counter in question; must be less than NPMC (currently 2).
    • u_char pmc_event β€” the particular event number to be monitored, as defined in <machine/perfmon.h>.
    • u_char pmc_unit β€” the unit mask value, specific to the event type (see the Intel documentation).
    • u_char pmc_flags β€” flags modifying the operation of the counter (see below).
    • u_char pmc_mask β€” the counter mask value; essentially, this is a threshold used to restrict the count to events lasting more (or less) than the specified number of clocks.

the following pmc_flags values are defined:

  • PMCF_USR β€” count events in user mode
  • PMCF_OS β€” count events in kernel mode
  • PMCF_E β€” count number of events rather than their duration
  • PMCF_INV β€” invert the sense of the counter mask comparison
  • PMIOGET β€” (struct pmc) returns the current configuration of the specified counter. like a lil vibe check on your counter's settings πŸ‘€
  • PMIOSTART
  • PMIOSTOP β€” (int) starts (stops) the specified counter. and here's the tea 🍡: due to hardware deficiencies, counters must be started and stopped in numerical order. (that is to say, counter 0 can never be stopped without first stopping counter 1.) the driver will not enforce this restriction (since it may not be present in future CPUs). so like, it's on you, bestie 😭
  • PMIORESET β€” (int) reset the specified counter to zero. the counter should be stopped with PMIOSTOP before it is reset. all counters are automatically reset by PMIOSETUP. 🧚
  • PMIOREAD β€” (struct pmc_data) get the current value of the counter. the pmc_data structure defines two fields:
    • int pmcd_num β€” the number of the counter to read
    • quad_t pmcd_value β€” the resulting value as a 64-bit signed integer

in the future, it may be possible to use the RDPMC instruction on Pentium Pro processors to read the counters directly. ✨

  • PMIOTSTAMP β€” (struct pmc_tstamp) read the time stamp counter. the pmc_tstamp structure defines two fields:
    • int pmct_rate β€” the approximate rate of the counter, in MHz
    • quad_t pmct_value β€” the current value of the counter as a 64-bit integer
it is important to note that the counter rate, as provided in the pmct_rate field, is often incorrect because of calibration difficulties and non-integral clock rates. this field should be considered more of a hint or sanity-check than an actual representation of the rate of clock ticks. so don't trust it with your life, she's just guessing πŸ’€

πŸŽ€ FILES πŸŽ€

  • /dev/perfmon β€” character device interface to counters
  • /usr/include/machine/perfmon.h β€” include file with definitions of structures and event types
  • /usr/share/examples/perfmon β€” sample source code demonstrating use of all the ioctl() commands

πŸ¦‹ SEE ALSO πŸ¦‹

  • ioctl(2), hwpmc(4)
  • Intel Corporation, Pentium Pro Family Developer's Manual, vol. 3, January 1996, Operating System Writer's Manual.

πŸ’œ HISTORY πŸ’œ

the perfmon device first appeared in FreeBSD 2.2. a true elder icon πŸ‘‘


🌈 AUTHORS 🌈

the perfmon driver was written by Garrett A. Wollman, MIT Laboratory for Computer Science. we love a legend 🫢


and that's the whole page, bestie β€” Debian, March 26, 1996, the i386 Kernel Interfaces Manual era of it all. mwah πŸ’…πŸ’–

πŸ“¦ where this page comes from