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