track deps log load time in metrics
[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 bool DepsLog::OpenForWrite(const string& path, string* err) {
28   file_ = fopen(path.c_str(), "ab");
29   if (!file_) {
30     *err = strerror(errno);
31     return false;
32   }
33   SetCloseOnExec(fileno(file_));
34
35   // Opening a file in append mode doesn't set the file pointer to the file's
36   // end on Windows. Do that explicitly.
37   fseek(file_, 0, SEEK_END);
38
39   /* XXX
40   if (ftell(log_file_) == 0) {
41     if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) {
42       *err = strerror(errno);
43       return false;
44     }
45   }
46   */
47
48   return true;
49 }
50
51 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
52                          const vector<Node*>& nodes) {
53   // Assign ids to all nodes that are missing one.
54   if (node->id() < 0)
55     RecordId(node);
56   for (vector<Node*>::const_iterator i = nodes.begin();
57        i != nodes.end(); ++i) {
58     if ((*i)->id() < 0)
59       RecordId(*i);
60   }
61
62   uint16_t size = 4 * (1 + 1 + nodes.size());
63   size |= 0x8000;  // Deps record: set high bit.
64   fwrite(&size, 2, 1, file_);
65   int id = node->id();
66   fwrite(&id, 4, 1, file_);
67   int timestamp = mtime;
68   fwrite(&timestamp, 4, 1, file_);
69   for (vector<Node*>::const_iterator i = nodes.begin();
70        i != nodes.end(); ++i) {
71     id = (*i)->id();
72     fwrite(&id, 4, 1, file_);
73   }
74
75   return true;
76 }
77
78 void DepsLog::Close() {
79   fclose(file_);
80   file_ = NULL;
81 }
82
83 bool DepsLog::Load(const string& path, State* state, string* err) {
84   METRIC_RECORD(".ninja_deps load");
85   char buf[32 << 10];
86   FILE* f = fopen(path.c_str(), "rb");
87   if (!f) {
88     if (errno == ENOENT)
89       return true;
90     *err = strerror(errno);
91     return false;
92   }
93
94   for (;;) {
95     uint16_t size;
96     if (fread(&size, 2, 1, f) < 1)
97       break;
98     bool is_deps = (size >> 15) != 0;
99     size = size & 0x7FFF;
100
101     if (fread(buf, size, 1, f) < 1)
102       break;
103
104     if (is_deps) {
105       assert(size % 4 == 0);
106       int* deps_data = reinterpret_cast<int*>(buf);
107       int out_id = deps_data[0];
108       int mtime = deps_data[1];
109       deps_data += 2;
110       int deps_count = (size / 4) - 2;
111
112       Deps* deps = new Deps;
113       deps->mtime = mtime;
114       deps->node_count = deps_count;
115       deps->nodes = new Node*[deps_count];
116       for (int i = 0; i < deps_count; ++i) {
117         assert(deps_data[i] < (int)nodes_.size());
118         assert(nodes_[deps_data[i]]);
119         deps->nodes[i] = nodes_[deps_data[i]];
120       }
121
122       if (out_id >= (int)deps_.size())
123         deps_.resize(out_id + 1);
124       if (deps_[out_id])
125         delete deps_[out_id];
126       deps_[out_id] = deps;
127     } else {
128       StringPiece path(buf, size);
129       Node* node = state->GetNode(path);
130       assert(node->id() < 0);
131       node->set_id(nodes_.size());
132       nodes_.push_back(node);
133     }
134   }
135   if (ferror(f)) {
136     *err = strerror(ferror(f));
137     return false;
138   }
139   fclose(f);
140   return true;
141 }
142
143 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
144   if (node->id() < 0)
145     return NULL;
146   return deps_[node->id()];
147 }
148
149 bool DepsLog::RecordId(Node* node) {
150   uint16_t size = node->path().size();
151   fwrite(&size, 2, 1, file_);
152   fwrite(node->path().data(), node->path().size(), 1, file_);
153
154   node->set_id(nodes_.size());
155   nodes_.push_back(node);
156
157   return true;
158 }