Shared system timer and executor services - monitoring

Change-Id: Ieaa889447dbcb78e4d27fe7409fae463177372b8
diff --git a/core/net/src/main/java/org/onosproject/core/impl/CoreManager.java b/core/net/src/main/java/org/onosproject/core/impl/CoreManager.java
index a40ded6..eae8cf6 100644
--- a/core/net/src/main/java/org/onosproject/core/impl/CoreManager.java
+++ b/core/net/src/main/java/org/onosproject/core/impl/CoreManager.java
@@ -23,6 +23,7 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onlab.metrics.MetricsService;
 import org.onlab.util.SharedExecutors;
 import org.onosproject.app.ApplicationService;
 import org.onosproject.cfg.ComponentConfigService;
@@ -80,6 +81,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected EventDeliveryService eventDeliveryService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MetricsService metricsService;
+
     private static final int DEFAULT_POOL_SIZE = 30;
     @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
             label = "Configure shared pool maximum size ")
@@ -90,6 +94,12 @@
             label = "Maximum number of millis an event sink has to process an event")
     private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
 
+    private static final boolean DEFAULT_PERFORMANCE_CHECK = false;
+    @Property(name = "sharedThreadPerformanceCheck", boolValue = DEFAULT_PERFORMANCE_CHECK,
+            label = "Enable queue performance check on shared pool")
+    private boolean calculatePoolPerformance = DEFAULT_PERFORMANCE_CHECK;
+
+
     @Activate
     public void activate() {
         registerApplication(CORE_APP_NAME);
@@ -177,8 +187,14 @@
             log.warn("maxEventTimeLimit must be greater than 1");
         }
 
-        log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}",
-                 sharedThreadPoolSize, maxEventTimeLimit);
+        Boolean performanceCheck = isPropertyEnabled(properties, "sharedThreadPerformanceCheck");
+        if (performanceCheck != null) {
+            calculatePoolPerformance = performanceCheck;
+            SharedExecutors.setCalculatePoolPerformance(calculatePoolPerformance, metricsService);
+        }
+
+        log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, calculatePoolPerformance={}",
+                 sharedThreadPoolSize, maxEventTimeLimit, calculatePoolPerformance);
     }
 
 
@@ -202,5 +218,26 @@
         return value;
     }
 
+    /**
+     * Check property name is defined and set to true.
+     *
+     * @param properties   properties to be looked up
+     * @param propertyName the name of the property to look up
+     * @return value when the propertyName is defined or return null
+     */
+    private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
+                                             String propertyName) {
+        Boolean value = null;
+        try {
+            String s = (String) properties.get(propertyName);
+            value = isNullOrEmpty(s) ? null : s.trim().equals("true");
+        } catch (ClassCastException e) {
+            // No propertyName defined.
+            value = null;
+        }
+        return value;
+    }
+
+
 
 }