1 /*******************************************************************************
2 * Copyright (c) 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.trace
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
20 import static org
.junit
.Assume
.assumeTrue
;
23 import java
.net
.URISyntaxException
;
24 import java
.util
.LinkedList
;
25 import java
.util
.List
;
27 import org
.eclipse
.core
.runtime
.URIUtil
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.shared
.CtfTestTrace
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceWriter
;
34 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.Utils
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.Test
;
37 import org
.junit
.runner
.RunWith
;
38 import org
.junit
.runners
.Parameterized
;
39 import org
.junit
.runners
.Parameterized
.Parameters
;
42 * CTFTraceWriter test cases
44 * @author Bernd Hufmann
47 @SuppressWarnings("javadoc")
48 @RunWith(Parameterized
.class)
49 public class CTFTraceWriterTest
{
51 private static File fTempDir
;
54 private static final long CLOCK_OFFSET
= 1332166405241713987L;
55 private static final int TOTAL_NB_EVENTS
= 695319;
56 private static final long LAST_EVENT_TIME
= 1332170692664579801L;
59 private static final long STREAM0_FIRST_PACKET_TIME
= CLOCK_OFFSET
+ 4277170993912L;
60 private static final long STREAM0_FIRST_EVENT_TIME
= 1332170682440316151L;
61 private static final long STREAM0_LAST_EVENT_TIME
= 1332170682702066969L;
62 private static final int STREAM0_FIRST_PACKET_NB_EVENTS
= 14219;
65 private static final long STREAM1_FIRST_PACKET_TIME
= CLOCK_OFFSET
+ 4277171555436L;
66 private static final int STREAM1_FIRST_PACKET_NB_EVENTS
= 8213;
67 private static final long STREAM1_FIRST_EVENT_TIME
= 1332170682440133097L;
68 private static final long STREAM1_FIFTH_PACKET_TIME
= CLOCK_OFFSET
+ 4277970712221L;
69 private static final long STREAM1_TENTH_PACKET_TIME
= CLOCK_OFFSET
+ 4279440048309L;
70 private static final long STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME
= 1332170683212426208L;
71 private static final long STREAM1_TENTH_PACKET_LAST_EVENT_TIME
= 1332170685256508077L;
74 private static final int NB_EVENTS_SEVERAL_PACKETS
= 138894;
78 private long fStartTime
;
79 private long fEndTime
;
80 private int fNbEvents
;
81 private long fFirstEventTime
;
82 private long fLastEventTime
;
85 * Gets a list of test case parameters.
87 * @return The list of test parameters
89 @Parameters(name
= "{index}: {0}")
90 public static Iterable
<Object
[]> getTestParams() {
91 final List
<Object
[]> params
= new LinkedList
<>();
93 addParams(params
, "WHOLE_TRACE",
97 STREAM1_FIRST_EVENT_TIME
,
100 addParams(params
, "NO_EVENTS_USING_INVERTED_TIME",
101 Long
.MAX_VALUE
, Long
.MIN_VALUE
,
106 addParams(params
, "NO_EVENTS_USING_FIRST_PACKET",
107 STREAM0_FIRST_PACKET_TIME
+ 1,
108 STREAM0_FIRST_PACKET_TIME
+ 1,
113 addParams(params
, "STREAM0_FIRST_PACKET_TIME",
114 STREAM0_FIRST_PACKET_TIME
,
115 STREAM0_FIRST_PACKET_TIME
,
116 STREAM0_FIRST_PACKET_NB_EVENTS
,
117 STREAM0_FIRST_EVENT_TIME
,
118 STREAM0_LAST_EVENT_TIME
);
120 addParams(params
, "BOTH_STREAMS_FIRST_PACKET_ONLY",
121 STREAM0_FIRST_PACKET_TIME
,
122 STREAM1_FIRST_PACKET_TIME
,
123 STREAM0_FIRST_PACKET_NB_EVENTS
+ STREAM1_FIRST_PACKET_NB_EVENTS
,
124 STREAM1_FIRST_EVENT_TIME
,
125 STREAM0_LAST_EVENT_TIME
);
127 addParams(params
, "BOTH_STREAMS_SEVERAL_PACKETS",
128 STREAM1_FIFTH_PACKET_TIME
,
129 STREAM1_TENTH_PACKET_TIME
,
130 NB_EVENTS_SEVERAL_PACKETS
,
131 STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME
,
132 STREAM1_TENTH_PACKET_LAST_EVENT_TIME
);
137 private static void addParams(List
<Object
[]> params
, String name
, long startTime
, long endTime
, int nbEvents
, long firstEventTime
, long lastEventTime
) {
138 Object array
[] = new Object
[] { name
, startTime
, endTime
, nbEvents
, firstEventTime
, lastEventTime
};
143 public static void beforeClass() {
144 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
146 if (property
!= null) {
148 dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
149 dir
= new File(dir
.getAbsolutePath() + File
.separator
);
153 } catch (URISyntaxException e
) {
157 dir
= new File(System
.getProperty("java.io.tmpdir")); //$NON-NLS-1$)
159 String tempDir
= dir
.getAbsolutePath() + File
.separator
+ "testcases" + File
.separator
;
160 fTempDir
= new File(tempDir
);
161 if (!fTempDir
.exists()) {
166 public CTFTraceWriterTest (String name
, long startTime
, long endTime
, int nbEvents
, long firstEventTime
, long lastEventTime
) {
168 fStartTime
= startTime
;
170 fNbEvents
= nbEvents
;
171 fFirstEventTime
= firstEventTime
;
172 fLastEventTime
= lastEventTime
;
176 * Test various time ranges
179 public void testKernelTrace() {
180 assumeTrue(CtfTestTrace
.KERNEL
.exists());
182 CTFTrace trace
= CtfTestTrace
.KERNEL
.getTrace();
183 CTFTraceWriter ctfWriter
= new CTFTraceWriter(checkNotNull(trace
));
184 String traceName
= createTraceName(fName
);
185 ctfWriter
.copyPackets(fStartTime
, fEndTime
, traceName
);
187 File metadata
= new File(traceName
+ Utils
.SEPARATOR
+ "metadata");
188 assertTrue("metadata", metadata
.exists());
190 CTFTrace outTrace
= new CTFTrace(traceName
);
194 try (CTFTraceReader reader
= new CTFTraceReader(outTrace
)) {
195 while(reader
.hasMoreEvents()) {
197 EventDefinition def
= reader
.getCurrentEventDef();
198 end
= def
.getTimestamp();
200 start
= outTrace
.getClock().getClockOffset() + reader
.getStartTime();
204 end
= outTrace
.getClock().getClockOffset() + end
;
207 if (fFirstEventTime
>= 0) {
208 assertEquals("first event time", Long
.valueOf(fFirstEventTime
), start
);
210 if (fLastEventTime
>= 0) {
211 assertEquals("last event time", fLastEventTime
, end
);
213 assertEquals(toString(), fNbEvents
, count
);
215 if (fNbEvents
== 0) {
216 assertFalse("channel0", getChannelFile(traceName
, 0).exists());
217 assertFalse("channel1", getChannelFile(traceName
, 1).exists());
220 } catch (CTFException e
) {
225 private static File
getChannelFile(String path
, int id
) {
226 File channel
= new File(path
+ Utils
.SEPARATOR
+ "channel_" + String
.valueOf(id
));
230 private static String
createTraceName(String testCase
) {
231 return fTempDir
.getAbsolutePath() + File
.separator
+ testCase
.toString();