| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010, 2012 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 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.core.trace; |
| 15 | |
| 16 | import java.io.File; |
| 17 | import java.io.FileNotFoundException; |
| 18 | |
| 19 | import org.eclipse.core.resources.IResource; |
| 20 | import org.eclipse.core.runtime.Path; |
| 21 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
| 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
| 25 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
| 26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 27 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
| 28 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 29 | |
| 30 | /** |
| 31 | * Abstract implementation of ITmfTrace. |
| 32 | * <p> |
| 33 | * Since the concept of 'location' is trace specific, the concrete classes have |
| 34 | * to provide the related methods, namely: |
| 35 | * <ul> |
| 36 | * <li> public ITmfLocation<?> getCurrentLocation() |
| 37 | * <li> public double getLocationRatio(ITmfLocation<?> location) |
| 38 | * <li> public ITmfContext seekEvent(ITmfLocation<?> location) |
| 39 | * <li> public ITmfContext seekEvent(double ratio) |
| 40 | * <li> public boolean validate(IProject project, String path) |
| 41 | * </ul> |
| 42 | * A concrete trace must provide its corresponding parser. A common way to |
| 43 | * accomplish this is by making the concrete class extend TmfTrace and |
| 44 | * implement ITmfEventParser. |
| 45 | * <p> |
| 46 | * The concrete class can either specify its own indexer or use the provided |
| 47 | * TmfCheckpointIndexer (default). In this case, the trace cache size will be |
| 48 | * used as checkpoint interval. |
| 49 | * |
| 50 | * @since 1.0 |
| 51 | * @version 1.0 |
| 52 | * @author Francois Chouinard |
| 53 | * |
| 54 | * @see ITmfEvent |
| 55 | * @see ITmfTraceIndexer |
| 56 | * @see ITmfEventParser |
| 57 | */ |
| 58 | public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> { |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | // Constants |
| 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | /** |
| 65 | * The default trace cache size |
| 66 | */ |
| 67 | public static final int DEFAULT_TRACE_CACHE_SIZE = 10000; |
| 68 | |
| 69 | // ------------------------------------------------------------------------ |
| 70 | // Attributes |
| 71 | // ------------------------------------------------------------------------ |
| 72 | |
| 73 | // The resource used for persistent properties for this trace |
| 74 | private IResource fResource; |
| 75 | |
| 76 | // The trace path |
| 77 | private String fPath; |
| 78 | |
| 79 | /** |
| 80 | * The trace cache page size |
| 81 | */ |
| 82 | protected int fCacheSize = DEFAULT_TRACE_CACHE_SIZE; |
| 83 | |
| 84 | /** |
| 85 | * The number of events collected (so far) |
| 86 | */ |
| 87 | protected long fNbEvents = 0; |
| 88 | |
| 89 | // The time span of the event stream |
| 90 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH; |
| 91 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; |
| 92 | |
| 93 | /** |
| 94 | * The trace streaming interval (0 = no streaming) |
| 95 | */ |
| 96 | protected long fStreamingInterval = 0; |
| 97 | |
| 98 | /** |
| 99 | * The trace indexer |
| 100 | */ |
| 101 | protected ITmfTraceIndexer<ITmfTrace<ITmfEvent>> fIndexer; |
| 102 | |
| 103 | /** |
| 104 | * The trace parser |
| 105 | */ |
| 106 | protected ITmfEventParser<T> fParser; |
| 107 | |
| 108 | // ------------------------------------------------------------------------ |
| 109 | // Construction |
| 110 | // ------------------------------------------------------------------------ |
| 111 | |
| 112 | /** |
| 113 | * The default, parameterless, constructor |
| 114 | */ |
| 115 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 116 | public TmfTrace() { |
| 117 | super(); |
| 118 | fIndexer = new TmfCheckpointIndexer(this); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * The standard constructor (non-live trace). Applicable when the trace |
| 123 | * implements its own parser and if at checkpoint-based index is OK. |
| 124 | * |
| 125 | * @param resource the resource associated to the trace |
| 126 | * @param type the trace event type |
| 127 | * @param path the trace path |
| 128 | * @param cacheSize the trace cache size |
| 129 | * @throws TmfTraceException |
| 130 | */ |
| 131 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize) throws TmfTraceException { |
| 132 | this(resource, type, path, cacheSize, 0, null); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * The standard constructor (live trace). Applicable when the trace |
| 137 | * implements its own parser and if at checkpoint-based index is OK. |
| 138 | * |
| 139 | * @param resource the resource associated to the trace |
| 140 | * @param type the trace event type |
| 141 | * @param path the trace path |
| 142 | * @param cacheSize the trace cache size |
| 143 | * @param interval the trace streaming interval |
| 144 | * @throws TmfTraceException |
| 145 | */ |
| 146 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, final long interval) throws TmfTraceException { |
| 147 | this(resource, type, path, cacheSize, interval, null); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * The 'non-default indexer' constructor. Allows to provide a trace |
| 152 | * specific indexer. |
| 153 | * |
| 154 | * @param resource the resource associated to the trace |
| 155 | * @param type the trace event type |
| 156 | * @param path the trace path |
| 157 | * @param cacheSize the trace cache size |
| 158 | * @param indexer the trace indexer |
| 159 | * @throws TmfTraceException |
| 160 | */ |
| 161 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, |
| 162 | final long interval, final ITmfTraceIndexer<?> indexer) throws TmfTraceException { |
| 163 | this(resource, type, path, cacheSize, interval, null, null); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * The full constructor where trace specific indexer/parser are provided. |
| 168 | * |
| 169 | * @param resource the resource associated to the trace |
| 170 | * @param type the trace event type |
| 171 | * @param path the trace path |
| 172 | * @param cacheSize the trace cache size |
| 173 | * @param indexer the trace indexer |
| 174 | * @param parser the trace event parser |
| 175 | * @throws TmfTraceException |
| 176 | */ |
| 177 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 178 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, |
| 179 | final long interval, final ITmfTraceIndexer<?> indexer, final ITmfEventParser<T> parser) throws TmfTraceException { |
| 180 | super(); |
| 181 | fCacheSize = (cacheSize > 0) ? cacheSize : DEFAULT_TRACE_CACHE_SIZE; |
| 182 | fStreamingInterval = interval; |
| 183 | fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize); |
| 184 | fParser = parser; |
| 185 | initialize(resource, path, type); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Copy constructor |
| 190 | * |
| 191 | * @param trace the original trace |
| 192 | */ |
| 193 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 194 | public TmfTrace(final TmfTrace<T> trace) throws TmfTraceException { |
| 195 | super(); |
| 196 | if (trace == null) |
| 197 | throw new IllegalArgumentException(); |
| 198 | fCacheSize = trace.getCacheSize(); |
| 199 | fStreamingInterval = trace.getStreamingInterval(); |
| 200 | fIndexer = new TmfCheckpointIndexer(this); |
| 201 | fParser = trace.fParser; |
| 202 | initialize(trace.getResource(), trace.getPath(), trace.getEventType()); |
| 203 | } |
| 204 | |
| 205 | // ------------------------------------------------------------------------ |
| 206 | // ITmfTrace - Initializers |
| 207 | // ------------------------------------------------------------------------ |
| 208 | |
| 209 | /* (non-Javadoc) |
| 210 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class) |
| 211 | */ |
| 212 | @Override |
| 213 | public void initTrace(final IResource resource, final String path, final Class<T> type) throws TmfTraceException { |
| 214 | initialize(resource, path, type); |
| 215 | fIndexer.buildIndex(false); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Initialize the trace common attributes and the base component. |
| 220 | * |
| 221 | * @param resource the Eclipse resource (trace) |
| 222 | * @param path the trace path |
| 223 | * @param type the trace event type |
| 224 | * |
| 225 | * @throws FileNotFoundException |
| 226 | */ |
| 227 | @SuppressWarnings("unchecked") |
| 228 | protected void initialize(final IResource resource, final String path, final Class<T> type) throws TmfTraceException { |
| 229 | if (path == null) |
| 230 | throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$ |
| 231 | fPath = path; |
| 232 | fResource = resource; |
| 233 | String traceName = (resource != null) ? resource.getName() : null; |
| 234 | // If no resource was provided, extract the display name the trace path |
| 235 | if (traceName == null) { |
| 236 | final int sep = path.lastIndexOf(Path.SEPARATOR); |
| 237 | traceName = (sep >= 0) ? path.substring(sep + 1) : path; |
| 238 | } |
| 239 | if (fParser == null) { |
| 240 | if (this instanceof ITmfEventParser) { |
| 241 | fParser = (ITmfEventParser<T>) this; |
| 242 | } else { |
| 243 | throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$ |
| 244 | } |
| 245 | } |
| 246 | super.init(traceName, type); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Indicates if the path points to an existing file/directory |
| 251 | * |
| 252 | * @param path the path to test |
| 253 | * @return true if the file/directory exists |
| 254 | */ |
| 255 | protected boolean fileExists(final String path) { |
| 256 | final File file = new File(path); |
| 257 | return file.exists(); |
| 258 | } |
| 259 | |
| 260 | // ------------------------------------------------------------------------ |
| 261 | // ITmfTrace - Basic getters |
| 262 | // ------------------------------------------------------------------------ |
| 263 | |
| 264 | /* (non-Javadoc) |
| 265 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType() |
| 266 | */ |
| 267 | @Override |
| 268 | @SuppressWarnings("unchecked") |
| 269 | public Class<T> getEventType() { |
| 270 | return (Class<T>) super.getType(); |
| 271 | } |
| 272 | |
| 273 | /* (non-Javadoc) |
| 274 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource() |
| 275 | */ |
| 276 | @Override |
| 277 | public IResource getResource() { |
| 278 | return fResource; |
| 279 | } |
| 280 | |
| 281 | /* (non-Javadoc) |
| 282 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath() |
| 283 | */ |
| 284 | @Override |
| 285 | public String getPath() { |
| 286 | return fPath; |
| 287 | } |
| 288 | |
| 289 | /* (non-Javadoc) |
| 290 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize() |
| 291 | */ |
| 292 | @Override |
| 293 | public int getCacheSize() { |
| 294 | return fCacheSize; |
| 295 | } |
| 296 | |
| 297 | /* (non-Javadoc) |
| 298 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval() |
| 299 | */ |
| 300 | @Override |
| 301 | public long getStreamingInterval() { |
| 302 | return fStreamingInterval; |
| 303 | } |
| 304 | |
| 305 | // ------------------------------------------------------------------------ |
| 306 | // ITmfTrace - Trace characteristics getters |
| 307 | // ------------------------------------------------------------------------ |
| 308 | |
| 309 | /* (non-Javadoc) |
| 310 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents() |
| 311 | */ |
| 312 | @Override |
| 313 | public synchronized long getNbEvents() { |
| 314 | return fNbEvents; |
| 315 | } |
| 316 | |
| 317 | /* (non-Javadoc) |
| 318 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange() |
| 319 | */ |
| 320 | @Override |
| 321 | public TmfTimeRange getTimeRange() { |
| 322 | return new TmfTimeRange(fStartTime, fEndTime); |
| 323 | } |
| 324 | |
| 325 | /* (non-Javadoc) |
| 326 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime() |
| 327 | */ |
| 328 | @Override |
| 329 | public ITmfTimestamp getStartTime() { |
| 330 | return fStartTime.clone(); |
| 331 | } |
| 332 | |
| 333 | /* (non-Javadoc) |
| 334 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime() |
| 335 | */ |
| 336 | @Override |
| 337 | public ITmfTimestamp getEndTime() { |
| 338 | return fEndTime.clone(); |
| 339 | } |
| 340 | |
| 341 | // ------------------------------------------------------------------------ |
| 342 | // Convenience setters |
| 343 | // ------------------------------------------------------------------------ |
| 344 | |
| 345 | /** |
| 346 | * Update the trace events time range |
| 347 | * |
| 348 | * @param range the new time range |
| 349 | */ |
| 350 | protected void setTimeRange(final TmfTimeRange range) { |
| 351 | fStartTime = range.getStartTime().clone(); |
| 352 | fEndTime = range.getEndTime().clone(); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Update the trace chronologically first event timestamp |
| 357 | * |
| 358 | * @param startTime the new first event timestamp |
| 359 | */ |
| 360 | protected void setStartTime(final ITmfTimestamp startTime) { |
| 361 | fStartTime = startTime.clone(); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Update the trace chronologically last event timestamp |
| 366 | * |
| 367 | * @param endTime the new last event timestamp |
| 368 | */ |
| 369 | protected void setEndTime(final ITmfTimestamp endTime) { |
| 370 | fEndTime = endTime.clone(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Update the trace streaming interval |
| 375 | * |
| 376 | * @param interval the new trace streaming interval |
| 377 | */ |
| 378 | protected void setStreamingInterval(final long interval) { |
| 379 | fStreamingInterval = (interval > 0) ? interval : 0; |
| 380 | } |
| 381 | |
| 382 | // ------------------------------------------------------------------------ |
| 383 | // ITmfTrace - SeekEvent operations (returning a trace context) |
| 384 | // ------------------------------------------------------------------------ |
| 385 | |
| 386 | /* (non-Javadoc) |
| 387 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long) |
| 388 | */ |
| 389 | @Override |
| 390 | public synchronized ITmfContext seekEvent(final long rank) { |
| 391 | |
| 392 | // A rank <= 0 indicates to seek the first event |
| 393 | if (rank <= 0) { |
| 394 | ITmfContext context = seekEvent((ITmfLocation<?>) null); |
| 395 | context.setRank(0); |
| 396 | return context; |
| 397 | } |
| 398 | |
| 399 | // Position the trace at the checkpoint |
| 400 | final ITmfContext context = fIndexer.seekIndex(rank); |
| 401 | |
| 402 | // And locate the requested event context |
| 403 | long pos = context.getRank(); |
| 404 | if (pos < rank) { |
| 405 | ITmfEvent event = readNextEvent(context); |
| 406 | while (event != null && ++pos < rank) { |
| 407 | event = readNextEvent(context); |
| 408 | } |
| 409 | } |
| 410 | return context; |
| 411 | } |
| 412 | |
| 413 | /* (non-Javadoc) |
| 414 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) |
| 415 | */ |
| 416 | @Override |
| 417 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
| 418 | |
| 419 | // A null timestamp indicates to seek the first event |
| 420 | if (timestamp == null) { |
| 421 | ITmfContext context = seekEvent((ITmfLocation<?>) null); |
| 422 | context.setRank(0); |
| 423 | return context; |
| 424 | } |
| 425 | |
| 426 | // Position the trace at the checkpoint |
| 427 | final ITmfContext context = fIndexer.seekIndex(timestamp); |
| 428 | |
| 429 | // And locate the requested event context |
| 430 | final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... |
| 431 | ITmfEvent event = readNextEvent(nextEventContext); |
| 432 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
| 433 | context.setLocation(nextEventContext.getLocation().clone()); |
| 434 | context.increaseRank(); |
| 435 | event = readNextEvent(nextEventContext); |
| 436 | } |
| 437 | return context; |
| 438 | } |
| 439 | |
| 440 | // ------------------------------------------------------------------------ |
| 441 | // ITmfTrace - Read operations (returning an actual event) |
| 442 | // ------------------------------------------------------------------------ |
| 443 | |
| 444 | /* (non-Javadoc) |
| 445 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext) |
| 446 | */ |
| 447 | @Override |
| 448 | public synchronized ITmfEvent readNextEvent(final ITmfContext context) { |
| 449 | // parseEvent() does not update the context |
| 450 | final ITmfEvent event = fParser.parseEvent(context); |
| 451 | if (event != null) { |
| 452 | updateAttributes(context, event.getTimestamp()); |
| 453 | context.setLocation(getCurrentLocation()); |
| 454 | context.increaseRank(); |
| 455 | processEvent(event); |
| 456 | } |
| 457 | return event; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Hook for special event processing by the concrete class |
| 462 | * (called by TmfTrace.getEvent()) |
| 463 | * |
| 464 | * @param event the event |
| 465 | */ |
| 466 | protected void processEvent(final ITmfEvent event) { |
| 467 | // Do nothing |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Update the trace attributes |
| 472 | * |
| 473 | * @param context the current trace context |
| 474 | * @param timestamp the corresponding timestamp |
| 475 | */ |
| 476 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { |
| 477 | if (fStartTime.compareTo(timestamp, false) > 0) { |
| 478 | fStartTime = timestamp; |
| 479 | } |
| 480 | if (fEndTime.compareTo(timestamp, false) < 0) { |
| 481 | fEndTime = timestamp; |
| 482 | } |
| 483 | if (context.hasValidRank()) { |
| 484 | long rank = context.getRank(); |
| 485 | if (fNbEvents <= rank) { |
| 486 | fNbEvents = rank + 1; |
| 487 | } |
| 488 | fIndexer.updateIndex(context, timestamp); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // ------------------------------------------------------------------------ |
| 493 | // TmfDataProvider |
| 494 | // ------------------------------------------------------------------------ |
| 495 | |
| 496 | /* (non-Javadoc) |
| 497 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest) |
| 498 | */ |
| 499 | @Override |
| 500 | public ITmfContext armRequest(final ITmfDataRequest<T> request) { |
| 501 | if (request instanceof ITmfEventRequest<?> |
| 502 | && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime()) |
| 503 | && request.getIndex() == 0) { |
| 504 | final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime()); |
| 505 | ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank()); |
| 506 | return context; |
| 507 | |
| 508 | } |
| 509 | return seekEvent(request.getIndex()); |
| 510 | } |
| 511 | |
| 512 | /* (non-Javadoc) |
| 513 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext) |
| 514 | */ |
| 515 | @Override |
| 516 | @SuppressWarnings("unchecked") |
| 517 | public T getNext(final ITmfContext context) { |
| 518 | if (context instanceof TmfContext) |
| 519 | return (T) readNextEvent(context); |
| 520 | return null; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | // ------------------------------------------------------------------------ |
| 525 | // toString |
| 526 | // ------------------------------------------------------------------------ |
| 527 | |
| 528 | /* (non-Javadoc) |
| 529 | * @see java.lang.Object#toString() |
| 530 | */ |
| 531 | @Override |
| 532 | @SuppressWarnings("nls") |
| 533 | public synchronized String toString() { |
| 534 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
| 535 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime |
| 536 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; |
| 537 | } |
| 538 | |
| 539 | } |