bafae0346d87d07d373bd60d2bc5cd1842def6bb
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 * Patrick Tasse - Updated for removal of context clone
13 * Geneviève Bastien - Added timestamp transforms, its saving to file and
14 * timestamp creation functions
15 *******************************************************************************/
16
17 package org.eclipse.tracecompass.tmf.core.trace;
18
19 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
20
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.MultiStatus;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.jdt.annotation.NonNull;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.eclipse.tracecompass.internal.tmf.core.Activator;
37 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
38 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
39 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
40 import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
41 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
42 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
43 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
44 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
45 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
46 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
47 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
48 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
49 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
50 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
51 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
52 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
53 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
54 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
55 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
56 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
57 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
58 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
59 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
60
61 import com.google.common.collect.ImmutableList;
62
63 /**
64 * Abstract implementation of ITmfTrace.
65 * <p>
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)
73 * <li> public IStatus validate(IProject project, String path)
74 * </ul>
75 * <p>
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>
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.
83 *
84 * @version 1.0
85 * @author Francois Chouinard
86 *
87 * @see ITmfEvent
88 * @see ITmfTraceIndexer
89 * @see ITmfEventParser
90 */
91 public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace, ITmfEventParser, ITmfTraceCompleteness {
92
93 // ------------------------------------------------------------------------
94 // Class attributes
95 // ------------------------------------------------------------------------
96
97 /**
98 * Basic aspects that should be valid for all trace types.
99 */
100 public static final @NonNull Collection<ITmfEventAspect> BASE_ASPECTS =
101 checkNotNull(ImmutableList.of(
102 ITmfEventAspect.BaseAspects.TIMESTAMP,
103 ITmfEventAspect.BaseAspects.EVENT_TYPE,
104 ITmfEventAspect.BaseAspects.CONTENTS
105 ));
106
107 // ------------------------------------------------------------------------
108 // Instance attributes
109 // ------------------------------------------------------------------------
110
111 // The resource used for persistent properties for this trace
112 private IResource fResource;
113
114 // The trace type id
115 private @Nullable String fTraceTypeId;
116
117 // The trace path
118 private String fPath;
119
120 // The trace cache page size
121 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
122
123 // The number of events collected (so far)
124 private volatile long fNbEvents = 0;
125
126 // The time span of the event stream
127 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
128 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
129
130 // The trace streaming interval (0 = no streaming)
131 private long fStreamingInterval = 0;
132
133 // The trace indexer
134 private ITmfTraceIndexer fIndexer;
135
136 private ITmfTimestampTransform fTsTransform;
137
138 private final Map<String, IAnalysisModule> fAnalysisModules =
139 Collections.synchronizedMap(new LinkedHashMap<String, IAnalysisModule>());
140
141 // ------------------------------------------------------------------------
142 // Construction
143 // ------------------------------------------------------------------------
144
145 /**
146 * The default, parameterless, constructor
147 */
148 public TmfTrace() {
149 super();
150 fIndexer = createIndexer(DEFAULT_BLOCK_SIZE);
151 }
152
153 /**
154 * Full constructor.
155 *
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.
168 * @throws TmfTraceException
169 * If something failed during the opening
170 */
171 protected TmfTrace(final IResource resource,
172 final Class<? extends ITmfEvent> type,
173 final String path,
174 final int cacheSize,
175 final long interval)
176 throws TmfTraceException {
177 super();
178 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
179 fStreamingInterval = interval;
180 initialize(resource, path, type);
181 }
182
183 /**
184 * Copy constructor
185 *
186 * @param trace the original trace
187 * @throws TmfTraceException Should not happen usually
188 */
189 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
190 super();
191 if (trace == null) {
192 throw new IllegalArgumentException();
193 }
194 fCacheSize = trace.getCacheSize();
195 fStreamingInterval = trace.getStreamingInterval();
196 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
197 }
198
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 */
207 protected ITmfTraceIndexer createIndexer(int interval) {
208 return new TmfCheckpointIndexer(this, interval);
209 }
210
211 // ------------------------------------------------------------------------
212 // ITmfTrace - Initializers
213 // ------------------------------------------------------------------------
214
215 @Override
216 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type, String name, String traceTypeId) throws TmfTraceException {
217 if (name == null) {
218 throw new IllegalArgumentException();
219 }
220 setName(name);
221 fTraceTypeId = traceTypeId;
222 initTrace(resource, path, type);
223 }
224
225 @Override
226 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
227 initialize(resource, path, type);
228 }
229
230 /**
231 * Initialize the trace common attributes and the base component.
232 *
233 * @param resource the Eclipse resource (trace)
234 * @param path the trace path
235 * @param type the trace event type
236 *
237 * @throws TmfTraceException If something failed during the initialization
238 */
239 protected void initialize(final IResource resource,
240 final String path,
241 final Class<? extends ITmfEvent> type)
242 throws TmfTraceException {
243 if (path == null) {
244 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
245 }
246 fPath = path;
247 fResource = resource;
248 String traceName = getName();
249 if (traceName.isEmpty()) {
250 traceName = (resource != null) ? resource.getName() : new Path(path).lastSegment();
251 }
252 super.init(traceName, type);
253 // register as VIP after super.init() because TmfComponent registers to signal manager there
254 TmfSignalManager.registerVIP(this);
255 if (fIndexer != null) {
256 fIndexer.dispose();
257 }
258 fIndexer = createIndexer(fCacheSize);
259 }
260
261 /**
262 * Indicates if the path points to an existing file/directory
263 *
264 * @param path the path to test
265 * @return true if the file/directory exists
266 */
267 protected boolean fileExists(final String path) {
268 final File file = new File(path);
269 return file.exists();
270 }
271
272 @Override
273 public void indexTrace(boolean waitForCompletion) {
274 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
275 }
276
277 /**
278 * Instantiate the applicable analysis modules and executes the analysis
279 * modules that are meant to be automatically executed
280 *
281 * @return An IStatus indicating whether the analysis could be run
282 * successfully or not
283 */
284 protected IStatus executeAnalysis() {
285 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
286
287 Class<? extends TmfTrace> className = checkNotNull(this.getClass());
288 Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(className);
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) {
297 status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage()));
298 }
299 }
300 return status;
301 }
302
303 @Override
304 public IAnalysisModule getAnalysisModule(String analysisId) {
305 return fAnalysisModules.get(analysisId);
306 }
307
308
309 @Override
310 public Iterable<IAnalysisModule> getAnalysisModules() {
311 synchronized (fAnalysisModules) {
312 Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values());
313 return modules;
314 }
315 }
316
317 @Override
318 public Iterable<ITmfEventAspect> getEventAspects() {
319 /* By default we provide only the base aspects valid for all trace types */
320 return BASE_ASPECTS;
321 }
322
323 /**
324 * Clears the trace
325 */
326 @Override
327 public synchronized void dispose() {
328 /* Clean up the index if applicable */
329 if (getIndexer() != null) {
330 getIndexer().dispose();
331 }
332
333 /* Clean up the analysis modules */
334 synchronized (fAnalysisModules) {
335 for (IAnalysisModule module : fAnalysisModules.values()) {
336 module.dispose();
337 }
338 fAnalysisModules.clear();
339 }
340
341 super.dispose();
342 }
343
344 // ------------------------------------------------------------------------
345 // ITmfTrace - Basic getters
346 // ------------------------------------------------------------------------
347
348 @Override
349 public Class<? extends ITmfEvent> getEventType() {
350 return super.getType();
351 }
352
353 @Override
354 public IResource getResource() {
355 return fResource;
356 }
357
358 @Override
359 public @Nullable String getTraceTypeId() {
360 return fTraceTypeId;
361 }
362
363 @Override
364 public String getPath() {
365 return fPath;
366 }
367
368 @Override
369 public int getCacheSize() {
370 return fCacheSize;
371 }
372
373 @Override
374 public long getStreamingInterval() {
375 return fStreamingInterval;
376 }
377
378 /**
379 * @return the trace indexer
380 */
381 protected ITmfTraceIndexer getIndexer() {
382 return fIndexer;
383 }
384
385 // ------------------------------------------------------------------------
386 // ITmfTrace - Trace characteristics getters
387 // ------------------------------------------------------------------------
388
389 @Override
390 public long getNbEvents() {
391 return fNbEvents;
392 }
393
394 @Override
395 public TmfTimeRange getTimeRange() {
396 return new TmfTimeRange(fStartTime, fEndTime);
397 }
398
399 @Override
400 public ITmfTimestamp getStartTime() {
401 return fStartTime;
402 }
403
404 @Override
405 public ITmfTimestamp getEndTime() {
406 return fEndTime;
407 }
408
409 @Override
410 public ITmfTimestamp getInitialRangeOffset() {
411 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
412 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
413 }
414
415 @Override
416 public String getHostId() {
417 return this.getName();
418 }
419
420 // ------------------------------------------------------------------------
421 // Convenience setters
422 // ------------------------------------------------------------------------
423
424 /**
425 * Set the trace cache size. Must be done at initialization time.
426 *
427 * @param cacheSize The trace cache size
428 */
429 protected void setCacheSize(final int cacheSize) {
430 fCacheSize = cacheSize;
431 }
432
433 /**
434 * Set the trace known number of events. This can be quite dynamic
435 * during indexing or for live traces.
436 *
437 * @param nbEvents The number of events
438 */
439 protected synchronized void setNbEvents(final long nbEvents) {
440 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
441 }
442
443 /**
444 * Update the trace events time range
445 *
446 * @param range the new time range
447 */
448 protected void setTimeRange(final TmfTimeRange range) {
449 fStartTime = range.getStartTime();
450 fEndTime = range.getEndTime();
451 }
452
453 /**
454 * Update the trace chronologically first event timestamp
455 *
456 * @param startTime the new first event timestamp
457 */
458 protected void setStartTime(final ITmfTimestamp startTime) {
459 fStartTime = startTime;
460 }
461
462 /**
463 * Update the trace chronologically last event timestamp
464 *
465 * @param endTime the new last event timestamp
466 */
467 protected void setEndTime(final ITmfTimestamp endTime) {
468 fEndTime = endTime;
469 }
470
471 /**
472 * Set the polling interval for live traces (default = 0 = no streaming).
473 *
474 * @param interval the new trace streaming interval
475 */
476 protected void setStreamingInterval(final long interval) {
477 fStreamingInterval = (interval > 0) ? interval : 0;
478 }
479
480 // ------------------------------------------------------------------------
481 // ITmfTrace - SeekEvent operations (returning a trace context)
482 // ------------------------------------------------------------------------
483
484 @Override
485 public synchronized ITmfContext seekEvent(final long rank) {
486
487 // A rank <= 0 indicates to seek the first event
488 if (rank <= 0) {
489 ITmfContext context = seekEvent((ITmfLocation) null);
490 context.setRank(0);
491 return context;
492 }
493
494 // Position the trace at the checkpoint
495 final ITmfContext context = fIndexer.seekIndex(rank);
496
497 // And locate the requested event context
498 long pos = context.getRank();
499 if (pos < rank) {
500 ITmfEvent event = getNext(context);
501 while ((event != null) && (++pos < rank)) {
502 event = getNext(context);
503 }
504 }
505 return context;
506 }
507
508 @Override
509 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
510
511 // A null timestamp indicates to seek the first event
512 if (timestamp == null) {
513 ITmfContext context = seekEvent((ITmfLocation) null);
514 context.setRank(0);
515 return context;
516 }
517
518 // Position the trace at the checkpoint
519 ITmfContext context = fIndexer.seekIndex(timestamp);
520
521 // And locate the requested event context
522 ITmfLocation previousLocation = context.getLocation();
523 long previousRank = context.getRank();
524 ITmfEvent event = getNext(context);
525 while (event != null && event.getTimestamp().compareTo(timestamp) < 0) {
526 previousLocation = context.getLocation();
527 previousRank = context.getRank();
528 event = getNext(context);
529 }
530 if (event == null) {
531 context.setLocation(null);
532 context.setRank(ITmfContext.UNKNOWN_RANK);
533 } else {
534 context.dispose();
535 context = seekEvent(previousLocation);
536 context.setRank(previousRank);
537 }
538 return context;
539 }
540
541 // ------------------------------------------------------------------------
542 // Read operations (returning an actual event)
543 // ------------------------------------------------------------------------
544
545 @Override
546 public abstract ITmfEvent parseEvent(ITmfContext context);
547
548 @Override
549 public synchronized ITmfEvent getNext(final ITmfContext context) {
550 // parseEvent() does not update the context
551 final ITmfEvent event = parseEvent(context);
552 if (event != null) {
553 updateAttributes(context, event.getTimestamp());
554 context.setLocation(getCurrentLocation());
555 context.increaseRank();
556 }
557 return event;
558 }
559
560 /**
561 * Update the trace attributes
562 *
563 * @param context the current trace context
564 * @param timestamp the corresponding timestamp
565 */
566 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
567 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp) > 0)) {
568 fStartTime = timestamp;
569 }
570 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp) < 0)) {
571 fEndTime = timestamp;
572 }
573 if (context.hasValidRank()) {
574 long rank = context.getRank();
575 if (fNbEvents <= rank) {
576 fNbEvents = rank + 1;
577 }
578 if (fIndexer != null) {
579 fIndexer.updateIndex(context, timestamp);
580 }
581 }
582 }
583
584 // ------------------------------------------------------------------------
585 // TmfDataProvider
586 // ------------------------------------------------------------------------
587
588 @Override
589 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
590 if (executorIsShutdown()) {
591 return null;
592 }
593 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
594 && (request.getIndex() == 0)) {
595 final ITmfContext context = seekEvent(request.getRange().getStartTime());
596 request.setStartIndex((int) context.getRank());
597 return context;
598
599 }
600 return seekEvent(request.getIndex());
601 }
602
603 // ------------------------------------------------------------------------
604 // Signal handlers
605 // ------------------------------------------------------------------------
606
607 /**
608 * Handler for the Trace Opened signal
609 *
610 * @param signal
611 * The incoming signal
612 */
613 @TmfSignalHandler
614 public void traceOpened(TmfTraceOpenedSignal signal) {
615 boolean signalIsForUs = false;
616 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
617 if (trace == this) {
618 signalIsForUs = true;
619 break;
620 }
621 }
622
623 if (!signalIsForUs) {
624 return;
625 }
626
627 /*
628 * The signal is either for this trace, or for an experiment containing
629 * this trace.
630 */
631 IStatus status = executeAnalysis();
632 if (!status.isOK()) {
633 Activator.log(status);
634 }
635
636 TmfTraceManager.refreshSupplementaryFiles(this);
637
638 if (signal.getTrace() == this) {
639 /* Additionally, the signal is directly for this trace. */
640 if (getNbEvents() == 0) {
641 return;
642 }
643
644 /* For a streaming trace, the range updated signal should be sent
645 * by the subclass when a new safe time is determined.
646 */
647 if (getStreamingInterval() > 0) {
648 return;
649 }
650
651 if (isComplete()) {
652 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
653 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
654
655 // Broadcast in separate thread to prevent deadlock
656 broadcastAsync(rangeUpdatedsignal);
657 }
658 return;
659 }
660 }
661
662 /**
663 * Signal handler for the TmfTraceRangeUpdatedSignal signal
664 *
665 * @param signal The incoming signal
666 */
667 @TmfSignalHandler
668 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
669 if (signal.getTrace() == this) {
670 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
671 }
672 }
673
674 /**
675 * Signal handler for the TmfTraceUpdatedSignal signal
676 *
677 * @param signal The incoming signal
678 */
679 @TmfSignalHandler
680 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
681 if (signal.getSource() == getIndexer()) {
682 fNbEvents = signal.getNbEvents();
683 fStartTime = signal.getRange().getStartTime();
684 fEndTime = signal.getRange().getEndTime();
685 }
686 }
687
688 // ------------------------------------------------------------------------
689 // Timestamp transformation functions
690 // ------------------------------------------------------------------------
691
692 @Override
693 public ITmfTimestampTransform getTimestampTransform() {
694 if (fTsTransform == null) {
695 fTsTransform = TimestampTransformFactory.getTimestampTransform(getResource());
696 }
697 return fTsTransform;
698 }
699
700 @Override
701 public void setTimestampTransform(final ITmfTimestampTransform tt) {
702 fTsTransform = tt;
703 TimestampTransformFactory.setTimestampTransform(getResource(), tt);
704 }
705
706 @Override
707 public ITmfTimestamp createTimestamp(long ts) {
708 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
709 }
710
711 // ------------------------------------------------------------------------
712 // toString
713 // ------------------------------------------------------------------------
714
715 @Override
716 @SuppressWarnings("nls")
717 public synchronized String toString() {
718 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
719 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
720 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
721 }
722
723 @Override
724 public boolean isComplete() {
725 /*
726 * Be default, all traces are "complete" which means no more data will
727 * be added later
728 */
729 return true;
730 }
731
732 @Override
733 public void setComplete(boolean isComplete) {
734 /*
735 * This should be overridden by trace classes that can support live
736 * reading (traces in an incomplete state)
737 */
738 }
739 }
This page took 0.047693 seconds and 5 git commands to generate.