sickcodes / Docker-OSX

Run macOS VM in a Docker! Run near native OSX-KVM in Docker! X11 Forwarding! CI/CD for OS X Security Research! Docker mac Containers.
https://hub.docker.com/r/sickcodes/docker-osx
GNU General Public License v3.0
36.13k stars 1.78k forks source link

Allocating more CPU cores #86

Open sanderfoobar opened 3 years ago

sanderfoobar commented 3 years ago

Starting with:

docker run --device /dev/kvm --device /dev/snd -e CORES=8 -e RAM=10 -p 8888:5999 -p 50922:10022 -d --privileged docker-osx-vnc:latest

Does not start the VM. QEMU args (truncated):

qemu-system-x86_64 -m 10000 -cpu Penryn,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+pcid,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,check -machine q35,accel=kvm:tcg -smp 4,cores=8 -usb[...]

CORES=4 works.

Just wondering, is it possible to allocate more cores?

sickcodes commented 3 years ago

I actually don't know the SMP, cores, sockets situation. I'll play around with it in virt-manager and come back with more info.

Someone else, or a QEMU whizz, would be better to answer this than me.

iedwin commented 3 years ago

Starting with:

docker run --device /dev/kvm --device /dev/snd -e CORES=8 -e RAM=10 -p 8888:5999 -p 50922:10022 -d --privileged docker-osx-vnc:latest

Does not start the VM. QEMU args (truncated):

qemu-system-x86_64 -m 10000 -cpu Penryn,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+pcid,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,check -machine q35,accel=kvm:tcg -smp 4,cores=8 -usb[...]

CORES=4 works.

Just wondering, is it possible to allocate more cores?

You could try -smp 16,cores=16,threads=1,sockets=1 and it means the vm will use 16 phsical CPU cores, 16 logic cores. I managed to run macOS vm with the paramter above. I also tried -smp 32,cores=16,threads=2,sockets=1 but macOS stuck after running several hours and cannot recover by itself. I guess qemu/clover may not be able to work well with the threads parameter greater than 1.

jpmorrison commented 2 years ago

I ran Passmark (cli) benchmark on the Linux host and the macos guest and saw big improvements with SMP/threads.

After I set SMP=8 and threads=2, with default CORES=4 Passmark reported twice the guest performance. I don't know if threads is needed or if qemu can figure that out automatically from SMP.

Host cpu is pretty high still pretty high (10%) even when the guest is idle: Load Avg: 0.36, 0.49, 0.53 CPU usage: 0.23% user, 0.59% sys, 99.16% idle SharedLibs: 448M resident, 76M data, 59M linkedit.

This has been pretty consistent no matter how many CPUs the guest has.

sickcodes commented 2 years ago

Thanks for the tip @jpmorrison, do you want to add your findings to the CPU in the README or create a more detailed bit? I'd be happy to later this week, unless you would like to add some notes :)

jpmorrison commented 2 years ago

Here's my Launch.sh (as-is). I've learned a lot from @sickcodes quickemu and various qemu/kvm pages.

CPU

VirtIO

Guest

# errors and launchd complaints stopped exposing /dev/tty.org.qemu.guest_agent.0
tail /var/log/guestagent.log
Mon Oct 18 04:10:05 2021 AppleQEMUGuestAgent 1.3-AppleVirtIO-16.140.6~70 (JazzSecuritySky tools)
Mon Oct 18 04:10:05 2021 Error opening serial port: '/dev/tty.org.qemu.guest_agent.0' (No such file or directory)
Mon Oct 18 04:10:06 2021 AppleQEMUGuestAgent 1.3-AppleVirtIO-16.140.6~70 (JazzSecuritySky tools)

ps -ef | grep -i qemu
    0   252     1   0 Mon04AM ??         0:00.01 /usr/libexec/AppleQEMUGuestAgent

USB redirection (Spice)

Launch.sh

#!/bin/bash
set -eux
sudo chown    $(id -u):$(id -g) /dev/kvm 2>/dev/null || true
sudo chown -R $(id -u):$(id -g) /dev/snd 2>/dev/null || true
RAM=half

EXTRA="-nographic -spice disable-ticketing,port=3001,disable-copy-paste=off,disable-agent-file-xfer=off -enable-kvm"
CPU=kvm64
SMP=8

