| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013 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.linuxtools.tmf.core.tests.ctfadaptor; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertFalse; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | import static org.junit.Assume.assumeTrue; |
| 19 | |
| 20 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp; |
| 22 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
| 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfLostEvent; |
| 25 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 26 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 27 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
| 28 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 29 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 30 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 31 | import org.junit.After; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | |
| 35 | /** |
| 36 | * Tests to verify that lost events are handled correctly. |
| 37 | * |
| 38 | * Be wary if you are using Babeltrace to cross-check those values. There could |
| 39 | * be a bug in Babeltrace with regards to lost events. See |
| 40 | * http://bugs.lttng.org/issues/589 |
| 41 | * |
| 42 | * It's not 100% sure at this point which implementation is correct, so for now |
| 43 | * these tests assume the Java implementation is the right one. |
| 44 | * |
| 45 | * @author Alexandre Montplaisir |
| 46 | */ |
| 47 | public class CtfTmfLostEventsTest { |
| 48 | |
| 49 | private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.HELLO_LOST; |
| 50 | |
| 51 | private CtfTmfTrace fixture = null; |
| 52 | |
| 53 | /** |
| 54 | * Class setup |
| 55 | */ |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | assumeTrue(testTrace.exists()); |
| 59 | fixture = testTrace.getTrace(); |
| 60 | fixture.indexTrace(true); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Clean-up |
| 65 | */ |
| 66 | @After |
| 67 | public void tearDown() { |
| 68 | if (fixture != null) { |
| 69 | fixture.dispose(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // ------------------------------------------------------------------------ |
| 74 | // Test methods |
| 75 | // ------------------------------------------------------------------------ |
| 76 | |
| 77 | /** |
| 78 | * Test that the number of events is reported correctly (a range of lost |
| 79 | * events is counted as one event). |
| 80 | */ |
| 81 | @Test |
| 82 | public void testNbEvents() { |
| 83 | final long expectedReal = 32300; |
| 84 | final long expectedLost = 562; |
| 85 | |
| 86 | EventCountRequest req = new EventCountRequest(); |
| 87 | fixture.sendRequest(req); |
| 88 | try { |
| 89 | req.waitForCompletion(); |
| 90 | } catch (InterruptedException e) { |
| 91 | e.printStackTrace(); |
| 92 | } |
| 93 | |
| 94 | assertEquals(expectedReal, req.getReal()); |
| 95 | assertEquals(expectedLost, req.getLost()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test getting the first lost event from the trace. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testFirstLostEvent() { |
| 103 | final long rank = 153; |
| 104 | final ITmfTimestamp start = new CtfTmfTimestamp(1376592664828848222L); |
| 105 | final ITmfTimestamp end = new CtfTmfTimestamp(1376592664828848540L); |
| 106 | final long nbLost = 859; |
| 107 | |
| 108 | final CtfTmfEvent ev = getOneEventTime(start); |
| 109 | /* Make sure seeking by rank yields the same event */ |
| 110 | final CtfTmfEvent ev2 = getOneEventRank(rank); |
| 111 | assertEquals(ev, ev2); |
| 112 | |
| 113 | assertTrue(ev instanceof ITmfLostEvent); |
| 114 | ITmfLostEvent event = (ITmfLostEvent) ev; |
| 115 | |
| 116 | assertEquals(start, event.getTimestamp()); |
| 117 | assertEquals(start, event.getTimeRange().getStartTime()); |
| 118 | assertEquals(end, event.getTimeRange().getEndTime()); |
| 119 | assertEquals(nbLost, event.getNbLostEvents()); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Test getting the second lost event from the trace. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testSecondLostEvent() { |
| 127 | final long rank = 191; |
| 128 | final ITmfTimestamp start = new CtfTmfTimestamp(1376592664829402521L); |
| 129 | final ITmfTimestamp end = new CtfTmfTimestamp(1376592664829403076L); |
| 130 | final long nbLost = 488; |
| 131 | |
| 132 | final CtfTmfEvent ev = getOneEventTime(start); |
| 133 | /* Make sure seeking by rank yields the same event */ |
| 134 | final CtfTmfEvent ev2 = getOneEventRank(rank); |
| 135 | assertEquals(ev, ev2); |
| 136 | |
| 137 | assertTrue(ev instanceof ITmfLostEvent); |
| 138 | ITmfLostEvent event = (ITmfLostEvent) ev; |
| 139 | |
| 140 | assertEquals(start, event.getTimestamp()); |
| 141 | assertEquals(start, event.getTimeRange().getStartTime()); |
| 142 | assertEquals(end, event.getTimeRange().getEndTime()); |
| 143 | assertEquals(nbLost, event.getNbLostEvents()); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Test getting one normal event from the trace (lost events should not |
| 148 | * interfere). |
| 149 | */ |
| 150 | @Test |
| 151 | public void testNormalEvent() { |
| 152 | final long rank = 200; |
| 153 | final ITmfTimestamp ts = new CtfTmfTimestamp(1376592664829423928L); |
| 154 | |
| 155 | final CtfTmfEvent event = getOneEventTime(ts); |
| 156 | /* Make sure seeking by rank yields the same event */ |
| 157 | final CtfTmfEvent event2 = getOneEventRank(rank); |
| 158 | assertEquals(event, event2); |
| 159 | |
| 160 | assertFalse(event instanceof ITmfLostEvent); |
| 161 | assertEquals(ts, event.getTimestamp()); |
| 162 | } |
| 163 | |
| 164 | // ------------------------------------------------------------------------ |
| 165 | // Event requests |
| 166 | // ------------------------------------------------------------------------ |
| 167 | |
| 168 | private CtfTmfEvent getOneEventRank(long rank) { |
| 169 | OneEventRequestPerRank req = new OneEventRequestPerRank(rank); |
| 170 | fixture.sendRequest(req); |
| 171 | try { |
| 172 | req.waitForCompletion(); |
| 173 | } catch (InterruptedException e) { |
| 174 | e.printStackTrace(); |
| 175 | } |
| 176 | return req.getEvent(); |
| 177 | } |
| 178 | |
| 179 | private CtfTmfEvent getOneEventTime(ITmfTimestamp ts) { |
| 180 | OneEventRequestPerTs req = new OneEventRequestPerTs(ts); |
| 181 | fixture.sendRequest(req); |
| 182 | try { |
| 183 | req.waitForCompletion(); |
| 184 | } catch (InterruptedException e) { |
| 185 | e.printStackTrace(); |
| 186 | } |
| 187 | return req.getEvent(); |
| 188 | } |
| 189 | |
| 190 | private class OneEventRequestPerRank extends TmfEventRequest { |
| 191 | |
| 192 | private CtfTmfEvent event = null; |
| 193 | |
| 194 | public OneEventRequestPerRank(long rank) { |
| 195 | super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, rank, 1, ExecutionType.FOREGROUND); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void handleData(ITmfEvent ev) { |
| 200 | /* Type is checked by the request, cast should be safe */ |
| 201 | event = (CtfTmfEvent) ev; |
| 202 | } |
| 203 | |
| 204 | public CtfTmfEvent getEvent() { |
| 205 | return event; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private class OneEventRequestPerTs extends TmfEventRequest { |
| 210 | |
| 211 | private CtfTmfEvent event = null; |
| 212 | |
| 213 | public OneEventRequestPerTs(ITmfTimestamp ts) { |
| 214 | super(CtfTmfEvent.class, |
| 215 | new TmfTimeRange(ts, TmfTimestamp.PROJECT_IS_CANNED), |
| 216 | 0, 1, ExecutionType.FOREGROUND); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void handleData(ITmfEvent ev) { |
| 221 | event = (CtfTmfEvent) ev; |
| 222 | } |
| 223 | |
| 224 | public CtfTmfEvent getEvent() { |
| 225 | return event; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private class EventCountRequest extends TmfEventRequest { |
| 230 | |
| 231 | private long nbReal = 0; |
| 232 | private long nbLost = 0; |
| 233 | |
| 234 | public EventCountRequest() { |
| 235 | super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, 0, |
| 236 | ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void handleData(ITmfEvent event) { |
| 241 | if (event instanceof ITmfLostEvent) { |
| 242 | nbLost++; |
| 243 | } else { |
| 244 | nbReal++; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public long getReal() { |
| 249 | return nbReal; |
| 250 | } |
| 251 | |
| 252 | public long getLost() { |
| 253 | return nbLost; |
| 254 | } |
| 255 | } |
| 256 | } |