blob: d0e7ff1431da568e01df0e0f84be2c589b048964 [file] [log] [blame]
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07003 *
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
17package org.onlab.util;
18
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080019import org.onlab.metrics.MetricsService;
20
Thomas Vachuska945e7b02015-03-18 14:23:53 -070021import java.util.Timer;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070022import java.util.concurrent.ExecutorService;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070023
Thomas Vachuskab0317c62015-04-08 23:58:58 -070024import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuska945e7b02015-03-18 14:23:53 -070025import static java.util.concurrent.Executors.newFixedThreadPool;
26import static java.util.concurrent.Executors.newSingleThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070027import static org.onlab.util.Tools.groupedThreads;
28
29/**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070030 * 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 Parlakisikaf1042d2015-03-14 01:08:29 -070037 */
38public final class SharedExecutors {
39
Thomas Vachuskab0317c62015-04-08 23:58:58 -070040 public static final int DEFAULT_POOL_SIZE = 30;
Murat Parlakisik553db172015-04-08 03:29:04 -070041
Thomas Vachuskab0317c62015-04-08 23:58:58 -070042 private static SharedExecutorService singleThreadExecutor =
43 new SharedExecutorService(
44 newSingleThreadExecutor(groupedThreads("onos/shared",
45 "onos-single-executor")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070046
Thomas Vachuskab0317c62015-04-08 23:58:58 -070047 private static SharedExecutorService poolThreadExecutor =
48 new SharedExecutorService(
49 newFixedThreadPool(DEFAULT_POOL_SIZE,
50 groupedThreads("onos/shared",
51 "onos-pool-executor-%d")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070052
Thomas Vachuskab0317c62015-04-08 23:58:58 -070053 private static SharedTimer sharedTimer = new SharedTimer();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070054
Thomas Vachuska945e7b02015-03-18 14:23:53 -070055 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070056 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070057 }
58
59 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070060 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070061 *
62 * @return shared single thread executor
63 */
64 public static ExecutorService getSingleThreadExecutor() {
65 return singleThreadExecutor;
66 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070067
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070068 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070069 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070070 *
71 * @return shared executor pool
72 */
73 public static ExecutorService getPoolThreadExecutor() {
74 return poolThreadExecutor;
75 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070076
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070077 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070078 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070079 *
80 * @return shared timer
81 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070082 public static Timer getTimer() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070083 return sharedTimer;
84 }
85
Murat Parlakisik553db172015-04-08 03:29:04 -070086 /**
87 * Sets the shared thread pool size.
Thomas Vachuskab0317c62015-04-08 23:58:58 -070088 *
89 * @param poolSize new pool size
Murat Parlakisik553db172015-04-08 03:29:04 -070090 */
91 public static void setPoolSize(int poolSize) {
Thomas Vachuskab0317c62015-04-08 23:58:58 -070092 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
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080098
99 public static void setCalculatePoolPerformance(boolean calculatePoolPerformance, MetricsService metricsService) {
100 poolThreadExecutor.setCalculatePoolPerformance(calculatePoolPerformance, metricsService);
101 }
102
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700103 /**
104 * Shuts down all shared timers and executors and therefore should be
105 * called only by the framework.
106 */
107 public static void shutdown() {
108 sharedTimer.shutdown();
109 singleThreadExecutor.backingExecutor().shutdown();
110 poolThreadExecutor.backingExecutor().shutdown();
111 }
112
113 // Timer extension which does not allow outside cancel method.
114 private static class SharedTimer extends Timer {
115
116 public SharedTimer() {
117 super("onos-shared-timer");
118 }
119
120 @Override
121 public void cancel() {
122 throw new UnsupportedOperationException("Cancel of shared timer is not allowed");
123 }
124
125 private void shutdown() {
126 super.cancel();
Murat Parlakisik553db172015-04-08 03:29:04 -0700127 }
128 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700129
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700130}