Advertisement
Querzion

etc-libvirt-hooks-qemu-d-winx-prepare-begin-20-reserve-hugepages.sh

Apr 5th, 2023
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.44 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. ## Load VM variables
  4. source "/etc/libvirt/hooks/qemu.d/Windows-11/vm-vars.conf"
  5.  
  6. ## Calculate number of hugepages to allocate from memory (in MB)
  7. HUGEPAGES="$(($VM_MEMORY/$(($(grep Hugepagesize /proc/meminfo | awk '{print $2}')))))"
  8.  
  9. echo "Allocating hugepages at 2048 KiB per page..."
  10. echo $HUGEPAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  11. ALLOC_PAGES=$(cat /proc/sys/vm/nr_hugepages)
  12.  
  13.  
  14. ## If successful, notify user
  15. if [ "$ALLOC_PAGES" -eq "$HUGEPAGES" ]
  16. then
  17.     echo "Succesfully allocated $ALLOC_PAGES / $HUGEPAGES pages!"
  18. fi
  19.  
  20.  
  21. ## Drop caches to free up memory for hugepages if not successful
  22. if [ "$ALLOC_PAGES" -ne "$HUGEPAGES" ]
  23. then
  24.     echo 3 > /proc/sys/vm/drop_caches
  25. fi
  26.  
  27. ## If not successful, try up to 10000 times to allocate
  28. TRIES=0
  29. while (( $ALLOC_PAGES != $HUGEPAGES && $TRIES < 1000 ))
  30. do
  31.     ## Defrag RAM then try to allocate pages again
  32.     echo 1 > /proc/sys/vm/compact_memory
  33.     echo $HUGEPAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  34.     ALLOC_PAGES=$(cat /proc/sys/vm/nr_hugepages)
  35.     ## If successful, notify user
  36.     echo "Succesfully allocated $ALLOC_PAGES / $HUGEPAGES pages!"
  37.     let TRIES+=1
  38. done
  39.  
  40. ## If still unable to allocate all requested pages, revert hugepages and quit
  41. if [ "$ALLOC_PAGES" -ne "$HUGEPAGES" ]
  42. then
  43.     echo "Not able to allocate all hugepages. Reverting..."
  44.     echo 0 > /proc/sys/vm/nr_hugepages
  45.     exit 1
  46. fi
  47.  
  48. sleep 1
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement