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.lib.buildtool; |
| 15 | |
| 16 | import com.google.common.base.Joiner; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.base.Predicate; |
| 18 | import com.google.common.base.Stopwatch; |
| 19 | import com.google.common.collect.HashBasedTable; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Iterables; |
| 24 | import com.google.common.collect.Ordering; |
| 25 | import com.google.common.collect.Table; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.Constants; |
| 27 | import com.google.devtools.build.lib.actions.Action; |
| 28 | import com.google.devtools.build.lib.actions.ActionCacheChecker; |
| 29 | import com.google.devtools.build.lib.actions.ActionContextConsumer; |
| 30 | import com.google.devtools.build.lib.actions.ActionContextMarker; |
| 31 | import com.google.devtools.build.lib.actions.ActionContextProvider; |
| 32 | import com.google.devtools.build.lib.actions.ActionGraph; |
| 33 | import com.google.devtools.build.lib.actions.ActionInputFileCache; |
| 34 | import com.google.devtools.build.lib.actions.Artifact; |
| 35 | import com.google.devtools.build.lib.actions.BlazeExecutor; |
| 36 | import com.google.devtools.build.lib.actions.BuildFailedException; |
| 37 | import com.google.devtools.build.lib.actions.ExecException; |
| 38 | import com.google.devtools.build.lib.actions.ExecutionStrategy; |
| 39 | import com.google.devtools.build.lib.actions.Executor; |
| 40 | import com.google.devtools.build.lib.actions.Executor.ActionContext; |
| 41 | import com.google.devtools.build.lib.actions.ExecutorInitException; |
| 42 | import com.google.devtools.build.lib.actions.LocalHostCapacity; |
| 43 | import com.google.devtools.build.lib.actions.ResourceManager; |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.actions.ResourceSet; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.actions.SimpleActionContextProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | import com.google.devtools.build.lib.actions.SpawnActionContext; |
| 47 | import com.google.devtools.build.lib.actions.TestExecException; |
| 48 | import com.google.devtools.build.lib.actions.cache.ActionCache; |
| 49 | import com.google.devtools.build.lib.analysis.BuildView; |
| 50 | import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.analysis.SymlinkTreeActionContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | import com.google.devtools.build.lib.analysis.WorkspaceStatusAction; |
| 55 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 56 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
| 57 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionPhaseCompleteEvent; |
| 58 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | import com.google.devtools.build.lib.events.Event; |
| 60 | import com.google.devtools.build.lib.events.EventHandler; |
| 61 | import com.google.devtools.build.lib.events.EventKind; |
| 62 | import com.google.devtools.build.lib.events.Reporter; |
| 63 | import com.google.devtools.build.lib.exec.CheckUpToDateFilter; |
| 64 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
| 65 | import com.google.devtools.build.lib.exec.OutputService; |
| 66 | import com.google.devtools.build.lib.exec.SingleBuildFileCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | import com.google.devtools.build.lib.exec.SymlinkTreeStrategy; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | import com.google.devtools.build.lib.profiler.ProfilePhase; |
| 70 | import com.google.devtools.build.lib.profiler.Profiler; |
| 71 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
| 72 | import com.google.devtools.build.lib.rules.fileset.FilesetActionContext; |
| 73 | import com.google.devtools.build.lib.rules.fileset.FilesetActionContextImpl; |
| 74 | import com.google.devtools.build.lib.rules.test.TestActionContext; |
| 75 | import com.google.devtools.build.lib.runtime.BlazeModule; |
| 76 | import com.google.devtools.build.lib.runtime.BlazeRuntime; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 78 | import com.google.devtools.build.lib.skyframe.AspectValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | import com.google.devtools.build.lib.skyframe.Builder; |
| 80 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | import com.google.devtools.build.lib.util.AbruptExitException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | import com.google.devtools.build.lib.util.ExitCode; |
| 83 | import com.google.devtools.build.lib.util.LoggingUtil; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 84 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 86 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 87 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | import com.google.devtools.build.lib.vfs.Path; |
| 89 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 90 | |
| 91 | import java.io.IOException; |
| 92 | import java.io.OutputStream; |
| 93 | import java.io.PrintWriter; |
| 94 | import java.util.ArrayList; |
| 95 | import java.util.Collection; |
| 96 | import java.util.HashMap; |
| 97 | import java.util.HashSet; |
| 98 | import java.util.LinkedHashSet; |
| 99 | import java.util.List; |
| 100 | import java.util.Map; |
| 101 | import java.util.Set; |
Philipp Wollermann | a068dee | 2015-06-08 11:15:02 +0000 | [diff] [blame] | 102 | import java.util.TreeMap; |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 103 | import java.util.UUID; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | import java.util.concurrent.TimeUnit; |
| 105 | import java.util.logging.Level; |
| 106 | import java.util.logging.Logger; |
| 107 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | /** |
| 109 | * This class manages the execution phase. The entry point is {@link #executeBuild}. |
| 110 | * |
| 111 | * <p>This is only intended for use by {@link BuildTool}. |
| 112 | * |
| 113 | * <p>This class contains an ActionCache, and refers to the BlazeRuntime's BuildView and |
| 114 | * PackageCache. |
| 115 | * |
| 116 | * @see BuildTool |
| 117 | * @see BuildView |
| 118 | */ |
| 119 | public class ExecutionTool { |
| 120 | private static class StrategyConverter { |
| 121 | private Table<Class<? extends ActionContext>, String, ActionContext> classMap = |
| 122 | HashBasedTable.create(); |
| 123 | private Map<Class<? extends ActionContext>, ActionContext> defaultClassMap = |
| 124 | new HashMap<>(); |
| 125 | |
| 126 | /** |
| 127 | * Aggregates all {@link ActionContext}s that are in {@code contextProviders}. |
| 128 | */ |
| 129 | @SuppressWarnings("unchecked") |
| 130 | private StrategyConverter(Iterable<ActionContextProvider> contextProviders) { |
| 131 | for (ActionContextProvider provider : contextProviders) { |
| 132 | for (ActionContext strategy : provider.getActionContexts()) { |
| 133 | ExecutionStrategy annotation = |
| 134 | strategy.getClass().getAnnotation(ExecutionStrategy.class); |
| 135 | if (annotation != null) { |
| 136 | defaultClassMap.put(annotation.contextType(), strategy); |
| 137 | |
| 138 | for (String name : annotation.name()) { |
| 139 | classMap.put(annotation.contextType(), name, strategy); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @SuppressWarnings("unchecked") |
| 147 | private <T extends ActionContext> T getStrategy(Class<T> clazz, String name) { |
| 148 | return (T) (name.isEmpty() ? defaultClassMap.get(clazz) : classMap.get(clazz, name)); |
| 149 | } |
| 150 | |
| 151 | private String getValidValues(Class<? extends ActionContext> context) { |
| 152 | return Joiner.on(", ").join(Ordering.natural().sortedCopy(classMap.row(context).keySet())); |
| 153 | } |
| 154 | |
| 155 | private String getUserFriendlyName(Class<? extends ActionContext> context) { |
| 156 | ActionContextMarker marker = context.getAnnotation(ActionContextMarker.class); |
| 157 | return marker != null |
| 158 | ? marker.name() |
| 159 | : context.getSimpleName(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static final Logger LOG = Logger.getLogger(ExecutionTool.class.getName()); |
| 164 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 165 | private final CommandEnvironment env; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 166 | private final BlazeRuntime runtime; |
| 167 | private final BuildRequest request; |
| 168 | private BlazeExecutor executor; |
| 169 | private ActionInputFileCache fileCache; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 170 | private final ImmutableList<ActionContextProvider> actionContextProviders; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 171 | |
Philipp Wollermann | a068dee | 2015-06-08 11:15:02 +0000 | [diff] [blame] | 172 | private Map<String, SpawnActionContext> spawnStrategyMap = |
| 173 | new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 174 | private List<ActionContext> strategies = new ArrayList<>(); |
| 175 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 176 | ExecutionTool(CommandEnvironment env, BuildRequest request) throws ExecutorInitException { |
| 177 | this.env = env; |
| 178 | this.runtime = env.getRuntime(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 179 | this.request = request; |
| 180 | |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 181 | // Create tools before getting the strategies from the modules as some of them need tools to |
| 182 | // determine whether the host actually supports certain strategies (e.g. sandboxing). |
| 183 | createToolsSymlinks(); |
| 184 | |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 185 | this.actionContextProviders = |
| 186 | getActionContextProvidersFromModules( |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 187 | runtime, |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 188 | new FilesetActionContextImpl.Provider( |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 189 | env.getReporter(), runtime.getWorkspaceName()), |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 190 | new SimpleActionContextProvider( |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 191 | new SymlinkTreeStrategy(env.getOutputService(), runtime.getBinTools()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 192 | StrategyConverter strategyConverter = new StrategyConverter(actionContextProviders); |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 193 | |
| 194 | ImmutableList<ActionContextConsumer> actionContextConsumers = |
| 195 | getActionContextConsumersFromModules( |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 196 | runtime, |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 197 | // TODO(philwo) - the ExecutionTool should not add arbitrary dependencies on its own, |
| 198 | // instead these dependencies should be added to the ActionContextConsumer of the module |
| 199 | // that actually depends on them. |
| 200 | new ActionContextConsumer() { |
| 201 | @Override |
| 202 | public Map<String, String> getSpawnActionContexts() { |
| 203 | return ImmutableMap.of(); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Map<Class<? extends ActionContext>, String> getActionContexts() { |
| 208 | return ImmutableMap.<Class<? extends ActionContext>, String>builder() |
| 209 | .put(FilesetActionContext.class, "") |
| 210 | .put(WorkspaceStatusAction.Context.class, "") |
| 211 | .put(SymlinkTreeActionContext.class, "") |
| 212 | .build(); |
| 213 | } |
| 214 | }); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | |
| 216 | for (ActionContextConsumer consumer : actionContextConsumers) { |
| 217 | // There are many different SpawnActions, and we want to control the action context they use |
| 218 | // independently from each other, for example, to run genrules locally and Java compile action |
| 219 | // in prod. Thus, for SpawnActions, we decide the action context to use not only based on the |
| 220 | // context class, but also the mnemonic of the action. |
| 221 | for (Map.Entry<String, String> entry : consumer.getSpawnActionContexts().entrySet()) { |
| 222 | SpawnActionContext context = |
| 223 | strategyConverter.getStrategy(SpawnActionContext.class, entry.getValue()); |
| 224 | if (context == null) { |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 225 | throw makeExceptionForInvalidStrategyValue( |
| 226 | entry.getValue(), |
| 227 | "spawn", |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | strategyConverter.getValidValues(SpawnActionContext.class)); |
| 229 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 230 | spawnStrategyMap.put(entry.getKey(), context); |
| 231 | } |
| 232 | |
| 233 | for (Map.Entry<Class<? extends ActionContext>, String> entry : |
| 234 | consumer.getActionContexts().entrySet()) { |
| 235 | ActionContext context = strategyConverter.getStrategy(entry.getKey(), entry.getValue()); |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 236 | if (context == null) { |
| 237 | throw makeExceptionForInvalidStrategyValue( |
| 238 | entry.getValue(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | strategyConverter.getUserFriendlyName(entry.getKey()), |
| 240 | strategyConverter.getValidValues(entry.getKey())); |
| 241 | } |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 242 | strategies.add(context); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | // If tests are to be run during build, too, we have to explicitly load the test action context. |
| 247 | if (request.shouldRunTests()) { |
| 248 | String testStrategyValue = request.getOptions(ExecutionOptions.class).testStrategy; |
| 249 | ActionContext context = strategyConverter.getStrategy(TestActionContext.class, |
| 250 | testStrategyValue); |
| 251 | if (context == null) { |
| 252 | throw makeExceptionForInvalidStrategyValue(testStrategyValue, "test", |
| 253 | strategyConverter.getValidValues(TestActionContext.class)); |
| 254 | } |
| 255 | strategies.add(context); |
| 256 | } |
| 257 | } |
| 258 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 259 | private static ImmutableList<ActionContextConsumer> getActionContextConsumersFromModules( |
| 260 | BlazeRuntime runtime, ActionContextConsumer... extraConsumers) { |
| 261 | ImmutableList.Builder<ActionContextConsumer> builder = ImmutableList.builder(); |
| 262 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 263 | builder.addAll(module.getActionContextConsumers()); |
| 264 | } |
| 265 | builder.add(extraConsumers); |
| 266 | return builder.build(); |
| 267 | } |
| 268 | |
| 269 | private static ImmutableList<ActionContextProvider> getActionContextProvidersFromModules( |
| 270 | BlazeRuntime runtime, ActionContextProvider... extraProviders) { |
| 271 | ImmutableList.Builder<ActionContextProvider> builder = ImmutableList.builder(); |
| 272 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 273 | builder.addAll(module.getActionContextProviders()); |
| 274 | } |
| 275 | builder.add(extraProviders); |
| 276 | return builder.build(); |
| 277 | } |
| 278 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 279 | private static ExecutorInitException makeExceptionForInvalidStrategyValue(String value, |
| 280 | String strategy, String validValues) { |
| 281 | return new ExecutorInitException(String.format( |
| 282 | "'%s' is an invalid value for %s strategy. Valid values are: %s", value, strategy, |
| 283 | validValues), ExitCode.COMMAND_LINE_ERROR); |
| 284 | } |
| 285 | |
| 286 | Executor getExecutor() throws ExecutorInitException { |
| 287 | if (executor == null) { |
| 288 | executor = createExecutor(); |
| 289 | } |
| 290 | return executor; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Creates an executor for the current set of blaze runtime, execution options, and request. |
| 295 | */ |
| 296 | private BlazeExecutor createExecutor() |
| 297 | throws ExecutorInitException { |
| 298 | return new BlazeExecutor( |
| 299 | runtime.getDirectories().getExecRoot(), |
| 300 | runtime.getDirectories().getOutputPath(), |
| 301 | getReporter(), |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 302 | env.getEventBus(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 303 | runtime.getClock(), |
| 304 | request, |
| 305 | request.getOptions(ExecutionOptions.class).verboseFailures, |
| 306 | request.getOptions(ExecutionOptions.class).showSubcommands, |
| 307 | strategies, |
| 308 | spawnStrategyMap, |
| 309 | actionContextProviders); |
| 310 | } |
| 311 | |
| 312 | void init() throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | getExecutor(); |
| 314 | } |
| 315 | |
| 316 | void shutdown() { |
| 317 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
| 318 | actionContextProvider.executionPhaseEnding(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Performs the execution phase (phase 3) of the build, in which the Builder |
| 324 | * is applied to the action graph to bring the targets up to date. (This |
| 325 | * function will return prior to execution-proper if --nobuild was specified.) |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 326 | * @param buildId UUID of the build id |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 327 | * @param analysisResult the analysis phase output |
| 328 | * @param buildResult the mutable build result |
Lukacs Berki | 3ea4d44 | 2016-01-21 15:15:30 +0000 | [diff] [blame] | 329 | * @param packageRoots package roots collected from loading phase and BuildConfigurationCollection |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 330 | * creation |
| 331 | */ |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 332 | void executeBuild(UUID buildId, AnalysisResult analysisResult, |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 333 | BuildResult buildResult, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 334 | BuildConfigurationCollection configurations, |
| 335 | ImmutableMap<PathFragment, Path> packageRoots) |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 336 | throws BuildFailedException, InterruptedException, TestExecException, AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 337 | Stopwatch timer = Stopwatch.createStarted(); |
| 338 | prepare(packageRoots, configurations); |
| 339 | |
| 340 | ActionGraph actionGraph = analysisResult.getActionGraph(); |
| 341 | |
| 342 | // Get top-level artifacts. |
| 343 | ImmutableSet<Artifact> additionalArtifacts = analysisResult.getAdditionalArtifactsToBuild(); |
| 344 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 345 | OutputService outputService = env.getOutputService(); |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 346 | ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 347 | if (outputService != null) { |
Eric Fellheimer | 1246eb3 | 2015-11-18 15:41:38 +0000 | [diff] [blame] | 348 | modifiedOutputFiles = outputService.startBuild(buildId, |
| 349 | request.getBuildOptions().finalizeActions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 350 | } else { |
| 351 | startLocalOutputBuild(); // TODO(bazel-team): this could be just another OutputService |
| 352 | } |
| 353 | |
Lukacs Berki | 85c63c4 | 2016-01-22 09:25:20 +0000 | [diff] [blame^] | 354 | if (getWorkspace().getFileSystem().supportsSymbolicLinks()) { |
| 355 | List<BuildConfiguration> targetConfigurations = configurations.getTargetConfigurations(); |
| 356 | BuildConfiguration targetConfiguration = targetConfigurations.size() == 1 |
| 357 | ? targetConfigurations.get(0) : null; |
| 358 | if (targetConfigurations.size() == 1) { |
| 359 | OutputDirectoryLinksUtils.createOutputDirectoryLinks( |
| 360 | runtime.getWorkspaceName(), getWorkspace(), getExecRoot(), |
| 361 | runtime.getOutputPath(), getReporter(), targetConfiguration, |
| 362 | request.getBuildOptions().getSymlinkPrefix()); |
| 363 | } |
| 364 | } |
| 365 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 366 | ActionCache actionCache = getActionCache(); |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 367 | SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor(); |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 368 | Builder builder = |
| 369 | createBuilder(request, executor, actionCache, skyframeExecutor, modifiedOutputFiles); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 370 | |
| 371 | // |
| 372 | // Execution proper. All statements below are logically nested in |
| 373 | // begin/end pairs. No early returns or exceptions please! |
| 374 | // |
| 375 | |
| 376 | Collection<ConfiguredTarget> configuredTargets = buildResult.getActualTargets(); |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 377 | env.getEventBus().post(new ExecutionStartingEvent(configuredTargets)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 378 | |
| 379 | getReporter().handle(Event.progress("Building...")); |
| 380 | |
| 381 | // Conditionally record dependency-checker log: |
| 382 | ExplanationHandler explanationHandler = |
| 383 | installExplanationHandler(request.getBuildOptions().explanationPath, |
| 384 | request.getOptionsDescription()); |
| 385 | |
| 386 | Set<ConfiguredTarget> builtTargets = new HashSet<>(); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 387 | Collection<AspectValue> aspects = analysisResult.getAspects(); |
| 388 | |
| 389 | Iterable<Artifact> allArtifactsForProviders = |
| 390 | Iterables.concat( |
| 391 | additionalArtifacts, |
| 392 | TopLevelArtifactHelper.getAllArtifactsToBuild( |
| 393 | analysisResult.getTargetsToBuild(), analysisResult.getTopLevelContext()) |
| 394 | .getAllArtifacts(), |
| 395 | TopLevelArtifactHelper.getAllArtifactsToBuildFromAspects( |
| 396 | aspects, analysisResult.getTopLevelContext()) |
| 397 | .getAllArtifacts(), |
| 398 | //TODO(dslomov): Artifacts to test from aspects? |
| 399 | TopLevelArtifactHelper.getAllArtifactsToTest(analysisResult.getTargetsToTest())); |
| 400 | |
| 401 | if (request.isRunningInEmacs()) { |
| 402 | // The syntax of this message is tightly constrained by lisp/progmodes/compile.el in emacs |
| 403 | request.getOutErr().printErrLn("blaze: Entering directory `" + getExecRoot() + "/'"); |
| 404 | } |
| 405 | boolean buildCompleted = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 406 | try { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 407 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
| 408 | actionContextProvider.executionPhaseStarting( |
| 409 | fileCache, |
| 410 | actionGraph, |
| 411 | allArtifactsForProviders); |
| 412 | } |
| 413 | executor.executionPhaseStarting(); |
| 414 | skyframeExecutor.drainChangedFiles(); |
| 415 | |
| 416 | if (request.getViewOptions().discardAnalysisCache) { |
| 417 | // Free memory by removing cache entries that aren't going to be needed. Note that in |
| 418 | // skyframe full, this destroys the action graph as well, so we can only do it after the |
| 419 | // action graph is no longer needed. |
Ulf Adams | ea44dcb | 2015-09-18 12:36:30 +0000 | [diff] [blame] | 420 | env.getView().clearAnalysisCache(analysisResult.getTargetsToBuild()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 421 | actionGraph = null; |
| 422 | } |
| 423 | |
| 424 | configureResourceManager(request); |
| 425 | |
| 426 | Profiler.instance().markPhase(ProfilePhase.EXECUTE); |
| 427 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 428 | builder.buildArtifacts( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 429 | env.getReporter(), |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 430 | additionalArtifacts, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 431 | analysisResult.getParallelTests(), |
| 432 | analysisResult.getExclusiveTests(), |
| 433 | analysisResult.getTargetsToBuild(), |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 434 | analysisResult.getAspects(), |
| 435 | executor, |
| 436 | builtTargets, |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 437 | request.getBuildOptions().explanationPath != null, |
| 438 | runtime.getLastExecutionTimeRange()); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 439 | buildCompleted = true; |
| 440 | } catch (BuildFailedException | TestExecException e) { |
| 441 | buildCompleted = true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 442 | throw e; |
| 443 | } finally { |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 444 | env.recordLastExecutionTime(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 445 | if (request.isRunningInEmacs()) { |
| 446 | request.getOutErr().printErrLn("blaze: Leaving directory `" + getExecRoot() + "/'"); |
| 447 | } |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 448 | if (buildCompleted) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 449 | getReporter().handle(Event.progress("Building complete.")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 452 | env.getEventBus().post(new ExecutionFinishedEvent(ImmutableMap.<String, Long> of(), 0L, |
Miguel Alcon Pinto | 9186f68 | 2015-04-09 21:49:34 +0000 | [diff] [blame] | 453 | skyframeExecutor.getOutputDirtyFilesAndClear(), |
| 454 | skyframeExecutor.getModifiedFilesDuringPreviousBuildAndClear())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 455 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 456 | executor.executionPhaseEnding(); |
| 457 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
| 458 | actionContextProvider.executionPhaseEnding(); |
| 459 | } |
| 460 | |
| 461 | Profiler.instance().markPhase(ProfilePhase.FINISH); |
| 462 | |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 463 | if (buildCompleted) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 464 | saveCaches(actionCache); |
| 465 | } |
| 466 | |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 467 | try (AutoProfiler p = AutoProfiler.profiled("Show results", ProfilerTask.INFO)) { |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 468 | buildResult.setSuccessfulTargets( |
| 469 | determineSuccessfulTargets(configuredTargets, builtTargets, timer)); |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 470 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
| 471 | buildResultPrinter.showBuildResult( |
| 472 | request, buildResult, configuredTargets, analysisResult.getAspects()); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 473 | } |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 474 | |
| 475 | try (AutoProfiler p = AutoProfiler.profiled("Show artifacts", ProfilerTask.INFO)) { |
| 476 | if (request.getBuildOptions().showArtifacts) { |
| 477 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
| 478 | buildResultPrinter.showArtifacts( |
| 479 | request, configuredTargets, analysisResult.getAspects()); |
| 480 | } |
| 481 | } |
| 482 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 483 | if (explanationHandler != null) { |
| 484 | uninstallExplanationHandler(explanationHandler); |
| 485 | } |
| 486 | // Finalize output service last, so that if we do throw an exception, we know all the other |
| 487 | // code has already run. |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 488 | if (env.getOutputService() != null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 489 | boolean isBuildSuccessful = |
| 490 | buildResult.getSuccessfulTargets().size() == configuredTargets.size(); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 491 | env.getOutputService().finalizeBuild(isBuildSuccessful); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | private void prepare(ImmutableMap<PathFragment, Path> packageRoots, |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 497 | BuildConfigurationCollection configurations) throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 498 | // Prepare for build. |
| 499 | Profiler.instance().markPhase(ProfilePhase.PREPARE); |
| 500 | |
| 501 | // Create some tools symlinks / cleanup per-build state |
| 502 | createActionLogDirectory(); |
| 503 | |
| 504 | // Plant the symlink forest. |
| 505 | plantSymlinkForest(packageRoots, configurations); |
| 506 | } |
| 507 | |
| 508 | private void createToolsSymlinks() throws ExecutorInitException { |
| 509 | try { |
| 510 | runtime.getBinTools().setupBuildTools(); |
| 511 | } catch (ExecException e) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 512 | throw new ExecutorInitException("Tools symlink creation failed", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | private void plantSymlinkForest(ImmutableMap<PathFragment, Path> packageRoots, |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 517 | BuildConfigurationCollection configurations) throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 518 | try { |
| 519 | FileSystemUtils.deleteTreesBelowNotPrefixed(getExecRoot(), |
| 520 | new String[] { ".", "_", Constants.PRODUCT_NAME + "-"}); |
| 521 | // Delete the build configuration's temporary directories |
| 522 | for (BuildConfiguration configuration : configurations.getTargetConfigurations()) { |
| 523 | configuration.prepareForExecutionPhase(); |
| 524 | } |
| 525 | FileSystemUtils.plantLinkForest(packageRoots, getExecRoot()); |
| 526 | } catch (IOException e) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 527 | throw new ExecutorInitException("Source forest creation failed", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 531 | private void createActionLogDirectory() throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 532 | Path directory = runtime.getDirectories().getActionConsoleOutputDirectory(); |
| 533 | try { |
| 534 | if (directory.exists()) { |
| 535 | FileSystemUtils.deleteTree(directory); |
| 536 | } |
| 537 | directory.createDirectory(); |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 538 | } catch (IOException e) { |
| 539 | throw new ExecutorInitException("Couldn't delete action output directory", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Prepare for a local output build. |
| 545 | */ |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 546 | private void startLocalOutputBuild() throws ExecutorInitException { |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 547 | try (AutoProfiler p = AutoProfiler.profiled("Starting local output build", ProfilerTask.INFO)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 548 | Path outputPath = runtime.getOutputPath(); |
| 549 | Path localOutputPath = runtime.getDirectories().getLocalOutputPath(); |
| 550 | |
| 551 | if (outputPath.isSymbolicLink()) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 552 | try { |
| 553 | // Remove the existing symlink first. |
| 554 | outputPath.delete(); |
| 555 | if (localOutputPath.exists()) { |
| 556 | // Pre-existing local output directory. Move to outputPath. |
| 557 | localOutputPath.renameTo(outputPath); |
| 558 | } |
| 559 | } catch (IOException e) { |
| 560 | throw new ExecutorInitException("Couldn't handle local output directory symlinks", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 561 | } |
| 562 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * If a path is supplied, creates and installs an ExplanationHandler. Returns |
| 568 | * an instance on success. Reports an error and returns null otherwise. |
| 569 | */ |
| 570 | private ExplanationHandler installExplanationHandler(PathFragment explanationPath, |
| 571 | String allOptions) { |
| 572 | if (explanationPath == null) { |
| 573 | return null; |
| 574 | } |
| 575 | ExplanationHandler handler; |
| 576 | try { |
| 577 | handler = new ExplanationHandler( |
| 578 | getWorkspace().getRelative(explanationPath).getOutputStream(), |
| 579 | allOptions); |
| 580 | } catch (IOException e) { |
| 581 | getReporter().handle(Event.warn(String.format( |
| 582 | "Cannot write explanation of rebuilds to file '%s': %s", |
| 583 | explanationPath, e.getMessage()))); |
| 584 | return null; |
| 585 | } |
| 586 | getReporter().handle( |
| 587 | Event.info("Writing explanation of rebuilds to '" + explanationPath + "'")); |
| 588 | getReporter().addHandler(handler); |
| 589 | return handler; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Uninstalls the specified ExplanationHandler (if any) and closes the log |
| 594 | * file. |
| 595 | */ |
| 596 | private void uninstallExplanationHandler(ExplanationHandler handler) { |
| 597 | if (handler != null) { |
| 598 | getReporter().removeHandler(handler); |
| 599 | handler.log.close(); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * An ErrorEventListener implementation that records DEPCHECKER events into a log |
| 605 | * file, iff the --explain flag is specified during a build. |
| 606 | */ |
| 607 | private static class ExplanationHandler implements EventHandler { |
| 608 | |
| 609 | private final PrintWriter log; |
| 610 | |
| 611 | private ExplanationHandler(OutputStream log, String optionsDescription) { |
| 612 | this.log = new PrintWriter(log); |
| 613 | this.log.println("Build options: " + optionsDescription); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | @Override |
| 618 | public void handle(Event event) { |
| 619 | if (event.getKind() == EventKind.DEPCHECKER) { |
| 620 | log.println(event.getMessage()); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Computes the result of the build. Sets the list of successful (up-to-date) |
| 627 | * targets in the request object. |
| 628 | * |
| 629 | * @param configuredTargets The configured targets whose artifacts are to be |
| 630 | * built. |
| 631 | * @param timer A timer that was started when the execution phase started. |
| 632 | */ |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 633 | private Collection<ConfiguredTarget> determineSuccessfulTargets( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 634 | Collection<ConfiguredTarget> configuredTargets, Set<ConfiguredTarget> builtTargets, |
| 635 | Stopwatch timer) { |
| 636 | // Maintain the ordering by copying builtTargets into a LinkedHashSet in the same iteration |
| 637 | // order as configuredTargets. |
| 638 | Collection<ConfiguredTarget> successfulTargets = new LinkedHashSet<>(); |
| 639 | for (ConfiguredTarget target : configuredTargets) { |
| 640 | if (builtTargets.contains(target)) { |
| 641 | successfulTargets.add(target); |
| 642 | } |
| 643 | } |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 644 | env.getEventBus().post( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 645 | new ExecutionPhaseCompleteEvent(timer.stop().elapsed(TimeUnit.MILLISECONDS))); |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 646 | return successfulTargets; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Dmitry Lomov | 56c7d55 | 2015-09-07 14:57:47 +0000 | [diff] [blame] | 649 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 650 | private ActionCache getActionCache() throws LocalEnvironmentException { |
| 651 | try { |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 652 | return env.getPersistentActionCache(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 653 | } catch (IOException e) { |
| 654 | // TODO(bazel-team): (2010) Ideally we should just remove all cache data and reinitialize |
| 655 | // caches. |
| 656 | LoggingUtil.logToRemote(Level.WARNING, "Failed to initialize action cache: " |
| 657 | + e.getMessage(), e); |
| 658 | throw new LocalEnvironmentException("couldn't create action cache: " + e.getMessage() |
| 659 | + ". If error persists, use 'blaze clean'"); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | private Builder createBuilder(BuildRequest request, |
| 664 | Executor executor, |
| 665 | ActionCache actionCache, |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 666 | SkyframeExecutor skyframeExecutor, |
| 667 | ModifiedFileSet modifiedOutputFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 668 | BuildRequest.BuildRequestOptions options = request.getBuildOptions(); |
| 669 | boolean verboseExplanations = options.verboseExplanations; |
| 670 | boolean keepGoing = request.getViewOptions().keepGoing; |
| 671 | |
| 672 | Path actionOutputRoot = runtime.getDirectories().getActionConsoleOutputDirectory(); |
| 673 | Predicate<Action> executionFilter = CheckUpToDateFilter.fromOptions( |
| 674 | request.getOptions(ExecutionOptions.class)); |
| 675 | |
| 676 | // jobs should have been verified in BuildRequest#validateOptions(). |
| 677 | Preconditions.checkState(options.jobs >= -1); |
| 678 | int actualJobs = options.jobs == 0 ? 1 : options.jobs; // Treat 0 jobs as a single task. |
| 679 | |
| 680 | // Unfortunately, the exec root cache is not shared with caches in the remote execution |
| 681 | // client. |
| 682 | fileCache = createBuildSingleFileCache(executor.getExecRoot()); |
| 683 | skyframeExecutor.setActionOutputRoot(actionOutputRoot); |
| 684 | return new SkyframeBuilder(skyframeExecutor, |
Ulf Adams | ea44dcb | 2015-09-18 12:36:30 +0000 | [diff] [blame] | 685 | new ActionCacheChecker(actionCache, env.getView().getArtifactFactory(), executionFilter, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 686 | verboseExplanations), |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 687 | keepGoing, actualJobs, |
| 688 | options.checkOutputFiles ? modifiedOutputFiles : ModifiedFileSet.NOTHING_MODIFIED, |
| 689 | options.finalizeActions, fileCache, request.getBuildOptions().progressReportInterval); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | private void configureResourceManager(BuildRequest request) { |
| 693 | ResourceManager resourceMgr = ResourceManager.instance(); |
| 694 | ExecutionOptions options = request.getOptions(ExecutionOptions.class); |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 695 | ResourceSet resources; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 696 | if (options.availableResources != null) { |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 697 | resources = options.availableResources; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 698 | resourceMgr.setRamUtilizationPercentage(100); |
| 699 | } else { |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 700 | resources = LocalHostCapacity.getLocalHostCapacity(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 701 | resourceMgr.setRamUtilizationPercentage(options.ramUtilizationPercentage); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 702 | } |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 703 | |
| 704 | resourceMgr.setAvailableResources(ResourceSet.create( |
| 705 | resources.getMemoryMb(), |
| 706 | resources.getCpuUsage(), |
| 707 | resources.getIoUsage(), |
| 708 | request.getExecutionOptions().usingLocalTestJobs() |
| 709 | ? request.getExecutionOptions().localTestJobs : Integer.MAX_VALUE |
| 710 | )); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Writes the cache files to disk, reporting any errors that occurred during |
| 715 | * writing. |
| 716 | */ |
| 717 | private void saveCaches(ActionCache actionCache) { |
| 718 | long actionCacheSizeInBytes = 0; |
| 719 | long actionCacheSaveTime; |
| 720 | |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 721 | AutoProfiler p = AutoProfiler.profiledAndLogged("Saving action cache", ProfilerTask.INFO, LOG); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 722 | try { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 723 | actionCacheSizeInBytes = actionCache.save(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 724 | } catch (IOException e) { |
| 725 | getReporter().handle(Event.error("I/O error while writing action log: " + e.getMessage())); |
| 726 | } finally { |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 727 | actionCacheSaveTime = p.completeAndGetElapsedTimeNanos(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 728 | } |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 729 | env.getEventBus().post(new CachesSavedEvent( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 730 | actionCacheSaveTime, actionCacheSizeInBytes)); |
| 731 | } |
| 732 | |
| 733 | private ActionInputFileCache createBuildSingleFileCache(Path execRoot) { |
| 734 | String cwd = execRoot.getPathString(); |
| 735 | FileSystem fs = runtime.getDirectories().getFileSystem(); |
| 736 | |
| 737 | ActionInputFileCache cache = null; |
| 738 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 739 | ActionInputFileCache pluggable = module.createActionInputCache(cwd, fs); |
| 740 | if (pluggable != null) { |
| 741 | Preconditions.checkState(cache == null); |
| 742 | cache = pluggable; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (cache == null) { |
| 747 | cache = new SingleBuildFileCache(cwd, fs); |
| 748 | } |
| 749 | return cache; |
| 750 | } |
| 751 | |
| 752 | private Reporter getReporter() { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 753 | return env.getReporter(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 756 | private Path getWorkspace() { |
| 757 | return runtime.getWorkspace(); |
| 758 | } |
| 759 | |
| 760 | private Path getExecRoot() { |
| 761 | return runtime.getExecRoot(); |
| 762 | } |
| 763 | } |