Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- clear && clear
- # this script starts SC2 with some tuning commands and if "-b" is passed, gathers and analyzes benchmark information while SC2 runs.
- # it requires the program "perf" to capture the benchmark data
- #
- # things, you should check/adjust in order to make it run on your machine:
- # - path variables - to: wine-prefix, SC2-executable (+commands), SC2-appdata-folder
- # - SC2-executable (+commands)
- # - cpu-governor settings
- # - cpu core number
- # - cpu core affinity
- #
- # further things you could customize:
- # - 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.
- # - process attributes - if you are unhappy with my settings (nice, schedule policy ...)
- # - probed benchmark data - but, by changing this, you will for sure break the benchmark data analysis
- #
- # just text-search for the listed things above and you will find the right place to edit.
- #
- #
- #
- # this script is not finished yet. there are quite some possibilies to improve it.
- # e.g. the benchmark results will always the saved in $sc_appdata_path/bench-logs/bench-result
- sudo -v # gets the preemptive super user privileges to execute only certain commands later on. dont panic, it wont start SC2 with sudo.
- sleep 2s # this also gives you the opportunity to kill the script with CTRL + C
- # --- VARIABLES:
- declare -i i; i=0
- declare -i j; j=0
- # --- 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.
- declare -i k; k=$RANDOM
- let "k %= 2","k += 1"
- declare -i l; if [ $k -eq 1 ]; then
- l=2; else l=1; fi
- declare sc_date; sc_date=`date +%Y-%m-%d_%H-%M-%S`
- # --- these 3 path variables ($sc_wine_pref, $sc_exec_path, $sc_appdata_path) point to the folders of ... just look at them.
- # --- 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.
- declare sc_wine_pref; sc_wine_pref=~/.wine/
- declare sc_exec_path; sc_exec_path=~/.wine/drive_c/Program\ Files/StarCraft\ II/Support/SC2Switcher.exe
- declare sc_appdata_path; sc_appdata_path=~/StarCraft\ II/
- declare -a kp_var
- declare -a kp_var_bak
- # --- 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.
- kp_var=(
- # --- scheduler parameters
- #kernel.sched_child_runs_first=1
- #kernel.sched_time_avg=250
- #kernel.sched_rt_period_us=957500
- #kernel.sched_rt_runtime_us=-1
- #kernel.sched_nr_migrate=48
- #kernel.sched_latency_ns=8000000
- #kernel.yama.ptrace_scope=0
- #kernel.sched_wakeup_granularity_ns=25000000
- #kernel.sched_min_granularity_ns=4000000
- #vm.overcommit_memory=2
- #vm.overcommit_ratio=50
- #kernel.ftrace_enabled=0
- # --- network parameters
- #net.core.rmem_max=67108864
- #net.core.wmem_max=67108864
- #'net.ipv4.tcp_rmem=4096 3932160 62914560'
- #'net.ipv4.tcp_wmem=4096 196608 3145728'
- #net.ipv4.tcp_mtu_probing=1
- #net.ipv4.tcp_timestamps=0
- )
- # --- "if" declares the all needed vars when benchmark mode is turned on.
- if [ "$1" == "-b" ]; then
- declare -i m; m=0 # all the integers act as counter vars or booleans
- declare -i n; n=0
- declare -i o; o=0
- declare -i reg_match; reg_match=0
- declare -i bv_line_m; bv_line_m=0
- declare -i bv_char_m1; bv_char_m1=1
- declare -i bv_char_m2; bv_char_m2=1
- declare -i bv_var_types; bv_var_types=0
- 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.
- declare bv_btime_inv; bv_btime_inv=$(echo "10 - bv_btime" | bc) # inverted $bv_btime to keep the benchmark cycle at 10 seconds.
- declare reg_line; reg_line=0
- declare bv_line; bv_line=0
- declare bv_char; bv_char=0
- declare -a bv_clean_data
- declare -a bv_results
- # --- "if" checks if a "bench-log" directory exists in ### and creates one if not.
- if [ ! -d "${sc_appdata_path}"bench-logs ]; then
- echo "BENCH-LOG DIR DOESNT EXIST ... CREATING NEW ONE"; sleep 2s
- mkdir "${sc_appdata_path}"bench-logs
- fi
- echo "----------------"; echo "-BENCHMARK MODE-"; echo "----------------"; sleep 3s;
- fi
- # --- "if" checks if the SC2 settings has been changed and creates new backup if so.
- if diff "${sc_appdata_path}"Variables.txt "`ls -r "${sc_appdata_path}"Variables.txt.bak.* | head -n1 | sed 's_ _\\ _g'`" >/dev/null; then
- echo "VARIABLES MATCHING BACKUP"
- else
- echo "VARIABLES ALTERED ... CREATING NEW BACKUP"
- cp "${sc_appdata_path}"Variables.txt "${sc_appdata_path}"Variables.txt.bak.$sc_date; sleep 2s
- fi
- 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.
- # --- you can ignore the following 3 lines. used to be an experiment what i never removed.
- sudo atieventsd --nosyslog
- sudo sync; sleep 2s
- echo "CACHE CLEARED"
- # --- this loop changes the kernel-parameters to the defined values in $kp_var and saves the original values for later
- if [ ${#kp_var[@]} ]; then
- echo "----- KERNEL SETTINGS: CHANGED -----"
- for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
- kp_var_bak[$i]=`sysctl -n $(echo ${kp_var[$i]:0:\`expr index "${kp_var[$i]}" "="\`} | sed -e 's_\=__g')`
- sudo sysctl -w "${kp_var[$i]}"
- done
- echo "----- KERNEL SETTINGS: CHANGED -----"
- else
- unset kp_var kp_var_bak
- fi
- # --- cpu-governor settings to max performance (for my 2 cpu cores). "man cpufreq-set" for details.
- sudo cpufreq-set -c 0 -g performance -d 2000000 -u 2000000
- sudo cpufreq-set -c 1 -g performance -d 2000000 -u 2000000
- echo "CPU GOVERNOR: PERFORMANCE"
- # --- starts SC2-executable (+commands). notice the "wine64" and the arguments i pass to SC2 and change it if desired.
- wine --version; sleep 1s
- echo "STARTING SC2 NOW"
- WINEDEBUG="-all" wine64 "$sc_exec_path" -opengl -fullproc -skipopenal -confinecursor -Leaks
- sleep 10s
- # --- these 8 commands change some process attributes.
- sudo renice -n -15 `pidof SC2.exe` # increases the priority of SC2 and wineserver process.
- sudo renice -n -5 `pidof wineserver`
- 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.
- sudo taskset -p $l `pidof wineserver`
- sudo chrt -f -p 30 `pidof SC2.exe` # changes the scheduler priority and policy to FIFO (First in First out) for SC2 and wineserver
- sudo chrt -f -p 10 `pidof wineserver`
- sudo ionice -c2 -n1 -p`pidof SC2.exe` # increases IO priority of SC2 and wineserver
- sudo ionice -c2 -n2 -p`pidof wineserver`
- # --- this loop runs as long as SC2 runs
- while ps ax | grep -v grep | grep SC2.exe > /dev/null; do
- # --- this will run "perf" every 10 seconds and reads out and prints all the SC2-realted probed benchmark data to "bench-sample" file.
- if [ "$1" == "-b" ]; then
- echo "BENCHMARK CYCLE: $m"
- echo "#: $m" >> "${sc_appdata_path}"bench-logs/bench-sample.$sc_date; sleep "$bv_btime_inv"
- sudo perf record -a -v -e sched:sched_switch -e sched:sched_wakeup sleep "$bv_btime"
- sudo perf sched latency | grep SC2.exe >> "${sc_appdata_path}"bench-logs/bench-sample.$sc_date
- echo "BENCHMARK SAMPLE #:$m WRITTEN TO FILE"; let "m += 1","i += 1"
- else
- let "i += 1"; sleep 10s
- fi
- # --- 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.
- if [ $i -eq 30 ]; then
- sudo -v; let "j +=5"; echo -e "$j MINUTES INGAME\n"; i=0
- fi
- done
- echo "TERMINATED"
- echo "YOU PLAYED $j MINUTES"
- # --- reverts the cpu-governor settings to ondemand (my default).
- sudo cpufreq-set -c 0 -g ondemand -d 800000 -u 2000000
- sudo cpufreq-set -c 1 -g ondemand -d 800000 -u 2000000
- echo "CPU GOVERNOR: ONDEMAND"
- # --- this loop changes the kernel-params back to default
- if [ ${#kp_var[@]} ]; then
- echo "----- KERNEL SETTINGS: DEFAULT -----"
- for ((i=0; i<=${#kp_var[@]} - 1; i++)); do
- sudo sysctl -w "${kp_var[$i]:0:`expr index "${kp_var[$i]}" "="`}${kp_var_bak[$i]}"
- done
- echo "----- KERNEL SETTINGS: DEFAULT -----"
- fi
- wineserver -k
- chmod +w "${sc_appdata_path}"Variables.txt # makes the Variables.txt writable again
- sensors -A
- if [ "$1" == "-b" ]; then
- i=0; j=0; m=0; n=0
- # --- this chapter prints the header of/to the bench-result file. consisting of used SC2 configurations, "Direct3D"-Registrykey, used kernel-parameters and spreadsheet columns.
- echo -e "\n-------------------------------------------\n---- bench-result.$sc_date -----\n-------------------------------------------" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- echo "Used SC2-Settings:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- 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
- echo -e "\nDirect3D Registrykey" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- while read reg_line; do
- if [ "$(echo $reg_line | grep Direct3D)" ]; then
- reg_match=1
- elif [ $reg_match -eq 1 ] && [ "$(echo $reg_line | grep Software)" ]; then
- reg_match=0
- elif [ $reg_match -eq 1 ]; then
- echo $reg_line >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- fi
- done < "$sc_wine_pref"user.reg
- echo -e "\nCustom kernel settings:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- for ((j=0; j<=${#kp_var[@]} - 1; j++)); do
- echo "${kp_var[$j]}" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- done
- echo "PROCESSING BENCHMAK SAMPLES"
- # ---this loop reads "bench-sample" line by line and modifies these lines.
- while read bv_line; do
- echo -n "."
- if [ "$bv_line" == "#: $m" ]; then
- bv_line_m=1
- let "m += 1"
- elif [ $bv_line_m == 1 ] && [ "$bv_char" != "" ]; then
- 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[].
- bv_char_m1=`expr index "$bv_line" "|"`
- bv_char_m2=`expr index "${bv_line:$bv_char_m1}" "|"`
- bv_char=${bv_line:$bv_char_m1:$bv_char_m2}
- # --- 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.
- #bv_char=${bv_line:$(expr index "$bv_line" "|"):$(expr index "${bv_line:$(expr index "$bv_line" "|")}" "|")}
- bv_line=${bv_line:$bv_char_m1}
- if [ "$bv_char" != "" ] && [ "$bv_char" != "0" ]; then
- let "o += 1"
- 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'`"
- fi
- done
- let "n += 1"
- if [ $bv_var_types -eq 0 ]; then
- bv_var_types=$o
- fi
- o=0
- bv_char=0
- fi
- done < "${sc_appdata_path}"bench-logs/bench-sample.$sc_date
- let "m -= 1"
- echo -e "\n\nBenchmark cycles run:$m\t\tTasks monitored:$n\nAverage Results:" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- # --- this loop calculates the average value of every $j-th elements of $bv_clean_data[] and puts it into $bv_results[] and prints it .
- for ((j=0; j<=$bv_var_types - 1; j++)); do
- for ((i=$j; i<=${#bv_clean_data[@]} - 1; i+=$bv_var_types)); do
- if [ ! ${bv_results[$j]} ]; then
- bv_results[$j]=0
- fi
- bv_results[$j]=$(echo "${bv_results[$j]} + ${bv_clean_data[$i]}" | bc)
- done
- bv_results[$j]=$(echo -e "scale=5;${bv_results[$j]}/$n" | bc)
- done
- echo -e "\nRuntime ms\t| Switches\t\t| Avg delay ms\t| Max delay ms" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- for ((j=0; j<=$bv_var_types - 1; j++)); do
- echo -e -n "${bv_results[$j]}\t\t" >> "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- done
- cat "${sc_appdata_path}"bench-logs/bench-result.$sc_date
- sudo rm perf.data* # this removes the "perf" benchmark samples
- fi
- 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
- sudo -K
- echo "FINISH"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement