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