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