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