1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
30 // The version is stored as 4 bytes after the signature and also serves as a
31 // byte order mark. Signature and version combined are 16 bytes long.
32 const char kFileSignature[] = "# ninjadeps\n";
33 const int kCurrentVersion = 1;
35 // Since the size field is 2 bytes and the top bit marks deps entries, a single
36 // record can be at most 32 kB. Set the buffer size to this and flush the file
37 // buffer after every record to make sure records aren't written partially.
38 const int kMaxBufferSize = 1 << 15;
40 // Record size is currently limited to 15 bit
41 const size_t kMaxRecordSize = (1 << 15) - 1;
47 bool DepsLog::OpenForWrite(const string& path, string* err) {
48 if (needs_recompaction_) {
49 if (!Recompact(path, err))
53 file_ = fopen(path.c_str(), "ab");
55 *err = strerror(errno);
58 setvbuf(file_, NULL, _IOFBF, kMaxBufferSize);
59 SetCloseOnExec(fileno(file_));
61 // Opening a file in append mode doesn't set the file pointer to the file's
62 // end on Windows. Do that explicitly.
63 fseek(file_, 0, SEEK_END);
65 if (ftell(file_) == 0) {
66 if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, file_) < 1) {
67 *err = strerror(errno);
70 if (fwrite(&kCurrentVersion, 4, 1, file_) < 1) {
71 *err = strerror(errno);
75 if (fflush(file_) != 0) {
76 *err = strerror(errno);
82 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
83 const vector<Node*>& nodes) {
84 return RecordDeps(node, mtime, nodes.size(),
85 nodes.empty() ? NULL : (Node**)&nodes.front());
88 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
89 int node_count, Node** nodes) {
90 // Track whether there's any new data to be recorded.
91 bool made_change = false;
93 // Assign ids to all nodes that are missing one.
99 for (int i = 0; i < node_count; ++i) {
100 if (nodes[i]->id() < 0) {
101 if (!RecordId(nodes[i]))
107 // See if the new data is different than the existing data, if any.
109 Deps* deps = GetDeps(node);
111 deps->mtime != mtime ||
112 deps->node_count != node_count) {
115 for (int i = 0; i < node_count; ++i) {
116 if (deps->nodes[i] != nodes[i]) {
124 // Don't write anything if there's no new info.
128 // Update on-disk representation.
129 size_t size = 4 * (1 + 1 + (uint16_t)node_count);
130 if (size > kMaxRecordSize) {
134 size |= 0x8000; // Deps record: set high bit.
135 uint16_t size16 = (uint16_t)size;
136 if (fwrite(&size16, 2, 1, file_) < 1)
139 if (fwrite(&id, 4, 1, file_) < 1)
141 int timestamp = mtime;
142 if (fwrite(×tamp, 4, 1, file_) < 1)
144 for (int i = 0; i < node_count; ++i) {
146 if (fwrite(&id, 4, 1, file_) < 1)
149 if (fflush(file_) != 0)
152 // Update in-memory representation.
153 Deps* deps = new Deps(mtime, node_count);
154 for (int i = 0; i < node_count; ++i)
155 deps->nodes[i] = nodes[i];
156 UpdateDeps(node->id(), deps);
161 void DepsLog::Close() {
167 bool DepsLog::Load(const string& path, State* state, string* err) {
168 METRIC_RECORD(".ninja_deps load");
170 FILE* f = fopen(path.c_str(), "rb");
174 *err = strerror(errno);
178 bool valid_header = true;
180 if (!fgets(buf, sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
181 valid_header = false;
182 if (!valid_header || strcmp(buf, kFileSignature) != 0 ||
183 version != kCurrentVersion) {
184 *err = "bad deps log signature or version; starting over";
186 unlink(path.c_str());
187 // Don't report this as a failure. An empty deps log will cause
188 // us to rebuild the outputs anyway.
193 bool read_failed = false;
194 int unique_dep_record_count = 0;
195 int total_dep_record_count = 0;
200 if (fread(&size, 2, 1, f) < 1) {
205 bool is_deps = (size >> 15) != 0;
206 size = size & 0x7FFF;
208 if (fread(buf, size, 1, f) < 1) {
214 assert(size % 4 == 0);
215 int* deps_data = reinterpret_cast<int*>(buf);
216 int out_id = deps_data[0];
217 int mtime = deps_data[1];
219 int deps_count = (size / 4) - 2;
221 Deps* deps = new Deps(mtime, deps_count);
222 for (int i = 0; i < deps_count; ++i) {
223 assert(deps_data[i] < (int)nodes_.size());
224 assert(nodes_[deps_data[i]]);
225 deps->nodes[i] = nodes_[deps_data[i]];
228 total_dep_record_count++;
229 if (!UpdateDeps(out_id, deps))
230 ++unique_dep_record_count;
232 StringPiece path(buf, size);
233 Node* node = state->GetNode(path);
234 assert(node->id() < 0);
235 node->set_id(nodes_.size());
236 nodes_.push_back(node);
241 // An error occurred while loading; try to recover by truncating the
242 // file to the last fully-read record.
244 *err = strerror(ferror(f));
246 *err = "premature end of file";
250 if (!Truncate(path.c_str(), offset, err))
253 // The truncate succeeded; we'll just report the load error as a
254 // warning because the build can proceed.
255 *err += "; recovering";
261 // Rebuild the log if there are too many dead records.
262 int kMinCompactionEntryCount = 1000;
263 int kCompactionRatio = 3;
264 if (total_dep_record_count > kMinCompactionEntryCount &&
265 total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
266 needs_recompaction_ = true;
272 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
273 // Abort if the node has no id (never referenced in the deps) or if
274 // there's no deps recorded for the node.
275 if (node->id() < 0 || node->id() >= (int)deps_.size())
277 return deps_[node->id()];
280 bool DepsLog::Recompact(const string& path, string* err) {
281 METRIC_RECORD(".ninja_deps recompact");
282 printf("Recompacting deps...\n");
285 string temp_path = path + ".recompact";
287 // OpenForWrite() opens for append. Make sure it's not appending to a
288 // left-over file from a previous recompaction attempt that crashed somehow.
289 unlink(temp_path.c_str());
292 if (!new_log.OpenForWrite(temp_path, err))
295 // Clear all known ids so that new ones can be reassigned. The new indices
296 // will refer to the ordering in new_log, not in the current log.
297 for (vector<Node*>::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
300 // Write out all deps again.
301 for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
302 Deps* deps = deps_[old_id];
303 if (!deps) continue; // If nodes_[old_id] is a leaf, it has no deps.
305 if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
306 deps->node_count, deps->nodes)) {
314 // All nodes now have ids that refer to new_log, so steal its data.
315 deps_.swap(new_log.deps_);
316 nodes_.swap(new_log.nodes_);
318 if (unlink(path.c_str()) < 0) {
319 *err = strerror(errno);
323 if (rename(temp_path.c_str(), path.c_str()) < 0) {
324 *err = strerror(errno);
331 bool DepsLog::UpdateDeps(int out_id, Deps* deps) {
332 if (out_id >= (int)deps_.size())
333 deps_.resize(out_id + 1);
335 bool delete_old = deps_[out_id] != NULL;
337 delete deps_[out_id];
338 deps_[out_id] = deps;
342 bool DepsLog::RecordId(Node* node) {
343 size_t size = node->path().size();
344 if (size > kMaxRecordSize) {
348 uint16_t size16 = (uint16_t)size;
349 if (fwrite(&size16, 2, 1, file_) < 1)
351 if (fwrite(node->path().data(), node->path().size(), 1, file_) < 1) {
352 assert(node->path().size() > 0);
355 if (fflush(file_) != 0)
358 node->set_id(nodes_.size());
359 nodes_.push_back(node);