From 5735ff9978ff1d2cbde994b9d7c9516a9a331e5b Mon Sep 17 00:00:00 2001 From: Ulf Adams Date: Fri, 25 Sep 2015 22:04:43 +0000 Subject: Refactor the LoadingPhaseRunner: create a new TransitivePackageLoader per run. The only implementation of TransitivePackageLoader at this point is in SkyframeLabelVisitor, which by itself does not keep any state; it relies on Skyframe to do all the caching. Creating a new one per run should ensure that we don't accidentally keep state there, and also reduces memory consumption (a little), because it can now be garbage collected after the loading phase. As a side effect, we no longer need to create an instance if we're using the simplified loading phase. -- MOS_MIGRATED_REVID=103979794 --- .../build/lib/pkgcache/LoadingPhaseRunner.java | 79 ++++++++++------------ 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunner.java b/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunner.java index b05a52336c..26b119386c 100644 --- a/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunner.java +++ b/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunner.java @@ -248,14 +248,12 @@ public class LoadingPhaseRunner { private final PackageManager packageManager; private final TargetPatternEvaluator targetPatternEvaluator; private final Set ruleNames; - private final TransitivePackageLoader pkgLoader; public LoadingPhaseRunner(PackageManager packageManager, Set ruleNames) { this.packageManager = packageManager; this.targetPatternEvaluator = packageManager.getTargetPatternEvaluator(); this.ruleNames = ruleNames; - this.pkgLoader = packageManager.newTransitiveLoader(); } public TargetPatternEvaluator getTargetPatternEvaluator() { @@ -371,19 +369,11 @@ public class LoadingPhaseRunner { callback.notifyTargets(targets.getTargets()); } maybeReportDeprecation(eventHandler, targets.getTargets()); - BaseLoadingResult result; if (enableLoading) { - result = doLoadingPhase(eventHandler, eventBus, targets.getTargets(), - labelsToLoadUnconditionally, keepGoing, options.loadingPhaseThreads); - LoadingResult loadingResult = new LoadingResult(targets.hasError(), !result.isSuccesful(), - result.getTargets(), testsToRun, - collectPackageRoots(pkgLoader.getErrorFreeVisitedPackages())); - freeMemoryAfterLoading(callback, pkgLoader.getVisitedPackageNames()); - return loadingResult; + return doLoadingPhase(eventHandler, eventBus, targets, testsToRun, + labelsToLoadUnconditionally, keepGoing, options.loadingPhaseThreads, callback); } else { - result = doSimpleLoadingPhase(eventHandler, eventBus, targets.getTargets(), keepGoing); - return new LoadingResult(targets.hasError(), !result.isSuccesful(), result.getTargets(), - testsToRun, collectPackageRoots(new ArrayList())); + return doSimpleLoadingPhase(eventHandler, eventBus, targets, testsToRun, keepGoing); } } @@ -400,12 +390,13 @@ public class LoadingPhaseRunner { * It only does test_suite expansion and emits necessary events and logging messages for legacy * support. */ - private BaseLoadingResult doSimpleLoadingPhase(EventHandler eventHandler, EventBus eventBus, - ImmutableSet targetsToLoad, boolean keepGoing) + private LoadingResult doSimpleLoadingPhase(EventHandler eventHandler, EventBus eventBus, + ResolvedTargets targets, ImmutableSet testsToRun, boolean keepGoing) throws LoadingFailedException { Stopwatch timer = preLoadingLogging(eventHandler); - BaseLoadingResult expandedResult; + ImmutableSet targetsToLoad = targets.getTargets(); + ResolvedTargets expandedResult; try { expandedResult = expandTestSuites(eventHandler, targetsToLoad, keepGoing); } catch (TargetParsingException e) { @@ -413,40 +404,39 @@ public class LoadingPhaseRunner { } postLoadingLogging(eventBus, targetsToLoad, expandedResult.getTargets(), timer); - return new BaseLoadingResult(expandedResult.getTargets(), expandedResult.isSuccesful()); + return new LoadingResult(targets.hasError(), expandedResult.hasError(), + expandedResult.getTargets(), testsToRun, ImmutableMap.of()); } /** * Visit the transitive closure of the targets, populating the package cache * and ensuring that all labels can be resolved and all rules were free from * errors. - * - * @param targetsToLoad the list of command-line target patterns specified by the user - * @param labelsToLoadUnconditionally the labels to load unconditionally (presumably for the build - * configuration) - * @param keepGoing if true, don't throw ViewCreationFailedException if some - * targets could not be loaded, just skip thm. */ - private BaseLoadingResult doLoadingPhase(EventHandler eventHandler, EventBus eventBus, - ImmutableSet targetsToLoad, ListMultimap labelsToLoadUnconditionally, - boolean keepGoing, int loadingPhaseThreads) + private LoadingResult doLoadingPhase(EventHandler eventHandler, EventBus eventBus, + ResolvedTargets targets, ImmutableSet testsToRun, + ListMultimap labelsToLoadUnconditionally, boolean keepGoing, + int loadingPhaseThreads, @Nullable Callback callback) throws InterruptedException, LoadingFailedException { Stopwatch timer = preLoadingLogging(eventHandler); - BaseLoadingResult baseResult = performLoadingOfTargets(eventHandler, eventBus, targetsToLoad, - labelsToLoadUnconditionally, keepGoing, loadingPhaseThreads); - BaseLoadingResult expandedResult; + TransitivePackageLoader pkgLoader = packageManager.newTransitiveLoader(); + BaseLoadingResult baseResult = performLoadingOfTargets(eventHandler, eventBus, pkgLoader, + targets.getTargets(), labelsToLoadUnconditionally, keepGoing, loadingPhaseThreads); + ResolvedTargets expandedResult; try { - expandedResult = expandTestSuites(eventHandler, baseResult.getTargets(), - keepGoing); + expandedResult = expandTestSuites(eventHandler, baseResult.getTargets(), keepGoing); } catch (TargetParsingException e) { // This shouldn't happen, because we've already loaded the targets successfully. throw (AssertionError) (new AssertionError("Unexpected target failure").initCause(e)); } + freeMemoryAfterLoading(callback, pkgLoader.getVisitedPackageNames()); postLoadingLogging(eventBus, baseResult.getTargets(), expandedResult.getTargets(), timer); - return new BaseLoadingResult(expandedResult.getTargets(), - baseResult.isSuccesful() && expandedResult.isSuccesful()); + LoadingResult loadingResult = new LoadingResult(targets.hasError(), + !baseResult.isSuccesful() || expandedResult.hasError(), + expandedResult.getTargets(), testsToRun, baseResult.roots); + return loadingResult; } private Stopwatch preLoadingLogging(EventHandler eventHandler) { @@ -464,9 +454,9 @@ public class LoadingPhaseRunner { } private BaseLoadingResult performLoadingOfTargets(EventHandler eventHandler, EventBus eventBus, - ImmutableSet targetsToLoad, ListMultimap labelsToLoadUnconditionally, - boolean keepGoing, int loadingPhaseThreads) throws InterruptedException, - LoadingFailedException { + TransitivePackageLoader pkgLoader, ImmutableSet targetsToLoad, + ListMultimap labelsToLoadUnconditionally, boolean keepGoing, + int loadingPhaseThreads) throws InterruptedException, LoadingFailedException { Set