Performance Counters for Linux------------------------------Performance counters are special hardware registers available on most modernCPUs. These registers count the number of certain types of hw events: suchas instructions executed, cachemisses suffered, or branches mis-predicted -without slowing down the kernel or applications. These registers can alsotrigger interrupts when a threshold number of events have passed - and canthus be used to profile the code that runs on that CPU.The Linux Performance Counter subsystem provides an abstraction of thesehardware capabilities. It provides per task and per CPU counters, countergroups, and it provides event capabilities on top of those. Itprovides "virtual" 64-bit counters, regardless of the width of theunderlying hardware counters.Performance counters are accessed via special file descriptors.There's one file descriptor per virtual counter used.The special file descriptor is opened via the sys_perf_event_open()system call:int sys_perf_event_open(struct perf_event_attr *hw_event_uptr,pid_t pid, int cpu, int group_fd,unsigned long flags);The syscall returns the new fd. The fd can be used via the normalVFS system calls: read() can be used to read the counter, fcntl()can be used to set the blocking mode, etc.Multiple counters can be kept open at a time, and the counterscan be poll()ed.When creating a new counter fd, 'perf_event_attr' is:struct perf_event_attr {/** The MSB of the config word signifies if the rest contains cpu* specific (raw) counter configuration data, if unset, the next* 7 bits are an event type and the rest of the bits are the event* identifier.*/__u64 config;__u64 irq_period;__u32 record_type;__u32 read_format;__u64 disabled : 1, /* off by default */inherit : 1, /* children inherit it */pinned : 1, /* must always be on PMU */exclusive : 1, /* only group on PMU */exclude_user : 1, /* don't count user */exclude_kernel : 1, /* ditto kernel */exclude_hv : 1, /* ditto hypervisor */exclude_idle : 1, /* don't count when idle */mmap : 1, /* include mmap data */munmap : 1, /* include munmap data */comm : 1, /* include comm data */__reserved_1 : 52;__u32 extra_config_len;__u32 wakeup_events; /* wakeup every n events */__u64 __reserved_2;__u64 __reserved_3;};The 'config' field specifies what the counter should count. Itis divided into 3 bit-fields:raw_type: 1 bit (most significant bit) 0x8000_0000_0000_0000type: 7 bits (next most significant) 0x7f00_0000_0000_0000event_id: 56 bits (least significant) 0x00ff_ffff_ffff_ffffIf 'raw_type' is 1, then the counter will count a hardware eventspecified by the remaining 63 bits of event_config. The encoding ismachine-specific.If 'raw_type' is 0, then the 'type' field says what kind of counterthis is, with the following encoding:enum perf_type_id {PERF_TYPE_HARDWARE = 0,PERF_TYPE_SOFTWARE = 1,PERF_TYPE_TRACEPOINT = 2,};A counter of PERF_TYPE_HARDWARE will count the hardware eventspecified by 'event_id':/** Generalized performance counter event types, used by the hw_event.event_id* parameter of the sys_perf_event_open() syscall:*/enum perf_hw_id {/** Common hardware events, generalized by the kernel:*/PERF_COUNT_HW_CPU_CYCLES = 0,PERF_COUNT_HW_INSTRUCTIONS = 1,PERF_COUNT_HW_CACHE_REFERENCES = 2,PERF_COUNT_HW_CACHE_MISSES = 3,PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,PERF_COUNT_HW_BRANCH_MISSES = 5,PERF_COUNT_HW_BUS_CYCLES = 6,};These are standardized types of events that work relatively uniformlyon all CPUs that implement Performance Counters support under Linux,although there may be variations (e.g., different CPUs might countcache references and misses at different levels of the cache hierarchy).If a CPU is not able to count the selected event, then the system callwill return -EINVAL.More hw_event_types are supported as well, but they are CPU-specificand accessed as raw events. For example, to count "External buscycles while bus lock signal asserted" events on Intel Core CPUs, passin a 0x4064 event_id value and set hw_event.raw_type to 1.A counter of type PERF_TYPE_SOFTWARE will count one of the availablesoftware events, selected by 'event_id':/** Special "software" counters provided by the kernel, even if the hardware* does not support performance counters. These counters measure various* physical and sw events of the kernel (and allow the profiling of them as* well):*/enum perf_sw_ids {PERF_COUNT_SW_CPU_CLOCK = 0,PERF_COUNT_SW_TASK_CLOCK = 1,PERF_COUNT_SW_PAGE_FAULTS = 2,PERF_COUNT_SW_CONTEXT_SWITCHES = 3,PERF_COUNT_SW_CPU_MIGRATIONS = 4,PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,PERF_COUNT_SW_EMULATION_FAULTS = 8,};Counters of the type PERF_TYPE_TRACEPOINT are available when the ftrace eventtracer is available, and event_id values can be obtained from/debug/tracing/events/*/*/idCounters come in two flavours: counting counters and samplingcounters. A "counting" counter is one that is used for counting thenumber of events that occur, and is characterised by havingirq_period = 0.A read() on a counter returns the current value of the counter and possibleadditional values as specified by 'read_format', each value is a u64 (8 bytes)in size./** Bits that can be set in hw_event.read_format to request that* reads on the counter should return the indicated quantities,* in increasing order of bit value, after the counter value.*/enum perf_event_read_format {PERF_FORMAT_TOTAL_TIME_ENABLED = 1,PERF_FORMAT_TOTAL_TIME_RUNNING = 2,};Using these additional values one can establish the overcommit ratio for aparticular counter allowing one to take the round-robin scheduling effectinto account.A "sampling" counter is one that is set up to generate an interruptevery N events, where N is given by 'irq_period'. A sampling counterhas irq_period > 0. The record_type controls what data is recorded on eachinterrupt:/** Bits that can be set in hw_event.record_type to request information* in the overflow packets.*/enum perf_event_record_format {PERF_RECORD_IP = 1U << 0,PERF_RECORD_TID = 1U << 1,PERF_RECORD_TIME = 1U << 2,PERF_RECORD_ADDR = 1U << 3,PERF_RECORD_GROUP = 1U << 4,PERF_RECORD_CALLCHAIN = 1U << 5,};Such (and other) events will be recorded in a ring-buffer, which isavailable to user-space using mmap() (see below).The 'disabled' bit specifies whether the counter starts out disabledor enabled. If it is initially disabled, it can be enabled by ioctlor prctl (see below).The 'inherit' bit, if set, specifies that this counter should countevents on descendant tasks as well as the task specified. This onlyapplies to new descendents, not to any existing descendents at thetime the counter is created (nor to any new descendents of existingdescendents).The 'pinned' bit, if set, specifies that the counter should always beon the CPU if at all possible. It only applies to hardware countersand only to group leaders. If a pinned counter cannot be put onto theCPU (e.g. because there are not enough hardware counters or because ofa conflict with some other event), then the counter goes into an'error' state, where reads return end-of-file (i.e. read() returns 0)until the counter is subsequently enabled or disabled.The 'exclusive' bit, if set, specifies that when this counter's groupis on the CPU, it should be the only group using the CPU's counters.In future, this will allow sophisticated monitoring programs to supplyextra configuration information via 'extra_config_len' to exploitadvanced features of the CPU's Performance Monitor Unit (PMU) that arenot otherwise accessible and that might disrupt other hardwarecounters.The 'exclude_user', 'exclude_kernel' and 'exclude_hv' bits provide away to request that counting of events be restricted to times when theCPU is in user, kernel and/or hypervisor mode.The 'mmap' and 'munmap' bits allow recording of PROT_EXEC mmap/munmapoperations, these can be used to relate userspace IP addresses to actualcode, even after the mapping (or even the whole process) is gone,these events are recorded in the ring-buffer (see below).The 'comm' bit allows tracking of process comm data on process creation.This too is recorded in the ring-buffer (see below).The 'pid' parameter to the sys_perf_event_open() system call allows thecounter to be specific to a task:pid == 0: if the pid parameter is zero, the counter is attached to thecurrent task.pid > 0: the counter is attached to a specific task (if the current taskhas sufficient privilege to do so)pid < 0: all tasks are counted (per cpu counters)The 'cpu' parameter allows a counter to be made specific to a CPU:cpu >= 0: the counter is restricted to a specific CPUcpu == -1: the counter counts on all CPUs(Note: the combination of 'pid == -1' and 'cpu == -1' is not valid.)A 'pid > 0' and 'cpu == -1' counter is a per task counter that countsevents of that task and 'follows' that task to whatever CPU the taskgets schedule to. Per task counters can be created by any user, fortheir own tasks.A 'pid == -1' and 'cpu == x' counter is a per CPU counter that countsall events on CPU-x. Per CPU counters need CAP_SYS_ADMIN privilege.The 'flags' parameter is currently unused and must be zero.The 'group_fd' parameter allows counter "groups" to be set up. Acounter group has one counter which is the group "leader". The leaderis created first, with group_fd = -1 in the sys_perf_event_open callthat creates it. The rest of the group members are createdsubsequently, with group_fd giving the fd of the group leader.(A single counter on its own is created with group_fd = -1 and isconsidered to be a group with only 1 member.)A counter group is scheduled onto the CPU as a unit, that is, it willonly be put onto the CPU if all of the counters in the group can beput onto the CPU. This means that the values of the member counterscan be meaningfully compared, added, divided (to get ratios), etc.,with each other, since they have counted events for the same set ofexecuted instructions.Like stated, asynchronous events, like counter overflow or PROT_EXEC mmaptracking are logged into a ring-buffer. This ring-buffer is created andaccessed through mmap().The mmap size should be 1+2^n pages, where the first page is a meta-data page(struct perf_event_mmap_page) that contains various bits of information suchas where the ring-buffer head is./** Structure of the page that can be mapped via mmap*/struct perf_event_mmap_page {__u32 version; /* version number of this structure */__u32 compat_version; /* lowest version this is compat with *//** Bits needed to read the hw counters in user-space.** u32 seq;* s64 count;** do {* seq = pc->lock;** barrier()* if (pc->index) {* count = pmc_read(pc->index - 1);* count += pc->offset;* } else* goto regular_read;** barrier();* } while (pc->lock != seq);** NOTE: for obvious reason this only works on self-monitoring* processes.*/__u32 lock; /* seqlock for synchronization */__u32 index; /* hardware counter identifier */__s64 offset; /* add to hardware counter value *//** Control data for the mmap() data buffer.** User-space reading this value should issue an rmb(), on SMP capable* platforms, after reading this value -- see perf_event_wakeup().*/__u32 data_head; /* head in the data section */};NOTE: the hw-counter userspace bits are arch specific and are currently onlyimplemented on powerpc.The following 2^n pages are the ring-buffer which contains events of the form:#define PERF_RECORD_MISC_KERNEL (1 << 0)#define PERF_RECORD_MISC_USER (1 << 1)#define PERF_RECORD_MISC_OVERFLOW (1 << 2)struct perf_event_header {__u32 type;__u16 misc;__u16 size;};enum perf_event_type {/** The MMAP events record the PROT_EXEC mappings so that we can* correlate userspace IPs to code. They have the following structure:** struct {* struct perf_event_header header;** u32 pid, tid;* u64 addr;* u64 len;* u64 pgoff;* char filename[];* };*/PERF_RECORD_MMAP = 1,PERF_RECORD_MUNMAP = 2,/** struct {* struct perf_event_header header;** u32 pid, tid;* char comm[];* };*/PERF_RECORD_COMM = 3,/** When header.misc & PERF_RECORD_MISC_OVERFLOW the event_type field* will be PERF_RECORD_*** struct {* struct perf_event_header header;** { u64 ip; } && PERF_RECORD_IP* { u32 pid, tid; } && PERF_RECORD_TID* { u64 time; } && PERF_RECORD_TIME* { u64 addr; } && PERF_RECORD_ADDR** { u64 nr;* { u64 event, val; } cnt[nr]; } && PERF_RECORD_GROUP** { u16 nr,* hv,* kernel,* user;* u64 ips[nr]; } && PERF_RECORD_CALLCHAIN* };*/};NOTE: PERF_RECORD_CALLCHAIN is arch specific and currently only implementedon x86.Notification of new events is possible through poll()/select()/epoll() andfcntl() managing signals.Normally a notification is generated for every page filled, however one canadditionally set perf_event_attr.wakeup_events to generate one everyso many counter overflow events.Future work will include a splice() interface to the ring-buffer.Counters can be enabled and disabled in two ways: via ioctl and viaprctl. When a counter is disabled, it doesn't count or generateevents but does continue to exist and maintain its count value.An individual counter can be enabled withioctl(fd, PERF_EVENT_IOC_ENABLE, 0);or disabled withioctl(fd, PERF_EVENT_IOC_DISABLE, 0);For a counter group, pass PERF_IOC_FLAG_GROUP as the third argument.Enabling or disabling the leader of a group enables or disables thewhole group; that is, while the group leader is disabled, none of thecounters in the group will count. Enabling or disabling a member of agroup other than the leader only affects that counter - disabling annon-leader stops that counter from counting but doesn't affect anyother counter.Additionally, non-inherited overflow counters can useioctl(fd, PERF_EVENT_IOC_REFRESH, nr);to enable a counter for 'nr' events, after which it gets disabled again.A process can enable or disable all the counter groups that areattached to it, using prctl:prctl(PR_TASK_PERF_EVENTS_ENABLE);prctl(PR_TASK_PERF_EVENTS_DISABLE);This applies to all counters on the current process, whether createdby this process or by another, and doesn't affect any counters thatthis process has created on other processes. It only enables ordisables the group leaders, not any other members in the groups.Arch requirements-----------------If your architecture does not have hardware performance metrics, you canstill use the generic software counters based on hrtimers for sampling.So to start with, in order to add HAVE_PERF_EVENTS to your Kconfig, youwill need at least this:- asm/perf_event.h - a basic stub will suffice at first- support for atomic64 types (and associated helper functions)If your architecture does have hardware capabilities, you can override theweak stub hw_perf_event_init() to register hardware counters.Architectures that have d-cache aliassing issues, such as Sparc and ARM,should select PERF_USE_VMALLOC in order to avoid these for perf mmap().
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。