81c1d6769575b128955ee4b800d6db6b5a8aaf5e
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / CtfTmfLostEventsTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ctf.core.tests.event;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfLostEvent;
23 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
24 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
25 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
26 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
27 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
28 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
29 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
30 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
31 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
32 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 /**
38 * Tests to verify that lost events are handled correctly.
39 *
40 * Be wary if you are using Babeltrace to cross-check those values. There could
41 * be a bug in Babeltrace with regards to lost events. See
42 * http://bugs.lttng.org/issues/589
43 *
44 * It's not 100% sure at this point which implementation is correct, so for now
45 * these tests assume the Java implementation is the right one.
46 *
47 * @author Alexandre Montplaisir
48 */
49 public class CtfTmfLostEventsTest {
50
51 private static final @NonNull CtfTestTrace testTrace = CtfTestTrace.HELLO_LOST;
52
53 private CtfTmfTrace fixture = null;
54
55 /**
56 * Class setup
57 */
58 @Before
59 public void setUp() {
60 fixture = CtfTmfTestTraceUtils.getTrace(testTrace);
61 fixture.indexTrace(true);
62 }
63
64 /**
65 * Clean-up
66 */
67 @After
68 public void tearDown() {
69 if (fixture != null) {
70 fixture.dispose();
71 }
72 }
73
74 // ------------------------------------------------------------------------
75 // Test methods
76 // ------------------------------------------------------------------------
77
78 /**
79 * Test that the number of events is reported correctly (a range of lost
80 * events is counted as one event).
81 */
82 @Test
83 public void testNbEvents() {
84 final long expectedReal = 32300;
85 final long expectedLost = 562;
86
87 EventCountRequest req = new EventCountRequest();
88 fixture.sendRequest(req);
89 try {
90 req.waitForCompletion();
91 } catch (InterruptedException e) {
92 e.printStackTrace();
93 }
94
95 assertEquals(expectedReal, req.getReal());
96 assertEquals(expectedLost, req.getLost());
97 }
98
99 /**
100 * Test that the number of events is reported correctly (a range of lost
101 * events is counted as one event). Events could be wrongly counted as lost
102 * events in certain situations.
103 */
104 @Test
105 public void testNbEventsBug475007() {
106 final CtfTestTrace tmfTestTrace = CtfTestTrace.DYNSCOPE;
107 CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(tmfTestTrace);
108 trace.indexTrace(true);
109
110 final long expectedReal = 100003;
111 final long expectedLost = 1;
112
113 EventCountRequest req = new EventCountRequest();
114 trace.sendRequest(req);
115 try {
116 req.waitForCompletion();
117 } catch (InterruptedException e) {
118 e.printStackTrace();
119 }
120
121 assertEquals(expectedReal, req.getReal());
122 assertEquals(expectedLost, req.getLost());
123
124 trace.dispose();
125 }
126
127 /**
128 * Test getting the first lost event from the trace.
129 */
130 @Test
131 public void testFirstLostEvent() {
132 final long rank = 190;
133 final long startTime = 1376592664828900165L;
134 final ITmfTimestamp start = new TmfNanoTimestamp(startTime);
135 final ITmfTimestamp end = new TmfNanoTimestamp(startTime + 502911L);
136 final long nbLost = 859;
137
138 validateLostEvent(rank, start, end, nbLost);
139 }
140
141 /**
142 * Test getting the second lost event from the trace.
143 */
144 @Test
145 public void testSecondLostEvent() {
146 final long rank = 229;
147 final long startTime = 1376592664829477058L;
148 final ITmfTimestamp start = new TmfNanoTimestamp(startTime);
149 final ITmfTimestamp end = new TmfNanoTimestamp(startTime + 347456L);
150 final long nbLost = 488;
151
152 validateLostEvent(rank, start, end, nbLost);
153 }
154
155 private void validateLostEvent(final long rank, final @NonNull ITmfTimestamp start, final ITmfTimestamp end, final long nbLost) {
156 final CtfTmfEvent ev = getOneEventTime(start);
157 /* Make sure seeking by rank yields the same event */
158 final CtfTmfEvent ev2 = getOneEventRank(rank);
159 assertEquals(ev, ev2);
160
161 assertTrue(ev instanceof ITmfLostEvent);
162 ITmfLostEvent event = (ITmfLostEvent) ev;
163
164 assertEquals(start, event.getTimestamp());
165 assertEquals(start, event.getTimeRange().getStartTime());
166 assertEquals(end, event.getTimeRange().getEndTime());
167 assertEquals(nbLost, event.getNbLostEvents());
168 }
169
170 /**
171 * Test getting one normal event from the trace (lost events should not
172 * interfere).
173 */
174 @Test
175 public void testNormalEvent() {
176 final long rank = 200;
177 final ITmfTimestamp ts = new TmfNanoTimestamp(1376592664829425780L);
178
179 final CtfTmfEvent event = getOneEventTime(ts);
180 /* Make sure seeking by rank yields the same event */
181 final CtfTmfEvent event2 = getOneEventRank(rank);
182 assertEquals(event, event2);
183
184 assertFalse(event instanceof ITmfLostEvent);
185 assertEquals(ts, event.getTimestamp());
186 }
187
188 /**
189 * Test getting a lost event from a trace that has a timestamp transform.
190 */
191 @Test
192 public void testLostEventWithTransform() {
193 CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(testTrace);
194 long offset = 1234567890L;
195 trace.setTimestampTransform(TimestampTransformFactory.createWithOffset(offset));
196 trace.indexTrace(true);
197
198 final long rank = 190;
199 final ITmfTimestamp start = new TmfNanoTimestamp(1376592664828900165L + offset);
200 final ITmfTimestamp end = new TmfNanoTimestamp(1376592664828900165L + 502911L + offset);
201 final long nbLost = 859;
202
203 ITmfContext context = trace.seekEvent(rank);
204 final CtfTmfEvent ev = trace.getNext(context);
205 context.dispose();
206
207 assertTrue(ev instanceof ITmfLostEvent);
208 ITmfLostEvent event = (ITmfLostEvent) ev;
209
210 assertEquals(start, event.getTimestamp());
211 assertEquals(start, event.getTimeRange().getStartTime());
212 assertEquals(end, event.getTimeRange().getEndTime());
213 assertEquals(nbLost, event.getNbLostEvents());
214
215 trace.setTimestampTransform(null);
216 trace.dispose();
217 }
218
219 // ------------------------------------------------------------------------
220 // Event requests
221 // ------------------------------------------------------------------------
222
223 private CtfTmfEvent getOneEventRank(long rank) {
224 OneEventRequestPerRank req = new OneEventRequestPerRank(rank);
225 fixture.sendRequest(req);
226 try {
227 req.waitForCompletion();
228 } catch (InterruptedException e) {
229 e.printStackTrace();
230 }
231 return req.getEvent();
232 }
233
234 private CtfTmfEvent getOneEventTime(@NonNull ITmfTimestamp ts) {
235 OneEventRequestPerTs req = new OneEventRequestPerTs(ts);
236 fixture.sendRequest(req);
237 try {
238 req.waitForCompletion();
239 } catch (InterruptedException e) {
240 e.printStackTrace();
241 }
242 return req.getEvent();
243 }
244
245 private class OneEventRequestPerRank extends TmfEventRequest {
246
247 private CtfTmfEvent event = null;
248
249 public OneEventRequestPerRank(long rank) {
250 super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, rank, 1, ExecutionType.FOREGROUND);
251 }
252
253 @Override
254 public void handleData(ITmfEvent ev) {
255 /* Type is checked by the request, cast should be safe */
256 event = (CtfTmfEvent) ev;
257 }
258
259 public CtfTmfEvent getEvent() {
260 return event;
261 }
262 }
263
264 private class OneEventRequestPerTs extends TmfEventRequest {
265
266 private CtfTmfEvent event = null;
267
268 public OneEventRequestPerTs(@NonNull ITmfTimestamp ts) {
269 super(CtfTmfEvent.class,
270 new TmfTimeRange(ts, ts),
271 0, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND);
272 }
273
274 @Override
275 public void handleData(ITmfEvent ev) {
276 event = (CtfTmfEvent) ev;
277 }
278
279 public CtfTmfEvent getEvent() {
280 return event;
281 }
282 }
283
284 private class EventCountRequest extends TmfEventRequest {
285
286 private long nbReal = 0;
287 private long nbLost = 0;
288
289 public EventCountRequest() {
290 super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, 0,
291 ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND);
292 }
293
294 @Override
295 public void handleData(ITmfEvent event) {
296 if (event instanceof ITmfLostEvent) {
297 nbLost++;
298 } else {
299 nbReal++;
300 }
301 }
302
303 public long getReal() {
304 return nbReal;
305 }
306
307 public long getLost() {
308 return nbLost;
309 }
310 }
311 }
This page took 0.050883 seconds and 4 git commands to generate.