Posted by Martin on Tue 11 Aug 06:44
report abuse | download | new post
- #!/usr/bin/perl -w
- my $CPULEV="dev.cpu.0.freq_levels";
- my $CPUFREQ="dev.cpu.0.freq";
- my $CPUTEMP="hw.acpi.thermal.tz0.temperature";
- my $CPUMAXTEMP=75;
- my @levels=`sysctl $CPULEV`;
- $levels=$levels[0];
- pop @levels;
- foreach (@levels) {
- s/\/.*//;
- }
- my @hist_temp=();
- my @hist_freq=();
- my $curfreq=&getval($CPUFREQ);
- my $curtemp=&getval($CPUTEMP);
- my $lastfreq=$curfreq;
- my $samplecount=10;
- my $i;
- my $j;
- for ($i=0; $i<$samplecount; $i++) {
- $hist_temp[$i]=$curtemp;
- $hist_freq[$i]=$curfreq;
- }
- while (1) {
- $i=($i+1)%($samplecount);
- $hist_temp[$i]=&getval($CPUTEMP);
- $hist_freq[$i]=$lastfreq;
- my $newfreq=0;
- for ($j=0; $j<$samplecount; $j++) {
- $newfreq += $hist_freq[$j];
- }
- $newfreq = $newfreq / $samplecount;
- print "Freq: $hist_freq[$i], temp: $hist_temp[$i] => want freq: $newfreq\n";
- my $sleeplength=5;
- my $tfreq=$newfreq;
- if ($hist_temp[$i]>$CPUMAXTEMP) {
- $sleeplength=1;
- for ($j=0; $j<scalar(@levels); $j++) {
- $newfreq = ($levels[$j]<$tfreq) ? $levels[$j] : $newfreq;
- }
- } else {
- $j=0;
- while (($j<scalar(@levels)) && ($levels[$j]<=$tfreq)) {
- $j++;
- }
- $newfreq=$levels[$j];
- }
- &setfreq($CPUFREQ, $newfreq);
- sleep $sleeplength;
- }
- sub getval {
- my @pa=@_;
- my @res = `sysctl $pa[0]`;
- $res=$res[0];
- $res =~ s/.*: //;
- }
- sub setfreq {
- my @pa=@_;
- if ($lastfreq!=$pa[1]) {
- $setfreq = "sysctl $pa[0]=$pa[1]";
- print "$setfreq\n";
- my $set=`$setfreq`;
- $lastfreq=$pa[1];
- }
- return 0;
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.