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 {
|
|
|
|
|
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-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
|
|
|
|
{
|
|
|
|
std::map<Activity::t, std::list<ActInfo>::iterator> its;
|
|
|
|
uint64_t done = 0;
|
|
|
|
uint64_t expected = 0;
|
|
|
|
};
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
struct State
|
|
|
|
{
|
2017-05-16 17:09:57 +03:00
|
|
|
std::map<Activity::t, Path> builds;
|
|
|
|
std::set<Activity::t> runningBuilds;
|
|
|
|
uint64_t succeededBuilds = 0;
|
|
|
|
uint64_t failedBuilds = 0;
|
2017-08-14 16:28:16 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
std::list<ActInfo> activities;
|
2017-05-16 17:09:57 +03:00
|
|
|
std::map<Activity::t, std::list<ActInfo>::iterator> its;
|
2017-08-14 20:00:03 +03:00
|
|
|
|
|
|
|
std::map<ActivityType, ActivitiesByType> activitiesByType;
|
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());
|
|
|
|
writeToStderr("\r\e[K");
|
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-14 20:00:03 +03:00
|
|
|
void createActivity(State & state, Activity::t activity, const std::string & s, ActivityType type = actUnknown)
|
2016-04-25 16:26:07 +03:00
|
|
|
{
|
2017-08-14 20:00:03 +03:00
|
|
|
state.activities.emplace_back(ActInfo{s, "", type});
|
|
|
|
auto i = std::prev(state.activities.end());
|
|
|
|
state.its.emplace(activity, i);
|
|
|
|
state.activitiesByType[type].its.emplace(activity, i);
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
void deleteActivity(State & state, Activity::t activity)
|
2016-04-25 16:26:07 +03:00
|
|
|
{
|
2017-05-16 17:09:57 +03:00
|
|
|
auto i = state.its.find(activity);
|
|
|
|
if (i != state.its.end()) {
|
2017-08-14 20:00:03 +03:00
|
|
|
auto & act = state.activitiesByType[i->second->type];
|
|
|
|
act.done += i->second->done;
|
|
|
|
|
|
|
|
for (auto & j : i->second->expectedByType)
|
|
|
|
state.activitiesByType[j.first].expected -= j.second;
|
|
|
|
|
|
|
|
act.its.erase(activity);
|
2017-05-16 17:09:57 +03:00
|
|
|
state.activities.erase(i->second);
|
|
|
|
state.its.erase(i);
|
|
|
|
}
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
void updateActivity(State & state, Activity::t activity, const std::string & s2)
|
2016-04-25 16:26:07 +03:00
|
|
|
{
|
2017-05-16 17:09:57 +03:00
|
|
|
auto i = state.its.find(activity);
|
|
|
|
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());
|
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()) {
|
|
|
|
line += ": ";
|
|
|
|
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-14 21:14:55 +03:00
|
|
|
auto add = [&](const std::string & s) {
|
2017-05-16 17:09:57 +03:00
|
|
|
if (!res.empty()) res += ", ";
|
2017-08-14 21:14:55 +03:00
|
|
|
res += s;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (state.failedBuilds) {
|
|
|
|
add(fmt(ANSI_RED "%d failed" ANSI_NORMAL, state.failedBuilds));
|
2017-05-16 17:09:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.builds.empty() || state.succeededBuilds)
|
|
|
|
{
|
|
|
|
if (!res.empty()) res += ", ";
|
|
|
|
if (!state.runningBuilds.empty())
|
|
|
|
res += fmt(ANSI_BLUE "%d" "/" ANSI_NORMAL, state.runningBuilds.size());
|
|
|
|
res += fmt(ANSI_GREEN "%d/%d built" ANSI_NORMAL,
|
|
|
|
state.succeededBuilds, state.succeededBuilds + state.builds.size());
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:12:36 +03:00
|
|
|
auto showActivity = [&](ActivityType type, const std::string & f, double unit) {
|
2017-08-14 21:14:55 +03:00
|
|
|
auto & act = state.activitiesByType[type];
|
2017-08-14 23:42:17 +03:00
|
|
|
uint64_t done = act.done, expected = act.done, running = 0;
|
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-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-14 21:14:55 +03:00
|
|
|
if (done || expected)
|
2017-08-14 23:42:17 +03:00
|
|
|
add(fmt(f, done / unit, expected / unit, running));
|
2017-08-14 21:14:55 +03:00
|
|
|
};
|
|
|
|
|
2017-08-14 23:42:17 +03:00
|
|
|
showActivity(actCopyPaths, ANSI_BLUE "%3$d" ANSI_NORMAL "/" ANSI_GREEN "%1$d" ANSI_NORMAL "/%2$d copied", 1);
|
2017-08-14 23:12:36 +03:00
|
|
|
showActivity(actDownload, "%1$.1f/%2$.1f MiB DL", MiB);
|
|
|
|
showActivity(actCopyPath, "%1$.1f/%2$.1f MiB copied", MiB);
|
2017-08-14 20:00:03 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
return res;
|
|
|
|
}
|
2017-05-16 17:09:57 +03:00
|
|
|
|
|
|
|
void event(const Event & ev) override
|
|
|
|
{
|
2017-08-14 20:00:03 +03:00
|
|
|
auto state(state_.lock());
|
|
|
|
|
2017-08-14 16:28:16 +03:00
|
|
|
if (ev.type == evStartActivity) {
|
|
|
|
Activity::t act = ev.getI(0);
|
2017-08-14 20:00:03 +03:00
|
|
|
createActivity(*state, act, ev.getS(2), (ActivityType) ev.getI(1));
|
2017-08-14 16:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.type == evStopActivity) {
|
|
|
|
Activity::t act = ev.getI(0);
|
|
|
|
deleteActivity(*state, act);
|
2017-08-14 20:00:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-08-14 23:42:17 +03:00
|
|
|
actInfo.running = ev.getI(3);
|
2017-08-14 20:00:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-08-14 16:28:16 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
if (ev.type == evBuildCreated) {
|
|
|
|
state->builds[ev.getI(0)] = ev.getS(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.type == evBuildStarted) {
|
|
|
|
Activity::t act = ev.getI(0);
|
|
|
|
state->runningBuilds.insert(act);
|
|
|
|
auto name = storePathToName(state->builds[act]);
|
|
|
|
if (hasSuffix(name, ".drv"))
|
|
|
|
name.resize(name.size() - 4);
|
|
|
|
createActivity(*state, act, fmt("building " ANSI_BOLD "%s" ANSI_NORMAL, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.type == evBuildFinished) {
|
|
|
|
Activity::t act = ev.getI(0);
|
|
|
|
if (ev.getI(1)) {
|
|
|
|
if (state->runningBuilds.count(act))
|
|
|
|
state->succeededBuilds++;
|
|
|
|
} else
|
|
|
|
state->failedBuilds++;
|
|
|
|
state->runningBuilds.erase(act);
|
|
|
|
state->builds.erase(act);
|
|
|
|
deleteActivity(*state, act);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.type == evBuildOutput) {
|
|
|
|
Activity::t act = ev.getI(0);
|
|
|
|
assert(state->runningBuilds.count(act));
|
|
|
|
updateActivity(*state, act, ev.getS(1));
|
|
|
|
}
|
|
|
|
|
2017-08-14 20:00:03 +03:00
|
|
|
update(*state);
|
2017-05-16 17:09:57 +03:00
|
|
|
}
|
2016-04-25 16:26:07 +03:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|