feat(build): run a build's steps on one worker per hardware thread
Build() started a thread for every stale shader, asset, C, CUDA and C++
unit, and Module::Compile one more for every stale partition, and
nothing bounded how many of them ran their step at once. Jorijn asked
for the amount of parallelism to be limited by the amount of threads
instead of just starting one per file; the threads are taken here to be
the machine's hardware threads.
The new :Jobs partition holds the two pieces. JobPool is worker threads
behind one queue, started as jobs need them up to a fixed number, and
JobPool::Process() is the pool of the process: at most a worker per
hardware thread, one where std:🧵:hardware_concurrency does not
know. JobGroup is the jobs of one build on a pool. A job starts once
the jobs it was added after have finished; the first job to fail keeps
the ones that have not started from starting, and Wait() returns its
error. No job waits for another job, and a JobGroup made on a worker of
its own pool throws: a worker that waited for a job still in the queue
would hold the thread that job needs.
Build() adds what it did on those threads to a JobGroup instead, and
the group's first error replaces the buildCancelled and buildError pair
the threads set. The module units no longer block on the `compiled`
flag of the units they import: Schedule() replaces Compile() on Module,
ModulePartition and Implementation and adds a unit's jobs after the jobs
that write the BMIs it imports. An interface unit is two jobs, its BMI
and then its object from the BMI, so the units importing it start when
the first is done, as they did when one thread ran both commands and
raised the flag in between. A primary interface now waits for the BMIs
of its partitions and no longer for their objects. Each of the two jobs
has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for
every interface unit, where a module and its partitions shared one
"Compiling interface X".
RunBuildCommand runs its command on a worker of the process's pool: on
the calling thread when that is one, otherwise it waits for one. A link,
an archive, the std module's precompile and project.cpp's compile are
steps under the limit that way. The threads Build() starts per
dependency and per external dependency stay: one of the first runs the
dependency's own Build(), or waits for the thread that does, and one of
the second runs git and cmake.
With the BMI and the object as two jobs, a build that fails between them
has written the BMI and skips the object, and Check, which reads
staleness off the BMI, would call the unit built from then on.
Schedule() therefore adds the object job alone for a unit whose BMI is
up to date and whose object is missing or older than the BMI. A build
interrupted between the two commands, or one whose second command
failed, left the same behind before, and it stayed: the link took the
object of the source before the edit, or failed on the missing one.
ImportCycleError is now what keeps Schedule() from following a round of
imports without end.
The limit is on steps. Outside it, as AGENTS.template.md lists: an
ExternalDependency's cmake --build, which keeps its own --parallel of
as many jobs as there are hardware threads; the tests RunTests runs,
--jobs= of them at once as before; the Crafter.Build modules
EnsureCrafterBuildPcms precompiles one after another; and the threads a
step starts for itself, which lld does for a link and Crafter.Asset's
GDeflate compressor for an asset. The pool is one process's. The --help
text says what a build runs at once and that test --jobs= counts tests.
ConcurrentDependencyReset read the `compiled` flag, which is gone. It
asserts on `checked` and `scheduled` instead, the state a reset from
another build's thread would clear now, with a unit scheduled twice as
the result, and the comment in its fixture says what the implementation
unit comes after now. TransitiveInterfaceChange's comment names
Schedule() where it named Compile() as what honours a partition's
imports. Two comments in :Platform that spoke of worker threads, a word
that now means the pool's, name the threads they mean. The bug-prover
lens's section said shaders compile on a thread of their own each, and
describes the pool now.
This commit is contained in:
parent
eff9868b68
commit
913310da86
19 changed files with 726 additions and 377 deletions
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
export module Crafter.Build:Implementation;
|
||||
import std;
|
||||
import :Jobs;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Crafter {
|
||||
|
|
@ -19,6 +20,9 @@ namespace Crafter {
|
|||
fs::path path;
|
||||
Implementation(fs::path&& path);
|
||||
bool Check(const fs::path& buildDir, const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min()) const;
|
||||
void Compile(const std::string_view clang, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) const;
|
||||
// Adds the job that compiles this unit to `jobs`, after the BMIs of
|
||||
// the stale interface units it imports (ModulePartition::Schedule,
|
||||
// Module::Schedule). For a unit Check found stale.
|
||||
void Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) const;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
export module Crafter.Build:Interface;
|
||||
import std;
|
||||
import :Jobs;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Crafter {
|
||||
|
|
@ -19,14 +20,38 @@ namespace Crafter {
|
|||
// GetInterfacesAndImplementations still produces a staleness edge.
|
||||
// Names that never resolve (`std`, module-mapped externals) stay here.
|
||||
std::vector<std::string> pendingImports;
|
||||
std::atomic<bool> compiled;
|
||||
// The state of one Build(), which clears it before it checks: whether
|
||||
// Check has run, what it found, whether Schedule has added this
|
||||
// unit's jobs, and the one among them that writes its BMI, which is
|
||||
// the job the units importing it come after.
|
||||
bool needsRecompiling = false;
|
||||
bool checked = false;
|
||||
bool scheduled = false;
|
||||
std::optional<JobGroup::Job> precompileJob;
|
||||
std::string name;
|
||||
fs::path path;
|
||||
ModulePartition(std::string&& name, fs::path&& path);
|
||||
// Whether this unit's BMI has to be written again. The object made
|
||||
// from the BMI is not Check's to judge: see Schedule.
|
||||
bool Check(const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min());
|
||||
void Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError);
|
||||
// Adds the jobs this unit needs to `jobs`, once however often it is
|
||||
// called, and returns the one that writes its BMI, if there is one.
|
||||
//
|
||||
// A unit Check found stale gets two, one for each run of `clang`: the
|
||||
// BMI, after the BMIs of the stale units this one imports, which are
|
||||
// scheduled here as well, and then the object from that BMI, which
|
||||
// only the link waits for, so that the units importing this one start
|
||||
// when the first is done.
|
||||
//
|
||||
// A unit whose BMI is up to date gets the second job alone when its
|
||||
// object is missing or older than the BMI. A build that fails or is
|
||||
// interrupted between the two leaves a unit like that, and by its BMI
|
||||
// alone every build after it would call the unit built: the link then
|
||||
// took the object of the source before the edit, or found none.
|
||||
//
|
||||
// Every unit this one imports must have been checked: Build() checks
|
||||
// all of a configuration's interfaces before it schedules any.
|
||||
std::optional<JobGroup::Job> Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir);
|
||||
};
|
||||
|
||||
export class Module {
|
||||
|
|
@ -34,20 +59,27 @@ namespace Crafter {
|
|||
// A primary module interface unit imports things too — `import Base;`
|
||||
// in `export module Widget;` is as much a layout dependency as the
|
||||
// same line in a partition. Recorded on the same three vectors, with
|
||||
// the same meanings, so Check() can see through them and Compile()
|
||||
// the same meanings, so Check() can see through them and Schedule()
|
||||
// can order itself behind a sibling module in the same Configuration
|
||||
// (issue #26).
|
||||
std::vector<Module*> moduleDependencies;
|
||||
std::vector<std::pair<Module*, fs::path>> externalModuleDependencies;
|
||||
std::vector<std::string> pendingImports;
|
||||
std::atomic<bool> compiled;
|
||||
// As in ModulePartition.
|
||||
bool needsRecompiling = false;
|
||||
bool checked = false;
|
||||
bool scheduled = false;
|
||||
std::optional<JobGroup::Job> precompileJob;
|
||||
std::vector<std::unique_ptr<ModulePartition>> partitions;
|
||||
std::string name;
|
||||
fs::path path;
|
||||
Module(std::string&& name, fs::path&& path);
|
||||
bool Check(const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min());
|
||||
void Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError);
|
||||
// As ModulePartition::Schedule, for the primary interface, and
|
||||
// schedules every partition of the module with it. The primary
|
||||
// interface comes after every stale partition of the module,
|
||||
// whichever of them it imports, and after the stale sibling modules
|
||||
// it imports.
|
||||
std::optional<JobGroup::Job> Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
129
interfaces/Crafter.Build-Jobs.cppm
Normal file
129
interfaces/Crafter.Build-Jobs.cppm
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Crafter.Build:Jobs;
|
||||
import std;
|
||||
|
||||
export namespace Crafter {
|
||||
// The threads build steps run on: a fixed number of workers taking jobs
|
||||
// from one queue, so that no more steps run at once than there are
|
||||
// workers, whichever builds they belong to. Process() is the pool every
|
||||
// build in this process shares, with a worker per hardware thread.
|
||||
//
|
||||
// A job is one step of a build: one compiler, linker or archiver run, one
|
||||
// shader or asset compiled in process. The pool bounds the steps that run
|
||||
// at once, not the threads a step starts for itself, as lld does for a
|
||||
// link and the compressor of Crafter.Asset for an asset.
|
||||
//
|
||||
// A job never waits for another job. Whatever waits does so on a
|
||||
// thread that is not a worker: a Build() for the builds of its
|
||||
// dependencies, a step for the steps it comes after (JobGroup holds it
|
||||
// back instead). A worker that waited for a job still in the queue would
|
||||
// keep from it the very thread it needs, and with every worker waiting
|
||||
// like that nothing would run again.
|
||||
//
|
||||
// Before this, Build() started a thread for every stale shader, asset
|
||||
// to compress, C, CUDA and C++ unit, and nothing bounded how many of
|
||||
// them ran at once.
|
||||
class JobPool {
|
||||
public:
|
||||
// At most `workers` jobs at once, at least one. The threads are
|
||||
// started as jobs need them: a build with two stale files starts two.
|
||||
explicit JobPool(std::uint32_t workers);
|
||||
// Runs what is still queued, then joins the workers. Nothing may be
|
||||
// handing it jobs any more: every group on it is gone by then.
|
||||
~JobPool();
|
||||
JobPool(const JobPool&) = delete;
|
||||
JobPool& operator=(const JobPool&) = delete;
|
||||
|
||||
// The pool of this process: one worker per hardware thread
|
||||
// (std::thread::hardware_concurrency), and one where that is not
|
||||
// known. Never destroyed, so that its workers are not joined while
|
||||
// the statics the last job may still be using are being destroyed.
|
||||
static JobPool& Process();
|
||||
|
||||
std::uint32_t Workers() const;
|
||||
|
||||
// Runs `work` on a worker and returns when it has finished; what it
|
||||
// throws is thrown here. For a step that stands by itself, as
|
||||
// RunBuildCommand uses it for a link. Called on a worker of this
|
||||
// pool it runs `work` there and then, since that thread is the one
|
||||
// the step would have occupied.
|
||||
void Run(const std::function<void()>& work);
|
||||
|
||||
private:
|
||||
friend class JobGroup;
|
||||
// Queues `job`, which must not throw. Throws only when the pool has
|
||||
// no thread and cannot start one, and then `job` is not queued.
|
||||
void Submit(std::function<void()> job);
|
||||
void Work();
|
||||
|
||||
const std::uint32_t limit;
|
||||
std::mutex mutex;
|
||||
std::condition_variable wake;
|
||||
std::deque<std::function<void()>> queue;
|
||||
std::vector<std::thread> threads;
|
||||
// Workers waiting for a job. A job queued while one waits is its to
|
||||
// take, so a thread is only started for a job beyond those.
|
||||
std::uint32_t idle = 0;
|
||||
bool stopping = false;
|
||||
};
|
||||
|
||||
// The jobs of one piece of work, such as the build of a configuration,
|
||||
// on the workers of a pool. A job starts once every job it was added
|
||||
// after has finished, so the order steps need (a unit after the BMIs it
|
||||
// imports) costs no thread while it is waited for.
|
||||
//
|
||||
// The first job to fail fails the group: no job of it starts after that,
|
||||
// the ones already running finish, and Wait() returns that first error.
|
||||
// Fail() does the same for work done outside the jobs, so one group
|
||||
// holds the outcome of everything a build runs at once.
|
||||
class JobGroup {
|
||||
public:
|
||||
using Job = std::size_t;
|
||||
|
||||
// Throws std::logic_error on a worker of `pool`: a job that waited
|
||||
// for jobs of its own would occupy a worker they need (see JobPool).
|
||||
explicit JobGroup(JobPool& pool = JobPool::Process());
|
||||
// Keeps what has not started from starting and waits for what is
|
||||
// running, so a job never outlives what it was given by reference,
|
||||
// whichever way the scope that made the group is left.
|
||||
~JobGroup();
|
||||
JobGroup(const JobGroup&) = delete;
|
||||
JobGroup& operator=(const JobGroup&) = delete;
|
||||
|
||||
// Adds a job, counted as a Progress::Task under `label` from now
|
||||
// until it has finished or been skipped. It starts once every job in
|
||||
// `after` has finished, at once when there is none; `after` names
|
||||
// jobs of this group, so a job can only come after ones added before
|
||||
// it and no two can wait for each other. `work` returns what went
|
||||
// wrong, empty when nothing did; an exception it throws is its error
|
||||
// too. It runs on a worker: it must not wait for other jobs, and it
|
||||
// must own what it uses or use only what outlives the group.
|
||||
Job Add(std::string label, std::function<std::string()> work, std::span<const Job> after = {});
|
||||
|
||||
// Fails the group with `error`, as a job returning it would. Without
|
||||
// effect on a group that has already failed.
|
||||
void Fail(std::string error);
|
||||
bool Failed() const;
|
||||
|
||||
// Returns once every job added so far has finished or been skipped:
|
||||
// the first error, empty when there was none.
|
||||
std::string Wait();
|
||||
|
||||
private:
|
||||
struct Node;
|
||||
void Start(Node& node);
|
||||
void Execute(Node& node);
|
||||
|
||||
JobPool& pool;
|
||||
mutable std::mutex mutex;
|
||||
std::condition_variable settled;
|
||||
// Owned through pointers: a job on a worker holds on to its node
|
||||
// while Add() grows the vector.
|
||||
std::vector<std::unique_ptr<Node>> nodes;
|
||||
std::size_t unfinished = 0;
|
||||
bool failed = false;
|
||||
std::string error;
|
||||
};
|
||||
}
|
||||
|
|
@ -42,6 +42,11 @@ namespace Crafter {
|
|||
// Returns an empty string on success (any warnings go to the user via
|
||||
// Progress::Diagnostic), otherwise the compiler's diagnostics. Callers
|
||||
// therefore keep the `if (!result.empty())` shape they already had.
|
||||
//
|
||||
// The command runs on a worker of JobPool::Process(), so no more of these
|
||||
// steps run at once than that pool has workers: called on one of its
|
||||
// workers it runs there, called from any other thread it waits for one.
|
||||
// A step that runs a compiler any other way is not counted.
|
||||
std::string RunBuildCommand(std::string_view command);
|
||||
export CommandResult RunCommandWithTimeout(std::string_view command, std::chrono::seconds timeout);
|
||||
// Runs `command` through sh with this process's own stdin, stdout and
|
||||
|
|
|
|||
|
|
@ -12,8 +12,10 @@ export namespace Crafter::Progress {
|
|||
|
||||
// RAII work-unit marker. Construction inflates the running total; destruction
|
||||
// increments completed and (on Default + TTY) redraws the status line. Move
|
||||
// semantics are disabled — keep these as plain stack locals inside the worker
|
||||
// lambda so the destructor fires when that thread's work ends.
|
||||
// semantics are disabled — keep these as plain stack locals around the work
|
||||
// they count, so the destructor fires when it ends. A job of a JobGroup
|
||||
// gets one from Add(), made when the job is queued, which is what puts
|
||||
// work that has not started yet in the total.
|
||||
class Task {
|
||||
public:
|
||||
explicit Task(std::string label);
|
||||
|
|
|
|||
|
|
@ -57,6 +57,10 @@ export namespace Crafter {
|
|||
|
||||
struct RunTestsOptions {
|
||||
std::vector<std::string> globs;
|
||||
// Tests built and run at once, from --jobs=; 0 is one per hardware
|
||||
// thread. It counts tests, not build steps: the steps of their builds
|
||||
// run on JobPool::Process() under that pool's own limit, and a test
|
||||
// that is running is not one of them.
|
||||
std::int32_t jobs = 0;
|
||||
std::optional<std::chrono::seconds> timeoutOverride;
|
||||
bool listOnly = false;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ export import :Toolchain;
|
|||
export import :Flatpak;
|
||||
export import :Clang;
|
||||
export import :Platform;
|
||||
export import :Jobs;
|
||||
export import :Implementation;
|
||||
export import :Shader;
|
||||
export import :External;
|
||||
|
|
|
|||
Loading…
Reference in a new issue