Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
worker.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/types.hh"
5#include "nix/util/logging.hh"
7#include "nix/store/build.hh"
13
14#include <functional>
15#include <future>
16#include <thread>
17#include <queue>
18
19namespace nix {
20
21/* Forward definition. */
22struct WorkerSettings;
24struct DerivationGoal;
29
42GoalPtr upcast_goal(std::shared_ptr<PathSubstitutionGoal> subGoal);
43GoalPtr upcast_goal(std::shared_ptr<DrvOutputSubstitutionGoal> subGoal);
44GoalPtr upcast_goal(std::shared_ptr<DerivationGoal> subGoal);
45
46typedef std::chrono::time_point<std::chrono::steady_clock> steady_time_point;
47
53struct Child
54{
55 WeakGoalPtr goal;
56 Goal * goal2; // ugly hackery
57 std::set<MuxablePipePollState::CommChannel> channels;
58 bool respectTimeouts;
59 bool inBuildSlot;
63 steady_time_point lastOutput;
64 steady_time_point timeStarted;
65};
66
67#ifndef _WIN32 // TODO Enable building on Windows
68/* Forward definition. */
69struct HookInstance;
70#endif
71
76class LocalBuilder : public Builder
77{
78public:
79 LocalBuilder(ref<Store> store, ref<Store> evalStore)
80 : store(store)
81 , evalStore(evalStore) {};
82
83 /* Builder interface — see `Builder` for documentation. */
84
85 void buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode) override;
86 std::vector<KeyedBuildResult>
87 buildPathsWithResults(const std::vector<DerivedPath> & reqs, BuildMode buildMode) override;
88 BuildResult buildDerivation(const StorePath & drvPath, const BasicDerivation & drv, BuildMode buildMode) override;
89 void ensurePath(const StorePath & path) override;
90 void repairPath(const StorePath & path) override;
91
92private:
97 inline std::shared_ptr<Worker> getWorker()
98 {
99 return std::make_shared<Worker>(*store, *evalStore);
100 }
101
102 ref<Store> store;
103 ref<Store> evalStore;
104};
105
109class Worker : public Builder
110{
111private:
112
113 /* Note: the worker should only have strong pointers to the
114 top-level goals. */
115
119 Goals topGoals;
120
124 WeakGoals awake;
125
129 WeakGoals wantingToBuild;
130
134 WeakGoals wantingToSubstitute;
135
139 std::list<Child> children;
140
145 size_t nrLocalBuilds;
146
150 size_t nrSubstitutions;
151
156
158
159 std::map<StorePath, std::map<OutputName, std::weak_ptr<DerivationGoal>>> derivationGoals;
160 std::map<StorePath, std::weak_ptr<DerivationResolutionGoal>> derivationResolutionGoals;
161 std::map<StorePath, std::weak_ptr<DerivationBuildingGoal>> derivationBuildingGoals;
162 std::map<StorePath, std::weak_ptr<PathSubstitutionGoal>> substitutionGoals;
163 std::map<DrvOutput, std::weak_ptr<DrvOutputSubstitutionGoal>> drvOutputSubstitutionGoals;
164
168 WeakGoals waitingForAWhile;
169
173 WeakGoals waitingForCompletion;
174
178 steady_time_point lastWokenUp;
179
183 std::map<StorePath, bool> pathContentsGoodCache;
184
185 class Waker
186 {
187#ifndef _WIN32
192 unix::SelfPipe wakeupPipe;
193#else
194 Descriptor ioport;
195#endif
199 Sync<std::queue<WeakGoalPtr>> wakeupQueue_;
200
201 friend class Worker;
202
203 void wakeAll(Worker & worker);
204
205 Waker()
206 {
207#ifndef _WIN32
208 wakeupPipe.create();
209#endif
210 }
211
212 public:
213 void enqueue(WeakGoalPtr goal);
214 };
215
219 ref<Waker> wakerState;
220
221public:
222
223 const Activity act;
224 const Activity actDerivations;
225 const Activity actSubstitutions;
226
231
232#ifdef _WIN32
233 AutoCloseFD ioport;
234#endif
235
236 Store & store;
237 Store & evalStore;
238
239 const WorkerSettings & settings;
240
248
249#ifndef _WIN32 // TODO Enable building on Windows
250 std::unique_ptr<HookInstance> hook;
251#endif
252
253 uint64_t expectedBuilds = 0;
254 uint64_t doneBuilds = 0;
255 uint64_t failedBuilds = 0;
256 uint64_t runningBuilds = 0;
257
258 uint64_t expectedSubstitutions = 0;
259 uint64_t doneSubstitutions = 0;
260 uint64_t failedSubstitutions = 0;
261 uint64_t runningSubstitutions = 0;
262 uint64_t expectedDownloadSize = 0;
263 uint64_t doneDownloadSize = 0;
264 uint64_t expectedNarSize = 0;
265 uint64_t doneNarSize = 0;
266
271 bool tryBuildHook = true;
272
273 Worker(Store & store, Store & evalStore);
274 ~Worker();
275
279
283private:
284 template<class G, typename... Args>
285 std::shared_ptr<G> initGoalIfNeeded(std::weak_ptr<G> & goal_weak, Args &&... args);
286
287 std::shared_ptr<DerivationTrampolineGoal> makeDerivationTrampolineGoal(
288 ref<const SingleDerivedPath> drvReq, const OutputsSpec & wantedOutputs, BuildMode buildMode);
289
290public:
291 std::shared_ptr<DerivationTrampolineGoal> makeDerivationTrampolineGoal(
292 const StorePath & drvPath, const OutputsSpec & wantedOutputs, const Derivation & drv, BuildMode buildMode);
293
294 std::shared_ptr<DerivationGoal> makeDerivationGoal(
295 const StorePath & drvPath,
297 const OutputName & wantedOutput,
298 BuildMode buildMode,
299 bool storeDerivation);
300
304 std::shared_ptr<DerivationResolutionGoal>
305 makeDerivationResolutionGoal(const StorePath & drvPath, ref<const Derivation> drv, BuildMode buildMode);
306
310 std::shared_ptr<DerivationBuildingGoal>
311 makeDerivationBuildingGoal(const StorePath & drvPath, ref<const BasicDerivation> drv, BuildMode buildMode);
312
316 std::shared_ptr<PathSubstitutionGoal> makePathSubstitutionGoal(
317 const StorePath & storePath, RepairFlag repair = NoRepair, std::optional<ContentAddress> ca = std::nullopt);
318 std::shared_ptr<DrvOutputSubstitutionGoal> makeDrvOutputSubstitutionGoal(const DrvOutput & id);
319
326 GoalPtr makeGoal(const DerivedPath & req, BuildMode buildMode = bmNormal);
327
331 void removeGoal(GoalPtr goal);
332
336 void wakeUp(GoalPtr goal);
337
342 std::weak_ptr<Waker> getCrossThreadWaker();
343
348 size_t getNrLocalBuilds();
349
353 size_t getNrSubstitutions();
354
359 void childStarted(
360 GoalPtr goal,
361 const std::set<MuxablePipePollState::CommChannel> & channels,
362 bool inBuildSlot,
363 bool respectTimeouts);
364
371 void childTerminated(Goal * goal);
372
380 void childTerminated(Goal * goal, JobCategory jobCategory);
381
386 void waitForBuildSlot(GoalPtr goal);
387
394 void waitForAWhile(GoalPtr goal);
395
399 void waitForCompletion(GoalPtr goal);
400
404 void run(const Goals & topGoals);
405
409 void waitForInput();
410
415 bool pathContentsGood(const StorePath & path);
416
417 void markContentsGood(const StorePath & path);
418
419 void updateProgress()
420 {
421 actDerivations.progress(doneBuilds, expectedBuilds + doneBuilds, runningBuilds, failedBuilds);
422 actSubstitutions.progress(
423 doneSubstitutions, expectedSubstitutions + doneSubstitutions, runningSubstitutions, failedSubstitutions);
424 act.setExpected(actFileTransfer, expectedDownloadSize + doneDownloadSize);
425 act.setExpected(actCopyPath, expectedNarSize + doneNarSize);
426 }
427
428 /* Builder interface — see `Builder` for documentation. */
429
430 void buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode) override;
431 std::vector<KeyedBuildResult>
432 buildPathsWithResults(const std::vector<DerivedPath> & reqs, BuildMode buildMode) override;
433 BuildResult buildDerivation(const StorePath & drvPath, const BasicDerivation & drv, BuildMode buildMode) override;
434 void ensurePath(const StorePath & path) override;
435 void repairPath(const StorePath & path) override;
436};
437
438} // namespace nix
Definition args.hh:29
Definition file-descriptor.hh:265
Definition drv-output-substitution-goal.hh:27
void buildPaths(const std::vector< DerivedPath > &reqs, BuildMode buildMode) override
Definition entry-points.cc:11
void repairPath(const StorePath &path) override
Definition entry-points.cc:40
BuildResult buildDerivation(const StorePath &drvPath, const BasicDerivation &drv, BuildMode buildMode) override
Definition entry-points.cc:22
void ensurePath(const StorePath &path) override
Definition entry-points.cc:27
std::vector< KeyedBuildResult > buildPathsWithResults(const std::vector< DerivedPath > &reqs, BuildMode buildMode) override
Definition entry-points.cc:17
Definition substitution-goal.hh:14
Definition path.hh:33
Definition store-api.hh:397
std::shared_ptr< DerivationResolutionGoal > makeDerivationResolutionGoal(const StorePath &drvPath, ref< const Derivation > drv, BuildMode buildMode)
Definition worker.cc:108
void ensurePath(const StorePath &path) override
Definition entry-points.cc:129
std::vector< KeyedBuildResult > buildPathsWithResults(const std::vector< DerivedPath > &reqs, BuildMode buildMode) override
Definition entry-points.cc:81
size_t getNrLocalBuilds()
Definition worker.cc:185
void childStarted(GoalPtr goal, const std::set< MuxablePipePollState::CommChannel > &channels, bool inBuildSlot, bool respectTimeouts)
Definition worker.cc:195
GoalPtr makeGoal(const DerivedPath &req, BuildMode buildMode=bmNormal)
Definition worker.cc:130
std::shared_ptr< DerivationBuildingGoal > makeDerivationBuildingGoal(const StorePath &drvPath, ref< const BasicDerivation > drv, BuildMode buildMode)
Definition worker.cc:114
void wakeUp(GoalPtr goal)
Definition worker.cc:179
std::weak_ptr< Waker > getCrossThreadWaker()
Definition worker.cc:520
void childTerminated(Goal *goal)
Definition worker.cc:222
void waitForCompletion(GoalPtr goal)
Definition worker.cc:277
bool pathContentsGood(const StorePath &path)
Definition worker.cc:553
BuildResult buildDerivation(const StorePath &drvPath, const BasicDerivation &drv, BuildMode buildMode) override
Definition entry-points.cc:113
void run(const Goals &topGoals)
Definition worker.cc:283
void removeGoal(GoalPtr goal)
Definition worker.cc:144
std::shared_ptr< PathSubstitutionGoal > makePathSubstitutionGoal(const StorePath &storePath, RepairFlag repair=NoRepair, std::optional< ContentAddress > ca=std::nullopt)
Definition worker.cc:120
void waitForInput()
Definition worker.cc:375
void waitForAWhile(GoalPtr goal)
Definition worker.cc:271
void repairPath(const StorePath &path) override
Definition entry-points.cc:147
ExitStatusFlags exitStatusFlags
Definition worker.hh:230
void buildPaths(const std::vector< DerivedPath > &reqs, BuildMode buildMode) override
Definition entry-points.cc:45
bool tryBuildHook
Definition worker.hh:271
void waitForBuildSlot(GoalPtr goal)
Definition worker.cc:254
fun< std::list< ref< Store > >()> getSubstituters
Definition worker.hh:247
size_t getNrSubstitutions()
Definition worker.cc:190
Definition fun.hh:19
Definition ref.hh:58
int Descriptor
Definition file-descriptor.hh:36
JobCategory
Definition goal.hh:57
std::shared_ptr< Goal > GoalPtr
Definition goal.hh:32
std::set< GoalPtr, CompareGoalPtrs > Goals
Definition goal.hh:43
std::string OutputName
Definition outputs-spec.hh:18
Definition logging.hh:221
Definition build-result.hh:123
Definition build.hh:17
Definition worker.hh:54
steady_time_point lastOutput
Definition worker.hh:63
Definition derivation-building-goal.hh:33
Definition derivation-goal.hh:34
Definition derivation-resolution-goal.hh:37
Definition derivation-trampoline-goal.hh:86
Definition derived-path-map.hh:32
Definition derived-path.hh:238
Definition realisation.hh:26
Definition build-result.hh:222
Definition goal.hh:79
Definition hook-instance.hh:22
Definition outputs-spec.hh:27
Definition worker-settings.hh:27
Definition file-descriptor.hh:377