/usr/share/perl5/OAR/Schedulers/ResourceTree.pm is in liboar-perl 2.5.6-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 397 398 399 400 401 402 403 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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | # $Id$
package OAR::Schedulers::ResourceTree;
use warnings;
use strict;
#use Data::Dumper;
use Storable qw(dclone);
#use Time::HiRes qw(gettimeofday);
###############################################################################
# RESOURCE TREE MANAGEMENT #
###############################################################################
# Prototypes
sub new();
sub destroy($);
sub clone($);
sub add_child($$$$);
sub get_children_list($);
sub is_node_a_leaf($);
sub get_father($);
sub get_current_resource_name($);
sub get_current_resource_value($);
sub get_current_children_number($);
sub get_current_level($);
sub get_max_available_children($);
sub set_needed_children_number($$);
sub get_needed_children_number($);
sub delete_subtree($);
sub get_previous_brother($);
sub get_next_brother($);
sub get_initial_child($);
sub get_last_child($);
###############################################################################
sub get_tree_leafs($);
sub get_tree_leafs_vec($);
sub delete_tree_nodes_with_not_enough_resources($);
sub delete_unnecessary_subtrees($);
sub delete_tree_nodes_with_not_enough_resources_and_unnecessary_subtrees($$);
###############################################################################
# Create a tree
# arg : number of needed children
# return the ref of the created tree
sub new(){
my $tree_ref;
$tree_ref->[0] = undef ; # father ref
$tree_ref->[1] = undef ; # ref of a hashtable with all initial children
$tree_ref->[2] = undef ; # name of the resource
$tree_ref->[3] = undef ; # value of this resource
$tree_ref->[4] = 0 ; # level indicator
$tree_ref->[5] = 0 ; # needed children number :
# -1 means ALL (Alive + Absent + Suspected resources)
# -2 means BEST (Alive resources at the time)
$tree_ref->[6] = 0 ; # maximum available children
$tree_ref->[7] = undef ; # previous brother ref
$tree_ref->[8] = undef ; # next brother ref
$tree_ref->[9] = undef ; # first child ref
$tree_ref->[10] = 0 ; # current children number
$tree_ref->[11] = undef ; # last child ref
$tree_ref->[12] = [] ; # array with all tree node refs (only on the first tree node)
return($tree_ref);
}
# Destroy data structure and tell Perl that the corresponding memory can be
# reused
# arg : ROOT tree ref
sub destroy($){
my $tree_ref = shift;
if (defined($tree_ref->[12])){
foreach my $n (@{$tree_ref->[12]}){
undef(@{$n});
undef($n);
}
undef(@{$tree_ref});
undef($tree_ref);
}
}
# clone the tree
# arg : tree ref
# return a copy of the tree ref
sub clone($){
my $tree_ref = shift;
return(dclone($tree_ref));
}
# return 1 if node is a leaf (no child)
# otherwise retuurn 0
sub is_node_a_leaf($){
my $tree_ref = shift;
if ($tree_ref and $tree_ref->[9]){
return(0);
}else{
return(1);
}
}
# add a child to the given tree ref (if child resource name is undef it seems
# that this child is a leaf of the tree)
# arg : tree ref, resource name, resource value, tree root ref
# return the ref of the child
sub add_child($$$$){
my $tree_ref = shift;
my $resource_name = shift;
my $resource_value = shift;
my $tree_root_ref = shift; # First node of the tree
my $tmp_ref;
if (!defined($tree_ref->[1]->{$resource_value})){
# Create a new tree node
$tmp_ref = [ $tree_ref, undef, $resource_name, $resource_value, $tree_ref->[4] + 1, 0, 0, undef, undef, undef, 0, undef ];
push(@{$tree_root_ref->[12]}, $tmp_ref);
$tree_ref->[1]->{$resource_value} = $tmp_ref;
$tree_ref->[6] += 1;
$tree_ref->[10] += 1;
# Add new brother
if ($tree_ref->[9]){
$tmp_ref->[8] = $tree_ref->[9];
$tree_ref->[9]->[7] = $tmp_ref;
}
$tree_ref->[9] = $tmp_ref;
# Init last child
if (!$tree_ref->[11]){
$tree_ref->[11] = $tmp_ref;
}
}else{
$tmp_ref = $tree_ref->[1]->{$resource_value};
}
return($tmp_ref);
}
# Store information about the number of needed children
sub set_needed_children_number($$){
my $tree_ref = shift;
my $needed_children_number = shift;
$tree_ref->[5] = $needed_children_number;
}
# Get previous brother on the same level for the same father
sub get_previous_brother($){
my $tree_ref = shift;
if (!$tree_ref){
return(undef);
}else{
return($tree_ref->[7]);
}
}
# Get next brother on the same level for the same father
sub get_next_brother($){
my $tree_ref = shift;
if (! $tree_ref){
return(undef);
}else{
return($tree_ref->[8]);
}
}
# Get initial child ref
sub get_initial_child($){
my $tree_ref = shift;
if (! $tree_ref){
return(undef);
}else{
return($tree_ref->[9]);
}
}
# Get last child ref
sub get_last_child($){
my $tree_ref = shift;
if (! $tree_ref){
return(undef);
}else{
return($tree_ref->[11]);
}
}
# Get the ref of the father tree
# arg : tree ref
# return a tree ref
sub get_father($){
my $tree_ref = shift;
if (!$tree_ref || !$tree_ref->[0]){
return(undef);
}else{
return($tree_ref->[0]);
}
}
# Get the current resource name
# arg : tree ref
# return the resource name
sub get_current_resource_name($){
my $tree_ref = shift;
if (! $tree_ref){
return(undef);
}else{
return($tree_ref->[2]);
}
}
# Get the current resource value
# arg : tree ref
# return the resource value
sub get_current_resource_value($){
my $tree_ref = shift;
if (! $tree_ref){
return(undef);
}else{
return($tree_ref->[3]);
}
}
# Get the current children number
# arg : tree ref
# return the resource value
sub get_current_children_number($){
my $tree_ref = shift;
if ($tree_ref){
return($tree_ref->[10]);
}else{
return(undef);
}
}
# Get the current level indicator
# arg : tree ref
# return the level indicator
sub get_current_level($){
my $tree_ref = shift;
if ($tree_ref){
return($tree_ref->[4]);
}else{
return(0);
}
}
# Get the maximum available number of children
# (just after the creation of the tree)
# arg : tree ref
# return an integer >= 0
sub get_max_available_children($){
my $tree_ref = shift;
if ($tree_ref){
return($tree_ref->[6]);
}else{
return(0);
}
}
# Get the number of needed children
# arg : tree ref
# return the number of needed children
sub get_needed_children_number($){
my $tree_ref = shift;
if ($tree_ref){
return($tree_ref->[5]);
}else{
return(0);
}
}
# Delete a subtree
# arg : tree ref to delete
# return father tree ref
sub delete_subtree($){
my $tree_ref = shift;
return(undef) if (! $tree_ref);
my $father_ref = $tree_ref->[0];
my $prev_brother = $tree_ref->[7];
my $next_brother = $tree_ref->[8];
if (! $prev_brother){
if ($father_ref){
$father_ref->[9] = $next_brother; # update father first child
}
}else{
$prev_brother->[8] = $next_brother; # update next brother
}
if (! $next_brother){
if ($father_ref){
$father_ref->[11] = $prev_brother; # update father last child
}
}else{
$next_brother->[7] = $prev_brother; # update previous brother
}
if ($father_ref and $father_ref->[1]){
$father_ref->[10] -= 1;
return($father_ref);
}else{
return(undef);
}
}
###############################################################################
# delete_tree_nodes_with_not_enough_resources
# Delete subtrees that do not fit wanted resources
# args: tree ref
# side effect : modify tree data structure
sub delete_tree_nodes_with_not_enough_resources($){
my $tree_ref = shift;
#print("START delete_tree_nodes_with_not_enough_resources\n");
# Search if there are enough values for each resource
# Tremaux algorithm (Deep first)
my $current_node = $tree_ref;
do{
if (defined(get_initial_child($current_node))){
# Go to child
$current_node = get_initial_child($current_node);
#print("Go to CHILD =".get_current_resource_value($current_node)."\n");
}else{
# Treate leaf
while(defined($current_node) and (!defined(get_next_brother($current_node)))){
# Step up
#print("Go to FATHER : ".get_current_resource_value($current_node)."\n") if (defined(get_current_resource_value($current_node)));
if ((get_needed_children_number($current_node) > get_current_children_number($current_node))
or ((get_needed_children_number($current_node) == -1) # ALL
and (get_max_available_children($current_node) > get_current_children_number($current_node)))
or ((get_needed_children_number($current_node) == -2) # BEST
and (get_current_children_number($current_node) <= 0))
){
# we want to delete the root
return(undef) if ($tree_ref == $current_node);
#print("DELETE 1".get_current_resource_value($current_node)."\n");
# Delete sub tree that does not fit with wanted resources
$current_node = delete_subtree($current_node);
}else{
$current_node = get_father($current_node);
}
}
if (defined(get_next_brother($current_node))){
# Treate brother
if ((get_needed_children_number($current_node) > get_current_children_number($current_node))
or ((get_needed_children_number($current_node) == -1) # ALL
and (get_max_available_children($current_node) > get_current_children_number($current_node)))
or ((get_needed_children_number($current_node) == -2) # BEST
and (get_current_children_number($current_node) <= 0))
){
#print("DELETE 2".get_current_resource_value($current_node)."\n");
# Delete sub tree that does not fit with wanted resources
delete_subtree($current_node);
}
$current_node = get_next_brother($current_node);
#print("Go to BROTHER : ".get_current_resource_value($current_node)."\n");
}
}
}while(defined($current_node));
if (!defined(get_initial_child($tree_ref))){
return(undef);
}else{
return($tree_ref);
}
}
# get_tree_leafs
# return a list of tree leafs
# arg: tree ref
sub get_tree_leafs($){
my $tree = shift;
my @result;
return(@result) if (!defined($tree));
# Search leafs
# Tremaux algorithm (Deep first)
my $current_node = $tree;
do{
if (defined(get_initial_child($current_node))){
# Go to child
$current_node = get_initial_child($current_node);
#print("Go to CHILD =".get_current_resource_value($current_node)."\n");
}else{
# Treate leaf
if (is_node_a_leaf($current_node) == 1){
#push(@result, $node_name_pile[0]);
push(@result, $current_node);
#print("Leaf: ".get_current_resource_name($current_node)." = ".get_current_resource_value($current_node)."\n");
}
# Look at brothers
while(defined($current_node) and (!defined(get_next_brother($current_node)))){
# Step up
$current_node = get_father($current_node);
#print("Go to FATHER : ".get_current_resource_value($current_node)."\n") if (defined(get_current_resource_value($current_node)));
}
if (defined(get_next_brother($current_node))){
# Treate brother
$current_node = get_next_brother($current_node);
#print("Go to BROTHER : ".get_current_resource_value($current_node)."\n");
}
}
}while(defined($current_node));
return(@result);
}
# get_tree_leafs_vec
# return a binary vector with the leaf resource_id AND a hashref with resource_id => tree_ref
# arg: root tree ref
sub get_tree_leafs_vec($){
my $tree = shift;
my $result_leafs_vec = '';
my %result_leafs_hash = ();
return($result_leafs_vec, \%result_leafs_hash) if (!$tree);
# Search leafs
# Tremaux algorithm (Deep first)
my $current_node = $tree;
do{
if (get_initial_child($current_node)){
# Go to child
$current_node = get_initial_child($current_node);
#print("Go to CHILD =".get_current_resource_value($current_node)."\n");
}else{
# Treate leaf
if (is_node_a_leaf($current_node) == 1){
vec($result_leafs_vec, get_current_resource_value($current_node), 1) = 1;
$result_leafs_hash{get_current_resource_value($current_node)} = $current_node;
#print("Leaf: ".get_current_resource_name($current_node)." = ".get_current_resource_value($current_node)."\n");
}
# Look at brothers
while($current_node and (!get_next_brother($current_node))){
# Step up
$current_node = get_father($current_node);
#print("Go to FATHER : ".get_current_resource_value($current_node)."\n") if (defined(get_current_resource_value($current_node)));
}
if (get_next_brother($current_node)){
# Treate brother
$current_node = get_next_brother($current_node);
#print("Go to BROTHER : ".get_current_resource_value($current_node)."\n");
}
}
}while($current_node);
return($result_leafs_vec, \%result_leafs_hash);
}
# delete_unnecessary_subtrees
# Delete subtrees that are not necessary (watch needed_children_number)
# args: tree ref
# side effect : modify tree data structure
sub delete_unnecessary_subtrees($){
my $tree_ref = shift;
return($tree_ref) if (!defined($tree_ref));
# Search if there are enough values for each resource
# Tremaux algorithm (Deep first)
my $current_node = $tree_ref;
do{
if ((get_needed_children_number($current_node) >= 0) and (get_needed_children_number($current_node) < get_current_children_number($current_node))){
# Delete extra sub tree
delete_subtree(get_initial_child($current_node));
}else{
if (defined(get_initial_child($current_node))){
# Go to child
$current_node = get_initial_child($current_node);
#print("Go to CHILD =".get_current_resource_value($current_node)."\n");
}else{
# Look at brothers
while(defined($current_node) and (!defined(get_next_brother($current_node)))){
# Step up
$current_node = get_father($current_node);
#print("Go to FATHER : ".get_current_resource_value($current_node)."\n") if (defined(get_current_resource_value($current_node)));
}
if (defined(get_next_brother($current_node))){
# Treate brother
$current_node = get_next_brother($current_node);
#print("Go to BROTHER : ".get_current_resource_value($current_node)."\n");
}
}
}
}while(defined($current_node));
return($tree_ref);
}
# delete_tree_nodes_with_not_enough_resources_and_unnecessary_subtrees
# Delete subtrees that do not fit wanted resources and unnecessary one
# args: tree ref, bit vector with authorized leafs (resource_id)
# side effect : modify tree data structure
sub delete_tree_nodes_with_not_enough_resources_and_unnecessary_subtrees($$){
my $tree_ref = shift;
my $authorized_leafs_vec = shift; # if undef then all resources are authorized
# print("START delete_tree_nodes_with_not_enough_resources\n");
# Record the current number of children that can be taken
my %nb_children_validated;
# Search if there are enough values for each resource
# Tremaux algorithm (Deep first)
my $current_node = $tree_ref;
do{
if (get_last_child($current_node)){
# Go to child
$nb_children_validated{$current_node} = 0;
$current_node = get_last_child($current_node);
# print("Go to CHILD : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."\n");
}else{
# Treate leaf
if (defined($authorized_leafs_vec)
and (is_node_a_leaf($current_node) == 1)
and ($tree_ref != $current_node)
and (get_current_resource_name($current_node) eq "resource_id")
and (!vec($authorized_leafs_vec, get_current_resource_value($current_node), 1))
){
# print("DELETE LEAF : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."\n");
# This leaf is not available
my $tmp_prev = get_previous_brother($current_node);
$current_node = delete_subtree($current_node);
if ($tmp_prev){
$current_node = $tmp_prev;
}
}else{
while($current_node and (! get_previous_brother($current_node))){
if ((get_needed_children_number($current_node) > get_current_children_number($current_node))
or ((get_needed_children_number($current_node) == -1) # ALL
and (get_max_available_children($current_node) > get_current_children_number($current_node)))
or ((get_needed_children_number($current_node) == -2) # BEST
and (get_current_children_number($current_node) <= 0))
or (($tree_ref != $current_node)
and (get_needed_children_number(get_father($current_node)) > 0)
and ($nb_children_validated{get_father($current_node)} >= get_needed_children_number(get_father($current_node))))
){
# we want to delete the root
return(undef) if ($tree_ref == $current_node);
# print("DELETE 2 : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."\n");
# Delete sub tree that does not fit with wanted resources
$current_node = delete_subtree($current_node);
}else{
# Step up
$current_node = get_father($current_node);
$nb_children_validated{$current_node}++ if ($current_node);
# print("Go to FATHER : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)." ($nb_children_validated{$current_node}/".get_needed_children_number($current_node).")\n") if (defined(get_current_resource_value($current_node)));
}
}
if ($current_node
and ($tree_ref != $current_node)
){
my $tmp_father = get_father($current_node);
if ((get_needed_children_number($tmp_father) > 0)
and ($nb_children_validated{$tmp_father} >= get_needed_children_number($tmp_father))
){
# We have enough nodes at this stage
$current_node->[7] = undef; # purge the rest of brothers
#$tmp_father->[10] = $nb_children_validated{$tmp_father} + 1;
# print("PURGE BROTHERS : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."($tmp_father->[10])\n");
}
}
if (get_previous_brother($current_node)){
# Treate brother
if ((get_needed_children_number($current_node) > get_current_children_number($current_node))
or ((get_needed_children_number($current_node) == -1) # ALL
and (get_max_available_children($current_node) > get_current_children_number($current_node)))
or ((get_needed_children_number($current_node) == -2) # BEST
and (get_current_children_number($current_node) <= 0))
){
# print("DELETE 3 : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."\n");
# Delete sub tree that does not fit with wanted resources
delete_subtree($current_node);
}elsif ($tree_ref != $current_node){
$nb_children_validated{get_father($current_node)}++;
}
$current_node = get_previous_brother($current_node) ;
$nb_children_validated{$current_node} = 0;
# print("Go to BROTHER : ".get_current_resource_name($current_node)."=".get_current_resource_value($current_node)."\n");
}
}
}
}while($current_node);
# print("STOP delete_tree_nodes_with_not_enough_resources\n");
if (! get_initial_child($tree_ref)){
return(undef);
}else{
return($tree_ref);
}
}
return 1;
|