[CTF] fix support for traces with per-event contexts
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
CommitLineData
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 14package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 15
6f4a1d2b 16import java.io.File;
a51b2b9f
AM
17import java.util.Collection;
18import java.util.HashMap;
b6cfa2bb 19import java.util.Iterator;
a51b2b9f 20import java.util.Map;
8c8bf09f 21
828e5592 22import org.eclipse.core.resources.IResource;
faa38350 23import org.eclipse.core.runtime.CoreException;
9b749023 24import org.eclipse.core.runtime.IPath;
6c13869b 25import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
72f1e62a 26import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
4df4581d 27import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
28import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
29import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 30import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
8584dc20
FC
31import org.eclipse.linuxtools.tmf.core.request.ITmfRequest;
32import org.eclipse.linuxtools.tmf.core.request.TmfBlockFilter;
d7ee91bb 33import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
faa38350 34import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
d7ee91bb 35import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
faa38350
PT
36import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
37import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
7898bb21 38import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
200789b3 39import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
1c0de632 40import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
8c8bf09f
ASL
41
42/**
09e86496
FC
43 * Abstract implementation of ITmfTrace.
44 * <p>
13cb5f43
FC
45 * Since the concept of 'location' is trace specific, the concrete classes have
46 * to provide the related methods, namely:
47 * <ul>
48 * <li> public ITmfLocation<?> getCurrentLocation()
49 * <li> public double getLocationRatio(ITmfLocation<?> location)
50 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
51 * <li> public ITmfContext seekEvent(double ratio)
2848c377 52 * <li> public boolean validate(IProject project, String path)
13cb5f43
FC
53 * </ul>
54 * A concrete trace must provide its corresponding parser. A common way to
55 * accomplish this is by making the concrete class extend TmfTrace and
56 * implement ITmfEventParser.
57 * <p>
58 * The concrete class can either specify its own indexer or use the provided
59 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
60 * used as checkpoint interval.
0bfb7d06 61 *
f7703ed6
FC
62 * @version 1.0
63 * @author Francois Chouinard
64 *
f7703ed6
FC
65 * @see ITmfEvent
66 * @see ITmfTraceIndexer
67 * @see ITmfEventParser
8c8bf09f 68 */
6256d8ad 69public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
62d1696a 70
e31e01e8 71 // ------------------------------------------------------------------------
8c8bf09f 72 // Attributes
e31e01e8 73 // ------------------------------------------------------------------------
8c8bf09f 74
09e86496
FC
75 // The resource used for persistent properties for this trace
76 private IResource fResource;
77
b0a282fb 78 // The trace path
12c155f5 79 private String fPath;
b0a282fb 80
0316808c
FC
81 // The trace cache page size
82 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
62d1696a 83
0316808c
FC
84 // The number of events collected (so far)
85 private long fNbEvents = 0;
62d1696a
FC
86
87 // The time span of the event stream
9cbe7899 88 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
a4115405 89 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
62d1696a 90
0316808c
FC
91 // The trace streaming interval (0 = no streaming)
92 private long fStreamingInterval = 0;
085d898f 93
0316808c 94 // The trace indexer
6256d8ad 95 private ITmfTraceIndexer fIndexer;
20658947 96
0316808c 97 // The trace parser
6256d8ad 98 private ITmfEventParser fParser;
7e6347b0 99
200789b3
AM
100 // The trace's statistics
101 private ITmfStatistics fStatistics;
102
d7ee91bb
PT
103 // The current selected time
104 private ITmfTimestamp fCurrentTime = TmfTimestamp.ZERO;
105
106 // The current selected range
107 private TmfTimeRange fCurrentRange = TmfTimeRange.NULL_RANGE;
108
a51b2b9f
AM
109 /**
110 * The collection of state systems that are registered with this trace. Each
111 * sub-class can decide to add its (one or many) state system to this map
112 * during their {@link #buildStateSystem()}.
113 *
114 * @since 2.0
115 */
116 protected final Map<String, ITmfStateSystem> fStateSystems =
117 new HashMap<String, ITmfStateSystem>();
118
e31e01e8 119 // ------------------------------------------------------------------------
3791b5df 120 // Construction
e31e01e8 121 // ------------------------------------------------------------------------
8c8bf09f 122
62d1696a 123 /**
3791b5df 124 * The default, parameterless, constructor
62d1696a 125 */
3791b5df
FC
126 public TmfTrace() {
127 super();
05bd3318
FC
128 }
129
130 /**
13cb5f43 131 * The standard constructor (non-live trace). Applicable when the trace
0bfb7d06
MK
132 * implements its own parser and if at checkpoint-based index is OK.
133 *
20658947
FC
134 * @param resource the resource associated to the trace
135 * @param type the trace event type
136 * @param path the trace path
137 * @param cacheSize the trace cache size
6f4e8ec0 138 * @throws TmfTraceException If something failed during the opening
20658947 139 */
6256d8ad 140 protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize) throws TmfTraceException {
9e0640dc 141 this(resource, type, path, cacheSize, 0);
20658947
FC
142 }
143
144 /**
13cb5f43
FC
145 * The standard constructor (live trace). Applicable when the trace
146 * implements its own parser and if at checkpoint-based index is OK.
0bfb7d06 147 *
20658947 148 * @param resource the resource associated to the trace
3791b5df
FC
149 * @param type the trace event type
150 * @param path the trace path
20658947
FC
151 * @param cacheSize the trace cache size
152 * @param interval the trace streaming interval
6f4e8ec0 153 * @throws TmfTraceException If something failed during the opening
05bd3318 154 */
6256d8ad 155 protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize, final long interval) throws TmfTraceException {
20658947 156 this(resource, type, path, cacheSize, interval, null);
05bd3318
FC
157 }
158
159 /**
13cb5f43
FC
160 * The 'non-default indexer' constructor. Allows to provide a trace
161 * specific indexer.
0bfb7d06 162 *
20658947 163 * @param resource the resource associated to the trace
3791b5df
FC
164 * @param type the trace event type
165 * @param path the trace path
20658947 166 * @param cacheSize the trace cache size
6f4e8ec0 167 * @param interval the trace streaming interval
20658947 168 * @param indexer the trace indexer
6f4e8ec0 169 * @throws TmfTraceException If something failed during the opening
05bd3318 170 */
6256d8ad
AM
171 protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize,
172 final long interval, final ITmfTraceIndexer indexer) throws TmfTraceException {
0316808c 173 this(resource, type, path, cacheSize, interval, indexer, null);
13cb5f43
FC
174 }
175
176 /**
0bfb7d06
MK
177 * The full constructor where trace specific indexer/parser are provided.
178 *
13cb5f43
FC
179 * @param resource the resource associated to the trace
180 * @param type the trace event type
181 * @param path the trace path
182 * @param cacheSize the trace cache size
6f4e8ec0 183 * @param interval the trace streaming interval
13cb5f43
FC
184 * @param indexer the trace indexer
185 * @param parser the trace event parser
6f4e8ec0 186 * @throws TmfTraceException If something failed during the opening
13cb5f43 187 */
6256d8ad
AM
188 protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize,
189 final long interval, final ITmfTraceIndexer indexer, final ITmfEventParser parser) throws TmfTraceException {
00641a97 190 super();
0316808c 191 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
3791b5df 192 fStreamingInterval = interval;
7e6347b0 193 fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize);
13cb5f43 194 fParser = parser;
09e86496 195 initialize(resource, path, type);
8c8bf09f
ASL
196 }
197
3791b5df
FC
198 /**
199 * Copy constructor
0bfb7d06 200 *
3791b5df 201 * @param trace the original trace
063f0d27 202 * @throws TmfTraceException Should not happen usually
3791b5df 203 */
6256d8ad 204 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
3791b5df 205 super();
0316808c 206 if (trace == null) {
3791b5df 207 throw new IllegalArgumentException();
0316808c 208 }
20658947
FC
209 fCacheSize = trace.getCacheSize();
210 fStreamingInterval = trace.getStreamingInterval();
7e6347b0 211 fIndexer = new TmfCheckpointIndexer(this);
13cb5f43
FC
212 fParser = trace.fParser;
213 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
3791b5df
FC
214 }
215
7e6347b0
FC
216 // ------------------------------------------------------------------------
217 // ITmfTrace - Initializers
218 // ------------------------------------------------------------------------
219
220 /* (non-Javadoc)
221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
222 */
223 @Override
6256d8ad 224 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
9dc3dee2 225 fIndexer = new TmfCheckpointIndexer(this, fCacheSize);
7e6347b0 226 initialize(resource, path, type);
7e6347b0
FC
227 }
228
09e86496 229 /**
1703b536 230 * Initialize the trace common attributes and the base component.
0bfb7d06
MK
231 *
232 * @param resource the Eclipse resource (trace)
1703b536
FC
233 * @param path the trace path
234 * @param type the trace event type
0bfb7d06 235 *
6f4e8ec0 236 * @throws TmfTraceException If something failed during the initialization
3791b5df 237 */
6256d8ad 238 protected void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
0316808c 239 if (path == null) {
b4f71e4a 240 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
0316808c 241 }
3791b5df 242 fPath = path;
1703b536 243 fResource = resource;
25e48683 244 String traceName = (resource != null) ? resource.getName() : null;
1703b536
FC
245 // If no resource was provided, extract the display name the trace path
246 if (traceName == null) {
9b749023 247 final int sep = path.lastIndexOf(IPath.SEPARATOR);
1703b536
FC
248 traceName = (sep >= 0) ? path.substring(sep + 1) : path;
249 }
2352aed9
FC
250 if (fParser == null) {
251 if (this instanceof ITmfEventParser) {
6256d8ad 252 fParser = (ITmfEventParser) this;
2352aed9
FC
253 } else {
254 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
255 }
256 }
3791b5df
FC
257 super.init(traceName, type);
258 }
259
2352aed9
FC
260 /**
261 * Indicates if the path points to an existing file/directory
0bfb7d06 262 *
2352aed9
FC
263 * @param path the path to test
264 * @return true if the file/directory exists
3791b5df 265 */
2352aed9 266 protected boolean fileExists(final String path) {
085d898f 267 final File file = new File(path);
3791b5df
FC
268 return file.exists();
269 }
270
c7e1020d
FC
271 /**
272 * Index the trace
0bfb7d06 273 *
c7e1020d
FC
274 * @param waitForCompletion index synchronously (true) or not (false)
275 */
276 protected void indexTrace(boolean waitForCompletion) {
9e0640dc 277 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
c7e1020d
FC
278 }
279
200789b3 280 /**
6f4e8ec0 281 * The default implementation of TmfTrace uses a TmfStatistics back-end.
200789b3
AM
282 * Override this if you want to specify another type (or none at all).
283 *
6f4e8ec0
AM
284 * @throws TmfTraceException
285 * If there was a problem setting up the statistics
200789b3
AM
286 * @since 2.0
287 */
288 protected void buildStatistics() throws TmfTraceException {
289 /*
290 * Initialize the statistics provider, but only if a Resource has been
291 * set (so we don't build it for experiments, for unit tests, etc.)
292 */
1c0de632 293 fStatistics = (fResource == null ? null : new TmfStateStatistics(this) );
200789b3
AM
294 }
295
faa38350
PT
296 /**
297 * Build the state system(s) associated with this trace type.
298 *
299 * Suppressing the warning, because the 'throws' will usually happen in
300 * sub-classes.
301 *
302 * @throws TmfTraceException
303 * If there is a problem during the build
304 * @since 2.0
305 */
306 @SuppressWarnings("unused")
307 protected void buildStateSystem() throws TmfTraceException {
308 /*
309 * Nothing is done in the base implementation, please specify
a51b2b9f 310 * how/if to register a new state system in derived classes.
faa38350
PT
311 */
312 return;
313 }
314
b5ee6881
FC
315 /**
316 * Clears the trace
317 */
318 @Override
319 public synchronized void dispose() {
1a4205d9 320 /* Clean up the index if applicable */
77551cc2
FC
321 if (getIndexer() != null) {
322 getIndexer().dispose();
323 }
1a4205d9
AM
324
325 /* Clean up the statistics */
326 if (fStatistics != null) {
327 fStatistics.dispose();
328 }
a51b2b9f
AM
329
330 /* Clean up the state systems */
331 for (ITmfStateSystem ss : fStateSystems.values()) {
332 ss.dispose();
333 }
334
b5ee6881
FC
335 super.dispose();
336 }
337
3791b5df 338 // ------------------------------------------------------------------------
09e86496 339 // ITmfTrace - Basic getters
e31e01e8 340 // ------------------------------------------------------------------------
8c8bf09f 341
09e86496 342 /* (non-Javadoc)
13cb5f43 343 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
25e48683
FC
344 */
345 @Override
6256d8ad
AM
346 public Class<ITmfEvent> getEventType() {
347 return (Class<ITmfEvent>) super.getType();
25e48683
FC
348 }
349
09e86496
FC
350 /* (non-Javadoc)
351 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
62d1696a 352 */
d4011df2 353 @Override
09e86496
FC
354 public IResource getResource() {
355 return fResource;
8c8bf09f
ASL
356 }
357
09e86496
FC
358 /* (non-Javadoc)
359 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
62d1696a 360 */
d4011df2 361 @Override
09e86496
FC
362 public String getPath() {
363 return fPath;
8c8bf09f
ASL
364 }
365
20658947
FC
366 /* (non-Javadoc)
367 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
368 */
369 @Override
370 public int getCacheSize() {
371 return fCacheSize;
372 }
373
374 /* (non-Javadoc)
375 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
376 */
377 @Override
378 public long getStreamingInterval() {
379 return fStreamingInterval;
380 }
381
0316808c
FC
382 /**
383 * @return the trace indexer
384 */
6256d8ad 385 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
386 return fIndexer;
387 }
388
389 /**
390 * @return the trace parser
391 */
6256d8ad 392 protected ITmfEventParser getParser() {
0316808c
FC
393 return fParser;
394 }
395
200789b3
AM
396 /**
397 * @since 2.0
398 */
399 @Override
400 public ITmfStatistics getStatistics() {
401 return fStatistics;
402 }
403
7898bb21
AM
404 /**
405 * @since 2.0
406 */
407 @Override
a51b2b9f
AM
408 public final ITmfStateSystem getStateSystem(String id) {
409 return fStateSystems.get(id);
410 }
411
412 /**
413 * @since 2.0
414 */
415 @Override
416 public final Collection<String> listStateSystems() {
417 return fStateSystems.keySet();
7898bb21
AM
418 }
419
09e86496
FC
420 // ------------------------------------------------------------------------
421 // ITmfTrace - Trace characteristics getters
422 // ------------------------------------------------------------------------
423
424 /* (non-Javadoc)
425 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
b0a282fb 426 */
d4011df2 427 @Override
8584dc20 428 public long getNbEvents() {
3791b5df 429 return fNbEvents;
b0a282fb
FC
430 }
431
09e86496
FC
432 /* (non-Javadoc)
433 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
62d1696a 434 */
d4011df2 435 @Override
12c155f5 436 public TmfTimeRange getTimeRange() {
cb866e08 437 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
438 }
439
09e86496
FC
440 /* (non-Javadoc)
441 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
e31e01e8 442 */
d4011df2 443 @Override
4df4581d 444 public ITmfTimestamp getStartTime() {
4593bd5b 445 return fStartTime;
146a887c
FC
446 }
447
09e86496
FC
448 /* (non-Javadoc)
449 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
e31e01e8 450 */
d4011df2 451 @Override
4df4581d 452 public ITmfTimestamp getEndTime() {
4593bd5b 453 return fEndTime;
20658947
FC
454 }
455
d7ee91bb
PT
456 /* (non-Javadoc)
457 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentTime()
458 */
459 /**
460 * @since 2.0
461 */
462 @Override
463 public ITmfTimestamp getCurrentTime() {
464 return fCurrentTime;
465 }
466
467 /* (non-Javadoc)
468 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentRange()
469 */
470 /**
471 * @since 2.0
472 */
473 @Override
474 public TmfTimeRange getCurrentRange() {
475 return fCurrentRange;
476 }
477
66262ad8
BH
478 /*
479 * (non-Javadoc)
480 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getInitialRangeOffset()
481 */
d7ee91bb 482 /**
d7ee91bb
PT
483 * @since 2.0
484 */
66262ad8
BH
485 @Override
486 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
487 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
488 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
489 }
490
20658947 491 // ------------------------------------------------------------------------
d7ee91bb 492 // Convenience setters
20658947
FC
493 // ------------------------------------------------------------------------
494
0316808c
FC
495 /**
496 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 497 *
0316808c
FC
498 * @param cacheSize The trace cache size
499 */
500 protected void setCacheSize(final int cacheSize) {
501 fCacheSize = cacheSize;
502 }
503
504 /**
505 * Set the trace known number of events. This can be quite dynamic
506 * during indexing or for live traces.
0bfb7d06 507 *
0316808c
FC
508 * @param nbEvents The number of events
509 */
510 protected synchronized void setNbEvents(final long nbEvents) {
511 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
512 }
513
20658947
FC
514 /**
515 * Update the trace events time range
0bfb7d06 516 *
20658947
FC
517 * @param range the new time range
518 */
519 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
520 fStartTime = range.getStartTime();
521 fEndTime = range.getEndTime();
20658947
FC
522 }
523
524 /**
525 * Update the trace chronologically first event timestamp
0bfb7d06 526 *
20658947
FC
527 * @param startTime the new first event timestamp
528 */
529 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 530 fStartTime = startTime;
20658947
FC
531 }
532
533 /**
534 * Update the trace chronologically last event timestamp
0bfb7d06 535 *
20658947
FC
536 * @param endTime the new last event timestamp
537 */
538 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 539 fEndTime = endTime;
20658947
FC
540 }
541
542 /**
0316808c 543 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 544 *
20658947
FC
545 * @param interval the new trace streaming interval
546 */
547 protected void setStreamingInterval(final long interval) {
1703b536 548 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
549 }
550
0316808c
FC
551 /**
552 * Set the trace indexer. Must be done at initialization time.
0bfb7d06 553 *
0316808c
FC
554 * @param indexer the trace indexer
555 */
6256d8ad 556 protected void setIndexer(final ITmfTraceIndexer indexer) {
0316808c
FC
557 fIndexer = indexer;
558 }
559
560 /**
561 * Set the trace parser. Must be done at initialization time.
0bfb7d06 562 *
0316808c
FC
563 * @param parser the new trace parser
564 */
6256d8ad 565 protected void setParser(final ITmfEventParser parser) {
0316808c
FC
566 fParser = parser;
567 }
568
09e86496 569 // ------------------------------------------------------------------------
7e6347b0 570 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
571 // ------------------------------------------------------------------------
572
573 /* (non-Javadoc)
7e6347b0 574 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
1b70b6dc
PT
575 */
576 @Override
7e6347b0 577 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 578
7e6347b0 579 // A rank <= 0 indicates to seek the first event
2352aed9 580 if (rank <= 0) {
1e1bef82 581 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
582 context.setRank(0);
583 return context;
584 }
09e86496 585
09e86496 586 // Position the trace at the checkpoint
7e6347b0 587 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
588
589 // And locate the requested event context
7e6347b0
FC
590 long pos = context.getRank();
591 if (pos < rank) {
c32744d6 592 ITmfEvent event = getNext(context);
0bfb7d06 593 while ((event != null) && (++pos < rank)) {
c32744d6 594 event = getNext(context);
7e6347b0 595 }
09e86496
FC
596 }
597 return context;
1b70b6dc
PT
598 }
599
09e86496 600 /* (non-Javadoc)
7e6347b0 601 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
09e86496
FC
602 */
603 @Override
7e6347b0 604 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 605
7e6347b0 606 // A null timestamp indicates to seek the first event
2352aed9 607 if (timestamp == null) {
1e1bef82 608 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
609 context.setRank(0);
610 return context;
611 }
09e86496 612
1703b536 613 // Position the trace at the checkpoint
408e65d2 614 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
615
616 // And locate the requested event context
7e6347b0 617 final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
c32744d6 618 ITmfEvent event = getNext(nextEventContext);
7e6347b0 619 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
4c9f2944 620 context.dispose();
3cec7398 621 context = nextEventContext.clone();
c32744d6 622 event = getNext(nextEventContext);
09e86496 623 }
4c9f2944 624 nextEventContext.dispose();
0316808c
FC
625 if (event == null) {
626 context.setLocation(null);
627 context.setRank(ITmfContext.UNKNOWN_RANK);
628 }
09e86496
FC
629 return context;
630 }
0283f7ff 631
b6cfa2bb
FC
632 // ------------------------------------------------------------------------
633 // ITmfTrace - Iterator operations
634 // ------------------------------------------------------------------------
635
636 /* (non-Javadoc)
637 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#iterator()
638 */
639 /**
640 * @since 2.0
641 */
642 @Override
643 public Iterator<ITmfEvent> iterator() {
644 return new TmfTraceIterator(this);
645 }
646
647 /* (non-Javadoc)
648 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#iterator(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
649 */
650 /**
651 * @since 2.0
652 */
653 @Override
654 public Iterator<ITmfEvent> iterator(ITmfLocation location) {
655 return new TmfTraceIterator(this, location);
656 }
657
658 /* (non-Javadoc)
659 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#iterator(long)
660 */
661 /**
662 * @since 2.0
663 */
664 @Override
665 public Iterator<ITmfEvent> iterator(long rank) {
666 return new TmfTraceIterator(this, rank);
667 }
668
669 /* (non-Javadoc)
670 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#iterator(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
671 */
672 /**
673 * @since 2.0
674 */
675 @Override
676 public Iterator<ITmfEvent> iterator(ITmfTimestamp timestamp) {
677 return new TmfTraceIterator(this, timestamp);
678 }
679
680 /* (non-Javadoc)
681 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#iterator(double)
682 */
683 /**
684 * @since 2.0
685 */
686 @Override
687 public Iterator<ITmfEvent> iterator(double ratio) {
688 return new TmfTraceIterator(this, ratio);
689 }
690
09e86496
FC
691 // ------------------------------------------------------------------------
692 // ITmfTrace - Read operations (returning an actual event)
693 // ------------------------------------------------------------------------
694
d337369a 695 /* (non-Javadoc)
b4f71e4a 696 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
abfad0aa 697 */
d4011df2 698 @Override
6256d8ad 699 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 700 // parseEvent() does not update the context
6256d8ad 701 final ITmfEvent event = fParser.parseEvent(context);
09e86496 702 if (event != null) {
d337369a 703 updateAttributes(context, event.getTimestamp());
09e86496
FC
704 context.setLocation(getCurrentLocation());
705 context.increaseRank();
706 processEvent(event);
707 }
708 return event;
709 }
710
711 /**
d337369a 712 * Hook for special event processing by the concrete class
7e6347b0 713 * (called by TmfTrace.getEvent())
0bfb7d06 714 *
d337369a 715 * @param event the event
09e86496
FC
716 */
717 protected void processEvent(final ITmfEvent event) {
d337369a 718 // Do nothing
09e86496
FC
719 }
720
d337369a
FC
721 /**
722 * Update the trace attributes
0bfb7d06 723 *
d337369a 724 * @param context the current trace context
2848c377 725 * @param timestamp the corresponding timestamp
d337369a
FC
726 */
727 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
0bfb7d06 728 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
4593bd5b 729 fStartTime = timestamp;
09e86496 730 }
0bfb7d06 731 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
4593bd5b 732 fEndTime = timestamp;
09e86496 733 }
d7ee91bb
PT
734 if (fCurrentRange == TmfTimeRange.NULL_RANGE) {
735 fCurrentTime = timestamp;
736 ITmfTimestamp initialOffset = getInitialRangeOffset();
737 long endValue = timestamp.getValue() + initialOffset.normalize(0, timestamp.getScale()).getValue();
738 ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, timestamp.getScale());
739 fCurrentRange = new TmfTimeRange(timestamp, endTimestamp);
740 }
09e86496 741 if (context.hasValidRank()) {
d337369a 742 long rank = context.getRank();
09e86496
FC
743 if (fNbEvents <= rank) {
744 fNbEvents = rank + 1;
745 }
200789b3
AM
746 if (fIndexer != null) {
747 fIndexer.updateIndex(context, timestamp);
748 }
09e86496 749 }
abfad0aa
FC
750 }
751
3791b5df 752 // ------------------------------------------------------------------------
d337369a 753 // TmfDataProvider
3791b5df
FC
754 // ------------------------------------------------------------------------
755
77c4a6df
AM
756 /**
757 * @since 2.0
d337369a 758 */
3791b5df 759 @Override
8584dc20 760 public synchronized ITmfContext armRequest(final ITmfRequest request) {
faa38350
PT
761 if (executorIsShutdown()) {
762 return null;
763 }
3791b5df 764
8584dc20
FC
765 ITmfTimestamp startTime = request.getTimeRange().getStartTime();
766 long startindex = request.getStartIndex();
767 if (!TmfTimestamp.BIG_BANG.equals(startTime) && startindex == 0) {
768 final ITmfContext context = seekEvent(request.getTimeRange().getStartTime());
769 request.addEventFilter(new TmfBlockFilter(context.getRank(), request.getNbRequested()));
770 return context;
3791b5df 771 }
8584dc20
FC
772
773 return seekEvent(request.getStartIndex());
3791b5df
FC
774 }
775
faa38350
PT
776 // ------------------------------------------------------------------------
777 // Signal handlers
778 // ------------------------------------------------------------------------
779
780 /**
781 * Handler for the Trace Opened signal
782 *
783 * @param signal
784 * The incoming signal
785 * @since 2.0
786 */
787 @TmfSignalHandler
788 public void traceOpened(TmfTraceOpenedSignal signal) {
789 ITmfTrace trace = signal.getTrace();
790 if (signal.getTrace() instanceof TmfExperiment) {
791 TmfExperiment experiment = (TmfExperiment) signal.getTrace();
792 for (ITmfTrace expTrace : experiment.getTraces()) {
793 if (expTrace == this) {
794 trace = expTrace;
795 break;
796 }
797 }
798 }
faa38350
PT
799 if (trace == this) {
800 /* the signal is for this trace or for an experiment containing this trace */
801 try {
802 buildStatistics();
803 } catch (TmfTraceException e) {
804 e.printStackTrace();
805 }
806 try {
807 buildStateSystem();
808 } catch (TmfTraceException e) {
809 e.printStackTrace();
810 }
811
812 /* Refresh the project, so it can pick up new files that got created. */
813 try {
814 if (fResource != null) {
815 fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
816 }
817 } catch (CoreException e) {
818 e.printStackTrace();
819 }
820 }
821 if (signal.getTrace() == this) {
822 /* the signal is for this trace or experiment */
823 if (getNbEvents() == 0) {
824 return;
825 }
826
827 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
828 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
829
830 // Broadcast in separate thread to prevent deadlock
831 new Thread() {
832 @Override
833 public void run() {
834 broadcast(rangeUpdatedsignal);
835 }
836 }.start();
837 return;
838 }
839 }
840
841 /**
842 * Signal handler for the TmfTraceRangeUpdatedSignal signal
843 *
844 * @param signal The incoming signal
845 * @since 2.0
846 */
847 @TmfSignalHandler
848 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
849 if (signal.getTrace() == this) {
850 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
851 }
852 }
853
d7ee91bb
PT
854 /**
855 * Signal handler for the TmfTimeSynchSignal signal
856 *
857 * @param signal The incoming signal
858 * @since 2.0
859 */
860 @TmfSignalHandler
861 public void synchToTime(final TmfTimeSynchSignal signal) {
862 if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
863 fCurrentTime = signal.getCurrentTime();
864 }
865 }
866
867 /**
868 * Signal handler for the TmfRangeSynchSignal signal
869 *
870 * @param signal The incoming signal
871 * @since 2.0
872 */
873 @TmfSignalHandler
874 public void synchToRange(final TmfRangeSynchSignal signal) {
875 if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
876 fCurrentTime = signal.getCurrentTime();
877 }
878 if (signal.getCurrentRange().getIntersection(getTimeRange()) != null) {
879 fCurrentRange = signal.getCurrentRange().getIntersection(getTimeRange());
880 }
881 }
882
3791b5df 883 // ------------------------------------------------------------------------
09e86496 884 // toString
3791b5df
FC
885 // ------------------------------------------------------------------------
886
d337369a
FC
887 /* (non-Javadoc)
888 * @see java.lang.Object#toString()
889 */
12c155f5 890 @Override
09e86496 891 @SuppressWarnings("nls")
8584dc20 892 public String toString() {
20658947
FC
893 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
894 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
895 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
896 }
897
8c8bf09f 898}
This page took 0.117257 seconds and 5 git commands to generate.