ResUtil.pm.in 13.7 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2019 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# This is a module so it can be used from the fast RPC path.
#
package ResUtil;
use Exporter;

@ISA = "Exporter";
32 33
@EXPORT = qw (CollectReservations CreateTimeline ComputeCounts
              ReservationUtilization);
34 35 36 37 38 39 40 41 42 43 44

# After package decl.
use strict;
use English;
use Date::Parse;
use Data::Dumper;
use POSIX qw(strftime);

# Emulab
use Reservation;
use Project;
45
use emutil;
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

# Debugging.
my $debug = 0;
sub DebugOn() { $debug = 2; };

#
# Collect the reservations we are interested in.
#
sub CollectReservations($$$)
{
    my ($project, $useronly, $allres) = @_;
    
    #
    # By default we look at all current reservations for a specific project.
    # Optionally add all historical reservations for the project.
    # Optionally look at reservations for a specific user in the project.
    #
    my @reservations = Reservation->ProjectReservations($project, $useronly,
							undef, 1);
    if ($allres) {
	#
	# Grab all of the historical reservations and append.
	#
	my @more = Reservation->HistoricalReservations($project,
						       $useronly, undef);
	@reservations = (@reservations, @more);
    }
    if (!@reservations) {
	return ();
    }
    if ($debug) {
	print "There are " . scalar(@reservations) . " reservations\n";
    }
    # Sort to make sure the earliest is first.
    @reservations = sort {$a->start() <=> $b->start()} @reservations;
    return @reservations;
}

#
# Create a timeline of alloc/free operations, using the project usage
# since the earliest reservation.
#

