Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.skyframe; |
| 15 | |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [diff] [blame] | 16 | import com.google.common.base.MoreObjects; |
Janak Ramakrishnan | 2f55dd8 | 2015-08-03 19:08:11 +0000 | [diff] [blame] | 17 | import com.google.common.base.MoreObjects.ToStringHelper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.util.Collections; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 27 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | * |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 32 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | * caller must synchronize access to this class. |
Nathan Harmata | bd05aa6 | 2015-02-24 01:28:11 +0000 | [diff] [blame] | 36 | * |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 37 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | */ |
| 61 | @ThreadCompatible |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 62 | class BuildingState { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | /** |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 64 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | * |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 70 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | * |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 74 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | * |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 77 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 84 | int signaledDeps = -1; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | |
| 86 | /** |
Janak Ramakrishnan | 772b5bb | 2016-06-29 00:20:36 +0000 | [diff] [blame] | 87 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | */ |
Janak Ramakrishnan | 772b5bb | 2016-06-29 00:20:36 +0000 | [diff] [blame] | 91 | protected Object reverseDepsToSignal = ImmutableList.of(); |
Janak Ramakrishnan | 441dcb1 | 2015-10-09 22:44:31 +0000 | [diff] [blame] | 92 | private List<Object> reverseDepsDataToConsolidate = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | |
| 94 | private static final ReverseDepsUtil<BuildingState> REVERSE_DEPS_UTIL = |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 95 | new ReverseDepsUtilImpl<BuildingState>() { |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 96 | @Override |
| 97 | void setReverseDepsObject(BuildingState container, Object object) { |
| 98 | container.reverseDepsToSignal = object; |
| 99 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 101 | @Override |
Janak Ramakrishnan | 441dcb1 | 2015-10-09 22:44:31 +0000 | [diff] [blame] | 102 | void setDataToConsolidate(BuildingState container, List<Object> dataToConsolidate) { |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 103 | container.reverseDepsDataToConsolidate = dataToConsolidate; |
| 104 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 106 | @Override |
| 107 | Object getReverseDepsObject(BuildingState container) { |
| 108 | return container.reverseDepsToSignal; |
| 109 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 111 | @Override |
Janak Ramakrishnan | 441dcb1 | 2015-10-09 22:44:31 +0000 | [diff] [blame] | 112 | List<Object> getDataToConsolidate(BuildingState container) { |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 113 | return container.reverseDepsDataToConsolidate; |
| 114 | } |
Mark Schaller | d2eff4e | 2015-11-24 01:20:17 +0000 | [diff] [blame] | 115 | |
| 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 Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 121 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 123 | /** Returns whether all known children of this node have signaled that they are done. */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 124 | final boolean isReady(int numDirectDeps) { |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 125 | Preconditions.checkState(signaledDeps <= numDirectDeps, "%s %s", numDirectDeps, this); |
| 126 | return signaledDeps == numDirectDeps; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | } |
| 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 Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 136 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns true if the entry is known to require re-evaluation. |
| 141 | * |
| 142 | * @see NodeEntry#isChanged() |
| 143 | */ |
| 144 | boolean isChanged() { |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 145 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Helper method to assert that node has finished building, as far as we can tell. We would |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 150 | * actually like to check that the node has been evaluated, but that is not available in this |
| 151 | * context. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 152 | */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 153 | protected void checkFinishedBuildingWhenAboutToSetValue() { |
| 154 | Preconditions.checkState(isEvaluating(), "not started building %s", this); |
| 155 | Preconditions.checkState(!isDirty(), "not done building %s", this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 159 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 162 | */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 163 | final boolean startEvaluating() { |
| 164 | boolean result = !isEvaluating(); |
| 165 | if (result) { |
| 166 | signaledDeps = 0; |
| 167 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | return result; |
| 169 | } |
| 170 | |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 171 | final boolean isEvaluating() { |
| 172 | return signaledDeps > -1; |
| 173 | } |
| 174 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 175 | /** |
| 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 Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 179 | * <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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 181 | * |
| 182 | * @see NodeEntry#signalDep(Version) |
| 183 | */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 184 | final boolean signalDep(boolean childChanged, int numDirectDeps) { |
| 185 | Preconditions.checkState(isEvaluating(), this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 186 | signaledDeps++; |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 187 | signalDepInternal(childChanged, numDirectDeps); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 188 | return isReady(numDirectDeps); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 191 | void signalDepInternal(boolean childChanged, int numDirectDeps) {} |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 192 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | /** |
| 194 | * Returns reverse deps to signal that have been registered this build. |
| 195 | * |
| 196 | * @see NodeEntry#getReverseDeps() |
| 197 | */ |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 198 | final ImmutableSet<SkyKey> getReverseDepsToSignal() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | 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 Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 207 | final void addReverseDepToSignal(SkyKey newReverseDep) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 208 | REVERSE_DEPS_UTIL.addReverseDeps(this, Collections.singleton(newReverseDep)); |
| 209 | } |
| 210 | |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 211 | /** @see NodeEntry#removeReverseDep(SkyKey) */ |
| 212 | final void removeReverseDepToSignal(SkyKey reverseDep) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | REVERSE_DEPS_UTIL.removeReverseDep(this, reverseDep); |
| 214 | } |
| 215 | |
Janak Ramakrishnan | 2f55dd8 | 2015-08-03 19:08:11 +0000 | [diff] [blame] | 216 | protected ToStringHelper getStringHelper() { |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [diff] [blame] | 217 | return MoreObjects.toStringHelper(this) |
Janak Ramakrishnan | 98ff521 | 2015-09-03 18:42:24 +0000 | [diff] [blame] | 218 | .add("hash", System.identityHashCode(this)) |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 219 | .add("signaledDeps/evaluating state", signaledDeps) |
| 220 | .add("reverseDepsToSignal", REVERSE_DEPS_UTIL.toString(this)); |
Janak Ramakrishnan | 2f55dd8 | 2015-08-03 19:08:11 +0000 | [diff] [blame] | 221 | } |
| 222 | @Override |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame^] | 223 | public final String toString() { |
Janak Ramakrishnan | 2f55dd8 | 2015-08-03 19:08:11 +0000 | [diff] [blame] | 224 | return getStringHelper().toString(); |
| 225 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | } |