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 | |
ff7b95a5 | 290 | Multimap<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(); |
c068a752 GB |
291 | for (IAnalysisModuleHelper helper : modules.values()) { |
292 | try { | |
293 | IAnalysisModule module = helper.newModule(this); | |
f479550c GB |
294 | if (module == null) { |
295 | continue; | |
296 | } | |
c068a752 GB |
297 | fAnalysisModules.put(module.getId(), module); |
298 | if (module.isAutomatic()) { | |
299 | status.add(module.schedule()); | |
300 | } | |
301 | } catch (TmfAnalysisException e) { | |
26683871 | 302 | status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage())); |
c068a752 GB |
303 | } |
304 | } | |
305 | return status; | |
306 | } | |
307 | ||
308 | @Override | |
ff3f02c8 | 309 | public IAnalysisModule getAnalysisModule(String analysisId) { |
c068a752 GB |
310 | return fAnalysisModules.get(analysisId); |
311 | } | |
312 | ||
ff3f02c8 | 313 | |
ff3f02c8 AM |
314 | @Override |
315 | public Iterable<IAnalysisModule> getAnalysisModules() { | |
316 | synchronized (fAnalysisModules) { | |
317 | Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values()); | |
318 | return modules; | |
319 | } | |
320 | } | |
321 | ||
b04903a2 AM |
322 | @Override |
323 | public Iterable<ITmfEventAspect> getEventAspects() { | |
324 | /* By default we provide only the base aspects valid for all trace types */ | |
325 | return BASE_ASPECTS; | |
326 | } | |
327 | ||
b5ee6881 FC |
328 | /** |
329 | * Clears the trace | |
330 | */ | |
331 | @Override | |
332 | public synchronized void dispose() { | |
1a4205d9 | 333 | /* Clean up the index if applicable */ |
77551cc2 FC |
334 | if (getIndexer() != null) { |
335 | getIndexer().dispose(); | |
336 | } | |
1a4205d9 | 337 | |
a1529f38 | 338 | /* Clean up the analysis modules */ |
ff3f02c8 AM |
339 | synchronized (fAnalysisModules) { |
340 | for (IAnalysisModule module : fAnalysisModules.values()) { | |
341 | module.dispose(); | |
342 | } | |
6ef5ae35 | 343 | fAnalysisModules.clear(); |
a1529f38 AM |
344 | } |
345 | ||
b5ee6881 FC |
346 | super.dispose(); |
347 | } | |
348 | ||
3791b5df | 349 | // ------------------------------------------------------------------------ |
09e86496 | 350 | // ITmfTrace - Basic getters |
e31e01e8 | 351 | // ------------------------------------------------------------------------ |
8c8bf09f | 352 | |
d4011df2 | 353 | @Override |
09e86496 FC |
354 | public IResource getResource() { |
355 | return fResource; | |
8c8bf09f ASL |
356 | } |
357 | ||
2b0005f0 PT |
358 | @Override |
359 | public @Nullable String getTraceTypeId() { | |
360 | return fTraceTypeId; | |
361 | } | |
362 | ||
d4011df2 | 363 | @Override |
09e86496 FC |
364 | public String getPath() { |
365 | return fPath; | |
8c8bf09f ASL |
366 | } |
367 | ||
20658947 FC |
368 | @Override |
369 | public int getCacheSize() { | |
370 | return fCacheSize; | |
371 | } | |
372 | ||
20658947 FC |
373 | @Override |
374 | public long getStreamingInterval() { | |
375 | return fStreamingInterval; | |
376 | } | |
377 | ||
0316808c FC |
378 | /** |
379 | * @return the trace indexer | |
380 | */ | |
6256d8ad | 381 | protected ITmfTraceIndexer getIndexer() { |
0316808c FC |
382 | return fIndexer; |
383 | } | |
384 | ||
09e86496 FC |
385 | // ------------------------------------------------------------------------ |
386 | // ITmfTrace - Trace characteristics getters | |
387 | // ------------------------------------------------------------------------ | |
388 | ||
d4011df2 | 389 | @Override |
e9a6e38e | 390 | public long getNbEvents() { |
3791b5df | 391 | return fNbEvents; |
b0a282fb FC |
392 | } |
393 | ||
d4011df2 | 394 | @Override |
6cfc180e | 395 | public @NonNull TmfTimeRange getTimeRange() { |
cb866e08 | 396 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
397 | } |
398 | ||
d4011df2 | 399 | @Override |
4df4581d | 400 | public ITmfTimestamp getStartTime() { |
4593bd5b | 401 | return fStartTime; |
146a887c FC |
402 | } |
403 | ||
d4011df2 | 404 | @Override |
4df4581d | 405 | public ITmfTimestamp getEndTime() { |
4593bd5b | 406 | return fEndTime; |
20658947 FC |
407 | } |
408 | ||
66262ad8 BH |
409 | @Override |
410 | public ITmfTimestamp getInitialRangeOffset() { | |
d7ee91bb PT |
411 | final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
412 | return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); | |
413 | } | |
414 | ||
bb52f9bc GB |
415 | @Override |
416 | public String getHostId() { | |
417 | return this.getName(); | |
418 | } | |
419 | ||
20658947 | 420 | // ------------------------------------------------------------------------ |
d7ee91bb | 421 | // Convenience setters |
20658947 FC |
422 | // ------------------------------------------------------------------------ |
423 | ||
0316808c FC |
424 | /** |
425 | * Set the trace cache size. Must be done at initialization time. | |
0bfb7d06 | 426 | * |
0316808c FC |
427 | * @param cacheSize The trace cache size |
428 | */ | |
429 | protected void setCacheSize(final int cacheSize) { | |
430 | fCacheSize = cacheSize; | |
431 | } | |
432 | ||
433 | /** | |
434 | * Set the trace known number of events. This can be quite dynamic | |
435 | * during indexing or for live traces. | |
0bfb7d06 | 436 | * |
0316808c FC |
437 | * @param nbEvents The number of events |
438 | */ | |
439 | protected synchronized void setNbEvents(final long nbEvents) { | |
440 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; | |
441 | } | |
442 | ||
20658947 FC |
443 | /** |
444 | * Update the trace events time range | |
0bfb7d06 | 445 | * |
20658947 FC |
446 | * @param range the new time range |
447 | */ | |
6cfc180e | 448 | protected void setTimeRange(final @NonNull TmfTimeRange range) { |
4593bd5b AM |
449 | fStartTime = range.getStartTime(); |
450 | fEndTime = range.getEndTime(); | |
20658947 FC |
451 | } |
452 | ||
453 | /** | |
454 | * Update the trace chronologically first event timestamp | |
0bfb7d06 | 455 | * |
20658947 FC |
456 | * @param startTime the new first event timestamp |
457 | */ | |
6cfc180e | 458 | protected void setStartTime(final @NonNull ITmfTimestamp startTime) { |
4593bd5b | 459 | fStartTime = startTime; |
20658947 FC |
460 | } |
461 | ||
462 | /** | |
463 | * Update the trace chronologically last event timestamp | |
0bfb7d06 | 464 | * |
20658947 FC |
465 | * @param endTime the new last event timestamp |
466 | */ | |
6cfc180e | 467 | protected void setEndTime(final @NonNull ITmfTimestamp endTime) { |
4593bd5b | 468 | fEndTime = endTime; |
20658947 FC |
469 | } |
470 | ||
471 | /** | |
0316808c | 472 | * Set the polling interval for live traces (default = 0 = no streaming). |
0bfb7d06 | 473 | * |
20658947 FC |
474 | * @param interval the new trace streaming interval |
475 | */ | |
476 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 477 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
478 | } |
479 | ||
09e86496 | 480 | // ------------------------------------------------------------------------ |
7e6347b0 | 481 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
482 | // ------------------------------------------------------------------------ |
483 | ||
1b70b6dc | 484 | @Override |
7e6347b0 | 485 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 486 | |
7e6347b0 | 487 | // A rank <= 0 indicates to seek the first event |
2352aed9 | 488 | if (rank <= 0) { |
1e1bef82 | 489 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
490 | context.setRank(0); |
491 | return context; | |
492 | } | |
09e86496 | 493 | |
09e86496 | 494 | // Position the trace at the checkpoint |
7e6347b0 | 495 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
496 | |
497 | // And locate the requested event context | |
7e6347b0 FC |
498 | long pos = context.getRank(); |
499 | if (pos < rank) { | |
c32744d6 | 500 | ITmfEvent event = getNext(context); |
0bfb7d06 | 501 | while ((event != null) && (++pos < rank)) { |
c32744d6 | 502 | event = getNext(context); |
7e6347b0 | 503 | } |
09e86496 FC |
504 | } |
505 | return context; | |
1b70b6dc PT |
506 | } |
507 | ||
09e86496 | 508 | @Override |
7e6347b0 | 509 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 510 | |
7e6347b0 | 511 | // A null timestamp indicates to seek the first event |
2352aed9 | 512 | if (timestamp == null) { |
1e1bef82 | 513 | ITmfContext context = seekEvent((ITmfLocation) null); |
2352aed9 FC |
514 | context.setRank(0); |
515 | return context; | |
516 | } | |
09e86496 | 517 | |
1703b536 | 518 | // Position the trace at the checkpoint |
408e65d2 | 519 | ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
520 | |
521 | // And locate the requested event context | |
ea271da6 PT |
522 | ITmfLocation previousLocation = context.getLocation(); |
523 | long previousRank = context.getRank(); | |
524 | ITmfEvent event = getNext(context); | |
065cc19b | 525 | while (event != null && event.getTimestamp().compareTo(timestamp) < 0) { |
ea271da6 PT |
526 | previousLocation = context.getLocation(); |
527 | previousRank = context.getRank(); | |
528 | event = getNext(context); | |
09e86496 | 529 | } |
0316808c FC |
530 | if (event == null) { |
531 | context.setLocation(null); | |
532 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
ea271da6 PT |
533 | } else { |
534 | context.dispose(); | |
535 | context = seekEvent(previousLocation); | |
536 | context.setRank(previousRank); | |
0316808c | 537 | } |
09e86496 FC |
538 | return context; |
539 | } | |
0283f7ff | 540 | |
09e86496 | 541 | // ------------------------------------------------------------------------ |
5733be39 | 542 | // Read operations (returning an actual event) |
09e86496 FC |
543 | // ------------------------------------------------------------------------ |
544 | ||
5733be39 AM |
545 | @Override |
546 | public abstract ITmfEvent parseEvent(ITmfContext context); | |
547 | ||
d4011df2 | 548 | @Override |
6256d8ad | 549 | public synchronized ITmfEvent getNext(final ITmfContext context) { |
09e86496 | 550 | // parseEvent() does not update the context |
5733be39 | 551 | final ITmfEvent event = parseEvent(context); |
09e86496 | 552 | if (event != null) { |
5904c11e | 553 | updateAttributes(context, event); |
09e86496 FC |
554 | context.setLocation(getCurrentLocation()); |
555 | context.increaseRank(); | |
09e86496 FC |
556 | } |
557 | return event; | |
558 | } | |
559 | ||
d337369a FC |
560 | /** |
561 | * Update the trace attributes | |
0bfb7d06 | 562 | * |
d337369a | 563 | * @param context the current trace context |
2848c377 | 564 | * @param timestamp the corresponding timestamp |
5904c11e | 565 | * @deprecated Use {@link #updateAttributes(ITmfContext, ITmfEvent)} |
d337369a | 566 | */ |
5904c11e | 567 | @Deprecated |
6cfc180e | 568 | protected synchronized void updateAttributes(final ITmfContext context, final @NonNull ITmfTimestamp timestamp) { |
5904c11e PT |
569 | updateAttributes(context, new TmfEvent(this, context.getRank(), timestamp, null, null)); |
570 | } | |
571 | ||
572 | /** | |
573 | * Update the trace attributes | |
574 | * | |
575 | * @param context the current trace context | |
576 | * @param event the corresponding event | |
0336f981 | 577 | * @since 1.1 |
5904c11e PT |
578 | */ |
579 | protected synchronized void updateAttributes(final ITmfContext context, final @NonNull ITmfEvent event) { | |
580 | ITmfTimestamp timestamp = event.getTimestamp(); | |
581 | ITmfTimestamp endTime = timestamp; | |
582 | if (event instanceof ITmfLostEvent) { | |
583 | endTime = ((ITmfLostEvent) event).getTimeRange().getEndTime(); | |
584 | } | |
065cc19b | 585 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp) > 0)) { |
4593bd5b | 586 | fStartTime = timestamp; |
09e86496 | 587 | } |
5904c11e PT |
588 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(endTime) < 0)) { |
589 | fEndTime = endTime; | |
09e86496 FC |
590 | } |
591 | if (context.hasValidRank()) { | |
d337369a | 592 | long rank = context.getRank(); |
09e86496 FC |
593 | if (fNbEvents <= rank) { |
594 | fNbEvents = rank + 1; | |
595 | } | |
200789b3 AM |
596 | if (fIndexer != null) { |
597 | fIndexer.updateIndex(context, timestamp); | |
598 | } | |
09e86496 | 599 | } |
abfad0aa FC |
600 | } |
601 | ||
3791b5df | 602 | // ------------------------------------------------------------------------ |
d337369a | 603 | // TmfDataProvider |
3791b5df FC |
604 | // ------------------------------------------------------------------------ |
605 | ||
606 | @Override | |
fd3f1eff | 607 | public synchronized ITmfContext armRequest(final ITmfEventRequest request) { |
faa38350 PT |
608 | if (executorIsShutdown()) { |
609 | return null; | |
610 | } | |
fd3f1eff AM |
611 | if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime()) |
612 | && (request.getIndex() == 0)) { | |
613 | final ITmfContext context = seekEvent(request.getRange().getStartTime()); | |
614 | request.setStartIndex((int) context.getRank()); | |
8584dc20 | 615 | return context; |
8584dc20 | 616 | |
5419a136 AM |
617 | } |
618 | return seekEvent(request.getIndex()); | |
3791b5df FC |
619 | } |
620 | ||
faa38350 PT |
621 | // ------------------------------------------------------------------------ |
622 | // Signal handlers | |
623 | // ------------------------------------------------------------------------ | |
624 | ||
625 | /** | |
626 | * Handler for the Trace Opened signal | |
627 | * | |
628 | * @param signal | |
629 | * The incoming signal | |
faa38350 PT |
630 | */ |
631 | @TmfSignalHandler | |
632 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
b9a5bf8f AM |
633 | boolean signalIsForUs = false; |
634 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) { | |
635 | if (trace == this) { | |
636 | signalIsForUs = true; | |
fe0c44c4 | 637 | break; |
faa38350 PT |
638 | } |
639 | } | |
faa38350 | 640 | |
b9a5bf8f | 641 | if (!signalIsForUs) { |
fe0c44c4 AM |
642 | return; |
643 | } | |
644 | ||
645 | /* | |
b9a5bf8f | 646 | * The signal is either for this trace, or for an experiment containing |
fe0c44c4 AM |
647 | * this trace. |
648 | */ | |
be4a197a | 649 | IStatus status = executeAnalysis(); |
b22a582a AM |
650 | if (!status.isOK()) { |
651 | Activator.log(status); | |
fe0c44c4 AM |
652 | } |
653 | ||
b5e8ee95 | 654 | TmfTraceManager.refreshSupplementaryFiles(this); |
fe0c44c4 | 655 | |
faa38350 | 656 | if (signal.getTrace() == this) { |
f8fc4a3a | 657 | /* Additionally, the signal is directly for this trace. */ |
faa38350 PT |
658 | if (getNbEvents() == 0) { |
659 | return; | |
660 | } | |
661 | ||
f8fc4a3a PT |
662 | /* For a streaming trace, the range updated signal should be sent |
663 | * by the subclass when a new safe time is determined. | |
664 | */ | |
665 | if (getStreamingInterval() > 0) { | |
666 | return; | |
667 | } | |
668 | ||
6fd3c6e9 MAL |
669 | if (isComplete()) { |
670 | final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH); | |
671 | final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange); | |
faa38350 | 672 | |
6fd3c6e9 MAL |
673 | // Broadcast in separate thread to prevent deadlock |
674 | broadcastAsync(rangeUpdatedsignal); | |
675 | } | |
faa38350 PT |
676 | return; |
677 | } | |
678 | } | |
679 | ||
680 | /** | |
681 | * Signal handler for the TmfTraceRangeUpdatedSignal signal | |
682 | * | |
683 | * @param signal The incoming signal | |
faa38350 PT |
684 | */ |
685 | @TmfSignalHandler | |
686 | public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) { | |
687 | if (signal.getTrace() == this) { | |
688 | getIndexer().buildIndex(getNbEvents(), signal.getRange(), false); | |
689 | } | |
690 | } | |
691 | ||
032ecd45 MAL |
692 | /** |
693 | * Signal handler for the TmfTraceUpdatedSignal signal | |
694 | * | |
695 | * @param signal The incoming signal | |
032ecd45 MAL |
696 | */ |
697 | @TmfSignalHandler | |
698 | public void traceUpdated(final TmfTraceUpdatedSignal signal) { | |
699 | if (signal.getSource() == getIndexer()) { | |
700 | fNbEvents = signal.getNbEvents(); | |
701 | fStartTime = signal.getRange().getStartTime(); | |
702 | fEndTime = signal.getRange().getEndTime(); | |
703 | } | |
704 | } | |
705 | ||
e73a4ba5 GB |
706 | // ------------------------------------------------------------------------ |
707 | // Timestamp transformation functions | |
708 | // ------------------------------------------------------------------------ | |
709 | ||
e73a4ba5 GB |
710 | @Override |
711 | public ITmfTimestampTransform getTimestampTransform() { | |
712 | if (fTsTransform == null) { | |
6b44794a | 713 | fTsTransform = TimestampTransformFactory.getTimestampTransform(getResource()); |
e73a4ba5 GB |
714 | } |
715 | return fTsTransform; | |
716 | } | |
717 | ||
e73a4ba5 GB |
718 | @Override |
719 | public void setTimestampTransform(final ITmfTimestampTransform tt) { | |
720 | fTsTransform = tt; | |
6b44794a | 721 | TimestampTransformFactory.setTimestampTransform(getResource(), tt); |
e73a4ba5 GB |
722 | } |
723 | ||
e73a4ba5 | 724 | @Override |
6cfc180e | 725 | public @NonNull ITmfTimestamp createTimestamp(long ts) { |
b2c463c5 | 726 | return new TmfNanoTimestamp(getTimestampTransform().transform(ts)); |
e73a4ba5 GB |
727 | } |
728 | ||
3791b5df | 729 | // ------------------------------------------------------------------------ |
09e86496 | 730 | // toString |
3791b5df FC |
731 | // ------------------------------------------------------------------------ |
732 | ||
12c155f5 | 733 | @Override |
09e86496 | 734 | @SuppressWarnings("nls") |
5419a136 | 735 | public synchronized String toString() { |
20658947 FC |
736 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
737 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
738 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
739 | } |
740 | ||
6fd3c6e9 MAL |
741 | @Override |
742 | public boolean isComplete() { | |
743 | /* | |
744 | * Be default, all traces are "complete" which means no more data will | |
745 | * be added later | |
746 | */ | |
747 | return true; | |
748 | } | |
749 | ||
6fd3c6e9 MAL |
750 | @Override |
751 | public void setComplete(boolean isComplete) { | |
752 | /* | |
753 | * This should be overridden by trace classes that can support live | |
754 | * reading (traces in an incomplete state) | |
755 | */ | |
756 | } | |
8c8bf09f | 757 | } |