2011-11-30 16:32:50 +00:00
|
|
|
#! /var/run/current-system/sw/bin/perl -w
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
use strict;
|
2008-11-11 14:45:33 +00:00
|
|
|
use Cwd;
|
2008-11-28 14:36:04 +00:00
|
|
|
use File::Basename;
|
2008-11-29 01:20:13 +00:00
|
|
|
use POSIX qw(dup2 :sys_wait_h);
|
2008-11-25 11:09:15 +00:00
|
|
|
use Hydra::Schema;
|
2008-11-28 14:36:04 +00:00
|
|
|
use Hydra::Helper::Nix;
|
2012-03-13 11:10:19 +00:00
|
|
|
use Hydra::Model::DB;
|
2011-08-19 16:09:14 +00:00
|
|
|
use IO::Handle;
|
2011-11-30 14:25:28 +00:00
|
|
|
use Nix::Store;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2012-03-13 11:10:19 +00:00
|
|
|
chdir Hydra::Model::DB::getHydraPath or die;
|
|
|
|
my $db = Hydra::Model::DB->new();
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2009-04-22 22:59:54 +00:00
|
|
|
STDOUT->autoflush();
|
|
|
|
|
2013-06-07 20:10:45 +00:00
|
|
|
my $lastTime;
|
|
|
|
|
2008-11-29 01:20:13 +00:00
|
|
|
#$SIG{CHLD} = 'IGNORE';
|
2008-11-29 01:01:22 +00:00
|
|
|
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2008-11-28 11:16:53 +00:00
|
|
|
sub unlockDeadBuilds {
|
|
|
|
# Unlock builds whose building process has died.
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2012-02-29 01:22:49 +00:00
|
|
|
my @builds = $db->resultset('Builds')->search({finished => 0, busy => 1});
|
2008-11-28 11:16:53 +00:00
|
|
|
foreach my $build (@builds) {
|
2012-02-29 01:22:49 +00:00
|
|
|
my $pid = $build->locker;
|
2008-11-29 01:20:13 +00:00
|
|
|
my $unlock = 0;
|
|
|
|
if ($pid == $$) {
|
2013-09-21 14:47:52 +00:00
|
|
|
if (!defined $lastTime || $build->starttime < $lastTime - 600) {
|
2008-11-29 01:20:13 +00:00
|
|
|
$unlock = 1;
|
|
|
|
}
|
|
|
|
} elsif (kill(0, $pid) != 1) { # see if we can signal the process
|
|
|
|
$unlock = 1;
|
|
|
|
}
|
|
|
|
if ($unlock) {
|
2008-11-28 11:16:53 +00:00
|
|
|
print "build ", $build->id, " pid $pid died, unlocking\n";
|
2013-01-22 21:48:02 +00:00
|
|
|
$build->update({ busy => 0, locker => ""});
|
2013-01-22 22:01:29 +00:00
|
|
|
$build->buildsteps->search({ busy => 1 })->update({ busy => 0, status => 4, stoptime => time });
|
2008-11-28 11:00:55 +00:00
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
2008-11-28 11:00:55 +00:00
|
|
|
});
|
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2010-08-31 16:19:33 +00:00
|
|
|
|
2013-05-03 14:18:27 +00:00
|
|
|
# Given a build, return an arbitrary queued build on which this build
|
|
|
|
# depends; or undef if no such build exists.
|
2010-04-13 08:42:44 +00:00
|
|
|
sub findBuildDependencyInQueue {
|
2013-06-07 19:51:15 +00:00
|
|
|
my ($buildsByDrv, $build) = @_;
|
2013-09-21 18:35:02 +00:00
|
|
|
return undef; # FIXME
|
2013-06-14 11:00:05 +00:00
|
|
|
return undef unless isValidPath($build->drvpath);
|
2013-05-03 14:18:27 +00:00
|
|
|
my @deps = grep { /\.drv$/ && $_ ne $build->drvpath } computeFSClosure(0, 0, $build->drvpath);
|
|
|
|
return unless scalar @deps > 0;
|
2013-06-07 19:51:15 +00:00
|
|
|
foreach my $d (@deps) {
|
|
|
|
my $b = $buildsByDrv->{$d};
|
|
|
|
next unless defined $b;
|
|
|
|
return $db->resultset('Builds')->find($b);
|
|
|
|
}
|
|
|
|
return undef;
|
2010-04-13 08:42:44 +00:00
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2010-08-31 16:19:33 +00:00
|
|
|
|
2008-11-28 11:16:53 +00:00
|
|
|
sub checkBuilds {
|
2013-03-23 12:31:28 +00:00
|
|
|
# print "looking for runnable builds...\n";
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2008-11-28 11:16:53 +00:00
|
|
|
my @buildsStarted;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2013-03-04 22:44:19 +00:00
|
|
|
my $machines = getMachines;
|
|
|
|
|
2013-03-05 17:01:44 +00:00
|
|
|
my %maxConcurrent;
|
2013-03-04 22:44:19 +00:00
|
|
|
|
|
|
|
foreach my $machineName (keys %{$machines}) {
|
2013-09-21 14:47:52 +00:00
|
|
|
foreach my $system (@{${$machines}{$machineName}{'systemTypes'}}) {
|
2013-03-04 22:44:19 +00:00
|
|
|
$maxConcurrent{$system} = (${$machines}{$machineName}{'maxJobs'} or 0) + ($maxConcurrent{$system} or 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2013-09-21 14:47:52 +00:00
|
|
|
# Cache scheduled builds by derivation path to speed up
|
2013-06-07 19:51:15 +00:00
|
|
|
# findBuildDependencyInQueue.
|
|
|
|
my $buildsByDrv = {};
|
|
|
|
$buildsByDrv->{$_->drvpath} = $_->id
|
2013-09-21 14:47:52 +00:00
|
|
|
foreach $db->resultset('Builds')->search({ finished => 0 }, { join => ['project'] });
|
2013-06-07 19:51:15 +00:00
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
# Get the system types for the runnable builds.
|
|
|
|
my @systemTypes = $db->resultset('Builds')->search(
|
2013-09-21 14:47:52 +00:00
|
|
|
{ finished => 0, busy => 0 },
|
2012-02-29 01:22:49 +00:00
|
|
|
{ join => ['project'], select => ['system'], as => ['system'], distinct => 1 });
|
2013-01-22 13:41:02 +00:00
|
|
|
|
2013-09-21 14:47:52 +00:00
|
|
|
# Get the total number of scheduling shares.
|
|
|
|
my $totalShares = getTotalShares($db);
|
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
# For each system type, select up to the maximum number of
|
2013-09-21 14:47:52 +00:00
|
|
|
# concurrent build for that system type.
|
2008-11-26 14:20:50 +00:00
|
|
|
foreach my $system (@systemTypes) {
|
|
|
|
# How many builds are already currently executing for this
|
|
|
|
# system type?
|
|
|
|
my $nrActive = $db->resultset('Builds')->search(
|
2012-02-29 01:22:49 +00:00
|
|
|
{finished => 0, busy => 1, system => $system->system})->count;
|
2008-11-26 14:20:50 +00:00
|
|
|
|
2013-03-05 17:01:44 +00:00
|
|
|
(my $systemTypeInfo) = $db->resultset('SystemTypes')->search({system => $system->system});
|
2013-03-07 15:52:31 +00:00
|
|
|
my $max = defined $systemTypeInfo ? $systemTypeInfo->maxconcurrent : $maxConcurrent{$system->system} // 2;
|
2013-03-05 17:01:44 +00:00
|
|
|
|
|
|
|
my $extraAllowed = $max - $nrActive;
|
2013-09-21 14:47:52 +00:00
|
|
|
next if $extraAllowed <= 0;
|
|
|
|
|
|
|
|
print STDERR "starting at most $extraAllowed builds for system ${\$system->system}\n";
|
|
|
|
|
|
|
|
j: while ($extraAllowed-- > 0) {
|
|
|
|
|
|
|
|
my @runnableJobsets = $db->resultset('Builds')->search(
|
|
|
|
{ finished => 0, busy => 0, system => $system->system },
|
|
|
|
{ select => ['project', 'jobset'], distinct => 1 });
|
|
|
|
|
|
|
|
next if @runnableJobsets == 0;
|
|
|
|
|
|
|
|
my $windowSize = 24 * 3600;
|
|
|
|
my $totalWindowSize = $windowSize * $max;
|
|
|
|
|
|
|
|
my @res;
|
|
|
|
|
|
|
|
foreach my $b (@runnableJobsets) {
|
|
|
|
my $jobset = $db->resultset('Jobsets')->find($b->get_column('project'), $b->get_column('jobset')) or die;
|
|
|
|
|
|
|
|
my $duration = $jobset->builds->search(
|
|
|
|
{ },
|
|
|
|
{ where => \ ("(finished = 0 or (me.stoptime >= " . (time() - $windowSize) . "))")
|
|
|
|
, join => 'buildsteps'
|
|
|
|
, select => \ "sum(coalesce(buildsteps.stoptime, ${\time}) - buildsteps.starttime)"
|
|
|
|
, as => "sum" })->single->get_column("sum") // 0;
|
|
|
|
|
|
|
|
# Add a 30s penalty for each started build. This
|
|
|
|
# is to account for jobsets that have running
|
|
|
|
# builds but no build steps yet.
|
|
|
|
$duration += $jobset->builds->search({ finished => 0, busy => 1 })->count * 30;
|
|
|
|
|
|
|
|
my $share = $jobset->schedulingshares;
|
|
|
|
my $delta = ($share / $totalShares) - ($duration / $totalWindowSize);
|
|
|
|
|
|
|
|
#printf STDERR "%s:%s: %d s, %.3f%%, allowance = %.3f%%\n", $jobset->get_column('project'), $jobset->name, $duration, $duration / $totalWindowSize, $delta;
|
|
|
|
|
|
|
|
push @res, { jobset => $jobset, delta => $delta };
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $r (sort { $b->{delta} <=> $a->{delta} } @res) {
|
|
|
|
my $jobset = $r->{jobset};
|
|
|
|
#print STDERR "selected ", $jobset->get_column('project'), ':', $jobset->name, "\n";
|
|
|
|
|
|
|
|
# Select the highest-priority build for this jobset.
|
|
|
|
my @builds = $jobset->builds->search(
|
|
|
|
{ finished => 0, busy => 0, system => $system->system },
|
|
|
|
{ order_by => ["priority DESC", "id"] });
|
|
|
|
|
|
|
|
foreach my $build (@builds) {
|
|
|
|
# Find a dependency of $build that has no queued
|
|
|
|
# dependencies itself. This isn't strictly necessary,
|
|
|
|
# but it ensures that Nix builds are done as part of
|
|
|
|
# their corresponding Hydra builds, rather than as a
|
|
|
|
# dependency of some other Hydra build.
|
|
|
|
while (my $dep = findBuildDependencyInQueue($buildsByDrv, $build)) {
|
|
|
|
$build = $dep;
|
|
|
|
}
|
|
|
|
next if $build->busy;
|
|
|
|
|
|
|
|
printf STDERR "starting build %d (%s:%s:%s) on %s (jobset allowance = %.3f%%)\n",
|
|
|
|
$build->id, $build->project->name, $build->jobset->name, $build->job->name, $build->system, $r->{delta};
|
|
|
|
|
|
|
|
my $logfile = getcwd . "/logs/" . $build->id;
|
|
|
|
mkdir(dirname $logfile);
|
|
|
|
unlink($logfile);
|
|
|
|
$build->update(
|
|
|
|
{ busy => 1
|
|
|
|
, locker => $$
|
|
|
|
, logfile => $logfile
|
|
|
|
, starttime => time()
|
|
|
|
});
|
|
|
|
push @buildsStarted, $build;
|
|
|
|
next j;
|
|
|
|
}
|
2013-05-03 14:18:27 +00:00
|
|
|
}
|
2013-06-07 19:18:09 +00:00
|
|
|
|
2013-09-21 14:47:52 +00:00
|
|
|
last; # nothing found, give up on this system type
|
2008-11-26 14:20:50 +00:00
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
# Actually start the builds we just selected. We need to do this
|
|
|
|
# outside the transaction in case it aborts or something.
|
2008-11-28 11:16:53 +00:00
|
|
|
foreach my $build (@buildsStarted) {
|
|
|
|
my $id = $build->id;
|
2008-11-10 13:33:12 +00:00
|
|
|
eval {
|
2012-02-29 01:22:49 +00:00
|
|
|
my $logfile = $build->logfile;
|
2008-11-11 10:27:36 +00:00
|
|
|
my $child = fork();
|
|
|
|
die unless defined $child;
|
|
|
|
if ($child == 0) {
|
2008-11-28 14:36:04 +00:00
|
|
|
eval {
|
|
|
|
open LOG, ">$logfile" or die "cannot create logfile $logfile";
|
|
|
|
POSIX::dup2(fileno(LOG), 1) or die;
|
|
|
|
POSIX::dup2(fileno(LOG), 2) or die;
|
2011-11-30 16:32:50 +00:00
|
|
|
exec("hydra-build", $id);
|
2008-11-28 14:36:04 +00:00
|
|
|
};
|
|
|
|
warn "cannot start build $id: $@";
|
2008-11-26 14:20:50 +00:00
|
|
|
POSIX::_exit(1);
|
2008-11-11 10:27:36 +00:00
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
warn $@;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2012-02-29 01:22:49 +00:00
|
|
|
$build->busy(0);
|
|
|
|
$build->locker($$);
|
|
|
|
$build->update;
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 10:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 14:18:30 +00:00
|
|
|
if (scalar(@ARGV) == 1 && $ARGV[0] eq "--unlock") {
|
|
|
|
unlockDeadBuilds;
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 10:27:36 +00:00
|
|
|
while (1) {
|
|
|
|
eval {
|
2008-11-29 01:20:13 +00:00
|
|
|
# Clean up zombies.
|
|
|
|
while ((waitpid(-1, &WNOHANG)) > 0) { };
|
2013-01-22 13:41:02 +00:00
|
|
|
|
2008-11-28 11:16:53 +00:00
|
|
|
unlockDeadBuilds;
|
2013-01-22 13:41:02 +00:00
|
|
|
|
2008-11-28 11:16:53 +00:00
|
|
|
checkBuilds;
|
2013-06-07 20:10:45 +00:00
|
|
|
|
|
|
|
$lastTime = time();
|
2008-11-11 10:27:36 +00:00
|
|
|
};
|
|
|
|
warn $@ if $@;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2013-03-23 12:31:28 +00:00
|
|
|
# print "sleeping...\n";
|
2008-11-12 11:09:21 +00:00
|
|
|
sleep(5);
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|