Categorygithub.com/elastic/go-perf
modulepackage
0.0.0-20241029065020-30bec95324b8
Repository: https://github.com/elastic/go-perf.git
Documentation: pkg.go.dev

# README


This is a clone of the golang.org/x/sys/unix/linux/perf submitted by acln0 to review at https://go-review.googlesource.com/c/sys/+/168059

An alternative working tree for this package can also be found at https://github.com/acln0/perf

This Elastic fork contains bugfixes and features necessary for our KProbes implementation.


perf API client package for Linux. See man 2 perf_event_open and include/uapi/linux/perf_event.h.

This package is in its early stages. The API is still under discussion: it may change at any moment, without prior notice. Furthermore, this document may not be completely up to date at all times.

Testing

Many of the things package perf does require elevated privileges on most systems. We would very much like for the tests to not require root to run. Because of this, we use a fairly specific testing model, described next.

If the host kernel does not support perf_event_open(2) (i.e. if the /proc/sys/kernel/perf_event_paranoid file is not present), then tests fail immediately with an error message.

Tests are designed in such a way that they are skipped if their requirements are not met by the underlying system. We would like the test suite to degrade gracefully, under certain circumstances.

For example, when running Linux in a virtualized environment, various hardware PMUs might not be available. In such situations, we would like the test suite to continue running. For this purpose, we introduce the mechanism described next.

Requirements for a test are specified by invoking the requires function, at the beginning of a test function. All tests that call perf_event_open must specify requirements this way. Currently, we use three kinds of requirements:

  • perf_event_paranoid values

  • the existence of various PMUs (e.g. "cpu", "software", "tracepoint")

  • tracefs is mounted, and readable

Today, setting perf_event_paranoid=1 and having a readable tracefs mounted at /sys/kernel/debug/tracing enables most of the tests. A select few require perf_event_paranoid=0. If the test process is running with CAP_SYS_ADMIN, perf_event_paranoid requirements are ignored, since they are considered fulfilled. The test process does not attempt to see if it is running as root, it only checks CAP_SYS_ADMIN.

If you find a test that, when ran without elevated privileges, fails with something akin to a permissions error, then it means the requirements for the test were not specified precisely. Please file a bug. Extending the test suite and making these requirements more precise is an ongoing process.

# Functions

AllCacheOpResults returns a slice of all known cache operation results.
AllCacheOps returns a slice of all known cache operations.
AllCaches returns a slice of all known cache types.
AllHardwareCounters returns a slice of all known hardware counters.
AllSoftwareCounters returns a slice of all known software counters.
Breakpoint returns a Configurator for a breakpoint event.
ExecutionBreakpoint returns a Configurator for an execution breakpoint at the specified address.
ExecutionBreakpointLength returns the length of an execution breakpoint.
HardwareCacheCounters returns cache counters which measure the cartesian product of the specified caches, operations and results.
LookupEventType probes /sys/bus/event_source/devices/<device>/type for the EventType value associated with the specified PMU.
LookupTracepointConfig probes /sys/kernel/debug/tracing/events/<category>/<event>/id for the Attr.Config value associated with the specified category and event.
MaxStack returns the maximum number of frame pointers in a recorded callchain.
Open opens the event configured by attr.
OpenCGroup is like Open, but activates per-container system-wide monitoring.
OpenWithFlags is like Open but allows to specify additional flags to be passed to perf_event_open(2).
Supported returns a boolean indicating whether the host kernel supports the perf_event_open system call, which is a prerequisite for the operations of this package.
Tracepoint returns a Configurator for the specified category and event.

# Constants

Cache operation results.
Software performance counters.
AllThreads configures the event to measure all threads on the specified CPU.
AnyCPU configures the specified process/thread to be measured on any CPU.
sample collided with another.
snapshot from overwrite mode.
record contains gaps.
record was truncated to fit.
Software performance counters.
Caches.
Hardware performance counters.
Hardware performance counters.
Branch sample privilege values.
Branch sample privilege values.
Branch sample privilege values.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch sample bits.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Branch classifications.
Supported event types.
Breakpoint length values.
Breakpoint length values.
Breakpoint length values.
Breakpoint length values.
Breakpoint types.
Breakpoint types.
Breakpoint types.
Breakpoint types.
Breakpoint types.
Hardware performance counters.
Hardware performance counters.
Hardware performance counters.
CallingThread configures the event to measure the calling thread.
Supported Skid settings.
Software performance counters.
Software performance counters.
Hardware performance counters.
Software performance counters.
DefaultNumPages is the number of pages used by MapRing.
Caches.
Software performance counters.
Software performance counters.
Known CPU modes.
Known CPU modes.
Supported event types.
Supported event types.
Known CPU modes.
Hardware performance counters.
Caches.
Known CPU modes.
Caches.
Caches.
Caches.
Software performance counters.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevelNumber flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
MemLevel flag bits.
locked transaction.
not available.
MemOp flag bits.
MemOp flag bits.
MemOp flag bits.
MemOp flag bits.
MemOp flag bits.
MemRemote flag bits.
MemSnoopMode flag bits.
MemSnoopMode flag bits.
MemSnoopMode flag bits.
MemSnoopMode flag bits.
MemSnoopMode flag bits.
forward.
hit level.
MemTLB flag bits.
MemTLB flag bits.
miss level.
not available.
OS fault handler.
Hardware Walker.
Software performance counters.
Cache operation results.
Supported Skid settings.
Supported Skid settings.
Caches.
Software performance counters.
Cache operations.
Supported event types.
Cache operations.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Known record types.
Hardware performance counters.
Supported Skid settings.
Supported event types.
Hardware performance counters.
Hardware performance counters.
Software performance counters.
Supported event types.
TransactionAsync indicates an asynchronous abort (unrelated to the reported instruction).
TransactionConflict indicates an abort rue to memory conflicts with other threads.
Transaction Elision indicates an abort from an elision type transaction (Intel CPU specific).
TransactionGeneric indicates an abort from a generic transaction.
TransactionReadCapacity indicates an abort due to read capacity overflow.
TransactionRetryable indicates whether retrying the transaction may have succeeded.
TransactionSync indicates a synchronous abort (related to the reported instruction).
TransactionWriteCapacity indicates an abort due to write capacity overflow.
Known CPU modes.
Known CPU modes.
Cache operations.

