Merge pull request #642 from nico/depslogidx
[platform/upstream/ninja.git] / src / deps_log.cc
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "deps_log.h"
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <string.h>
21 #ifndef _WIN32
22 #include <unistd.h>
23 #endif
24
25 #include "graph.h"
26 #include "metrics.h"
27 #include "state.h"
28 #include "util.h"
29
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 = 2;
34
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;
39
40 // Record size is currently limited to 15 bit
41 const size_t kMaxRecordSize = (1 << 15) - 1;
42
43 DepsLog::~DepsLog() {
44   Close();
45 }
46
47 bool DepsLog::OpenForWrite(const string& path, string* err) {
48   if (needs_recompaction_) {
49     if (!Recompact(path, err))
50       return false;
51   }
52   
53   file_ = fopen(path.c_str(), "ab");
54   if (!file_) {
55     *err = strerror(errno);
56     return false;
57   }
58   setvbuf(file_, NULL, _IOFBF, kMaxBufferSize);
59   SetCloseOnExec(fileno(file_));
60
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);
64
65   if (ftell(file_) == 0) {
66     if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, file_) < 1) {
67       *err = strerror(errno);
68       return false;
69     }
70     if (fwrite(&kCurrentVersion, 4, 1, file_) < 1) {
71       *err = strerror(errno);
72       return false;
73     }
74   }
75   if (fflush(file_) != 0) {
76     *err = strerror(errno);
77     return false;
78   }
79   return true;
80 }
81
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());
86 }
87
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;
92
93   // Assign ids to all nodes that are missing one.
94   if (node->id() < 0) {
95     if (!RecordId(node))
96       return false;
97     made_change = true;
98   }
99   for (int i = 0; i < node_count; ++i) {
100     if (nodes[i]->id() < 0) {
101       if (!RecordId(nodes[i]))
102         return false;
103       made_change = true;
104     }
105   }
106
107   // See if the new data is different than the existing data, if any.
108   if (!made_change) {
109     Deps* deps = GetDeps(node);
110     if (!deps ||
111         deps->mtime != mtime ||
112         deps->node_count != node_count) {
113       made_change = true;
114     } else {
115       for (int i = 0; i < node_count; ++i) {
116         if (deps->nodes[i] != nodes[i]) {
117           made_change = true;
118           break;
119         }
120       }
121     }
122   }
123
124   // Don't write anything if there's no new info.
125   if (!made_change)
126     return true;
127
128   // Update on-disk representation.
129   size_t size = 4 * (1 + 1 + (uint16_t)node_count);
130   if (size > kMaxRecordSize) {
131     errno = ERANGE;
132     return false;
133   }
134   size |= 0x8000;  // Deps record: set high bit.
135   uint16_t size16 = (uint16_t)size;
136   if (fwrite(&size16, 2, 1, file_) < 1)
137     return false;
138   int id = node->id();
139   if (fwrite(&id, 4, 1, file_) < 1)
140     return false;
141   int timestamp = mtime;
142   if (fwrite(&timestamp, 4, 1, file_) < 1)
143     return false;
144   for (int i = 0; i < node_count; ++i) {
145     id = nodes[i]->id();
146     if (fwrite(&id, 4, 1, file_) < 1)
147       return false;
148   }
149   if (fflush(file_) != 0)
150       return false;
151
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);
157
158   return true;
159 }
160
161 void DepsLog::Close() {
162   if (file_)
163     fclose(file_);
164   file_ = NULL;
165 }
166
167 bool DepsLog::Load(const string& path, State* state, string* err) {
168   METRIC_RECORD(".ninja_deps load");
169   char buf[32 << 10];
170   FILE* f = fopen(path.c_str(), "rb");
171   if (!f) {
172     if (errno == ENOENT)
173       return true;
174     *err = strerror(errno);
175     return false;
176   }
177
178   bool valid_header = true;
179   int version = 0;
180   if (!fgets(buf, sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
181     valid_header = false;
182   // Note: For version differences, this should migrate to the new format.
183   // But the v1 format could sometimes (rarely) end up with invalid data, so
184   // don't migrate v1 to v2 to force a rebuild.
185   if (!valid_header || strcmp(buf, kFileSignature) != 0 ||
186       version != kCurrentVersion) {
187     if (version == 1)
188       *err = "deps log potentially corrupt; rebuilding";
189     else
190       *err = "bad deps log signature or version; starting over";
191     fclose(f);
192     unlink(path.c_str());
193     // Don't report this as a failure.  An empty deps log will cause
194     // us to rebuild the outputs anyway.
195     return true;
196   }
197
198   long offset;
199   bool read_failed = false;
200   int unique_dep_record_count = 0;
201   int total_dep_record_count = 0;
202   for (;;) {
203     offset = ftell(f);
204
205     uint16_t size;
206     if (fread(&size, 2, 1, f) < 1) {
207       if (!feof(f))
208         read_failed = true;
209       break;
210     }
211     bool is_deps = (size >> 15) != 0;
212     size = size & 0x7FFF;
213
214     if (fread(buf, size, 1, f) < 1) {
215       read_failed = true;
216       break;
217     }
218
219     if (is_deps) {
220       assert(size % 4 == 0);
221       int* deps_data = reinterpret_cast<int*>(buf);
222       int out_id = deps_data[0];
223       int mtime = deps_data[1];
224       deps_data += 2;
225       int deps_count = (size / 4) - 2;
226
227       Deps* deps = new Deps(mtime, deps_count);
228       for (int i = 0; i < deps_count; ++i) {
229         assert(deps_data[i] < (int)nodes_.size());
230         assert(nodes_[deps_data[i]]);
231         deps->nodes[i] = nodes_[deps_data[i]];
232       }
233
234       total_dep_record_count++;
235       if (!UpdateDeps(out_id, deps))
236         ++unique_dep_record_count;
237     } else {
238       int path_size = size - 4;
239       StringPiece path(buf, path_size);
240       Node* node = state->GetNode(path);
241
242       // Check that the expected index matches the actual index. This can only
243       // happen if two ninja processes write to the same deps log concurrently.
244       // (This uses unary complement to make the checksum look less like a
245       // dependency record entry.)
246       unsigned checksum;
247       memcpy(&checksum, buf + path_size, sizeof checksum);
248       int expected_id = ~checksum;
249       int id = nodes_.size();
250       if (id != expected_id) {
251         read_failed = true;
252         break;
253       }
254
255       assert(node->id() < 0);
256       node->set_id(id);
257       nodes_.push_back(node);
258     }
259   }
260
261   if (read_failed) {
262     // An error occurred while loading; try to recover by truncating the
263     // file to the last fully-read record.
264     if (ferror(f)) {
265       *err = strerror(ferror(f));
266     } else {
267       *err = "premature end of file";
268     }
269     fclose(f);
270
271     if (!Truncate(path.c_str(), offset, err))
272       return false;
273
274     // The truncate succeeded; we'll just report the load error as a
275     // warning because the build can proceed.
276     *err += "; recovering";
277     return true;
278   }
279
280   fclose(f);
281
282   // Rebuild the log if there are too many dead records.
283   int kMinCompactionEntryCount = 1000;
284   int kCompactionRatio = 3;
285   if (total_dep_record_count > kMinCompactionEntryCount &&
286       total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
287     needs_recompaction_ = true;
288   }
289
290   return true;
291 }
292
293 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
294   // Abort if the node has no id (never referenced in the deps) or if
295   // there's no deps recorded for the node.
296   if (node->id() < 0 || node->id() >= (int)deps_.size())
297     return NULL;
298   return deps_[node->id()];
299 }
300
301 bool DepsLog::Recompact(const string& path, string* err) {
302   METRIC_RECORD(".ninja_deps recompact");
303   printf("Recompacting deps...\n");
304
305   Close();
306   string temp_path = path + ".recompact";
307
308   // OpenForWrite() opens for append.  Make sure it's not appending to a
309   // left-over file from a previous recompaction attempt that crashed somehow.
310   unlink(temp_path.c_str());
311
312   DepsLog new_log;
313   if (!new_log.OpenForWrite(temp_path, err))
314     return false;
315
316   // Clear all known ids so that new ones can be reassigned.  The new indices
317   // will refer to the ordering in new_log, not in the current log.
318   for (vector<Node*>::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
319     (*i)->set_id(-1);
320   
321   // Write out all deps again.
322   for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
323     Deps* deps = deps_[old_id];
324     if (!deps) continue;  // If nodes_[old_id] is a leaf, it has no deps.
325
326     if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
327                             deps->node_count, deps->nodes)) {
328       new_log.Close();
329       return false;
330     }
331   }
332
333   new_log.Close();
334
335   // All nodes now have ids that refer to new_log, so steal its data.
336   deps_.swap(new_log.deps_);
337   nodes_.swap(new_log.nodes_);
338
339   if (unlink(path.c_str()) < 0) {
340     *err = strerror(errno);
341     return false;
342   }
343
344   if (rename(temp_path.c_str(), path.c_str()) < 0) {
345     *err = strerror(errno);
346     return false;
347   }
348
349   return true;
350 }
351
352 bool DepsLog::UpdateDeps(int out_id, Deps* deps) {
353   if (out_id >= (int)deps_.size())
354     deps_.resize(out_id + 1);
355
356   bool delete_old = deps_[out_id] != NULL;
357   if (delete_old)
358     delete deps_[out_id];
359   deps_[out_id] = deps;
360   return delete_old;
361 }
362
363 bool DepsLog::RecordId(Node* node) {
364   size_t size = node->path().size() + 4;
365   if (size > kMaxRecordSize) {
366     errno = ERANGE;
367     return false;
368   }
369   uint16_t size16 = (uint16_t)size;
370   if (fwrite(&size16, 2, 1, file_) < 1)
371     return false;
372   if (fwrite(node->path().data(), node->path().size(), 1, file_) < 1) {
373     assert(node->path().size() > 0);
374     return false;
375   }
376   int id = nodes_.size();
377   unsigned checksum = ~(unsigned)id;
378   if (fwrite(&checksum, 4, 1, file_) < 1)
379     return false;
380   if (fflush(file_) != 0)
381     return false;
382
383   node->set_id(id);
384   nodes_.push_back(node);
385
386   return true;
387 }