Fix some null warnings
[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
PT
301 // Initialize the location array if necessary
302 TmfLocationArray locationArray = ((location == null) ?
fa62dc1d 303 new TmfLocationArray(length) :
ea271da6
PT
304 ((TmfExperimentLocation) location).getLocationInfo());
305
306 ITmfLocation[] locations = locationArray.getLocations();
307 long[] ranks = locationArray.getRanks();
308
a79913eb 309 // Create and populate the context's traces contexts
fa62dc1d 310 final TmfExperimentContext context = new TmfExperimentContext(length);
9b635e61 311
d62bb185 312 // Position the traces
ea271da6 313 long rank = 0;
fa62dc1d 314 for (int i = 0; i < length; i++) {
a79913eb 315 // Get the relevant trace attributes
fa62dc1d 316 final ITmfContext traceContext = ((ITmfTrace) getChild(i)).seekEvent(locations[i]);
07ef7847 317 context.setContext(i, traceContext);
ea271da6 318 traceContext.setRank(ranks[i]);
7f38b742
GB
319 // update location after seek
320 locations[i] = traceContext.getLocation();
fa62dc1d 321 context.setEvent(i, ((ITmfTrace) getChild(i)).getNext(traceContext));
ea271da6 322 rank += ranks[i];
a79913eb 323 }
8f50c396 324
a79913eb 325 // Finalize context
ea271da6 326 context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
a79913eb 327 context.setLastTrace(TmfExperimentContext.NO_TRACE);
ea271da6 328 context.setRank(rank);
49e2f79a 329
9b749023 330 return context;
a79913eb 331 }
9f584e4c 332
3bd44ac8
FC
333 // ------------------------------------------------------------------------
334 // ITmfTrace - SeekEvent operations (returning a trace context)
335 // ------------------------------------------------------------------------
336
c76c54bb 337 @Override
0316808c 338 public ITmfContext seekEvent(final double ratio) {
91f6e587 339 final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
c76c54bb
FC
340 return context;
341 }
342
a79913eb 343 @Override
1e1bef82 344 public double getLocationRatio(final ITmfLocation location) {
9e0640dc 345 if (location instanceof TmfExperimentLocation) {
ea271da6
PT
346 long rank = 0;
347 TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
348 for (int i = 0; i < locationArray.size(); i++) {
349 rank += locationArray.getRank(i);
350 }
351 return (double) rank / getNbEvents();
9e0640dc
FC
352 }
353 return 0.0;
c76c54bb
FC
354 }
355
a79913eb 356 @Override
1e1bef82 357 public ITmfLocation getCurrentLocation() {
ea271da6
PT
358 // never used
359 return null;
a79913eb 360 }
c76c54bb 361
9e0640dc
FC
362 // ------------------------------------------------------------------------
363 // ITmfTrace trace positioning
364 // ------------------------------------------------------------------------
365
07671572 366 @Override
6256d8ad 367 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
ea271da6
PT
368 final ITmfContext tmpContext = seekEvent(context.getLocation());
369 final ITmfEvent event = getNext(tmpContext);
07671572
FC
370 return event;
371 }
a79913eb 372
0316808c 373 @Override
6256d8ad 374 public synchronized ITmfEvent getNext(ITmfContext context) {
a79913eb
FC
375
376 // Validate the context
9e0640dc 377 if (!(context instanceof TmfExperimentContext)) {
a79913eb 378 return null; // Throw an exception?
9e0640dc 379 }
0e8c76f8 380
fa62dc1d
BH
381 int length = getNbChildren();
382
0e8c76f8 383 // Make sure that we have something to read from
fa62dc1d 384 if (length == 0) {
0e8c76f8
BH
385 return null;
386 }
387
a87cc4ef 388 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 389
e73a4ba5
GB
390 // If an event was consumed previously, first get the next one from that
391 // trace
cbdacf03 392 final int lastTrace = expContext.getLastTrace();
a79913eb 393 if (lastTrace != TmfExperimentContext.NO_TRACE) {
07ef7847 394 final ITmfContext traceContext = expContext.getContext(lastTrace);
fa62dc1d 395 expContext.setEvent(lastTrace, ((ITmfTrace) getChild(lastTrace)).getNext(traceContext));
a79913eb 396 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
397 }
398
399 // Scan the candidate events and identify the "next" trace to read from
400 int trace = TmfExperimentContext.NO_TRACE;
a4115405 401 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
fa62dc1d 402 for (int i = 0; i < length; i++) {
07ef7847 403 final ITmfEvent event = expContext.getEvent(i);
fa62dc1d 404
cbf0057c 405 if (event != null) {
cbdacf03 406 final ITmfTimestamp otherTS = event.getTimestamp();
065cc19b 407 if (otherTS.compareTo(timestamp) < 0) {
a79913eb
FC
408 trace = i;
409 timestamp = otherTS;
410 }
411 }
412 }
a87cc4ef 413
6256d8ad 414 ITmfEvent event = null;
07671572 415 if (trace != TmfExperimentContext.NO_TRACE) {
07ef7847 416 event = expContext.getEvent(trace);
408e65d2 417 if (event != null) {
5904c11e 418 updateAttributes(expContext, event);
408e65d2
FC
419 expContext.increaseRank();
420 expContext.setLastTrace(trace);
07ef7847
AM
421 final ITmfContext traceContext = expContext.getContext(trace);
422 if (traceContext == null) {
423 throw new IllegalStateException();
424 }
17324c9a 425
ea271da6
PT
426 // Update the experiment location
427 TmfLocationArray locationArray = new TmfLocationArray(
428 ((TmfExperimentLocation) expContext.getLocation()).getLocationInfo(),
429 trace, traceContext.getLocation(), traceContext.getRank());
430 expContext.setLocation(new TmfExperimentLocation(locationArray));
408e65d2 431 }
07671572 432 }
a87cc4ef 433
a87cc4ef 434 return event;
a79913eb
FC
435 }
436
66262ad8
BH
437 @Override
438 public ITmfTimestamp getInitialRangeOffset() {
d77f31da 439
fa62dc1d
BH
440 List<ITmfTrace> children = getChildren(ITmfTrace.class);
441
442 if (children.isEmpty()) {
66262ad8
BH
443 return super.getInitialRangeOffset();
444 }
445
446 ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
fa62dc1d
BH
447 for (ITmfTrace trace : children) {
448 ITmfTimestamp ts = (trace).getInitialRangeOffset();
66262ad8
BH
449 if (ts.compareTo(initTs) < 0) {
450 initTs = ts;
451 }
452 }
453 return initTs;
454 }
455
04ba3554
GB
456 /**
457 * Get the path to the folder in the supplementary file where
458 * synchronization-related data can be kept so they are not deleted when the
7f38b742
GB
459 * experiment is synchronized. Analysis involved in synchronization can put
460 * their supplementary files in there so they are preserved after
461 * synchronization.
04ba3554 462 *
7f38b742
GB
463 * If the directory does not exist, it will be created. A return value of
464 * <code>null</code> means either the trace resource does not exist or
465 * supplementary resources cannot be kept.
466 *
467 * @param absolute
468 * If <code>true</code>, it returns the absolute path in the file
469 * system, including the supplementary file path. Otherwise, it
470 * returns only the directory name.
04ba3554
GB
471 * @return The path to the folder where synchronization-related
472 * supplementary files can be kept or <code>null</code> if not
473 * available.
04ba3554 474 */
7f38b742 475 public String getSynchronizationFolder(boolean absolute) {
04ba3554
GB
476 /* Set up the path to the synchronization file we'll use */
477 IResource resource = this.getResource();
478 String syncDirectory = null;
479
480 try {
481 /* get the directory where the file will be stored. */
482 if (resource != null) {
7f38b742 483 String fullDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
04ba3554 484 /* Create the synchronization data directory if not present */
7f38b742
GB
485 if (fullDirectory != null) {
486 fullDirectory = fullDirectory + File.separator + SYNCHRONIZATION_DIRECTORY;
487 File syncDir = new File(fullDirectory);
04ba3554
GB
488 syncDir.mkdirs();
489 }
7f38b742
GB
490 if (absolute) {
491 syncDirectory = fullDirectory;
492 } else {
493 syncDirectory = SYNCHRONIZATION_DIRECTORY;
494 }
04ba3554
GB
495 }
496 } catch (CoreException e) {
497 return null;
498 }
499
500 return syncDirectory;
501 }
502
e73a4ba5
GB
503 /**
504 * Synchronizes the traces of an experiment. By default it only tries to
505 * read a synchronization file if it exists
506 *
507 * @return The synchronization object
e73a4ba5 508 */
4d2a4a2c 509 public SynchronizationAlgorithm synchronizeTraces() {
e73a4ba5
GB
510 return synchronizeTraces(false);
511 }
512
513 /**
514 * Synchronizes the traces of an experiment.
515 *
516 * @param doSync
517 * Whether to actually synchronize or just try opening a sync
518 * file
519 * @return The synchronization object
e73a4ba5 520 */
4d2a4a2c
GB
521 public SynchronizationAlgorithm synchronizeTraces(boolean doSync) {
522 fSyncLock.lock();
e73a4ba5 523
4d2a4a2c
GB
524 try {
525 String syncDirectory = getSynchronizationFolder(true);
e73a4ba5 526
4d2a4a2c 527 final File syncFile = (syncDirectory != null) ? new File(syncDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null;
e73a4ba5 528
aa353506 529 final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, Collections.singleton(this), doSync);
e73a4ba5 530
4d2a4a2c 531 final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo);
e73a4ba5 532
4d2a4a2c
GB
533 /* Broadcast in separate thread to prevent deadlock */
534 new Thread() {
535 @Override
536 public void run() {
537 broadcast(signal);
538 }
539 }.start();
e73a4ba5 540
4d2a4a2c
GB
541 return syncAlgo;
542 } finally {
543 fSyncLock.unlock();
544 }
e73a4ba5
GB
545 }
546
a79913eb 547 @Override
3b38ea61 548 @SuppressWarnings("nls")
5419a136 549 public synchronized String toString() {
a79913eb
FC
550 return "[TmfExperiment (" + getName() + ")]";
551 }
8c8bf09f
ASL
552
553 // ------------------------------------------------------------------------
9e0640dc 554 // Streaming support
8c8bf09f
ASL
555 // ------------------------------------------------------------------------
556
1b70b6dc 557 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
558
559 if (fInitialized) {
828e5592 560 return;
9e0640dc 561 }
828e5592
PT
562 fInitialized = true;
563
1b70b6dc 564 if (getStreamingInterval() == 0) {
0316808c 565 final ITmfContext context = seekEvent(0);
cbdacf03 566 final ITmfEvent event = getNext(context);
4c9f2944 567 context.dispose();
9b749023 568 if (event == null) {
1b70b6dc 569 return;
9b749023 570 }
4593bd5b 571 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 572 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
573
574 // Broadcast in separate thread to prevent deadlock
575 new Thread() {
576 @Override
577 public void run() {
578 broadcast(signal);
579 }
580 }.start();
1b70b6dc
PT
581 return;
582 }
583
9e0640dc 584 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 585 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 586 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 587 private TmfTimeRange timeRange = null;
1b70b6dc
PT
588
589 @Override
590 public void run() {
fc7cd0be 591 while (!executorIsShutdown()) {
9e0640dc 592 if (!getIndexer().isIndexing()) {
a4115405
FC
593 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
594 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
fa62dc1d
BH
595
596 for (final ITmfTrace trace : getChildren(ITmfTrace.class)) {
9b749023 597 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 598 startTimestamp = trace.getStartTime();
9b749023
AM
599 }
600 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 601 endTimestamp = trace.getEndTime();
9b749023 602 }
1b70b6dc 603 }
6cfc180e
GB
604 ITmfTimestamp safeTs = safeTimestamp;
605 if (safeTs != null && (lastSafeTimestamp == null || safeTs.compareTo(lastSafeTimestamp) > 0)) {
606 timeRange = new TmfTimeRange(startTimestamp, safeTs);
607 lastSafeTimestamp = safeTs;
9b749023 608 } else {
1b70b6dc 609 timeRange = null;
9b749023 610 }
1b70b6dc
PT
611 safeTimestamp = endTimestamp;
612 if (timeRange != null) {
faa38350
PT
613 final TmfTraceRangeUpdatedSignal signal =
614 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.16578 seconds and 5 git commands to generate.