1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.event
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
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
;
38 * Tests to verify that lost events are handled correctly.
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
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.
47 * @author Alexandre Montplaisir
49 public class CtfTmfLostEventsTest
{
51 private static final @NonNull CtfTestTrace testTrace
= CtfTestTrace
.HELLO_LOST
;
53 private CtfTmfTrace fixture
= null;
60 fixture
= CtfTmfTestTraceUtils
.getTrace(testTrace
);
61 fixture
.indexTrace(true);
68 public void tearDown() {
69 if (fixture
!= null) {
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Test that the number of events is reported correctly (a range of lost
80 * events is counted as one event).
83 public void testNbEvents() {
84 final long expectedReal
= 32300;
85 final long expectedLost
= 562;
87 EventCountRequest req
= new EventCountRequest();
88 fixture
.sendRequest(req
);
90 req
.waitForCompletion();
91 } catch (InterruptedException e
) {
95 assertEquals(expectedReal
, req
.getReal());
96 assertEquals(expectedLost
, req
.getLost());
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.
105 public void testNbEventsBug475007() {
106 final CtfTestTrace tmfTestTrace
= CtfTestTrace
.DYNSCOPE
;
107 CtfTmfTrace trace
= CtfTmfTestTraceUtils
.getTrace(tmfTestTrace
);
108 trace
.indexTrace(true);
110 final long expectedReal
= 100003;
111 final long expectedLost
= 1;
113 EventCountRequest req
= new EventCountRequest();
114 trace
.sendRequest(req
);
116 req
.waitForCompletion();
117 } catch (InterruptedException e
) {
121 assertEquals(expectedReal
, req
.getReal());
122 assertEquals(expectedLost
, req
.getLost());
128 * Test getting the first lost event from the trace.
131 public void testFirstLostEvent() {
132 final long rank
= 152;
133 final long startTime
= 1376592664828848222L;
134 final ITmfTimestamp start
= new TmfNanoTimestamp(startTime
);
135 final ITmfTimestamp end
= new TmfNanoTimestamp(startTime
+ 554854L);
136 final long nbLost
= 859;
138 validateLostEvent(rank
, start
, end
, nbLost
);
142 * Test getting the second lost event from the trace.
145 public void testSecondLostEvent() {
146 final long rank
= 191;
147 final long startTime
= 1376592664829402521L;
148 final ITmfTimestamp start
= new TmfNanoTimestamp(startTime
);
149 final ITmfTimestamp end
= new TmfNanoTimestamp(startTime
+ 421993L);
150 final long nbLost
= 488;
152 validateLostEvent(rank
, start
, end
, nbLost
);
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
);
161 assertTrue(ev
instanceof ITmfLostEvent
);
162 ITmfLostEvent event
= (ITmfLostEvent
) ev
;
164 assertEquals(start
, event
.getTimestamp());
165 assertEquals(start
, event
.getTimeRange().getStartTime());
166 assertEquals(end
, event
.getTimeRange().getEndTime());
167 assertEquals(nbLost
, event
.getNbLostEvents());
171 * Test getting one normal event from the trace (lost events should not
175 public void testNormalEvent() {
176 final long rank
= 193;
177 final ITmfTimestamp ts
= new TmfNanoTimestamp(1376592664829411423L);
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
);
184 assertFalse(event
instanceof ITmfLostEvent
);
185 assertEquals(ts
, event
.getTimestamp());
189 * Test getting a lost event from a trace that has a timestamp transform.
192 public void testLostEventWithTransform() {
193 CtfTmfTrace trace
= CtfTmfTestTraceUtils
.getTrace(testTrace
);
194 long offset
= 1234567890L;
195 trace
.setTimestampTransform(TimestampTransformFactory
.createWithOffset(offset
));
196 trace
.indexTrace(true);
198 final long rank
= 152;
199 final ITmfTimestamp start
= new TmfNanoTimestamp(1376592664828848222L + offset
);
200 final ITmfTimestamp end
= new TmfNanoTimestamp(1376592664828848222L + 554854L + offset
);
201 final long nbLost
= 859;
203 ITmfContext context
= trace
.seekEvent(rank
);
204 final CtfTmfEvent ev
= trace
.getNext(context
);
207 assertTrue(ev
instanceof ITmfLostEvent
);
208 ITmfLostEvent event
= (ITmfLostEvent
) ev
;
210 assertEquals(start
, event
.getTimestamp());
211 assertEquals(start
, event
.getTimeRange().getStartTime());
212 assertEquals(end
, event
.getTimeRange().getEndTime());
213 assertEquals(nbLost
, event
.getNbLostEvents());
215 trace
.setTimestampTransform(null);
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
223 private CtfTmfEvent
getOneEventRank(long rank
) {
224 OneEventRequestPerRank req
= new OneEventRequestPerRank(rank
);
225 fixture
.sendRequest(req
);
227 req
.waitForCompletion();
228 } catch (InterruptedException e
) {
231 return req
.getEvent();
234 private CtfTmfEvent
getOneEventTime(@NonNull ITmfTimestamp ts
) {
235 OneEventRequestPerTs req
= new OneEventRequestPerTs(ts
);
236 fixture
.sendRequest(req
);
238 req
.waitForCompletion();
239 } catch (InterruptedException e
) {
242 return req
.getEvent();
245 private class OneEventRequestPerRank
extends TmfEventRequest
{
247 private CtfTmfEvent event
= null;
249 public OneEventRequestPerRank(long rank
) {
250 super(CtfTmfEvent
.class, TmfTimeRange
.ETERNITY
, rank
, 1, ExecutionType
.FOREGROUND
);
254 public void handleData(ITmfEvent ev
) {
255 /* Type is checked by the request, cast should be safe */
256 event
= (CtfTmfEvent
) ev
;
259 public CtfTmfEvent
getEvent() {
264 private class OneEventRequestPerTs
extends TmfEventRequest
{
266 private CtfTmfEvent event
= null;
268 public OneEventRequestPerTs(@NonNull ITmfTimestamp ts
) {
269 super(CtfTmfEvent
.class,
270 new TmfTimeRange(ts
, ts
),
271 0, 1, ExecutionType
.FOREGROUND
);
275 public void handleData(ITmfEvent ev
) {
276 event
= (CtfTmfEvent
) ev
;
279 public CtfTmfEvent
getEvent() {
284 private class EventCountRequest
extends TmfEventRequest
{
286 private long nbReal
= 0;
287 private long nbLost
= 0;
289 public EventCountRequest() {
290 super(CtfTmfEvent
.class, TmfTimeRange
.ETERNITY
, 0,
291 ITmfEventRequest
.ALL_DATA
, ExecutionType
.FOREGROUND
);
295 public void handleData(ITmfEvent event
) {
296 if (event
instanceof ITmfLostEvent
) {
303 public long getReal() {
307 public long getLost() {