Search in sources :

Example 1 with CycleException

use of com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException in project buck by facebook.

the class TargetGraphHashing method hashTargetGraph.

/**
   * Given a {@link TargetGraph} and any number of root nodes to traverse,
   * returns a map of {@code (BuildTarget, HashCode)} pairs for all root
   * build targets and their dependencies.
   */
public ImmutableMap<BuildTarget, HashCode> hashTargetGraph() throws CycleException {
    try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ShowTargetHashes"))) {
        AcyclicDepthFirstPostOrderTraversal<TargetNode<?, ?>> traversal = new AcyclicDepthFirstPostOrderTraversal<>(node -> targetGraph.getAll(node.getDeps()).iterator());
        final Map<BuildTarget, ForkJoinTask<HashCode>> buildTargetHashes = new HashMap<>();
        Queue<ForkJoinTask<HashCode>> tasksToSchedule = new ArrayDeque<>();
        // Start all the node tasks, bottom up
        for (final TargetNode<?, ?> node : traversal.traverse(roots)) {
            HashNodeTask task = new HashNodeTask(node, buildTargetHashes);
            buildTargetHashes.put(node.getBuildTarget(), task);
            tasksToSchedule.add(task);
        }
        // Execute tasks in parallel
        ForkJoinPool pool = new ForkJoinPool(numThreads);
        for (ForkJoinTask<HashCode> task : tasksToSchedule) {
            pool.execute(task);
        }
        // Wait for all scheduled tasks to complete
        return ImmutableMap.copyOf(Maps.transformEntries(buildTargetHashes, (key, value) -> value.join()));
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) PerfEventId(com.facebook.buck.event.PerfEventId) RecursiveTask(java.util.concurrent.RecursiveTask) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Map(java.util.Map) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) CycleException(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException) Hasher(com.google.common.hash.Hasher) StringHashing(com.facebook.buck.hashing.StringHashing) Path(java.nio.file.Path) FileHashLoader(com.facebook.buck.hashing.FileHashLoader) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) ForkJoinTask(java.util.concurrent.ForkJoinTask) ImmutableMap(com.google.common.collect.ImmutableMap) HashCode(com.google.common.hash.HashCode) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) ForkJoinPool(java.util.concurrent.ForkJoinPool) Preconditions(com.google.common.base.Preconditions) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) HashMap(java.util.HashMap) ForkJoinTask(java.util.concurrent.ForkJoinTask) ArrayDeque(java.util.ArrayDeque) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Aggregations

BuckEventBus (com.facebook.buck.event.BuckEventBus)1 PerfEventId (com.facebook.buck.event.PerfEventId)1 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)1 AcyclicDepthFirstPostOrderTraversal (com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal)1 CycleException (com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException)1 FileHashLoader (com.facebook.buck.hashing.FileHashLoader)1 StringHashing (com.facebook.buck.hashing.StringHashing)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Logger (com.facebook.buck.log.Logger)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Maps (com.google.common.collect.Maps)1 HashCode (com.google.common.hash.HashCode)1 Hasher (com.google.common.hash.Hasher)1 Hashing (com.google.common.hash.Hashing)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1