Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 42 | private static SharedExecutorService singleThreadExecutor; |
| 43 | private static SharedExecutorService poolThreadExecutor; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 44 | |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 45 | private static SharedTimer sharedTimer; |
Ray Milkey | 397d8d4 | 2019-03-22 09:05:02 -0700 | [diff] [blame] | 46 | private static final Object SHARED_TIMER_LOCK = new Object(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 47 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 48 | // Ban public construction |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 49 | private SharedExecutors() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 53 | * Returns the shared single thread executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 54 | * |
| 55 | * @return shared single thread executor |
| 56 | */ |
| 57 | public static ExecutorService getSingleThreadExecutor() { |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 58 | setup(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 59 | return singleThreadExecutor; |
| 60 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 61 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 62 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 63 | * Returns the shared thread pool executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 64 | * |
| 65 | * @return shared executor pool |
| 66 | */ |
| 67 | public static ExecutorService getPoolThreadExecutor() { |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 68 | setup(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 69 | return poolThreadExecutor; |
| 70 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 71 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 72 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 73 | * Returns the shared timer. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 74 | * |
| 75 | * @return shared timer |
| 76 | */ |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 77 | public static Timer getTimer() { |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 78 | setup(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 79 | return sharedTimer; |
| 80 | } |
| 81 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Sets the shared thread pool size. |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 84 | * |
| 85 | * @param poolSize new pool size |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 86 | */ |
| 87 | public static void setPoolSize(int poolSize) { |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 88 | checkArgument(poolSize > 0, "Shared pool size size must be greater than 0"); |
| 89 | poolThreadExecutor.setBackingExecutor( |
| 90 | newFixedThreadPool(poolSize, groupedThreads("onos/shared", |
| 91 | "onos-pool-executor-%d"))); |
| 92 | } |
| 93 | |
Thomas Vachuska | 0666f15 | 2016-08-05 12:03:54 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Enables or disables calculation of the pool performance metrics. If |
| 96 | * the metrics service is not null metric collection will be enabled; |
| 97 | * otherwise it will be disabled. |
| 98 | * |
| 99 | * @param metricsService optional metric service |
| 100 | */ |
| 101 | public static void setMetricsService(MetricsService metricsService) { |
| 102 | poolThreadExecutor.setMetricsService(metricsService); |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Shuts down all shared timers and executors and therefore should be |
| 107 | * called only by the framework. |
| 108 | */ |
| 109 | public static void shutdown() { |
Ray Milkey | 397d8d4 | 2019-03-22 09:05:02 -0700 | [diff] [blame] | 110 | synchronized (SHARED_TIMER_LOCK) { |
| 111 | sharedTimer.shutdown(); |
| 112 | singleThreadExecutor.backingExecutor().shutdown(); |
| 113 | poolThreadExecutor.backingExecutor().shutdown(); |
| 114 | sharedTimer = null; |
| 115 | singleThreadExecutor = null; |
| 116 | poolThreadExecutor = null; |
| 117 | } |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Ray Milkey | 397d8d4 | 2019-03-22 09:05:02 -0700 | [diff] [blame] | 120 | private static void setup() { |
| 121 | synchronized (SHARED_TIMER_LOCK) { |
| 122 | if (sharedTimer == null) { |
| 123 | sharedTimer = new SharedTimer(); |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 124 | |
Ray Milkey | 397d8d4 | 2019-03-22 09:05:02 -0700 | [diff] [blame] | 125 | singleThreadExecutor = |
| 126 | new SharedExecutorService( |
| 127 | newSingleThreadExecutor(groupedThreads("onos/shared", |
| 128 | "onos-single-executor"))); |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 129 | |
Ray Milkey | 397d8d4 | 2019-03-22 09:05:02 -0700 | [diff] [blame] | 130 | poolThreadExecutor = |
| 131 | new SharedExecutorService( |
| 132 | newFixedThreadPool(DEFAULT_POOL_SIZE, |
| 133 | groupedThreads("onos/shared", |
| 134 | "onos-pool-executor-%d"))); |
| 135 | } |
Ray Milkey | 9bf3b2a8 | 2019-03-14 13:47:31 -0700 | [diff] [blame] | 136 | } |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Timer extension which does not allow outside cancel method. |
| 140 | private static class SharedTimer extends Timer { |
| 141 | |
| 142 | public SharedTimer() { |
| 143 | super("onos-shared-timer"); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void cancel() { |
| 148 | throw new UnsupportedOperationException("Cancel of shared timer is not allowed"); |
| 149 | } |
| 150 | |
| 151 | private void shutdown() { |
| 152 | super.cancel(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 155 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 156 | } |