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