Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build edges that previously failed asap #60

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,23 @@ bool Plan::CheckDependencyCycle(Node* node, vector<Node*>* stack, string* err) {
Edge* Plan::FindWork() {
if (ready_.empty())
return NULL;
set<Edge*>::iterator i = ready_.begin();
Edge* edge = *i;
ready_.erase(i);
set<Edge*>::iterator best;

int best_score = -1;
for (set<Edge*>::iterator i = ready_.begin(); i != ready_.end(); i++) {
Edge* edge = *i;

int score = edge->priority_;

if (score > best_score) {
best = i;
best_score = score;
}
}

Edge* edge = *best;
ready_.erase(best);

return edge;
}

Expand Down Expand Up @@ -507,6 +521,6 @@ void Builder::FinishEdge(Edge* edge, bool success, const string& output) {

int start_time, end_time;
status_->BuildEdgeFinished(edge, success, output, &start_time, &end_time);
if (success && log_)
log_->RecordCommand(edge, start_time, end_time);
if (/*success &&*/ log_)
log_->RecordCommand(edge, start_time, end_time, success ? 0 : 1);
}
34 changes: 26 additions & 8 deletions src/build_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

namespace {

const char kFileSignature[] = "# ninja log v2\n";
const int kCurrentVersion = 2;
const char kFileSignature_V2[] = "# ninja log v2\n";
const char kFileSignature_V3[] = "# ninja log v3\n";
const int kCurrentVersion = 3;

}

Expand All @@ -57,7 +58,7 @@ bool BuildLog::OpenForWrite(const string& path, string* err) {
setvbuf(log_file_, NULL, _IOLBF, BUFSIZ);

if (ftell(log_file_) == 0) {
if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, log_file_) < 1) {
if (fwrite(kFileSignature_V3, sizeof(kFileSignature_V3) - 1, 1, log_file_) < 1) {
*err = strerror(errno);
return false;
}
Expand All @@ -66,7 +67,7 @@ bool BuildLog::OpenForWrite(const string& path, string* err) {
return true;
}

void BuildLog::RecordCommand(Edge* edge, int start_time, int end_time) {
void BuildLog::RecordCommand(Edge* edge, int start_time, int end_time, int exit_code) {
if (!log_file_)
return;

Expand All @@ -86,6 +87,7 @@ void BuildLog::RecordCommand(Edge* edge, int start_time, int end_time) {
log_entry->command = command;
log_entry->start_time = start_time;
log_entry->end_time = end_time;
log_entry->exit_code = exit_code;

WriteEntry(log_file_, *log_entry);
}
Expand Down Expand Up @@ -114,18 +116,24 @@ bool BuildLog::Load(const string& path, string* err) {
while (fgets(buf, sizeof(buf), file)) {
if (!log_version) {
log_version = 1; // Assume by default.
if (strcmp(buf, kFileSignature) == 0) {
if (strcmp(buf, kFileSignature_V2) == 0) {
log_version = 2;
continue;
}
if (strcmp(buf, kFileSignature_V3) == 0) {
log_version = 3;
continue;
}
}

char* start = buf;
char* end = strchr(start, ' ');
if (!end)
continue;
*end = 0;

int start_time = 0, end_time = 0;
int exit_code = 0;
if (log_version == 1) {
// In v1 we logged how long the command took; we don't use this info.
// int time_ms = atoi(start);
Expand All @@ -141,6 +149,15 @@ bool BuildLog::Load(const string& path, string* err) {
*end = 0;
end_time = atoi(start);
start = end + 1;

if (log_version >= 3) {
char* end = strchr(start, ' ');
if (!end)
continue;
*end = 0;
exit_code = atoi(start);
start = end + 1;
}
}

end = strchr(start, ' ');
Expand All @@ -167,6 +184,7 @@ bool BuildLog::Load(const string& path, string* err) {
entry->output = output;
entry->start_time = start_time;
entry->end_time = end_time;
entry->exit_code = exit_code;
entry->command = string(start, end - start);
}

Expand Down Expand Up @@ -195,8 +213,8 @@ BuildLog::LogEntry* BuildLog::LookupByOutput(const string& path) {
}

void BuildLog::WriteEntry(FILE* f, const LogEntry& entry) {
fprintf(f, "%d %d %s %s\n",
entry.start_time, entry.end_time,
fprintf(f, "%d %d %d %s %s\n",
entry.start_time, entry.end_time, entry.exit_code,
entry.output.c_str(), entry.command.c_str());
}

Expand All @@ -210,7 +228,7 @@ bool BuildLog::Recompact(const string& path, string* err) {
return false;
}

if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, f) < 1) {
if (fwrite(kFileSignature_V3, sizeof(kFileSignature_V3) - 1, 1, f) < 1) {
*err = strerror(errno);
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/build_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct BuildLog {

void SetConfig(BuildConfig* config) { config_ = config; }
bool OpenForWrite(const string& path, string* err);
void RecordCommand(Edge* edge, int start_time, int end_time);
void RecordCommand(Edge* edge, int start_time, int end_time, int exit_code);
void Close();

/// Load the on-disk log.
Expand All @@ -46,11 +46,13 @@ struct BuildLog {
string command;
int start_time;
int end_time;
int exit_code;

// Used by tests.
bool operator==(const LogEntry& o) {
return output == o.output && command == o.command &&
start_time == o.start_time && end_time == o.end_time;
start_time == o.start_time && end_time == o.end_time &&
exit_code == o.exit_code;
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/build_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ TEST_F(BuildLogTest, WriteRead) {
string err;
EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
ASSERT_EQ("", err);
log1.RecordCommand(state_.edges_[0], 15, 18);
log1.RecordCommand(state_.edges_[1], 20, 25);
log1.RecordCommand(state_.edges_[0], 15, 18, 1);
log1.RecordCommand(state_.edges_[1], 20, 25, 2);
log1.Close();

BuildLog log2;
Expand Down Expand Up @@ -84,8 +84,8 @@ TEST_F(BuildLogTest, Truncate) {
string err;
EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
ASSERT_EQ("", err);
log1.RecordCommand(state_.edges_[0], 15, 18);
log1.RecordCommand(state_.edges_[1], 20, 25);
log1.RecordCommand(state_.edges_[0], 15, 18, 1);
log1.RecordCommand(state_.edges_[1], 20, 25, 2);
log1.Close();

struct stat statbuf;
Expand Down
17 changes: 13 additions & 4 deletions src/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,29 @@ bool Edge::RecomputeDirty(State* state, DiskInterface* disk_interface,
// yet (or never will). Stat them if we haven't already.
(*i)->file_->StatIfNecessary(disk_interface);

BuildLog::LogEntry* log_entry = 0;
if (state && state->build_log_) {
log_entry = state->build_log_->LookupByOutput((*i)->file_->path_);
if (log_entry) {
if (log_entry->exit_code != 0) {
priority_++;
}
}
}

// Output is dirty if we're dirty, we're missing the output,
// or if it's older than the most recent input mtime.
if (dirty || !(*i)->file_->exists() ||
(*i)->file_->mtime_ < most_recent_input) {
(*i)->dirty_ = true;
} else {
// May also be dirty due to the command changing since the last build.
BuildLog::LogEntry* entry;
if (state->build_log_ &&
(entry = state->build_log_->LookupByOutput((*i)->file_->path_))) {
if (command != entry->command)
if (log_entry) {
if (command != log_entry->command)
(*i)->dirty_ = true;
}
}

}
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using namespace std;

#include "eval_env.h"
#include "build_log.h"

struct DiskInterface;

Expand Down Expand Up @@ -89,7 +90,7 @@ struct State;

/// An edge in the dependency graph; links between Nodes using Rules.
struct Edge {
Edge() : rule_(NULL), env_(NULL), implicit_deps_(0), order_only_deps_(0) {}
Edge() : rule_(NULL), env_(NULL), priority_(0), implicit_deps_(0), order_only_deps_(0) {}

bool RecomputeDirty(State* state, DiskInterface* disk_interface, string* err);
string EvaluateCommand(); // XXX move to env, take env ptr
Expand All @@ -103,6 +104,9 @@ struct Edge {
vector<Node*> outputs_;
Env* env_;

// We choose the higher priority edges first
int priority_;

// XXX There are three types of inputs.
// 1) explicit deps, which show up as $in on the command line;
// 2) implicit deps, which the target depends on implicitly (e.g. C headers),
Expand Down