Recording events

Events can be recorded to a file for later reporting and analysis.
You can record events for the host using the --host option.
You can record events for a guest using the --guest option.
You can use both options at the same time to record events for both the host and a guest.

If you use just the --host option the default output file will be perf.data.host.
If you use just the --guest option the default output file will be perf.data.guest.
If you use both options the default output file will be perf.data.kvm.
Use the -o option after the record key word to save the output to a different file name.

# perf kvm --host --guest [kvm options] record -a -o my.perf.data

Recording events for a guest

Using copies of guest files

In order to record events for a guest, the perf tool needs the /proc/kallsyms and /proc/modules for the guest.
These are passed to perf with the --guestkallsyms and --guestmodules options.
The files will have to be on the host, but you can get them easily using ssh.

The order of the arguments is important.
In general, the syntax for using perf to profile kvm is: perf kvm <perf kvm args> <perf command> <perf command args>
In this case the perf kvm arguments are --host --guest --guestkallsyms=/tmp/guest.kallsyms --guestmodules=/tmp/guest.modules, the perf command is record, and the perf command argument is -a (profile all processes).

perf will record events until it is terminated with SIGINT.
It must be SIGINT.
If perf is terminated with any other signal, such as SIGTERM or SIGQUIT, the pref kvm report command (see below) won't correctly process the file generated.
It will just list the headers with no data.

Using sshfs

perf kvm has a way of getting the guest's kallsyms and modules by itself instead of you hving to provide them.
It makes use of sshfs to mount the root file system of the guest so that it can get the files directly from the guest.

Here's how it works.
You create a directory, e.g., /tmp/guestmount.
You then create a subdirectory that has for its name the PID of the qemu process for the guest.
Then you use sshfs to mount the guest's root file system on that subdirectory.
For example: