blob: e931a12c4a8aa2f7d2dbc7bbb178626e172f691c [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.skyframe;
15
Googler2c19a572015-07-16 08:38:49 +000016import com.google.common.base.MoreObjects;
Janak Ramakrishnan2f55dd82015-08-03 19:08:11 +000017import com.google.common.base.MoreObjects.ToStringHelper;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
20import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible;
Mark Schaller6df81792015-12-10 18:47:47 +000021import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import java.util.Collections;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025
26/**
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000027 * Data the NodeEntry uses to maintain its state before it is done building. It allows the {@link
28 * NodeEntry} to keep the current state of the entry across invalidation and successive evaluations.
29 * A done node does not contain any of this data. However, if a node is marked dirty, its entry
30 * acquires a new {@link DirtyBuildingState} object, which persists until it is done again.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031 *
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000032 * <p>This class should be considered a private inner class of {@link InMemoryNodeEntry} -- no other
33 * classes should instantiate a {@code BuildingState} object or call any of its methods directly. It
34 * is in a separate file solely to keep the {@link NodeEntry} class readable. In particular, the
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 * caller must synchronize access to this class.
Nathan Harmatabd05aa62015-02-24 01:28:11 +000036 *
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000037 * <p>During its life, a node can go through states as follows:
38 *
39 * <ol>
40 * <li>Non-existent
41 * <li>Just created ({@link #isEvaluating} is false)
42 * <li>Evaluating ({@link #isEvaluating} is true)
43 * <li>Done (meaning this buildingState object is null)
44 * <li>Just created (when it is dirtied during evaluation)
45 * <li>Reset (just before it is re-evaluated)
46 * <li>Evaluating
47 * <li>Done
48 * </ol>
49 *
50 * <p>The "just created" state is there to allow the {@link EvaluableGraph#createIfAbsentBatch} and
51 * {@link NodeEntry#addReverseDepAndCheckIfDone} methods to be separate. All callers have to call
52 * both methods in that order if they want to create a node. The second method calls {@link
53 * #startEvaluating}, which transitions the current node to the "evaluating" state and returns true
54 * only the first time it was called. A caller that gets "true" back from that call must start the
55 * evaluation of this node, while any subsequent callers must not.
56 *
57 * <p>An entry is set to "evaluating" as soon as it is scheduled for evaluation. Thus, even a node
58 * that is never actually built (for instance, a dirty node that is verified as clean) is in the
59 * "evaluating" state until it is done.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 */
61@ThreadCompatible
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000062class BuildingState {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063 /**
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000064 * The number of dependencies that are known to be done in a {@link NodeEntry} if it is already
65 * evaluating, and a sentinel (-1) indicating that it has not yet started evaluating otherwise.
66 * There is a potential check-then-act race here during evaluation, so we need to make sure that
67 * when this is increased, we always check if the new value is equal to the number of required
68 * dependencies, and if so, we must re-schedule the node for evaluation.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069 *
Janak Ramakrishnan50934992016-07-06 17:09:51 +000070 * <p>There are two potential pitfalls here: 1) If multiple dependencies signal this node in close
71 * succession, this node should be scheduled exactly once. 2) If a thread is still working on this
72 * node, it should not be scheduled.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 *
Janak Ramakrishnan50934992016-07-06 17:09:51 +000074 * <p>The first problem is solved by the {@link #signalDep} method, which also returns if the node
75 * needs to be re-scheduled, and ensures that only one thread gets a true return value.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 *
Janak Ramakrishnan50934992016-07-06 17:09:51 +000077 * <p>The second problem is solved by first adding the newly discovered deps to a node's {@link
78 * InMemoryNodeEntry#directDeps}, and then looping through the direct deps and registering this
79 * node as a reverse dependency. This ensures that the signaledDeps counter can only reach {@link
80 * InMemoryNodeEntry#directDeps#numElements} on the very last iteration of the loop, i.e., the
81 * thread is not working on the node anymore. Note that this requires that there is no code after
82 * the loop in {@code ParallelEvaluator.Evaluate#run}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010083 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +000084 int signaledDeps = -1;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010085
86 /**
Janak Ramakrishnan772b5bb2016-06-29 00:20:36 +000087 * The set of reverse dependencies that are registered before the node has finished building. Upon
88 * building, these reverse deps will be signaled and then stored in the permanent {@link
89 * InMemoryNodeEntry#reverseDeps}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010090 */
Janak Ramakrishnan772b5bb2016-06-29 00:20:36 +000091 protected Object reverseDepsToSignal = ImmutableList.of();
Janak Ramakrishnan441dcb12015-10-09 22:44:31 +000092 private List<Object> reverseDepsDataToConsolidate = null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010093
94 private static final ReverseDepsUtil<BuildingState> REVERSE_DEPS_UTIL =
Janak Ramakrishnan4f487f42015-11-23 18:51:55 +000095 new ReverseDepsUtilImpl<BuildingState>() {
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +000096 @Override
97 void setReverseDepsObject(BuildingState container, Object object) {
98 container.reverseDepsToSignal = object;
99 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000101 @Override
Janak Ramakrishnan441dcb12015-10-09 22:44:31 +0000102 void setDataToConsolidate(BuildingState container, List<Object> dataToConsolidate) {
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000103 container.reverseDepsDataToConsolidate = dataToConsolidate;
104 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100105
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000106 @Override
107 Object getReverseDepsObject(BuildingState container) {
108 return container.reverseDepsToSignal;
109 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000111 @Override
Janak Ramakrishnan441dcb12015-10-09 22:44:31 +0000112 List<Object> getDataToConsolidate(BuildingState container) {
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000113 return container.reverseDepsDataToConsolidate;
114 }
Mark Schallerd2eff4e2015-11-24 01:20:17 +0000115
116 @Override
117 public void consolidateReverseDeps(BuildingState container) {
118 // #consolidateReverseDeps is only supported for node entries, not building states.
119 throw new UnsupportedOperationException();
120 }
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000121 };
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100122
Janak Ramakrishnan50934992016-07-06 17:09:51 +0000123 /** Returns whether all known children of this node have signaled that they are done. */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000124 final boolean isReady(int numDirectDeps) {
Janak Ramakrishnan50934992016-07-06 17:09:51 +0000125 Preconditions.checkState(signaledDeps <= numDirectDeps, "%s %s", numDirectDeps, this);
126 return signaledDeps == numDirectDeps;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100127 }
128
129 /**
130 * Returns true if the entry is marked dirty, meaning that at least one of its transitive
131 * dependencies is marked changed.
132 *
133 * @see NodeEntry#isDirty()
134 */
135 boolean isDirty() {
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000136 return false;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100137 }
138
139 /**
140 * Returns true if the entry is known to require re-evaluation.
141 *
142 * @see NodeEntry#isChanged()
143 */
144 boolean isChanged() {
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000145 return false;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100146 }
147
148 /**
149 * Helper method to assert that node has finished building, as far as we can tell. We would
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000150 * actually like to check that the node has been evaluated, but that is not available in this
151 * context.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100152 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000153 protected void checkFinishedBuildingWhenAboutToSetValue() {
154 Preconditions.checkState(isEvaluating(), "not started building %s", this);
155 Preconditions.checkState(!isDirty(), "not done building %s", this);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100156 }
157
158 /**
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000159 * Puts the node in the "evaluating" state if it is not already in it. Returns true if the node
160 * wasn't already evaluating and false otherwise. Should only be called by {@link
161 * NodeEntry#addReverseDepAndCheckIfDone}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100162 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000163 final boolean startEvaluating() {
164 boolean result = !isEvaluating();
165 if (result) {
166 signaledDeps = 0;
167 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100168 return result;
169 }
170
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000171 final boolean isEvaluating() {
172 return signaledDeps > -1;
173 }
174
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100175 /**
176 * Increments the number of children known to be finished. Returns true if the number of children
177 * finished is equal to the number of known children.
178 *
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000179 * <p>If the node is dirty and checking its deps for changes, this also updates dirty state as
180 * needed, via {@link #signalDepInternal}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100181 *
182 * @see NodeEntry#signalDep(Version)
183 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000184 final boolean signalDep(boolean childChanged, int numDirectDeps) {
185 Preconditions.checkState(isEvaluating(), this);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100186 signaledDeps++;
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000187 signalDepInternal(childChanged, numDirectDeps);
Janak Ramakrishnan50934992016-07-06 17:09:51 +0000188 return isReady(numDirectDeps);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100189 }
190
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000191 void signalDepInternal(boolean childChanged, int numDirectDeps) {}
Janak Ramakrishnan5877b8b2015-09-22 17:37:10 +0000192
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100193 /**
194 * Returns reverse deps to signal that have been registered this build.
195 *
196 * @see NodeEntry#getReverseDeps()
197 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000198 final ImmutableSet<SkyKey> getReverseDepsToSignal() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100199 return REVERSE_DEPS_UTIL.getReverseDeps(this);
200 }
201
202 /**
203 * Adds a reverse dependency that should be notified when this entry is done.
204 *
205 * @see NodeEntry#addReverseDepAndCheckIfDone(SkyKey)
206 */
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000207 final void addReverseDepToSignal(SkyKey newReverseDep) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100208 REVERSE_DEPS_UTIL.addReverseDeps(this, Collections.singleton(newReverseDep));
209 }
210
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000211 /** @see NodeEntry#removeReverseDep(SkyKey) */
212 final void removeReverseDepToSignal(SkyKey reverseDep) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100213 REVERSE_DEPS_UTIL.removeReverseDep(this, reverseDep);
214 }
215
Janak Ramakrishnan2f55dd82015-08-03 19:08:11 +0000216 protected ToStringHelper getStringHelper() {
Googler2c19a572015-07-16 08:38:49 +0000217 return MoreObjects.toStringHelper(this)
Janak Ramakrishnan98ff5212015-09-03 18:42:24 +0000218 .add("hash", System.identityHashCode(this))
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000219 .add("signaledDeps/evaluating state", signaledDeps)
220 .add("reverseDepsToSignal", REVERSE_DEPS_UTIL.toString(this));
Janak Ramakrishnan2f55dd82015-08-03 19:08:11 +0000221 }
222 @Override
Janak Ramakrishnan5dddb002016-07-06 20:07:23 +0000223 public final String toString() {
Janak Ramakrishnan2f55dd82015-08-03 19:08:11 +0000224 return getStringHelper().toString();
225 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100226}