<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://linux-kvm.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Steved</id>
	<title>KVM - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://linux-kvm.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Steved"/>
	<link rel="alternate" type="text/html" href="https://linux-kvm.org/page/Special:Contributions/Steved"/>
	<updated>2026-04-15T21:18:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.5</generator>
	<entry>
		<id>https://linux-kvm.org/index.php?title=Perf_events&amp;diff=3036</id>
		<title>Perf events</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=Perf_events&amp;diff=3036"/>
		<updated>2010-06-24T16:11:36Z</updated>

		<summary type="html">&lt;p&gt;Steved: Added text for recording and reporting events&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page describes how to count and trace performance events in the KVM kernel module.&lt;br /&gt;
&lt;br /&gt;
There are two tools, &amp;lt;tt&amp;gt;kvm_stat&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;kvm_trace&amp;lt;/tt&amp;gt;, which were previously used for these tasks.  Now this can be done using standard Linux tracing tools.&lt;br /&gt;
&lt;br /&gt;
== Counting events ==&lt;br /&gt;
&lt;br /&gt;
Often you want event counts after running a benchmark:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo mount -t debugfs none /sys/kernel/debug&lt;br /&gt;
$ sudo ./perf stat -e &#039;kvm:*&#039; -a sleep 1h&lt;br /&gt;
^C&lt;br /&gt;
 Performance counter stats for &#039;sleep 1h&#039;:&lt;br /&gt;
&lt;br /&gt;
           8330  kvm:kvm_entry            #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_hypercall        #      0.000 M/sec&lt;br /&gt;
           4060  kvm:kvm_pio              #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_cpuid            #      0.000 M/sec&lt;br /&gt;
           2681  kvm:kvm_apic             #      0.000 M/sec&lt;br /&gt;
           8343  kvm:kvm_exit             #      0.000 M/sec&lt;br /&gt;
            737  kvm:kvm_inj_virq         #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_page_fault       #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_msr              #      0.000 M/sec&lt;br /&gt;
            664  kvm:kvm_cr               #      0.000 M/sec&lt;br /&gt;
            872  kvm:kvm_pic_set_irq      #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_apic_ipi         #      0.000 M/sec&lt;br /&gt;
            738  kvm:kvm_apic_accept_irq  #      0.000 M/sec&lt;br /&gt;
            874  kvm:kvm_set_irq          #      0.000 M/sec&lt;br /&gt;
            874  kvm:kvm_ioapic_set_irq   #      0.000 M/sec&lt;br /&gt;
              0  kvm:kvm_msi_set_irq      #      0.000 M/sec&lt;br /&gt;
            433  kvm:kvm_ack_irq          #      0.000 M/sec&lt;br /&gt;
           2685  kvm:kvm_mmio             #      0.000 M/sec&lt;br /&gt;
&lt;br /&gt;
    3.493562100  seconds time elapsed&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;perf&amp;lt;/tt&amp;gt; tool is part of the Linux kernel tree in &amp;lt;tt&amp;gt;tools/perf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Tracing events ==&lt;br /&gt;
&lt;br /&gt;
Detailed traces can be generated using ftrace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mount -t debugfs none /sys/kernel/debug&lt;br /&gt;
# echo 1 &amp;gt;/sys/kernel/debug/tracing/events/kvm/enable&lt;br /&gt;
# cat /sys/kernel/debug/tracing/trace_pipe&lt;br /&gt;
[...]&lt;br /&gt;
             kvm-5664  [000] 11906.220178: kvm_entry: vcpu 0&lt;br /&gt;
             kvm-5664  [000] 11906.220181: kvm_exit: reason apic_access rip 0xc011518c&lt;br /&gt;
             kvm-5664  [000] 11906.220183: kvm_mmio: mmio write len 4 gpa 0xfee000b0 val 0x0&lt;br /&gt;
             kvm-5664  [000] 11906.220183: kvm_apic: apic_write APIC_EOI = 0x0&lt;br /&gt;
             kvm-5664  [000] 11906.220184: kvm_ack_irq: irqchip IOAPIC pin 11&lt;br /&gt;
             kvm-5664  [000] 11906.220185: kvm_entry: vcpu 0&lt;br /&gt;
             kvm-5664  [000] 11906.220188: kvm_exit: reason io_instruction rip 0xc01e4473&lt;br /&gt;
             kvm-5664  [000] 11906.220188: kvm_pio: pio_read at 0xc13e size 2 count 1&lt;br /&gt;
             kvm-5664  [000] 11906.220193: kvm_entry: vcpu 0&lt;br /&gt;
