BTree index on disk
[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 }
146
147 /**
148 * Full constructor.
149 *
150 * @param resource
151 * The resource associated to the trace
152 * @param type
153 * The type of events that will be read from this trace
154 * @param path
155 * The path to the trace on the filesystem
156 * @param cacheSize
157 * The trace cache size. Pass '-1' to use the default specified
158 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
159 * @param interval
160 * The trace streaming interval. You can use '0' for post-mortem
161 * traces.
162 * @param indexer
163 * The trace indexer. You can pass 'null' to use a default
164 * checkpoint indexer.
165 * @param parser
166 * The trace event parser. Use 'null' if (and only if) the trace
167 * object itself is also the ITmfEventParser to be used.
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 final ITmfTraceIndexer indexer,
177 final ITmfEventParser parser)
178 throws TmfTraceException {
179 super();
180 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
181 fStreamingInterval = interval;
182 fParser = parser;
183 fIndexer = indexer;
184 initialize(resource, path, type);
185 }
186
187 /**
188 * Copy constructor
189 *
190 * @param trace the original trace
191 * @throws TmfTraceException Should not happen usually
192 */
193 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
194 super();
195 if (trace == null) {
196 throw new IllegalArgumentException();
197 }
198 fCacheSize = trace.getCacheSize();
199 fStreamingInterval = trace.getStreamingInterval();
200 fParser = trace.fParser;
201 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
202 }
203
204 /**
205 * Creates the indexer instance. Classes extending this class can override
206 * this to provide a different indexer implementation.
207 *
208 * @param interval the checkpoints interval
209 *
210 * @return the indexer
211 * @since 3.0
212 */
213 protected ITmfTraceIndexer createIndexer(int interval) {
214 return new TmfCheckpointIndexer(this, interval);
215 }
216
217 // ------------------------------------------------------------------------
218 // ITmfTrace - Initializers
219 // ------------------------------------------------------------------------
220
221 @Override
222 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
223 initialize(resource, path, type);
224 }
225
226 /**
227 * Initialize the trace common attributes and the base component.
228 *
229 * @param resource the Eclipse resource (trace)
230 * @param path the trace path
231 * @param type the trace event type
232 *
233 * @throws TmfTraceException If something failed during the initialization
234 */
235 protected void initialize(final IResource resource,
236 final String path,
237 final Class<? extends ITmfEvent> type)
238 throws TmfTraceException {
239 if (path == null) {
240 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
241 }
242 fPath = path;
243 fResource = resource;
244 String traceName = (resource != null) ? resource.getName() : null;
245 // If no resource was provided, extract the display name the trace path
246 if (traceName == null) {
247 traceName = new Path(path).lastSegment();
248 }
249 if (fParser == null) {
250 if (this instanceof ITmfEventParser) {
251 fParser = (ITmfEventParser) this;
252 } else {
253 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
254 }
255 }
256 super.init(traceName, type);
257 // register as VIP after super.init() because TmfComponent registers to signal manager there
258 TmfSignalManager.registerVIP(this);
259 if (fIndexer == null) {
260 fIndexer = createIndexer(fCacheSize);
261 }
262 }
263
264 /**
265 * Indicates if the path points to an existing file/directory
266 *
267 * @param path the path to test
268 * @return true if the file/directory exists
269 */
270 protected boolean fileExists(final String path) {
271 final File file = new File(path);
272 return file.exists();
273 }
274
275 /**
276 * @since 2.0
277 */
278 @Override
279 public void indexTrace(boolean waitForCompletion) {
280 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
281 }
282
283 /**
284 * The default implementation of TmfTrace uses a TmfStatistics back-end.
285 * Override this if you want to specify another type (or none at all).
286 *
287 * @return An IStatus indicating if the statistics could be built
288 * successfully or not.
289 * @since 3.0
290 */
291 protected IStatus buildStatistics() {
292 /*
293 * Initialize the statistics provider, but only if a Resource has been
294 * set (so we don't build it for experiments, for unit tests, etc.)
295 */
296 try {
297 fStatistics = (fResource == null ? null : new TmfStateStatistics(this) );
298 } catch (TmfTraceException e) {
299 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
300 }
301 return Status.OK_STATUS;
302 }
303
304 /**
305 * Build the state system(s) associated with this trace type.
306 *
307 * @return An IStatus indicating if the state system could be build
308 * successfully or not.
309 * @since 3.0
310 */
311 protected IStatus buildStateSystem() {
312 /*
313 * Nothing is done in the base implementation, please specify
314 * how/if to register a new state system in derived classes.
315 */
316 return Status.OK_STATUS;
317 }
318
319 /**
320 * Clears the trace
321 */
322 @Override
323 public synchronized void dispose() {
324 /* Clean up the index if applicable */
325 if (getIndexer() != null) {
326 getIndexer().dispose();
327 }
328
329 /* Clean up the statistics */
330 if (fStatistics != null) {
331 fStatistics.dispose();
332 }
333
334 /* Clean up the state systems */
335 for (ITmfStateSystem ss : fStateSystems.values()) {
336 ss.dispose();
337 }
338
339 super.dispose();
340 }
341
342 // ------------------------------------------------------------------------
343 // ITmfTrace - Basic getters
344 // ------------------------------------------------------------------------
345
346 @Override
347 public Class<ITmfEvent> getEventType() {
348 return (Class<ITmfEvent>) super.getType();
349 }
350
351 @Override
352 public IResource getResource() {
353 return fResource;
354 }
355
356 @Override
357 public String getPath() {
358 return fPath;
359 }
360
361 @Override
362 public int getCacheSize() {
363 return fCacheSize;
364 }
365
366 @Override
367 public long getStreamingInterval() {
368 return fStreamingInterval;
369 }
370
371 /**
372 * @return the trace indexer
373 * @since 3.0
374 */
375 protected ITmfTraceIndexer getIndexer() {
376 return fIndexer;
377 }
378
379 /**
380 * @return the trace parser
381 */
382 protected ITmfEventParser getParser() {
383 return fParser;
384 }
385
386 /**
387 * @since 2.0
388 */
389 @Override
390 public ITmfStatistics getStatistics() {
391 return fStatistics;
392 }
393
394 /**
395 * @since 2.0
396 */
397 @Override
398 public final Map<String, ITmfStateSystem> getStateSystems() {
399 return Collections.unmodifiableMap(fStateSystems);
400 }
401
402 /**
403 * @since 2.0
404 */
405 @Override
406 public final void registerStateSystem(String id, ITmfStateSystem ss) {
407 fStateSystems.put(id, ss);
408 }
409
410 // ------------------------------------------------------------------------
411 // ITmfTrace - Trace characteristics getters
412 // ------------------------------------------------------------------------
413
414 @Override
415 public synchronized long getNbEvents() {
416 return fNbEvents;
417 }
418
419 /**
420 * @since 2.0
421 */
422 @Override
423 public TmfTimeRange getTimeRange() {
424 return new TmfTimeRange(fStartTime, fEndTime);
425 }
426
427 /**
428 * @since 2.0
429 */
430 @Override
431 public ITmfTimestamp getStartTime() {
432 return fStartTime;
433 }
434
435 /**
436 * @since 2.0
437 */
438 @Override
439 public ITmfTimestamp getEndTime() {
440 return fEndTime;
441 }
442
443 /**
444 * @since 2.0
445 */
446 @Override
447 public ITmfTimestamp getInitialRangeOffset() {
448 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
449 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
450 }
451
452 /**
453 * @since 3.0
454 */
455 @Override
456 public String getHostId() {
457 return this.getName();
458 }
459
460 // ------------------------------------------------------------------------
461 // Convenience setters
462 // ------------------------------------------------------------------------
463
464 /**
465 * Set the trace cache size. Must be done at initialization time.
466 *
467 * @param cacheSize The trace cache size
468 */
469 protected void setCacheSize(final int cacheSize) {
470 fCacheSize = cacheSize;
471 }
472
473 /**
474 * Set the trace known number of events. This can be quite dynamic
475 * during indexing or for live traces.
476 *
477 * @param nbEvents The number of events
478 */
479 protected synchronized void setNbEvents(final long nbEvents) {
480 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
481 }
482
483 /**
484 * Update the trace events time range
485 *
486 * @param range the new time range
487 * @since 2.0
488 */
489 protected void setTimeRange(final TmfTimeRange range) {
490 fStartTime = range.getStartTime();
491 fEndTime = range.getEndTime();
492 }
493
494 /**
495 * Update the trace chronologically first event timestamp
496 *
497 * @param startTime the new first event timestamp
498 * @since 2.0
499 */
500 protected void setStartTime(final ITmfTimestamp startTime) {
501 fStartTime = startTime;
502 }
503
504 /**
505 * Update the trace chronologically last event timestamp
506 *
507 * @param endTime the new last event timestamp
508 * @since 2.0
509 */
510 protected void setEndTime(final ITmfTimestamp endTime) {
511 fEndTime = endTime;
512 }
513
514 /**
515 * Set the polling interval for live traces (default = 0 = no streaming).
516 *
517 * @param interval the new trace streaming interval
518 */
519 protected void setStreamingInterval(final long interval) {
520 fStreamingInterval = (interval > 0) ? interval : 0;
521 }
522
523 /**
524 * Set the trace indexer. Must be done at initialization time.
525 *
526 * @param indexer the trace indexer
527 * @since 3.0
528 */
529 protected void setIndexer(final ITmfTraceIndexer indexer) {
530 fIndexer = indexer;
531 }
532
533 /**
534 * Set the trace parser. Must be done at initialization time.
535 *
536 * @param parser the new trace parser
537 */
538 protected void setParser(final ITmfEventParser parser) {
539 fParser = parser;
540 }
541
542 // ------------------------------------------------------------------------
543 // ITmfTrace - SeekEvent operations (returning a trace context)
544 // ------------------------------------------------------------------------
545
546 @Override
547 public synchronized ITmfContext seekEvent(final long rank) {
548
549 // A rank <= 0 indicates to seek the first event
550 if (rank <= 0) {
551 ITmfContext context = seekEvent((ITmfLocation) null);
552 context.setRank(0);
553 return context;
554 }
555
556 // Position the trace at the checkpoint
557 final ITmfContext context = fIndexer.seekIndex(rank);
558
559 // And locate the requested event context
560 long pos = context.getRank();
561 if (pos < rank) {
562 ITmfEvent event = getNext(context);
563 while ((event != null) && (++pos < rank)) {
564 event = getNext(context);
565 }
566 }
567 return context;
568 }
569
570 /**
571 * @since 2.0
572 */
573 @Override
574 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
575
576 // A null timestamp indicates to seek the first event
577 if (timestamp == null) {
578 ITmfContext context = seekEvent((ITmfLocation) null);
579 context.setRank(0);
580 return context;
581 }
582
583 // Position the trace at the checkpoint
584 ITmfContext context = fIndexer.seekIndex(timestamp);
585
586 // And locate the requested event context
587 ITmfLocation previousLocation = context.getLocation();
588 long previousRank = context.getRank();
589 ITmfEvent event = getNext(context);
590 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
591 previousLocation = context.getLocation();
592 previousRank = context.getRank();
593 event = getNext(context);
594 }
595 if (event == null) {
596 context.setLocation(null);
597 context.setRank(ITmfContext.UNKNOWN_RANK);
598 } else {
599 context.dispose();
600 context = seekEvent(previousLocation);
601 context.setRank(previousRank);
602 }
603 return context;
604 }
605
606 // ------------------------------------------------------------------------
607 // ITmfTrace - Read operations (returning an actual event)
608 // ------------------------------------------------------------------------
609
610 @Override
611 public synchronized ITmfEvent getNext(final ITmfContext context) {
612 // parseEvent() does not update the context
613 final ITmfEvent event = fParser.parseEvent(context);
614 if (event != null) {
615 updateAttributes(context, event.getTimestamp());
616 context.setLocation(getCurrentLocation());
617 context.increaseRank();
618 processEvent(event);
619 }
620 return event;
621 }
622
623 /**
624 * Hook for special event processing by the concrete class
625 * (called by TmfTrace.getEvent())
626 *
627 * @param event the event
628 */
629 protected void processEvent(final ITmfEvent event) {
630 // Do nothing
631 }
632
633 /**
634 * Update the trace attributes
635 *
636 * @param context the current trace context
637 * @param timestamp the corresponding timestamp
638 * @since 2.0
639 */
640 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
641 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
642 fStartTime = timestamp;
643 }
644 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
645 fEndTime = timestamp;
646 }
647 if (context.hasValidRank()) {
648 long rank = context.getRank();
649 if (fNbEvents <= rank) {
650 fNbEvents = rank + 1;
651 }
652 if (fIndexer != null) {
653 fIndexer.updateIndex(context, timestamp);
654 }
655 }
656 }
657
658 // ------------------------------------------------------------------------
659 // TmfDataProvider
660 // ------------------------------------------------------------------------
661
662 /**
663 * @since 2.0
664 */
665 @Override
666 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
667 if (executorIsShutdown()) {
668 return null;
669 }
670 if ((request instanceof ITmfEventRequest)
671 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
672 && (request.getIndex() == 0))
673 {
674 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
675 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
676 return context;
677
678 }
679 return seekEvent(request.getIndex());
680 }
681
682 // ------------------------------------------------------------------------
683 // Signal handlers
684 // ------------------------------------------------------------------------
685
686 /**
687 * Handler for the Trace Opened signal
688 *
689 * @param signal
690 * The incoming signal
691 * @since 2.0
692 */
693 @TmfSignalHandler
694 public void traceOpened(TmfTraceOpenedSignal signal) {
695 boolean signalIsForUs = false;
696 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
697 if (trace == this) {
698 signalIsForUs = true;
699 break;
700 }
701 }
702
703 if (!signalIsForUs) {
704 return;
705 }
706
707 /*
708 * The signal is either for this trace, or for an experiment containing
709 * this trace.
710 */
711 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
712 status.add(buildStatistics());
713 status.add(buildStateSystem());
714 if (!status.isOK()) {
715 Activator.log(status);
716 }
717
718 /* Refresh the project, so it can pick up new files that got created. */
719 try {
720 if (fResource != null) {
721 fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
722 }
723 } catch (CoreException e) {
724 e.printStackTrace();
725 }
726
727 if (signal.getTrace() == this) {
728 /* Additionally, the signal is directly for this trace. */
729 if (getNbEvents() == 0) {
730 return;
731 }
732
733 /* For a streaming trace, the range updated signal should be sent
734 * by the subclass when a new safe time is determined.
735 */
736 if (getStreamingInterval() > 0) {
737 return;
738 }
739
740 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
741 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
742
743 // Broadcast in separate thread to prevent deadlock
744 new Thread() {
745 @Override
746 public void run() {
747 broadcast(rangeUpdatedsignal);
748 }
749 }.start();
750 return;
751 }
752 }
753
754 /**
755 * Signal handler for the TmfTraceRangeUpdatedSignal signal
756 *
757 * @param signal The incoming signal
758 * @since 2.0
759 */
760 @TmfSignalHandler
761 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
762 if (signal.getTrace() == this) {
763 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
764 }
765 }
766
767 /**
768 * Signal handler for the TmfTraceUpdatedSignal signal
769 *
770 * @param signal The incoming signal
771 * @since 2.0
772 */
773 @TmfSignalHandler
774 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
775 if (signal.getSource() == getIndexer()) {
776 fNbEvents = signal.getNbEvents();
777 fStartTime = signal.getRange().getStartTime();
778 fEndTime = signal.getRange().getEndTime();
779 }
780 }
781
782 /**
783 * Returns the file resource used to store synchronization formula. The file
784 * may not exist.
785 *
786 * @return the synchronization file
787 */
788 private File getSyncFormulaFile() {
789 File file = null;
790 if (fResource instanceof IFolder) {
791 try {
792 String supplDirectory;
793
794 supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
795
796 file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
797
798 } catch (CoreException e) {
799
800 }
801 }
802 return file;
803 }
804
805 // ------------------------------------------------------------------------
806 // Timestamp transformation functions
807 // ------------------------------------------------------------------------
808
809 /**
810 * @since 3.0
811 */
812 @Override
813 public ITmfTimestampTransform getTimestampTransform() {
814 if (fTsTransform == null) {
815 /* Check if a formula is stored somewhere in the resources */
816 File sync_file = getSyncFormulaFile();
817 if (sync_file != null && sync_file.exists()) {
818
819 try {
820 FileInputStream fis = new FileInputStream(sync_file);
821 ObjectInputStream ois = new ObjectInputStream(fis);
822 fTsTransform = (ITmfTimestampTransform) ois.readObject();
823
824 ois.close();
825 fis.close();
826 } catch (ClassNotFoundException e1) {
827 fTsTransform = TmfTimestampTransform.IDENTITY;
828 } catch (FileNotFoundException e1) {
829 fTsTransform = TmfTimestampTransform.IDENTITY;
830 } catch (IOException e1) {
831 fTsTransform = TmfTimestampTransform.IDENTITY;
832 }
833 } else {
834 fTsTransform = TmfTimestampTransform.IDENTITY;
835 }
836 }
837 return fTsTransform;
838 }
839
840 /**
841 * @since 3.0
842 */
843 @Override
844 public void setTimestampTransform(final ITmfTimestampTransform tt) {
845 fTsTransform = tt;
846
847 /* Save the timestamp transform to a file */
848 File sync_file = getSyncFormulaFile();
849 if (sync_file != null) {
850 if (sync_file.exists()) {
851 sync_file.delete();
852 }
853 FileOutputStream fos;
854 ObjectOutputStream oos;
855
856 /* Save the header of the file */
857 try {
858 fos = new FileOutputStream(sync_file, false);
859 oos = new ObjectOutputStream(fos);
860
861 oos.writeObject(fTsTransform);
862 oos.close();
863 fos.close();
864 } catch (IOException e1) {
865 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
866 }
867 }
868 }
869
870 /**
871 * @since 3.0
872 */
873 @Override
874 public ITmfTimestamp createTimestamp(long ts) {
875 return new TmfTimestamp(getTimestampTransform().transform(ts));
876 }
877
878 // ------------------------------------------------------------------------
879 // toString
880 // ------------------------------------------------------------------------
881
882 @Override
883 @SuppressWarnings("nls")
884 public synchronized String toString() {
885 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
886 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
887 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
888 }
889
890 }
This page took 0.051679 seconds and 5 git commands to generate.