Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.util; |
| 18 | |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 19 | import org.onlab.metrics.MetricsService; |
| 20 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 21 | import java.util.Timer; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 22 | import java.util.concurrent.ExecutorService; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 23 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 25 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 26 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 27 | import static org.onlab.util.Tools.groupedThreads; |
| 28 | |
| 29 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 30 | * Utility for managing a set of shared execution resources, such as a timer, |
| 31 | * single thread executor and thread pool executor for use by various parts of |
| 32 | * the platform or by applications. |
| 33 | * <p> |
| 34 | * Whenever possible, use of these shared resources is encouraged over creating |
| 35 | * separate ones. |
| 36 | * </p> |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 37 | */ |
| 38 | public final class SharedExecutors { |
| 39 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 40 | public static final int DEFAULT_POOL_SIZE = 30; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 42 | private static SharedExecutorService singleThreadExecutor = |
| 43 | new SharedExecutorService( |
| 44 | newSingleThreadExecutor(groupedThreads("onos/shared", |
| 45 | "onos-single-executor"))); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 47 | private static SharedExecutorService poolThreadExecutor = |
| 48 | new SharedExecutorService( |
| 49 | newFixedThreadPool(DEFAULT_POOL_SIZE, |
| 50 | groupedThreads("onos/shared", |
| 51 | "onos-pool-executor-%d"))); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 52 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 53 | private static SharedTimer sharedTimer = new SharedTimer(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 55 | // Ban public construction |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 56 | private SharedExecutors() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 60 | * Returns the shared single thread executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 61 | * |
| 62 | * @return shared single thread executor |
| 63 | */ |
| 64 | public static ExecutorService getSingleThreadExecutor() { |
| 65 | return singleThreadExecutor; |
| 66 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 67 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 68 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 69 | * Returns the shared thread pool executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 70 | * |
| 71 | * @return shared executor pool |
| 72 | */ |
| 73 | public static ExecutorService getPoolThreadExecutor() { |
| 74 | return poolThreadExecutor; |
| 75 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 76 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 77 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 78 | * Returns the shared timer. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 79 | * |
| 80 | * @return shared timer |
| 81 | */ |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 82 | public static Timer getTimer() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 83 | return sharedTimer; |
| 84 | } |
| 85 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Sets the shared thread pool size. |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 88 | * |
| 89 | * @param poolSize new pool size |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 90 | */ |
| 91 | public static void setPoolSize(int poolSize) { |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 92 | checkArgument(poolSize > 0, "Shared pool size size must be greater than 0"); |
| 93 | poolThreadExecutor.setBackingExecutor( |
| 94 | newFixedThreadPool(poolSize, groupedThreads("onos/shared", |
| 95 | "onos-pool-executor-%d"))); |
| 96 | } |
| 97 | |
Thomas Vachuska | 0666f15 | 2016-08-05 12:03:54 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Enables or disables calculation of the pool performance metrics. If |
| 100 | * the metrics service is not null metric collection will be enabled; |
| 101 | * otherwise it will be disabled. |
| 102 | * |
| 103 | * @param metricsService optional metric service |
| 104 | */ |
| 105 | public static void setMetricsService(MetricsService metricsService) { |
| 106 | poolThreadExecutor.setMetricsService(metricsService); |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Shuts down all shared timers and executors and therefore should be |
| 111 | * called only by the framework. |
| 112 | */ |
| 113 | public static void shutdown() { |
| 114 | sharedTimer.shutdown(); |
| 115 | singleThreadExecutor.backingExecutor().shutdown(); |
| 116 | poolThreadExecutor.backingExecutor().shutdown(); |
| 117 | } |
| 118 | |
| 119 | // Timer extension which does not allow outside cancel method. |
| 120 | private static class SharedTimer extends Timer { |
| 121 | |
| 122 | public SharedTimer() { |
| 123 | super("onos-shared-timer"); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void cancel() { |
| 128 | throw new UnsupportedOperationException("Cancel of shared timer is not allowed"); |
| 129 | } |
| 130 | |
| 131 | private void shutdown() { |
| 132 | super.cancel(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 135 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 136 | } |