-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutionProfile.pm
396 lines (303 loc) · 9.57 KB
/
ExecutionProfile.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package ExecutionProfile;
use parent 'Displayable';
use strict;
use warnings;
use List::Util qw(min max);
use Data::Dumper;
use Carp;
use Profile;
use BinarySearchTree;
use Platform;
use ProcessorRange;
use overload '""' => \&stringification;
sub new {
my ($class, $processors_number, $reduction_algorithm) = @_;
my $self = {
processors_number => $processors_number,
reduction_algorithm => $reduction_algorithm,
profile_tree => BinarySearchTree->new(-1, 0),
};
$self->{profile_tree}->add_content(Profile->new(0, undef, [0, $self->{processors_number} - 1]));
bless $self, $class;
return $self;
}
sub get_free_processors {
my ($self, $job, $starting_time) = @_;
my $duration = 0;
my $left_processors = ProcessorRange->new(0, $self->{processors_number} - 1);
my $requested_time = $job->requested_time();
$self->{profile_tree}->nodes_loop($starting_time, undef,
sub {
my ($profile) = @_;
# stop if we have enough profiles
return 0 if $duration >= $requested_time;
# profiles must all be contiguous
return 0 unless $profile->starting_time() == $starting_time + $duration;
$left_processors->intersection($profile->processors());
return 0 if $left_processors->size() < $job->requested_cpus();
$duration = (defined $profile->ending_time()) ? $duration + $profile->duration() : $requested_time;
return 1;
});
# check if the nodes_loop section found all needed CPUs
if ($left_processors->size() < $job->requested_cpus() or $duration < $requested_time) {
$left_processors->free_allocated_memory();
return;
}
$self->{reduction_algorithm}->reduce($job, $left_processors);
unless ($left_processors->size()) {
$left_processors->free_allocated_memory();
return;
}
return $left_processors;
}
sub available_processors {
my ($self, $starting_time) = @_;
my @available_processors;
my $profile = $self->{profile_tree}->find_content($starting_time);
@available_processors = $profile->processors()->processors_ids() if defined $profile;
return @available_processors;
}
sub remove_job {
my ($self, $job, $current_time) = @_;
my $starting_time = $job->starting_time();
my $job_ending_time = $job->submitted_ending_time();
my @impacted_profiles;
Profile::set_comparison_function('all_times');
$self->{profile_tree}->nodes_loop($starting_time, $job_ending_time,
sub {
my ($profile) = @_;
push @impacted_profiles, $profile;
return 1;
}
);
Profile::set_comparison_function('default');
unless (@impacted_profiles) {
# avoid starting in the past
my $start = max($current_time, $starting_time);
# only remove if it is still there
if ($job_ending_time > $start) {
my $new_profile = Profile->new(
$start,
$job_ending_time,
$job->assigned_processors()->copy_range()
);
$self->{profile_tree}->add_content($new_profile);
}
return;
}
# split at the first profile
if ($impacted_profiles[0]->starting_time() < $starting_time) {
# remove
my $first_profile = shift @impacted_profiles;
$self->{profile_tree}->remove_content($first_profile);
# split in two
my $first_profile_ending_time = $first_profile->ending_time();
$first_profile->ending_time($starting_time);
my $second_profile = Profile->new(
$starting_time,
$first_profile_ending_time,
$first_profile->processors()->copy_range()
);
# put back
$self->{profile_tree}->add_content($first_profile);
$self->{profile_tree}->add_content($second_profile);
unshift @impacted_profiles, $second_profile;
}
# split at the last profile
if ($impacted_profiles[-1]->ends_after($job_ending_time)) {
# remove
my $first_profile = pop @impacted_profiles;
$self->{profile_tree}->remove_content($first_profile);
# split in two
my $second_profile = Profile->new(
$job_ending_time,
$first_profile->ending_time(),
$first_profile->processors()->copy_range()
);
$first_profile->ending_time($job_ending_time);
# put back
$self->{profile_tree}->add_content($first_profile);
$self->{profile_tree}->add_content($second_profile);
push @impacted_profiles, $first_profile;
}
# update profiles
my $previous_profile_ending_time = max($starting_time, $current_time);
for my $profile (@impacted_profiles) {
$profile->remove_job($job);
if ($profile->starting_time() > $previous_profile_ending_time) {
my $new_profile = Profile->new(
$previous_profile_ending_time,
$profile->starting_time(),
$job->assigned_processors()->copy_range()
);
$self->{profile_tree}->add_content($new_profile);
}
$previous_profile_ending_time = $profile->ending_time();
}
# gap at the end
if ($job_ending_time > $previous_profile_ending_time) {
my $new_profile = Profile->new(
$previous_profile_ending_time,
$job_ending_time,
$job->assigned_processors()->copy_range()
);
$self->{profile_tree}->add_content($new_profile);
}
return;
}
sub add_job {
my ($self, $starting_time, $job) = @_;
my @profiles_to_update;
$self->{profile_tree}->nodes_loop($starting_time, undef,
sub {
my ($profile) = @_;
return 0 if $profile->starting_time() >= $starting_time + $job->requested_time();
push @profiles_to_update, $profile;
return 1;
});
for my $profile (@profiles_to_update) {
$self->{profile_tree}->remove_content($profile);
$self->{profile_tree}->add_content($_) for $profile->add_job($job);
$profile->processors()->free_allocated_memory();
}
return;
}
sub could_start_job {
my ($self, $job, $starting_time) = @_;
my $enough_processors = 1;
my $included_time = 0;
$self->{profile_tree}->nodes_loop($starting_time, undef,
sub {
my ($profile) = @_;
# gap in the profile, can't use it to run the job
return 0 if $starting_time != $profile->starting_time();
# not enough cpus
if ($profile->processors()->size() < $job->requested_cpus()) {
$enough_processors = 0;
return 0;
}
# ok to return if it's the last profile
unless (defined $profile->duration()) {
$included_time = $job->requested_time();
return 0;
}
$included_time += $profile->duration();
# ok to return, profile may be good for the job
return 0 if $starting_time >= $job->requested_time();
return 1;
});
return ($enough_processors and $included_time >= $job->requested_time());
}
sub find_first_profile {
my ($self, $job) = @_;
# used to return the results
my $starting_time;
my $processors;
# used to keep track of the starting times
my @included_profiles;
my $previous_ending_time;
$self->{profile_tree}->nodes_loop(undef, undef,
sub {
my ($profile) = @_;
# not enough processors
if ($profile->processors()->size() < $job->requested_cpus()) {
@included_profiles = ();
return 1;
}
# gap in the list of profiles
@included_profiles = () if defined $previous_ending_time and
$previous_ending_time != $profile->starting_time();
push @included_profiles, $profile;
while (@included_profiles and (not defined $included_profiles[-1]->ending_time() or
$included_profiles[-1]->ending_time() - $included_profiles[0]->starting_time() > $job->requested_time())) {
my $start_profile = shift @included_profiles;
$starting_time = $start_profile->starting_time();
$processors = $self->get_free_processors($job, $start_profile->starting_time());
return 0 if defined $processors;
}
$previous_ending_time = $profile->ending_time();
return 1;
});
return ($starting_time, $processors) if defined $processors;
return;
}
sub set_current_time {
my ($self, $current_time) = @_;
my $updated_profile;
my @removed_profiles;
$self->{profile_tree}->nodes_loop(undef, undef,
sub {
my ($profile) = @_;
return 0 if $profile->starting_time() == $current_time;
if (not defined $profile->ending_time() or $profile->ending_time() > $current_time) {
$updated_profile = $profile;
return 0;
}
push @removed_profiles, $profile;
return 1;
});
if (defined $updated_profile) {
$self->{profile_tree}->remove_content($updated_profile);
$updated_profile->starting_time($current_time);
$self->{profile_tree}->add_content($updated_profile);
}
for my $profile (@removed_profiles) {
$self->{profile_tree}->remove_content($profile);
$profile->processors()->free_allocated_memory();
}
return;
}
sub free_profiles {
my ($self) = @_;
my @profiles;
$self->{profile_tree}->nodes_loop(undef, undef,
sub {
my ($profile) = @_;
push @profiles, $profile;
return 1;
}
);
for my $profile (@profiles) {
$self->{profile_tree}->remove_content($profile);
$profile->processors()->free_allocated_memory();
}
return;
}
sub stringification {
my ($self) = @_;
my @profiles;
$self->{profile_tree}->nodes_loop(undef, undef,
sub {
my ($profile) = @_;
push @profiles, $profile;
return 1;
});
return join("\n", @profiles);
}
sub save_svg {
my ($self, $svg_filename, $time) = @_;
$time = 0 unless defined $time;
my @profiles;
$self->{profile_tree}->nodes_loop(undef, undef,
sub {
my ($profile) = @_;
push @profiles, $profile;
return 1;
});
my $last_starting_time = $profiles[-1]->starting_time();
return unless $last_starting_time;
open(my $filehandle, '>', "$svg_filename") or die "unable to open $svg_filename";
print $filehandle "<svg width=\"800\" height=\"600\">\n";
my $w_ratio = 800/$last_starting_time;
my $h_ratio = 600/$self->{processors_number};
# red line at the current time
my $current_x = $w_ratio * $time;
print $filehandle "<line x1=\"$current_x\" x2=\"$current_x\" y1=\"0\" y2=\"600\" style=\"stroke:rgb(255,0,0);stroke-width:5\"/>\n";
for my $profile_index (0..($#profiles - 1)) {
$profiles[$profile_index]->svg($filehandle, $w_ratio, $h_ratio, $time, $profile_index);
}
print $filehandle "</svg>\n";
close $filehandle;
return;
}
1;