1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
10 * Matthew Khouzam - Initial implementation and API
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.core
.synchronization
;
15 import java
.io
.FileInputStream
;
16 import java
.io
.FileOutputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.ObjectInputStream
;
19 import java
.io
.ObjectOutputStream
;
20 import java
.math
.BigDecimal
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfConstantTransform
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransform
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransformLinearFast
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
32 * A factory to generate timestamp tranforms
34 * @author Matthew Khouzam
37 public final class TimestampTransformFactory
{
39 private static final String SYNCHRONIZATION_FORMULA_FILE
= "sync_formula"; //$NON-NLS-1$
41 private TimestampTransformFactory() {
45 * Creates the identity timestamp transform
47 * @return The identity timestamp transform
49 public static ITmfTimestampTransform
getDefaultTransform() {
50 return TmfTimestampTransform
.IDENTITY
;
54 * Create an offsetted transform
57 * the offset in long format, nanosecond scale
58 * @return the offsetted transform
60 public static ITmfTimestampTransform
createWithOffset(long offset
) {
62 return TmfTimestampTransform
.IDENTITY
;
64 return new TmfConstantTransform(offset
);
68 * Create an offsetted transform
71 * the offset in a timestamp with scale
72 * @return the offsetted transform
74 public static ITmfTimestampTransform
createWithOffset(ITmfTimestamp offset
) {
75 if (offset
.getValue() == 0) {
76 return TmfTimestampTransform
.IDENTITY
;
78 return new TmfConstantTransform(offset
);
82 * Create a timestamp transform corresponding to a linear equation, with
83 * slope and offset. The expected timestamp transform is such that f(t) =
84 * m*x + b, where m is the slope and b the offset.
90 * @return the transform
92 public static ITmfTimestampTransform
createLinear(double factor
, ITmfTimestamp offset
) {
94 return createWithOffset(offset
);
96 return new TmfTimestampTransformLinearFast(factor
, offset
.normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
100 * Create a timestamp transform corresponding to a linear equation, with
101 * slope and offset. The expected timestamp transform is such that f(t) =
102 * m*x + b, where m is the slope and b the offset.
107 * the offset in nanoseconds
108 * @return the transform
110 public static ITmfTimestampTransform
createLinear(double factor
, long offset
) {
112 return createWithOffset(offset
);
114 return new TmfTimestampTransformLinearFast(factor
, offset
);
118 * Create a timestamp transform corresponding to a linear equation, with
119 * slope and offset expressed in BigDecimal. The expected timestamp
120 * transform is such that f(t) = m*x + b, where m is the slope and b the
126 * the offset in nanoseconds
127 * @return the transform
129 public static ITmfTimestampTransform
createLinear(BigDecimal factor
, BigDecimal offset
) {
130 if (factor
.equals(BigDecimal
.ONE
)) {
131 return createWithOffset(offset
.longValueExact());
133 return new TmfTimestampTransformLinearFast(factor
, offset
);
137 * Returns the file resource used to store synchronization formula. The file
142 * @return the synchronization file
144 private static File
getSyncFormulaFile(IResource resource
) {
145 if (resource
== null) {
149 String supplDirectory
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
150 return new File(supplDirectory
+ File
.separator
+ SYNCHRONIZATION_FORMULA_FILE
);
151 } catch (CoreException e
) {
158 * Returns the timestamp transform for a trace resource
162 * @return the timestamp transform
165 public static ITmfTimestampTransform
getTimestampTransform(IResource resource
) {
166 File syncFile
= getSyncFormulaFile(resource
);
167 if (syncFile
!= null && syncFile
.exists()) {
168 /* Read the serialized object from file */
169 try (FileInputStream fis
= new FileInputStream(syncFile
);
170 ObjectInputStream ois
= new ObjectInputStream(fis
);) {
171 return (ITmfTimestampTransform
) ois
.readObject();
172 } catch (ClassNotFoundException
| IOException e
) {
175 return TimestampTransformFactory
.getDefaultTransform();
179 * Sets the trace resource's timestamp transform
184 * The timestamp transform for all timestamps of this trace, or
188 public static void setTimestampTransform(IResource resource
, ITmfTimestampTransform tt
) {
189 /* Save the timestamp transform to a file */
190 File syncFile
= getSyncFormulaFile(resource
);
191 if (syncFile
!= null) {
192 if (syncFile
.exists()) {
198 /* Write the serialized object to file */
199 try (FileOutputStream fos
= new FileOutputStream(syncFile
, false);
200 ObjectOutputStream oos
= new ObjectOutputStream(fos
);) {
202 } catch (IOException e1
) {
203 Activator
.logError("Error writing timestamp transform for trace", e1
); //$NON-NLS-1$