tmf: Replace TmfFilterEventTypeNode with TmfFilterTraceTypeNode
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
089a4872 2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
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
e73a4ba5
GB
13 * Geneviève Bastien - Added timestamp transforms, its saving to file and
14 * timestamp creation functions
8c8bf09f
ASL
15 *******************************************************************************/
16
2bdf0193 17package org.eclipse.tracecompass.tmf.core.trace;
8c8bf09f 18
5db5a3a4
AM
19import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
20
6f4a1d2b 21import java.io.File;
b04903a2 22import java.util.Collection;
35c160d9 23import java.util.Collections;
ff3f02c8 24import java.util.HashSet;
35c160d9 25import java.util.LinkedHashMap;
a51b2b9f 26import java.util.Map;
ff3f02c8 27import java.util.Set;
8c8bf09f 28
828e5592 29import org.eclipse.core.resources.IResource;
42459d24 30import org.eclipse.core.runtime.IStatus;
b22a582a 31import org.eclipse.core.runtime.MultiStatus;
032ecd45 32import org.eclipse.core.runtime.Path;
42459d24 33import org.eclipse.core.runtime.Status;
ba27dd38 34import org.eclipse.jdt.annotation.NonNull;
2b0005f0 35import org.eclipse.jdt.annotation.Nullable;
2bdf0193
AM
36import org.eclipse.tracecompass.internal.tmf.core.Activator;
37import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
38import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
39import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
40import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
41import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
b04903a2 42import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
2bdf0193
AM
43import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
44import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
45import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
46import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
47import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
48import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
49import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
50import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
51import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
52import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
53import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
54import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
55import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
56import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
57import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
58import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
59import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
8c8bf09f 60
b04903a2
AM
61import com.google.common.collect.ImmutableList;
62
8c8bf09f 63/**
09e86496
FC
64 * Abstract implementation of ITmfTrace.
65 * <p>
13cb5f43
FC
66 * Since the concept of 'location' is trace specific, the concrete classes have
67 * to provide the related methods, namely:
68 * <ul>
69 * <li> public ITmfLocation<?> getCurrentLocation()
70 * <li> public double getLocationRatio(ITmfLocation<?> location)
71 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
72 * <li> public ITmfContext seekEvent(double ratio)
da1a4b39 73 * <li> public IStatus validate(IProject project, String path)
13cb5f43 74 * </ul>
13cb5f43 75 * <p>
6b44794a
MK
76 * When constructing an event, the concrete trace should use the trace's
77 * timestamp transform to create the timestamp, by either transforming the
78 * parsed time value directly or by using the method createTimestamp().
79 * <p>
13cb5f43
FC
80 * The concrete class can either specify its own indexer or use the provided
81 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
82 * used as checkpoint interval.
0bfb7d06 83 *
f7703ed6
FC
84 * @version 1.0
85 * @author Francois Chouinard
86 *
f7703ed6
FC
87 * @see ITmfEvent
88 * @see ITmfTraceIndexer
89 * @see ITmfEventParser
8c8bf09f 90 */
5733be39 91public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace, ITmfEventParser, ITmfTraceCompleteness {
62d1696a 92
e31e01e8 93 // ------------------------------------------------------------------------
b04903a2
AM
94 // Class attributes
95 // ------------------------------------------------------------------------
96
97 /**
98 * Basic aspects that should be valid for all trace types.
99 */
8192209b 100 public static final @NonNull Collection<ITmfEventAspect> BASE_ASPECTS =
5db5a3a4 101 checkNotNull(ImmutableList.of(
b04903a2
AM
102 ITmfEventAspect.BaseAspects.TIMESTAMP,
103 ITmfEventAspect.BaseAspects.EVENT_TYPE,
104 ITmfEventAspect.BaseAspects.CONTENTS
5db5a3a4 105 ));
b04903a2
AM
106
107 // ------------------------------------------------------------------------
108 // Instance attributes
e31e01e8 109 // ------------------------------------------------------------------------
8c8bf09f 110
09e86496
FC
111 // The resource used for persistent properties for this trace
112 private IResource fResource;
113
2b0005f0
PT
114 // The trace type id
115 private @Nullable String fTraceTypeId;
116
b0a282fb 117 // The trace path
12c155f5 118 private String fPath;
b0a282fb 119
0316808c
FC
120 // The trace cache page size
121 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
62d1696a 122
0316808c 123 // The number of events collected (so far)
e9a6e38e 124 private volatile long fNbEvents = 0;
62d1696a
FC
125
126 // The time span of the event stream
9cbe7899 127 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
a4115405 128 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
62d1696a 129
0316808c
FC
130 // The trace streaming interval (0 = no streaming)
131 private long fStreamingInterval = 0;
085d898f 132
0316808c 133 // The trace indexer
6256d8ad 134 private ITmfTraceIndexer fIndexer;
20658947 135
e73a4ba5
GB
136 private ITmfTimestampTransform fTsTransform;
137
ff3f02c8
AM
138 private final Map<String, IAnalysisModule> fAnalysisModules =
139 Collections.synchronizedMap(new LinkedHashMap<String, IAnalysisModule>());
c068a752 140
e31e01e8 141 // ------------------------------------------------------------------------
3791b5df 142 // Construction
e31e01e8 143 // ------------------------------------------------------------------------
8c8bf09f 144
62d1696a 145 /**
3791b5df 146 * The default, parameterless, constructor
62d1696a 147 */
3791b5df
FC
148 public TmfTrace() {
149 super();
ab186fbb 150 fIndexer = createIndexer(DEFAULT_BLOCK_SIZE);
05bd3318
FC
151 }
152
153 /**
8cf330ae 154 * Full constructor.
0bfb7d06 155 *
8cf330ae
AM
156 * @param resource
157 * The resource associated to the trace
158 * @param type
159 * The type of events that will be read from this trace
160 * @param path
161 * The path to the trace on the filesystem
162 * @param cacheSize
163 * The trace cache size. Pass '-1' to use the default specified
164 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
165 * @param interval
166 * The trace streaming interval. You can use '0' for post-mortem
167 * traces.
8cf330ae
AM
168 * @throws TmfTraceException
169 * If something failed during the opening
20658947 170 */
8cf330ae
AM
171 protected TmfTrace(final IResource resource,
172 final Class<? extends ITmfEvent> type,
173 final String path,
174 final int cacheSize,
5733be39
AM
175 final long interval)
176 throws TmfTraceException {
00641a97 177 super();
0316808c 178 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
3791b5df 179 fStreamingInterval = interval;
09e86496 180 initialize(resource, path, type);
8c8bf09f
ASL
181 }
182
3791b5df
FC
183 /**
184 * Copy constructor
0bfb7d06 185 *
3791b5df 186 * @param trace the original trace
063f0d27 187 * @throws TmfTraceException Should not happen usually
3791b5df 188 */
6256d8ad 189 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
3791b5df 190 super();
0316808c 191 if (trace == null) {
3791b5df 192 throw new IllegalArgumentException();
0316808c 193 }
20658947
FC
194 fCacheSize = trace.getCacheSize();
195 fStreamingInterval = trace.getStreamingInterval();
13cb5f43 196 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
3791b5df
FC
197 }
198
032ecd45
MAL
199 /**
200 * Creates the indexer instance. Classes extending this class can override
201 * this to provide a different indexer implementation.
202 *
203 * @param interval the checkpoints interval
204 *
205 * @return the indexer
206 * @since 3.0
207 */
208 protected ITmfTraceIndexer createIndexer(int interval) {
209 return new TmfCheckpointIndexer(this, interval);
210 }
211
7e6347b0
FC
212 // ------------------------------------------------------------------------
213 // ITmfTrace - Initializers
214 // ------------------------------------------------------------------------
215
339d539c 216 @Override
2b0005f0 217 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type, String name, String traceTypeId) throws TmfTraceException {
6d8922ce
GB
218 if (name == null) {
219 throw new IllegalArgumentException();
220 }
339d539c 221 setName(name);
2b0005f0 222 fTraceTypeId = traceTypeId;
339d539c
PT
223 initTrace(resource, path, type);
224 }
225
7e6347b0 226 @Override
6256d8ad 227 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
7e6347b0 228 initialize(resource, path, type);
7e6347b0
FC
229 }
230
09e86496 231 /**
1703b536 232 * Initialize the trace common attributes and the base component.
0bfb7d06
MK
233 *
234 * @param resource the Eclipse resource (trace)
1703b536
FC
235 * @param path the trace path
236 * @param type the trace event type
0bfb7d06 237 *
6f4e8ec0 238 * @throws TmfTraceException If something failed during the initialization
3791b5df 239 */
248af329
AM
240 protected void initialize(final IResource resource,
241 final String path,
242 final Class<? extends ITmfEvent> type)
243 throws TmfTraceException {
0316808c 244 if (path == null) {
b4f71e4a 245 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
0316808c 246 }
3791b5df 247 fPath = path;
1703b536 248 fResource = resource;
339d539c 249 String traceName = getName();
d40ddf8a 250 if (traceName.isEmpty()) {
339d539c 251 traceName = (resource != null) ? resource.getName() : new Path(path).lastSegment();
1703b536 252 }
3791b5df 253 super.init(traceName, type);
fec1ac0b
BH
254 // register as VIP after super.init() because TmfComponent registers to signal manager there
255 TmfSignalManager.registerVIP(this);
1a3f1ec3
GB
256 if (fIndexer != null) {
257 fIndexer.dispose();
258 }
ab186fbb 259 fIndexer = createIndexer(fCacheSize);
3791b5df
FC
260 }
261
2352aed9
FC
262 /**
263 * Indicates if the path points to an existing file/directory
0bfb7d06 264 *
2352aed9
FC
265 * @param path the path to test
266 * @return true if the file/directory exists
3791b5df 267 */
2352aed9 268 protected boolean fileExists(final String path) {
085d898f 269 final File file = new File(path);
3791b5df
FC
270 return file.exists();
271 }
272
c7e1020d 273 /**
51e75066 274 * @since 2.0
c7e1020d 275 */
51e75066
AM
276 @Override
277 public void indexTrace(boolean waitForCompletion) {
9e0640dc 278 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
c7e1020d
FC
279 }
280
c068a752
GB
281 /**
282 * Instantiate the applicable analysis modules and executes the analysis
283 * modules that are meant to be automatically executed
284 *
285 * @return An IStatus indicating whether the analysis could be run
286 * successfully or not
287 * @since 3.0
288 */
289 protected IStatus executeAnalysis() {
290 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
ba27dd38 291
5db5a3a4 292 Class<? extends TmfTrace> className = checkNotNull(this.getClass());
ba27dd38 293 Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(className);
c068a752
GB
294 for (IAnalysisModuleHelper helper : modules.values()) {
295 try {
296 IAnalysisModule module = helper.newModule(this);
297 fAnalysisModules.put(module.getId(), module);
298 if (module.isAutomatic()) {
299 status.add(module.schedule());
300 }
301 } catch (TmfAnalysisException e) {
26683871 302 status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage()));
c068a752
GB
303 }
304 }
305 return status;
306 }
307
c4767854
AM
308 /**
309 * @since 3.0
310 */
c068a752 311 @Override
ff3f02c8 312 public IAnalysisModule getAnalysisModule(String analysisId) {
c068a752
GB
313 return fAnalysisModules.get(analysisId);
314 }
315
ff3f02c8
AM
316
317 /**
318 * @since 3.0
319 */
320 @Override
321 public Iterable<IAnalysisModule> getAnalysisModules() {
322 synchronized (fAnalysisModules) {
323 Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values());
324 return modules;
325 }
326 }
327
b04903a2
AM
328 @Override
329 public Iterable<ITmfEventAspect> getEventAspects() {
330 /* By default we provide only the base aspects valid for all trace types */
331 return BASE_ASPECTS;
332 }
333
b5ee6881
FC
334 /**
335 * Clears the trace
336 */
337 @Override
338 public synchronized void dispose() {
1a4205d9 339 /* Clean up the index if applicable */
77551cc2
FC
340 if (getIndexer() != null) {
341 getIndexer().dispose();
342 }
1a4205d9 343
a1529f38 344 /* Clean up the analysis modules */
ff3f02c8
AM
345 synchronized (fAnalysisModules) {
346 for (IAnalysisModule module : fAnalysisModules.values()) {
347 module.dispose();
348 }
6ef5ae35 349 fAnalysisModules.clear();
a1529f38
AM
350 }
351
b5ee6881
FC
352 super.dispose();
353 }
354
3791b5df 355 // ------------------------------------------------------------------------
09e86496 356 // ITmfTrace - Basic getters
e31e01e8 357 // ------------------------------------------------------------------------
8c8bf09f 358
25e48683 359 @Override
0f89d4ba
AM
360 public Class<? extends ITmfEvent> getEventType() {
361 return super.getType();
25e48683
FC
362 }
363
d4011df2 364 @Override
09e86496
FC
365 public IResource getResource() {
366 return fResource;
8c8bf09f
ASL
367 }
368
2b0005f0
PT
369 @Override
370 public @Nullable String getTraceTypeId() {
371 return fTraceTypeId;
372 }
373
d4011df2 374 @Override
09e86496
FC
375 public String getPath() {
376 return fPath;
8c8bf09f
ASL
377 }
378
20658947
FC
379 @Override
380 public int getCacheSize() {
381 return fCacheSize;
382 }
383
20658947
FC
384 @Override
385 public long getStreamingInterval() {
386 return fStreamingInterval;
387 }
388
0316808c
FC
389 /**
390 * @return the trace indexer
a3db8436 391 * @since 3.0
0316808c 392 */
6256d8ad 393 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
394 return fIndexer;
395 }
396
09e86496
FC
397 // ------------------------------------------------------------------------
398 // ITmfTrace - Trace characteristics getters
399 // ------------------------------------------------------------------------
400
d4011df2 401 @Override
e9a6e38e 402 public long getNbEvents() {
3791b5df 403 return fNbEvents;
b0a282fb
FC
404 }
405
3bd46eef
AM
406 /**
407 * @since 2.0
62d1696a 408 */
d4011df2 409 @Override
12c155f5 410 public TmfTimeRange getTimeRange() {
cb866e08 411 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
412 }
413
3bd46eef
AM
414 /**
415 * @since 2.0
e31e01e8 416 */
d4011df2 417 @Override
4df4581d 418 public ITmfTimestamp getStartTime() {
4593bd5b 419 return fStartTime;
146a887c
FC
420 }
421
3bd46eef
AM
422 /**
423 * @since 2.0
e31e01e8 424 */
d4011df2 425 @Override
4df4581d 426 public ITmfTimestamp getEndTime() {
4593bd5b 427 return fEndTime;
20658947
FC
428 }
429
d7ee91bb 430 /**
d7ee91bb
PT
431 * @since 2.0
432 */
66262ad8
BH
433 @Override
434 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
435 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
436 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
437 }
438
bb52f9bc
GB
439 /**
440 * @since 3.0
441 */
442 @Override
443 public String getHostId() {
444 return this.getName();
445 }
446
20658947 447 // ------------------------------------------------------------------------
d7ee91bb 448 // Convenience setters
20658947
FC
449 // ------------------------------------------------------------------------
450
0316808c
FC
451 /**
452 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 453 *
0316808c
FC
454 * @param cacheSize The trace cache size
455 */
456 protected void setCacheSize(final int cacheSize) {
457 fCacheSize = cacheSize;
458 }
459
460 /**
461 * Set the trace known number of events. This can be quite dynamic
462 * during indexing or for live traces.
0bfb7d06 463 *
0316808c
FC
464 * @param nbEvents The number of events
465 */
466 protected synchronized void setNbEvents(final long nbEvents) {
467 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
468 }
469
20658947
FC
470 /**
471 * Update the trace events time range
0bfb7d06 472 *
20658947 473 * @param range the new time range
3bd46eef 474 * @since 2.0
20658947
FC
475 */
476 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
477 fStartTime = range.getStartTime();
478 fEndTime = range.getEndTime();
20658947
FC
479 }
480
481 /**
482 * Update the trace chronologically first event timestamp
0bfb7d06 483 *
20658947 484 * @param startTime the new first event timestamp
3bd46eef 485 * @since 2.0
20658947
FC
486 */
487 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 488 fStartTime = startTime;
20658947
FC
489 }
490
491 /**
492 * Update the trace chronologically last event timestamp
0bfb7d06 493 *
20658947 494 * @param endTime the new last event timestamp
3bd46eef 495 * @since 2.0
20658947
FC
496 */
497 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 498 fEndTime = endTime;
20658947
FC
499 }
500
501 /**
0316808c 502 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 503 *
20658947
FC
504 * @param interval the new trace streaming interval
505 */
506 protected void setStreamingInterval(final long interval) {
1703b536 507 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
508 }
509
09e86496 510 // ------------------------------------------------------------------------
7e6347b0 511 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
512 // ------------------------------------------------------------------------
513
1b70b6dc 514 @Override
7e6347b0 515 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 516
7e6347b0 517 // A rank <= 0 indicates to seek the first event
2352aed9 518 if (rank <= 0) {
1e1bef82 519 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
520 context.setRank(0);
521 return context;
522 }
09e86496 523
09e86496 524 // Position the trace at the checkpoint
7e6347b0 525 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
526
527 // And locate the requested event context
7e6347b0
FC
528 long pos = context.getRank();
529 if (pos < rank) {
c32744d6 530 ITmfEvent event = getNext(context);
0bfb7d06 531 while ((event != null) && (++pos < rank)) {
c32744d6 532 event = getNext(context);
7e6347b0 533 }
09e86496
FC
534 }
535 return context;
1b70b6dc
PT
536 }
537
3bd46eef
AM
538 /**
539 * @since 2.0
09e86496
FC
540 */
541 @Override
7e6347b0 542 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 543
7e6347b0 544 // A null timestamp indicates to seek the first event
2352aed9 545 if (timestamp == null) {
1e1bef82 546 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
547 context.setRank(0);
548 return context;
549 }
09e86496 550
1703b536 551 // Position the trace at the checkpoint
408e65d2 552 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
553
554 // And locate the requested event context
ea271da6
PT
555 ITmfLocation previousLocation = context.getLocation();
556 long previousRank = context.getRank();
557 ITmfEvent event = getNext(context);
065cc19b 558 while (event != null && event.getTimestamp().compareTo(timestamp) < 0) {
ea271da6
PT
559 previousLocation = context.getLocation();
560 previousRank = context.getRank();
561 event = getNext(context);
09e86496 562 }
0316808c
FC
563 if (event == null) {
564 context.setLocation(null);
565 context.setRank(ITmfContext.UNKNOWN_RANK);
ea271da6
PT
566 } else {
567 context.dispose();
568 context = seekEvent(previousLocation);
569 context.setRank(previousRank);
0316808c 570 }
09e86496
FC
571 return context;
572 }
0283f7ff 573
09e86496 574 // ------------------------------------------------------------------------
5733be39 575 // Read operations (returning an actual event)
09e86496
FC
576 // ------------------------------------------------------------------------
577
5733be39
AM
578 @Override
579 public abstract ITmfEvent parseEvent(ITmfContext context);
580
d4011df2 581 @Override
6256d8ad 582 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 583 // parseEvent() does not update the context
5733be39 584 final ITmfEvent event = parseEvent(context);
09e86496 585 if (event != null) {
d337369a 586 updateAttributes(context, event.getTimestamp());
09e86496
FC
587 context.setLocation(getCurrentLocation());
588 context.increaseRank();
09e86496
FC
589 }
590 return event;
591 }
592
d337369a
FC
593 /**
594 * Update the trace attributes
0bfb7d06 595 *
d337369a 596 * @param context the current trace context
2848c377 597 * @param timestamp the corresponding timestamp
3bd46eef 598 * @since 2.0
d337369a
FC
599 */
600 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
065cc19b 601 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp) > 0)) {
4593bd5b 602 fStartTime = timestamp;
09e86496 603 }
065cc19b 604 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp) < 0)) {
4593bd5b 605 fEndTime = timestamp;
09e86496
FC
606 }
607 if (context.hasValidRank()) {
d337369a 608 long rank = context.getRank();
09e86496
FC
609 if (fNbEvents <= rank) {
610 fNbEvents = rank + 1;
611 }
200789b3
AM
612 if (fIndexer != null) {
613 fIndexer.updateIndex(context, timestamp);
614 }
09e86496 615 }
abfad0aa
FC
616 }
617
3791b5df 618 // ------------------------------------------------------------------------
d337369a 619 // TmfDataProvider
3791b5df
FC
620 // ------------------------------------------------------------------------
621
77c4a6df
AM
622 /**
623 * @since 2.0
d337369a 624 */
3791b5df 625 @Override
fd3f1eff 626 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
faa38350
PT
627 if (executorIsShutdown()) {
628 return null;
629 }
fd3f1eff
AM
630 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
631 && (request.getIndex() == 0)) {
632 final ITmfContext context = seekEvent(request.getRange().getStartTime());
633 request.setStartIndex((int) context.getRank());
8584dc20 634 return context;
8584dc20 635
5419a136
AM
636 }
637 return seekEvent(request.getIndex());
3791b5df
FC
638 }
639
faa38350
PT
640 // ------------------------------------------------------------------------
641 // Signal handlers
642 // ------------------------------------------------------------------------
643
644 /**
645 * Handler for the Trace Opened signal
646 *
647 * @param signal
648 * The incoming signal
649 * @since 2.0
650 */
651 @TmfSignalHandler
652 public void traceOpened(TmfTraceOpenedSignal signal) {
b9a5bf8f
AM
653 boolean signalIsForUs = false;
654 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
655 if (trace == this) {
656 signalIsForUs = true;
fe0c44c4 657 break;
faa38350
PT
658 }
659 }
faa38350 660
b9a5bf8f 661 if (!signalIsForUs) {
fe0c44c4
AM
662 return;
663 }
664
665 /*
b9a5bf8f 666 * The signal is either for this trace, or for an experiment containing
fe0c44c4
AM
667 * this trace.
668 */
be4a197a 669 IStatus status = executeAnalysis();
b22a582a
AM
670 if (!status.isOK()) {
671 Activator.log(status);
fe0c44c4
AM
672 }
673
b5e8ee95 674 TmfTraceManager.refreshSupplementaryFiles(this);
fe0c44c4 675
faa38350 676 if (signal.getTrace() == this) {
f8fc4a3a 677 /* Additionally, the signal is directly for this trace. */
faa38350
PT
678 if (getNbEvents() == 0) {
679 return;
680 }
681
f8fc4a3a
PT
682 /* For a streaming trace, the range updated signal should be sent
683 * by the subclass when a new safe time is determined.
684 */
685 if (getStreamingInterval() > 0) {
686 return;
687 }
688
6fd3c6e9
MAL
689 if (isComplete()) {
690 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
691 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
faa38350 692
6fd3c6e9
MAL
693 // Broadcast in separate thread to prevent deadlock
694 broadcastAsync(rangeUpdatedsignal);
695 }
faa38350
PT
696 return;
697 }
698 }
699
700 /**
701 * Signal handler for the TmfTraceRangeUpdatedSignal signal
702 *
703 * @param signal The incoming signal
704 * @since 2.0
705 */
706 @TmfSignalHandler
707 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
708 if (signal.getTrace() == this) {
709 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
710 }
711 }
712
032ecd45
MAL
713 /**
714 * Signal handler for the TmfTraceUpdatedSignal signal
715 *
716 * @param signal The incoming signal
c4767854 717 * @since 3.0
032ecd45
MAL
718 */
719 @TmfSignalHandler
720 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
721 if (signal.getSource() == getIndexer()) {
722 fNbEvents = signal.getNbEvents();
723 fStartTime = signal.getRange().getStartTime();
724 fEndTime = signal.getRange().getEndTime();
725 }
726 }
727
e73a4ba5
GB
728 // ------------------------------------------------------------------------
729 // Timestamp transformation functions
730 // ------------------------------------------------------------------------
731
732 /**
733 * @since 3.0
734 */
735 @Override
736 public ITmfTimestampTransform getTimestampTransform() {
737 if (fTsTransform == null) {
6b44794a 738 fTsTransform = TimestampTransformFactory.getTimestampTransform(getResource());
e73a4ba5
GB
739 }
740 return fTsTransform;
741 }
742
743 /**
744 * @since 3.0
745 */
746 @Override
747 public void setTimestampTransform(final ITmfTimestampTransform tt) {
748 fTsTransform = tt;
6b44794a 749 TimestampTransformFactory.setTimestampTransform(getResource(), tt);
e73a4ba5
GB
750 }
751
752 /**
753 * @since 3.0
754 */
755 @Override
756 public ITmfTimestamp createTimestamp(long ts) {
b2c463c5 757 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
e73a4ba5
GB
758 }
759
3791b5df 760 // ------------------------------------------------------------------------
09e86496 761 // toString
3791b5df
FC
762 // ------------------------------------------------------------------------
763
12c155f5 764 @Override
09e86496 765 @SuppressWarnings("nls")
5419a136 766 public synchronized String toString() {
20658947
FC
767 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
768 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
769 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
770 }
771
6fd3c6e9
MAL
772 /**
773 * @since 3.1
774 */
775 @Override
776 public boolean isComplete() {
777 /*
778 * Be default, all traces are "complete" which means no more data will
779 * be added later
780 */
781 return true;
782 }
783
784 /**
785 * @since 3.1
786 */
787 @Override
788 public void setComplete(boolean isComplete) {
789 /*
790 * This should be overridden by trace classes that can support live
791 * reading (traces in an incomplete state)
792 */
793 }
8c8bf09f 794}
This page took 0.158285 seconds and 5 git commands to generate.