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
;
22 import java
.net
.URISyntaxException
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.URIUtil
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.shared
.CtfTestTraceUtils
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceWriter
;
33 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.Utils
;
34 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
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
= 1332170682702069762L;
71 private static final long STREAM1_TENTH_PACKET_LAST_EVENT_TIME
= 1332170685256508077L;
74 private static final int NB_EVENTS_SEVERAL_PACKETS
= 167585;
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
, "STREAM0_FIRST_PACKET_TIME",
107 STREAM0_FIRST_PACKET_TIME
,
108 STREAM0_FIRST_PACKET_TIME
,
109 STREAM0_FIRST_PACKET_NB_EVENTS
,
110 STREAM0_FIRST_EVENT_TIME
,
111 STREAM0_LAST_EVENT_TIME
);
113 addParams(params
, "BOTH_STREAMS_FIRST_PACKET_ONLY",
114 STREAM0_FIRST_PACKET_TIME
,
115 STREAM1_FIRST_PACKET_TIME
,
116 STREAM0_FIRST_PACKET_NB_EVENTS
+ STREAM1_FIRST_PACKET_NB_EVENTS
,
117 STREAM1_FIRST_EVENT_TIME
,
118 STREAM0_LAST_EVENT_TIME
);
120 addParams(params
, "BOTH_STREAMS_SEVERAL_PACKETS",
121 STREAM1_FIFTH_PACKET_TIME
,
122 STREAM1_TENTH_PACKET_TIME
,
123 NB_EVENTS_SEVERAL_PACKETS
,
124 STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME
,
125 STREAM1_TENTH_PACKET_LAST_EVENT_TIME
);
130 private static void addParams(List
<Object
[]> params
, String name
, long startTime
, long endTime
, int nbEvents
, long firstEventTime
, long lastEventTime
) {
131 Object array
[] = new Object
[] { name
, startTime
, endTime
, nbEvents
, firstEventTime
, lastEventTime
};
136 public static void beforeClass() {
137 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
139 if (property
!= null) {
141 dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
142 dir
= new File(dir
.getAbsolutePath() + File
.separator
);
146 } catch (URISyntaxException e
) {
150 dir
= new File(System
.getProperty("java.io.tmpdir")); //$NON-NLS-1$)
152 String tempDir
= dir
.getAbsolutePath() + File
.separator
+ "testcases" + File
.separator
;
153 fTempDir
= new File(tempDir
);
154 if (!fTempDir
.exists()) {
159 public CTFTraceWriterTest (String name
, long startTime
, long endTime
, int nbEvents
, long firstEventTime
, long lastEventTime
) {
161 fStartTime
= startTime
;
163 fNbEvents
= nbEvents
;
164 fFirstEventTime
= firstEventTime
;
165 fLastEventTime
= lastEventTime
;
169 * Test various time ranges
172 public void testKernelTrace() {
174 CTFTrace trace
= CtfTestTraceUtils
.getTrace(CtfTestTrace
.KERNEL
);
175 CTFTraceWriter ctfWriter
= new CTFTraceWriter(checkNotNull(trace
));
176 String traceName
= createTraceName(fName
);
177 ctfWriter
.copyPackets(fStartTime
, fEndTime
, traceName
);
179 File metadata
= new File(traceName
+ Utils
.SEPARATOR
+ "metadata");
180 assertTrue("metadata", metadata
.exists());
182 CTFTrace outTrace
= new CTFTrace(traceName
);
186 try (CTFTraceReader reader
= new CTFTraceReader(outTrace
)) {
187 while(reader
.hasMoreEvents()) {
189 EventDefinition def
= reader
.getCurrentEventDef();
190 end
= def
.getTimestamp();
192 start
= outTrace
.getClock().getClockOffset() + reader
.getStartTime();
196 end
= outTrace
.getClock().getClockOffset() + end
;
199 if (fFirstEventTime
>= 0) {
200 assertEquals("first event time", Long
.valueOf(fFirstEventTime
), start
);
202 if (fLastEventTime
>= 0) {
203 assertEquals("last event time", fLastEventTime
, end
);
205 assertEquals(toString(), fNbEvents
, count
);
207 if (fNbEvents
== 0) {
208 assertFalse("channel0", getChannelFile(traceName
, 0).exists());
209 assertFalse("channel1", getChannelFile(traceName
, 1).exists());
212 } catch (CTFException e
) {
213 fail(e
.getMessage());
217 private static File
getChannelFile(String path
, int id
) {
218 File channel
= new File(path
+ Utils
.SEPARATOR
+ "channel_" + String
.valueOf(id
));
222 private static String
createTraceName(String testCase
) {
223 return fTempDir
.getAbsolutePath() + File
.separator
+ testCase
.toString();