Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 Ericsson |
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 |
8c8bf09f ASL |
13 | *******************************************************************************/ |
14 | ||
6c13869b | 15 | package org.eclipse.linuxtools.tmf.core.trace; |
8c8bf09f | 16 | |
6f4a1d2b | 17 | import java.io.File; |
35c160d9 AM |
18 | import java.util.Collections; |
19 | import java.util.LinkedHashMap; | |
a51b2b9f | 20 | import java.util.Map; |
8c8bf09f | 21 | |
828e5592 | 22 | import org.eclipse.core.resources.IResource; |
faa38350 | 23 | import org.eclipse.core.runtime.CoreException; |
9b749023 | 24 | import org.eclipse.core.runtime.IPath; |
42459d24 | 25 | import org.eclipse.core.runtime.IStatus; |
b22a582a | 26 | import org.eclipse.core.runtime.MultiStatus; |
42459d24 | 27 | import org.eclipse.core.runtime.Status; |
b22a582a | 28 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
6c13869b | 29 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
72f1e62a | 30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
b4f71e4a | 31 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
5419a136 AM |
32 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
33 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
faa38350 | 34 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
fec1ac0b | 35 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
faa38350 PT |
36 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal; |
37 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal; | |
7898bb21 | 38 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; |
200789b3 | 39 | import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics; |
1c0de632 | 40 | import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics; |
3bd46eef AM |
41 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
42 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
43 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
8c8bf09f ASL |
44 | |
45 | /** | |
09e86496 FC |
46 | * Abstract implementation of ITmfTrace. |
47 | * <p> | |
13cb5f43 FC |
48 | * Since the concept of 'location' is trace specific, the concrete classes have |
49 | * to provide the related methods, namely: | |
50 | * <ul> | |
51 | * <li> public ITmfLocation<?> getCurrentLocation() | |
52 | * <li> public double getLocationRatio(ITmfLocation<?> location) | |
53 | * <li> public ITmfContext seekEvent(ITmfLocation<?> location) | |
54 | * <li> public ITmfContext seekEvent(double ratio) | |
da1a4b39 | 55 | * <li> public IStatus validate(IProject project, String path) |
13cb5f43 FC |
56 | * </ul> |
57 | * A concrete trace must provide its corresponding parser. A common way to | |
58 | * accomplish this is by making the concrete class extend TmfTrace and | |
59 | * implement ITmfEventParser. | |
60 | * <p> | |
61 | * The concrete class can either specify its own indexer or use the provided | |
62 | * TmfCheckpointIndexer (default). In this case, the trace cache size will be | |
63 | * used as checkpoint interval. | |
0bfb7d06 | 64 | * |
f7703ed6 FC |
65 | * @version 1.0 |
66 | * @author Francois Chouinard | |
67 | * | |
f7703ed6 FC |
68 | * @see ITmfEvent |
69 | * @see ITmfTraceIndexer | |
70 | * @see ITmfEventParser | |
8c8bf09f | 71 | */ |
6256d8ad | 72 | public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace { |
62d1696a | 73 | |
e31e01e8 | 74 | // ------------------------------------------------------------------------ |
8c8bf09f | 75 | // Attributes |
e31e01e8 | 76 | // ------------------------------------------------------------------------ |
8c8bf09f | 77 | |
09e86496 FC |
78 | // The resource used for persistent properties for this trace |
79 | private IResource fResource; | |
80 | ||
b0a282fb | 81 | // The trace path |
12c155f5 | 82 | private String fPath; |
b0a282fb | 83 | |
0316808c FC |
84 | // The trace cache page size |
85 | private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; | |
62d1696a | 86 | |
0316808c FC |
87 | // The number of events collected (so far) |
88 | private long fNbEvents = 0; | |
62d1696a FC |
89 | |
90 | // The time span of the event stream | |
9cbe7899 | 91 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG; |
a4115405 | 92 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; |
62d1696a | 93 | |
0316808c FC |
94 | // The trace streaming interval (0 = no streaming) |
95 | private long fStreamingInterval = 0; | |
085d898f | 96 | |
0316808c | 97 | // The trace indexer |
6256d8ad | 98 | private ITmfTraceIndexer fIndexer; |
20658947 | 99 | |
0316808c | 100 | // The trace parser |
6256d8ad | 101 | private ITmfEventParser fParser; |
7e6347b0 | 102 | |
200789b3 AM |
103 | // The trace's statistics |
104 | private ITmfStatistics fStatistics; | |
105 | ||
a51b2b9f AM |
106 | /** |
107 | * The collection of state systems that are registered with this trace. Each | |
108 | * sub-class can decide to add its (one or many) state system to this map | |
109 | * during their {@link #buildStateSystem()}. | |
110 | * | |
111 | * @since 2.0 | |
112 | */ | |
113 | protected final Map<String, ITmfStateSystem> fStateSystems = | |
35c160d9 | 114 | new LinkedHashMap<String, ITmfStateSystem>(); |
a51b2b9f | 115 | |
e31e01e8 | 116 | // ------------------------------------------------------------------------ |
3791b5df | 117 | // Construction |
e31e01e8 | 118 | // ------------------------------------------------------------------------ |
8c8bf09f | 119 | |
62d1696a | 120 | /** |
3791b5df | 121 | * The default, parameterless, constructor |
62d1696a | 122 | */ |
3791b5df FC |
123 | public TmfTrace() { |
124 | super(); | |
05bd3318 FC |
125 | } |
126 | ||
127 | /** | |
8cf330ae | 128 | * Full constructor. |
0bfb7d06 | 129 | * |
8cf330ae AM |
130 | * @param resource |
131 | * The resource associated to the trace | |
132 | * @param type | |
133 | * The type of events that will be read from this trace | |
134 | * @param path | |
135 | * The path to the trace on the filesystem | |
136 | * @param cacheSize | |
137 | * The trace cache size. Pass '-1' to use the default specified | |
138 | * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE} | |
139 | * @param interval | |
140 | * The trace streaming interval. You can use '0' for post-mortem | |
141 | * traces. | |
142 | * @param indexer | |
143 | * The trace indexer. You can pass 'null' to use a default | |
144 | * checkpoint indexer. | |
145 | * @param parser | |
146 | * The trace event parser. Use 'null' if (and only if) the trace | |
147 | * object itself is also the ITmfEventParser to be used. | |
148 | * @throws TmfTraceException | |
149 | * If something failed during the opening | |
20658947 | 150 | */ |
8cf330ae AM |
151 | protected TmfTrace(final IResource resource, |
152 | final Class<? extends ITmfEvent> type, | |
153 | final String path, | |
154 | final int cacheSize, | |
155 | final long interval, | |
156 | final ITmfTraceIndexer indexer, | |
157 | final ITmfEventParser parser) | |
158 | throws TmfTraceException { | |
00641a97 | 159 | super(); |
0316808c | 160 | fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; |
3791b5df | 161 | fStreamingInterval = interval; |
7e6347b0 | 162 | fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize); |
13cb5f43 | 163 | fParser = parser; |
09e86496 | 164 | initialize(resource, path, type); |
8c8bf09f ASL |
165 | } |
166 | ||
3791b5df FC |
167 | /** |
168 | * Copy constructor | |
0bfb7d06 | 169 | * |
3791b5df | 170 | * @param trace the original trace |
063f0d27 | 171 | * @throws TmfTraceException Should not happen usually |
3791b5df | 172 | */ |
6256d8ad | 173 | public TmfTrace(final TmfTrace trace) throws TmfTraceException { |
3791b5df | 174 | super(); |
0316808c | 175 | if (trace == null) { |
3791b5df | 176 | throw new IllegalArgumentException(); |
0316808c | 177 | } |
20658947 FC |
178 | fCacheSize = trace.getCacheSize(); |
179 | fStreamingInterval = trace.getStreamingInterval(); | |
7e6347b0 | 180 | fIndexer = new TmfCheckpointIndexer(this); |
13cb5f43 FC |
181 | fParser = trace.fParser; |
182 | initialize(trace.getResource(), trace.getPath(), trace.getEventType()); | |
3791b5df FC |
183 | } |
184 | ||
7e6347b0 FC |
185 | // ------------------------------------------------------------------------ |
186 | // ITmfTrace - Initializers | |
187 | // ------------------------------------------------------------------------ | |
188 | ||
7e6347b0 | 189 | @Override |
6256d8ad | 190 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
9dc3dee2 | 191 | fIndexer = new TmfCheckpointIndexer(this, fCacheSize); |
7e6347b0 | 192 | initialize(resource, path, type); |
7e6347b0 FC |
193 | } |
194 | ||
09e86496 | 195 | /** |
1703b536 | 196 | * Initialize the trace common attributes and the base component. |
0bfb7d06 MK |
197 | * |
198 | * @param resource the Eclipse resource (trace) | |
1703b536 FC |
199 | * @param path the trace path |
200 | * @param type the trace event type | |
0bfb7d06 | 201 | * |
6f4e8ec0 | 202 | * @throws TmfTraceException If something failed during the initialization |
3791b5df | 203 | */ |
248af329 AM |
204 | protected void initialize(final IResource resource, |
205 | final String path, | |
206 | final Class<? extends ITmfEvent> type) | |
207 | throws TmfTraceException { | |
0316808c | 208 | if (path == null) { |
b4f71e4a | 209 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
0316808c | 210 | } |
3791b5df | 211 | fPath = path; |
1703b536 | 212 | fResource = resource; |
25e48683 | 213 | String traceName = (resource != null) ? resource.getName() : null; |
1703b536 FC |
214 | // If no resource was provided, extract the display name the trace path |
215 | if (traceName == null) { | |
9b749023 | 216 | final int sep = path.lastIndexOf(IPath.SEPARATOR); |
1703b536 FC |
217 | traceName = (sep >= 0) ? path.substring(sep + 1) : path; |
218 | } | |
2352aed9 FC |
219 | if (fParser == null) { |
220 | if (this instanceof ITmfEventParser) { | |
6256d8ad | 221 | fParser = (ITmfEventParser) this; |
2352aed9 FC |
222 | } else { |
223 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ | |
224 | } | |
225 | } | |
3791b5df | 226 | super.init(traceName, type); |
fec1ac0b BH |
227 | // register as VIP after super.init() because TmfComponent registers to signal manager there |
228 | TmfSignalManager.registerVIP(this); | |
3791b5df FC |
229 | } |
230 | ||
2352aed9 FC |
231 | /** |
232 | * Indicates if the path points to an existing file/directory | |
0bfb7d06 | 233 | * |
2352aed9 FC |
234 | * @param path the path to test |
235 | * @return true if the file/directory exists | |
3791b5df | 236 | */ |
2352aed9 | 237 | protected boolean fileExists(final String path) { |
085d898f | 238 | final File file = new File(path); |
3791b5df FC |
239 | return file.exists(); |
240 | } | |
241 | ||
c7e1020d | 242 | /** |
51e75066 | 243 | * @since 2.0 |
c7e1020d | 244 | */ |
51e75066 AM |
245 | @Override |
246 | public void indexTrace(boolean waitForCompletion) { | |
9e0640dc | 247 | getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion); |
c7e1020d FC |
248 | } |
249 | ||
200789b3 | 250 | /** |
6f4e8ec0 | 251 | * The default implementation of TmfTrace uses a TmfStatistics back-end. |
200789b3 AM |
252 | * Override this if you want to specify another type (or none at all). |
253 | * | |
b22a582a AM |
254 | * @return An IStatus indicating if the statistics could be built |
255 | * successfully or not. | |
256 | * @since 3.0 | |
200789b3 | 257 | */ |
b22a582a | 258 | protected IStatus buildStatistics() { |
200789b3 AM |
259 | /* |
260 | * Initialize the statistics provider, but only if a Resource has been | |
261 | * set (so we don't build it for experiments, for unit tests, etc.) | |
262 | */ | |
b22a582a AM |
263 | try { |
264 | fStatistics = (fResource == null ? null : new TmfStateStatistics(this) ); | |
265 | } catch (TmfTraceException e) { | |
266 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); | |
267 | } | |
268 | return Status.OK_STATUS; | |
200789b3 AM |
269 | } |
270 | ||
faa38350 PT |
271 | /** |
272 | * Build the state system(s) associated with this trace type. | |
273 | * | |
274 | * Suppressing the warning, because the 'throws' will usually happen in | |
275 | * sub-classes. | |
276 | * | |
42459d24 AM |
277 | * @return An IStatus indicating if the state system could be build |
278 | * successfully or not. | |
279 | * @since 3.0 | |
faa38350 | 280 | */ |
42459d24 | 281 | protected IStatus buildStateSystem() { |
faa38350 PT |
282 | /* |
283 | * Nothing is done in the base implementation, please specify | |
a51b2b9f | 284 | * how/if to register a new state system in derived classes. |
faa38350 | 285 | */ |
42459d24 | 286 | return Status.OK_STATUS; |
faa38350 PT |
287 | } |
288 | ||
b5ee6881 FC |
289 | /** |
290 | * Clears the trace | |
291 | */ | |
292 | @Override | |
293 | public synchronized void dispose() { | |
1a4205d9 | 294 | /* Clean up the index if applicable */ |
77551cc2 FC |
295 | if (getIndexer() != null) { |
296 | getIndexer().dispose(); | |
297 | } | |
1a4205d9 AM |
298 | |
299 | /* Clean up the statistics */ | |
300 | if (fStatistics != null) { | |
301 | fStatistics.dispose(); | |
302 | } | |
a51b2b9f AM |
303 | |
304 | /* Clean up the state systems */ | |
305 | for (ITmfStateSystem ss : fStateSystems.values()) { | |
306 | ss.dispose(); | |
307 | } | |
308 | ||
b5ee6881 FC |
309 | super.dispose(); |
310 | } | |
311 | ||
3791b5df | 312 | // ------------------------------------------------------------------------ |
09e86496 | 313 | // ITmfTrace - Basic getters |
e31e01e8 | 314 | // ------------------------------------------------------------------------ |
8c8bf09f | 315 | |
25e48683 | 316 | @Override |
6256d8ad AM |
317 | public Class<ITmfEvent> getEventType() { |
318 | return (Class<ITmfEvent>) super.getType(); | |
25e48683 FC |
319 | } |
320 | ||
d4011df2 | 321 | @Override |
09e86496 FC |
322 | public IResource getResource() { |
323 | return fResource; | |
8c8bf09f ASL |
324 | } |
325 | ||
d4011df2 | 326 | @Override |
09e86496 FC |
327 | public String getPath() { |
328 | return fPath; | |
8c8bf09f ASL |
329 | } |
330 | ||
20658947 FC |
331 | @Override |
332 | public int getCacheSize() { | |
333 | return fCacheSize; | |
334 | } | |
335 | ||
20658947 FC |
336 | @Override |
337 | public long getStreamingInterval() { | |
338 | return fStreamingInterval; | |
339 | } | |
340 | ||
0316808c FC |
341 | /** |
342 | * @return the trace indexer | |
343 | */ | |
6256d8ad | 344 | protected ITmfTraceIndexer getIndexer() { |
0316808c FC |
345 | return fIndexer; |
346 | } | |
347 | ||
348 | /** | |
349 | * @return the trace parser | |
350 | */ | |
6256d8ad | 351 | protected ITmfEventParser getParser() { |
0316808c FC |
352 | return fParser; |
353 | } | |
354 | ||
200789b3 AM |
355 | /** |
356 | * @since 2.0 | |
357 | */ | |
358 | @Override | |
359 | public ITmfStatistics getStatistics() { | |
360 | return fStatistics; | |
361 | } | |
362 | ||
7898bb21 AM |
363 | /** |
364 | * @since 2.0 | |
365 | */ | |
366 | @Override | |
35c160d9 AM |
367 | public final Map<String, ITmfStateSystem> getStateSystems() { |
368 | return Collections.unmodifiableMap(fStateSystems); | |
7898bb21 AM |
369 | } |
370 | ||
6c5e0863 AM |
371 | /** |
372 | * @since 2.0 | |
373 | */ | |
374 | @Override | |
375 | public final void registerStateSystem(String id, ITmfStateSystem ss) { | |
376 | fStateSystems.put(id, ss); | |
377 | } | |
378 | ||
09e86496 FC |
379 | // ------------------------------------------------------------------------ |
380 | // ITmfTrace - Trace characteristics getters | |
381 | // ------------------------------------------------------------------------ | |
382 | ||
d4011df2 | 383 | @Override |
5419a136 | 384 | public synchronized long getNbEvents() { |
3791b5df | 385 | return fNbEvents; |
b0a282fb FC |
386 | } |
387 | ||
3bd46eef AM |
388 | /** |
389 | * @since 2.0 | |
62d1696a | 390 | */ |
d4011df2 | 391 | @Override |
12c155f5 | 392 | public TmfTimeRange getTimeRange() { |
cb866e08 | 393 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
394 | } |
395 | ||
3bd46eef AM |
396 | /** |
397 | * @since 2.0 | |
e31e01e8 | 398 | */ |
d4011df2 | 399 | @Override |
4df4581d | 400 | public ITmfTimestamp getStartTime() { |
4593bd5b | 401 | return fStartTime; |
146a887c FC |
402 | } |
403 | ||
3bd46eef AM |
404 | /** |
405 | * @since 2.0 | |
e31e01e8 | 406 | */ |
d4011df2 | 407 | @Override |
4df4581d | 408 | public ITmfTimestamp getEndTime() { |
4593bd5b | 409 | return fEndTime; |
20658947 FC |
410 | } |
411 | ||
d7ee91bb | 412 | /** |
d7ee91bb PT |
413 | * @since 2.0 |
414 | */ | |
66262ad8 BH |
415 | @Override |
416 | public ITmfTimestamp getInitialRangeOffset() { | |
d7ee91bb PT |
417 | final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
418 | return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); | |
419 | } | |
420 | ||
bb52f9bc GB |
421 | /** |
422 | * @since 3.0 | |
423 | */ | |
424 | @Override | |
425 | public String getHostId() { | |
426 | return this.getName(); | |
427 | } | |
428 | ||
20658947 | 429 | // ------------------------------------------------------------------------ |
d7ee91bb | 430 | // Convenience setters |
20658947 FC |
431 | // ------------------------------------------------------------------------ |
432 | ||
0316808c FC |
433 | /** |
434 | * Set the trace cache size. Must be done at initialization time. | |
0bfb7d06 | 435 | * |
0316808c FC |
436 | * @param cacheSize The trace cache size |
437 | */ | |
438 | protected void setCacheSize(final int cacheSize) { | |
439 | fCacheSize = cacheSize; | |
440 | } | |
441 | ||
442 | /** | |
443 | * Set the trace known number of events. This can be quite dynamic | |
444 | * during indexing or for live traces. | |
0bfb7d06 | 445 | * |
0316808c FC |
446 | * @param nbEvents The number of events |
447 | */ | |
448 | protected synchronized void setNbEvents(final long nbEvents) { | |
449 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; | |
450 | } | |
451 | ||
20658947 FC |
452 | /** |
453 | * Update the trace events time range | |
0bfb7d06 | 454 | * |
20658947 | 455 | * @param range the new time range |
3bd46eef | 456 | * @since 2.0 |
20658947 FC |
457 | */ |
458 | protected void setTimeRange(final TmfTimeRange range) { | |
4593bd5b AM |
459 | fStartTime = range.getStartTime(); |
460 | fEndTime = range.getEndTime(); | |
20658947 FC |
461 | } |
462 | ||
463 | /** | |
464 | * Update the trace chronologically first event timestamp | |
0bfb7d06 | 465 | * |
20658947 | 466 | * @param startTime the new first event timestamp |
3bd46eef | 467 | * @since 2.0 |
20658947 FC |
468 | */ |
469 | protected void setStartTime(final ITmfTimestamp startTime) { | |
4593bd5b | 470 | fStartTime = startTime; |
20658947 FC |
471 | } |
472 | ||
473 | /** | |
474 | * Update the trace chronologically last event timestamp | |
0bfb7d06 | 475 | * |
20658947 | 476 | * @param endTime the new last event timestamp |
3bd46eef | 477 | * @since 2.0 |
20658947 FC |
478 | */ |
479 | protected void setEndTime(final ITmfTimestamp endTime) { | |
4593bd5b | 480 | fEndTime = endTime; |
20658947 FC |
481 | } |
482 | ||
483 | /** | |
0316808c | 484 | * Set the polling interval for live traces (default = 0 = no streaming). |
0bfb7d06 | 485 | * |
20658947 FC |
486 | * @param interval the new trace streaming interval |
487 | */ | |
488 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 489 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
490 | } |
491 | ||
0316808c FC |
492 | /** |
493 | * Set the trace indexer. Must be done at initialization time. | |
0bfb7d06 | 494 | * |
0316808c FC |
495 | * @param indexer the trace indexer |
496 | */ | |
6256d8ad | 497 | protected void setIndexer(final ITmfTraceIndexer indexer) { |
0316808c FC |
498 | fIndexer = indexer; |
499 | } | |
500 | ||
501 | /** | |
502 | * Set the trace parser. Must be done at initialization time. | |
0bfb7d06 | 503 | * |
0316808c FC |
504 | * @param parser the new trace parser |
505 | */ | |
6256d8ad | 506 | protected void setParser(final ITmfEventParser parser) { |
0316808c FC |
507 | fParser = parser; |
508 | } | |
509 | ||
09e86496 | 510 | // ------------------------------------------------------------------------ |
7e6347b0 | 511 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
512 | // ------------------------------------------------------------------------ |
513 | ||
1b70b6dc | 514 | @Override |
7e6347b0 | 515 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 516 | |
7e6347b0 | 517 | // A rank <= 0 indicates to seek the first event |
2352aed9 | 518 | if (rank <= 0) { |
1e1bef82 | 519 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
520 | context.setRank(0); |
521 | return context; | |
522 | } | |
09e86496 | 523 | |
09e86496 | 524 | // Position the trace at the checkpoint |
7e6347b0 | 525 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
526 | |
527 | // And locate the requested event context | |
7e6347b0 FC |
528 | long pos = context.getRank(); |
529 | if (pos < rank) { | |
c32744d6 | 530 | ITmfEvent event = getNext(context); |
0bfb7d06 | 531 | while ((event != null) && (++pos < rank)) { |
c32744d6 | 532 | event = getNext(context); |
7e6347b0 | 533 | } |
09e86496 FC |
534 | } |
535 | return context; | |
1b70b6dc PT |
536 | } |
537 | ||
3bd46eef AM |
538 | /** |
539 | * @since 2.0 | |
09e86496 FC |
540 | */ |
541 | @Override | |
7e6347b0 | 542 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 543 | |
7e6347b0 | 544 | // A null timestamp indicates to seek the first event |
2352aed9 | 545 | if (timestamp == null) { |
1e1bef82 | 546 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
547 | context.setRank(0); |
548 | return context; | |
549 | } | |
09e86496 | 550 | |
1703b536 | 551 | // Position the trace at the checkpoint |
408e65d2 | 552 | ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
553 | |
554 | // And locate the requested event context | |
ea271da6 PT |
555 | ITmfLocation previousLocation = context.getLocation(); |
556 | long previousRank = context.getRank(); | |
557 | ITmfEvent event = getNext(context); | |
7e6347b0 | 558 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
ea271da6 PT |
559 | previousLocation = context.getLocation(); |
560 | previousRank = context.getRank(); | |
561 | event = getNext(context); | |
09e86496 | 562 | } |
0316808c FC |
563 | if (event == null) { |
564 | context.setLocation(null); | |
565 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
ea271da6 PT |
566 | } else { |
567 | context.dispose(); | |
568 | context = seekEvent(previousLocation); | |
569 | context.setRank(previousRank); | |
0316808c | 570 | } |
09e86496 FC |
571 | return context; |
572 | } | |
0283f7ff | 573 | |
09e86496 FC |
574 | // ------------------------------------------------------------------------ |
575 | // ITmfTrace - Read operations (returning an actual event) | |
576 | // ------------------------------------------------------------------------ | |
577 | ||
d4011df2 | 578 | @Override |
6256d8ad | 579 | public synchronized ITmfEvent getNext(final ITmfContext context) { |
09e86496 | 580 | // parseEvent() does not update the context |
6256d8ad | 581 | final ITmfEvent event = fParser.parseEvent(context); |
09e86496 | 582 | if (event != null) { |
d337369a | 583 | updateAttributes(context, event.getTimestamp()); |
09e86496 FC |
584 | context.setLocation(getCurrentLocation()); |
585 | context.increaseRank(); | |
586 | processEvent(event); | |
587 | } | |
588 | return event; | |
589 | } | |
590 | ||
591 | /** | |
d337369a | 592 | * Hook for special event processing by the concrete class |
7e6347b0 | 593 | * (called by TmfTrace.getEvent()) |
0bfb7d06 | 594 | * |
d337369a | 595 | * @param event the event |
09e86496 FC |
596 | */ |
597 | protected void processEvent(final ITmfEvent event) { | |
d337369a | 598 | // Do nothing |
09e86496 FC |
599 | } |
600 | ||
d337369a FC |
601 | /** |
602 | * Update the trace attributes | |
0bfb7d06 | 603 | * |
d337369a | 604 | * @param context the current trace context |
2848c377 | 605 | * @param timestamp the corresponding timestamp |
3bd46eef | 606 | * @since 2.0 |
d337369a FC |
607 | */ |
608 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { | |
0bfb7d06 | 609 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) { |
4593bd5b | 610 | fStartTime = timestamp; |
09e86496 | 611 | } |
0bfb7d06 | 612 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) { |
4593bd5b | 613 | fEndTime = timestamp; |
09e86496 FC |
614 | } |
615 | if (context.hasValidRank()) { | |
d337369a | 616 | long rank = context.getRank(); |
09e86496 FC |
617 | if (fNbEvents <= rank) { |
618 | fNbEvents = rank + 1; | |
619 | } | |
200789b3 AM |
620 | if (fIndexer != null) { |
621 | fIndexer.updateIndex(context, timestamp); | |
622 | } | |
09e86496 | 623 | } |
abfad0aa FC |
624 | } |
625 | ||
3791b5df | 626 | // ------------------------------------------------------------------------ |
d337369a | 627 | // TmfDataProvider |
3791b5df FC |
628 | // ------------------------------------------------------------------------ |
629 | ||
77c4a6df AM |
630 | /** |
631 | * @since 2.0 | |
d337369a | 632 | */ |
3791b5df | 633 | @Override |
5419a136 | 634 | public synchronized ITmfContext armRequest(final ITmfDataRequest request) { |
faa38350 PT |
635 | if (executorIsShutdown()) { |
636 | return null; | |
637 | } | |
5419a136 AM |
638 | if ((request instanceof ITmfEventRequest) |
639 | && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime()) | |
640 | && (request.getIndex() == 0)) | |
641 | { | |
642 | final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime()); | |
643 | ((ITmfEventRequest) request).setStartIndex((int) context.getRank()); | |
8584dc20 | 644 | return context; |
8584dc20 | 645 | |
5419a136 AM |
646 | } |
647 | return seekEvent(request.getIndex()); | |
3791b5df FC |
648 | } |
649 | ||
faa38350 PT |
650 | // ------------------------------------------------------------------------ |
651 | // Signal handlers | |
652 | // ------------------------------------------------------------------------ | |
653 | ||
654 | /** | |
655 | * Handler for the Trace Opened signal | |
656 | * | |
657 | * @param signal | |
658 | * The incoming signal | |
659 | * @since 2.0 | |
660 | */ | |
661 | @TmfSignalHandler | |
662 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
b9a5bf8f AM |
663 | boolean signalIsForUs = false; |
664 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) { | |
665 | if (trace == this) { | |
666 | signalIsForUs = true; | |
fe0c44c4 | 667 | break; |
faa38350 PT |
668 | } |
669 | } | |
faa38350 | 670 | |
b9a5bf8f | 671 | if (!signalIsForUs) { |
fe0c44c4 AM |
672 | return; |
673 | } | |
674 | ||
675 | /* | |
b9a5bf8f | 676 | * The signal is either for this trace, or for an experiment containing |
fe0c44c4 AM |
677 | * this trace. |
678 | */ | |
b22a582a AM |
679 | MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null); |
680 | status.add(buildStatistics()); | |
681 | status.add(buildStateSystem()); | |
682 | if (!status.isOK()) { | |
683 | Activator.log(status); | |
fe0c44c4 AM |
684 | } |
685 | ||
686 | /* Refresh the project, so it can pick up new files that got created. */ | |
687 | try { | |
688 | if (fResource != null) { | |
689 | fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); | |
faa38350 | 690 | } |
fe0c44c4 AM |
691 | } catch (CoreException e) { |
692 | e.printStackTrace(); | |
faa38350 | 693 | } |
fe0c44c4 | 694 | |
faa38350 | 695 | if (signal.getTrace() == this) { |
f8fc4a3a | 696 | /* Additionally, the signal is directly for this trace. */ |
faa38350 PT |
697 | if (getNbEvents() == 0) { |
698 | return; | |
699 | } | |
700 | ||
f8fc4a3a PT |
701 | /* For a streaming trace, the range updated signal should be sent |
702 | * by the subclass when a new safe time is determined. | |
703 | */ | |
704 | if (getStreamingInterval() > 0) { | |
705 | return; | |
706 | } | |
707 | ||
faa38350 PT |
708 | final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH); |
709 | final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange); | |
710 | ||
711 | // Broadcast in separate thread to prevent deadlock | |
712 | new Thread() { | |
713 | @Override | |
714 | public void run() { | |
715 | broadcast(rangeUpdatedsignal); | |
716 | } | |
717 | }.start(); | |
718 | return; | |
719 | } | |
720 | } | |
721 | ||
722 | /** | |
723 | * Signal handler for the TmfTraceRangeUpdatedSignal signal | |
724 | * | |
725 | * @param signal The incoming signal | |
726 | * @since 2.0 | |
727 | */ | |
728 | @TmfSignalHandler | |
729 | public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) { | |
730 | if (signal.getTrace() == this) { | |
731 | getIndexer().buildIndex(getNbEvents(), signal.getRange(), false); | |
732 | } | |
733 | } | |
734 | ||
3791b5df | 735 | // ------------------------------------------------------------------------ |
09e86496 | 736 | // toString |
3791b5df FC |
737 | // ------------------------------------------------------------------------ |
738 | ||
12c155f5 | 739 | @Override |
09e86496 | 740 | @SuppressWarnings("nls") |
5419a136 | 741 | public synchronized String toString() { |
20658947 FC |
742 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
743 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
744 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
745 | } |
746 | ||
8c8bf09f | 747 | } |