-
Notifications
You must be signed in to change notification settings - Fork 13
/
BinaryFTraceIteratorTest.java
379 lines (335 loc) · 14.6 KB
/
BinaryFTraceIteratorTest.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*******************************************************************************
* Copyright (c) 2022 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.ftrace.core.tests.binary.iterator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.eclipse.tracecompass.incubator.ftrace.core.tests.shared.FTraceUtils;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.binary.context.BinaryFTraceLocation;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.binary.context.BinaryFTraceLocationInfo;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.binary.header.BinaryFTraceHeaderInfo;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.binary.iterator.BinaryFTraceIterator;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.binary.parser.BinaryFTraceFileParser;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.event.GenericFtraceEvent;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.trace.BinaryFTrace;
import org.eclipse.tracecompass.testtraces.ftrace.FtraceTestTrace;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Tests for {@link BinaryFTraceIterator}.
*
* @author Hoang Thuan Pham
*/
public class BinaryFTraceIteratorTest {
// Trace with 4 CPUs
private static BinaryFTraceHeaderInfo multipleEventTrace;
// An empty trace that is recorded on a machine that has 4 CPUs
private static BinaryFTraceHeaderInfo emptyTrace;
private static BinaryFTrace ftrace;
/**
* Initialize variables used in the test
*
* @throws IOException
* if an error occured while getting the test trace paths
* @throws TmfTraceException
* if an error occured while parsing the test traces
*/
@BeforeClass
public static void initTest() throws TmfTraceException, IOException {
ftrace = new BinaryFTrace();
multipleEventTrace = BinaryFTraceFileParser.parse(FTraceUtils.getTraceAbsolutePath(FtraceTestTrace.TEST_2_6_MULTIPLE_CPUS));
emptyTrace = BinaryFTraceFileParser.parse(FTraceUtils.getTraceAbsolutePath(FtraceTestTrace.TEST_2_6_EMPTY));
}
/**
* Clean up after tests are finished
*/
@AfterClass
public static void onFinishTests() {
ftrace.dispose();
}
/**
* Test initialize an iterator without a location. The iterator should
* position itself at the first location in the trace.
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void constructorTestWithNoLocation() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
BinaryFTraceLocation location = iterator.getLocation();
// Make sure that the iterator positions itself at the correct
// location (Min timestamp value)
assertNotNull(location);
assertEquals(location.getLocationInfo().getTimestamp(), TmfTimestamp.BIG_BANG.toNanos());
assertEquals(iterator.getRank(), 0);
}
}
/**
* Test the has more events method
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testHasMoreEvents() throws IOException {
// This trace has no events, so has more events returns false
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(emptyTrace, ftrace)) {
assertFalse(iterator.hasMoreEvents());
}
// This trace has multiple events, so has more events returns true
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
assertTrue(iterator.hasMoreEvents());
}
}
/**
* Test getting the current event of the iterator
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testGetCurrentEvent() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
GenericFtraceEvent event = iterator.getCurrentEvent();
assertEquals(event.getName(), BinaryFTraceIteratorTestData.FIRST_TRACE_EVENT_NAME);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.FIRST_TRACE_EVENT_TS);
}
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(emptyTrace, ftrace)) {
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNull(event);
}
}
/**
* Seek an event at the beginning of the trace
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testSeekFirstEvent() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.FIRST_TRACE_EVENT_TS);
assertEquals(event.getName(), BinaryFTraceIteratorTestData.FIRST_TRACE_EVENT_NAME);
}
}
/**
* Seek an event in the middle of the trace with an imprecise timestamp
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testSeekEventWithImpreciseTimestamp() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.MID_TRACE_EVENT_IMPRECISE_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
boolean ret = iterator.seek(locationInfo);
assertTrue(ret);
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS);
assertEquals(event.getName(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_NAME);
}
}
/**
* Seek an event in the middle of the trace with a precise timestamp
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testSeekEventWithPreciseTimestamp() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
boolean ret = iterator.seek(locationInfo);
assertTrue(ret);
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS);
assertEquals(event.getName(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_NAME);
}
}
/**
* Seek the last event of the trace
*
* @throws IOException
* If an error occured while reading the trace
*/
@Test
public void testSeekLastEvent() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.LAST_TRACE_EVENT_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
boolean ret = iterator.seek(locationInfo);
assertTrue(ret);
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.LAST_TRACE_EVENT_TS);
}
}
/**
* Test getting the next event
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testAdvance() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
// Advance to the second event of the trace
boolean ret = iterator.advance();
assertTrue(ret);
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.SECOND_TRACE_EVENT_TS);
assertEquals(event.getName(), BinaryFTraceIteratorTestData.SECOND_TRACE_EVENT_NAME);
// Position the iterator at the last event and try to seek the next
// one
iterator.seek(BinaryFTraceIteratorTestData.LAST_TRACE_EVENT_TS);
ret = iterator.advance();
assertFalse(ret);
}
}
/**
* Test getting the current timestamp
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testGetCurrentTimestamp() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.MID_TRACE_EVENT_IMPRECISE_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
boolean ret = iterator.seek(locationInfo);
assertTrue(ret);
assertEquals(iterator.getCurrentTimestamp(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS);
}
}
/**
* Test the setLocation() method. The setLocation() method should position
* the trace at the correct position.
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testSetLocation() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
// The setLocation method should position the trace at the correct
// position
iterator.setLocation(new BinaryFTraceLocation(locationInfo));
GenericFtraceEvent event = iterator.getCurrentEvent();
assertNotNull(event);
assertEquals(event.getTimestamp().toNanos(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS);
assertEquals(event.getName(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_NAME);
}
}
/**
* Test increaseRank() function, but also getRank()
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testIncreaseRank() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
assertEquals(iterator.getRank(), 0);
iterator.increaseRank();
assertEquals(iterator.getRank(), 1);
}
}
/**
* Test for setting rank
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testSetRank() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
iterator.setRank(12);
assertEquals(iterator.getRank(), 12);
}
}
/**
* Test for checking if the rank is valid. Test for setRank and
* hasValidRank.
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testHasValidRank() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
// Intially the rank should be 0
assertTrue(iterator.hasValidRank());
iterator.setRank(-1);
assertFalse(iterator.hasValidRank());
}
}
/**
* Seek an event in the middle of the trace with a precise timestamp
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testGetLocation() throws IOException {
BinaryFTraceLocationInfo locationInfo = new BinaryFTraceLocationInfo(BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS, 0);
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
iterator.seek(locationInfo);
assertEquals(iterator.getLocation().getLocationInfo().getTimestamp(), BinaryFTraceIteratorTestData.MID_TRACE_EVENT_TS);
}
}
/**
* Test the compareTo() method. The comparison is made by rank.
*
* @throws IOException
* if an error occurred while iterating through trace events
*/
@Test
public void testCompareTo() throws IOException {
assertNotNull(ftrace);
try (BinaryFTraceIterator iterator1 = new BinaryFTraceIterator(multipleEventTrace, ftrace);
BinaryFTraceIterator iterator2 = new BinaryFTraceIterator(multipleEventTrace, ftrace)) {
// Initially 2 iterators have the same rank of 0, so they are equal
assertEquals(iterator1.compareTo(iterator2), 0);
// Iterator 1 is larger
iterator1.increaseRank();
assertTrue(iterator1.compareTo(iterator2) > 0);
assertTrue(iterator2.compareTo(iterator1) < 0);
}
}
}