forked from nitsanw/HdrLogProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionHistogramLogs.java
More file actions
267 lines (236 loc) · 10 KB
/
Copy pathUnionHistogramLogs.java
File metadata and controls
267 lines (236 loc) · 10 KB
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.HdrHistogram.Histogram;
import org.HdrHistogram.HistogramLogReader;
import org.HdrHistogram.HistogramLogWriter;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
public class UnionHistogramLogs {
@Option(name = "-start", aliases = "-s", usage = "relative log start time in seconds, (default: 0.0)", required = false)
public double start = 0.0;
@Option(name = "-end", aliases = "-e", usage = "relative log end time in seconds, (default: MAX_DOUBLE)", required = false)
public double end = Double.MAX_VALUE;
@Option(name = "-verbose", aliases = "-v", usage = "verbose logging, (default: false)", required = false)
public boolean verbose = false;
@Option(name = "-relative", aliases = "-r", usage = "relative timeline merge, (default: false)", required = false)
public boolean relative = false;
@Option(name = "-inputPath", aliases = "-ip", usage = "set path to use for input files, defaults to current folder", required = false)
public void setInputPath(String inputFolderName) {
inputPath = new File(inputFolderName);
if (!inputPath.exists())
throw new IllegalArgumentException("inputPath:" + inputFolderName + " must exist!");
if (!inputPath.isDirectory())
throw new IllegalArgumentException("inputPath:" + inputFolderName + " must be a directory!");
}
@Option(name = "-inputFile", aliases = "-if", usage = "add an input hdr log from input path, also takes regexp", required = false)
public void addInputFile(String inputFile) {
final Predicate<String> predicate = Pattern.compile(inputFile).asPredicate();
inputFiles.addAll(Arrays.asList(inputPath.listFiles(pathname -> {
return predicate.test(pathname.getName());
})));
}
@Option(name = "-inputFileAbsolute", aliases = "-ifa", usage = "add an input hdr log file by absolute path, also takes regexp", required = false)
public void addInputFileAbsolute(String inputFile) {
inputFiles.add(new File(inputFile));
}
@Option(name = "-taggedInputFile", aliases = "-tif", usage = "a <tag>=<filename> add an input file, tag all histograms from this file with tag. If histograms have a tag it will be conactanated to file tag.", required = false)
public void addInputFileAbs(String inputFileNameAndTag) {
String[] args = inputFileNameAndTag.split("=");
if (args.length != 2)
throw new IllegalArgumentException("This value:" + inputFileNameAndTag + " should be a <tag>=<file>, neither tag nor filename allow the '=' char");
String tag = args[0];
String inputFileName = args[1];
File in = new File(inputFileName);
if (!in.exists())
throw new IllegalArgumentException("file:" + inputFileName + " must exist!");
inputFiles.add(in);
inputFilesTags.put(in, tag);
}
@Option(name = "-outputFile", aliases = "-of", usage = "set an output file destination, default goes to sysout", required = false)
public void setOutputFile(String outputFileName) {
outputFile = new File(outputFileName);
}
private File inputPath = new File(".");
private Set<File> inputFiles = new HashSet<>();
private Map<File, String> inputFilesTags = new HashMap<>();
private File outputFile;
public static void main(String[] args) throws Exception {
UnionHistogramLogs app = new UnionHistogramLogs();
CmdLineParser parser = new CmdLineParser(app);
try {
parser.parseArgument(args);
app.execute();
} catch (CmdLineException e) {
System.out.println(e.getMessage());
parser.printUsage(System.out);
}
}
private void execute() throws FileNotFoundException {
if (verbose) {
if (end != Double.MAX_VALUE)
System.out.printf("start:%.2f end:%.2f path:%s\n", start, end, inputPath.getAbsolutePath());
else
System.out.printf("start:%.2f end: MAX path:%s\n", start, inputPath.getAbsolutePath());
if (!inputFiles.isEmpty())
System.out.println("Reading files:");
else
System.out.println("No input files :(");
for (File inputFile : inputFiles) {
System.out.println(inputFile.getAbsolutePath());
}
}
if (inputFiles.isEmpty())
return;
PrintStream report = System.out;
if (outputFile != null) {
report = new PrintStream(new FileOutputStream(outputFile));
}
union(report);
}
class HistogramIterator implements Comparable<HistogramIterator> {
HistogramLogReader reader;
Histogram next;
String tag;
public HistogramIterator(HistogramLogReader reader) {
this.reader = reader;
read();
}
public HistogramIterator(HistogramLogReader reader, String tag) {
this.reader = reader;
this.tag = tag;
read();
}
private void read() {
do {
next = (Histogram) reader.nextIntervalHistogram(start, end);
} while (next == null && reader.hasNext());
if (next == null)
return;
// replace start time with a relative one
if (relative) {
long length = next.getEndTimeStamp() - next.getStartTimeStamp();
long nextStartTime = (long) (next.getStartTimeStamp() - reader.getStartTimeSec() * 1000);
next.setStartTimeStamp(nextStartTime);
next.setEndTimeStamp(next.getStartTimeStamp() + length);
}
if (tag != null) {
String nextTag = next.getTag();
if (nextTag == null) {
next.setTag(tag);
}
else {
next.setTag(tag+"::"+nextTag);
}
}
}
Histogram next() {
Histogram c = next;
read();
return c;
}
boolean hasNext() {
return next != null;
}
@Override
public int compareTo(HistogramIterator o) {
if (!hasNext() && !o.hasNext())
return 0;
if (!hasNext())
return -1;
if (!o.hasNext())
return 1;
return (int) (next.getStartTimeStamp() - o.next.getStartTimeStamp());
}
}
private void union(PrintStream out) throws FileNotFoundException {
List<HistogramIterator> ins = new ArrayList<>();
for (File inputFile : inputFiles) {
ins.add(new HistogramIterator(new HistogramLogReader(inputFile), inputFilesTags.get(inputFile)));
}
ins.removeIf(e -> !e.hasNext());
Collections.sort(ins);
if (ins.isEmpty()) {
if (verbose) {
System.out.println("Input files do not contain range");
}
return;
}
// Init writer
HistogramLogWriter writer = new HistogramLogWriter(out);
writer.outputLogFormatVersion();
writer.outputComment("Union of:" + inputFiles + " start:" + start + " end:" + end + " relative:" + relative);
if (!relative) {
long startTimeStamp = (long) (ins.get(0).reader.getStartTimeSec() * 1000);
writer.setBaseTime(startTimeStamp);
writer.outputBaseTime(startTimeStamp);
writer.outputStartTime(startTimeStamp);
}
writer.outputLegend();
Map<String, Histogram> unionedByTag = new HashMap<>();
// iterators are now sorted by start time
while (!ins.isEmpty()) {
HistogramIterator input = ins.get(0);
Histogram next = input.next();
Histogram union = unionedByTag.computeIfAbsent(next.getTag(), k -> {
Histogram h = new Histogram(next.getNumberOfSignificantValueDigits());
h.setTag(k);
return h;
});
long nextStart = next.getStartTimeStamp();
long nextEnd = next.getEndTimeStamp();
long unionStart = union.getStartTimeStamp();
long unionEnd = union.getEndTimeStamp();
// iterators are sorted, so we know nextStart >= unionStart
boolean rollover = false;
if (unionStart == Long.MAX_VALUE) {
union.add(next);
} else if (nextStart < unionEnd && nextEnd <= unionEnd) {
union.add(next);
}
else if (nextStart < unionEnd) {
double nLength = nextEnd - nextStart;
double overlap = (unionEnd - nextStart) / nLength;
if(overlap > 0.8) {
union.add(next);
// prevent an ever expanding union
union.setStartTimeStamp(unionStart);
union.setEndTimeStamp(unionEnd);
}
else {
rollover = true;
}
}
else {
rollover = true;
}
if (rollover) {
writer.outputIntervalHistogram(union);
union.reset();
union.setEndTimeStamp(0L);
union.setStartTimeStamp(Long.MAX_VALUE);
union.setTag(next.getTag());
union.add(next);
}
// trim and sort
ins.removeIf(e -> !e.hasNext());
Collections.sort(ins);
}
// write last hgrms
for (Histogram u : unionedByTag.values()) {
writer.outputIntervalHistogram(u);
}
}
}