tmf: annotate TmfContext#location as nullable
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / experiment / TmfExperiment.java
CommitLineData
8c8bf09f 1/*******************************************************************************
5904c11e 2 * Copyright (c) 2009, 2015 Ericsson, École Polytechnique de Montréal
ce2388e0 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
ce2388e0 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
0316808c 11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
ea271da6
PT
12 * Patrick Tasse - Updated for removal of context clone
13 * Patrick Tasse - Updated for ranks in experiment location
e73a4ba5 14 * Geneviève Bastien - Added support of experiment synchronization
b3dd2736 15 * Added the initExperiment method and default constructor
d77f31da 16 * Bernd Hufmann - Updated for added interfaces to ITmfEventProvider
8c8bf09f
ASL
17 *******************************************************************************/
18
5c5fa260 19package org.eclipse.tracecompass.tmf.core.trace.experiment;
8c8bf09f 20
e73a4ba5 21import java.io.File;
032ecd45 22import java.nio.ByteBuffer;
4d2a4a2c 23import java.util.Collections;
fa62dc1d 24import java.util.List;
4d2a4a2c
GB
25import java.util.concurrent.locks.Lock;
26import java.util.concurrent.locks.ReentrantLock;
e73a4ba5 27
12c155f5 28import org.eclipse.core.resources.IProject;
828e5592 29import org.eclipse.core.resources.IResource;
e73a4ba5 30import org.eclipse.core.runtime.CoreException;
a94410d9 31import org.eclipse.core.runtime.IStatus;
9928ddeb 32import org.eclipse.core.runtime.MultiStatus;
a94410d9 33import org.eclipse.core.runtime.Status;
aa353506 34import org.eclipse.jdt.annotation.NonNull;
4178260e 35import org.eclipse.jdt.annotation.Nullable;
2bdf0193 36import org.eclipse.tracecompass.internal.tmf.core.Activator;
5c5fa260
AM
37import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentContext;
38import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentLocation;
39import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfLocationArray;
2bdf0193
AM
40import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
41import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
42import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
43import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
44import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
45import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
46import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
47import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSynchronizedSignal;
48import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
49import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationManager;
50import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
51import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
52import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
5c5fa260 53import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
5c5fa260
AM
54import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
55import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
56import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
2bdf0193
AM
57import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
58import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
59import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
60import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
8c8bf09f
ASL
61
62/**
9e0640dc 63 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
cbdacf03 64 * that are part of a tracing experiment.
4b7b3670
FC
65 *
66 * @version 1.0
67 * @author Francois Chouinard
8c8bf09f 68 */
5733be39 69public class TmfExperiment extends TmfTrace implements ITmfPersistentlyIndexable {
8c8bf09f 70
c32744d6
FC
71 // ------------------------------------------------------------------------
72 // Constants
73 // ------------------------------------------------------------------------
74
e73a4ba5
GB
75 /**
76 * The file name of the Synchronization
77 *
7f38b742
GB
78 * @deprecated This file name shouldn't be used directly anymore. All
79 * synchronization files have been moved to a folder and you
80 * should use the {@link #getSynchronizationFolder(boolean)}
81 * method to return the path to this folder.
e73a4ba5 82 */
04ba3554 83 @Deprecated
7f38b742 84 public static final String SYNCHRONIZATION_FILE_NAME = "synchronization.bin"; //$NON-NLS-1$
e73a4ba5 85
04ba3554
GB
86 /**
87 * The name of the directory containing trace synchronization data. This
88 * directory typically will be preserved when traces are synchronized.
89 * Analysis involved in synchronization can put their supplementary files in
90 * there so they are not deleted when synchronized traces are copied.
04ba3554 91 */
7f38b742 92 private static final String SYNCHRONIZATION_DIRECTORY = "sync_data"; //$NON-NLS-1$
04ba3554 93
9e0640dc
FC
94 /**
95 * The default index page size
96 */
97 public static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
c32744d6 98
8c8bf09f
ASL
99 // ------------------------------------------------------------------------
100 // Attributes
101 // ------------------------------------------------------------------------
102
9e0640dc
FC
103 /**
104 * The set of traces that constitute the experiment
105 */
106 private boolean fInitialized = false;
a1091415 107
4d2a4a2c
GB
108 /**
109 * Lock for synchronization methods. These methods cannot be 'synchronized'
110 * since it makes it impossible to use an event request on the experiment
111 * during synchronization (the request thread would block)
112 */
113 private final Lock fSyncLock = new ReentrantLock();
4178260e 114
8c8bf09f 115 // ------------------------------------------------------------------------
9e0640dc 116 // Construction
8c8bf09f
ASL
117 // ------------------------------------------------------------------------
118
9e0640dc 119 /**
4178260e
AM
120 * Default constructor. Should not be used directly, but is needed for
121 * extension points.
04ba3554 122 *
4178260e 123 * @deprecated Do not call this directly (but do not remove it either!)
b3dd2736 124 */
4178260e 125 @Deprecated
b3dd2736
GB
126 public TmfExperiment() {
127 super();
128 }
129
130 /**
4178260e 131 * Constructor of an experiment, taking the type, path, traces,
99504bb8
GB
132 * indexPageSize and resource
133 *
134 * @param type
4178260e 135 * The event type
99504bb8 136 * @param path
4178260e 137 * The experiment path
99504bb8 138 * @param traces
4178260e 139 * The experiment set of traces
99504bb8 140 * @param indexPageSize
4178260e
AM
141 * The experiment index page size. You can use
142 * {@link TmfExperiment#DEFAULT_INDEX_PAGE_SIZE} for a default
143 * value.
99504bb8 144 * @param resource
4178260e
AM
145 * The resource associated to the experiment. You can use 'null'
146 * for no resources (tests, etc.)
99504bb8 147 */
4178260e
AM
148 public TmfExperiment(final Class<? extends ITmfEvent> type,
149 final String path,
150 final ITmfTrace[] traces,
151 final int indexPageSize,
152 final @Nullable IResource resource) {
b3dd2736 153 initExperiment(type, path, traces, indexPageSize, resource);
8c8bf09f 154 }
a79913eb 155
032ecd45
MAL
156 @Override
157 protected ITmfTraceIndexer createIndexer(int interval) {
158 if (getCheckpointSize() > 0) {
159 return new TmfBTreeTraceIndexer(this, interval);
160 }
161 return super.createIndexer(interval);
162 }
163
8c8bf09f 164 /**
ff4ed569 165 * Clears the experiment
8c8bf09f
ASL
166 */
167 @Override
a79913eb
FC
168 public synchronized void dispose() {
169
77551cc2
FC
170 // Clean up the index if applicable
171 if (getIndexer() != null) {
172 getIndexer().dispose();
173 }
b5ee6881 174
2fb2eb37 175 super.dispose();
8c8bf09f
ASL
176 }
177
9e0640dc
FC
178 // ------------------------------------------------------------------------
179 // ITmfTrace - Initializers
180 // ------------------------------------------------------------------------
181
9e0640dc 182 @Override
6256d8ad 183 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
4178260e 184 /* Do nothing for experiments */
9e0640dc
FC
185 }
186
b3dd2736
GB
187 /**
188 * Initialization of an experiment, taking the type, path, traces,
189 * indexPageSize and resource
190 *
191 * @param type
192 * the event type
193 * @param path
194 * the experiment path
195 * @param traces
196 * the experiment set of traces
197 * @param indexPageSize
198 * the experiment index page size
199 * @param resource
200 * the resource associated to the experiment
b3dd2736 201 */
4178260e
AM
202 public void initExperiment(final Class<? extends ITmfEvent> type,
203 final String path,
204 final ITmfTrace[] traces,
205 final int indexPageSize,
206 final @Nullable IResource resource) {
207
b3dd2736
GB
208 setCacheSize(indexPageSize);
209 setStreamingInterval(0);
d77f31da 210
7976315b 211 // traces have to be set before super.initialize()
d77f31da
BH
212 if (traces != null) {
213 // initialize
d77f31da
BH
214 for (ITmfTrace trace : traces) {
215 if (trace != null) {
216 addChild(trace);
217 }
218 }
219 }
220
b3dd2736
GB
221 try {
222 super.initialize(resource, path, type);
223 } catch (TmfTraceException e) {
224 Activator.logError("Error initializing experiment", e); //$NON-NLS-1$
225 }
226
b3dd2736 227 if (resource != null) {
04ba3554 228 this.synchronizeTraces();
b3dd2736
GB
229 }
230 }
231
9e0640dc 232 @Override
a94410d9
MK
233 public IStatus validate(final IProject project, final String path) {
234 return Status.OK_STATUS;
9e0640dc
FC
235 }
236
8c8bf09f 237 // ------------------------------------------------------------------------
e31e01e8 238 // Accessors
8c8bf09f
ASL
239 // ------------------------------------------------------------------------
240
f0c0d2c2
AM
241 /**
242 * Get the traces contained in this experiment.
243 *
244 * @return The array of contained traces
245 */
aa353506 246 public List<@NonNull ITmfTrace> getTraces() {
fa62dc1d 247 return getChildren(ITmfTrace.class);
8c8bf09f
ASL
248 }
249
8c8bf09f 250 /**
cbdacf03
FC
251 * Returns the timestamp of the event at the requested index. If none,
252 * returns null.
9b749023 253 *
e73a4ba5
GB
254 * @param index
255 * the event index (rank)
0d9a6d76 256 * @return the corresponding event timestamp
8c8bf09f 257 */
cbdacf03 258 public ITmfTimestamp getTimestamp(final int index) {
0316808c 259 final ITmfContext context = seekEvent(index);
c32744d6 260 final ITmfEvent event = getNext(context);
4c9f2944 261 context.dispose();
a79913eb 262 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
263 }
264
49e2f79a
FC
265 // ------------------------------------------------------------------------
266 // Request management
267 // ------------------------------------------------------------------------
268
269 @Override
fd3f1eff 270 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
9b749023 271
6a953367 272 // Make sure we have something to read from
fa62dc1d 273 if (getChildren().isEmpty()) {
6a953367
BH
274 return null;
275 }
9b749023 276
fd3f1eff
AM
277 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
278 && request.getIndex() == 0) {
279 final ITmfContext context = seekEvent(request.getRange().getStartTime());
280 request.setStartIndex((int) context.getRank());
49e2f79a 281 return context;
5419a136 282
49e2f79a
FC
283 }
284
5419a136 285 return seekEvent(request.getIndex());
49e2f79a
FC
286 }
287
a79913eb 288 // ------------------------------------------------------------------------
9f584e4c
FC
289 // ITmfTrace trace positioning
290 // ------------------------------------------------------------------------
291
a79913eb 292 @Override
1e1bef82 293 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
a79913eb 294 // Validate the location
9e0640dc 295 if (location != null && !(location instanceof TmfExperimentLocation)) {
a79913eb 296 return null; // Throw an exception?
9e0640dc 297 }
d77f31da 298
fa62dc1d 299 int length = getNbChildren();
8f50c396 300
ea271da6 301 // Initialize the location array if necessary
38db0431 302 TmfLocationArray locationArray = ((location == null) ? new TmfLocationArray(length) : ((TmfExperimentLocation) location).getLocationInfo());
ea271da6
PT
303
304 ITmfLocation[] locations = locationArray.getLocations();
305 long[] ranks = locationArray.getRanks();
306
a79913eb 307 // Create and populate the context's traces contexts
fa62dc1d 308 final TmfExperimentContext context = new TmfExperimentContext(length);
9b635e61 309
d62bb185 310 // Position the traces
ea271da6 311 long rank = 0;
fa62dc1d 312 for (int i = 0; i < length; i++) {
a79913eb 313 // Get the relevant trace attributes
fa62dc1d 314 final ITmfContext traceContext = ((ITmfTrace) getChild(i)).seekEvent(locations[i]);
07ef7847 315 context.setContext(i, traceContext);
ea271da6 316 traceContext.setRank(ranks[i]);
7f38b742
GB
317 // update location after seek
318 locations[i] = traceContext.getLocation();
fa62dc1d 319 context.setEvent(i, ((ITmfTrace) getChild(i)).getNext(traceContext));
ea271da6 320 rank += ranks[i];
a79913eb 321 }
8f50c396 322
a79913eb 323 // Finalize context
ea271da6 324 context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
a79913eb 325 context.setLastTrace(TmfExperimentContext.NO_TRACE);
ea271da6 326 context.setRank(rank);
49e2f79a 327
9b749023 328 return context;
a79913eb 329 }
9f584e4c 330
3bd44ac8
FC
331 // ------------------------------------------------------------------------
332 // ITmfTrace - SeekEvent operations (returning a trace context)
333 // ------------------------------------------------------------------------
334
c76c54bb 335 @Override
0316808c 336 public ITmfContext seekEvent(final double ratio) {
91f6e587 337 final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
c76c54bb
FC
338 return context;
339 }
340
a79913eb 341 @Override
1e1bef82 342 public double getLocationRatio(final ITmfLocation location) {
9e0640dc 343 if (location instanceof TmfExperimentLocation) {
ea271da6
PT
344 long rank = 0;
345 TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
346 for (int i = 0; i < locationArray.size(); i++) {
347 rank += locationArray.getRank(i);
348 }
349 return (double) rank / getNbEvents();
9e0640dc
FC
350 }
351 return 0.0;
c76c54bb
FC
352 }
353
a79913eb 354 @Override
1e1bef82 355 public ITmfLocation getCurrentLocation() {
ea271da6
PT
356 // never used
357 return null;
a79913eb 358 }
c76c54bb 359
9e0640dc
FC
360 // ------------------------------------------------------------------------
361 // ITmfTrace trace positioning
362 // ------------------------------------------------------------------------
363
07671572 364 @Override
6256d8ad 365 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
ea271da6
PT
366 final ITmfContext tmpContext = seekEvent(context.getLocation());
367 final ITmfEvent event = getNext(tmpContext);
07671572
FC
368 return event;
369 }
a79913eb 370
0316808c 371 @Override
6256d8ad 372 public synchronized ITmfEvent getNext(ITmfContext context) {
a79913eb
FC
373
374 // Validate the context
9e0640dc 375 if (!(context instanceof TmfExperimentContext)) {
a79913eb 376 return null; // Throw an exception?
9e0640dc 377 }
0e8c76f8 378
fa62dc1d
BH
379 int length = getNbChildren();
380
0e8c76f8 381 // Make sure that we have something to read from
fa62dc1d 382 if (length == 0) {
0e8c76f8
BH
383 return null;
384 }
385
a87cc4ef 386 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 387
e73a4ba5
GB
388 // If an event was consumed previously, first get the next one from that
389 // trace
cbdacf03 390 final int lastTrace = expContext.getLastTrace();
a79913eb 391 if (lastTrace != TmfExperimentContext.NO_TRACE) {
07ef7847 392 final ITmfContext traceContext = expContext.getContext(lastTrace);
fa62dc1d 393 expContext.setEvent(lastTrace, ((ITmfTrace) getChild(lastTrace)).getNext(traceContext));
a79913eb 394 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
395 }
396
397 // Scan the candidate events and identify the "next" trace to read from
398 int trace = TmfExperimentContext.NO_TRACE;
a4115405 399 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
fa62dc1d 400 for (int i = 0; i < length; i++) {
07ef7847 401 final ITmfEvent event = expContext.getEvent(i);
fa62dc1d 402
cbf0057c 403 if (event != null) {
cbdacf03 404 final ITmfTimestamp otherTS = event.getTimestamp();
065cc19b 405 if (otherTS.compareTo(timestamp) < 0) {
a79913eb
FC
406 trace = i;
407 timestamp = otherTS;
408 }
409 }
410 }
a87cc4ef 411
6256d8ad 412 ITmfEvent event = null;
07671572 413 if (trace != TmfExperimentContext.NO_TRACE) {
07ef7847 414 event = expContext.getEvent(trace);
408e65d2 415 if (event != null) {
5904c11e 416 updateAttributes(expContext, event);
408e65d2
FC
417 expContext.increaseRank();
418 expContext.setLastTrace(trace);
07ef7847
AM
419 final ITmfContext traceContext = expContext.getContext(trace);
420 if (traceContext == null) {
421 throw new IllegalStateException();
422 }
17324c9a 423
ea271da6 424 // Update the experiment location
38db0431
MK
425 ITmfLocation location = expContext.getLocation();
426 if (location instanceof TmfExperimentLocation) {
427 TmfLocationArray locationArray = new TmfLocationArray(
428 ((TmfExperimentLocation) location).getLocationInfo(),
429 trace, traceContext.getLocation(), traceContext.getRank());
430 expContext.setLocation(new TmfExperimentLocation(locationArray));
431 }
408e65d2 432 }
07671572 433 }
a87cc4ef 434
a87cc4ef 435 return event;
a79913eb
FC
436 }
437
66262ad8
BH
438 @Override
439 public ITmfTimestamp getInitialRangeOffset() {
d77f31da 440
fa62dc1d
BH
441 List<ITmfTrace> children = getChildren(ITmfTrace.class);
442
443 if (children.isEmpty()) {
66262ad8
BH
444 return super.getInitialRangeOffset();
445 }
446
447 ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
fa62dc1d
BH
448 for (ITmfTrace trace : children) {
449 ITmfTimestamp ts = (trace).getInitialRangeOffset();
66262ad8
BH
450 if (ts.compareTo(initTs) < 0) {
451 initTs = ts;
452 }
453 }
454 return initTs;
455 }
456
04ba3554
GB
457 /**
458 * Get the path to the folder in the supplementary file where
459 * synchronization-related data can be kept so they are not deleted when the
7f38b742
GB
460 * experiment is synchronized. Analysis involved in synchronization can put
461 * their supplementary files in there so they are preserved after
462 * synchronization.
04ba3554 463 *
7f38b742
GB
464 * If the directory does not exist, it will be created. A return value of
465 * <code>null</code> means either the trace resource does not exist or
466 * supplementary resources cannot be kept.
467 *
468 * @param absolute
469 * If <code>true</code>, it returns the absolute path in the file
470 * system, including the supplementary file path. Otherwise, it
471 * returns only the directory name.
04ba3554
GB
472 * @return The path to the folder where synchronization-related
473 * supplementary files can be kept or <code>null</code> if not
474 * available.
04ba3554 475 */
7f38b742 476 public String getSynchronizationFolder(boolean absolute) {
04ba3554
GB
477 /* Set up the path to the synchronization file we'll use */
478 IResource resource = this.getResource();
479 String syncDirectory = null;
480
481 try {
482 /* get the directory where the file will be stored. */
483 if (resource != null) {
7f38b742 484 String fullDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
04ba3554 485 /* Create the synchronization data directory if not present */
7f38b742
GB
486 if (fullDirectory != null) {
487 fullDirectory = fullDirectory + File.separator + SYNCHRONIZATION_DIRECTORY;
488 File syncDir = new File(fullDirectory);
04ba3554
GB
489 syncDir.mkdirs();
490 }
7f38b742
GB
491 if (absolute) {
492 syncDirectory = fullDirectory;
493 } else {
494 syncDirectory = SYNCHRONIZATION_DIRECTORY;
495 }
04ba3554
GB
496 }
497 } catch (CoreException e) {
498 return null;
499 }
500
501 return syncDirectory;
502 }
503
e73a4ba5
GB
504 /**
505 * Synchronizes the traces of an experiment. By default it only tries to
506 * read a synchronization file if it exists
507 *
508 * @return The synchronization object
e73a4ba5 509 */
4d2a4a2c 510 public SynchronizationAlgorithm synchronizeTraces() {
e73a4ba5
GB
511 return synchronizeTraces(false);
512 }
513
514 /**
515 * Synchronizes the traces of an experiment.
516 *
517 * @param doSync
518 * Whether to actually synchronize or just try opening a sync
519 * file
520 * @return The synchronization object
e73a4ba5 521 */
4d2a4a2c
GB
522 public SynchronizationAlgorithm synchronizeTraces(boolean doSync) {
523 fSyncLock.lock();
e73a4ba5 524
4d2a4a2c
GB
525 try {
526 String syncDirectory = getSynchronizationFolder(true);
e73a4ba5 527
4d2a4a2c 528 final File syncFile = (syncDirectory != null) ? new File(syncDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null;
e73a4ba5 529
aa353506 530 final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, Collections.singleton(this), doSync);
e73a4ba5 531
4d2a4a2c 532 final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo);
e73a4ba5 533
4d2a4a2c
GB
534 /* Broadcast in separate thread to prevent deadlock */
535 new Thread() {
536 @Override
537 public void run() {
538 broadcast(signal);
539 }
540 }.start();
e73a4ba5 541
4d2a4a2c
GB
542 return syncAlgo;
543 } finally {
544 fSyncLock.unlock();
545 }
e73a4ba5
GB
546 }
547
a79913eb 548 @Override
3b38ea61 549 @SuppressWarnings("nls")
5419a136 550 public synchronized String toString() {
a79913eb
FC
551 return "[TmfExperiment (" + getName() + ")]";
552 }
8c8bf09f
ASL
553
554 // ------------------------------------------------------------------------
9e0640dc 555 // Streaming support
8c8bf09f
ASL
556 // ------------------------------------------------------------------------
557
1b70b6dc 558 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
559
560 if (fInitialized) {
828e5592 561 return;
9e0640dc 562 }
828e5592
PT
563 fInitialized = true;
564
1b70b6dc 565 if (getStreamingInterval() == 0) {
0316808c 566 final ITmfContext context = seekEvent(0);
cbdacf03 567 final ITmfEvent event = getNext(context);
4c9f2944 568 context.dispose();
9b749023 569 if (event == null) {
1b70b6dc 570 return;
9b749023 571 }
4593bd5b 572 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 573 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
574
575 // Broadcast in separate thread to prevent deadlock
576 new Thread() {
577 @Override
578 public void run() {
579 broadcast(signal);
580 }
581 }.start();
1b70b6dc
PT
582 return;
583 }
584
9e0640dc 585 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 586 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 587 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 588 private TmfTimeRange timeRange = null;
1b70b6dc
PT
589
590 @Override
591 public void run() {
fc7cd0be 592 while (!executorIsShutdown()) {
9e0640dc 593 if (!getIndexer().isIndexing()) {
a4115405
FC
594 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
595 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
fa62dc1d
BH
596
597 for (final ITmfTrace trace : getChildren(ITmfTrace.class)) {
9b749023 598 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 599 startTimestamp = trace.getStartTime();
9b749023
AM
600 }
601 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 602 endTimestamp = trace.getEndTime();
9b749023 603 }
1b70b6dc 604 }
6cfc180e
GB
605 ITmfTimestamp safeTs = safeTimestamp;
606 if (safeTs != null && (lastSafeTimestamp == null || safeTs.compareTo(lastSafeTimestamp) > 0)) {
607 timeRange = new TmfTimeRange(startTimestamp, safeTs);
608 lastSafeTimestamp = safeTs;
9b749023 609 } else {
1b70b6dc 610 timeRange = null;
9b749023 611 }
1b70b6dc
PT
612 safeTimestamp = endTimestamp;
613 if (timeRange != null) {
38db0431 614 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1b70b6dc
PT
615 broadcast(signal);
616 }
617 }
618 try {
619 Thread.sleep(getStreamingInterval());
cbdacf03 620 } catch (final InterruptedException e) {
1b70b6dc
PT
621 e.printStackTrace();
622 }
623 }
624 }
625 };
626 thread.start();
627 }
628
1b70b6dc
PT
629 @Override
630 public long getStreamingInterval() {
631 long interval = 0;
fa62dc1d 632 for (final ITmfTrace trace : getChildren(ITmfTrace.class)) {
1b70b6dc 633 interval = Math.max(interval, trace.getStreamingInterval());
9b749023 634 }
1b70b6dc
PT
635 return interval;
636 }
637
8c8bf09f
ASL
638 // ------------------------------------------------------------------------
639 // Signal handlers
640 // ------------------------------------------------------------------------
641
faa38350 642 @Override
9e0640dc 643 @TmfSignalHandler
faa38350 644 public void traceOpened(TmfTraceOpenedSignal signal) {
9e0640dc 645 if (signal.getTrace() == this) {
faa38350 646 initializeStreamingMonitor();
9928ddeb
GB
647
648 /* Initialize the analysis */
649 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
650 status.add(executeAnalysis());
651 if (!status.isOK()) {
652 Activator.log(status);
653 }
b5e8ee95 654 TmfTraceManager.refreshSupplementaryFiles(this);
9e0640dc 655 }
a1091415
PT
656 }
657
032ecd45
MAL
658 @Override
659 public synchronized int getCheckpointSize() {
660 int totalCheckpointSize = 0;
661 try {
fa62dc1d
BH
662 List<ITmfTrace> children = getChildren(ITmfTrace.class);
663 for (ITmfTrace trace : children) {
664 if (!(trace instanceof ITmfPersistentlyIndexable)) {
665 return 0;
666 }
032ecd45 667
fa62dc1d
BH
668 ITmfPersistentlyIndexable persistableIndexTrace = (ITmfPersistentlyIndexable) trace;
669 int currentTraceCheckpointSize = persistableIndexTrace.getCheckpointSize();
670 if (currentTraceCheckpointSize <= 0) {
671 return 0;
032ecd45 672 }
fa62dc1d
BH
673 totalCheckpointSize += currentTraceCheckpointSize;
674 // each entry in the TmfLocationArray has a rank in addition
675 // of the location
676 totalCheckpointSize += 8;
032ecd45
MAL
677 }
678 } catch (UnsupportedOperationException e) {
679 return 0;
680 }
681
682 return totalCheckpointSize;
683 }
684
685 @Override
686 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
fa62dc1d
BH
687 List<ITmfTrace> children = getChildren(ITmfTrace.class);
688 int length = children.size();
689 ITmfLocation[] locations = new ITmfLocation[length];
690 long[] ranks = new long[length];
691 for (int i = 0; i < length; ++i) {
692 final ITmfTrace trace = children.get(i);
032ecd45
MAL
693 locations[i] = ((ITmfPersistentlyIndexable) trace).restoreLocation(bufferIn);
694 ranks[i] = bufferIn.getLong();
695 }
696 TmfLocationArray arr = new TmfLocationArray(locations, ranks);
697 TmfExperimentLocation l = new TmfExperimentLocation(arr);
698 return l;
699 }
4dc47e28 700}
This page took 0.166637 seconds and 5 git commands to generate.