0001 perf-intel-pt(1)
0002 ================
0003
0004 NAME
0005 ----
0006 perf-intel-pt - Support for Intel Processor Trace within perf tools
0007
0008 SYNOPSIS
0009 --------
0010 [verse]
0011 'perf record' -e intel_pt//
0012
0013 DESCRIPTION
0014 -----------
0015
0016 Intel Processor Trace (Intel PT) is an extension of Intel Architecture that
0017 collects information about software execution such as control flow, execution
0018 modes and timings and formats it into highly compressed binary packets.
0019 Technical details are documented in the Intel 64 and IA-32 Architectures
0020 Software Developer Manuals, Chapter 36 Intel Processor Trace.
0021
0022 Intel PT is first supported in Intel Core M and 5th generation Intel Core
0023 processors that are based on the Intel micro-architecture code name Broadwell.
0024
0025 Trace data is collected by 'perf record' and stored within the perf.data file.
0026 See below for options to 'perf record'.
0027
0028 Trace data must be 'decoded' which involves walking the object code and matching
0029 the trace data packets. For example a TNT packet only tells whether a
0030 conditional branch was taken or not taken, so to make use of that packet the
0031 decoder must know precisely which instruction was being executed.
0032
0033 Decoding is done on-the-fly. The decoder outputs samples in the same format as
0034 samples output by perf hardware events, for example as though the "instructions"
0035 or "branches" events had been recorded. Presently 3 tools support this:
0036 'perf script', 'perf report' and 'perf inject'. See below for more information
0037 on using those tools.
0038
0039 The main distinguishing feature of Intel PT is that the decoder can determine
0040 the exact flow of software execution. Intel PT can be used to understand why
0041 and how did software get to a certain point, or behave a certain way. The
0042 software does not have to be recompiled, so Intel PT works with debug or release
0043 builds, however the executed images are needed - which makes use in JIT-compiled
0044 environments, or with self-modified code, a challenge. Also symbols need to be
0045 provided to make sense of addresses.
0046
0047 A limitation of Intel PT is that it produces huge amounts of trace data
0048 (hundreds of megabytes per second per core) which takes a long time to decode,
0049 for example two or three orders of magnitude longer than it took to collect.
0050 Another limitation is the performance impact of tracing, something that will
0051 vary depending on the use-case and architecture.
0052
0053
0054 Quickstart
0055 ----------
0056
0057 It is important to start small. That is because it is easy to capture vastly
0058 more data than can possibly be processed.
0059
0060 The simplest thing to do with Intel PT is userspace profiling of small programs.
0061 Data is captured with 'perf record' e.g. to trace 'ls' userspace-only:
0062
0063 perf record -e intel_pt//u ls
0064
0065 And profiled with 'perf report' e.g.
0066
0067 perf report
0068
0069 To also trace kernel space presents a problem, namely kernel self-modifying
0070 code. A fairly good kernel image is available in /proc/kcore but to get an
0071 accurate image a copy of /proc/kcore needs to be made under the same conditions
0072 as the data capture. 'perf record' can make a copy of /proc/kcore if the option
0073 --kcore is used, but access to /proc/kcore is restricted e.g.
0074
0075 sudo perf record -o pt_ls --kcore -e intel_pt// -- ls
0076
0077 which will create a directory named 'pt_ls' and put the perf.data file (named
0078 simply 'data') and copies of /proc/kcore, /proc/kallsyms and /proc/modules into
0079 it. The other tools understand the directory format, so to use 'perf report'
0080 becomes:
0081
0082 sudo perf report -i pt_ls
0083
0084 Because samples are synthesized after-the-fact, the sampling period can be
0085 selected for reporting. e.g. sample every microsecond
0086
0087 sudo perf report pt_ls --itrace=i1usge
0088
0089 See the sections below for more information about the --itrace option.
0090
0091 Beware the smaller the period, the more samples that are produced, and the
0092 longer it takes to process them.
0093
0094 Also note that the coarseness of Intel PT timing information will start to
0095 distort the statistical value of the sampling as the sampling period becomes
0096 smaller.
0097
0098 To represent software control flow, "branches" samples are produced. By default
0099 a branch sample is synthesized for every single branch. To get an idea what
0100 data is available you can use the 'perf script' tool with all itrace sampling
0101 options, which will list all the samples.
0102
0103 perf record -e intel_pt//u ls
0104 perf script --itrace=ibxwpe
0105
0106 An interesting field that is not printed by default is 'flags' which can be
0107 displayed as follows:
0108
0109 perf script --itrace=ibxwpe -F+flags
0110
0111 The flags are "bcrosyiABExghDt" which stand for branch, call, return, conditional,
0112 system, asynchronous, interrupt, transaction abort, trace begin, trace end,
0113 in transaction, VM-entry, VM-exit, interrupt disabled, and interrupt disable
0114 toggle respectively.
0115
0116 perf script also supports higher level ways to dump instruction traces:
0117
0118 perf script --insn-trace --xed
0119
0120 Dump all instructions. This requires installing the xed tool (see XED below)
0121 Dumping all instructions in a long trace can be fairly slow. It is usually better
0122 to start with higher level decoding, like
0123
0124 perf script --call-trace
0125
0126 or
0127
0128 perf script --call-ret-trace
0129
0130 and then select a time range of interest. The time range can then be examined
0131 in detail with
0132
0133 perf script --time starttime,stoptime --insn-trace --xed
0134
0135 While examining the trace it's also useful to filter on specific CPUs using
0136 the -C option
0137
0138 perf script --time starttime,stoptime --insn-trace --xed -C 1
0139
0140 Dump all instructions in time range on CPU 1.
0141
0142 Another interesting field that is not printed by default is 'ipc' which can be
0143 displayed as follows:
0144
0145 perf script --itrace=be -F+ipc
0146
0147 There are two ways that instructions-per-cycle (IPC) can be calculated depending
0148 on the recording.
0149
0150 If the 'cyc' config term (see config terms section below) was used, then IPC is
0151 calculated using the cycle count from CYC packets, otherwise MTC packets are
0152 used - refer to the 'mtc' config term. When MTC is used, however, the values
0153 are less accurate because the timing is less accurate.
0154
0155 Because Intel PT does not update the cycle count on every branch or instruction,
0156 the values will often be zero. When there are values, they will be the number
0157 of instructions and number of cycles since the last update, and thus represent
0158 the average IPC since the last IPC for that event type. Note IPC for "branches"
0159 events is calculated separately from IPC for "instructions" events.
0160
0161 Even with the 'cyc' config term, it is possible to produce IPC information for
0162 every change of timestamp, but at the expense of accuracy. That is selected by
0163 specifying the itrace 'A' option. Due to the granularity of timestamps, the
0164 actual number of cycles increases even though the cycles reported does not.
0165 The number of instructions is known, but if IPC is reported, cycles can be too
0166 low and so IPC is too high. Note that inaccuracy decreases as the period of
0167 sampling increases i.e. if the number of cycles is too low by a small amount,
0168 that becomes less significant if the number of cycles is large. It may also be
0169 useful to use the 'A' option in conjunction with dlfilter-show-cycles.so to
0170 provide higher granularity cycle information.
0171
0172 Also note that the IPC instruction count may or may not include the current
0173 instruction. If the cycle count is associated with an asynchronous branch
0174 (e.g. page fault or interrupt), then the instruction count does not include the
0175 current instruction, otherwise it does. That is consistent with whether or not
0176 that instruction has retired when the cycle count is updated.
0177
0178 Another note, in the case of "branches" events, non-taken branches are not
0179 presently sampled, so IPC values for them do not appear e.g. a CYC packet with a
0180 TNT packet that starts with a non-taken branch. To see every possible IPC
0181 value, "instructions" events can be used e.g. --itrace=i0ns
0182
0183 While it is possible to create scripts to analyze the data, an alternative
0184 approach is available to export the data to a sqlite or postgresql database.
0185 Refer to script export-to-sqlite.py or export-to-postgresql.py for more details,
0186 and to script exported-sql-viewer.py for an example of using the database.
0187
0188 There is also script intel-pt-events.py which provides an example of how to
0189 unpack the raw data for power events and PTWRITE. The script also displays
0190 branches, and supports 2 additional modes selected by option:
0191
0192 --insn-trace - instruction trace
0193 --src-trace - source trace
0194
0195 As mentioned above, it is easy to capture too much data. One way to limit the
0196 data captured is to use 'snapshot' mode which is explained further below.
0197 Refer to 'new snapshot option' and 'Intel PT modes of operation' further below.
0198
0199 Another problem that will be experienced is decoder errors. They can be caused
0200 by inability to access the executed image, self-modified or JIT-ed code, or the
0201 inability to match side-band information (such as context switches and mmaps)
0202 which results in the decoder not knowing what code was executed.
0203
0204 There is also the problem of perf not being able to copy the data fast enough,
0205 resulting in data lost because the buffer was full. See 'Buffer handling' below
0206 for more details.
0207
0208
0209 perf record
0210 -----------
0211
0212 new event
0213 ~~~~~~~~~
0214
0215 The Intel PT kernel driver creates a new PMU for Intel PT. PMU events are
0216 selected by providing the PMU name followed by the "config" separated by slashes.
0217 An enhancement has been made to allow default "config" e.g. the option
0218
0219 -e intel_pt//
0220
0221 will use a default config value. Currently that is the same as
0222
0223 -e intel_pt/tsc,noretcomp=0/
0224
0225 which is the same as
0226
0227 -e intel_pt/tsc=1,noretcomp=0/
0228
0229 Note there are now new config terms - see section 'config terms' further below.
0230
0231 The config terms are listed in /sys/devices/intel_pt/format. They are bit
0232 fields within the config member of the struct perf_event_attr which is
0233 passed to the kernel by the perf_event_open system call. They correspond to bit
0234 fields in the IA32_RTIT_CTL MSR. Here is a list of them and their definitions:
0235
0236 $ grep -H . /sys/bus/event_source/devices/intel_pt/format/*
0237 /sys/bus/event_source/devices/intel_pt/format/cyc:config:1
0238 /sys/bus/event_source/devices/intel_pt/format/cyc_thresh:config:19-22
0239 /sys/bus/event_source/devices/intel_pt/format/mtc:config:9
0240 /sys/bus/event_source/devices/intel_pt/format/mtc_period:config:14-17
0241 /sys/bus/event_source/devices/intel_pt/format/noretcomp:config:11
0242 /sys/bus/event_source/devices/intel_pt/format/psb_period:config:24-27
0243 /sys/bus/event_source/devices/intel_pt/format/tsc:config:10
0244
0245 Note that the default config must be overridden for each term i.e.
0246
0247 -e intel_pt/noretcomp=0/
0248
0249 is the same as:
0250
0251 -e intel_pt/tsc=1,noretcomp=0/
0252
0253 So, to disable TSC packets use:
0254
0255 -e intel_pt/tsc=0/
0256
0257 It is also possible to specify the config value explicitly:
0258
0259 -e intel_pt/config=0x400/
0260
0261 Note that, as with all events, the event is suffixed with event modifiers:
0262
0263 u userspace
0264 k kernel
0265 h hypervisor
0266 G guest
0267 H host
0268 p precise ip
0269
0270 'h', 'G' and 'H' are for virtualization which are not used by Intel PT.
0271 'p' is also not relevant to Intel PT. So only options 'u' and 'k' are
0272 meaningful for Intel PT.
0273
0274 perf_event_attr is displayed if the -vv option is used e.g.
0275
0276 ------------------------------------------------------------
0277 perf_event_attr:
0278 type 6
0279 size 112
0280 config 0x400
0281 { sample_period, sample_freq } 1
0282 sample_type IP|TID|TIME|CPU|IDENTIFIER
0283 read_format ID
0284 disabled 1
0285 inherit 1
0286 exclude_kernel 1
0287 exclude_hv 1
0288 enable_on_exec 1
0289 sample_id_all 1
0290 ------------------------------------------------------------
0291 sys_perf_event_open: pid 31104 cpu 0 group_fd -1 flags 0x8
0292 sys_perf_event_open: pid 31104 cpu 1 group_fd -1 flags 0x8
0293 sys_perf_event_open: pid 31104 cpu 2 group_fd -1 flags 0x8
0294 sys_perf_event_open: pid 31104 cpu 3 group_fd -1 flags 0x8
0295 ------------------------------------------------------------
0296
0297
0298 config terms
0299 ~~~~~~~~~~~~
0300
0301 The June 2015 version of Intel 64 and IA-32 Architectures Software Developer
0302 Manuals, Chapter 36 Intel Processor Trace, defined new Intel PT features.
0303 Some of the features are reflect in new config terms. All the config terms are
0304 described below.
0305
0306 tsc Always supported. Produces TSC timestamp packets to provide
0307 timing information. In some cases it is possible to decode
0308 without timing information, for example a per-thread context
0309 that does not overlap executable memory maps.
0310
0311 The default config selects tsc (i.e. tsc=1).
0312
0313 noretcomp Always supported. Disables "return compression" so a TIP packet
0314 is produced when a function returns. Causes more packets to be
0315 produced but might make decoding more reliable.
0316
0317 The default config does not select noretcomp (i.e. noretcomp=0).
0318
0319 psb_period Allows the frequency of PSB packets to be specified.
0320
0321 The PSB packet is a synchronization packet that provides a
0322 starting point for decoding or recovery from errors.
0323
0324 Support for psb_period is indicated by:
0325
0326 /sys/bus/event_source/devices/intel_pt/caps/psb_cyc
0327
0328 which contains "1" if the feature is supported and "0"
0329 otherwise.
0330
0331 Valid values are given by:
0332
0333 /sys/bus/event_source/devices/intel_pt/caps/psb_periods
0334
0335 which contains a hexadecimal value, the bits of which represent
0336 valid values e.g. bit 2 set means value 2 is valid.
0337
0338 The psb_period value is converted to the approximate number of
0339 trace bytes between PSB packets as:
0340
0341 2 ^ (value + 11)
0342
0343 e.g. value 3 means 16KiB bytes between PSBs
0344
0345 If an invalid value is entered, the error message
0346 will give a list of valid values e.g.
0347
0348 $ perf record -e intel_pt/psb_period=15/u uname
0349 Invalid psb_period for intel_pt. Valid values are: 0-5
0350
0351 If MTC packets are selected, the default config selects a value
0352 of 3 (i.e. psb_period=3) or the nearest lower value that is
0353 supported (0 is always supported). Otherwise the default is 0.
0354
0355 If decoding is expected to be reliable and the buffer is large
0356 then a large PSB period can be used.
0357
0358 Because a TSC packet is produced with PSB, the PSB period can
0359 also affect the granularity to timing information in the absence
0360 of MTC or CYC.
0361
0362 mtc Produces MTC timing packets.
0363
0364 MTC packets provide finer grain timestamp information than TSC
0365 packets. MTC packets record time using the hardware crystal
0366 clock (CTC) which is related to TSC packets using a TMA packet.
0367
0368 Support for this feature is indicated by:
0369
0370 /sys/bus/event_source/devices/intel_pt/caps/mtc
0371
0372 which contains "1" if the feature is supported and
0373 "0" otherwise.
0374
0375 The frequency of MTC packets can also be specified - see
0376 mtc_period below.
0377
0378 mtc_period Specifies how frequently MTC packets are produced - see mtc
0379 above for how to determine if MTC packets are supported.
0380
0381 Valid values are given by:
0382
0383 /sys/bus/event_source/devices/intel_pt/caps/mtc_periods
0384
0385 which contains a hexadecimal value, the bits of which represent
0386 valid values e.g. bit 2 set means value 2 is valid.
0387
0388 The mtc_period value is converted to the MTC frequency as:
0389
0390 CTC-frequency / (2 ^ value)
0391
0392 e.g. value 3 means one eighth of CTC-frequency
0393
0394 Where CTC is the hardware crystal clock, the frequency of which
0395 can be related to TSC via values provided in cpuid leaf 0x15.
0396
0397 If an invalid value is entered, the error message
0398 will give a list of valid values e.g.
0399
0400 $ perf record -e intel_pt/mtc_period=15/u uname
0401 Invalid mtc_period for intel_pt. Valid values are: 0,3,6,9
0402
0403 The default value is 3 or the nearest lower value
0404 that is supported (0 is always supported).
0405
0406 cyc Produces CYC timing packets.
0407
0408 CYC packets provide even finer grain timestamp information than
0409 MTC and TSC packets. A CYC packet contains the number of CPU
0410 cycles since the last CYC packet. Unlike MTC and TSC packets,
0411 CYC packets are only sent when another packet is also sent.
0412
0413 Support for this feature is indicated by:
0414
0415 /sys/bus/event_source/devices/intel_pt/caps/psb_cyc
0416
0417 which contains "1" if the feature is supported and
0418 "0" otherwise.
0419
0420 The number of CYC packets produced can be reduced by specifying
0421 a threshold - see cyc_thresh below.
0422
0423 cyc_thresh Specifies how frequently CYC packets are produced - see cyc
0424 above for how to determine if CYC packets are supported.
0425
0426 Valid cyc_thresh values are given by:
0427
0428 /sys/bus/event_source/devices/intel_pt/caps/cycle_thresholds
0429
0430 which contains a hexadecimal value, the bits of which represent
0431 valid values e.g. bit 2 set means value 2 is valid.
0432
0433 The cyc_thresh value represents the minimum number of CPU cycles
0434 that must have passed before a CYC packet can be sent. The
0435 number of CPU cycles is:
0436
0437 2 ^ (value - 1)
0438
0439 e.g. value 4 means 8 CPU cycles must pass before a CYC packet
0440 can be sent. Note a CYC packet is still only sent when another
0441 packet is sent, not at, e.g. every 8 CPU cycles.
0442
0443 If an invalid value is entered, the error message
0444 will give a list of valid values e.g.
0445
0446 $ perf record -e intel_pt/cyc,cyc_thresh=15/u uname
0447 Invalid cyc_thresh for intel_pt. Valid values are: 0-12
0448
0449 CYC packets are not requested by default.
0450
0451 pt Specifies pass-through which enables the 'branch' config term.
0452
0453 The default config selects 'pt' if it is available, so a user will
0454 never need to specify this term.
0455
0456 branch Enable branch tracing. Branch tracing is enabled by default so to
0457 disable branch tracing use 'branch=0'.
0458
0459 The default config selects 'branch' if it is available.
0460
0461 ptw Enable PTWRITE packets which are produced when a ptwrite instruction
0462 is executed.
0463
0464 Support for this feature is indicated by:
0465
0466 /sys/bus/event_source/devices/intel_pt/caps/ptwrite
0467
0468 which contains "1" if the feature is supported and
0469 "0" otherwise.
0470
0471 As an alternative, refer to "Emulated PTWRITE" further below.
0472
0473 fup_on_ptw Enable a FUP packet to follow the PTWRITE packet. The FUP packet
0474 provides the address of the ptwrite instruction. In the absence of
0475 fup_on_ptw, the decoder will use the address of the previous branch
0476 if branch tracing is enabled, otherwise the address will be zero.
0477 Note that fup_on_ptw will work even when branch tracing is disabled.
0478
0479 pwr_evt Enable power events. The power events provide information about
0480 changes to the CPU C-state.
0481
0482 Support for this feature is indicated by:
0483
0484 /sys/bus/event_source/devices/intel_pt/caps/power_event_trace
0485
0486 which contains "1" if the feature is supported and
0487 "0" otherwise.
0488
0489 event Enable Event Trace. The events provide information about asynchronous
0490 events.
0491
0492 Support for this feature is indicated by:
0493
0494 /sys/bus/event_source/devices/intel_pt/caps/event_trace
0495
0496 which contains "1" if the feature is supported and
0497 "0" otherwise.
0498
0499 notnt Disable TNT packets. Without TNT packets, it is not possible to walk
0500 executable code to reconstruct control flow, however FUP, TIP, TIP.PGE
0501 and TIP.PGD packets still indicate asynchronous control flow, and (if
0502 return compression is disabled - see noretcomp) return statements.
0503 The advantage of eliminating TNT packets is reducing the size of the
0504 trace and corresponding tracing overhead.
0505
0506 Support for this feature is indicated by:
0507
0508 /sys/bus/event_source/devices/intel_pt/caps/tnt_disable
0509
0510 which contains "1" if the feature is supported and
0511 "0" otherwise.
0512
0513
0514 AUX area sampling option
0515 ~~~~~~~~~~~~~~~~~~~~~~~~
0516
0517 To select Intel PT "sampling" the AUX area sampling option can be used:
0518
0519 --aux-sample
0520
0521 Optionally it can be followed by the sample size in bytes e.g.
0522
0523 --aux-sample=8192
0524
0525 In addition, the Intel PT event to sample must be defined e.g.
0526
0527 -e intel_pt//u
0528
0529 Samples on other events will be created containing Intel PT data e.g. the
0530 following will create Intel PT samples on the branch-misses event, note the
0531 events must be grouped using {}:
0532
0533 perf record --aux-sample -e '{intel_pt//u,branch-misses:u}'
0534
0535 An alternative to '--aux-sample' is to add the config term 'aux-sample-size' to
0536 events. In this case, the grouping is implied e.g.
0537
0538 perf record -e intel_pt//u -e branch-misses/aux-sample-size=8192/u
0539
0540 is the same as:
0541
0542 perf record -e '{intel_pt//u,branch-misses/aux-sample-size=8192/u}'
0543
0544 but allows for also using an address filter e.g.:
0545
0546 perf record -e intel_pt//u --filter 'filter * @/bin/ls' -e branch-misses/aux-sample-size=8192/u -- ls
0547
0548 It is important to select a sample size that is big enough to contain at least
0549 one PSB packet. If not a warning will be displayed:
0550
0551 Intel PT sample size (%zu) may be too small for PSB period (%zu)
0552
0553 The calculation used for that is: if sample_size <= psb_period + 256 display the
0554 warning. When sampling is used, psb_period defaults to 0 (2KiB).
0555
0556 The default sample size is 4KiB.
0557
0558 The sample size is passed in aux_sample_size in struct perf_event_attr. The
0559 sample size is limited by the maximum event size which is 64KiB. It is
0560 difficult to know how big the event might be without the trace sample attached,
0561 but the tool validates that the sample size is not greater than 60KiB.
0562
0563
0564 new snapshot option
0565 ~~~~~~~~~~~~~~~~~~~
0566
0567 The difference between full trace and snapshot from the kernel's perspective is
0568 that in full trace we don't overwrite trace data that the user hasn't collected
0569 yet (and indicated that by advancing aux_tail), whereas in snapshot mode we let
0570 the trace run and overwrite older data in the buffer so that whenever something
0571 interesting happens, we can stop it and grab a snapshot of what was going on
0572 around that interesting moment.
0573
0574 To select snapshot mode a new option has been added:
0575
0576 -S
0577
0578 Optionally it can be followed by the snapshot size e.g.
0579
0580 -S0x100000
0581
0582 The default snapshot size is the auxtrace mmap size. If neither auxtrace mmap size
0583 nor snapshot size is specified, then the default is 4MiB for privileged users
0584 (or if /proc/sys/kernel/perf_event_paranoid < 0), 128KiB for unprivileged users.
0585 If an unprivileged user does not specify mmap pages, the mmap pages will be
0586 reduced as described in the 'new auxtrace mmap size option' section below.
0587
0588 The snapshot size is displayed if the option -vv is used e.g.
0589
0590 Intel PT snapshot size: %zu
0591
0592
0593 new auxtrace mmap size option
0594 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0595
0596 Intel PT buffer size is specified by an addition to the -m option e.g.
0597
0598 -m,16
0599
0600 selects a buffer size of 16 pages i.e. 64KiB.
0601
0602 Note that the existing functionality of -m is unchanged. The auxtrace mmap size
0603 is specified by the optional addition of a comma and the value.
0604
0605 The default auxtrace mmap size for Intel PT is 4MiB/page_size for privileged users
0606 (or if /proc/sys/kernel/perf_event_paranoid < 0), 128KiB for unprivileged users.
0607 If an unprivileged user does not specify mmap pages, the mmap pages will be
0608 reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the
0609 user is likely to get an error as they exceed their mlock limit (Max locked
0610 memory as shown in /proc/self/limits). Note that perf does not count the first
0611 512KiB (actually /proc/sys/kernel/perf_event_mlock_kb minus 1 page) per cpu
0612 against the mlock limit so an unprivileged user is allowed 512KiB per cpu plus
0613 their mlock limit (which defaults to 64KiB but is not multiplied by the number
0614 of cpus).
0615
0616 In full-trace mode, powers of two are allowed for buffer size, with a minimum
0617 size of 2 pages. In snapshot mode or sampling mode, it is the same but the
0618 minimum size is 1 page.
0619
0620 The mmap size and auxtrace mmap size are displayed if the -vv option is used e.g.
0621
0622 mmap length 528384
0623 auxtrace mmap length 4198400
0624
0625
0626 Intel PT modes of operation
0627 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0628
0629 Intel PT can be used in 3 modes:
0630 full-trace mode
0631 sample mode
0632 snapshot mode
0633
0634 Full-trace mode traces continuously e.g.
0635
0636 perf record -e intel_pt//u uname
0637
0638 Sample mode attaches a Intel PT sample to other events e.g.
0639
0640 perf record --aux-sample -e intel_pt//u -e branch-misses:u
0641
0642 Snapshot mode captures the available data when a signal is sent or "snapshot"
0643 control command is issued. e.g. using a signal
0644
0645 perf record -v -e intel_pt//u -S ./loopy 1000000000 &
0646 [1] 11435
0647 kill -USR2 11435
0648 Recording AUX area tracing snapshot
0649
0650 Note that the signal sent is SIGUSR2.
0651 Note that "Recording AUX area tracing snapshot" is displayed because the -v
0652 option is used.
0653
0654 The advantage of using "snapshot" control command is that the access is
0655 controlled by access to a FIFO e.g.
0656
0657 $ mkfifo perf.control
0658 $ mkfifo perf.ack
0659 $ cat perf.ack &
0660 [1] 15235
0661 $ sudo ~/bin/perf record --control fifo:perf.control,perf.ack -S -e intel_pt//u -- sleep 60 &
0662 [2] 15243
0663 $ ps -e | grep perf
0664 15244 pts/1 00:00:00 perf
0665 $ kill -USR2 15244
0666 bash: kill: (15244) - Operation not permitted
0667 $ echo snapshot > perf.control
0668 ack
0669
0670 The 3 Intel PT modes of operation cannot be used together.
0671
0672
0673 Buffer handling
0674 ~~~~~~~~~~~~~~~
0675
0676 There may be buffer limitations (i.e. single ToPa entry) which means that actual
0677 buffer sizes are limited to powers of 2 up to 4MiB (MAX_ORDER). In order to
0678 provide other sizes, and in particular an arbitrarily large size, multiple
0679 buffers are logically concatenated. However an interrupt must be used to switch
0680 between buffers. That has two potential problems:
0681 a) the interrupt may not be handled in time so that the current buffer
0682 becomes full and some trace data is lost.
0683 b) the interrupts may slow the system and affect the performance
0684 results.
0685
0686 If trace data is lost, the driver sets 'truncated' in the PERF_RECORD_AUX event
0687 which the tools report as an error.
0688
0689 In full-trace mode, the driver waits for data to be copied out before allowing
0690 the (logical) buffer to wrap-around. If data is not copied out quickly enough,
0691 again 'truncated' is set in the PERF_RECORD_AUX event. If the driver has to
0692 wait, the intel_pt event gets disabled. Because it is difficult to know when
0693 that happens, perf tools always re-enable the intel_pt event after copying out
0694 data.
0695
0696
0697 Intel PT and build ids
0698 ~~~~~~~~~~~~~~~~~~~~~~
0699
0700 By default "perf record" post-processes the event stream to find all build ids
0701 for executables for all addresses sampled. Deliberately, Intel PT is not
0702 decoded for that purpose (it would take too long). Instead the build ids for
0703 all executables encountered (due to mmap, comm or task events) are included
0704 in the perf.data file.
0705
0706 To see buildids included in the perf.data file use the command:
0707
0708 perf buildid-list
0709
0710 If the perf.data file contains Intel PT data, that is the same as:
0711
0712 perf buildid-list --with-hits
0713
0714
0715 Snapshot mode and event disabling
0716 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0717
0718 In order to make a snapshot, the intel_pt event is disabled using an IOCTL,
0719 namely PERF_EVENT_IOC_DISABLE. However doing that can also disable the
0720 collection of side-band information. In order to prevent that, a dummy
0721 software event has been introduced that permits tracking events (like mmaps) to
0722 continue to be recorded while intel_pt is disabled. That is important to ensure
0723 there is complete side-band information to allow the decoding of subsequent
0724 snapshots.
0725
0726 A test has been created for that. To find the test:
0727
0728 perf test list
0729 ...
0730 23: Test using a dummy software event to keep tracking
0731
0732 To run the test:
0733
0734 perf test 23
0735 23: Test using a dummy software event to keep tracking : Ok
0736
0737
0738 perf record modes (nothing new here)
0739 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0740
0741 perf record essentially operates in one of three modes:
0742 per thread
0743 per cpu
0744 workload only
0745
0746 "per thread" mode is selected by -t or by --per-thread (with -p or -u or just a
0747 workload).
0748 "per cpu" is selected by -C or -a.
0749 "workload only" mode is selected by not using the other options but providing a
0750 command to run (i.e. the workload).
0751
0752 In per-thread mode an exact list of threads is traced. There is no inheritance.
0753 Each thread has its own event buffer.
0754
0755 In per-cpu mode all processes (or processes from the selected cgroup i.e. -G
0756 option, or processes selected with -p or -u) are traced. Each cpu has its own
0757 buffer. Inheritance is allowed.
0758
0759 In workload-only mode, the workload is traced but with per-cpu buffers.
0760 Inheritance is allowed. Note that you can now trace a workload in per-thread
0761 mode by using the --per-thread option.
0762
0763
0764 Privileged vs non-privileged users
0765 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0766
0767 Unless /proc/sys/kernel/perf_event_paranoid is set to -1, unprivileged users
0768 have memory limits imposed upon them. That affects what buffer sizes they can
0769 have as outlined above.
0770
0771 The v4.2 kernel introduced support for a context switch metadata event,
0772 PERF_RECORD_SWITCH, which allows unprivileged users to see when their processes
0773 are scheduled out and in, just not by whom, which is left for the
0774 PERF_RECORD_SWITCH_CPU_WIDE, that is only accessible in system wide context,
0775 which in turn requires CAP_PERFMON or CAP_SYS_ADMIN.
0776
0777 Please see the 45ac1403f564 ("perf: Add PERF_RECORD_SWITCH to indicate context
0778 switches") commit, that introduces these metadata events for further info.
0779
0780 When working with kernels < v4.2, the following considerations must be taken,
0781 as the sched:sched_switch tracepoints will be used to receive such information:
0782
0783 Unless /proc/sys/kernel/perf_event_paranoid is set to -1, unprivileged users are
0784 not permitted to use tracepoints which means there is insufficient side-band
0785 information to decode Intel PT in per-cpu mode, and potentially workload-only
0786 mode too if the workload creates new processes.
0787
0788 Note also, that to use tracepoints, read-access to debugfs is required. So if
0789 debugfs is not mounted or the user does not have read-access, it will again not
0790 be possible to decode Intel PT in per-cpu mode.
0791
0792
0793 sched_switch tracepoint
0794 ~~~~~~~~~~~~~~~~~~~~~~~
0795
0796 The sched_switch tracepoint is used to provide side-band data for Intel PT
0797 decoding in kernels where the PERF_RECORD_SWITCH metadata event isn't
0798 available.
0799
0800 The sched_switch events are automatically added. e.g. the second event shown
0801 below:
0802
0803 $ perf record -vv -e intel_pt//u uname
0804 ------------------------------------------------------------
0805 perf_event_attr:
0806 type 6
0807 size 112
0808 config 0x400
0809 { sample_period, sample_freq } 1
0810 sample_type IP|TID|TIME|CPU|IDENTIFIER
0811 read_format ID
0812 disabled 1
0813 inherit 1
0814 exclude_kernel 1
0815 exclude_hv 1
0816 enable_on_exec 1
0817 sample_id_all 1
0818 ------------------------------------------------------------
0819 sys_perf_event_open: pid 31104 cpu 0 group_fd -1 flags 0x8
0820 sys_perf_event_open: pid 31104 cpu 1 group_fd -1 flags 0x8
0821 sys_perf_event_open: pid 31104 cpu 2 group_fd -1 flags 0x8
0822 sys_perf_event_open: pid 31104 cpu 3 group_fd -1 flags 0x8
0823 ------------------------------------------------------------
0824 perf_event_attr:
0825 type 2
0826 size 112
0827 config 0x108
0828 { sample_period, sample_freq } 1
0829 sample_type IP|TID|TIME|CPU|PERIOD|RAW|IDENTIFIER
0830 read_format ID
0831 inherit 1
0832 sample_id_all 1
0833 exclude_guest 1
0834 ------------------------------------------------------------
0835 sys_perf_event_open: pid -1 cpu 0 group_fd -1 flags 0x8
0836 sys_perf_event_open: pid -1 cpu 1 group_fd -1 flags 0x8
0837 sys_perf_event_open: pid -1 cpu 2 group_fd -1 flags 0x8
0838 sys_perf_event_open: pid -1 cpu 3 group_fd -1 flags 0x8
0839 ------------------------------------------------------------
0840 perf_event_attr:
0841 type 1
0842 size 112
0843 config 0x9
0844 { sample_period, sample_freq } 1
0845 sample_type IP|TID|TIME|IDENTIFIER
0846 read_format ID
0847 disabled 1
0848 inherit 1
0849 exclude_kernel 1
0850 exclude_hv 1
0851 mmap 1
0852 comm 1
0853 enable_on_exec 1
0854 task 1
0855 sample_id_all 1
0856 mmap2 1
0857 comm_exec 1
0858 ------------------------------------------------------------
0859 sys_perf_event_open: pid 31104 cpu 0 group_fd -1 flags 0x8
0860 sys_perf_event_open: pid 31104 cpu 1 group_fd -1 flags 0x8
0861 sys_perf_event_open: pid 31104 cpu 2 group_fd -1 flags 0x8
0862 sys_perf_event_open: pid 31104 cpu 3 group_fd -1 flags 0x8
0863 mmap size 528384B
0864 AUX area mmap length 4194304
0865 perf event ring buffer mmapped per cpu
0866 Synthesizing auxtrace information
0867 Linux
0868 [ perf record: Woken up 1 times to write data ]
0869 [ perf record: Captured and wrote 0.042 MB perf.data ]
0870
0871 Note, the sched_switch event is only added if the user is permitted to use it
0872 and only in per-cpu mode.
0873
0874 Note also, the sched_switch event is only added if TSC packets are requested.
0875 That is because, in the absence of timing information, the sched_switch events
0876 cannot be matched against the Intel PT trace.
0877
0878
0879 perf script
0880 -----------
0881
0882 By default, perf script will decode trace data found in the perf.data file.
0883 This can be further controlled by new option --itrace.
0884
0885
0886 New --itrace option
0887 ~~~~~~~~~~~~~~~~~~~
0888
0889 Having no option is the same as
0890
0891 --itrace
0892
0893 which, in turn, is the same as
0894
0895 --itrace=cepwx
0896
0897 The letters are:
0898
0899 i synthesize "instructions" events
0900 b synthesize "branches" events
0901 x synthesize "transactions" events
0902 w synthesize "ptwrite" events
0903 p synthesize "power" events (incl. PSB events)
0904 c synthesize branches events (calls only)
0905 r synthesize branches events (returns only)
0906 o synthesize PEBS-via-PT events
0907 I synthesize Event Trace events
0908 e synthesize tracing error events
0909 d create a debug log
0910 g synthesize a call chain (use with i or x)
0911 G synthesize a call chain on existing event records
0912 l synthesize last branch entries (use with i or x)
0913 L synthesize last branch entries on existing event records
0914 s skip initial number of events
0915 q quicker (less detailed) decoding
0916 A approximate IPC
0917 Z prefer to ignore timestamps (so-called "timeless" decoding)
0918
0919 "Instructions" events look like they were recorded by "perf record -e
0920 instructions".
0921
0922 "Branches" events look like they were recorded by "perf record -e branches". "c"
0923 and "r" can be combined to get calls and returns.
0924
0925 "Transactions" events correspond to the start or end of transactions. The
0926 'flags' field can be used in perf script to determine whether the event is a
0927 transaction start, commit or abort.
0928
0929 Note that "instructions", "branches" and "transactions" events depend on code
0930 flow packets which can be disabled by using the config term "branch=0". Refer
0931 to the config terms section above.
0932
0933 "ptwrite" events record the payload of the ptwrite instruction and whether
0934 "fup_on_ptw" was used. "ptwrite" events depend on PTWRITE packets which are
0935 recorded only if the "ptw" config term was used. Refer to the config terms
0936 section above. perf script "synth" field displays "ptwrite" information like
0937 this: "ip: 0 payload: 0x123456789abcdef0" where "ip" is 1 if "fup_on_ptw" was
0938 used.
0939
0940 "Power" events correspond to power event packets and CBR (core-to-bus ratio)
0941 packets. While CBR packets are always recorded when tracing is enabled, power
0942 event packets are recorded only if the "pwr_evt" config term was used. Refer to
0943 the config terms section above. The power events record information about
0944 C-state changes, whereas CBR is indicative of CPU frequency. perf script
0945 "event,synth" fields display information like this:
0946 cbr: cbr: 22 freq: 2189 MHz (200%)
0947 mwait: hints: 0x60 extensions: 0x1
0948 pwre: hw: 0 cstate: 2 sub-cstate: 0
0949 exstop: ip: 1
0950 pwrx: deepest cstate: 2 last cstate: 2 wake reason: 0x4
0951 Where:
0952 "cbr" includes the frequency and the percentage of maximum non-turbo
0953 "mwait" shows mwait hints and extensions
0954 "pwre" shows C-state transitions (to a C-state deeper than C0) and
0955 whether initiated by hardware
0956 "exstop" indicates execution stopped and whether the IP was recorded
0957 exactly,
0958 "pwrx" indicates return to C0
0959 For more details refer to the Intel 64 and IA-32 Architectures Software
0960 Developer Manuals.
0961
0962 PSB events show when a PSB+ occurred and also the byte-offset in the trace.
0963 Emitting a PSB+ can cause a CPU a slight delay. When doing timing analysis
0964 of code with Intel PT, it is useful to know if a timing bubble was caused
0965 by Intel PT or not.
0966
0967 Error events show where the decoder lost the trace. Error events
0968 are quite important. Users must know if what they are seeing is a complete
0969 picture or not. The "e" option may be followed by flags which affect what errors
0970 will or will not be reported. Each flag must be preceded by either '+' or '-'.
0971 The flags supported by Intel PT are:
0972 -o Suppress overflow errors
0973 -l Suppress trace data lost errors
0974 For example, for errors but not overflow or data lost errors:
0975
0976 --itrace=e-o-l
0977
0978 The "d" option will cause the creation of a file "intel_pt.log" containing all
0979 decoded packets and instructions. Note that this option slows down the decoder
0980 and that the resulting file may be very large. The "d" option may be followed
0981 by flags which affect what debug messages will or will not be logged. Each flag
0982 must be preceded by either '+' or '-'. The flags support by Intel PT are:
0983 -a Suppress logging of perf events
0984 +a Log all perf events
0985 +o Output to stdout instead of "intel_pt.log"
0986 By default, logged perf events are filtered by any specified time ranges, but
0987 flag +a overrides that.
0988
0989 In addition, the period of the "instructions" event can be specified. e.g.
0990
0991 --itrace=i10us
0992
0993 sets the period to 10us i.e. one instruction sample is synthesized for each 10
0994 microseconds of trace. Alternatives to "us" are "ms" (milliseconds),
0995 "ns" (nanoseconds), "t" (TSC ticks) or "i" (instructions).
0996
0997 "ms", "us" and "ns" are converted to TSC ticks.
0998
0999 The timing information included with Intel PT does not give the time of every
1000 instruction. Consequently, for the purpose of sampling, the decoder estimates
1001 the time since the last timing packet based on 1 tick per instruction. The time
1002 on the sample is *not* adjusted and reflects the last known value of TSC.
1003
1004 For Intel PT, the default period is 100us.
1005
1006 Setting it to a zero period means "as often as possible".
1007
1008 In the case of Intel PT that is the same as a period of 1 and a unit of
1009 'instructions' (i.e. --itrace=i1i).
1010
1011 Also the call chain size (default 16, max. 1024) for instructions or
1012 transactions events can be specified. e.g.
1013
1014 --itrace=ig32
1015 --itrace=xg32
1016
1017 Also the number of last branch entries (default 64, max. 1024) for instructions or
1018 transactions events can be specified. e.g.
1019
1020 --itrace=il10
1021 --itrace=xl10
1022
1023 Note that last branch entries are cleared for each sample, so there is no overlap
1024 from one sample to the next.
1025
1026 The G and L options are designed in particular for sample mode, and work much
1027 like g and l but add call chain and branch stack to the other selected events
1028 instead of synthesized events. For example, to record branch-misses events for
1029 'ls' and then add a call chain derived from the Intel PT trace:
1030
1031 perf record --aux-sample -e '{intel_pt//u,branch-misses:u}' -- ls
1032 perf report --itrace=Ge
1033
1034 Although in fact G is a default for perf report, so that is the same as just:
1035
1036 perf report
1037
1038 One caveat with the G and L options is that they work poorly with "Large PEBS".
1039 Large PEBS means PEBS records will be accumulated by hardware and the written
1040 into the event buffer in one go. That reduces interrupts, but can give very
1041 late timestamps. Because the Intel PT trace is synchronized by timestamps,
1042 the PEBS events do not match the trace. Currently, Large PEBS is used only in
1043 certain circumstances:
1044 - hardware supports it
1045 - PEBS is used
1046 - event period is specified, instead of frequency
1047 - the sample type is limited to the following flags:
1048 PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR |
1049 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID |
1050 PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER |
1051 PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR |
1052 PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER |
1053 PERF_SAMPLE_PERIOD (and sometimes) | PERF_SAMPLE_TIME
1054 Because Intel PT sample mode uses a different sample type to the list above,
1055 Large PEBS is not used with Intel PT sample mode. To avoid Large PEBS in other
1056 cases, avoid specifying the event period i.e. avoid the 'perf record' -c option,
1057 --count option, or 'period' config term.
1058
1059 To disable trace decoding entirely, use the option --no-itrace.
1060
1061 It is also possible to skip events generated (instructions, branches, transactions)
1062 at the beginning. This is useful to ignore initialization code.
1063
1064 --itrace=i0nss1000000
1065
1066 skips the first million instructions.
1067
1068 The q option changes the way the trace is decoded. The decoding is much faster
1069 but much less detailed. Specifically, with the q option, the decoder does not
1070 decode TNT packets, and does not walk object code, but gets the ip from FUP and
1071 TIP packets. The q option can be used with the b and i options but the period
1072 is not used. The q option decodes more quickly, but is useful only if the
1073 control flow of interest is represented or indicated by FUP, TIP, TIP.PGE, or
1074 TIP.PGD packets (refer below). However the q option could be used to find time
1075 ranges that could then be decoded fully using the --time option.
1076
1077 What will *not* be decoded with the (single) q option:
1078
1079 - direct calls and jmps
1080 - conditional branches
1081 - non-branch instructions
1082
1083 What *will* be decoded with the (single) q option:
1084
1085 - asynchronous branches such as interrupts
1086 - indirect branches
1087 - function return target address *if* the noretcomp config term (refer
1088 config terms section) was used
1089 - start of (control-flow) tracing
1090 - end of (control-flow) tracing, if it is not out of context
1091 - power events, ptwrite, transaction start and abort
1092 - instruction pointer associated with PSB packets
1093
1094 Note the q option does not specify what events will be synthesized e.g. the p
1095 option must be used also to show power events.
1096
1097 Repeating the q option (double-q i.e. qq) results in even faster decoding and even
1098 less detail. The decoder decodes only extended PSB (PSB+) packets, getting the
1099 instruction pointer if there is a FUP packet within PSB+ (i.e. between PSB and
1100 PSBEND). Note PSB packets occur regularly in the trace based on the psb_period
1101 config term (refer config terms section). There will be a FUP packet if the
1102 PSB+ occurs while control flow is being traced.
1103
1104 What will *not* be decoded with the qq option:
1105
1106 - everything except instruction pointer associated with PSB packets
1107
1108 What *will* be decoded with the qq option:
1109
1110 - instruction pointer associated with PSB packets
1111
1112 The Z option is equivalent to having recorded a trace without TSC
1113 (i.e. config term tsc=0). It can be useful to avoid timestamp issues when
1114 decoding a trace of a virtual machine.
1115
1116
1117 dlfilter-show-cycles.so
1118 ~~~~~~~~~~~~~~~~~~~~~~~
1119
1120 Cycles can be displayed using dlfilter-show-cycles.so in which case the itrace A
1121 option can be useful to provide higher granularity cycle information:
1122
1123 perf script --itrace=A --call-trace --dlfilter dlfilter-show-cycles.so
1124
1125 To see a list of dlfilters:
1126
1127 perf script -v --list-dlfilters
1128
1129 See also linkperf:perf-dlfilters[1]
1130
1131
1132 dump option
1133 ~~~~~~~~~~~
1134
1135 perf script has an option (-D) to "dump" the events i.e. display the binary
1136 data.
1137
1138 When -D is used, Intel PT packets are displayed. The packet decoder does not
1139 pay attention to PSB packets, but just decodes the bytes - so the packets seen
1140 by the actual decoder may not be identical in places where the data is corrupt.
1141 One example of that would be when the buffer-switching interrupt has been too
1142 slow, and the buffer has been filled completely. In that case, the last packet
1143 in the buffer might be truncated and immediately followed by a PSB as the trace
1144 continues in the next buffer.
1145
1146 To disable the display of Intel PT packets, combine the -D option with
1147 --no-itrace.
1148
1149
1150 perf report
1151 -----------
1152
1153 By default, perf report will decode trace data found in the perf.data file.
1154 This can be further controlled by new option --itrace exactly the same as
1155 perf script, with the exception that the default is --itrace=igxe.
1156
1157
1158 perf inject
1159 -----------
1160
1161 perf inject also accepts the --itrace option in which case tracing data is
1162 removed and replaced with the synthesized events. e.g.
1163
1164 perf inject --itrace -i perf.data -o perf.data.new
1165
1166 Below is an example of using Intel PT with autofdo. It requires autofdo
1167 (https://github.com/google/autofdo) and gcc version 5. The bubble
1168 sort example is from the AutoFDO tutorial (https://gcc.gnu.org/wiki/AutoFDO/Tutorial)
1169 amended to take the number of elements as a parameter.
1170
1171 $ gcc-5 -O3 sort.c -o sort_optimized
1172 $ ./sort_optimized 30000
1173 Bubble sorting array of 30000 elements
1174 2254 ms
1175
1176 $ cat ~/.perfconfig
1177 [intel-pt]
1178 mispred-all = on
1179
1180 $ perf record -e intel_pt//u ./sort 3000
1181 Bubble sorting array of 3000 elements
1182 58 ms
1183 [ perf record: Woken up 2 times to write data ]
1184 [ perf record: Captured and wrote 3.939 MB perf.data ]
1185 $ perf inject -i perf.data -o inj --itrace=i100usle --strip
1186 $ ./create_gcov --binary=./sort --profile=inj --gcov=sort.gcov -gcov_version=1
1187 $ gcc-5 -O3 -fauto-profile=sort.gcov sort.c -o sort_autofdo
1188 $ ./sort_autofdo 30000
1189 Bubble sorting array of 30000 elements
1190 2155 ms
1191
1192 Note there is currently no advantage to using Intel PT instead of LBR, but
1193 that may change in the future if greater use is made of the data.
1194
1195
1196 PEBS via Intel PT
1197 -----------------
1198
1199 Some hardware has the feature to redirect PEBS records to the Intel PT trace.
1200 Recording is selected by using the aux-output config term e.g.
1201
1202 perf record -c 10000 -e '{intel_pt/branch=0/,cycles/aux-output/ppp}' uname
1203
1204 Originally, software only supported redirecting at most one PEBS event because it
1205 was not able to differentiate one event from another. To overcome that, more recent
1206 kernels and perf tools add support for the PERF_RECORD_AUX_OUTPUT_HW_ID side-band event.
1207 To check for the presence of that event in a PEBS-via-PT trace:
1208
1209 perf script -D --no-itrace | grep PERF_RECORD_AUX_OUTPUT_HW_ID
1210
1211 To display PEBS events from the Intel PT trace, use the itrace 'o' option e.g.
1212
1213 perf script --itrace=oe
1214
1215 XED
1216 ---
1217
1218 include::build-xed.txt[]
1219
1220
1221 Tracing Virtual Machines (kernel only)
1222 --------------------------------------
1223
1224 Currently, kernel tracing is supported with either "timeless" decoding
1225 (i.e. no TSC timestamps) or VM Time Correlation. VM Time Correlation is an extra step
1226 using 'perf inject' and requires unchanging VMX TSC Offset and no VMX TSC Scaling.
1227
1228 Other limitations and caveats
1229
1230 VMX controls may suppress packets needed for decoding resulting in decoding errors
1231 VMX controls may block the perf NMI to the host potentially resulting in lost trace data
1232 Guest kernel self-modifying code (e.g. jump labels or JIT-compiled eBPF) will result in decoding errors
1233 Guest thread information is unknown
1234 Guest VCPU is unknown but may be able to be inferred from the host thread
1235 Callchains are not supported
1236
1237 Example using "timeless" decoding
1238
1239 Start VM
1240
1241 $ sudo virsh start kubuntu20.04
1242 Domain kubuntu20.04 started
1243
1244 Mount the guest file system. Note sshfs needs -o direct_io to enable reading of proc files. root access is needed to read /proc/kcore.
1245
1246 $ mkdir vm0
1247 $ sshfs -o direct_io root@vm0:/ vm0
1248
1249 Copy the guest /proc/kallsyms, /proc/modules and /proc/kcore
1250
1251 $ perf buildid-cache -v --kcore vm0/proc/kcore
1252 kcore added to build-id cache directory /home/user/.debug/[kernel.kcore]/9600f316a53a0f54278885e8d9710538ec5f6a08/2021021807494306
1253 $ KALLSYMS=/home/user/.debug/[kernel.kcore]/9600f316a53a0f54278885e8d9710538ec5f6a08/2021021807494306/kallsyms
1254
1255 Find the VM process
1256
1257 $ ps -eLl | grep 'KVM\|PID'
1258 F S UID PID PPID LWP C PRI NI ADDR SZ WCHAN TTY TIME CMD
1259 3 S 64055 1430 1 1440 1 80 0 - 1921718 - ? 00:02:47 CPU 0/KVM
1260 3 S 64055 1430 1 1441 1 80 0 - 1921718 - ? 00:02:41 CPU 1/KVM
1261 3 S 64055 1430 1 1442 1 80 0 - 1921718 - ? 00:02:38 CPU 2/KVM
1262 3 S 64055 1430 1 1443 2 80 0 - 1921718 - ? 00:03:18 CPU 3/KVM
1263
1264 Start an open-ended perf record, tracing the VM process, do something on the VM, and then ctrl-C to stop.
1265 TSC is not supported and tsc=0 must be specified. That means mtc is useless, so add mtc=0.
1266 However, IPC can still be determined, hence cyc=1 can be added.
1267 Only kernel decoding is supported, so 'k' must be specified.
1268 Intel PT traces both the host and the guest so --guest and --host need to be specified.
1269 Without timestamps, --per-thread must be specified to distinguish threads.
1270
1271 $ sudo perf kvm --guest --host --guestkallsyms $KALLSYMS record --kcore -e intel_pt/tsc=0,mtc=0,cyc=1/k -p 1430 --per-thread
1272 ^C
1273 [ perf record: Woken up 1 times to write data ]
1274 [ perf record: Captured and wrote 5.829 MB ]
1275
1276 perf script can be used to provide an instruction trace
1277
1278 $ perf script --guestkallsyms $KALLSYMS --insn-trace --xed -F+ipc | grep -C10 vmresume | head -21
1279 CPU 0/KVM 1440 ffffffff82133cdd __vmx_vcpu_run+0x3d ([kernel.kallsyms]) movq 0x48(%rax), %r9
1280 CPU 0/KVM 1440 ffffffff82133ce1 __vmx_vcpu_run+0x41 ([kernel.kallsyms]) movq 0x50(%rax), %r10
1281 CPU 0/KVM 1440 ffffffff82133ce5 __vmx_vcpu_run+0x45 ([kernel.kallsyms]) movq 0x58(%rax), %r11
1282 CPU 0/KVM 1440 ffffffff82133ce9 __vmx_vcpu_run+0x49 ([kernel.kallsyms]) movq 0x60(%rax), %r12
1283 CPU 0/KVM 1440 ffffffff82133ced __vmx_vcpu_run+0x4d ([kernel.kallsyms]) movq 0x68(%rax), %r13
1284 CPU 0/KVM 1440 ffffffff82133cf1 __vmx_vcpu_run+0x51 ([kernel.kallsyms]) movq 0x70(%rax), %r14
1285 CPU 0/KVM 1440 ffffffff82133cf5 __vmx_vcpu_run+0x55 ([kernel.kallsyms]) movq 0x78(%rax), %r15
1286 CPU 0/KVM 1440 ffffffff82133cf9 __vmx_vcpu_run+0x59 ([kernel.kallsyms]) movq (%rax), %rax
1287 CPU 0/KVM 1440 ffffffff82133cfc __vmx_vcpu_run+0x5c ([kernel.kallsyms]) callq 0xffffffff82133c40
1288 CPU 0/KVM 1440 ffffffff82133c40 vmx_vmenter+0x0 ([kernel.kallsyms]) jz 0xffffffff82133c46
1289 CPU 0/KVM 1440 ffffffff82133c42 vmx_vmenter+0x2 ([kernel.kallsyms]) vmresume IPC: 0.11 (50/445)
1290 :1440 1440 ffffffffbb678b06 native_write_msr+0x6 ([guest.kernel.kallsyms]) nopl %eax, (%rax,%rax,1)
1291 :1440 1440 ffffffffbb678b0b native_write_msr+0xb ([guest.kernel.kallsyms]) retq IPC: 0.04 (2/41)
1292 :1440 1440 ffffffffbb666646 lapic_next_deadline+0x26 ([guest.kernel.kallsyms]) data16 nop
1293 :1440 1440 ffffffffbb666648 lapic_next_deadline+0x28 ([guest.kernel.kallsyms]) xor %eax, %eax
1294 :1440 1440 ffffffffbb66664a lapic_next_deadline+0x2a ([guest.kernel.kallsyms]) popq %rbp
1295 :1440 1440 ffffffffbb66664b lapic_next_deadline+0x2b ([guest.kernel.kallsyms]) retq IPC: 0.16 (4/25)
1296 :1440 1440 ffffffffbb74607f clockevents_program_event+0x8f ([guest.kernel.kallsyms]) test %eax, %eax
1297 :1440 1440 ffffffffbb746081 clockevents_program_event+0x91 ([guest.kernel.kallsyms]) jz 0xffffffffbb74603c IPC: 0.06 (2/30)
1298 :1440 1440 ffffffffbb74603c clockevents_program_event+0x4c ([guest.kernel.kallsyms]) popq %rbx
1299 :1440 1440 ffffffffbb74603d clockevents_program_event+0x4d ([guest.kernel.kallsyms]) popq %r12
1300
1301 Example using VM Time Correlation
1302
1303 Start VM
1304
1305 $ sudo virsh start kubuntu20.04
1306 Domain kubuntu20.04 started
1307
1308 Mount the guest file system. Note sshfs needs -o direct_io to enable reading of proc files. root access is needed to read /proc/kcore.
1309
1310 $ mkdir -p vm0
1311 $ sshfs -o direct_io root@vm0:/ vm0
1312
1313 Copy the guest /proc/kallsyms, /proc/modules and /proc/kcore
1314
1315 $ perf buildid-cache -v --kcore vm0/proc/kcore
1316 same kcore found in /home/user/.debug/[kernel.kcore]/cc9c55a98c5e4ec0aeda69302554aabed5cd6491/2021021312450777
1317 $ KALLSYMS=/home/user/.debug/\[kernel.kcore\]/cc9c55a98c5e4ec0aeda69302554aabed5cd6491/2021021312450777/kallsyms
1318
1319 Find the VM process
1320
1321 $ ps -eLl | grep 'KVM\|PID'
1322 F S UID PID PPID LWP C PRI NI ADDR SZ WCHAN TTY TIME CMD
1323 3 S 64055 16998 1 17005 13 80 0 - 1818189 - ? 00:00:16 CPU 0/KVM
1324 3 S 64055 16998 1 17006 4 80 0 - 1818189 - ? 00:00:05 CPU 1/KVM
1325 3 S 64055 16998 1 17007 3 80 0 - 1818189 - ? 00:00:04 CPU 2/KVM
1326 3 S 64055 16998 1 17008 4 80 0 - 1818189 - ? 00:00:05 CPU 3/KVM
1327
1328 Start an open-ended perf record, tracing the VM process, do something on the VM, and then ctrl-C to stop.
1329 IPC can be determined, hence cyc=1 can be added.
1330 Only kernel decoding is supported, so 'k' must be specified.
1331 Intel PT traces both the host and the guest so --guest and --host need to be specified.
1332
1333 $ sudo perf kvm --guest --host --guestkallsyms $KALLSYMS record --kcore -e intel_pt/cyc=1/k -p 16998
1334 ^C[ perf record: Woken up 1 times to write data ]
1335 [ perf record: Captured and wrote 9.041 MB perf.data.kvm ]
1336
1337 Now 'perf inject' can be used to determine the VMX TCS Offset. Note, Intel PT TSC packets are
1338 only 7-bytes, so the TSC Offset might differ from the actual value in the 8th byte. That will
1339 have no effect i.e. the resulting timestamps will be correct anyway.
1340
1341 $ perf inject -i perf.data.kvm --vm-time-correlation=dry-run
1342 ERROR: Unknown TSC Offset for VMCS 0x1bff6a
1343 VMCS: 0x1bff6a TSC Offset 0xffffe42722c64c41
1344 ERROR: Unknown TSC Offset for VMCS 0x1cbc08
1345 VMCS: 0x1cbc08 TSC Offset 0xffffe42722c64c41
1346 ERROR: Unknown TSC Offset for VMCS 0x1c3ce8
1347 VMCS: 0x1c3ce8 TSC Offset 0xffffe42722c64c41
1348 ERROR: Unknown TSC Offset for VMCS 0x1cbce9
1349 VMCS: 0x1cbce9 TSC Offset 0xffffe42722c64c41
1350
1351 Each virtual CPU has a different Virtual Machine Control Structure (VMCS)
1352 shown above with the calculated TSC Offset. For an unchanging TSC Offset
1353 they should all be the same for the same virtual machine.
1354
1355 Now that the TSC Offset is known, it can be provided to 'perf inject'
1356
1357 $ perf inject -i perf.data.kvm --vm-time-correlation="dry-run 0xffffe42722c64c41"
1358
1359 Note the options for 'perf inject' --vm-time-correlation are:
1360
1361 [ dry-run ] [ <TSC Offset> [ : <VMCS> [ , <VMCS> ]... ] ]...
1362
1363 So it is possible to specify different TSC Offsets for different VMCS.
1364 The option "dry-run" will cause the file to be processed but without updating it.
1365 Note it is also possible to get a intel_pt.log file by adding option --itrace=d
1366
1367 There were no errors so, do it for real
1368
1369 $ perf inject -i perf.data.kvm --vm-time-correlation=0xffffe42722c64c41 --force
1370
1371 'perf script' can be used to see if there are any decoder errors
1372
1373 $ perf script -i perf.data.kvm --guestkallsyms $KALLSYMS --itrace=e-o
1374
1375 There were none.
1376
1377 'perf script' can be used to provide an instruction trace showing timestamps
1378
1379 $ perf script -i perf.data.kvm --guestkallsyms $KALLSYMS --insn-trace --xed -F+ipc | grep -C10 vmresume | head -21
1380 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133cdd __vmx_vcpu_run+0x3d ([kernel.kallsyms]) movq 0x48(%rax), %r9
1381 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133ce1 __vmx_vcpu_run+0x41 ([kernel.kallsyms]) movq 0x50(%rax), %r10
1382 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133ce5 __vmx_vcpu_run+0x45 ([kernel.kallsyms]) movq 0x58(%rax), %r11
1383 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133ce9 __vmx_vcpu_run+0x49 ([kernel.kallsyms]) movq 0x60(%rax), %r12
1384 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133ced __vmx_vcpu_run+0x4d ([kernel.kallsyms]) movq 0x68(%rax), %r13
1385 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133cf1 __vmx_vcpu_run+0x51 ([kernel.kallsyms]) movq 0x70(%rax), %r14
1386 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133cf5 __vmx_vcpu_run+0x55 ([kernel.kallsyms]) movq 0x78(%rax), %r15
1387 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133cf9 __vmx_vcpu_run+0x59 ([kernel.kallsyms]) movq (%rax), %rax
1388 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133cfc __vmx_vcpu_run+0x5c ([kernel.kallsyms]) callq 0xffffffff82133c40
1389 CPU 1/KVM 17006 [001] 11500.262865593: ffffffff82133c40 vmx_vmenter+0x0 ([kernel.kallsyms]) jz 0xffffffff82133c46
1390 CPU 1/KVM 17006 [001] 11500.262866075: ffffffff82133c42 vmx_vmenter+0x2 ([kernel.kallsyms]) vmresume IPC: 0.05 (40/769)
1391 :17006 17006 [001] 11500.262869216: ffffffff82200cb0 asm_sysvec_apic_timer_interrupt+0x0 ([guest.kernel.kallsyms]) clac
1392 :17006 17006 [001] 11500.262869216: ffffffff82200cb3 asm_sysvec_apic_timer_interrupt+0x3 ([guest.kernel.kallsyms]) pushq $0xffffffffffffffff
1393 :17006 17006 [001] 11500.262869216: ffffffff82200cb5 asm_sysvec_apic_timer_interrupt+0x5 ([guest.kernel.kallsyms]) callq 0xffffffff82201160
1394 :17006 17006 [001] 11500.262869216: ffffffff82201160 error_entry+0x0 ([guest.kernel.kallsyms]) cld
1395 :17006 17006 [001] 11500.262869216: ffffffff82201161 error_entry+0x1 ([guest.kernel.kallsyms]) pushq %rsi
1396 :17006 17006 [001] 11500.262869216: ffffffff82201162 error_entry+0x2 ([guest.kernel.kallsyms]) movq 0x8(%rsp), %rsi
1397 :17006 17006 [001] 11500.262869216: ffffffff82201167 error_entry+0x7 ([guest.kernel.kallsyms]) movq %rdi, 0x8(%rsp)
1398 :17006 17006 [001] 11500.262869216: ffffffff8220116c error_entry+0xc ([guest.kernel.kallsyms]) pushq %rdx
1399 :17006 17006 [001] 11500.262869216: ffffffff8220116d error_entry+0xd ([guest.kernel.kallsyms]) pushq %rcx
1400 :17006 17006 [001] 11500.262869216: ffffffff8220116e error_entry+0xe ([guest.kernel.kallsyms]) pushq %rax
1401
1402
1403 Tracing Virtual Machines (including user space)
1404 -----------------------------------------------
1405
1406 It is possible to use perf record to record sideband events within a virtual machine, so that an Intel PT trace on the host can be decoded.
1407 Sideband events from the guest perf.data file can be injected into the host perf.data file using perf inject.
1408
1409 Here is an example of the steps needed:
1410
1411 On the guest machine:
1412
1413 Check that no-kvmclock kernel command line option was used to boot:
1414
1415 Note, this is essential to enable time correlation between host and guest machines.
1416
1417 $ cat /proc/cmdline
1418 BOOT_IMAGE=/boot/vmlinuz-5.10.0-16-amd64 root=UUID=cb49c910-e573-47e0-bce7-79e293df8e1d ro no-kvmclock
1419
1420 There is no BPF support at present so, if possible, disable JIT compiling:
1421
1422 $ echo 0 | sudo tee /proc/sys/net/core/bpf_jit_enable
1423 0
1424
1425 Start perf record to collect sideband events:
1426
1427 $ sudo perf record -o guest-sideband-testing-guest-perf.data --sample-identifier --buildid-all --switch-events --kcore -a -e dummy
1428
1429 On the host machine:
1430
1431 Start perf record to collect Intel PT trace:
1432
1433 Note, the host trace will get very big, very fast, so the steps from starting to stopping the host trace really need to be done so that they happen in the shortest time possible.
1434
1435 $ sudo perf record -o guest-sideband-testing-host-perf.data -m,64M --kcore -a -e intel_pt/cyc/
1436
1437 On the guest machine:
1438
1439 Run a small test case, just 'uname' in this example:
1440
1441 $ uname
1442 Linux
1443
1444 On the host machine:
1445
1446 Stop the Intel PT trace:
1447
1448 ^C
1449 [ perf record: Woken up 1 times to write data ]
1450 [ perf record: Captured and wrote 76.122 MB guest-sideband-testing-host-perf.data ]
1451
1452 On the guest machine:
1453
1454 Stop the Intel PT trace:
1455
1456 ^C
1457 [ perf record: Woken up 1 times to write data ]
1458 [ perf record: Captured and wrote 1.247 MB guest-sideband-testing-guest-perf.data ]
1459
1460 And then copy guest-sideband-testing-guest-perf.data to the host (not shown here).
1461
1462 On the host machine:
1463
1464 With the 2 perf.data recordings, and with their ownership changed to the user.
1465
1466 Identify the TSC Offset:
1467
1468 $ perf inject -i guest-sideband-testing-host-perf.data --vm-time-correlation=dry-run
1469 VMCS: 0x103fc6 TSC Offset 0xfffffa6ae070cb20
1470 VMCS: 0x103ff2 TSC Offset 0xfffffa6ae070cb20
1471 VMCS: 0x10fdaa TSC Offset 0xfffffa6ae070cb20
1472 VMCS: 0x24d57c TSC Offset 0xfffffa6ae070cb20
1473
1474 Correct Intel PT TSC timestamps for the guest machine:
1475
1476 $ perf inject -i guest-sideband-testing-host-perf.data --vm-time-correlation=0xfffffa6ae070cb20 --force
1477
1478 Identify the guest machine PID:
1479
1480 $ perf script -i guest-sideband-testing-host-perf.data --no-itrace --show-task-events | grep KVM
1481 CPU 0/KVM 0 [000] 0.000000: PERF_RECORD_COMM: CPU 0/KVM:13376/13381
1482 CPU 1/KVM 0 [000] 0.000000: PERF_RECORD_COMM: CPU 1/KVM:13376/13382
1483 CPU 2/KVM 0 [000] 0.000000: PERF_RECORD_COMM: CPU 2/KVM:13376/13383
1484 CPU 3/KVM 0 [000] 0.000000: PERF_RECORD_COMM: CPU 3/KVM:13376/13384
1485
1486 Note, the QEMU option -name debug-threads=on is needed so that thread names
1487 can be used to determine which thread is running which VCPU as above. libvirt seems to use this by default.
1488
1489 Create a guestmount, assuming the guest machine is 'vm_to_test':
1490
1491 $ mkdir -p ~/guestmount/13376
1492 $ sshfs -o direct_io vm_to_test:/ ~/guestmount/13376
1493
1494 Inject the guest perf.data file into the host perf.data file:
1495
1496 Note, due to the guestmount option, guest object files and debug files will be copied into the build ID cache from the guest machine, with the notable exception of VDSO.
1497 If needed, VDSO can be copied manually in a fashion similar to that used by the perf-archive script.
1498
1499 $ perf inject -i guest-sideband-testing-host-perf.data -o inj --guestmount ~/guestmount --guest-data=guest-sideband-testing-guest-perf.data,13376,0xfffffa6ae070cb20
1500
1501 Show an excerpt from the result. In this case the CPU and time range have been to chosen to show interaction between guest and host when 'uname' is starting to run on the guest machine:
1502
1503 Notes:
1504
1505 - the CPU displayed, [002] in this case, is always the host CPU
1506 - events happening in the virtual machine start with VM:13376 VCPU:003, which shows the hypervisor PID 13376 and the VCPU number
1507 - only calls and errors are displayed i.e. --itrace=ce
1508 - branches entering and exiting the virtual machine are split, and show as 2 branches to/from "0 [unknown] ([unknown])"
1509
1510 $ perf script -i inj --itrace=ce -F+machine_pid,+vcpu,+addr,+pid,+tid,-period --ns --time 7919.408803365,7919.408804631 -C 2
1511 CPU 3/KVM 13376/13384 [002] 7919.408803365: branches: ffffffffc0f8ebe0 vmx_vcpu_enter_exit+0xc0 ([kernel.kallsyms]) => ffffffffc0f8edc0 __vmx_vcpu_run+0x0 ([kernel.kallsyms])
1512 CPU 3/KVM 13376/13384 [002] 7919.408803365: branches: ffffffffc0f8edd5 __vmx_vcpu_run+0x15 ([kernel.kallsyms]) => ffffffffc0f8eca0 vmx_update_host_rsp+0x0 ([kernel.kallsyms])
1513 CPU 3/KVM 13376/13384 [002] 7919.408803365: branches: ffffffffc0f8ee1b __vmx_vcpu_run+0x5b ([kernel.kallsyms]) => ffffffffc0f8ed60 vmx_vmenter+0x0 ([kernel.kallsyms])
1514 CPU 3/KVM 13376/13384 [002] 7919.408803461: branches: ffffffffc0f8ed62 vmx_vmenter+0x2 ([kernel.kallsyms]) => 0 [unknown] ([unknown])
1515 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408803461: branches: 0 [unknown] ([unknown]) => 7f851c9b5a5c init_cacheinfo+0x3ac (/usr/lib/x86_64-linux-gnu/libc-2.31.so)
1516 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408803567: branches: 7f851c9b5a5a init_cacheinfo+0x3aa (/usr/lib/x86_64-linux-gnu/libc-2.31.so) => 0 [unknown] ([unknown])
1517 CPU 3/KVM 13376/13384 [002] 7919.408803567: branches: 0 [unknown] ([unknown]) => ffffffffc0f8ed80 vmx_vmexit+0x0 ([kernel.kallsyms])
1518 CPU 3/KVM 13376/13384 [002] 7919.408803596: branches: ffffffffc0f6619a vmx_vcpu_run+0x26a ([kernel.kallsyms]) => ffffffffb2255c60 x86_virt_spec_ctrl+0x0 ([kernel.kallsyms])
1519 CPU 3/KVM 13376/13384 [002] 7919.408803801: branches: ffffffffc0f66445 vmx_vcpu_run+0x515 ([kernel.kallsyms]) => ffffffffb2290b30 native_write_msr+0x0 ([kernel.kallsyms])
1520 CPU 3/KVM 13376/13384 [002] 7919.408803850: branches: ffffffffc0f661f8 vmx_vcpu_run+0x2c8 ([kernel.kallsyms]) => ffffffffc1092300 kvm_load_host_xsave_state+0x0 ([kernel.kallsyms])
1521 CPU 3/KVM 13376/13384 [002] 7919.408803850: branches: ffffffffc1092327 kvm_load_host_xsave_state+0x27 ([kernel.kallsyms]) => ffffffffc1092220 kvm_load_host_xsave_state.part.0+0x0 ([kernel.kallsyms])
1522 CPU 3/KVM 13376/13384 [002] 7919.408803862: branches: ffffffffc0f662cf vmx_vcpu_run+0x39f ([kernel.kallsyms]) => ffffffffc0f63f90 vmx_recover_nmi_blocking+0x0 ([kernel.kallsyms])
1523 CPU 3/KVM 13376/13384 [002] 7919.408803862: branches: ffffffffc0f662e9 vmx_vcpu_run+0x3b9 ([kernel.kallsyms]) => ffffffffc0f619a0 __vmx_complete_interrupts+0x0 ([kernel.kallsyms])
1524 CPU 3/KVM 13376/13384 [002] 7919.408803872: branches: ffffffffc109cfb2 vcpu_enter_guest+0x752 ([kernel.kallsyms]) => ffffffffc0f5f570 vmx_handle_exit_irqoff+0x0 ([kernel.kallsyms])
1525 CPU 3/KVM 13376/13384 [002] 7919.408803881: branches: ffffffffc109d028 vcpu_enter_guest+0x7c8 ([kernel.kallsyms]) => ffffffffb234f900 __srcu_read_lock+0x0 ([kernel.kallsyms])
1526 CPU 3/KVM 13376/13384 [002] 7919.408803897: branches: ffffffffc109d06f vcpu_enter_guest+0x80f ([kernel.kallsyms]) => ffffffffc0f72e30 vmx_handle_exit+0x0 ([kernel.kallsyms])
1527 CPU 3/KVM 13376/13384 [002] 7919.408803897: branches: ffffffffc0f72e3d vmx_handle_exit+0xd ([kernel.kallsyms]) => ffffffffc0f727c0 __vmx_handle_exit+0x0 ([kernel.kallsyms])
1528 CPU 3/KVM 13376/13384 [002] 7919.408803897: branches: ffffffffc0f72b15 __vmx_handle_exit+0x355 ([kernel.kallsyms]) => ffffffffc0f60ae0 vmx_flush_pml_buffer+0x0 ([kernel.kallsyms])
1529 CPU 3/KVM 13376/13384 [002] 7919.408803903: branches: ffffffffc0f72994 __vmx_handle_exit+0x1d4 ([kernel.kallsyms]) => ffffffffc10b7090 kvm_emulate_cpuid+0x0 ([kernel.kallsyms])
1530 CPU 3/KVM 13376/13384 [002] 7919.408803903: branches: ffffffffc10b70f1 kvm_emulate_cpuid+0x61 ([kernel.kallsyms]) => ffffffffc10b6e10 kvm_cpuid+0x0 ([kernel.kallsyms])
1531 CPU 3/KVM 13376/13384 [002] 7919.408803941: branches: ffffffffc10b7125 kvm_emulate_cpuid+0x95 ([kernel.kallsyms]) => ffffffffc1093110 kvm_skip_emulated_instruction+0x0 ([kernel.kallsyms])
1532 CPU 3/KVM 13376/13384 [002] 7919.408803941: branches: ffffffffc109311f kvm_skip_emulated_instruction+0xf ([kernel.kallsyms]) => ffffffffc0f5e180 vmx_get_rflags+0x0 ([kernel.kallsyms])
1533 CPU 3/KVM 13376/13384 [002] 7919.408803951: branches: ffffffffc109312a kvm_skip_emulated_instruction+0x1a ([kernel.kallsyms]) => ffffffffc0f5fd30 vmx_skip_emulated_instruction+0x0 ([kernel.kallsyms])
1534 CPU 3/KVM 13376/13384 [002] 7919.408803951: branches: ffffffffc0f5fd79 vmx_skip_emulated_instruction+0x49 ([kernel.kallsyms]) => ffffffffc0f5fb50 skip_emulated_instruction+0x0 ([kernel.kallsyms])
1535 CPU 3/KVM 13376/13384 [002] 7919.408803956: branches: ffffffffc0f5fc68 skip_emulated_instruction+0x118 ([kernel.kallsyms]) => ffffffffc0f6a940 vmx_cache_reg+0x0 ([kernel.kallsyms])
1536 CPU 3/KVM 13376/13384 [002] 7919.408803964: branches: ffffffffc0f5fc11 skip_emulated_instruction+0xc1 ([kernel.kallsyms]) => ffffffffc0f5f9e0 vmx_set_interrupt_shadow+0x0 ([kernel.kallsyms])
1537 CPU 3/KVM 13376/13384 [002] 7919.408803980: branches: ffffffffc109f8b1 vcpu_run+0x71 ([kernel.kallsyms]) => ffffffffc10ad2f0 kvm_cpu_has_pending_timer+0x0 ([kernel.kallsyms])
1538 CPU 3/KVM 13376/13384 [002] 7919.408803980: branches: ffffffffc10ad2fb kvm_cpu_has_pending_timer+0xb ([kernel.kallsyms]) => ffffffffc10b0490 apic_has_pending_timer+0x0 ([kernel.kallsyms])
1539 CPU 3/KVM 13376/13384 [002] 7919.408803991: branches: ffffffffc109f899 vcpu_run+0x59 ([kernel.kallsyms]) => ffffffffc109c860 vcpu_enter_guest+0x0 ([kernel.kallsyms])
1540 CPU 3/KVM 13376/13384 [002] 7919.408803993: branches: ffffffffc109cd4c vcpu_enter_guest+0x4ec ([kernel.kallsyms]) => ffffffffc0f69140 vmx_prepare_switch_to_guest+0x0 ([kernel.kallsyms])
1541 CPU 3/KVM 13376/13384 [002] 7919.408803996: branches: ffffffffc109cd7d vcpu_enter_guest+0x51d ([kernel.kallsyms]) => ffffffffb234f930 __srcu_read_unlock+0x0 ([kernel.kallsyms])
1542 CPU 3/KVM 13376/13384 [002] 7919.408803996: branches: ffffffffc109cd9c vcpu_enter_guest+0x53c ([kernel.kallsyms]) => ffffffffc0f609b0 vmx_sync_pir_to_irr+0x0 ([kernel.kallsyms])
1543 CPU 3/KVM 13376/13384 [002] 7919.408803996: branches: ffffffffc0f60a6d vmx_sync_pir_to_irr+0xbd ([kernel.kallsyms]) => ffffffffc10adc20 kvm_lapic_find_highest_irr+0x0 ([kernel.kallsyms])
1544 CPU 3/KVM 13376/13384 [002] 7919.408804010: branches: ffffffffc0f60abd vmx_sync_pir_to_irr+0x10d ([kernel.kallsyms]) => ffffffffc0f60820 vmx_set_rvi+0x0 ([kernel.kallsyms])
1545 CPU 3/KVM 13376/13384 [002] 7919.408804019: branches: ffffffffc109ceca vcpu_enter_guest+0x66a ([kernel.kallsyms]) => ffffffffb2249840 fpregs_assert_state_consistent+0x0 ([kernel.kallsyms])
1546 CPU 3/KVM 13376/13384 [002] 7919.408804021: branches: ffffffffc109cf10 vcpu_enter_guest+0x6b0 ([kernel.kallsyms]) => ffffffffc0f65f30 vmx_vcpu_run+0x0 ([kernel.kallsyms])
1547 CPU 3/KVM 13376/13384 [002] 7919.408804024: branches: ffffffffc0f6603b vmx_vcpu_run+0x10b ([kernel.kallsyms]) => ffffffffb229bed0 __get_current_cr3_fast+0x0 ([kernel.kallsyms])
1548 CPU 3/KVM 13376/13384 [002] 7919.408804024: branches: ffffffffc0f66055 vmx_vcpu_run+0x125 ([kernel.kallsyms]) => ffffffffb2253050 cr4_read_shadow+0x0 ([kernel.kallsyms])
1549 CPU 3/KVM 13376/13384 [002] 7919.408804030: branches: ffffffffc0f6608d vmx_vcpu_run+0x15d ([kernel.kallsyms]) => ffffffffc10921e0 kvm_load_guest_xsave_state+0x0 ([kernel.kallsyms])
1550 CPU 3/KVM 13376/13384 [002] 7919.408804030: branches: ffffffffc1092207 kvm_load_guest_xsave_state+0x27 ([kernel.kallsyms]) => ffffffffc1092110 kvm_load_guest_xsave_state.part.0+0x0 ([kernel.kallsyms])
1551 CPU 3/KVM 13376/13384 [002] 7919.408804032: branches: ffffffffc0f660c6 vmx_vcpu_run+0x196 ([kernel.kallsyms]) => ffffffffb22061a0 perf_guest_get_msrs+0x0 ([kernel.kallsyms])
1552 CPU 3/KVM 13376/13384 [002] 7919.408804032: branches: ffffffffb22061a9 perf_guest_get_msrs+0x9 ([kernel.kallsyms]) => ffffffffb220cda0 intel_guest_get_msrs+0x0 ([kernel.kallsyms])
1553 CPU 3/KVM 13376/13384 [002] 7919.408804039: branches: ffffffffc0f66109 vmx_vcpu_run+0x1d9 ([kernel.kallsyms]) => ffffffffc0f652c0 clear_atomic_switch_msr+0x0 ([kernel.kallsyms])
1554 CPU 3/KVM 13376/13384 [002] 7919.408804040: branches: ffffffffc0f66119 vmx_vcpu_run+0x1e9 ([kernel.kallsyms]) => ffffffffc0f73f60 intel_pmu_lbr_is_enabled+0x0 ([kernel.kallsyms])
1555 CPU 3/KVM 13376/13384 [002] 7919.408804042: branches: ffffffffc0f73f81 intel_pmu_lbr_is_enabled+0x21 ([kernel.kallsyms]) => ffffffffc10b68e0 kvm_find_cpuid_entry+0x0 ([kernel.kallsyms])
1556 CPU 3/KVM 13376/13384 [002] 7919.408804045: branches: ffffffffc0f66454 vmx_vcpu_run+0x524 ([kernel.kallsyms]) => ffffffffc0f61ff0 vmx_update_hv_timer+0x0 ([kernel.kallsyms])
1557 CPU 3/KVM 13376/13384 [002] 7919.408804057: branches: ffffffffc0f66142 vmx_vcpu_run+0x212 ([kernel.kallsyms]) => ffffffffc10af100 kvm_wait_lapic_expire+0x0 ([kernel.kallsyms])
1558 CPU 3/KVM 13376/13384 [002] 7919.408804057: branches: ffffffffc0f66156 vmx_vcpu_run+0x226 ([kernel.kallsyms]) => ffffffffb2255c60 x86_virt_spec_ctrl+0x0 ([kernel.kallsyms])
1559 CPU 3/KVM 13376/13384 [002] 7919.408804057: branches: ffffffffc0f66161 vmx_vcpu_run+0x231 ([kernel.kallsyms]) => ffffffffc0f8eb20 vmx_vcpu_enter_exit+0x0 ([kernel.kallsyms])
1560 CPU 3/KVM 13376/13384 [002] 7919.408804057: branches: ffffffffc0f8eb44 vmx_vcpu_enter_exit+0x24 ([kernel.kallsyms]) => ffffffffb2353e10 rcu_note_context_switch+0x0 ([kernel.kallsyms])
1561 CPU 3/KVM 13376/13384 [002] 7919.408804057: branches: ffffffffb2353e1c rcu_note_context_switch+0xc ([kernel.kallsyms]) => ffffffffb2353db0 rcu_qs+0x0 ([kernel.kallsyms])
1562 CPU 3/KVM 13376/13384 [002] 7919.408804066: branches: ffffffffc0f8ebe0 vmx_vcpu_enter_exit+0xc0 ([kernel.kallsyms]) => ffffffffc0f8edc0 __vmx_vcpu_run+0x0 ([kernel.kallsyms])
1563 CPU 3/KVM 13376/13384 [002] 7919.408804066: branches: ffffffffc0f8edd5 __vmx_vcpu_run+0x15 ([kernel.kallsyms]) => ffffffffc0f8eca0 vmx_update_host_rsp+0x0 ([kernel.kallsyms])
1564 CPU 3/KVM 13376/13384 [002] 7919.408804066: branches: ffffffffc0f8ee1b __vmx_vcpu_run+0x5b ([kernel.kallsyms]) => ffffffffc0f8ed60 vmx_vmenter+0x0 ([kernel.kallsyms])
1565 CPU 3/KVM 13376/13384 [002] 7919.408804162: branches: ffffffffc0f8ed62 vmx_vmenter+0x2 ([kernel.kallsyms]) => 0 [unknown] ([unknown])
1566 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804162: branches: 0 [unknown] ([unknown]) => 7f851c9b5a5c init_cacheinfo+0x3ac (/usr/lib/x86_64-linux-gnu/libc-2.31.so)
1567 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804273: branches: 7f851cb7c0e4 _dl_init+0x74 (/usr/lib/x86_64-linux-gnu/ld-2.31.so) => 7f851cb7bf50 call_init.part.0+0x0 (/usr/lib/x86_64-linux-gnu/ld-2.31.so)
1568 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804526: branches: 55e0c00136f0 _start+0x0 (/usr/bin/uname) => ffffffff83200ac0 asm_exc_page_fault+0x0 ([kernel.kallsyms])
1569 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804526: branches: ffffffff83200ac3 asm_exc_page_fault+0x3 ([kernel.kallsyms]) => ffffffff83201290 error_entry+0x0 ([kernel.kallsyms])
1570 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804534: branches: ffffffff832012fa error_entry+0x6a ([kernel.kallsyms]) => ffffffff830b59a0 sync_regs+0x0 ([kernel.kallsyms])
1571 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804631: branches: ffffffff83200ad9 asm_exc_page_fault+0x19 ([kernel.kallsyms]) => ffffffff830b8210 exc_page_fault+0x0 ([kernel.kallsyms])
1572 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804631: branches: ffffffff830b82a4 exc_page_fault+0x94 ([kernel.kallsyms]) => ffffffff830b80e0 __kvm_handle_async_pf+0x0 ([kernel.kallsyms])
1573 VM:13376 VCPU:003 uname 3404/3404 [002] 7919.408804631: branches: ffffffff830b80ed __kvm_handle_async_pf+0xd ([kernel.kallsyms]) => ffffffff830b80c0 kvm_read_and_reset_apf_flags+0x0 ([kernel.kallsyms])
1574
1575
1576 Tracing Virtual Machines - Guest Code
1577 -------------------------------------
1578
1579 A common case for KVM test programs is that the test program acts as the
1580 hypervisor, creating, running and destroying the virtual machine, and
1581 providing the guest object code from its own object code. In this case,
1582 the VM is not running an OS, but only the functions loaded into it by the
1583 hypervisor test program, and conveniently, loaded at the same virtual
1584 addresses. To support that, option "--guest-code" has been added to perf script
1585 and perf kvm report.
1586
1587 Here is an example tracing a test program from the kernel's KVM selftests:
1588
1589 # perf record --kcore -e intel_pt/cyc/ -- tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test
1590 [ perf record: Woken up 1 times to write data ]
1591 [ perf record: Captured and wrote 0.280 MB perf.data ]
1592 # perf script --guest-code --itrace=bep --ns -F-period,+addr,+flags
1593 [SNIP]
1594 tsc_msrs_test 18436 [007] 10897.962087733: branches: call ffffffffc13b2ff5 __vmx_vcpu_run+0x15 (vmlinux) => ffffffffc13b2f50 vmx_update_host_rsp+0x0 (vmlinux)
1595 tsc_msrs_test 18436 [007] 10897.962087733: branches: return ffffffffc13b2f5d vmx_update_host_rsp+0xd (vmlinux) => ffffffffc13b2ffa __vmx_vcpu_run+0x1a (vmlinux)
1596 tsc_msrs_test 18436 [007] 10897.962087733: branches: call ffffffffc13b303b __vmx_vcpu_run+0x5b (vmlinux) => ffffffffc13b2f80 vmx_vmenter+0x0 (vmlinux)
1597 tsc_msrs_test 18436 [007] 10897.962087836: branches: vmentry ffffffffc13b2f82 vmx_vmenter+0x2 (vmlinux) => 0 [unknown] ([unknown])
1598 [guest/18436] 18436 [007] 10897.962087836: branches: vmentry 0 [unknown] ([unknown]) => 402c81 guest_code+0x131 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1599 [guest/18436] 18436 [007] 10897.962087836: branches: call 402c81 guest_code+0x131 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 40dba0 ucall+0x0 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1600 [guest/18436] 18436 [007] 10897.962088248: branches: vmexit 40dba0 ucall+0x0 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 0 [unknown] ([unknown])
1601 tsc_msrs_test 18436 [007] 10897.962088248: branches: vmexit 0 [unknown] ([unknown]) => ffffffffc13b2fa0 vmx_vmexit+0x0 (vmlinux)
1602 tsc_msrs_test 18436 [007] 10897.962088248: branches: jmp ffffffffc13b2fa0 vmx_vmexit+0x0 (vmlinux) => ffffffffc13b2fd2 vmx_vmexit+0x32 (vmlinux)
1603 tsc_msrs_test 18436 [007] 10897.962088256: branches: return ffffffffc13b2fd2 vmx_vmexit+0x32 (vmlinux) => ffffffffc13b3040 __vmx_vcpu_run+0x60 (vmlinux)
1604 tsc_msrs_test 18436 [007] 10897.962088270: branches: return ffffffffc13b30b6 __vmx_vcpu_run+0xd6 (vmlinux) => ffffffffc13b2f2e vmx_vcpu_enter_exit+0x4e (vmlinux)
1605 [SNIP]
1606 tsc_msrs_test 18436 [007] 10897.962089321: branches: call ffffffffc13b2ff5 __vmx_vcpu_run+0x15 (vmlinux) => ffffffffc13b2f50 vmx_update_host_rsp+0x0 (vmlinux)
1607 tsc_msrs_test 18436 [007] 10897.962089321: branches: return ffffffffc13b2f5d vmx_update_host_rsp+0xd (vmlinux) => ffffffffc13b2ffa __vmx_vcpu_run+0x1a (vmlinux)
1608 tsc_msrs_test 18436 [007] 10897.962089321: branches: call ffffffffc13b303b __vmx_vcpu_run+0x5b (vmlinux) => ffffffffc13b2f80 vmx_vmenter+0x0 (vmlinux)
1609 tsc_msrs_test 18436 [007] 10897.962089424: branches: vmentry ffffffffc13b2f82 vmx_vmenter+0x2 (vmlinux) => 0 [unknown] ([unknown])
1610 [guest/18436] 18436 [007] 10897.962089424: branches: vmentry 0 [unknown] ([unknown]) => 40dba0 ucall+0x0 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1611 [guest/18436] 18436 [007] 10897.962089701: branches: jmp 40dc1b ucall+0x7b (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 40dc39 ucall+0x99 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1612 [guest/18436] 18436 [007] 10897.962089701: branches: jcc 40dc3c ucall+0x9c (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 40dc20 ucall+0x80 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1613 [guest/18436] 18436 [007] 10897.962089701: branches: jcc 40dc3c ucall+0x9c (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 40dc20 ucall+0x80 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1614 [guest/18436] 18436 [007] 10897.962089701: branches: jcc 40dc37 ucall+0x97 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 40dc50 ucall+0xb0 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test)
1615 [guest/18436] 18436 [007] 10897.962089878: branches: vmexit 40dc55 ucall+0xb5 (/home/user/git/work/tools/testing/selftests/kselftest_install/kvm/tsc_msrs_test) => 0 [unknown] ([unknown])
1616 tsc_msrs_test 18436 [007] 10897.962089878: branches: vmexit 0 [unknown] ([unknown]) => ffffffffc13b2fa0 vmx_vmexit+0x0 (vmlinux)
1617 tsc_msrs_test 18436 [007] 10897.962089878: branches: jmp ffffffffc13b2fa0 vmx_vmexit+0x0 (vmlinux) => ffffffffc13b2fd2 vmx_vmexit+0x32 (vmlinux)
1618 tsc_msrs_test 18436 [007] 10897.962089887: branches: return ffffffffc13b2fd2 vmx_vmexit+0x32 (vmlinux) => ffffffffc13b3040 __vmx_vcpu_run+0x60 (vmlinux)
1619 tsc_msrs_test 18436 [007] 10897.962089901: branches: return ffffffffc13b30b6 __vmx_vcpu_run+0xd6 (vmlinux) => ffffffffc13b2f2e vmx_vcpu_enter_exit+0x4e (vmlinux)
1620 [SNIP]
1621
1622 # perf kvm --guest-code --guest --host report -i perf.data --stdio | head -20
1623
1624 # To display the perf.data header info, please use --header/--header-only options.
1625 #
1626 #
1627 # Total Lost Samples: 0
1628 #
1629 # Samples: 12 of event 'instructions'
1630 # Event count (approx.): 2274583
1631 #
1632 # Children Self Command Shared Object Symbol
1633 # ........ ........ ............. .................... ...........................................
1634 #
1635 54.70% 0.00% tsc_msrs_test [kernel.vmlinux] [k] entry_SYSCALL_64_after_hwframe
1636 |
1637 ---entry_SYSCALL_64_after_hwframe
1638 do_syscall_64
1639 |
1640 |--29.44%--syscall_exit_to_user_mode
1641 | exit_to_user_mode_prepare
1642 | task_work_run
1643 | __fput
1644
1645
1646 Event Trace
1647 -----------
1648
1649 Event Trace records information about asynchronous events, for example interrupts,
1650 faults, VM exits and entries. The information is recorded in CFE and EVD packets,
1651 and also the Interrupt Flag is recorded on the MODE.Exec packet. The CFE packet
1652 contains a type field to identify one of the following:
1653
1654 1 INTR interrupt, fault, exception, NMI
1655 2 IRET interrupt return
1656 3 SMI system management interrupt
1657 4 RSM resume from system management mode
1658 5 SIPI startup interprocessor interrupt
1659 6 INIT INIT signal
1660 7 VMENTRY VM-Entry
1661 8 VMEXIT VM-Entry
1662 9 VMEXIT_INTR VM-Exit due to interrupt
1663 10 SHUTDOWN Shutdown
1664
1665 For more details, refer to the Intel 64 and IA-32 Architectures Software
1666 Developer Manuals (version 076 or later).
1667
1668 The capability to do Event Trace is indicated by the
1669 /sys/bus/event_source/devices/intel_pt/caps/event_trace file.
1670
1671 Event trace is selected for recording using the "event" config term. e.g.
1672
1673 perf record -e intel_pt/event/u uname
1674
1675 Event trace events are output using the --itrace I option. e.g.
1676
1677 perf script --itrace=Ie
1678
1679 perf script displays events containing CFE type, vector and event data,
1680 in the form:
1681
1682 evt: hw int (t) cfe: INTR IP: 1 vector: 3 PFA: 0x8877665544332211
1683
1684 The IP flag indicates if the event binds to an IP, which includes any case where
1685 flow control packet generation is enabled, as well as when CFE packet IP bit is
1686 set.
1687
1688 perf script displays events containing changes to the Interrupt Flag in the form:
1689
1690 iflag: t IFLAG: 1->0 via branch
1691
1692 where "via branch" indicates a branch (interrupt or return from interrupt) and
1693 "non branch" indicates an instruction such as CFI, STI or POPF).
1694
1695 In addition, the current state of the interrupt flag is indicated by the presence
1696 or absence of the "D" (interrupt disabled) perf script flag. If the interrupt
1697 flag is changed, then the "t" flag is also included i.e.
1698
1699 no flag, interrupts enabled IF=1
1700 t interrupts become disabled IF=1 -> IF=0
1701 D interrupts are disabled IF=0
1702 Dt interrupts become enabled IF=0 -> IF=1
1703
1704 The intel-pt-events.py script illustrates how to access Event Trace information
1705 using a Python script.
1706
1707
1708 TNT Disable
1709 -----------
1710
1711 TNT packets are disabled using the "notnt" config term. e.g.
1712
1713 perf record -e intel_pt/notnt/u uname
1714
1715 In that case the --itrace q option is forced because walking executable code
1716 to reconstruct the control flow is not possible.
1717
1718
1719 Emulated PTWRITE
1720 ----------------
1721
1722 Later perf tools support a method to emulate the ptwrite instruction, which
1723 can be useful if hardware does not support the ptwrite instruction.
1724
1725 Instead of using the ptwrite instruction, a function is used which produces
1726 a trace that encodes the payload data into TNT packets. Here is an example
1727 of the function:
1728
1729 #include <stdint.h>
1730
1731 void perf_emulate_ptwrite(uint64_t x)
1732 __attribute__((externally_visible, noipa, no_instrument_function, naked));
1733
1734 #define PERF_EMULATE_PTWRITE_8_BITS \
1735 "1: shl %rax\n" \
1736 " jc 1f\n" \
1737 "1: shl %rax\n" \
1738 " jc 1f\n" \
1739 "1: shl %rax\n" \
1740 " jc 1f\n" \
1741 "1: shl %rax\n" \
1742 " jc 1f\n" \
1743 "1: shl %rax\n" \
1744 " jc 1f\n" \
1745 "1: shl %rax\n" \
1746 " jc 1f\n" \
1747 "1: shl %rax\n" \
1748 " jc 1f\n" \
1749 "1: shl %rax\n" \
1750 " jc 1f\n"
1751
1752 /* Undefined instruction */
1753 #define PERF_EMULATE_PTWRITE_UD2 ".byte 0x0f, 0x0b\n"
1754
1755 #define PERF_EMULATE_PTWRITE_MAGIC PERF_EMULATE_PTWRITE_UD2 ".ascii \"perf,ptwrite \"\n"
1756
1757 void perf_emulate_ptwrite(uint64_t x __attribute__ ((__unused__)))
1758 {
1759 /* Assumes SysV ABI : x passed in rdi */
1760 __asm__ volatile (
1761 "jmp 1f\n"
1762 PERF_EMULATE_PTWRITE_MAGIC
1763 "1: mov %rdi, %rax\n"
1764 PERF_EMULATE_PTWRITE_8_BITS
1765 PERF_EMULATE_PTWRITE_8_BITS
1766 PERF_EMULATE_PTWRITE_8_BITS
1767 PERF_EMULATE_PTWRITE_8_BITS
1768 PERF_EMULATE_PTWRITE_8_BITS
1769 PERF_EMULATE_PTWRITE_8_BITS
1770 PERF_EMULATE_PTWRITE_8_BITS
1771 PERF_EMULATE_PTWRITE_8_BITS
1772 "1: ret\n"
1773 );
1774 }
1775
1776 For example, a test program with the function above:
1777
1778 #include <stdio.h>
1779 #include <stdint.h>
1780 #include <stdlib.h>
1781
1782 #include "perf_emulate_ptwrite.h"
1783
1784 int main(int argc, char *argv[])
1785 {
1786 uint64_t x = 0;
1787
1788 if (argc > 1)
1789 x = strtoull(argv[1], NULL, 0);
1790 perf_emulate_ptwrite(x);
1791 return 0;
1792 }
1793
1794 Can be compiled and traced:
1795
1796 $ gcc -Wall -Wextra -O3 -g -o eg_ptw eg_ptw.c
1797 $ perf record -e intel_pt//u ./eg_ptw 0x1234567890abcdef
1798 [ perf record: Woken up 1 times to write data ]
1799 [ perf record: Captured and wrote 0.017 MB perf.data ]
1800 $ perf script --itrace=ew
1801 eg_ptw 19875 [007] 8061.235912: ptwrite: IP: 0 payload: 0x1234567890abcdef 55701249a196 perf_emulate_ptwrite+0x16 (/home/user/eg_ptw)
1802 $
1803
1804
1805 EXAMPLE
1806 -------
1807
1808 Examples can be found on perf wiki page "Perf tools support for IntelĀ® Processor Trace":
1809
1810 https://perf.wiki.kernel.org/index.php/Perf_tools_support_for_Intel%C2%AE_Processor_Trace
1811
1812
1813 SEE ALSO
1814 --------
1815
1816 linkperf:perf-record[1], linkperf:perf-script[1], linkperf:perf-report[1],
1817 linkperf:perf-inject[1]