Progress indicator: Cleanup

This commit is contained in:
Eelco Dolstra 2017-08-16 16:38:23 +02:00
parent dff12b38f9
commit 40bffe0a43
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
7 changed files with 105 additions and 151 deletions

View file

@ -335,8 +335,8 @@ public:
{ {
actDerivations.progress(doneBuilds, expectedBuilds + doneBuilds, runningBuilds, failedBuilds); actDerivations.progress(doneBuilds, expectedBuilds + doneBuilds, runningBuilds, failedBuilds);
actSubstitutions.progress(doneSubstitutions, expectedSubstitutions + doneSubstitutions, runningSubstitutions, failedSubstitutions); actSubstitutions.progress(doneSubstitutions, expectedSubstitutions + doneSubstitutions, runningSubstitutions, failedSubstitutions);
logger->event(evSetExpected, act, actDownload, expectedDownloadSize + doneDownloadSize); act.setExpected(actDownload, expectedDownloadSize + doneDownloadSize);
logger->event(evSetExpected, act, actCopyPath, expectedNarSize + doneNarSize); act.setExpected(actCopyPath, expectedNarSize + doneNarSize);
} }
}; };
@ -1386,7 +1386,7 @@ void DerivationGoal::tryToBuild()
bool buildLocally = buildMode != bmNormal || drv->willBuildLocally(); bool buildLocally = buildMode != bmNormal || drv->willBuildLocally();
auto started = [&]() { auto started = [&]() {
act = std::make_unique<Activity>(actBuild, fmt("building '%s'", drvPath)); act = std::make_unique<Activity>(*logger, actBuild, fmt("building '%s'", drvPath));
mcRunningBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.runningBuilds); mcRunningBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.runningBuilds);
worker.updateProgress(); worker.updateProgress();
}; };
@ -3257,7 +3257,7 @@ void DerivationGoal::flushLine()
logTail.push_back(currentLogLine); logTail.push_back(currentLogLine);
if (logTail.size() > settings.logLines) logTail.pop_front(); if (logTail.size() > settings.logLines) logTail.pop_front();
} }
logger->event(evBuildOutput, *act, currentLogLine); act->progress(currentLogLine);
currentLogLine = ""; currentLogLine = "";
currentLogLinePos = 0; currentLogLinePos = 0;
} }
@ -3647,9 +3647,9 @@ static bool working = false;
Worker::Worker(LocalStore & store) Worker::Worker(LocalStore & store)
: act(actRealise) : act(*logger, actRealise)
, actDerivations(actBuilds) , actDerivations(*logger, actBuilds)
, actSubstitutions(actCopyPaths) , actSubstitutions(*logger, actCopyPaths)
, store(store) , store(store)
{ {
/* Debugging: prevent recursive workers. */ /* Debugging: prevent recursive workers. */

View file

@ -85,7 +85,7 @@ struct CurlDownloader : public Downloader
DownloadItem(CurlDownloader & downloader, const DownloadRequest & request) DownloadItem(CurlDownloader & downloader, const DownloadRequest & request)
: downloader(downloader) : downloader(downloader)
, request(request) , request(request)
, act(actDownload, fmt("downloading '%s'", request.uri)) , act(*logger, actDownload, fmt("downloading '%s'", request.uri))
{ {
if (!request.expectedETag.empty()) if (!request.expectedETag.empty())
requestHeaders = curl_slist_append(requestHeaders, ("If-None-Match: " + request.expectedETag).c_str()); requestHeaders = curl_slist_append(requestHeaders, ("If-None-Match: " + request.expectedETag).c_str());

View file

@ -565,7 +565,7 @@ void Store::buildPaths(const PathSet & paths, BuildMode buildMode)
void copyStorePath(ref<Store> srcStore, ref<Store> dstStore, void copyStorePath(ref<Store> srcStore, ref<Store> dstStore,
const Path & storePath, RepairFlag repair, CheckSigsFlag checkSigs) const Path & storePath, RepairFlag repair, CheckSigsFlag checkSigs)
{ {
Activity act(actCopyPath, fmt("copying path '%s'", storePath)); Activity act(*logger, actCopyPath, fmt("copying path '%s'", storePath));
auto info = srcStore->queryPathInfo(storePath); auto info = srcStore->queryPathInfo(storePath);
@ -621,7 +621,7 @@ void copyPaths(ref<Store> srcStore, ref<Store> dstStore, const PathSet & storePa
for (auto & path : storePaths) for (auto & path : storePaths)
if (!valid.count(path)) missing.insert(path); if (!valid.count(path)) missing.insert(path);
Activity act(actCopyPaths, fmt("copying %d paths", missing.size())); Activity act(*logger, actCopyPaths, fmt("copying %d paths", missing.size()));
std::atomic<size_t> nrDone{0}; std::atomic<size_t> nrDone{0};
std::atomic<uint64_t> bytesExpected{0}; std::atomic<uint64_t> bytesExpected{0};
@ -646,7 +646,7 @@ void copyPaths(ref<Store> srcStore, ref<Store> dstStore, const PathSet & storePa
auto info = srcStore->queryPathInfo(storePath); auto info = srcStore->queryPathInfo(storePath);
bytesExpected += info->narSize; bytesExpected += info->narSize;
logger->event(evSetExpected, act, actCopyPath, bytesExpected); act.setExpected(actCopyPath, bytesExpected);
return info->references; return info->references;
}, },
@ -655,7 +655,6 @@ void copyPaths(ref<Store> srcStore, ref<Store> dstStore, const PathSet & storePa
checkInterrupt(); checkInterrupt();
if (!dstStore->isValidPath(storePath)) { if (!dstStore->isValidPath(storePath)) {
printInfo("copying '%s'...", storePath);
MaintainCount<decltype(nrRunning)> mc(nrRunning); MaintainCount<decltype(nrRunning)> mc(nrRunning);
showProgress(); showProgress();
copyStorePath(srcStore, dstStore, storePath, repair, checkSigs); copyStorePath(srcStore, dstStore, storePath, repair, checkSigs);

View file

@ -43,10 +43,6 @@ public:
writeToStderr(prefix + (tty ? fs.s : filterANSIEscapes(fs.s)) + "\n"); writeToStderr(prefix + (tty ? fs.s : filterANSIEscapes(fs.s)) + "\n");
} }
void event(const Event & ev) override
{
}
}; };
Verbosity verbosity = lvlInfo; Verbosity verbosity = lvlInfo;
@ -78,22 +74,10 @@ Logger * makeDefaultLogger()
std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32}; std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
Activity::Activity() : id(nextId++) { }; Activity::Activity(Logger & logger, ActivityType type, const std::string & s)
: logger(logger), id(nextId++)
Activity::Activity(ActivityType type, std::string msg)
: Activity()
{ {
logger->event(evStartActivity, id, type, msg); logger.startActivity(id, type, s);
}
Activity::~Activity()
{
logger->event(evStopActivity, id);
}
void Activity::progress(uint64_t done, uint64_t expected, uint64_t running, uint64_t failed) const
{
logger->event(evProgress, id, done, expected, running, failed);
} }
} }

View file

@ -23,63 +23,7 @@ typedef enum {
actBuild = 105, actBuild = 105,
} ActivityType; } ActivityType;
class Activity typedef uint64_t ActivityId;
{
public:
typedef uint64_t t;
const t id;
Activity();
Activity(const Activity & act) : id(act.id) { };
Activity(uint64_t id) : id(id) { };
Activity(ActivityType type, std::string msg = "");
~Activity();
void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) const;
};
typedef enum {
evBuildOutput = 2,
evStartActivity = 1000,
evStopActivity = 1001,
evProgress = 1002,
evSetExpected = 1003,
} EventType;
struct Event
{
struct Field
{
// FIXME: use std::variant.
enum { tInt, tString } type;
uint64_t i = 0;
std::string s;
Field(const std::string & s) : type(tString), s(s) { }
Field(const char * s) : type(tString), s(s) { }
Field(const uint64_t & i) : type(tInt), i(i) { }
Field(const Activity & act) : type(tInt), i(act.id) { }
};
typedef std::vector<Field> Fields;
EventType type;
Fields fields;
std::string getS(size_t n) const
{
assert(n < fields.size());
assert(fields[n].type == Field::tString);
return fields[n].s;
}
uint64_t getI(size_t n) const
{
assert(n < fields.size());
assert(fields[n].type == Field::tInt);
return fields[n].i;
}
};
class Logger class Logger
{ {
@ -98,16 +42,38 @@ public:
virtual void warn(const std::string & msg); virtual void warn(const std::string & msg);
template<typename... Args> virtual void startActivity(ActivityId act, ActivityType type, const std::string & s) { };
void event(EventType type, const Args & ... args)
{
Event ev;
ev.type = type;
nop{(ev.fields.emplace_back(Event::Field(args)), 1)...};
event(ev);
}
virtual void event(const Event & ev) = 0; virtual void stopActivity(ActivityId act) { };
virtual void progress(ActivityId act, uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) { };
virtual void progress(ActivityId act, const std::string & s) { };
virtual void setExpected(ActivityId act, ActivityType type, uint64_t expected) { };
};
struct Activity
{
Logger & logger;
const ActivityId id;
Activity(Logger & logger, ActivityType type, const std::string & s = "");
~Activity()
{ logger.stopActivity(id); }
void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) const
{ logger.progress(id, done, expected, running, failed); }
void progress(const std::string & s) const
{ logger.progress(id, s); }
void setExpected(ActivityType type2, uint64_t expected) const
{ logger.setExpected(id, type2, expected); }
friend class Logger;
}; };
extern Logger * logger; extern Logger * logger;

