Martin
By: a guest | Aug 11th, 2009 | Syntax:
Perl | Size: 2.05 KB | Hits: 148 | Expires: Never
#!/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];
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);
}
sub getval {
my @pa=@_;
my @res = `sysctl $pa[0]`;
$res=$res[0];
$res =~ s/.*: //;
$res =~ s/[A-Z]$//;
}
sub setfreq {
my @pa=@_;
if ($lastfreq!=$pa[1]) {
$setfreq = "sysctl $pa[0]=$pa[1]";
my $set=`$setfreq`;
$lastfreq=$pa[1];
}
}