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