Skip to content

Commit

Permalink
Simplify running edges status
Browse files Browse the repository at this point in the history
Store the number of running edges instead of trying to compute it
from the started and finshed edge counts, which may be different
for starting and ending status messages.  Allows removing the status
parameter to PrintStatus and the EdgeStatus enum.
  • Loading branch information
colincross authored and Eli Ribble committed Aug 12, 2019
1 parent 0353e23 commit d602fab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
26 changes: 15 additions & 11 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool DryRunCommandRunner::WaitForCommand(Result* result) {

BuildStatus::BuildStatus(const BuildConfig& config)
: config_(config),
started_edges_(0), finished_edges_(0), total_edges_(0),
started_edges_(0), finished_edges_(0), total_edges_(0), running_edges_(0),
time_millis_(0), progress_status_format_(NULL),
current_rate_(config.parallelism) {

Expand All @@ -101,7 +101,7 @@ void BuildStatus::BuildEdgeStarted(Edge* edge, int64_t start_time_millis) {
time_millis_ = start_time_millis;

if (edge->use_console() || printer_.is_smart_terminal())
PrintStatus(edge, start_time_millis, kEdgeStarted);
PrintStatus(edge, start_time_millis);

if (edge->use_console())
printer_.SetConsoleLocked(true);
Expand All @@ -120,7 +120,12 @@ void BuildStatus::BuildEdgeFinished(Edge* edge, int64_t end_time_millis,
return;

if (!edge->use_console())
PrintStatus(edge, end_time_millis, kEdgeFinished);
PrintStatus(edge, end_time_millis);

// Update running_edges_ after PrintStatus so that the number of running
// edges doesn't oscillate between config.parallelism_ and
// config.parallelism_ - 1.
--running_edges_;

// Print the command that is spewing before printing its output.
if (!success) {
Expand Down Expand Up @@ -183,6 +188,9 @@ void BuildStatus::BuildLoadDyndeps() {
}

void BuildStatus::BuildStarted() {
started_edges_ = 0;
finished_edges_ = 0;
running_edges_ = 0;
}

void BuildStatus::BuildFinished() {
Expand All @@ -191,7 +199,7 @@ void BuildStatus::BuildFinished() {
}

string BuildStatus::FormatProgressStatus(
const char* progress_status_format, int64_t time, EdgeStatus status) const {
const char* progress_status_format, int64_t time) const {
string out;
char buf[32];
for (const char* s = progress_status_format; *s != '\0'; ++s) {
Expand All @@ -216,11 +224,7 @@ string BuildStatus::FormatProgressStatus(

// Running edges.
case 'r': {
int running_edges = started_edges_ - finished_edges_;
// count the edge that just finished as a running edge
if (status == kEdgeFinished)
running_edges++;
snprintf(buf, sizeof(buf), "%d", running_edges);
snprintf(buf, sizeof(buf), "%d", running_edges_);
out += buf;
break;
}
Expand Down Expand Up @@ -276,7 +280,7 @@ string BuildStatus::FormatProgressStatus(
return out;
}

void BuildStatus::PrintStatus(Edge* edge, int64_t time, EdgeStatus status) {
void BuildStatus::PrintStatus(Edge* edge, int64_t time) {
if (config_.verbosity == BuildConfig::QUIET)
return;

Expand All @@ -286,7 +290,7 @@ void BuildStatus::PrintStatus(Edge* edge, int64_t time, EdgeStatus status) {
if (to_print.empty() || force_full_command)
to_print = edge->GetBinding("command");

to_print = FormatProgressStatus(progress_status_format_, time, status) + to_print;
to_print = FormatProgressStatus(progress_status_format_, time) + to_print;

printer_.Print(to_print,
force_full_command ? LinePrinter::FULL : LinePrinter::ELIDE);
Expand Down
13 changes: 4 additions & 9 deletions src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,20 @@ struct BuildStatus {
void BuildStarted();
void BuildFinished();

enum EdgeStatus {
kEdgeStarted,
kEdgeFinished,
};

/// Format the progress status string by replacing the placeholders.
/// See the user manual for more information about the available
/// placeholders.
/// @param progress_status_format The format of the progress status.
/// @param status The status of the edge.
string FormatProgressStatus(const char* progress_status_format, int64_t time,
EdgeStatus status) const;
string FormatProgressStatus(const char* progress_status_format,
int64_t time) const;

private:
void PrintStatus(Edge* edge, int64_t time, EdgeStatus status);
void PrintStatus(Edge* edge, int64_t time);

const BuildConfig& config_;

int started_edges_, finished_edges_, total_edges_;
int started_edges_, finished_edges_, total_edges_, running_edges_;
int64_t time_millis_;

/// Prints progress output.
Expand Down
9 changes: 3 additions & 6 deletions src/build_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,7 @@ TEST_F(BuildWithLogTest, RestatTest) {
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ("", err);
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]", 0,
BuildStatus::kEdgeStarted));
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]", 0));
command_runner_.commands_ran_.clear();
state_.Reset();

Expand Down Expand Up @@ -1827,14 +1826,12 @@ TEST_F(BuildTest, StatusFormatElapsed) {
status_.BuildStarted();
// Before any task is done, the elapsed time must be zero.
EXPECT_EQ("[%/e0.000]",
status_.FormatProgressStatus("[%%/e%e]", 0,
BuildStatus::kEdgeStarted));
status_.FormatProgressStatus("[%%/e%e]", 0));
}

TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]", 0,
BuildStatus::kEdgeStarted));
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]", 0));
}

TEST_F(BuildTest, FailedDepsParse) {
Expand Down

0 comments on commit d602fab

Please sign in to comment.