add recompaction to depslog
[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         delete deps_[out_id];
176       deps_[out_id] = deps;
177     } else {
178       StringPiece path(buf, size);
179       Node* node = state->GetNode(path);
180       assert(node->id() < 0);
181       node->set_id(nodes_.size());
182       nodes_.push_back(node);
183     }
184   }
185   if (ferror(f)) {
186     *err = strerror(ferror(f));
187     return false;
188   }
189   fclose(f);
190   return true;
191 }
192
193 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
194   if (node->id() < 0)
195     return NULL;
196   return deps_[node->id()];
197 }
198
199 bool DepsLog::Recompact(const string& path, string* err) {
200   METRIC_RECORD(".ninja_deps recompact");
201   printf("Recompacting deps...\n");
202
203   string temp_path = path + ".recompact";
204   DepsLog new_log;
205   if (!new_log.OpenForWrite(temp_path, err))
206     return false;
207
208   // Clear all known ids so that new ones can be reassigned.
209   for (vector<Node*>::iterator i = nodes_.begin();
210        i != nodes_.end(); ++i) {
211     (*i)->set_id(-1);
212   }
213
214   // Write out all deps again.
215   for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
216     Deps* deps = deps_[old_id];
217     if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
218                             deps->node_count, deps->nodes)) {
219       new_log.Close();
220       return false;
221     }
222   }
223
224   new_log.Close();
225
226   if (unlink(path.c_str()) < 0) {
227     *err = strerror(errno);
228     return false;
229   }
230
231   if (rename(temp_path.c_str(), path.c_str()) < 0) {
232     *err = strerror(errno);
233     return false;
234   }
235
236   return true;
237 }
238
239 bool DepsLog::RecordId(Node* node) {
240   uint16_t size = (uint16_t)node->path().size();
241   fwrite(&size, 2, 1, file_);
242   fwrite(node->path().data(), node->path().size(), 1, file_);
243
244   node->set_id(nodes_.size());
245   nodes_.push_back(node);
246
247   return true;
248 }