Move collecting handler to separate function.

s/master/collector/g
This commit is contained in:
John Soo 2022-04-20 21:01:02 -07:00
parent a27faabd0a
commit b9a87464a0
No known key found for this signature in database
GPG key ID: D8A148F8CE4DDBC2

View file

@ -182,7 +182,7 @@ static void worker(
auto vRoot = topLevelValue(state, autoArgs); auto vRoot = topLevelValue(state, autoArgs);
while (true) { while (true) {
/* Wait for the master to send us a job name. */ /* Wait for the collector to send us a job name. */
writeLine(to.get(), "next"); writeLine(to.get(), "next");
auto s = readLine(from.get()); auto s = readLine(from.get());
@ -192,7 +192,7 @@ static void worker(
debug("worker process %d at '%s'", getpid(), attrPath); debug("worker process %d at '%s'", getpid(), attrPath);
/* Evaluate it and send info back to the master. */ /* Evaluate it and send info back to the collector. */
nlohmann::json reply; nlohmann::json reply;
reply["attr"] = attrPath; reply["attr"] = attrPath;
@ -287,7 +287,7 @@ static void worker(
writeLine(to.get(), reply.dump()); writeLine(to.get(), reply.dump());
/* If our RSS exceeds the maximum, exit. The master will /* If our RSS exceeds the maximum, exit. The collector will
start a new process. */ start a new process. */
struct rusage r; struct rusage r;
getrusage(RUSAGE_SELF, &r); getrusage(RUSAGE_SELF, &r);
@ -341,54 +341,15 @@ struct Proc {
~Proc() { } ~Proc() { }
}; };
int main(int argc, char * * argv) struct State
{ {
/* Prevent undeclared dependencies in the evaluation via
$NIX_PATH. */
unsetenv("NIX_PATH");
/* We are doing the garbage collection by killing forks */
setenv("GC_DONT_GC", "1", 1);
return handleExceptions(argv[0], [&]() {
initNix();
initGC();
myArgs.parseCmdline(argvToStrings(argc, argv));
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
settings.builders = "";
/* Prevent access to paths outside of the Nix search path and
to the environment. */
evalSettings.restrictEval = false;
/* When building a flake, use pure evaluation (no access to
'getEnv', 'currentSystem' etc. */
evalSettings.pureEval = myArgs.evalMode == evalAuto ? myArgs.flake : myArgs.evalMode == evalPure;
if (myArgs.releaseExpr == "") throw UsageError("no expression specified");
if (myArgs.gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
if (myArgs.showTrace) {
loggerSettings.showTrace.assign(true);
}
struct State
{
std::set<std::string> todo{""}; std::set<std::string> todo{""};
std::set<std::string> active; std::set<std::string> active;
std::exception_ptr exc; std::exception_ptr exc;
}; };
std::condition_variable wakeup; std::function<void()> collector(Sync<State> & state_, std::condition_variable & wakeup) {
return [&]() {
Sync<State> state_;
/* Start a handler thread per worker process. */
auto handler = [&]()
{
try { try {
std::optional<std::unique_ptr<Proc>> proc_; std::optional<std::unique_ptr<Proc>> proc_;
@ -463,10 +424,49 @@ int main(int argc, char * * argv)
wakeup.notify_all(); wakeup.notify_all();
} }
}; };
}
int main(int argc, char * * argv)
{
/* Prevent undeclared dependencies in the evaluation via
$NIX_PATH. */
unsetenv("NIX_PATH");
/* We are doing the garbage collection by killing forks */
setenv("GC_DONT_GC", "1", 1);
return handleExceptions(argv[0], [&]() {
initNix();
initGC();
myArgs.parseCmdline(argvToStrings(argc, argv));
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
settings.builders = "";
/* Prevent access to paths outside of the Nix search path and
to the environment. */
evalSettings.restrictEval = false;
/* When building a flake, use pure evaluation (no access to
'getEnv', 'currentSystem' etc. */
evalSettings.pureEval = myArgs.evalMode == evalAuto ? myArgs.flake : myArgs.evalMode == evalPure;
if (myArgs.releaseExpr == "") throw UsageError("no expression specified");
if (myArgs.gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
if (myArgs.showTrace) {
loggerSettings.showTrace.assign(true);
}
Sync<State> state_;
/* Start a collector thread per worker process. */
std::vector<std::thread> threads; std::vector<std::thread> threads;
std::condition_variable wakeup;
for (size_t i = 0; i < myArgs.nrWorkers; i++) for (size_t i = 0; i < myArgs.nrWorkers; i++)
threads.emplace_back(std::thread(handler)); threads.emplace_back(std::thread(collector(state_, wakeup)));
for (auto & thread : threads) for (auto & thread : threads)
thread.join(); thread.join();