Newer
Older
package refit.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import org.HdrHistogram.ConcurrentHistogram;
import org.HdrHistogram.Histogram;
import org.HdrHistogram.WriterReaderPhaser;
import refit.config.REFITConfig;
public class REFITIntervalStatistics extends Thread {
private static final String WARMUP_HISTOGRAM_FN = REFITConfig.OUTPUT_DIRECTORY + "/" + REFITConfig.OUTPUT_ID + "-warmup.histogram";
private static final String HISTOGRAM_FN = REFITConfig.OUTPUT_DIRECTORY + "/" + REFITConfig.OUTPUT_ID + ".histogram";
private final int intervalInMs;
private final REFITStatisticsListener listener;
private volatile boolean running;
private volatile EventCounters event;
private final WriterReaderPhaser eventPhaser;
private final AtomicBoolean firstEvent;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
public long overallDurationInUs;
public int overallEventCounter;
public long overallEventValueAggregation;
private AtomicIntegerArray replyStats;
private final REFITDataLogger dataLogger;
private final Histogram warmupHistogram;
private final Histogram histogram;
public REFITIntervalStatistics(int intervalInMs, REFITStatisticsListener listener, File dataSink) {
setName("STATS-" + REFITConfig.OUTPUT_ID);
this.intervalInMs = intervalInMs;
this.listener = listener;
running = false;
event = null;
eventPhaser = new WriterReaderPhaser();
firstEvent = new AtomicBoolean(true);
this.replyStats = new AtomicIntegerArray(REFITConfig.TOTAL_NR_OF_REPLICAS);
this.dataLogger = (dataSink != null) ? new REFITDataLogger(dataSink) : null;
this.warmupHistogram = new ConcurrentHistogram(100 * 1000 * 1000, 2);
this.histogram = new ConcurrentHistogram(100 * 1000 * 1000, 2);
}
@Override
public void run() {
Thread logThread = null;
if (dataLogger != null) {
logThread = new Thread(dataLogger, "DATALOGGER-" + REFITConfig.OUTPUT_ID);
logThread.start();
}
long statisticsStartTime = REFITTime.nanoTime.getAsLong() / 1000;
int nextEpochCounter = 1;
eventPhaser.readerLock();
// initialize event object
event = new EventCounters(nextEpochCounter);
eventPhaser.flipPhase();
eventPhaser.readerUnlock();
running = true;
while (running) {
// compensate thread timing drift
int timePassed = (int) ((REFITTime.nanoTime.getAsLong() / 1000 - statisticsStartTime) / 1000);
int remainingSleepTime = nextEpochCounter * intervalInMs - timePassed;
// May raise an error if the time jumps
try {
REFITTime.sleep(remainingSleepTime);
} catch (final InterruptedException ignored) {
}
nextEpochCounter++;
eventPhaser.readerLock();
// replace event object
EventCounters oldEvent = event;
event = new EventCounters(nextEpochCounter);
// wait until all writers have finished
eventPhaser.flipPhase();
// extract data
int myResultIndex = oldEvent.epochCounter;
int myEventCount = oldEvent.counter.get();
long myEventValueAggregation = oldEvent.valueAggregation.get();
long myEventValueMin = oldEvent.valueMin.get();
long myEventValueMax = oldEvent.valueMax.get();
eventPhaser.readerUnlock();
// don't print incomplete last interval
if (!running) {
break;
}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
overallEventCounter += myEventCount;
overallEventValueAggregation += myEventValueAggregation;
float myEventValueAverage = oldEvent.valueAggregation.get() / (float) myEventCount;
listener.statisticsIntervalResult(myResultIndex, myEventCount, myEventValueAverage,
myEventValueMin, myEventValueMax);
}
long statisticsEndTime = REFITTime.nanoTime.getAsLong() / 1000;
overallDurationInUs = statisticsEndTime - statisticsStartTime;
if (logThread != null) {
dataLogger.shutdown();
try {
logThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void dumpHistogram(Histogram histogram, String fileName) {
try (PrintStream output = new PrintStream(fileName)) {
histogram.outputPercentileDistribution(output, 1.);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void printShortStatistic(Histogram histogram) {
double[] steps = {0., 0.25, 0.5, 0.75, 0.99, 1.};
for (double step : steps) {
long value = histogram.getValueAtPercentile(step * 100);
System.out.printf("Percentile %d%%: %f ms\n", (int) (step * 100), value / 1000.);
}
}
public void end() {
running = false;
try {
join();
} catch (InterruptedException ignored) {
}
// make sure that all threads have left the critical section of the event method
eventPhaser.readerLock();
eventPhaser.flipPhase();
eventPhaser.readerUnlock();
listener.statisticsOverallResult(lastEpochCounter,
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
overallEventCounter,
overallEventCounter / (float) (overallDurationInUs / (intervalInMs * 1000)),
overallEventValueAggregation / (float) overallEventCounter);
dumpHistogram(warmupHistogram, WARMUP_HISTOGRAM_FN);
dumpHistogram(histogram, HISTOGRAM_FN);
System.out.println("=== Warmup histogram ===");
printShortStatistic(warmupHistogram);
System.out.println("=== Histogram ===");
printShortStatistic(histogram);
}
public void event(final long value, final short id, String tag) {
final long currentTime = REFITTime.timeMicroUTC.getAsLong();
if (dataLogger != null && running) dataLogger.log(id + " " + currentTime + " " + value + " " + tag);
// Use phaser the ensure non-blocking statistic updates
long enterVal = eventPhaser.writerCriticalSectionEnter();
// Drop events while not running
// we must enter the critical section before checking `running` to make sure that `end` can wait until no-one
// is any longer in this section after `running` is reset to false
if (!running) {
eventPhaser.writerCriticalSectionExit(enterVal);
return;
}
EventCounters event = this.event;
boolean firstEvent = this.firstEvent.getAndSet(false);
int epochCounter = event.epochCounter;
event.counter.incrementAndGet();
event.valueAggregation.addAndGet(value);
event.valueMin.accumulateAndGet(value, Math::min);
event.valueMax.accumulateAndGet(value, Math::max);
Histogram histogram = (epochCounter > REFITConfig.CLIENT_WARM_UP_SECONDS) ? this.histogram : this.warmupHistogram;
histogram.recordValue(value);
eventPhaser.writerCriticalSectionExit(enterVal);
if (firstEvent) {
listener.statisticsStartedResult();
}
}
public void trackReply(short from) {
replyStats.incrementAndGet(from);
}
public void printReplyStats() {
for (int i = 0; i < replyStats.length(); i++) {
final short key = (short) i;
final int count = replyStats.getAndSet(i, 0);
System.out.println(String.format("Replica %3d: %7d\n", key, count));
}
}
private static class EventCounters {
public final AtomicInteger counter;
public final AtomicLong valueAggregation;
public final AtomicLong valueMin;
public final AtomicLong valueMax;
public final int epochCounter;
public EventCounters(int newEpochCount) {
counter = new AtomicInteger(0);
valueAggregation = new AtomicLong(0);
valueMin = new AtomicLong(Long.MAX_VALUE);
valueMax = new AtomicLong(Long.MIN_VALUE);
epochCounter = newEpochCount;
}
}
}