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