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