title: Qemu+kvm high cpu usage
body: <p>I was running a KVM host with Intel(R) Xeon(R) CPU E3-1230 v2 (8 cores total). When playing a 1080p video using VLC player, the <code>perf stat</code> result of the KVM process is as follows.</p>
  
  <pre><code>Performance counter stats for process id '120879':
  
        65235.881169 task-clock                #    1.631 CPUs utilized           [100.00%]
              99,073 context-switches          #    0.002 M/sec                   [100.00%]
              16,616 cpu-migrations            #    0.255 K/sec                   [100.00%]
               8,280 page-faults               #    0.127 K/sec                  
     129,136,372,749 cycles                    #    1.980 GHz                     [100.00%]
      69,852,938,816 stalled-cycles-frontend   #   54.09% frontend cycles idle    [100.00%]
     &lt;not supported&gt; stalled-cycles-backend  
     176,171,866,933 instructions              #    1.36  insns per cycle        
                                               #    0.40  stalled cycles per insn [100.00%]
      13,853,398,215 branches                  #  212.359 M/sec                   [100.00%]
         313,064,663 branch-misses             #    2.26% of all branches        
  
        40.001590915 seconds time elapsed
  </code></pre>
  
  <p>The VM guest is consuming too much CPU. I wonder why the freqency in the result is 1.98GHz while the actual CPU frequency is 3.3GHz (the cpufreq governor is set to performance). Also, the context-switches and CPU-migration is very frequent. The only running application is VLC.</p>
  
  <p>The <code>perf kvm --host --guest stat</code> result of the process is:</p>
  
  <pre><code>Performance counter stats for process id '17720':
  
         6935.898736 task-clock:HG             #    1.653 CPUs utilized           [100.00%]
              11,713 context-switches:HG       #    0.002 M/sec                   [100.00%]
               2,027 cpu-migrations:HG         #    0.292 K/sec                   [100.00%]
             460,754 page-faults:HG            #    0.066 M/sec                  
      24,166,977,190 cycles:HG                 #    3.484 GHz                     [100.00%]
      12,972,225,263 stalled-cycles-frontend:HG #   53.68% frontend cycles idle    [100.00%]
     &lt;not supported&gt; stalled-cycles-backend:HG
      33,789,733,514 instructions:HG           #    1.40  insns per cycle        
                                               #    0.38  stalled cycles per insn [100.00%]
       3,111,479,733 branches:HG               #  448.605 M/sec                   [100.00%]
          60,351,733 branch-misses:HG          #    1.94% of all branches        
  
         4.196966586 seconds time elapsed 
  </code></pre>
  
  <p>The configuration for qemu-kvm is (some devices and chardev options are omitted):</p>
  
  <pre><code>qemu-kvm
  -chardev socket,id=qmp,path=/var/run/qemu-server/$vmid.qmp,server,nowait
  -mon chardev=qmp,mode=control
  -vnc :0,websocket,to=200
  -enable-kvm
  -daemonize
  -smp sockets=1,cores=2
  -cpu core2duo,hv_spinlocks=0xffff,hv_relaxed,hv_time,hv_vapic,+sse4.1,+sse4.2,+x2apic,+pcid,+pdcm,+xtpr,+ht,+ss,+acpi,+ds
  -nodefaults
  -vga qxl
  -spice port=0,disable-ticketing,up_port=5699,down_port=5500,os_type=win7,jpeg-wan-compression=never,jpeg_quality=85,streaming-video=filter
  -boot menu=off,splash-time=1
  -m 4096
  -drive file=/sf/data/local/images/host-001e67b7f807/9cef0faa00d6/Win7x86-VM0001_hzq.vm/vm-disk-1.qcow2,if=none,id=drive-virtio0,cache=none,aio=native,forecast=enable,cache_cnt=4096,cache_size=256
  -device virtio-blk-pci,drive=drive-virtio0,id=virtio0,bus=pci.0,addr=0xa,bootindex=100
  -rtc driftfix=slew,clock=rt,base=localtime
  -global kvm-pit.lost_tick_policy=discard
  -global PIIX4_PM.disable_s3=1
  -chardev socket,path=/var/run/qemu-server/$vmid.virtser,server,nowait,id=channelser
  -device qxl
  </code></pre>
  
  <p>How do I reduce the CPU usage of KVM?</p>