Search in sources :

Example 16 with ForkJoinPool

use of java.util.concurrent.ForkJoinPool 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)

Example 17 with ForkJoinPool

use of java.util.concurrent.ForkJoinPool in project buck by facebook.

the class PythonLibraryDescriptionTest method versionedResources.

@Test
public void versionedResources() throws Exception {
    BuildTarget target = BuildTargetFactory.newInstance("//foo:lib");
    SourcePath matchedSource = new FakeSourcePath("foo/a.py");
    SourcePath unmatchedSource = new FakeSourcePath("foo/b.py");
    GenruleBuilder transitiveDepBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:tdep")).setOut("out");
    VersionedAliasBuilder depBuilder = new VersionedAliasBuilder(BuildTargetFactory.newInstance("//:dep")).setVersions(ImmutableMap.of(Version.of("1.0"), transitiveDepBuilder.getTarget(), Version.of("2.0"), transitiveDepBuilder.getTarget()));
    AbstractNodeBuilder<?, ?, ?> builder = new PythonLibraryBuilder(target).setVersionedResources(VersionMatchedCollection.<SourceList>builder().add(ImmutableMap.of(depBuilder.getTarget(), Version.of("1.0")), SourceList.ofUnnamedSources(ImmutableSortedSet.of(matchedSource))).add(ImmutableMap.of(depBuilder.getTarget(), Version.of("2.0")), SourceList.ofUnnamedSources(ImmutableSortedSet.of(unmatchedSource))).build());
    TargetGraph targetGraph = VersionedTargetGraphBuilder.transform(new FixedVersionSelector(ImmutableMap.of(builder.getTarget(), ImmutableMap.of(depBuilder.getTarget(), Version.of("1.0")))), TargetGraphAndBuildTargets.of(TargetGraphFactory.newInstance(transitiveDepBuilder.build(), depBuilder.build(), builder.build()), ImmutableSet.of(builder.getTarget())), new ForkJoinPool()).getTargetGraph();
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    PythonLibrary library = (PythonLibrary) resolver.requireRule(builder.getTarget());
    assertThat(library.getPythonPackageComponents(PythonTestUtils.PYTHON_PLATFORM, CxxPlatformUtils.DEFAULT_PLATFORM).getResources().values(), Matchers.contains(matchedSource));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) GenruleBuilder(com.facebook.buck.shell.GenruleBuilder) CxxGenruleBuilder(com.facebook.buck.cxx.CxxGenruleBuilder) FixedVersionSelector(com.facebook.buck.versions.FixedVersionSelector) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) VersionedAliasBuilder(com.facebook.buck.versions.VersionedAliasBuilder) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) SourceList(com.facebook.buck.rules.coercer.SourceList) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 18 with ForkJoinPool

use of java.util.concurrent.ForkJoinPool in project buck by facebook.

the class VersionedTargetGraphCacheTest method testPoolChangeCausesHit.

@Test
public void testPoolChangeCausesHit() throws Exception {
    VersionedTargetGraphCache cache = new VersionedTargetGraphCache();
    TargetGraphAndBuildTargets graph = createSimpleGraph();
    VersionedTargetGraphCacheResult firstResult = cache.getVersionedTargetGraph(BUS, graph, ImmutableMap.of(), POOL);
    assertEmpty(firstResult);
    VersionedTargetGraphCacheResult secondResult = cache.getVersionedTargetGraph(BUS, graph, ImmutableMap.of(), new ForkJoinPool(2));
    assertHit(secondResult, firstResult.getTargetGraphAndBuildTargets());
}
Also used : TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 19 with ForkJoinPool

use of java.util.concurrent.ForkJoinPool in project torodb by torodb.

the class DefaultConcurrentToolsFactory method createExecutorService.

@Override
@SuppressFBWarnings(value = { "NP_NONNULL_PARAM_VIOLATION" }, justification = "ForkJoinPool constructor admits a null " + "UncaughtExceptionHandler")
public ExecutorService createExecutorService(String prefix, boolean blockerTasks, int maxThreads) {
    ExecutorService executorService;
    if (blockerTasks) {
        ThreadFactory threadFactory = blockerThreadFactoryFunction.apply(prefix);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maxThreads, maxThreads, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), threadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        executorService = threadPoolExecutor;
    } else {
        ForkJoinWorkerThreadFactory threadFactory = forkJoinThreadFactoryFunction.apply(prefix);
        executorService = new ForkJoinPool(maxThreads, threadFactory, null, true);
    }
    shutdownHelper.terminateOnShutdown(prefix, executorService);
    return executorService;
}
Also used : ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ForkJoinPool(java.util.concurrent.ForkJoinPool) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 20 with ForkJoinPool

use of java.util.concurrent.ForkJoinPool in project jdk8u_jdk by JetBrains.

the class NQueensCS method main.

/**
     * Usage: NQueensCS [minBoardSize=N] [maxBoardSize=N] [procs=N] [reps=N]
     */
public static void main(String[] args) throws Exception {
    // Board sizes too small: hard to measure well.
    // Board sizes too large: take too long to run.
    final int minBoardSize = intArg(args, "minBoardSize", 8);
    final int maxBoardSize = intArg(args, "maxBoardSize", 15);
    final int procs = intArg(args, "procs", 0);
    for (int reps = intArg(args, "reps", 10); reps > 0; reps--) {
        ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : new ForkJoinPool(procs);
        lastStealCount = g.getStealCount();
        for (int i = minBoardSize; i <= maxBoardSize; i++) test(g, i);
        System.out.println(g);
        g.shutdown();
    }
}
Also used : ForkJoinPool(java.util.concurrent.ForkJoinPool)

Aggregations

ForkJoinPool (java.util.concurrent.ForkJoinPool)24 Test (org.junit.Test)6 List (java.util.List)5 Map (java.util.Map)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 File (java.io.File)3 Serializable (java.io.Serializable)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)2 ForkJoinWorkerThread (java.util.concurrent.ForkJoinWorkerThread)2