-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.pm
209 lines (148 loc) · 5.17 KB
/
Profile.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
package Profile;
use strict;
use warnings;
use POSIX;
use Scalar::Util qw(blessed);
use Data::Dumper qw(Dumper);
use Carp;
use List::Util qw(min);
use overload
'""' => \&stringification,
'<=>' => \&three_way_comparison,
'-' => \&subtract,
;
sub new {
my ($class, $starting_time, $ending_time, $ids) = @_;
my $self = {
starting_time => $starting_time,
ending_time => $ending_time,
};
die "invalid profile duration ($self->{ending_time} - $self->{starting_time}" if defined $self->{ending_time} and $self->{ending_time} <= $self->{starting_time};
$self->{processors} = (defined blessed($ids) and blessed($ids) eq 'ProcessorRange') ? $ids : ProcessorRange->new(@$ids);
bless $self, $class;
return $self;
}
sub stringification {
my ($self) = @_;
my $cpus_number = scalar $self->{processors}->processors_ids();
return "[$self->{starting_time} ; $cpus_number = ($self->{processors}) " . (defined $self->{ending_time} ? ": $self->{ending_time}]" : "]");
}
sub processors {
my ($self, $processors) = @_;
$self->{processors} = $processors if defined $processors;
return $self->{processors};
}
sub processors_ids {
my ($self) = @_;
return $self->{processors}->processors_ids();
}
sub duration {
my ($self) = @_;
return $self->{ending_time} - $self->{starting_time} if defined $self->{ending_time};
return;
}
sub ending_time {
my ($self, $ending_time) = @_;
$self->{ending_time} = $ending_time if defined $ending_time;
return $self->{ending_time};
}
sub add_job {
my ($self, $job) = @_;
return $self->split_by_job($job);
}
sub remove_job {
my ($self, $job) = @_;
$self->{processors}->add($job->assigned_processors());
return;
}
sub split_by_job {
my ($self, $job) = @_;
my @profiles;
my $middle_start = $self->{starting_time};
my $middle_end = (defined $self->{ending_time}) ? min($self->{ending_time}, $job->submitted_ending_time()) : $job->submitted_ending_time();
my $middle_profile = Profile->new($middle_start, $middle_end, $self->{processors}->copy_range());
$middle_profile->{processors}->remove($job->assigned_processors());
if ($middle_profile->{processors}->size()) {
push @profiles, $middle_profile
} else {
$middle_profile->processors()->free_allocated_memory();
}
if (not defined $self->{ending_time} or $job->submitted_ending_time() < $self->{ending_time}) {
my $end_profile = Profile->new($job->submitted_ending_time(), $self->{ending_time}, $self->{processors}->copy_range());
push @profiles, $end_profile;
}
return @profiles;
}
sub remove_processors {
my ($self, $job) = @_;
my $assigned_processors_ids = $job->assigned_processors_ids();
$self->{processors}->remove($assigned_processors_ids);
return;
}
sub starting_time {
my ($self, $starting_time) = @_;
$self->{starting_time} = $starting_time if defined $starting_time;
return $self->{starting_time};
}
sub ends_after {
my ($self, $time) = @_;
return 1 unless defined $self->{ending_time};
return $self->{ending_time} > $time;
}
sub svg {
my ($self, $fh, $w_ratio, $h_ratio, $current_time, $index) = @_;
my @svg_colors = qw(red green blue purple orange saddlebrown mediumseagreen darkolivegreen darkred dimgray mediumpurple midnightblue olive chartreuse darkorchid hotpink lightskyblue peru goldenrod mediumslateblue orangered darkmagenta darkgoldenrod mediumslateblue);
$self->{processors}->ranges_loop(
sub {
my ($start, $end) = @_;
#rectangle
my $x = $self->{starting_time} * $w_ratio;
my $w = $self->duration() * $w_ratio;
my $y = $start * $h_ratio;
my $h = $h_ratio * ($end - $start + 1);
my $color = $svg_colors[$index % @svg_colors];
my $sw = min($w_ratio, $h_ratio) / 10;
$w = 1 if $w < 1;
print $fh "\t<rect x=\"$x\" y=\"$y\" width=\"$w\" height=\"$h\" style=\"fill:$color;fill-opacity:0.2;stroke:black;stroke-width:$sw\"/>\n";
return 1;
}
);
return;
}
# Comparison functions
my $comparison_function = 'default';
my %comparison_functions = (
'default' => \&starting_times_comparison,
'all_times' => \&all_times_comparison
);
sub set_comparison_function {
($comparison_function) = @_;
return;
}
sub three_way_comparison {
my ($self, $other, $inverted) = @_;
return $comparison_functions{$comparison_function}->($self, $other, $inverted);
}
sub starting_times_comparison {
my ($self, $other, $inverted) = @_;
# Save two calls to the comparison functions if $other is a Profile
$other = $other->starting_time() if defined blessed($other) and blessed($other) eq 'Profile';
return $other <=> $self->{starting_time} if $inverted;
return $self->{starting_time} <=> $other;
}
sub all_times_comparison {
my ($self, $other, $inverted) = @_;
return $self->{starting_time} <=> $other->{starting_time} if defined blessed($other) and blessed($other) eq 'Profile';
my $coef = $inverted ? -1 : 1;
return -$coef if defined $self->{ending_time} and $self->{ending_time} <= $other;
return $coef if $self->{starting_time} >= $other;
return 0;
}
sub subtract {
my ($self, $other, $inverted) = @_;
# Save two calls to the comparison functions if $other is a Profile
$other = $other->starting_time() if defined blessed($other) and blessed($other) eq 'Profile';
return $other - $self->{starting_time} if $inverted;
return $self->{starting_time} - $other;
}
1;