2016-03-29 15:29:50 +03:00
|
|
|
#include "progress-bar.hh"
|
2016-04-25 16:26:07 +03:00
|
|
|
#include "util.hh"
|
|
|
|
#include "sync.hh"
|
2017-05-16 17:09:57 +03:00
|
|
|
#include "store-api.hh"
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
#include <map>
|
2017-05-16 17:09:57 +03:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2017-08-14 20:00:03 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
2016-03-29 15:29:50 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
static std::string getS(const std::vector<Logger::Field> & fields, size_t n)
|
|
|
|
{
|
|
|
|
assert(n < fields.size());
|
|
|
|
assert(fields[n].type == Logger::Field::tString);
|
|
|
|
return fields[n].s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t getI(const std::vector<Logger::Field> & fields, size_t n)
|
|
|
|
{
|
|
|
|
assert(n < fields.size());
|
|
|
|
assert(fields[n].type == Logger::Field::tInt);
|
|
|
|
return fields[n].i;
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
class ProgressBar : public Logger
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
2016-04-25 16:26:07 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
struct ActInfo
|
|
|
|
{
|
2017-05-16 17:09:57 +03:00
|
|
|
std::string s, s2;
|
2017-08-14 20:00:03 +03:00
|
|
|
ActivityType type = actUnknown;
|
|
|
|
uint64_t done = 0;
|
|
|
|
uint64_t expected = 0;
|
2017-08-14 23:42:17 +03:00
|
|
|
uint64_t running = 0;
|
2017-08-15 16:31:59 +03:00
|
|
|
uint64_t failed = 0;
|
2017-08-14 20:00:03 +03:00
|
|
|
std::map<ActivityType, uint64_t> expectedByType;
|
2016-03-29 15:29:50 +03:00
|
|
|
};
|
|
|
|
|
2017-08-14 20:00:03 +03:00
|
|
|
struct ActivitiesByType
|
|
|
|
{
|
2017-08-16 17:38:23 +03:00
|
|
|
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
2017-08-14 20:00:03 +03:00
|
|
|
uint64_t done = 0;
|
|
|
|
uint64_t expected = 0;
|
2017-08-15 16:31:59 +03:00
|
|
|
uint64_t failed = 0;
|
2017-08-14 20:00:03 +03:00
|
|
|
};
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
std::list<ActInfo> activities;
|
2017-08-16 17:38:23 +03:00
|
|
|
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
2017-08-14 20:00:03 +03:00
|
|
|
|
|
|
|
std::map<ActivityType, ActivitiesByType> activitiesByType;
|
2017-08-16 18:32:18 +03:00
|
|
|
|
|
|
|
uint64_t filesLinked = 0, bytesLinked = 0;
|
2016-04-25 16:26:07 +03:00
|
|
|
};
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
Sync<State> state_;
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
int width = 0;
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
public:
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
ProgressBar()
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws) == 0)
|
|
|
|
width = ws.ws_col;
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
~ProgressBar()
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-08-15 16:31:59 +03:00
|
|
|
std::string status = getStatus(*state);
|
2016-04-25 16:26:07 +03:00
|
|
|
writeToStderr("\r\e[K");
|
2017-08-15 16:31:59 +03:00
|
|
|
if (status != "")
|
|
|
|
writeToStderr("[" + status + "]\n");
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
void log(Verbosity lvl, const FormatOrString & fs) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
log(*state, lvl, fs.s);
|
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
void log(State & state, Verbosity lvl, const std::string & s)
|
|
|
|
{
|
|
|
|
writeToStderr("\r\e[K" + s + "\n");
|
|
|
|
update(state);
|
|
|
|
}
|
|
|
|
|
2017-08-16 17:38:23 +03:00
|
|
|
void startActivity(ActivityId act, ActivityType type, const std::string & s) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-08-16 18:32:18 +03:00
|
|
|
|
|
|
|
state->activities.emplace_back(ActInfo{s, "", type});
|
|
|
|
auto i = std::prev(state->activities.end());
|
|
|
|
state->its.emplace(act, i);
|
|
|
|
state->activitiesByType[type].its.emplace(act, i);
|
|
|
|
|
2017-08-16 17:38:23 +03:00
|
|
|
update(*state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopActivity(ActivityId act) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-08-16 18:32:18 +03:00
|
|
|
|
|
|
|
auto i = state->its.find(act);
|
|
|
|
if (i != state->its.end()) {
|
|
|
|
auto & actByType = state->activitiesByType[i->second->type];
|
|
|
|
actByType.done += i->second->done;
|
|
|
|
actByType.failed += i->second->failed;
|
|
|
|
|
|
|
|
for (auto & j : i->second->expectedByType)
|
|
|
|
state->activitiesByType[j.first].expected -= j.second;
|
|
|
|
|
|
|
|
actByType.its.erase(act);
|
|
|
|
state->activities.erase(i->second);
|
|
|
|
state->its.erase(i);
|
|
|
|
}
|
|
|
|
|
2017-08-16 17:38:23 +03:00
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
void result(ActivityId act, ResultType type, const std::vector<Field> & fields) override
|
2016-04-25 16:26:07 +03:00
|
|
|
{
|
2017-08-16 18:32:18 +03:00
|
|
|
auto state(state_.lock());
|
2017-08-14 20:00:03 +03:00
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
if (type == resFileLinked) {
|
|
|
|
state->filesLinked++;
|
|
|
|
state->bytesLinked += getI(fields, 0);
|
|
|
|
update(*state);
|
2017-05-16 17:09:57 +03:00
|
|
|
}
|
2016-04-25 16:26:07 +03:00
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
else if (type == resBuildLogLine) {
|
|
|
|
auto s2 = trim(getS(fields, 0));
|
|
|
|
if (!s2.empty()) {
|
|
|
|
auto i = state->its.find(act);
|
|
|
|
assert(i != state->its.end());
|
|
|
|
ActInfo info = *i->second;
|
|
|
|
state->activities.erase(i->second);
|
|
|
|
info.s2 = s2;
|
|
|
|
state->activities.emplace_back(info);
|
|
|
|
i->second = std::prev(state->activities.end());
|
|
|
|
update(*state);
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void update()
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-05-16 17:09:57 +03:00
|
|
|
update(*state);
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void update(State & state)
|
|
|
|
{
|
|
|
|
std::string line = "\r";
|
|
|
|
|
|
|
|
std::string status = getStatus(state);
|
|
|
|
if (!status.empty()) {
|
|
|
|
line += '[';
|
|
|
|
line += status;
|
|
|
|
line += "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.activities.empty()) {
|
|
|
|
if (!status.empty()) line += " ";
|
2017-05-16 17:09:57 +03:00
|
|
|
auto i = state.activities.rbegin();
|
2017-08-14 21:14:55 +03:00
|
|
|
|
|
|
|
while (i != state.activities.rend() && i->s.empty() && i->s2.empty())
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (i != state.activities.rend()) {
|
|
|
|
line += i->s;
|
|
|
|
if (!i->s2.empty()) {
|
2017-08-15 16:31:59 +03:00
|
|
|
if (!i->s.empty()) line += ": ";
|
2017-08-14 21:14:55 +03:00
|
|
|
line += i->s2;
|
|
|
|
}
|
2017-05-16 17:09:57 +03:00
|
|
|
}
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
line += "\e[K";
|
2017-05-16 17:09:57 +03:00
|
|
|
writeToStderr(std::string(line, 0, width - 1));
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string getStatus(State & state)
|
|
|
|
{
|
2017-08-14 20:00:03 +03:00
|
|
|
auto MiB = 1024.0 * 1024.0;
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
std::string res;
|
2017-05-16 17:09:57 +03:00
|
|
|
|
2017-08-16 18:00:24 +03:00
|
|
|
auto renderActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
|
2017-08-14 21:14:55 +03:00
|
|
|
auto & act = state.activitiesByType[type];
|
2017-08-15 16:31:59 +03:00
|
|
|
uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed;
|
2017-08-14 21:14:55 +03:00
|
|
|
for (auto & j : act.its) {
|
|
|
|
done += j.second->done;
|
|
|
|
expected += j.second->expected;
|
2017-08-14 23:42:17 +03:00
|
|
|
running += j.second->running;
|
2017-08-15 16:31:59 +03:00
|
|
|
failed += j.second->failed;
|
2017-08-14 21:14:55 +03:00
|
|
|
}
|
2017-08-14 20:00:03 +03:00
|
|
|
|
2017-08-14 21:14:55 +03:00
|
|
|
expected = std::max(expected, act.expected);
|
2017-08-14 20:00:03 +03:00
|
|
|
|
2017-08-15 16:31:59 +03:00
|
|
|
std::string s;
|
|
|
|
|
|
|
|
if (running || done || expected || failed) {
|
|
|
|
if (running)
|
|
|
|
s = fmt(ANSI_BLUE + numberFmt + ANSI_NORMAL "/" ANSI_GREEN + numberFmt + ANSI_NORMAL "/" + numberFmt,
|
|
|
|
running / unit, done / unit, expected / unit);
|
|
|
|
else if (expected != done)
|
|
|
|
s = fmt(ANSI_GREEN + numberFmt + ANSI_NORMAL "/" + numberFmt,
|
|
|
|
done / unit, expected / unit);
|
|
|
|
else
|
|
|
|
s = fmt(done ? ANSI_GREEN + numberFmt + ANSI_NORMAL : numberFmt, done / unit);
|
|
|
|
s = fmt(itemFmt, s);
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
s += fmt(" (" ANSI_RED "%d failed" ANSI_NORMAL ")", failed / unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
2017-08-16 18:00:24 +03:00
|
|
|
auto showActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
|
2017-08-15 16:31:59 +03:00
|
|
|
auto s = renderActivity(type, itemFmt, numberFmt, unit);
|
|
|
|
if (s.empty()) return;
|
|
|
|
if (!res.empty()) res += ", ";
|
|
|
|
res += s;
|
2017-08-14 21:14:55 +03:00
|
|
|
};
|
|
|
|
|
2017-08-16 18:00:24 +03:00
|
|
|
showActivity(actBuilds, "%s built");
|
2017-08-15 16:31:59 +03:00
|
|
|
|
2017-08-16 18:00:24 +03:00
|
|
|
auto s1 = renderActivity(actCopyPaths, "%s copied");
|
2017-08-15 16:31:59 +03:00
|
|
|
auto s2 = renderActivity(actCopyPath, "%s MiB", "%.1f", MiB);
|
|
|
|
|
|
|
|
if (!s1.empty() || !s2.empty()) {
|
|
|
|
if (!res.empty()) res += ", ";
|
|
|
|
if (s1.empty()) res += "0 copied"; else res += s1;
|
|
|
|
if (!s2.empty()) { res += " ("; res += s2; res += ')'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
showActivity(actDownload, "%s MiB DL", "%.1f", MiB);
|
2017-08-14 20:00:03 +03:00
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
{
|
|
|
|
auto s = renderActivity(actOptimiseStore, "%s paths optimised");
|
|
|
|
if (s != "") {
|
|
|
|
s += fmt(", %.1f MiB / %d inodes freed", state.bytesLinked / MiB, state.filesLinked);
|
|
|
|
if (!res.empty()) res += ", ";
|
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 18:00:24 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
StartProgressBar::StartProgressBar()
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
2016-04-25 16:26:07 +03:00
|
|
|
if (isatty(STDERR_FILENO)) {
|
|
|
|
prev = logger;
|
|
|
|
logger = new ProgressBar();
|
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
StartProgressBar::~StartProgressBar()
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
2016-04-25 16:26:07 +03:00
|
|
|
if (prev) {
|
|
|
|
auto bar = logger;
|
|
|
|
logger = prev;
|
|
|
|
delete bar;
|
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|