depslog: track dead record count
[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
22 #include "graph.h"
23 #include "metrics.h"
24 #include "state.h"
25 #include "util.h"
26
27 namespace {
28 const char kFileSignature[] = "# ninja deps v%d\n";
29 const int kCurrentVersion = 1;
30 }  // anonymous namespace
31
32
33 bool DepsLog::OpenForWrite(const string& path, string* err) {
34   file_ = fopen(path.c_str(), "ab");
35   if (!file_) {
36     *err = strerror(errno);
37     return false;
38   }
39   SetCloseOnExec(fileno(file_));
40
41   // Opening a file in append mode doesn't set the file pointer to the file's
42   // end on Windows. Do that explicitly.
43   fseek(file_, 0, SEEK_END);
44
45   if (ftell(file_) == 0) {
46     if (fprintf(file_, kFileSignature, kCurrentVersion) < 0) {
47       *err = strerror(errno);
48       return false;
49     }
50   }
51
52   return true;
53 }
54
55 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
56                          const vector<Node*>& nodes) {
57   return RecordDeps(node, mtime, nodes.size(), (Node**)&nodes.front());
58 }
59
60 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
61                          int node_count, Node** nodes) {
62   // Track whether there's any new data to be recorded.
63   bool made_change = false;
64
65   // Assign ids to all nodes that are missing one.
66   if (node->id() < 0) {
67     RecordId(node);
68     made_change = true;
69   }
70   for (int i = 0; i < node_count; ++i) {
71     if (nodes[i]->id() < 0) {
72       RecordId(nodes[i]);
73       made_change = true;
74     }
75   }
76
77   // See if the new data is different than the existing data, if any.
78   if (!made_change) {
79     Deps* deps = GetDeps(node);
80     if (!deps ||
81         deps->mtime != mtime ||
82         deps->node_count != node_count) {
83       made_change = true;
84     } else {
85       for (int i = 0; i < node_count; ++i) {
86         if (deps->nodes[i] != nodes[i]) {
87           made_change = true;
88           break;
89         }
90       }
91     }
92   }
93
94   // Don't write anything if there's no new info.
95   if (!made_change)
96     return true;
97
98   uint16_t size = 4 * (1 + 1 + (uint16_t)node_count);
99   size |= 0x8000;  // Deps record: set high bit.
100   fwrite(&size, 2, 1, file_);
101   int id = node->id();
102   fwrite(&id, 4, 1, file_);
103   int timestamp = mtime;
104   fwrite(&timestamp, 4, 1, file_);
105   for (int i = 0; i < node_count; ++i) {
106     id = nodes[i]->id();
107     fwrite(&id, 4, 1, file_);
108   }
109
110   return true;
111 }
112
113 void DepsLog::Close() {
114   fclose(file_);
115   file_ = NULL;
116 }
117
118 bool DepsLog::Load(const string& path, State* state, string* err) {
119   METRIC_RECORD(".ninja_deps load");
120   char buf[32 << 10];
121   FILE* f = fopen(path.c_str(), "rb");
122   if (!f) {
123     if (errno == ENOENT)
124       return true;
125     *err = strerror(errno);
126     return false;
127   }
128
129   if (!fgets(buf, sizeof(buf), f)) {
130     *err = strerror(errno);
131     return false;
132   }
133   int version = 0;
134   sscanf(buf, kFileSignature, &version);
135   if (version != kCurrentVersion) {
136     *err = "bad deps log signature or version; starting over";
137     fclose(f);
138     unlink(path.c_str());
139     // Don't report this as a failure.  An empty deps log will cause
140     // us to rebuild the outputs anyway.
141     return true;
142   }
143
144   for (;;) {
145     uint16_t size;
146     if (fread(&size, 2, 1, f) < 1)
147       break;
148     bool is_deps = (size >> 15) != 0;
149     size = size & 0x7FFF;
150
151     if (fread(buf, size, 1, f) < 1)
152       break;
153
154     if (is_deps) {
155       assert(size % 4 == 0);
156       int* deps_data = reinterpret_cast<int*>(buf);
157       int out_id = deps_data[0];
158       int mtime = deps_data[1];
159       deps_data += 2;
160       int deps_count = (size / 4) - 2;
161
162       Deps* deps = new Deps;
163       deps->mtime = mtime;
164       deps->node_count = deps_count;
165       deps->nodes = new Node*[deps_count];
166       for (int i = 0; i < deps_count; ++i) {
167         assert(deps_data[i] < (int)nodes_.size());
168         assert(nodes_[deps_data[i]]);
169         deps->nodes[i] = nodes_[deps_data[i]];
170       }
171
172       if (out_id >= (int)deps_.size())
173         deps_.resize(out_id + 1);
174       if (deps_[out_id]) {
175         ++dead_record_count_;
176         delete deps_[out_id];
177       }
178       deps_[out_id] = deps;
179     } else {
180       StringPiece path(buf, size);
181       Node* node = state->GetNode(path);
182       assert(node->id() < 0);
183       node->set_id(nodes_.size());
184       nodes_.push_back(node);
185     }
186   }
187   if (ferror(f)) {
188     *err = strerror(ferror(f));
189     return false;
190   }
191   fclose(f);
192   return true;
193 }
194
195 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
196   if (node->id() < 0)
197     return NULL;
198   return deps_[node->id()];
199 }
200
201 bool DepsLog::Recompact(const string& path, string* err) {
202   METRIC_RECORD(".ninja_deps recompact");
203   printf("Recompacting deps...\n");
204
205   string temp_path = path + ".recompact";
206   DepsLog new_log;
207   if (!new_log.OpenForWrite(temp_path, err))
208     return false;
209
210   // Clear all known ids so that new ones can be reassigned.
211   for (vector<Node*>::iterator i = nodes_.begin();
212        i != nodes_.end(); ++i) {
213     (*i)->set_id(-1);
214   }
215
216   // Write out all deps again.
217   for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
218     Deps* deps = deps_[old_id];
219     if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
220                             deps->node_count, deps->nodes)) {
221       new_log.Close();
222       return false;
223     }
224   }
225
226   new_log.Close();
227
228   if (unlink(path.c_str()) < 0) {
229     *err = strerror(errno);
230     return false;
231   }
232
233   if (rename(temp_path.c_str(), path.c_str()) < 0) {
234     *err = strerror(errno);
235     return false;
236   }
237
238   return true;
239 }
240
241 bool DepsLog::RecordId(Node* node) {
242   uint16_t size = (uint16_t)node->path().size();
243   fwrite(&size, 2, 1, file_);
244   fwrite(node->path().data(), node->path().size(), 1, file_);
245
246   node->set_id(nodes_.size());
247   nodes_.push_back(node);
248
249   return true;
250 }