1# SPDX-License-Identifier: BSD-3-Clause 2# Copyright (C) 2017 Intel Corporation 3# All rights reserved. 4# 5 6: ${SPDK_VHOST_VERBOSE=false} 7: ${VHOST_DIR="$HOME/vhost_test"} 8: ${QEMU_BIN:="qemu-system-x86_64"} 9: ${QEMU_IMG_BIN="qemu-img"} 10 11TEST_DIR=$(readlink -f $rootdir/..) 12VM_DIR=$VHOST_DIR/vms 13TARGET_DIR=$VHOST_DIR/vhost 14VM_PASSWORD="root" 15 16VM_IMAGE=${VM_IMAGE:-"$DEPENDENCY_DIR/vhost/spdk_test_image.qcow2"} 17FIO_BIN=${FIO_BIN:-} 18 19WORKDIR=$(readlink -f "$(dirname "$0")") 20 21if ! hash $QEMU_IMG_BIN $QEMU_BIN; then 22 echo 'ERROR: QEMU is not installed on this system. Unable to run vhost tests.' >&2 23 return 1 24fi 25 26mkdir -p $VHOST_DIR 27mkdir -p $VM_DIR 28mkdir -p $TARGET_DIR 29 30# 31# Source config describing QEMU and VHOST cores and NUMA 32# 33source $rootdir/test/vhost/common/autotest.config 34source "$rootdir/test/scheduler/common.sh" 35 36function vhosttestinit() { 37 if [ "$TEST_MODE" == "iso" ]; then 38 $rootdir/scripts/setup.sh 39 fi 40 41 if [[ -e $VM_IMAGE.gz && ! -e $VM_IMAGE ]]; then 42 gzip -dc "$VM_IMAGE.gz" > "$VM_IMAGE" 43 fi 44 45 # Look for the VM image 46 if [[ ! -f $VM_IMAGE ]]; then 47 [[ $1 != "--no_vm" ]] || return 0 48 echo "$VM_IMAGE is missing" >&2 49 return 1 50 fi 51} 52 53function vhosttestfini() { 54 if [ "$TEST_MODE" == "iso" ]; then 55 $rootdir/scripts/setup.sh reset 56 fi 57} 58 59function message() { 60 local verbose_out 61 if ! $SPDK_VHOST_VERBOSE; then 62 verbose_out="" 63 elif [[ ${FUNCNAME[2]} == "source" ]]; then 64 verbose_out=" (file $(basename ${BASH_SOURCE[1]}):${BASH_LINENO[1]})" 65 else 66 verbose_out=" (function ${FUNCNAME[2]}:${BASH_LINENO[1]})" 67 fi 68 69 local msg_type="$1" 70 shift 71 echo -e "${msg_type}${verbose_out}: $*" 72} 73 74function fail() { 75 echo "===========" >&2 76 message "FAIL" "$@" >&2 77 echo "===========" >&2 78 exit 1 79} 80 81function error() { 82 echo "===========" >&2 83 message "ERROR" "$@" >&2 84 echo "===========" >&2 85 # Don't 'return 1' since the stack trace will be incomplete (why?) missing upper command. 86 false 87} 88 89function warning() { 90 message "WARN" "$@" >&2 91} 92 93function notice() { 94 message "INFO" "$@" 95} 96 97function check_qemu_packedring_support() { 98 qemu_version=$($QEMU_BIN -version | grep -Po "(?<=version )\d+.\d+.\d+") 99 if [[ "$qemu_version" < "4.2.0" ]]; then 100 error "This qemu binary does not support packed ring" 101 fi 102} 103 104function get_vhost_dir() { 105 local vhost_name="$1" 106 107 if [[ -z "$vhost_name" ]]; then 108 error "vhost name must be provided to get_vhost_dir" 109 return 1 110 fi 111 112 echo "$TARGET_DIR/${vhost_name}" 113} 114 115function vhost_run() { 116 local OPTIND 117 local vhost_name 118 local run_gen_nvme=true 119 local vhost_bin="vhost" 120 local vhost_args=() 121 local cmd=() 122 123 while getopts "n:b:g" optchar; do 124 case "$optchar" in 125 n) vhost_name="$OPTARG" ;; 126 b) vhost_bin="$OPTARG" ;; 127 g) 128 run_gen_nvme=false 129 notice "Skipping gen_nvme.sh NVMe bdev configuration" 130 ;; 131 *) 132 error "Unknown param $optchar" 133 return 1 134 ;; 135 esac 136 done 137 shift $((OPTIND - 1)) 138 139 vhost_args=("$@") 140 141 if [[ -z "$vhost_name" ]]; then 142 error "vhost name must be provided to vhost_run" 143 return 1 144 fi 145 146 local vhost_dir 147 vhost_dir="$(get_vhost_dir $vhost_name)" 148 local vhost_app="$SPDK_BIN_DIR/$vhost_bin" 149 local vhost_log_file="$vhost_dir/vhost.log" 150 local vhost_pid_file="$vhost_dir/vhost.pid" 151 local vhost_socket="$vhost_dir/usvhost" 152 notice "starting vhost app in background" 153 [[ -r "$vhost_pid_file" ]] && vhost_kill $vhost_name 154 [[ -d $vhost_dir ]] && rm -f $vhost_dir/* 155 mkdir -p $vhost_dir 156 157 if [[ ! -x $vhost_app ]]; then 158 error "application not found: $vhost_app" 159 return 1 160 fi 161 162 cmd=("$vhost_app" "-r" "$vhost_dir/rpc.sock" "${vhost_args[@]}") 163 if [[ "$vhost_bin" =~ vhost ]]; then 164 cmd+=(-S "$vhost_dir") 165 fi 166 167 notice "Loging to: $vhost_log_file" 168 notice "Socket: $vhost_socket" 169 notice "Command: ${cmd[*]}" 170 171 timing_enter vhost_start 172 173 iobuf_small_count=${iobuf_small_count:-16383} 174 iobuf_large_count=${iobuf_large_count:-2047} 175 176 "${cmd[@]}" --wait-for-rpc & 177 vhost_pid=$! 178 echo $vhost_pid > $vhost_pid_file 179 180 notice "waiting for app to run..." 181 waitforlisten "$vhost_pid" "$vhost_dir/rpc.sock" 182 183 "$rootdir/scripts/rpc.py" -s "$vhost_dir/rpc.sock" \ 184 iobuf_set_options \ 185 --small-pool-count="$iobuf_small_count" \ 186 --large-pool-count="$iobuf_large_count" 187 188 "$rootdir/scripts/rpc.py" -s "$vhost_dir/rpc.sock" \ 189 framework_start_init 190 191 #do not generate nvmes if pci access is disabled 192 if [[ "${cmd[*]}" != *"--no-pci"* ]] && [[ "${cmd[*]}" != *"-u"* ]] && $run_gen_nvme; then 193 $rootdir/scripts/gen_nvme.sh | $rootdir/scripts/rpc.py -s $vhost_dir/rpc.sock load_subsystem_config 194 fi 195 196 notice "vhost started - pid=$vhost_pid" 197 198 timing_exit vhost_start 199} 200 201function vhost_kill() { 202 local rc=0 203 local vhost_name="$1" 204 205 if [[ -z "$vhost_name" ]]; then 206 error "Must provide vhost name to vhost_kill" 207 return 0 208 fi 209 210 local vhost_dir 211 vhost_dir="$(get_vhost_dir $vhost_name)" 212 local vhost_pid_file="$vhost_dir/vhost.pid" 213 214 if [[ ! -r $vhost_pid_file ]]; then 215 warning "no vhost pid file found" 216 return 0 217 fi 218 219 timing_enter vhost_kill 220 local vhost_pid 221 vhost_pid="$(cat $vhost_pid_file)" 222 notice "killing vhost (PID $vhost_pid) app" 223 224 if kill -INT $vhost_pid > /dev/null; then 225 notice "sent SIGINT to vhost app - waiting 60 seconds to exit" 226 for ((i = 0; i < 60; i++)); do 227 if kill -0 $vhost_pid; then 228 echo "." 229 sleep 1 230 else 231 break 232 fi 233 done 234 if kill -0 $vhost_pid; then 235 error "ERROR: vhost was NOT killed - sending SIGABRT" 236 kill -ABRT $vhost_pid 237 rc=1 238 else 239 while kill -0 $vhost_pid; do 240 echo "." 241 done 242 fi 243 elif kill -0 $vhost_pid; then 244 error "vhost NOT killed - you need to kill it manually" 245 rc=1 246 else 247 notice "vhost was not running" 248 fi 249 250 timing_exit vhost_kill 251 252 rm -rf "$vhost_dir" 253 254 return $rc 255} 256 257function vhost_rpc() { 258 local vhost_name="$1" 259 260 if [[ -z "$vhost_name" ]]; then 261 error "vhost name must be provided to vhost_rpc" 262 return 1 263 fi 264 shift 265 266 $rootdir/scripts/rpc.py -s $(get_vhost_dir $vhost_name)/rpc.sock "$@" 267} 268 269### 270# Mgmt functions 271### 272 273function assert_number() { 274 [[ "$1" =~ [0-9]+ ]] && return 0 275 276 error "Invalid or missing parameter: need number but got '$1'" 277 return 1 278} 279 280# Run command on vm with given password 281# First argument - vm number 282# Second argument - ssh password for vm 283# 284function vm_sshpass() { 285 vm_num_is_valid $1 || return 1 286 287 local ssh_cmd 288 ssh_cmd="sshpass -p $2 ssh \ 289 -o UserKnownHostsFile=/dev/null \ 290 -o StrictHostKeyChecking=no \ 291 -o User=root \ 292 -p $(vm_ssh_socket $1) $VM_SSH_OPTIONS 127.0.0.1" 293 294 shift 2 295 $ssh_cmd "$@" 296} 297 298# Helper to validate VM number 299# param $1 VM number 300# 301function vm_num_is_valid() { 302 [[ "$1" =~ ^[0-9]+$ ]] && return 0 303 304 error "Invalid or missing parameter: vm number '$1'" 305 return 1 306} 307 308# Print network socket for given VM number 309# param $1 virtual machine number 310# 311function vm_ssh_socket() { 312 vm_num_is_valid $1 || return 1 313 local vm_dir="$VM_DIR/$1" 314 315 cat $vm_dir/ssh_socket 316} 317 318function vm_fio_socket() { 319 vm_num_is_valid $1 || return 1 320 local vm_dir="$VM_DIR/$1" 321 322 cat $vm_dir/fio_socket 323} 324 325# Execute command on given VM 326# param $1 virtual machine number 327# 328function vm_exec() { 329 vm_num_is_valid $1 || return 1 330 331 local vm_num="$1" 332 shift 333 334 sshpass -p "$VM_PASSWORD" ssh \ 335 -o UserKnownHostsFile=/dev/null \ 336 -o StrictHostKeyChecking=no \ 337 -o User=root \ 338 -p $(vm_ssh_socket $vm_num) $VM_SSH_OPTIONS 127.0.0.1 \ 339 "$@" 340} 341 342# Execute scp command on given VM 343# param $1 virtual machine number 344# 345function vm_scp() { 346 vm_num_is_valid $1 || return 1 347 348 local vm_num="$1" 349 shift 350 351 sshpass -p "$VM_PASSWORD" scp \ 352 -o UserKnownHostsFile=/dev/null \ 353 -o StrictHostKeyChecking=no \ 354 -o User=root \ 355 -P $(vm_ssh_socket $vm_num) $VM_SSH_OPTIONS \ 356 "$@" 357} 358 359# check if specified VM is running 360# param $1 VM num 361function vm_is_running() { 362 vm_num_is_valid $1 || return 1 363 local vm_dir="$VM_DIR/$1" 364 365 if [[ ! -r $vm_dir/qemu.pid ]]; then 366 return 1 367 fi 368 369 local vm_pid 370 vm_pid="$(cat $vm_dir/qemu.pid)" 371 372 if /bin/kill -0 $vm_pid; then 373 return 0 374 else 375 if [[ $EUID -ne 0 ]]; then 376 warning "not root - assuming VM running since can't be checked" 377 return 0 378 fi 379 380 # not running - remove pid file 381 rm -f $vm_dir/qemu.pid 382 return 1 383 fi 384} 385 386# check if specified VM is running 387# param $1 VM num 388function vm_os_booted() { 389 vm_num_is_valid $1 || return 1 390 local vm_dir="$VM_DIR/$1" 391 392 if [[ ! -r $vm_dir/qemu.pid ]]; then 393 error "VM $1 is not running" 394 return 1 395 fi 396 397 if ! VM_SSH_OPTIONS="-o ControlMaster=no" vm_exec $1 "true" 2> /dev/null; then 398 # Shutdown existing master. Ignore errors as it might not exist. 399 VM_SSH_OPTIONS="-O exit" vm_exec $1 "true" 2> /dev/null 400 return 1 401 fi 402 403 return 0 404} 405 406# Shutdown given VM 407# param $1 virtual machine number 408# return non-zero in case of error. 409function vm_shutdown() { 410 vm_num_is_valid $1 || return 1 411 local vm_dir="$VM_DIR/$1" 412 if [[ ! -d "$vm_dir" ]]; then 413 error "VM$1 ($vm_dir) not exist - setup it first" 414 return 1 415 fi 416 417 if ! vm_is_running $1; then 418 notice "VM$1 ($vm_dir) is not running" 419 return 0 420 fi 421 422 # Temporarily disabling exit flag for next ssh command, since it will 423 # "fail" due to shutdown 424 notice "Shutting down virtual machine $vm_dir" 425 set +e 426 vm_exec $1 "nohup sh -c 'shutdown -h -P now'" || true 427 notice "VM$1 is shutting down - wait a while to complete" 428 set -e 429} 430 431# Kill given VM 432# param $1 virtual machine number 433# 434function vm_kill() { 435 vm_num_is_valid $1 || return 1 436 local vm_dir="$VM_DIR/$1" 437 438 if [[ ! -r $vm_dir/qemu.pid ]]; then 439 return 0 440 fi 441 442 local vm_pid 443 vm_pid="$(cat $vm_dir/qemu.pid)" 444 445 notice "Killing virtual machine $vm_dir (pid=$vm_pid)" 446 # First kill should fail, second one must fail 447 if /bin/kill $vm_pid; then 448 notice "process $vm_pid killed" 449 rm -rf $vm_dir 450 elif vm_is_running $1; then 451 error "Process $vm_pid NOT killed" 452 return 1 453 fi 454} 455 456# List all VM numbers in VM_DIR 457# 458function vm_list_all() { 459 local vms=() 460 vms=("$VM_DIR"/+([0-9])) 461 if ((${#vms[@]} > 0)); then 462 basename --multiple "${vms[@]}" 463 fi 464} 465 466# Kills all VM in $VM_DIR 467# 468function vm_kill_all() { 469 local vm 470 for vm in $(vm_list_all); do 471 vm_kill $vm 472 done 473 474 rm -rf $VM_DIR 475} 476 477# Shutdown all VM in $VM_DIR 478# 479function vm_shutdown_all() { 480 # XXX: temporarily disable to debug shutdown issue 481 # xtrace_disable 482 483 local vms 484 vms=$(vm_list_all) 485 local vm 486 487 for vm in $vms; do 488 vm_shutdown $vm 489 done 490 491 notice "Waiting for VMs to shutdown..." 492 local timeo=90 493 while [[ $timeo -gt 0 ]]; do 494 local all_vms_down=1 495 for vm in $vms; do 496 if vm_is_running $vm; then 497 all_vms_down=0 498 break 499 fi 500 done 501 502 if [[ $all_vms_down == 1 ]]; then 503 notice "All VMs successfully shut down" 504 xtrace_restore 505 return 0 506 fi 507 508 ((timeo -= 1)) 509 sleep 1 510 done 511 512 rm -rf $VM_DIR 513 514 xtrace_restore 515} 516 517function vm_setup() { 518 xtrace_disable 519 local OPTIND optchar vm_num 520 521 local os="" 522 local os_mode="" 523 local qemu_args=() 524 local disk_type_g=NOT_DEFINED 525 local read_only="false" 526 # List created of a strings separated with a ":" 527 local disks=() 528 local raw_cache="" 529 local vm_incoming="" 530 local vm_migrate_to="" 531 local force_vm="" 532 local guest_memory=1024 533 local queue_number="" 534 local vhost_dir 535 local packed=false 536 vhost_dir="$(get_vhost_dir 0)" 537 while getopts ':-:' optchar; do 538 case "$optchar" in 539 -) 540 case "$OPTARG" in 541 os=*) os="${OPTARG#*=}" ;; 542 os-mode=*) os_mode="${OPTARG#*=}" ;; 543 qemu-args=*) qemu_args+=("${OPTARG#*=}") ;; 544 disk-type=*) disk_type_g="${OPTARG#*=}" ;; 545 read-only=*) read_only="${OPTARG#*=}" ;; 546 disks=*) IFS=":" read -ra disks <<< "${OPTARG#*=}" ;; 547 raw-cache=*) raw_cache=",cache${OPTARG#*=}" ;; 548 force=*) force_vm=${OPTARG#*=} ;; 549 memory=*) guest_memory=${OPTARG#*=} ;; 550 queue_num=*) queue_number=${OPTARG#*=} ;; 551 incoming=*) vm_incoming="${OPTARG#*=}" ;; 552 migrate-to=*) vm_migrate_to="${OPTARG#*=}" ;; 553 vhost-name=*) vhost_dir="$(get_vhost_dir ${OPTARG#*=})" ;; 554 spdk-boot=*) local boot_from="${OPTARG#*=}" ;; 555 packed) packed=true ;; 556 *) 557 error "unknown argument $OPTARG" 558 return 1 559 ;; 560 esac 561 ;; 562 *) 563 error "vm_create Unknown param $OPTARG" 564 return 1 565 ;; 566 esac 567 done 568 569 # Find next directory we can use 570 if [[ -n $force_vm ]]; then 571 vm_num=$force_vm 572 573 vm_num_is_valid $vm_num || return 1 574 local vm_dir="$VM_DIR/$vm_num" 575 [[ -d $vm_dir ]] && warning "removing existing VM in '$vm_dir'" 576 else 577 local vm_dir="" 578 579 set +x 580 for ((i = 0; i <= 256; i++)); do 581 local vm_dir="$VM_DIR/$i" 582 [[ ! -d $vm_dir ]] && break 583 done 584 xtrace_restore 585 586 vm_num=$i 587 fi 588 589 if [[ $vm_num -eq 256 ]]; then 590 error "no free VM found. do some cleanup (256 VMs created, are you insane?)" 591 return 1 592 fi 593 594 if [[ -n "$vm_migrate_to" && -n "$vm_incoming" ]]; then 595 error "'--incoming' and '--migrate-to' cannot be used together" 596 return 1 597 elif [[ -n "$vm_incoming" ]]; then 598 if [[ -n "$os_mode" || -n "$os" ]]; then 599 error "'--incoming' can't be used together with '--os' nor '--os-mode'" 600 return 1 601 fi 602 603 os_mode="original" 604 os="$VM_DIR/$vm_incoming/os.qcow2" 605 elif [[ -n "$vm_migrate_to" ]]; then 606 [[ "$os_mode" != "backing" ]] && warning "Using 'backing' mode for OS since '--migrate-to' is used" 607 os_mode=backing 608 fi 609 610 notice "Creating new VM in $vm_dir" 611 mkdir -p $vm_dir 612 613 if [[ "$os_mode" == "backing" ]]; then 614 notice "Creating backing file for OS image file: $os" 615 if ! $QEMU_IMG_BIN create -f qcow2 -b $os $vm_dir/os.qcow2 -F qcow2; then 616 error "Failed to create OS backing file in '$vm_dir/os.qcow2' using '$os'" 617 return 1 618 fi 619 620 local os=$vm_dir/os.qcow2 621 elif [[ "$os_mode" == "original" ]]; then 622 warning "Using original OS image file: $os" 623 elif [[ "$os_mode" != "snapshot" ]]; then 624 if [[ -z "$os_mode" ]]; then 625 notice "No '--os-mode' parameter provided - using 'snapshot'" 626 os_mode="snapshot" 627 else 628 error "Invalid '--os-mode=$os_mode'" 629 return 1 630 fi 631 fi 632 633 local qemu_mask_param="VM_${vm_num}_qemu_mask" 634 local qemu_numa_node_param="VM_${vm_num}_qemu_numa_node" 635 636 if [[ -z "${!qemu_mask_param}" ]] || [[ -z "${!qemu_numa_node_param}" ]]; then 637 error "Parameters ${qemu_mask_param} or ${qemu_numa_node_param} not found in autotest.config file" 638 return 1 639 fi 640 641 local task_mask=${!qemu_mask_param} 642 643 notice "TASK MASK: $task_mask" 644 local cmd=(taskset -a -c "$task_mask" "$QEMU_BIN") 645 local vm_socket_offset=$((10000 + 100 * vm_num)) 646 647 local ssh_socket=$((vm_socket_offset + 0)) 648 local fio_socket=$((vm_socket_offset + 1)) 649 local monitor_port=$((vm_socket_offset + 2)) 650 local migration_port=$((vm_socket_offset + 3)) 651 local gdbserver_socket=$((vm_socket_offset + 4)) 652 local vnc_socket=$((100 + vm_num)) 653 local qemu_pid_file="$vm_dir/qemu.pid" 654 local cpu_num=0 655 656 set +x 657 # cpu list for taskset can be comma separated or range 658 # or both at the same time, so first split on commas 659 cpu_list=$(echo $task_mask | tr "," "\n") 660 queue_number=0 661 for c in $cpu_list; do 662 # if range is detected - count how many cpus 663 if [[ $c =~ [0-9]+-[0-9]+ ]]; then 664 val=$((c - 1)) 665 val=${val#-} 666 else 667 val=1 668 fi 669 cpu_num=$((cpu_num + val)) 670 queue_number=$((queue_number + val)) 671 done 672 673 if [ -z $queue_number ]; then 674 queue_number=$cpu_num 675 fi 676 677 # Normalize tcp ports to make sure they are available 678 ssh_socket=$(get_free_tcp_port "$ssh_socket") 679 fio_socket=$(get_free_tcp_port "$fio_socket") 680 monitor_port=$(get_free_tcp_port "$monitor_port") 681 migration_port=$(get_free_tcp_port "$migration_port") 682 gdbserver_socket=$(get_free_tcp_port "$gdbserver_socket") 683 vnc_socket=$(get_free_tcp_port "$vnc_socket") 684 685 xtrace_restore 686 687 local node_num=${!qemu_numa_node_param} 688 local boot_disk_present=false 689 notice "NUMA NODE: $node_num" 690 cmd+=(-m "$guest_memory" --enable-kvm -cpu host -smp "$cpu_num" -vga std -vnc ":$vnc_socket" -daemonize) 691 cmd+=(-object "memory-backend-file,id=mem,size=${guest_memory}M,mem-path=/dev/hugepages,share=on,prealloc=yes,host-nodes=$node_num,policy=bind") 692 [[ $os_mode == snapshot ]] && cmd+=(-snapshot) 693 [[ -n "$vm_incoming" ]] && cmd+=(-incoming "tcp:0:$migration_port") 694 cmd+=(-monitor "telnet:127.0.0.1:$monitor_port,server,nowait") 695 cmd+=(-numa "node,memdev=mem") 696 cmd+=(-pidfile "$qemu_pid_file") 697 cmd+=(-serial "file:$vm_dir/serial.log") 698 cmd+=(-D "$vm_dir/qemu.log") 699 cmd+=(-chardev "file,path=$vm_dir/seabios.log,id=seabios" -device "isa-debugcon,iobase=0x402,chardev=seabios") 700 cmd+=(-net "user,hostfwd=tcp::$ssh_socket-:22,hostfwd=tcp::$fio_socket-:8765") 701 cmd+=(-net nic) 702 if [[ -z "$boot_from" ]]; then 703 cmd+=(-drive "file=$os,if=none,id=os_disk") 704 cmd+=(-device "ide-hd,drive=os_disk,bootindex=0") 705 fi 706 707 if ((${#disks[@]} == 0)) && [[ $disk_type_g == virtio* ]]; then 708 disks=("default_virtio.img") 709 elif ((${#disks[@]} == 0)); then 710 error "No disks defined, aborting" 711 return 1 712 fi 713 714 for disk in "${disks[@]}"; do 715 # Each disk can define its type in a form of a disk_name,type. The remaining parts 716 # of the string are dropped. 717 IFS="," read -r disk disk_type _ <<< "$disk" 718 [[ -z $disk_type ]] && disk_type=$disk_type_g 719 720 case $disk_type in 721 virtio) 722 local raw_name="RAWSCSI" 723 local raw_disk=$vm_dir/test.img 724 725 # Create disk file if it not exist or it is smaller than 1G 726 if [[ -f $disk && $(stat --printf="%s" $disk) -ge $((1024 * 1024 * 1024)) ]]; then 727 raw_disk=$disk 728 notice "Using existing image $raw_disk" 729 else 730 notice "Creating Virtio disc $raw_disk" 731 dd if=/dev/zero of=$raw_disk bs=1024k count=1024 732 fi 733 734 cmd+=(-device "virtio-scsi-pci,num_queues=$queue_number") 735 cmd+=(-device "scsi-hd,drive=hd$i,vendor=$raw_name") 736 cmd+=(-drive "if=none,id=hd$i,file=$raw_disk,format=raw$raw_cache") 737 ;; 738 spdk_vhost_scsi) 739 notice "using socket $vhost_dir/naa.$disk.$vm_num" 740 cmd+=(-chardev "socket,id=char_$disk,path=$vhost_dir/naa.$disk.$vm_num") 741 cmd+=(-device "vhost-user-scsi-pci,id=scsi_$disk,num_queues=$queue_number,chardev=char_$disk") 742 if [[ "$disk" == "$boot_from" ]]; then 743 cmd[-1]+=,bootindex=0 744 boot_disk_present=true 745 fi 746 ;; 747 spdk_vhost_blk) 748 notice "using socket $vhost_dir/naa.$disk.$vm_num" 749 cmd+=(-chardev "socket,id=char_$disk,path=$vhost_dir/naa.$disk.$vm_num") 750 cmd+=(-device "vhost-user-blk-pci,num-queues=$queue_number,chardev=char_$disk") 751 if [[ "$disk" == "$boot_from" ]]; then 752 cmd[-1]+=,bootindex=0 753 boot_disk_present=true 754 fi 755 756 if $packed; then 757 check_qemu_packedring_support 758 notice "Enabling packed ring support for VM $vm_num, controller $vhost_dir/naa.$disk.$vm_num" 759 cmd[-1]+=,packed=on 760 fi 761 ;; 762 kernel_vhost) 763 if [[ -z $disk ]]; then 764 error "need WWN for $disk_type" 765 return 1 766 elif [[ ! $disk =~ ^[[:alpha:]]{3}[.][[:xdigit:]]+$ ]]; then 767 error "$disk_type - disk(wnn)=$disk does not look like WNN number" 768 return 1 769 fi 770 notice "Using kernel vhost disk wwn=$disk" 771 cmd+=(-device "vhost-scsi-pci,wwpn=$disk,num_queues=$queue_number") 772 ;; 773 vfio_user) 774 notice "using socket $VM_DIR/$vm_num/domain/muser$disk/$disk/cntrl" 775 cmd+=(-device "vfio-user-pci,x-msg-timeout=5000,socket=$VM_DIR/$vm_num/muser/domain/muser$disk/$disk/cntrl") 776 if [[ "$disk" == "$boot_from" ]]; then 777 cmd[-1]+=",bootindex=0" 778 boot_disk_present=true 779 fi 780 ;; 781 vfio_user_virtio) 782 notice "using socket $VM_DIR/vfu_tgt/virtio.$disk" 783 cmd+=(-device "vfio-user-pci,x-msg-timeout=5000,socket=$VM_DIR/vfu_tgt/virtio.$disk") 784 if [[ "$disk" == "$boot_from" ]]; then 785 cmd[-1]+=",bootindex=0" 786 boot_disk_present=true 787 fi 788 ;; 789 *) 790 error "unknown mode '$disk_type', use: virtio, spdk_vhost_scsi, spdk_vhost_blk, kernel_vhost, vfio_user or vfio_user_virtio" 791 return 1 792 ;; 793 esac 794 done 795 796 if [[ -n $boot_from ]] && [[ $boot_disk_present == false ]]; then 797 error "Boot from $boot_from is selected but device is not present" 798 return 1 799 fi 800 801 ((${#qemu_args[@]})) && cmd+=("${qemu_args[@]}") 802 notice "Saving to $vm_dir/run.sh" 803 cat <<- RUN > "$vm_dir/run.sh" 804 #!/bin/bash 805 shopt -s nullglob extglob 806 rootdir=$rootdir 807 source "\$rootdir/test/scheduler/common.sh" 808 qemu_log () { 809 echo "=== qemu.log ===" 810 [[ -s $vm_dir/qemu.log ]] && cat $vm_dir/qemu.log 811 echo "=== qemu.log ===" 812 } 813 814 if [[ \$EUID -ne 0 ]]; then 815 echo "Go away user come back as root" 816 exit 1 817 fi 818 819 trap "qemu_log" EXIT 820 821 qemu_cmd=($(printf '%s\n' "${cmd[@]}")) 822 chmod +r $vm_dir/* 823 echo "Running VM in $vm_dir" 824 rm -f $qemu_pid_file 825 cgroup=\$(get_cgroup \$$) 826 set_cgroup_attr_top_bottom \$$ cgroup.subtree_control "+cpuset" 827 create_cgroup \$cgroup/qemu.$vm_num 828 set_cgroup_attr "\$cgroup/qemu.$vm_num" cpuset.mems "$node_num" 829 set_cgroup_attr "\$cgroup/qemu.$vm_num" cpuset.cpus "$task_mask" 830 "\${qemu_cmd[@]}" 831 832 echo "Waiting for QEMU pid file" 833 sleep 1 834 [[ ! -f $qemu_pid_file ]] && sleep 1 835 [[ ! -f $qemu_pid_file ]] && echo "ERROR: no qemu pid file found" && exit 1 836 set_cgroup_attr "\$cgroup/qemu.$vm_num" cgroup.threads \$(< "$qemu_pid_file") 837 exit 0 838 # EOF 839 RUN 840 chmod +x $vm_dir/run.sh 841 842 # Save generated sockets redirection 843 echo $ssh_socket > $vm_dir/ssh_socket 844 echo $fio_socket > $vm_dir/fio_socket 845 echo $monitor_port > $vm_dir/monitor_port 846 847 rm -f $vm_dir/migration_port 848 [[ -z $vm_incoming ]] || echo $migration_port > $vm_dir/migration_port 849 850 echo $gdbserver_socket > $vm_dir/gdbserver_socket 851 echo $vnc_socket >> $vm_dir/vnc_socket 852 853 [[ -z $vm_incoming ]] || ln -fs $VM_DIR/$vm_incoming $vm_dir/vm_incoming 854 [[ -z $vm_migrate_to ]] || ln -fs $VM_DIR/$vm_migrate_to $vm_dir/vm_migrate_to 855} 856 857function vm_run() { 858 local OPTIND optchar vm 859 local run_all=false 860 local vms_to_run="" 861 862 while getopts 'a-:' optchar; do 863 case "$optchar" in 864 a) run_all=true ;; 865 *) 866 error "Unknown param $OPTARG" 867 return 1 868 ;; 869 esac 870 done 871 872 if $run_all; then 873 vms_to_run="$(vm_list_all)" 874 else 875 shift $((OPTIND - 1)) 876 for vm in "$@"; do 877 vm_num_is_valid $1 || return 1 878 if [[ ! -x $VM_DIR/$vm/run.sh ]]; then 879 error "VM$vm not defined - setup it first" 880 return 1 881 fi 882 vms_to_run+=" $vm" 883 done 884 fi 885 886 for vm in $vms_to_run; do 887 if vm_is_running $vm; then 888 warning "VM$vm ($VM_DIR/$vm) already running" 889 continue 890 fi 891 892 notice "running $VM_DIR/$vm/run.sh" 893 if ! $VM_DIR/$vm/run.sh; then 894 error "FAILED to run vm $vm" 895 return 1 896 fi 897 done 898} 899 900function vm_print_logs() { 901 vm_num=$1 902 warning "================" 903 warning "QEMU LOG:" 904 if [[ -r $VM_DIR/$vm_num/qemu.log ]]; then 905 cat $VM_DIR/$vm_num/qemu.log 906 else 907 warning "LOG qemu.log not found" 908 fi 909 910 warning "VM LOG:" 911 if [[ -r $VM_DIR/$vm_num/serial.log ]]; then 912 cat $VM_DIR/$vm_num/serial.log 913 else 914 warning "LOG serial.log not found" 915 fi 916 917 warning "SEABIOS LOG:" 918 if [[ -r $VM_DIR/$vm_num/seabios.log ]]; then 919 cat $VM_DIR/$vm_num/seabios.log 920 else 921 warning "LOG seabios.log not found" 922 fi 923 warning "================" 924} 925 926# Wait for all created VMs to boot. 927# param $1 max wait time 928function vm_wait_for_boot() { 929 assert_number $1 930 931 xtrace_disable 932 933 local all_booted=false 934 local timeout_time=$1 935 [[ $timeout_time -lt 10 ]] && timeout_time=10 936 local timeout_time 937 timeout_time=$(date -d "+$timeout_time seconds" +%s) 938 939 notice "Waiting for VMs to boot" 940 shift 941 if [[ "$*" == "" ]]; then 942 local vms_to_check="$VM_DIR/[0-9]*" 943 else 944 local vms_to_check="" 945 for vm in "$@"; do 946 vms_to_check+=" $VM_DIR/$vm" 947 done 948 fi 949 950 for vm in $vms_to_check; do 951 local vm_num 952 vm_num=$(basename $vm) 953 local i=0 954 notice "waiting for VM$vm_num ($vm)" 955 while ! vm_os_booted $vm_num; do 956 if ! vm_is_running $vm_num; then 957 warning "VM $vm_num is not running" 958 vm_print_logs $vm_num 959 xtrace_restore 960 return 1 961 fi 962 963 if [[ $(date +%s) -gt $timeout_time ]]; then 964 warning "timeout waiting for machines to boot" 965 vm_print_logs $vm_num 966 xtrace_restore 967 return 1 968 fi 969 if ((i > 30)); then 970 local i=0 971 echo 972 fi 973 echo -n "." 974 sleep 1 975 done 976 echo "" 977 notice "VM$vm_num ready" 978 #Change Timeout for stopping services to prevent lengthy powerdowns 979 #Check that remote system is not Cygwin in case of Windows VMs 980 local vm_os 981 vm_os=$(vm_exec $vm_num "uname -o") 982 if [[ "$vm_os" != "Cygwin" ]]; then 983 vm_exec $vm_num "echo 'DefaultTimeoutStopSec=10' >> /etc/systemd/system.conf; systemctl daemon-reexec" 984 fi 985 done 986 987 notice "all VMs ready" 988 xtrace_restore 989 return 0 990} 991 992function vm_start_fio_server() { 993 local OPTIND optchar 994 local readonly='' 995 local fio_bin='' 996 while getopts ':-:' optchar; do 997 case "$optchar" in 998 -) 999 case "$OPTARG" in 1000 fio-bin=*) local fio_bin="${OPTARG#*=}" ;; 1001 readonly) local readonly="--readonly" ;; 1002 *) error "Invalid argument '$OPTARG'" && return 1 ;; 1003 esac 1004 ;; 1005 *) error "Invalid argument '$OPTARG'" && return 1 ;; 1006 esac 1007 done 1008 1009 shift $((OPTIND - 1)) 1010 for vm_num in "$@"; do 1011 notice "Starting fio server on VM$vm_num" 1012 if [[ $fio_bin != "" ]]; then 1013 vm_exec $vm_num 'cat > /root/fio; chmod +x /root/fio' < $fio_bin 1014 vm_exec $vm_num /root/fio $readonly --eta=never --server --daemonize=/root/fio.pid 1015 else 1016 vm_exec $vm_num fio $readonly --eta=never --server --daemonize=/root/fio.pid 1017 fi 1018 done 1019} 1020 1021function vm_check_scsi_location() { 1022 # Script to find wanted disc 1023 local script='shopt -s nullglob; 1024 for entry in /sys/block/sd*; do 1025 disk_type="$(cat $entry/device/vendor)"; 1026 if [[ $disk_type == INTEL* ]] || [[ $disk_type == RAWSCSI* ]] || [[ $disk_type == LIO-ORG* ]]; then 1027 fname=$(basename $entry); 1028 echo -n " $fname"; 1029 fi; 1030 done' 1031 1032 SCSI_DISK="$(echo "$script" | vm_exec $1 bash -s)" 1033 1034 if [[ -z "$SCSI_DISK" ]]; then 1035 error "no test disk found!" 1036 return 1 1037 fi 1038} 1039 1040# Script to perform scsi device reset on all disks in VM 1041# param $1 VM num 1042# param $2..$n Disks to perform reset on 1043function vm_reset_scsi_devices() { 1044 for disk in "${@:2}"; do 1045 notice "VM$1 Performing device reset on disk $disk" 1046 vm_exec $1 sg_reset /dev/$disk -vNd 1047 done 1048} 1049 1050function vm_check_blk_location() { 1051 local script='shopt -s nullglob; cd /sys/block; echo vd*' 1052 SCSI_DISK="$(echo "$script" | vm_exec $1 bash -s)" 1053 1054 if [[ -z "$SCSI_DISK" ]]; then 1055 error "no blk test disk found!" 1056 return 1 1057 fi 1058} 1059 1060function vm_check_nvme_location() { 1061 SCSI_DISK="$(vm_exec $1 "grep -l SPDK /sys/class/nvme/*/model" | awk -F/ '{print $5"n1"}')" 1062 if [[ -z "$SCSI_DISK" ]]; then 1063 error "no vfio-user nvme test disk found!" 1064 return 1 1065 fi 1066} 1067 1068function run_fio() { 1069 local arg 1070 local job_file="" 1071 local fio_bin="" 1072 local vms=() 1073 local out="" 1074 local vm 1075 local run_server_mode=true 1076 local run_plugin_mode=false 1077 local fio_start_cmd 1078 local fio_output_format="normal" 1079 local fio_gtod_reduce=false 1080 local wait_for_fio=true 1081 1082 for arg in "$@"; do 1083 case "$arg" in 1084 --job-file=*) local job_file="${arg#*=}" ;; 1085 --fio-bin=*) local fio_bin="${arg#*=}" ;; 1086 --vm=*) vms+=("${arg#*=}") ;; 1087 --out=*) 1088 local out="${arg#*=}" 1089 mkdir -p $out 1090 ;; 1091 --local) run_server_mode=false ;; 1092 --plugin) 1093 notice "Using plugin mode. Disabling server mode." 1094 run_plugin_mode=true 1095 run_server_mode=false 1096 ;; 1097 --json) fio_output_format="json" ;; 1098 --hide-results) hide_results=true ;; 1099 --no-wait-for-fio) wait_for_fio=false ;; 1100 --gtod-reduce) fio_gtod_reduce=true ;; 1101 *) 1102 error "Invalid argument '$arg'" 1103 return 1 1104 ;; 1105 esac 1106 done 1107 1108 if [[ -n "$fio_bin" && ! -r "$fio_bin" ]]; then 1109 error "FIO binary '$fio_bin' does not exist" 1110 return 1 1111 fi 1112 1113 if [[ -z "$fio_bin" ]]; then 1114 fio_bin="fio" 1115 fi 1116 1117 if [[ ! -r "$job_file" ]]; then 1118 error "Fio job '$job_file' does not exist" 1119 return 1 1120 fi 1121 1122 fio_start_cmd="$fio_bin --eta=never " 1123 1124 local job_fname 1125 job_fname=$(basename "$job_file") 1126 log_fname="${job_fname%%.*}.log" 1127 fio_start_cmd+=" --output=$out/$log_fname --output-format=$fio_output_format " 1128 1129 # prepare job file for each VM 1130 for vm in "${vms[@]}"; do 1131 local vm_num=${vm%%:*} 1132 local vmdisks=${vm#*:} 1133 1134 sed "s@filename=@filename=$vmdisks@;s@description=\(.*\)@description=\1 (VM=$vm_num)@" "$job_file" \ 1135 | vm_exec $vm_num "cat > /root/$job_fname" 1136 1137 if $fio_gtod_reduce; then 1138 vm_exec $vm_num "echo 'gtod_reduce=1' >> /root/$job_fname" 1139 fi 1140 1141 vm_exec $vm_num cat /root/$job_fname 1142 1143 if $run_server_mode; then 1144 fio_start_cmd+="--client=127.0.0.1,$(vm_fio_socket $vm_num) --remote-config /root/$job_fname " 1145 fi 1146 1147 if ! $run_server_mode; then 1148 if [[ -n "$fio_bin" ]]; then 1149 if ! $run_plugin_mode && [[ -e $fio_bin ]]; then 1150 vm_exec $vm_num 'cat > /root/fio; chmod +x /root/fio' < $fio_bin 1151 vm_fio_bin="/root/fio" 1152 else 1153 vm_fio_bin=$fio_bin 1154 fi 1155 fi 1156 1157 notice "Running local fio on VM $vm_num" 1158 vm_exec $vm_num "$vm_fio_bin --output=/root/$log_fname --output-format=$fio_output_format /root/$job_fname & echo \$! > /root/fio.pid" & 1159 vm_exec_pids+=("$!") 1160 fi 1161 done 1162 1163 if ! $run_server_mode; then 1164 if ! $wait_for_fio; then 1165 return 0 1166 fi 1167 echo "Waiting for guest fio instances to finish.." 1168 wait "${vm_exec_pids[@]}" 1169 1170 for vm in "${vms[@]}"; do 1171 local vm_num=${vm%%:*} 1172 vm_exec $vm_num cat /root/$log_fname > "$out/vm${vm_num}_${log_fname}" 1173 done 1174 return 0 1175 fi 1176 1177 $fio_start_cmd 1178 sleep 1 1179 1180 if [[ "$fio_output_format" == "json" ]]; then 1181 # Fio in client-server mode produces a lot of "trash" output 1182 # preceding JSON structure, making it not possible to parse. 1183 # Remove these lines from file. 1184 # shellcheck disable=SC2005 1185 echo "$(grep -vP '^[<\w]' "$out/$log_fname")" > "$out/$log_fname" 1186 fi 1187 1188 if [[ ! $hide_results ]]; then 1189 cat $out/$log_fname 1190 fi 1191} 1192 1193# Parsing fio results for json output and client-server mode only! 1194function parse_fio_results() { 1195 local fio_log_dir=$1 1196 local fio_log_filename=$2 1197 local fio_csv_filename 1198 1199 # Variables used in parsing loop 1200 local log_file 1201 local rwmode mixread mixwrite 1202 local lat_key lat_divisor 1203 local client_stats iops bw 1204 local read_avg_lat read_min_lat read_max_lat 1205 local write_avg_lat write_min_lat write_min_lat 1206 local clients 1207 1208 declare -A results 1209 results["iops"]=0 1210 results["bw"]=0 1211 results["avg_lat"]=0 1212 results["min_lat"]=0 1213 results["max_lat"]=0 1214 1215 # Loop using the log filename to see if there are any other 1216 # matching files. This is in case we ran fio test multiple times. 1217 log_files=("$fio_log_dir/$fio_log_filename"*) 1218 for log_file in "${log_files[@]}"; do 1219 # Save entire array to avoid opening $log_file multiple times 1220 clients=$(jq -r '.client_stats' "$log_file") 1221 [[ -n $clients ]] 1222 rwmode=$(jq -r '.[0]["job options"]["rw"]' <<< "$clients") 1223 mixread=1 1224 mixwrite=1 1225 if [[ $rwmode = *"rw"* ]]; then 1226 mixread=$(jq -r '.[0]["job options"]["rwmixread"]' <<< "$clients") 1227 mixread=$(bc -l <<< "scale=3; $mixread/100") 1228 mixwrite=$(bc -l <<< "scale=3; 1-$mixread") 1229 fi 1230 1231 client_stats=$(jq -r '.[] | select(.jobname == "All clients")' <<< "$clients") 1232 if [[ -z $client_stats ]]; then 1233 # Potentially single client (single VM) 1234 client_stats=$(jq -r '.[]' <<< "$clients") 1235 fi 1236 1237 # Check latency unit and later normalize to microseconds 1238 lat_key="lat_us" 1239 lat_divisor=1 1240 if jq -er '.read["lat_ns"]' &> /dev/null <<< $client_stats; then 1241 lat_key="lat_ns" 1242 lat_divisor=1000 1243 fi 1244 1245 # Horrific bash float point arithmetic operations below. 1246 # Viewer discretion is advised. 1247 iops=$(jq -r '[.read["iops"],.write["iops"]] | add' <<< $client_stats) 1248 bw=$(jq -r '[.read["bw"],.write["bw"]] | add' <<< $client_stats) 1249 read_avg_lat=$(jq -r --arg lat_key $lat_key '.read[$lat_key]["mean"]' <<< $client_stats) 1250 read_min_lat=$(jq -r --arg lat_key $lat_key '.read[$lat_key]["min"]' <<< $client_stats) 1251 read_max_lat=$(jq -r --arg lat_key $lat_key '.read[$lat_key]["max"]' <<< $client_stats) 1252 write_avg_lat=$(jq -r --arg lat_key $lat_key '.write[$lat_key]["mean"]' <<< $client_stats) 1253 write_min_lat=$(jq -r --arg lat_key $lat_key '.write[$lat_key]["min"]' <<< $client_stats) 1254 write_max_lat=$(jq -r --arg lat_key $lat_key '.write[$lat_key]["max"]' <<< $client_stats) 1255 1256 results["iops"]=$(bc -l <<< "${results[iops]} + $iops") 1257 results["bw"]=$(bc -l <<< "${results[bw]} + $bw") 1258 results["avg_lat"]=$(bc -l <<< "${results[avg_lat]} + ($mixread*$read_avg_lat + $mixwrite*$write_avg_lat)/$lat_divisor") 1259 results["min_lat"]=$(bc -l <<< "${results[min_lat]} + ($mixread*$read_min_lat + $mixwrite*$write_min_lat)/$lat_divisor") 1260 results["max_lat"]=$(bc -l <<< "${results[max_lat]} + ($mixread*$read_max_lat + $mixwrite*$write_max_lat)/$lat_divisor") 1261 done 1262 1263 results["iops"]=$(bc -l <<< "scale=3; ${results[iops]} / ${#log_files[@]}") 1264 results["bw"]=$(bc -l <<< "scale=3; ${results[bw]} / ${#log_files[@]}") 1265 results["avg_lat"]=$(bc -l <<< "scale=3; ${results[avg_lat]} / ${#log_files[@]}") 1266 results["min_lat"]=$(bc -l <<< "scale=3; ${results[min_lat]} / ${#log_files[@]}") 1267 results["max_lat"]=$(bc -l <<< "scale=3; ${results[max_lat]} / ${#log_files[@]}") 1268 1269 fio_csv_filename="${fio_log_filename%%.*}.csv" 1270 cat <<- EOF > "$fio_log_dir/$fio_csv_filename" 1271 iops,bw,avg_lat,min_lat,max_lat 1272 ${results["iops"]},${results["bw"]},${results["avg_lat"]},${results["min_lat"]},${results["max_lat"]} 1273 EOF 1274} 1275 1276# Shutdown or kill any running VM and SPDK APP. 1277# 1278function at_app_exit() { 1279 local vhost_name 1280 1281 notice "APP EXITING" 1282 notice "killing all VMs" 1283 vm_kill_all 1284 # Kill vhost application 1285 notice "killing vhost app" 1286 1287 for vhost_name in "$TARGET_DIR"/*; do 1288 vhost_kill "$(basename "$vhost_name")" 1289 done 1290 1291 notice "EXIT DONE" 1292} 1293 1294function error_exit() { 1295 trap - ERR 1296 print_backtrace 1297 set +e 1298 error "Error on $1 $2" 1299 1300 at_app_exit 1301 exit 1 1302} 1303 1304function lookup_dev_irqs() { 1305 local vm=$1 irqs=() cpus=() 1306 local script_get_irqs script_get_cpus 1307 1308 mkdir -p "$VHOST_DIR/irqs" 1309 1310 # All vhost tests depend either on virtio_blk or virtio_scsi drivers on the VM side. 1311 # Considering that, simply iterate over virtio bus and pick pci device corresponding 1312 # to each virtio device. 1313 # For vfio-user setup, look for bare nvme devices. 1314 1315 script_get_irqs=$( 1316 cat <<- 'SCRIPT' 1317 shopt -s nullglob 1318 for virtio in /sys/bus/virtio/devices/virtio*; do 1319 irqs+=("$(readlink -f "$virtio")/../msi_irqs/"*) 1320 done 1321 irqs+=(/sys/class/nvme/nvme*/device/msi_irqs/*) 1322 printf '%u\n' "${irqs[@]##*/}" 1323 SCRIPT 1324 ) 1325 1326 script_get_cpus=$( 1327 cat <<- 'SCRIPT' 1328 cpus=(/sys/devices/system/cpu/cpu[0-9]*) 1329 printf '%u\n' "${cpus[@]##*cpu}" 1330 SCRIPT 1331 ) 1332 1333 irqs=($(vm_exec "$vm" "$script_get_irqs")) 1334 cpus=($(vm_exec "$vm" "$script_get_cpus")) 1335 ((${#irqs[@]} > 0 && ${#cpus[@]} > 0)) 1336 1337 printf '%u\n' "${irqs[@]}" > "$VHOST_DIR/irqs/$vm.irqs" 1338 printf '%u\n' "${cpus[@]}" > "$VHOST_DIR/irqs/$vm.cpus" 1339} 1340 1341function irqs() { 1342 local vm 1343 for vm; do 1344 vm_exec "$vm" "while :; do cat /proc/interrupts; sleep 1s; done" > "$VHOST_DIR/irqs/$vm.interrupts" & 1345 irqs_pids+=($!) 1346 done 1347} 1348 1349function parse_irqs() { 1350 local iter=${1:-1} 1351 "$rootdir/test/vhost/parse_irqs.sh" "$VHOST_DIR/irqs/"*.interrupts 1352 rm "$VHOST_DIR/irqs/"*.interrupts 1353 1354 mkdir -p "$VHOST_DIR/irqs/$iter" 1355 mv "$VHOST_DIR/irqs/"*.parsed "$VHOST_DIR/irqs/$iter/" 1356} 1357 1358function collect_perf() { 1359 local cpus=$1 outf=$2 runtime=$3 delay=$4 1360 1361 mkdir -p "$VHOST_DIR/perf" 1362 1363 perf record -g \ 1364 ${cpus:+-C "$cpus"} \ 1365 ${outf:+-o "$outf"} \ 1366 ${delay:+-D $((delay * 1000))} \ 1367 -z \ 1368 ${runtime:+ -- sleep $((runtime + delay))} 1369} 1370 1371function parse_perf() { 1372 local iter=${1:-1} 1373 local report out 1374 1375 mkdir -p "$VHOST_DIR/perf/$iter" 1376 shift 1377 1378 for report in "$@" "$VHOST_DIR/perf/"*.perf; do 1379 [[ -f $report ]] || continue 1380 perf report \ 1381 -n \ 1382 -i "$report" \ 1383 --header \ 1384 --stdio > "$VHOST_DIR/perf/$iter/${report##*/}.parsed" 1385 cp "$report" "$VHOST_DIR/perf/$iter/" 1386 done 1387 rm "$VHOST_DIR/perf/"*.perf 1388} 1389 1390function get_from_fio() { 1391 local opt=$1 conf=$2 1392 1393 [[ -n $opt && -f $conf ]] || return 1 1394 1395 awk -F= "/^$opt/{print \$2}" "$conf" 1396} 1397 1398function get_free_tcp_port() { 1399 local port=$1 to=${2:-1} sockets=() 1400 1401 mapfile -t sockets < /proc/net/tcp 1402 1403 # If there's a TCP socket in a listening state keep incrementing $port until 1404 # we find one that's not used. $to determines how long should we look for: 1405 # 0: don't increment, just check if given $port is in use 1406 # >0: increment $to times 1407 # <0: no increment limit 1408 1409 while [[ ${sockets[*]} == *":$(printf '%04X' "$port") 00000000:0000 0A"* ]]; do 1410 ((to-- && ++port <= 65535)) || return 1 1411 done 1412 1413 echo "$port" 1414} 1415 1416function limit_vhost_kernel_threads() { 1417 local cpus=$1 nodes cpu _cpus=() _nodes=() 1418 local _pids=() pid cgroup pid 1419 1420 xtrace_disable_per_cmd map_cpus 1421 1422 _cpus=($(parse_cpu_list <(echo "$cpus"))) 1423 1424 for cpu in "${_cpus[@]}"; do 1425 _nodes+=("${cpu_node_map[cpu]}") 1426 done 1427 1428 nodes=$(fold_array_onto_string "${_nodes[@]}") 1429 1430 # vhost kernel threads are named as vhost-PID 1431 _pids=($(pgrep vhost)) 1432 ((${#_pids[@]} > 0)) || return 1 1433 1434 # All threads should be located under the same initial cgroup. kthreadd does not put them 1435 # under root cgroup, but rather the cgroup of a session from which target/vhost was configured. 1436 # We create dedicated cgroup under the initial one to move all the threads only once instead of 1437 # having an extra step of moving them to the root cgroup first. 1438 set_cgroup_attr_top_bottom "${_pids[0]}" cgroup.subtree_control "+cpuset" 1439 1440 cgroup=$(get_cgroup "${_pids[0]}") 1441 create_cgroup "$cgroup/vhost" 1442 1443 set_cgroup_attr "$cgroup/vhost" cpuset.cpus "$cpus" 1444 set_cgroup_attr "$cgroup/vhost" cpuset.mems "$nodes" 1445 1446 for pid in "${_pids[@]}"; do 1447 move_proc "$pid" "$cgroup/vhost" "$cgroup" cgroup.threads 1448 done 1449} 1450