ubuntuusers.de

intel-phc-undervolt.bash

Datum:
13. März 2011 07:28
Code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/bash
# Script downloaded from http://www.openmindedbrain.info
#
# http://www.linux-phc.org/

#                                   #
# Below just some useful functions  #
#                                   #

# Text Colors:
esc="\033["
tc000="${esc}30m"
tcf00="${esc}31m"
tc0f0="${esc}32m"
tcff0="${esc}33m"
tc00f="${esc}34m"
tcf0f="${esc}35m"
tc0ff="${esc}36m"
tcfff="${esc}37m"
tcRst="${esc}0m"
# Background Colors:
bc000="${esc}40m"
bcf00="${esc}41m"
bc0f0="${esc}42m"
bcff0="${esc}43m"
bc00f="${esc}44m"
bcf0f="${esc}45m"
bc0ff="${esc}46m"
bcfff="${esc}47m"


check () {
s=$?
if [ "$s" = "0" ]; then
 printf "$tc0f0> Success!$tcRst\n"
else
 printf "$tcf00> PhaiL!!!$tcRst\n$tcff0> Smash your head on keyboard to continue.$tcRst\n"
 read -n 1 -s > /dev/null
fi
return $s
}

silent_check () {
s=$?
return $s
}

confirm () {
loop=1
while [ "$loop" = "1" ]; do
 printf "Do you want to continue? [Y/n/?] "
 read -n 1 -s answer
 case "$answer" in
  "Y" | "y" | "" )
   printf "${tc0f0}Yes$tcRst\n"
   loop=0
   return 0
   ;;
  "?" )
   printf "${tc00f}?$tcRst\nIt's really just a ${tc0f0}Yes$tcRst or ${tcf00}No$tcRst >:-[\n"
   ;;
  "N" | "n" )
   printf "${tcf00}No$tcRst\n"
   loop=0
   return 1
   ;;
  * )
   printf "${tcff0}$answer$tcRst\n"
   ;;
 esac
done
}

confirm_section () {
confirm || {
 printf "$tc00f> Skipping entire section.$tcRst\n"
 exit 1
}
}

backup () {
printf "Creating backup ${file}~\n"
sudo cp -a "$file" "${file}~"
check
return $?
}

file_check () {
s=0
if [[ "$create" && ! -f "$file" ]]; then
 create=
 printf "  Create $file\n"
 printf "$content" | sudo tee "$file"
 check || return $?
 s=1
fi
if [[ "$owner" && "`stat -c %U:%G \"$file\"`" != "$owner" ]]; then
 printf "  Change ownsership of $file\n"
 sudo chown "$owner" "$file"
 check || return $?
 s=1
fi
if [[ "$perm" && "`stat -c %a \"$file\"`" != "$perm" ]]; then
 printf "  Change permissions of $file\n"
 sudo chmod "$perm" "$file"
 check || return $?
 s=1
fi
if [ "$s" = "0" ]; then
 printf "${tc00f}> SKIPPED:$tcRst Already applied\n"
fi
return 0
}

pattern_count () {
awk "/$pattern/{n++}; END {print n+0}" "$file"
}
line_count () {
awk "/$line/{n++}; END {print n+0}" "$file"
}

pattern_confirm () {
if [ ! -f "$file" ]; then
 printf "${tcff0}> WARNING:$tcRst Could not find $file\n"
 return 1
fi
if [[ ( "$pattern"  && "`pattern_count`" -gt "0" ) || ( ! "$pattern" && "`line_count`" = "0" ) ]]; then
 printf "${tc00f}> SKIPPED:$tcRst Already applied\n"
 return 1
fi
return 0
}

append () {
printf "Appending to $file\n"
printf "$append" | sudo tee -a "$file" > /dev/null
check
return $?
}

replace () {
printf "Scanning $file\n"
result="`awk \"/$line/{sub(/$search/, \\\"$replace\\\")}; {print}\" \"${file}\"`"
check && {
 printf "Writing $file\n"
 printf "$result" | sudo tee "$file" > /dev/null
}
check
return $?
}

confirm_append () {
pattern_confirm && confirm && append
return $?
}

confirm_replace () {
pattern_confirm && confirm && replace
return $?
}

