Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 3 | import java.util.Map.Entry; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 4 | import java.util.Queue; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 5 | import java.util.concurrent.ConcurrentHashMap; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 6 | import java.util.concurrent.ConcurrentLinkedQueue; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 7 | |
| 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
| 10 | |
| 11 | /** |
| 12 | * Class for collecting performance measurements |
| 13 | */ |
| 14 | public class PerformanceMonitor { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 15 | private final static Logger log = LoggerFactory.getLogger(PerformanceMonitor.class); |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 16 | |
| 17 | // experiment name -> PerformanceMonitor |
| 18 | private static final ConcurrentHashMap<String,PerformanceMonitor> perfMons = new ConcurrentHashMap<>(); |
| 19 | public static PerformanceMonitor experiment(String name) { |
| 20 | PerformanceMonitor pm = perfMons.get(name); |
| 21 | if (pm == null) { |
| 22 | pm = new PerformanceMonitor(); |
| 23 | PerformanceMonitor existing = perfMons.putIfAbsent(name, pm); |
| 24 | if (existing != null) { |
| 25 | pm = existing; |
| 26 | } |
| 27 | } |
| 28 | return pm; |
| 29 | } |
| 30 | |
| 31 | // tag -> Measurements |
| 32 | private final ConcurrentHashMap<String, Queue<Measurement>> map = new ConcurrentHashMap<>(); |
| 33 | private long overhead; |
| 34 | private long experimentStart = Long.MAX_VALUE; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 35 | private final static double normalization = Math.pow(10, 6); |
| 36 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Start a performance measurement, identified by a tag |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 39 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 40 | * Note: Only a single measurement can use the same tag at a time. |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 41 | * ..... not true anymore. |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 42 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 43 | * @param tag for performance measurement |
| 44 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 45 | public Measurement startStep(String tag) { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 46 | long start = System.nanoTime(); |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 47 | if(start < experimentStart) { |
| 48 | experimentStart = start; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 49 | } |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 50 | Queue<Measurement> list = map.get(tag); |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 51 | if(list == null) { |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 52 | list = new ConcurrentLinkedQueue<Measurement>(); |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 53 | Queue<Measurement> existing_list = map.putIfAbsent(tag, list); |
| 54 | if (existing_list != null) { |
| 55 | // someone concurrently added, using theirs |
| 56 | list = existing_list; |
| 57 | } |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 58 | } |
| 59 | Measurement m = new Measurement(); |
| 60 | list.add(m); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 61 | m.start(); |
| 62 | overhead += System.nanoTime() - start; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 63 | return m; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 64 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 65 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 66 | /** |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 67 | * Stop a performance measurement. |
| 68 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 69 | * You must have already started a measurement with tag. |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 70 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 71 | * @param tag for performance measurement |
| 72 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 73 | public void stopStep(String tag) { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 74 | long time = System.nanoTime(); |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 75 | Queue<Measurement> list = map.get(tag); |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 76 | if(list == null || list.size() == 0) { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 77 | log.error("Tag {} does not exist", tag); |
| 78 | } |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 79 | list.peek().stop(time); |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 80 | if(list.size() > 1) { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 81 | log.error("Tag {} has multiple measurements", tag); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 82 | } |
| 83 | overhead += System.nanoTime() - time; |
| 84 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 85 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Clear all performance measurements. |
| 88 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 89 | public void reset() { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 90 | map.clear(); |
| 91 | overhead = 0; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 92 | experimentStart = Long.MAX_VALUE; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 93 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 94 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Write all performance measurements to the log |
| 97 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 98 | public void reportAll() { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 99 | String result = "Performance Results: (avg/start/stop/count)\n"; |
Brian O'Connor | 0d9963f | 2014-01-14 14:44:21 -0800 | [diff] [blame] | 100 | if(map.size() == 0) { |
| 101 | result += "No Measurements"; |
| 102 | log.error(result); |
| 103 | return; |
| 104 | } |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 105 | long experimentEnd = -1; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 106 | for(Entry<String, Queue<Measurement>> e : map.entrySet()) { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 107 | String key = e.getKey(); |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 108 | Queue<Measurement> list = e.getValue(); |
Brian O'Connor | c751883 | 2014-01-16 10:50:04 -0800 | [diff] [blame] | 109 | long total = 0, count = 0; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 110 | long start = Long.MAX_VALUE, stop = -1; |
| 111 | for(Measurement m : list) { |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 112 | if(m.stop < 0) { |
| 113 | continue; // measurement has not been stopped |
| 114 | } |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 115 | // Collect overall start and end times |
| 116 | if(m.start < start) { |
| 117 | start = m.start; |
| 118 | } |
| 119 | if(m.stop > stop) { |
| 120 | stop = m.stop; |
| 121 | if(stop > experimentEnd) { |
| 122 | experimentEnd = stop; |
| 123 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 124 | } |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 125 | // Collect statistics for average |
| 126 | total += m.elapsed(); |
| 127 | count++; |
| 128 | } |
| 129 | double avg = (double) total / count; |
| 130 | // Normalize start/stop |
| 131 | start -= experimentStart; |
| 132 | stop -= experimentStart; |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 133 | result += key + '=' + |
| 134 | (avg / normalization) + '/' + |
| 135 | (start / normalization) + '/' + |
| 136 | (stop / normalization) + '/' + |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 137 | count + '\n'; |
| 138 | } |
| 139 | double overheadMs = overhead / normalization; |
| 140 | double experimentElapsed = (experimentEnd - experimentStart) / normalization; |
| 141 | result += "TotalTime:" + experimentElapsed + "/Overhead:" + overheadMs; |
| 142 | log.error(result); |
| 143 | // log.error("Performance Results: {} with measurement overhead: {} ms", map, overheadMilli); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 144 | } |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Write the performance measurement for a tag to the log |
| 148 | * |
| 149 | * @param tag the tag name. |
| 150 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 151 | public void reportStep(String tag) { |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 152 | Queue<Measurement> list = map.get(tag); |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 153 | if(list == null) { |
| 154 | return; //TODO |
| 155 | } |
| 156 | //TODO: fix this; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 157 | Measurement m = list.peek(); |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 158 | if (m != null) { |
Brian O'Connor | 0d9963f | 2014-01-14 14:44:21 -0800 | [diff] [blame] | 159 | log.error("Performance Result: tag = {} start = {} stop = {} elapsed = {}", |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 160 | tag, m.start, m.stop, m.toString()); |
| 161 | } else { |
Brian O'Connor | 0d9963f | 2014-01-14 14:44:21 -0800 | [diff] [blame] | 162 | log.error("Performance Result: unknown tag {}", tag); |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 166 | /** |
| 167 | * A single performance measurement |
| 168 | */ |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 169 | public static class Measurement { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 170 | long start; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 171 | long stop = -1; |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 172 | |
| 173 | /** |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 174 | * Start the measurement |
| 175 | */ |
| 176 | public void start() { |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 177 | if(start <= 0) { |
| 178 | start = System.nanoTime(); |
| 179 | } |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 180 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 181 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 182 | /** |
| 183 | * Stop the measurement |
| 184 | */ |
| 185 | public void stop() { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 186 | long now = System.nanoTime(); |
| 187 | stop(now); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 188 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 189 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Stop the measurement at a specific time |
| 192 | * @param time to stop |
| 193 | */ |
| 194 | public void stop(long time){ |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 195 | if(stop <= 0) { |
| 196 | stop = time; |
| 197 | } |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 198 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 199 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Compute the elapsed time of the measurement in nanoseconds |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 202 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 203 | * @return the measurement time in nanoseconds, or -1 if the measurement is stil running. |
| 204 | */ |
| 205 | public long elapsed() { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 206 | if(stop <= 0) { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | else { |
| 210 | return stop - start; |
| 211 | } |
| 212 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 213 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Returns the number of milliseconds for the measurement as a String. |
| 216 | */ |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 217 | @Override |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 218 | public String toString() { |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 219 | double milli = elapsed() / normalization; |
| 220 | double startMs = start / normalization; |
| 221 | double stopMs = stop / normalization; |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 222 | |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 223 | return milli + "ms/" + startMs + '/' + stopMs; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 226 | |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 227 | @Deprecated |
| 228 | private static final PerformanceMonitor theInstance = new PerformanceMonitor(); |
| 229 | |
| 230 | @Deprecated |
| 231 | public static Measurement start(String tag) { |
| 232 | return theInstance.startStep(tag); |
| 233 | } |
| 234 | |
| 235 | @Deprecated |
| 236 | public static void stop(String tag) { |
| 237 | theInstance.stopStep(tag);; |
| 238 | } |
| 239 | |
| 240 | @Deprecated |
| 241 | public static void clear() { |
| 242 | theInstance.reset();; |
| 243 | } |
| 244 | |
| 245 | @Deprecated |
| 246 | public static void report() { |
| 247 | theInstance.reportAll();; |
| 248 | } |
| 249 | |
| 250 | @Deprecated |
| 251 | public static void report(String tag) { |
| 252 | theInstance.reportStep(tag); |
| 253 | } |
| 254 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 255 | public static void main(String args[]){ |
| 256 | // test the measurement overhead |
| 257 | String tag; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 258 | for(int i = 0; i < 2; i++){ |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 259 | tag = "foo foo foo"; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 260 | Measurement m; |
| 261 | m = start(tag); System.out.println(tag); m.stop(); |
| 262 | m = start(tag); System.out.println(tag); m.stop(); |
| 263 | m = start(tag); System.out.println(tag); m.stop(); |
| 264 | m = start(tag); System.out.println(tag); m.stop(); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 265 | tag = "bar"; |
| 266 | start(tag); stop(tag); |
| 267 | tag = "baz"; |
| 268 | start(tag); stop(tag); |
| 269 | report(); |
| 270 | clear(); |
| 271 | } |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 272 | for(int i = 0; i < 100; i++){ |
| 273 | tag = "a"; |
| 274 | start(tag); stop(tag); |
| 275 | start(tag); stop(tag); |
| 276 | |
| 277 | start(tag); stop(tag); |
| 278 | start(tag); stop(tag); |
| 279 | start(tag); stop(tag); |
| 280 | start(tag); stop(tag); |
| 281 | start(tag); stop(tag); |
| 282 | start(tag); stop(tag); |
| 283 | |
| 284 | tag = "b"; |
| 285 | start(tag); stop(tag); |
| 286 | tag = "c"; |
| 287 | start(tag); stop(tag); |
| 288 | report(); |
| 289 | clear(); |
| 290 | } |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 291 | } |
| 292 | } |