2000 年以后制造的所有 PC 都应当能够提供降低功耗的硬件和软件。您需要一个新 Linux 内核,并且它将有助于获得内置有许多节能工具的 Linux 发行版。不过,关闭屏幕或者触发自动关机就可以提供显著的节能优点。如果您拥有的硬件较旧或者不具有 ACPI 功能,应当仍然会发现本文提供的代码十分有用。
#!/usr/bin/perl -w
# focusTracker.pl - collect focus data for usage visualizations
use strict;
use X11::GUITest qw( :ALL ); # find application focus
use threads; # non blocking reads from xev
use Thread::Queue; # non blocking reads from xev
$SIG{INT} = \&printHeader; # print header file to stderr on exit
$|=1; # non-buffered output
my %log = (); # focus tracking data structure
my $lastId = ""; # last focused window id
my $pipe = ""; # non blocking event reads via xev
my _cnnew1_cnnew1@win = (); # binary activity data for maxWindows applications
my $cpu = ""; # cpu usage from iostat
my $mbread_s = ""; # disk read mb/s from iostat
my $totalWindow = 0; # total used windows
my $maxWindows = 50; # total tracked windows
while(my $line = <STDIN> )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
next if( $line =~ /Linux/ || length($line) < 10 ); # header line, empty line
my $windowId = GetInputFocus();
my $windowName = GetWindowName( $windowId ) || "NoWindowName";
if( ! exists($log{$windowId}) )
{
# if this is a new window, assign it to the next position in the data set
$log{ $windowId }{ order } = $totalWindow;
$log{ $windowId }{ name } = $windowName;
$totalWindow++ if( $totalWindow < $maxWindows );
}# if a newly tracked window
}else
{
# data on the pipe indicates activity
if( $pipe->pending )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
$win[ $log{$windowId}{order} ] = 1;
# clear the pipe
while( $pipe->pending ){ my $line = $pipe->dequeue or next }
}#if events detected for that window
}#if pipe settings
# CPU usage, disk reads, focus tracking data
print "$cpu $mbread_s @win \n";
}#if device line
}#while input
2000 年以后制造的所有 PC 都应当能够提供降低功耗的硬件和软件。您需要一个新 Linux 内核,并且它将有助于获得内置有许多节能工具的 Linux 发行版。不过,关闭屏幕或者触发自动关机就可以提供显著的节能优点。如果您拥有的硬件较旧或者不具有 ACPI 功能,应当仍然会发现本文提供的代码十分有用。
#!/usr/bin/perl -w
# focusTracker.pl - collect focus data for usage visualizations
use strict;
use X11::GUITest qw( :ALL ); # find application focus
use threads; # non blocking reads from xev
use Thread::Queue; # non blocking reads from xev
$SIG{INT} = \&printHeader; # print header file to stderr on exit
$|=1; # non-buffered output
my %log = (); # focus tracking data structure
my $lastId = ""; # last focused window id
my $pipe = ""; # non blocking event reads via xev
my _cnnew1_cnnew1@win = (); # binary activity data for maxWindows applications
my $cpu = ""; # cpu usage from iostat
my $mbread_s = ""; # disk read mb/s from iostat
my $totalWindow = 0; # total used windows
my $maxWindows = 50; # total tracked windows
while(my $line = <STDIN> )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
next if( $line =~ /Linux/ || length($line) < 10 ); # header line, empty line
my $windowId = GetInputFocus();
my $windowName = GetWindowName( $windowId ) || "NoWindowName";
if( ! exists($log{$windowId}) )
{
# if this is a new window, assign it to the next position in the data set
$log{ $windowId }{ order } = $totalWindow;
$log{ $windowId }{ name } = $windowName;
$totalWindow++ if( $totalWindow < $maxWindows );
}# if a newly tracked window
}else
{
# data on the pipe indicates activity
if( $pipe->pending )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
$win[ $log{$windowId}{order} ] = 1;
# clear the pipe
while( $pipe->pending ){ my $line = $pipe->dequeue or next }
}#if events detected for that window
}#if pipe settings
# CPU usage, disk reads, focus tracking data
print "$cpu $mbread_s @win \n";
}#if device line
}#while input
此外,考虑与 Eterm/vim 使用情况关联的低 CPU 及磁盘使用率。激活较低的功耗模式在这里是合乎逻辑的,因为在 vim 中输入文本只需少量的电量。让硬盘停转并且降低 CPU 速度可以在 CPU 及磁盘活动低于设定阈值的任何时间范围内执行。
inactivityRulesFile 设置
根据上面的说明,修改后的规则如下所示:
清单 8. 示例 inactivityRulesFile
# format is:
# start end CPU disk timeOut application command
1130_#_1230_#_null_#_null_#_10_#_firefox_#_xscreensaver-command -activate
0610_#_1910_#_6_#_2_#_30_#_vim_#_echo 6 > /proc/acpi/processor/CPU0/performance
注意,如果只希望检查用户活动,则可以将 CPU 和磁盘值指定为空。如果 CPU 和磁盘值已指定,那么它们将被设置为运行命令前必须达到的最低使用量值。timeOut 变量将指定检查 CPU 和磁盘最低值之前需要等待的用户处于不活动状态的秒数。应用程序变量可以在 X Window System 中将任意文本指定为应用程序标题。
}else
{
$app{ lastActivity }++;
print "no events for window $windowName last "
print "activity seconds $app{lastActivity} ago\n";
my $currTime = `date +%H%M`;
for my $ruleNum ( 0..$ruleCount-1)
{
next unless( $app{cmdRun} == 0 );
next unless( $windowName =~ /$rules[$ruleNum]{appName}/i );
next unless( $app{lastActivity} >= $rules[$ruleNum]{timeOut} );
next unless( $currTime >= $rules[$ruleNum]{start} &&
$currTime <= $rules[$ruleNum]{stop} );
my $conditions = 0;
$conditions++ if( $rules[$ruleNum]{cpu} eq "null" );
$conditions++ if( $rules[$ruleNum]{disk} eq "null" );
$conditions++ if( $rules[$ruleNum]{cpu} ne "null" &&
$rules[$ruleNum]{cpu} <= $cpu );
$conditions++ if( $rules[$ruleNum]{disk} ne "null" &&
$rules[$ruleNum]{disk} <= $mbread_s );
next unless( $conditions > 1 );
print "running $rules[$ruleNum]{cmd}\n";
$app{ cmdRun } = 1;
system( $rules[$ruleNum]{cmd} );
}#for each rule to process
}#if events detected for that window
}#if pipe settings
}#if device line
}#while input
从确保规则命令尚未处理的检查开始,将根据上面所示的列表处理每个规则。然后将执行名称匹配检查,其中诸如 “Controlling ACPI Centrino features / enhanced speedstep via software in Linux - Mozilla Firefox” 之类的窗口名称将匹配 “firefox” 的规则应用程序名称。接下来,必须达到全部不活动时间(以秒为单位),以及需要处理的命令的时间窗口。最后,如果满足 CPU 和磁盘条件,则运行该命令并且处理下一条规则。清单 13 显示了完成 monitorUsage.pl 程序所需的常见 createPipe 子例程。
清单 13. monitorUsage.pl createPipe 子例程
sub createPipe
{
my $cmd = shift;
my $queue = new Thread::Queue;
async{
my $pid = open my $pipe, $cmd or die $!;
$queue->enqueue(
了解如何通过监视应用程序使用模式和用户活动降低 Linux® 计算机中的功耗。
现代计算机中内置的高级电源配置管理界面(Advanced Configuration and Power Interface,ACPI)和电源配置系统为降低整体功耗提供了各种方法。Linux 及其相关用户空间程序配有大量可以在各种环境下控制 PC 功耗所需的工具。
2000 年以后制造的所有 PC 都应当能够提供降低功耗的硬件和软件。您需要一个新 Linux 内核,并且它将有助于获得内置有许多节能工具的 Linux 发行版。不过,关闭屏幕或者触发自动关机就可以提供显著的节能优点。如果您拥有的硬件较旧或者不具有 ACPI 功能,应当仍然会发现本文提供的代码十分有用。
#!/usr/bin/perl -w
# focusTracker.pl - collect focus data for usage visualizations
use strict;
use X11::GUITest qw( :ALL ); # find application focus
use threads; # non blocking reads from xev
use Thread::Queue; # non blocking reads from xev
$SIG{INT} = \&printHeader; # print header file to stderr on exit
$|=1; # non-buffered output
my %log = (); # focus tracking data structure
my $lastId = ""; # last focused window id
my $pipe = ""; # non blocking event reads via xev
my _cnnew1_cnnew1@win = (); # binary activity data for maxWindows applications
my $cpu = ""; # cpu usage from iostat
my $mbread_s = ""; # disk read mb/s from iostat
my $totalWindow = 0; # total used windows
my $maxWindows = 50; # total tracked windows
while(my $line = <STDIN> )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
next if( $line =~ /Linux/ || length($line) < 10 ); # header line, empty line
my $windowId = GetInputFocus();
my $windowName = GetWindowName( $windowId ) || "NoWindowName";
if( ! exists($log{$windowId}) )
{
# if this is a new window, assign it to the next position in the data set
$log{ $windowId }{ order } = $totalWindow;
$log{ $windowId }{ name } = $windowName;
$totalWindow++ if( $totalWindow < $maxWindows );
}# if a newly tracked window
}else
{
# data on the pipe indicates activity
if( $pipe->pending )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
$win[ $log{$windowId}{order} ] = 1;
# clear the pipe
while( $pipe->pending ){ my $line = $pipe->dequeue or next }
}#if events detected for that window
}#if pipe settings
# CPU usage, disk reads, focus tracking data
print "$cpu $mbread_s @win \n";
}#if device line
}#while input
2000 年以后制造的所有 PC 都应当能够提供降低功耗的硬件和软件。您需要一个新 Linux 内核,并且它将有助于获得内置有许多节能工具的 Linux 发行版。不过,关闭屏幕或者触发自动关机就可以提供显著的节能优点。如果您拥有的硬件较旧或者不具有 ACPI 功能,应当仍然会发现本文提供的代码十分有用。
#!/usr/bin/perl -w
# focusTracker.pl - collect focus data for usage visualizations
use strict;
use X11::GUITest qw( :ALL ); # find application focus
use threads; # non blocking reads from xev
use Thread::Queue; # non blocking reads from xev
$SIG{INT} = \&printHeader; # print header file to stderr on exit
$|=1; # non-buffered output
my %log = (); # focus tracking data structure
my $lastId = ""; # last focused window id
my $pipe = ""; # non blocking event reads via xev
my _cnnew1_cnnew1@win = (); # binary activity data for maxWindows applications
my $cpu = ""; # cpu usage from iostat
my $mbread_s = ""; # disk read mb/s from iostat
my $totalWindow = 0; # total used windows
my $maxWindows = 50; # total tracked windows
while(my $line = <STDIN> )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
next if( $line =~ /Linux/ || length($line) < 10 ); # header line, empty line
my $windowId = GetInputFocus();
my $windowName = GetWindowName( $windowId ) || "NoWindowName";
if( ! exists($log{$windowId}) )
{
# if this is a new window, assign it to the next position in the data set
$log{ $windowId }{ order } = $totalWindow;
$log{ $windowId }{ name } = $windowName;
$totalWindow++ if( $totalWindow < $maxWindows );
}# if a newly tracked window
}else
{
# data on the pipe indicates activity
if( $pipe->pending )
{
for my $c (0..$maxWindows){ $win[$c] = 0 } #initialize all data positions
$win[ $log{$windowId}{order} ] = 1;
# clear the pipe
while( $pipe->pending ){ my $line = $pipe->dequeue or next }
}#if events detected for that window
}#if pipe settings
# CPU usage, disk reads, focus tracking data
print "$cpu $mbread_s @win \n";
}#if device line
}#while input
此外,考虑与 Eterm/vim 使用情况关联的低 CPU 及磁盘使用率。激活较低的功耗模式在这里是合乎逻辑的,因为在 vim 中输入文本只需少量的电量。让硬盘停转并且降低 CPU 速度可以在 CPU 及磁盘活动低于设定阈值的任何时间范围内执行。
inactivityRulesFile 设置
根据上面的说明,修改后的规则如下所示:
清单 8. 示例 inactivityRulesFile
# format is:
# start end CPU disk timeOut application command
1130_#_1230_#_null_#_null_#_10_#_firefox_#_xscreensaver-command -activate
0610_#_1910_#_6_#_2_#_30_#_vim_#_echo 6 > /proc/acpi/processor/CPU0/performance
注意,如果只希望检查用户活动,则可以将 CPU 和磁盘值指定为空。如果 CPU 和磁盘值已指定,那么它们将被设置为运行命令前必须达到的最低使用量值。timeOut 变量将指定检查 CPU 和磁盘最低值之前需要等待的用户处于不活动状态的秒数。应用程序变量可以在 X Window System 中将任意文本指定为应用程序标题。
}else
{
$app{ lastActivity }++;
print "no events for window $windowName last "
print "activity seconds $app{lastActivity} ago\n";
my $currTime = `date +%H%M`;
for my $ruleNum ( 0..$ruleCount-1)
{
next unless( $app{cmdRun} == 0 );
next unless( $windowName =~ /$rules[$ruleNum]{appName}/i );
next unless( $app{lastActivity} >= $rules[$ruleNum]{timeOut} );
next unless( $currTime >= $rules[$ruleNum]{start} &&
$currTime <= $rules[$ruleNum]{stop} );
my $conditions = 0;
$conditions++ if( $rules[$ruleNum]{cpu} eq "null" );
$conditions++ if( $rules[$ruleNum]{disk} eq "null" );
$conditions++ if( $rules[$ruleNum]{cpu} ne "null" &&
$rules[$ruleNum]{cpu} <= $cpu );
$conditions++ if( $rules[$ruleNum]{disk} ne "null" &&
$rules[$ruleNum]{disk} <= $mbread_s );
next unless( $conditions > 1 );
print "running $rules[$ruleNum]{cmd}\n";
$app{ cmdRun } = 1;
system( $rules[$ruleNum]{cmd} );
}#for each rule to process
}#if events detected for that window
}#if pipe settings
}#if device line
}#while input
从确保规则命令尚未处理的检查开始,将根据上面所示的列表处理每个规则。然后将执行名称匹配检查,其中诸如 “Controlling ACPI Centrino features / enhanced speedstep via software in Linux - Mozilla Firefox” 之类的窗口名称将匹配 “firefox” 的规则应用程序名称。接下来,必须达到全部不活动时间(以秒为单位),以及需要处理的命令的时间窗口。最后,如果满足 CPU 和磁盘条件,则运行该命令并且处理下一条规则。清单 13 显示了完成 monitorUsage.pl 程序所需的常见 createPipe 子例程。
此外,考虑与 Eterm/vim 使用情况关联的低 CPU 及磁盘使用率。激活较低的功耗模式在这里是合乎逻辑的,因为在 vim 中输入文本只需少量的电量。让硬盘停转并且降低 CPU 速度可以在 CPU 及磁盘活动低于设定阈值的任何时间范围内执行。
inactivityRulesFile 设置
根据上面的说明,修改后的规则如下所示:
清单 8. 示例 inactivityRulesFile
___FCKpd___7
注意,如果只希望检查用户活动,则可以将 CPU 和磁盘值指定为空。如果 CPU 和磁盘值已指定,那么它们将被设置为运行命令前必须达到的最低使用量值。timeOut 变量将指定检查 CPU 和磁盘最低值之前需要等待的用户处于不活动状态的秒数。应用程序变量可以在 X Window System 中将任意文本指定为应用程序标题。
从确保规则命令尚未处理的检查开始,将根据上面所示的列表处理每个规则。然后将执行名称匹配检查,其中诸如 “Controlling ACPI Centrino features / enhanced speedstep via software in Linux - Mozilla Firefox” 之类的窗口名称将匹配 “firefox” 的规则应用程序名称。接下来,必须达到全部不活动时间(以秒为单位),以及需要处理的命令的时间窗口。最后,如果满足 CPU 和磁盘条件,则运行该命令并且处理下一条规则。清单 13 显示了完成 monitorUsage.pl 程序所需的常见 createPipe 子例程。
通过提供的工具和代码,您可以通过一系列有关应用程序使用情况的规则来降低功耗。在调整内核、hdparm、ACPI 及 CPU 设置后,添加这些应用程序监视器可以更有效地进入低电量状态。使用 focusTracker 和 kst 显示以查找不活动间隙,从而创建更环保的规则。(责任编辑:A6)
) while <$pipe>;
$queue->enqueue( undef );
}->detach;
# detach causes the threads to be silently terminated on program exit
return $queue;
}#createPipe
此外,考虑与 Eterm/vim 使用情况关联的低 CPU 及磁盘使用率。激活较低的功耗模式在这里是合乎逻辑的,因为在 vim 中输入文本只需少量的电量。让硬盘停转并且降低 CPU 速度可以在 CPU 及磁盘活动低于设定阈值的任何时间范围内执行。
inactivityRulesFile 设置
根据上面的说明,修改后的规则如下所示:
清单 8. 示例 inactivityRulesFile
___FCKpd___7
注意,如果只希望检查用户活动,则可以将 CPU 和磁盘值指定为空。如果 CPU 和磁盘值已指定,那么它们将被设置为运行命令前必须达到的最低使用量值。timeOut 变量将指定检查 CPU 和磁盘最低值之前需要等待的用户处于不活动状态的秒数。应用程序变量可以在 X Window System 中将任意文本指定为应用程序标题。
从确保规则命令尚未处理的检查开始,将根据上面所示的列表处理每个规则。然后将执行名称匹配检查,其中诸如 “Controlling ACPI Centrino features / enhanced speedstep via software in Linux - Mozilla Firefox” 之类的窗口名称将匹配 “firefox” 的规则应用程序名称。接下来,必须达到全部不活动时间(以秒为单位),以及需要处理的命令的时间窗口。最后,如果满足 CPU 和磁盘条件,则运行该命令并且处理下一条规则。清单 13 显示了完成 monitorUsage.pl 程序所需的常见 createPipe 子例程。