357c0ab69475bd2cbcd5673c8a1761048e263f28
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / ofpspecific / EventsTimeCounter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics.ofpspecific;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
17
18 /**
19  * Created by Martin Bobak <mbobak@cisco.com> on 28.5.2015.
20  */
21 public final class EventsTimeCounter {
22
23     private static final Map<String, Map<String, EventTimeCounter>> DEVICES_EVENTS = new HashMap<>();
24
25     private EventsTimeCounter() {
26         // Hiding implicit constructor
27     }
28
29     public static void markStart(final EventIdentifier eventIdentifier) {
30         Map<String, EventTimeCounter> deviceEvents = getOrCreateCountersForDevice(eventIdentifier.getDeviceId());
31         EventTimeCounter eventTimeCounter = getOrCreateEventOfType(eventIdentifier.getEventName(), deviceEvents);
32         eventTimeCounter.markStart();
33     }
34
35     public static void markEnd(final EventIdentifier eventIdentifier) {
36         Map<String, EventTimeCounter> deviceEvents = getOrCreateCountersForDevice(eventIdentifier.getDeviceId());
37         EventTimeCounter eventTimeCounter = getOrCreateEventOfType(eventIdentifier.getEventName(), deviceEvents);
38         eventTimeCounter.markEnd();
39     }
40
41     private static EventTimeCounter getOrCreateEventOfType(final String event, final Map<String, EventTimeCounter> deviceEvents) {
42         EventTimeCounter lookup = deviceEvents.get(event);
43         if (null == lookup) {
44             lookup = new EventTimeCounter();
45             deviceEvents.put(event, lookup);
46         }
47         return lookup;
48     }
49
50     private static Map<String, EventTimeCounter> getOrCreateCountersForDevice(final String deviceId) {
51         Map<String, EventTimeCounter> lookup = DEVICES_EVENTS.get(deviceId);
52         if (null == lookup) {
53             lookup = new HashMap<>();
54             DEVICES_EVENTS.put(deviceId, lookup);
55         }
56
57         return lookup;
58     }
59
60     public static List<String> provideTimes() {
61         List<String> dump = new ArrayList<>();
62         for (Map.Entry<String, Map<String, EventTimeCounter>> deviceEntry : DEVICES_EVENTS.entrySet()) {
63             Map<String, EventTimeCounter> eventsMap = deviceEntry.getValue();
64             dump.add("================================================");
65             dump.add(String.format("DEVICE : %s", deviceEntry.getKey()));
66             for (Map.Entry<String, EventTimeCounter> eventEntry : eventsMap.entrySet()) {
67                 final String eventName = eventEntry.getKey();
68                 final EventTimeCounter eventTimeCounter = eventEntry.getValue();
69                 dump.add(String.format("%s", eventName));
70                 dump.add(String.format("    MIN TIME (ms):  %d",
71                         TimeUnit.MILLISECONDS.convert(eventTimeCounter.getMinimum(), TimeUnit.NANOSECONDS)));
72                 dump.add(String.format("    MAX TIME (ms):  %d",
73                         TimeUnit.MILLISECONDS.convert(eventTimeCounter.getMaximum(), TimeUnit.NANOSECONDS)));
74                 dump.add(String.format("    AVG TIME (ms):  %d",
75                         TimeUnit.MILLISECONDS.convert(eventTimeCounter.getAverage(), TimeUnit.NANOSECONDS)));
76
77             }
78         }
79         return dump;
80     }
81
82     public static void resetAllCounters() {
83         DEVICES_EVENTS.clear();
84     }
85
86
87     private static final class EventTimeCounter {
88
89         private volatile long delta = 0;
90         private volatile long average = 0;
91         private volatile long minimum = 0;
92         private volatile long maximum = 0;
93         private volatile long summary = 0;
94         private volatile int counter = 0;
95
96         public synchronized void markStart() {
97             delta = System.nanoTime();
98         }
99
100         public synchronized void markEnd() {
101             if (0 == delta) {
102                 return;
103             }
104             counter++;
105             delta = System.nanoTime() - delta;
106
107             if (delta < minimum || minimum == 0) {
108                 minimum = delta;
109             }
110             if (delta > maximum) {
111                 maximum = delta;
112             }
113             if (average > 0 && delta > (average * 1.8)) {
114                 summary += average;
115             } else {
116                 summary += delta;
117             }
118             average = summary / counter;
119         }
120
121         public synchronized void resetCounters() {
122             delta = 0;
123             average = 0;
124             minimum = 0;
125             maximum = 0;
126             summary = 0;
127             counter = 0;
128
129         }
130
131         public synchronized long getAverage() {
132             return average;
133         }
134
135         public synchronized long getMinimum() {
136             return minimum;
137         }
138
139         public synchronized long getMaximum() {
140             return maximum;
141         }
142
143     }
144
145
146 }