blib/lib/NBI/EcoScheduler.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 94.4% 34 / 36 Conditioncond 65.1% 28 / 43 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 82.5% 166 / 201 CC 38 SCAR 26.6
CC
38
Cov
88%
find_eco_begin
25.1
_windows_for_day
5.5
format_delay
5.0
CRAP 14.4
« lib/NBI/Manifest.pm ↑ Summary blib/lib/NBI/Job.pm »
1 package NBI::EcoScheduler;
2 #ABSTRACT: Find energy-efficient SLURM job start times
3 #
4 # NBI::EcoScheduler - Selects low-energy-price windows for SLURM jobs.
5 #
6 # DESCRIPTION:
7 # Given a job's walltime (hours) and an optional config hash, returns the
8 # next available epoch timestamp that falls within a "cheap energy" window.
9 # Three tiers of slots are considered:
10 # Tier 1 - job fits entirely inside an eco window AND avoids peak hours
11 # Tier 2 - job starts in an eco window, avoids peak hours, but overruns the window
12 # Tier 3 - job starts in an eco window but overlaps peak hours (fallback)
13 #
14 # Public functions:
15 # - find_eco_begin($duration_h, $config, $now) -> ($epoch, $tier) or (undef,undef)
16 # - epoch_to_slurm($epoch) -> "YYYY-MM-DDTHH:MM:SS"
17 # - format_delay($epoch, $now) -> "2h 05m" / "now"
18 #
19 # Private helpers:
20 # - _windows_for_day($midnight, $dow, $cfg) -> list of [start,end] epochs
21 # - _avoid_for_day($midnight, $cfg) -> list of [start,end] epochs
22 # - _parse_window_string($str, $midnight) -> list of [start,end] epochs
23 # - _job_overlaps_avoid($start, $dur_h, \@avoid) -> bool
24 #
25 # RELATIONSHIPS:
26 # - Called by bin/runjob when eco mode is active.
27 # - Results fed into NBI::Opts->{start_date} / NBI::Opts->{start_time}.
28 # - Config hash comes from NBI::Slurm::load_config().
29 #
30  
31 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use POSIX qw(mktime strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 6 ▶$NBI::EcoScheduler::VERSION = $NBI::Slurm::VERSION // '0.17.0';
Condition100%2/2
$NBI::Slurm::VERSION // "0.17.0"
l!l
51
MC/DC100%1/1
$NBI::Slurm::VERSION // "0.17.0"
$NBI::Slurm::VERSION
Truth table100%2/2
A
A= $NBI::Slurm::VERSION
Aresult
11
00
37  
38 # ---------------------------------------------------------------------------
39 # Hardcoded defaults - used when config keys are absent.
40 # Mon-Fri: 00:00-06:00 (night window before peak morning commute)
41 # Sat-Sun: 00:00-07:00 and 11:00-16:00 (weekend has longer cheap windows)
42 # Avoid every day: 17:00-20:00 (evening peak)
43 # ---------------------------------------------------------------------------
44 6 our %DEFAULTS = (
45 eco_windows_weekday => '00:00-06:00',
46 eco_windows_weekend => '00:00-07:00,11:00-16:00',
47 eco_avoid => '17:00-20:00',
48 eco_lookahead_days => 3,
49 eco_default => 1,
50 );
51  
52 # ---------------------------------------------------------------------------
53 # Public API
54 # ---------------------------------------------------------------------------
55  
56 10 ▶sub find_eco_begin {
Subroutine find_eco_begin: called CC=24 SCAR=32.2
Pod documented
57 # Find the next eco-friendly start epoch for a job of $duration_h hours.
58 #
59 # Parameters:
60 # $duration_h - job walltime in hours (default: 1)
61 # $config - hashref from load_config(), may be empty or undef
62 # $now - current epoch (defaults to time(); injectable for testing)
63 #
64 # Returns: ($begin_epoch, $tier) or (undef, undef) if nothing found.
65 # $tier 1 = perfect: fits in window, avoids peak
66 # $tier 2 = acceptable: avoids peak but overruns eco window
67 # $tier 3 = fallback: starts in eco window but overlaps peak hours
68 10 my ($duration_h, $config, $now) = @_;
69 10 ▶ $duration_h //= 1;
Condition50%1/2
$duration_h //= 1
l!l
100
MC/DC0%0/1
$duration_h //= 1
$duration_h
Truth table50%1/2
A
A= $duration_h
Aresult
11
00
70 10 ▶ $config //= {};
Condition50%1/2
$config //= {}
l!l
100
MC/DC0%0/1
$config //= {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
71 10 ▶ $now //= time();
Condition100%2/2
$now //= time
l!l
91
MC/DC100%1/1
$now //= time
$now
Truth table100%2/2
A
A= $now
Aresult
11
00
72  
73 # Caller config overrides defaults
74 10 my %cfg = (%DEFAULTS, %$config);
75 10 my $lookahead = int($cfg{eco_lookahead_days});
76  
77 10 my ($best_epoch, $best_tier) = (undef, undef);
78 10 my @now_tm = localtime($now);
79  
80 10 for my $day_offset (0 .. $lookahead) {
81 18 my $day_midnight = mktime(0, 0, 0,
82 $now_tm[3] + $day_offset, $now_tm[4], $now_tm[5]);
83  
84 18 my $dow = (localtime($day_midnight))[6]; # 0=Sun … 6=Sat
85 18 my @eco = _windows_for_day($day_midnight, $dow, \%cfg);
86 18 my @avoid = _avoid_for_day($day_midnight, \%cfg);
87  
88 18 my ($day_t1, $day_t2, $day_t3);
89  
90 18 for my $window (@eco) {
91 22 my ($w_start, $w_end) = @$window;
92  
93 # Earliest possible candidate: 1-minute safety buffer from "now"
94 22 ▶ my $candidate = ($w_start > $now + 60) ? $w_start : $now + 60;
Branch: 1 branch
BranchTrueFalseTotal
$w_start > $now + 60 ? : 10 12 22
95 22 ▶ next if $candidate >= $w_end; # window already past
Branch: 1 branch
BranchTrueFalseTotal
if $candidate >= $w_end 9 13 22
96  
97 13 my $fits = ($candidate + $duration_h * 3600) <= $w_end;
98 13 my $overlaps = _job_overlaps_avoid($candidate, $duration_h, \@avoid);
99  
100 13 ▶ if ($fits && !$overlaps) { $day_t1 //= $candidate }
Branch: 2 branches
BranchTrueFalseTotal
if ($fits and not $overlaps) { } 8 5 13
elsif (not $overlaps) { } 2 3 5
Condition66%4/6
$fits and not $overlaps
!ll&&!rl&&r
508
$day_t1 //= $candidate
l!l&&r!l&&!r
260
MC/DC50%1/2
$fits and not $overlaps
$fits not $overlaps
MC/DC0%0/2
$day_t1 //= $candidate
$day_t1 $candidate
Truth table66%2/3
A and not(B)
A= $fitsB= not $overlaps
ABresult
0X0
100
111
Truth table66%2/3
A //= B
A= $day_t1B= $candidate
ABresult
1X1
011
000
101 2 ▶ elsif (!$overlaps) { $day_t2 //= $candidate }
Condition33%1/3
$day_t2 //= $candidate
l!l&&r!l&&!r
020
MC/DC0%0/2
$day_t2 //= $candidate
$day_t2 $candidate
Truth table33%1/3
A //= B
A= $day_t2B= $candidate
ABresult
1X1
011
000
102 3 ▶ else { $day_t3 //= $candidate }
Condition33%1/3
$day_t3 //= $candidate
l!l&&r!l&&!r
030
MC/DC0%0/2
$day_t3 //= $candidate
$day_t3 $candidate
Truth table33%1/3
A //= B
A= $day_t3B= $candidate
ABresult
1X1
011
000
103 }
104  
105 18 ▶ my $day_best = $day_t1 // $day_t2 // $day_t3;
Condition100%6/6
($day_t1 // $day_t2) // $day_t3
l!l&&r!l&&!r
837
$day_t1 // $day_t2
l!l&&r!l&&!r
6210
MC/DC100%3/3
($day_t1 // $day_t2) // $day_t3
$day_t1 $day_t2 $day_t3
Truth table100%4/4
A // B // C
A= $day_t1B= $day_t2C= $day_t3
ABCresult
1XX1
01X1
0011
0000
106 18 ▶ my $day_tier = defined $day_t1 ? 1
Branch: 3 branches
BranchTrueFalseTotal
defined $day_t1 ? : 6 12 18
defined $day_t2 ? : 2 10 12
defined $day_t3 ? : 3 7 10
107 : defined $day_t2 ? 2
108 : defined $day_t3 ? 3
109 : undef;
110 18 ▶ next unless defined $day_best;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $day_best 7 11 18
111  
112 # First candidate found - record it
113 11 ▶ if (!defined $best_epoch) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $best_epoch) { } 9 2 11
elsif ($day_tier < $best_tier) { } 0 2 2
114 9 ($best_epoch, $best_tier) = ($day_best, $day_tier);
115 } elsif ($day_tier < $best_tier) {
116 # A later day offers a better tier - upgrade
117 0 ($best_epoch, $best_tier) = ($day_best, $day_tier);
118 }
119  
120 # Prefer starting sooner over waiting for a "perfect" distant slot:
121 # once we have a T1 or T2 (avoids peak hours) stop scanning -
122 # a slightly imperfect slot today beats a perfect slot in 3 days.
123 11 ▶ last if $best_tier <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $best_tier <= 2 8 3 11
124 }
125  
126 10 ▶ return ($best_epoch, $best_tier) if defined $best_epoch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $best_epoch 9 1 10
127 1 return (undef, undef);
128 }
129  
130 7 ▶sub epoch_to_slurm {
Subroutine epoch_to_slurm: called CC=1 SCAR=0.0
Pod documented
131 # Convert a Unix epoch to the SLURM --begin format "YYYY-MM-DDTHH:MM:SS".
132 7 my $epoch = shift;
133 7 return strftime("%Y-%m-%dT%H:%M:%S", localtime($epoch));
134 }
135  
136 6 ▶sub format_delay {
Subroutine format_delay: called CC=5 SCAR=16.1
Pod documented
137 # Return a human-readable string describing how far in the future $begin_epoch is.
138 # e.g. "now", "45m", "6h 05m", "1d 2h 30m"
139 6 my ($begin_epoch, $now) = @_;
140 6 ▶ $now //= time();
Condition100%2/2
$now //= time
l!l
51
MC/DC100%1/1
$now //= time
$now
Truth table100%2/2
A
A= $now
Aresult
11
00
141 6 my $secs = $begin_epoch - $now;
142 6 ▶ return "now" if $secs <= 60;
Branch: 1 branch
BranchTrueFalseTotal
if $secs <= 60 2 4 6
143 4 my $days = int($secs / 86400);
144 4 my $hours = int(($secs % 86400) / 3600);
145 4 my $mins = int(($secs % 3600) / 60);
146 4 ▶ return sprintf("%dd %dh %02dm", $days, $hours, $mins) if $days;
Branch: 1 branch
BranchTrueFalseTotal
if $days 1 3 4
147 3 ▶ return sprintf("%dh %02dm", $hours, $mins) if $hours;
Branch: 1 branch
BranchTrueFalseTotal
if $hours 2 1 3
148 1 return sprintf("%dm", $mins);
149 }
150  
151 # ---------------------------------------------------------------------------
152 # Private helpers
153 # ---------------------------------------------------------------------------
154  
155 18 ▶sub _windows_for_day {
Subroutine _windows_for_day: called CC=5 SCAR=17.0
156 # Return eco windows for a given day midnight + day-of-week.
157 18 my ($day_midnight, $dow, $cfg) = @_;
158 18 ▶ my $is_weekend = ($dow == 0 || $dow == 6);
Condition100%3/3
$dow == 0 || $dow == 6
l!l&&r!l&&!r
2214
MC/DC100%2/2
$dow == 0 || $dow == 6
$dow == 0 $dow == 6
Truth table100%3/3
A || B
A= $dow == 0B= $dow == 6
ABresult
1X1
011
000
159 my $str = $is_weekend
160 ? ($cfg->{eco_windows_weekend} // $DEFAULTS{eco_windows_weekend})
161 18 ▶ : ($cfg->{eco_windows_weekday} // $DEFAULTS{eco_windows_weekday});
Branch: 1 branch
BranchTrueFalseTotal
$is_weekend ? : 4 14 18
Condition33%2/6
$cfg->{"eco_windows_weekend"} // $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekend"}
l!l&&r!l&&!r
400
$cfg->{"eco_windows_weekday"} // $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekday"}
l!l&&r!l&&!r
1400
MC/DC0%0/2
$cfg->{"eco_windows_weekend"} // $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekend"}
$cfg->{"eco_windows_weekend"} $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekend"}
MC/DC0%0/2
$cfg->{"eco_windows_weekday"} // $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekday"}
$cfg->{"eco_windows_weekday"} $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekday"}
Truth table33%1/3
A // B
A= $cfg->{"eco_windows_weekend"}B= $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekend"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $cfg->{"eco_windows_weekday"}B= $NBI::EcoScheduler::DEFAULTS{"eco_windows_weekday"}
ABresult
1X1
011
000
162 18 return _parse_window_string($str, $day_midnight);
163 }
164  
165 18 ▶sub _avoid_for_day {
Subroutine _avoid_for_day: called CC=2 SCAR=7.6
166 # Return avoid windows for a given day midnight.
167 18 my ($day_midnight, $cfg) = @_;
168 18 ▶ my $str = $cfg->{eco_avoid} // $DEFAULTS{eco_avoid};
Condition33%1/3
$cfg->{"eco_avoid"} // $NBI::EcoScheduler::DEFAULTS{"eco_avoid"}
l!l&&r!l&&!r
1800
MC/DC0%0/2
$cfg->{"eco_avoid"} // $NBI::EcoScheduler::DEFAULTS{"eco_avoid"}
$cfg->{"eco_avoid"} $NBI::EcoScheduler::DEFAULTS{"eco_avoid"}
Truth table33%1/3
A // B
A= $cfg->{"eco_avoid"}B= $NBI::EcoScheduler::DEFAULTS{"eco_avoid"}
ABresult
1X1
011
000
169 18 return _parse_window_string($str, $day_midnight);
170 }
171  
172 39 ▶sub _parse_window_string {
Subroutine _parse_window_string: called CC=3 SCAR=11.2
173 # Parse "HH:MM-HH:MM,HH:MM-HH:MM,..." anchored to $day_midnight.
174 # Returns a list of [start_epoch, end_epoch] pairs.
175 39 my ($str, $day_midnight) = @_;
176 39 my @windows;
177 39 for my $part (split /,/, $str) {
178 44 $part =~ s/^\s+|\s+$//g;
179 44 ▶ if ($part =~ /^(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})$/) { } 44 0 44
180 44 my ($sh, $sm, $eh, $em) = ($1, $2, $3, $4);
181 44 push @windows, [
182 $day_midnight + $sh * 3600 + $sm * 60,
183 $day_midnight + $eh * 3600 + $em * 60,
184 ];
185 } else {
186 0 carp "NBI::EcoScheduler: cannot parse window spec '$part', skipping\n";
187 }
188 }
189 39 return @windows;
190 }
191  
192 17 ▶sub _job_overlaps_avoid {
Subroutine _job_overlaps_avoid: called CC=4 SCAR=13.9
193 # Return 1 if a job starting at $start_epoch running for $duration_h hours
194 # overlaps any of the avoid windows.
195 17 my ($start, $duration_h, $avoid_windows) = @_;
196 17 my $end = $start + $duration_h * 3600;
197 17 for my $w (@$avoid_windows) {
198 17 my ($a, $b) = @$w;
199 17 ▶ return 1 if ($start < $b && $end > $a);
Branch: 1 branch
BranchTrueFalseTotal
if $start < $b and $end > $a 5 12 17
Condition66%2/3
$start < $b and $end > $a
!ll&&!rl&&r
0125
MC/DC50%1/2
$start < $b and $end > $a
$start < $b $end > $a
Truth table66%2/3
A and B
A= $start < $bB= $end > $a
ABresult
0X0
100
111
200 }
201 12 return 0;
202 }
203  
204 6 1;
205  
206 __END__
« lib/NBI/Manifest.pm ↑ Summary blib/lib/NBI/Job.pm »