TMF: Introduce a fast linear timestamp transform
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / synchronization / TimestampTransformFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial implementation and API
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.core.synchronization;
13
14 import java.io.File;
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;
21
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;
30
31 /**
32 * A factory to generate timestamp tranforms
33 *
34 * @author Matthew Khouzam
35 * @since 3.1
36 */
37 public final class TimestampTransformFactory {
38
39 private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$
40
41 private TimestampTransformFactory() {
42 }
43
44 /**
45 * Creates the identity timestamp transform
46 *
47 * @return The identity timestamp transform
48 */
49 public static ITmfTimestampTransform getDefaultTransform() {
50 return TmfTimestampTransform.IDENTITY;
51 }
52
53 /**
54 * Create an offsetted transform
55 *
56 * @param offset
57 * the offset in long format, nanosecond scale
58 * @return the offsetted transform
59 */
60 public static ITmfTimestampTransform createWithOffset(long offset) {
61 if (offset == 0) {
62 return TmfTimestampTransform.IDENTITY;
63 }
64 return new TmfConstantTransform(offset);
65 }
66
67 /**
68 * Create an offsetted transform
69 *
70 * @param offset
71 * the offset in a timestamp with scale
72 * @return the offsetted transform
73 */
74 public static ITmfTimestampTransform createWithOffset(ITmfTimestamp offset) {
75 if (offset.getValue() == 0) {
76 return TmfTimestampTransform.IDENTITY;
77 }
78 return new TmfConstantTransform(offset);
79 }
80
81 /**
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.
85 *
86 * @param factor
87 * the slope
88 * @param offset
89 * the offset
90 * @return the transform
91 */
92 public static ITmfTimestampTransform createLinear(double factor, ITmfTimestamp offset) {
93 if (factor == 1.0) {
94 return createWithOffset(offset);
95 }
96 return new TmfTimestampTransformLinearFast(factor, offset.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue());
97 }
98
99 /**
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.
103 *
104 * @param factor
105 * the slope
106 * @param offset
107 * the offset in nanoseconds
108 * @return the transform
109 */
110 public static ITmfTimestampTransform createLinear(double factor, long offset) {
111 if (factor == 1.0) {
112 return createWithOffset(offset);
113 }
114 return new TmfTimestampTransformLinearFast(factor, offset);
115 }
116
117 /**
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
121 * offset.
122 *
123 * @param factor
124 * the slope
125 * @param offset
126 * the offset in nanoseconds
127 * @return the transform
128 */
129 public static ITmfTimestampTransform createLinear(BigDecimal factor, BigDecimal offset) {
130 if (factor.equals(BigDecimal.ONE)) {
131 return createWithOffset(offset.longValueExact());
132 }
133 return new TmfTimestampTransformLinearFast(factor, offset);
134 }
135
136 /**
137 * Returns the file resource used to store synchronization formula. The file
138 * may not exist.
139 *
140 * @param resource
141 * the trace resource
142 * @return the synchronization file
143 */
144 private static File getSyncFormulaFile(IResource resource) {
145 if (resource == null) {
146 return null;
147 }
148 try {
149 String supplDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
150 return new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
151 } catch (CoreException e) {
152 /* Ignored */
153 }
154 return null;
155 }
156
157 /**
158 * Returns the timestamp transform for a trace resource
159 *
160 * @param resource
161 * the trace resource
162 * @return the timestamp transform
163 * @since 3.2
164 */
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) {
173 }
174 }
175 return TimestampTransformFactory.getDefaultTransform();
176 }
177
178 /**
179 * Sets the trace resource's timestamp transform
180 *
181 * @param resource
182 * the trace resource
183 * @param tt
184 * The timestamp transform for all timestamps of this trace, or
185 * null to clear it
186 * @since 3.2
187 */
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()) {
193 syncFile.delete();
194 }
195 if (tt == null) {
196 return;
197 }
198 /* Write the serialized object to file */
199 try (FileOutputStream fos = new FileOutputStream(syncFile, false);
200 ObjectOutputStream oos = new ObjectOutputStream(fos);) {
201 oos.writeObject(tt);
202 } catch (IOException e1) {
203 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
204 }
205 }
206 }
207
208 }
This page took 0.036282 seconds and 5 git commands to generate.