Changeset 1143


Ignore:
Timestamp:
Jan 10, 2011, 2:26:33 AM (9 years ago)
Author:
bruno
Message:
  • Adds function pb_set_parallel which set $pbparallel depending on memory size for VMs
  • Adds pb_set_port and pb_get_port functions to deal with SSH communication port with VMs using a range
  • build2v and setup2v are now parallel
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb/bin/pb

    r1140 r1143  
    16681668    }
    16691669
    1670     # Useless for VE
    1671     my $nport;
    1672     if ($cmt !~ /^VE/) {
    1673         $nport = $sshport->{$ENV{'PBPROJ'}};
    1674         $nport = "$pbport" if (defined $pbport);
    1675     }
     1670    # Useless for VE
     1671    my $nport = pb_get_port($sshport->{$ENV{'PBPROJ'}},$cmt) if ($cmt !~ /^VE/);
    16761672
    16771673    # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
     
    19151911        # TODO: vmmonport should be optional
    19161912        my ($ptr,$ptr2,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmcmd","vmpath","vmport","vmsize","vmmonport");
    1917         my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
     1913        my ($vmopt,$vmmem,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmmem","vmtmout","vmsnap");
    19181914
    19191915        my $vmtype = $ptr->{$ENV{'PBPROJ'}};
     
    19381934        } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
    19391935            $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
     1936        }
     1937
     1938        # How much memory to allocate for VMs
     1939        if (defined $vmmem->{$v}) {
     1940            $ENV{'PBVMOPT'} .= " -m $vmmem->{$v}";
     1941        } elsif (defined $vmmem->{$ENV{'PBPROJ'}}) {
     1942            $ENV{'PBVMOPT'} .= " -m $vmmem->{$ENV{'PBPROJ'}}";
    19401943        }
    19411944
     
    19631966            $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
    19641967        }
    1965         my $nport = $vmport->{$ENV{'PBPROJ'}};
    1966         $nport = "$pbport" if (defined $pbport);
     1968        my $nport = pb_get_port($vmport->{$ENV{'PBPROJ'}});
    19671969   
    19681970        my $cmd;
     
    22242226pb_build2ssh() if (($all == 1) && ($action eq "build"));
    22252227
     2228# Adapt // mode to memory size
     2229$pbparallel = pb_set_parallel($vtype);
     2230
    22262231my ($vmexist,$vmpid) = (undef,undef);
    22272232my $pm = new Parallel::ForkManager($pbparallel) if (defined $pbparallel);
    22282233
     2234# Set which port the VM will use to communicate
     2235$pm->run_on_start(pb_set_port();
     2236
     2237my $counter = 0;
    22292238foreach my $v (@$v) {
    2230     $pm->start and next if (defined $pbparallel);
     2239    $counter++;
     2240    # Modulo pbparallel
     2241    $counter = 1 if ($counter > $pbparallel);
     2242    $pm->start($counter) and next if (defined $pbparallel);
    22312243    # Prepare the script to be executed on the VM/VE
    22322244    # in $ENV{'PBDESTDIR'}/pbscript.$$
     
    23312343
    23322344    if ($vtype eq "vm") {
    2333         # Skip that VM if it something went wrong
     2345        # Skip that VM if something went wrong
    23342346        if (($vmpid == 0) && ($vmexist == 0)) {
    23352347            $pm->finish if (defined $pbparallel);
     
    25002512my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
    25012513
     2514# Adapt // mode to memory size
     2515$pbparallel = pb_set_parallel($vtype);
     2516
     2517my $pm = new Parallel::ForkManager($pbparallel) if (defined $pbparallel);
     2518
     2519# Set which port the VM will use to communicate
     2520$pm->run_on_start(pb_set_port();
     2521
     2522my $counter = 0;
    25022523foreach my $v (@$vm) {
     2524    $counter++;
     2525    # Modulo pbparallel
     2526    $counter = 1 if ($counter > $pbparallel);
     2527    $pm->start($counter) and next if (defined $pbparallel);
     2528
    25032529    # Deal with date sync.
    25042530    my ($ntpline,$dateline) = pb_date2v($vtype,$v);
     
    25472573       
    25482574        my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
    2549         my $nport = $vmport->{$ENV{'PBPROJ'}};
    2550         $nport = "$pbport" if (defined $pbport);
     2575        my $nport = pb_get_port($vmport->{$ENV{'PBPROJ'}});
    25512576   
    25522577        # Skip that VM if something went wrong
     
    28802905   
    28812906    pb_script2v($pbscript,$vtype,$pbforce,$v);
     2907    $pm->finish if (defined $pbparallel);
     2908}
     2909if (defined $pbparallel) {
     2910    $pm->wait_all_children;
    28822911}
    28832912return;
     
    34293458}
    34303459
     3460# Manages VM SSH port communication
     3461sub pb_get_port {
     3462
     3463my $port = shift;
     3464my $cmt = shift;
     3465
     3466die "No port passed in parameter. Report to dev team\n" if (not defined $port);
     3467my $nport = $port;
     3468# Maybe a port was given as parameter so overwrite
     3469$nport = "$pbport" if (defined $pbport);
     3470# Maybe in // mode so use the env var set up as an offset to the base port, except when called from send2target for Packages
     3471if ($cmt ne "Packages") {
     3472    $nport += $ENV{'PBVMPORT'} if ((defined $pbparallel) && (defined $ENV{'PBVMPORT'}));
     3473}
     3474return($nport);
     3475}
     3476
     3477sub pb_set_port {
     3478       
     3479my ($pid,$ident) = @_;
     3480$ENV{'PBVMPORT'} = $ident;
     3481}
     3482
     3483sub pb_set_parallel {
     3484
     3485my $vtype = shift;
     3486
     3487my ($pbvmmem) = pb_conf_get_if("pbvmmem");
     3488
     3489# Take care of memory size if VM, parallel mode and more than 1 action
     3490if ((defined $pbparallel) && ($pbparallel ne 1) && ($vtype eq "vm")) {
     3491    eval
     3492    {
     3493        require Linux::SysInfo;
     3494        Linux::SysInfo->import();
     3495    };
     3496    if ($@) {
     3497        # Linux::SysInfo not found, defaulting to 4
     3498        $pbparallel = 4;
     3499    } else {
     3500        # Using the memory size
     3501        my $si = Linux::SysInfo::sysinfo();
     3502        if (not defined $si) {
     3503            $pbparallel = 4
     3504        } else {
     3505            # Keep the number of VM whose memory can be allocated
     3506            my $ram = $si->{"totalram"}-$si->{"sharedram"}-$si->{"bufferram"};
     3507            my $ram2;
     3508            if ((defined $pbvmmem) and (defined $pbvmmem->{$ENV{'PBPROJ'}})) {
     3509                $ram2 = $pbvmmem->{$ENV{'PBPROJ'}};
     3510            } else {
     3511                # Default for KVM/QEMU
     3512                $ram2 = 128;
     3513            }
     3514            $pbparallel = sprintf("%d",$ram/$ram2);
     3515        }
     3516    }
     3517}
     3518return($pbparallel);
     3519}
     3520
     3521
    343135221;
Note: See TracChangeset for help on using the changeset viewer.