sub CreateTimeline($@)
{
    my ($project, @reservations) = @_;
92 93 94 95 96 97 98 99 100 101 102 103

    #
    # Remember all the types we care about; when computing the counts
    # do not bother with nodes that are not in the set of types reserved.
    #
    my %typesinuse = ();
    
    foreach my $res (@reservations) {
	my $type  = $res->type();	
	$typesinuse{$type} = $type;
    }
    
104 105 106 107 108 109 110 111 112 113 114
    # We want the earliest/latest reservation for getting project usage();
    my $earliest  = $reservations[0];
    if ($debug) {
	print "The earliest reservation start is " .
	    POSIX::strftime("%m/%d/20%y %H:%M:%S",
			    localtime($earliest->start())) . " \n";
    }
    #
    # We want to bound the end of the search to the latest end if we
    # have only historial reservations.
    #
115 116 117 118 119 120 121 122 123 124 125
    # We have to watch for reservations that are deleted before they
    # expire; that is the actual end time for the reservation.
    #
    my $sortfunc = sub {
	my $a  = $_[0];
	my $b  = $_[1];
	my $s1 = (defined($a->deleted()) ? $a->deleted() : $a->end());
	my $s2 = (defined($b->deleted()) ? $b->deleted() : $b->end());
	return $s1 <=> $s2;
    };
    my @tmp = sort $sortfunc @reservations;
126
    my $latest = $tmp[-1];
127 128 129 130
    my $end = (defined($latest->deleted()) ?
	       $latest->deleted() : $latest->end());
    $end = time() if ($end > time());

131 132
    if ($debug) {
	print "The latest reservation end is " .
133
	    POSIX::strftime("%m/%d/20%y %H:%M:%S", localtime($end)) . " \n";
134 135 136
    }
    
    # Get the usage since the beginning of the earliest reservation.
137
    my $usage = $project->Usage($earliest->start(), $end);
138 139
    if ($debug) {
	print "There are " . scalar(@$usage) . " usage records\n";
140 141 142 143
    }

    @tmp = ();
    foreach my $ref (@$usage) {
144 145 146 147 148
	foreach my $type (keys(%typesinuse)) {
	    if (exists($ref->{'nodes'}->{$type})) {
	        $ref->{'types'}->{$type} = {"count" => 1};
	    }
	}
149 150 151 152 153
	foreach my $type (keys(%{$ref->{'types'}})) {
	    if (exists($typesinuse{$type})) {
		push(@tmp, $ref);
		last;
	    }
154 155
	}
    }
156 157
    if (!scalar(@tmp)) {
	print STDERR "There are no usage records left to process\n"
158 159 160
	    if ($debug);
	return ();
    }
161 162 163 164
    if ($debug > 1) {
	print Dumper(@tmp);
    }
    
165 166 167
    # Form a timeline of changes in allocation.
    my @timeline = ();

168
    foreach my $ref (@tmp) {
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
	push(@timeline, 
	     {"t" => $ref->{'start'}, "details" => $ref, "op" => "alloc"});

	# Experiment ended so nodes are now free.
	push(@timeline, 
	     {"t" => $ref->{'end'}, "details" => $ref, "op" => "free"})
	    if ($ref->{'end'} ne "");
    }
    # And sort the new list.
    @timeline = sort {$a->{'t'} <=> $b->{'t'}} @timeline;

    #
    # Correlate the reservations with the sorted list using the start/end
    # of the reservation and the timestamps in the timeline. This tells us
    # what reservations are active at each point in the timeline.
    #
    foreach my $ref (@timeline) {
	my $stamp   = $ref->{'t'};
	my @reslist = ();

	#print Dumper($ref);

	#
	# This will eventually be too inefficient ...
	#
	foreach my $res (@reservations) {
	    my $resStart = $res->start();
	    my $resEnd   = $res->end();

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	    if ($stamp >= $resStart && $stamp <= $resEnd) {
		push(@reslist, $res);
		next;
	    }
	    #
	    # But what if this usage record is for an experiment that started
	    # prior to the beginning of the reservation, and one of two cases
	    # is true: 1) The experiment is still running. 2) The end of the
	    # experiment is during the reservation.
	    #
	    # In both these cases the reservation is overlaps with the
	    # experiment.
	    #
	    if ($ref->{'op'} eq "alloc" && $stamp <= $resStart &&
		($ref->{'details'}->{'end'} eq "" ||
		 ($ref->{'details'}->{'end'} >= $resStart &&
		  $ref->{'details'}->{'end'} <= $resEnd))) {
		push(@reslist, $res);
Leigh Stoller's avatar
Leigh Stoller committed
216
		print "$res is active for $stamp\n" if ($debug);
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
	}
	next
	    if (!@reslist);
    
	# Each timestamp gets a list of active reservations.
	$ref->{'reservations'} = \@reslist;

	# Count up number of node/types reserved by the project and
	# by each user.
	my $pid = $project->pid();
	my $reserved = {$pid => {}};
	foreach my $res (@reslist) {
	    my $type  = $res->type();
	    my $nodes = $res->nodes();
	    my $uid   = $res->uid();

	    # Remember that we care about this type for later when doing
	    # allocated counts.
	    $typesinuse{$type} = $type;

	    if (!exists($reserved->{$pid}->{$type})) {
		$reserved->{$pid}->{$type} = 0;
	    }
	    $reserved->{$pid}->{$type} += $nodes;
	    
	    if (!exists($reserved->{$uid})) {
		$reserved->{$uid} = {};
	    }
	    if (!exists($reserved->{$uid}->{$type})) {
		$reserved->{$uid}->{$type} = 0;
	    }
	    $reserved->{$uid}->{$type} += $nodes;
	}
	$ref->{'reserved'} = $reserved;
    }
    # Hmm, this happens.
    return ()
	if (!@timeline);
	
    #
    # Now compute the node counts for each entry in the timeline.
    #
    my @counts = ComputeCounts($project, \%typesinuse, @timeline);

262 263 264
    return @counts
	if (1);

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 397 398 399 400 401 402 403
    #
    # We want to add additional entrys for the start of each reservation.
    #
    @tmp = ();
    
    foreach my $ref (@counts) {
	#
	# If this entry is after the timestamp for the first
	# reservation on the list (which is also sorted), then
	# create a new timeline entry for the very start of
	# the reservation so we have counts in use at the very
	# start (since often nodes will not be allocated till
	# sometime later).
	#
	while (@reservations &&
	       $ref->{'t'} >= $reservations[0]->start()) {
	    # Shallow copy.
	    my $new = { %{$ref} };
	    $new->{'t'} = $reservations[0]->start();
	    push(@tmp, $new);
	    shift(@reservations);
	}
	push(@tmp, $ref)
    }
    return @tmp;
}

#
# Now compute total number of nodes of each type in use at each stamp,
# starting with the counts in the first one. This gives us a timeline
# of nodes allocated and released by the project. We also compute counts
# for each user who is active during the timeline.
#
sub ComputeCounts($$@)
{
    my ($project, $typelist, @timeline) = @_;
    my $pid = $project->pid();

    #
    # This is the initial count, which is a copy of the type count array.
    # We also calculate nodes in use for each user, so the initial count 
    # is the starting point for the user in the initial record.
    #
    my %counts = map { $_ => 0 } keys(%{$typelist});
    foreach my $type (keys(%{$timeline[0]->{'details'}->{'types'}})) {
	# Skip types that are not in the set of reserved types.
	next
	    if (!exists($typelist->{$type}));
	    
	$counts{$type} =
	    $timeline[0]->{'details'}->{'types'}->{$type}->{'count'};
    }
    
    $timeline[0]->{'allocated'} = {
	$pid => \%counts,
	# Counts for the user in the first record.
	$timeline[0]->{'details'}->{'user_uid'} => { %counts },
    };

    #
    # Now loop over the rest of the timeline entries.
    #
    for (my $i = 1; $i < scalar(@timeline); $i++) {
	my $ref = $timeline[$i];
	my $types    = $ref->{'details'}->{'types'};
	my $user_uid = $ref->{'details'}->{'user_uid'};
	# Copy (shallow) of previous counts.
	my $allocated = { %{$timeline[$i - 1]->{'allocated'}} };
	# Copy (shallow) of previous project counts.
	my $pcounts = { %{$allocated->{$pid}} };
	# Copy (shallow) of previous user counts or new entry
	# if this is the first allocation by the user.
	my $ucounts = {};
	if (exists($allocated->{$user_uid})) {
	    $ucounts = { %{$allocated->{$user_uid}} };
	}

	# Now update the counts based on current entry.
	foreach my $type (keys(%$types)) {
	    # Skip types that are not in the set of reserved types.
	    next
		if (!exists($typelist->{$type}));
	    
	    my $count = $types->{$type}->{'count'};
	    # If this is a free, we want to subtract the count
	    if ($ref->{'op'} eq "free") {
		$count = 0 - $count;
	    }
	    if (exists($pcounts->{$type})) {
		$pcounts->{$type} += $count;
	    }
	    else {
		$pcounts->{$type} = $count;
	    }
	    if (exists($ucounts->{$type})) {
		$ucounts->{$type} += $count;
	    }
	    else {
		$ucounts->{$type} = $count;
	    }
	}
	$allocated->{$pid} = $pcounts;
	$allocated->{$user_uid} = $ucounts;
	$ref->{'allocated'} = $allocated;
    }
    #
    # OK, now we have the project timeline
    #
    my @bypid = map { {"t"         => $_->{'t'},
		       "allocated" => $_->{'allocated'},
		       "reserved"  => $_->{'reserved'},
		       "reslist"   => $_->{'reservations'}
	} } @timeline;

    if ($debug) {
	foreach my $ref (@bypid) {
	    my @counts = ();
	
	    print POSIX::strftime("%m/%d/20%y %H:%M:%S",
				  localtime($ref->{'t'}));
	    print "\n";
	    foreach my $res (@{$ref->{'reslist'}}) {
		print " " . $res . "\n";
	    }
	    foreach my $who (keys(%{$ref->{'reserved'}})) {
		my $clist = $ref->{'reserved'}->{$who};
		my @clist = map {$_ . ":" . $clist->{$_}} keys(%$clist);
		print "   $who: " . join(",", @clist) . "\n";
	    }
	    foreach my $who (keys(%{$ref->{'allocated'}})) {
		my $clist = $ref->{'allocated'}->{$who};
		my @clist = map {$_ . ":" . $clist->{$_}} keys(%$clist);
		print " $who: " . join(",", @clist) . "\n";
	    }
	}
    }
    return @bypid;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
#
# Given a reservation details hash, calculate a utilization number
# from the history array.
#
sub ReservationUtilization($@)
{
    my ($res, @records) = @_;
    my $count      = $res->nodes();
    my $active     = defined($res->idx()) ? 1 : 0;
    my $resstart   = $res->start();
    my $resend     = ($active ? time() :
		      (defined($res->deleted()) ?
		       $res->deleted() : $res->end()));
    my $reshours   = (($resend - $resstart) / 3600) * $count;
    my $usedhours  = 0;
    my $inuse      = 0;
    my $laststamp  = $resstart;
    my $type       = $res->type();
    my $uid        = $res->uid();
    my @tmp        = @records;

    # Init for caller.
    $res->data('reshours', $reshours);
    $res->data('usedhours', undef);
    $res->data('utilization', undef);

    #
    # Scan past timeline entries that are *before* the start of the
    # reservation; these are experiments that were running when the
    # reservation started, and provide the number of nodes allocated
    # at the time the reservation starts.
    #
    while (@tmp) {
	my $ref       = $tmp[0];
	my $stamp     = $ref->{'t'};
	my $allocated = $ref->{'allocated'};
	$ref->{'tt'}  = TBDateStringGMT($ref->{'t'});

	last
	    if ($stamp >= $resstart);
	    
	# Watch for nothing allocated by the user at this time stamp
	my $using = 0;
	if (exists($allocated->{$uid})) {
	    $using = $allocated->{$uid}->{$type};
	}
	$inuse = $using;
	$inuse = $count if ($inuse > $count);
	shift(@tmp);
    }
    foreach my $ref (@tmp) {
	my $stamp     = $ref->{'t'};
	my $reserved  = $ref->{'reserved'};
	my $allocated = $ref->{'allocated'};
	$ref->{'tt'}  = TBDateStringGMT($ref->{'t'});

	# If this stamp is after the reservation, we can stop. The
	# last entry will be the current number of nodes used till
	# the end of the reservation. This entry is typically for the
	# end of an experiment start before the end of the reservation.
	last
	    if ($stamp > $resend);

	# Watch for nothing allocated by the user at this time stamp
	my $using = 0;
	if (exists($allocated->{$uid})) {
	    $using = $allocated->{$uid}->{$type};
	}
	$usedhours += (($stamp - $laststamp) / 3600) * $inuse;
	$laststamp  = $stamp;
	$inuse      = $using;
	$inuse      = $count if ($inuse > $count);
    }
    # And then a final entry for usage until the end of the reservation.
    if ($laststamp) {
	$usedhours += (($resend - $laststamp) / 3600) * $inuse;
    }
    $res->data('reshours', $reshours);
    $res->data('usedhours', $usedhours);
    my $utilization = POSIX::ceil(($usedhours/$reshours) * 100.0);
    $utilization = 100 if ($utilization > 100);
    $res->data('utilization', $utilization);

    return 0;
}

490
1;