Fix test case for local time zone
[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;
5419a136
AM
27import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
28import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
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
5419a136 205 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
9b749023 206
6a953367
BH
207 // Make sure we have something to read from
208 if (fTraces == null) {
209 return null;
210 }
9b749023 211
5419a136
AM
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());
49e2f79a 218 return context;
5419a136 219
49e2f79a
FC
220 }
221
5419a136 222 return seekEvent(request.getIndex());
49e2f79a
FC
223 }
224
a79913eb 225 // ------------------------------------------------------------------------
9f584e4c
FC
226 // ITmfTrace trace positioning
227 // ------------------------------------------------------------------------
228
9e0640dc
FC
229 /* (non-Javadoc)
230 *
9b749023 231 * Returns a brand new context based on the location provided and
9e0640dc 232 * initializes the event queues
9b749023 233 *
9e0640dc
FC
234 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
235 */
a79913eb 236 @Override
1e1bef82 237 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
a79913eb 238 // Validate the location
9e0640dc 239 if (location != null && !(location instanceof TmfExperimentLocation)) {
a79913eb 240 return null; // Throw an exception?
9e0640dc
FC
241 }
242 // Make sure we have something to read from
243 if (fTraces == null) {
a79913eb 244 return null;
9e0640dc 245 }
8f50c396 246
a79913eb 247 // Create and populate the context's traces contexts
0316808c 248 final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
d62bb185
FC
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 }
9b635e61 259
d62bb185 260 // Position the traces
a79913eb
FC
261 for (int i = 0; i < fTraces.length; i++) {
262 // Get the relevant trace attributes
d62bb185 263 final ITmfLocation trcLocation = expLocations[i];
5cc97265 264 context.getContexts()[i] = fTraces[i].seekEvent(trcLocation);
d62bb185 265 expLocations[i] = context.getContexts()[i].getLocation();
c32744d6 266 context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]);
a79913eb 267 }
8f50c396 268
a79913eb 269 // Finalize context
d62bb185 270 context.setLocation(new TmfExperimentLocation(new TmfLocationArray(expLocations)));
a79913eb 271 context.setLastTrace(TmfExperimentContext.NO_TRACE);
17324c9a 272 context.setRank((location == null) ? 0 : ITmfContext.UNKNOWN_RANK);
49e2f79a 273
9b749023 274 return context;
a79913eb 275 }
9f584e4c 276
3bd44ac8
FC
277 // ------------------------------------------------------------------------
278 // ITmfTrace - SeekEvent operations (returning a trace context)
279 // ------------------------------------------------------------------------
280
9e0640dc
FC
281 /* (non-Javadoc)
282 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
283 */
c76c54bb 284 @Override
0316808c 285 public ITmfContext seekEvent(final double ratio) {
91f6e587 286 final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
c76c54bb
FC
287 return context;
288 }
289
9e0640dc
FC
290 /* (non-Javadoc)
291 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
292 */
a79913eb 293 @Override
1e1bef82 294 public double getLocationRatio(final ITmfLocation location) {
9e0640dc 295 if (location instanceof TmfExperimentLocation) {
5cc97265 296 return (double) seekEvent(location).getRank() / getNbEvents();
9e0640dc
FC
297 }
298 return 0.0;
c76c54bb
FC
299 }
300
9e0640dc
FC
301 /* (non-Javadoc)
302 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
303 */
a79913eb 304 @Override
1e1bef82
FC
305 public ITmfLocation getCurrentLocation() {
306 ITmfLocation[] locations = new ITmfLocation[fTraces.length];
a87cc4ef 307 for (int i = 0; i < fTraces.length; i++) {
5cc97265 308 locations[i] = fTraces[i].getCurrentLocation();
a87cc4ef
FC
309 }
310 return new TmfExperimentLocation(new TmfLocationArray(locations));
a79913eb 311 }
c76c54bb 312
9e0640dc
FC
313 // ------------------------------------------------------------------------
314 // ITmfTrace trace positioning
315 // ------------------------------------------------------------------------
316
07671572 317 /* (non-Javadoc)
408e65d2 318 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
07671572
FC
319 */
320 @Override
6256d8ad 321 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
408e65d2 322 final ITmfContext savedContext = context.clone();
6256d8ad 323 final ITmfEvent event = getNext(savedContext);
07671572
FC
324 return event;
325 }
a79913eb 326
ce2388e0 327 /* (non-Javadoc)
408e65d2 328 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
a79913eb 329 */
0316808c 330 @Override
6256d8ad 331 public synchronized ITmfEvent getNext(ITmfContext context) {
a79913eb
FC
332
333 // Validate the context
9e0640dc 334 if (!(context instanceof TmfExperimentContext)) {
a79913eb 335 return null; // Throw an exception?
9e0640dc 336 }
0e8c76f8
BH
337
338 // Make sure that we have something to read from
339 if (fTraces == null) {
340 return null;
341 }
342
a87cc4ef 343 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 344
a87cc4ef 345 // If an event was consumed previously, first get the next one from that trace
cbdacf03 346 final int lastTrace = expContext.getLastTrace();
a79913eb 347 if (lastTrace != TmfExperimentContext.NO_TRACE) {
cbdacf03 348 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
c32744d6 349 expContext.getEvents()[lastTrace] = fTraces[lastTrace].getNext(traceContext);
a79913eb 350 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
351 }
352
353 // Scan the candidate events and identify the "next" trace to read from
354 int trace = TmfExperimentContext.NO_TRACE;
a4115405 355 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
0316808c 356 for (int i = 0; i < fTraces.length; i++) {
cbdacf03 357 final ITmfEvent event = expContext.getEvents()[i];
a79913eb 358 if (event != null && event.getTimestamp() != null) {
cbdacf03 359 final ITmfTimestamp otherTS = event.getTimestamp();
a79913eb
FC
360 if (otherTS.compareTo(timestamp, true) < 0) {
361 trace = i;
362 timestamp = otherTS;
363 }
364 }
365 }
a87cc4ef 366
6256d8ad 367 ITmfEvent event = null;
07671572 368 if (trace != TmfExperimentContext.NO_TRACE) {
6256d8ad 369 event = expContext.getEvents()[trace];
408e65d2
FC
370 if (event != null) {
371 updateAttributes(expContext, event.getTimestamp());
408e65d2
FC
372 expContext.increaseRank();
373 expContext.setLastTrace(trace);
17324c9a
FC
374 final ITmfContext traceContext = expContext.getContexts()[trace];
375
d62bb185
FC
376 expContext.setLocation(new TmfExperimentLocation(
377 (TmfExperimentLocation) expContext.getLocation(),
378 trace, traceContext.getLocation()));
17324c9a 379
408e65d2
FC
380 processEvent(event);
381 }
07671572 382 }
a87cc4ef 383
a87cc4ef 384 return event;
a79913eb
FC
385 }
386
66262ad8
BH
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
bcbea6a6 409 /* (non-Javadoc)
a79913eb
FC
410 * @see java.lang.Object#toString()
411 */
412 @Override
3b38ea61 413 @SuppressWarnings("nls")
5419a136 414 public synchronized String toString() {
a79913eb
FC
415 return "[TmfExperiment (" + getName() + ")]";
416 }
8c8bf09f
ASL
417
418 // ------------------------------------------------------------------------
9e0640dc 419 // Streaming support
8c8bf09f
ASL
420 // ------------------------------------------------------------------------
421
1b70b6dc 422 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
423
424 if (fInitialized) {
828e5592 425 return;
9e0640dc 426 }
828e5592
PT
427 fInitialized = true;
428
1b70b6dc 429 if (getStreamingInterval() == 0) {
0316808c 430 final ITmfContext context = seekEvent(0);
cbdacf03 431 final ITmfEvent event = getNext(context);
4c9f2944 432 context.dispose();
9b749023 433 if (event == null) {
1b70b6dc 434 return;
9b749023 435 }
4593bd5b 436 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 437 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
438
439 // Broadcast in separate thread to prevent deadlock
440 new Thread() {
441 @Override
442 public void run() {
443 broadcast(signal);
444 }
445 }.start();
1b70b6dc
PT
446 return;
447 }
448
9e0640dc 449 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 450 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 451 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 452 private TmfTimeRange timeRange = null;
1b70b6dc
PT
453
454 @Override
455 public void run() {
fc7cd0be 456 while (!executorIsShutdown()) {
9e0640dc 457 if (!getIndexer().isIndexing()) {
a4115405
FC
458 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
459 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
6256d8ad 460 for (final ITmfTrace trace : fTraces) {
9b749023 461 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 462 startTimestamp = trace.getStartTime();
9b749023
AM
463 }
464 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 465 endTimestamp = trace.getEndTime();
9b749023 466 }
1b70b6dc 467 }
6be2d5cc 468 if (safeTimestamp != null && (lastSafeTimestamp == null || safeTimestamp.compareTo(lastSafeTimestamp, false) > 0)) {
1b70b6dc 469 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
6be2d5cc 470 lastSafeTimestamp = safeTimestamp;
9b749023 471 } else {
1b70b6dc 472 timeRange = null;
9b749023 473 }
1b70b6dc
PT
474 safeTimestamp = endTimestamp;
475 if (timeRange != null) {
faa38350
PT
476 final TmfTraceRangeUpdatedSignal signal =
477 new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1b70b6dc
PT
478 broadcast(signal);
479 }
480 }
481 try {
482 Thread.sleep(getStreamingInterval());
cbdacf03 483 } catch (final InterruptedException e) {
1b70b6dc
PT
484 e.printStackTrace();
485 }
486 }
487 }
488 };
489 thread.start();
490 }
491
9e0640dc 492 /* (non-Javadoc)
1b70b6dc
PT
493 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
494 */
495 @Override
496 public long getStreamingInterval() {
497 long interval = 0;
6256d8ad 498 for (final ITmfTrace trace : fTraces) {
1b70b6dc 499 interval = Math.max(interval, trace.getStreamingInterval());
9b749023 500 }
1b70b6dc
PT
501 return interval;
502 }
503
8c8bf09f
ASL
504 // ------------------------------------------------------------------------
505 // Signal handlers
506 // ------------------------------------------------------------------------
507
faa38350
PT
508 /* (non-Javadoc)
509 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#traceOpened(org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal)
828e5592 510 */
faa38350 511 @Override
9e0640dc 512 @TmfSignalHandler
faa38350 513 public void traceOpened(TmfTraceOpenedSignal signal) {
9e0640dc 514 if (signal.getTrace() == this) {
faa38350 515 initializeStreamingMonitor();
9e0640dc 516 }
a1091415
PT
517 }
518
4dc47e28 519}
This page took 0.088467 seconds and 5 git commands to generate.