Skip to content

Commit b01d622

Browse files
committedMay 16, 2017
Improve progress indicator
1 parent e80257f commit b01d622

26 files changed

+339
-168
lines changed
 

‎src/libexpr/eval.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ void EvalState::evalFile(const Path & path, Value & v)
642642
return;
643643
}
644644

645-
Activity act(*logger, lvlTalkative, format("evaluating file ‘%1%’") % path2);
645+
printTalkative("evaluating file ‘%1%’", path2);
646646
Expr * e = parseExprFromFile(checkSourcePath(path2));
647647
try {
648648
eval(e, v);

‎src/libexpr/get-drvs.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static void getDerivations(EvalState & state, Value & vIn,
289289
bound to the attribute with the "lower" name should take
290290
precedence). */
291291
for (auto & i : v.attrs->lexicographicOrder()) {
292-
Activity act(*logger, lvlDebug, format("evaluating attribute ‘%1%’") % i->name);
292+
debug("evaluating attribute ‘%1%’", i->name);
293293
if (!std::regex_match(std::string(i->name), attrRegex))
294294
continue;
295295
string pathPrefix2 = addToPath(pathPrefix, i->name);
@@ -310,7 +310,6 @@ static void getDerivations(EvalState & state, Value & vIn,
310310

311311
else if (v.isList()) {
312312
for (unsigned int n = 0; n < v.listSize(); ++n) {
313-
Activity act(*logger, lvlDebug, "evaluating list element");
314313
string pathPrefix2 = addToPath(pathPrefix, (format("%1%") % n).str());
315314
if (getDerivation(state, *v.listElems()[n], pathPrefix2, drvs, done, ignoreAssertionFailures))
316315
getDerivations(state, *v.listElems()[n], pathPrefix2, autoArgs, drvs, done, ignoreAssertionFailures);

‎src/libexpr/primops.cc

+2-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static void prim_scopedImport(EvalState & state, const Pos & pos, Value * * args
127127
env->values[displ++] = attr.value;
128128
}
129129

130-
Activity act(*logger, lvlTalkative, format("evaluating file ‘%1%’") % path);
130+
printTalkative("evaluating file ‘%1%’", path);
131131
Expr * e = state.parseExprFromFile(resolveExprPath(path), staticEnv);
132132

133133
e->eval(state, *env, v);
@@ -326,8 +326,6 @@ typedef list<Value *> ValueList;
326326

327327
static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * args, Value & v)
328328
{
329-
Activity act(*logger, lvlDebug, "finding dependencies");
330-
331329
state.forceAttrs(*args[0], pos);
332330

333331
/* Get the start set. */
@@ -499,8 +497,6 @@ void prim_valueSize(EvalState & state, const Pos & pos, Value * * args, Value &
499497
derivation. */
500498
static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * * args, Value & v)
501499
{
502-
Activity act(*logger, lvlVomit, "evaluating derivation");
503-
504500
state.forceAttrs(*args[0], pos);
505501

506502
/* Figure out the name first (for stack backtraces). */
@@ -544,7 +540,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
544540
for (auto & i : args[0]->attrs->lexicographicOrder()) {
545541
if (i->name == state.sIgnoreNulls) continue;
546542
string key = i->name;
547-
Activity act(*logger, lvlVomit, format("processing attribute ‘%1%’") % key);
543+
vomit("processing attribute ‘%1%’", key);
548544

549545
auto handleHashMode = [&](const std::string & s) {
550546
if (s == "recursive") outputHashRecursive = true;

‎src/libexpr/primops/fetchgit.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Path exportGit(ref<Store> store, const std::string & uri, const std::string & re
1717
runProgram("git", true, { "init", "--bare", cacheDir });
1818
}
1919

20-
Activity act(*logger, lvlInfo, format("fetching Git repository ‘%s’") % uri);
20+
//Activity act(*logger, lvlInfo, format("fetching Git repository ‘%s’") % uri);
2121

2222
std::string localRef = "pid-" + std::to_string(getpid());
2323
Path localRefFile = cacheDir + "/refs/heads/" + localRef;

‎src/libstore/build.cc

+28-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class Goal : public std::enable_shared_from_this<Goal>
120120
/* Whether the goal is finished. */
121121
ExitCode exitCode;
122122

123+
Activity act;
124+
123125
Goal(Worker & worker) : worker(worker)
124126
{
125127
nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
@@ -168,7 +170,8 @@ class Goal : public std::enable_shared_from_this<Goal>
168170
virtual string key() = 0;
169171

170172
protected:
171-
void amDone(ExitCode result);
173+
174+
virtual void amDone(ExitCode result);
172175
};
173176

174177

@@ -902,6 +905,12 @@ class DerivationGoal : public Goal
902905

903906
void repairClosure();
904907

908+
void amDone(ExitCode result)
909+
{
910+
logger->event(evBuildFinished, act, result == ecSuccess);
911+
Goal::amDone(result);
912+
}
913+
905914
void done(BuildResult::Status status, const string & msg = "");
906915
};
907916

@@ -920,6 +929,8 @@ DerivationGoal::DerivationGoal(const Path & drvPath, const StringSet & wantedOut
920929
state = &DerivationGoal::getDerivation;
921930
name = (format("building of ‘%1%’") % drvPath).str();
922931
trace("created");
932+
933+
logger->event(evBuildCreated, act, drvPath);
923934
}
924935

925936

@@ -935,6 +946,8 @@ DerivationGoal::DerivationGoal(const Path & drvPath, const BasicDerivation & drv
935946
name = (format("building of %1%") % showPaths(drv.outputPaths())).str();
936947
trace("created");
937948

949+
logger->event(evBuildCreated, act, drvPath);
950+
938951
/* Prevent the .chroot directory from being
939952
garbage-collected. (See isActiveTempFile() in gc.cc.) */
940953
worker.store.addTempRoot(drvPath);
@@ -2112,6 +2125,8 @@ void DerivationGoal::startBuilder()
21122125
}
21132126
debug(msg);
21142127
}
2128+
2129+
logger->event(evBuildStarted, act);
21152130
}
21162131

21172132

@@ -2857,7 +2872,7 @@ void DerivationGoal::registerOutputs()
28572872
contained in it. Compute the SHA-256 NAR hash at the same
28582873
time. The hash is stored in the database so that we can
28592874
verify later on whether nobody has messed with the store. */
2860-
Activity act(*logger, lvlTalkative, format("scanning for references inside ‘%1%’") % path);
2875+
debug("scanning for references inside ‘%1%’", path);
28612876
HashResult hash;
28622877
PathSet references = scanForReferences(actualPath, allPaths, hash);
28632878

@@ -3130,6 +3145,7 @@ void DerivationGoal::flushLine()
31303145
logTail.push_back(currentLogLine);
31313146
if (logTail.size() > settings.logLines) logTail.pop_front();
31323147
}
3148+
logger->event(evBuildOutput, act, currentLogLine);
31333149
currentLogLine = "";
31343150
currentLogLinePos = 0;
31353151
}
@@ -3244,6 +3260,12 @@ class SubstitutionGoal : public Goal
32443260
void handleEOF(int fd);
32453261

32463262
Path getStorePath() { return storePath; }
3263+
3264+
void amDone(ExitCode result)
3265+
{
3266+
logger->event(evSubstitutionFinished, act, result == ecSuccess);
3267+
Goal::amDone(result);
3268+
}
32473269
};
32483270

32493271

@@ -3256,6 +3278,7 @@ SubstitutionGoal::SubstitutionGoal(const Path & storePath, Worker & worker, bool
32563278
state = &SubstitutionGoal::init;
32573279
name = (format("substitution of ‘%1%’") % storePath).str();
32583280
trace("created");
3281+
logger->event(evSubstitutionCreated, act, storePath);
32593282
}
32603283

32613284

@@ -3391,6 +3414,8 @@ void SubstitutionGoal::tryToRun()
33913414

33923415
printInfo(format("fetching path ‘%1%’...") % storePath);
33933416

3417+
logger->event(evSubstitutionStarted, act);
3418+
33943419
outPipe.create();
33953420

33963421
promise = std::promise<void>();
@@ -3637,7 +3662,7 @@ void Worker::run(const Goals & _topGoals)
36373662
{
36383663
for (auto & i : _topGoals) topGoals.insert(i);
36393664

3640-
Activity act(*logger, lvlDebug, "entered goal loop");
3665+
debug("entered goal loop");
36413666

36423667
while (1) {
36433668

‎src/libstore/builtins.cc

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
2828
DownloadRequest request(url);
2929
request.verifyTLS = false;
3030

31-
/* Show a progress indicator, even though stderr is not a tty. */
32-
request.showProgress = DownloadRequest::yes;
33-
3431
/* Note: have to use a fresh downloader here because we're in
3532
a forked process. */
3633
auto data = makeDownloader()->download(request);

‎src/libstore/download.cc

+6-32
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ struct CurlDownloader : public Downloader
6363
CurlDownloader & downloader;
6464
DownloadRequest request;
6565
DownloadResult result;
66+
Activity act;
6667
bool done = false; // whether either the success or failure function has been called
6768
std::function<void(const DownloadResult &)> success;
6869
std::function<void(std::exception_ptr exc)> failure;
6970
CURL * req = 0;
7071
bool active = false; // whether the handle has been added to the multi object
7172
std::string status;
7273

73-
bool showProgress = false;
74-
double prevProgressTime{0}, startTime{0};
75-
unsigned int moveBack{1};
76-
7774
unsigned int attempt = 0;
7875

7976
/* Don't start this download until the specified time point
@@ -87,12 +84,10 @@ struct CurlDownloader : public Downloader
8784
DownloadItem(CurlDownloader & downloader, const DownloadRequest & request)
8885
: downloader(downloader), request(request)
8986
{
90-
showProgress =
91-
request.showProgress == DownloadRequest::yes ||
92-
(request.showProgress == DownloadRequest::automatic && isatty(STDERR_FILENO));
93-
9487
if (!request.expectedETag.empty())
9588
requestHeaders = curl_slist_append(requestHeaders, ("If-None-Match: " + request.expectedETag).c_str());
89+
90+
logger->event(evDownloadCreated, act, request.uri);
9691
}
9792

9893
~DownloadItem()
@@ -109,6 +104,7 @@ struct CurlDownloader : public Downloader
109104
} catch (...) {
110105
ignoreException();
111106
}
107+
logger->event(evDownloadDestroyed, act);
112108
}
113109

114110
template<class T>
@@ -171,19 +167,7 @@ struct CurlDownloader : public Downloader
171167

172168
int progressCallback(double dltotal, double dlnow)
173169
{
174-
if (showProgress) {
175-
double now = getTime();
176-
if (prevProgressTime <= now - 1) {
177-
string s = (format(" [%1$.0f/%2$.0f KiB, %3$.1f KiB/s]")
178-
% (dlnow / 1024.0)
179-
% (dltotal / 1024.0)
180-
% (now == startTime ? 0 : dlnow / 1024.0 / (now - startTime))).str();
181-
std::cerr << "\e[" << moveBack << "D" << s;
182-
moveBack = s.size();
183-
std::cerr.flush();
184-
prevProgressTime = now;
185-
}
186-
}
170+
logger->event(evDownloadProgress, act, dltotal, dlnow);
187171
return _isInterrupted;
188172
}
189173

@@ -201,13 +185,6 @@ struct CurlDownloader : public Downloader
201185

202186
void init()
203187
{
204-
// FIXME: handle parallel downloads.
205-
if (showProgress) {
206-
std::cerr << (format("downloading ‘%1%’... ") % request.uri);
207-
std::cerr.flush();
208-
startTime = getTime();
209-
}
210-
211188
if (!req) req = curl_easy_init();
212189

213190
curl_easy_reset(req);
@@ -263,10 +240,6 @@ struct CurlDownloader : public Downloader
263240

264241
void finish(CURLcode code)
265242
{
266-
if (showProgress)
267-
//std::cerr << "\e[" << moveBack << "D\e[K\n";
268-
std::cerr << "\n";
269-
270243
long httpStatus = 0;
271244
curl_easy_getinfo(req, CURLINFO_RESPONSE_CODE, &httpStatus);
272245

@@ -292,6 +265,7 @@ struct CurlDownloader : public Downloader
292265
try {
293266
result.data = decodeContent(encoding, ref<std::string>(result.data));
294267
callSuccess(success, failure, const_cast<const DownloadResult &>(result));
268+
logger->event(evDownloadSucceeded, act, result.data->size());
295269
} catch (...) {
296270
done = true;
297271
callFailure(failure, std::current_exception());

‎src/libstore/download.hh

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct DownloadRequest
1313
std::string uri;
1414
std::string expectedETag;
1515
bool verifyTLS = true;
16-
enum { yes, no, automatic } showProgress = yes;
1716
bool head = false;
1817
size_t tries = 5;
1918
unsigned int baseRetryTimeMs = 250;

‎src/libstore/export-import.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ void Store::exportPaths(const Paths & paths, Sink & sink)
3030
std::reverse(sorted.begin(), sorted.end());
3131

3232
std::string doneLabel("paths exported");
33-
logger->incExpected(doneLabel, sorted.size());
33+
//logger->incExpected(doneLabel, sorted.size());
3434

3535
for (auto & path : sorted) {
36-
Activity act(*logger, lvlInfo, format("exporting path ‘%s’") % path);
36+
//Activity act(*logger, lvlInfo, format("exporting path ‘%s’") % path);
3737
sink << 1;
3838
exportPath(path, sink);
39-
logger->incProgress(doneLabel);
39+
//logger->incProgress(doneLabel);
4040
}
4141

4242
sink << 0;
@@ -81,7 +81,7 @@ Paths Store::importPaths(Source & source, std::shared_ptr<FSAccessor> accessor,
8181

8282
info.path = readStorePath(*this, source);
8383

84-
Activity act(*logger, lvlInfo, format("importing path ‘%s’") % info.path);
84+
//Activity act(*logger, lvlInfo, format("importing path ‘%s’") % info.path);
8585

8686
info.references = readStorePaths<PathSet>(*this, source);
8787

‎src/libstore/gc.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void LocalStore::tryToDelete(GCState & state, const Path & path)
615615
auto realPath = realStoreDir + "/" + baseNameOf(path);
616616
if (realPath == linksDir || realPath == trashDir) return;
617617

618-
Activity act(*logger, lvlDebug, format("considering whether to delete ‘%1%’") % path);
618+
//Activity act(*logger, lvlDebug, format("considering whether to delete ‘%1%’") % path);
619619

620620
if (!isStorePath(path) || !isValidPath(path)) {
621621
/* A lock file belonging to a path that we're building right

‎src/libstore/http-binary-cache-store.cc

-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class HttpBinaryCacheStore : public BinaryCacheStore
5050
{
5151
try {
5252
DownloadRequest request(cacheUri + "/" + path);
53-
request.showProgress = DownloadRequest::no;
5453
request.head = true;
5554
request.tries = 5;
5655
getDownloader()->download(request);
@@ -76,7 +75,6 @@ class HttpBinaryCacheStore : public BinaryCacheStore
7675
std::function<void(std::exception_ptr exc)> failure) override
7776
{
7877
DownloadRequest request(cacheUri + "/" + path);
79-
request.showProgress = DownloadRequest::no;
8078
request.tries = 8;
8179

8280
getDownloader()->enqueueDownload(request,

‎src/libstore/optimise-store.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void LocalStore::optimiseStore(OptimiseStats & stats)
240240
for (auto & i : paths) {
241241
addTempRoot(i);
242242
if (!isValidPath(i)) continue; /* path was GC'ed, probably */
243-
Activity act(*logger, lvlChatty, format("hashing files in ‘%1%’") % i);
243+
//Activity act(*logger, lvlChatty, format("hashing files in ‘%1%’") % i);
244244
optimisePath_(stats, realStoreDir + "/" + baseNameOf(i), inodeHash);
245245
}
246246
}

‎src/libstore/store-api.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ void copyPaths(ref<Store> from, ref<Store> to, const PathSet & storePaths,
822822

823823
std::string copiedLabel = "copied";
824824

825-
logger->setExpected(copiedLabel, missing.size());
825+
//logger->setExpected(copiedLabel, missing.size());
826826

827827
ThreadPool pool;
828828

@@ -838,13 +838,14 @@ void copyPaths(ref<Store> from, ref<Store> to, const PathSet & storePaths,
838838
checkInterrupt();
839839

840840
if (!to->isValidPath(storePath)) {
841-
Activity act(*logger, lvlInfo, format("copying ‘%s’...") % storePath);
841+
//Activity act(*logger, lvlInfo, format("copying ‘%s’...") % storePath);
842842

843843
copyStorePath(from, to, storePath, false, dontCheckSigs);
844844

845-
logger->incProgress(copiedLabel);
845+
//logger->incProgress(copiedLabel);
846846
} else
847-
logger->incExpected(copiedLabel, -1);
847+
;
848+
//logger->incExpected(copiedLabel, -1);
848849
});
849850

850851
pool.process();

‎src/libutil/logging.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "logging.hh"
22
#include "util.hh"
33

4+
#include <atomic>
5+
46
namespace nix {
57

68
Logger * logger = makeDefaultLogger();
@@ -42,12 +44,7 @@ class SimpleLogger : public Logger
4244
writeToStderr(prefix + (tty ? fs.s : filterANSIEscapes(fs.s)) + "\n");
4345
}
4446

45-
void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) override
46-
{
47-
log(lvl, fs);
48-
}
49-
50-
void stopActivity(Activity & activity) override
47+
void event(const Event & ev) override
5148
{
5249
}
5350
};
@@ -79,4 +76,8 @@ Logger * makeDefaultLogger()
7976
return new SimpleLogger();
8077
}
8178

