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. |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.exec; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
ulfjack | 734b9e9 | 2017-06-19 16:22:17 +0200 | [diff] [blame] | 18 | import com.google.devtools.build.lib.actions.ActionContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.ExecutionStrategy; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.events.Reporter; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.rules.fileset.FilesetActionContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import java.util.concurrent.LinkedBlockingQueue; |
| 23 | import java.util.concurrent.ThreadPoolExecutor; |
| 24 | import 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) |
| 33 | public 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 Wollermann | edc15b7 | 2015-06-18 12:52:48 +0000 | [diff] [blame] | 39 | public static class Provider extends ActionContextProvider { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | 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 |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 60 | public Iterable<? extends ActionContext> getActionContexts() { |
| 61 | return ImmutableList.of(impl); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | 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 | } |