Commit | Line | Data |
---|---|---|
e5cfb647 MK |
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 | *******************************************************************************/ | |
2bdf0193 | 12 | package org.eclipse.tracecompass.tmf.core.synchronization; |
e5cfb647 | 13 | |
6b44794a MK |
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; | |
e5cfb647 MK |
20 | import java.math.BigDecimal; |
21 | ||
6b44794a MK |
22 | import org.eclipse.core.resources.IResource; |
23 | import org.eclipse.core.runtime.CoreException; | |
5dca27ae GB |
24 | import org.eclipse.jdt.annotation.NonNullByDefault; |
25 | import org.eclipse.jdt.annotation.Nullable; | |
26 | import org.eclipse.tracecompass.common.core.NonNullUtils; | |
2bdf0193 AM |
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; | |
5745c0a3 | 30 | import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinearFast; |
2bdf0193 AM |
31 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; |
32 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
e5cfb647 MK |
33 | |
34 | /** | |
35 | * A factory to generate timestamp tranforms | |
36 | * | |
37 | * @author Matthew Khouzam | |
e5cfb647 | 38 | */ |
5dca27ae | 39 | @NonNullByDefault |
e5cfb647 MK |
40 | public final class TimestampTransformFactory { |
41 | ||
6b44794a MK |
42 | private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$ |
43 | ||
e5cfb647 MK |
44 | private TimestampTransformFactory() { |
45 | } | |
46 | ||
9ffcda7d GB |
47 | /** |
48 | * Creates the identity timestamp transform | |
49 | * | |
50 | * @return The identity timestamp transform | |
51 | */ | |
52 | public static ITmfTimestampTransform getDefaultTransform() { | |
53 | return TmfTimestampTransform.IDENTITY; | |
54 | } | |
55 | ||
e5cfb647 MK |
56 | /** |
57 | * Create an offsetted transform | |
58 | * | |
59 | * @param offset | |
60 | * the offset in long format, nanosecond scale | |
61 | * @return the offsetted transform | |
62 | */ | |
9ffcda7d | 63 | public static ITmfTimestampTransform createWithOffset(long offset) { |
e5cfb647 MK |
64 | if (offset == 0) { |
65 | return TmfTimestampTransform.IDENTITY; | |
66 | } | |
67 | return new TmfConstantTransform(offset); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Create an offsetted transform | |
72 | * | |
73 | * @param offset | |
74 | * the offset in a timestamp with scale | |
75 | * @return the offsetted transform | |
76 | */ | |
9ffcda7d | 77 | public static ITmfTimestampTransform createWithOffset(ITmfTimestamp offset) { |
e5cfb647 MK |
78 | if (offset.getValue() == 0) { |
79 | return TmfTimestampTransform.IDENTITY; | |
80 | } | |
81 | return new TmfConstantTransform(offset); | |
82 | } | |
83 | ||
84 | /** | |
9ffcda7d GB |
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. | |
e5cfb647 MK |
88 | * |
89 | * @param factor | |
90 | * the slope | |
91 | * @param offset | |
92 | * the offset | |
93 | * @return the transform | |
94 | */ | |
9ffcda7d | 95 | public static ITmfTimestampTransform createLinear(double factor, ITmfTimestamp offset) { |
e5cfb647 | 96 | if (factor == 1.0) { |
9ffcda7d | 97 | return createWithOffset(offset); |
e5cfb647 | 98 | } |
5745c0a3 | 99 | return new TmfTimestampTransformLinearFast(factor, offset.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue()); |
e5cfb647 MK |
100 | } |
101 | ||
102 | /** | |
9ffcda7d GB |
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. | |
e5cfb647 MK |
106 | * |
107 | * @param factor | |
108 | * the slope | |
109 | * @param offset | |
110 | * the offset in nanoseconds | |
111 | * @return the transform | |
112 | */ | |
9ffcda7d | 113 | public static ITmfTimestampTransform createLinear(double factor, long offset) { |
e5cfb647 | 114 | if (factor == 1.0) { |
9ffcda7d | 115 | return createWithOffset(offset); |
e5cfb647 | 116 | } |
5745c0a3 | 117 | return new TmfTimestampTransformLinearFast(factor, offset); |
e5cfb647 MK |
118 | } |
119 | ||
120 | /** | |
9ffcda7d GB |
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 | |
124 | * offset. | |
e5cfb647 MK |
125 | * |
126 | * @param factor | |
127 | * the slope | |
128 | * @param offset | |
129 | * the offset in nanoseconds | |
130 | * @return the transform | |
131 | */ | |
9ffcda7d | 132 | public static ITmfTimestampTransform createLinear(BigDecimal factor, BigDecimal offset) { |
e5cfb647 | 133 | if (factor.equals(BigDecimal.ONE)) { |
9ffcda7d | 134 | return createWithOffset(offset.longValueExact()); |
e5cfb647 | 135 | } |
5745c0a3 | 136 | return new TmfTimestampTransformLinearFast(factor, offset); |
e5cfb647 MK |
137 | } |
138 | ||
6b44794a MK |
139 | /** |
140 | * Returns the file resource used to store synchronization formula. The file | |
141 | * may not exist. | |
142 | * | |
143 | * @param resource | |
144 | * the trace resource | |
145 | * @return the synchronization file | |
146 | */ | |
5dca27ae | 147 | private static @Nullable File getSyncFormulaFile(@Nullable IResource resource) { |
6b44794a MK |
148 | if (resource == null) { |
149 | return null; | |
150 | } | |
151 | try { | |
152 | String supplDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER); | |
153 | return new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE); | |
154 | } catch (CoreException e) { | |
155 | /* Ignored */ | |
156 | } | |
157 | return null; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Returns the timestamp transform for a trace resource | |
162 | * | |
163 | * @param resource | |
164 | * the trace resource | |
165 | * @return the timestamp transform | |
6b44794a | 166 | */ |
5dca27ae | 167 | public static ITmfTimestampTransform getTimestampTransform(@Nullable IResource resource) { |
6b44794a MK |
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);) { | |
5dca27ae | 173 | return NonNullUtils.checkNotNull((ITmfTimestampTransform) ois.readObject()); |
6b44794a MK |
174 | } catch (ClassNotFoundException | IOException e) { |
175 | } | |
176 | } | |
177 | return TimestampTransformFactory.getDefaultTransform(); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Sets the trace resource's timestamp transform | |
182 | * | |
183 | * @param resource | |
184 | * the trace resource | |
185 | * @param tt | |
186 | * The timestamp transform for all timestamps of this trace, or | |
187 | * null to clear it | |
6b44794a | 188 | */ |
5dca27ae | 189 | public static void setTimestampTransform(@Nullable IResource resource, @Nullable ITmfTimestampTransform tt) { |
6b44794a MK |
190 | /* Save the timestamp transform to a file */ |
191 | File syncFile = getSyncFormulaFile(resource); | |
192 | if (syncFile != null) { | |
193 | if (syncFile.exists()) { | |
194 | syncFile.delete(); | |
195 | } | |
196 | if (tt == null) { | |
197 | return; | |
198 | } | |
199 | /* Write the serialized object to file */ | |
200 | try (FileOutputStream fos = new FileOutputStream(syncFile, false); | |
201 | ObjectOutputStream oos = new ObjectOutputStream(fos);) { | |
202 | oos.writeObject(tt); | |
203 | } catch (IOException e1) { | |
204 | Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$ | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
e5cfb647 | 209 | } |