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 | ||
192 | /* (non-Javadoc) | |
193 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class) | |
194 | */ | |
195 | @Override | |
6256d8ad | 196 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
9dc3dee2 | 197 | fIndexer = new TmfCheckpointIndexer(this, fCacheSize); |
7e6347b0 | 198 | initialize(resource, path, type); |
7e6347b0 FC |
199 | } |
200 | ||
09e86496 | 201 | /** |
1703b536 | 202 | * Initialize the trace common attributes and the base component. |
0bfb7d06 MK |
203 | * |
204 | * @param resource the Eclipse resource (trace) | |
1703b536 FC |
205 | * @param path the trace path |
206 | * @param type the trace event type | |
0bfb7d06 | 207 | * |
6f4e8ec0 | 208 | * @throws TmfTraceException If something failed during the initialization |
3791b5df | 209 | */ |
6256d8ad | 210 | protected void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
0316808c | 211 | if (path == null) { |
b4f71e4a | 212 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
0316808c | 213 | } |
3791b5df | 214 | fPath = path; |
1703b536 | 215 | fResource = resource; |
25e48683 | 216 | String traceName = (resource != null) ? resource.getName() : null; |
1703b536 FC |
217 | // If no resource was provided, extract the display name the trace path |
218 | if (traceName == null) { | |
9b749023 | 219 | final int sep = path.lastIndexOf(IPath.SEPARATOR); |
1703b536 FC |
220 | traceName = (sep >= 0) ? path.substring(sep + 1) : path; |
221 | } | |
2352aed9 FC |
222 | if (fParser == null) { |
223 | if (this instanceof ITmfEventParser) { | |
6256d8ad | 224 | fParser = (ITmfEventParser) this; |
2352aed9 FC |
225 | } else { |
226 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ | |
227 | } | |
228 | } | |
3791b5df FC |
229 | super.init(traceName, type); |
230 | } | |
231 | ||
2352aed9 FC |
232 | /** |
233 | * Indicates if the path points to an existing file/directory | |
0bfb7d06 | 234 | * |
2352aed9 FC |
235 | * @param path the path to test |
236 | * @return true if the file/directory exists | |
3791b5df | 237 | */ |
2352aed9 | 238 | protected boolean fileExists(final String path) { |
085d898f | 239 | final File file = new File(path); |
3791b5df FC |
240 | return file.exists(); |
241 | } | |
242 | ||
c7e1020d FC |
243 | /** |
244 | * Index the trace | |
0bfb7d06 | 245 | * |
c7e1020d FC |
246 | * @param waitForCompletion index synchronously (true) or not (false) |
247 | */ | |
248 | protected void indexTrace(boolean waitForCompletion) { | |
9e0640dc | 249 | getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion); |
c7e1020d FC |
250 | } |
251 | ||
200789b3 | 252 | /** |
6f4e8ec0 | 253 | * The default implementation of TmfTrace uses a TmfStatistics back-end. |
200789b3 AM |
254 | * Override this if you want to specify another type (or none at all). |
255 | * | |
6f4e8ec0 AM |
256 | * @throws TmfTraceException |
257 | * If there was a problem setting up the statistics | |
200789b3 AM |
258 | * @since 2.0 |
259 | */ | |
260 | protected void buildStatistics() throws TmfTraceException { | |
261 | /* | |
262 | * Initialize the statistics provider, but only if a Resource has been | |
263 | * set (so we don't build it for experiments, for unit tests, etc.) | |
264 | */ | |
1c0de632 | 265 | fStatistics = (fResource == null ? null : new TmfStateStatistics(this) ); |
200789b3 AM |
266 | } |
267 | ||
faa38350 PT |
268 | /** |
269 | * Build the state system(s) associated with this trace type. | |
270 | * | |
271 | * Suppressing the warning, because the 'throws' will usually happen in | |
272 | * sub-classes. | |
273 | * | |
274 | * @throws TmfTraceException | |
275 | * If there is a problem during the build | |
276 | * @since 2.0 | |
277 | */ | |
278 | @SuppressWarnings("unused") | |
279 | protected void buildStateSystem() throws TmfTraceException { | |
280 | /* | |
281 | * Nothing is done in the base implementation, please specify | |
a51b2b9f | 282 | * how/if to register a new state system in derived classes. |
faa38350 PT |
283 | */ |
284 | return; | |
285 | } | |
286 | ||
b5ee6881 FC |
287 | /** |
288 | * Clears the trace | |
289 | */ | |
290 | @Override | |
291 | public synchronized void dispose() { | |
1a4205d9 | 292 | /* Clean up the index if applicable */ |
77551cc2 FC |
293 | if (getIndexer() != null) { |
294 | getIndexer().dispose(); | |
295 | } | |
1a4205d9 AM |
296 | |
297 | /* Clean up the statistics */ | |
298 | if (fStatistics != null) { | |
299 | fStatistics.dispose(); | |
300 | } | |
a51b2b9f AM |
301 | |
302 | /* Clean up the state systems */ | |
303 | for (ITmfStateSystem ss : fStateSystems.values()) { | |
304 | ss.dispose(); | |
305 | } | |
306 | ||
b5ee6881 FC |
307 | super.dispose(); |
308 | } | |
309 | ||
3791b5df | 310 | // ------------------------------------------------------------------------ |
09e86496 | 311 | // ITmfTrace - Basic getters |
e31e01e8 | 312 | // ------------------------------------------------------------------------ |
8c8bf09f | 313 | |
fe0c44c4 AM |
314 | /** |
315 | * @since 2.0 | |
316 | */ | |
317 | @Override | |
318 | public ITmfTrace[] getTraces() { | |
319 | return new ITmfTrace[] { this }; | |
320 | } | |
321 | ||
09e86496 | 322 | /* (non-Javadoc) |
13cb5f43 | 323 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType() |
25e48683 FC |
324 | */ |
325 | @Override | |
6256d8ad AM |
326 | public Class<ITmfEvent> getEventType() { |
327 | return (Class<ITmfEvent>) super.getType(); | |
25e48683 FC |
328 | } |
329 | ||
09e86496 FC |
330 | /* (non-Javadoc) |
331 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource() | |
62d1696a | 332 | */ |
d4011df2 | 333 | @Override |
09e86496 FC |
334 | public IResource getResource() { |
335 | return fResource; | |
8c8bf09f ASL |
336 | } |
337 | ||
09e86496 FC |
338 | /* (non-Javadoc) |
339 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath() | |
62d1696a | 340 | */ |
d4011df2 | 341 | @Override |
09e86496 FC |
342 | public String getPath() { |
343 | return fPath; | |
8c8bf09f ASL |
344 | } |
345 | ||
20658947 FC |
346 | /* (non-Javadoc) |
347 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize() | |
348 | */ | |
349 | @Override | |
350 | public int getCacheSize() { | |
351 | return fCacheSize; | |
352 | } | |
353 | ||
354 | /* (non-Javadoc) | |
355 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval() | |
356 | */ | |
357 | @Override | |
358 | public long getStreamingInterval() { | |
359 | return fStreamingInterval; | |
360 | } | |
361 | ||
0316808c FC |
362 | /** |
363 | * @return the trace indexer | |
364 | */ | |
6256d8ad | 365 | protected ITmfTraceIndexer getIndexer() { |
0316808c FC |
366 | return fIndexer; |
367 | } | |
368 | ||
369 | /** | |
370 | * @return the trace parser | |
371 | */ | |
6256d8ad | 372 | protected ITmfEventParser getParser() { |
0316808c FC |
373 | return fParser; |
374 | } | |
375 | ||
200789b3 AM |
376 | /** |
377 | * @since 2.0 | |
378 | */ | |
379 | @Override | |
380 | public ITmfStatistics getStatistics() { | |
381 | return fStatistics; | |
382 | } | |
383 | ||
7898bb21 AM |
384 | /** |
385 | * @since 2.0 | |
386 | */ | |
387 | @Override | |
35c160d9 AM |
388 | public final Map<String, ITmfStateSystem> getStateSystems() { |
389 | return Collections.unmodifiableMap(fStateSystems); | |
7898bb21 AM |
390 | } |
391 | ||
6c5e0863 AM |
392 | /** |
393 | * @since 2.0 | |
394 | */ | |
395 | @Override | |
396 | public final void registerStateSystem(String id, ITmfStateSystem ss) { | |
397 | fStateSystems.put(id, ss); | |
398 | } | |
399 | ||
09e86496 FC |
400 | // ------------------------------------------------------------------------ |
401 | // ITmfTrace - Trace characteristics getters | |
402 | // ------------------------------------------------------------------------ | |
403 | ||
d4011df2 | 404 | @Override |
5419a136 | 405 | public synchronized long getNbEvents() { |
3791b5df | 406 | return fNbEvents; |
b0a282fb FC |
407 | } |
408 | ||
3bd46eef AM |
409 | /** |
410 | * @since 2.0 | |
62d1696a | 411 | */ |
d4011df2 | 412 | @Override |
12c155f5 | 413 | public TmfTimeRange getTimeRange() { |
cb866e08 | 414 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
415 | } |
416 | ||
3bd46eef AM |
417 | /** |
418 | * @since 2.0 | |
e31e01e8 | 419 | */ |
d4011df2 | 420 | @Override |
4df4581d | 421 | public ITmfTimestamp getStartTime() { |
4593bd5b | 422 | return fStartTime; |
146a887c FC |
423 | } |
424 | ||
3bd46eef AM |
425 | /** |
426 | * @since 2.0 | |
e31e01e8 | 427 | */ |
d4011df2 | 428 | @Override |
4df4581d | 429 | public ITmfTimestamp getEndTime() { |
4593bd5b | 430 | return fEndTime; |
20658947 FC |
431 | } |
432 | ||
d7ee91bb PT |
433 | /** |
434 | * @since 2.0 | |
435 | */ | |
436 | @Override | |
437 | public ITmfTimestamp getCurrentTime() { | |
438 | return fCurrentTime; | |
439 | } | |
440 | ||
d7ee91bb PT |
441 | /** |
442 | * @since 2.0 | |
443 | */ | |
444 | @Override | |
445 | public TmfTimeRange getCurrentRange() { | |
446 | return fCurrentRange; | |
447 | } | |
448 | ||
449 | /** | |
d7ee91bb PT |
450 | * @since 2.0 |
451 | */ | |
66262ad8 BH |
452 | @Override |
453 | public ITmfTimestamp getInitialRangeOffset() { | |
d7ee91bb PT |
454 | final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
455 | return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); | |
456 | } | |
457 | ||
20658947 | 458 | // ------------------------------------------------------------------------ |
d7ee91bb | 459 | // Convenience setters |
20658947 FC |
460 | // ------------------------------------------------------------------------ |
461 | ||
0316808c FC |
462 | /** |
463 | * Set the trace cache size. Must be done at initialization time. | |
0bfb7d06 | 464 | * |
0316808c FC |
465 | * @param cacheSize The trace cache size |
466 | */ | |
467 | protected void setCacheSize(final int cacheSize) { | |
468 | fCacheSize = cacheSize; | |
469 | } | |
470 | ||
471 | /** | |
472 | * Set the trace known number of events. This can be quite dynamic | |
473 | * during indexing or for live traces. | |
0bfb7d06 | 474 | * |
0316808c FC |
475 | * @param nbEvents The number of events |
476 | */ | |
477 | protected synchronized void setNbEvents(final long nbEvents) { | |
478 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; | |
479 | } | |
480 | ||
20658947 FC |
481 | /** |
482 | * Update the trace events time range | |
0bfb7d06 | 483 | * |
20658947 | 484 | * @param range the new time range |
3bd46eef | 485 | * @since 2.0 |
20658947 FC |
486 | */ |
487 | protected void setTimeRange(final TmfTimeRange range) { | |
4593bd5b AM |
488 | fStartTime = range.getStartTime(); |
489 | fEndTime = range.getEndTime(); | |
20658947 FC |
490 | } |
491 | ||
492 | /** | |
493 | * Update the trace chronologically first event timestamp | |
0bfb7d06 | 494 | * |
20658947 | 495 | * @param startTime the new first event timestamp |
3bd46eef | 496 | * @since 2.0 |
20658947 FC |
497 | */ |
498 | protected void setStartTime(final ITmfTimestamp startTime) { | |
4593bd5b | 499 | fStartTime = startTime; |
20658947 FC |
500 | } |
501 | ||
502 | /** | |
503 | * Update the trace chronologically last event timestamp | |
0bfb7d06 | 504 | * |
20658947 | 505 | * @param endTime the new last event timestamp |
3bd46eef | 506 | * @since 2.0 |
20658947 FC |
507 | */ |
508 | protected void setEndTime(final ITmfTimestamp endTime) { | |
4593bd5b | 509 | fEndTime = endTime; |
20658947 FC |
510 | } |
511 | ||
512 | /** | |
0316808c | 513 | * Set the polling interval for live traces (default = 0 = no streaming). |
0bfb7d06 | 514 | * |
20658947 FC |
515 | * @param interval the new trace streaming interval |
516 | */ | |
517 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 518 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
519 | } |
520 | ||
0316808c FC |
521 | /** |
522 | * Set the trace indexer. Must be done at initialization time. | |
0bfb7d06 | 523 | * |
0316808c FC |
524 | * @param indexer the trace indexer |
525 | */ | |
6256d8ad | 526 | protected void setIndexer(final ITmfTraceIndexer indexer) { |
0316808c FC |
527 | fIndexer = indexer; |
528 | } | |
529 | ||
530 | /** | |
531 | * Set the trace parser. Must be done at initialization time. | |
0bfb7d06 | 532 | * |
0316808c FC |
533 | * @param parser the new trace parser |
534 | */ | |
6256d8ad | 535 | protected void setParser(final ITmfEventParser parser) { |
0316808c FC |
536 | fParser = parser; |
537 | } | |
538 | ||
09e86496 | 539 | // ------------------------------------------------------------------------ |
7e6347b0 | 540 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
541 | // ------------------------------------------------------------------------ |
542 | ||
1b70b6dc | 543 | @Override |
7e6347b0 | 544 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 545 | |
7e6347b0 | 546 | // A rank <= 0 indicates to seek the first event |
2352aed9 | 547 | if (rank <= 0) { |
1e1bef82 | 548 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
549 | context.setRank(0); |
550 | return context; | |
551 | } | |
09e86496 | 552 | |
09e86496 | 553 | // Position the trace at the checkpoint |
7e6347b0 | 554 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
555 | |
556 | // And locate the requested event context | |
7e6347b0 FC |
557 | long pos = context.getRank(); |
558 | if (pos < rank) { | |
c32744d6 | 559 | ITmfEvent event = getNext(context); |
0bfb7d06 | 560 | while ((event != null) && (++pos < rank)) { |
c32744d6 | 561 | event = getNext(context); |
7e6347b0 | 562 | } |
09e86496 FC |
563 | } |
564 | return context; | |
1b70b6dc PT |
565 | } |
566 | ||
3bd46eef AM |
567 | /** |
568 | * @since 2.0 | |
09e86496 FC |
569 | */ |
570 | @Override | |
7e6347b0 | 571 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 572 | |
7e6347b0 | 573 | // A null timestamp indicates to seek the first event |
2352aed9 | 574 | if (timestamp == null) { |
1e1bef82 | 575 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
576 | context.setRank(0); |
577 | return context; | |
578 | } | |
09e86496 | 579 | |
1703b536 | 580 | // Position the trace at the checkpoint |
408e65d2 | 581 | ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
582 | |
583 | // And locate the requested event context | |
ea271da6 PT |
584 | ITmfLocation previousLocation = context.getLocation(); |
585 | long previousRank = context.getRank(); | |
586 | ITmfEvent event = getNext(context); | |
7e6347b0 | 587 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
ea271da6 PT |
588 | previousLocation = context.getLocation(); |
589 | previousRank = context.getRank(); | |
590 | event = getNext(context); | |
09e86496 | 591 | } |
0316808c FC |
592 | if (event == null) { |
593 | context.setLocation(null); | |
594 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
ea271da6 PT |
595 | } else { |
596 | context.dispose(); | |
597 | context = seekEvent(previousLocation); | |
598 | context.setRank(previousRank); | |
0316808c | 599 | } |
09e86496 FC |
600 | return context; |
601 | } | |
0283f7ff | 602 | |
09e86496 FC |
603 | // ------------------------------------------------------------------------ |
604 | // ITmfTrace - Read operations (returning an actual event) | |
605 | // ------------------------------------------------------------------------ | |
606 | ||
d4011df2 | 607 | @Override |
6256d8ad | 608 | public synchronized ITmfEvent getNext(final ITmfContext context) { |
09e86496 | 609 | // parseEvent() does not update the context |
6256d8ad | 610 | final ITmfEvent event = fParser.parseEvent(context); |
09e86496 | 611 | if (event != null) { |
d337369a | 612 | updateAttributes(context, event.getTimestamp()); |
09e86496 FC |
613 | context.setLocation(getCurrentLocation()); |
614 | context.increaseRank(); | |
615 | processEvent(event); | |
616 | } | |
617 | return event; | |
618 | } | |
619 | ||
620 | /** | |
d337369a | 621 | * Hook for special event processing by the concrete class |
7e6347b0 | 622 | * (called by TmfTrace.getEvent()) |
0bfb7d06 | 623 | * |
d337369a | 624 | * @param event the event |
09e86496 FC |
625 | */ |
626 | protected void processEvent(final ITmfEvent event) { | |
d337369a | 627 | // Do nothing |
09e86496 FC |
628 | } |
629 | ||
d337369a FC |
630 | /** |
631 | * Update the trace attributes | |
0bfb7d06 | 632 | * |
d337369a | 633 | * @param context the current trace context |
2848c377 | 634 | * @param timestamp the corresponding timestamp |
3bd46eef | 635 | * @since 2.0 |
d337369a FC |
636 | */ |
637 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { | |
0bfb7d06 | 638 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) { |
4593bd5b | 639 | fStartTime = timestamp; |
09e86496 | 640 | } |
0bfb7d06 | 641 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) { |
4593bd5b | 642 | fEndTime = timestamp; |
09e86496 | 643 | } |
d7ee91bb PT |
644 | if (fCurrentRange == TmfTimeRange.NULL_RANGE) { |
645 | fCurrentTime = timestamp; | |
646 | ITmfTimestamp initialOffset = getInitialRangeOffset(); | |
647 | long endValue = timestamp.getValue() + initialOffset.normalize(0, timestamp.getScale()).getValue(); | |
648 | ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, timestamp.getScale()); | |
649 | fCurrentRange = new TmfTimeRange(timestamp, endTimestamp); | |
650 | } | |
09e86496 | 651 | if (context.hasValidRank()) { |
d337369a | 652 | long rank = context.getRank(); |
09e86496 FC |
653 | if (fNbEvents <= rank) { |
654 | fNbEvents = rank + 1; | |
655 | } | |
200789b3 AM |
656 | if (fIndexer != null) { |
657 | fIndexer.updateIndex(context, timestamp); | |
658 | } | |
09e86496 | 659 | } |
abfad0aa FC |
660 | } |
661 | ||
3791b5df | 662 | // ------------------------------------------------------------------------ |
d337369a | 663 | // TmfDataProvider |
3791b5df FC |
664 | // ------------------------------------------------------------------------ |
665 | ||
77c4a6df AM |
666 | /** |
667 | * @since 2.0 | |
d337369a | 668 | */ |
3791b5df | 669 | @Override |
5419a136 | 670 | public synchronized ITmfContext armRequest(final ITmfDataRequest request) { |
faa38350 PT |
671 | if (executorIsShutdown()) { |
672 | return null; | |
673 | } | |
5419a136 AM |
674 | if ((request instanceof ITmfEventRequest) |
675 | && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime()) | |
676 | && (request.getIndex() == 0)) | |
677 | { | |
678 | final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime()); | |
679 | ((ITmfEventRequest) request).setStartIndex((int) context.getRank()); | |
8584dc20 | 680 | return context; |
8584dc20 | 681 | |
5419a136 AM |
682 | } |
683 | return seekEvent(request.getIndex()); | |
3791b5df FC |
684 | } |
685 | ||
faa38350 PT |
686 | // ------------------------------------------------------------------------ |
687 | // Signal handlers | |
688 | // ------------------------------------------------------------------------ | |
689 | ||
690 | /** | |
691 | * Handler for the Trace Opened signal | |
692 | * | |
693 | * @param signal | |
694 | * The incoming signal | |
695 | * @since 2.0 | |
696 | */ | |
697 | @TmfSignalHandler | |
698 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
fe0c44c4 AM |
699 | ITmfTrace trace = null; |
700 | for (ITmfTrace expTrace : signal.getTrace().getTraces()) { | |
701 | if (expTrace == this) { | |
702 | trace = expTrace; | |
703 | break; | |
faa38350 PT |
704 | } |
705 | } | |
faa38350 | 706 | |
fe0c44c4 AM |
707 | if (trace == null) { |
708 | /* This signal is not for us */ | |
709 | return; | |
710 | } | |
711 | ||
712 | /* | |
713 | * The signal is for this trace, or for an experiment containing | |
714 | * this trace. | |
715 | */ | |
716 | try { | |
717 | buildStatistics(); | |
718 | buildStateSystem(); | |
719 | } catch (TmfTraceException e) { | |
720 | e.printStackTrace(); | |
721 | } | |
722 | ||
723 | /* Refresh the project, so it can pick up new files that got created. */ | |
724 | try { | |
725 | if (fResource != null) { | |
726 | fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); | |
faa38350 | 727 | } |
fe0c44c4 AM |
728 | } catch (CoreException e) { |
729 | e.printStackTrace(); | |
faa38350 | 730 | } |
fe0c44c4 | 731 | |
faa38350 | 732 | if (signal.getTrace() == this) { |
fe0c44c4 | 733 | /* Additionally, the signal is directly for this trace or experiment. */ |
faa38350 PT |
734 | if (getNbEvents() == 0) { |
735 | return; | |
736 | } | |
737 | ||
738 | final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH); | |
739 | final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange); | |
740 | ||
741 | // Broadcast in separate thread to prevent deadlock | |
742 | new Thread() { | |
743 | @Override | |
744 | public void run() { | |
745 | broadcast(rangeUpdatedsignal); | |
746 | } | |
747 | }.start(); | |
748 | return; | |
749 | } | |
750 | } | |
751 | ||
752 | /** | |
753 | * Signal handler for the TmfTraceRangeUpdatedSignal signal | |
754 | * | |
755 | * @param signal The incoming signal | |
756 | * @since 2.0 | |
757 | */ | |
758 | @TmfSignalHandler | |
759 | public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) { | |
760 | if (signal.getTrace() == this) { | |
761 | getIndexer().buildIndex(getNbEvents(), signal.getRange(), false); | |
762 | } | |
763 | } | |
764 | ||
d7ee91bb PT |
765 | /** |
766 | * Signal handler for the TmfTimeSynchSignal signal | |
767 | * | |
768 | * @param signal The incoming signal | |
769 | * @since 2.0 | |
770 | */ | |
771 | @TmfSignalHandler | |
772 | public void synchToTime(final TmfTimeSynchSignal signal) { | |
773 | if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) { | |
774 | fCurrentTime = signal.getCurrentTime(); | |
775 | } | |
776 | } | |
777 | ||
778 | /** | |
779 | * Signal handler for the TmfRangeSynchSignal signal | |
780 | * | |
781 | * @param signal The incoming signal | |
782 | * @since 2.0 | |
783 | */ | |
784 | @TmfSignalHandler | |
785 | public void synchToRange(final TmfRangeSynchSignal signal) { | |
786 | if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) { | |
787 | fCurrentTime = signal.getCurrentTime(); | |
788 | } | |
789 | if (signal.getCurrentRange().getIntersection(getTimeRange()) != null) { | |
790 | fCurrentRange = signal.getCurrentRange().getIntersection(getTimeRange()); | |
791 | } | |
792 | } | |
793 | ||
3791b5df | 794 | // ------------------------------------------------------------------------ |
09e86496 | 795 | // toString |
3791b5df FC |
796 | // ------------------------------------------------------------------------ |
797 | ||
12c155f5 | 798 | @Override |
09e86496 | 799 | @SuppressWarnings("nls") |
5419a136 | 800 | public synchronized String toString() { |
20658947 FC |
801 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
802 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
803 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
804 | } |
805 | ||
8c8bf09f | 806 | } |