2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.statistics;
11 import java.util.Timer;
12 import java.util.TimerTask;
13 import java.util.concurrent.ConcurrentHashMap;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Singleton class to hold and process counters
23 public final class StatisticsCounters {
26 * Default delay between two writings into log (milliseconds)
28 public static final int DEFAULT_LOG_REPORT_PERIOD = 10000;
30 * Minimal delay between two writings into log (milliseconds)
32 public static final int MINIMAL_LOG_REPORT_PERIOD = 500;
33 private static StatisticsCounters instanceHolder;
34 private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsCounters.class);
36 private Timer logReporter;
37 private int logReportPeriod;
38 private boolean runLogReport;
39 private Map<CounterEventTypes, Counter> countersMap;
40 private boolean runCounting;
41 // array to hold enabled counter types
42 private CounterEventTypes[] enabledCounters = {
43 CounterEventTypes.DS_ENCODE_FAIL,
44 CounterEventTypes.DS_ENCODE_SUCCESS,
45 CounterEventTypes.DS_ENTERED_OFJAVA,
46 CounterEventTypes.DS_FLOW_MODS_ENTERED,
47 CounterEventTypes.DS_FLOW_MODS_SENT,
48 CounterEventTypes.US_DECODE_FAIL,
49 CounterEventTypes.US_DECODE_SUCCESS,
50 CounterEventTypes.US_MESSAGE_PASS,
51 CounterEventTypes.US_RECEIVED_IN_OFJAVA};
54 * Get instance of statistics counters, first created object does not start counting and log reporting
57 public synchronized static StatisticsCounters getInstance() {
58 if (instanceHolder == null) {
59 instanceHolder = new StatisticsCounters();
61 return instanceHolder;
64 private StatisticsCounters() {
65 countersMap = new ConcurrentHashMap<>();
66 for(CounterEventTypes cet : enabledCounters){
67 countersMap.put(cet, new Counter());
70 this.logReportPeriod = -1;
71 this.runLogReport = false;
72 LOGGER.debug("StaticsCounters has been created");
77 * @param resetCounters - true = statistics counters will be reset before start counting
78 * @param reportToLogs - true = statistics counters will periodically write to log
79 * @param logReportDelay - delay between two writings into logs in milliseconds (for details see startLogReport(int logReportDelay))
81 public void startCounting(boolean resetCounters, boolean reportToLogs, int logReportDelay){
85 LOGGER.debug("Start counting...");
87 startLogReport(logReportDelay);
96 * Start counting (counters are set to 0 before start counting)
97 * @param reportToLogs - true = statistics counters will periodically write to log
98 * @param logReportDelay - delay between two writings into logs in milliseconds (for details see startLogReport(int logReportDelay))
100 public void startCounting(boolean reportToLogs, int logReportDelay){
104 startCounting(true,reportToLogs,logReportDelay);
108 * Stop counting, values in counters are untouched, log reporter is stopped
110 public void stopCounting(){
112 LOGGER.debug("Stop counting...");
117 * Give an information if counting is running
118 * @return true, if counting is running, otherwise false
120 public boolean isRunCounting(){
125 * Start write statistics into logs, if writing is run calling has no effect.
126 * If method is called without previous setting of report delay than DEFAULT_LOG_REPORT_PERIOD will be used.
128 public void startLogReport(){
132 if(this.logReportPeriod <= 0){
133 this.logReportPeriod = DEFAULT_LOG_REPORT_PERIOD;
135 if(this.logReportPeriod <= MINIMAL_LOG_REPORT_PERIOD){
136 this.logReportPeriod = MINIMAL_LOG_REPORT_PERIOD;
138 logReporter = new Timer("SC_Timer");
139 logReporter.schedule(new LogReporterTask(this), this.logReportPeriod,this.logReportPeriod);
141 LOGGER.debug("Statistics log reporter has been scheduled with period {} ms", this.logReportPeriod);
145 * Start write statistics into logs with given delay between writings, if writing is run calling has no effect.
146 * @param logReportDelay - delay between two writings into logs (milliseconds).
147 * It is mandatory if reportToLogs is true, value have to be greater than 0 (zero)
148 * If value is smaller than MINIMAL_LOG_REPORT_PERIOD, the value MINIMAL_LOG_REPORT_PERIOD will be used.
149 * @exception IllegalArgumentException if logReportDelay is not greater than 0 (zero)
151 public void startLogReport(int logReportDelay){
155 if(logReportDelay <= 0){
156 throw new IllegalArgumentException("logReportPeriod have to bee greater than 0 zero");
158 if(logReportDelay < MINIMAL_LOG_REPORT_PERIOD){
159 this.logReportPeriod = MINIMAL_LOG_REPORT_PERIOD;
161 this.logReportPeriod = logReportDelay;
167 * Stop write statistics into logs, counting does not stop
169 public void stopLogReport(){
171 if(logReporter != null){
172 logReporter.cancel();
173 LOGGER.debug("Statistics log reporter has been canceled");
175 runLogReport = false;
180 * Give an information if log reporter is running (statistics are write into logs).
181 * @return true if log reporter writes statistics into log, otherwise false
183 public boolean isRunLogReport(){
188 * @return the current delay between two writings into logs
190 public int getLogReportPeriod() {
191 return logReportPeriod;
195 * @return the enabled counters
197 protected CounterEventTypes[] getEnabledCounters() {
198 return enabledCounters;
201 * @return the countersMap
203 protected Map<CounterEventTypes, Counter> getCountersMap() {
208 * Give an information if is given counter is enabled
209 * @param counterEventKey
210 * @return true if counter has been Enabled, otherwise false
212 public boolean isCounterEnabled(CounterEventTypes counterEventKey){
213 if (counterEventKey == null) {
216 return countersMap.containsKey(counterEventKey);
220 * Get counter by counter event type
221 * @param counterEventKey key to identify counter (can not be null)
222 * @return - Counter object or null if counter has not been enabled
223 * @exception - IllegalArgumentException if counterEventKey is null
225 public Counter getCounter(CounterEventTypes counterEventKey) {
226 if (counterEventKey == null) {
227 throw new IllegalArgumentException("counterEventKey can not be null");
229 return countersMap.get(counterEventKey);
233 * Increment value of given counter
234 * @param counterEventKey key to identify counter
236 public void incrementCounter(CounterEventTypes counterEventKey) {
238 if (isCounterEnabled(counterEventKey)){
239 countersMap.get(counterEventKey).incrementCounter();
245 * Set values of all counter to 0 (zero)
247 public void resetCounters() {
248 for(CounterEventTypes cet : enabledCounters){
249 countersMap.get(cet).reset();
251 LOGGER.debug("StaticsCounters has been reset");
255 * internal class to process logReporter
259 private static class LogReporterTask extends TimerTask {
260 private static final Logger LOG = LoggerFactory.getLogger(LogReporterTask.class);
262 private StatisticsCounters sc;
263 public LogReporterTask(StatisticsCounters sc) {
269 for(CounterEventTypes cet : sc.getEnabledCounters()){
270 LOG.debug(cet.toString() + ": " + sc.getCountersMap().get(cet).toString());