-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelemetryFileInputStream.java
320 lines (254 loc) · 11.8 KB
/
TelemetryFileInputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
107
108
109
110
111
112
113
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
164
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// A FileInputStream that logs throughput (KB/s) of reads.
//
// (Nothing really file-specific about most of it, actually--could easily be
// adapted for an InputStream instead)
//
// Makes reasonable attempts to be robust and low-overhead. Here's the plan:
//
// * Each TelemetryFileInputStream instance logs the size of each read to a
// shared Telemetry instance. Right now that means taking a mutex, but it
// isn't held for long. The observation is written to a circular buffer and
// we cross our fingers that we don't run out of space.
//
// * An aggregation thread wakes up periodically (1 second by default), grabs
// the mutex and tallies up the observations seen in the last second. The
// aggregated "timestep" total is written to a second array, and the
// circular buffer is cleared.
//
// * When we have accumulated enough timestep values to cover a report period,
// we publish a copy of the array (via AtomicReference). The goal here is
// to decouple the (critical path) aggregation process from IO.
//
// * A third reporting thread wakes up periodically and checks to see whether a
// new report has been published. If so, it logs it.
//
// If the circular buffer fills up, it just wraps around but shouldn't cause
// problems. A warning is logged if that happens and the affected stats are
// skipped.
//
// If logging stalls for some reason, it shouldn't matter: the aggregation
// thread will just publish reports that nobody ever reads. Some people make
// good careers doing that.
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
public class TelemetryFileInputStream extends FileInputStream {
// The maximum number of observations you're expecting to receive during
// `timestepPeriod`. If you exceed this we'll wrap around and lose
// data, but that case is detected and the report is discarded.
//
private static final int MAX_OBSERVATION_COUNT = 1000000;
// How often we'll log some stats
private static final int REPORT_PERIOD = 60000;
// How often we'll roll up the observations seen so far. Bigger number
// means lower overhead but longer pauses.
private static final int TIMESTEP_PERIOD = 1000;
private static Telemetry t = new Telemetry(TIMESTEP_PERIOD, REPORT_PERIOD, MAX_OBSERVATION_COUNT);
public TelemetryFileInputStream(String s) throws FileNotFoundException {
super(s);
}
public TelemetryFileInputStream(File f) throws FileNotFoundException {
super(f);
}
public TelemetryFileInputStream(FileDescriptor f) throws FileNotFoundException {
super(f);
}
// NOTE: We implicitly assume here that read(byte[]) doesn't call
// read(byte[], int, int) to do its work. That's currently true, but if
// it changed we would end up double-counting!
//
public int read(byte[] b) throws IOException {
int result;
t.readPending();
try {
result = super.read(b);
t.addObservation(result);
} finally {
t.readComplete();
}
return result;
}
public int read(byte[] b, int off, int len) throws IOException {
int result;
t.readPending();
try {
result = super.read(b, off, len);
t.addObservation(result);
} finally {
t.readComplete();
}
return result;
}
// The magic...
private static class Telemetry {
private final int NO_REPORT_YET = -1;
private final Object mutex = new Object();
private int lastWritePos = -1;
private boolean overflowed = false;
private int maxObservationCount;
private int reportPeriod;
private int timestepPeriod;
private int timestepsPerReport;
private long[] observations;
private AtomicLong lastReportTime = new AtomicLong(NO_REPORT_YET);
private AtomicLong readsPending = new AtomicLong(0);
private AtomicReference<TelemetryReport> lastReport = new AtomicReference<>();
private Semaphore reportReadySemaphore = new Semaphore(0);
public Telemetry (int timestepPeriod, int reportPeriod, int maxObservationCount) {
this.timestepPeriod = timestepPeriod;
this.reportPeriod = reportPeriod;
this.maxObservationCount = maxObservationCount;
this.observations = new long[maxObservationCount];
timestepsPerReport = (reportPeriod / timestepPeriod);
Thread aggregation = new Thread(() -> { runAggregationLoop(); });
aggregation.setDaemon(true);
aggregation.start();
Thread report = new Thread(() -> { runReportLoop(); });
report.setDaemon(true);
report.start();
}
public void readPending() {
readsPending.incrementAndGet();
}
public void readComplete() {
readsPending.decrementAndGet();
}
// Log a single observation and get out of the way as quickly as we can.
public void addObservation(int observation) {
synchronized(mutex) {
lastWritePos++;
if (lastWritePos >= MAX_OBSERVATION_COUNT) {
overflowed = true;
lastWritePos = 0;
}
observations[lastWritePos] = observation;
}
}
// Dart in and roll up the observations we've seen in the last timestep.
// Periodically publishes a report to be logged. On the critical path,
// since it holds up observations being written while running.
private void runAggregationLoop() {
int currentTimestep = -1;
TelemetryReport report = new TelemetryReport(timestepsPerReport);
while (true) {
currentTimestep++;
try {
Thread.sleep(timestepPeriod);
} catch (InterruptedException e) {
break;
}
synchronized (mutex) {
if (overflowed) {
System.err.println("WARNING: overflowed circular buffer. Skipping this set of observations");
overflowed = false;
report.observationSums[currentTimestep] = -1;
report.readsPendingCounts[currentTimestep] = -1;
} else {
report.readsPendingCounts[currentTimestep] = readsPending.get();
report.readsCompletedCount += lastWritePos + 1;
for (int i = 0; i <= lastWritePos; i++) {
report.observationSums[currentTimestep] += observations[i];
}
// The next timestep will write at 0
lastWritePos = -1;
}
// Publish our report for the last `reportPeriod` if we've
// got a full set.
if (currentTimestep + 1 == timestepsPerReport) {
lastReport.set(report);
lastReportTime.set(System.currentTimeMillis());
reportReadySemaphore.release();
currentTimestep = -1;
report = new TelemetryReport(timestepsPerReport);
}
}
}
}
// Log published reports as they show up.
private void runReportLoop() {
long lastSeenReportTime = NO_REPORT_YET;
while (true) {
try {
reportReadySemaphore.acquire();
} catch (InterruptedException e) {
break;
}
if (lastSeenReportTime != lastReportTime.get()) {
TelemetryReport report = lastReport.get();
long now = lastReportTime.get();
long minimumTransfer = Long.MAX_VALUE;
long maximumTransfer = Long.MIN_VALUE;
long totalTransfer = 0;
long minimumPending = Long.MAX_VALUE;
long maximumPending = Long.MIN_VALUE;
long totalPending = 0;
int validTimestepCount = 0;
for (int i = 0; i < timestepsPerReport; i++) {
if (report.observationSums[i] < 0) {
continue;
}
validTimestepCount += 1;
if (report.observationSums[i] < minimumTransfer) {
minimumTransfer = report.observationSums[i];
}
if (report.observationSums[i] > maximumTransfer) {
maximumTransfer = report.observationSums[i];
}
totalTransfer += report.observationSums[i];
if (report.readsPendingCounts[i] < minimumPending) {
minimumPending = report.readsPendingCounts[i];
}
if (report.readsPendingCounts[i] > maximumPending) {
maximumPending = report.readsPendingCounts[i];
}
totalPending += report.readsPendingCounts[i];
}
StringBuilder sb = new StringBuilder();
if (minimumTransfer != Long.MAX_VALUE) {
sb.append(String.format("minimum xfr=%.2f KB/s", (minimumTransfer / (timestepPeriod / 1000.0) / 1024.0)));
}
if (maximumTransfer != Long.MIN_VALUE) {
if (sb.length() > 0) { sb.append("; "); }
sb.append(String.format("maximum xfr=%.2f KB/s", (maximumTransfer / (timestepPeriod / 1000.0) / 1024.0)));
}
if (validTimestepCount > 0) {
if (sb.length() > 0) { sb.append("; "); }
sb.append(String.format("average xfr=%.2f KB/s", (totalTransfer / validTimestepCount / (timestepPeriod / 1000.0) / 1024.0)));
}
if (minimumPending != Long.MAX_VALUE) {
if (sb.length() > 0) { sb.append("; "); }
sb.append(String.format("minimum pending=%d", minimumPending));
}
if (maximumPending != Long.MIN_VALUE) {
if (sb.length() > 0) { sb.append("; "); }
sb.append(String.format("maximum pending=%d", maximumPending));
}
if (validTimestepCount > 0) {
if (sb.length() > 0) { sb.append("; "); }
sb.append(String.format("average pending=%.2f", ((float)totalPending / validTimestepCount)));
}
if (sb.length() > 0) {
sb.append("; reads_completed=" + report.readsCompletedCount);
System.err.println(now + " " + sb.toString());
}
lastSeenReportTime = now;
}
}
}
private static class TelemetryReport {
public long[] observationSums;
public long[] readsPendingCounts;
public long readsCompletedCount;
public TelemetryReport(int timestepsPerReport) {
observationSums = new long[timestepsPerReport];
readsPendingCounts = new long[timestepsPerReport];
}
}
}
}