From be4e4da61ee173f6fad1d571161bca45856d16a1 Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 8 Feb 2022 21:53:02 +0100 Subject: [PATCH] Make cpu config easier. --- base.conf.tmpl | 3 +- base.sh | 5 ++ config.conf | 3 ++ cpus | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ start | 10 ++-- 5 files changed, 137 insertions(+), 5 deletions(-) create mode 100755 base.sh create mode 100755 config.conf create mode 100755 cpus diff --git a/base.conf.tmpl b/base.conf.tmpl index 98d34f7..ae01a69 100644 --- a/base.conf.tmpl +++ b/base.conf.tmpl @@ -1,6 +1,7 @@ +-name qemu-vm,debug-threads=on -boot order=dc -machine type=q35,accel=kvm,kernel_irqchip=on --smp 8,sockets=1,cores=4,threads=2 +-smp $NUM_THREADS,sockets=1,cores=$NUM_PROCESSORS,threads=$NUM_THREADS_PER_CORE -enable-kvm -cpu host,kvm=on,topoext,tsc_deadline,tsc_adjust,hv_relaxed,hv_vapic,hv_spinlocks=0x1fff,hv_vpindex,hv_runtime,hv_crash,hv_time,hv_synic,hv_stimer,hv_tlbflush,hv_ipi,hv_vendor_id=null,hv_reset,hv_frequencies,hv_reenlightenment,hv_evmcs,hv_stimer_direct -m 12G diff --git a/base.sh b/base.sh new file mode 100755 index 0000000..7d2cd3f --- /dev/null +++ b/base.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +export NUM_THREADS_PER_CORE=$(./cpus processors_per_core) +export NUM_THREADS=$(./cpus decompress_seq "$(./cpus compute_vm $NUM_PROCESSORS)" | wc -w) + diff --git a/config.conf b/config.conf new file mode 100755 index 0000000..1966def --- /dev/null +++ b/config.conf @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +export NUM_PROCESSORS=4 diff --git a/cpus b/cpus new file mode 100755 index 0000000..c18d26d --- /dev/null +++ b/cpus @@ -0,0 +1,121 @@ +#!/usr/bin/env sh + +all_processors() { + grep -E '(processor|core id)' /proc/cpuinfo | while : ; do + read -r line0 || break + read -r line1 || break + if echo "$line0" | grep -q 'processor'; then + processor="$(echo "$line0" | sed -En 's/^[^0-9]*([0-9]+)$/\1/p')" + core="$(echo "$line1" | sed -En 's/^[^0-9]*([0-9]+)$/\1/p')" + elif echo "$line1" | grep -q 'processor'; then + processor="$(echo "$line1" | sed -En 's/^[^0-9]*([0-9]+)$/\1/p')" + core="$(echo "$line0" | sed -En 's/^[^0-9]*([0-9]+)$/\1/p')" + fi + echo "$processor $core" + done +} + +take_host_processors() { + all_processors | sort -hk2 | head -n$1 +} + +take_vm_processors() { + all_processors | sort -hk2 | tail -n+$(($1+1)) +} + +_compress_seq_sub() { + first="$(echo "$@" | awk '{print $1}')" + last="$(echo "$@" | awk '{print $NF}')" + if [ $first = $last ]; then + printf '%s,' "$first" + else + printf '%s-%s,' "$first" "$last" + fi +} + +_compress_seq() { + buffer= + while read -r item; do + if [ -z "$buffer" ]; then + buffer=$item + continue + fi + if [ $(($(echo "$buffer" | awk '{print $NF}')+1)) -eq $item ]; then + buffer="$buffer $item" + else + _compress_seq_sub $buffer + buffer=$item + fi + done + _compress_seq_sub $buffer + echo +} + +compress_seq() { + _compress_seq | rev | cut -c2- | rev +} + +_decompress_seq() { + ( + IFS=, + for item in $@; do + if echo "$item" | grep -q '-'; then + num0="$(echo "$item" | awk -F- '{print $1}')" + num1="$(echo "$item" | awk -F- '{print $2}')" + printf '%s ' "$(seq $num0 $num1 | xargs echo)" + else + printf '%s ' $item + fi + done + echo + ) +} + +decompress_seq() { + _decompress_seq "$1" | rev | cut -c2- | rev +} + +check_argument_is_number() { + if [ -z "$1" ]; then + echo "Error: must specify number of processors" >&2 + return 1 + fi + if [ "$1" -eq "$1" ] 2>/dev/null; then + true + else + echo "Error: provided argument '$1' is not a number" >&2 + return 1 + fi +} + +compute_all() { + all_processors | sort -hk1 | awk '{print $1}' | compress_seq +} + +compute_host() { + if ! check_argument_is_number $1; then + return $? + fi + take_host_processors $1 | sort -hk1 | awk '{print $1}' | \ + compress_seq +} + +compute_vm() { + if ! check_argument_is_number $1; then + return $? + fi + take_vm_processors $1 | sort -hk1 | awk '{print $1}' | \ + compress_seq +} + +processors_per_core() { + processor_count=$(all_processors | awk '{print $1}' | sort -h | uniq | wc -l) + core_count=$(all_processors | awk '{print $2}' | sort -h | uniq | wc -l) + echo $((processor_count/core_count)) +} + + + + + +"$@" diff --git a/start b/start index 061858c..a5c81e1 100755 --- a/start +++ b/start @@ -45,7 +45,7 @@ $SUDO true NET_CONF_FILE="$($SUDO mktemp)" # create bridge -NET_CONF_FILE="$NET_CONF_FILE" $DEBUG $SUDO --preserve-env=NET_CONF_FILE \ +NET_CONF_FILE="$NET_CONF_FILE" $SUDO --preserve-env=NET_CONF_FILE \ ./net create # rebind devices @@ -54,7 +54,7 @@ IFS=$'\n' for device_override in $(< vfio_devices.txt); do device="$(echo "$device_override" | awk '{print $1}')" override="$(echo "$device_override" | awk '{print $2}')" - $DEBUG $SUDO ./pci vfio_rebind_device "$device" "$override" + $SUDO ./pci vfio_rebind_device "$device" "$override" done IFS="$old_IFS" @@ -63,6 +63,8 @@ EFI_VARS="$(mktemp)" cp /usr/share/ovmf/x64/OVMF_VARS.fd "$EFI_VARS" EFI_FIRMWARE=/usr/share/ovmf/x64/OVMF_CODE.fd +source config.conf + base_path=base default_path=default hardware_path=hardware @@ -129,7 +131,7 @@ read -ra hardware_arguments -d '' < "$o_hardware_path" || true read -ra specific_arguments -d '' < "$o_specific_path" || true # append or true to reverse network changes -$DEBUG $SUDO nice --adjustment=-20 taskset --cpu-list '2-5,8-11' \ +$DEBUG $SUDO nice --adjustment=-20 taskset --cpu-list "$(./cpus compute_vm "$NUM_PROCESSORS")" \ qemu-system-x86_64 \ -name "$VMNAME,process=VMNAME" \ -drive if=pflash,format=raw,readonly=on,file="$EFI_FIRMWARE" \ @@ -139,5 +141,5 @@ $DEBUG $SUDO nice --adjustment=-20 taskset --cpu-list '2-5,8-11' \ "${hardware_arguments[@]}" \ "${specific_arguments[@]}" || true -NET_CONF_FILE="$NET_CONF_FILE" $DEBUG $SUDO --preserve-env=NET_CONF_FILE \ +NET_CONF_FILE="$NET_CONF_FILE" $SUDO --preserve-env=NET_CONF_FILE \ ./net delete