|
| 1 | +#pragma once |
| 2 | +/* |
| 3 | + Keeping declaration for structs that are specific to linux |
| 4 | + structures added from |
| 5 | + <sys/epoll.h> |
| 6 | + <linux/perf_event.h> |
| 7 | + <linux/types.h> |
| 8 | +*/ |
| 9 | +union epoll_data { |
| 10 | + void *ptr; |
| 11 | + int fd; |
| 12 | + uint32_t u32; |
| 13 | + uint64_t u64; |
| 14 | + }; |
| 15 | + |
| 16 | +typedef union epoll_data epoll_data_t; |
| 17 | +struct epoll_event { |
| 18 | + uint32_t events; /* Epoll events */ |
| 19 | + epoll_data_t data; /* User data variable */ |
| 20 | + }; |
| 21 | +typedef uint64_t __u64; |
| 22 | +typedef uint64_t __aligned_u64; |
| 23 | +typedef uint32_t __u32; |
| 24 | +typedef uint16_t __u16; |
| 25 | +typedef uint8_t __u8; |
| 26 | + |
| 27 | +typedef int32_t __s32; |
| 28 | +typedef int64_t __s64; |
| 29 | + |
| 30 | +struct perf_event_header { |
| 31 | + __u32 type; |
| 32 | + __u16 misc; |
| 33 | + __u16 size; |
| 34 | + }; |
| 35 | + |
| 36 | +/* |
| 37 | +https://github.com/torvalds/linux/blob/f06ce441457d4abc4d76be7acba26868a2d02b1c/include/uapi/linux/perf_event.h#L571 |
| 38 | +*/ |
| 39 | +struct perf_event_mmap_page { |
| 40 | + |
| 41 | + __u32 version; /* version number of this structure */ |
| 42 | + __u32 compat_version; /* lowest version this is compat with */ |
| 43 | + |
| 44 | + /* |
| 45 | + * Bits needed to read the hw events in user-space. |
| 46 | + * |
| 47 | + * u32 seq, time_mult, time_shift, index, width; |
| 48 | + * u64 count, enabled, running; |
| 49 | + * u64 cyc, time_offset; |
| 50 | + * s64 pmc = 0; |
| 51 | + * |
| 52 | + * do { |
| 53 | + * seq = pc->lock; |
| 54 | + * barrier() |
| 55 | + * |
| 56 | + * enabled = pc->time_enabled; |
| 57 | + * running = pc->time_running; |
| 58 | + * |
| 59 | + * if (pc->cap_usr_time && enabled != running) { |
| 60 | + * cyc = rdtsc(); |
| 61 | + * time_offset = pc->time_offset; |
| 62 | + * time_mult = pc->time_mult; |
| 63 | + * time_shift = pc->time_shift; |
| 64 | + * } |
| 65 | + * |
| 66 | + * index = pc->index; |
| 67 | + * count = pc->offset; |
| 68 | + * if (pc->cap_user_rdpmc && index) { |
| 69 | + * width = pc->pmc_width; |
| 70 | + * pmc = rdpmc(index - 1); |
| 71 | + * } |
| 72 | + * |
| 73 | + * barrier(); |
| 74 | + * } while (pc->lock != seq); |
| 75 | + * |
| 76 | + * NOTE: for obvious reason this only works on self-monitoring |
| 77 | + * processes. |
| 78 | + */ |
| 79 | + __u32 lock; /* seqlock for synchronization */ |
| 80 | + __u32 index; /* hardware event identifier */ |
| 81 | + __s64 offset; /* add to hardware event value */ |
| 82 | + __u64 time_enabled; /* time event active */ |
| 83 | + __u64 time_running; /* time event on cpu */ |
| 84 | + union { |
| 85 | + __u64 capabilities; |
| 86 | + struct { |
| 87 | + __u64 cap_bit0 : 1, /* Always 0, deprecated, see commit 860f085b74e9 */ |
| 88 | + cap_bit0_is_deprecated : 1, /* Always 1, signals that bit 0 is zero */ |
| 89 | + |
| 90 | + cap_user_rdpmc : 1, /* The RDPMC instruction can be used to read counts */ |
| 91 | + cap_user_time : 1, /* The time_{shift,mult,offset} fields are used */ |
| 92 | + cap_user_time_zero : 1, /* The time_zero field is used */ |
| 93 | + cap_user_time_short : 1, /* the time_{cycle,mask} fields are used */ |
| 94 | + cap_____res : 58; |
| 95 | + }; |
| 96 | + }; |
| 97 | + |
| 98 | + /* |
| 99 | + * If cap_user_rdpmc this field provides the bit-width of the value |
| 100 | + * read using the rdpmc() or equivalent instruction. This can be used |
| 101 | + * to sign extend the result like: |
| 102 | + * |
| 103 | + * pmc <<= 64 - width; |
| 104 | + * pmc >>= 64 - width; // signed shift right |
| 105 | + * count += pmc; |
| 106 | + */ |
| 107 | + __u16 pmc_width; |
| 108 | + |
| 109 | + /* |
| 110 | + * If cap_usr_time the below fields can be used to compute the time |
| 111 | + * delta since time_enabled (in ns) using rdtsc or similar. |
| 112 | + * |
| 113 | + * u64 quot, rem; |
| 114 | + * u64 delta; |
| 115 | + * |
| 116 | + * quot = (cyc >> time_shift); |
| 117 | + * rem = cyc & (((u64)1 << time_shift) - 1); |
| 118 | + * delta = time_offset + quot * time_mult + |
| 119 | + * ((rem * time_mult) >> time_shift); |
| 120 | + * |
| 121 | + * Where time_offset,time_mult,time_shift and cyc are read in the |
| 122 | + * seqcount loop described above. This delta can then be added to |
| 123 | + * enabled and possible running (if index), improving the scaling: |
| 124 | + * |
| 125 | + * enabled += delta; |
| 126 | + * if (index) |
| 127 | + * running += delta; |
| 128 | + * |
| 129 | + * quot = count / running; |
| 130 | + * rem = count % running; |
| 131 | + * count = quot * enabled + (rem * enabled) / running; |
| 132 | + */ |
| 133 | + __u16 time_shift; |
| 134 | + __u32 time_mult; |
| 135 | + __u64 time_offset; |
| 136 | + /* |
| 137 | + * If cap_usr_time_zero, the hardware clock (e.g. TSC) can be calculated |
| 138 | + * from sample timestamps. |
| 139 | + * |
| 140 | + * time = timestamp - time_zero; |
| 141 | + * quot = time / time_mult; |
| 142 | + * rem = time % time_mult; |
| 143 | + * cyc = (quot << time_shift) + (rem << time_shift) / time_mult; |
| 144 | + * |
| 145 | + * And vice versa: |
| 146 | + * |
| 147 | + * quot = cyc >> time_shift; |
| 148 | + * rem = cyc & (((u64)1 << time_shift) - 1); |
| 149 | + * timestamp = time_zero + quot * time_mult + |
| 150 | + * ((rem * time_mult) >> time_shift); |
| 151 | + */ |
| 152 | + __u64 time_zero; |
| 153 | + |
| 154 | + __u32 size; /* Header size up to __reserved[] fields. */ |
| 155 | + __u32 __reserved_1; |
| 156 | + |
| 157 | + /* |
| 158 | + * If cap_usr_time_short, the hardware clock is less than 64bit wide |
| 159 | + * and we must compute the 'cyc' value, as used by cap_usr_time, as: |
| 160 | + * |
| 161 | + * cyc = time_cycles + ((cyc - time_cycles) & time_mask) |
| 162 | + * |
| 163 | + * NOTE: this form is explicitly chosen such that cap_usr_time_short |
| 164 | + * is a correction on top of cap_usr_time, and code that doesn't |
| 165 | + * know about cap_usr_time_short still works under the assumption |
| 166 | + * the counter doesn't wrap. |
| 167 | + */ |
| 168 | + __u64 time_cycles; |
| 169 | + __u64 time_mask; |
| 170 | + |
| 171 | + /* |
| 172 | + * Hole for extension of the self monitor capabilities |
| 173 | + */ |
| 174 | + |
| 175 | + __u8 __reserved[116*8]; /* align to 1k. */ |
| 176 | + |
| 177 | + /* |
| 178 | + * Control data for the mmap() data buffer. |
| 179 | + * |
| 180 | + * User-space reading the @data_head value should issue an smp_rmb(), |
| 181 | + * after reading this value. |
| 182 | + * |
| 183 | + * When the mapping is PROT_WRITE the @data_tail value should be |
| 184 | + * written by userspace to reflect the last read data, after issueing |
| 185 | + * an smp_mb() to separate the data read from the ->data_tail store. |
| 186 | + * In this case the kernel will not over-write unread data. |
| 187 | + * |
| 188 | + * See perf_output_put_handle() for the data ordering. |
| 189 | + * |
| 190 | + * data_{offset,size} indicate the location and size of the perf record |
| 191 | + * buffer within the mmapped area. |
| 192 | + */ |
| 193 | + __u64 data_head; /* head in the data section */ |
| 194 | + __u64 data_tail; /* user-space written tail */ |
| 195 | + __u64 data_offset; /* where the buffer starts */ |
| 196 | + __u64 data_size; /* data buffer size */ |
| 197 | + |
| 198 | + /* |
| 199 | + * AUX area is defined by aux_{offset,size} fields that should be set |
| 200 | + * by the userspace, so that |
| 201 | + * |
| 202 | + * aux_offset >= data_offset + data_size |
| 203 | + * |
| 204 | + * prior to mmap()ing it. Size of the mmap()ed area should be aux_size. |
| 205 | + * |
| 206 | + * Ring buffer pointers aux_{head,tail} have the same semantics as |
| 207 | + * data_{head,tail} and same ordering rules apply. |
| 208 | + */ |
| 209 | + __u64 aux_head; |
| 210 | + __u64 aux_tail; |
| 211 | + __u64 aux_offset; |
| 212 | + __u64 aux_size; |
| 213 | +}; |
| 214 | + |
| 215 | + |
0 commit comments