Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 Ericsson |
0283f7ff | 3 | * |
d18dd09b ASL |
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 | |
0283f7ff | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
ea271da6 | 11 | * Patrick Tasse - Updated for removal of context clone |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
4918b8f2 | 14 | package org.eclipse.linuxtools.tmf.tests.stubs.trace; |
d18dd09b ASL |
15 | |
16 | import java.io.FileNotFoundException; | |
17 | import java.io.IOException; | |
18 | import java.io.RandomAccessFile; | |
73005152 | 19 | import java.util.concurrent.locks.ReentrantLock; |
d18dd09b | 20 | |
2352aed9 | 21 | import org.eclipse.core.resources.IProject; |
20658947 | 22 | import org.eclipse.core.resources.IResource; |
6256d8ad | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
b4f71e4a | 24 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
3bd46eef AM |
25 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
26 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
6c13869b | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
0316808c | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
20658947 | 31 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer; |
6c13869b | 32 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
cb8c854e | 33 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; |
6c13869b | 34 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
d18dd09b ASL |
35 | |
36 | /** | |
37 | * <b><u>TmfTraceStub</u></b> | |
38 | * <p> | |
8d2e2848 | 39 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 40 | */ |
cad06250 | 41 | @SuppressWarnings("javadoc") |
6256d8ad | 42 | public class TmfTraceStub extends TmfTrace implements ITmfEventParser { |
d18dd09b | 43 | |
e31e01e8 | 44 | // ------------------------------------------------------------------------ |
d18dd09b | 45 | // Attributes |
e31e01e8 | 46 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
47 | |
48 | // The actual stream | |
ff4ed569 | 49 | private RandomAccessFile fTrace; |
d18dd09b | 50 | |
7e6347b0 FC |
51 | // // The associated event parser |
52 | // private ITmfEventParser<TmfEvent> fParser; | |
d18dd09b | 53 | |
73005152 | 54 | // The synchronization lock |
085d898f FC |
55 | private final ReentrantLock fLock = new ReentrantLock(); |
56 | ||
66262ad8 BH |
57 | private ITmfTimestamp fInitialRangeOffset = null; |
58 | ||
e31e01e8 | 59 | // ------------------------------------------------------------------------ |
d18dd09b | 60 | // Constructors |
e31e01e8 | 61 | // ------------------------------------------------------------------------ |
d18dd09b | 62 | |
20658947 FC |
63 | public TmfTraceStub() { |
64 | super(); | |
0316808c | 65 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
66 | } |
67 | ||
d18dd09b | 68 | /** |
3ef62bac | 69 | * @param path |
d18dd09b ASL |
70 | * @throws FileNotFoundException |
71 | */ | |
b4f71e4a | 72 | public TmfTraceStub(final String path) throws TmfTraceException { |
0316808c | 73 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false); |
8d2e2848 FC |
74 | } |
75 | ||
76 | /** | |
3ef62bac | 77 | * @param path |
e31e01e8 | 78 | * @param cacheSize |
8d2e2848 FC |
79 | * @throws FileNotFoundException |
80 | */ | |
b4f71e4a | 81 | public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException { |
3ef62bac | 82 | this(path, cacheSize, false); |
d18dd09b ASL |
83 | } |
84 | ||
1703b536 FC |
85 | /** |
86 | * @param path | |
87 | * @param cacheSize | |
88 | * @throws FileNotFoundException | |
89 | */ | |
b4f71e4a | 90 | public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException { |
2771b032 | 91 | super(null, ITmfEvent.class, path, cacheSize, interval); |
b4f71e4a FC |
92 | try { |
93 | fTrace = new RandomAccessFile(path, "r"); | |
94 | } catch (FileNotFoundException e) { | |
95 | throw new TmfTraceException(e.getMessage()); | |
96 | } | |
0316808c | 97 | setParser(new TmfEventParserStub(this)); |
1703b536 FC |
98 | } |
99 | ||
20658947 FC |
100 | /** |
101 | * @param path | |
102 | * @param cacheSize | |
103 | * @throws FileNotFoundException | |
104 | */ | |
6256d8ad | 105 | public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException { |
20658947 FC |
106 | this(path, cacheSize, false, null, indexer); |
107 | } | |
108 | ||
d18dd09b | 109 | /** |
3ef62bac | 110 | * @param path |
ff4ed569 | 111 | * @param waitForCompletion |
d18dd09b ASL |
112 | * @throws FileNotFoundException |
113 | */ | |
b4f71e4a | 114 | public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException { |
0316808c | 115 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion); |
8d2e2848 | 116 | } |
085d898f | 117 | |
8d2e2848 | 118 | /** |
3ef62bac | 119 | * @param path |
8d2e2848 | 120 | * @param cacheSize |
ff4ed569 | 121 | * @param waitForCompletion |
8d2e2848 FC |
122 | * @throws FileNotFoundException |
123 | */ | |
b4f71e4a | 124 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
2771b032 | 125 | super(null, ITmfEvent.class, path, cacheSize); |
b4f71e4a FC |
126 | try { |
127 | fTrace = new RandomAccessFile(path, "r"); | |
128 | } catch (FileNotFoundException e) { | |
129 | throw new TmfTraceException(e.getMessage()); | |
130 | } | |
0316808c | 131 | setParser(new TmfEventParserStub(this)); |
07671572 FC |
132 | if (waitForCompletion) { |
133 | indexTrace(); | |
134 | } | |
d18dd09b ASL |
135 | } |
136 | ||
20658947 FC |
137 | /** |
138 | * @param path | |
139 | * @param cacheSize | |
140 | * @param waitForCompletion | |
141 | * @throws FileNotFoundException | |
142 | */ | |
b4f71e4a | 143 | public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
2771b032 | 144 | super(resource, ITmfEvent.class, path, cacheSize); |
b4f71e4a FC |
145 | try { |
146 | fTrace = new RandomAccessFile(path, "r"); | |
147 | } catch (FileNotFoundException e) { | |
148 | throw new TmfTraceException(e.getMessage()); | |
149 | } | |
0316808c | 150 | setParser(new TmfEventParserStub(this)); |
20658947 | 151 | } |
085d898f | 152 | |
73005152 | 153 | /** |
3ef62bac | 154 | * @param path |
73005152 BH |
155 | * @param cacheSize |
156 | * @param waitForCompletion | |
157 | * @param parser | |
158 | * @throws FileNotFoundException | |
159 | */ | |
20658947 | 160 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, |
6256d8ad | 161 | final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException { |
2771b032 | 162 | super(null, ITmfEvent.class, path, cacheSize, 0, indexer); |
b4f71e4a FC |
163 | try { |
164 | fTrace = new RandomAccessFile(path, "r"); | |
165 | } catch (FileNotFoundException e) { | |
166 | throw new TmfTraceException(e.getMessage()); | |
167 | } | |
0316808c | 168 | setParser((parser != null) ? parser : new TmfEventParserStub(this)); |
73005152 | 169 | } |
085d898f | 170 | |
ff4ed569 | 171 | /** |
f17b2f70 | 172 | * Copy constructor |
ff4ed569 | 173 | */ |
b4f71e4a | 174 | public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException { |
20658947 | 175 | super(trace); |
b4f71e4a FC |
176 | try { |
177 | fTrace = new RandomAccessFile(getPath(), "r"); | |
178 | } catch (FileNotFoundException e) { | |
179 | throw new TmfTraceException(e.getMessage()); | |
180 | } | |
0316808c | 181 | setParser(new TmfEventParserStub(this)); |
ff4ed569 | 182 | } |
085d898f | 183 | |
20658947 | 184 | public void indexTrace() { |
c7e1020d | 185 | indexTrace(true); |
20658947 FC |
186 | } |
187 | ||
188 | @Override | |
6256d8ad | 189 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
b4f71e4a FC |
190 | try { |
191 | fTrace = new RandomAccessFile(path, "r"); | |
192 | } catch (FileNotFoundException e) { | |
193 | throw new TmfTraceException(e.getMessage()); | |
194 | } | |
0316808c | 195 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
196 | super.initTrace(resource, path, type); |
197 | } | |
f17b2f70 | 198 | |
1703b536 | 199 | @Override |
6256d8ad | 200 | public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
1703b536 FC |
201 | super.initialize(resource, path, type); |
202 | } | |
203 | ||
e31e01e8 | 204 | // ------------------------------------------------------------------------ |
d18dd09b | 205 | // Accessors |
e31e01e8 | 206 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
207 | |
208 | public RandomAccessFile getStream() { | |
209 | return fTrace; | |
210 | } | |
211 | ||
66262ad8 BH |
212 | public void setInitialRangeOffset(ITmfTimestamp initOffset) { |
213 | fInitialRangeOffset = initOffset; | |
214 | } | |
215 | ||
216 | @Override | |
217 | public ITmfTimestamp getInitialRangeOffset() { | |
218 | if (fInitialRangeOffset != null) { | |
219 | return fInitialRangeOffset; | |
220 | } | |
221 | return super.getInitialRangeOffset(); | |
222 | } | |
223 | ||
e31e01e8 | 224 | // ------------------------------------------------------------------------ |
d18dd09b | 225 | // Operators |
e31e01e8 | 226 | // ------------------------------------------------------------------------ |
d18dd09b | 227 | |
085d898f | 228 | @Override |
1e1bef82 | 229 | public TmfContext seekEvent(final ITmfLocation location) { |
d18dd09b | 230 | try { |
09e86496 FC |
231 | fLock.lock(); |
232 | try { | |
233 | if (fTrace != null) { | |
234 | // Position the trace at the requested location and | |
235 | // returns the corresponding context | |
236 | long loc = 0; | |
237 | long rank = 0; | |
238 | if (location != null) { | |
5976d44a | 239 | loc = (Long) location.getLocationInfo(); |
09e86496 FC |
240 | rank = ITmfContext.UNKNOWN_RANK; |
241 | } | |
20658947 | 242 | if (loc != fTrace.getFilePointer()) { |
09e86496 | 243 | fTrace.seek(loc); |
20658947 | 244 | } |
09e86496 FC |
245 | final TmfContext context = new TmfContext(getCurrentLocation(), rank); |
246 | return context; | |
73005152 | 247 | } |
09e86496 FC |
248 | } catch (final IOException e) { |
249 | e.printStackTrace(); | |
250 | } catch (final NullPointerException e) { | |
251 | e.printStackTrace(); | |
73005152 | 252 | } |
3427112b FC |
253 | finally{ |
254 | fLock.unlock(); | |
255 | } | |
09e86496 | 256 | } catch (final NullPointerException e) { |
085d898f FC |
257 | e.printStackTrace(); |
258 | } | |
085d898f | 259 | return null; |
d18dd09b ASL |
260 | } |
261 | ||
c76c54bb | 262 | |
085d898f | 263 | @Override |
7e6347b0 | 264 | public TmfContext seekEvent(final double ratio) { |
085d898f | 265 | fLock.lock(); |
c76c54bb | 266 | try { |
73005152 | 267 | if (fTrace != null) { |
15e89960 | 268 | final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length()))); |
7e6347b0 | 269 | final TmfContext context = seekEvent(location); |
73005152 BH |
270 | context.setRank(ITmfContext.UNKNOWN_RANK); |
271 | return context; | |
272 | } | |
085d898f | 273 | } catch (final IOException e) { |
c76c54bb | 274 | e.printStackTrace(); |
73005152 BH |
275 | } finally { |
276 | fLock.unlock(); | |
c76c54bb | 277 | } |
085d898f | 278 | |
c76c54bb FC |
279 | return null; |
280 | } | |
281 | ||
282 | @Override | |
1e1bef82 | 283 | public double getLocationRatio(ITmfLocation location) { |
73005152 | 284 | fLock.lock(); |
c76c54bb | 285 | try { |
0283f7ff | 286 | if (fTrace != null) { |
5976d44a FC |
287 | if (location.getLocationInfo() instanceof Long) { |
288 | return (double) ((Long) location.getLocationInfo()) / fTrace.length(); | |
0283f7ff FC |
289 | } |
290 | } | |
085d898f | 291 | } catch (final IOException e) { |
c76c54bb | 292 | e.printStackTrace(); |
73005152 BH |
293 | } finally { |
294 | fLock.unlock(); | |
c76c54bb FC |
295 | } |
296 | return 0; | |
297 | } | |
298 | ||
4e3aa37d | 299 | @Override |
1e1bef82 | 300 | public ITmfLocation getCurrentLocation() { |
73005152 | 301 | fLock.lock(); |
d18dd09b | 302 | try { |
0283f7ff | 303 | if (fTrace != null) { |
cb8c854e | 304 | return new TmfLongLocation(fTrace.getFilePointer()); |
0283f7ff | 305 | } |
085d898f FC |
306 | } catch (final IOException e) { |
307 | e.printStackTrace(); | |
308 | } finally { | |
309 | fLock.unlock(); | |
310 | } | |
311 | return null; | |
312 | } | |
313 | ||
314 | @Override | |
6256d8ad | 315 | public ITmfEvent parseEvent(final ITmfContext context) { |
085d898f FC |
316 | fLock.lock(); |
317 | try { | |
318 | // parseNextEvent will update the context | |
0316808c | 319 | if (fTrace != null && getParser() != null && context != null) { |
ea271da6 | 320 | final ITmfEvent event = getParser().parseEvent(context); |
085d898f | 321 | return event; |
73005152 | 322 | } |
73005152 BH |
323 | } finally { |
324 | fLock.unlock(); | |
d18dd09b ASL |
325 | } |
326 | return null; | |
327 | } | |
328 | ||
b75d6b65 | 329 | @Override |
9b749023 | 330 | public synchronized void setNbEvents(final long nbEvents) { |
b75d6b65 FC |
331 | super.setNbEvents(nbEvents); |
332 | } | |
333 | ||
085d898f FC |
334 | @Override |
335 | public void setTimeRange(final TmfTimeRange range) { | |
336 | super.setTimeRange(range); | |
337 | } | |
d18dd09b | 338 | |
085d898f FC |
339 | @Override |
340 | public void setStartTime(final ITmfTimestamp startTime) { | |
341 | super.setStartTime(startTime); | |
ff4ed569 FC |
342 | } |
343 | ||
085d898f FC |
344 | @Override |
345 | public void setEndTime(final ITmfTimestamp endTime) { | |
346 | super.setEndTime(endTime); | |
ff4ed569 FC |
347 | } |
348 | ||
1703b536 FC |
349 | @Override |
350 | public void setStreamingInterval(final long interval) { | |
351 | super.setStreamingInterval(interval); | |
352 | } | |
353 | ||
085d898f | 354 | @Override |
9b749023 | 355 | public synchronized void dispose() { |
085d898f FC |
356 | fLock.lock(); |
357 | try { | |
358 | if (fTrace != null) { | |
359 | fTrace.close(); | |
360 | fTrace = null; | |
361 | } | |
362 | } catch (final IOException e) { | |
363 | // Ignore | |
364 | } finally { | |
365 | fLock.unlock(); | |
366 | } | |
367 | super.dispose(); | |
ff4ed569 FC |
368 | } |
369 | ||
2352aed9 FC |
370 | /* (non-Javadoc) |
371 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String) | |
372 | */ | |
373 | @Override | |
374 | public boolean validate(IProject project, String path) { | |
375 | return fileExists(path); | |
376 | } | |
377 | ||
9b749023 | 378 | } |