79+
std::atomic<uint64_t> Activity::nextId{(uint64_t) getpid() << 32};
80+
81+
Activity::Activity() : id(nextId++) { };
82+
8283
}

‎src/libutil/logging.hh

+65-26
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,64 @@ typedef enum {
1313
lvlVomit
1414
} Verbosity;
1515

16-
class Activity;
16+
class Activity
17+
{
18+
static std::atomic<uint64_t> nextId;
19+
public:
20+
typedef uint64_t t;
21+
const t id;
22+
Activity();
23+
Activity(const Activity & act) : id(act.id) { };
24+
Activity(uint64_t id) : id(id) { };
25+
};
26+
27+
typedef enum {
28+
evBuildCreated = 0,
29+
evBuildStarted = 1,
30+
evBuildOutput = 2,
31+
evBuildFinished = 3,
32+
evDownloadCreated = 4,
33+
evDownloadDestroyed = 5,
34+
evDownloadProgress = 6,
35+
evDownloadSucceeded = 7,
36+
evSubstitutionCreated = 8,
37+
evSubstitutionStarted = 9,
38+
evSubstitutionFinished = 10,
39+
} EventType;
40+
41+
struct Event
42+
{
43+
struct Field
44+
{
45+
// FIXME: use std::variant.
46+
enum { tInt, tString } type;
47+
uint64_t i = 0;
48+
std::string s;
49+
Field(const std::string & s) : type(tString), s(s) { }
50+
Field(const char * s) : type(tString), s(s) { }
51+
Field(const uint64_t & i) : type(tInt), i(i) { }
52+
Field(const Activity & act) : type(tInt), i(act.id) { }
53+
};
54+
55+
typedef std::vector<Field> Fields;
56+
57+
EventType type;
58+
Fields fields;
59+
60+
std::string getS(size_t n) const
61+
{
62+
assert(n < fields.size());
63+
assert(fields[n].type == Field::tString);
64+
return fields[n].s;
65+
}
66+
67+
uint64_t getI(size_t n) const
68+
{
69+
assert(n < fields.size());
70+
assert(fields[n].type == Field::tInt);
71+
return fields[n].i;
72+
}
73+
};
1774