# CPUID flags: these might not be needed with CPU=host used by quickemu, Haven't tested.
# Qemu recommends +pdpe1gb,+pcid
# Kholia recommends +vmx,+rdtscp
# Forgot who suggested +hypervisor  - it might be what enables /usr/libexec/AppleQEMUGuestAgent 
# Other flags don't seem to help if they work, and qemu ignores others. 
CPUID_FLAGS="vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,+hypervisor,+pcid,+pdpe1gb,+pclmulqdq,+vmx,+rdtscp,+tsc-deadline,+ss,+ds-cpl,+dtes64,+pdcm,+xtpr,+vmx-unrestricted-guest,+vmx-vintr-pending,check"

# Share a directory to the guest
# security_model=none for NFS and root_squash otherwise the guest can't create files
# security_model=mapped-xattr can be used with local filesystem
#PUBLIC=/test
PUBLIC=/mnt
PUBLIC_TAG="NFS"
sudo mount -t nfs 172.17.0.1:/exports/vm /mnt

AUDIO_DRIVER="spice"
# Virtio-net has too many issues in Catalina 
#NETWORKING=virtio-net-pci
[[ "${RAM}" = max ]] && export RAM="$(("$(head -n1 /proc/meminfo | tr -dc "[:digit:]") / 1000000"))"
[[ "${RAM}" = half ]] && export RAM="$(("$(head -n1 /proc/meminfo | tr -dc "[:digit:]") / 2000000"))"
sudo chown -R $(id -u):$(id -g) /dev/snd 2>/dev/null || true
exec qemu-system-x86_64 -m ${RAM:-2}000 \
-cpu ${CPU:-Penryn},${CPUID_FLAGS:-vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,+hypervisor,+pcid,+pdpe1gb,+pclmulqdq,check,}${BOOT_ARGS} \
-machine q35,${KVM-"accel=kvm:tcg"} \
-smp ${CPU_STRING:-${SMP:-4},cores=${CORES:-4},threads=2} \
-usb -device usb-kbd -device usb-mouse \
-device isa-applesmc,osk=ourhardworkbythesewordsguardedpleasedontsteal\(c\)AppleComputerInc \
-drive if=pflash,format=raw,readonly=on,file=/home/arch/OSX-KVM/OVMF_CODE.fd \
-drive if=pflash,format=raw,file=/home/arch/OSX-KVM/OVMF_VARS-1024x768.fd \
-smbios type=2 \
-audiodev ${AUDIO_DRIVER:-alsa},id=hda -device ich9-intel-hda -device hda-duplex,audiodev=hda \
-drive id=OpenCoreBoot,if=virtio,snapshot=on,format=qcow2,file=${BOOTDISK:-/home/arch/OSX-KVM/OpenCore-Catalina/OpenCore.qcow2} \
-drive id=InstallMedia,if=virtio,file=/home/arch/OSX-KVM/BaseSystem.img,format=qcow2 \
-drive id=MacHDD,if=virtio,file=${IMAGE_PATH:-/home/arch/OSX-KVM/mac_hdd_ng.img},format=${IMAGE_FORMAT:-qcow2} \
-netdev user,id=net0,hostfwd=tcp::${INTERNAL_SSH_PORT:-10022}-:22,hostfwd=tcp::${SCREEN_SHARE_PORT:-5900}-:5900,${ADDITIONAL_PORTS} \
-device ${NETWORKING:-vmxnet3},netdev=net0,id=net0,mac=${MAC_ADDRESS:-52:54:00:09:49:17} \
-device virtio-serial-pci \
-chardev spicevmc,id=vdagent0,name=vdagent \
-device virtserialport,chardev=vdagent0,name=com.redhat.spice.0 \
-device virtio-mouse \
-device virtio-keyboard \
-monitor stdio \
-boot menu=on \
-device qxl \
-serial none \
-fsdev local,id=fsdev0,path=${PUBLIC},security_model=none -device virtio-9p-pci,fsdev=fsdev0,mount_tag=${PUBLIC_TAG} \
-device usb-ehci,id=spicepass \
-chardev spicevmc,id=usbredirchardev1,name=usbredir \
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 \
-chardev spicevmc,id=usbredirchardev2,name=usbredir \
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2 \
-chardev spicevmc,id=usbredirchardev3,name=usbredir \
-device usb-redir,chardev=usbredirchardev3,id=usbredirdev3 \
-chardev spiceport,id=webdav0,name=org.spice-space.webdav.0 \
-device virtserialport,chardev=webdav0,name=org.spice-space.webdav.0 \
-chardev socket,path=/tmp/qga.sock,server,nowait,id=qga0 \
-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0 \
${EXTRA:-}
(END)