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