1875
class Logger
1976
{
@@ -32,34 +89,16 @@ public:
3289

3390
virtual void warn(const std::string & msg);
3491

35-
virtual void setExpected(const std::string & label, uint64_t value = 1) { }
36-
virtual void setProgress(const std::string & label, uint64_t value = 1) { }
37-
virtual void incExpected(const std::string & label, uint64_t value = 1) { }
38-
virtual void incProgress(const std::string & label, uint64_t value = 1) { }
39-
40-
private:
41-
42-
virtual void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) = 0;
43-
44-
virtual void stopActivity(Activity & activity) = 0;
45-
46-
};
47-
48-
class Activity
49-
{
50-
public:
51-
Logger & logger;
52-
53-
Activity(Logger & logger, Verbosity lvl, const FormatOrString & fs)
54-
: logger(logger)
92+
template<typename... Args>
93+
void event(EventType type, const Args & ... args)
5594
{
56-
logger.startActivity(*this, lvl, fs);
95+
Event ev;
96+
ev.type = type;
97+
nop{(ev.fields.emplace_back(Event::Field(args)), 1)...};
98+
event(ev);
5799
}
58100

59-
~Activity()
60-
{
61-
logger.stopActivity(*this);
62-
}
101+
virtual void event(const Event & ev) = 0;
63102
};
64103

