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