line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## |
2
|
|
|
|
|
|
|
## Load Average Constraint |
3
|
|
|
|
|
|
|
## Author: Vipul Ved Prakash . |
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
|
|
|
1
|
|
572
|
use Schedule::Chronic::Base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
9
|
1
|
|
|
1
|
|
5
|
use Schedule::Chronic::Timer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Schedule::Chronic::Base); |
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
556
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
sub init { |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
0
|
0
|
|
my ($self, $schedule, $task, $logger, $active, $load_avg) = @_; |
29
|
0
|
0
|
|
|
|
|
return unless ref $self; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
$$self{schedule} = $schedule; |
32
|
0
|
|
|
|
|
|
$$self{task} = $task; |
33
|
0
|
0
|
|
|
|
|
$$self{active} = $active if $active; |
34
|
0
|
0
|
|
|
|
|
$$self{load_avg} = $load_avg if $load_avg; |
35
|
0
|
|
|
|
|
|
$$self{logger} = $logger; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
return $self; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub met { |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
0
|
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
|
0
|
|
|
|
|
if ($load_avg <= $$self{load_avg}) { |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
$$self{timer}->set($$self{active}) unless |
53
|
|
|
|
|
|
|
$$self{timer}->running(); |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
if ($$self{timer}->get() <= 0) { |
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
|
0
|
|
|
|
|
my $divisor = $$self{load_avg} == 0 ? 0.01 : $$self{load_avg}; |
70
|
0
|
|
|
|
|
|
my $wait = int($load_avg/$divisor); |
71
|
0
|
0
|
|
|
|
|
$wait = 5 if $wait < 5; |
72
|
0
|
0
|
|
|
|
|
$wait = 120 if $wait > 120; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$$self{wait} = $wait; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return 0; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub state { |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
84
|
0
|
0
|
|
|
|
|
open LOADAVG, "/proc/loadavg" or die $!; |
85
|
0
|
|
|
|
|
|
my $load_avg = ; |
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
|
|
|
|
|
|
|
sub wait { |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
0
|
0
|
0
|
|
return $_[0]->{wait} || 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|