Skip to content

Instantly share code, notes, and snippets.

@seidler2547
Last active December 16, 2015 05:49
Show Gist options
  • Save seidler2547/5387267 to your computer and use it in GitHub Desktop.
Save seidler2547/5387267 to your computer and use it in GitHub Desktop.
Plagued by the wrong CPU frequency scaling on AMD FX processors (e.g. on 3.2.0 kernel), I wrote this simple userspace governor script which will always set the two Bulldozer/Piledriver cores in the same module to the same frequency based on the current CPU usage.
for i in $(grep -Eo "cpu[0-9]+" /proc/stat); do cpufreq-set -c ${i/cpu/} -g userspace ; done
bash -c 'while :;do grep -E "cpu[0-9]+" /proc/stat;sleep 0.2;done|gawk '"'"'BEGIN{getline MINF<"/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq" ;getline MAXF<"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"}{cn=$1;sub(/cpu/,"",cn);if(cn==0)for(i in cu){ocu[i]=cu[i];cu[i]=0;oci[i]=ci[i];ci[i]=0}pg=rshift(cn,1);for(i=2;i<=NF;i++){if(i!=5)cu[pg]+=$i;ci[pg]+=$i}if(pg*2!=cn){tf=200*(cu[pg]-ocu[pg])/(ci[pg]-oci[pg]);if(tf>100)tf=100;if(tf<0)tf=0;sc=tf*(MAXF-MINF)/100+MINF-300000;sc-=sc%100000;for(j=0;j<2;j++)print sc|"cat>/sys/devices/system/cpu/cpu"(pg*2+j)"/cpufreq/scaling_setspeed"}}'"'&"
@seidler2547
Copy link
Author

Customizing

  • the sleep 0.2 determines the polling frequency
  • since the kernel always sets the frequency to the highest frequency step greater or equal to the value written, there is a down offset of 300000 kHz (300MHz) set in the code ([...]+MINF-300000). Set to approx. 1/2 the distance between the maximum frequency step and the one just below, it's a basic bias towards powersave or performance
  • to ignore "nice" load, add &&i!=3 after if(i!=5
  • to ignore "iowait" load, add &&i!=6 after if(i!=5 (can be combined)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment