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 // static
65 bool Pool::WeightedEdgeCmp(const Edge* a, const Edge* b) {
66  if (!a) return b;
67  if (!b) return false;
68  int weight_diff = a->weight() - b->weight();
69  return ((weight_diff < 0) || (weight_diff == 0 && a < b));
70 }
71 
73 Pool State::kConsolePool("console", 1);
74 const Rule State::kPhonyRule("phony");
75 
80 }
81 
82 void State::AddPool(Pool* pool) {
83  assert(LookupPool(pool->name()) == NULL);
84  pools_[pool->name()] = pool;
85 }
86 
87 Pool* State::LookupPool(const string& pool_name) {
88  map<string, Pool*>::iterator i = pools_.find(pool_name);
89  if (i == pools_.end())
90  return NULL;
91  return i->second;
92 }
93 
94 Edge* State::AddEdge(const Rule* rule) {
95  Edge* edge = new Edge();
96  edge->rule_ = rule;
97  edge->pool_ = &State::kDefaultPool;
98  edge->env_ = &bindings_;
99  edges_.push_back(edge);
100  return edge;
101 }
102 
103 Node* State::GetNode(StringPiece path, unsigned int slash_bits) {
104  Node* node = LookupNode(path);
105  if (node)
106  return node;
107  node = new Node(path.AsString(), slash_bits);
108  paths_[node->path()] = node;
109  return node;
110 }
111 
113  METRIC_RECORD("lookup node");
114  Paths::const_iterator i = paths_.find(path);
115  if (i != paths_.end())
116  return i->second;
117  return NULL;
118 }
119 
120 Node* State::SpellcheckNode(const string& path) {
121  const bool kAllowReplacements = true;
122  const int kMaxValidEditDistance = 3;
123 
124  int min_distance = kMaxValidEditDistance + 1;
125  Node* result = NULL;
126  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) {
127  int distance = EditDistance(
128  i->first, path, kAllowReplacements, kMaxValidEditDistance);
129  if (distance < min_distance && i->second) {
130  min_distance = distance;
131  result = i->second;
132  }
133  }
134  return result;
135 }
136 
137 void State::AddIn(Edge* edge, StringPiece path, unsigned int slash_bits) {
138  Node* node = GetNode(path, slash_bits);
139  edge->inputs_.push_back(node);
140  node->AddOutEdge(edge);
141 }
142 
143 bool State::AddOut(Edge* edge, StringPiece path, unsigned int slash_bits) {
144  Node* node = GetNode(path, slash_bits);
145  if (node->in_edge())
146  return false;
147  edge->outputs_.push_back(node);
148  node->set_in_edge(edge);
149  return true;
150 }
151 
152 bool State::AddDefault(StringPiece path, string* err) {
153  Node* node = LookupNode(path);
154  if (!node) {
155  *err = "unknown target '" + path.AsString() + "'";
156  return false;
157  }
158  defaults_.push_back(node);
159  return true;
160 }
161 
162 vector<Node*> State::RootNodes(string* err) {
163  vector<Node*> root_nodes;
164  // Search for nodes with no output.
165  for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) {
166  for (vector<Node*>::iterator out = (*e)->outputs_.begin();
167  out != (*e)->outputs_.end(); ++out) {
168  if ((*out)->out_edges().empty())
169  root_nodes.push_back(*out);
170  }
171  }
172 
173  if (!edges_.empty() && root_nodes.empty())
174  *err = "could not determine root nodes of build graph";
175 
176  return root_nodes;
177 }
178 
179 vector<Node*> State::DefaultNodes(string* err) {
180  return defaults_.empty() ? RootNodes(err) : defaults_;
181 }
182 
183 void State::Reset() {
184  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i)
185  i->second->ResetState();
186  for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e)
187  (*e)->outputs_ready_ = false;
188 }
189 
190 void State::Dump() {
191  for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) {
192  Node* node = i->second;
193  printf("%s %s [id:%d]\n",
194  node->path().c_str(),
195  node->status_known() ? (node->dirty() ? "dirty" : "clean")
196  : "unknown",
197  node->id());
198  }
199  if (!pools_.empty()) {
200  printf("resource_pools:\n");
201  for (map<string, Pool*>::const_iterator it = pools_.begin();
202  it != pools_.end(); ++it)
203  {
204  if (!it->second->name().empty()) {
205  it->second->Dump();
206  }
207  }
208  }
209 }
void Reset()
Reset state.
Definition: state.cc:183
vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:124
void EdgeScheduled(const Edge &edge)
informs this Pool that the given edge is committed to be run.
Definition: state.cc:26
Pool * LookupPool(const string &pool_name)
Definition: state.cc:87
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
Paths paths_
Definition: state.h:118
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:120
vector< Node * > defaults_
Definition: state.h:127
Edge * in_edge() const
Definition: graph.h:83
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:90
Node * GetNode(StringPiece path, unsigned int slash_bits)
Definition: state.cc:103
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:124
DelayedEdges delayed_
Definition: state.h:80
Edge * AddEdge(const Rule *rule)
Definition: state.cc:94
vector< Node * > inputs_
Definition: graph.h:150
State()
Definition: state.cc:76
void AddRule(const Rule *rule)
Definition: eval_env.cc:32
vector< Node * > DefaultNodes(string *error)
Definition: state.cc:179
void Dump()
Dump the nodes and Pools (useful for debugging).
Definition: state.cc:190
BindingEnv * env_
Definition: graph.h:152
An invokable build command and associated metadata (description, etc.).
Definition: eval_env.h:55
bool dirty() const
Definition: graph.h:79
bool status_known() const
Definition: graph.h:69
static Pool kConsolePool
Definition: state.h:86
void DelayEdge(Edge *edge)
adds the given edge to this Pool to be delayed.
Definition: state.cc:36
A pool for delayed edges.
Definition: state.h:39
void AddPool(Pool *pool)
Definition: state.cc:82
vector< Node * > RootNodes(string *error)
Definition: state.cc:162
#define METRIC_RECORD(name)
The primary interface to metrics.
Definition: metrics.h:85
BindingEnv bindings_
Definition: state.h:126
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:86
Pool * pool_
Definition: graph.h:149
bool AddDefault(StringPiece path, string *error)
Definition: state.cc:152
const Rule * rule_
Definition: graph.h:148
static bool WeightedEdgeCmp(const Edge *a, const Edge *b)
Definition: state.cc:65
string name_
Definition: state.h:70
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
bool AddOut(Edge *edge, StringPiece path, unsigned int slash_bits)
Definition: state.cc:143
void set_in_edge(Edge *edge)
Definition: graph.h:84
void Dump() const
Dump the Pool and its edges (useful for debugging).
Definition: state.cc:54
void AddIn(Edge *edge, StringPiece path, unsigned int slash_bits)
Definition: state.cc:137
int weight() const
Definition: graph.h:158
map< string, Pool * > pools_
All the pools used in the graph.
Definition: state.h:121
static Pool kDefaultPool
Definition: state.h:85
int EditDistance(const StringPiece &s1, const StringPiece &s2, bool allow_replacements, int max_edit_distance)
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:74
int depth_
Definition: state.h:75
static const Rule kPhonyRule
Definition: state.h:87
Node * LookupNode(StringPiece path) const
Definition: state.cc:112
vector< Node * > outputs_
Definition: graph.h:151