1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIPERF_PERF_RECORD_FORMAT_H 16 #define HIPERF_PERF_RECORD_FORMAT_H 17 18 #include <string> 19 #include <linux/types.h> 20 21 #include "utilities.h" 22 23 namespace OHOS { 24 namespace Developtools { 25 namespace HiPerf { 26 // description from https://man7.org/linux/man-pages/man2/perf_event_open.2.html 27 28 #define SAMPLE_ID_ALL 0 29 #define PERF_SAMPLE_SERVER_PID (1U << 31) 30 31 struct sample_id { 32 u32 pid; 33 u32 tid; /* if PERF_SAMPLE_TID set */ 34 u64 time; /* if PERF_SAMPLE_TIME set */ 35 u64 id; /* if PERF_SAMPLE_ID set */ 36 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID set */ 37 u32 cpu, res; /* if PERF_SAMPLE_CPU set */ 38 u64 id2; /* if PERF_SAMPLE_IDENTIFIER set */ 39 }; 40 41 // If PERF_FORMAT_GROUP was not specified 42 struct read_format { 43 __u64 value; /* The value of the event */ 44 __u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */ 45 __u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */ 46 __u64 id; /* if PERF_FORMAT_ID */ 47 }; 48 49 /* 50 The MMAP events record the PROT_EXEC mappings so that 51 we can correlate user-space IPs to code. They have 52 the following structure: 53 pid is the process ID. 54 tid is the thread ID. 55 addr is the address of the allocated memory. 56 len is the length of the allocated memory. 57 pgoff is the page offset of the allocated memory. 58 filename 59 is a string describing the backing of 60 the allocated memory. 61 */ 62 struct PerfRecordMmapData { 63 u32 pid, tid; 64 u64 addr; 65 u64 len; 66 u64 pgoff; 67 char filename[KILO]; 68 #if SAMPLE_ID_ALL 69 struct sample_id sample_id; 70 #endif 71 }; 72 73 /* 74 This record includes extended information on mmap(2) 75 calls returning executable mappings. The format is 76 similar to that of the PERF_RECORD_MMAP record, but 77 includes extra values that allow uniquely identifying 78 shared mappings. 79 80 pid is the process ID. 81 tid is the thread ID. 82 addr is the address of the allocated memory. 83 len is the length of the allocated memory. 84 pgoff is the page offset of the allocated memory. 85 maj is the major ID of the underlying device. 86 min is the minor ID of the underlying device. 87 ino is the inode number. 88 ino_generation 89 is the inode generation. 90 prot is the protection information. 91 flags is the flags information. 92 filename 93 is a string describing the backing of the 94 allocated memory. 95 */ 96 struct PerfRecordMmap2Data { 97 u32 pid; 98 u32 tid; 99 u64 addr; 100 u64 len; 101 u64 pgoff; 102 u32 maj; 103 u32 min; 104 u64 ino; 105 u64 ino_generation; 106 u32 prot; 107 u32 flags; 108 char filename[KILO]; 109 #if SAMPLE_ID_ALL 110 struct sample_id sample_id; 111 #endif 112 }; 113 114 /* 115 This record indicates when events are lost. 116 id is the unique event ID for the samples that were lost. 117 lost is the number of events that were lost. 118 */ 119 struct PerfRecordLostData { 120 u64 id; 121 u64 lost; 122 #if SAMPLE_ID_ALL 123 struct sample_id sample_id; 124 #endif 125 }; 126 127 /* 128 This record indicates a change in the process name. 129 pid is the process ID. 130 tid is the thread ID. 131 comm is a string containing the new name of the process. 132 */ 133 struct PerfRecordCommData { 134 u32 pid; 135 u32 tid; 136 char comm[KILO]; 137 #if SAMPLE_ID_ALL 138 struct sample_id sample_id; 139 #endif 140 }; 141 142 // This record indicates a sample. 143 struct PerfRecordSampleData { 144 u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */ 145 u64 ip; /* if PERF_SAMPLE_IP */ 146 u32 pid, tid; /* if PERF_SAMPLE_TID */ 147 u64 time; /* if PERF_SAMPLE_TIME */ 148 u64 addr; /* if PERF_SAMPLE_ADDR */ 149 u64 id; /* if PERF_SAMPLE_ID */ 150 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */ 151 u32 cpu, res; /* if PERF_SAMPLE_CPU */ 152 u64 period; /* if PERF_SAMPLE_PERIOD */ 153 struct read_format v; 154 /* if PERF_SAMPLE_READ */ 155 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */ 156 u64 *ips; /* if PERF_SAMPLE_CALLCHAIN */ 157 u32 raw_size; /* if PERF_SAMPLE_RAW */ 158 u8 *raw_data; /* if PERF_SAMPLE_RAW */ 159 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */ 160 struct perf_branch_entry *lbr; /* if PERF_SAMPLE_BRANCH_STACK */ 161 u64 user_abi; /* if PERF_SAMPLE_REGS_USER */ 162 u64 reg_mask; 163 u64 reg_nr; 164 u64 *user_regs; /* if PERF_SAMPLE_REGS_USER */ 165 u64 stack_size; /* if PERF_SAMPLE_STACK_USER */ 166 u8 *stack_data; /* if PERF_SAMPLE_STACK_USER */ 167 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER && stack_size != 0 */ 168 u64 weight; /* if PERF_SAMPLE_WEIGHT */ 169 u64 data_src; /* if PERF_SAMPLE_DATA_SRC */ 170 u64 transaction; /* if PERF_SAMPLE_TRANSACTION */ 171 u64 intr_abi; /* if PERF_SAMPLE_REGS_INTR */ 172 u64 intr_regs[0]; /* if PERF_SAMPLE_REGS_INTR */ 173 u64 phys_addr; /* if PERF_SAMPLE_PHYS_ADDR */ 174 u64 cgroup; /* if PERF_SAMPLE_CGROUP */ 175 u64 server_nr; /* if PERF_SAMPLE_SERVER_PID */ 176 u64 *server_pids; /* if PERF_SAMPLE_SERVER_PID */ 177 }; 178 179 /* 180 This record indicates a process exit event. 181 */ 182 struct PerfRecordExitData { 183 u32 pid, ppid; 184 u32 tid, ptid; 185 u64 time; 186 #if SAMPLE_ID_ALL 187 struct sample_id sample_id; 188 #endif 189 }; 190 191 /* 192 This record indicates a throttle/unthrottle event. 193 */ 194 struct PerfRecordThrottleData { 195 u64 time; 196 u64 id; 197 u64 stream_id; 198 #if SAMPLE_ID_ALL 199 struct sample_id sample_id; 200 #endif 201 }; 202 203 /* 204 This record indicates a fork event. 205 */ 206 struct PerfRecordForkData { 207 u32 pid, ppid; 208 u32 tid, ptid; 209 u64 time; 210 #if SAMPLE_ID_ALL 211 struct sample_id sample_id; 212 #endif 213 }; 214 215 /* 216 When using hardware sampling (such as Intel PEBS) this 217 record indicates some number of samples that may have 218 been lost. 219 */ 220 struct PerfRecordLostSamplesData { 221 u64 lost; 222 #if SAMPLE_ID_ALL 223 struct sample_id sample_id; 224 #endif 225 }; 226 227 /* 228 This record indicates which process has initiated an 229 instruction trace event, allowing tools to properly 230 correlate the instruction addresses in the AUX buffer 231 with the proper executable. 232 233 pid process ID of the thread starting an 234 instruction trace. 235 tid thread ID of the thread starting an instruction 236 trace. 237 */ 238 struct PerfRecordItraceStartData { 239 u32 pid; 240 u32 tid; 241 }; 242 243 /* 244 This record reports that new data is available in the 245 separate AUX buffer region. 246 247 aux_offset 248 offset in the AUX mmap region where the new 249 data begins. 250 aux_size 251 size of the data made available. 252 flags describes the AUX update. 253 PERF_AUX_FLAG_TRUNCATED 254 if set, then the data returned was 255 truncated to fit the available buffer 256 size. 257 258 PERF_AUX_FLAG_OVERWRITE 259 if set, then the data returned has 260 overwritten previous data. 261 */ 262 struct PerfRecordAuxData { 263 u64 aux_offset; 264 u64 aux_size; 265 u64 flags; 266 #if SAMPLE_ID_ALL 267 struct sample_id sample_id; 268 #endif 269 }; 270 271 /* 272 This record indicates a read event. 273 */ 274 struct PerfRecordReadData { 275 u32 pid, tid; 276 read_format values; 277 #if SAMPLE_ID_ALL 278 struct sample_id sample_id; 279 #endif 280 }; 281 282 /* 283 This record indicates a context switch has happened. 284 The PERF_RECORD_MISC_SWITCH_OUT bit in the misc field 285 indicates whether it was a context switch into or away 286 from the current process. 287 */ 288 struct PerfRecordSwitchData { 289 #if SAMPLE_ID_ALL 290 struct sample_id sample_id; 291 #endif 292 }; 293 294 /* 295 As with PERF_RECORD_SWITCH this record indicates a 296 context switch has happened, but it only occurs when 297 sampling in CPU-wide mode and provides additional 298 information on the process being switched to/from. 299 The PERF_RECORD_MISC_SWITCH_OUT bit in the misc field 300 indicates whether it was a context switch into or away 301 from the current process. 302 303 next_prev_pid 304 The process ID of the previous (if switching 305 in) or next (if switching out) process on the 306 CPU. 307 308 next_prev_tid 309 The thread ID of the previous (if switching in) 310 or next (if switching out) thread on the CPU. 311 */ 312 struct PerfRecordSwitchCpuWideData { 313 u32 next_prev_pid; 314 u32 next_prev_tid; 315 #if SAMPLE_ID_ALL 316 struct sample_id sample_id; 317 #endif 318 }; 319 320 /* 321 This record includes various namespace information of 322 a process. 323 324 pid is the process ID 325 tid is the thread ID 326 327 nr_namespace 328 is the number of namespaces in this record 329 330 Each namespace has dev and inode fields and is 331 recorded in the fixed position like below: 332 333 NET_NS_INDEX=0 334 Network namespace 335 UTS_NS_INDEX=1 336 UTS namespace 337 IPC_NS_INDEX=2 338 IPC namespace 339 PID_NS_INDEX=3 340 PID namespace 341 USER_NS_INDEX=4 342 User namespace 343 MNT_NS_INDEX=5 344 Mount namespace 345 CGROUP_NS_INDEX=6 346 Cgroup namespace 347 */ 348 struct PerfRecordNamespacesData { 349 u32 pid; 350 u32 tid; 351 u64 nr_namespaces; 352 struct name_space { 353 u64 dev; 354 u64 inode; 355 } namespaces[0]; 356 #if SAMPLE_ID_ALL 357 struct sample_id sample_id; 358 #endif 359 }; 360 } // namespace HiPerf 361 } // namespace Developtools 362 } // namespace OHOS 363 #endif // HIPERF_PERF_RECORD_FORMAT_H 364