install_confirm () {
printf "\nInstall: $tc0f0$pkgs$tcRst\n"
dpkg -l $pkgs > /dev/null 2>&1 && {
 dpkg -l $pkgs | grep -q "^[pu]" || {
  printf "${tc00f}> SKIPPED:$tcRst Already installed\n"
  return 0
 }
}
confirm && sudo aptitude --without-recommends install $pkgs
return $?
}

purge_confirm () {
printf "\nUninstall (purge): $tcf00$pkgs$tcRst\n"
dpkg -l $pkgs 2> /dev/null | grep -q "^i" > /dev/null || {
 printf "${tc00f}> SKIPPED:$tcRst None installed\n"
 return 0
}
confirm && sudo aptitude --without-recommends purge $pkgs
return $?
}

#                                   #
# End of the useful functions       #
#                                   #




#                       #
#                       #
#                       #
# Below the actual code #
#                       #
#                       #
#                       #

above_vids=4
burn_wait=93

printf "\n
This script will optimize your voltages at every speed setting 
by systematically lowering them while stressing the CPU.
Each voltage will be turned down until your system crashes, and the final
setting for that voltage will be $tc0f0$above_vids$tcRst VIDs above that to \"ensure\" stability.

WARNING:
This script will crash your system as many times as there are VIDs to tweak.
You might destroy your hardware, break laws and/or die in vain if you continue.\n\n"
confirm || exit 0

num_cpus=0
cpu0freq=/sys/devices/system/cpu/cpu0/cpufreq
cpusfreq=""
while [ [1] ]; do
 cat /sys/devices/system/cpu/cpu$num_cpus/cpufreq/phc_default_vids > /dev/null 2>&1
 silent_check || break
 cpusfreq+="/sys/devices/system/cpu/cpu$num_cpus/cpufreq "
 let num_cpus++
done

if [ $num_cpus = 0 ]; then
 printf "$tcf00> ERROR:$tcRst No CPU core capable of undervolting found.\n"
 exit 1
fi

printf "$tc0f0>Found $num_cpus CPU cores.$tcRst\n"
if [ $num_cpus -gt 1 ]; then
 printf "Multiple cores found, the script will use the
same voltages for all cores, this means final
setting will be $tc0f0$above_vids$tcRst VIDs above the weakest core.\n"
fi

printf "\nInstall required packages.\nWill use burnMMX (part of cpuburn package) to stress CPU."
pkgs="cpuburn" # cpufrequtils
install_confirm

def_vids=`cat $cpu0freq/phc_default_vids`
c=0
for i in $def_vids; do
 let c++
 def_vids_arr[c]=$i
done

if [ -f phc_tweaked_vids ]; then
 printf "\nLoad VIDs from 'phc_tweaked_vids' file\n"
 cur_vids=`cat phc_tweaked_vids`
else
 printf "\nRead default VIDs.\n"
 cur_vids="$def_vids"
fi

count_phc=`printf "$def_vids" | awk '{print NF}'`
count_tweak=`printf "$cur_vids" | awk '{printf NF}'`

if [ "$count_phc" != "$count_tweak" ]; then
 printf "$tcf00> ERROR:$tcRst Wrong VID count!\n"
 exit 1
fi
let count_phc--
check || {
 printf "$tcf00> ERROR:$tcRst Number of VIDs is zero!\n"
 exit 1
}

if [[ -f phc_tweaked_vids && -f phc_cur_pos ]]; then
 printf "Load position from 'phc_cur_pos'\n"
 cur_pos=`cat phc_cur_pos`
 let ++cur_pos
 check || exit 1
else
 printf "Reset position to 0.\n"
 cur_pos=0
fi

printf "Read available frequencies.\n"
freqs=`cat $cpu0freq/scaling_available_frequencies`

c=0
for i in $freqs; do
 let c++
 freq[c]=$i
done
if [ "$c" != "$count_tweak" ]; then
 printf "$tcf00> ERROR:$tcRst Number of frequencies ($c) and VIDs ($count_tweak) do not match!\n"
 exit 1
fi
check

