Ninja
state.cc
Go to the documentation of this file.
1 // Copyright 2011 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 "state.h"
16 
17 #include <assert.h>
18 #include <stdio.h>
19 
20 #include "edit_distance.h"
21 #include "graph.h"
22 #include "metrics.h"
23 #include "util.h"
24 
25 
26 void Pool::EdgeScheduled(const Edge& edge) {
27  if (depth_ != 0)
28  current_use_ += edge.weight();
29 }
30 
31 void Pool::EdgeFinished(const Edge& edge) {
32  if (depth_ != 0)
33  current_use_ -= edge.weight();
34 }
35 
36 void Pool::DelayEdge(Edge* edge) {
37  assert(depth_ != 0);
38  delayed_.insert(edge);
39 }
40 
41 void Pool::RetrieveReadyEdges(set<Edge*>* ready_queue) {
42  DelayedEdges::iterator it = delayed_.begin();
43  while (it != delayed_.end()) {
44  Edge* edge = *it;
45  if (current_use_ + edge->weight() > depth_)
46  break;
47  ready_queue->insert(edge);
48  EdgeScheduled(*edge);
49  ++it;
50  }
51  delayed_.erase(delayed_.begin(), it);
52 }
53 
54 void Pool::Dump() const {
55  printf("%s (%d/%d) ->\n", name_.c_str(), current_use_, depth_);
56  for (DelayedEdges::const_iterator it = delayed_.begin();
57  it != delayed_.end(); ++it)
58  {
59  printf("\t");
60  (*it)->Dump();
61  }
62 }
63 
64 bool Pool::WeightedEdgeCmp(const Edge* a, const Edge* b) {
65  if (!a) return b;
66  if (!b) return false;
67  int weight_diff = a->weight() - b->weight();
68  return ((weight_diff < 0) || (weight_diff == 0 && a < b));
69 }
70 
72 const Rule State::kPhonyRule("phony");
73 
77 }
78 
79 void State::AddRule(const Rule* rule) {
80  assert(LookupRule(rule->name()) == NULL);
81  rules_[rule->name()] = rule;
82 }
83 
84 const Rule* State::LookupRule(const string& rule_name) {
85  map<string, const Rule*>::iterator i = rules_.find(rule_name);
86  if (i == rules_.end())
87  return NULL;
88  return i->second;
89 }
90 
91 void State::AddPool(Pool* pool) {
92  assert(LookupPool(pool->name()) == NULL);
93  pools_[pool->name()] = pool;
94 }
95 
96 Pool* State::LookupPool(const string& pool_name) {
97  map<string, Pool*>::iterator i = pools_.find(pool_name);
98  if (i == pools_.end())
99  return NULL;
100  return i->second;
101 }
102 
103 Edge* State::AddEdge(const Rule* rule) {
104  Edge* edge = new Edge();
105  edge->rule_ = rule;
106  edge->pool_ = &State::kDefaultPool;
107  edge->env_ = &bindings_;
108  edges_.push_back(edge);
109  return edge;
110 }
111 
113  Node* node = LookupNode(path);
114  if (node)
115  return node;
116  node = new Node(path.AsString());
117  paths_[node->path()] = node;
118  return node;
119 }
120 
122  METRIC_RECORD("lookup node");
123  Paths::iterator i = paths_.find(path);
124  if (i != paths_.end())
125  return i->second;
126  return NULL;
127 }
128 
129 Node* State::SpellcheckNode(const string& path) {
130  const bool kAllowReplacements = true;
131  const int kMaxValidEditDistance = 3;
132 
133  int min_distance = kMaxValidEditDistance + 1;
134  Node* result = NULL;
135  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) {
136  int distance = EditDistance(
137  i->first, path, kAllowReplacements, kMaxValidEditDistance);
138  if (distance < min_distance && i->second) {
139  min_distance = distance;
140  result = i->second;
141  }
142  }
143  return result;
144 }
145 
146 void State::AddIn(Edge* edge, StringPiece path) {
147  Node* node = GetNode(path);
148  edge->inputs_.push_back(node);
149  node->AddOutEdge(edge);
150 }
151 
152 void State::AddOut(Edge* edge, StringPiece path) {
153  Node* node = GetNode(path);
154  edge->outputs_.push_back(node);
155  if (node->in_edge()) {
156  Warning("multiple rules generate %s. "
157  "builds involving this target will not be correct; "
158  "continuing anyway",
159  path.AsString().c_str());
160  }
161  node->set_in_edge(edge);
162 }
163 
164 bool State::AddDefault(StringPiece path, string* err) {
165  Node* node = LookupNode(path);
166  if (!node) {
167  *err = "unknown target '" + path.AsString() + "'";
168  return false;
169  }
170  defaults_.push_back(node);
171  return true;
172 }
173 
174 vector<Node*> State::RootNodes(string* err) {
175  vector<Node*> root_nodes;
176  // Search for nodes with no output.
177  for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) {
178  for (vector<Node*>::iterator out = (*e)->outputs_.begin();
179  out != (*e)->outputs_.end(); ++out) {
180  if ((*out)->out_edges().empty())
181  root_nodes.push_back(*out);
182  }
183  }
184 
185  if (!edges_.empty() && root_nodes.empty())
186  *err = "could not determine root nodes of build graph";
187 
188  assert(edges_.empty() || !root_nodes.empty());
189  return root_nodes;
190 }
191 
192 vector<Node*> State::DefaultNodes(string* err) {
193  return defaults_.empty() ? RootNodes(err) : defaults_;
194 }
195 
196 void State::Reset() {
197  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i)
198  i->second->ResetState();
199  for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e)
200  (*e)->outputs_ready_ = false;
201 }
202 
203 void State::Dump() {
204  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) {
205  Node* node = i->second;
206  printf("%s %s [id:%d]\n",
207  node->path().c_str(),
208  node->status_known() ? (node->dirty() ? "dirty" : "clean")
209  : "unknown",
210  node->id());
211  }
212  if (!pools_.empty()) {
213  printf("resource_pools:\n");
214  for (map<string, Pool*>::const_iterator it = pools_.begin();
215  it != pools_.end(); ++it)
216  {
217  if (!it->second->name().empty()) {
218  it->second->Dump();
219  }
220  }
221  }
222 }
void Reset()
Reset state.
Definition: state.cc:196
void AddIn(Edge *edge, StringPiece path)
Definition: state.cc:146
vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:128
void EdgeScheduled(const Edge &edge)
informs this Pool that the given edge is committed to be run.
Definition: state.cc:26
Node * GetNode(StringPiece path)
Definition: state.cc:112
Pool * LookupPool(const string &pool_name)
Definition: state.cc:96
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
Paths paths_
Definition: state.h:119
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
Node * SpellcheckNode(const string &path)
Definition: state.cc:129
vector< Node * > defaults_
Definition: state.h:131
Edge * in_edge() const
Definition: graph.h:80
string AsString() const
Convert the slice into a full-fledged std::string, copying the data into a new string.
Definition: string_piece.h:45
void AddOutEdge(Edge *edge)
Definition: graph.h:87
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:137
DelayedEdges delayed_
Definition: state.h:79
Edge * AddEdge(const Rule *rule)
Definition: state.cc:103
vector< Node * > inputs_
Definition: graph.h:156
State()
Definition: state.cc:74
void AddOut(Edge *edge, StringPiece path)
Definition: state.cc:152
vector< Node * > DefaultNodes(string *error)
Definition: state.cc:192
void Dump()
Dump the nodes and Pools (useful for debugging).
Definition: state.cc:203
void AddRule(const Rule *rule)
Definition: state.cc:79
BindingEnv * env_
Definition: graph.h:158
An invokable build command and associated metadata (description, etc.).
Definition: graph.h:116
bool dirty() const
Definition: graph.h:76
bool status_known() const
Definition: graph.h:69
void DelayEdge(Edge *edge)
adds the given edge to this Pool to be delayed.
Definition: state.cc:36
Node * LookupNode(StringPiece path)
Definition: state.cc:121
A pool for delayed edges.
Definition: state.h:39
void AddPool(Pool *pool)
Definition: state.cc:91
vector< Node * > RootNodes(string *error)
Definition: state.cc:174
#define METRIC_RECORD(name)
The primary interface to metrics.
Definition: metrics.h:85
BindingEnv bindings_
Definition: state.h:130
const string & name() const
Definition: state.h:46
const string & path() const
Definition: graph.h:73
void RetrieveReadyEdges(set< Edge * > *ready_queue)
Pool will add zero or more edges to the ready_queue.
Definition: state.cc:41
int id() const
Definition: graph.h:83
Pool * pool_
Definition: graph.h:155
bool AddDefault(StringPiece path, string *error)
Definition: state.cc:164
const Rule * rule_
Definition: graph.h:154
static bool WeightedEdgeCmp(const Edge *a, const Edge *b)
Definition: state.cc:64
string name_
Definition: state.h:69
void EdgeFinished(const Edge &edge)
informs this Pool that the given edge is no longer runnable, and should relinquish its resources back...
Definition: state.cc:31
void set_in_edge(Edge *edge)
Definition: graph.h:81
const string & name() const
Definition: graph.h:119
void Dump() const
Dump the Pool and its edges (useful for debugging).
Definition: state.cc:54
int weight() const
Definition: graph.h:163
map< string, Pool * > pools_
All the pools used in the graph.
Definition: state.h:125
void Warning(const char *msg,...)
Log a warning message.
Definition: util.cc:69
static Pool kDefaultPool
Definition: state.h:84
int EditDistance(const StringPiece &s1, const StringPiece &s2, bool allow_replacements, int max_edit_distance)
map< string, const Rule * > rules_
All the rules used in the graph.
Definition: state.h:122
const Rule * LookupRule(const string &rule_name)
Definition: state.cc:84
int current_use_
|current_use_| is the total of the weights of the edges which are currently scheduled in the Plan (i...
Definition: state.h:73
int depth_
Definition: state.h:74
static const Rule kPhonyRule
Definition: state.h:85
vector< Node * > outputs_
Definition: graph.h:157