^D&lt;br /&gt;
# echo 0 &amp;gt;/sys/kernel/debug/tracing/events/kvm/enable&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Recording events ==&lt;br /&gt;
&lt;br /&gt;
Events can be recorded to a file for later reporting and analysis.&lt;br /&gt;
You can record events for the host using the &amp;lt;tt&amp;gt;--host&amp;lt;/tt&amp;gt; option.&lt;br /&gt;
You can record events for a guest using the &amp;lt;tt&amp;gt;--guest&amp;lt;/tt&amp;gt; option.&lt;br /&gt;
You can use both options at the same time to record events for both the host and a guest.&lt;br /&gt;
&lt;br /&gt;
If you use just the &amp;lt;tt&amp;gt;--host&amp;lt;/tt&amp;gt; option the default output file will be &amp;lt;tt&amp;gt;perf.data.host&amp;lt;/tt&amp;gt;.&lt;br /&gt;
If you use just the &amp;lt;tt&amp;gt;--guest&amp;lt;/tt&amp;gt; option the default output file will be &amp;lt;tt&amp;gt;perf.data.guest&amp;lt;/tt&amp;gt;.&lt;br /&gt;
If you use both options the default output file will be &amp;lt;tt&amp;gt;perf.data.kvm&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;-o&amp;lt;/tt&amp;gt; option after the &amp;lt;tt&amp;gt;record&amp;lt;/tt&amp;gt; key word to save the output to a different file name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --host --guest [kvm options] record -a -o my.perf.data&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recording events for a guest ===&lt;br /&gt;
&lt;br /&gt;
==== Using copies of guest files ====&lt;br /&gt;
&lt;br /&gt;
In order to record events for a guest, the perf tool needs the &amp;lt;tt&amp;gt;/proc/kallsyms&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;/proc/modules&amp;lt;/tt&amp;gt; for the guest.&lt;br /&gt;
These are passed to &amp;lt;tt&amp;gt;perf&amp;lt;/tt&amp;gt; with the &amp;lt;tt&amp;gt;--guestkallsyms&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;--guestmodules&amp;lt;/tt&amp;gt; options.&lt;br /&gt;
The files will have to be on the host, but you can get them easily using ssh.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# ssh guest &amp;quot;cat /proc/kallsyms&amp;quot; &amp;gt; /tmp/guest.kallsyms&lt;br /&gt;
# ssh guest &amp;quot;cat /proc/modules&amp;quot; &amp;gt; /tmp/guest.modules&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is better to use &amp;lt;tt&amp;gt;ssh&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; the files and redirect the output than to use &amp;lt;tt&amp;gt;scp&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Experience has shown that &amp;lt;tt&amp;gt;scp guest:/proc/kallsyms /tmp/guest.kallsyms&amp;lt;/tt&amp;gt; will return an empty file.&lt;br /&gt;
&lt;br /&gt;
Using these options you can record the events for a host and a guest with the following command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --host --guest --guestkallsyms=/tmp/guest.kallsyms --guestmodules=/tmp/guest.modules record -a&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The order of the arguments is important.&lt;br /&gt;
In general, the syntax for using perf to profile kvm is:  &amp;lt;tt&amp;gt;perf kvm &amp;lt;perf kvm args&amp;gt; &amp;lt;perf command&amp;gt; &amp;lt;perf command args&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
In this case the perf kvm arguments are &amp;lt;tt&amp;gt;--host --guest --guestkallsyms=/tmp/guest.kallsyms --guestmodules=/tmp/guest.modules&amp;lt;/tt&amp;gt;, the perf command is &amp;lt;tt&amp;gt;record&amp;lt;/tt&amp;gt;, and the perf command argument is &amp;lt;tt&amp;gt;-a&amp;lt;/tt&amp;gt; (profile all processes).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;perf&amp;lt;/tt&amp;gt; will record events until it is terminated with SIGINT.&lt;br /&gt;
&#039;&#039;&#039;It must be SIGINT.&#039;&#039;&#039;&lt;br /&gt;
If perf is terminated with any other signal, such as SIGTERM or SIGQUIT, the &amp;lt;tt&amp;gt;pref kvm report&amp;lt;/tt&amp;gt; command (see below) won&#039;t correctly process the file generated.&lt;br /&gt;
It will just list the headers with no data.&lt;br /&gt;
&lt;br /&gt;
==== Using sshfs ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;perf kvm&amp;lt;/tt&amp;gt; has a way of getting the guest&#039;s &amp;lt;tt&amp;gt;kallsyms&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;modules&amp;lt;/tt&amp;gt; by itself instead of you hving to provide them.&lt;br /&gt;
It makes use of &amp;lt;tt&amp;gt;sshfs&amp;lt;/tt&amp;gt; to mount the root file system of the guest so that it can get the files directly from the guest.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;sshfs&amp;lt;/tt&amp;gt; depends on &amp;lt;tt&amp;gt;fuse&amp;lt;/tt&amp;gt;, the Filesystem in Userspace.&lt;br /&gt;
If your Linux distribution does not have &amp;lt;tt&amp;gt;fuse&amp;lt;/tt&amp;gt;, see the [http://fuse.sourceforge.net fuse project page on SourceForge].&lt;br /&gt;
If your Linux distribution does not have &amp;lt;tt&amp;gt;sshfs&amp;lt;/tt&amp;gt;, see the [http://fuse.sourceforge.net/sshfs.html sshfs project page on SourceForge].&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how it works.&lt;br /&gt;
You create a directory, e.g., &amp;lt;tt&amp;gt;/tmp/guestmount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
You then create a subdirectory that has for its name the PID of the qemu process for the guest.&lt;br /&gt;
Then you use &amp;lt;tt&amp;gt;sshfs&amp;lt;/tt&amp;gt; to mount the guest&#039;s root file system on that subdirectory.&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkdir -p /tmp/guestmount&lt;br /&gt;
# ps -eo pid,cmd | grep qemu | grep -v grep&lt;br /&gt;
24764 /usr/libexec/qemu-kvm -M pc -m 4096 -smp 4 -name guest01 -boot c -drive file=/var/lib/libvirt/images/guest01.img ...&lt;br /&gt;
# mkdir /tmp/guestmount/24764&lt;br /&gt;
# sshfs -o allow_other,direct_io guest:/ /tmp/guestmount/24764&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, instead of using the &amp;lt;tt&amp;gt;--guestkallsyms&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;--guestmodules&amp;lt;/tt&amp;gt; options, you use the &amp;lt;tt&amp;gt;--guestmount&amp;lt;/tt&amp;gt; option.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --host --guest --guestmount=/tmp/guestmount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are finished recording, unmount the guest file system using &amp;lt;tt&amp;gt;fusermount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# fusermount -u /tmp/guestmount/24764&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reporting events ==&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;perf kvm report&amp;lt;/tt&amp;gt; command to generate a report from a data file created by &amp;lt;tt&amp;gt;perf kvm record&amp;lt;/tt&amp;gt;.&lt;br /&gt;
To generate a report for the host, use the &amp;lt;tt&amp;gt;--host&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --host --guestmount=/tmp/guestmount report&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To generate a report for the guest, use the &amp;lt;tt&amp;gt;--guest&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --guest --guestmount=/tmp/guestmount report&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the perf report command will read &amp;lt;tt&amp;gt;perf.data.host&amp;lt;/tt&amp;gt; for a host report and &amp;lt;tt&amp;gt;perf.data.guest&amp;lt;/tt&amp;gt; for a guest report.&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;-i&amp;lt;/tt&amp;gt; option if you want to use a different input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# perf kvm --host --guestmount=/tmp/guestmount report -i /tmp/perf.data.kvm&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;perf report&amp;lt;/tt&amp;gt; command sends its output to standard out.  If you want the output to go to a file, redirect the output to a file.&lt;br /&gt;
Don&#039;t use the &amp;lt;tt&amp;gt;-o&amp;lt;/tt&amp;gt; option.&lt;br /&gt;
Contrary to the help text, it does not work.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/trace/ftrace.txt;h=03485bfbd7975792f50d54dabf77533938f255c9;hb=HEAD ftrace.txt]&lt;br /&gt;
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/trace/events.txt;h=02ac6ed38b2d0ba73c44298484b7d7bac540dae2;hb=HEAD events.txt]&lt;br /&gt;
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/trace/tracepoint-analysis.txt;h=87bee3c129ba71f8c359b5e849bafd177f421136;hb=HEAD tracepoint-analysis.txt]&lt;/div&gt;</summary>
		<author><name>Steved</name></author>
	</entry>
</feed>