65104
extern Logger * logger;

‎src/libutil/util.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ void deletePath(const Path & path)
372372

373373
void deletePath(const Path & path, unsigned long long & bytesFreed)
374374
{
375-
Activity act(*logger, lvlDebug, format("recursively deleting path ‘%1%’") % path);
375+
//Activity act(*logger, lvlDebug, format("recursively deleting path ‘%1%’") % path);
376376
bytesFreed = 0;
377377
_deletePath(path, bytesFreed);
378378
}

‎src/libutil/util.hh

+2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ void ignoreException();
364364
#define ANSI_NORMAL "\e[0m"
365365
#define ANSI_BOLD "\e[1m"
366366
#define ANSI_RED "\e[31;1m"
367+
#define ANSI_GREEN "\e[32;1m"
368+
#define ANSI_BLUE "\e[34;1m"
367369

368370

369371
/* Filter out ANSI escape codes from the given string. If ‘nixOnly’ is

‎src/nix-channel/nix-channel.cc

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ static void update(const StringSet & channelNames)
112112
// The URL doesn't unpack directly, so let's try treating it like a full channel folder with files in it
113113
// Check if the channel advertises a binary cache.
114114
DownloadRequest request(url + "/binary-cache-url");
115-
request.showProgress = DownloadRequest::no;
116115
try {
117116
auto dlRes = dl->download(request);
118117
extraAttrs = "binaryCacheURL = \"" + *dlRes.data + "\";";

‎src/nix-daemon/nix-daemon.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ class TunnelLogger : public Logger
8282
defaultLogger->log(lvl, fs);
8383
}
8484

85-
void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) override
86-
{
87-
log(lvl, fs);
88-
}
89-
90-
void stopActivity(Activity & activity) override
85+
void event(const Event & ev) override
9186
{
9287
}
9388
};

‎src/nix-env/nix-env.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
989989
try {
990990
if (i.hasFailed()) continue;
991991

992-
Activity act(*logger, lvlDebug, format("outputting query result ‘%1%’") % i.attrPath);
992+
//Activity act(*logger, lvlDebug, format("outputting query result ‘%1%’") % i.attrPath);
993993

994994
if (globals.prebuiltOnly &&
995995
validPaths.find(i.queryOutPath()) == validPaths.end() &&

‎src/nix-instantiate/nix-instantiate.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace nix;
1919

2020
static Expr * parseStdin(EvalState & state)
2121
{
22-
Activity act(*logger, lvlTalkative, format("parsing standard input"));
22+
//Activity act(*logger, lvlTalkative, format("parsing standard input"));
2323
return state.parseExprFromString(drainFD(0), absPath("."));
2424
}
2525

‎src/nix/installables.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ PathSet InstallablesCommand::buildInstallables(ref<Store> store, bool dryRun)
223223
buildables.insert(b.begin(), b.end());
224224
}
225225

226-
printMissing(store, buildables);
227-
228-
if (!dryRun)
226+
if (dryRun)
227+
printMissing(store, buildables);
228+
else
229229
store->buildPaths(buildables);
230230

231231
PathSet outPaths;

‎src/nix/main.cc

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
2727

2828
void mainWrapped(int argc, char * * argv)
2929
{
30+
verbosity = lvlError;
3031
settings.verboseBuild = false;
3132

3233
initNix();

‎src/nix/progress-bar.cc

+198-53
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "progress-bar.hh"
22
#include "util.hh"
33
#include "sync.hh"
4+
#include "store-api.hh"
45

56
#include <map>
7+
#include <atomic>
8+
9+
#include <sys/ioctl.h>
610

711
namespace nix {
812

@@ -12,31 +16,47 @@ class ProgressBar : public Logger
1216

1317
struct ActInfo
1418
{
15-
Activity * activity;
16-
Verbosity lvl;
17-
std::string s;
19+
std::string s, s2;
1820
};
1921

20-
struct Progress
22+
struct DownloadInfo
2123
{
22-
uint64_t expected = 0, progress = 0;
24+
std::string uri;
25+
uint64_t current = 0;
26+
uint64_t expected = 0;
2327
};
2428

2529
struct State
2630
{
31+
std::map<Activity::t, Path> builds;
32+
std::set<Activity::t> runningBuilds;
33+
uint64_t succeededBuilds = 0;
34+
uint64_t failedBuilds = 0;
35+
std::map<Activity::t, Path> substitutions;
36+
std::set<Activity::t> runningSubstitutions;
37+
uint64_t succeededSubstitutions = 0;
38+
uint64_t downloadedBytes = 0; // finished downloads
39+
std::map<Activity::t, DownloadInfo> downloads;
2740
std::list<ActInfo> activities;
28-
std::map<Activity *, std::list<ActInfo>::iterator> its;
29-
std::map<std::string, Progress> progress;
41+
std::map<Activity::t, std::list<ActInfo>::iterator> its;
3042
};
3143

3244
Sync<State> state_;
3345

46+
int width = 0;
47+
3448
public:
3549

50+
ProgressBar()
51+
{
52+
struct winsize ws;
53+
if (ioctl(1, TIOCGWINSZ, &ws) == 0)
54+
width = ws.ws_col;
55+
}
56+
3657
~ProgressBar()
3758
{
3859
auto state(state_.lock());
39-
assert(state->activities.empty());
4060
writeToStderr("\r\e[K");
4161
}
4262

@@ -52,52 +72,36 @@ class ProgressBar : public Logger
5272
update(state);
5373
}
5474

55-
void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) override
56-
{
57-
if (lvl > verbosity) return;
58-
auto state(state_.lock());
59-
state->activities.emplace_back(ActInfo{&activity, lvl, fs.s});
60-
state->its.emplace(&activity, std::prev(state->activities.end()));
61-
update(*state);
62-
}
63-
64-
void stopActivity(Activity & activity) override
65-
{
66-
auto state(state_.lock());
67-
auto i = state->its.find(&activity);
68-
if (i == state->its.end()) return;
69-
state->activities.erase(i->second);
70-
state->its.erase(i);
71-
update(*state);
72-
}
73-
74-
void setExpected(const std::string & label, uint64_t value) override
75+
void createActivity(State & state, Activity::t activity, const std::string & s)
7576
{
76-
auto state(state_.lock());
77-
state->progress[label].expected = value;
78-
}
79-
80-
void setProgress(const std::string & label, uint64_t value) override
81-
{
82-
auto state(state_.lock());
83-
state->progress[label].progress = value;
77+
state.activities.emplace_back(ActInfo{s});
78+
state.its.emplace(activity, std::prev(state.activities.end()));
8479
}
8580

86-
void incExpected(const std::string & label, uint64_t value) override
81+
void deleteActivity(State & state, Activity::t activity)
8782
{
88-
auto state(state_.lock());
89-
state->progress[label].expected += value;
83+
auto i = state.its.find(activity);
84+
if (i != state.its.end()) {
85+
state.activities.erase(i->second);
86+
state.its.erase(i);
87+
}
9088
}
9189

92-
void incProgress(const std::string & label, uint64_t value) override
90+
void updateActivity(State & state, Activity::t activity, const std::string & s2)
9391
{
94-
auto state(state_.lock());
95-
state->progress[label].progress += value;
92+
auto i = state.its.find(activity);
93+
assert(i != state.its.end());
94+
ActInfo info = *i->second;
95+
state.activities.erase(i->second);
96+
info.s2 = s2;
97+
state.activities.emplace_back(info);
98+
i->second = std::prev(state.activities.end());
9699
}
97100

98101
void update()
99102
{
100103
auto state(state_.lock());
104+
update(*state);
101105
}
102106

103107
void update(State & state)
@@ -113,28 +117,169 @@ class ProgressBar : public Logger
113117

114118
if (!state.activities.empty()) {
115119
if (!status.empty()) line += " ";
116-
line += state.activities.rbegin()->s;
120+
auto i = state.activities.rbegin();
121+
line += i->s;
122+
if (!i->s2.empty()) {
123+
line += ": ";
124+
line += i->s2;
125+
}
117126
}
118127

119128
line += "\e[K";
120-
writeToStderr(line);
129+
writeToStderr(std::string(line, 0, width - 1));
121130
}
122131

123132
std::string getStatus(State & state)
124133
{
125134
std::string res;
126-
for (auto & p : state.progress)
127-
if (p.second.expected || p.second.progress) {
128-
if (!res.empty()) res += ", ";
129-
res += std::to_string(p.second.progress);
130-
if (p.second.expected) {
131-
res += "/";
132-
res += std::to_string(p.second.expected);
133-
}
134-
res += " "; res += p.first;
135+
136+
if (state.failedBuilds) {
137+
if (!res.empty()) res += ", ";
138+
res += fmt(ANSI_RED "%d failed" ANSI_NORMAL, state.failedBuilds);
139+
}
140+
141+
if (!state.builds.empty() || state.succeededBuilds)
142+
{
143+
if (!res.empty()) res += ", ";
144+
if (!state.runningBuilds.empty())
145+
res += fmt(ANSI_BLUE "%d" "/" ANSI_NORMAL, state.runningBuilds.size());
146+
res += fmt(ANSI_GREEN "%d/%d built" ANSI_NORMAL,
147+
state.succeededBuilds, state.succeededBuilds + state.builds.size());
148+
}
149+
150+
if (!state.substitutions.empty() || state.succeededSubstitutions) {
151+
if (!res.empty()) res += ", ";
152+
if (!state.runningSubstitutions.empty())
153+
res += fmt(ANSI_BLUE "%d" "/" ANSI_NORMAL, state.runningSubstitutions.size());
154+
res += fmt(ANSI_GREEN "%d/%d fetched" ANSI_NORMAL,
155+
state.succeededSubstitutions,
156+
state.succeededSubstitutions + state.substitutions.size());
157+
}
158+
159+
if (!state.downloads.empty() || state.downloadedBytes) {
160+
if (!res.empty()) res += ", ";
161+
uint64_t expected = state.downloadedBytes, current = state.downloadedBytes;
162+
for (auto & i : state.downloads) {
163+
expected += i.second.expected;
164+
current += i.second.current;
135165
}
166+
res += fmt("%1$.0f/%2$.0f KiB", current / 1024.0, expected / 1024.0);
167+
}
168+
136169
return res;
137170
}
171+
172+
void event(const Event & ev) override
173+
{
174+
if (ev.type == evBuildCreated) {
175+
auto state(state_.lock());
176+
state->builds[ev.getI(0)] = ev.getS(1);
177+
update(*state);
178+
}
179+
180+
if (ev.type == evBuildStarted) {
181+
auto state(state_.lock());
182+
Activity::t act = ev.getI(0);
183+
state->runningBuilds.insert(act);
184+
auto name = storePathToName(state->builds[act]);
185+
if (hasSuffix(name, ".drv"))
186+
name.resize(name.size() - 4);
187+
createActivity(*state, act, fmt("building " ANSI_BOLD "%s" ANSI_NORMAL, name));
188+
update(*state);
189+
}
190+
191+
if (ev.type == evBuildFinished) {
192+
auto state(state_.lock());
193+
Activity::t act = ev.getI(0);
194+
if (ev.getI(1)) {
195+
if (state->runningBuilds.count(act))
196+
state->succeededBuilds++;
197+
} else
198+
state->failedBuilds++;
199+
state->runningBuilds.erase(act);
200+
state->builds.erase(act);
201+
deleteActivity(*state, act);
202+
update(*state);
203+
}
204+
205+
if (ev.type == evBuildOutput) {
206+
auto state(state_.lock());
207+
Activity::t act = ev.getI(0);
208+
assert(state->runningBuilds.count(act));
209+
updateActivity(*state, act, ev.getS(1));
210+
update(*state);
211+
}
212+
213+
if (ev.type == evSubstitutionCreated) {
214+
auto state(state_.lock());
215+
state->substitutions[ev.getI(0)] = ev.getS(1);
216+
update(*state);
217+
}
218+
219+
if (ev.type == evSubstitutionStarted) {
220+
auto state(state_.lock());
221+
Activity::t act = ev.getI(0);
222+
state->runningSubstitutions.insert(act);
223+
auto name = storePathToName(state->substitutions[act]);
224+
createActivity(*state, act, fmt("fetching " ANSI_BOLD "%s" ANSI_NORMAL, name));
225+
update(*state);
226+
}
227+
228+
if (ev.type == evSubstitutionFinished) {
229+
auto state(state_.lock());
230+
Activity::t act = ev.getI(0);
231+
if (ev.getI(1)) {
232+
if (state->runningSubstitutions.count(act))
233+
state->succeededSubstitutions++;
234+
}
235+
state->runningSubstitutions.erase(act);
236+
state->substitutions.erase(act);
237+
deleteActivity(*state, act);
238+
update(*state);
239+
}
240+
241+
if (ev.type == evDownloadCreated) {
242+
auto state(state_.lock());
243+
Activity::t act = ev.getI(0);
244+
std::string uri = ev.getS(1);
245+
state->downloads.emplace(act, DownloadInfo{uri});
246+
if (state->runningSubstitutions.empty()) // FIXME: hack
247+
createActivity(*state, act, fmt("downloading " ANSI_BOLD "%s" ANSI_NORMAL "", uri));
248+
update(*state);
249+
}
250+
251+
if (ev.type == evDownloadProgress) {
252+
auto state(state_.lock());
253+
Activity::t act = ev.getI(0);
254+
auto i = state->downloads.find(act);
255+
assert(i != state->downloads.end());
256+
i->second.expected = ev.getI(1);
257+
i->second.current = ev.getI(2);
258+
update(*state);
259+
}
260+
261+
if (ev.type == evDownloadSucceeded) {
262+
auto state(state_.lock());
263+
Activity::t act = ev.getI(0);
264+
auto i = state->downloads.find(act);
265+
assert(i != state->downloads.end());
266+
state->downloadedBytes += ev.getI(1);
267+
state->downloads.erase(i);
268+
deleteActivity(*state, act);
269+
update(*state);
270+
}
271+
272+
if (ev.type == evDownloadDestroyed) {
273+
auto state(state_.lock());
274+
Activity::t act = ev.getI(0);
275+
auto i = state->downloads.find(act);
276+
if (i != state->downloads.end()) {
277+
state->downloads.erase(i);
278+
deleteActivity(*state, act);
279+
update(*state);
280+
}
281+
}
282+
}
138283
};
139284

140285
StartProgressBar::StartProgressBar()

‎src/nix/sigs.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ struct CmdCopySigs : StorePathsCommand
4242
std::string doneLabel = "done";
4343
std::atomic<size_t> added{0};
4444

45-
logger->setExpected(doneLabel, storePaths.size());
45+
//logger->setExpected(doneLabel, storePaths.size());
4646

4747
auto doPath = [&](const Path & storePath) {
48-
Activity act(*logger, lvlInfo, format("getting signatures for ‘%s’") % storePath);
48+
//Activity act(*logger, lvlInfo, format("getting signatures for ‘%s’") % storePath);
4949

5050
checkInterrupt();
5151

@@ -76,7 +76,7 @@ struct CmdCopySigs : StorePathsCommand
7676
added += newSigs.size();
7777
}
7878

79-
logger->incProgress(doneLabel);
79+
//logger->incProgress(doneLabel);
8080
};
8181

8282
for (auto & storePath : storePaths)

‎src/nix/verify.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ struct CmdVerify : StorePathsCommand
6565
std::string untrustedLabel("untrusted");
6666
std::string corruptedLabel("corrupted");
6767
std::string failedLabel("failed");
68-
logger->setExpected(doneLabel, storePaths.size());
68+
//logger->setExpected(doneLabel, storePaths.size());
6969

7070
ThreadPool pool;
7171

7272
auto doPath = [&](const Path & storePath) {
7373
try {
7474
checkInterrupt();
7575

76-
Activity act(*logger, lvlInfo, format("checking ‘%s’") % storePath);
76+
//Activity act(*logger, lvlInfo, format("checking ‘%s’") % storePath);
7777

7878
auto info = store->queryPathInfo(storePath);
7979

@@ -85,7 +85,7 @@ struct CmdVerify : StorePathsCommand
8585
auto hash = sink.finish();
8686

8787
if (hash.first != info->narHash) {
88-
logger->incProgress(corruptedLabel);
88+
//logger->incProgress(corruptedLabel);
8989
corrupted = 1;
9090
printError(
9191
format("path ‘%s’ was modified! expected hash ‘%s’, got ‘%s’")
@@ -137,19 +137,19 @@ struct CmdVerify : StorePathsCommand
137137
}
138138

139139
if (!good) {
140-
logger->incProgress(untrustedLabel);
140+
//logger->incProgress(untrustedLabel);
141141
untrusted++;
142142
printError(format("path ‘%s’ is untrusted") % info->path);
143143
}
144144

145145
}
146146

147-
logger->incProgress(doneLabel);
147+
//logger->incProgress(doneLabel);
148148
done++;
149149

150150
} catch (Error & e) {
151151
printError(format(ANSI_RED "error:" ANSI_NORMAL " %s") % e.what());
152-
logger->incProgress(failedLabel);
152+
//logger->incProgress(failedLabel);
153153
failed++;
154154
}
155155
};

0 commit comments

Comments
 (0)
Please sign in to comment.