Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 import org.eclipse.core.resources.IFolder;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.MultiStatus;
35 import org.eclipse.core.runtime.Path;
36 import org.eclipse.core.runtime.Status;
37 import org.eclipse.linuxtools.internal.tmf.core.Activator;
38 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
39 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
40 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
41 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
42 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
43 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
44 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
45 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
46 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
47 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
48 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
49 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
50 import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
51 import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
52 import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
53 import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform;
54 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
55 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
56 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
57 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
58 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
59 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
60
61 /**
62 * Abstract implementation of ITmfTrace.
63 * <p>
64 * Since the concept of 'location' is trace specific, the concrete classes have
65 * to provide the related methods, namely:
66 * <ul>
67 * <li> public ITmfLocation<?> getCurrentLocation()
68 * <li> public double getLocationRatio(ITmfLocation<?> location)
69 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
70 * <li> public ITmfContext seekEvent(double ratio)
71 * <li> public IStatus validate(IProject project, String path)
72 * </ul>
73 * A concrete trace must provide its corresponding parser. A common way to
74 * accomplish this is by making the concrete class extend TmfTrace and
75 * implement ITmfEventParser.
76 * <p>
77 * The concrete class can either specify its own indexer or use the provided
78 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
79 * used as checkpoint interval.
80 *
81 * @version 1.0
82 * @author Francois Chouinard
83 *
84 * @see ITmfEvent
85 * @see ITmfTraceIndexer
86 * @see ITmfEventParser
87 */
88 public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
89
90 // ------------------------------------------------------------------------
91 // Attributes
92 // ------------------------------------------------------------------------
93
94 // The resource used for persistent properties for this trace
95 private IResource fResource;
96
97 // The trace path
98 private String fPath;
99
100 // The trace cache page size
101 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
102
103 // The number of events collected (so far)
104 private long fNbEvents = 0;
105
106 // The time span of the event stream
107 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
108 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
109
110 // The trace streaming interval (0 = no streaming)
111 private long fStreamingInterval = 0;
112
113 // The trace indexer
114 private ITmfTraceIndexer fIndexer;
115
116 // The trace parser
117 private ITmfEventParser fParser;
118
119 // The trace's statistics
120 private ITmfStatistics fStatistics;
121
122 /**
123 * The collection of state systems that are registered with this trace. Each
124 * sub-class can decide to add its (one or many) state system to this map
125 * during their {@link #buildStateSystem()}.
126 *
127 * @since 2.0
128 */
129 protected final Map<String, ITmfStateSystem> fStateSystems =
130 new LinkedHashMap<String, ITmfStateSystem>();
131
132 private ITmfTimestampTransform fTsTransform;
133
134 private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$
135
136 // ------------------------------------------------------------------------
137 // Construction
138 // ------------------------------------------------------------------------
139
140 /**
141 * The default, parameterless, constructor
142 */
143 public TmfTrace() {
144 super();
145 fIndexer = createIndexer(DEFAULT_BLOCK_SIZE);
146 }
147
148 /**
149 * Full constructor.
150 *
151 * @param resource
152 * The resource associated to the trace
153 * @param type
154 * The type of events that will be read from this trace
155 * @param path
156 * The path to the trace on the filesystem
157 * @param cacheSize
158 * The trace cache size. Pass '-1' to use the default specified
159 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
160 * @param interval
161 * The trace streaming interval. You can use '0' for post-mortem
162 * traces.
163 * @param parser
164 * The trace event parser. Use 'null' if (and only if) the trace
165 * object itself is also the ITmfEventParser to be used.
166 * @throws TmfTraceException
167 * If something failed during the opening
168 */
169 protected TmfTrace(final IResource resource,
170 final Class<? extends ITmfEvent> type,
171 final String path,
172 final int cacheSize,
173 final long interval,
174 final ITmfEventParser parser)
175 throws TmfTraceException {
176 super();
177 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
178 fStreamingInterval = interval;
179 fParser = parser;
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 fParser = trace.fParser;
197 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
198 }
199
200 /**
201 * Creates the indexer instance. Classes extending this class can override
202 * this to provide a different indexer implementation.
203 *
204 * @param interval the checkpoints interval
205 *
206 * @return the indexer
207 * @since 3.0
208 */
209 protected ITmfTraceIndexer createIndexer(int interval) {
210 return new TmfCheckpointIndexer(this, interval);
211 }
212
213 // ------------------------------------------------------------------------
214 // ITmfTrace - Initializers
215 // ------------------------------------------------------------------------
216
217 @Override
218 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
219 initialize(resource, path, type);
220 }
221
222 /**
223 * Initialize the trace common attributes and the base component.
224 *
225 * @param resource the Eclipse resource (trace)
226 * @param path the trace path
227 * @param type the trace event type
228 *
229 * @throws TmfTraceException If something failed during the initialization
230 */
231 protected void initialize(final IResource resource,
232 final String path,
233 final Class<? extends ITmfEvent> type)
234 throws TmfTraceException {
235 if (path == null) {
236 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
237 }
238 fPath = path;
239 fResource = resource;
240 String traceName = (resource != null) ? resource.getName() : null;
241 // If no resource was provided, extract the display name the trace path
242 if (traceName == null) {
243 traceName = new Path(path).lastSegment();
244 }
245 if (fParser == null) {
246 if (this instanceof ITmfEventParser) {
247 fParser = (ITmfEventParser) this;
248 } else {
249 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
250 }
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 fIndexer = createIndexer(fCacheSize);
256 }
257
258 /**
259 * Indicates if the path points to an existing file/directory
260 *
261 * @param path the path to test
262 * @return true if the file/directory exists
263 */
264 protected boolean fileExists(final String path) {
265 final File file = new File(path);
266 return file.exists();
267 }
268
269 /**
270 * @since 2.0
271 */
272 @Override
273 public void indexTrace(boolean waitForCompletion) {
274 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
275 }
276
277 /**
278 * The default implementation of TmfTrace uses a TmfStatistics back-end.
279 * Override this if you want to specify another type (or none at all).
280 *
281 * @return An IStatus indicating if the statistics could be built
282 * successfully or not.
283 * @since 3.0
284 */
285 protected IStatus buildStatistics() {
286 /*
287 * Initialize the statistics provider, but only if a Resource has been
288 * set (so we don't build it for experiments, for unit tests, etc.)
289 */
290 try {
291 fStatistics = (fResource == null ? null : new TmfStateStatistics(this) );
292 } catch (TmfTraceException e) {
293 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
294 }
295 return Status.OK_STATUS;
296 }
297
298 /**
299 * Build the state system(s) associated with this trace type.
300 *
301 * @return An IStatus indicating if the state system could be build
302 * successfully or not.
303 * @since 3.0
304 */
305 protected IStatus buildStateSystem() {
306 /*
307 * Nothing is done in the base implementation, please specify
308 * how/if to register a new state system in derived classes.
309 */
310 return Status.OK_STATUS;
311 }
312
313 /**
314 * Clears the trace
315 */
316 @Override
317 public synchronized void dispose() {
318 /* Clean up the index if applicable */
319 if (getIndexer() != null) {
320 getIndexer().dispose();
321 }
322
323 /* Clean up the statistics */
324 if (fStatistics != null) {
325 fStatistics.dispose();
326 }
327
328 /* Clean up the state systems */
329 for (ITmfStateSystem ss : fStateSystems.values()) {
330 ss.dispose();
331 }
332
333 super.dispose();
334 }
335
336 // ------------------------------------------------------------------------
337 // ITmfTrace - Basic getters
338 // ------------------------------------------------------------------------
339
340 @Override
341 public Class<ITmfEvent> getEventType() {
342 return (Class<ITmfEvent>) super.getType();
343 }
344
345 @Override
346 public IResource getResource() {
347 return fResource;
348 }
349
350 @Override
351 public String getPath() {
352 return fPath;
353 }
354
355 @Override
356 public int getCacheSize() {
357 return fCacheSize;
358 }
359
360 @Override
361 public long getStreamingInterval() {
362 return fStreamingInterval;
363 }
364
365 /**
366 * @return the trace indexer
367 * @since 3.0
368 */
369 protected ITmfTraceIndexer getIndexer() {
370 return fIndexer;
371 }
372
373 /**
374 * @return the trace parser
375 */
376 protected ITmfEventParser getParser() {
377 return fParser;
378 }
379
380 /**
381 * @since 2.0
382 */
383 @Override
384 public ITmfStatistics getStatistics() {
385 return fStatistics;
386 }
387
388 /**
389 * @since 2.0
390 */
391 @Override
392 public final Map<String, ITmfStateSystem> getStateSystems() {
393 return Collections.unmodifiableMap(fStateSystems);
394 }
395
396 /**
397 * @since 2.0
398 */
399 @Override
400 public final void registerStateSystem(String id, ITmfStateSystem ss) {
401 fStateSystems.put(id, ss);
402 }
403
404 // ------------------------------------------------------------------------
405 // ITmfTrace - Trace characteristics getters
406 // ------------------------------------------------------------------------
407
408 @Override
409 public synchronized long getNbEvents() {
410 return fNbEvents;
411 }
412
413 /**
414 * @since 2.0
415 */
416 @Override
417 public TmfTimeRange getTimeRange() {
418 return new TmfTimeRange(fStartTime, fEndTime);
419 }
420
421 /**
422 * @since 2.0
423 */
424 @Override
425 public ITmfTimestamp getStartTime() {
426 return fStartTime;
427 }
428
429 /**
430 * @since 2.0
431 */
432 @Override
433 public ITmfTimestamp getEndTime() {
434 return fEndTime;
435 }
436
437 /**
438 * @since 2.0
439 */
440 @Override
441 public ITmfTimestamp getInitialRangeOffset() {
442 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
443 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
444 }
445
446 /**
447 * @since 3.0
448 */
449 @Override
450 public String getHostId() {
451 return this.getName();
452 }
453
454 // ------------------------------------------------------------------------
455 // Convenience setters
456 // ------------------------------------------------------------------------
457
458 /**
459 * Set the trace cache size. Must be done at initialization time.
460 *
461 * @param cacheSize The trace cache size
462 */
463 protected void setCacheSize(final int cacheSize) {
464 fCacheSize = cacheSize;
465 }
466
467 /**
468 * Set the trace known number of events. This can be quite dynamic
469 * during indexing or for live traces.
470 *
471 * @param nbEvents The number of events
472 */
473 protected synchronized void setNbEvents(final long nbEvents) {
474 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
475 }
476
477 /**
478 * Update the trace events time range
479 *
480 * @param range the new time range
481 * @since 2.0
482 */
483 protected void setTimeRange(final TmfTimeRange range) {
484 fStartTime = range.getStartTime();
485 fEndTime = range.getEndTime();
486 }
487
488 /**
489 * Update the trace chronologically first event timestamp
490 *
491 * @param startTime the new first event timestamp
492 * @since 2.0
493 */
494 protected void setStartTime(final ITmfTimestamp startTime) {
495 fStartTime = startTime;
496 }
497
498 /**
499 * Update the trace chronologically last event timestamp
500 *
501 * @param endTime the new last event timestamp
502 * @since 2.0
503 */
504 protected void setEndTime(final ITmfTimestamp endTime) {
505 fEndTime = endTime;
506 }
507
508 /**
509 * Set the polling interval for live traces (default = 0 = no streaming).
510 *
511 * @param interval the new trace streaming interval
512 */
513 protected void setStreamingInterval(final long interval) {
514 fStreamingInterval = (interval > 0) ? interval : 0;
515 }
516
517 /**
518 * Set the trace parser. Must be done at initialization time.
519 *
520 * @param parser the new trace parser
521 */
522 protected void setParser(final ITmfEventParser parser) {
523 fParser = parser;
524 }
525
526 // ------------------------------------------------------------------------
527 // ITmfTrace - SeekEvent operations (returning a trace context)
528 // ------------------------------------------------------------------------
529
530 @Override
531 public synchronized ITmfContext seekEvent(final long rank) {
532
533 // A rank <= 0 indicates to seek the first event
534 if (rank <= 0) {
535 ITmfContext context = seekEvent((ITmfLocation) null);
536 context.setRank(0);
537 return context;
538 }
539
540 // Position the trace at the checkpoint
541 final ITmfContext context = fIndexer.seekIndex(rank);
542
543 // And locate the requested event context
544 long pos = context.getRank();
545 if (pos < rank) {
546 ITmfEvent event = getNext(context);
547 while ((event != null) && (++pos < rank)) {
548 event = getNext(context);
549 }
550 }
551 return context;
552 }
553
554 /**
555 * @since 2.0
556 */
557 @Override
558 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
559
560 // A null timestamp indicates to seek the first event
561 if (timestamp == null) {
562 ITmfContext context = seekEvent((ITmfLocation) null);
563 context.setRank(0);
564 return context;
565 }
566
567 // Position the trace at the checkpoint
568 ITmfContext context = fIndexer.seekIndex(timestamp);
569
570 // And locate the requested event context
571 ITmfLocation previousLocation = context.getLocation();
572 long previousRank = context.getRank();
573 ITmfEvent event = getNext(context);
574 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
575 previousLocation = context.getLocation();
576 previousRank = context.getRank();
577 event = getNext(context);
578 }
579 if (event == null) {
580 context.setLocation(null);
581 context.setRank(ITmfContext.UNKNOWN_RANK);
582 } else {
583 context.dispose();
584 context = seekEvent(previousLocation);
585 context.setRank(previousRank);
586 }
587 return context;
588 }
589
590 // ------------------------------------------------------------------------
591 // ITmfTrace - Read operations (returning an actual event)
592 // ------------------------------------------------------------------------
593
594 @Override
595 public synchronized ITmfEvent getNext(final ITmfContext context) {
596 // parseEvent() does not update the context
597 final ITmfEvent event = fParser.parseEvent(context);
598 if (event != null) {
599 updateAttributes(context, event.getTimestamp());
600 context.setLocation(getCurrentLocation());
601 context.increaseRank();
602 processEvent(event);
603 }
604 return event;
605 }
606
607 /**
608 * Hook for special event processing by the concrete class
609 * (called by TmfTrace.getEvent())
610 *
611 * @param event the event
612 */
613 protected void processEvent(final ITmfEvent event) {
614 // Do nothing
615 }
616
617 /**
618 * Update the trace attributes
619 *
620 * @param context the current trace context
621 * @param timestamp the corresponding timestamp
622 * @since 2.0
623 */
624 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
625 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
626 fStartTime = timestamp;
627 }
628 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
629 fEndTime = timestamp;
630 }
631 if (context.hasValidRank()) {
632 long rank = context.getRank();
633 if (fNbEvents <= rank) {
634 fNbEvents = rank + 1;
635 }
636 if (fIndexer != null) {
637 fIndexer.updateIndex(context, timestamp);
638 }
639 }
640 }
641
642 // ------------------------------------------------------------------------
643 // TmfDataProvider
644 // ------------------------------------------------------------------------
645
646 /**
647 * @since 2.0
648 */
649 @Override
650 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
651 if (executorIsShutdown()) {
652 return null;
653 }
654 if ((request instanceof ITmfEventRequest)
655 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
656 && (request.getIndex() == 0))
657 {
658 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
659 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
660 return context;
661
662 }
663 return seekEvent(request.getIndex());
664 }
665
666 // ------------------------------------------------------------------------
667 // Signal handlers
668 // ------------------------------------------------------------------------
669
670 /**
671 * Handler for the Trace Opened signal
672 *
673 * @param signal
674 * The incoming signal
675 * @since 2.0
676 */
677 @TmfSignalHandler
678 public void traceOpened(TmfTraceOpenedSignal signal) {
679 boolean signalIsForUs = false;
680 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
681 if (trace == this) {
682 signalIsForUs = true;
683 break;
684 }
685 }
686
687 if (!signalIsForUs) {
688 return;
689 }
690
691 /*
692 * The signal is either for this trace, or for an experiment containing
693 * this trace.
694 */
695 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
696 status.add(buildStatistics());
697 status.add(buildStateSystem());
698 if (!status.isOK()) {
699 Activator.log(status);
700 }
701
702 /* Refresh the project, so it can pick up new files that got created. */
703 try {
704 if (fResource != null) {
705 fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
706 }
707 } catch (CoreException e) {
708 e.printStackTrace();
709 }
710
711 if (signal.getTrace() == this) {
712 /* Additionally, the signal is directly for this trace. */
713 if (getNbEvents() == 0) {
714 return;
715 }
716
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
724 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
725 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
726
727 // Broadcast in separate thread to prevent deadlock
728 new Thread() {
729 @Override
730 public void run() {
731 broadcast(rangeUpdatedsignal);
732 }
733 }.start();
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 2.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 {
804 FileInputStream fis = new FileInputStream(sync_file);
805 ObjectInputStream ois = new ObjectInputStream(fis);
806 fTsTransform = (ITmfTimestampTransform) ois.readObject();
807
808 ois.close();
809 fis.close();
810 } catch (ClassNotFoundException e1) {
811 fTsTransform = TmfTimestampTransform.IDENTITY;
812 } catch (FileNotFoundException e1) {
813 fTsTransform = TmfTimestampTransform.IDENTITY;
814 } catch (IOException e1) {
815 fTsTransform = TmfTimestampTransform.IDENTITY;
816 }
817 } else {
818 fTsTransform = TmfTimestampTransform.IDENTITY;
819 }
820 }
821 return fTsTransform;
822 }
823
824 /**
825 * @since 3.0
826 */
827 @Override
828 public void setTimestampTransform(final ITmfTimestampTransform tt) {
829 fTsTransform = tt;
830
831 /* Save the timestamp transform to a file */
832 File sync_file = getSyncFormulaFile();
833 if (sync_file != null) {
834 if (sync_file.exists()) {
835 sync_file.delete();
836 }
837 FileOutputStream fos;
838 ObjectOutputStream oos;
839
840 /* Save the header of the file */
841 try {
842 fos = new FileOutputStream(sync_file, false);
843 oos = new ObjectOutputStream(fos);
844
845 oos.writeObject(fTsTransform);
846 oos.close();
847 fos.close();
848 } catch (IOException e1) {
849 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
850 }
851 }
852 }
853
854 /**
855 * @since 3.0
856 */
857 @Override
858 public ITmfTimestamp createTimestamp(long ts) {
859 return new TmfTimestamp(getTimestampTransform().transform(ts));
860 }
861
862 // ------------------------------------------------------------------------
863 // toString
864 // ------------------------------------------------------------------------
865
866 @Override
867 @SuppressWarnings("nls")
868 public synchronized String toString() {
869 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
870 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
871 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
872 }
873
874 }
This page took 0.054129 seconds and 6 git commands to generate.