| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal |
| 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 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 |
| 12 | * Patrick Tasse - Updated for removal of context clone |
| 13 | * Geneviève Bastien - Added timestamp transforms, its saving to file and |
| 14 | * timestamp creation functions |
| 15 | *******************************************************************************/ |
| 16 | |
| 17 | package org.eclipse.linuxtools.tmf.core.trace; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.FileInputStream; |
| 21 | import java.io.FileOutputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.ObjectInputStream; |
| 24 | import java.io.ObjectOutputStream; |
| 25 | import java.util.Collections; |
| 26 | import java.util.HashSet; |
| 27 | import java.util.LinkedHashMap; |
| 28 | import java.util.Map; |
| 29 | import java.util.Map.Entry; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import org.eclipse.core.resources.IFolder; |
| 33 | import org.eclipse.core.resources.IResource; |
| 34 | import org.eclipse.core.runtime.CoreException; |
| 35 | import org.eclipse.core.runtime.IStatus; |
| 36 | import org.eclipse.core.runtime.MultiStatus; |
| 37 | import org.eclipse.core.runtime.Path; |
| 38 | import org.eclipse.core.runtime.Status; |
| 39 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
| 40 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
| 41 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; |
| 42 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper; |
| 43 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager; |
| 44 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
| 45 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 46 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; |
| 47 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 48 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 49 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
| 50 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
| 51 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal; |
| 52 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal; |
| 53 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal; |
| 54 | import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; |
| 55 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform; |
| 56 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 57 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 58 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 59 | import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer; |
| 60 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; |
| 61 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; |
| 62 | |
| 63 | /** |
| 64 | * Abstract implementation of ITmfTrace. |
| 65 | * <p> |
| 66 | * Since the concept of 'location' is trace specific, the concrete classes have |
| 67 | * to provide the related methods, namely: |
| 68 | * <ul> |
| 69 | * <li> public ITmfLocation<?> getCurrentLocation() |
| 70 | * <li> public double getLocationRatio(ITmfLocation<?> location) |
| 71 | * <li> public ITmfContext seekEvent(ITmfLocation<?> location) |
| 72 | * <li> public ITmfContext seekEvent(double ratio) |
| 73 | * <li> public IStatus validate(IProject project, String path) |
| 74 | * </ul> |
| 75 | * A concrete trace must provide its corresponding parser. A common way to |
| 76 | * accomplish this is by making the concrete class extend TmfTrace and |
| 77 | * implement ITmfEventParser. |
| 78 | * <p> |
| 79 | * The concrete class can either specify its own indexer or use the provided |
| 80 | * TmfCheckpointIndexer (default). In this case, the trace cache size will be |
| 81 | * used as checkpoint interval. |
| 82 | * |
| 83 | * @version 1.0 |
| 84 | * @author Francois Chouinard |
| 85 | * |
| 86 | * @see ITmfEvent |
| 87 | * @see ITmfTraceIndexer |
| 88 | * @see ITmfEventParser |
| 89 | */ |
| 90 | public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace { |
| 91 | |
| 92 | // ------------------------------------------------------------------------ |
| 93 | // Attributes |
| 94 | // ------------------------------------------------------------------------ |
| 95 | |
| 96 | // The resource used for persistent properties for this trace |
| 97 | private IResource fResource; |
| 98 | |
| 99 | // The trace path |
| 100 | private String fPath; |
| 101 | |
| 102 | // The trace cache page size |
| 103 | private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; |
| 104 | |
| 105 | // The number of events collected (so far) |
| 106 | private volatile long fNbEvents = 0; |
| 107 | |
| 108 | // The time span of the event stream |
| 109 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG; |
| 110 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; |
| 111 | |
| 112 | // The trace streaming interval (0 = no streaming) |
| 113 | private long fStreamingInterval = 0; |
| 114 | |
| 115 | // The trace indexer |
| 116 | private ITmfTraceIndexer fIndexer; |
| 117 | |
| 118 | // The trace parser |
| 119 | private ITmfEventParser fParser; |
| 120 | |
| 121 | private ITmfTimestampTransform fTsTransform; |
| 122 | |
| 123 | private final Map<String, IAnalysisModule> fAnalysisModules = |
| 124 | Collections.synchronizedMap(new LinkedHashMap<String, IAnalysisModule>()); |
| 125 | |
| 126 | private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$ |
| 127 | |
| 128 | // ------------------------------------------------------------------------ |
| 129 | // Construction |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | /** |
| 133 | * The default, parameterless, constructor |
| 134 | */ |
| 135 | public TmfTrace() { |
| 136 | super(); |
| 137 | fIndexer = createIndexer(DEFAULT_BLOCK_SIZE); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Full constructor. |
| 142 | * |
| 143 | * @param resource |
| 144 | * The resource associated to the trace |
| 145 | * @param type |
| 146 | * The type of events that will be read from this trace |
| 147 | * @param path |
| 148 | * The path to the trace on the filesystem |
| 149 | * @param cacheSize |
| 150 | * The trace cache size. Pass '-1' to use the default specified |
| 151 | * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE} |
| 152 | * @param interval |
| 153 | * The trace streaming interval. You can use '0' for post-mortem |
| 154 | * traces. |
| 155 | * @param parser |
| 156 | * The trace event parser. Use 'null' if (and only if) the trace |
| 157 | * object itself is also the ITmfEventParser to be used. |
| 158 | * @throws TmfTraceException |
| 159 | * If something failed during the opening |
| 160 | */ |
| 161 | protected TmfTrace(final IResource resource, |
| 162 | final Class<? extends ITmfEvent> type, |
| 163 | final String path, |
| 164 | final int cacheSize, |
| 165 | final long interval, |
| 166 | final ITmfEventParser parser) |
| 167 | throws TmfTraceException { |
| 168 | super(); |
| 169 | fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE; |
| 170 | fStreamingInterval = interval; |
| 171 | fParser = parser; |
| 172 | initialize(resource, path, type); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Copy constructor |
| 177 | * |
| 178 | * @param trace the original trace |
| 179 | * @throws TmfTraceException Should not happen usually |
| 180 | */ |
| 181 | public TmfTrace(final TmfTrace trace) throws TmfTraceException { |
| 182 | super(); |
| 183 | if (trace == null) { |
| 184 | throw new IllegalArgumentException(); |
| 185 | } |
| 186 | fCacheSize = trace.getCacheSize(); |
| 187 | fStreamingInterval = trace.getStreamingInterval(); |
| 188 | fParser = trace.fParser; |
| 189 | initialize(trace.getResource(), trace.getPath(), trace.getEventType()); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Creates the indexer instance. Classes extending this class can override |
| 194 | * this to provide a different indexer implementation. |
| 195 | * |
| 196 | * @param interval the checkpoints interval |
| 197 | * |
| 198 | * @return the indexer |
| 199 | * @since 3.0 |
| 200 | */ |
| 201 | protected ITmfTraceIndexer createIndexer(int interval) { |
| 202 | return new TmfCheckpointIndexer(this, interval); |
| 203 | } |
| 204 | |
| 205 | // ------------------------------------------------------------------------ |
| 206 | // ITmfTrace - Initializers |
| 207 | // ------------------------------------------------------------------------ |
| 208 | |
| 209 | @Override |
| 210 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type, String name) throws TmfTraceException { |
| 211 | setName(name); |
| 212 | initTrace(resource, path, type); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
| 217 | initialize(resource, path, type); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Initialize the trace common attributes and the base component. |
| 222 | * |
| 223 | * @param resource the Eclipse resource (trace) |
| 224 | * @param path the trace path |
| 225 | * @param type the trace event type |
| 226 | * |
| 227 | * @throws TmfTraceException If something failed during the initialization |
| 228 | */ |
| 229 | protected void initialize(final IResource resource, |
| 230 | final String path, |
| 231 | final Class<? extends ITmfEvent> type) |
| 232 | throws TmfTraceException { |
| 233 | if (path == null) { |
| 234 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
| 235 | } |
| 236 | fPath = path; |
| 237 | fResource = resource; |
| 238 | String traceName = getName(); |
| 239 | if (traceName == null || traceName.isEmpty()) { |
| 240 | traceName = (resource != null) ? resource.getName() : new Path(path).lastSegment(); |
| 241 | } |
| 242 | if (fParser == null) { |
| 243 | if (this instanceof ITmfEventParser) { |
| 244 | fParser = (ITmfEventParser) this; |
| 245 | } else { |
| 246 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ |
| 247 | } |
| 248 | } |
| 249 | super.init(traceName, type); |
| 250 | // register as VIP after super.init() because TmfComponent registers to signal manager there |
| 251 | TmfSignalManager.registerVIP(this); |
| 252 | fIndexer = createIndexer(fCacheSize); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Indicates if the path points to an existing file/directory |
| 257 | * |
| 258 | * @param path the path to test |
| 259 | * @return true if the file/directory exists |
| 260 | */ |
| 261 | protected boolean fileExists(final String path) { |
| 262 | final File file = new File(path); |
| 263 | return file.exists(); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @since 2.0 |
| 268 | */ |
| 269 | @Override |
| 270 | public void indexTrace(boolean waitForCompletion) { |
| 271 | getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Instantiate the applicable analysis modules and executes the analysis |
| 276 | * modules that are meant to be automatically executed |
| 277 | * |
| 278 | * @return An IStatus indicating whether the analysis could be run |
| 279 | * successfully or not |
| 280 | * @since 3.0 |
| 281 | */ |
| 282 | protected IStatus executeAnalysis() { |
| 283 | MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null); |
| 284 | Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(this.getClass()); |
| 285 | for (IAnalysisModuleHelper helper : modules.values()) { |
| 286 | try { |
| 287 | IAnalysisModule module = helper.newModule(this); |
| 288 | fAnalysisModules.put(module.getId(), module); |
| 289 | if (module.isAutomatic()) { |
| 290 | status.add(module.schedule()); |
| 291 | } |
| 292 | } catch (TmfAnalysisException e) { |
| 293 | status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e)); |
| 294 | } |
| 295 | } |
| 296 | return status; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @since 3.0 |
| 301 | */ |
| 302 | @Override |
| 303 | public IAnalysisModule getAnalysisModule(String analysisId) { |
| 304 | return fAnalysisModules.get(analysisId); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | * @since 3.0 |
| 310 | */ |
| 311 | @Override |
| 312 | public Iterable<IAnalysisModule> getAnalysisModules() { |
| 313 | synchronized (fAnalysisModules) { |
| 314 | Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values()); |
| 315 | return modules; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @since 3.0 |
| 321 | */ |
| 322 | @Override |
| 323 | public <T extends IAnalysisModule> T getAnalysisModuleOfClass(Class<T> moduleClass, String id) { |
| 324 | Iterable<T> modules = getAnalysisModulesOfClass(moduleClass); |
| 325 | for (T module : modules) { |
| 326 | if (id.equals(module.getId())) { |
| 327 | return module; |
| 328 | } |
| 329 | } |
| 330 | return null; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @since 3.0 |
| 335 | */ |
| 336 | @Override |
| 337 | public <T> Iterable<T> getAnalysisModulesOfClass(Class<T> moduleClass) { |
| 338 | Set<T> modules = new HashSet<>(); |
| 339 | synchronized (fAnalysisModules) { |
| 340 | for (Entry<String, IAnalysisModule> entry : fAnalysisModules.entrySet()) { |
| 341 | if (moduleClass.isAssignableFrom(entry.getValue().getClass())) { |
| 342 | modules.add(moduleClass.cast(entry.getValue())); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | return modules; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Clears the trace |
| 351 | */ |
| 352 | @Override |
| 353 | public synchronized void dispose() { |
| 354 | /* Clean up the index if applicable */ |
| 355 | if (getIndexer() != null) { |
| 356 | getIndexer().dispose(); |
| 357 | } |
| 358 | |
| 359 | /* Clean up the analysis modules */ |
| 360 | synchronized (fAnalysisModules) { |
| 361 | for (IAnalysisModule module : fAnalysisModules.values()) { |
| 362 | module.dispose(); |
| 363 | } |
| 364 | fAnalysisModules.clear(); |
| 365 | } |
| 366 | |
| 367 | super.dispose(); |
| 368 | } |
| 369 | |
| 370 | // ------------------------------------------------------------------------ |
| 371 | // ITmfTrace - Basic getters |
| 372 | // ------------------------------------------------------------------------ |
| 373 | |
| 374 | @Override |
| 375 | public Class<? extends ITmfEvent> getEventType() { |
| 376 | return super.getType(); |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public IResource getResource() { |
| 381 | return fResource; |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public String getPath() { |
| 386 | return fPath; |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public int getCacheSize() { |
| 391 | return fCacheSize; |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public long getStreamingInterval() { |
| 396 | return fStreamingInterval; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @return the trace indexer |
| 401 | * @since 3.0 |
| 402 | */ |
| 403 | protected ITmfTraceIndexer getIndexer() { |
| 404 | return fIndexer; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @return the trace parser |
| 409 | */ |
| 410 | protected ITmfEventParser getParser() { |
| 411 | return fParser; |
| 412 | } |
| 413 | |
| 414 | // ------------------------------------------------------------------------ |
| 415 | // ITmfTrace - Trace characteristics getters |
| 416 | // ------------------------------------------------------------------------ |
| 417 | |
| 418 | @Override |
| 419 | public long getNbEvents() { |
| 420 | return fNbEvents; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @since 2.0 |
| 425 | */ |
| 426 | @Override |
| 427 | public TmfTimeRange getTimeRange() { |
| 428 | return new TmfTimeRange(fStartTime, fEndTime); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * @since 2.0 |
| 433 | */ |
| 434 | @Override |
| 435 | public ITmfTimestamp getStartTime() { |
| 436 | return fStartTime; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @since 2.0 |
| 441 | */ |
| 442 | @Override |
| 443 | public ITmfTimestamp getEndTime() { |
| 444 | return fEndTime; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @since 2.0 |
| 449 | */ |
| 450 | @Override |
| 451 | public ITmfTimestamp getInitialRangeOffset() { |
| 452 | final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
| 453 | return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @since 3.0 |
| 458 | */ |
| 459 | @Override |
| 460 | public String getHostId() { |
| 461 | return this.getName(); |
| 462 | } |
| 463 | |
| 464 | // ------------------------------------------------------------------------ |
| 465 | // Convenience setters |
| 466 | // ------------------------------------------------------------------------ |
| 467 | |
| 468 | /** |
| 469 | * Set the trace cache size. Must be done at initialization time. |
| 470 | * |
| 471 | * @param cacheSize The trace cache size |
| 472 | */ |
| 473 | protected void setCacheSize(final int cacheSize) { |
| 474 | fCacheSize = cacheSize; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Set the trace known number of events. This can be quite dynamic |
| 479 | * during indexing or for live traces. |
| 480 | * |
| 481 | * @param nbEvents The number of events |
| 482 | */ |
| 483 | protected synchronized void setNbEvents(final long nbEvents) { |
| 484 | fNbEvents = (nbEvents > 0) ? nbEvents : 0; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Update the trace events time range |
| 489 | * |
| 490 | * @param range the new time range |
| 491 | * @since 2.0 |
| 492 | */ |
| 493 | protected void setTimeRange(final TmfTimeRange range) { |
| 494 | fStartTime = range.getStartTime(); |
| 495 | fEndTime = range.getEndTime(); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Update the trace chronologically first event timestamp |
| 500 | * |
| 501 | * @param startTime the new first event timestamp |
| 502 | * @since 2.0 |
| 503 | */ |
| 504 | protected void setStartTime(final ITmfTimestamp startTime) { |
| 505 | fStartTime = startTime; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Update the trace chronologically last event timestamp |
| 510 | * |
| 511 | * @param endTime the new last event timestamp |
| 512 | * @since 2.0 |
| 513 | */ |
| 514 | protected void setEndTime(final ITmfTimestamp endTime) { |
| 515 | fEndTime = endTime; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Set the polling interval for live traces (default = 0 = no streaming). |
| 520 | * |
| 521 | * @param interval the new trace streaming interval |
| 522 | */ |
| 523 | protected void setStreamingInterval(final long interval) { |
| 524 | fStreamingInterval = (interval > 0) ? interval : 0; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Set the trace parser. Must be done at initialization time. |
| 529 | * |
| 530 | * @param parser the new trace parser |
| 531 | */ |
| 532 | protected void setParser(final ITmfEventParser parser) { |
| 533 | fParser = parser; |
| 534 | } |
| 535 | |
| 536 | // ------------------------------------------------------------------------ |
| 537 | // ITmfTrace - SeekEvent operations (returning a trace context) |
| 538 | // ------------------------------------------------------------------------ |
| 539 | |
| 540 | @Override |
| 541 | public synchronized ITmfContext seekEvent(final long rank) { |
| 542 | |
| 543 | // A rank <= 0 indicates to seek the first event |
| 544 | if (rank <= 0) { |
| 545 | ITmfContext context = seekEvent((ITmfLocation) null); |
| 546 | context.setRank(0); |
| 547 | return context; |
| 548 | } |
| 549 | |
| 550 | // Position the trace at the checkpoint |
| 551 | final ITmfContext context = fIndexer.seekIndex(rank); |
| 552 | |
| 553 | // And locate the requested event context |
| 554 | long pos = context.getRank(); |
| 555 | if (pos < rank) { |
| 556 | ITmfEvent event = getNext(context); |
| 557 | while ((event != null) && (++pos < rank)) { |
| 558 | event = getNext(context); |
| 559 | } |
| 560 | } |
| 561 | return context; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * @since 2.0 |
| 566 | */ |
| 567 | @Override |
| 568 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
| 569 | |
| 570 | // A null timestamp indicates to seek the first event |
| 571 | if (timestamp == null) { |
| 572 | ITmfContext context = seekEvent((ITmfLocation) null); |
| 573 | context.setRank(0); |
| 574 | return context; |
| 575 | } |
| 576 | |
| 577 | // Position the trace at the checkpoint |
| 578 | ITmfContext context = fIndexer.seekIndex(timestamp); |
| 579 | |
| 580 | // And locate the requested event context |
| 581 | ITmfLocation previousLocation = context.getLocation(); |
| 582 | long previousRank = context.getRank(); |
| 583 | ITmfEvent event = getNext(context); |
| 584 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
| 585 | previousLocation = context.getLocation(); |
| 586 | previousRank = context.getRank(); |
| 587 | event = getNext(context); |
| 588 | } |
| 589 | if (event == null) { |
| 590 | context.setLocation(null); |
| 591 | context.setRank(ITmfContext.UNKNOWN_RANK); |
| 592 | } else { |
| 593 | context.dispose(); |
| 594 | context = seekEvent(previousLocation); |
| 595 | context.setRank(previousRank); |
| 596 | } |
| 597 | return context; |
| 598 | } |
| 599 | |
| 600 | // ------------------------------------------------------------------------ |
| 601 | // ITmfTrace - Read operations (returning an actual event) |
| 602 | // ------------------------------------------------------------------------ |
| 603 | |
| 604 | @Override |
| 605 | public synchronized ITmfEvent getNext(final ITmfContext context) { |
| 606 | // parseEvent() does not update the context |
| 607 | final ITmfEvent event = fParser.parseEvent(context); |
| 608 | if (event != null) { |
| 609 | updateAttributes(context, event.getTimestamp()); |
| 610 | context.setLocation(getCurrentLocation()); |
| 611 | context.increaseRank(); |
| 612 | processEvent(event); |
| 613 | } |
| 614 | return event; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Hook for special event processing by the concrete class |
| 619 | * (called by TmfTrace.getEvent()) |
| 620 | * |
| 621 | * @param event the event |
| 622 | */ |
| 623 | protected void processEvent(final ITmfEvent event) { |
| 624 | // Do nothing |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Update the trace attributes |
| 629 | * |
| 630 | * @param context the current trace context |
| 631 | * @param timestamp the corresponding timestamp |
| 632 | * @since 2.0 |
| 633 | */ |
| 634 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { |
| 635 | if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) { |
| 636 | fStartTime = timestamp; |
| 637 | } |
| 638 | if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) { |
| 639 | fEndTime = timestamp; |
| 640 | } |
| 641 | if (context.hasValidRank()) { |
| 642 | long rank = context.getRank(); |
| 643 | if (fNbEvents <= rank) { |
| 644 | fNbEvents = rank + 1; |
| 645 | } |
| 646 | if (fIndexer != null) { |
| 647 | fIndexer.updateIndex(context, timestamp); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // ------------------------------------------------------------------------ |
| 653 | // TmfDataProvider |
| 654 | // ------------------------------------------------------------------------ |
| 655 | |
| 656 | /** |
| 657 | * @since 2.0 |
| 658 | */ |
| 659 | @Override |
| 660 | public synchronized ITmfContext armRequest(final ITmfEventRequest request) { |
| 661 | if (executorIsShutdown()) { |
| 662 | return null; |
| 663 | } |
| 664 | if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime()) |
| 665 | && (request.getIndex() == 0)) { |
| 666 | final ITmfContext context = seekEvent(request.getRange().getStartTime()); |
| 667 | request.setStartIndex((int) context.getRank()); |
| 668 | return context; |
| 669 | |
| 670 | } |
| 671 | return seekEvent(request.getIndex()); |
| 672 | } |
| 673 | |
| 674 | // ------------------------------------------------------------------------ |
| 675 | // Signal handlers |
| 676 | // ------------------------------------------------------------------------ |
| 677 | |
| 678 | /** |
| 679 | * Handler for the Trace Opened signal |
| 680 | * |
| 681 | * @param signal |
| 682 | * The incoming signal |
| 683 | * @since 2.0 |
| 684 | */ |
| 685 | @TmfSignalHandler |
| 686 | public void traceOpened(TmfTraceOpenedSignal signal) { |
| 687 | boolean signalIsForUs = false; |
| 688 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) { |
| 689 | if (trace == this) { |
| 690 | signalIsForUs = true; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (!signalIsForUs) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * The signal is either for this trace, or for an experiment containing |
| 701 | * this trace. |
| 702 | */ |
| 703 | IStatus status = executeAnalysis(); |
| 704 | if (!status.isOK()) { |
| 705 | Activator.log(status); |
| 706 | } |
| 707 | |
| 708 | TmfTraceManager.refreshSupplementaryFiles(this); |
| 709 | |
| 710 | if (signal.getTrace() == this) { |
| 711 | /* Additionally, the signal is directly for this trace. */ |
| 712 | if (getNbEvents() == 0) { |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* For a streaming trace, the range updated signal should be sent |
| 717 | * by the subclass when a new safe time is determined. |
| 718 | */ |
| 719 | if (getStreamingInterval() > 0) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH); |
| 724 | final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange); |
| 725 | |
| 726 | // Broadcast in separate thread to prevent deadlock |
| 727 | broadcastAsync(rangeUpdatedsignal); |
| 728 | return; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Signal handler for the TmfTraceRangeUpdatedSignal signal |
| 734 | * |
| 735 | * @param signal The incoming signal |
| 736 | * @since 2.0 |
| 737 | */ |
| 738 | @TmfSignalHandler |
| 739 | public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) { |
| 740 | if (signal.getTrace() == this) { |
| 741 | getIndexer().buildIndex(getNbEvents(), signal.getRange(), false); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Signal handler for the TmfTraceUpdatedSignal signal |
| 747 | * |
| 748 | * @param signal The incoming signal |
| 749 | * @since 3.0 |
| 750 | */ |
| 751 | @TmfSignalHandler |
| 752 | public void traceUpdated(final TmfTraceUpdatedSignal signal) { |
| 753 | if (signal.getSource() == getIndexer()) { |
| 754 | fNbEvents = signal.getNbEvents(); |
| 755 | fStartTime = signal.getRange().getStartTime(); |
| 756 | fEndTime = signal.getRange().getEndTime(); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Returns the file resource used to store synchronization formula. The file |
| 762 | * may not exist. |
| 763 | * |
| 764 | * @return the synchronization file |
| 765 | */ |
| 766 | private File getSyncFormulaFile() { |
| 767 | File file = null; |
| 768 | if (fResource instanceof IFolder) { |
| 769 | try { |
| 770 | String supplDirectory; |
| 771 | |
| 772 | supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER); |
| 773 | |
| 774 | file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE); |
| 775 | |
| 776 | } catch (CoreException e) { |
| 777 | |
| 778 | } |
| 779 | } |
| 780 | return file; |
| 781 | } |
| 782 | |
| 783 | // ------------------------------------------------------------------------ |
| 784 | // Timestamp transformation functions |
| 785 | // ------------------------------------------------------------------------ |
| 786 | |
| 787 | /** |
| 788 | * @since 3.0 |
| 789 | */ |
| 790 | @Override |
| 791 | public ITmfTimestampTransform getTimestampTransform() { |
| 792 | if (fTsTransform == null) { |
| 793 | /* Check if a formula is stored somewhere in the resources */ |
| 794 | File sync_file = getSyncFormulaFile(); |
| 795 | if (sync_file != null && sync_file.exists()) { |
| 796 | |
| 797 | try (FileInputStream fis = new FileInputStream(sync_file); |
| 798 | ObjectInputStream ois = new ObjectInputStream(fis);) { |
| 799 | |
| 800 | fTsTransform = (ITmfTimestampTransform) ois.readObject(); |
| 801 | |
| 802 | } catch (ClassNotFoundException | IOException e) { |
| 803 | fTsTransform = TmfTimestampTransform.IDENTITY; |
| 804 | } |
| 805 | } else { |
| 806 | fTsTransform = TmfTimestampTransform.IDENTITY; |
| 807 | } |
| 808 | } |
| 809 | return fTsTransform; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @since 3.0 |
| 814 | */ |
| 815 | @Override |
| 816 | public void setTimestampTransform(final ITmfTimestampTransform tt) { |
| 817 | fTsTransform = tt; |
| 818 | |
| 819 | /* Save the timestamp transform to a file */ |
| 820 | File sync_file = getSyncFormulaFile(); |
| 821 | if (sync_file != null) { |
| 822 | if (sync_file.exists()) { |
| 823 | sync_file.delete(); |
| 824 | } |
| 825 | FileOutputStream fos; |
| 826 | ObjectOutputStream oos; |
| 827 | |
| 828 | /* Save the header of the file */ |
| 829 | try { |
| 830 | fos = new FileOutputStream(sync_file, false); |
| 831 | oos = new ObjectOutputStream(fos); |
| 832 | |
| 833 | oos.writeObject(fTsTransform); |
| 834 | oos.close(); |
| 835 | fos.close(); |
| 836 | } catch (IOException e1) { |
| 837 | Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$ |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * @since 3.0 |
| 844 | */ |
| 845 | @Override |
| 846 | public ITmfTimestamp createTimestamp(long ts) { |
| 847 | return new TmfTimestamp(getTimestampTransform().transform(ts)); |
| 848 | } |
| 849 | |
| 850 | // ------------------------------------------------------------------------ |
| 851 | // toString |
| 852 | // ------------------------------------------------------------------------ |
| 853 | |
| 854 | @Override |
| 855 | @SuppressWarnings("nls") |
| 856 | public synchronized String toString() { |
| 857 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
| 858 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime |
| 859 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; |
| 860 | } |
| 861 | |
| 862 | } |