diff --git a/src/hydra-queue-runner/build-remote.cc b/src/hydra-queue-runner/build-remote.cc index 45ebd695..d0bf7d2c 100644 --- a/src/hydra-queue-runner/build-remote.cc +++ b/src/hydra-queue-runner/build-remote.cc @@ -8,6 +8,7 @@ #include "state.hh" #include "util.hh" #include "worker-protocol.hh" +#include "finally.hh" using namespace nix; @@ -75,7 +76,6 @@ static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Chil static void copyClosureTo(ref destStore, FdSource & from, FdSink & to, const PathSet & paths, - counter & bytesSent, bool useSubstitutes = false) { PathSet closure; @@ -103,9 +103,6 @@ static void copyClosureTo(ref destStore, printMsg(lvlDebug, format("sending %1% missing paths") % missing.size()); - for (auto & p : missing) - bytesSent += destStore->queryPathInfo(p).narSize; - to << cmdImportPaths; destStore->exportPaths(missing, false, to); to.flush(); @@ -115,19 +112,6 @@ static void copyClosureTo(ref destStore, } -static void copyClosureFrom(ref destStore, - FdSource & from, FdSink & to, const PathSet & paths, counter & bytesReceived, - std::shared_ptr accessor) -{ - to << cmdExportPaths << 0 << paths; - to.flush(); - destStore->importPaths(false, from, accessor); - - for (auto & p : paths) - bytesReceived += destStore->queryPathInfo(p).narSize; -} - - void State::buildRemote(ref destStore, Machine::ptr machine, Step::ptr step, unsigned int maxSilentTime, unsigned int buildTimeout, @@ -153,6 +137,11 @@ void State::buildRemote(ref destStore, FdSource from(child.from); FdSink to(child.to); + Finally updateStats([&]() { + bytesReceived += from.read; + bytesSent += to.written; + }); + /* Handshake. */ bool sendDerivation = true; unsigned int remoteVersion; @@ -239,7 +228,7 @@ void State::buildRemote(ref destStore, auto now1 = std::chrono::steady_clock::now(); - copyClosureTo(destStore, from, to, inputs, bytesSent, true); + copyClosureTo(destStore, from, to, inputs, true); auto now2 = std::chrono::steady_clock::now(); @@ -302,7 +291,9 @@ void State::buildRemote(ref destStore, auto now1 = std::chrono::steady_clock::now(); - copyClosureFrom(destStore, from, to, outputs, bytesReceived, result.accessor); + to << cmdExportPaths << 0 << outputs; + to.flush(); + destStore->importPaths(false, from, result.accessor); auto now2 = std::chrono::steady_clock::now(); diff --git a/src/hydra-queue-runner/finally.hh b/src/hydra-queue-runner/finally.hh new file mode 100644 index 00000000..47c64dea --- /dev/null +++ b/src/hydra-queue-runner/finally.hh @@ -0,0 +1,12 @@ +#pragma once + +/* A trivial class to run a function at the end of a scope. */ +class Finally +{ +private: + std::function fun; + +public: + Finally(std::function fun) : fun(fun) { } + ~Finally() { fun(); } +};