# Variables

ErrBadRecord is returned by ReadRecord when a read record can't be decoded.
ErrDisabled is returned from ReadRecord and ReadRawRecord if the event being monitored is attached to a different process, and that process exits.
ErrNoReadRecord is returned by ReadRecord when it is disabled on a group event, due to different configurations of the leader and follower events.

# Structs

Attr configures a perf event.
AuxRecord (PERF_RECORD_AUX) reports that new data is available in the AUX buffer region.
BranchEntry is a sampled branch.
BranchSampleFormat specifies what branches to include in a branch record.
CommRecord (PERF_RECORD_COMM) indicates a change in the process name.
Count is a measurement taken by an Event.
CountFormat configures the format of Count or GroupCount measurements.
Event is an active perf event.
ExitRecord (PERF_RECORD_EXIT) indicates a process exit event.
ForkRecord (PERF_RECORD_FORK) indicates a fork event.
Group configures a group of events.
GroupCount is a group of measurements taken by an Event group.
A HardwareCacheCounter groups a cache, a cache operation, and an operation result.
ItraceStartRecord (PERF_RECORD_ITRACE_START) indicates which process has initiated an instruction trace event, allowing tools to correlate instruction addresses in the AUX buffer with the proper executable.
LostRecord (PERF_RECORD_LOST) indicates when events are lost.
LostSamplesRecord (PERF_RECORD_LOST_SAMPLES) indicates some number of samples that may have been lost, when using hardware sampling such as Intel PEBS.
Mmap2Record (PERF_RECORD_MMAP2) includes extended information on mmap(2) calls returning executable mappings.
MmapRecord (PERF_RECORD_MMAP) records PROT_EXEC mappings such that user-space IPs can be correlated to code.
NamespacesRecord (PERF_RECORD_NAMESPACES) describes the namespaces of a process when it is created.
Options contains low level event configuration options.
RawRecord is a raw overflow record, read from the memory mapped ring buffer associated with an Event.
ReadGroupRecord (PERF_RECORD_READ) indicates a read event on a group event.
ReadRecord (PERF_RECORD_READ) indicates a read event.
RecordHeader is the header present in every overflow record.
SampleFormat configures information requested in overflow packets.
SampleGroupRecord indicates a sample from an event group.
SampleID contains identifiers for when and where a record was collected.
SampleRecord indicates a sample.
SwitchCPUWideRecord (PERF_RECORD_SWITCH_CPU_WIDE) indicates a context switch, but only occurs when sampling in CPU-wide mode.
SwitchRecord (PERF_RECORD_SWITCH) indicates that a context switch has happened.
ThrottleRecord (PERF_RECORD_THROTTLE) indicates a throttle event.
UnthrottleRecord (PERF_RECORD_UNTHROTTLE) indicates an unthrottle event.

# Interfaces

A Configurator configures event attributes.
Record is the interface implemented by all record types.

# Type aliases

AuxFlag describes an update to a record in the AUX buffer region.
BranchSample specifies a type of branch to sample.
BranchSamplePrivilege speifies a branch sample privilege level.
BranchType classifies a BranchEntry.
BreakpointLength is the length of the breakpoint being measured.
BreakpointType is the type of a breakpoint.
Cache identifies a cache.
CacheOp is a cache operation.
CacheOpResult is the result of a cache operation.
CPUMode is a CPU operation mode.
DataSource records where in the memory hierarchy the data associated with a sampled instruction came from.
EventType is the overall type of a performance event.
HardwareCounter is a hardware performance counter.
MemLevel is a memory level.
MemLevelNumber is a memory level number.
MemLock is a memory locking mode.
MemOp is a memory operation.
MemRemote indicates whether remote memory was accessed.
MemSnoopMode is a memory snoop mode.
MemSnoopModeX is an extended memory snoop mode.
MemTLB is a TLB access mode.
RecordType is the type of an overflow record.
Skid is an instruction pointer skid constraint.
SoftwareCounter is a software performance counter.
Transaction describes a transactional memory abort.