2010-08-30 Francois Chouinard <fchouinard@gmail.com> Quick fix for a ClassCastExcep...
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / experiment / TmfExperiment.java
CommitLineData
8c8bf09f
ASL
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.experiment;
14
9f584e4c 15import java.util.Collections;
8c8bf09f
ASL
16import java.util.Vector;
17
fc6ccf6f 18import org.eclipse.linuxtools.tmf.component.TmfEventProvider;
8c8bf09f
ASL
19import org.eclipse.linuxtools.tmf.event.TmfEvent;
20import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
21import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
2fb2eb37
FC
22import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
23import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
550d787e
FC
24import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
25import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
045df77d 26import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType;
ff4ed569
FC
27import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal;
28import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal;
8c8bf09f 29import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler;
550d787e 30import org.eclipse.linuxtools.tmf.signal.TmfSignalManager;
ff4ed569 31import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal;
9f584e4c
FC
32import org.eclipse.linuxtools.tmf.trace.ITmfContext;
33import org.eclipse.linuxtools.tmf.trace.ITmfLocation;
8c8bf09f 34import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
9f584e4c
FC
35import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint;
36import org.eclipse.linuxtools.tmf.trace.TmfContext;
8c8bf09f
ASL
37
38/**
39 * <b><u>TmfExperiment</u></b>
40 * <p>
41 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces
9b635e61 42 * that are part of a tracing experiment.
8c8bf09f
ASL
43 * <p>
44 */
fc6ccf6f 45public class TmfExperiment<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace {
8c8bf09f
ASL
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50
51 // The currently selected experiment
cbd4ad82 52 private static TmfExperiment<?> fCurrentExperiment = null;
e31e01e8 53
550d787e 54 // The set of traces that constitute the experiment
9f584e4c 55 private ITmfTrace[] fTraces;
8c8bf09f
ASL
56
57 // The total number of events
9f584e4c 58 private long fNbEvents;
8c8bf09f
ASL
59
60 // The experiment time range
61 private TmfTimeRange fTimeRange;
62
9b635e61 63 // The experiment reference timestamp (default: Zero)
8c8bf09f
ASL
64 private TmfTimestamp fEpoch;
65
9f584e4c
FC
66 // The experiment index
67 private Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
68
9b635e61
FC
69 private TmfExperimentContext fSavedContext;
70
8c8bf09f
ASL
71 // ------------------------------------------------------------------------
72 // Constructors
73 // ------------------------------------------------------------------------
74
75 /**
76 * @param type
77 * @param id
78 * @param traces
79 * @param epoch
80 * @param indexPageSize
81 */
82 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize) {
045df77d 83 this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false);
cb866e08
FC
84 }
85
9b635e61 86 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) {
ce785d7d 87 super(id, type);
8c8bf09f 88
9f584e4c 89 fTraces = traces;
8c8bf09f
ASL
90 fEpoch = epoch;
91 fIndexPageSize = indexPageSize;
92
9b635e61 93 if (preIndexExperiment) indexExperiment(true);
cb866e08 94
8c8bf09f 95 updateTimeRange();
550d787e 96 }
8c8bf09f
ASL
97
98 /**
99 * @param type
100 * @param id
101 * @param traces
102 */
103 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces) {
104 this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE);
105 }
106
107 /**
108 * @param type
109 * @param id
110 * @param traces
111 * @param indexPageSize
112 */
113 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, int indexPageSize) {
114 this(type, id, traces, TmfTimestamp.Zero, indexPageSize);
115 }
377f1ad8 116
ce785d7d 117 public TmfExperiment(TmfExperiment<T> other) {
550d787e 118 super(other.getName() + "(clone)", other.fType);
377f1ad8 119
ce785d7d
FC
120 fEpoch = other.fEpoch;
121 fIndexPageSize = other.fIndexPageSize;
377f1ad8 122
ce785d7d
FC
123 fTraces = new ITmfTrace[other.fTraces.length];
124 for (int trace = 0; trace < other.fTraces.length; trace++) {
125 fTraces[trace] = other.fTraces[trace].createTraceCopy();
377f1ad8
WB
126 }
127
ce785d7d
FC
128 fNbEvents = other.fNbEvents;
129 fTimeRange = other.fTimeRange;
377f1ad8
WB
130 }
131
132 public TmfExperiment<T> createTraceCopy() {
550d787e
FC
133 TmfExperiment<T> experiment = new TmfExperiment<T>(this);
134 TmfSignalManager.deregister(experiment);
135 return experiment;
377f1ad8
WB
136 }
137
8c8bf09f 138 /**
ff4ed569 139 * Clears the experiment
8c8bf09f
ASL
140 */
141 @Override
2fb2eb37 142 public void dispose() {
550d787e
FC
143 if (fTraces != null) {
144 for (ITmfTrace trace : fTraces) {
145 trace.dispose();
146 }
147 fTraces = null;
148 }
149 if (fCheckpoints != null) {
150 fCheckpoints.clear();
ff4ed569 151 }
2fb2eb37 152 super.dispose();
8c8bf09f
ASL
153 }
154
cbd4ad82
FC
155 private static void setCurrentExperiment(TmfExperiment<?> experiment) {
156 fCurrentExperiment = experiment;
157 }
158
9f584e4c 159 // ------------------------------------------------------------------------
cbd4ad82 160 // ITmfTrace
9f584e4c
FC
161 // ------------------------------------------------------------------------
162
163 public String getPath() {
164 return null;
165 }
166
9f584e4c
FC
167 public long getNbEvents() {
168 return fNbEvents;
169 }
170
54d55ced
FC
171 public int getCacheSize() {
172 return fIndexPageSize;
173 }
174
9f584e4c
FC
175 public TmfTimeRange getTimeRange() {
176 return fTimeRange;
177 }
178
179 public TmfTimestamp getStartTime() {
180 return fTimeRange.getStartTime();
181 }
182
183 public TmfTimestamp getEndTime() {
184 return fTimeRange.getEndTime();
185 }
186
54d55ced
FC
187 public Vector<TmfCheckpoint> getCheckpoints() {
188 return fCheckpoints;
189 }
190
8c8bf09f 191 // ------------------------------------------------------------------------
e31e01e8 192 // Accessors
8c8bf09f
ASL
193 // ------------------------------------------------------------------------
194
e31e01e8
FC
195 public static TmfExperiment<?> getCurrentExperiment() {
196 return fCurrentExperiment;
8c8bf09f
ASL
197 }
198
8c8bf09f
ASL
199 public TmfTimestamp getEpoch() {
200 return fEpoch;
201 }
202
9f584e4c
FC
203 public ITmfTrace[] getTraces() {
204 return fTraces;
8c8bf09f
ASL
205 }
206
207 /**
208 * Returns the rank of the first event with the requested timestamp.
209 * If none, returns the index of the next event (if any).
210 *
85fb0e54 211 * @param timestamp
8c8bf09f
ASL
212 * @return
213 */
85fb0e54
FC
214 public long getRank(TmfTimestamp timestamp) {
215 TmfExperimentContext context = seekEvent(timestamp);
8c8bf09f
ASL
216 return context.getRank();
217 }
218
219 /**
220 * Returns the timestamp of the event at the requested index.
221 * If none, returns null.
222 *
223 * @param index
224 * @return
225 */
226 public TmfTimestamp getTimestamp(int index) {
85fb0e54 227 TmfExperimentContext context = seekEvent(index);
7f407ead 228 TmfEvent event = getNextEvent(context);
85fb0e54 229 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
230 }
231
232 // ------------------------------------------------------------------------
233 // Operators
234 // ------------------------------------------------------------------------
235
236 /**
237 * Update the total number of events
238 */
239 private void updateNbEvents() {
240 int nbEvents = 0;
241 for (ITmfTrace trace : fTraces) {
242 nbEvents += trace.getNbEvents();
243 }
244 fNbEvents = nbEvents;
245 }
246
247 /**
248 * Update the global time range
249 */
250 private void updateTimeRange() {
251 TmfTimestamp startTime = fTimeRange != null ? fTimeRange.getStartTime() : TmfTimestamp.BigCrunch;
252 TmfTimestamp endTime = fTimeRange != null ? fTimeRange.getEndTime() : TmfTimestamp.BigBang;
253
254 for (ITmfTrace trace : fTraces) {
550d787e
FC
255 TmfTimestamp traceStartTime = trace.getStartTime();
256 if (traceStartTime.compareTo(startTime, true) < 0)
257 startTime = traceStartTime;
258 TmfTimestamp traceEndTime = trace.getEndTime();
259 if (traceEndTime.compareTo(endTime, true) > 0)
260 endTime = traceEndTime;
8c8bf09f 261 }
36548af3 262 fTimeRange = new TmfTimeRange(startTime, endTime);
8c8bf09f
ASL
263 }
264
265 // ------------------------------------------------------------------------
266 // TmfProvider
267 // ------------------------------------------------------------------------
268
269 @Override
2fb2eb37 270 public ITmfContext armRequest(ITmfDataRequest<T> request) {
9b635e61 271// Tracer.trace("Ctx: Arming request - start");
2fb2eb37 272 TmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ?
9b635e61 273 ((ITmfEventRequest<T>) request).getRange().getStartTime() : null;
9f584e4c
FC
274 TmfExperimentContext context = (timestamp != null) ?
275 seekEvent(timestamp) : seekEvent(request.getIndex());
9b635e61 276// Tracer.trace("Ctx: Arming request - done");
8c8bf09f
ASL
277 return context;
278 }
279
280 @SuppressWarnings("unchecked")
281 @Override
282 public T getNext(ITmfContext context) {
283 if (context instanceof TmfExperimentContext) {
284 return (T) getNextEvent((TmfExperimentContext) context);
285 }
286 return null;
287 }
288
550d787e 289 // ------------------------------------------------------------------------
9f584e4c
FC
290 // ITmfTrace trace positioning
291 // ------------------------------------------------------------------------
292
293 // Returns a brand new context based on the location provided
8f50c396 294 // and initializes the event queues
9b635e61 295 public synchronized TmfExperimentContext seekLocation(ITmfLocation<?> location) {
8f50c396
FC
296
297 // Validate the location
298 if (location != null && !(location instanceof TmfExperimentLocation)) {
299 return null; // Throw an exception?
9f584e4c 300 }
8f50c396
FC
301
302 // Instantiate the location
303 TmfExperimentLocation expLocation = (location == null)
304 ? new TmfExperimentLocation(new ITmfLocation<?>[fTraces.length], new long[fTraces.length])
305 : (TmfExperimentLocation) location.clone();
306
307 // Create and populate the context's traces contexts
308 TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]);
9b635e61
FC
309// Tracer.trace("Ctx: SeekLocation - start");
310
8f50c396
FC
311 long rank = 0;
312 for (int i = 0; i < fTraces.length; i++) {
313 // Get the relevant trace attributes
314 ITmfLocation<?> traceLocation = expLocation.getLocation()[i];
315 long traceRank = expLocation.getRanks()[i];
316
317 // Set the corresponding sub-context
318 context.getContexts()[i] = fTraces[i].seekLocation(traceLocation);
319 context.getContexts()[i].setRank(traceRank);
320 rank += traceRank;
321
322 // Set the trace location and read the corresponding event
323 expLocation.getLocation()[i] = context.getContexts()[i].getLocation();
fa867360 324 context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]);
8f50c396
FC
325 }
326
9b635e61
FC
327// Tracer.trace("Ctx: SeekLocation - done");
328
8f50c396
FC
329 // Finalize context
330 context.setLocation(expLocation);
8f50c396 331 context.setLastTrace(TmfExperimentContext.NO_TRACE);
9b635e61
FC
332 context.setRank(rank);
333
334 fSavedContext = new TmfExperimentContext(context);
335
8f50c396 336 return context;
9f584e4c
FC
337 }
338
54d55ced
FC
339 /* (non-Javadoc)
340 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
341 */
9b635e61
FC
342 public synchronized TmfExperimentContext seekEvent(TmfTimestamp timestamp) {
343
344// Tracer.trace("Ctx: seekEvent(TS) - start");
8c8bf09f 345
9f584e4c
FC
346 if (timestamp == null) {
347 timestamp = TmfTimestamp.BigBang;
348 }
349
350 // First, find the right checkpoint
351 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
352
353 // In the very likely case that the checkpoint was not found, bsearch
354 // returns its negated would-be location (not an offset...). From that
355 // index, we can then position the stream and get the event.
356 if (index < 0) {
357 index = Math.max(0, -(index + 2));
358 }
359
360 // Position the experiment at the checkpoint
452ad365 361 ITmfLocation<?> location;
9f584e4c
FC
362 synchronized (fCheckpoints) {
363 if (fCheckpoints.size() > 0) {
364 if (index >= fCheckpoints.size()) {
365 index = fCheckpoints.size() - 1;
366 }
367 location = fCheckpoints.elementAt(index).getLocation();
368 }
369 else {
370 location = null;
371 }
372 }
373
85fb0e54 374 TmfExperimentContext context = seekLocation(location);
cbd4ad82 375 context.setRank((long) index * fIndexPageSize);
9f584e4c 376
9b635e61 377 // And locate the event
fa867360 378 TmfEvent event = parseEvent(context);
9f584e4c 379 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
fa867360
FC
380 getNextEvent(context);
381 event = parseEvent(context);
9f584e4c
FC
382 }
383
9b635e61 384 if (event != null) {
fa867360 385 fSavedContext = new TmfExperimentContext(context);
9b635e61
FC
386 }
387 else {
fa867360
FC
388 context.setLocation(null);
389 context.setRank(ITmfContext.UNKNOWN_RANK);
9b635e61 390 }
fa867360 391 return context;
9f584e4c 392 }
8c8bf09f 393
54d55ced
FC
394 /* (non-Javadoc)
395 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
396 */
9b635e61
FC
397 public synchronized TmfExperimentContext seekEvent(long rank) {
398
399// Tracer.trace("Ctx: seekEvent(rank) - start");
9f584e4c 400
54d55ced
FC
401 // Position the stream at the previous checkpoint
402 int index = (int) rank / fIndexPageSize;
403 ITmfLocation<?> location;
404 synchronized (fCheckpoints) {
405 if (fCheckpoints.size() == 0) {
406 location = null;
407 }
408 else {
409 if (index >= fCheckpoints.size()) {
410 index = fCheckpoints.size() - 1;
411 }
412 location = fCheckpoints.elementAt(index).getLocation();
413 }
414 }
e31e01e8 415
54d55ced 416 TmfExperimentContext context = seekLocation(location);
9b635e61 417 context.setRank((long) index * fIndexPageSize);
54d55ced 418
9b635e61 419 // And locate the event
fa867360 420 TmfEvent event = parseEvent(context);
9b635e61 421 long pos = context.getRank();
fa867360
FC
422 while (event != null && pos++ < rank) {
423 getNextEvent(context);
424 event = parseEvent(context);
54d55ced 425 }
9f584e4c 426
9b635e61 427 if (event != null) {
fa867360 428 fSavedContext = new TmfExperimentContext(context);
9b635e61
FC
429 }
430 else {
fa867360
FC
431 context.setLocation(null);
432 context.setRank(ITmfContext.UNKNOWN_RANK);
9b635e61 433 }
fa867360 434 return context;
8c8bf09f
ASL
435 }
436
437 /**
438 * Scan the next events from all traces and return the next one
439 * in chronological order.
440 *
441 * @param context
442 * @return
443 */
9b635e61
FC
444
445// private void dumpContext(TmfExperimentContext context, boolean isBefore) {
446
447// TmfContext context0 = context.getContexts()[0];
448// TmfEvent event0 = context.getEvents()[0];
449// TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
450// long rank0 = context.getRank();
451// int trace = context.getLastTrace();
452//
453// StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
454//
455// result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
456// result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
457// result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
458// result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
459// Tracer.trace(result.toString());
460// }
461
9f584e4c 462 public synchronized TmfEvent getNextEvent(TmfContext context) {
54d55ced 463
8f50c396
FC
464 // Validate the context
465 if (!(context instanceof TmfExperimentContext)) {
466 return null; // Throw an exception?
467 }
54d55ced 468
9b635e61
FC
469 if (!fSavedContext.equals(context)) {
470// Tracer.trace("Ctx: Restoring context");
471 seekLocation(context.getLocation());
472 }
473
8f50c396
FC
474 TmfExperimentContext expContext = (TmfExperimentContext) context;
475
9b635e61
FC
476// dumpContext(expContext, true);
477
8f50c396
FC
478 // If an event was consumed previously, get the next one from that trace
479 int lastTrace = expContext.getLastTrace();
480 if (lastTrace != TmfExperimentContext.NO_TRACE) {
481 TmfContext traceContext = expContext.getContexts()[lastTrace];
482 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
fa867360 483 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
8f50c396
FC
484 }
485
486 // Scan the candidate events and identify the "next" trace to read from
487 int trace = TmfExperimentContext.NO_TRACE;
488 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
489 for (int i = 0; i < expContext.getTraces().length; i++) {
490 TmfEvent event = expContext.getEvents()[i];
491 if (event != null && event.getTimestamp() != null) {
492 TmfTimestamp otherTS = event.getTimestamp();
493 if (otherTS.compareTo(timestamp, true) < 0) {
494 trace = i;
495 timestamp = otherTS;
8c8bf09f
ASL
496 }
497 }
498 }
8f50c396
FC
499
500 // Update the experiment context and set the "next" event
501 TmfEvent event = null;
fa867360
FC
502 if (trace != TmfExperimentContext.NO_TRACE) {
503 updateIndex(expContext, timestamp);
504 TmfContext traceContext = expContext.getContexts()[trace];
505 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
506 expLocation.getLocation()[trace] = traceContext.getLocation().clone();
507 expLocation.getRanks()[trace] = traceContext.getRank();
8f50c396
FC
508 expContext.setLastTrace(trace);
509 expContext.updateRank(1);
8f50c396
FC
510 event = expContext.getEvents()[trace];
511 }
512
9b635e61
FC
513// if (event != null) {
514// Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
515// dumpContext(expContext, false);
516// Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
517// }
518
519 fSavedContext = new TmfExperimentContext(expContext);
520
8f50c396 521 return event;
8c8bf09f
ASL
522 }
523
9b635e61 524 public synchronized void updateIndex(ITmfContext context, TmfTimestamp timestamp) {
550d787e 525 // Build the index as we go along
9b635e61 526 long rank = context.getRank();
550d787e
FC
527 if (context.isValidRank() && (rank % fIndexPageSize) == 0) {
528 // Determine the table position
9b635e61 529 long position = rank / fIndexPageSize;
550d787e
FC
530 // Add new entry at proper location (if empty)
531 if (fCheckpoints.size() == position) {
532 ITmfLocation<?> location = context.getLocation().clone();
533 fCheckpoints.add(new TmfCheckpoint(timestamp, location));
534// System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString());
535 }
536 }
537 }
538
54d55ced 539 /* (non-Javadoc)
045df77d 540 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.trace.TmfContext)
54d55ced 541 */
9f584e4c 542 public TmfEvent parseEvent(TmfContext context) {
54d55ced 543
9b635e61
FC
544 // Validate the context
545 if (!(context instanceof TmfExperimentContext)) {
546 return null; // Throw an exception?
547 }
54d55ced 548
9b635e61
FC
549 if (!fSavedContext.equals(context)) {
550// Tracer.trace("Ctx: Restoring context");
551 seekLocation(context.getLocation());
552 }
553
554 TmfExperimentContext expContext = (TmfExperimentContext) context;
555
556 // If an event was consumed previously, get the next one from that trace
557 int lastTrace = expContext.getLastTrace();
558 if (lastTrace != TmfExperimentContext.NO_TRACE) {
559 TmfContext traceContext = expContext.getContexts()[lastTrace];
560 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
fa867360 561 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
045df77d 562 fSavedContext = new TmfExperimentContext(expContext);
9b635e61
FC
563 }
564
565 // Scan the candidate events and identify the "next" trace to read from
566 int trace = TmfExperimentContext.NO_TRACE;
567 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
568 for (int i = 0; i < expContext.getTraces().length; i++) {
569 TmfEvent event = expContext.getEvents()[i];
570 if (event != null && event.getTimestamp() != null) {
571 TmfTimestamp otherTS = event.getTimestamp();
572 if (otherTS.compareTo(timestamp, true) < 0) {
573 trace = i;
574 timestamp = otherTS;
85fb0e54
FC
575 }
576 }
85fb0e54 577 }
54d55ced 578
9b635e61 579 TmfEvent event = null;
fa867360 580 if (trace != TmfExperimentContext.NO_TRACE) {
9b635e61
FC
581 event = expContext.getEvents()[trace];
582 }
583 return event;
8c8bf09f
ASL
584 }
585
586 /* (non-Javadoc)
587 * @see java.lang.Object#toString()
588 */
589 @Override
590 public String toString() {
ce785d7d 591 return "[TmfExperiment (" + getName() + ")]";
8c8bf09f
ASL
592 }
593
594 // ------------------------------------------------------------------------
595 // Indexing
596 // ------------------------------------------------------------------------
597
598 /*
599 * The experiment holds the globally ordered events of its set of traces.
600 * It is expected to provide access to each individual event by index i.e.
9f584e4c 601 * it must be possible to request the Nth event of the experiment.
8c8bf09f
ASL
602 *
603 * The purpose of the index is to keep the information needed to rapidly
604 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
605 * event).
606 */
607
608 // The index page size
9b635e61 609 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
550d787e 610 private final int fIndexPageSize;
e31e01e8 611
9b635e61
FC
612// private static BufferedWriter fEventLog = null;
613// private static BufferedWriter openLogFile(String filename) {
614// BufferedWriter outfile = null;
615// try {
616// outfile = new BufferedWriter(new FileWriter(filename));
617// } catch (IOException e) {
618// e.printStackTrace();
619// }
620// return outfile;
621// }
622
550d787e 623 @SuppressWarnings("unchecked")
cb866e08 624 private void indexExperiment(boolean waitForCompletion) {
550d787e 625
550d787e 626 fCheckpoints.clear();
9b635e61
FC
627
628// fEventLog = openLogFile("TraceEvent.log");
550d787e
FC
629
630 ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.LONG) {
631
cb866e08 632// long indexingStart = System.nanoTime();
550d787e
FC
633
634 TmfTimestamp startTime = null;
635 TmfTimestamp lastTime = null;
550d787e
FC
636
637 @Override
638 public void handleData() {
639 TmfEvent[] events = getData();
640 if (events.length > 0) {
9b635e61
FC
641
642// try {
643// if (fEventLog != null) {
644// fEventLog.write(events[0].getTimestamp().toString());
645// fEventLog.newLine();
646// fEventLog.flush();
647// }
648// } catch (IOException e) {
649// e.printStackTrace();
650// }
651
550d787e
FC
652 TmfTimestamp ts = events[0].getTimestamp();
653 if (startTime == null)
654 startTime = new TmfTimestamp(ts);
655 lastTime = new TmfTimestamp(ts);
656
cb866e08 657 if ((fNbRead % DEFAULT_INDEX_PAGE_SIZE) == 0) {
550d787e
FC
658 updateExperiment();
659 }
660 }
9aae0442 661 }
e31e01e8 662
550d787e
FC
663 @Override
664 public void handleSuccess() {
cb866e08 665
9b635e61
FC
666// try {
667// fEventLog.close();
668// fEventLog = null;
669// } catch (IOException e) {
670// e.printStackTrace();
671// }
672
673// long indexingEnd = System.nanoTime();
674
675 updateExperiment();
676// System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
cb866e08 677
9b635e61
FC
678// long average = (indexingEnd - indexingStart) / fNbEvents;
679// System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed=" + (indexingEnd * 1.0 - indexingStart) / 1000000000);
680// System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "." + (average % 1000) + " us/evt)");
cb866e08
FC
681
682// for (int i = 0; i < fCheckpoints.size(); i++) {
683// TmfCheckpoint checkpoint = fCheckpoints.get(i);
684// System.out.println("fCheckpoints[" + i + "] " + checkpoint.getTimestamp() + ", " + checkpoint.getLocation().toString());
685// }
e31e01e8
FC
686 }
687
550d787e 688 private void updateExperiment() {
6c042a35
FC
689 if (fNbRead != 0) {
690 fTimeRange = new TmfTimeRange(startTime, new TmfTimestamp(lastTime));
691 fNbEvents = fNbRead;
692 notifyListeners();
693 }
550d787e
FC
694 }
695 };
e31e01e8 696
550d787e 697 sendRequest((ITmfDataRequest<T>) request);
cb866e08
FC
698 if (waitForCompletion)
699 try {
700 request.waitForCompletion();
701 } catch (InterruptedException e) {
702 e.printStackTrace();
703 }
550d787e
FC
704 }
705
706 protected void notifyListeners() {
9b635e61 707 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
9f584e4c
FC
708 }
709
8c8bf09f
ASL
710 // ------------------------------------------------------------------------
711 // Signal handlers
712 // ------------------------------------------------------------------------
713
714 @TmfSignalHandler
951d134a 715 public void experimentSelected(TmfExperimentSelectedSignal<T> signal) {
ff4ed569
FC
716 TmfExperiment<?> experiment = signal.getExperiment();
717 if (experiment == this) {
718 setCurrentExperiment(experiment);
cb866e08 719 indexExperiment(false);
ff4ed569
FC
720 }
721 else {
722 dispose();
723 }
8c8bf09f
ASL
724 }
725
726 @TmfSignalHandler
727 public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
8c8bf09f
ASL
728 }
729
730 @TmfSignalHandler
731 public void traceUpdated(TmfTraceUpdatedSignal signal) {
732 // TODO: Incremental index update
733 synchronized(this) {
734 updateNbEvents();
735 updateTimeRange();
736 }
9b635e61 737 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , signal.getTrace()));
8c8bf09f
ASL
738 }
739
3d62f8b7
FC
740// @Override
741// public void queueResult(T data) {
742//// super.queueResult(data);
743// }
cb866e08 744
9b635e61
FC
745 // ------------------------------------------------------------------------
746 // TmfDataProvider
747 // ------------------------------------------------------------------------
748
749 @Override
750 protected void queueLongRequest(final ITmfDataRequest<T> request) {
751
752 // TODO: Handle the data requests also...
753 if (!(request instanceof ITmfEventRequest<?>)) {
754 super.queueRequest(request);
755 return;
756 }
757
758 final TmfExperiment<T> experiment = this;
759
760 Thread thread = new Thread() {
761 @Override
762 public void run() {
763
764// final long requestStart = System.nanoTime();
765
766 final Integer[] CHUNK_SIZE = new Integer[1];
767 CHUNK_SIZE[0] = fIndexPageSize + 1;
768
769 final ITmfEventRequest<T> req = (ITmfEventRequest<T>) request;
770
771 final Integer[] nbRead = new Integer[1];
772 nbRead[0] = 0;
773
774// final TmfTimestamp[] timestamp = new TmfTimestamp[1];
775// timestamp[0] = new TmfTimestamp(req.getRange().getStartTime());
776// final TmfTimestamp endTS = req.getRange().getEndTime();
777
778 final Boolean[] isFinished = new Boolean[1];
779 isFinished[0] = Boolean.FALSE;
780
781 while (!isFinished[0]) {
782
783// TmfEventRequest<T> subRequest = new TmfEventRequest<T>(req.getDataType(), new TmfTimeRange(timestamp[0], endTS),
784// requestedSize, req.getBlockize(), ExecutionType.LONG)
785 TmfDataRequest<T> subRequest = new TmfDataRequest<T>(req.getDataType(), nbRead[0], CHUNK_SIZE[0],
786 req.getBlockize(), ExecutionType.LONG)
787 {
788// int count = 0;
789 @Override
790 public void handleData() {
791 T[] data = getData();
792// if (count == 0) {
793// System.out.println("First event of the block: " + data[0].getTimestamp());
794// }
795// count++;
796// Tracer.trace("Ndx: " + ((TmfEvent) data[0]).getTimestamp());
797 req.setData(data);
798 req.handleData();
799 if (fNbRead == CHUNK_SIZE[0]) {
800 nbRead[0] += fNbRead;
801// System.out.println("fNbRead=" + fNbRead + ", count=" + count +", total=" + nbRead[0] + ", TS=" + data[0].getTimestamp());
802 }
803 if (fNbRead > CHUNK_SIZE[0]) {
804 System.out.println("ERROR - Read too many events");
805 }
806 }
807 @Override
808 public void handleCompleted() {
809// System.out.println("Request completed at: " + timestamp[0]);
810 if (fNbRead < CHUNK_SIZE[0]) {
811 req.done();
812 isFinished[0] = Boolean.TRUE;
813 nbRead[0] += fNbRead;
814// System.out.println("fNbRead=" + fNbRead + ", count=" + count +", total=" + nbRead[0]);
815 }
816 super.handleCompleted();
817 }
818 };
819
820 if (!isFinished[0]) {
821 experiment.queueRequest(subRequest);
822
823 try {
824 subRequest.waitForCompletion();
825// System.out.println("Finished at " + timestamp[0]);
826 } catch (InterruptedException e) {
827 e.printStackTrace();
828 }
829
830// TmfTimestamp newTS = new TmfTimestamp(timestamp[0].getValue() + 1, timestamp[0].getScale(), timestamp[0].getPrecision());
831// timestamp[0] = newTS;
832 CHUNK_SIZE[0] = fIndexPageSize;
833// System.out.println("New timestamp: " + timestamp[0]);
834 }
835 }
836// final long requestEnded = System.nanoTime();
837// System.out.println("Background request completed. Elapsed= " + (requestEnded * 1.0 - requestStart) / 1000000000);
838 }
839 };
840 thread.start();
841 }
842
8c8bf09f 843}
This page took 0.072024 seconds and 5 git commands to generate.