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