46 file_ = fopen(path.c_str(),
"ab");
48 *err = strerror(errno);
55 fseek(
file_, 0, SEEK_END);
57 if (ftell(
file_) == 0) {
59 *err = strerror(errno);
63 *err = strerror(errno);
72 const vector<Node*>& nodes) {
74 nodes.empty() ? NULL : (
Node**)&nodes.front());
78 int node_count,
Node** nodes) {
80 bool made_change =
false;
87 for (
int i = 0; i < node_count; ++i) {
88 if (nodes[i]->
id() < 0) {
98 deps->
mtime != mtime ||
102 for (
int i = 0; i < node_count; ++i) {
103 if (deps->
nodes[i] != nodes[i]) {
118 fwrite(&size, 2, 1,
file_);
120 fwrite(&
id, 4, 1,
file_);
121 int timestamp = mtime;
122 fwrite(×tamp, 4, 1,
file_);
123 for (
int i = 0; i < node_count; ++i) {
125 fwrite(&
id, 4, 1,
file_);
130 for (
int i = 0; i < node_count; ++i)
131 deps->
nodes[i] = nodes[i];
146 FILE* f = fopen(path.c_str(),
"rb");
150 *err = strerror(errno);
154 bool valid_header =
true;
156 if (!fgets(buf,
sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
157 valid_header =
false;
160 *err =
"bad deps log signature or version; starting over";
162 unlink(path.c_str());
169 bool read_failed =
false;
170 int unique_dep_record_count = 0;
171 int total_dep_record_count = 0;
176 if (fread(&size, 2, 1, f) < 1) {
181 bool is_deps = (size >> 15) != 0;
182 size = size & 0x7FFF;
184 if (fread(buf, size, 1, f) < 1) {
190 assert(size % 4 == 0);
191 int* deps_data =
reinterpret_cast<int*
>(buf);
192 int out_id = deps_data[0];
193 int mtime = deps_data[1];
195 int deps_count = (size / 4) - 2;
198 for (
int i = 0; i < deps_count; ++i) {
199 assert(deps_data[i] < (
int)
nodes_.size());
200 assert(
nodes_[deps_data[i]]);
204 total_dep_record_count++;
206 ++unique_dep_record_count;
210 assert(node->
id() < 0);
220 *err = strerror(ferror(f));
222 *err =
"premature end of file";
226 if (!
Truncate(path.c_str(), offset, err))
231 *err +=
"; recovering";
238 int kMinCompactionEntryCount = 1000;
239 int kCompactionRatio = 3;
240 if (total_dep_record_count > kMinCompactionEntryCount &&
241 total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
251 if (node->
id() < 0 || node->
id() >= (int)
deps_.size())
258 printf(
"Recompacting deps...\n");
260 string temp_path = path +
".recompact";
264 unlink(temp_path.c_str());
267 if (!new_log.OpenForWrite(temp_path, err))
272 for (vector<Node*>::iterator i =
nodes_.begin(); i !=
nodes_.end(); ++i)
276 for (
int old_id = 0; old_id < (int)
deps_.size(); ++old_id) {
280 if (!new_log.RecordDeps(
nodes_[old_id], deps->
mtime,
290 deps_.swap(new_log.deps_);
291 nodes_.swap(new_log.nodes_);
293 if (unlink(path.c_str()) < 0) {
294 *err = strerror(errno);
298 if (rename(temp_path.c_str(), path.c_str()) < 0) {
299 *err = strerror(errno);
307 if (out_id >= (
int)
deps_.size())
308 deps_.resize(out_id + 1);
310 bool delete_old =
deps_[out_id] != NULL;
312 delete deps_[out_id];
319 fwrite(&size, 2, 1,
file_);
const int kCurrentVersion
const char kFileSignature[]
vector< Deps * > deps_
Maps id -> deps of that id.
Node * GetNode(StringPiece path)
StringPiece represents a slice of a string whose memory is managed externally.
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
As build commands run they can output extra dependency information (e.g.
vector< Node * > nodes_
Maps id -> Node.
bool OpenForWrite(const string &path, string *err)
Deps * GetDeps(Node *node)
bool Load(const string &path, State *state, string *err)
bool Recompact(const string &path, string *err)
Rewrite the known log entries, throwing away old data.
bool RecordId(Node *node)
#define METRIC_RECORD(name)
The primary interface to metrics.
const string & path() const
bool Truncate(const string &path, size_t size, string *err)
Truncates a file to the given size.
const vector< Deps * > & deps() const
Global state (file status, loaded rules) for a single run.
bool RecordDeps(Node *node, TimeStamp mtime, const vector< Node * > &nodes)
bool UpdateDeps(int out_id, Deps *deps)