Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
089a4872 | 2 | * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal |
0bfb7d06 | 3 | * |
8c8bf09f ASL |
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 | |
0bfb7d06 | 8 | * |
8c8bf09f | 9 | * Contributors: |
20658947 FC |
10 | * Francois Chouinard - Initial API and implementation |
11 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 | |
ea271da6 | 12 | * Patrick Tasse - Updated for removal of context clone |
e73a4ba5 GB |
13 | * Geneviève Bastien - Added timestamp transforms, its saving to file and |
14 | * timestamp creation functions | |
8c8bf09f ASL |
15 | *******************************************************************************/ |
16 | ||
6c13869b | 17 | package org.eclipse.linuxtools.tmf.core.trace; |
8c8bf09f | 18 | |
6f4a1d2b | 19 | import java.io.File; |
e73a4ba5 | 20 | import java.io.FileInputStream; |
e73a4ba5 GB |
21 | import java.io.FileOutputStream; |
22 | import java.io.IOException; | |
23 | import java.io.ObjectInputStream; | |
24 | import java.io.ObjectOutputStream; | |
35c160d9 | 25 | import java.util.Collections; |
ff3f02c8 | 26 | import java.util.HashSet; |
35c160d9 | 27 | import java.util.LinkedHashMap; |
a51b2b9f | 28 | import java.util.Map; |
c068a752 | 29 | import java.util.Map.Entry; |
ff3f02c8 | 30 | import java.util.Set; |
8c8bf09f | 31 | |
e73a4ba5 | 32 | import org.eclipse.core.resources.IFolder; |
828e5592 | 33 | import org.eclipse.core.resources.IResource; |
faa38350 | 34 | import org.eclipse.core.runtime.CoreException; |
42459d24 | 35 | import org.eclipse.core.runtime.IStatus; |
b22a582a | 36 | import org.eclipse.core.runtime.MultiStatus; |
032ecd45 | 37 | import org.eclipse.core.runtime.Path; |
42459d24 | 38 | import org.eclipse.core.runtime.Status; |
b22a582a | 39 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
e73a4ba5 | 40 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
c068a752 GB |
41 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; |
42 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper; | |
43 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager; | |
6c13869b | 44 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
72f1e62a | 45 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
c068a752 | 46 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; |
b4f71e4a | 47 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
5419a136 | 48 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
faa38350 | 49 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
fec1ac0b | 50 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
faa38350 PT |
51 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal; |
52 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal; | |
032ecd45 | 53 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal; |
e73a4ba5 GB |
54 | import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; |
55 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform; | |
3bd46eef AM |
56 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
57 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
58 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
a3db8436 AM |
59 | import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer; |
60 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; | |
61 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; | |
8c8bf09f ASL |
62 | |
63 | /** | |
09e86496 FC |
64 | * Abstract implementation of ITmfTrace. |
65 | * <p> | |
13cb5f43 FC |
66 | * Since the concept of 'location' is trace specific, the concrete classes have |
67 | * to provide the related methods, namely: | |
68 | * <ul> | |
69 | * <li> public ITmfLocation<?> getCurrentLocation() | |
70 | * <li> public double getLocationRatio(ITmfLocation<?> location) | |
71 | * <li> public ITmfContext seekEvent(ITmfLocation<?> location) | |
72 | * <li> public ITmfContext seekEvent(double ratio) | |
da1a4b39 | 73 | * <li> public IStatus validate(IProject project, String path) |
13cb5f43 FC |
74 | * </ul> |
75 | * A concrete trace must provide its corresponding parser. A common way to | |
76 | * accomplish this is by making the concrete class extend TmfTrace and | |
77 | * implement ITmfEventParser. | |
78 | * <p> | |
79 | * The concrete class can either specify its own indexer or use the provided | |
80 | * TmfCheckpointIndexer (default). In this case, the trace cache size will be | |
81 | * used as checkpoint interval. | |
0bfb7d06 | 82 | * |
f7703ed6 FC |
83 | * @version 1.0 |
84 | * @author Francois Chouinard | |
85 | * | |
f7703ed6 FC |
86 | * @see ITmfEvent |
87 | * @see ITmfTraceIndexer | |
88 | * @see ITmfEventParser | |
8c8bf09f | 89 | */ |
6256d8ad | 90 | public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace { |
62d1696a | 91 | |
e31e01e8 | 92 | // ------------------------------------------------------------------------ |
8c8bf09f | 93 | // Attributes |
e31e01e8 | 94 | // ------------------------------------------------------------------------ |
8c8bf09f | 95 | |
09e86496 FC |
96 | // The resource used for persistent properties for this trace |
97 | private IResource fResource; | |
98 | ||
b0a282fb | 99 | // The trace path |
12c155f5 | 100 | private String fPath; |
b0a282fb | 101 | |
0316808c FC |
102 | // The trace cache page size |
103 | private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; | |
62d1696a | 104 | |
0316808c | 105 | // The number of events collected (so far) |
e9a6e38e | 106 | private volatile long fNbEvents = 0; |
62d1696a FC |
107 | |
108 | // The time span of the event stream | |
9cbe7899 | 109 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG; |
a4115405 | 110 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; |
62d1696a | 111 | |
0316808c FC |
112 | // The trace streaming interval (0 = no streaming) |
113 | private long fStreamingInterval = 0; | |
085d898f | 114 | |
0316808c | 115 | // The trace indexer |
6256d8ad | 116 | private ITmfTraceIndexer fIndexer; |
20658947 | 117 | |
0316808c | 118 | // The trace parser |
6256d8ad | 119 | private ITmfEventParser fParser; |
7e6347b0 | 120 | |
e73a4ba5 GB |
121 | private ITmfTimestampTransform fTsTransform; |
122 | ||
ff3f02c8 AM |
123 | private final Map<String, IAnalysisModule> fAnalysisModules = |
124 | Collections.synchronizedMap(new LinkedHashMap<String, IAnalysisModule>()); | |
c068a752 | 125 | |
e73a4ba5 GB |
126 | private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$ |
127 | ||
e31e01e8 | 128 | // ------------------------------------------------------------------------ |
3791b5df | 129 | // Construction |
e31e01e8 | 130 | // ------------------------------------------------------------------------ |
8c8bf09f | 131 | |
62d1696a | 132 | /** |
3791b5df | 133 | * The default, parameterless, constructor |
62d1696a | 134 | */ |
3791b5df FC |
135 | public TmfTrace() { |
136 | super(); | |
ab186fbb | 137 | fIndexer = createIndexer(DEFAULT_BLOCK_SIZE); |
05bd3318 FC |
138 | } |
139 | ||
140 | /** | |
8cf330ae | 141 | * Full constructor. |
0bfb7d06 | 142 | * |
8cf330ae AM |
143 | * @param resource |
144 | * The resource associated to the trace | |
145 | * @param type | |
146 | * The type of events that will be read from this trace | |
147 | * @param path | |
148 | * The path to the trace on the filesystem | |
149 | * @param cacheSize | |
150 | * The trace cache size. Pass '-1' to use the default specified | |
151 | * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE} | |
152 | * @param interval | |
153 | * The trace streaming interval. You can use '0' for post-mortem | |
154 | * traces. | |
8cf330ae AM |
155 | * @param parser |
156 | * The trace event parser. Use 'null' if (and only if) the trace | |
157 | * object itself is also the ITmfEventParser to be used. | |
158 | * @throws TmfTraceException | |
159 | * If something failed during the opening | |
20658947 | 160 | */ |
8cf330ae AM |
161 | protected TmfTrace(final IResource resource, |
162 | final Class<? extends ITmfEvent> type, | |
163 | final String path, | |
164 | final int cacheSize, | |
165 | final long interval, | |
8cf330ae AM |
166 | final ITmfEventParser parser) |
167 | throws TmfTraceException { | |
00641a97 | 168 | super(); |
0316808c | 169 | fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; |
3791b5df | 170 | fStreamingInterval = interval; |
13cb5f43 | 171 | fParser = parser; |
09e86496 | 172 | initialize(resource, path, type); |
8c8bf09f ASL |
173 | } |
174 | ||
3791b5df FC |
175 | /** |
176 | * Copy constructor | |
0bfb7d06 | 177 | * |
3791b5df | 178 | * @param trace the original trace |
063f0d27 | 179 | * @throws TmfTraceException Should not happen usually |
3791b5df | 180 | */ |
6256d8ad | 181 | public TmfTrace(final TmfTrace trace) throws TmfTraceException { |
3791b5df | 182 | super(); |
0316808c | 183 | if (trace == null) { |
3791b5df | 184 | throw new IllegalArgumentException(); |
0316808c | 185 | } |
20658947 FC |
186 | fCacheSize = trace.getCacheSize(); |
187 | fStreamingInterval = trace.getStreamingInterval(); | |
13cb5f43 FC |
188 | fParser = trace.fParser; |
189 | initialize(trace.getResource(), trace.getPath(), trace.getEventType()); | |
3791b5df FC |
190 | } |
191 | ||
032ecd45 MAL |
192 | /** |
193 | * Creates the indexer instance. Classes extending this class can override | |
194 | * this to provide a different indexer implementation. | |
195 | * | |
196 | * @param interval the checkpoints interval | |
197 | * | |
198 | * @return the indexer | |
199 | * @since 3.0 | |
200 | */ | |
201 | protected ITmfTraceIndexer createIndexer(int interval) { | |
202 | return new TmfCheckpointIndexer(this, interval); | |
203 | } | |
204 | ||
7e6347b0 FC |
205 | // ------------------------------------------------------------------------ |
206 | // ITmfTrace - Initializers | |
207 | // ------------------------------------------------------------------------ | |
208 | ||
339d539c PT |
209 | @Override |
210 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type, String name) throws TmfTraceException { | |
211 | setName(name); | |
212 | initTrace(resource, path, type); | |
213 | } | |
214 | ||
7e6347b0 | 215 | @Override |
6256d8ad | 216 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
7e6347b0 | 217 | initialize(resource, path, type); |
7e6347b0 FC |
218 | } |
219 | ||
09e86496 | 220 | /** |
1703b536 | 221 | * Initialize the trace common attributes and the base component. |
0bfb7d06 MK |
222 | * |
223 | * @param resource the Eclipse resource (trace) | |
1703b536 FC |
224 | * @param path the trace path |
225 | * @param type the trace event type | |
0bfb7d06 | 226 | * |
6f4e8ec0 | 227 | * @throws TmfTraceException If something failed during the initialization |
3791b5df | 228 | */ |
248af329 AM |
229 | protected void initialize(final IResource resource, |
230 | final String path, | |
231 | final Class<? extends ITmfEvent> type) | |
232 | throws TmfTraceException { | |
0316808c | 233 | if (path == null) { |
b4f71e4a | 234 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
0316808c | 235 | } |
3791b5df | 236 | fPath = path; |
1703b536 | 237 | fResource = resource; |
339d539c PT |
238 | String traceName = getName(); |
239 | if (traceName == null || traceName.isEmpty()) { | |
240 | traceName = (resource != null) ? resource.getName() : new Path(path).lastSegment(); | |
1703b536 | 241 | } |
2352aed9 FC |
242 | if (fParser == null) { |
243 | if (this instanceof ITmfEventParser) { | |
6256d8ad | 244 | fParser = (ITmfEventParser) this; |
2352aed9 FC |
245 | } else { |
246 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ | |
247 | } | |
248 | } | |
3791b5df | 249 | super.init(traceName, type); |
fec1ac0b BH |
250 | // register as VIP after super.init() because TmfComponent registers to signal manager there |
251 | TmfSignalManager.registerVIP(this); | |
1a3f1ec3 GB |
252 | if (fIndexer != null) { |
253 | fIndexer.dispose(); | |
254 | } | |
ab186fbb | 255 | fIndexer = createIndexer(fCacheSize); |
3791b5df FC |
256 | } |
257 | ||
2352aed9 FC |
258 | /** |
259 | * Indicates if the path points to an existing file/directory | |
0bfb7d06 | 260 | * |
2352aed9 FC |
261 | * @param path the path to test |
262 | * @return true if the file/directory exists | |
3791b5df | 263 | */ |
2352aed9 | 264 | protected boolean fileExists(final String path) { |
085d898f | 265 | final File file = new File(path); |
3791b5df FC |
266 | return file.exists(); |
267 | } | |
268 | ||
c7e1020d | 269 | /** |
51e75066 | 270 | * @since 2.0 |
c7e1020d | 271 | */ |
51e75066 AM |
272 | @Override |
273 | public void indexTrace(boolean waitForCompletion) { | |
9e0640dc | 274 | getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion); |
c7e1020d FC |
275 | } |
276 | ||
c068a752 GB |
277 | /** |
278 | * Instantiate the applicable analysis modules and executes the analysis | |
279 | * modules that are meant to be automatically executed | |
280 | * | |
281 | * @return An IStatus indicating whether the analysis could be run | |
282 | * successfully or not | |
283 | * @since 3.0 | |
284 | */ | |
285 | protected IStatus executeAnalysis() { | |
286 | MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null); | |
287 | Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(this.getClass()); | |
288 | for (IAnalysisModuleHelper helper : modules.values()) { | |
289 | try { | |
290 | IAnalysisModule module = helper.newModule(this); | |
291 | fAnalysisModules.put(module.getId(), module); | |
292 | if (module.isAutomatic()) { | |
293 | status.add(module.schedule()); | |
294 | } | |
295 | } catch (TmfAnalysisException e) { | |
26683871 | 296 | status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage())); |
c068a752 GB |
297 | } |
298 | } | |
299 | return status; | |
300 | } | |
301 | ||
c4767854 AM |
302 | /** |
303 | * @since 3.0 | |
304 | */ | |
c068a752 | 305 | @Override |
ff3f02c8 | 306 | public IAnalysisModule getAnalysisModule(String analysisId) { |
c068a752 GB |
307 | return fAnalysisModules.get(analysisId); |
308 | } | |
309 | ||
ff3f02c8 AM |
310 | |
311 | /** | |
312 | * @since 3.0 | |
313 | */ | |
314 | @Override | |
315 | public Iterable<IAnalysisModule> getAnalysisModules() { | |
316 | synchronized (fAnalysisModules) { | |
317 | Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values()); | |
318 | return modules; | |
319 | } | |
320 | } | |
321 | ||
c4767854 AM |
322 | /** |
323 | * @since 3.0 | |
324 | */ | |
c068a752 | 325 | @Override |
ff3f02c8 AM |
326 | public <T extends IAnalysisModule> T getAnalysisModuleOfClass(Class<T> moduleClass, String id) { |
327 | Iterable<T> modules = getAnalysisModulesOfClass(moduleClass); | |
328 | for (T module : modules) { | |
329 | if (id.equals(module.getId())) { | |
330 | return module; | |
c068a752 GB |
331 | } |
332 | } | |
ff3f02c8 | 333 | return null; |
c068a752 GB |
334 | } |
335 | ||
c4767854 AM |
336 | /** |
337 | * @since 3.0 | |
338 | */ | |
c068a752 | 339 | @Override |
ff3f02c8 AM |
340 | public <T> Iterable<T> getAnalysisModulesOfClass(Class<T> moduleClass) { |
341 | Set<T> modules = new HashSet<>(); | |
342 | synchronized (fAnalysisModules) { | |
343 | for (Entry<String, IAnalysisModule> entry : fAnalysisModules.entrySet()) { | |
344 | if (moduleClass.isAssignableFrom(entry.getValue().getClass())) { | |
345 | modules.add(moduleClass.cast(entry.getValue())); | |
346 | } | |
347 | } | |
348 | } | |
349 | return modules; | |
c068a752 GB |
350 | } |
351 | ||
b5ee6881 FC |
352 | /** |
353 | * Clears the trace | |
354 | */ | |
355 | @Override | |
356 | public synchronized void dispose() { | |
1a4205d9 | 357 | /* Clean up the index if applicable */ |
77551cc2 FC |
358 | if (getIndexer() != null) { |
359 | getIndexer().dispose(); | |
360 | } | |
1a4205d9 | 361 | |
a1529f38 | 362 | /* Clean up the analysis modules */ |
ff3f02c8 AM |
363 | synchronized (fAnalysisModules) { |
364 | for (IAnalysisModule module : fAnalysisModules.values()) { | |
365 | module.dispose(); | |
366 | } | |
6ef5ae35 | 367 | fAnalysisModules.clear(); |
a1529f38 AM |
368 | } |
369 | ||
b5ee6881 FC |
370 | super.dispose(); |
371 | } | |
372 | ||
3791b5df | 373 | // ------------------------------------------------------------------------ |
09e86496 | 374 | // ITmfTrace - Basic getters |
e31e01e8 | 375 | // ------------------------------------------------------------------------ |
8c8bf09f | 376 | |
25e48683 | 377 | @Override |
0f89d4ba AM |
378 | public Class<? extends ITmfEvent> getEventType() { |
379 | return super.getType(); | |
25e48683 FC |
380 | } |
381 | ||
d4011df2 | 382 | @Override |
09e86496 FC |
383 | public IResource getResource() { |
384 | return fResource; | |
8c8bf09f ASL |
385 | } |
386 | ||
d4011df2 | 387 | @Override |
09e86496 FC |
388 | public String getPath() { |
389 | return fPath; | |
8c8bf09f ASL |
390 | } |
391 | ||
20658947 FC |
392 | @Override |
393 | public int getCacheSize() { | |
394 | return fCacheSize; | |
395 | } | |
396 | ||
20658947 FC |
397 | @Override |
398 | public long getStreamingInterval() { | |
399 | return fStreamingInterval; | |
400 | } | |
401 | ||
0316808c FC |
402 | /** |
403 | * @return the trace indexer | |
a3db8436 | 404 | * @since 3.0 |
0316808c | 405 | */ |
6256d8ad | 406 | protected ITmfTraceIndexer getIndexer() { |
0316808c FC |
407 | return fIndexer; |
408 | } | |
409 | ||
410 | /** | |
411 | * @return the trace parser | |
412 | */ | |
6256d8ad | 413 | protected ITmfEventParser getParser() { |
0316808c FC |
414 | return fParser; |
415 | } | |
416 | ||
09e86496 FC |
417 | // ------------------------------------------------------------------------ |
418 | // ITmfTrace - Trace characteristics getters | |
419 | // ------------------------------------------------------------------------ | |
420 | ||
d4011df2 | 421 | @Override |
e9a6e38e | 422 | public long getNbEvents() { |
3791b5df | 423 | return fNbEvents; |
b0a282fb FC |
424 | } |
425 | ||
3bd46eef AM |
426 | /** |
427 | * @since 2.0 | |
62d1696a | 428 | */ |
d4011df2 | 429 | @Override |
12c155f5 | 430 | public TmfTimeRange getTimeRange() { |
cb866e08 | 431 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
432 | } |
433 | ||
3bd46eef AM |
434 | /** |
435 | * @since 2.0 | |
e31e01e8 | 436 | */ |
d4011df2 | 437 | @Override |
4df4581d | 438 | public ITmfTimestamp getStartTime() { |
4593bd5b | 439 | return fStartTime; |
146a887c FC |
440 | } |
441 | ||
3bd46eef AM |
442 | /** |
443 | * @since 2.0 | |
e31e01e8 | 444 | */ |
d4011df2 | 445 | @Override |
4df4581d | 446 | public ITmfTimestamp getEndTime() { |
4593bd5b | 447 | return fEndTime; |
20658947 FC |
448 | } |
449 | ||
d7ee91bb | 450 | /** |
d7ee91bb PT |
451 | * @since 2.0 |
452 | */ | |
66262ad8 BH |
453 | @Override |
454 | public ITmfTimestamp getInitialRangeOffset() { | |
d7ee91bb PT |
455 | final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
456 | return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); | |
457 | } | |
458 | ||
bb52f9bc GB |
459 | /** |
460 | * @since 3.0 | |
461 | */ | |
462 | @Override | |
463 | public String getHostId() { | |
464 | return this.getName(); | |
465 | } | |
466 | ||
20658947 | 467 | // ------------------------------------------------------------------------ |
d7ee91bb | 468 | // Convenience setters |
20658947 FC |
469 | // ------------------------------------------------------------------------ |
470 | ||
0316808c FC |
471 | /** |
472 | * Set the trace cache size. Must be done at initialization time. | |
0bfb7d06 | 473 | * |
0316808c FC |
474 | * @param cacheSize The trace cache size |
475 | */ | |
476 | protected void setCacheSize(final int cacheSize) { | |
477 | fCacheSize = cacheSize; | |
478 | } | |
479 | ||
480 | /** | |
481 | * Set the trace known number of events. This can be quite dynamic | |
482 | * during indexing or for live traces. | |
0bfb7d06 | 483 | * |
0316808c FC |
484 | * @param nbEvents The number of events |
485 | */ | |
486 | protected synchronized void setNbEvents(final long nbEvents) { | |
487 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; | |
488 | } | |
489 | ||
20658947 FC |
490 | /** |
491 | * Update the trace events time range | |
0bfb7d06 | 492 | * |
20658947 | 493 | * @param range the new time range |
3bd46eef | 494 | * @since 2.0 |
20658947 FC |
495 | */ |
496 | protected void setTimeRange(final TmfTimeRange range) { | |
4593bd5b AM |
497 | fStartTime = range.getStartTime(); |
498 | fEndTime = range.getEndTime(); | |
20658947 FC |
499 | } |
500 | ||
501 | /** | |
502 | * Update the trace chronologically first event timestamp | |
0bfb7d06 | 503 | * |
20658947 | 504 | * @param startTime the new first event timestamp |
3bd46eef | 505 | * @since 2.0 |
20658947 FC |
506 | */ |
507 | protected void setStartTime(final ITmfTimestamp startTime) { | |
4593bd5b | 508 | fStartTime = startTime; |
20658947 FC |
509 | } |
510 | ||
511 | /** | |
512 | * Update the trace chronologically last event timestamp | |
0bfb7d06 | 513 | * |
20658947 | 514 | * @param endTime the new last event timestamp |
3bd46eef | 515 | * @since 2.0 |
20658947 FC |
516 | */ |
517 | protected void setEndTime(final ITmfTimestamp endTime) { | |
4593bd5b | 518 | fEndTime = endTime; |
20658947 FC |
519 | } |
520 | ||
521 | /** | |
0316808c | 522 | * Set the polling interval for live traces (default = 0 = no streaming). |
0bfb7d06 | 523 | * |
20658947 FC |
524 | * @param interval the new trace streaming interval |
525 | */ | |
526 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 527 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
528 | } |
529 | ||
0316808c FC |
530 | /** |
531 | * Set the trace parser. Must be done at initialization time. | |
0bfb7d06 | 532 | * |
0316808c FC |
533 | * @param parser the new trace parser |
534 | */ | |
6256d8ad | 535 | protected void setParser(final ITmfEventParser parser) { |
0316808c FC |
536 | fParser = parser; |
537 | } | |
538 | ||
09e86496 | 539 | // ------------------------------------------------------------------------ |
7e6347b0 | 540 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
541 | // ------------------------------------------------------------------------ |
542 | ||
1b70b6dc | 543 | @Override |
7e6347b0 | 544 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 545 | |
7e6347b0 | 546 | // A rank <= 0 indicates to seek the first event |
2352aed9 | 547 | if (rank <= 0) { |
1e1bef82 | 548 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
549 | context.setRank(0); |
550 | return context; | |
551 | } | |
09e86496 | 552 | |
09e86496 | 553 | // Position the trace at the checkpoint |
7e6347b0 | 554 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
555 | |
556 | // And locate the requested event context | |
7e6347b0 FC |
557 | long pos = context.getRank(); |
558 | if (pos < rank) { | |
c32744d6 | 559 | ITmfEvent event = getNext(context); |
0bfb7d06 | 560 | while ((event != null) && (++pos < rank)) { |
c32744d6 | 561 | event = getNext(context); |
7e6347b0 | 562 | } |
09e86496 FC |
563 | } |
564 | return context; | |
1b70b6dc PT |
565 | } |
566 | ||
3bd46eef AM |
567 | /** |
568 | * @since 2.0 | |
09e86496 FC |
569 | */ |
570 | @Override | |
7e6347b0 | 571 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 572 | |
7e6347b0 | 573 | // A null timestamp indicates to seek the first event |
2352aed9 | 574 | if (timestamp == null) { |
1e1bef82 | 575 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
576 | context.setRank(0); |
577 | return context; | |
578 | } | |
09e86496 | 579 | |
1703b536 | 580 | // Position the trace at the checkpoint |
408e65d2 | 581 | ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
582 | |
583 | // And locate the requested event context | |
ea271da6 PT |
584 | ITmfLocation previousLocation = context.getLocation(); |
585 | long previousRank = context.getRank(); | |
586 | ITmfEvent event = getNext(context); | |
7e6347b0 | 587 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
ea271da6 PT |
588 | previousLocation = context.getLocation(); |
589 | previousRank = context.getRank(); | |
590 | event = getNext(context); | |
09e86496 | 591 | } |
0316808c FC |
592 | if (event == null) { |
593 | context.setLocation(null); | |
594 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
ea271da6 PT |
595 | } else { |
596 | context.dispose(); | |
597 | context = seekEvent(previousLocation); | |
598 | context.setRank(previousRank); | |
0316808c | 599 | } |
09e86496 FC |
600 | return context; |
601 | } | |
0283f7ff | 602 | |
09e86496 FC |
603 | // ------------------------------------------------------------------------ |
604 | // ITmfTrace - Read operations (returning an actual event) | |
605 | // ------------------------------------------------------------------------ | |
606 | ||
d4011df2 | 607 | @Override |
6256d8ad | 608 | public synchronized ITmfEvent getNext(final ITmfContext context) { |
09e86496 | 609 | // parseEvent() does not update the context |
6256d8ad | 610 | final ITmfEvent event = fParser.parseEvent(context); |
09e86496 | 611 | if (event != null) { |
d337369a | 612 | updateAttributes(context, event.getTimestamp()); |
09e86496 FC |
613 | context.setLocation(getCurrentLocation()); |
614 | context.increaseRank(); | |
615 | processEvent(event); | |
616 | } | |
617 | return event; | |
618 | } | |
619 | ||
620 | /** | |
d337369a | 621 | * Hook for special event processing by the concrete class |
7e6347b0 | 622 | * (called by TmfTrace.getEvent()) |
0bfb7d06 | 623 | * |
d337369a | 624 | * @param event the event |
09e86496 FC |
625 | */ |
626 | protected void processEvent(final ITmfEvent event) { | |
d337369a | 627 | // Do nothing |
09e86496 FC |
628 | } |
629 | ||
d337369a FC |
630 | /** |
631 | * Update the trace attributes | |
0bfb7d06 | 632 | * |
d337369a | 633 | * @param context the current trace context |
2848c377 | 634 | * @param timestamp the corresponding timestamp |
3bd46eef | 635 | * @since 2.0 |
d337369a FC |
636 | */ |
637 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { | |
0bfb7d06 | 638 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) { |
4593bd5b | 639 | fStartTime = timestamp; |
09e86496 | 640 | } |
0bfb7d06 | 641 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) { |
4593bd5b | 642 | fEndTime = timestamp; |
09e86496 FC |
643 | } |
644 | if (context.hasValidRank()) { | |
d337369a | 645 | long rank = context.getRank(); |
09e86496 FC |
646 | if (fNbEvents <= rank) { |
647 | fNbEvents = rank + 1; | |
648 | } | |
200789b3 AM |
649 | if (fIndexer != null) { |
650 | fIndexer.updateIndex(context, timestamp); | |
651 | } | |
09e86496 | 652 | } |
abfad0aa FC |
653 | } |
654 | ||
3791b5df | 655 | // ------------------------------------------------------------------------ |
d337369a | 656 | // TmfDataProvider |
3791b5df FC |
657 | // ------------------------------------------------------------------------ |
658 | ||
77c4a6df AM |
659 | /** |
660 | * @since 2.0 | |
d337369a | 661 | */ |
3791b5df | 662 | @Override |
fd3f1eff | 663 | public synchronized ITmfContext armRequest(final ITmfEventRequest request) { |
faa38350 PT |
664 | if (executorIsShutdown()) { |
665 | return null; | |
666 | } | |
fd3f1eff AM |
667 | if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime()) |
668 | && (request.getIndex() == 0)) { | |
669 | final ITmfContext context = seekEvent(request.getRange().getStartTime()); | |
670 | request.setStartIndex((int) context.getRank()); | |
8584dc20 | 671 | return context; |
8584dc20 | 672 | |
5419a136 AM |
673 | } |
674 | return seekEvent(request.getIndex()); | |
3791b5df FC |
675 | } |
676 | ||
faa38350 PT |
677 | // ------------------------------------------------------------------------ |
678 | // Signal handlers | |
679 | // ------------------------------------------------------------------------ | |
680 | ||
681 | /** | |
682 | * Handler for the Trace Opened signal | |
683 | * | |
684 | * @param signal | |
685 | * The incoming signal | |
686 | * @since 2.0 | |
687 | */ | |
688 | @TmfSignalHandler | |
689 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
b9a5bf8f AM |
690 | boolean signalIsForUs = false; |
691 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) { | |
692 | if (trace == this) { | |
693 | signalIsForUs = true; | |
fe0c44c4 | 694 | break; |
faa38350 PT |
695 | } |
696 | } | |
faa38350 | 697 | |
b9a5bf8f | 698 | if (!signalIsForUs) { |
fe0c44c4 AM |
699 | return; |
700 | } | |
701 | ||
702 | /* | |
b9a5bf8f | 703 | * The signal is either for this trace, or for an experiment containing |
fe0c44c4 AM |
704 | * this trace. |
705 | */ | |
be4a197a | 706 | IStatus status = executeAnalysis(); |
b22a582a AM |
707 | if (!status.isOK()) { |
708 | Activator.log(status); | |
fe0c44c4 AM |
709 | } |
710 | ||
b5e8ee95 | 711 | TmfTraceManager.refreshSupplementaryFiles(this); |
fe0c44c4 | 712 | |
faa38350 | 713 | if (signal.getTrace() == this) { |
f8fc4a3a | 714 | /* Additionally, the signal is directly for this trace. */ |
faa38350 PT |
715 | if (getNbEvents() == 0) { |
716 | return; | |
717 | } | |
718 | ||
f8fc4a3a PT |
719 | /* For a streaming trace, the range updated signal should be sent |
720 | * by the subclass when a new safe time is determined. | |
721 | */ | |
722 | if (getStreamingInterval() > 0) { | |
723 | return; | |
724 | } | |
725 | ||
faa38350 PT |
726 | final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH); |
727 | final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange); | |
728 | ||
729 | // Broadcast in separate thread to prevent deadlock | |
089a4872 | 730 | broadcastAsync(rangeUpdatedsignal); |
faa38350 PT |
731 | return; |
732 | } | |
733 | } | |
734 | ||
735 | /** | |
736 | * Signal handler for the TmfTraceRangeUpdatedSignal signal | |
737 | * | |
738 | * @param signal The incoming signal | |
739 | * @since 2.0 | |
740 | */ | |
741 | @TmfSignalHandler | |
742 | public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) { | |
743 | if (signal.getTrace() == this) { | |
744 | getIndexer().buildIndex(getNbEvents(), signal.getRange(), false); | |
745 | } | |
746 | } | |
747 | ||
032ecd45 MAL |
748 | /** |
749 | * Signal handler for the TmfTraceUpdatedSignal signal | |
750 | * | |
751 | * @param signal The incoming signal | |
c4767854 | 752 | * @since 3.0 |
032ecd45 MAL |
753 | */ |
754 | @TmfSignalHandler | |
755 | public void traceUpdated(final TmfTraceUpdatedSignal signal) { | |
756 | if (signal.getSource() == getIndexer()) { | |
757 | fNbEvents = signal.getNbEvents(); | |
758 | fStartTime = signal.getRange().getStartTime(); | |
759 | fEndTime = signal.getRange().getEndTime(); | |
760 | } | |
761 | } | |
762 | ||
e73a4ba5 GB |
763 | /** |
764 | * Returns the file resource used to store synchronization formula. The file | |
765 | * may not exist. | |
766 | * | |
767 | * @return the synchronization file | |
768 | */ | |
769 | private File getSyncFormulaFile() { | |
770 | File file = null; | |
771 | if (fResource instanceof IFolder) { | |
772 | try { | |
773 | String supplDirectory; | |
774 | ||
775 | supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER); | |
776 | ||
777 | file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE); | |
778 | ||
779 | } catch (CoreException e) { | |
780 | ||
781 | } | |
782 | } | |
783 | return file; | |
784 | } | |
785 | ||
786 | // ------------------------------------------------------------------------ | |
787 | // Timestamp transformation functions | |
788 | // ------------------------------------------------------------------------ | |
789 | ||
790 | /** | |
791 | * @since 3.0 | |
792 | */ | |
793 | @Override | |
794 | public ITmfTimestampTransform getTimestampTransform() { | |
795 | if (fTsTransform == null) { | |
796 | /* Check if a formula is stored somewhere in the resources */ | |
797 | File sync_file = getSyncFormulaFile(); | |
798 | if (sync_file != null && sync_file.exists()) { | |
799 | ||
a4524c1b AM |
800 | try (FileInputStream fis = new FileInputStream(sync_file); |
801 | ObjectInputStream ois = new ObjectInputStream(fis);) { | |
802 | ||
e73a4ba5 GB |
803 | fTsTransform = (ITmfTimestampTransform) ois.readObject(); |
804 | ||
a4524c1b | 805 | } catch (ClassNotFoundException | IOException e) { |
e73a4ba5 GB |
806 | fTsTransform = TmfTimestampTransform.IDENTITY; |
807 | } | |
808 | } else { | |
809 | fTsTransform = TmfTimestampTransform.IDENTITY; | |
810 | } | |
811 | } | |
812 | return fTsTransform; | |
813 | } | |
814 | ||
815 | /** | |
816 | * @since 3.0 | |
817 | */ | |
818 | @Override | |
819 | public void setTimestampTransform(final ITmfTimestampTransform tt) { | |
820 | fTsTransform = tt; | |
821 | ||
822 | /* Save the timestamp transform to a file */ | |
823 | File sync_file = getSyncFormulaFile(); | |
824 | if (sync_file != null) { | |
825 | if (sync_file.exists()) { | |
826 | sync_file.delete(); | |
827 | } | |
e73a4ba5 GB |
828 | |
829 | /* Save the header of the file */ | |
7b3eb8c0 AM |
830 | try (FileOutputStream fos = new FileOutputStream(sync_file, false); |
831 | ObjectOutputStream oos = new ObjectOutputStream(fos);) { | |
e73a4ba5 GB |
832 | |
833 | oos.writeObject(fTsTransform); | |
e73a4ba5 GB |
834 | } catch (IOException e1) { |
835 | Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$ | |
836 | } | |
837 | } | |
838 | } | |
839 | ||
840 | /** | |
841 | * @since 3.0 | |
842 | */ | |
843 | @Override | |
844 | public ITmfTimestamp createTimestamp(long ts) { | |
845 | return new TmfTimestamp(getTimestampTransform().transform(ts)); | |
846 | } | |
847 | ||
3791b5df | 848 | // ------------------------------------------------------------------------ |
09e86496 | 849 | // toString |
3791b5df FC |
850 | // ------------------------------------------------------------------------ |
851 | ||
12c155f5 | 852 | @Override |
09e86496 | 853 | @SuppressWarnings("nls") |
5419a136 | 854 | public synchronized String toString() { |
20658947 FC |
855 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
856 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
857 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
858 | } |
859 | ||
8c8bf09f | 860 | } |