Commit | Line | Data |
---|---|---|
51173fa1 BH |
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; | |
51173fa1 BH |
20 | |
21 | import java.io.File; | |
22 | import java.net.URISyntaxException; | |
23 | import java.util.LinkedList; | |
24 | import java.util.List; | |
25 | ||
26 | import org.eclipse.core.runtime.URIUtil; | |
27 | import org.eclipse.tracecompass.ctf.core.CTFException; | |
28 | import org.eclipse.tracecompass.ctf.core.event.EventDefinition; | |
c4d57ac1 | 29 | import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils; |
51173fa1 BH |
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; | |
1b638236 | 33 | import org.eclipse.tracecompass.internal.ctf.core.trace.Utils; |
c4d57ac1 | 34 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
51173fa1 BH |
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; | |
229d7d5a | 70 | private static final long STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME = 1332170682702069762L; |
51173fa1 BH |
71 | private static final long STREAM1_TENTH_PACKET_LAST_EVENT_TIME = 1332170685256508077L; |
72 | ||
73 | // Miscellaneous | |
229d7d5a | 74 | private static final int NB_EVENTS_SEVERAL_PACKETS = 167585; |
51173fa1 BH |
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 | ||
51173fa1 BH |
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() { | |
51173fa1 | 173 | try { |
c4d57ac1 | 174 | CTFTrace trace = CtfTestTraceUtils.getTrace(CtfTestTrace.KERNEL); |
51173fa1 BH |
175 | CTFTraceWriter ctfWriter = new CTFTraceWriter(checkNotNull(trace)); |
176 | String traceName = createTraceName(fName); | |
177 | ctfWriter.copyPackets(fStartTime, fEndTime, traceName); | |
178 | ||
179 | File metadata = new File(traceName + Utils.SEPARATOR + "metadata"); | |
180 | assertTrue("metadata", metadata.exists()); | |
181 | ||
182 | CTFTrace outTrace = new CTFTrace(traceName); | |
183 | int count = 0; | |
184 | Long start = null; | |
185 | long end = 0; | |
186 | try (CTFTraceReader reader = new CTFTraceReader(outTrace)) { | |
187 | while(reader.hasMoreEvents()) { | |
188 | count++; | |
189 | EventDefinition def = reader.getCurrentEventDef(); | |
190 | end = def.getTimestamp(); | |
191 | if (start == null) { | |
192 | start = outTrace.getClock().getClockOffset() + reader.getStartTime(); | |
193 | } | |
194 | reader.advance(); | |
195 | } | |
196 | end = outTrace.getClock().getClockOffset() + end; | |
197 | } | |
198 | ||
199 | if (fFirstEventTime >= 0) { | |
200 | assertEquals("first event time", Long.valueOf(fFirstEventTime), start); | |
201 | } | |
202 | if (fLastEventTime >= 0) { | |
203 | assertEquals("last event time", fLastEventTime, end); | |
204 | } | |
205 | assertEquals(toString(), fNbEvents, count); | |
206 | ||
207 | if (fNbEvents == 0) { | |
208 | assertFalse("channel0", getChannelFile(traceName, 0).exists()); | |
209 | assertFalse("channel1", getChannelFile(traceName, 1).exists()); | |
210 | } | |
211 | ||
212 | } catch (CTFException e) { | |
229d7d5a | 213 | fail(e.getMessage()); |
51173fa1 BH |
214 | } |
215 | } | |
216 | ||
217 | private static File getChannelFile(String path, int id) { | |
218 | File channel = new File(path + Utils.SEPARATOR + "channel_" + String.valueOf(id)); | |
219 | return channel; | |
220 | } | |
221 | ||
222 | private static String createTraceName(String testCase) { | |
223 | return fTempDir.getAbsolutePath() + File.separator + testCase.toString(); | |
224 | } | |
225 | ||
226 | } |