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