ctf: fix output writer segment intersection algorithm
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFTraceWriterTest.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.tests.trace;
14
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;
21
22 import java.io.File;
23 import java.net.URISyntaxException;
24 import java.util.LinkedList;
25 import java.util.List;
26
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;
40
41 /**
42 * CTFTraceWriter test cases
43 *
44 * @author Bernd Hufmann
45 *
46 */
47 @SuppressWarnings("javadoc")
48 @RunWith(Parameterized.class)
49 public class CTFTraceWriterTest {
50
51 private static File fTempDir;
52
53 // Trace details
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;
57
58 // Stream 0 values
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;
63
64 // Stream 1 values
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;
72
73 // Miscellaneous
74 private static final int NB_EVENTS_SEVERAL_PACKETS = 167585;
75
76 // Test parameters
77 private String fName;
78 private long fStartTime;
79 private long fEndTime;
80 private int fNbEvents;
81 private long fFirstEventTime;
82 private long fLastEventTime;
83
84 /**
85 * Gets a list of test case parameters.
86 *
87 * @return The list of test parameters
88 */
89 @Parameters(name = "{index}: {0}")
90 public static Iterable<Object[]> getTestParams() {
91 final List<Object[]> params = new LinkedList<>();
92
93 addParams(params, "WHOLE_TRACE",
94 0,
95 Long.MAX_VALUE,
96 TOTAL_NB_EVENTS,
97 STREAM1_FIRST_EVENT_TIME,
98 LAST_EVENT_TIME);
99
100 addParams(params, "NO_EVENTS_USING_INVERTED_TIME",
101 Long.MAX_VALUE, Long.MIN_VALUE,
102 0,
103 -1,
104 -1);
105
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);
112
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);
119
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);
126
127 return params;
128 }
129
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 };
132 params.add(array);
133 }
134
135 @BeforeClass
136 public static void beforeClass() {
137 String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
138 File dir = null;
139 if (property != null) {
140 try {
141 dir = URIUtil.toFile(URIUtil.fromString(property));
142 dir = new File(dir.getAbsolutePath() + File.separator);
143 if (!dir.exists()) {
144 dir.mkdirs();
145 }
146 } catch (URISyntaxException e) {
147 }
148 }
149 if (dir == null) {
150 dir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$)
151 }
152 String tempDir = dir.getAbsolutePath() + File.separator + "testcases" + File.separator;
153 fTempDir = new File(tempDir);
154 if (!fTempDir.exists()) {
155 fTempDir.mkdirs();
156 }
157 }
158
159 public CTFTraceWriterTest (String name, long startTime, long endTime, int nbEvents, long firstEventTime, long lastEventTime) {
160 fName = name;
161 fStartTime = startTime;
162 fEndTime = endTime;
163 fNbEvents = nbEvents;
164 fFirstEventTime = firstEventTime;
165 fLastEventTime = lastEventTime;
166 }
167
168 /**
169 * Test various time ranges
170 */
171 @Test
172 public void testKernelTrace() {
173 assumeTrue(CtfTestTrace.KERNEL.exists());
174 try {
175 CTFTrace trace = CtfTestTrace.KERNEL.getTrace();
176 CTFTraceWriter ctfWriter = new CTFTraceWriter(checkNotNull(trace));
177 String traceName = createTraceName(fName);
178 ctfWriter.copyPackets(fStartTime, fEndTime, traceName);
179
180 File metadata = new File(traceName + Utils.SEPARATOR + "metadata");
181 assertTrue("metadata", metadata.exists());
182
183 CTFTrace outTrace = new CTFTrace(traceName);
184 int count = 0;
185 Long start = null;
186 long end = 0;
187 try (CTFTraceReader reader = new CTFTraceReader(outTrace)) {
188 while(reader.hasMoreEvents()) {
189 count++;
190 EventDefinition def = reader.getCurrentEventDef();
191 end = def.getTimestamp();
192 if (start == null) {
193 start = outTrace.getClock().getClockOffset() + reader.getStartTime();
194 }
195 reader.advance();
196 }
197 end = outTrace.getClock().getClockOffset() + end;
198 }
199
200 if (fFirstEventTime >= 0) {
201 assertEquals("first event time", Long.valueOf(fFirstEventTime), start);
202 }
203 if (fLastEventTime >= 0) {
204 assertEquals("last event time", fLastEventTime, end);
205 }
206 assertEquals(toString(), fNbEvents, count);
207
208 if (fNbEvents == 0) {
209 assertFalse("channel0", getChannelFile(traceName, 0).exists());
210 assertFalse("channel1", getChannelFile(traceName, 1).exists());
211 }
212
213 } catch (CTFException e) {
214 fail(e.getMessage());
215 }
216 }
217
218 private static File getChannelFile(String path, int id) {
219 File channel = new File(path + Utils.SEPARATOR + "channel_" + String.valueOf(id));
220 return channel;
221 }
222
223 private static String createTraceName(String testCase) {
224 return fTempDir.getAbsolutePath() + File.separator + testCase.toString();
225 }
226
227 }
This page took 0.036694 seconds and 6 git commands to generate.