blob: 87f8e7de516d54ec18e02e04e249f03239295f57 [file] [log] [blame]
Nathan Harmata593dc522016-09-28 23:35:46 +00001// Copyright 2016 The Bazel Authors. 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.
14package com.google.devtools.build.lib.query2;
15
laurentlb3d2a68c2017-06-30 00:32:04 +020016import static com.google.common.collect.ImmutableSet.toImmutableSet;
17
Googler2b503882016-11-28 21:54:43 +000018import com.google.common.annotations.VisibleForTesting;
Googlerb3610d52016-10-24 19:18:36 +000019import com.google.common.base.Preconditions;
Nathan Harmataf44211c2016-10-10 16:31:18 +000020import com.google.common.collect.ArrayListMultimap;
Nathan Harmata593dc522016-09-28 23:35:46 +000021import com.google.common.collect.ImmutableList;
22import com.google.common.collect.Iterables;
Googler13dc56a2016-12-07 15:49:20 +000023import com.google.common.collect.ListMultimap;
Googlerb3610d52016-10-24 19:18:36 +000024import com.google.common.collect.Maps;
Nathan Harmata41b54172016-11-10 18:54:09 +000025import com.google.common.collect.Multimap;
laurentlb3d2a68c2017-06-30 00:32:04 +020026import com.google.common.collect.Streams;
Nathan Harmata593dc522016-09-28 23:35:46 +000027import com.google.devtools.build.lib.cmdline.Label;
Nathan Harmataf44211c2016-10-10 16:31:18 +000028import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Nathan Harmata593dc522016-09-28 23:35:46 +000029import com.google.devtools.build.lib.collect.CompactHashSet;
Nathan Harmata41b54172016-11-10 18:54:09 +000030import com.google.devtools.build.lib.concurrent.MultisetSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +000031import com.google.devtools.build.lib.packages.Target;
32import com.google.devtools.build.lib.query2.engine.Callback;
Nathan Harmata7a5a2362017-03-08 22:42:01 +000033import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryTaskFuture;
Nathan Harmata593dc522016-09-28 23:35:46 +000034import com.google.devtools.build.lib.query2.engine.QueryException;
35import com.google.devtools.build.lib.query2.engine.QueryExpression;
Nathan Harmata7a5a2362017-03-08 22:42:01 +000036import com.google.devtools.build.lib.query2.engine.Uniquifier;
Nathan Harmata593dc522016-09-28 23:35:46 +000037import com.google.devtools.build.lib.query2.engine.VariableContext;
38import com.google.devtools.build.lib.skyframe.PackageValue;
39import com.google.devtools.build.lib.skyframe.SkyFunctions;
Googlerb3610d52016-10-24 19:18:36 +000040import com.google.devtools.build.lib.util.Pair;
Nathan Harmata593dc522016-09-28 23:35:46 +000041import com.google.devtools.build.lib.vfs.PathFragment;
42import com.google.devtools.build.skyframe.SkyKey;
Googlerb3610d52016-10-24 19:18:36 +000043import java.util.ArrayList;
Nathan Harmata593dc522016-09-28 23:35:46 +000044import java.util.Collection;
Googlerb3610d52016-10-24 19:18:36 +000045import java.util.LinkedList;
46import java.util.Map;
Nathan Harmata593dc522016-09-28 23:35:46 +000047import java.util.Set;
Nathan Harmata593dc522016-09-28 23:35:46 +000048
Nathan Harmata593dc522016-09-28 23:35:46 +000049/**
50 * Parallel implementations of various functionality in {@link SkyQueryEnvironment}.
51 *
52 * <p>Special attention is given to memory usage. Naive parallel implementations of query
53 * functionality would lead to memory blowup. Instead of dealing with {@link Target}s, we try to
54 * deal with {@link SkyKey}s as much as possible to reduce the number of {@link Package}s forcibly
55 * in memory at any given time.
56 */
57// TODO(bazel-team): Be more deliberate about bounding memory usage here.
58class ParallelSkyQueryUtils {
Googler2b503882016-11-28 21:54:43 +000059
60 /** The maximum number of keys to visit at once. */
61 @VisibleForTesting static final int VISIT_BATCH_SIZE = 10000;
62
Nathan Harmata593dc522016-09-28 23:35:46 +000063 private ParallelSkyQueryUtils() {
64 }
65
66 /**
67 * Specialized parallel variant of {@link SkyQueryEnvironment#getAllRdeps} that is appropriate
68 * when there is no depth-bound.
69 */
Nathan Harmata7a5a2362017-03-08 22:42:01 +000070 static QueryTaskFuture<Void> getAllRdepsUnboundedParallel(
Nathan Harmata593dc522016-09-28 23:35:46 +000071 SkyQueryEnvironment env,
72 QueryExpression expression,
73 VariableContext<Target> context,
Nathan Harmata7a5a2362017-03-08 22:42:01 +000074 Callback<Target> callback,
75 MultisetSemaphore<PackageIdentifier> packageSemaphore) {
76 return env.eval(
Nathan Harmata593dc522016-09-28 23:35:46 +000077 expression,
78 context,
Googler7184b6f2017-05-16 05:25:49 +020079 ParallelVisitor.createParallelVisitorCallback(
Googler2b503882016-11-28 21:54:43 +000080 new AllRdepsUnboundedVisitor.Factory(env, callback, packageSemaphore)));
Nathan Harmata593dc522016-09-28 23:35:46 +000081 }
82
83 /** Specialized parallel variant of {@link SkyQueryEnvironment#getRBuildFiles}. */
84 static void getRBuildFilesParallel(
85 SkyQueryEnvironment env,
86 Collection<PathFragment> fileIdentifiers,
Nathan Harmata7a5a2362017-03-08 22:42:01 +000087 Callback<Target> callback,
Nathan Harmata41b54172016-11-10 18:54:09 +000088 MultisetSemaphore<PackageIdentifier> packageSemaphore)
Nathan Harmata593dc522016-09-28 23:35:46 +000089 throws QueryException, InterruptedException {
Nathan Harmata7a5a2362017-03-08 22:42:01 +000090 Uniquifier<SkyKey> keyUniquifier = env.createSkyKeyUniquifier();
Nathan Harmata41b54172016-11-10 18:54:09 +000091 RBuildFilesVisitor visitor =
Googler2b503882016-11-28 21:54:43 +000092 new RBuildFilesVisitor(env, keyUniquifier, callback, packageSemaphore);
Nathan Harmata593dc522016-09-28 23:35:46 +000093 visitor.visitAndWaitForCompletion(env.getSkyKeysForFileFragments(fileIdentifiers));
94 }
95
96 /** A helper class that computes 'rbuildfiles(<blah>)' via BFS. */
Googler7184b6f2017-05-16 05:25:49 +020097 private static class RBuildFilesVisitor extends ParallelVisitor<SkyKey> {
98 private final SkyQueryEnvironment env;
Nathan Harmata41b54172016-11-10 18:54:09 +000099 private final MultisetSemaphore<PackageIdentifier> packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000100
101 private RBuildFilesVisitor(
102 SkyQueryEnvironment env,
Nathan Harmata7a5a2362017-03-08 22:42:01 +0000103 Uniquifier<SkyKey> uniquifier,
Nathan Harmata41b54172016-11-10 18:54:09 +0000104 Callback<Target> callback,
105 MultisetSemaphore<PackageIdentifier> packageSemaphore) {
Googler7184b6f2017-05-16 05:25:49 +0200106 super(uniquifier, callback, VISIT_BATCH_SIZE);
107 this.env = env;
Nathan Harmata41b54172016-11-10 18:54:09 +0000108 this.packageSemaphore = packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000109 }
110
111 @Override
112 protected Visit getVisitResult(Iterable<SkyKey> values) throws InterruptedException {
113 Collection<Iterable<SkyKey>> reverseDeps = env.graph.getReverseDeps(values).values();
114 Set<SkyKey> keysToUseForResult = CompactHashSet.create();
115 Set<SkyKey> keysToVisitNext = CompactHashSet.create();
116 for (SkyKey rdep : Iterables.concat(reverseDeps)) {
117 if (rdep.functionName().equals(SkyFunctions.PACKAGE)) {
118 keysToUseForResult.add(rdep);
119 // Every package has a dep on the external package, so we need to include those edges too.
120 if (rdep.equals(PackageValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER))) {
121 keysToVisitNext.add(rdep);
122 }
123 } else if (!rdep.functionName().equals(SkyFunctions.PACKAGE_LOOKUP)) {
124 // Packages may depend on the existence of subpackages, but these edges aren't relevant to
125 // rbuildfiles.
126 keysToVisitNext.add(rdep);
127 }
128 }
129 return new Visit(keysToUseForResult, keysToVisitNext);
130 }
131
132 @Override
Nathan Harmata41b54172016-11-10 18:54:09 +0000133 protected void processResultantTargets(
134 Iterable<SkyKey> keysToUseForResult, Callback<Target> callback)
135 throws QueryException, InterruptedException {
136 Set<PackageIdentifier> pkgIdsNeededForResult =
laurentlb3d2a68c2017-06-30 00:32:04 +0200137 Streams.stream(keysToUseForResult)
138 .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER)
139 .collect(toImmutableSet());
Nathan Harmata41b54172016-11-10 18:54:09 +0000140 packageSemaphore.acquireAll(pkgIdsNeededForResult);
141 try {
142 callback.process(SkyQueryEnvironment.getBuildFilesForPackageValues(
143 env.graph.getSuccessfulValues(keysToUseForResult).values()));
144 } finally {
145 packageSemaphore.releaseAll(pkgIdsNeededForResult);
146 }
Nathan Harmata593dc522016-09-28 23:35:46 +0000147 }
Googlerb3610d52016-10-24 19:18:36 +0000148
149 @Override
150 protected Iterable<SkyKey> preprocessInitialVisit(Iterable<SkyKey> keys) {
151 return keys;
152 }
Nathan Harmata593dc522016-09-28 23:35:46 +0000153 }
154
Googlerb3610d52016-10-24 19:18:36 +0000155 /**
156 * A helper class that computes 'allrdeps(<blah>)' via BFS.
157 *
158 * <p>The visitor uses a pair of <node, reverse dep> to keep track the nodes to visit and avoid
159 * dealing with targetification of reverse deps until they are needed. The node itself is needed
160 * to filter out disallowed deps later. Compared against the approach using a single SkyKey, it
161 * consumes 16 more bytes in a 64-bit environment for each edge. However it defers the need to
162 * load all the packages which have at least a target as a rdep of the current batch, thus greatly
163 * reduces the risk of OOMs. The additional memory usage should not be a large concern here, as
164 * even with 10M edges, the memory overhead is around 160M, and the memory can be reclaimed by
165 * regular GC.
166 */
Googler7184b6f2017-05-16 05:25:49 +0200167 private static class AllRdepsUnboundedVisitor extends ParallelVisitor<Pair<SkyKey, SkyKey>> {
168 private final SkyQueryEnvironment env;
Nathan Harmata41b54172016-11-10 18:54:09 +0000169 private final MultisetSemaphore<PackageIdentifier> packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000170
171 private AllRdepsUnboundedVisitor(
172 SkyQueryEnvironment env,
Nathan Harmata7a5a2362017-03-08 22:42:01 +0000173 Uniquifier<Pair<SkyKey, SkyKey>> uniquifier,
174 Callback<Target> callback,
Nathan Harmata41b54172016-11-10 18:54:09 +0000175 MultisetSemaphore<PackageIdentifier> packageSemaphore) {
Googler7184b6f2017-05-16 05:25:49 +0200176 super(uniquifier, callback, VISIT_BATCH_SIZE);
177 this.env = env;
Nathan Harmata41b54172016-11-10 18:54:09 +0000178 this.packageSemaphore = packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000179 }
180
181 /**
182 * A {@link Factory} for {@link AllRdepsUnboundedVisitor} instances, each of which will be used
183 * to perform visitation of the reverse transitive closure of the {@link Target}s passed in a
Nathan Harmata7a5a2362017-03-08 22:42:01 +0000184 * single {@link Callback#process} call. Note that all the created instances share the same
185 * {@link Uniquifier} so that we don't visit the same Skyframe node more than once.
Nathan Harmata593dc522016-09-28 23:35:46 +0000186 */
Googler7184b6f2017-05-16 05:25:49 +0200187 private static class Factory implements ParallelVisitor.Factory {
Nathan Harmata593dc522016-09-28 23:35:46 +0000188 private final SkyQueryEnvironment env;
Nathan Harmata7a5a2362017-03-08 22:42:01 +0000189 private final Uniquifier<Pair<SkyKey, SkyKey>> uniquifier;
190 private final Callback<Target> callback;
Nathan Harmata41b54172016-11-10 18:54:09 +0000191 private final MultisetSemaphore<PackageIdentifier> packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000192
193 private Factory(
194 SkyQueryEnvironment env,
Nathan Harmata7a5a2362017-03-08 22:42:01 +0000195 Callback<Target> callback,
Nathan Harmata41b54172016-11-10 18:54:09 +0000196 MultisetSemaphore<PackageIdentifier> packageSemaphore) {
Nathan Harmata593dc522016-09-28 23:35:46 +0000197 this.env = env;
Googlerb3610d52016-10-24 19:18:36 +0000198 this.uniquifier = env.createReverseDepSkyKeyUniquifier();
Nathan Harmata593dc522016-09-28 23:35:46 +0000199 this.callback = callback;
Nathan Harmata41b54172016-11-10 18:54:09 +0000200 this.packageSemaphore = packageSemaphore;
Nathan Harmata593dc522016-09-28 23:35:46 +0000201 }
202
203 @Override
Googler7184b6f2017-05-16 05:25:49 +0200204 public ParallelVisitor<Pair<SkyKey, SkyKey>> create() {
Googler2b503882016-11-28 21:54:43 +0000205 return new AllRdepsUnboundedVisitor(env, uniquifier, callback, packageSemaphore);
Nathan Harmata593dc522016-09-28 23:35:46 +0000206 }
207 }
208
209 @Override
Googlerb3610d52016-10-24 19:18:36 +0000210 protected Visit getVisitResult(Iterable<Pair<SkyKey, SkyKey>> keys)
211 throws InterruptedException {
212 Collection<SkyKey> filteredKeys = new ArrayList<>();
Nathan Harmatabe597992016-10-10 15:59:00 +0000213
Googlerb3610d52016-10-24 19:18:36 +0000214 // Build a raw reverse dep map from pairs of SkyKeys to filter out the disallowed deps.
215 Map<SkyKey, Collection<SkyKey>> reverseDepsMap = Maps.newHashMap();
216 for (Pair<SkyKey, SkyKey> reverseDepPair : keys) {
217 // First-level nodes do not have a parent node (they may have one in Skyframe but we do not
218 // need to retrieve them.
219 if (reverseDepPair.first == null) {
220 filteredKeys.add(Preconditions.checkNotNull(reverseDepPair.second));
221 continue;
222 }
223
laurentlb3d2a68c2017-06-30 00:32:04 +0200224 reverseDepsMap.computeIfAbsent(reverseDepPair.first, k -> new LinkedList<SkyKey>());
Googlerb3610d52016-10-24 19:18:36 +0000225
226 reverseDepsMap.get(reverseDepPair.first).add(reverseDepPair.second);
Nathan Harmataf44211c2016-10-10 16:31:18 +0000227 }
Googlerb3610d52016-10-24 19:18:36 +0000228
Nathan Harmata41b54172016-11-10 18:54:09 +0000229 Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap =
230 env.makePackageKeyToTargetKeyMap(Iterables.concat(reverseDepsMap.values()));
231 Set<PackageIdentifier> pkgIdsNeededForTargetification =
laurentlb3d2a68c2017-06-30 00:32:04 +0200232 packageKeyToTargetKeyMap
233 .keySet()
234 .stream()
235 .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER)
236 .collect(toImmutableSet());
Nathan Harmata41b54172016-11-10 18:54:09 +0000237 packageSemaphore.acquireAll(pkgIdsNeededForTargetification);
238
239 try {
240 // Filter out disallowed deps. We cannot defer the targetification any further as we do not
241 // want to retrieve the rdeps of unwanted nodes (targets).
242 if (!reverseDepsMap.isEmpty()) {
243 Collection<Target> filteredTargets =
244 env.filterRawReverseDepsOfTransitiveTraversalKeys(
245 reverseDepsMap, packageKeyToTargetKeyMap);
laurentlb3d2a68c2017-06-30 00:32:04 +0200246 filteredTargets
247 .stream()
248 .map(SkyQueryEnvironment.TARGET_TO_SKY_KEY)
249 .forEachOrdered(filteredKeys::add);
Nathan Harmata41b54172016-11-10 18:54:09 +0000250 }
251 } finally {
252 packageSemaphore.releaseAll(pkgIdsNeededForTargetification);
Googlerb3610d52016-10-24 19:18:36 +0000253 }
254
255 // Retrieve the reverse deps as SkyKeys and defer the targetification and filtering to next
256 // recursive visitation.
257 Map<SkyKey, Iterable<SkyKey>> unfilteredReverseDeps = env.graph.getReverseDeps(filteredKeys);
258
Googler2b503882016-11-28 21:54:43 +0000259 ImmutableList.Builder<Pair<SkyKey, SkyKey>> builder = ImmutableList.builder();
Googlerb3610d52016-10-24 19:18:36 +0000260 for (Map.Entry<SkyKey, Iterable<SkyKey>> rdeps : unfilteredReverseDeps.entrySet()) {
261 for (SkyKey rdep : rdeps.getValue()) {
262 Label label = SkyQueryEnvironment.SKYKEY_TO_LABEL.apply(rdep);
263 if (label != null) {
Googler2b503882016-11-28 21:54:43 +0000264 builder.add(Pair.of(rdeps.getKey(), rdep));
Googlerb3610d52016-10-24 19:18:36 +0000265 }
266 }
267 }
268
Googler2b503882016-11-28 21:54:43 +0000269 return new Visit(/*keysToUseForResult=*/ filteredKeys, /*keysToVisit=*/ builder.build());
Nathan Harmata593dc522016-09-28 23:35:46 +0000270 }
271
272 @Override
Nathan Harmata41b54172016-11-10 18:54:09 +0000273 protected void processResultantTargets(
274 Iterable<SkyKey> keysToUseForResult, Callback<Target> callback)
275 throws QueryException, InterruptedException {
276 Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap =
277 env.makePackageKeyToTargetKeyMap(keysToUseForResult);
278 Set<PackageIdentifier> pkgIdsNeededForResult =
laurentlb3d2a68c2017-06-30 00:32:04 +0200279 packageKeyToTargetKeyMap
280 .keySet()
281 .stream()
282 .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER)
283 .collect(toImmutableSet());
Nathan Harmata41b54172016-11-10 18:54:09 +0000284 packageSemaphore.acquireAll(pkgIdsNeededForResult);
285 try {
286 callback.process(
287 env.makeTargetsFromPackageKeyToTargetKeyMap(packageKeyToTargetKeyMap).values());
288 } finally {
289 packageSemaphore.releaseAll(pkgIdsNeededForResult);
290 }
Nathan Harmata593dc522016-09-28 23:35:46 +0000291 }
Googlerb3610d52016-10-24 19:18:36 +0000292
293 @Override
294 protected Iterable<Pair<SkyKey, SkyKey>> preprocessInitialVisit(Iterable<SkyKey> keys) {
laurentlb3d2a68c2017-06-30 00:32:04 +0200295 return Iterables.transform(keys, key -> Pair.of(null, key));
Googlerb3610d52016-10-24 19:18:36 +0000296 }
Googler2b503882016-11-28 21:54:43 +0000297
298 @Override
299 protected Iterable<Task> getVisitTasks(Collection<Pair<SkyKey, SkyKey>> pendingKeysToVisit) {
300 // Group pending visits by package.
Googler13dc56a2016-12-07 15:49:20 +0000301 ListMultimap<PackageIdentifier, Pair<SkyKey, SkyKey>> visitsByPackage =
Googler2b503882016-11-28 21:54:43 +0000302 ArrayListMultimap.create();
303 for (Pair<SkyKey, SkyKey> visit : pendingKeysToVisit) {
304 Label label = SkyQueryEnvironment.SKYKEY_TO_LABEL.apply(visit.second);
305 if (label != null) {
306 visitsByPackage.put(label.getPackageIdentifier(), visit);
307 }
308 }
309
310 ImmutableList.Builder<Task> builder = ImmutableList.builder();
311
312 // A couple notes here:
313 // (i) ArrayListMultimap#values returns the values grouped by key, which is exactly what we
314 // want.
315 // (ii) ArrayListMultimap#values returns a Collection view, so we make a copy to avoid
316 // accidentally retaining the entire ArrayListMultimap object.
317 for (Iterable<Pair<SkyKey, SkyKey>> keysToVisitBatch :
318 Iterables.partition(ImmutableList.copyOf(visitsByPackage.values()), VISIT_BATCH_SIZE)) {
319 builder.add(new VisitTask(keysToVisitBatch));
320 }
321
322 return builder.build();
323 }
Nathan Harmata593dc522016-09-28 23:35:46 +0000324 }
Nathan Harmata593dc522016-09-28 23:35:46 +0000325}
326