Advertisement
Guest User

SC2 Starter/Tuning/Benchmark Script

a guest
Aug 20th, 2012
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.34 KB | None | 0 0
  1. #!/bin/bash
  2. clear && clear
  3.  
  4. # this script starts SC2 with some tuning commands and if "-b" is passed, gathers and analyzes benchmark information while SC2 runs.
  5. # it requires the program "perf" to capture the benchmark data
  6. #
  7. # things, you should check/adjust in order to make it run on your machine:
  8. #   - path variables - to: wine-prefix, SC2-executable (+commands), SC2-appdata-folder
  9. #   - SC2-executable (+commands)
  10. #   - cpu-governor settings
  11. #   - cpu core number
  12. #   - cpu core affinity
  13. #
  14. # further things you could customize:
  15. #   - kernel parameters - these were my initial reason to write this script, though i highly recommend you, to find out on your own if you want to use my values because they are still very experimental.
  16. #   - process attributes - if you are unhappy with my settings (nice, schedule policy ...)
  17. #   - probed benchmark data - but, by changing this, you will for sure break the benchmark data analysis
  18. #
  19. # just text-search for the listed things above and you will find the right place to edit.
  20. #
  21. #
  22. #
  23. # this script is not finished yet. there are quite some possibilies to improve it.
  24. # e.g. the benchmark results will always the saved in $sc_appdata_path/bench-logs/bench-result
  25.  
  26.  
  27.  
  28. sudo -v # gets the preemptive super user privileges to execute only certain commands later on. dont panic, it wont start SC2 with sudo.
  29. sleep 2s # this also gives you the opportunity to kill the script with CTRL + C
  30.  
  31. # --- VARIABLES:
  32. declare -i i; i=0
  33. declare -i j; j=0
  34. # --- following 4 lines randomly set k/l to 1/2 or 2/1 to assign the SC2/wineserver process randomly to cpu core number 1/2 or 2/1. i wanted to have some rotation in that. if you want a static core assignment or you have more than 2 cores (so randomly k!=l & k/l = [1-4]), you might adjust this.
  35. declare -i k; k=$RANDOM
  36. let "k %= 2","k += 1"
  37. declare -i l; if [ $k -eq 1 ]; then
  38. l=2; else l=1; fi
  39. declare sc_date; sc_date=`date +%Y-%m-%d_%H-%M-%S`
  40. # --- these 3 path variables ($sc_wine_pref, $sc_exec_path, $sc_appdata_path) point to the folders of ... just look at them.
  41. # --- note: there are no " or ' quotes around them. i dont know why quotes break the script neither do i why it runs without them :) - just stay with the syntax.
  42. declare sc_wine_pref; sc_wine_pref=~/.wine/
  43. declare sc_exec_path; sc_exec_path=~/.wine/drive_c/Program\ Files/StarCraft\ II/Support/SC2Switcher.exe
  44. declare sc_appdata_path; sc_appdata_path=~/StarCraft\ II/
  45. declare -a kp_var
  46. declare -a kp_var_bak
  47.  
  48. # --- following array defines the kernel parameters and the new values, to which the script will change them before SC2-start and set them back to default afterwards.
  49. kp_var=(
  50. # --- scheduler parameters
  51. #kernel.sched_child_runs_first=1
  52. #kernel.sched_time_avg=250
  53. #kernel.sched_rt_period_us=957500
  54. #kernel.sched_rt_runtime_us=-1
  55. #kernel.sched_nr_migrate=48
  56. #kernel.sched_latency_ns=8000000
  57. #kernel.yama.ptrace_scope=0
  58. #kernel.sched_wakeup_granularity_ns=25000000
  59. #kernel.sched_min_granularity_ns=4000000
  60. #vm.overcommit_memory=2
  61. #vm.overcommit_ratio=50
  62. #kernel.ftrace_enabled=0
  63. # --- network parameters
  64. #net.core.rmem_max=67108864
  65. #net.core.wmem_max=67108864
  66. #'net.ipv4.tcp_rmem=4096 3932160 62914560'
  67. #'net.ipv4.tcp_wmem=4096 196608 3145728'
  68. #net.ipv4.tcp_mtu_probing=1
  69. #net.ipv4.tcp_timestamps=0
  70. )
  71.  
  72. # --- "if" declares the all needed vars when benchmark mode is turned on.
  73. if [ "$1" == "-b" ]; then
  74.     declare -i m; m=0 # all the integers act as counter vars or booleans
  75.     declare -i n; n=0
  76.     declare -i o; o=0
  77.     declare -i reg_match; reg_match=0
  78.     declare -i bv_line_m; bv_line_m=0
  79.     declare -i bv_char_m1; bv_char_m1=1
  80.     declare -i bv_char_m2; bv_char_m2=1
  81.     declare -i bv_var_types; bv_var_types=0
  82.     declare bv_btime; bv_btime=1 # $bv_btime = timeinterval, how log all the processes are monitored every 10 seconds. needs to be between 1-10 but caution, all processes are captured so all above 2-3 seconds might write about 150-200mb dump on hd, which gets overwritten every cycle.
  83.     declare bv_btime_inv; bv_btime_inv=$(echo "10 - bv_btime" | bc) # inverted $bv_btime to keep the benchmark cycle at 10 seconds.
  84.     declare reg_line; reg_line=0
  85.     declare bv_line; bv_line=0
  86.     declare bv_char; bv_char=0
  87.     declare -a bv_clean_data
  88.     declare -a bv_results
  89.     # --- "if" checks if a "bench-log" directory exists in ### and creates one if not.
  90.     if [ ! -d "${sc_appdata_path}"bench-logs ]; then
  91.         echo "BENCH-LOG DIR DOESNT EXIST ... CREATING NEW ONE"; sleep 2s
  92.         mkdir "${sc_appdata_path}"bench-logs
  93.     fi
  94.     echo "----------------"; echo "-BENCHMARK MODE-"; echo "----------------"; sleep 3s;
  95. fi
  96.  
  97. # --- "if" checks if the SC2 settings has been changed and creates new backup if so.
  98. if diff "${sc_appdata_path}"Variables.txt "`ls -r "${sc_appdata_path}"Variables.txt.bak.* | head -n1 | sed 's_ _\\ _g'`" >/dev/null; then
  99.     echo "VARIABLES MATCHING BACKUP"
  100. else
  101.     echo "VARIABLES ALTERED ... CREATING NEW BACKUP"
  102.     cp "${sc_appdata_path}"Variables.txt "${sc_appdata_path}"Variables.txt.bak.$sc_date; sleep 2s
  103. fi
  104.  
  105. chmod -w "${sc_appdata_path}"Variables.txt # write-protect the SC2 settings to prevent any unintended changes by SC2. changed back to writable when SC2 is closed.
  106. # --- you can ignore the following 3 lines. used to be an experiment what i never removed.
  107. sudo atieventsd --nosyslog
  108. sudo sync; sleep 2s
  109. echo "CACHE CLEARED"
  110.  
  111. # --- this loop changes the kernel-parameters to the defined values in $kp_var and saves the original values for later
  112. if [ ${#kp_var[@]} ]; then
  113.     echo "----- KERNEL SETTINGS: CHANGED -----"
  114.     for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
  115.         kp_var_bak[$i]=`sysctl -n $(echo ${kp_var[$i]:0:\`expr index "${kp_var[$i]}" "="\`} | sed -e 's_\=__g')`
  116.         sudo sysctl -w "${kp_var[$i]}"
  117.     done
  118.     echo "----- KERNEL SETTINGS: CHANGED -----"
  119. else
  120.     unset kp_var kp_var_bak
  121. fi
  122.  
  123. # --- cpu-governor settings to max performance (for my 2 cpu cores). "man cpufreq-set" for details.
  124. sudo cpufreq-set -c 0 -g performance -d 2000000 -u 2000000
  125. sudo cpufreq-set -c 1 -g performance -d 2000000 -u 2000000
  126. echo "CPU GOVERNOR: PERFORMANCE"
  127.  
  128. # --- starts SC2-executable (+commands). notice the "wine64" and the arguments i pass to SC2 and change it if desired.
  129. wine --version; sleep 1s
  130. echo "STARTING SC2 NOW"
  131. WINEDEBUG="-all" wine64 "$sc_exec_path" -opengl -fullproc -skipopenal -confinecursor -Leaks
  132. sleep 10s
  133.  
  134. # --- these 8 commands change some process attributes.
  135. sudo renice -n -15 `pidof SC2.exe` # increases the priority of SC2 and wineserver process.
  136. sudo renice -n -5 `pidof wineserver`
  137. sudo taskset -p $k `pidof SC2.exe` # sets SC2 to one of my cpu cores and wineserver to the other one, cpu core affinity might need to be adjusted.
  138. sudo taskset -p $l `pidof wineserver`
  139. sudo chrt -f -p 30 `pidof SC2.exe` # changes the scheduler priority and policy to FIFO (First in First out) for SC2 and wineserver
  140. sudo chrt -f -p 10 `pidof wineserver`
  141. sudo ionice -c2 -n1 -p`pidof SC2.exe` # increases IO priority of SC2 and wineserver
  142. sudo ionice -c2 -n2 -p`pidof wineserver`
  143.  
  144. # --- this loop runs as long as SC2 runs
  145. while ps ax | grep -v grep | grep SC2.exe > /dev/null; do
  146.     # --- this will run "perf" every 10 seconds and reads out and prints all the SC2-realted probed benchmark data to "bench-sample" file.
  147.     if [ "$1" == "-b" ]; then
  148.         echo "BENCHMARK CYCLE: $m"
  149.         echo "#: $m" >> "${sc_appdata_path}"bench-logs/bench-sample.$sc_date; sleep "$bv_btime_inv"
  150.         sudo perf record -a -v -e sched:sched_switch -e sched:sched_wakeup sleep "$bv_btime"
  151.         sudo perf sched latency | grep SC2.exe >> "${sc_appdata_path}"bench-logs/bench-sample.$sc_date
  152.         echo "BENCHMARK SAMPLE #:$m WRITTEN TO FILE"; let "m += 1","i += 1"    
  153.     else
  154.         let "i += 1"; sleep 10s
  155.     fi
  156.     # --- this "if" renews the sudo and stuff so in the end, you dont need to give your password again to revert e.g. the kernel-parameters.
  157.     if [ $i -eq 30 ]; then
  158.         sudo -v; let "j +=5"; echo -e "$j MINUTES INGAME\n"; i=0
  159.     fi
  160. done
  161. echo "TERMINATED"
  162. echo "YOU PLAYED $j MINUTES"
  163.  
  164. # --- reverts the cpu-governor settings to ondemand (my default).
  165. sudo cpufreq-set -c 0 -g ondemand -d 800000 -u 2000000
  166. sudo cpufreq-set -c 1 -g ondemand -d 800000 -u 2000000
  167. echo "CPU GOVERNOR: ONDEMAND"
  168.  
  169. # --- this loop changes the kernel-params back to default
  170. if [ ${#kp_var[@]} ]; then
  171.     echo "----- KERNEL SETTINGS: DEFAULT -----"
  172.     for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
  173.         sudo sysctl -w "${kp_var[$i]:0:`expr index "${kp_var[$i]}" "="`}${kp_var_bak[$i]}"
  174.     done
  175.     echo "----- KERNEL SETTINGS: DEFAULT -----"
  176. fi
  177.  
  178. wineserver -k
  179. chmod +w "${sc_appdata_path}"Variables.txt # makes the Variables.txt writable again
  180. sensors -A
  181.  
  182. if [ "$1" == "-b" ]; then
  183.     i=0; j=0; m=0; n=0
  184.  
  185.     # --- this chapter prints the header of/to the bench-result file. consisting of used SC2 configurations, "Direct3D"-Registrykey, used kernel-parameters and spreadsheet columns.
  186.     echo -e "\n-------------------------------------------\n---- bench-result.$sc_date -----\n-------------------------------------------" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  187.     echo "Used SC2-Settings:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  188.     ls -r "${sc_appdata_path}"Variables.txt.bak.* | head -n1 | sed "s_/home\/`whoami`\/StarCraft II\/__" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  189.     echo -e "\nDirect3D Registrykey" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  190.     while read reg_line; do
  191.         if [ "$(echo $reg_line | grep Direct3D)" ]; then
  192.             reg_match=1
  193.         elif [ $reg_match -eq 1 ] && [ "$(echo $reg_line | grep Software)" ]; then
  194.             reg_match=0
  195.         elif [ $reg_match -eq 1 ]; then
  196.             echo $reg_line >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  197.         fi
  198.     done < "$sc_wine_pref"user.reg
  199.     echo -e "\nCustom kernel settings:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  200.     for ((j=0; j<=${#kp_var[@]} - 1; j++)); do
  201.         echo "${kp_var[$j]}" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  202.     done
  203.  
  204.     echo "PROCESSING BENCHMAK SAMPLES"
  205.     # ---this loop reads "bench-sample" line by line and modifies these lines.
  206.     while read bv_line; do
  207.         echo -n "."
  208.         if [ "$bv_line" == "#: $m" ]; then
  209.             bv_line_m=1
  210.             let "m += 1"
  211.         elif [ $bv_line_m == 1 ] && [ "$bv_char" != "" ]; then
  212.             while [ "$bv_char" != "" ]; do # this loop cuts out every character between two "|"s out of the bench-sample, puts them into $bv_char, purifies it from non-digits and put the clean result into $bv_clean_data[].
  213.                 bv_char_m1=`expr index "$bv_line" "|"`
  214.                 bv_char_m2=`expr index "${bv_line:$bv_char_m1}" "|"`
  215.                 bv_char=${bv_line:$bv_char_m1:$bv_char_m2}
  216.                 # --- in the following line, i wanted to combine the upper 3 lines into a single one because i want to avoid $bv_char_m1/2 vars, but no success or very slow processing yet.
  217.                 #bv_char=${bv_line:$(expr index "$bv_line" "|"):$(expr index "${bv_line:$(expr index "$bv_line" "|")}" "|")}
  218.                 bv_line=${bv_line:$bv_char_m1}
  219.                 if [ "$bv_char" != "" ] && [ "$bv_char" != "0" ]; then
  220.                     let "o += 1"
  221.                     bv_clean_data[${#bv_clean_data[@]}]="`echo $bv_char | sed -e 's/[A-Za-z]*//g' -e 's/[\:]*//g' -e 's_[\|]*__g' -e 's_[\ ]*__g'`"
  222.                 fi
  223.             done
  224.             let "n += 1"
  225.             if [ $bv_var_types -eq 0 ]; then
  226.                 bv_var_types=$o
  227.             fi
  228.             o=0
  229.             bv_char=0
  230.         fi
  231.     done < "${sc_appdata_path}"bench-logs/bench-sample.$sc_date
  232.     let "m -= 1"
  233.     echo -e "\n\nBenchmark cycles run:$m\t\tTasks monitored:$n\nAverage Results:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  234.  
  235.     # --- this loop calculates the average value of every $j-th elements of $bv_clean_data[] and puts it into $bv_results[] and prints it .
  236.     for ((j=0; j<=$bv_var_types - 1; j++)); do
  237.         for ((i=$j; i<=${#bv_clean_data[@]} - 1; i+=$bv_var_types)); do
  238.             if [ ! ${bv_results[$j]} ]; then
  239.                 bv_results[$j]=0
  240.             fi
  241.             bv_results[$j]=$(echo "${bv_results[$j]} + ${bv_clean_data[$i]}" | bc)
  242.         done
  243.         bv_results[$j]=$(echo -e "scale=5;${bv_results[$j]}/$n" | bc)
  244.     done
  245.     echo -e "\nRuntime ms\t| Switches\t\t| Avg delay ms\t| Max delay ms" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  246.     for ((j=0; j<=$bv_var_types - 1; j++)); do
  247.         echo -e -n "${bv_results[$j]}\t\t" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  248.     done
  249.     cat "${sc_appdata_path}"bench-logs/bench-result.$sc_date
  250.     sudo rm perf.data* # this removes the "perf" benchmark samples
  251. fi
  252. unset i j k l m n o sc_date bv_btime bv_btime_inv bv_char bv_line bv_line_m bv_char_m1 bv_char_m2 bv_var_types bv_clean_data bv_results reg_line reg_match kp_var kp_var_bak sc_wine_pref sc_exec_path sc_appdata_path
  253. sudo -K
  254. echo "FINISH"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement