Remove the generic location (replace by Comparable)
[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 extends TmfTrace implements ITmfEventParser {
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[] 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 the event type
89 * @param id the experiment id
90 * @param traces the experiment set of traces
91 */
92 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
93 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE);
94 }
95
96 /**
97 * @param type the event type
98 * @param path the experiment path
99 * @param traces the experiment set of traces
100 * @param indexPageSize the experiment index page size
101 */
102 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
103 setCacheSize(indexPageSize);
104 setStreamingInterval(0);
105 setIndexer(new TmfCheckpointIndexer(this, indexPageSize));
106 setParser(this);
107 try {
108 super.initialize(null, path, type);
109 } catch (TmfTraceException e) {
110 e.printStackTrace();
111 }
112
113 fTraces = traces;
114 setTimeRange(TmfTimeRange.NULL_RANGE);
115 }
116
117 /**
118 * Clears the experiment
119 */
120 @Override
121 public synchronized void dispose() {
122
123 final TmfExperimentDisposedSignal signal = new TmfExperimentDisposedSignal(this, this);
124 broadcast(signal);
125
126 if (fCurrentExperiment == this) {
127 fCurrentExperiment = null;
128 }
129
130 // Clean up the index if applicable
131 if (getIndexer() != null) {
132 getIndexer().dispose();
133 }
134
135 if (fTraces != null) {
136 for (final ITmfTrace trace : fTraces) {
137 trace.dispose();
138 }
139 fTraces = null;
140 }
141 super.dispose();
142 }
143
144 // ------------------------------------------------------------------------
145 // ITmfTrace - Initializers
146 // ------------------------------------------------------------------------
147
148 /* (non-Javadoc)
149 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
150 */
151 @Override
152 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
153 }
154
155 /* (non-Javadoc)
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
157 */
158 @Override
159 public boolean validate(final IProject project, final String path) {
160 return true;
161 }
162
163 // ------------------------------------------------------------------------
164 // Accessors
165 // ------------------------------------------------------------------------
166
167 /**
168 * Selects the current, framework-wide, experiment
169 *
170 * @param experiment das experiment
171 */
172 public static void setCurrentExperiment(final TmfExperiment experiment) {
173 if (fCurrentExperiment != null && fCurrentExperiment != experiment) {
174 fCurrentExperiment.dispose();
175 }
176 fCurrentExperiment = experiment;
177 }
178
179 /**
180 * @return das experiment
181 */
182 public static TmfExperiment getCurrentExperiment() {
183 return fCurrentExperiment;
184 }
185
186 /**
187 * Get the list of traces. Handle with care...
188 *
189 * @return the experiment traces
190 */
191 public ITmfTrace[] getTraces() {
192 return fTraces;
193 }
194
195 /**
196 * Returns the timestamp of the event at the requested index. If none,
197 * returns null.
198 *
199 * @param index the event index (rank)
200 * @return the corresponding event timestamp
201 */
202 public ITmfTimestamp getTimestamp(final int index) {
203 final ITmfContext context = seekEvent(index);
204 final ITmfEvent event = getNext(context);
205 return (event != null) ? event.getTimestamp() : null;
206 }
207
208 /**
209 * Set the file to be used for bookmarks on this experiment
210 *
211 * @param file the bookmarks file
212 */
213 public void setBookmarksFile(final IFile file) {
214 fBookmarksFile = file;
215 }
216
217 /**
218 * Get the file used for bookmarks on this experiment
219 *
220 * @return the bookmarks file or null if none is set
221 */
222 public IFile getBookmarksFile() {
223 return fBookmarksFile;
224 }
225
226 // ------------------------------------------------------------------------
227 // Request management
228 // ------------------------------------------------------------------------
229
230 /* (non-Javadoc)
231 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
232 */
233 @Override
234 protected synchronized ITmfContext armRequest(final ITmfDataRequest request) {
235
236 // Make sure we have something to read from
237 if (fTraces == null) {
238 return null;
239 }
240
241 if (request instanceof ITmfEventRequest
242 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
243 && request.getIndex() == 0)
244 {
245 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
246 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
247 return context;
248
249 }
250
251 // Check if we are already at the right index
252 if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex()) {
253 return fExperimentContext;
254 }
255
256 return seekEvent(request.getIndex());
257 }
258
259 // ------------------------------------------------------------------------
260 // ITmfTrace trace positioning
261 // ------------------------------------------------------------------------
262
263 /* (non-Javadoc)
264 *
265 * Returns a brand new context based on the location provided and
266 * initializes the event queues
267 *
268 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
269 */
270 @Override
271 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
272 // Validate the location
273 if (location != null && !(location instanceof TmfExperimentLocation)) {
274 return null; // Throw an exception?
275 }
276 // Make sure we have something to read from
277 if (fTraces == null) {
278 return null;
279 }
280
281 // Instantiate the location
282 final TmfExperimentLocation expLocation = (location == null)
283 ? new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation[fTraces.length]))
284 : (TmfExperimentLocation) location.clone();
285
286 // Create and populate the context's traces contexts
287 final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
288
289 for (int i = 0; i < fTraces.length; i++) {
290 // Get the relevant trace attributes
291 final ITmfLocation trcLocation = expLocation.getLocationData().getLocations()[i];
292 context.getContexts()[i] = fTraces[i].seekEvent(trcLocation);
293 expLocation.getLocationData().getLocations()[i] = context.getContexts()[i].getLocation().clone();
294 context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]);
295 }
296
297 // Finalize context
298 context.setLocation(expLocation);
299 context.setLastTrace(TmfExperimentContext.NO_TRACE);
300 context.setRank((location == null) ? 0 : ITmfContext.UNKNOWN_RANK);
301
302 fExperimentContext = context;
303 return context;
304 }
305
306 // ------------------------------------------------------------------------
307 // ITmfTrace - SeekEvent operations (returning a trace context)
308 // ------------------------------------------------------------------------
309
310 /* (non-Javadoc)
311 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
312 */
313 @Override
314 public ITmfContext seekEvent(final double ratio) {
315 final ITmfContext context = seekEvent((long) (ratio * getNbEvents()));
316 return context;
317 }
318
319 /* (non-Javadoc)
320 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
321 */
322 @Override
323 public double getLocationRatio(final ITmfLocation location) {
324 if (location instanceof TmfExperimentLocation) {
325 return (double) seekEvent(location).getRank() / getNbEvents();
326 }
327 return 0.0;
328 }
329
330 /* (non-Javadoc)
331 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
332 */
333 @Override
334 public ITmfLocation getCurrentLocation() {
335 ITmfLocation[] locations = new ITmfLocation[fTraces.length];
336 for (int i = 0; i < fTraces.length; i++) {
337 locations[i] = fTraces[i].getCurrentLocation();
338 }
339 return new TmfExperimentLocation(new TmfLocationArray(locations));
340 }
341
342 // ------------------------------------------------------------------------
343 // ITmfTrace trace positioning
344 // ------------------------------------------------------------------------
345
346 /* (non-Javadoc)
347 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
348 */
349 @Override
350 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
351 final ITmfContext savedContext = context.clone();
352 final ITmfEvent event = getNext(savedContext);
353 return event;
354 }
355
356 /* (non-Javadoc)
357 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
358 */
359 @Override
360 public synchronized ITmfEvent getNext(ITmfContext context) {
361
362 // Validate the context
363 if (!(context instanceof TmfExperimentContext)) {
364 return null; // Throw an exception?
365 }
366
367 // Make sure that we have something to read from
368 if (fTraces == null) {
369 return null;
370 }
371
372 TmfExperimentContext expContext = (TmfExperimentContext) context;
373
374 // If an event was consumed previously, first get the next one from that trace
375 final int lastTrace = expContext.getLastTrace();
376 if (lastTrace != TmfExperimentContext.NO_TRACE) {
377 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
378 expContext.getEvents()[lastTrace] = fTraces[lastTrace].getNext(traceContext);
379 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
380 }
381
382 // Scan the candidate events and identify the "next" trace to read from
383 int trace = TmfExperimentContext.NO_TRACE;
384 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
385 for (int i = 0; i < fTraces.length; i++) {
386 final ITmfEvent event = expContext.getEvents()[i];
387 if (event != null && event.getTimestamp() != null) {
388 final ITmfTimestamp otherTS = event.getTimestamp();
389 if (otherTS.compareTo(timestamp, true) < 0) {
390 trace = i;
391 timestamp = otherTS;
392 }
393 }
394 }
395
396 ITmfEvent event = null;
397 if (trace != TmfExperimentContext.NO_TRACE) {
398 event = expContext.getEvents()[trace];
399 if (event != null) {
400 updateAttributes(expContext, event.getTimestamp());
401 expContext.increaseRank();
402 expContext.setLastTrace(trace);
403 final ITmfContext traceContext = expContext.getContexts()[trace];
404
405 TmfExperimentLocation location = (TmfExperimentLocation) expContext.getLocation();
406 if (location != null) {
407 location.getLocationData().getLocations()[trace] = traceContext.getLocation().clone();
408 }
409
410 fExperimentContext = expContext.clone();
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 synchronized 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 }
444 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp().clone(), TmfTimestamp.BIG_CRUNCH);
445 final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
446
447 // Broadcast in separate thread to prevent deadlock
448 new Thread() {
449 @Override
450 public void run() {
451 broadcast(signal);
452 }
453 }.start();
454 return;
455 }
456
457 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
458 private ITmfTimestamp safeTimestamp = null;
459 private ITmfTimestamp lastSafeTimestamp = null;
460 private TmfTimeRange timeRange = null;
461
462 @Override
463 public void run() {
464 while (!executorIsShutdown()) {
465 if (!getIndexer().isIndexing()) {
466 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
467 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
468 for (final ITmfTrace trace : fTraces) {
469 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
470 startTimestamp = trace.getStartTime();
471 }
472 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
473 endTimestamp = trace.getEndTime();
474 }
475 }
476 if (safeTimestamp != null && (lastSafeTimestamp == null || safeTimestamp.compareTo(lastSafeTimestamp, false) > 0)) {
477 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
478 lastSafeTimestamp = safeTimestamp;
479 } else {
480 timeRange = null;
481 }
482 safeTimestamp = endTimestamp;
483 if (timeRange != null) {
484 final TmfExperimentRangeUpdatedSignal signal =
485 new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
486 broadcast(signal);
487 }
488 }
489 try {
490 Thread.sleep(getStreamingInterval());
491 } catch (final InterruptedException e) {
492 e.printStackTrace();
493 }
494 }
495 }
496 };
497 thread.start();
498 }
499
500 /* (non-Javadoc)
501 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
502 */
503 @Override
504 public long getStreamingInterval() {
505 long interval = 0;
506 for (final ITmfTrace trace : fTraces) {
507 interval = Math.max(interval, trace.getStreamingInterval());
508 }
509 return interval;
510 }
511
512 // ------------------------------------------------------------------------
513 // Signal handlers
514 // ------------------------------------------------------------------------
515
516 private Integer fEndSynchReference;
517
518 /**
519 * Signal handler for the TmfExperimentSelectedSignal signal
520 *
521 * @param signal The incoming signal
522 */
523 @TmfSignalHandler
524 public void experimentSelected(final TmfExperimentSelectedSignal signal) {
525 final TmfExperiment experiment = signal.getExperiment();
526 if (experiment == this) {
527 setCurrentExperiment(experiment);
528 fEndSynchReference = Integer.valueOf(signal.getReference());
529 }
530 }
531
532 /**
533 * Signal handler for the TmfEndSynchSignal signal
534 *
535 * @param signal The incoming signal
536 */
537 @TmfSignalHandler
538 public void endSync(final TmfEndSynchSignal signal) {
539 if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
540 fEndSynchReference = null;
541 initializeStreamingMonitor();
542 }
543 }
544
545 /**
546 * Signal handler for the TmfTraceUpdatedSignal signal
547 *
548 * @param signal The incoming signal
549 */
550 @TmfSignalHandler
551 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
552 if (signal.getTrace() == this) {
553 broadcast(new TmfExperimentUpdatedSignal(this, this));
554 }
555 }
556
557 /**
558 * Signal handler for the TmfExperimentRangeUpdatedSignal signal
559 *
560 * @param signal The incoming signal
561 */
562 @TmfSignalHandler
563 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
564 if (signal.getExperiment() == this) {
565 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
566 }
567 }
568
569 }
This page took 0.058936 seconds and 5 git commands to generate.