2020-10-11 16:17:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-10-12 17:16:48 +00:00
|
|
|
#include "types.hh"
|
2020-10-11 16:17:24 +00:00
|
|
|
#include "lock.hh"
|
|
|
|
#include "local-store.hh"
|
2020-10-12 17:16:48 +00:00
|
|
|
#include "goal.hh"
|
2009-01-12 16:30:32 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
/* Forward definition. */
|
2015-07-20 01:15:45 +00:00
|
|
|
class DerivationGoal;
|
2020-10-12 20:47:22 +00:00
|
|
|
class SubstitutionGoal;
|
|
|
|
|
2020-10-13 18:04:24 +00:00
|
|
|
/* Workaround for not being able to declare a something like
|
|
|
|
|
|
|
|
class SubstitutionGoal : public Goal;
|
|
|
|
|
|
|
|
even when Goal is a complete type; */
|
2020-10-12 20:47:22 +00:00
|
|
|
GoalPtr upcast_goal(std::shared_ptr<SubstitutionGoal> subGoal);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2016-12-06 20:58:04 +00:00
|
|
|
typedef std::chrono::time_point<std::chrono::steady_clock> steady_time_point;
|
|
|
|
|
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
/* A mapping used to remember for each child process to what goal it
|
2005-10-17 15:33:24 +00:00
|
|
|
belongs, and file descriptors for receiving log data and output
|
|
|
|
path creation commands. */
|
2004-06-19 21:45:04 +00:00
|
|
|
struct Child
|
|
|
|
{
|
2004-06-25 15:36:09 +00:00
|
|
|
WeakGoalPtr goal;
|
2016-08-30 13:45:39 +00:00
|
|
|
Goal * goal2; // ugly hackery
|
2005-10-17 15:33:24 +00:00
|
|
|
set<int> fds;
|
2013-04-23 16:04:59 +00:00
|
|
|
bool respectTimeouts;
|
2004-06-19 21:45:04 +00:00
|
|
|
bool inBuildSlot;
|
2016-12-06 20:58:04 +00:00
|
|
|
steady_time_point lastOutput; /* time we last got output on stdout/stderr */
|
|
|
|
steady_time_point timeStarted;
|
2004-06-19 21:45:04 +00:00
|
|
|
};
|
|
|
|
|
2020-10-12 17:16:48 +00:00
|
|
|
/* Forward definition. */
|
|
|
|
struct HookInstance;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
/* The worker class. */
|
|
|
|
class Worker
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2004-06-25 15:36:09 +00:00
|
|
|
/* Note: the worker should only have strong pointers to the
|
|
|
|
top-level goals. */
|
|
|
|
|
|
|
|
/* The top-level goals of the worker. */
|
|
|
|
Goals topGoals;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
/* Goals that are ready to do some work. */
|
2004-06-25 15:36:09 +00:00
|
|
|
WeakGoals awake;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
/* Goals waiting for a build slot. */
|
2004-06-25 15:36:09 +00:00
|
|
|
WeakGoals wantingToBuild;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
/* Child processes currently running. */
|
2016-04-29 11:57:08 +00:00
|
|
|
std::list<Child> children;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2009-03-31 21:14:07 +00:00
|
|
|
/* Number of build slots occupied. This includes local builds and
|
|
|
|
substitutions but not remote builds via the build hook. */
|
|
|
|
unsigned int nrLocalBuilds;
|
2004-06-19 21:45:04 +00:00
|
|
|
|
2005-01-19 11:16:11 +00:00
|
|
|
/* Maps used to prevent multiple instantiations of a goal for the
|
2005-01-20 16:01:07 +00:00
|
|
|
same derivation / path. */
|
2020-10-12 20:47:22 +00:00
|
|
|
std::map<StorePath, std::weak_ptr<DerivationGoal>> derivationGoals;
|
|
|
|
std::map<StorePath, std::weak_ptr<SubstitutionGoal>> substitutionGoals;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2007-08-28 11:36:17 +00:00
|
|
|
/* Goals waiting for busy paths to be unlocked. */
|
|
|
|
WeakGoals waitingForAnyGoal;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2009-03-23 01:05:54 +00:00
|
|
|
/* Goals sleeping for a few seconds (polling a lock). */
|
|
|
|
WeakGoals waitingForAWhile;
|
|
|
|
|
|
|
|
/* Last time the goals in `waitingForAWhile' where woken up. */
|
2016-12-06 20:58:04 +00:00
|
|
|
steady_time_point lastWokenUp;
|
2011-06-30 15:19:13 +00:00
|
|
|
|
2016-04-08 16:07:13 +00:00
|
|
|
/* Cache for pathContentsGood(). */
|
2019-12-05 18:11:09 +00:00
|
|
|
std::map<StorePath, bool> pathContentsGoodCache;
|
2016-04-08 16:07:13 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
public:
|
|
|
|
|
2017-08-14 18:14:55 +00:00
|
|
|
const Activity act;
|
2017-08-15 13:31:59 +00:00
|
|
|
const Activity actDerivations;
|
2017-08-14 20:12:36 +00:00
|
|
|
const Activity actSubstitutions;
|
2017-08-14 18:14:55 +00:00
|
|
|
|
2010-12-13 16:53:23 +00:00
|
|
|
/* Set if at least one derivation had a BuildError (i.e. permanent
|
|
|
|
failure). */
|
|
|
|
bool permanentFailure;
|
|
|
|
|
2014-08-17 17:09:03 +00:00
|
|
|
/* Set if at least one derivation had a timeout. */
|
|
|
|
bool timedOut;
|
|
|
|
|
2019-07-01 22:12:12 +00:00
|
|
|
/* Set if at least one derivation fails with a hash mismatch. */
|
|
|
|
bool hashMismatch;
|
|
|
|
|
|
|
|
/* Set if at least one derivation is not deterministic in check mode. */
|
|
|
|
bool checkMismatch;
|
|
|
|
|
2008-06-09 13:52:45 +00:00
|
|
|
LocalStore & store;
|
|
|
|
|
2017-01-19 14:15:09 +00:00
|
|
|
std::unique_ptr<HookInstance> hook;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2017-08-15 13:31:59 +00:00
|
|
|
uint64_t expectedBuilds = 0;
|
|
|
|
uint64_t doneBuilds = 0;
|
|
|
|
uint64_t failedBuilds = 0;
|
|
|
|
uint64_t runningBuilds = 0;
|
|
|
|
|
2017-08-14 20:12:36 +00:00
|
|
|
uint64_t expectedSubstitutions = 0;
|
|
|
|
uint64_t doneSubstitutions = 0;
|
2017-08-15 13:31:59 +00:00
|
|
|
uint64_t failedSubstitutions = 0;
|
2017-08-14 20:42:17 +00:00
|
|
|
uint64_t runningSubstitutions = 0;
|
2017-08-14 18:14:55 +00:00
|
|
|
uint64_t expectedDownloadSize = 0;
|
|
|
|
uint64_t doneDownloadSize = 0;
|
|
|
|
uint64_t expectedNarSize = 0;
|
|
|
|
uint64_t doneNarSize = 0;
|
|
|
|
|
2017-10-24 09:00:16 +00:00
|
|
|
/* Whether to ask the build hook if it can build a derivation. If
|
|
|
|
it answers with "decline-permanently", we don't try again. */
|
|
|
|
bool tryBuildHook = true;
|
|
|
|
|
2008-06-09 13:52:45 +00:00
|
|
|
Worker(LocalStore & store);
|
2004-06-18 18:09:32 +00:00
|
|
|
~Worker();
|
|
|
|
|
2004-06-25 15:36:09 +00:00
|
|
|
/* Make a goal (with caching). */
|
2020-08-22 20:44:47 +00:00
|
|
|
|
|
|
|
/* derivation goal */
|
|
|
|
private:
|
|
|
|
std::shared_ptr<DerivationGoal> makeDerivationGoalCommon(
|
|
|
|
const StorePath & drvPath, const StringSet & wantedOutputs,
|
|
|
|
std::function<std::shared_ptr<DerivationGoal>()> mkDrvGoal);
|
|
|
|
public:
|
|
|
|
std::shared_ptr<DerivationGoal> makeDerivationGoal(
|
|
|
|
const StorePath & drvPath,
|
|
|
|
const StringSet & wantedOutputs, BuildMode buildMode = bmNormal);
|
|
|
|
std::shared_ptr<DerivationGoal> makeBasicDerivationGoal(
|
|
|
|
const StorePath & drvPath, const BasicDerivation & drv,
|
|
|
|
const StringSet & wantedOutputs, BuildMode buildMode = bmNormal);
|
|
|
|
|
|
|
|
/* substitution goal */
|
2020-10-12 20:47:22 +00:00
|
|
|
std::shared_ptr<SubstitutionGoal> makeSubstitutionGoal(const StorePath & storePath, RepairFlag repair = NoRepair, std::optional<ContentAddress> ca = std::nullopt);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2004-06-25 15:36:09 +00:00
|
|
|
/* Remove a dead goal. */
|
2004-06-18 18:09:32 +00:00
|
|
|
void removeGoal(GoalPtr goal);
|
|
|
|
|
|
|
|
/* Wake up a goal (i.e., there is something for it to do). */
|
|
|
|
void wakeUp(GoalPtr goal);
|
|
|
|
|
2009-03-31 21:14:07 +00:00
|
|
|
/* Return the number of local build and substitution processes
|
|
|
|
currently running (but not remote builds via the build
|
|
|
|
hook). */
|
|
|
|
unsigned int getNrLocalBuilds();
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2006-12-08 17:26:21 +00:00
|
|
|
/* Registers a running child process. `inBuildSlot' means that
|
|
|
|
the process counts towards the jobs limit. */
|
2016-04-29 11:57:08 +00:00
|
|
|
void childStarted(GoalPtr goal, const set<int> & fds,
|
|
|
|
bool inBuildSlot, bool respectTimeouts);
|
2006-12-08 17:26:21 +00:00
|
|
|
|
|
|
|
/* Unregisters a running child process. `wakeSleepers' should be
|
|
|
|
false if there is no sense in waking up goals that are sleeping
|
|
|
|
because they can't run yet (e.g., there is no free build slot,
|
|
|
|
or the hook would still say `postpone'). */
|
2016-08-30 13:45:39 +00:00
|
|
|
void childTerminated(Goal * goal, bool wakeSleepers = true);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2006-12-08 17:26:21 +00:00
|
|
|
/* Put `goal' to sleep until a build slot becomes available (which
|
|
|
|
might be right away). */
|
|
|
|
void waitForBuildSlot(GoalPtr goal);
|
|
|
|
|
2007-08-28 11:36:17 +00:00
|
|
|
/* Wait for any goal to finish. Pretty indiscriminate way to
|
|
|
|
wait for some resource that some other goal is holding. */
|
|
|
|
void waitForAnyGoal(GoalPtr goal);
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2009-03-23 01:05:54 +00:00
|
|
|
/* Wait for a few seconds and then retry this goal. Used when
|
|
|
|
waiting for a lock held by another process. This kind of
|
|
|
|
polling is inefficient, but POSIX doesn't really provide a way
|
|
|
|
to wait for multiple locks in the main select() loop. */
|
|
|
|
void waitForAWhile(GoalPtr goal);
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2005-02-23 11:19:27 +00:00
|
|
|
/* Loop until the specified top-level goals have finished. */
|
|
|
|
void run(const Goals & topGoals);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
/* Wait for input to become available. */
|
|
|
|
void waitForInput();
|
2010-12-13 16:53:23 +00:00
|
|
|
|
|
|
|
unsigned int exitStatus();
|
2016-04-08 16:07:13 +00:00
|
|
|
|
|
|
|
/* Check whether the given valid path exists and has the right
|
|
|
|
contents. */
|
2019-12-05 18:11:09 +00:00
|
|
|
bool pathContentsGood(const StorePath & path);
|
2016-04-08 16:07:13 +00:00
|
|
|
|
2020-06-16 20:20:18 +00:00
|
|
|
void markContentsGood(const StorePath & path);
|
2017-08-14 18:14:55 +00:00
|
|
|
|
|
|
|
void updateProgress()
|
|
|
|
{
|
2017-08-15 13:31:59 +00:00
|
|
|
actDerivations.progress(doneBuilds, expectedBuilds + doneBuilds, runningBuilds, failedBuilds);
|
|
|
|
actSubstitutions.progress(doneSubstitutions, expectedSubstitutions + doneSubstitutions, runningSubstitutions, failedSubstitutions);
|
2020-04-06 21:43:43 +00:00
|
|
|
act.setExpected(actFileTransfer, expectedDownloadSize + doneDownloadSize);
|
2017-08-16 14:38:23 +00:00
|
|
|
act.setExpected(actCopyPath, expectedNarSize + doneNarSize);
|
2017-08-14 18:14:55 +00:00
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
};
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|