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