Ninja
state.h
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 #ifndef NINJA_STATE_H_
16 #define NINJA_STATE_H_
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <vector>
22 using namespace std;
23 
24 #include "eval_env.h"
25 #include "hash_map.h"
26 
27 struct Edge;
28 struct Node;
29 struct Rule;
30 
31 /// A pool for delayed edges.
32 /// Pools are scoped to a State. Edges within a State will share Pools. A Pool
33 /// will keep a count of the total 'weight' of the currently scheduled edges. If
34 /// a Plan attempts to schedule an Edge which would cause the total weight to
35 /// exceed the depth of the Pool, the Pool will enque the Edge instead of
36 /// allowing the Plan to schedule it. The Pool will relinquish queued Edges when
37 /// the total scheduled weight diminishes enough (i.e. when a scheduled edge
38 /// completes).
39 struct Pool {
40  explicit Pool(const string& name, int depth)
41  : name_(name), current_use_(0), depth_(depth), delayed_(&WeightedEdgeCmp) { }
42 
43  // A depth of 0 is infinite
44  bool is_valid() const { return depth_ >= 0; }
45  int depth() const { return depth_; }
46  const string& name() const { return name_; }
47 
48  /// true if the Pool might delay this edge
49  bool ShouldDelayEdge() const { return depth_ != 0; }
50 
51  /// informs this Pool that the given edge is committed to be run.
52  /// Pool will count this edge as using resources from this pool.
53  void EdgeScheduled(const Edge& edge);
54 
55  /// informs this Pool that the given edge is no longer runnable, and should
56  /// relinquish its resources back to the pool
57  void EdgeFinished(const Edge& edge);
58 
59  /// adds the given edge to this Pool to be delayed.
60  void DelayEdge(Edge* edge);
61 
62  /// Pool will add zero or more edges to the ready_queue
63  void RetrieveReadyEdges(set<Edge*>* ready_queue);
64 
65  /// Dump the Pool and its edges (useful for debugging).
66  void Dump() const;
67 
68  private:
69  string name_;
70 
71  /// |current_use_| is the total of the weights of the edges which are
72  /// currently scheduled in the Plan (i.e. the edges in Plan::ready_).
74  int depth_;
75 
76  static bool WeightedEdgeCmp(const Edge* a, const Edge* b);
77 
78  typedef set<Edge*,bool(*)(const Edge*, const Edge*)> DelayedEdges;
80 };
81 
82 /// Global state (file status, loaded rules) for a single run.
83 struct State {
85  static const Rule kPhonyRule;
86 
87  State();
88 
89  void AddRule(const Rule* rule);
90  const Rule* LookupRule(const string& rule_name);
91 
92  void AddPool(Pool* pool);
93  Pool* LookupPool(const string& pool_name);
94 
95  Edge* AddEdge(const Rule* rule);
96 
97  Node* GetNode(StringPiece path);
98  Node* LookupNode(StringPiece path);
99  Node* SpellcheckNode(const string& path);
100 
101  void AddIn(Edge* edge, StringPiece path);
102  void AddOut(Edge* edge, StringPiece path);
103  bool AddDefault(StringPiece path, string* error);
104 
105  /// Reset state. Keeps all nodes and edges, but restores them to the
106  /// state where we haven't yet examined the disk for dirty state.
107  void Reset();
108 
109  /// Dump the nodes and Pools (useful for debugging).
110  void Dump();
111 
112  /// @return the root node(s) of the graph. (Root nodes have no output edges).
113  /// @param error where to write the error message if somethings went wrong.
114  vector<Node*> RootNodes(string* error);
115  vector<Node*> DefaultNodes(string* error);
116 
117  /// Mapping of path -> Node.
120 
121  /// All the rules used in the graph.
122  map<string, const Rule*> rules_;
123 
124  /// All the pools used in the graph.
125  map<string, Pool*> pools_;
126 
127  /// All the edges of the graph.
128  vector<Edge*> edges_;
129 
131  vector<Node*> defaults_;
132 };
133 
134 #endif // NINJA_STATE_H_
Pool(const string &name, int depth)
Definition: state.h:40
vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:128
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
bool ShouldDelayEdge() const
true if the Pool might delay this edge
Definition: state.h:49
vector< Node * > defaults_
Definition: state.h:131
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:137
DelayedEdges delayed_
Definition: state.h:79
int depth() const
Definition: state.h:45
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:35
An invokable build command and associated metadata (description, etc.).
Definition: graph.h:116
A template for hash_maps keyed by a StringPiece whose string is owned externally (typically by the va...
Definition: hash_map.h:101
A pool for delayed edges.
Definition: state.h:39
BindingEnv bindings_
Definition: state.h:130
const string & name() const
Definition: state.h:46
set< Edge *, bool(*)(const Edge *, const Edge *)> DelayedEdges
Definition: state.h:78
ExternalStringHashMap< Node * >::Type Paths
Mapping of path -> Node.
Definition: state.h:118
bool is_valid() const
Definition: state.h:44
string name_
Definition: state.h:69
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
map< string, Pool * > pools_
All the pools used in the graph.
Definition: state.h:125
static Pool kDefaultPool
Definition: state.h:84
map< string, const Rule * > rules_
All the rules used in the graph.
Definition: state.h:122
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