Refactor TmfExperiment (2)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / experiment / 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
6c13869b 14package org.eclipse.linuxtools.tmf.core.experiment;
8c8bf09f 15
a1091415 16import org.eclipse.core.resources.IFile;
12c155f5 17import org.eclipse.core.resources.IProject;
828e5592 18import org.eclipse.core.resources.IResource;
72f1e62a 19import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
4df4581d 20import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
21import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
22import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
0316808c 23import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
6c13869b
FC
24import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
25import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
1b70b6dc 26import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
6c13869b
FC
27import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentDisposedSignal;
28import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
29import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
30import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
31import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
6c13869b 32import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
0316808c 33import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
6c13869b
FC
34import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
35import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
0316808c
FC
36import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
37import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
8c8bf09f
ASL
38
39/**
cbdacf03
FC
40 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces
41 * that are part of a tracing experiment.
8c8bf09f 42 */
0316808c 43public class TmfExperiment<T extends ITmfEvent> extends TmfTrace<T> implements ITmfEventParser<T> {
8c8bf09f
ASL
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
a79913eb 49 // The currently selected experiment
0316808c 50 private static TmfExperiment<?> fCurrentExperiment = null;
e31e01e8 51
a79913eb 52 // The set of traces that constitute the experiment
0316808c 53 private ITmfTrace<T>[] fTraces;
8c8bf09f 54
a87cc4ef
FC
55// // The experiment time range
56// private TmfTimeRange fTimeRange;
8c8bf09f 57
a87cc4ef
FC
58// // The total number of events
59// private long fNbEvents;
8c8bf09f 60
a87cc4ef
FC
61// // The experiment index
62// private Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
9f584e4c 63
a87cc4ef
FC
64// // The current experiment context
65// private TmfExperimentContext fExperimentContext;
a79913eb 66
828e5592
PT
67 // Flag to initialize only once
68 private boolean fInitialized = false;
69
a1091415
PT
70 // The experiment bookmarks file
71 private IFile fBookmarksFile;
72
73 // The properties resource
828e5592
PT
74 private IResource fResource;
75
8c8bf09f
ASL
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79
12c155f5 80 @Override
cbdacf03 81 public boolean validate(final IProject project, final String path) {
12c155f5
FC
82 return true;
83 }
84
85 @Override
25e48683
FC
86 public void initTrace(final IResource resource, final String path, final Class<T> eventType) {
87 fResource = resource;
96c6806f
PT
88 }
89
8c8bf09f
ASL
90 /**
91 * @param type
92 * @param id
93 * @param traces
94 * @param epoch
95 * @param indexPageSize
0316808c 96 * @throws TmfTraceException
8c8bf09f 97 */
cbdacf03 98 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
0316808c
FC
99 final int indexPageSize)
100 {
a4115405 101 this(type, id, traces, TmfTimestamp.ZERO, indexPageSize, false);
a79913eb 102 }
cb866e08 103
0316808c
FC
104 @SuppressWarnings({ "unchecked", "rawtypes" })
105 public TmfExperiment(final Class<T> type, final String path, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
106 final int indexPageSize, final boolean preIndexExperiment)
107 {
108 setCacheSize(indexPageSize);
109 setStreamingInterval(0);
110 setIndexer(new TmfCheckpointIndexer(this));
111 setParser(this);
112 try {
113 super.initialize(null, path, type);
114 } catch (TmfTraceException e) {
115 e.printStackTrace();
116 }
8c8bf09f 117
a79913eb 118 fTraces = traces;
a87cc4ef 119 setTimeRange(TmfTimeRange.NULL_RANGE);
8c8bf09f 120
a87cc4ef
FC
121 if (preIndexExperiment) {
122 getIndexer().buildIndex(true);
0316808c 123// updateTimeRange();
a87cc4ef 124 }
a79913eb 125 }
8c8bf09f 126
0316808c
FC
127// /**
128// * Initialize the trace common attributes and the base component.
129// *
130// * @param resource the Eclipse resource (trace)
131// * @param path the trace path
132// * @param type the trace event type
133// *
134// * @throws TmfTraceException
135// */
136// @Override
137// @SuppressWarnings("unchecked")
138// protected void initialize(final IResource resource, final String path, final Class<T> type) {
139// setParser(this);
140// super.initialize(resource, path, type);
141// }
142
143// protected TmfExperiment(final String id, final Class<T> type) {
144// super(id, type);
145// }
82e04272 146
8c8bf09f
ASL
147 /**
148 * @param type
149 * @param id
150 * @param traces
0316808c 151 * @throws TmfTraceException
8c8bf09f 152 */
cbdacf03 153 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces) {
a4115405 154 this(type, id, traces, TmfTimestamp.ZERO, DEFAULT_INDEX_PAGE_SIZE);
8c8bf09f
ASL
155 }
156
157 /**
158 * @param type
159 * @param id
160 * @param traces
161 * @param indexPageSize
0316808c 162 * @throws TmfTraceException
8c8bf09f 163 */
cbdacf03 164 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final int indexPageSize) {
a4115405 165 this(type, id, traces, TmfTimestamp.ZERO, indexPageSize);
8c8bf09f 166 }
a79913eb 167
8c8bf09f 168 /**
ff4ed569 169 * Clears the experiment
8c8bf09f
ASL
170 */
171 @Override
12c155f5 172 @SuppressWarnings("rawtypes")
a79913eb
FC
173 public synchronized void dispose() {
174
cbdacf03 175 final TmfExperimentDisposedSignal<T> signal = new TmfExperimentDisposedSignal<T>(this, this);
a79913eb 176 broadcast(signal);
cbdacf03 177 if (fCurrentExperiment == this)
09d11238 178 fCurrentExperiment = null;
a79913eb
FC
179
180 if (fTraces != null) {
cbdacf03 181 for (final ITmfTrace trace : fTraces)
a79913eb 182 trace.dispose();
a79913eb
FC
183 fTraces = null;
184 }
a87cc4ef
FC
185// if (fCheckpoints != null)
186// fCheckpoints.clear();
2fb2eb37 187 super.dispose();
8c8bf09f
ASL
188 }
189
9f584e4c 190 // ------------------------------------------------------------------------
cbd4ad82 191 // ITmfTrace
9f584e4c
FC
192 // ------------------------------------------------------------------------
193
a87cc4ef
FC
194// @Override
195// public Class<T> getEventType() {
196// return fType;
197// }
25e48683 198
a87cc4ef
FC
199// @Override
200// public long getNbEvents() {
201// return fNbEvents;
202// }
9f584e4c 203
a87cc4ef
FC
204// @Override
205// public int getCacheSize() {
206// return fIndexPageSize;
207// }
54d55ced 208
a87cc4ef
FC
209// @Override
210// public TmfTimeRange getTimeRange() {
211// return fTimeRange;
212// }
9f584e4c 213
a87cc4ef
FC
214// @Override
215// public ITmfTimestamp getStartTime() {
216// return fTimeRange.getStartTime();
217// }
9f584e4c 218
a87cc4ef
FC
219// @Override
220// public ITmfTimestamp getEndTime() {
221// return fTimeRange.getEndTime();
222// }
9f584e4c 223
a87cc4ef
FC
224// public Vector<TmfCheckpoint> getCheckpoints() {
225// return fCheckpoints;
226// }
54d55ced 227
8c8bf09f 228 // ------------------------------------------------------------------------
e31e01e8 229 // Accessors
8c8bf09f
ASL
230 // ------------------------------------------------------------------------
231
cbdacf03
FC
232 public static void setCurrentExperiment(final TmfExperiment<?> experiment) {
233 if (fCurrentExperiment != null && fCurrentExperiment != experiment)
09d11238 234 fCurrentExperiment.dispose();
a79913eb 235 fCurrentExperiment = experiment;
f6b14ce2
FC
236 }
237
e31e01e8 238 public static TmfExperiment<?> getCurrentExperiment() {
a79913eb 239 return fCurrentExperiment;
8c8bf09f
ASL
240 }
241
12c155f5 242 public ITmfTrace<T>[] getTraces() {
a79913eb 243 return fTraces;
8c8bf09f
ASL
244 }
245
8c8bf09f 246 /**
cbdacf03
FC
247 * Returns the timestamp of the event at the requested index. If none,
248 * returns null.
a79913eb 249 *
0d9a6d76
FC
250 * @param index the event index (rank)
251 * @return the corresponding event timestamp
8c8bf09f 252 */
cbdacf03 253 public ITmfTimestamp getTimestamp(final int index) {
0316808c
FC
254// final TmfExperimentContext context = seekEvent(index);
255 final ITmfContext context = seekEvent(index);
b4f71e4a 256 final ITmfEvent event = readNextEvent(context);
a79913eb 257 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
258 }
259
260 // ------------------------------------------------------------------------
261 // Operators
262 // ------------------------------------------------------------------------
263
8c8bf09f
ASL
264 /**
265 * Update the global time range
266 */
a79913eb 267 protected void updateTimeRange() {
a87cc4ef
FC
268 TmfTimeRange timeRange = getTimeRange();
269 ITmfTimestamp startTime = timeRange.equals(TmfTimeRange.NULL_RANGE) ? TmfTimestamp.BIG_CRUNCH : timeRange.getStartTime();
270 ITmfTimestamp endTime = timeRange.equals(TmfTimeRange.NULL_RANGE) ? TmfTimestamp.BIG_BANG : timeRange.getEndTime();
a79913eb 271
cbdacf03
FC
272 for (final ITmfTrace<T> trace : fTraces) {
273 final ITmfTimestamp traceStartTime = trace.getStartTime();
a79913eb
FC
274 if (traceStartTime.compareTo(startTime, true) < 0)
275 startTime = traceStartTime;
cbdacf03 276 final ITmfTimestamp traceEndTime = trace.getEndTime();
a79913eb
FC
277 if (traceEndTime.compareTo(endTime, true) > 0)
278 endTime = traceEndTime;
279 }
a87cc4ef 280 setTimeRange(new TmfTimeRange(startTime, endTime));
8c8bf09f
ASL
281 }
282
283 // ------------------------------------------------------------------------
284 // TmfProvider
285 // ------------------------------------------------------------------------
a79913eb 286
a79913eb 287 @Override
cbdacf03 288 public ITmfContext armRequest(final ITmfDataRequest<T> request) {
ce2388e0
FC
289 // Tracer.trace("Ctx: Arming request - start");
290 ITmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? ((ITmfEventRequest<T>) request).getRange().getStartTime() : null;
cbdacf03 291 if (TmfTimestamp.BIG_BANG.equals(timestamp) || request.getIndex() > 0)
a79913eb 292 timestamp = null; // use request index
0316808c
FC
293// TmfExperimentContext context = null;
294 ITmfContext context = null;
a79913eb
FC
295 if (timestamp != null) {
296 // seek by timestamp
297 context = seekEvent(timestamp);
298 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
cbdacf03 299 } else // Seek by rank
a87cc4ef
FC
300// if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex())
301// // We are already at the right context -> no need to seek
302// context = fExperimentContext;
303// else
ce2388e0
FC
304 context = seekEvent(request.getIndex());
305 // Tracer.trace("Ctx: Arming request - done");
a79913eb
FC
306 return context;
307 }
308
0316808c
FC
309// @Override
310// @SuppressWarnings("unchecked")
311// public T getNext(final ITmfContext context) {
312// if (context instanceof TmfExperimentContext)
313// return (T) readNextEvent(context);
314// return null;
315// }
a79913eb
FC
316
317 // ------------------------------------------------------------------------
9f584e4c
FC
318 // ITmfTrace trace positioning
319 // ------------------------------------------------------------------------
320
a79913eb
FC
321 // Returns a brand new context based on the location provided
322 // and initializes the event queues
323 @Override
7e6347b0 324 public synchronized TmfExperimentContext seekEvent(final ITmfLocation<?> location) {
a79913eb 325 // Validate the location
cbdacf03 326 if (location != null && !(location instanceof TmfExperimentLocation))
a79913eb 327 return null; // Throw an exception?
8f50c396 328
cbdacf03 329 if (fTraces == null)
a79913eb 330 return null;
8f50c396 331
a79913eb 332 // Instantiate the location
cbdacf03 333 final TmfExperimentLocation expLocation = (location == null) ? new TmfExperimentLocation(new TmfLocationArray(
0316808c 334 new ITmfLocation<?>[fTraces.length])) : (TmfExperimentLocation) location.clone();
8f50c396 335
a79913eb 336 // Create and populate the context's traces contexts
0316808c 337 final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
ce2388e0 338 // Tracer.trace("Ctx: SeekLocation - start");
9b635e61 339
0316808c 340// long rank = 0;
a79913eb
FC
341 for (int i = 0; i < fTraces.length; i++) {
342 // Get the relevant trace attributes
0316808c
FC
343 final ITmfLocation<?> traceLocation = expLocation.getLocation().getLocations()[i];
344// final long traceRank = expLocation.getRanks()[i];
8f50c396 345
a79913eb 346 // Set the corresponding sub-context
7e6347b0 347 context.getContexts()[i] = fTraces[i].seekEvent(traceLocation);
0316808c
FC
348// context.getContexts()[i].setRank(traceRank);
349// rank += traceRank;
8f50c396 350
a79913eb 351 // Set the trace location and read the corresponding event
cbdacf03
FC
352 /*
353 * The (TmfContext) cast should be safe since we created 'context'
354 * ourselves higher up.
355 */
0316808c 356 expLocation.getLocation().getLocations()[i] = context.getContexts()[i].getLocation().clone();
b4f71e4a 357 context.getEvents()[i] = fTraces[i].readNextEvent(context.getContexts()[i]);
a79913eb 358 }
8f50c396 359
ce2388e0 360 // Tracer.trace("Ctx: SeekLocation - done");
9b635e61 361
a79913eb
FC
362 // Finalize context
363 context.setLocation(expLocation);
364 context.setLastTrace(TmfExperimentContext.NO_TRACE);
0316808c
FC
365// context.setRank(rank);
366 context.setRank(ITmfContext.UNKNOWN_RANK);
9b635e61 367
a87cc4ef 368// fExperimentContext = context;
9b635e61 369
a79913eb
FC
370 return context;
371 }
9f584e4c 372
0316808c
FC
373// /* (non-Javadoc)
374// * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
375// */
376// @Override
377// public synchronized TmfExperimentContext seekEvent(ITmfTimestamp timestamp) {
378//
379// // Tracer.trace("Ctx: seekEvent(TS) - start");
380//
381// if (timestamp == null)
382// timestamp = TmfTimestamp.BIG_BANG;
383//
384// // First, find the right checkpoint
385// int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
386//
387// // In the very likely case that the checkpoint was not found, bsearch
388// // returns its negated would-be location (not an offset...). From that
389// // index, we can then position the stream and get the event.
390// if (index < 0)
391// index = Math.max(0, -(index + 2));
392//
393// // Position the experiment at the checkpoint
394// ITmfLocation<?> location;
395// synchronized (fCheckpoints) {
396// if (fCheckpoints.size() > 0) {
397// if (index >= fCheckpoints.size())
398// index = fCheckpoints.size() - 1;
399// location = fCheckpoints.elementAt(index).getLocation();
400// } else
401// location = null;
402// }
403//
404// final TmfExperimentContext context = seekEvent(location);
405// context.setRank((long) index * fIndexPageSize);
406//
407// // And locate the requested event context
408// final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
409// ITmfEvent event = readNextEvent(nextEventContext);
410// while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
411// context.setLocation(nextEventContext.getLocation().clone());
412// context.increaseRank();
413// event = readNextEvent(nextEventContext);
414// }
415// if (event == null) {
416// context.setLocation(null);
417// context.setRank(ITmfContext.UNKNOWN_RANK);
418// }
419// return context;
420//
421//// // And locate the event
422//// ITmfEvent event = parseEvent(context);
423//// while ((event != null) && (event.getTimestamp().compareTo(timestamp, false) < 0)) {
424//// readNextEvent(context);
425//// event = parseEvent(context);
426//// }
427//
428//// if (event == null) {
429//// context.setLocation(null);
430//// context.setRank(ITmfContext.UNKNOWN_RANK);
431//// }
432////
433//// return context;
434// }
8c8bf09f 435
bcbea6a6 436 /* (non-Javadoc)
a79913eb
FC
437 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
438 */
0316808c
FC
439// @Override
440// public synchronized TmfExperimentContext seekEvent(final long rank) {
441//
442// // Tracer.trace("Ctx: seekEvent(rank) - start");
443//
444// // Position the stream at the previous checkpoint
445// int index = (int) rank / fIndexPageSize;
446// ITmfLocation<?> location;
447// synchronized (fCheckpoints) {
448// if (fCheckpoints.size() == 0)
449// location = null;
450// else {
451// if (index >= fCheckpoints.size())
452// index = fCheckpoints.size() - 1;
453// location = fCheckpoints.elementAt(index).getLocation();
454// }
455// }
456//
457// final TmfExperimentContext context = seekEvent(location);
458// context.setRank((long) index * fIndexPageSize);
459//
460//// // Position the trace at the checkpoint
461//// final ITmfContext context = fIndexer.seekIndex(rank);
462//
463// // And locate the requested event context
464// long pos = context.getRank();
465// if (pos < rank) {
466// ITmfEvent event = readNextEvent(context);
467// while (event != null && ++pos < rank) {
468// event = readNextEvent(context);
469// }
470// if (event == null) {
471// context.setLocation(null);
472// context.setRank(ITmfContext.UNKNOWN_RANK);
473// }
474// }
475// return context;
476//
477//// // And locate the event
478//// ITmfEvent event = parseEvent(context);
479//// long pos = context.getRank();
480//// while ((event != null) && (pos++ < rank)) {
481//// readNextEvent(context);
482//// event = parseEvent(context);
483//// }
484////
485//// if (event == null) {
486//// context.setLocation(null);
487//// context.setRank(ITmfContext.UNKNOWN_RANK);
488//// }
489////
490//// return context;
491// }
8c8bf09f 492
c76c54bb 493 @Override
0316808c
FC
494 public ITmfContext seekEvent(final double ratio) {
495 final ITmfContext context = seekEvent((long) (ratio * getNbEvents()));
c76c54bb
FC
496 return context;
497 }
498
a79913eb 499 @Override
cbdacf03
FC
500 public double getLocationRatio(final ITmfLocation<?> location) {
501 if (location instanceof TmfExperimentLocation)
7e6347b0 502 return (double) seekEvent(location).getRank() / getNbEvents();
c76c54bb
FC
503 return 0;
504 }
505
a79913eb
FC
506 @Override
507 public ITmfLocation<?> getCurrentLocation() {
a87cc4ef
FC
508// if (fExperimentContext != null)
509// return fExperimentContext.getLocation();
510// return null;
511 ITmfLocation<?>[] locations = new ITmfLocation<?>[fTraces.length];
512 for (int i = 0; i < fTraces.length; i++) {
513 locations[i] = fTraces[i].getCurrentLocation();
514 }
515 return new TmfExperimentLocation(new TmfLocationArray(locations));
a79913eb 516 }
c76c54bb 517
ce2388e0 518 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
ce2388e0
FC
519 // TmfContext context0 = context.getContexts()[0];
520 // TmfEvent event0 = context.getEvents()[0];
521 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
522 // long rank0 = context.getRank();
523 // int trace = context.getLastTrace();
524 //
525 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
526 //
527 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
528 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
529 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
530 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
531 // Tracer.trace(result.toString());
532 // }
54d55ced 533
0316808c
FC
534// /**
535// * Scan the next events from all traces and return the next one in
536// * chronological order.
537// *
538// * @param context the trace context
539// * @return the next event
540// */
541// @Override
542// public synchronized ITmfEvent readNextEvent(final ITmfContext context) {
543//
544// // Validate the context
545// if (!(context instanceof TmfExperimentContext))
546// return null; // Throw an exception?
547//
548// if (!context.equals(fExperimentContext))
549// // Tracer.trace("Ctx: Restoring context");
550// fExperimentContext = seekEvent(context.getLocation());
551//
552// final TmfExperimentContext expContext = (TmfExperimentContext) context;
553//
554// // dumpContext(expContext, true);
555// // If an event was consumed previously, get the next one from that trace
556// final int lastTrace = expContext.getLastTrace();
557// if (lastTrace != TmfExperimentContext.NO_TRACE) {
558// final ITmfContext traceContext = expContext.getContexts()[lastTrace];
559// expContext.getEvents()[lastTrace] = fTraces[lastTrace].readNextEvent(traceContext);
560// expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
561// }
562//
563// // Scan the candidate events and identify the "next" trace to read from
564// final ITmfEvent eventArray[] = expContext.getEvents();
565// if (eventArray == null)
566// return null;
567// int trace = TmfExperimentContext.NO_TRACE;
568// ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
569// if (eventArray.length == 1) {
570// if (eventArray[0] != null) {
571// timestamp = eventArray[0].getTimestamp();
572// trace = 0;
573// }
574// } else
575// for (int i = 0; i < eventArray.length; i++) {
576// final ITmfEvent event = eventArray[i];
577// if (event != null && event.getTimestamp() != null) {
578// final ITmfTimestamp otherTS = event.getTimestamp();
579// if (otherTS.compareTo(timestamp, true) < 0) {
580// trace = i;
581// timestamp = otherTS;
582// }
583// }
584// }
585//
586// // Update the experiment context and set the "next" event
587// ITmfEvent event = null;
588// if (trace != TmfExperimentContext.NO_TRACE) {
589// updateIndex(expContext, timestamp);
590//
591// final ITmfContext traceContext = expContext.getContexts()[trace];
592// final TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
593// // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
594// expLocation.getLocation().getLocations()[trace] = traceContext.getLocation().clone();
595//
596// // updateIndex(expContext, timestamp);
597//
598//// expLocation.getRanks()[trace] = traceContext.getRank();
599// expContext.setLastTrace(trace);
600// expContext.increaseRank();
601// event = expContext.getEvents()[trace];
602// fExperimentContext = expContext;
603// }
604//
605// // if (event != null) {
606// // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
607// // dumpContext(expContext, false);
608// // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
609// // }
610//
611// return event;
612// }
613
614// public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
615// // Build the index as we go along
616// final long rank = context.getRank();
617// if (context.hasValidRank() && (rank % fIndexPageSize) == 0) {
618// // Determine the table position
619// final long position = rank / fIndexPageSize;
620// // Add new entry at proper location (if empty)
621// if (fCheckpoints.size() == position) {
622// final ITmfLocation<?> location = context.getLocation().clone();
623// fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
624//// System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString());
625// }
626// }
627// }
a79913eb 628
ce2388e0
FC
629 /* (non-Javadoc)
630 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
a79913eb 631 */
0316808c
FC
632 @SuppressWarnings("unchecked")
633 @Override
a87cc4ef 634 public T parseEvent(ITmfContext context) {
a79913eb
FC
635
636 // Validate the context
cbdacf03 637 if (!(context instanceof TmfExperimentContext))
a79913eb 638 return null; // Throw an exception?
a79913eb 639
a87cc4ef
FC
640// if (!context.equals(fExperimentContext))
641// // Tracer.trace("Ctx: Restoring context");
642// fExperimentContext = seekEvent(context.getLocation());
a79913eb 643
a87cc4ef 644 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 645
a87cc4ef 646 // If an event was consumed previously, first get the next one from that trace
cbdacf03 647 final int lastTrace = expContext.getLastTrace();
a79913eb 648 if (lastTrace != TmfExperimentContext.NO_TRACE) {
cbdacf03 649 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
0316808c 650 expContext.getEvents()[lastTrace] = fTraces[lastTrace].readNextEvent(traceContext);
a79913eb 651 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
0316808c 652// fExperimentContext = (TmfExperimentContext) context;
a79913eb
FC
653 }
654
655 // Scan the candidate events and identify the "next" trace to read from
656 int trace = TmfExperimentContext.NO_TRACE;
a4115405 657 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
0316808c 658 for (int i = 0; i < fTraces.length; i++) {
cbdacf03 659 final ITmfEvent event = expContext.getEvents()[i];
a79913eb 660 if (event != null && event.getTimestamp() != null) {
cbdacf03 661 final ITmfTimestamp otherTS = event.getTimestamp();
a79913eb
FC
662 if (otherTS.compareTo(timestamp, true) < 0) {
663 trace = i;
664 timestamp = otherTS;
665 }
666 }
667 }
a87cc4ef
FC
668
669 T event = null;
670 if (trace != TmfExperimentContext.NO_TRACE)
671 event = (T) expContext.getEvents()[trace];
672
673// TmfExperimentLocation location = (TmfExperimentLocation) fExperimentContext.getLocation();
674// location.getLocation().getLocations()[trace] = fTraces[trace].getCurrentLocation();
675 expContext.setLastTrace(trace);
676 return event;
0316808c
FC
677
678// // Scan the candidate events and identify the "next" trace to read from
679// final ITmfEvent eventArray[] = expContext.getEvents();
680// if (eventArray == null)
681// return null;
682// int trace = TmfExperimentContext.NO_TRACE;
683// ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
684// if (eventArray.length == 1) {
685// if (eventArray[0] != null) {
686// timestamp = eventArray[0].getTimestamp();
687// trace = 0;
688// }
689// } else
690// for (int i = 0; i < eventArray.length; i++) {
691// final ITmfEvent event = eventArray[i];
692// if (event != null && event.getTimestamp() != null) {
693// final ITmfTimestamp otherTS = event.getTimestamp();
694// if (otherTS.compareTo(timestamp, true) < 0) {
695// trace = i;
696// timestamp = otherTS;
697// }
698// }
699// }
700//
701// // Update the experiment context and set the "next" event
702// ITmfEvent event = null;
703// if (trace != TmfExperimentContext.NO_TRACE) {
704// updateIndex(expContext, timestamp);
705//
706// final ITmfContext traceContext = expContext.getContexts()[trace];
707// final TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
708// // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
709// expLocation.getLocation().getLocations()[trace] = traceContext.getLocation().clone();
710//
711// // updateIndex(expContext, timestamp);
712//
713//// expLocation.getRanks()[trace] = traceContext.getRank();
714// expContext.setLastTrace(trace);
715// expContext.increaseRank();
716// event = expContext.getEvents()[trace];
717// fExperimentContext = expContext;
718// }
719//
720// // if (event != null) {
721// // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
722// // dumpContext(expContext, false);
723// // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
724// // }
725//
726// return event;
727
a79913eb
FC
728 }
729
bcbea6a6 730 /* (non-Javadoc)
a79913eb
FC
731 * @see java.lang.Object#toString()
732 */
733 @Override
3b38ea61 734 @SuppressWarnings("nls")
a79913eb
FC
735 public String toString() {
736 return "[TmfExperiment (" + getName() + ")]";
737 }
8c8bf09f
ASL
738
739 // ------------------------------------------------------------------------
740 // Indexing
741 // ------------------------------------------------------------------------
742
1b70b6dc 743 private synchronized void initializeStreamingMonitor() {
cbdacf03 744 if (fInitialized)
828e5592 745 return;
828e5592
PT
746 fInitialized = true;
747
1b70b6dc 748 if (getStreamingInterval() == 0) {
0316808c 749 final ITmfContext context = seekEvent(0);
cbdacf03
FC
750 final ITmfEvent event = getNext(context);
751 if (event == null)
1b70b6dc 752 return;
cbdacf03 753 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp().clone(), TmfTimestamp.BIG_CRUNCH);
828e5592
PT
754 final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
755
756 // Broadcast in separate thread to prevent deadlock
757 new Thread() {
758 @Override
759 public void run() {
760 broadcast(signal);
761 }
762 }.start();
1b70b6dc
PT
763 return;
764 }
765
bcbea6a6
FC
766 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { ////$NON-NLS-1$
767 private ITmfTimestamp safeTimestamp = null;
768 private TmfTimeRange timeRange = null;
1b70b6dc
PT
769
770 @Override
771 public void run() {
772 while (!fExecutor.isShutdown()) {
773 if (!isIndexingBusy()) {
a4115405
FC
774 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
775 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
cbdacf03
FC
776 for (final ITmfTrace<T> trace : fTraces) {
777 if (trace.getStartTime().compareTo(startTimestamp) < 0)
1b70b6dc 778 startTimestamp = trace.getStartTime();
cbdacf03 779 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0)
1b70b6dc 780 endTimestamp = trace.getEndTime();
1b70b6dc 781 }
cbdacf03 782 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0)
1b70b6dc 783 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
cbdacf03 784 else
1b70b6dc 785 timeRange = null;
1b70b6dc
PT
786 safeTimestamp = endTimestamp;
787 if (timeRange != null) {
cbdacf03 788 final TmfExperimentRangeUpdatedSignal signal =
1b70b6dc
PT
789 new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
790 broadcast(signal);
791 }
792 }
793 try {
794 Thread.sleep(getStreamingInterval());
cbdacf03 795 } catch (final InterruptedException e) {
1b70b6dc
PT
796 e.printStackTrace();
797 }
798 }
799 }
800 };
801 thread.start();
802 }
803
cbdacf03
FC
804 /*
805 * (non-Javadoc)
806 *
1b70b6dc
PT
807 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
808 */
809 @Override
810 public long getStreamingInterval() {
811 long interval = 0;
cbdacf03 812 for (final ITmfTrace<T> trace : fTraces)
1b70b6dc 813 interval = Math.max(interval, trace.getStreamingInterval());
1b70b6dc
PT
814 return interval;
815 }
816
a79913eb 817 /*
cbdacf03
FC
818 * The experiment holds the globally ordered events of its set of traces. It
819 * is expected to provide access to each individual event by index i.e. it
820 * must be possible to request the Nth event of the experiment.
a79913eb 821 *
cbdacf03
FC
822 * The purpose of the index is to keep the information needed to rapidly
823 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
824 * event).
a79913eb 825 */
8c8bf09f 826
a79913eb
FC
827 // The index page size
828 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
a87cc4ef 829// protected int fIndexPageSize;
a79913eb 830 protected boolean fIndexing = false;
a4115405 831 protected TmfTimeRange fIndexingPendingRange = TmfTimeRange.NULL_RANGE;
e31e01e8 832
1b70b6dc
PT
833 private Integer fEndSynchReference;
834
ce2388e0
FC
835 // private static BufferedWriter fEventLog = null;
836 // private static BufferedWriter openLogFile(String filename) {
837 // BufferedWriter outfile = null;
838 // try {
839 // outfile = new BufferedWriter(new FileWriter(filename));
840 // } catch (IOException e) {
841 // e.printStackTrace();
842 // }
843 // return outfile;
844 // }
9b635e61 845
a79913eb 846 protected boolean isIndexingBusy() {
a87cc4ef 847// synchronized (fCheckpoints) {
a79913eb 848 return fIndexing;
a87cc4ef 849// }
a79913eb
FC
850 }
851
0316808c
FC
852// @SuppressWarnings("unchecked")
853// private void indexExperiment(final boolean waitForCompletion, final int index, final TmfTimeRange timeRange) {
854//
855// synchronized (fCheckpoints) {
856// if (fIndexing)
857// return;
858// fIndexing = true;
859// }
860//
861// final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
862//
863// @Override
864// protected IStatus run(final IProgressMonitor monitor) {
865// while (!monitor.isCanceled())
866// try {
867// Thread.sleep(100);
868// } catch (final InterruptedException e) {
869// return Status.OK_STATUS;
870// }
871// monitor.done();
872// return Status.OK_STATUS;
873// }
874// };
875// job.schedule();
876//
877// // fEventLog = openLogFile("TraceEvent.log");
878// // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
879//
880// final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, timeRange, index,
881// TmfDataRequest.ALL_DATA,
882// fIndexPageSize, ITmfDataRequest.ExecutionType.BACKGROUND) { // PATA
883// // FOREGROUND
884//
885//// long indexingStart = System.nanoTime();
886//
887// ITmfTimestamp startTime = (fTimeRange == TmfTimeRange.NULL_RANGE) ? null : fTimeRange.getStartTime();
888// ITmfTimestamp lastTime = (fTimeRange == TmfTimeRange.NULL_RANGE) ? null : fTimeRange.getEndTime();
889// long initialNbEvents = fNbEvents;
890//
891// @Override
892// public void handleStarted() {
893// super.handleStarted();
894// }
895//
896// @Override
897// public void handleData(final ITmfEvent event) {
898// super.handleData(event);
899// if (event != null) {
900// final ITmfTimestamp ts = event.getTimestamp();
901// if (startTime == null)
902// startTime = ts.clone();
903// lastTime = ts.clone();
904// }
905// if ((getNbRead() % fIndexPageSize) == 1 && getNbRead() != 1) {
906// updateExperiment();
907// }
908// }
909//
910// @Override
911// public void handleSuccess() {
912//// long indexingEnd = System.nanoTime();
913//
914// // if the end time is a real value then it is the streaming safe
915// // time stamp
916// // set the last time to the safe time stamp to prevent
917// // unnecessary indexing requests
918// if (getRange().getEndTime() != TmfTimestamp.BIG_CRUNCH)
919// lastTime = getRange().getEndTime();
920// updateExperiment();
921// ///////////////////////////////////////////////////////////////////
922//// System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
923//// long average = (indexingEnd - indexingStart) / fNbEvents;
924//// System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
925//// + (indexingEnd * 1.0 - indexingStart) / 1000000000);
926//// System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
927//// + (average % 1000) + " us/evt)");
928// ///////////////////////////////////////////////////////////////////
929// super.handleSuccess();
930// }
931//
932// @Override
933// public void handleCompleted() {
934// job.cancel();
935// super.handleCompleted();
936// synchronized (fCheckpoints) {
937// fIndexing = false;
938// if (fIndexingPendingRange != TmfTimeRange.NULL_RANGE) {
939// indexExperiment(false, (int) fNbEvents, fIndexingPendingRange);
940// fIndexingPendingRange = TmfTimeRange.NULL_RANGE;
941// }
942// }
943// }
944//
945// private void updateExperiment() {
946// final int nbRead = getNbRead();
947// if (startTime != null)
948// fTimeRange = new TmfTimeRange(startTime, lastTime.clone());
949// if (nbRead != 0) {
950// // updateTimeRange();
951// // updateNbEvents();
952// fNbEvents = initialNbEvents + nbRead;
953// notifyListeners();
954// }
955// }
956// };
957//
958// sendRequest((ITmfDataRequest<T>) request);
959// if (waitForCompletion)
960// try {
961// request.waitForCompletion();
962// } catch (final InterruptedException e) {
963// e.printStackTrace();
964// }
965// }
a79913eb
FC
966
967 protected void notifyListeners() {
968 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
cbdacf03
FC
969 // broadcast(new TmfExperimentRangeUpdatedSignal(this, this,
970 // fTimeRange)); // , null));
a79913eb
FC
971 }
972
8c8bf09f
ASL
973 // ------------------------------------------------------------------------
974 // Signal handlers
975 // ------------------------------------------------------------------------
976
977 @TmfSignalHandler
cbdacf03
FC
978 public void experimentSelected(final TmfExperimentSelectedSignal<T> signal) {
979 final TmfExperiment<?> experiment = signal.getExperiment();
a79913eb
FC
980 if (experiment == this) {
981 setCurrentExperiment(experiment);
6e85c58d 982 fEndSynchReference = Integer.valueOf(signal.getReference());
a87cc4ef
FC
983// fCheckpoints.clear();
984// fNbEvents = 0;
a79913eb 985 }
8c8bf09f
ASL
986 }
987
1b70b6dc 988 @TmfSignalHandler
cbdacf03 989 public void endSync(final TmfEndSynchSignal signal) {
1b70b6dc
PT
990 if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
991 fEndSynchReference = null;
992 initializeStreamingMonitor();
993 }
1b70b6dc
PT
994 }
995
8c8bf09f 996 @TmfSignalHandler
cbdacf03 997 public void experimentUpdated(final TmfExperimentUpdatedSignal signal) {
8c8bf09f
ASL
998 }
999
1b70b6dc 1000 @TmfSignalHandler
cbdacf03 1001 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
0316808c
FC
1002 if (signal.getExperiment() == this) {
1003// indexExperiment(false, (int) fNbEvents, signal.getRange());
1004 getIndexer().buildIndex(false);
1005 }
1b70b6dc
PT
1006 }
1007
0316808c
FC
1008// @TmfSignalHandler
1009// public void traceUpdated(final TmfTraceUpdatedSignal signal) {
1010// for (final ITmfTrace<T> trace : fTraces)
1011// if (trace == signal.getTrace()) {
1012// synchronized (fCheckpoints) {
1013// if (fIndexing) {
1014// if (fIndexingPendingRange == TmfTimeRange.NULL_RANGE)
1015// fIndexingPendingRange = signal.getRange();
1016// else {
1017// ITmfTimestamp startTime = fIndexingPendingRange.getStartTime();
1018// ITmfTimestamp endTime = fIndexingPendingRange.getEndTime();
1019// if (signal.getRange().getStartTime().compareTo(startTime) < 0)
1020// startTime = signal.getRange().getStartTime();
1021// if (signal.getRange().getEndTime().compareTo(endTime) > 0)
1022// endTime = signal.getRange().getEndTime();
1023// fIndexingPendingRange = new TmfTimeRange(startTime, endTime);
1024// }
1025// return;
1026// }
1027// }
1028// indexExperiment(false, (int) fNbEvents, signal.getRange());
1029// return;
1030// }
1031// }
8c8bf09f 1032
12c155f5
FC
1033 @Override
1034 public String getPath() {
1035 // TODO Auto-generated method stub
1036 return null;
1037 }
1038
828e5592 1039 /**
a1091415 1040 * Set the file to be used for bookmarks on this experiment
cbdacf03 1041 *
a1091415 1042 * @param file the bookmarks file
828e5592 1043 */
cbdacf03 1044 public void setBookmarksFile(final IFile file) {
a1091415
PT
1045 fBookmarksFile = file;
1046 }
1047
1048 /**
1049 * Get the file used for bookmarks on this experiment
cbdacf03 1050 *
a1091415
PT
1051 * @return the bookmarks file or null if none is set
1052 */
1053 public IFile getBookmarksFile() {
1054 return fBookmarksFile;
1055 }
1056
cbdacf03
FC
1057 /*
1058 * (non-Javadoc)
1059 *
a1091415 1060 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
828e5592 1061 */
a1091415 1062 @Override
828e5592
PT
1063 public IResource getResource() {
1064 return fResource;
1065 }
4dc47e28 1066}
This page took 0.097235 seconds and 5 git commands to generate.