STRUCT PERF_SAMPLE(9)
π struct perf_sample(9) β the Kernel API Manual, but make it make sense π
okay bestie, so this page is from the Kernel API Manual and it's literally all about struct perf_sample β the little glow-up container that holds one perf sample so you don't have to rawdog the variable-length event yourself. Let's get into it β¨
β¨ NAME β¨
struct perf_sample β the struct that says "I got you" for one perf sample event π
(the original name section is literally just the struct name with a dash, no cap, that's it)
π«Ά SYNOPSIS π«Ά
here's the whole struct, character for character, because bestie we do not paraphrase code:
struct perf_sample {
struct evsel *evsel;
u64 ip;
u32 pid;
u32 tid;
u64 time;
u64 addr;
u64 id;
u64 stream_id;
u64 period;
u64 weight;
u64 transaction;
u64 insn_cnt;
u64 cyc_cnt;
u32 cpu;
u32 raw_size;
u64 data_src;
u64 phys_addr;
u64 data_page_size;
u64 code_page_size;
u64 cgroup;
u64 file_offset;
u32 flags;
u32 machine_pid;
u32 vcpu;
u16 insn_len;
u16 misc;
u16 ins_lat;
u16 weight3;
u8 cpumode;
bool no_hw_idx;
bool deferred_callchain;
bool merged_callchain;
u64 deferred_cookie;
char insn[MAX_INSN];
void *raw_data;
struct ip_callchain *callchain;
struct branch_stack *branch_stack;
u64 *branch_stack_cntr;
struct regs_dump *user_regs;
struct regs_dump *intr_regs;
struct stack_dump user_stack;
struct sample_read read;
struct aux_sample aux_sample;
struct simd_flags simd_flags;
};
like, look at her. main character struct energy π
πΈ Members πΈ
okay so every single one of these fields, and what it actually means:
evselβ backward reference to the evsel that was used when constructing the sample. it's giving "who's your parent" energy.ipβ the sample eventPERF_SAMPLE_IPvalue. the instruction pointer, bestie.pidβ the sample eventPERF_SAMPLE_TIDpid value.tidβ the sample eventPERF_SAMPLE_TIDtid value.timeβ the sample eventPERF_SAMPLE_TIMEvalue.addrβ the sample eventPERF_SAMPLE_ADDRvalue.idβ the sample eventPERF_SAMPLE_IDorPERF_SAMPLE_IDENTIFIERvalue.stream_idβ the sample eventPERF_SAMPLE_STREAM_IDvalue.periodβ the sample eventPERF_SAMPLE_PERIODvalue.weightβ data determined byPERF_SAMPLE_WEIGHTorPERF_SAMPLE_WEIGHT_STRUCT. two sources, one field, she's versatile like that πtransactionβ the sample eventPERF_SAMPLE_TRANSACTIONvalue.insn_cntβ filled in and used by intel-pt.cyc_cntβ filled in and used by intel-pt. (yes, the intel-pt girlies get two fields, we love that for them)cpuβ the sample eventPERF_SAMPLE_CPUvalue.raw_sizeβ the size in bytes of raw data fromPERF_SAMPLE_RAW. β οΈ for alignment reasons this should always besizeof(u32)followed by a multiple ofsizeof(u64). like, always. don't fight the alignment, bestie.data_srcβ the sample eventPERF_SAMPLE_DATA_SRCvalue.phys_addrβ the sample eventPERF_SAMPLE_PHYS_ADDRvalue.data_page_sizeβ the sample eventPERF_SAMPLE_DATA_PAGE_SIZEvalue.code_page_sizeβ the sample eventPERF_SAMPLE_CODE_PAGE_SIZEvalue.cgroupβ the sample eventPERF_SAMPLE_CGROUPvalue.file_offsetβ byte offset of this event in the perf.data file. iykyk πflagsβ extra flag data from auxiliary events like intel-pt.machine_pidβ the guest machine pid, derived from the sample id.vcpuβ the guest machine vcpu, derived from the sample id.insn_lenβ instruction length from auxiliary events like intel-pt. the instruction itself is held ininsn. see, teamwork π«Άmiscβ the entirestruct perf_event_headermisc variable. the whole thing. she's not doing halves.ins_latβ instruction latency information fromweight2inPERF_SAMPLE_WEIGHT_STRUCT, or from auxiliary events like intel-pt.weight3β fromPERF_SAMPLE_WEIGHT_STRUCT. on x86 it holdsretire_lat, on powerpc it holdsp_stage_cyc. platform-dependent queen πcpumodeβ the cpumode from thestruct perf_event_headermisc variable, masked withCPUMODE_MASK. gives you user, kernel and hypervisor information. the full trio β¨no_hw_idxβ forPERF_SAMPLE_BRANCH_STACK, this istruewhenPERF_SAMPLE_BRANCH_HW_INDEXisn't set.deferred_callchainβ when processingPERF_SAMPLE_CALLCHAIN, a deferred user callchain marker was encountered.merged_callchainβ a synthesized merged callchain that is allocated and needs freeing. ποΈ yes, you gotta free her, don't leave her hanging.deferred_cookieβ identifier of the deferred callchain in the laterPERF_RECORD_CALLCHAIN_DEFERREDevent. a little cookie that tells you which one is which πͺinsnβ a copy of the sampled instruction, filled in byperf_sample__fetch_insn.raw_dataβ pointer into the original event forPERF_SAMPLE_RAWdata.callchainβ pointer into the original event forPERF_SAMPLE_CALLCHAINdata. β οΈ for deferred callchains this may be a copy that needs freeing β seesample__merge_deferred_callchain. don't leak her, bestie πbranch_stackβ pointer into the original event forPERF_SAMPLE_BRANCH_STACKdata.branch_stack_cntrβ pointer into the original event forPERF_SAMPLE_BRANCH_COUNTERSdata.user_regsβ values and pointers into the sample forPERF_SAMPLE_REGS_USER.intr_regsβ values and pointers into the sample forPERF_SAMPLE_REGS_INTR.user_stackβ size and pointer into the sample forPERF_SAMPLE_STACK_USER.readβ the sample eventPERF_SAMPLE_READcounter values. the valid values depend on theattr.read_formatPERF_FORMAT_values. so like, read the format, girlies πaux_sampleβ similar to raw data but with a 64-bit size and alignment,PERF_SAMPLE_AUXdata. the deluxe version πsimd_flagsβ SIMD flag information from ARM SPE auxiliary events.
π₯ Description π₯
so here's the tea, bestie β
a sample is generally filled in by evlist__parse_sample/evsel__parse_sample, which fills in the variables from a "union perf_event *event" β and that event is data from a perf ring buffer or a perf.data file. fr fr.
the "event" sample is variable in length, as determined by the perf_event_attr (in the evsel) plus details within the sample event itself. and that's literally why struct perf_sample exists: so you don't have to care about the variable-length nature of the original event. it's giving "I'll handle the chaos, you just read the fields" π§
to avoid being excessively large, parts of struct perf_sample are pointers into the original sample event. so heads up: in general, the lifetime of a struct perf_sample needs to be less than the "union perf_event *event" it was derived from. > β οΈ like, do not let your struct outlive its source event, that's undefined-adjacent heartbreak π
the struct regs_dump user_regs and intr_regs are lazily allocated β again for size reasons β because they hold a cache of looked-up registers. the function pair perf_sample__init and perf_sample__exit correctly initialize and clean up these values. so like, use the pair. that's the whole vibe check β
π SEE ALSO π
- Kernel file
./tools/perf/util/sample.hβ go read it, it's right there π
and that's the page, bestie π go forth and sample responsibly β¨
cute little sign-off: this one lives at ./tools/perf/util, dated October 2026 π β and yes, the header said struct perf_sample(9) and the footer said struct perf_sample(9) too, because consistency is iconic π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms