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
23#include "eval_env.h"
24#include "graph.h"
25#include "hash_map.h"
26#include "util.h"
27
28struct Edge;
29struct Node;
30struct Rule;
31
32/// A pool for delayed edges.
33/// Pools are scoped to a State. Edges within a State will share Pools. A Pool
34/// will keep a count of the total 'weight' of the currently scheduled edges. If
35/// a Plan attempts to schedule an Edge which would cause the total weight to
36/// exceed the depth of the Pool, the Pool will enqueue the Edge instead of
37/// allowing the Plan to schedule it. The Pool will relinquish queued Edges when
38/// the total scheduled weight diminishes enough (i.e. when a scheduled edge
39/// completes).
40struct Pool {
41 Pool(const std::string& name, int depth)
43
44 // A depth of 0 is infinite
45 bool is_valid() const { return depth_ >= 0; }
46 int depth() const { return depth_; }
47 const std::string& name() const { return name_; }
48 int current_use() const { return current_use_; }
49
50 /// true if the Pool might delay this edge
51 bool ShouldDelayEdge() const { return depth_ != 0; }
52
53 /// informs this Pool that the given edge is committed to be run.
54 /// Pool will count this edge as using resources from this pool.
55 void EdgeScheduled(const Edge& edge);
56
57 /// informs this Pool that the given edge is no longer runnable, and should
58 /// relinquish its resources back to the pool
59 void EdgeFinished(const Edge& edge);
60
61 /// adds the given edge to this Pool to be delayed.
62 void DelayEdge(Edge* edge);
63
64 /// Pool will add zero or more edges to the ready_queue
65 void RetrieveReadyEdges(EdgePriorityQueue* ready_queue);
66
67 /// Dump the Pool and its edges (useful for debugging).
68 void Dump() const;
69
70 private:
71 std::string name_;
72
73 /// |current_use_| is the total of the weights of the edges which are
74 /// currently scheduled in the Plan (i.e. the edges in Plan::ready_).
76 int depth_;
77
79 bool operator()(const Edge* a, const Edge* b) const {
80 if (!a) return b;
81 if (!b) return false;
82 int weight_diff = a->weight() - b->weight();
83 if (weight_diff != 0) {
84 return weight_diff < 0;
85 }
86 return EdgePriorityGreater()(a, b);
87 }
88 };
89
90 typedef std::set<Edge*, WeightedEdgeCmp> DelayedEdges;
92};
93
94/// Global state (file status) for a single run.
95struct State {
98
99 State();
100
101 void AddPool(Pool* pool);
102 Pool* LookupPool(const std::string& pool_name);
103
104 Edge* AddEdge(const Rule* rule);
105
106 Node* GetNode(StringPiece path, uint64_t slash_bits);
107 Node* LookupNode(StringPiece path) const;
108 Node* SpellcheckNode(const std::string& path);
109
110 /// Add input / output / validation nodes to a given edge. This also
111 /// ensures that the generated_by_dep_loader() flag for all these nodes
112 /// is set to false, to indicate that they come from the input manifest.
113 void AddIn(Edge* edge, StringPiece path, uint64_t slash_bits);
114 bool AddOut(Edge* edge, StringPiece path, uint64_t slash_bits, std::string* err);
115 void AddValidation(Edge* edge, StringPiece path, uint64_t slash_bits);
116 bool AddDefault(StringPiece path, std::string* error);
117
118 /// Reset state. Keeps all nodes and edges, but restores them to the
119 /// state where we haven't yet examined the disk for dirty state.
120 void Reset();
121
122 /// Dump the nodes and Pools (useful for debugging).
123 void Dump();
124
125 /// @return the root node(s) of the graph. (Root nodes have no output edges).
126 /// @param error where to write the error message if somethings went wrong.
127 std::vector<Node*> RootNodes(std::string* error) const;
128 std::vector<Node*> DefaultNodes(std::string* error) const;
129
130 /// Mapping of path -> Node.
133
134 /// All the pools used in the graph.
135 std::map<std::string, Pool*> pools_;
136
137 /// All the edges of the graph.
138 std::vector<Edge*> edges_;
139
141 std::vector<Node*> defaults_;
142};
143
144#endif // NINJA_STATE_H_
An Env which contains a mapping of variables to values as well as a pointer to a parent scope.
Definition eval_env.h:93
An edge in the dependency graph; links between Nodes using Rules.
Definition graph.h:175
int weight() const
Definition graph.h:232
emhash8::HashMap< StringPiece, V > Type
Definition hash_map.h:44
Information about a node in the dependency graph: the file, whether it's dirty, mtime,...
Definition graph.h:42
bool operator()(const Edge *a, const Edge *b) const
Definition state.h:79
A pool for delayed edges.
Definition state.h:40
int depth_
Definition state.h:76
DelayedEdges delayed_
Definition state.h:91
bool ShouldDelayEdge() const
true if the Pool might delay this edge
Definition state.h:51
void DelayEdge(Edge *edge)
adds the given edge to this Pool to be delayed.
Definition state.cc:36
Pool(const std::string &name, int depth)
Definition state.h:41
void RetrieveReadyEdges(EdgePriorityQueue *ready_queue)
Pool will add zero or more edges to the ready_queue.
Definition state.cc:41
int current_use() const
Definition state.h:48
void EdgeScheduled(const Edge &edge)
informs this Pool that the given edge is committed to be run.
Definition state.cc:26
const std::string & name() const
Definition state.h:47
int depth() const
Definition state.h:46
std::string name_
Definition state.h:71
bool is_valid() const
Definition state.h:45
std::set< Edge *, WeightedEdgeCmp > DelayedEdges
Definition state.h:90
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
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:75
void Dump() const
Dump the Pool and its edges (useful for debugging).
Definition state.cc:54
An invocable build command and associated metadata (description, etc.).
Definition eval_env.h:66
std::vector< Node * > RootNodes(std::string *error) const
Definition state.cc:169
void AddValidation(Edge *edge, StringPiece path, uint64_t slash_bits)
Definition state.cc:152
std::vector< Edge * > edges_
All the edges of the graph.
Definition state.h:138
static Pool kConsolePool
Definition state.h:97
std::vector< Node * > DefaultNodes(std::string *error) const
Definition state.cc:187
bool AddDefault(StringPiece path, std::string *error)
Definition state.cc:159
void AddPool(Pool *pool)
Definition state.cc:73
Edge * AddEdge(const Rule *rule)
Definition state.cc:85
Node * SpellcheckNode(const std::string &path)
Definition state.cc:111
Paths paths_
Definition state.h:132
Node * GetNode(StringPiece path, uint64_t slash_bits)
Definition state.cc:95
static Pool kDefaultPool
Definition state.h:96
void AddIn(Edge *edge, StringPiece path, uint64_t slash_bits)
Add input / output / validation nodes to a given edge.
Definition state.cc:128
Node * LookupNode(StringPiece path) const
Definition state.cc:104
State()
Definition state.cc:67
BindingEnv bindings_
Definition state.h:140
std::map< std::string, Pool * > pools_
All the pools used in the graph.
Definition state.h:135
Pool * LookupPool(const std::string &pool_name)
Definition state.cc:78
void Reset()
Reset state.
Definition state.cc:191
std::vector< Node * > defaults_
Definition state.h:141
void Dump()
Dump the nodes and Pools (useful for debugging).
Definition state.cc:201
ExternalStringHashMap< Node * >::Type Paths
Mapping of path -> Node.
Definition state.h:131
bool AddOut(Edge *edge, StringPiece path, uint64_t slash_bits, std::string *err)
Definition state.cc:135
StringPiece represents a slice of a string whose memory is managed externally.
unsigned long long uint64_t
Definition win32port.h:29