« lib/Schedule/Chronic/Constraint/DiskIO.pm
↑ Summary
blib/lib/Schedule/Chronic/Constraint/Xscreensaver.pm »
| 1 | ## | ||||||||||||
| 2 | ## Load Average Constraint | ||||||||||||
| 3 | ## Author: Vipul Ved Prakash <mail@vipul.net>. | ||||||||||||
| 4 | ## $Id: Loadavg.pm,v 1.6 2004/08/15 21:03:35 hackworth Exp $ | ||||||||||||
| 5 | ## | ||||||||||||
| 6 | |||||||||||||
| 7 | package Schedule::Chronic::Constraint::Loadavg; | ||||||||||||
| 8 | 1 | ▶ | use Schedule::Chronic::Base; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Schedule::Chronic::Timer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use base qw(Schedule::Chronic::Base); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 14 | |||||||||||||
| 15 | 0 | my ($class) = @_; | |||||||||||
| 16 | |||||||||||||
| 17 | 0 | return bless { | |||||||||||
| 18 | active => 60, | ||||||||||||
| 19 | load_avg => 0.00, | ||||||||||||
| 20 | timer => new Schedule::Chronic::Timer ('down'), | ||||||||||||
| 21 | }, $class; | ||||||||||||
| 22 | |||||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | |||||||||||||
| 26 | 0 | ▶ | sub init { | ||||||||||
|
Subroutine
init: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 27 | |||||||||||||
| 28 | 0 | my ($self, $schedule, $task, $logger, $active, $load_avg) = @_; | |||||||||||
| 29 | 0 | ▶ | return unless ref $self; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | 0 | $$self{schedule} = $schedule; | |||||||||||
| 32 | 0 | $$self{task} = $task; | |||||||||||
| 33 | 0 | ▶ | $$self{active} = $active if $active; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 0 | ▶ | $$self{load_avg} = $load_avg if $load_avg; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | $$self{logger} = $logger; | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | return $self; | |||||||||||
| 38 | |||||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | |||||||||||||
| 42 | 0 | ▶ | sub met { | ||||||||||
|
Subroutine
met: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 43 | |||||||||||||
| 44 | 0 | my ($self) = @_; | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | my $load_avg = $self->state(); | |||||||||||
| 47 | |||||||||||||
| 48 | 0 | $self->debug(" load average = $load_avg ($$self{load_avg})"); | |||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | if ($load_avg <= $$self{load_avg}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | $$self{timer}->set($$self{active}) unless | ||||||||||||
| 53 | 0 | ▶ | $$self{timer}->running(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | if ($$self{timer}->get() <= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | return 1; | |||||||||||
| 57 | } else { | ||||||||||||
| 58 | 0 | return 0; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 0 | $$self{timer}->stop(); | |||||||||||
| 64 | |||||||||||||
| 65 | # Compute wait as a factor of the current load_avg and the required | ||||||||||||
| 66 | # load_avg. The wait should be bound between 5 and 120 seconds. | ||||||||||||
| 67 | # There ought to be a better algorithm to derive the wait... FIX. | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | my $divisor = $$self{load_avg} == 0 ? 0.01 : $$self{load_avg}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | my $wait = int($load_avg/$divisor); | |||||||||||
| 71 | 0 | ▶ | $wait = 5 if $wait < 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | ▶ | $wait = 120 if $wait > 120; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | |||||||||||||
| 74 | 0 | $$self{wait} = $wait; | |||||||||||
| 75 | |||||||||||||
| 76 | 0 | return 0; | |||||||||||
| 77 | |||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub state { | ||||||||||
|
Subroutine
state: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 82 | |||||||||||||
| 83 | 0 | my ($self) = @_; | |||||||||||
| 84 | 0 | ▶ | open LOADAVG, "/proc/loadavg" or die $!; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | my $load_avg = <LOADAVG>; | |||||||||||
| 86 | 0 | close LOADAVG; | |||||||||||
| 87 | |||||||||||||
| 88 | 0 | my @undef; | |||||||||||
| 89 | 0 | ($load_avg, @undef) = split /\s/, $load_avg; | |||||||||||
| 90 | 0 | return $load_avg; | |||||||||||
| 91 | |||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | sub wait { | ||||||||||
|
Subroutine
wait: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 96 | |||||||||||||
| 97 | 0 | ▶ | return $_[0]->{wait} || 0; | ||||||||||
|
Condition0%0/2
$_[0]{'wait'} || 0
MC/DC0%0/1
$_[0]{'wait'} || 0
$_[0]{'wait'}
Truth table0%0/2
A
A= $_[0]{'wait'}
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | |||||||||||||
| 101 | 1 | 1; | |||||||||||
| 102 | |||||||||||||