View file

@ -81,10 +81,6 @@ class TunnelLogger : public Logger
} else } else
defaultLogger->log(lvl, fs); defaultLogger->log(lvl, fs);
} }
void event(const Event & ev) override
{
}
}; };

View file

@ -29,7 +29,7 @@ private:
struct ActivitiesByType struct ActivitiesByType
{ {
std::map<Activity::t, std::list<ActInfo>::iterator> its; std::map<ActivityId, std::list<ActInfo>::iterator> its;
uint64_t done = 0; uint64_t done = 0;
uint64_t expected = 0; uint64_t expected = 0;
uint64_t failed = 0; uint64_t failed = 0;
@ -38,7 +38,7 @@ private:
struct State struct State
{ {
std::list<ActInfo> activities; std::list<ActInfo> activities;
std::map<Activity::t, std::list<ActInfo>::iterator> its; std::map<ActivityId, std::list<ActInfo>::iterator> its;
std::map<ActivityType, ActivitiesByType> activitiesByType; std::map<ActivityType, ActivitiesByType> activitiesByType;
}; };
@ -77,7 +77,61 @@ public:
update(state); update(state);
} }
void createActivity(State & state, Activity::t activity, const std::string & s, ActivityType type = actUnknown) void startActivity(ActivityId act, ActivityType type, const std::string & s) override
{
auto state(state_.lock());
createActivity(*state, act, s, type);
update(*state);
}
void stopActivity(ActivityId act) override
{
auto state(state_.lock());
deleteActivity(*state, act);
update(*state);
}
void progress(ActivityId act, uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) override
{
auto state(state_.lock());
auto i = state->its.find(act);
assert(i != state->its.end());
ActInfo & actInfo = *i->second;
actInfo.done = done;
actInfo.expected = expected;
actInfo.running = running;
actInfo.failed = failed;
update(*state);
}
void progress(ActivityId act, const std::string & s) override
{
auto state(state_.lock());
auto s2 = trim(s);
if (!s2.empty()) {
updateActivity(*state, act, s2);
update(*state);
}
}
void setExpected(ActivityId act, ActivityType type, uint64_t expected) override
{
auto state(state_.lock());
auto i = state->its.find(act);
assert(i != state->its.end());
ActInfo & actInfo = *i->second;
auto & j = actInfo.expectedByType[type];
state->activitiesByType[type].expected -= j;
j = expected;
state->activitiesByType[type].expected += j;
update(*state);
}
void createActivity(State & state, ActivityId activity, const std::string & s, ActivityType type = actUnknown)
{ {
state.activities.emplace_back(ActInfo{s, "", type}); state.activities.emplace_back(ActInfo{s, "", type});
auto i = std::prev(state.activities.end()); auto i = std::prev(state.activities.end());
@ -85,7 +139,7 @@ public:
state.activitiesByType[type].its.emplace(activity, i); state.activitiesByType[type].its.emplace(activity, i);
} }
void deleteActivity(State & state, Activity::t activity) void deleteActivity(State & state, ActivityId activity)
{ {
auto i = state.its.find(activity); auto i = state.its.find(activity);
if (i != state.its.end()) { if (i != state.its.end()) {
@ -102,7 +156,7 @@ public:
} }
} }
void updateActivity(State & state, Activity::t activity, const std::string & s2) void updateActivity(State & state, ActivityId activity, const std::string & s2)
{ {
auto i = state.its.find(activity); auto i = state.its.find(activity);
assert(i != state.its.end()); assert(i != state.its.end());
@ -210,51 +264,6 @@ public:
return res; return res;
} }
void event(const Event & ev) override
{
auto state(state_.lock());
if (ev.type == evStartActivity) {
Activity::t act = ev.getI(0);
createActivity(*state, act, ev.getS(2), (ActivityType) ev.getI(1));
}
if (ev.type == evStopActivity) {
Activity::t act = ev.getI(0);
deleteActivity(*state, act);
}
if (ev.type == evProgress) {
auto i = state->its.find(ev.getI(0));
assert(i != state->its.end());
ActInfo & actInfo = *i->second;
actInfo.done = ev.getI(1);
actInfo.expected = ev.getI(2);
actInfo.running = ev.getI(3);
actInfo.failed = ev.getI(4);
}
if (ev.type == evSetExpected) {
auto i = state->its.find(ev.getI(0));
assert(i != state->its.end());
ActInfo & actInfo = *i->second;
auto type = (ActivityType) ev.getI(1);
auto & j = actInfo.expectedByType[type];
state->activitiesByType[type].expected -= j;
j = ev.getI(2);
state->activitiesByType[type].expected += j;
}
if (ev.type == evBuildOutput) {
Activity::t act = ev.getI(0);
auto s = trim(ev.getS(1));
if (!s.empty())
updateActivity(*state, act, s);
}
update(*state);
}
}; };
StartProgressBar::StartProgressBar() StartProgressBar::StartProgressBar()