#!/usr/bin/perl -w # acpuf.pl # Simple ACPI CPUfreq changer, requires 2.6 kernel with working cpufreq. # # Author: Jakub Jankowski # # Copyright (c) 2004 Jakub Jankowski All rights reserved. # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # use strict; use vars qw($rcsid $VERSION); $rcsid = '$Id: acpuf.pl,v 1.3 2004/10/22 10:47:06 shasta Exp $'; ($VERSION) = $rcsid =~ /,v (\d+\.\d+) /; #---------------------------------------------------------------------- # Configure section: # # file containing AC adapter's state # (required format: 'state: on-line', or 'state: off-line') my $acfile = '/proc/acpi/ac_adapter/AC0/state'; # where to echo governor change my $govfile = '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor'; # which governor to use when AC adapter is connected # (performance, ondemand, powersave) my $online_governor = "performance"; # which governor to use when AC adapter is disconnected # (performance, ondemand, powersave) my $offline_governor = "ondemand"; # interval between checks (in seconds) my $interval = 5; # sleep for that many seconds on error my $errinterval = 20; # whether to print debug messages (1) or not (0) my $debug = 0; #---------------------------------------------------------------------- # no need to edit anything below # my $current_governor = ""; $ENV{'LD_PRELOAD'} = ""; $ENV{'LD_LIBRARY_PATH'} = ""; $ENV{'PATH'} = "/usr/bin:/bin:/usr/sbin:/sbin"; #---------------------------------------------------------------------- # # main() sub debug($) { print($_[0]) if ($debug); } sub change_governor($) { local *G; return if ($current_governor eq $_[0]); if (!defined open(G, ">$govfile")) { # error opening $govfile, wait a bit longer debug("Error opening $govfile for writing! sleeping for $errinterval seconds\n"); sleep($errinterval); return; } debug("echoing $_[0] to $govfile\n"); print G "$_[0]\n"; close(G); $current_governor = $_[0]; return; } print("acpuf.pl v" . $VERSION . " starting...\n"); while(1) { my $found = 0; local *F; if (!defined open(F, "<$acfile")) { # error opening $file, wait a bit longer debug("Error opening $acfile, sleeping for $errinterval seconds\n"); sleep($errinterval); } else { my @fcontent = ; close(F); $found = 0; foreach my $line (@fcontent) { if ($line =~ /^state:\W+(.*)/) { debug("Found out state: $1\n"); if ($1 eq 'on-line') { debug("We're online, let's get some power\n"); $found = 1; change_governor($online_governor); last; } else { debug("We're not online, let's save the energy\n"); $found = 1; change_governor($offline_governor); last; } } } if (!$found) { debug("Something is broken inside $acfile, sleeping for $errinterval seconds\n"); sleep($errinterval); } else { # normal behaviour sleep($interval); } } } 1;