#printf "$cur_vids" | awk '{for (i=1; i<=NF; i++) print $i}'
c=0
for i in $cur_vids; do
 let c++
 vid[c]=$i
 if [ "$c" -lt "$cur_pos" ]; then
  vids_done="$vids_done$i "
 fi
 if [ "$c" = "$cur_pos" ]; then
  printf "\nLast VID: $i\n"
  if [ "${vid[$c]}" -eq 0 -a $c -eq 1 ]; then
   printf "I don't trust the CPU can work with the first VID at 0.\nI'm ignoring it and replace it with the default VID.\n"
   let vid[c]=$def_vids
   vid_last="${vid[c]} "
  else
   let tmp="${vid[c]}"+$above_vids
   if [ $tmp -gt "${def_vids_arr[$c]}" ]; then
    let more="${def_vids_arr[$c]}"-"${vid[$c]}"
   else
    more=$above_vids
   fi
   let vid[c]+=$more
   if [ "${vid[$c]}" -gt "${vid[$(( $c - 1 ))]}" ]; then
    printf "Replace with VID from previous position.\n"
    let vid[c]=vid[c-1]
   else
    printf "Increase by +$more\n"
   fi
   vid_last="${vid[c]} "
  fi
 fi
 if [ "$(( c - 1 ))" = "$cur_pos" ]; then
  vid_next="$i"
 else if [ "$c" -gt "$cur_pos" ]; then
   vids_rem="$vids_rem $i"
  fi
 fi
done

printf "\nDefault VIDs: $def_vids
Current VIDs: $tc0f0$vids_done$tcf00$vid_last$tcff0$vid_next$tcRst$vids_rem\n"

printf "$vids_done$vid_last$vid_next$vids_rem" > phc_tweaked_vids
printf "$cur_pos" > phc_cur_pos

if [ "$cur_pos" -gt "$count_phc" ]; then
 printf "\nAll VIDs have been tweaked!
Results are in the file 'phc_tweaked_vids' - use with care.\n"
 printf "\nAll done! - Have a nice day.\n"
 exit 0 
fi

if [ "x$vid_last" != "x" ]; then
 if [ "$vid_next" -gt "${vid[$cur_pos]}" ]; then
  printf "\nNext VID higher than last - copying."
  vid[$(( cur_pos + 1 ))]=${vid[$cur_pos]}
 fi
fi
let ++cur_pos

printf "\nSwitch to 'userspace' scaling governor.\n"
for i in $cpusfreq; do
 printf "userspace" | sudo tee $i/scaling_governor > /dev/null
done
check || exit 1

printf "Set frequency to ${freq[$cur_pos]}.\n"
for i in $cpusfreq; do
 printf "${freq[$cur_pos]}" | sudo tee $i/scaling_setspeed > /dev/null
done
check || exit 1

printf "Run burnMMX.\n"
pkill burnMMX
for i in $cpusfreq; do
 burnMMX &
 printf " PID: $!\n"
done

recover () {
 printf "\n\nRecovering CPU.\n"
 pkill burnMMX
 for i in $cpusfreq; do
  printf "$def_vids" | sudo tee $i/phc_vids > /dev/null
  printf "ondemand" | sudo tee $i/scaling_governor > /dev/null
 done
 printf "\nRun this script again to continue the optimization.\n"
}

printf "\n-----\nStart testing.\n"
confirm || {
 recover
 exit 1
}

while [[ 1 ]]; do
 let vid[cur_pos]--
 if [ "${vid[$cur_pos]}" -lt "0" ]; then
  printf "\n\nThe lowest acceptable VID is 0."
  recover
  exit 0
 fi

 printf "\nDefault VIDs: $def_vids
Current VIDs: $tc0f0$vids_done$tc0f0$vid_last$tcf00${vid[$cur_pos]}$tcRst$vids_rem
Testing VID: ${vid[$cur_pos]} ($(( ${vid[$cur_pos]} * 16 + 700)) mV)\n"

 printf "$vids_done$vid_last${vid[$cur_pos]}$vids_rem" > phc_tweaked_vids
 sync

 for i in $cpusfreq; do
  printf "$vids_done$vid_last${vid[$cur_pos]}$vids_rem" | sudo tee $i/phc_vids > /dev/null
 done

 c=0
 while [ $c -lt $burn_wait ]; do
  burnMMX_instances=`ps aux | grep [b]urnMMX | wc -l`
  if [ $burnMMX_instances -ne $num_cpus ]; then
   printf "\nburnMMX instances different than the CPU cores number. (Maybe one crashed)!"
   recover
   exit 0
  else
   printf "."
   if [ $c -eq 30 -o $c -eq 61 ]; then
    printf "\n"
   fi
   sleep 0.5
   let c++
  fi
 done
done