Guest User

SC2 Starter/Tuning/Benchmark Script

a guest
Sep 8th, 2012
197
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # USAGE:
  4. # start without parameters: (normal SC2-start with usual tweaks)
  5. #   creates Variables.txt-backup, starts SC2, changes kernel parameters/modules, changes process priority/affinity/... and undoes all the changes afterwards.
  6. #
  7. # start with "-b" flag: (benchmark-mode. SC2-start with tweaks, gathers performance information)
  8. #   only gathers performance information in the background and prints the results in "$sc_appdata_path"bench-logs/bench-result when SC2 is terminated.
  9. #
  10. # start with "-b -t" flag: (automated benchmark-mode. SC2-start, gathers performance information, automatically starts a replay and quits SC2 to have a reproducible workload)
  11. #
  12. #
  13. # ADJUSTMENTS:
  14. # (text-search for the listed things and you will find the right place to edit. there might be several of it.)
  15. # things, you should check/adjust in order to make make a normal SC2-start:
  16. #   - path variables - to: wine-prefix, SC2-executable, SC2-appdata-folder
  17. #   - SC2-executable (+commands)
  18. #   - cpu-governor settings
  19. #   - cpu core number
  20. #   - cpu core affinity
  21. #
  22. # further things you could customize: (the script should work fine without adjusting these)
  23. #   - kernel parameters - these were my initial reason to write this script. i highly recommend you, to find out on your own if you want to use my values because they are still very experimental.
  24. #   - kernel-modules - these modules will be removed before and added back after SC2.
  25. #   - process attributes - if you are unhappy with my settings (nice, schedule policy ...).
  26. #
  27. # things you need to adjust for the benchmark-mode: (if "-b" is passed)
  28. #   - probed benchmark data - but, by changing this, you will for sure break the benchmark data analysis.
  29. #   -- NOTE: right now, this script will only handle "bench-sample_fps" files, the parts that calculate the average cpu-/gpu-load are missing but gathering the data is possible.
  30. #
  31. # things you need to adjust for the automated benchmark-mode: (if "-b -t" is passed)
  32. #   - install the package "xautomation". it ("xte") makes automated key-inputs and clicks from enter password over start replay to set player-camera.
  33. #   - click-coordinates - you need to change the x- and  y- coordinates several times for a variety of buttons. (xte 'mousemove X Y')
  34. #   -- im running SC2 with a resolution of 1024x768. if you use a different one, you must adjust the coordinates.
  35. #   - battle.net account-password - you save your password in this script so you dont need to enter it manually all the time.
  36. #
  37. # this script is not finished yet. there are quite some possibilies to improve it.
  38. # e.g. the benchmark results will always the saved in $sc_appdata_path/bench-logs/bench-result
  39.  
  40.  
  41. clear
  42. sudo -v # gets the preemptive super user privileges to execute only certain commands later on. dont panic, it wont start SC2 with sudo.
  43. sleep 2s # this also gives you the opportunity to kill the script with CTRL + C
  44.  
  45. # --- VARIABLES:
  46. declare -i i; i=0
  47. declare -i j; j=0
  48. # --- 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.
  49. declare -i k; k=$RANDOM
  50. let "k %= 2","k += 1"
  51. declare -i l; if [ $k -eq 1 ]; then
  52. l=2; else l=1; fi
  53. declare sc_date; sc_date=`date +%Y-%m-%d_%H-%M-%S`
  54. # --- these 3 path variables ($sc_wine_pref, $sc_exec_path, $sc_appdata_path) point to the folders of ... just look at them.
  55. # ---- 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 ("space" = "\ ").
  56. declare sc_wine_pref; sc_wine_pref=~/.wine/
  57. declare sc_exec_path; sc_exec_path=~/.wine/drive_c/Program\ Files/StarCraft\ II/Support/SC2Switcher.exe
  58. declare sc_appdata_path; sc_appdata_path=~/StarCraft\ II/
  59.  
  60. # --- 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.
  61. : ' <--- commented out, just delete this and the last line of this chapter to enable it.
  62. declare -a kp_var
  63. declare -a kp_var_bak
  64. kp_var=(
  65. # --- scheduler parameters
  66. kernel.sched_child_runs_first=1
  67. kernel.sched_time_avg=250
  68. kernel.sched_rt_period_us=957500
  69. kernel.sched_rt_runtime_us=-1
  70. kernel.sched_nr_migrate=48
  71. kernel.sched_latency_ns=8000000
  72. kernel.yama.ptrace_scope=0
  73. kernel.sched_wakeup_granularity_ns=25000000
  74. kernel.sched_min_granularity_ns=4000000
  75. vm.overcommit_memory=2
  76. vm.overcommit_ratio=50
  77. kernel.ftrace_enabled=0
  78. kernel.perf_event_paranoid=-1
  79. kernel.io_delay_type=3
  80. kernel.watchdog=0
  81. kernel.nmi_watchdog=0
  82. # --- network parameters
  83. net.core.rmem_max=67108864
  84. net.core.wmem_max=67108864
  85. "net.ipv4.tcp_rmem=4096 3932160 62914560"
  86. "net.ipv4.tcp_wmem=4096 196608 3145728"
  87. net.ipv4.tcp_mtu_probing=1
  88. net.ipv4.tcp_timestamps=0
  89. )
  90. ' #<--- remove this line to enable this chapter.
  91.  
  92. # --- these are the the kernel-modules that will be remove and added back again after SC2 is terminated.
  93. : ' <--- commented out, just delete this and the last line of this chapter to enable it.
  94. declare -a km_var
  95. km_var=(
  96. rfcomm
  97. bnep
  98. bluetooth
  99. psmouse
  100. joydev
  101. ppdev
  102. sparse_keymap
  103. uvcvideo
  104. videodev
  105. v4l2_compat_ioctl32
  106. serio_raw
  107. video
  108. acer_wmi
  109. mxm_wmi
  110. wmi
  111. sdhci_pci
  112. sdhci
  113. iwlwifi
  114. mac80211
  115. cfg80211
  116. snd_seq_midi
  117. snd_rawmidi
  118. snd_seq_midi_event
  119. 'lp'
  120. parport_pc
  121. parport
  122. pcmcia
  123. yenta_socket
  124. pcmcia_rsrc
  125. pcmcia_core
  126. )
  127. ' #<--- remove this line to enable this chapter.
  128.  
  129. # --- this part stores the battle.net account-password for the automatic login. if you like, you can save your password in this file.
  130. if [ "$1" == "-b" ] && [ "$2" == "-t" ]; then
  131.     #declare acc_pw; acc_pw="PASSWORD" # <- save your pw here
  132.     if [ ! $acc_pw ]; then
  133.         declare acc_pw
  134.         echo "Please enter Battle.net Account Password"
  135.         read acc_pw
  136.         history -c; reset
  137.     fi
  138. fi
  139.  
  140. # --- "if" declares the all needed vars when benchmark mode is turned on.
  141. if [ "$1" == "-b" ]; then
  142.     declare bv_line; bv_line=0
  143.     declare -a bv_clean_data
  144.     declare bv_results; bv_results=0
  145.     # --- "if" checks if a "bench-log" directory exists in $sc_appdata_path and creates one if not.
  146.     if [ ! -d "$sc_appdata_path"bench-logs ]; then
  147.         echo "BENCH-LOG DIR DOESNT EXIST ... CREATING NEW ONE"; sleep 2s
  148.         mkdir "$sc_appdata_path"bench-logs
  149.     fi
  150. fi
  151.  
  152. # --- "if" checks if the SC2 settings has been changed and creates new backup if so.
  153. if diff "$sc_appdata_path"Variables.txt "`ls -r "$sc_appdata_path"Variables.txt.bak.* | head -n1 | sed 's_ _\\ _g'`" >/dev/null; then
  154.     echo "VARIABLES MATCHING BACKUP"
  155. else
  156.     echo "VARIABLES ALTERED ... CREATING NEW BACKUP"
  157.     cp "$sc_appdata_path"Variables.txt "$sc_appdata_path"Variables.txt.bak.$sc_date; sleep 2s
  158. fi
  159.  
  160. 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.
  161. # --- you can ignore the following lines. used to be an experiment what i never removed.
  162. sudo atieventsd --nosyslog
  163. sudo sync; sleep 2s
  164. echo "CACHE CLEARED"
  165.  
  166. # --- this loop changes the kernel-parameters to the defined values in $kp_var and saves the original values in $kp_var_bak
  167. if [ ${#kp_var[@]} -gt 0 ]; then
  168.     echo "CHANGING KERNEL PARAMETERS"
  169.     for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
  170.         kp_var_bak[$i]=`sysctl -n $(echo ${kp_var[$i]:0:\`expr index "${kp_var[$i]}" "="\`} | sed -e 's_\=__g')`
  171.         sudo sysctl -w "${kp_var[$i]}"
  172.     done
  173. else
  174.     unset kp_var kp_var_bak
  175. fi
  176.  
  177. # --- this loop checks if the listed kernel-modules exist and if not, removes them from the array. after that, the selected modules will be removed
  178. if [ ${#km_var[@]} -gt 0 ]; then
  179.     echo "REMOVING KERNEL MODULES"
  180.     for ((i=0; i<=${#km_var[@]} - 1; i++)); do
  181.         if ! lsmod | grep "${km_var[$i]} " >/dev/null; then
  182.             echo "${km_var[$i]} kernel-module is not loaded"
  183.             unset km_var[$i]
  184.         fi
  185.     done
  186.     sudo modprobe -r ${km_var[@]}
  187. fi
  188.  
  189. # --- cpu-governor settings to max performance (for my 2 cpu cores). "man cpufreq-set" for details.
  190. echo "SETTING CPU GOVERNOR: PERFORMANCE"
  191. sudo cpufreq-set -c 0 -g performance -d 2000000 -u 2000000
  192. sudo cpufreq-set -c 1 -g performance -d 2000000 -u 2000000
  193.  
  194. # --- starts SC2-executable (+commands). notice the "wine64" and the arguments i pass to SC2 and change it if desired.
  195. echo -e "\nSTARTING SC2 NOW"
  196. wine --version; sleep 1s
  197. sleep 1s; xrandr -s 1024x768; sleep 1s
  198. if [ "$1" == "-b" ]; then
  199.     echo "----------------"; echo "-BENCHMARK MODE-"; echo "----------------";
  200.     WINEDEBUG="-all,+fps" wine64 "$sc_exec_path" -opengl -fullproc -skipopenal -confinecursor -Leaks &> "$sc_appdata_path"bench-logs/bench-sample_fps.$sc_date
  201. else
  202.     WINEDEBUG="-all" wine64 "$sc_exec_path" -opengl -fullproc -skipopenal -confinecursor -Leaks
  203. fi
  204. sleep 15s
  205.  
  206. # --- these commands change some process attributes.
  207. sudo renice -n -18 `pidof SC2.exe` # increases the priority of SC2 and wineserver process.
  208. sudo renice -n -18 `pidof wineserver`
  209. 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.
  210. sudo taskset -p $l `pidof wineserver`
  211. sudo chrt -f -p 80 `pidof SC2.exe` # changes the scheduler priority and policy to FIFO (First in First out) for SC2 and wineserver.
  212. sudo chrt -f -p 70 `pidof wineserver`
  213. chrt -p `pidof SC2.exe`
  214. chrt -p `pidof wineserver`
  215. sudo ionice -c2 -n0 -p`pidof SC2.exe` # increases IO priority of SC2 and wineserver.
  216. sudo ionice -c2 -n0 -p`pidof wineserver`
  217. echo -e "ionice: $(pidof SC2.exe): $(sudo ionice -p `pidof SC2.exe`)\nionice: $(pidof wineserver): $(sudo ionice -p `pidof wineserver`)\n"
  218. #sudo schedtool -1 -p 90
  219.  
  220. # --- this part enters the $acc_pw, starts the "BENCHMARK.SC2Replay" and sets player-camera.
  221. # --- its a very ugly solution though, you need to adjust the click-coordinates and and put a replay in the "vs AI" replay-folder. the first one in the list will be started.
  222. if [ "$1" == "-b" ] && [ "$2" == "-t" ]; then
  223.     sleep 25s # wait for SC2 login screen
  224.     xte "str $acc_pw"; sleep 1s; xte 'key Return' # enters password
  225.     unset acc_pw
  226.     sleep 20s # wait for SC2 menu
  227.     xte 'mousemove 320 120'; sleep 1s; xte 'mouseclick 1'; sleep 3s # click on replay-button
  228.     xte 'mousemove 150 370'; sleep 1s; xte 'mouseclick 1';  sleep 3s # click on "VS AI"-button
  229.     xte 'mousemove 380 665'; sleep 1s; xte 'mouseclick 1'; sleep 50s # starts replay and waits for the loading
  230.     xte 'key 2'; sleep 1s # sets player2-camera
  231. fi
  232.  
  233. # --- this loop runs as long as SC2 runs
  234. i=0
  235. j=0
  236. while [ $(pidof SC2.exe) ]; do
  237.     # --- (experimental) this "if" would collect probed benchmark data. i decided to focus on fps only, so this "if" is commented-out.
  238.     #if [ "$1" == "-b" ]; then
  239.         #for ((i=0; i<=10; i++)); do
  240.             #echo "BENCHMARK CYCLE: $m"; let "m += 1","i += 1"; sleep 1s
  241.             #aticonfig --od-getclocks >> "$sc_appdata_path"bench-logs/bench-sample_gpuload.$sc_date
  242.             #ps -eLo pid,%cpu,%mem,cputime,lwp | grep `pidof SC2.exe` >> "$sc_appdata_path"bench-logs/bench-sample_cpuload.$sc_date
  243.         #done
  244.     #else
  245.         sleep 10s; let "i += 1"
  246.     #fi
  247.  
  248.     # --- 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.
  249.     if [ $i -eq 30 ]; then
  250.         sudo -v; let "j += 5"; echo -e "$j MINUTES INGAME\n"; i=0
  251.     fi
  252.     if [ "$1" == "-b" ] && [ "$2" == "-t" ] && [ $j -eq 5 ]; then # end condition of the benchmark: after 5 minutes ($j={5...15...50...}), the following part will close the replay and SC2.
  253.         xte 'key F10'; sleep 1s; xte 'key v'; sleep 15s # quits the replay
  254.         xte 'key Escape'; sleep 2s; xte 'mousemove 500 460'; sleep 1s; xte 'mouseclick 1'; sleep 5s # adjust the click-coordinates to "Quit Starcraft"-Button.
  255.     fi
  256. done
  257. sleep 1s; xrandr -s 1280x800; sleep 1s
  258. echo -e "\nTERMINATED\nYOU PLAYED $j MINUTES\n"
  259. wineserver -k
  260. chmod +w "$sc_appdata_path"Variables.txt # makes the Variables.txt writable again.
  261. sensors -A # this just put the temperature of your cpu out.
  262.  
  263. # --- reverts the cpu-governor settings to ondemand (my default).
  264. echo "SETTING CPU GOVERNOR: ONDEMAND"
  265. sudo cpufreq-set -c 0 -g ondemand -d 800000 -u 2000000
  266. sudo cpufreq-set -c 1 -g ondemand -d 800000 -u 2000000
  267.  
  268. # --- this loop changes the kernel-params back to default
  269. if [ ${#kp_var[@]} -gt 0 ]; then
  270.     echo "RESTORING KERNEL PARAMETERS"
  271.     for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
  272.         sudo sysctl -w "${kp_var[$i]:0:`expr index "${kp_var[$i]}" "="`}${kp_var_bak[$i]}"
  273.     done
  274. fi
  275.  
  276. # --- loads the deactivated kernel-modules again
  277. if [ ${#km_var[@]} -gt 0 ]; then
  278.     echo "RESTORING KERNEL MODULES"
  279.     sudo modprobe -a ${km_var[@]}
  280. fi
  281.  
  282. # --- this chapter runs the benchmark analysis and prints the results to a file in $sc_appdata_path/bench-logs/
  283. if [ "$1" == "-b" ]; then
  284.     i=0; j=0
  285.  
  286.     # --- this chapter prints the header of/to the bench-result file. consisting of used SC2 configurations, "Direct3D"-Registrykey, used kernel-parameters and spreadsheet columns.
  287.     echo -e "\n-------------------------------------------\n---- bench-result.$sc_date -----\n-------------------------------------------" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  288.     echo "Used SC2-Settings:" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  289.     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
  290.     echo -e "\nDirect3D Registrykey:" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  291.     while read bv_line; do
  292.         if [ "$(echo $bv_line | grep Direct3D)" ]; then
  293.             i=1
  294.         elif [ $i -eq 1 ] && [ "$(echo $bv_line | grep Software)" ]; then
  295.             i=0
  296.         elif [ $i -eq 1 ]; then
  297.             echo $bv_line >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  298.         fi
  299.     done < "$sc_wine_pref"user.reg
  300.     if [ ${#kp_var[@]} -gt 0 ]; then
  301.         echo -e "\nCustom kernel settings:" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  302.         for ((j=0; j<=${#kp_var[@]} - 1; j++)); do
  303.             echo "${kp_var[$j]}" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  304.         done
  305.     fi
  306.     if [ ${#km_var[@]} -gt 0 ]; then
  307.         echo -e "\nRemoved kernel modules:\n${km_var[@]}" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  308.     fi
  309.  
  310.     echo -e "\nPROCESSING BENCHMAK SAMPLES"
  311.     # ---this loop reads "bench-sample" line by line and modifies these lines.
  312.     while read bv_line; do
  313.         echo -n "."
  314.         if `echo $bv_line | grep "swapchain_gl_present" 1>/dev/null 2>&1`; then
  315.             bv_clean_data[${#bv_clean_data[@]}]=$(echo ${bv_line:`expr index "$bv_line" "@"` + 7} | sed -e 's/[A-Za-z]*//g')
  316.         fi
  317.     done < "$sc_appdata_path"bench-logs/bench-sample_fps.$sc_date
  318.     echo "done"
  319.     for ((i=0; i<=${#bv_clean_data[@]} - 1; i++)); do # this loop calculates the average fps
  320.         bv_results=$(echo "$bv_results + ${bv_clean_data[$i]}" | bc)
  321.     done
  322.     echo -e "\nAverage FPS:" >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  323.     echo "scale=10;$bv_results/${#bv_clean_data[@]}" | bc >> "$sc_appdata_path"bench-logs/bench-result.$sc_date
  324. fi
  325. unset i j k l sc_date bv_line bv_clean_data bv_results kp_var kp_var_bak km_var sc_wine_pref sc_exec_path sc_appdata_path
  326. sudo -K
  327. echo -e "\nFINISH"
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×