blob: d21b27069e1a254ed55a38bd92e6d7a3ab7fd2a5 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
Ulf Adamsdba3c832016-12-21 16:50:02 +000014package com.google.devtools.build.lib.exec;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010015
16import com.google.common.collect.ImmutableList;
17import com.google.common.util.concurrent.ThreadFactoryBuilder;
ulfjack734b9e92017-06-19 16:22:17 +020018import com.google.devtools.build.lib.actions.ActionContext;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.actions.ExecutionStrategy;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.lib.events.Reporter;
Ulf Adamsdba3c832016-12-21 16:50:02 +000021import com.google.devtools.build.lib.rules.fileset.FilesetActionContext;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import java.util.concurrent.LinkedBlockingQueue;
23import java.util.concurrent.ThreadPoolExecutor;
24import java.util.concurrent.TimeUnit;
25
26/**
27 * Context for Fileset manifest actions. It currently only provides a ThreadPoolExecutor.
28 *
29 * <p>Fileset is a legacy, google-internal mechanism to make parts of the source tree appear as a
30 * tree in the output directory.
31 */
32@ExecutionStrategy(contextType = FilesetActionContext.class)
33public final class FilesetActionContextImpl implements FilesetActionContext {
34 // TODO(bazel-team): it would be nice if this weren't shipped in Bazel at all.
35
36 /**
37 * Factory class.
38 */
Philipp Wollermannedc15b72015-06-18 12:52:48 +000039 public static class Provider extends ActionContextProvider {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 private FilesetActionContextImpl impl;
41 private final Reporter reporter;
42 private final ThreadPoolExecutor filesetPool;
43
44 public Provider(Reporter reporter, String workspaceName) {
45 this.reporter = reporter;
46 this.filesetPool = newFilesetPool(100);
47 this.impl = new FilesetActionContextImpl(filesetPool, workspaceName);
48 }
49
50 private static ThreadPoolExecutor newFilesetPool(int threads) {
51 ThreadPoolExecutor pool = new ThreadPoolExecutor(threads, threads, 3L, TimeUnit.SECONDS,
52 new LinkedBlockingQueue<Runnable>());
53 // Do not consume threads when not in use.
54 pool.allowCoreThreadTimeOut(true);
55 pool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("Fileset worker %d").build());
56 return pool;
57 }
58
59 @Override
ulfjackacd291a2017-06-16 15:25:42 +020060 public Iterable<? extends ActionContext> getActionContexts() {
61 return ImmutableList.of(impl);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062 }
63
64 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 public void executionPhaseEnding() {
66 BlazeExecutor.shutdownHelperPool(reporter, filesetPool, "Fileset");
67 }
68 }
69
70 private final ThreadPoolExecutor filesetPool;
71 private final String workspaceName;
72
73 private FilesetActionContextImpl(ThreadPoolExecutor filesetPool, String workspaceName) {
74 this.filesetPool = filesetPool;
75 this.workspaceName = workspaceName;
76 }
77
78 @Override
79 public ThreadPoolExecutor getFilesetPool() {
80 return filesetPool;
81 }
82
83 @Override
84 public String getWorkspaceName() {
85 return workspaceName;
86 }
87}