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