Refactor TmfTrace and TmfExperiment
[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
828e5592
PT
55 // Flag to initialize only once
56 private boolean fInitialized = false;
57
a1091415
PT
58 // The experiment bookmarks file
59 private IFile fBookmarksFile;
60
07671572
FC
61// // The properties resource
62// private IResource fResource;
828e5592 63
8c8bf09f
ASL
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
12c155f5 68 @Override
cbdacf03 69 public boolean validate(final IProject project, final String path) {
12c155f5
FC
70 return true;
71 }
72
73 @Override
07671572
FC
74 public void initTrace(final IResource resource, final String path, final Class<T> type) {
75// fResource = resource;
76 try {
77 super.initTrace(resource, path, type);
78 } catch (TmfTraceException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
96c6806f
PT
82 }
83
8c8bf09f
ASL
84 /**
85 * @param type
86 * @param id
87 * @param traces
88 * @param epoch
89 * @param indexPageSize
0316808c 90 * @throws TmfTraceException
8c8bf09f 91 */
cbdacf03 92 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
0316808c
FC
93 final int indexPageSize)
94 {
a4115405 95 this(type, id, traces, TmfTimestamp.ZERO, indexPageSize, false);
a79913eb 96 }
cb866e08 97
0316808c
FC
98 @SuppressWarnings({ "unchecked", "rawtypes" })
99 public TmfExperiment(final Class<T> type, final String path, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
100 final int indexPageSize, final boolean preIndexExperiment)
101 {
102 setCacheSize(indexPageSize);
103 setStreamingInterval(0);
07671572 104 setIndexer(new TmfCheckpointIndexer(this, indexPageSize));
0316808c
FC
105 setParser(this);
106 try {
107 super.initialize(null, path, type);
108 } catch (TmfTraceException e) {
109 e.printStackTrace();
110 }
8c8bf09f 111
a79913eb 112 fTraces = traces;
a87cc4ef 113 setTimeRange(TmfTimeRange.NULL_RANGE);
8c8bf09f 114
a87cc4ef
FC
115 if (preIndexExperiment) {
116 getIndexer().buildIndex(true);
a87cc4ef 117 }
a79913eb 118 }
8c8bf09f
ASL
119
120 /**
121 * @param type
122 * @param id
123 * @param traces
0316808c 124 * @throws TmfTraceException
8c8bf09f 125 */
cbdacf03 126 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces) {
a4115405 127 this(type, id, traces, TmfTimestamp.ZERO, DEFAULT_INDEX_PAGE_SIZE);
8c8bf09f
ASL
128 }
129
130 /**
131 * @param type
132 * @param id
133 * @param traces
134 * @param indexPageSize
0316808c 135 * @throws TmfTraceException
8c8bf09f 136 */
cbdacf03 137 public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final int indexPageSize) {
a4115405 138 this(type, id, traces, TmfTimestamp.ZERO, indexPageSize);
8c8bf09f 139 }
a79913eb 140
8c8bf09f 141 /**
ff4ed569 142 * Clears the experiment
8c8bf09f
ASL
143 */
144 @Override
12c155f5 145 @SuppressWarnings("rawtypes")
a79913eb
FC
146 public synchronized void dispose() {
147
cbdacf03 148 final TmfExperimentDisposedSignal<T> signal = new TmfExperimentDisposedSignal<T>(this, this);
a79913eb 149 broadcast(signal);
cbdacf03 150 if (fCurrentExperiment == this)
09d11238 151 fCurrentExperiment = null;
a79913eb
FC
152
153 if (fTraces != null) {
cbdacf03 154 for (final ITmfTrace trace : fTraces)
a79913eb 155 trace.dispose();
a79913eb
FC
156 fTraces = null;
157 }
2fb2eb37 158 super.dispose();
8c8bf09f
ASL
159 }
160
161 // ------------------------------------------------------------------------
e31e01e8 162 // Accessors
8c8bf09f
ASL
163 // ------------------------------------------------------------------------
164
cbdacf03
FC
165 public static void setCurrentExperiment(final TmfExperiment<?> experiment) {
166 if (fCurrentExperiment != null && fCurrentExperiment != experiment)
09d11238 167 fCurrentExperiment.dispose();
a79913eb 168 fCurrentExperiment = experiment;
f6b14ce2
FC
169 }
170
e31e01e8 171 public static TmfExperiment<?> getCurrentExperiment() {
a79913eb 172 return fCurrentExperiment;
8c8bf09f
ASL
173 }
174
12c155f5 175 public ITmfTrace<T>[] getTraces() {
a79913eb 176 return fTraces;
8c8bf09f
ASL
177 }
178
8c8bf09f 179 /**
cbdacf03
FC
180 * Returns the timestamp of the event at the requested index. If none,
181 * returns null.
a79913eb 182 *
0d9a6d76
FC
183 * @param index the event index (rank)
184 * @return the corresponding event timestamp
8c8bf09f 185 */
cbdacf03 186 public ITmfTimestamp getTimestamp(final int index) {
0316808c 187 final ITmfContext context = seekEvent(index);
b4f71e4a 188 final ITmfEvent event = readNextEvent(context);
a79913eb 189 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
190 }
191
8c8bf09f
ASL
192 // ------------------------------------------------------------------------
193 // TmfProvider
194 // ------------------------------------------------------------------------
a79913eb 195
a79913eb 196 @Override
cbdacf03 197 public ITmfContext armRequest(final ITmfDataRequest<T> request) {
ce2388e0 198 ITmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? ((ITmfEventRequest<T>) request).getRange().getStartTime() : null;
07671572
FC
199 if (TmfTimestamp.BIG_BANG.equals(timestamp) || request.getIndex() > 0) {
200 timestamp = null;
201 }
202
0316808c 203 ITmfContext context = null;
07671572 204 if (timestamp != null) { // Seek by timestamp
a79913eb
FC
205 context = seekEvent(timestamp);
206 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
07671572
FC
207 } else { // Seek by rank
208 context = seekEvent(request.getIndex());
209 }
210
a79913eb
FC
211 return context;
212 }
213
a79913eb 214 // ------------------------------------------------------------------------
9f584e4c
FC
215 // ITmfTrace trace positioning
216 // ------------------------------------------------------------------------
217
a79913eb
FC
218 // Returns a brand new context based on the location provided
219 // and initializes the event queues
220 @Override
7e6347b0 221 public synchronized TmfExperimentContext seekEvent(final ITmfLocation<?> location) {
a79913eb 222 // Validate the location
cbdacf03 223 if (location != null && !(location instanceof TmfExperimentLocation))
a79913eb 224 return null; // Throw an exception?
8f50c396 225
cbdacf03 226 if (fTraces == null)
a79913eb 227 return null;
8f50c396 228
a79913eb 229 // Instantiate the location
cbdacf03 230 final TmfExperimentLocation expLocation = (location == null) ? new TmfExperimentLocation(new TmfLocationArray(
0316808c 231 new ITmfLocation<?>[fTraces.length])) : (TmfExperimentLocation) location.clone();
8f50c396 232
a79913eb 233 // Create and populate the context's traces contexts
0316808c 234 final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
9b635e61 235
a79913eb
FC
236 for (int i = 0; i < fTraces.length; i++) {
237 // Get the relevant trace attributes
0316808c 238 final ITmfLocation<?> traceLocation = expLocation.getLocation().getLocations()[i];
7e6347b0 239 context.getContexts()[i] = fTraces[i].seekEvent(traceLocation);
0316808c 240 expLocation.getLocation().getLocations()[i] = context.getContexts()[i].getLocation().clone();
b4f71e4a 241 context.getEvents()[i] = fTraces[i].readNextEvent(context.getContexts()[i]);
a79913eb 242 }
8f50c396 243
a79913eb
FC
244 // Finalize context
245 context.setLocation(expLocation);
246 context.setLastTrace(TmfExperimentContext.NO_TRACE);
0316808c 247 context.setRank(ITmfContext.UNKNOWN_RANK);
a79913eb
FC
248 return context;
249 }
9f584e4c 250
c76c54bb 251 @Override
0316808c
FC
252 public ITmfContext seekEvent(final double ratio) {
253 final ITmfContext context = seekEvent((long) (ratio * getNbEvents()));
c76c54bb
FC
254 return context;
255 }
256
a79913eb 257 @Override
cbdacf03
FC
258 public double getLocationRatio(final ITmfLocation<?> location) {
259 if (location instanceof TmfExperimentLocation)
7e6347b0 260 return (double) seekEvent(location).getRank() / getNbEvents();
c76c54bb
FC
261 return 0;
262 }
263
a79913eb
FC
264 @Override
265 public ITmfLocation<?> getCurrentLocation() {
a87cc4ef
FC
266 ITmfLocation<?>[] locations = new ITmfLocation<?>[fTraces.length];
267 for (int i = 0; i < fTraces.length; i++) {
268 locations[i] = fTraces[i].getCurrentLocation();
269 }
270 return new TmfExperimentLocation(new TmfLocationArray(locations));
a79913eb 271 }
c76c54bb 272
07671572
FC
273 /* (non-Javadoc)
274 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
275 */
276 @Override
277 public synchronized ITmfEvent readNextEvent(final ITmfContext context) {
278 // parseEvent() does not update the context
279 final ITmfEvent event = parseEvent(context);
280 if (event != null) {
281 updateAttributes(context, event.getTimestamp());
282
283 TmfExperimentContext expContext = (TmfExperimentContext) context;
284 int trace = expContext.getLastTrace();
285 if (trace != TmfExperimentContext.NO_TRACE) {
286 TmfExperimentLocation location = (TmfExperimentLocation) expContext.getLocation();
287 location.getLocation().getLocations()[trace] = expContext.getContexts()[trace].getLocation();
288 }
289
290 context.increaseRank();
291 processEvent(event);
292 }
293 return event;
294 }
a79913eb 295
ce2388e0
FC
296 /* (non-Javadoc)
297 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
a79913eb 298 */
0316808c
FC
299 @SuppressWarnings("unchecked")
300 @Override
a87cc4ef 301 public T parseEvent(ITmfContext context) {
a79913eb
FC
302
303 // Validate the context
cbdacf03 304 if (!(context instanceof TmfExperimentContext))
a79913eb 305 return null; // Throw an exception?
a79913eb 306
a87cc4ef 307 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 308
a87cc4ef 309 // If an event was consumed previously, first get the next one from that trace
cbdacf03 310 final int lastTrace = expContext.getLastTrace();
a79913eb 311 if (lastTrace != TmfExperimentContext.NO_TRACE) {
cbdacf03 312 final ITmfContext traceContext = expContext.getContexts()[lastTrace];
0316808c 313 expContext.getEvents()[lastTrace] = fTraces[lastTrace].readNextEvent(traceContext);
a79913eb 314 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
315 }
316
317 // Scan the candidate events and identify the "next" trace to read from
318 int trace = TmfExperimentContext.NO_TRACE;
a4115405 319 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
0316808c 320 for (int i = 0; i < fTraces.length; i++) {
cbdacf03 321 final ITmfEvent event = expContext.getEvents()[i];
a79913eb 322 if (event != null && event.getTimestamp() != null) {
cbdacf03 323 final ITmfTimestamp otherTS = event.getTimestamp();
a79913eb
FC
324 if (otherTS.compareTo(timestamp, true) < 0) {
325 trace = i;
326 timestamp = otherTS;
327 }
328 }
329 }
a87cc4ef
FC
330
331 T event = null;
07671572 332 if (trace != TmfExperimentContext.NO_TRACE) {
a87cc4ef 333 event = (T) expContext.getEvents()[trace];
07671572 334 }
a87cc4ef 335
a87cc4ef
FC
336 expContext.setLastTrace(trace);
337 return event;
a79913eb
FC
338 }
339
bcbea6a6 340 /* (non-Javadoc)
a79913eb
FC
341 * @see java.lang.Object#toString()
342 */
343 @Override
3b38ea61 344 @SuppressWarnings("nls")
a79913eb
FC
345 public String toString() {
346 return "[TmfExperiment (" + getName() + ")]";
347 }
8c8bf09f
ASL
348
349 // ------------------------------------------------------------------------
350 // Indexing
351 // ------------------------------------------------------------------------
352
1b70b6dc 353 private synchronized void initializeStreamingMonitor() {
cbdacf03 354 if (fInitialized)
828e5592 355 return;
828e5592
PT
356 fInitialized = true;
357
1b70b6dc 358 if (getStreamingInterval() == 0) {
0316808c 359 final ITmfContext context = seekEvent(0);
cbdacf03
FC
360 final ITmfEvent event = getNext(context);
361 if (event == null)
1b70b6dc 362 return;
cbdacf03 363 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp().clone(), TmfTimestamp.BIG_CRUNCH);
828e5592
PT
364 final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
365
366 // Broadcast in separate thread to prevent deadlock
367 new Thread() {
368 @Override
369 public void run() {
370 broadcast(signal);
371 }
372 }.start();
1b70b6dc
PT
373 return;
374 }
375
bcbea6a6
FC
376 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { ////$NON-NLS-1$
377 private ITmfTimestamp safeTimestamp = null;
378 private TmfTimeRange timeRange = null;
1b70b6dc
PT
379
380 @Override
381 public void run() {
382 while (!fExecutor.isShutdown()) {
383 if (!isIndexingBusy()) {
a4115405
FC
384 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
385 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
cbdacf03
FC
386 for (final ITmfTrace<T> trace : fTraces) {
387 if (trace.getStartTime().compareTo(startTimestamp) < 0)
1b70b6dc 388 startTimestamp = trace.getStartTime();
cbdacf03 389 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0)
1b70b6dc 390 endTimestamp = trace.getEndTime();
1b70b6dc 391 }
cbdacf03 392 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0)
1b70b6dc 393 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
cbdacf03 394 else
1b70b6dc 395 timeRange = null;
1b70b6dc
PT
396 safeTimestamp = endTimestamp;
397 if (timeRange != null) {
cbdacf03 398 final TmfExperimentRangeUpdatedSignal signal =
1b70b6dc
PT
399 new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
400 broadcast(signal);
401 }
402 }
403 try {
404 Thread.sleep(getStreamingInterval());
cbdacf03 405 } catch (final InterruptedException e) {
1b70b6dc
PT
406 e.printStackTrace();
407 }
408 }
409 }
410 };
411 thread.start();
412 }
413
cbdacf03
FC
414 /*
415 * (non-Javadoc)
416 *
1b70b6dc
PT
417 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
418 */
419 @Override
420 public long getStreamingInterval() {
421 long interval = 0;
cbdacf03 422 for (final ITmfTrace<T> trace : fTraces)
1b70b6dc 423 interval = Math.max(interval, trace.getStreamingInterval());
1b70b6dc
PT
424 return interval;
425 }
426
a79913eb 427 /*
cbdacf03
FC
428 * The experiment holds the globally ordered events of its set of traces. It
429 * is expected to provide access to each individual event by index i.e. it
430 * must be possible to request the Nth event of the experiment.
a79913eb 431 *
cbdacf03
FC
432 * The purpose of the index is to keep the information needed to rapidly
433 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
434 * event).
a79913eb 435 */
8c8bf09f 436
a79913eb
FC
437 // The index page size
438 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
a87cc4ef 439// protected int fIndexPageSize;
a79913eb 440 protected boolean fIndexing = false;
a4115405 441 protected TmfTimeRange fIndexingPendingRange = TmfTimeRange.NULL_RANGE;
e31e01e8 442
1b70b6dc
PT
443 private Integer fEndSynchReference;
444
a79913eb 445 protected boolean isIndexingBusy() {
07671572 446 return fIndexing;
a79913eb
FC
447 }
448
a79913eb
FC
449
450 protected void notifyListeners() {
451 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
452 }
453
8c8bf09f
ASL
454 // ------------------------------------------------------------------------
455 // Signal handlers
456 // ------------------------------------------------------------------------
457
458 @TmfSignalHandler
cbdacf03
FC
459 public void experimentSelected(final TmfExperimentSelectedSignal<T> signal) {
460 final TmfExperiment<?> experiment = signal.getExperiment();
a79913eb
FC
461 if (experiment == this) {
462 setCurrentExperiment(experiment);
6e85c58d 463 fEndSynchReference = Integer.valueOf(signal.getReference());
a79913eb 464 }
8c8bf09f
ASL
465 }
466
1b70b6dc 467 @TmfSignalHandler
cbdacf03 468 public void endSync(final TmfEndSynchSignal signal) {
1b70b6dc
PT
469 if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
470 fEndSynchReference = null;
471 initializeStreamingMonitor();
472 }
1b70b6dc
PT
473 }
474
8c8bf09f 475 @TmfSignalHandler
cbdacf03 476 public void experimentUpdated(final TmfExperimentUpdatedSignal signal) {
8c8bf09f
ASL
477 }
478
1b70b6dc 479 @TmfSignalHandler
cbdacf03 480 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
0316808c 481 if (signal.getExperiment() == this) {
0316808c
FC
482 getIndexer().buildIndex(false);
483 }
1b70b6dc
PT
484 }
485
12c155f5
FC
486 @Override
487 public String getPath() {
488 // TODO Auto-generated method stub
489 return null;
490 }
491
828e5592 492 /**
a1091415 493 * Set the file to be used for bookmarks on this experiment
cbdacf03 494 *
a1091415 495 * @param file the bookmarks file
828e5592 496 */
cbdacf03 497 public void setBookmarksFile(final IFile file) {
a1091415
PT
498 fBookmarksFile = file;
499 }
500
501 /**
502 * Get the file used for bookmarks on this experiment
cbdacf03 503 *
a1091415
PT
504 * @return the bookmarks file or null if none is set
505 */
506 public IFile getBookmarksFile() {
507 return fBookmarksFile;
508 }
509
07671572
FC
510// /*
511// * (non-Javadoc)
512// *
513// * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
514// */
515// @Override
516// public IResource getResource() {
517// return fResource;
518// }
4dc47e28 519}
07671572
FC
520
521
522///*******************************************************************************
523// * Copyright (c) 2009, 2010 Ericsson
524// *
525// * All rights reserved. This program and the accompanying materials are
526// * made available under the terms of the Eclipse Public License v1.0 which
527// * accompanies this distribution, and is available at
528// * http://www.eclipse.org/legal/epl-v10.html
529// *
530// * Contributors:
531// * Francois Chouinard - Initial API and implementation
532// *******************************************************************************/
533//
534//package org.eclipse.linuxtools.tmf.core.experiment;
535//
536//import java.util.Collections;
537//import java.util.Vector;
538//
539//import org.eclipse.core.resources.IFile;
540//import org.eclipse.core.resources.IProject;
541//import org.eclipse.core.resources.IResource;
542//import org.eclipse.core.runtime.IProgressMonitor;
543//import org.eclipse.core.runtime.IStatus;
544//import org.eclipse.core.runtime.Status;
545//import org.eclipse.core.runtime.jobs.Job;
546//import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
547//import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
548//import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
549//import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
550//import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
551//import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
552//import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
553//import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
554//import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
555//import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
556//import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentDisposedSignal;
557//import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
558//import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
559//import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
560//import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
561//import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
562//import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
563//import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
564//import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
565//import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
566//import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
567//
568///**
569// * <b><u>TmfExperiment</u></b>
570// * <p>
571// * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces
572// * that are part of a tracing experiment.
573// * <p>
574// */
575//public class TmfExperiment<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> {
576//
577// // ------------------------------------------------------------------------
578// // Attributes
579// // ------------------------------------------------------------------------
580//
581// // The currently selected experiment
582// protected static TmfExperiment<?> fCurrentExperiment = null;
583//
584// // The set of traces that constitute the experiment
585// protected ITmfTrace<T>[] fTraces;
586//
587// // The total number of events
588// protected long fNbEvents;
589//
590// // The experiment time range
591// protected TmfTimeRange fTimeRange;
592//
593// // The experiment reference timestamp (default: ZERO)
594// protected ITmfTimestamp fEpoch;
595//
596// // The experiment index
597// protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
598//
599// // The current experiment context
600// protected TmfExperimentContext fExperimentContext;
601//
602// // Flag to initialize only once
603// private boolean fInitialized = false;
604//
605// // The experiment bookmarks file
606// private IFile fBookmarksFile;
607//
608// // The properties resource
609// private IResource fResource;
610//
611// // ------------------------------------------------------------------------
612// // Constructors
613// // ------------------------------------------------------------------------
614//
615// @Override
616// public TmfExperiment<T> clone() throws CloneNotSupportedException {
617// throw new CloneNotSupportedException();
618// }
619//
620// @Override
621// public boolean validate(final IProject project, final String path) {
622// return true;
623// }
624//
625// @Override
626// public void initTrace(final IResource resource, final String path, final Class<T> eventType) {
627// fResource = resource;
628// }
629//
630// /**
631// * @param type
632// * @param id
633// * @param traces
634// * @param epoch
635// * @param indexPageSize
636// */
637// public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
638// final int indexPageSize) {
639// this(type, id, traces, TmfTimestamp.ZERO, indexPageSize, false);
640// }
641//
642// public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final ITmfTimestamp epoch,
643// final int indexPageSize, final boolean preIndexExperiment) {
644// super(id, type);
645//
646// fTraces = traces;
647// fEpoch = epoch;
648// fIndexPageSize = indexPageSize;
649// fTimeRange = TmfTimeRange.NULL_RANGE;
650//
651// if (preIndexExperiment) {
652// indexExperiment(true, 0, TmfTimeRange.ETERNITY);
653// updateTimeRange();
654// }
655// }
656//
657// protected TmfExperiment(final String id, final Class<T> type) {
658// super(id, type);
659// }
660//
661// /**
662// * @param type
663// * @param id
664// * @param traces
665// */
666// public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces) {
667// this(type, id, traces, TmfTimestamp.ZERO, DEFAULT_INDEX_PAGE_SIZE);
668// }
669//
670// /**
671// * @param type
672// * @param id
673// * @param traces
674// * @param indexPageSize
675// */
676// public TmfExperiment(final Class<T> type, final String id, final ITmfTrace<T>[] traces, final int indexPageSize) {
677// this(type, id, traces, TmfTimestamp.ZERO, indexPageSize);
678// }
679//
680// /**
681// * Clears the experiment
682// */
683// @Override
684// @SuppressWarnings("rawtypes")
685// public synchronized void dispose() {
686//
687// final TmfExperimentDisposedSignal<T> signal = new TmfExperimentDisposedSignal<T>(this, this);
688// broadcast(signal);
689// if (fCurrentExperiment == this)
690// fCurrentExperiment = null;
691//
692// if (fTraces != null) {
693// for (final ITmfTrace trace : fTraces)
694// trace.dispose();
695// fTraces = null;
696// }
697// if (fCheckpoints != null)
698// fCheckpoints.clear();
699// super.dispose();
700// }
701//
702// // ------------------------------------------------------------------------
703// // ITmfTrace
704// // ------------------------------------------------------------------------
705//
706// @Override
707// public Class<T> getEventType() {
708// return fType;
709// }
710//
711// @Override
712// public long getNbEvents() {
713// return fNbEvents;
714// }
715//
716// @Override
717// public int getCacheSize() {
718// return fIndexPageSize;
719// }
720//
721// @Override
722// public TmfTimeRange getTimeRange() {
723// return fTimeRange;
724// }
725//
726// @Override
727// public ITmfTimestamp getStartTime() {
728// return fTimeRange.getStartTime();
729// }
730//
731// @Override
732// public ITmfTimestamp getEndTime() {
733// return fTimeRange.getEndTime();
734// }
735//
736// public Vector<TmfCheckpoint> getCheckpoints() {
737// return fCheckpoints;
738// }
739//
740// // ------------------------------------------------------------------------
741// // Accessors
742// // ------------------------------------------------------------------------
743//
744// public static void setCurrentExperiment(final TmfExperiment<?> experiment) {
745// if (fCurrentExperiment != null && fCurrentExperiment != experiment)
746// fCurrentExperiment.dispose();
747// fCurrentExperiment = experiment;
748// }
749//
750// public static TmfExperiment<?> getCurrentExperiment() {
751// return fCurrentExperiment;
752// }
753//
754// public ITmfTimestamp getEpoch() {
755// return fEpoch;
756// }
757//
758// public ITmfTrace<T>[] getTraces() {
759// return fTraces;
760// }
761//
762// /**
763// * Returns the timestamp of the event at the requested index. If none,
764// * returns null.
765// *
766// * @param index the event index (rank)
767// * @return the corresponding event timestamp
768// */
769// public ITmfTimestamp getTimestamp(final int index) {
770// final TmfExperimentContext context = seekEvent(index);
771// final ITmfEvent event = readNextEvent(context);
772// return (event != null) ? event.getTimestamp() : null;
773// }
774//
775// // ------------------------------------------------------------------------
776// // Operators
777// // ------------------------------------------------------------------------
778//
779// /**
780// * Update the global time range
781// */
782// protected void updateTimeRange() {
783// ITmfTimestamp startTime = fTimeRange != TmfTimeRange.NULL_RANGE ? fTimeRange.getStartTime() : TmfTimestamp.BIG_CRUNCH;
784// ITmfTimestamp endTime = fTimeRange != TmfTimeRange.NULL_RANGE ? fTimeRange.getEndTime() : TmfTimestamp.BIG_BANG;
785//
786// for (final ITmfTrace<T> trace : fTraces) {
787// final ITmfTimestamp traceStartTime = trace.getStartTime();
788// if (traceStartTime.compareTo(startTime, true) < 0)
789// startTime = traceStartTime;
790// final ITmfTimestamp traceEndTime = trace.getEndTime();
791// if (traceEndTime.compareTo(endTime, true) > 0)
792// endTime = traceEndTime;
793// }
794// fTimeRange = new TmfTimeRange(startTime, endTime);
795// }
796//
797// // ------------------------------------------------------------------------
798// // TmfProvider
799// // ------------------------------------------------------------------------
800//
801// @Override
802// public ITmfContext armRequest(final ITmfDataRequest<T> request) {
803// // Tracer.trace("Ctx: Arming request - start");
804// ITmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? ((ITmfEventRequest<T>) request).getRange().getStartTime() : null;
805// if (TmfTimestamp.BIG_BANG.equals(timestamp) || request.getIndex() > 0)
806// timestamp = null; // use request index
807// TmfExperimentContext context = null;
808// if (timestamp != null) {
809// // seek by timestamp
810// context = seekEvent(timestamp);
811// ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
812// } else // Seek by rank
813// if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex())
814// // We are already at the right context -> no need to seek
815// context = fExperimentContext;
816// else
817// context = seekEvent(request.getIndex());
818// // Tracer.trace("Ctx: Arming request - done");
819// return context;
820// }
821//
822// @Override
823// @SuppressWarnings("unchecked")
824// public T getNext(final ITmfContext context) {
825// if (context instanceof TmfExperimentContext)
826// return (T) readNextEvent(context);
827// return null;
828// }
829//
830// // ------------------------------------------------------------------------
831// // ITmfTrace trace positioning
832// // ------------------------------------------------------------------------
833//
834// // Returns a brand new context based on the location provided
835// // and initializes the event queues
836// @Override
837// public synchronized TmfExperimentContext seekEvent(final ITmfLocation<?> location) {
838// // Validate the location
839// if (location != null && !(location instanceof TmfExperimentLocation))
840// return null; // Throw an exception?
841//
842// if (fTraces == null)
843// return null;
844//
845// // Instantiate the location
846// final TmfExperimentLocation expLocation = (location == null) ? new TmfExperimentLocation(new TmfLocationArray(
847// new ITmfLocation<?>[fTraces.length]), new long[fTraces.length]) : (TmfExperimentLocation) location.clone();
848//
849// // Create and populate the context's traces contexts
850// final TmfExperimentContext context = new TmfExperimentContext(fTraces, new ITmfContext[fTraces.length]);
851// // Tracer.trace("Ctx: SeekLocation - start");
852//
853// long rank = 0;
854// for (int i = 0; i < fTraces.length; i++) {
855// // Get the relevant trace attributes
856// final ITmfLocation<?> traceLocation = expLocation.getLocation().locations[i];
857// final long traceRank = expLocation.getRanks()[i];
858//
859// // Set the corresponding sub-context
860// context.getContexts()[i] = fTraces[i].seekEvent(traceLocation);
861// context.getContexts()[i].setRank(traceRank);
862// rank += traceRank;
863//
864// // Set the trace location and read the corresponding event
865// /*
866// * The (TmfContext) cast should be safe since we created 'context'
867// * ourselves higher up.
868// */
869// expLocation.getLocation().locations[i] = context.getContexts()[i].getLocation().clone();
870// context.getEvents()[i] = fTraces[i].readNextEvent(context.getContexts()[i]);
871// }
872//
873// // Tracer.trace("Ctx: SeekLocation - done");
874//
875// // Finalize context
876// context.setLocation(expLocation);
877// context.setLastTrace(TmfExperimentContext.NO_TRACE);
878// context.setRank(rank);
879//
880// fExperimentContext = context;
881//
882// return context;
883// }
884//
885// /* (non-Javadoc)
886// * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
887// */
888// @Override
889// public synchronized TmfExperimentContext seekEvent(ITmfTimestamp timestamp) {
890//
891// // Tracer.trace("Ctx: seekEvent(TS) - start");
892//
893// if (timestamp == null)
894// timestamp = TmfTimestamp.BIG_BANG;
895//
896// // First, find the right checkpoint
897// int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
898//
899// // In the very likely case that the checkpoint was not found, bsearch
900// // returns its negated would-be location (not an offset...). From that
901// // index, we can then position the stream and get the event.
902// if (index < 0)
903// index = Math.max(0, -(index + 2));
904//
905// // Position the experiment at the checkpoint
906// ITmfLocation<?> location;
907// synchronized (fCheckpoints) {
908// if (fCheckpoints.size() > 0) {
909// if (index >= fCheckpoints.size())
910// index = fCheckpoints.size() - 1;
911// location = fCheckpoints.elementAt(index).getLocation();
912// } else
913// location = null;
914// }
915//
916// final TmfExperimentContext context = seekEvent(location);
917// context.setRank((long) index * fIndexPageSize);
918//
919// // And locate the event
920// ITmfEvent event = parseEvent(context);
921// while ((event != null) && (event.getTimestamp().compareTo(timestamp, false) < 0)) {
922// readNextEvent(context);
923// event = parseEvent(context);
924// }
925//
926// if (event == null) {
927// context.setLocation(null);
928// context.setRank(ITmfContext.UNKNOWN_RANK);
929// }
930//
931// return context;
932// }
933//
934// /* (non-Javadoc)
935// * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
936// */
937// @Override
938// public synchronized TmfExperimentContext seekEvent(final long rank) {
939//
940// // Tracer.trace("Ctx: seekEvent(rank) - start");
941//
942// // Position the stream at the previous checkpoint
943// int index = (int) rank / fIndexPageSize;
944// ITmfLocation<?> location;
945// synchronized (fCheckpoints) {
946// if (fCheckpoints.size() == 0)
947// location = null;
948// else {
949// if (index >= fCheckpoints.size())
950// index = fCheckpoints.size() - 1;
951// location = fCheckpoints.elementAt(index).getLocation();
952// }
953// }
954//
955// final TmfExperimentContext context = seekEvent(location);
956// context.setRank((long) index * fIndexPageSize);
957//
958// // And locate the event
959// ITmfEvent event = parseEvent(context);
960// long pos = context.getRank();
961// while ((event != null) && (pos++ < rank)) {
962// readNextEvent(context);
963// event = parseEvent(context);
964// }
965//
966// if (event == null) {
967// context.setLocation(null);
968// context.setRank(ITmfContext.UNKNOWN_RANK);
969// }
970//
971// return context;
972// }
973//
974// @Override
975// public TmfContext seekEvent(final double ratio) {
976// final TmfContext context = seekEvent((long) (ratio * getNbEvents()));
977// return context;
978// }
979//
980// @Override
981// public double getLocationRatio(final ITmfLocation<?> location) {
982// if (location instanceof TmfExperimentLocation)
983// return (double) seekEvent(location).getRank() / getNbEvents();
984// return 0;
985// }
986//
987// @Override
988// public ITmfLocation<?> getCurrentLocation() {
989// if (fExperimentContext != null)
990// return fExperimentContext.getLocation();
991// return null;
992// }
993//
994// // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
995// // TmfContext context0 = context.getContexts()[0];
996// // TmfEvent event0 = context.getEvents()[0];
997// // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
998// // long rank0 = context.getRank();
999// // int trace = context.getLastTrace();
1000// //
1001// // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
1002// //
1003// // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
1004// // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
1005// // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
1006// // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
1007// // Tracer.trace(result.toString());
1008// // }
1009//
1010// /**
1011// * Scan the next events from all traces and return the next one in
1012// * chronological order.
1013// *
1014// * @param context the trace context
1015// * @return the next event
1016// */
1017// @Override
1018// public synchronized ITmfEvent readNextEvent(final ITmfContext context) {
1019//
1020// // Validate the context
1021// if (!(context instanceof TmfExperimentContext))
1022// return null; // Throw an exception?
1023//
1024// if (!context.equals(fExperimentContext))
1025// // Tracer.trace("Ctx: Restoring context");
1026// fExperimentContext = seekEvent(context.getLocation());
1027//
1028// final TmfExperimentContext expContext = (TmfExperimentContext) context;
1029//
1030// // dumpContext(expContext, true);
1031//
1032// // If an event was consumed previously, get the next one from that trace
1033// final int lastTrace = expContext.getLastTrace();
1034// if (lastTrace != TmfExperimentContext.NO_TRACE) {
1035// final ITmfContext traceContext = expContext.getContexts()[lastTrace];
1036// expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].readNextEvent(traceContext);
1037// expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
1038// }
1039//
1040// // Scan the candidate events and identify the "next" trace to read from
1041// final ITmfEvent eventArray[] = expContext.getEvents();
1042// if (eventArray == null)
1043// return null;
1044// int trace = TmfExperimentContext.NO_TRACE;
1045// ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
1046// if (eventArray.length == 1) {
1047// if (eventArray[0] != null) {
1048// timestamp = eventArray[0].getTimestamp();
1049// trace = 0;
1050// }
1051// } else
1052// for (int i = 0; i < eventArray.length; i++) {
1053// final ITmfEvent event = eventArray[i];
1054// if (event != null && event.getTimestamp() != null) {
1055// final ITmfTimestamp otherTS = event.getTimestamp();
1056// if (otherTS.compareTo(timestamp, true) < 0) {
1057// trace = i;
1058// timestamp = otherTS;
1059// }
1060// }
1061// }
1062// // Update the experiment context and set the "next" event
1063// ITmfEvent event = null;
1064// if (trace != TmfExperimentContext.NO_TRACE) {
1065// updateIndex(expContext, timestamp);
1066//
1067// final ITmfContext traceContext = expContext.getContexts()[trace];
1068// final TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
1069// // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
1070// expLocation.getLocation().locations[trace] = traceContext.getLocation().clone();
1071//
1072// // updateIndex(expContext, timestamp);
1073//
1074// expLocation.getRanks()[trace] = traceContext.getRank();
1075// expContext.setLastTrace(trace);
1076// expContext.increaseRank();
1077// event = expContext.getEvents()[trace];
1078// fExperimentContext = expContext;
1079// }
1080//
1081// // if (event != null) {
1082// // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
1083// // dumpContext(expContext, false);
1084// // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
1085// // }
1086//
1087// return event;
1088// }
1089//
1090// public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
1091// // Build the index as we go along
1092// final long rank = context.getRank();
1093// if (context.hasValidRank() && (rank % fIndexPageSize) == 0) {
1094// // Determine the table position
1095// final long position = rank / fIndexPageSize;
1096// // Add new entry at proper location (if empty)
1097// if (fCheckpoints.size() == position) {
1098// final ITmfLocation<?> location = context.getLocation().clone();
1099// fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
1100// // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
1101// // + location.toString());
1102// }
1103// }
1104// }
1105//
1106// /* (non-Javadoc)
1107// * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
1108// */
1109//// @Override
1110// public ITmfEvent parseEvent(final ITmfContext context) {
1111//
1112// // Validate the context
1113// if (!(context instanceof TmfExperimentContext))
1114// return null; // Throw an exception?
1115//
1116// if (!context.equals(fExperimentContext))
1117// // Tracer.trace("Ctx: Restoring context");
1118// seekEvent(context.getLocation());
1119//
1120// final TmfExperimentContext expContext = (TmfExperimentContext) context;
1121//
1122// // If an event was consumed previously, get the next one from that trace
1123// final int lastTrace = expContext.getLastTrace();
1124// if (lastTrace != TmfExperimentContext.NO_TRACE) {
1125// final ITmfContext traceContext = expContext.getContexts()[lastTrace];
1126// expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].readNextEvent(traceContext);
1127// expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
1128// fExperimentContext = (TmfExperimentContext) context;
1129// }
1130//
1131// // Scan the candidate events and identify the "next" trace to read from
1132// int trace = TmfExperimentContext.NO_TRACE;
1133// ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
1134// for (int i = 0; i < expContext.getTraces().length; i++) {
1135// final ITmfEvent event = expContext.getEvents()[i];
1136// if (event != null && event.getTimestamp() != null) {
1137// final ITmfTimestamp otherTS = event.getTimestamp();
1138// if (otherTS.compareTo(timestamp, true) < 0) {
1139// trace = i;
1140// timestamp = otherTS;
1141// }
1142// }
1143// }
1144//
1145// ITmfEvent event = null;
1146// if (trace != TmfExperimentContext.NO_TRACE)
1147// event = expContext.getEvents()[trace];
1148//
1149// return event;
1150// }
1151//
1152// /* (non-Javadoc)
1153// * @see java.lang.Object#toString()
1154// */
1155// @Override
1156// @SuppressWarnings("nls")
1157// public String toString() {
1158// return "[TmfExperiment (" + getName() + ")]";
1159// }
1160//
1161// // ------------------------------------------------------------------------
1162// // Indexing
1163// // ------------------------------------------------------------------------
1164//
1165// private synchronized void initializeStreamingMonitor() {
1166// if (fInitialized)
1167// return;
1168// fInitialized = true;
1169//
1170// if (getStreamingInterval() == 0) {
1171// final TmfContext context = seekEvent(0);
1172// final ITmfEvent event = getNext(context);
1173// if (event == null)
1174// return;
1175// final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp().clone(), TmfTimestamp.BIG_CRUNCH);
1176// final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
1177//
1178// // Broadcast in separate thread to prevent deadlock
1179// new Thread() {
1180// @Override
1181// public void run() {
1182// broadcast(signal);
1183// }
1184// }.start();
1185// return;
1186// }
1187//
1188// final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { ////$NON-NLS-1$
1189// private ITmfTimestamp safeTimestamp = null;
1190// private TmfTimeRange timeRange = null;
1191//
1192// @Override
1193// public void run() {
1194// while (!fExecutor.isShutdown()) {
1195// if (!isIndexingBusy()) {
1196// ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
1197// ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
1198// for (final ITmfTrace<T> trace : fTraces) {
1199// if (trace.getStartTime().compareTo(startTimestamp) < 0)
1200// startTimestamp = trace.getStartTime();
1201// if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0)
1202// endTimestamp = trace.getEndTime();
1203// }
1204// if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0)
1205// timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
1206// else
1207// timeRange = null;
1208// safeTimestamp = endTimestamp;
1209// if (timeRange != null) {
1210// final TmfExperimentRangeUpdatedSignal signal =
1211// new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1212// broadcast(signal);
1213// }
1214// }
1215// try {
1216// Thread.sleep(getStreamingInterval());
1217// } catch (final InterruptedException e) {
1218// e.printStackTrace();
1219// }
1220// }
1221// }
1222// };
1223// thread.start();
1224// }
1225//
1226// /*
1227// * (non-Javadoc)
1228// *
1229// * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
1230// */
1231// @Override
1232// public long getStreamingInterval() {
1233// long interval = 0;
1234// for (final ITmfTrace<T> trace : fTraces)
1235// interval = Math.max(interval, trace.getStreamingInterval());
1236// return interval;
1237// }
1238//
1239// /*
1240// * The experiment holds the globally ordered events of its set of traces. It
1241// * is expected to provide access to each individual event by index i.e. it
1242// * must be possible to request the Nth event of the experiment.
1243// *
1244// * The purpose of the index is to keep the information needed to rapidly
1245// * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
1246// * event).
1247// */
1248//
1249// // The index page size
1250// private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
1251// protected int fIndexPageSize;
1252// protected boolean fIndexing = false;
1253// protected TmfTimeRange fIndexingPendingRange = TmfTimeRange.NULL_RANGE;
1254//
1255// private Integer fEndSynchReference;
1256//
1257// // private static BufferedWriter fEventLog = null;
1258// // private static BufferedWriter openLogFile(String filename) {
1259// // BufferedWriter outfile = null;
1260// // try {
1261// // outfile = new BufferedWriter(new FileWriter(filename));
1262// // } catch (IOException e) {
1263// // e.printStackTrace();
1264// // }
1265// // return outfile;
1266// // }
1267//
1268// protected boolean isIndexingBusy() {
1269// synchronized (fCheckpoints) {
1270// return fIndexing;
1271// }
1272// }
1273//
1274// @SuppressWarnings("unchecked")
1275// private void indexExperiment(final boolean waitForCompletion, final int index, final TmfTimeRange timeRange) {
1276//
1277// synchronized (fCheckpoints) {
1278// if (fIndexing)
1279// return;
1280// fIndexing = true;
1281// }
1282//
1283// final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
1284//
1285// @Override
1286// protected IStatus run(final IProgressMonitor monitor) {
1287// while (!monitor.isCanceled())
1288// try {
1289// Thread.sleep(100);
1290// } catch (final InterruptedException e) {
1291// return Status.OK_STATUS;
1292// }
1293// monitor.done();
1294// return Status.OK_STATUS;
1295// }
1296// };
1297// job.schedule();
1298//
1299// // fEventLog = openLogFile("TraceEvent.log");
1300// // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
1301//
1302// final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, timeRange, index,
1303// TmfDataRequest.ALL_DATA,
1304// fIndexPageSize, ITmfDataRequest.ExecutionType.BACKGROUND) { // PATA
1305// // FOREGROUND
1306//
1307// // long indexingStart = System.nanoTime();
1308//
1309// ITmfTimestamp startTime = (fTimeRange == TmfTimeRange.NULL_RANGE) ? null : fTimeRange.getStartTime();
1310// ITmfTimestamp lastTime = (fTimeRange == TmfTimeRange.NULL_RANGE) ? null : fTimeRange.getEndTime();
1311// long initialNbEvents = fNbEvents;
1312//
1313// @Override
1314// public void handleStarted() {
1315// super.handleStarted();
1316// }
1317//
1318// @Override
1319// public void handleData(final ITmfEvent event) {
1320// super.handleData(event);
1321// if (event != null) {
1322// final ITmfTimestamp ts = event.getTimestamp();
1323// if (startTime == null)
1324// startTime = ts.clone();
1325// lastTime = ts.clone();
1326// }
1327// if ((getNbRead() % fIndexPageSize) == 1 && getNbRead() != 1)
1328// updateExperiment();
1329// }
1330//
1331// @Override
1332// public void handleSuccess() {
1333// // long indexingEnd = System.nanoTime();
1334//
1335// // if the end time is a real value then it is the streaming safe
1336// // time stamp
1337// // set the last time to the safe time stamp to prevent
1338// // unnecessary indexing requests
1339// if (getRange().getEndTime() != TmfTimestamp.BIG_CRUNCH)
1340// lastTime = getRange().getEndTime();
1341// updateExperiment();
1342// // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
1343//
1344// // long average = (indexingEnd - indexingStart) / fNbEvents;
1345// // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
1346// // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
1347// // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
1348// // + (average % 1000) + " us/evt)");
1349// super.handleSuccess();
1350// }
1351//
1352// @Override
1353// public void handleCompleted() {
1354// job.cancel();
1355// super.handleCompleted();
1356// synchronized (fCheckpoints) {
1357// fIndexing = false;
1358// if (fIndexingPendingRange != TmfTimeRange.NULL_RANGE) {
1359// indexExperiment(false, (int) fNbEvents, fIndexingPendingRange);
1360// fIndexingPendingRange = TmfTimeRange.NULL_RANGE;
1361// }
1362// }
1363// }
1364//
1365// private void updateExperiment() {
1366// final int nbRead = getNbRead();
1367// if (startTime != null)
1368// fTimeRange = new TmfTimeRange(startTime, lastTime.clone());
1369// if (nbRead != 0) {
1370// // updateTimeRange();
1371// // updateNbEvents();
1372// fNbEvents = initialNbEvents + nbRead;
1373// notifyListeners();
1374// }
1375// }
1376// };
1377//
1378// sendRequest((ITmfDataRequest<T>) request);
1379// if (waitForCompletion)
1380// try {
1381// request.waitForCompletion();
1382// } catch (final InterruptedException e) {
1383// e.printStackTrace();
1384// }
1385// }
1386//
1387// protected void notifyListeners() {
1388// broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
1389// // broadcast(new TmfExperimentRangeUpdatedSignal(this, this,
1390// // fTimeRange)); // , null));
1391// }
1392//
1393// // ------------------------------------------------------------------------
1394// // Signal handlers
1395// // ------------------------------------------------------------------------
1396//
1397// @TmfSignalHandler
1398// public void experimentSelected(final TmfExperimentSelectedSignal<T> signal) {
1399// final TmfExperiment<?> experiment = signal.getExperiment();
1400// if (experiment == this) {
1401// setCurrentExperiment(experiment);
1402// fEndSynchReference = Integer.valueOf(signal.getReference());
1403// }
1404// }
1405//
1406// @TmfSignalHandler
1407// public void endSync(final TmfEndSynchSignal signal) {
1408// if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
1409// fEndSynchReference = null;
1410// initializeStreamingMonitor();
1411// }
1412// }
1413//
1414// @TmfSignalHandler
1415// public void experimentUpdated(final TmfExperimentUpdatedSignal signal) {
1416// }
1417//
1418// @TmfSignalHandler
1419// public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
1420// if (signal.getExperiment() == this)
1421// indexExperiment(false, (int) fNbEvents, signal.getRange());
1422// }
1423//
1424// @TmfSignalHandler
1425// public void traceUpdated(final TmfTraceUpdatedSignal signal) {
1426// for (final ITmfTrace<T> trace : fTraces)
1427// if (trace == signal.getTrace()) {
1428// synchronized (fCheckpoints) {
1429// if (fIndexing) {
1430// if (fIndexingPendingRange == TmfTimeRange.NULL_RANGE)
1431// fIndexingPendingRange = signal.getRange();
1432// else {
1433// ITmfTimestamp startTime = fIndexingPendingRange.getStartTime();
1434// ITmfTimestamp endTime = fIndexingPendingRange.getEndTime();
1435// if (signal.getRange().getStartTime().compareTo(startTime) < 0)
1436// startTime = signal.getRange().getStartTime();
1437// if (signal.getRange().getEndTime().compareTo(endTime) > 0)
1438// endTime = signal.getRange().getEndTime();
1439// fIndexingPendingRange = new TmfTimeRange(startTime, endTime);
1440// }
1441// return;
1442// }
1443// }
1444// indexExperiment(false, (int) fNbEvents, signal.getRange());
1445// return;
1446// }
1447// }
1448//
1449// @Override
1450// public String getPath() {
1451// // TODO Auto-generated method stub
1452// return null;
1453// }
1454//
1455// /**
1456// * Set the file to be used for bookmarks on this experiment
1457// *
1458// * @param file the bookmarks file
1459// */
1460// public void setBookmarksFile(final IFile file) {
1461// fBookmarksFile = file;
1462// }
1463//
1464// /**
1465// * Get the file used for bookmarks on this experiment
1466// *
1467// * @return the bookmarks file or null if none is set
1468// */
1469// public IFile getBookmarksFile() {
1470// return fBookmarksFile;
1471// }
1472//
1473// /*
1474// * (non-Javadoc)
1475// *
1476// * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
1477// */
1478// @Override
1479// public IResource getResource() {
1480// return fResource;
1481// }
1482//}
This page took 0.112512 seconds and 5 git commands to generate.