Use a read-only TextPropertyDescriptor for event properties
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 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
ea271da6 12 * Patrick Tasse - Updated for removal of context clone
8c8bf09f
ASL
13 *******************************************************************************/
14
6c13869b 15package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 16
6f4a1d2b 17import java.io.File;
35c160d9
AM
18import java.util.Collections;
19import java.util.LinkedHashMap;
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;
b4f71e4a 27import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
5419a136
AM
28import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
29import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
d7ee91bb 30import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
faa38350 31import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
d7ee91bb 32import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
faa38350
PT
33import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
34import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
7898bb21 35import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
200789b3 36import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
1c0de632 37import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
3bd46eef
AM
38import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
39import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
40import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
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 =
35c160d9 117 new LinkedHashMap<String, ITmfStateSystem>();
a51b2b9f 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
fe0c44c4
AM
342 /**
343 * @since 2.0
344 */
345 @Override
346 public ITmfTrace[] getTraces() {
347 return new ITmfTrace[] { this };
348 }
349
09e86496 350 /* (non-Javadoc)
13cb5f43 351 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
25e48683
FC
352 */
353 @Override
6256d8ad
AM
354 public Class<ITmfEvent> getEventType() {
355 return (Class<ITmfEvent>) super.getType();
25e48683
FC
356 }
357
09e86496
FC
358 /* (non-Javadoc)
359 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
62d1696a 360 */
d4011df2 361 @Override
09e86496
FC
362 public IResource getResource() {
363 return fResource;
8c8bf09f
ASL
364 }
365
09e86496
FC
366 /* (non-Javadoc)
367 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
62d1696a 368 */
d4011df2 369 @Override
09e86496
FC
370 public String getPath() {
371 return fPath;
8c8bf09f
ASL
372 }
373
20658947
FC
374 /* (non-Javadoc)
375 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
376 */
377 @Override
378 public int getCacheSize() {
379 return fCacheSize;
380 }
381
382 /* (non-Javadoc)
383 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
384 */
385 @Override
386 public long getStreamingInterval() {
387 return fStreamingInterval;
388 }
389
0316808c
FC
390 /**
391 * @return the trace indexer
392 */
6256d8ad 393 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
394 return fIndexer;
395 }
396
397 /**
398 * @return the trace parser
399 */
6256d8ad 400 protected ITmfEventParser getParser() {
0316808c
FC
401 return fParser;
402 }
403
200789b3
AM
404 /**
405 * @since 2.0
406 */
407 @Override
408 public ITmfStatistics getStatistics() {
409 return fStatistics;
410 }
411
7898bb21
AM
412 /**
413 * @since 2.0
414 */
415 @Override
35c160d9
AM
416 public final Map<String, ITmfStateSystem> getStateSystems() {
417 return Collections.unmodifiableMap(fStateSystems);
7898bb21
AM
418 }
419
6c5e0863
AM
420 /**
421 * @since 2.0
422 */
423 @Override
424 public final void registerStateSystem(String id, ITmfStateSystem ss) {
425 fStateSystems.put(id, ss);
426 }
427
09e86496
FC
428 // ------------------------------------------------------------------------
429 // ITmfTrace - Trace characteristics getters
430 // ------------------------------------------------------------------------
431
d4011df2 432 @Override
5419a136 433 public synchronized long getNbEvents() {
3791b5df 434 return fNbEvents;
b0a282fb
FC
435 }
436
3bd46eef
AM
437 /**
438 * @since 2.0
62d1696a 439 */
d4011df2 440 @Override
12c155f5 441 public TmfTimeRange getTimeRange() {
cb866e08 442 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
443 }
444
3bd46eef
AM
445 /**
446 * @since 2.0
e31e01e8 447 */
d4011df2 448 @Override
4df4581d 449 public ITmfTimestamp getStartTime() {
4593bd5b 450 return fStartTime;
146a887c
FC
451 }
452
3bd46eef
AM
453 /**
454 * @since 2.0
e31e01e8 455 */
d4011df2 456 @Override
4df4581d 457 public ITmfTimestamp getEndTime() {
4593bd5b 458 return fEndTime;
20658947
FC
459 }
460
d7ee91bb
PT
461 /**
462 * @since 2.0
463 */
464 @Override
465 public ITmfTimestamp getCurrentTime() {
466 return fCurrentTime;
467 }
468
d7ee91bb
PT
469 /**
470 * @since 2.0
471 */
472 @Override
473 public TmfTimeRange getCurrentRange() {
474 return fCurrentRange;
475 }
476
477 /**
d7ee91bb
PT
478 * @since 2.0
479 */
66262ad8
BH
480 @Override
481 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
482 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
483 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
484 }
485
20658947 486 // ------------------------------------------------------------------------
d7ee91bb 487 // Convenience setters
20658947
FC
488 // ------------------------------------------------------------------------
489
0316808c
FC
490 /**
491 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 492 *
0316808c
FC
493 * @param cacheSize The trace cache size
494 */
495 protected void setCacheSize(final int cacheSize) {
496 fCacheSize = cacheSize;
497 }
498
499 /**
500 * Set the trace known number of events. This can be quite dynamic
501 * during indexing or for live traces.
0bfb7d06 502 *
0316808c
FC
503 * @param nbEvents The number of events
504 */
505 protected synchronized void setNbEvents(final long nbEvents) {
506 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
507 }
508
20658947
FC
509 /**
510 * Update the trace events time range
0bfb7d06 511 *
20658947 512 * @param range the new time range
3bd46eef 513 * @since 2.0
20658947
FC
514 */
515 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
516 fStartTime = range.getStartTime();
517 fEndTime = range.getEndTime();
20658947
FC
518 }
519
520 /**
521 * Update the trace chronologically first event timestamp
0bfb7d06 522 *
20658947 523 * @param startTime the new first event timestamp
3bd46eef 524 * @since 2.0
20658947
FC
525 */
526 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 527 fStartTime = startTime;
20658947
FC
528 }
529
530 /**
531 * Update the trace chronologically last event timestamp
0bfb7d06 532 *
20658947 533 * @param endTime the new last event timestamp
3bd46eef 534 * @since 2.0
20658947
FC
535 */
536 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 537 fEndTime = endTime;
20658947
FC
538 }
539
540 /**
0316808c 541 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 542 *
20658947
FC
543 * @param interval the new trace streaming interval
544 */
545 protected void setStreamingInterval(final long interval) {
1703b536 546 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
547 }
548
0316808c
FC
549 /**
550 * Set the trace indexer. Must be done at initialization time.
0bfb7d06 551 *
0316808c
FC
552 * @param indexer the trace indexer
553 */
6256d8ad 554 protected void setIndexer(final ITmfTraceIndexer indexer) {
0316808c
FC
555 fIndexer = indexer;
556 }
557
558 /**
559 * Set the trace parser. Must be done at initialization time.
0bfb7d06 560 *
0316808c
FC
561 * @param parser the new trace parser
562 */
6256d8ad 563 protected void setParser(final ITmfEventParser parser) {
0316808c
FC
564 fParser = parser;
565 }
566
09e86496 567 // ------------------------------------------------------------------------
7e6347b0 568 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
569 // ------------------------------------------------------------------------
570
1b70b6dc 571 @Override
7e6347b0 572 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 573
7e6347b0 574 // A rank <= 0 indicates to seek the first event
2352aed9 575 if (rank <= 0) {
1e1bef82 576 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
577 context.setRank(0);
578 return context;
579 }
09e86496 580
09e86496 581 // Position the trace at the checkpoint
7e6347b0 582 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
583
584 // And locate the requested event context
7e6347b0
FC
585 long pos = context.getRank();
586 if (pos < rank) {
c32744d6 587 ITmfEvent event = getNext(context);
0bfb7d06 588 while ((event != null) && (++pos < rank)) {
c32744d6 589 event = getNext(context);
7e6347b0 590 }
09e86496
FC
591 }
592 return context;
1b70b6dc
PT
593 }
594
3bd46eef
AM
595 /**
596 * @since 2.0
09e86496
FC
597 */
598 @Override
7e6347b0 599 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 600
7e6347b0 601 // A null timestamp indicates to seek the first event
2352aed9 602 if (timestamp == null) {
1e1bef82 603 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
604 context.setRank(0);
605 return context;
606 }
09e86496 607
1703b536 608 // Position the trace at the checkpoint
408e65d2 609 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
610
611 // And locate the requested event context
ea271da6
PT
612 ITmfLocation previousLocation = context.getLocation();
613 long previousRank = context.getRank();
614 ITmfEvent event = getNext(context);
7e6347b0 615 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
ea271da6
PT
616 previousLocation = context.getLocation();
617 previousRank = context.getRank();
618 event = getNext(context);
09e86496 619 }
0316808c
FC
620 if (event == null) {
621 context.setLocation(null);
622 context.setRank(ITmfContext.UNKNOWN_RANK);
ea271da6
PT
623 } else {
624 context.dispose();
625 context = seekEvent(previousLocation);
626 context.setRank(previousRank);
0316808c 627 }
09e86496
FC
628 return context;
629 }
0283f7ff 630
09e86496
FC
631 // ------------------------------------------------------------------------
632 // ITmfTrace - Read operations (returning an actual event)
633 // ------------------------------------------------------------------------
634
d4011df2 635 @Override
6256d8ad 636 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 637 // parseEvent() does not update the context
6256d8ad 638 final ITmfEvent event = fParser.parseEvent(context);
09e86496 639 if (event != null) {
d337369a 640 updateAttributes(context, event.getTimestamp());
09e86496
FC
641 context.setLocation(getCurrentLocation());
642 context.increaseRank();
643 processEvent(event);
644 }
645 return event;
646 }
647
648 /**
d337369a 649 * Hook for special event processing by the concrete class
7e6347b0 650 * (called by TmfTrace.getEvent())
0bfb7d06 651 *
d337369a 652 * @param event the event
09e86496
FC
653 */
654 protected void processEvent(final ITmfEvent event) {
d337369a 655 // Do nothing
09e86496
FC
656 }
657
d337369a
FC
658 /**
659 * Update the trace attributes
0bfb7d06 660 *
d337369a 661 * @param context the current trace context
2848c377 662 * @param timestamp the corresponding timestamp
3bd46eef 663 * @since 2.0
d337369a
FC
664 */
665 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
0bfb7d06 666 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
4593bd5b 667 fStartTime = timestamp;
09e86496 668 }
0bfb7d06 669 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
4593bd5b 670 fEndTime = timestamp;
09e86496 671 }
d7ee91bb
PT
672 if (fCurrentRange == TmfTimeRange.NULL_RANGE) {
673 fCurrentTime = timestamp;
674 ITmfTimestamp initialOffset = getInitialRangeOffset();
675 long endValue = timestamp.getValue() + initialOffset.normalize(0, timestamp.getScale()).getValue();
676 ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, timestamp.getScale());
677 fCurrentRange = new TmfTimeRange(timestamp, endTimestamp);
678 }
09e86496 679 if (context.hasValidRank()) {
d337369a 680 long rank = context.getRank();
09e86496
FC
681 if (fNbEvents <= rank) {
682 fNbEvents = rank + 1;
683 }
200789b3
AM
684 if (fIndexer != null) {
685 fIndexer.updateIndex(context, timestamp);
686 }
09e86496 687 }
abfad0aa
FC
688 }
689
3791b5df 690 // ------------------------------------------------------------------------
d337369a 691 // TmfDataProvider
3791b5df
FC
692 // ------------------------------------------------------------------------
693
77c4a6df
AM
694 /**
695 * @since 2.0
d337369a 696 */
3791b5df 697 @Override
5419a136 698 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
faa38350
PT
699 if (executorIsShutdown()) {
700 return null;
701 }
5419a136
AM
702 if ((request instanceof ITmfEventRequest)
703 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
704 && (request.getIndex() == 0))
705 {
706 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
707 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
8584dc20 708 return context;
8584dc20 709
5419a136
AM
710 }
711 return seekEvent(request.getIndex());
3791b5df
FC
712 }
713
faa38350
PT
714 // ------------------------------------------------------------------------
715 // Signal handlers
716 // ------------------------------------------------------------------------
717
718 /**
719 * Handler for the Trace Opened signal
720 *
721 * @param signal
722 * The incoming signal
723 * @since 2.0
724 */
725 @TmfSignalHandler
726 public void traceOpened(TmfTraceOpenedSignal signal) {
fe0c44c4
AM
727 ITmfTrace trace = null;
728 for (ITmfTrace expTrace : signal.getTrace().getTraces()) {
729 if (expTrace == this) {
730 trace = expTrace;
731 break;
faa38350
PT
732 }
733 }
faa38350 734
fe0c44c4
AM
735 if (trace == null) {
736 /* This signal is not for us */
737 return;
738 }
739
740 /*
741 * The signal is for this trace, or for an experiment containing
742 * this trace.
743 */
744 try {
745 buildStatistics();
746 buildStateSystem();
747 } catch (TmfTraceException e) {
748 e.printStackTrace();
749 }
750
751 /* Refresh the project, so it can pick up new files that got created. */
752 try {
753 if (fResource != null) {
754 fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
faa38350 755 }
fe0c44c4
AM
756 } catch (CoreException e) {
757 e.printStackTrace();
faa38350 758 }
fe0c44c4 759
faa38350 760 if (signal.getTrace() == this) {
fe0c44c4 761 /* Additionally, the signal is directly for this trace or experiment. */
faa38350
PT
762 if (getNbEvents() == 0) {
763 return;
764 }
765
766 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
767 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
768
769 // Broadcast in separate thread to prevent deadlock
770 new Thread() {
771 @Override
772 public void run() {
773 broadcast(rangeUpdatedsignal);
774 }
775 }.start();
776 return;
777 }
778 }
779
780 /**
781 * Signal handler for the TmfTraceRangeUpdatedSignal signal
782 *
783 * @param signal The incoming signal
784 * @since 2.0
785 */
786 @TmfSignalHandler
787 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
788 if (signal.getTrace() == this) {
789 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
790 }
791 }
792
d7ee91bb
PT
793 /**
794 * Signal handler for the TmfTimeSynchSignal signal
795 *
796 * @param signal The incoming signal
797 * @since 2.0
798 */
799 @TmfSignalHandler
800 public void synchToTime(final TmfTimeSynchSignal signal) {
801 if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
802 fCurrentTime = signal.getCurrentTime();
803 }
804 }
805
806 /**
807 * Signal handler for the TmfRangeSynchSignal signal
808 *
809 * @param signal The incoming signal
810 * @since 2.0
811 */
812 @TmfSignalHandler
813 public void synchToRange(final TmfRangeSynchSignal signal) {
814 if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
815 fCurrentTime = signal.getCurrentTime();
816 }
817 if (signal.getCurrentRange().getIntersection(getTimeRange()) != null) {
818 fCurrentRange = signal.getCurrentRange().getIntersection(getTimeRange());
819 }
820 }
821
3791b5df 822 // ------------------------------------------------------------------------
09e86496 823 // toString
3791b5df
FC
824 // ------------------------------------------------------------------------
825
12c155f5 826 @Override
09e86496 827 @SuppressWarnings("nls")
5419a136 828 public synchronized String toString() {
20658947
FC
829 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
830 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
831 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
832 }
833
8c8bf09f 834}
This page took 0.110233 seconds and 5 git commands to generate.