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
.jdt
.annotation
.NonNullByDefault
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfConstantTransform
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransform
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransformLinearFast
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 * A factory to generate timestamp tranforms
37 * @author Matthew Khouzam
40 public final class TimestampTransformFactory
{
42 private static final String SYNCHRONIZATION_FORMULA_FILE
= "sync_formula"; //$NON-NLS-1$
44 private TimestampTransformFactory() {
48 * Creates the identity timestamp transform
50 * @return The identity timestamp transform
52 public static ITmfTimestampTransform
getDefaultTransform() {
53 return TmfTimestampTransform
.IDENTITY
;
57 * Create an offsetted transform
60 * the offset in long format, nanosecond scale
61 * @return the offsetted transform
63 public static ITmfTimestampTransform
createWithOffset(long offset
) {
65 return TmfTimestampTransform
.IDENTITY
;
67 return new TmfConstantTransform(offset
);
71 * Create an offsetted transform
74 * the offset in a timestamp with scale
75 * @return the offsetted transform
77 public static ITmfTimestampTransform
createWithOffset(ITmfTimestamp offset
) {
78 if (offset
.getValue() == 0) {
79 return TmfTimestampTransform
.IDENTITY
;
81 return new TmfConstantTransform(offset
);
85 * Create a timestamp transform corresponding to a linear equation, with
86 * slope and offset. The expected timestamp transform is such that f(t) =
87 * m*x + b, where m is the slope and b the offset.
93 * @return the transform
95 public static ITmfTimestampTransform
createLinear(double factor
, ITmfTimestamp offset
) {
97 return createWithOffset(offset
);
99 return new TmfTimestampTransformLinearFast(factor
, offset
.toNanos());
103 * Create a timestamp transform corresponding to a linear equation, with
104 * slope and offset. The expected timestamp transform is such that f(t) =
105 * m*x + b, where m is the slope and b the offset.
110 * the offset in nanoseconds
111 * @return the transform
113 public static ITmfTimestampTransform
createLinear(double factor
, long offset
) {
115 return createWithOffset(offset
);
117 return new TmfTimestampTransformLinearFast(factor
, offset
);
121 * Create a timestamp transform corresponding to a linear equation, with
122 * slope and offset expressed in BigDecimal. The expected timestamp
123 * transform is such that f(t) = m*x + b, where m is the slope and b the
129 * the offset in nanoseconds
130 * @return the transform
132 public static ITmfTimestampTransform
createLinear(BigDecimal factor
, BigDecimal offset
) {
133 if (factor
.equals(BigDecimal
.ONE
)) {
134 return createWithOffset(offset
.longValueExact());
136 return new TmfTimestampTransformLinearFast(factor
, offset
);
140 * Returns the file resource used to store synchronization formula. The file
145 * @return the synchronization file
147 private static @Nullable File
getSyncFormulaFile(@Nullable IResource resource
) {
148 if (resource
== null) {
152 String supplDirectory
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
153 return new File(supplDirectory
+ File
.separator
+ SYNCHRONIZATION_FORMULA_FILE
);
154 } catch (CoreException e
) {
161 * Returns the timestamp transform for a trace resource
165 * @return the timestamp transform
167 public static ITmfTimestampTransform
getTimestampTransform(@Nullable IResource resource
) {
168 File syncFile
= getSyncFormulaFile(resource
);
169 if (syncFile
!= null && syncFile
.exists()) {
170 /* Read the serialized object from file */
171 try (FileInputStream fis
= new FileInputStream(syncFile
);
172 ObjectInputStream ois
= new ObjectInputStream(fis
);) {
173 return NonNullUtils
.checkNotNull((ITmfTimestampTransform
) ois
.readObject());
174 } catch (ClassNotFoundException
| IOException e
) {
177 return TimestampTransformFactory
.getDefaultTransform();
181 * Sets the trace resource's timestamp transform
186 * The timestamp transform for all timestamps of this trace, or
189 public static void setTimestampTransform(@Nullable IResource resource
, @Nullable ITmfTimestampTransform tt
) {
190 /* Save the timestamp transform to a file */
191 File syncFile
= getSyncFormulaFile(resource
);
192 if (syncFile
!= null) {
193 if (syncFile
.exists()) {
199 /* Write the serialized object to file */
200 try (FileOutputStream fos
= new FileOutputStream(syncFile
, false);
201 ObjectOutputStream oos
= new ObjectOutputStream(fos
);) {
203 } catch (IOException e1
) {
204 Activator
.logError("Error writing timestamp transform for trace", e1
); //$NON-NLS-1$