Package: qemu-affinity
qemu-affinityis a tool to easily pin certainQEMUthreads to select CPU cores.Getting StartedInstallingThis will install theqemu-affinitycommand to/usr/local/bin.Frompippip install qemu-affinityFrom sourceClone the repo and run:python setup.py installRequirementsqemu-affinityrequires Python 3.Noteqemu-systeminstances must be started with the-name<vmname>,debug-threads=onargument forqemu-affinityto correctly identify and set the affinity of specificQEMUthreads.Usageqemu-affinity qemu-system-pid
                          [-h] [--dry-run] [-v] [-p [AFFINITY]]
                          [-q AFFINITY [AFFINITY ...]]
                          [-k THREAD_AFFINITY [THREAD_AFFINITY ...]]
                          [-i THREAD_AFFINITY [THREAD_AFFINITY ...]]
                          [-w THREAD_AFFINITY [THREAD_AFFINITY ...]]
                          [-t THREAD_AFFINITY [THREAD_AFFINITY ...]]Positional argumentsqemu-system-pidPID of the qemu-system processOptional arguments-h,--helpshow this help message and exit--dry-rundon’t modify thread affinity values (useful with-v)-v,--verbosebe verbose-pAFFINITY,--process-affinityAFFINITYsetqemu-systemprocess affinity (and default for new threads)-qAFFINITY [AFFINITY...],--qemu-affinityAFFINITY [AFFINITY...]setqemu-systemthread affinity (partial name selectors not allowed)-kTHREAD_AFFINITY [THREAD_AFFINITY...],--kvm-affinityTHREAD_AFFINITY [THREAD_AFFINITY...]set KVM (CPU <n>/KVM) thread affinity-iTHREAD_AFFINITY [THREAD_AFFINITY...],--io-affinityTHREAD_AFFINITY [THREAD_AFFINITY...]set IO object (IO <name>) thread affinity-wTHREAD_AFFINITY [THREAD_AFFINITY...],--worker-affinityTHREAD_AFFINITY [THREAD_AFFINITY...]set qemu worker (worker) thread affinity (partial name selectors not allowed, only positional)-tTHREAD_AFFINITY [THREAD_AFFINITY...],--thread-affinityTHREAD_AFFINITY [THREAD_AFFINITY...]set arbitary (<name>) thread affinityAFFINITYis anaffinity-specandTHREAD_AFFINITYcan be one of:affinity-specselector:affinity-specWhereaffinity-specis a CPU number, a range (inclusive) of CPU numbers separated by a
dash (-), or a comma-delimited (,) list of CPU numbers or ranges.For example:0specifies CPU 00,1,2,3specifies CPU 0, 1, 2 and 30-3same as above0,2-4,6specifies CPU 0, 2, 3, 4 and 6and, whereselectoris one of:*, meaning all threadspartial-namefor-k(CPU <partial-name>/KVM) and-i(IO <partial-name>)namefor-tThe first variant,affinity-spec, selects threads based on argument position.e.g.,-k0,4 1,5 2,6 3,7pins the first KVM thread to CPUs 0 and 4, the second KVM thread to CPUs 1 and 5, and so on.The second variant,selector:affinity-spec, selects threads byselector, which is a partial name or wildcard.
KVM threads have numeric names (0,1,2, etc.).
IO threads have user-supplied names (-objectiothread,id=name).e.g.,-k2:2,6-imyiothread:7 *:0pins KVM thread2(akaCPU 2/KVM) to CPUs 2 and 6, IO threadmyiothread(akaIO myiothread) to CPU 7, and all remaining IO threads to
CPU 0.The two variants can be combined.e.g.,-k0,4 *:2,6pins the first KVM thread to CPUs 0 and 4,and all remaining KVM threads to CPUs 2 and 6.Known Limitationsthe built-in help (qemu-affinity-h) lists theqemu-system-pidargument last in the list which may conflict with multi-argument parameters
such as-q,-k,-i,-w, and-t.Either specifyqemu-system-pidat the beginning of the argument list or use--to separate the multi-argument parameters from the positional parameters.-t/--thread-affinityonly applies to the first of multiple threads that share an identical name (such as the QEMUworkerthreads).-tis unable to specify different affinities for threads with duplicate names, nor is it able to apply the same affinity value to multiple threads with the same name
(*applies toallthreads, not just a sub-set).e.g.-tabc:1 abc:2results in an error, and there is no way to set all threads with the name “abc” to the same affinity value.Additionally, there is no way to select the nththread with the same name.e.g.-tabc:1will always select the 1stthread with the name “abc”.Sample UsageThe followingsystemd.service(5)startsQEMUas a daemon and then pins the 4 KVM threads (one for each emulated CPU core) to host CPUs 2, 3, 4 and 5. IO threads and other QEMU worker threads are pinned
to host CPUs 0 and 1.In this example the host kernel has been configured to isolate cores 2, 3, 4 & 5 so they can be solely utilised byQEMU.[Unit]
Description=QEMU virtual machine
After=network.target netctl@br0.service

[Service]
CPUSchedulingPolicy=rr

Type=forking
PIDFile=/run/qemu_ex.pid

Environment=QEMU_AUDIO_DRV=pa

ExecStart=/usr/bin/qemu-system-x86_64 -name example-qemu-machine,debug-threads=on -daemonize -pidfile /run/qemu_ex.pid -monitor unix:/tmp/qemu_ex.sock,server,nowait -smp cores=4,threads=1,sockets=1 ...
ExecStartPost=/bin/sh -c 'exec /usr/bin/qemu-affinity $MAINPID -p 0-1 -i *:0-1 -q 0-1 -w *:0-1 -k 2 3 4 5'

ExecStop=/bin/sh -c 'while test -d /proc/$MAINPID; do /usr/bin/echo system_powerdown | /usr/bin/socat - UNIX-CONNECT:/tmp/qemu_ex.sock; sleep 60; done'
TimeoutStopSec=1m

[Install]
WantedBy=multi-user.target