一种工具,可以轻松地固定某些qemu线程以选择cpu核心。
qemu-affinit的Python项目详细描述
qemu-affinity是一个工具,可以轻松地固定某些QEMU线程以选择cpu核心。
开始
安装
这将把qemu-affinity命令安装到/usr/local/bin。
来自pip
pip install qemu-affinity
源代码
克隆repo并运行:
python setup.py install
要求
qemu-affinity需要python 3。
注意
qemu-system实例必须以qemu-affinity的-name <vm name>,debug-threads=on参数启动,才能正确标识和设置特定qemu线程的关联。
使用量
qemu-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 ...]]
位置参数
- qemu-system-pid
qemu系统过程的pid
可选参数
- -h,--help
- 显示此帮助消息并退出
- --dry-run
- 不要修改线程关联值(对-v有用)
- -v,--verbose
- 冗长
- -p AFFINITY,--process-affinity AFFINITY
- 设置qemu系统进程关联(以及新线程的默认值)
- -q AFFINITY [AFFINITY ...],--qemu-affinity AFFINITY [AFFINITY ...]
- 设置qemu系统线程关联(不允许部分名称选择器)
- -k THREAD_AFFINITY [THREAD_AFFINITY ...],--kvm-affinity THREAD_AFFINITY [THREAD_AFFINITY ...]
- 设置kvm(cpu<;n>;/kvm)线程关联性
- -i THREAD_AFFINITY [THREAD_AFFINITY ...],--io-affinity THREAD_AFFINITY [THREAD_AFFINITY ...]
- 设置io对象(io<;name>;)线程关联性
- -w THREAD_AFFINITY [THREAD_AFFINITY ...],--worker-affinity THREAD_AFFINITY [THREAD_AFFINITY ...]
- 设置qemu worker(worker)线程关联(不允许部分名称选择器,仅限位置选择器)
- -t THREAD_AFFINITY [THREAD_AFFINITY ...],--thread-affinity THREAD_AFFINITY [THREAD_AFFINITY ...]
- 设置任意(<;name>;)线程关联性
AFFINITY是关联规范
以及
THREAD_AFFINITY可以是:
- affinity-spec
- selector^{tt27}$affinity-spec
其中affinity spec是一个CPU号,由 破折号(-)或以逗号分隔的(,)CPU编号或范围列表。
For example:
- ^{tt30}$
- specifies CPU 0
- ^{tt31}$
- specifies CPU 0, 1, 2 and 3
- ^{tt32}$
- same as above
- ^{tt33}$
- specifies CPU 0, 2, 3, 4 and 6
其中selector是:
- ^{tt34}$, meaning all threads
- partial-name for ^{tt35}$ (CPU <partial-name>/KVM) and ^{tt36}$ (IO <partial-name>)
- name for ^{tt37}$
第一个变量affinity spec,根据参数位置选择线程。
e.g., ^{tt38}$ pins the first KVM thread to CPUs 0 and 4, the second KVM thread to CPUs 1 and 5, and so on.
第二个变量selector:affinity spec通过selector选择线程,后者是部分名称或通配符。 kvm线程有数字名称(0,1,2等)。 IO线程具有用户提供的名称(-object iothread,id=name)。
e.g., ^{tt41}$ pins KVM thread 2 (aka CPU 2/KVM) to CPUs 2 and 6, IO thread myiothread (aka IO myiothread) to CPU 7, and all remaining IO threads to CPU 0.
这两种变体可以组合。
- e.g., ^{tt42}$ pins the first KVM thread to CPUs 0 and 4,
- and all remaining KVM threads to CPUs 2 and 6.
已知限制
内置帮助(qemu-affinity-h)列出列表中最后一个可能与多参数参数冲突的qemu system pid参数 例如-q、-k、-i、-w和-t。
Either specify qemu-system-pid at the beginning of the argument list or use ^{tt49}$ to separate the multi-argument parameters from the positional parameters.
-t/--thread-affinity仅适用于共享相同名称的多个线程(例如qemuworker线程)中的第一个线程。
^{tt37}$ is 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 (^{tt34}$ applies to all threads, not just a sub-set).
e.g. ^{tt54}$ results 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 nth thread with the same name.
e.g. ^{tt55}$ will always select the 1st thread with the name “abc”.
示例用法
下面的systemd.service(5)以守护进程的身份启动qemu,然后将4个kvm线程(每个模拟cpu核心一个)固定到宿主cpu 2、3、4和5。IO线程和其他QEMU工作线程被固定 托管CPU 0和1。
在本例中,主机内核被配置为隔离核心2、3、4和5,因此它们可以由qemu单独使用。
[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