tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfExperiment.java
CommitLineData
8c8bf09f 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 Ericsson
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
8c8bf09f
ASL
14 *******************************************************************************/
15
9e0640dc 16package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 17
a1091415 18import org.eclipse.core.resources.IFile;
12c155f5 19import org.eclipse.core.resources.IProject;
828e5592 20import org.eclipse.core.resources.IResource;
a94410d9
MK
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
9e0640dc
FC
23import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
24import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
25import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
72f1e62a 26import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
0316808c 27import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
5419a136
AM
28import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
29import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
ea279a69 30import org.eclipse.linuxtools.tmf.core.signal.TmfClearExperimentSignal;
6c13869b 31import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
faa38350
PT
32import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
33import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
3bd46eef
AM
34import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
35import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
36import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
8c8bf09f
ASL
37
38/**
9e0640dc 39 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
cbdacf03 40 * that are part of a tracing experiment.
4b7b3670
FC
41 *
42 * @version 1.0
43 * @author Francois Chouinard
8c8bf09f 44 */
6256d8ad 45public class TmfExperiment extends TmfTrace implements ITmfEventParser {
8c8bf09f 46
c32744d6
FC
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50
9e0640dc
FC
51 /**
52 * The default index page size
53 */
54 public static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
c32744d6 55
8c8bf09f
ASL
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59
9e0640dc
FC
60 /**
61 * The set of traces that constitute the experiment
62 */
6256d8ad 63 protected ITmfTrace[] fTraces;
8c8bf09f 64
9e0640dc
FC
65 /**
66 * The set of traces that constitute the experiment
67 */
68 private boolean fInitialized = false;
a1091415 69
9e0640dc
FC
70 /**
71 * The experiment bookmarks file
72 */
73 private IFile fBookmarksFile;
828e5592 74
8c8bf09f 75 // ------------------------------------------------------------------------
9e0640dc 76 // Construction
8c8bf09f
ASL
77 // ------------------------------------------------------------------------
78
9e0640dc 79 /**
0283f7ff
FC
80 * @param type the event type
81 * @param id the experiment id
82 * @param traces the experiment set of traces
9e0640dc 83 */
6256d8ad 84 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
99504bb8 85 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, null);
96c6806f
PT
86 }
87
99504bb8
GB
88 /**
89 * Constructor of experiment taking type, path, traces and resource
90 *
91 * @param type
92 * the event type
93 * @param id
94 * the experiment id
95 * @param traces
96 * the experiment set of traces
97 * @param resource
98 * the resource associated to the experiment
99 */
100 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces, IResource resource) {
101 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, resource);
102 }
103
104
8c8bf09f 105 /**
0283f7ff
FC
106 * @param type the event type
107 * @param path the experiment path
108 * @param traces the experiment set of traces
109 * @param indexPageSize the experiment index page size
8c8bf09f 110 */
6256d8ad 111 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
99504bb8
GB
112 this(type, path, traces, indexPageSize, null);
113 }
114
115 /**
116 * Full constructor of an experiment, taking the type, path, traces,
117 * indexPageSize and resource
118 *
119 * @param type
120 * the event type
121 * @param path
122 * the experiment path
123 * @param traces
124 * the experiment set of traces
125 * @param indexPageSize
126 * the experiment index page size
127 * @param resource
128 * the resource associated to the experiment
129 */
130 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
0316808c
FC
131 setCacheSize(indexPageSize);
132 setStreamingInterval(0);
07671572 133 setIndexer(new TmfCheckpointIndexer(this, indexPageSize));
0316808c
FC
134 setParser(this);
135 try {
99504bb8 136 super.initialize(resource, path, type);
0316808c
FC
137 } catch (TmfTraceException e) {
138 e.printStackTrace();
139 }
8c8bf09f 140
a79913eb 141 fTraces = traces;
8c8bf09f 142 }
a79913eb 143
8c8bf09f 144 /**
ff4ed569 145 * Clears the experiment
8c8bf09f
ASL
146 */
147 @Override
a79913eb
FC
148 public synchronized void dispose() {
149
77551cc2
FC
150 // Clean up the index if applicable
151 if (getIndexer() != null) {
152 getIndexer().dispose();
153 }
b5ee6881 154
a79913eb 155 if (fTraces != null) {
9b749023 156 for (final ITmfTrace trace : fTraces) {
a79913eb 157 trace.dispose();
9b749023 158 }
a79913eb
FC
159 fTraces = null;
160 }
2fb2eb37 161 super.dispose();
8c8bf09f
ASL
162 }
163
ea279a69
FC
164 /**
165 * @param signal the clear view signal
166 * @since 2.0
167 */
168 @TmfSignalHandler
169 public void handleClearExperimentSignal(TmfClearExperimentSignal signal) {
170 dispose();
171 }
172
9e0640dc
FC
173 // ------------------------------------------------------------------------
174 // ITmfTrace - Initializers
175 // ------------------------------------------------------------------------
176
9e0640dc 177 @Override
6256d8ad 178 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
9e0640dc
FC
179 }
180
a94410d9
MK
181 /**
182 * @since 2.0
183 */
9e0640dc 184 @Override
a94410d9
MK
185 public IStatus validate(final IProject project, final String path) {
186 return Status.OK_STATUS;
9e0640dc
FC
187 }
188
8c8bf09f 189 // ------------------------------------------------------------------------
e31e01e8 190 // Accessors
8c8bf09f
ASL
191 // ------------------------------------------------------------------------
192
fe0c44c4 193 @Override
6256d8ad 194 public ITmfTrace[] getTraces() {
a79913eb 195 return fTraces;
8c8bf09f
ASL
196 }
197
8c8bf09f 198 /**
cbdacf03
FC
199 * Returns the timestamp of the event at the requested index. If none,
200 * returns null.
9b749023 201 *
0d9a6d76
FC
202 * @param index the event index (rank)
203 * @return the corresponding event timestamp
3bd46eef 204 * @since 2.0
8c8bf09f 205 */
cbdacf03 206 public ITmfTimestamp getTimestamp(final int index) {
0316808c 207 final ITmfContext context = seekEvent(index);
c32744d6 208 final ITmfEvent event = getNext(context);
4c9f2944 209 context.dispose();
a79913eb 210 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
211 }
212
9e0640dc
FC
213 /**
214 * Set the file to be used for bookmarks on this experiment
9b749023 215 *
9e0640dc
FC
216 * @param file the bookmarks file
217 */
218 public void setBookmarksFile(final IFile file) {
219 fBookmarksFile = file;
220 }
07671572 221
9e0640dc
FC
222 /**
223 * Get the file used for bookmarks on this experiment
9b749023 224 *
9e0640dc
FC
225 * @return the bookmarks file or null if none is set
226 */
227 public IFile getBookmarksFile() {
228 return fBookmarksFile;
a79913eb
FC
229 }
230
49e2f79a
FC
231 // ------------------------------------------------------------------------
232 // Request management
233 // ------------------------------------------------------------------------
234
e6809677
PT
235 /**
236 * @since 2.0
3bd44ac8 237 */
49e2f79a 238 @Override
5419a136 239 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
9b749023 240
6a953367
BH
241 // Make sure we have something to read from
242 if (fTraces == null) {
243 return null;
244 }
9b749023 245
5419a136
AM
246 if (request instanceof ITmfEventRequest
247 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
248 && request.getIndex() == 0)
249 {
250 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
251 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
49e2f79a 252 return context;
5419a136 253
49e2f79a
FC
254 }
255
5419a136 256 return seekEvent(request.getIndex());
49e2f79a
FC
257 }
258
a79913eb 259 // ------------------------------------------------------------------------
9f584e4c
FC
260 // ITmfTrace trace positioning
261 // ------------------------------------------------------------------------
262
a79913eb 263 @Override
1e1bef82 264 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
a79913eb 265 // Validate the location
9e0640dc 266 if (location != null && !(location instanceof TmfExperimentLocation)) {
a79913eb 267 return null; // Throw an exception?
9e0640dc
FC
268 }
269 // Make sure we have something to read from
270 if (fTraces == null) {
a79913eb 271 return null;
9e0640dc 272 }
8f50c396 273
ea271da6
PT
274 // Initialize the location array if necessary
275 TmfLocationArray locationArray = ((location == null) ?
276 new TmfLocationArray(fTraces.length) :
277 ((TmfExperimentLocation) location).getLocationInfo());
278
279 ITmfLocation[] locations = locationArray.getLocations();
280 long[] ranks = locationArray.getRanks();
281
a79913eb 282 // Create and populate the context's traces contexts
ea271da6 283 final TmfExperimentContext context = new TmfExperimentContext(fTraces.length);
9b635e61 284
d62bb185 285 // Position the traces
ea271da6 286 long rank = 0;
a79913eb
FC
287 for (int i = 0; i < fTraces.length; i++) {
288 // Get the relevant trace attributes
ea271da6
PT
289 final ITmfContext traceContext = fTraces[i].seekEvent(locations[i]);
290 context.getContexts()[i] = traceContext;
291 traceContext.setRank(ranks[i]);
292 locations[i] = traceContext.getLocation(); // update location after seek
293 context.getEvents()[i] = fTraces[i].getNext(traceContext);
294 rank += ranks[i];
a79913eb 295 }
8f50c396 296
a79913eb 297 // Finalize context
ea271da6 298 context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
a79913eb 299 context.setLastTrace(TmfExperimentContext.NO_TRACE);
ea271da6 300 context.setRank(rank);
49e2f79a 301
9b749023 302 return context;
a79913eb 303 }
9f584e4c 304
3bd44ac8
FC
305 // ------------------------------------------------------------------------
306 // ITmfTrace - SeekEvent operations (returning a trace context)
307 // ------------------------------------------------------------------------
308
c76c54bb 309 @Override
0316808c 310 public ITmfContext seekEvent(final double ratio) {
91f6e587 311 final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
c76c54bb
FC
312 return context;
313 }
314
a79913eb 315 @Override
1e1bef82 316 public double getLocationRatio(final ITmfLocation location) {
9e0640dc 317 if (location instanceof TmfExperimentLocation) {
ea271da6
PT
318 long rank = 0;
319 TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
320 for (int i = 0; i < locationArray.size(); i++) {
321 rank += locationArray.getRank(i);
322 }
323 return (double) rank / getNbEvents();
9e0640dc
FC
324 }
325 return 0.0;
c76c54bb
FC
326 }
327
a79913eb 328 @Override
1e1bef82 329 public ITmfLocation getCurrentLocation() {
ea271da6
PT
330 // never used
331 return null;
a79913eb 332 }
c76c54bb 333
9e0640dc
FC
334 // ------------------------------------------------------------------------
335 // ITmfTrace trace positioning
336 // ------------------------------------------------------------------------
337
07671572 338 @Override
6256d8ad 339 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
ea271da6
PT
340 final ITmfContext tmpContext = seekEvent(context.getLocation());
341 final ITmfEvent event = getNext(tmpContext);
07671572
FC
342 return event;
343 }
a79913eb 344
0316808c 345 @Override
6256d8ad 346 public synchronized ITmfEvent getNext(ITmfContext context) {
a79913eb
FC
347
348 // Validate the context
9e0640dc 349 if (!(context instanceof TmfExperimentContext)) {
a79913eb 350 return null; // Throw an exception?
9e0640dc 351 }
0e8c76f8
BH
352
353 // Make sure that we have something to read from
354 if (fTraces == null) {
355 return null;
356 }
357
a87cc4ef 358 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 359
a87cc4ef 360 // If an event was consumed previously, first get the next one from that trace
cbdacf03 361 final int lastTrace = expContext.getLastTrace();
a79913eb 362 if (lastTrace != TmfExperimentContext.NO_TRACE) {
cbdacf03 363 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
c32744d6 364 expContext.getEvents()[lastTrace] = fTraces[lastTrace].getNext(traceContext);
a79913eb 365 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
366 }
367
368 // Scan the candidate events and identify the "next" trace to read from
369 int trace = TmfExperimentContext.NO_TRACE;
a4115405 370 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
0316808c 371 for (int i = 0; i < fTraces.length; i++) {
cbdacf03 372 final ITmfEvent event = expContext.getEvents()[i];
a79913eb 373 if (event != null && event.getTimestamp() != null) {
cbdacf03 374 final ITmfTimestamp otherTS = event.getTimestamp();
a79913eb
FC
375 if (otherTS.compareTo(timestamp, true) < 0) {
376 trace = i;
377 timestamp = otherTS;
378 }
379 }
380 }
a87cc4ef 381
6256d8ad 382 ITmfEvent event = null;
07671572 383 if (trace != TmfExperimentContext.NO_TRACE) {
6256d8ad 384 event = expContext.getEvents()[trace];
408e65d2
FC
385 if (event != null) {
386 updateAttributes(expContext, event.getTimestamp());
408e65d2
FC
387 expContext.increaseRank();
388 expContext.setLastTrace(trace);
17324c9a
FC
389 final ITmfContext traceContext = expContext.getContexts()[trace];
390
ea271da6
PT
391 // Update the experiment location
392 TmfLocationArray locationArray = new TmfLocationArray(
393 ((TmfExperimentLocation) expContext.getLocation()).getLocationInfo(),
394 trace, traceContext.getLocation(), traceContext.getRank());
395 expContext.setLocation(new TmfExperimentLocation(locationArray));
17324c9a 396
408e65d2
FC
397 processEvent(event);
398 }
07671572 399 }
a87cc4ef 400
a87cc4ef 401 return event;
a79913eb
FC
402 }
403
66262ad8
BH
404 /**
405 * @since 2.0
406 */
407 @Override
408 public ITmfTimestamp getInitialRangeOffset() {
409 if ((fTraces == null) || (fTraces.length == 0)) {
410 return super.getInitialRangeOffset();
411 }
412
413 ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
414 for (int i = 0; i < fTraces.length; i++) {
415 ITmfTimestamp ts = fTraces[i].getInitialRangeOffset();
416 if (ts.compareTo(initTs) < 0) {
417 initTs = ts;
418 }
419 }
420 return initTs;
421 }
422
a79913eb 423 @Override
3b38ea61 424 @SuppressWarnings("nls")
5419a136 425 public synchronized String toString() {
a79913eb
FC
426 return "[TmfExperiment (" + getName() + ")]";
427 }
8c8bf09f
ASL
428
429 // ------------------------------------------------------------------------
9e0640dc 430 // Streaming support
8c8bf09f
ASL
431 // ------------------------------------------------------------------------
432
1b70b6dc 433 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
434
435 if (fInitialized) {
828e5592 436 return;
9e0640dc 437 }
828e5592
PT
438 fInitialized = true;
439
1b70b6dc 440 if (getStreamingInterval() == 0) {
0316808c 441 final ITmfContext context = seekEvent(0);
cbdacf03 442 final ITmfEvent event = getNext(context);
4c9f2944 443 context.dispose();
9b749023 444 if (event == null) {
1b70b6dc 445 return;
9b749023 446 }
4593bd5b 447 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 448 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
449
450 // Broadcast in separate thread to prevent deadlock
451 new Thread() {
452 @Override
453 public void run() {
454 broadcast(signal);
455 }
456 }.start();
1b70b6dc
PT
457 return;
458 }
459
9e0640dc 460 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 461 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 462 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 463 private TmfTimeRange timeRange = null;
1b70b6dc
PT
464
465 @Override
466 public void run() {
fc7cd0be 467 while (!executorIsShutdown()) {
9e0640dc 468 if (!getIndexer().isIndexing()) {
a4115405
FC
469 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
470 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
6256d8ad 471 for (final ITmfTrace trace : fTraces) {
9b749023 472 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 473 startTimestamp = trace.getStartTime();
9b749023
AM
474 }
475 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 476 endTimestamp = trace.getEndTime();
9b749023 477 }
1b70b6dc 478 }
6be2d5cc 479 if (safeTimestamp != null && (lastSafeTimestamp == null || safeTimestamp.compareTo(lastSafeTimestamp, false) > 0)) {
1b70b6dc 480 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
6be2d5cc 481 lastSafeTimestamp = safeTimestamp;
9b749023 482 } else {
1b70b6dc 483 timeRange = null;
9b749023 484 }
1b70b6dc
PT
485 safeTimestamp = endTimestamp;
486 if (timeRange != null) {
faa38350
PT
487 final TmfTraceRangeUpdatedSignal signal =
488 new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1b70b6dc
PT
489 broadcast(signal);
490 }
491 }
492 try {
493 Thread.sleep(getStreamingInterval());
cbdacf03 494 } catch (final InterruptedException e) {
1b70b6dc
PT
495 e.printStackTrace();
496 }
497 }
498 }
499 };
500 thread.start();
501 }
502
1b70b6dc
PT
503 @Override
504 public long getStreamingInterval() {
505 long interval = 0;
6256d8ad 506 for (final ITmfTrace trace : fTraces) {
1b70b6dc 507 interval = Math.max(interval, trace.getStreamingInterval());
9b749023 508 }
1b70b6dc
PT
509 return interval;
510 }
511
8c8bf09f
ASL
512 // ------------------------------------------------------------------------
513 // Signal handlers
514 // ------------------------------------------------------------------------
515
faa38350 516 @Override
9e0640dc 517 @TmfSignalHandler
faa38350 518 public void traceOpened(TmfTraceOpenedSignal signal) {
9e0640dc 519 if (signal.getTrace() == this) {
faa38350 520 initializeStreamingMonitor();
9e0640dc 521 }
a1091415
PT
522 }
523
4dc47e28 524}
This page took 0.088109 seconds and 5 git commands to generate.