#!/usr/bin/perl -wT # # ctrl_athcool.pl # Version: 1.0.0 05-JUL-2007 # Author: David Mathog, Biology Division, Caltech # Contact: mathog@caltech.edu # Copyright: 2007, David Mathog and Caltech # # This procedure keeps on eye on how busy the system is and # toggles athcool on or off. If the system is busy, it turns it # off, and if it is slow it turns it on. This enables power saving # on a more or less idle system, without sacrificing (much) performance when # the system is busy. # # It looks ONLY at the idle time in /proc/stat. The higher that value per # second the more idle the system is, the lower that value per second, the more # busy the system is. Start this as a daemon when athcool starts. # # # # Parameters are: # None, values are hardwired in here # ##################################################################### # License terms: # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #################################################################### use strict; #################################################################### # site specific values, modify to fit your site!!!! # delete @ENV{'ENV', 'BASH_ENV'}; #or -T complains $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin'; my $STATFILE = '/proc/stat'; my $ATHCOOL = '/usr/sbin/athcool'; my $INTERVAL = 1; # time sampling interval in seconds my $THRESH_DOWN = 50; # threshold for idle measurement going down (indicates a busy system) my $THRESH_UP = 70; # threshold for idle measurement going up (indicates an idle system) my $DELAY_OFF = 1; # seconds it must busy (below THRESH_DOWN) before athcool off my $DELAY_ON = 3; # seconds it must idle (above THRESH_UP) before athcool on #################################################################### # # Do not modify anything below this point # my $lastidle=0; #idle time info from last interval my $idle; #idle time info from this interval my $idif; my $line; my @chunks; my $discard_text; my $count_down = $DELAY_OFF; my $count_up = $DELAY_ON; # start athcool (script must know what state athcool is in, harmless if athcool is already running) my $athcool_state = 1; $discard_text= `$ATHCOOL on`; while (1) { open (SFILE, "<$STATFILE") or die; $line = ; close(SFILE); @chunks = split(" ",$line); #0 is "CPU" string $idle = $chunks[4]; #Time in idle state if($lastidle == 0){ $lastidle = $idle; # and don't shift anything } else { $idif = $idle - $lastidle; # fraction of time idle 0 to 100 (more or less) $lastidle = $idle; if($idif >= $THRESH_UP){ $count_up -= 1; $count_down = $DELAY_OFF; if($count_up <= 0){ $count_up= 0; if($athcool_state == 0){ $discard_text= `$ATHCOOL on`; $athcool_state = 1; } } } elsif($idif <= $THRESH_DOWN){ $count_up = $DELAY_ON; $count_down -= 1; if($count_down <= 0){ $count_down = 0; if($athcool_state == 1){ $discard_text= `$ATHCOOL off`; $athcool_state = 0; } } } else { $count_up = $DELAY_ON; $count_down = $DELAY_OFF; } } sleep(1); } exit;