Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.query2; |
| 15 | |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 16 | import com.google.common.annotations.VisibleForTesting; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 17 | import com.google.common.base.Function; |
| 18 | import com.google.common.base.Predicate; |
| 19 | import com.google.common.base.Predicates; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.packages.Target; |
| 24 | import com.google.devtools.build.lib.query2.engine.Callback; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryTaskFuture; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment.ThreadSafeMutableSet; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.query2.engine.QueryException; |
| 28 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.query2.engine.QueryExpressionContext; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.query2.engine.QueryUtil; |
| 31 | import com.google.devtools.build.lib.query2.engine.QueryUtil.AggregateAllCallback; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 33 | import com.google.devtools.build.skyframe.SkyKey; |
| 34 | import java.util.Collection; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 35 | import java.util.Collections; |
nharmata | 2399df0 | 2018-04-10 12:30:03 -0700 | [diff] [blame] | 36 | import java.util.Objects; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 37 | import java.util.Set; |
Googler | 45b5953 | 2018-05-03 11:10:20 -0700 | [diff] [blame] | 38 | import java.util.concurrent.ConcurrentHashMap; |
nharmata | 2399df0 | 2018-04-10 12:30:03 -0700 | [diff] [blame] | 39 | import javax.annotation.Nullable; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 40 | |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Parallel implementations of various functionality in {@link SkyQueryEnvironment}. |
| 43 | * |
| 44 | * <p>Special attention is given to memory usage. Naive parallel implementations of query |
| 45 | * functionality would lead to memory blowup. Instead of dealing with {@link Target}s, we try to |
| 46 | * deal with {@link SkyKey}s as much as possible to reduce the number of {@link Package}s forcibly |
| 47 | * in memory at any given time. |
| 48 | */ |
| 49 | // TODO(bazel-team): Be more deliberate about bounding memory usage here. |
nharmata | 2399df0 | 2018-04-10 12:30:03 -0700 | [diff] [blame] | 50 | public class ParallelSkyQueryUtils { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 51 | |
| 52 | /** The maximum number of keys to visit at once. */ |
nharmata | a72e3139 | 2019-02-19 17:07:10 -0800 | [diff] [blame] | 53 | @VisibleForTesting public static final int VISIT_BATCH_SIZE = 10000; |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 54 | |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 55 | private ParallelSkyQueryUtils() { |
| 56 | } |
| 57 | |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 58 | static QueryTaskFuture<Void> getAllRdepsUnboundedParallel( |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 59 | SkyQueryEnvironment env, |
| 60 | QueryExpression expression, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 61 | QueryExpressionContext<Target> context, |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 62 | Callback<Target> callback) { |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 63 | return env.eval( |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 64 | expression, |
| 65 | context, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 66 | ParallelVisitorUtils.createParallelVisitorCallback( |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 67 | new RdepsUnboundedVisitor.Factory( |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 68 | env, /*unfilteredUniverse=*/ Predicates.alwaysTrue(), callback))); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static QueryTaskFuture<Void> getAllRdepsBoundedParallel( |
| 72 | SkyQueryEnvironment env, |
| 73 | QueryExpression expression, |
| 74 | int depth, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 75 | QueryExpressionContext<Target> context, |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 76 | Callback<Target> callback) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 77 | return env.eval( |
| 78 | expression, |
| 79 | context, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 80 | ParallelVisitorUtils.createParallelVisitorCallback( |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 81 | new RdepsBoundedVisitor.Factory( |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 82 | env, depth, /*universe=*/ Predicates.alwaysTrue(), callback))); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static QueryTaskFuture<Void> getRdepsInUniverseUnboundedParallel( |
| 86 | SkyQueryEnvironment env, |
| 87 | QueryExpression expression, |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 88 | Predicate<SkyKey> unfilteredUniverse, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 89 | QueryExpressionContext<Target> context, |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 90 | Callback<Target> callback) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 91 | return env.eval( |
| 92 | expression, |
| 93 | context, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 94 | ParallelVisitorUtils.createParallelVisitorCallback( |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 95 | new RdepsUnboundedVisitor.Factory(env, unfilteredUniverse, callback))); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static QueryTaskFuture<Predicate<SkyKey>> getDTCSkyKeyPredicateFuture( |
| 99 | SkyQueryEnvironment env, |
| 100 | QueryExpression expression, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 101 | QueryExpressionContext<Target> context, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 102 | int processResultsBatchSize, |
| 103 | int concurrencyLevel) { |
| 104 | QueryTaskFuture<ThreadSafeMutableSet<Target>> universeValueFuture = |
| 105 | QueryUtil.evalAll(env, context, expression); |
| 106 | |
| 107 | Function<ThreadSafeMutableSet<Target>, QueryTaskFuture<Predicate<SkyKey>>> |
| 108 | getTransitiveClosureAsyncFunction = |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 109 | universeValue -> { |
| 110 | ThreadSafeAggregateAllSkyKeysCallback aggregateAllCallback = |
| 111 | new ThreadSafeAggregateAllSkyKeysCallback(concurrencyLevel); |
| 112 | return env.execute( |
| 113 | () -> { |
shreyax | 6bebe38 | 2019-11-13 06:26:26 -0800 | [diff] [blame] | 114 | UnfilteredSkyKeyLabelDTCVisitor visitor = |
| 115 | new UnfilteredSkyKeyLabelDTCVisitor.Factory( |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 116 | env, |
| 117 | env.createSkyKeyUniquifier(), |
| 118 | processResultsBatchSize, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 119 | aggregateAllCallback) |
| 120 | .create(); |
| 121 | visitor.visitAndWaitForCompletion( |
shreyax | 55bc521 | 2020-01-27 11:46:39 -0800 | [diff] [blame] | 122 | SkyQueryEnvironment.makeLabelsStrict(universeValue)); |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 123 | return Predicates.in(aggregateAllCallback.getResult()); |
| 124 | }); |
| 125 | }; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 126 | |
| 127 | return env.transformAsync(universeValueFuture, getTransitiveClosureAsyncFunction); |
| 128 | } |
| 129 | |
| 130 | static QueryTaskFuture<Void> getRdepsInUniverseBoundedParallel( |
| 131 | SkyQueryEnvironment env, |
| 132 | QueryExpression expression, |
| 133 | int depth, |
| 134 | Predicate<SkyKey> universe, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 135 | QueryExpressionContext<Target> context, |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 136 | Callback<Target> callback) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 137 | return env.eval( |
| 138 | expression, |
| 139 | context, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 140 | ParallelVisitorUtils.createParallelVisitorCallback( |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 141 | new RdepsBoundedVisitor.Factory(env, depth, universe, callback))); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** Specialized parallel variant of {@link SkyQueryEnvironment#getRBuildFiles}. */ |
| 145 | static void getRBuildFilesParallel( |
| 146 | SkyQueryEnvironment env, |
| 147 | Collection<PathFragment> fileIdentifiers, |
nharmata | 20efb2a | 2018-09-28 12:54:12 -0700 | [diff] [blame] | 148 | QueryExpressionContext<Target> context, |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 149 | Callback<Target> callback) throws QueryException, InterruptedException { |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 150 | RBuildFilesVisitor visitor = |
Googler | b39486c | 2019-01-03 08:58:16 -0800 | [diff] [blame] | 151 | new RBuildFilesVisitor( |
| 152 | env, |
nharmata | a72e3139 | 2019-02-19 17:07:10 -0800 | [diff] [blame] | 153 | /*visitUniquifier=*/ env.createSkyKeyUniquifier(), |
| 154 | /*resultUniquifier=*/ env.createSkyKeyUniquifier(), |
Googler | b39486c | 2019-01-03 08:58:16 -0800 | [diff] [blame] | 155 | context, |
shreyax | d8dde88 | 2019-02-19 19:40:04 -0800 | [diff] [blame] | 156 | callback); |
shreyax | d73f7d7 | 2020-02-20 13:20:24 -0800 | [diff] [blame] | 157 | visitor.visitFileIdentifiersAndWaitForCompletion(env.graph, fileIdentifiers); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 158 | } |
| 159 | |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 160 | static QueryTaskFuture<Void> getDepsUnboundedParallel( |
| 161 | SkyQueryEnvironment env, |
| 162 | QueryExpression expression, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 163 | QueryExpressionContext<Target> context, |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 164 | Callback<Target> callback, |
shreyax | 55bc521 | 2020-01-27 11:46:39 -0800 | [diff] [blame] | 165 | boolean depsNeedFiltering, |
| 166 | QueryExpression caller) { |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 167 | return env.eval( |
| 168 | expression, |
| 169 | context, |
adgar | ec7084d | 2019-08-06 17:02:41 -0700 | [diff] [blame] | 170 | ParallelVisitorUtils.createParallelVisitorCallback( |
shreyax | 55bc521 | 2020-01-27 11:46:39 -0800 | [diff] [blame] | 171 | new DepsUnboundedVisitor.Factory(env, callback, depsNeedFiltering, context, caller))); |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
shreyax | 19b4377 | 2018-05-18 11:45:44 -0700 | [diff] [blame] | 174 | static class DepAndRdep { |
| 175 | @Nullable final SkyKey dep; |
| 176 | final SkyKey rdep; |
nharmata | 2399df0 | 2018-04-10 12:30:03 -0700 | [diff] [blame] | 177 | |
shreyax | 19b4377 | 2018-05-18 11:45:44 -0700 | [diff] [blame] | 178 | DepAndRdep(@Nullable SkyKey dep, SkyKey rdep) { |
nharmata | 2399df0 | 2018-04-10 12:30:03 -0700 | [diff] [blame] | 179 | this.dep = dep; |
| 180 | this.rdep = rdep; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public boolean equals(Object obj) { |
| 185 | if (!(obj instanceof DepAndRdep)) { |
| 186 | return false; |
| 187 | } |
| 188 | DepAndRdep other = (DepAndRdep) obj; |
| 189 | return Objects.equals(dep, other.dep) && rdep.equals(other.rdep); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int hashCode() { |
| 194 | // N.B. - We deliberately use a garbage-free hashCode implementation (rather than e.g. |
| 195 | // Objects#hash). Depending on the structure of the graph being traversed, this method can |
| 196 | // be very hot. |
| 197 | return 31 * Objects.hashCode(dep) + rdep.hashCode(); |
| 198 | } |
| 199 | } |
| 200 | |
shreyax | 19b4377 | 2018-05-18 11:45:44 -0700 | [diff] [blame] | 201 | static class DepAndRdepAtDepth { |
| 202 | final DepAndRdep depAndRdep; |
| 203 | final int rdepDepth; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 204 | |
shreyax | 19b4377 | 2018-05-18 11:45:44 -0700 | [diff] [blame] | 205 | DepAndRdepAtDepth(DepAndRdep depAndRdep, int rdepDepth) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 206 | this.depAndRdep = depAndRdep; |
| 207 | this.rdepDepth = rdepDepth; |
| 208 | } |
| 209 | } |
| 210 | |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 211 | /** Thread-safe {@link AggregateAllCallback} backed by a concurrent {@link Set}. */ |
| 212 | @ThreadSafe |
| 213 | private static class ThreadSafeAggregateAllSkyKeysCallback |
| 214 | implements AggregateAllCallback<SkyKey, ImmutableSet<SkyKey>> { |
| 215 | |
| 216 | private final Set<SkyKey> results; |
| 217 | |
| 218 | private ThreadSafeAggregateAllSkyKeysCallback(int concurrencyLevel) { |
| 219 | this.results = |
Googler | 45b5953 | 2018-05-03 11:10:20 -0700 | [diff] [blame] | 220 | Collections.newSetFromMap( |
| 221 | new ConcurrentHashMap<>( |
| 222 | /*initialCapacity=*/ concurrencyLevel, /*loadFactor=*/ 0.75f)); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void process(Iterable<SkyKey> partialResult) |
| 227 | throws QueryException, InterruptedException { |
| 228 | Iterables.addAll(results, partialResult); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public ImmutableSet<SkyKey> getResult() { |
| 233 | return ImmutableSet.copyOf(results); |
| 234 | } |
| 235 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 236 | } |