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