Revert "Fix for bug 381411: Implement ranked location in experiment."
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfExperiment.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.trace;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
20 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
21 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
25 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
26 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
27 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
28 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
29 import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
30 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentDisposedSignal;
31 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
32 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
33 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
34 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
35 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
36
37 /**
38 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
39 * that are part of a tracing experiment.
40 *
41 * @version 1.0
42 * @author Francois Chouinard
43 */
44 public class TmfExperiment<T extends ITmfEvent> extends TmfTrace<T> implements ITmfEventParser<T> {
45
46 // ------------------------------------------------------------------------
47 // Constants
48 // ------------------------------------------------------------------------
49
50 /**
51 * The default index page size
52 */
53 public static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
59 /**
60 * The currently selected experiment (null if none)
61 */
62 protected static TmfExperiment<?> fCurrentExperiment = null;
63
64 /**
65 * The set of traces that constitute the experiment
66 */
67 protected ITmfTrace<T>[] fTraces;
68
69 /**
70 * The set of traces that constitute the experiment
71 */
72 private boolean fInitialized = false;
73
74 /**
75 * The experiment bookmarks file
76 */
77 private IFile fBookmarksFile;
78
79
80 // Saved experiment context (optimization)
81 private TmfExperimentContext fExperimentContext;
82
83 // ------------------------------------------------------------------------
84 // Construction
85 // ------------------------------------------------------------------------
86
87 /**
88 * @param type
89 * @param id
90 * @param traces
91 * @throws TmfTraceException
92 */
93 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces) {
94 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE);
95 }
96
97 /**
98 * @param type
99 * @param id
100 * @param traces
101 * @param indexPageSize
102 * @throws TmfTraceException
103 */
104 @SuppressWarnings({ "unchecked", "rawtypes" })
105 public TmfExperiment(final Class<T> type, final String path, final ITmfTrace<T>[] traces, final int indexPageSize) {
106 setCacheSize(indexPageSize);
107 setStreamingInterval(0);
108 setIndexer(new TmfCheckpointIndexer(this, indexPageSize));
109 setParser(this);
110 try {
111 super.initialize(null, path, type);
112 } catch (TmfTraceException e) {
113 e.printStackTrace();
114 }
115
116 fTraces = traces;
117 setTimeRange(TmfTimeRange.NULL_RANGE);
118 }
119
120 /**
121 * Clears the experiment
122 */
123 @Override
124 @SuppressWarnings("rawtypes")
125 public synchronized void dispose() {
126
127 final TmfExperimentDisposedSignal<T> signal = new TmfExperimentDisposedSignal<T>(this, this);
128 broadcast(signal);
129
130 if (fCurrentExperiment == this) {
131 fCurrentExperiment = null;
132 }
133
134 // Clean up the index if applicable
135 if (getIndexer() != null) {
136 getIndexer().dispose();
137 }
138
139 if (fTraces != null) {
140 for (final ITmfTrace trace : fTraces)
141 trace.dispose();
142 fTraces = null;
143 }
144 super.dispose();
145 }
146
147 // ------------------------------------------------------------------------
148 // ITmfTrace - Initializers
149 // ------------------------------------------------------------------------
150
151 /* (non-Javadoc)
152 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
153 */
154 @Override
155 public boolean validate(final IProject project, final String path) {
156 return true;
157 }
158
159 /* (non-Javadoc)
160 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
161 */
162 @Override
163 public void initTrace(final IResource resource, final String path, final Class<T> type) {
164 }
165
166 // ------------------------------------------------------------------------
167 // Accessors
168 // ------------------------------------------------------------------------
169
170 /**
171 * Selects the current, framework-wide, experiment
172 *
173 * @param experiment das experiment
174 */
175 public static void setCurrentExperiment(final TmfExperiment<?> experiment) {
176 if (fCurrentExperiment != null && fCurrentExperiment != experiment) {
177 fCurrentExperiment.dispose();
178 }
179 fCurrentExperiment = experiment;
180 }
181
182 /**
183 * @return das experiment
184 */
185 public static TmfExperiment<?> getCurrentExperiment() {
186 return fCurrentExperiment;
187 }
188
189 /**
190 * Get the list of traces. Handle with care...
191 *
192 * @return the experiment traces
193 */
194 public ITmfTrace<T>[] getTraces() {
195 return fTraces;
196 }
197
198 /**
199 * Returns the timestamp of the event at the requested index. If none,
200 * returns null.
201 *
202 * @param index the event index (rank)
203 * @return the corresponding event timestamp
204 */
205 public ITmfTimestamp getTimestamp(final int index) {
206 final ITmfContext context = seekEvent(index);
207 final ITmfEvent event = getNext(context);
208 return (event != null) ? event.getTimestamp() : null;
209 }
210
211 /**
212 * Set the file to be used for bookmarks on this experiment
213 *
214 * @param file the bookmarks file
215 */
216 public void setBookmarksFile(final IFile file) {
217 fBookmarksFile = file;
218 }
219
220 /**
221 * Get the file used for bookmarks on this experiment
222 *
223 * @return the bookmarks file or null if none is set
224 */
225 public IFile getBookmarksFile() {
226 return fBookmarksFile;
227 }
228
229 // ------------------------------------------------------------------------
230 // Request management
231 // ------------------------------------------------------------------------
232
233 /* (non-Javadoc)
234 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
235 */
236 @Override
237 protected synchronized ITmfContext armRequest(final ITmfDataRequest<T> request) {
238
239 // Make sure we have something to read from
240 if (fTraces == null) {
241 return null;
242 }
243
244 if (request instanceof ITmfEventRequest<?>
245 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime())
246 && request.getIndex() == 0)
247 {
248 final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
249 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
250 return context;
251
252 }
253
254 // Check if we are already at the right index
255 if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex()) {
256 return fExperimentContext;
257 }
258
259 return seekEvent(request.getIndex());
260 }
261
262 // ------------------------------------------------------------------------
263 // ITmfTrace trace positioning
264 // ------------------------------------------------------------------------
265
266 /* (non-Javadoc)
267 *
268 * Returns a brand new context based on the location provided and
269 * initializes the event queues
270 *
271 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
272 */
273 @Override
274 public synchronized ITmfContext seekEvent(final ITmfLocation<?> location) {
275 // Validate the location
276 if (location != null && !(location instanceof TmfExperimentLocation)) {
277 return null; // Throw an exception?
278 }
279 // Make sure we have something to read from
280 if (fTraces == null) {
281 return null;
282 }
283
284 // Instantiate the location
285 final TmfExperimentLocation expLocation = (location == null)
286 ? new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation<?>[fTraces.length]))
287 : (TmfExperimentLocation) location.clone();
288
289 // Create and populate the context's traces contexts
290 final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
291
292 for (int i = 0; i < fTraces.length; i++) {
293 // Get the relevant trace attributes
294 final ITmfLocation<?> trcLocation = expLocation.getLocation().getLocations()[i];
295 context.getContexts()[i] = fTraces[i].seekEvent(trcLocation);
296 expLocation.getLocation().getLocations()[i] = context.getContexts()[i].getLocation().clone();
297 context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]);
298 }
299
300 // Finalize context
301 context.setLocation(expLocation);
302 context.setLastTrace(TmfExperimentContext.NO_TRACE);
303 context.setRank(ITmfContext.UNKNOWN_RANK);
304
305 fExperimentContext = context;
306 return (ITmfContext) context;
307 }
308
309 // ------------------------------------------------------------------------
310 // ITmfTrace - SeekEvent operations (returning a trace context)
311 // ------------------------------------------------------------------------
312
313 /* (non-Javadoc)
314 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
315 */
316 @Override
317 public ITmfContext seekEvent(final double ratio) {
318 final ITmfContext context = seekEvent((long) (ratio * getNbEvents()));
319 return context;
320 }
321
322 /* (non-Javadoc)
323 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
324 */
325 @Override
326 public double getLocationRatio(final ITmfLocation<?> location) {
327 if (location instanceof TmfExperimentLocation) {
328 return (double) seekEvent(location).getRank() / getNbEvents();
329 }
330 return 0.0;
331 }
332
333 /* (non-Javadoc)
334 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
335 */
336 @Override
337 public ITmfLocation<?> getCurrentLocation() {
338 ITmfLocation<?>[] locations = new ITmfLocation<?>[fTraces.length];
339 for (int i = 0; i < fTraces.length; i++) {
340 locations[i] = fTraces[i].getCurrentLocation();
341 }
342 return new TmfExperimentLocation(new TmfLocationArray(locations));
343 }
344
345 // ------------------------------------------------------------------------
346 // ITmfTrace trace positioning
347 // ------------------------------------------------------------------------
348
349 /* (non-Javadoc)
350 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
351 */
352 @Override
353 public synchronized T parseEvent(final ITmfContext context) {
354 final ITmfContext savedContext = context.clone();
355 final T event = getNext(savedContext);
356 return event;
357 }
358
359 /* (non-Javadoc)
360 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
361 */
362 @Override
363 @SuppressWarnings("unchecked")
364 public synchronized T getNext(ITmfContext context) {
365
366 // Validate the context
367 if (!(context instanceof TmfExperimentContext)) {
368 return null; // Throw an exception?
369 }
370
371 // Make sure that we have something to read from
372 if (fTraces == null) {
373 return null;
374 }
375
376 TmfExperimentContext expContext = (TmfExperimentContext) context;
377
378 // If an event was consumed previously, first get the next one from that trace
379 final int lastTrace = expContext.getLastTrace();
380 if (lastTrace != TmfExperimentContext.NO_TRACE) {
381 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
382 expContext.getEvents()[lastTrace] = fTraces[lastTrace].getNext(traceContext);
383 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
384 }
385
386 // Scan the candidate events and identify the "next" trace to read from
387 int trace = TmfExperimentContext.NO_TRACE;
388 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
389 for (int i = 0; i < fTraces.length; i++) {
390 final ITmfEvent event = expContext.getEvents()[i];
391 if (event != null && event.getTimestamp() != null) {
392 final ITmfTimestamp otherTS = event.getTimestamp();
393 if (otherTS.compareTo(timestamp, true) < 0) {
394 trace = i;
395 timestamp = otherTS;
396 }
397 }
398 }
399
400 T event = null;
401 if (trace != TmfExperimentContext.NO_TRACE) {
402 event = (T) expContext.getEvents()[trace];
403 if (event != null) {
404 updateAttributes(expContext, event.getTimestamp());
405 expContext.increaseRank();
406 expContext.setLastTrace(trace);
407 final TmfExperimentLocation location = (TmfExperimentLocation) expContext.getLocation();
408 final ITmfContext traceContext = expContext.getContexts()[trace];
409 location.getLocation().getLocations()[trace] = traceContext.getLocation().clone();
410 fExperimentContext = expContext;
411 processEvent(event);
412 }
413 }
414
415 return event;
416 }
417
418 /* (non-Javadoc)
419 * @see java.lang.Object#toString()
420 */
421 @Override
422 @SuppressWarnings("nls")
423 public String toString() {
424 return "[TmfExperiment (" + getName() + ")]";
425 }
426
427 // ------------------------------------------------------------------------
428 // Streaming support
429 // ------------------------------------------------------------------------
430
431 private synchronized void initializeStreamingMonitor() {
432
433 if (fInitialized) {
434 return;
435 }
436 fInitialized = true;
437
438 if (getStreamingInterval() == 0) {
439 final ITmfContext context = seekEvent(0);
440 final ITmfEvent event = getNext(context);
441 if (event == null)
442 return;
443 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp().clone(), TmfTimestamp.BIG_CRUNCH);
444 final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
445
446 // Broadcast in separate thread to prevent deadlock
447 new Thread() {
448 @Override
449 public void run() {
450 broadcast(signal);
451 }
452 }.start();
453 return;
454 }
455
456 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
457 private ITmfTimestamp safeTimestamp = null;
458 private TmfTimeRange timeRange = null;
459
460 @Override
461 public void run() {
462 while (!fExecutor.isShutdown()) {
463 if (!getIndexer().isIndexing()) {
464 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
465 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
466 for (final ITmfTrace<T> trace : fTraces) {
467 if (trace.getStartTime().compareTo(startTimestamp) < 0)
468 startTimestamp = trace.getStartTime();
469 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0)
470 endTimestamp = trace.getEndTime();
471 }
472 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0)
473 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
474 else
475 timeRange = null;
476 safeTimestamp = endTimestamp;
477 if (timeRange != null) {
478 final TmfExperimentRangeUpdatedSignal signal =
479 new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
480 broadcast(signal);
481 }
482 }
483 try {
484 Thread.sleep(getStreamingInterval());
485 } catch (final InterruptedException e) {
486 e.printStackTrace();
487 }
488 }
489 }
490 };
491 thread.start();
492 }
493
494 /* (non-Javadoc)
495 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
496 */
497 @Override
498 public long getStreamingInterval() {
499 long interval = 0;
500 for (final ITmfTrace<T> trace : fTraces)
501 interval = Math.max(interval, trace.getStreamingInterval());
502 return interval;
503 }
504
505 // ------------------------------------------------------------------------
506 // Signal handlers
507 // ------------------------------------------------------------------------
508
509 private Integer fEndSynchReference;
510
511 /**
512 * Signal handler for the TmfExperimentSelectedSignal signal
513 *
514 * @param signal
515 */
516 @TmfSignalHandler
517 public void experimentSelected(final TmfExperimentSelectedSignal<T> signal) {
518 final TmfExperiment<?> experiment = signal.getExperiment();
519 if (experiment == this) {
520 setCurrentExperiment(experiment);
521 fEndSynchReference = Integer.valueOf(signal.getReference());
522 }
523 }
524
525 /**
526 * Signal handler for the TmfEndSynchSignal signal
527 *
528 * @param signal
529 */
530 @TmfSignalHandler
531 public void endSync(final TmfEndSynchSignal signal) {
532 if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
533 fEndSynchReference = null;
534 initializeStreamingMonitor();
535 }
536 }
537
538 /**
539 * Signal handler for the TmfTraceUpdatedSignal signal
540 *
541 * @param signal
542 */
543 @TmfSignalHandler
544 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
545 if (signal.getTrace() == this) {
546 broadcast(new TmfExperimentUpdatedSignal(this, this));
547 }
548 }
549
550 /**
551 * Signal handler for the TmfExperimentRangeUpdatedSignal signal
552 *
553 * @param signal
554 */
555 @TmfSignalHandler
556 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
557 if (signal.getExperiment() == this) {
558 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
559 }
560 }
561
562 }
This page took 0.043337 seconds and 6 git commands to generate.