Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
09e86496 | 2 | * Copyright (c) 2009, 2010, 2012 Ericsson |
0bfb7d06 | 3 | * |
8c8bf09f 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 | |
0bfb7d06 | 8 | * |
8c8bf09f | 9 | * Contributors: |
20658947 FC |
10 | * Francois Chouinard - Initial API and implementation |
11 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 | |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.trace; |
8c8bf09f | 15 | |
6f4a1d2b | 16 | import java.io.File; |
8c8bf09f | 17 | |
828e5592 | 18 | import org.eclipse.core.resources.IResource; |
9b749023 | 19 | import org.eclipse.core.runtime.IPath; |
6c13869b | 20 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
72f1e62a | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
4df4581d | 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
23 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
b4f71e4a | 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b FC |
26 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
27 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
8c8bf09f ASL |
28 | |
29 | /** | |
09e86496 FC |
30 | * Abstract implementation of ITmfTrace. |
31 | * <p> | |
13cb5f43 FC |
32 | * Since the concept of 'location' is trace specific, the concrete classes have |
33 | * to provide the related methods, namely: | |
34 | * <ul> | |
35 | * <li> public ITmfLocation<?> getCurrentLocation() | |
36 | * <li> public double getLocationRatio(ITmfLocation<?> location) | |
37 | * <li> public ITmfContext seekEvent(ITmfLocation<?> location) | |
38 | * <li> public ITmfContext seekEvent(double ratio) | |
2848c377 | 39 | * <li> public boolean validate(IProject project, String path) |
13cb5f43 FC |
40 | * </ul> |
41 | * A concrete trace must provide its corresponding parser. A common way to | |
42 | * accomplish this is by making the concrete class extend TmfTrace and | |
43 | * implement ITmfEventParser. | |
44 | * <p> | |
45 | * The concrete class can either specify its own indexer or use the provided | |
46 | * TmfCheckpointIndexer (default). In this case, the trace cache size will be | |
47 | * used as checkpoint interval. | |
0bfb7d06 | 48 | * |
f7703ed6 FC |
49 | * @version 1.0 |
50 | * @author Francois Chouinard | |
51 | * | |
f7703ed6 FC |
52 | * @see ITmfEvent |
53 | * @see ITmfTraceIndexer | |
54 | * @see ITmfEventParser | |
8c8bf09f | 55 | */ |
3791b5df | 56 | public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> { |
62d1696a | 57 | |
e31e01e8 | 58 | // ------------------------------------------------------------------------ |
8c8bf09f | 59 | // Attributes |
e31e01e8 | 60 | // ------------------------------------------------------------------------ |
8c8bf09f | 61 | |
09e86496 FC |
62 | // The resource used for persistent properties for this trace |
63 | private IResource fResource; | |
64 | ||
b0a282fb | 65 | // The trace path |
12c155f5 | 66 | private String fPath; |
b0a282fb | 67 | |
0316808c FC |
68 | // The trace cache page size |
69 | private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; | |
62d1696a | 70 | |
0316808c FC |
71 | // The number of events collected (so far) |
72 | private long fNbEvents = 0; | |
62d1696a FC |
73 | |
74 | // The time span of the event stream | |
a4115405 FC |
75 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH; |
76 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; | |
62d1696a | 77 | |
0316808c FC |
78 | // The trace streaming interval (0 = no streaming) |
79 | private long fStreamingInterval = 0; | |
085d898f | 80 | |
0316808c FC |
81 | // The trace indexer |
82 | private ITmfTraceIndexer<ITmfTrace<ITmfEvent>> fIndexer; | |
20658947 | 83 | |
0316808c FC |
84 | // The trace parser |
85 | private ITmfEventParser<T> fParser; | |
7e6347b0 | 86 | |
e31e01e8 | 87 | // ------------------------------------------------------------------------ |
3791b5df | 88 | // Construction |
e31e01e8 | 89 | // ------------------------------------------------------------------------ |
8c8bf09f | 90 | |
62d1696a | 91 | /** |
3791b5df | 92 | * The default, parameterless, constructor |
62d1696a | 93 | */ |
3791b5df FC |
94 | public TmfTrace() { |
95 | super(); | |
05bd3318 FC |
96 | } |
97 | ||
98 | /** | |
13cb5f43 | 99 | * The standard constructor (non-live trace). Applicable when the trace |
0bfb7d06 MK |
100 | * implements its own parser and if at checkpoint-based index is OK. |
101 | * | |
20658947 FC |
102 | * @param resource the resource associated to the trace |
103 | * @param type the trace event type | |
104 | * @param path the trace path | |
105 | * @param cacheSize the trace cache size | |
2352aed9 | 106 | * @throws TmfTraceException |
20658947 | 107 | */ |
b4f71e4a | 108 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize) throws TmfTraceException { |
9e0640dc | 109 | this(resource, type, path, cacheSize, 0); |
20658947 FC |
110 | } |
111 | ||
112 | /** | |
13cb5f43 FC |
113 | * The standard constructor (live trace). Applicable when the trace |
114 | * implements its own parser and if at checkpoint-based index is OK. | |
0bfb7d06 | 115 | * |
20658947 | 116 | * @param resource the resource associated to the trace |
3791b5df FC |
117 | * @param type the trace event type |
118 | * @param path the trace path | |
20658947 FC |
119 | * @param cacheSize the trace cache size |
120 | * @param interval the trace streaming interval | |
2352aed9 | 121 | * @throws TmfTraceException |
05bd3318 | 122 | */ |
b4f71e4a | 123 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, final long interval) throws TmfTraceException { |
20658947 | 124 | this(resource, type, path, cacheSize, interval, null); |
05bd3318 FC |
125 | } |
126 | ||
127 | /** | |
13cb5f43 FC |
128 | * The 'non-default indexer' constructor. Allows to provide a trace |
129 | * specific indexer. | |
0bfb7d06 | 130 | * |
20658947 | 131 | * @param resource the resource associated to the trace |
3791b5df FC |
132 | * @param type the trace event type |
133 | * @param path the trace path | |
20658947 FC |
134 | * @param cacheSize the trace cache size |
135 | * @param indexer the trace indexer | |
2352aed9 | 136 | * @throws TmfTraceException |
05bd3318 | 137 | */ |
20658947 | 138 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, |
b4f71e4a | 139 | final long interval, final ITmfTraceIndexer<?> indexer) throws TmfTraceException { |
0316808c | 140 | this(resource, type, path, cacheSize, interval, indexer, null); |
13cb5f43 FC |
141 | } |
142 | ||
143 | /** | |
0bfb7d06 MK |
144 | * The full constructor where trace specific indexer/parser are provided. |
145 | * | |
13cb5f43 FC |
146 | * @param resource the resource associated to the trace |
147 | * @param type the trace event type | |
148 | * @param path the trace path | |
149 | * @param cacheSize the trace cache size | |
150 | * @param indexer the trace indexer | |
151 | * @param parser the trace event parser | |
2352aed9 | 152 | * @throws TmfTraceException |
13cb5f43 FC |
153 | */ |
154 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
155 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, | |
2352aed9 | 156 | final long interval, final ITmfTraceIndexer<?> indexer, final ITmfEventParser<T> parser) throws TmfTraceException { |
00641a97 | 157 | super(); |
0316808c | 158 | fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; |
3791b5df | 159 | fStreamingInterval = interval; |
7e6347b0 | 160 | fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize); |
13cb5f43 | 161 | fParser = parser; |
09e86496 | 162 | initialize(resource, path, type); |
8c8bf09f ASL |
163 | } |
164 | ||
3791b5df FC |
165 | /** |
166 | * Copy constructor | |
0bfb7d06 | 167 | * |
3791b5df | 168 | * @param trace the original trace |
063f0d27 | 169 | * @throws TmfTraceException Should not happen usually |
3791b5df | 170 | */ |
20658947 | 171 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
b4f71e4a | 172 | public TmfTrace(final TmfTrace<T> trace) throws TmfTraceException { |
3791b5df | 173 | super(); |
0316808c | 174 | if (trace == null) { |
3791b5df | 175 | throw new IllegalArgumentException(); |
0316808c | 176 | } |
20658947 FC |
177 | fCacheSize = trace.getCacheSize(); |
178 | fStreamingInterval = trace.getStreamingInterval(); | |
7e6347b0 | 179 | fIndexer = new TmfCheckpointIndexer(this); |
13cb5f43 FC |
180 | fParser = trace.fParser; |
181 | initialize(trace.getResource(), trace.getPath(), trace.getEventType()); | |
3791b5df FC |
182 | } |
183 | ||
7e6347b0 FC |
184 | // ------------------------------------------------------------------------ |
185 | // ITmfTrace - Initializers | |
186 | // ------------------------------------------------------------------------ | |
187 | ||
188 | /* (non-Javadoc) | |
189 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class) | |
190 | */ | |
191 | @Override | |
9dc3dee2 | 192 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
b4f71e4a | 193 | public void initTrace(final IResource resource, final String path, final Class<T> type) throws TmfTraceException { |
9dc3dee2 | 194 | fIndexer = new TmfCheckpointIndexer(this, fCacheSize); |
7e6347b0 | 195 | initialize(resource, path, type); |
7e6347b0 FC |
196 | } |
197 | ||
09e86496 | 198 | /** |
1703b536 | 199 | * Initialize the trace common attributes and the base component. |
0bfb7d06 MK |
200 | * |
201 | * @param resource the Eclipse resource (trace) | |
1703b536 FC |
202 | * @param path the trace path |
203 | * @param type the trace event type | |
0bfb7d06 | 204 | * |
0316808c | 205 | * @throws TmfTraceException |
3791b5df | 206 | */ |
2352aed9 | 207 | @SuppressWarnings("unchecked") |
b4f71e4a | 208 | protected void initialize(final IResource resource, final String path, final Class<T> type) throws TmfTraceException { |
0316808c | 209 | if (path == null) { |
b4f71e4a | 210 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
0316808c | 211 | } |
3791b5df | 212 | fPath = path; |
1703b536 | 213 | fResource = resource; |
25e48683 | 214 | String traceName = (resource != null) ? resource.getName() : null; |
1703b536 FC |
215 | // If no resource was provided, extract the display name the trace path |
216 | if (traceName == null) { | |
9b749023 | 217 | final int sep = path.lastIndexOf(IPath.SEPARATOR); |
1703b536 FC |
218 | traceName = (sep >= 0) ? path.substring(sep + 1) : path; |
219 | } | |
2352aed9 FC |
220 | if (fParser == null) { |
221 | if (this instanceof ITmfEventParser) { | |
222 | fParser = (ITmfEventParser<T>) this; | |
223 | } else { | |
224 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ | |
225 | } | |
226 | } | |
3791b5df FC |
227 | super.init(traceName, type); |
228 | } | |
229 | ||
2352aed9 FC |
230 | /** |
231 | * Indicates if the path points to an existing file/directory | |
0bfb7d06 | 232 | * |
2352aed9 FC |
233 | * @param path the path to test |
234 | * @return true if the file/directory exists | |
3791b5df | 235 | */ |
2352aed9 | 236 | protected boolean fileExists(final String path) { |
085d898f | 237 | final File file = new File(path); |
3791b5df FC |
238 | return file.exists(); |
239 | } | |
240 | ||
c7e1020d FC |
241 | /** |
242 | * Index the trace | |
0bfb7d06 | 243 | * |
c7e1020d FC |
244 | * @param waitForCompletion index synchronously (true) or not (false) |
245 | */ | |
246 | protected void indexTrace(boolean waitForCompletion) { | |
9e0640dc | 247 | getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion); |
c7e1020d FC |
248 | } |
249 | ||
b5ee6881 FC |
250 | /** |
251 | * Clears the trace | |
252 | */ | |
253 | @Override | |
254 | public synchronized void dispose() { | |
77551cc2 FC |
255 | // Clean up the index if applicable |
256 | if (getIndexer() != null) { | |
257 | getIndexer().dispose(); | |
258 | } | |
b5ee6881 FC |
259 | super.dispose(); |
260 | } | |
261 | ||
3791b5df | 262 | // ------------------------------------------------------------------------ |
09e86496 | 263 | // ITmfTrace - Basic getters |
e31e01e8 | 264 | // ------------------------------------------------------------------------ |
8c8bf09f | 265 | |
09e86496 | 266 | /* (non-Javadoc) |
13cb5f43 | 267 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType() |
25e48683 FC |
268 | */ |
269 | @Override | |
09e86496 | 270 | @SuppressWarnings("unchecked") |
13cb5f43 | 271 | public Class<T> getEventType() { |
09e86496 | 272 | return (Class<T>) super.getType(); |
25e48683 FC |
273 | } |
274 | ||
09e86496 FC |
275 | /* (non-Javadoc) |
276 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource() | |
62d1696a | 277 | */ |
d4011df2 | 278 | @Override |
09e86496 FC |
279 | public IResource getResource() { |
280 | return fResource; | |
8c8bf09f ASL |
281 | } |
282 | ||
09e86496 FC |
283 | /* (non-Javadoc) |
284 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath() | |
62d1696a | 285 | */ |
d4011df2 | 286 | @Override |
09e86496 FC |
287 | public String getPath() { |
288 | return fPath; | |
8c8bf09f ASL |
289 | } |
290 | ||
20658947 FC |
291 | /* (non-Javadoc) |
292 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize() | |
293 | */ | |
294 | @Override | |
295 | public int getCacheSize() { | |
296 | return fCacheSize; | |
297 | } | |
298 | ||
299 | /* (non-Javadoc) | |
300 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval() | |
301 | */ | |
302 | @Override | |
303 | public long getStreamingInterval() { | |
304 | return fStreamingInterval; | |
305 | } | |
306 | ||
0316808c FC |
307 | /** |
308 | * @return the trace indexer | |
309 | */ | |
310 | protected ITmfTraceIndexer<ITmfTrace<ITmfEvent>> getIndexer() { | |
311 | return fIndexer; | |
312 | } | |
313 | ||
314 | /** | |
315 | * @return the trace parser | |
316 | */ | |
317 | protected ITmfEventParser<T> getParser() { | |
318 | return fParser; | |
319 | } | |
320 | ||
09e86496 FC |
321 | // ------------------------------------------------------------------------ |
322 | // ITmfTrace - Trace characteristics getters | |
323 | // ------------------------------------------------------------------------ | |
324 | ||
325 | /* (non-Javadoc) | |
326 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents() | |
b0a282fb | 327 | */ |
d4011df2 | 328 | @Override |
afc86f78 | 329 | public synchronized long getNbEvents() { |
3791b5df | 330 | return fNbEvents; |
b0a282fb FC |
331 | } |
332 | ||
09e86496 FC |
333 | /* (non-Javadoc) |
334 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange() | |
62d1696a | 335 | */ |
d4011df2 | 336 | @Override |
12c155f5 | 337 | public TmfTimeRange getTimeRange() { |
cb866e08 | 338 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
339 | } |
340 | ||
09e86496 FC |
341 | /* (non-Javadoc) |
342 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime() | |
e31e01e8 | 343 | */ |
d4011df2 | 344 | @Override |
4df4581d | 345 | public ITmfTimestamp getStartTime() { |
20658947 | 346 | return fStartTime.clone(); |
146a887c FC |
347 | } |
348 | ||
09e86496 FC |
349 | /* (non-Javadoc) |
350 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime() | |
e31e01e8 | 351 | */ |
d4011df2 | 352 | @Override |
4df4581d | 353 | public ITmfTimestamp getEndTime() { |
20658947 FC |
354 | return fEndTime.clone(); |
355 | } | |
356 | ||
357 | // ------------------------------------------------------------------------ | |
0316808c | 358 | // Convenience setters/getters |
20658947 FC |
359 | // ------------------------------------------------------------------------ |
360 | ||
0316808c FC |
361 | /** |
362 | * Set the trace cache size. Must be done at initialization time. | |
0bfb7d06 | 363 | * |
0316808c FC |
364 | * @param cacheSize The trace cache size |
365 | */ | |
366 | protected void setCacheSize(final int cacheSize) { | |
367 | fCacheSize = cacheSize; | |
368 | } | |
369 | ||
370 | /** | |
371 | * Set the trace known number of events. This can be quite dynamic | |
372 | * during indexing or for live traces. | |
0bfb7d06 | 373 | * |
0316808c FC |
374 | * @param nbEvents The number of events |
375 | */ | |
376 | protected synchronized void setNbEvents(final long nbEvents) { | |
377 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; | |
378 | } | |
379 | ||
20658947 FC |
380 | /** |
381 | * Update the trace events time range | |
0bfb7d06 | 382 | * |
20658947 FC |
383 | * @param range the new time range |
384 | */ | |
385 | protected void setTimeRange(final TmfTimeRange range) { | |
386 | fStartTime = range.getStartTime().clone(); | |
387 | fEndTime = range.getEndTime().clone(); | |
388 | } | |
389 | ||
390 | /** | |
391 | * Update the trace chronologically first event timestamp | |
0bfb7d06 | 392 | * |
20658947 FC |
393 | * @param startTime the new first event timestamp |
394 | */ | |
395 | protected void setStartTime(final ITmfTimestamp startTime) { | |
396 | fStartTime = startTime.clone(); | |
397 | } | |
398 | ||
399 | /** | |
400 | * Update the trace chronologically last event timestamp | |
0bfb7d06 | 401 | * |
20658947 FC |
402 | * @param endTime the new last event timestamp |
403 | */ | |
404 | protected void setEndTime(final ITmfTimestamp endTime) { | |
405 | fEndTime = endTime.clone(); | |
406 | } | |
407 | ||
408 | /** | |
0316808c | 409 | * Set the polling interval for live traces (default = 0 = no streaming). |
0bfb7d06 | 410 | * |
20658947 FC |
411 | * @param interval the new trace streaming interval |
412 | */ | |
413 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 414 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
415 | } |
416 | ||
0316808c FC |
417 | /** |
418 | * Set the trace indexer. Must be done at initialization time. | |
0bfb7d06 | 419 | * |
0316808c FC |
420 | * @param indexer the trace indexer |
421 | */ | |
422 | protected void setIndexer(final ITmfTraceIndexer<ITmfTrace<ITmfEvent>> indexer) { | |
423 | fIndexer = indexer; | |
424 | } | |
425 | ||
426 | /** | |
427 | * Set the trace parser. Must be done at initialization time. | |
0bfb7d06 | 428 | * |
0316808c FC |
429 | * @param parser the new trace parser |
430 | */ | |
431 | protected void setParser(final ITmfEventParser<T> parser) { | |
432 | fParser = parser; | |
433 | } | |
434 | ||
09e86496 | 435 | // ------------------------------------------------------------------------ |
7e6347b0 | 436 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
437 | // ------------------------------------------------------------------------ |
438 | ||
439 | /* (non-Javadoc) | |
7e6347b0 | 440 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long) |
1b70b6dc PT |
441 | */ |
442 | @Override | |
7e6347b0 | 443 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 444 | |
7e6347b0 | 445 | // A rank <= 0 indicates to seek the first event |
2352aed9 FC |
446 | if (rank <= 0) { |
447 | ITmfContext context = seekEvent((ITmfLocation<?>) null); | |
448 | context.setRank(0); | |
449 | return context; | |
450 | } | |
09e86496 | 451 | |
09e86496 | 452 | // Position the trace at the checkpoint |
7e6347b0 | 453 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
454 | |
455 | // And locate the requested event context | |
7e6347b0 FC |
456 | long pos = context.getRank(); |
457 | if (pos < rank) { | |
c32744d6 | 458 | ITmfEvent event = getNext(context); |
0bfb7d06 | 459 | while ((event != null) && (++pos < rank)) { |
c32744d6 | 460 | event = getNext(context); |
7e6347b0 | 461 | } |
09e86496 FC |
462 | } |
463 | return context; | |
1b70b6dc PT |
464 | } |
465 | ||
09e86496 | 466 | /* (non-Javadoc) |
7e6347b0 | 467 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) |
09e86496 FC |
468 | */ |
469 | @Override | |
7e6347b0 | 470 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 471 | |
7e6347b0 | 472 | // A null timestamp indicates to seek the first event |
2352aed9 FC |
473 | if (timestamp == null) { |
474 | ITmfContext context = seekEvent((ITmfLocation<?>) null); | |
475 | context.setRank(0); | |
476 | return context; | |
477 | } | |
09e86496 | 478 | |
1703b536 | 479 | // Position the trace at the checkpoint |
408e65d2 | 480 | ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
481 | |
482 | // And locate the requested event context | |
7e6347b0 | 483 | final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... |
c32744d6 | 484 | ITmfEvent event = getNext(nextEventContext); |
7e6347b0 | 485 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
3cec7398 | 486 | context = nextEventContext.clone(); |
c32744d6 | 487 | event = getNext(nextEventContext); |
09e86496 | 488 | } |
0316808c FC |
489 | if (event == null) { |
490 | context.setLocation(null); | |
491 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
492 | } | |
09e86496 FC |
493 | return context; |
494 | } | |
17324c9a | 495 | |
09e86496 FC |
496 | // ------------------------------------------------------------------------ |
497 | // ITmfTrace - Read operations (returning an actual event) | |
498 | // ------------------------------------------------------------------------ | |
499 | ||
d337369a | 500 | /* (non-Javadoc) |
b4f71e4a | 501 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext) |
abfad0aa | 502 | */ |
d4011df2 | 503 | @Override |
c32744d6 | 504 | public synchronized T getNext(final ITmfContext context) { |
09e86496 | 505 | // parseEvent() does not update the context |
c32744d6 | 506 | final T event = fParser.parseEvent(context); |
09e86496 | 507 | if (event != null) { |
d337369a | 508 | updateAttributes(context, event.getTimestamp()); |
09e86496 FC |
509 | context.setLocation(getCurrentLocation()); |
510 | context.increaseRank(); | |
511 | processEvent(event); | |
512 | } | |
513 | return event; | |
514 | } | |
515 | ||
516 | /** | |
d337369a | 517 | * Hook for special event processing by the concrete class |
7e6347b0 | 518 | * (called by TmfTrace.getEvent()) |
0bfb7d06 | 519 | * |
d337369a | 520 | * @param event the event |
09e86496 FC |
521 | */ |
522 | protected void processEvent(final ITmfEvent event) { | |
d337369a | 523 | // Do nothing |
09e86496 FC |
524 | } |
525 | ||
d337369a FC |
526 | /** |
527 | * Update the trace attributes | |
0bfb7d06 | 528 | * |
d337369a | 529 | * @param context the current trace context |
2848c377 | 530 | * @param timestamp the corresponding timestamp |
d337369a FC |
531 | */ |
532 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { | |
0bfb7d06 | 533 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) { |
49e2f79a | 534 | fStartTime = timestamp.clone(); |
09e86496 | 535 | } |
0bfb7d06 | 536 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) { |
49e2f79a | 537 | fEndTime = timestamp.clone(); |
09e86496 FC |
538 | } |
539 | if (context.hasValidRank()) { | |
d337369a | 540 | long rank = context.getRank(); |
09e86496 FC |
541 | if (fNbEvents <= rank) { |
542 | fNbEvents = rank + 1; | |
543 | } | |
d337369a | 544 | fIndexer.updateIndex(context, timestamp); |
09e86496 | 545 | } |
abfad0aa FC |
546 | } |
547 | ||
3791b5df | 548 | // ------------------------------------------------------------------------ |
d337369a | 549 | // TmfDataProvider |
3791b5df FC |
550 | // ------------------------------------------------------------------------ |
551 | ||
d337369a FC |
552 | /* (non-Javadoc) |
553 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest) | |
554 | */ | |
3791b5df | 555 | @Override |
9e0640dc | 556 | protected ITmfContext armRequest(final ITmfDataRequest<T> request) { |
0bfb7d06 | 557 | if ((request instanceof ITmfEventRequest<?>) |
0316808c | 558 | && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime()) |
0bfb7d06 | 559 | && (request.getIndex() == 0)) |
0316808c | 560 | { |
085d898f | 561 | final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime()); |
3791b5df FC |
562 | ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank()); |
563 | return context; | |
564 | ||
565 | } | |
566 | return seekEvent(request.getIndex()); | |
567 | } | |
568 | ||
3791b5df | 569 | // ------------------------------------------------------------------------ |
09e86496 | 570 | // toString |
3791b5df FC |
571 | // ------------------------------------------------------------------------ |
572 | ||
d337369a FC |
573 | /* (non-Javadoc) |
574 | * @see java.lang.Object#toString() | |
575 | */ | |
12c155f5 | 576 | @Override |
09e86496 | 577 | @SuppressWarnings("nls") |
afc86f78 | 578 | public synchronized String toString() { |
20658947 FC |
579 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
580 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
581 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
582 | } |
583 | ||
8c8bf09f | 584 | } |