blob: e76253d12db04617146f5fe7fa2f48654f4fddc8 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.perfmon;
2
3import java.util.Collection;
4import java.util.List;
5import java.util.Map;
6import java.util.concurrent.ConcurrentHashMap;
7
8import org.codehaus.jackson.map.annotate.JsonSerialize;
9
10import net.floodlightcontroller.core.IOFMessageListener;
11
12@JsonSerialize(using=CumulativeTimeBucketJSONSerializer.class)
13public class CumulativeTimeBucket {
14 private long startTime_ns; // First pkt time-stamp in this bucket
15 private Map<Integer, OneComponentTime> compStats;
16 private long totalPktCnt;
17 private long totalProcTimeNs; // total processing time for one pkt in
18 private long sumSquaredProcTimeNs2;
19 private long maxTotalProcTimeNs;
20 private long minTotalProcTimeNs;
21 private long avgTotalProcTimeNs;
22 private long sigmaTotalProcTimeNs; // std. deviation
23
24 public long getStartTimeNs() {
25 return startTime_ns;
26 }
27
28 public long getTotalPktCnt() {
29 return totalPktCnt;
30 }
31
32 public long getAverageProcTimeNs() {
33 return avgTotalProcTimeNs;
34 }
35
36 public long getMinTotalProcTimeNs() {
37 return minTotalProcTimeNs;
38 }
39
40 public long getMaxTotalProcTimeNs() {
41 return maxTotalProcTimeNs;
42 }
43
44 public long getTotalSigmaProcTimeNs() {
45 return sigmaTotalProcTimeNs;
46 }
47
48 public int getNumComps() {
49 return compStats.values().size();
50 }
51
52 public Collection<OneComponentTime> getModules() {
53 return compStats.values();
54 }
55
56 public CumulativeTimeBucket(List<IOFMessageListener> listeners) {
57 compStats = new ConcurrentHashMap<Integer, OneComponentTime>(listeners.size());
58 for (IOFMessageListener l : listeners) {
59 OneComponentTime oct = new OneComponentTime(l);
60 compStats.put(oct.hashCode(), oct);
61 }
62 startTime_ns = System.nanoTime();
63 }
64
65 private void updateSquaredProcessingTime(long curTimeNs) {
66 sumSquaredProcTimeNs2 += (Math.pow(curTimeNs, 2));
67 }
68
69 /**
70 * Resets all counters and counters for each component time
71 */
72 public void reset() {
73 startTime_ns = System.nanoTime();
74 totalPktCnt = 0;
75 totalProcTimeNs = 0;
76 avgTotalProcTimeNs = 0;
77 sumSquaredProcTimeNs2 = 0;
78 maxTotalProcTimeNs = Long.MIN_VALUE;
79 minTotalProcTimeNs = Long.MAX_VALUE;
80 sigmaTotalProcTimeNs = 0;
81 for (OneComponentTime oct : compStats.values()) {
82 oct.resetAllCounters();
83 }
84 }
85
86 private void computeSigma() {
87 // Computes std. deviation from the sum of count numbers and from
88 // the sum of the squares of count numbers
89 double temp = totalProcTimeNs;
90 temp = Math.pow(temp, 2) / totalPktCnt;
91 temp = (sumSquaredProcTimeNs2 - temp) / totalPktCnt;
92 sigmaTotalProcTimeNs = (long) Math.sqrt(temp);
93 }
94
95 public void computeAverages() {
96 // Must be called last to, needs latest info
97 computeSigma();
98
99 for (OneComponentTime oct : compStats.values()) {
100 oct.computeSigma();
101 }
102 }
103
104 public void updatePerPacketCounters(long procTimeNs) {
105 totalPktCnt++;
106 totalProcTimeNs += procTimeNs;
107 avgTotalProcTimeNs = totalProcTimeNs / totalPktCnt;
108 updateSquaredProcessingTime(procTimeNs);
109
110 if (procTimeNs > maxTotalProcTimeNs) {
111 maxTotalProcTimeNs = procTimeNs;
112 }
113
114 if (procTimeNs < minTotalProcTimeNs) {
115 minTotalProcTimeNs = procTimeNs;
116 }
117 }
118
119 public void updateOneComponent(IOFMessageListener l, long procTimeNs) {
120 compStats.get(l.hashCode()).updatePerPacketCounters(procTimeNs);
121 }
122}