Added latency analysis feature (bug 331467)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
e31e01e8 2 * Copyright (c) 2009, 2010 Ericsson
8c8bf09f
ASL
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
6c13869b 13package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 14
6f4a1d2b 15import java.io.File;
62d1696a 16import java.io.FileNotFoundException;
62d1696a 17import java.util.Collections;
8c8bf09f
ASL
18import java.util.Vector;
19
12c155f5 20import org.eclipse.core.resources.IProject;
05bd3318
FC
21import org.eclipse.core.runtime.IProgressMonitor;
22import org.eclipse.core.runtime.IStatus;
c7e2f194 23import org.eclipse.core.runtime.Path;
05bd3318
FC
24import org.eclipse.core.runtime.Status;
25import org.eclipse.core.runtime.jobs.Job;
6c13869b
FC
26import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
27import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
28import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
29import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
30import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
31import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
32import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
33import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
34import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
8c8bf09f
ASL
35
36/**
146a887c 37 * <b><u>TmfTrace</u></b>
8c8bf09f 38 * <p>
12c155f5
FC
39 * Abstract implementation of ITmfTrace. It should be sufficient to extend this class and provide implementation for
40 * <code>getCurrentLocation()</code> and <code>seekLocation()</code>, as well as a proper parser, to have a working
4e3aa37d 41 * concrete implementation.
ff4ed569 42 * <p>
12c155f5
FC
43 * Note: The notion of event rank is still under heavy discussion. Although used by the Events View and probably useful
44 * in the general case, there is no easy way to implement it for LTTng (actually a strong case is being made that this
45 * is useless).
ff4ed569 46 * <p>
12c155f5
FC
47 * That it is not supported by LTTng does by no mean indicate that it is not useful for (just about) every other tracing
48 * tool. Therefore, this class provides a minimal (and partial) implementation of rank. However, the current
ff4ed569 49 * implementation should not be relied on in the general case.
54d55ced 50 *
4e3aa37d 51 * TODO: Add support for live streaming (notifications, incremental indexing, ...)
8c8bf09f 52 */
12c155f5 53public abstract class TmfTrace<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T>, Cloneable {
62d1696a 54
e31e01e8 55 // ------------------------------------------------------------------------
62d1696a 56 // Constants
e31e01e8 57 // ------------------------------------------------------------------------
62d1696a
FC
58
59 // The default number of events to cache
12c155f5 60 // TODO: Make the DEFAULT_CACHE_SIZE a preference
b12f4544 61 public static final int DEFAULT_INDEX_PAGE_SIZE = 50000;
8c8bf09f 62
e31e01e8 63 // ------------------------------------------------------------------------
8c8bf09f 64 // Attributes
e31e01e8 65 // ------------------------------------------------------------------------
8c8bf09f 66
b0a282fb 67 // The trace path
12c155f5 68 private String fPath;
b0a282fb 69
00641a97 70 // The trace name
c5b45b39 71 private String fTraceName;
00641a97 72
8d2e2848 73 // The cache page size AND checkpoints interval
12c155f5 74 protected int fIndexPageSize = DEFAULT_INDEX_PAGE_SIZE;
62d1696a
FC
75
76 // The set of event stream checkpoints (for random access)
9f584e4c 77 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
62d1696a
FC
78
79 // The number of events collected
a3fe52fc 80 protected long fNbEvents = 0;
62d1696a
FC
81
82 // The time span of the event stream
cb866e08 83 private TmfTimestamp fStartTime = TmfTimestamp.BigCrunch;
12c155f5 84 private TmfTimestamp fEndTime = TmfTimestamp.BigBang;
62d1696a 85
e31e01e8 86 // ------------------------------------------------------------------------
50adc88e 87 // Constructors
e31e01e8 88 // ------------------------------------------------------------------------
8c8bf09f 89
12c155f5 90 public TmfTrace() {
00641a97 91 super();
12c155f5
FC
92 }
93
94 @Override
95 public void initTrace(String path, Class<T> eventType) throws FileNotFoundException {
96c6806f 96 initTmfTrace(path, eventType, DEFAULT_INDEX_PAGE_SIZE, false, null);
12c155f5
FC
97 }
98
99 @Override
100 public void initTrace(String path, Class<T> eventType, int cacheSize) throws FileNotFoundException {
96c6806f 101 initTmfTrace(path, eventType, cacheSize, false, null);
12c155f5
FC
102 }
103
104 @Override
105 public void initTrace(String path, Class<T> eventType, boolean indexTrace) throws FileNotFoundException {
96c6806f 106 initTmfTrace(path, eventType, DEFAULT_INDEX_PAGE_SIZE, indexTrace, null);
12c155f5
FC
107 }
108
109 @Override
110 public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace) throws FileNotFoundException {
96c6806f 111 initTmfTrace(path, eventType, cacheSize, indexTrace, null);
c5b45b39
FC
112 }
113
96c6806f
PT
114 @Override
115 public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace, String name) throws FileNotFoundException {
116 initTmfTrace(path, eventType, cacheSize, indexTrace, name);
117 }
118
119 private void initTmfTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace, String name) throws FileNotFoundException {
12c155f5 120 fPath = path;
96c6806f
PT
121 if (name != null) {
122 fTraceName = name;
123 }
c5b45b39
FC
124 if (fTraceName == null) {
125 fTraceName = ""; //$NON-NLS-1$
00641a97 126 if (path != null) {
c7e2f194 127 int sep = path.lastIndexOf(Path.SEPARATOR);
c5b45b39 128 fTraceName = (sep >= 0) ? path.substring(sep + 1) : path;
00641a97 129 }
12c155f5 130 }
c5b45b39 131 super.init(fTraceName, eventType);
12c155f5 132 fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_INDEX_PAGE_SIZE;
12c155f5
FC
133 if (indexTrace)
134 indexTrace(false);
135 }
136
137 @Override
138 public boolean validate(IProject project, String path) {
139 File file = new File(path);
140 return file.exists();
141 }
142
ff4ed569
FC
143 /**
144 * @param path
145 * @throws FileNotFoundException
146 */
ce785d7d 147 protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException {
12c155f5 148 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, true);
ff4ed569
FC
149 }
150
62d1696a 151 /**
e31e01e8
FC
152 * @param path
153 * @param cacheSize
62d1696a
FC
154 * @throws FileNotFoundException
155 */
ce785d7d 156 protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException {
12c155f5 157 this(name, type, path, cacheSize, true);
05bd3318
FC
158 }
159
160 /**
161 * @param path
162 * @param indexTrace
163 * @throws FileNotFoundException
164 */
165 protected TmfTrace(String name, Class<T> type, String path, boolean indexTrace) throws FileNotFoundException {
12c155f5 166 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, indexTrace);
05bd3318
FC
167 }
168
169 /**
170 * @param path
171 * @param cacheSize
172 * @param indexTrace
173 * @throws FileNotFoundException
174 */
175 protected TmfTrace(String name, Class<T> type, String path, int cacheSize, boolean indexTrace) throws FileNotFoundException {
00641a97 176 super();
c5b45b39 177 fTraceName = name;
00641a97 178 initTrace(path, type, cacheSize, indexTrace);
8c8bf09f
ASL
179 }
180
ff4ed569 181 @SuppressWarnings("unchecked")
12c155f5
FC
182 @Override
183 public TmfTrace<T> clone() throws CloneNotSupportedException {
184 TmfTrace<T> clone = (TmfTrace<T>) super.clone();
185 clone.fCheckpoints = fCheckpoints;
186 clone.fStartTime = new TmfTimestamp(fStartTime);
187 clone.fEndTime = new TmfTimestamp(fEndTime);
188 return clone;
8c8bf09f
ASL
189 }
190
e31e01e8 191 // ------------------------------------------------------------------------
8c8bf09f 192 // Accessors
e31e01e8 193 // ------------------------------------------------------------------------
8c8bf09f 194
62d1696a 195 /**
b0a282fb 196 * @return the trace path
62d1696a 197 */
d4011df2 198 @Override
12c155f5 199 public String getPath() {
b0a282fb 200 return fPath;
8c8bf09f
ASL
201 }
202
62d1696a
FC
203 /* (non-Javadoc)
204 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
205 */
d4011df2 206 @Override
12c155f5 207 public long getNbEvents() {
62d1696a 208 return fNbEvents;
8c8bf09f
ASL
209 }
210
b0a282fb
FC
211 /**
212 * @return the size of the cache
213 */
d4011df2 214 @Override
12c155f5 215 public int getCacheSize() {
9f584e4c 216 return fIndexPageSize;
b0a282fb
FC
217 }
218
62d1696a
FC
219 /* (non-Javadoc)
220 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
221 */
d4011df2 222 @Override
12c155f5 223 public TmfTimeRange getTimeRange() {
cb866e08 224 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
225 }
226
e31e01e8
FC
227 /* (non-Javadoc)
228 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
229 */
d4011df2 230 @Override
12c155f5
FC
231 public TmfTimestamp getStartTime() {
232 return fStartTime;
146a887c
FC
233 }
234
e31e01e8
FC
235 /* (non-Javadoc)
236 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
237 */
d4011df2 238 @Override
12c155f5
FC
239 public TmfTimestamp getEndTime() {
240 return fEndTime;
146a887c
FC
241 }
242
ff4ed569 243 @SuppressWarnings("unchecked")
12c155f5
FC
244 public Vector<TmfCheckpoint> getCheckpoints() {
245 return (Vector<TmfCheckpoint>) fCheckpoints.clone();
54d55ced
FC
246 }
247
abfad0aa 248 /**
12c155f5
FC
249 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
250 * (if any).
251 *
abfad0aa
FC
252 * @param timestamp
253 * @return
254 */
d4011df2 255 @Override
12c155f5 256 public long getRank(TmfTimestamp timestamp) {
abfad0aa
FC
257 TmfContext context = seekEvent(timestamp);
258 return context.getRank();
259 }
260
e31e01e8 261 // ------------------------------------------------------------------------
8c8bf09f 262 // Operators
e31e01e8 263 // ------------------------------------------------------------------------
8c8bf09f 264
4e3aa37d 265 protected void setTimeRange(TmfTimeRange range) {
12c155f5
FC
266 fStartTime = range.getStartTime();
267 fEndTime = range.getEndTime();
4e3aa37d
FC
268 }
269
270 protected void setStartTime(TmfTimestamp startTime) {
12c155f5 271 fStartTime = startTime;
4e3aa37d
FC
272 }
273
274 protected void setEndTime(TmfTimestamp endTime) {
12c155f5
FC
275 fEndTime = endTime;
276 }
277
278 // ------------------------------------------------------------------------
279 // TmfProvider
280 // ------------------------------------------------------------------------
281
282 @Override
283 public ITmfContext armRequest(ITmfDataRequest<T> request) {
284 if (request instanceof ITmfEventRequest<?>
285 && !TmfTimestamp.BigBang.equals(((ITmfEventRequest<T>) request).getRange().getStartTime()) && request.getIndex() == 0) {
286 ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
287 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
288 return context;
289
290 }
291 return seekEvent(request.getIndex());
292 }
293
294 /**
295 * Return the next piece of data based on the context supplied. The context would typically be updated for the
296 * subsequent read.
297 *
298 * @param context
299 * @return
300 */
301 @SuppressWarnings("unchecked")
302 @Override
303 public T getNext(ITmfContext context) {
304 if (context instanceof TmfContext) {
305 return (T) getNextEvent((TmfContext) context);
306 }
307 return null;
308 }
309
310 // ------------------------------------------------------------------------
311 // ITmfTrace
312 // ------------------------------------------------------------------------
e31e01e8 313
146a887c
FC
314 /* (non-Javadoc)
315 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
316 */
d4011df2 317 @Override
12c155f5 318 public TmfContext seekEvent(TmfTimestamp timestamp) {
62d1696a 319
12c155f5
FC
320 if (timestamp == null) {
321 timestamp = TmfTimestamp.BigBang;
322 }
4e3aa37d 323
12c155f5
FC
324 // First, find the right checkpoint
325 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
62d1696a 326
8d2e2848 327 // In the very likely case that the checkpoint was not found, bsearch
62d1696a
FC
328 // returns its negated would-be location (not an offset...). From that
329 // index, we can then position the stream and get the event.
330 if (index < 0) {
331 index = Math.max(0, -(index + 2));
332 }
333
334 // Position the stream at the checkpoint
452ad365 335 ITmfLocation<?> location;
e31e01e8 336 synchronized (fCheckpoints) {
12c155f5
FC
337 if (fCheckpoints.size() > 0) {
338 if (index >= fCheckpoints.size()) {
339 index = fCheckpoints.size() - 1;
340 }
341 location = fCheckpoints.elementAt(index).getLocation();
342 } else {
343 location = null;
344 }
8d2e2848 345 }
54d55ced
FC
346 TmfContext context = seekLocation(location);
347 context.setRank(index * fIndexPageSize);
62d1696a 348
54d55ced 349 // And locate the event
ff4ed569 350 TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
62d1696a
FC
351 TmfEvent event = getNextEvent(nextEventContext);
352 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
12c155f5
FC
353 context.setLocation(nextEventContext.getLocation().clone());
354 context.updateRank(1);
355 event = getNextEvent(nextEventContext);
62d1696a
FC
356 }
357
54d55ced 358 return context;
62d1696a
FC
359 }
360
146a887c
FC
361 /* (non-Javadoc)
362 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
363 */
d4011df2 364 @Override
12c155f5 365 public TmfContext seekEvent(long rank) {
62d1696a
FC
366
367 // Position the stream at the previous checkpoint
9f584e4c 368 int index = (int) rank / fIndexPageSize;
452ad365 369 ITmfLocation<?> location;
e31e01e8 370 synchronized (fCheckpoints) {
12c155f5
FC
371 if (fCheckpoints.size() == 0) {
372 location = null;
373 } else {
374 if (index >= fCheckpoints.size()) {
375 index = fCheckpoints.size() - 1;
376 }
377 location = fCheckpoints.elementAt(index).getLocation();
378 }
8d2e2848 379 }
54d55ced 380
9f584e4c
FC
381 TmfContext context = seekLocation(location);
382 long pos = index * fIndexPageSize;
383 context.setRank(pos);
e31e01e8 384
9f584e4c 385 if (pos < rank) {
e31e01e8 386 TmfEvent event = getNextEvent(context);
9f584e4c 387 while (event != null && ++pos < rank) {
12c155f5 388 event = getNextEvent(context);
e31e01e8 389 }
165c977c 390 }
62d1696a 391
8f50c396 392 return context;
8c8bf09f
ASL
393 }
394
12c155f5
FC
395 /*
396 * (non-Javadoc)
397 *
398 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.
399 * linuxtools.tmf.trace.ITmfTrace.TraceContext)
400 */
401 @Override
402 public synchronized TmfEvent getNextEvent(TmfContext context) {
403 // parseEvent() does not update the context
404 TmfEvent event = parseEvent(context);
405 if (event != null) {
406 updateIndex(context, context.getRank(), event.getTimestamp());
407 context.setLocation(getCurrentLocation());
408 context.updateRank(1);
409 processEvent(event);
410 }
411 return event;
412 }
550d787e 413
12c155f5
FC
414 protected synchronized void updateIndex(ITmfContext context, long rank, TmfTimestamp timestamp) {
415 if (fStartTime.compareTo(timestamp, false) > 0)
416 fStartTime = timestamp;
417 if (fEndTime.compareTo(timestamp, false) < 0)
418 fEndTime = timestamp;
419 if (context.isValidRank()) {
420 if (fNbEvents <= rank)
421 fNbEvents = rank + 1;
422 // Build the index as we go along
423 if ((rank % fIndexPageSize) == 0) {
424 // Determine the table position
425 long position = rank / fIndexPageSize;
426 // Add new entry at proper location (if empty)
427 if (fCheckpoints.size() == position) {
428 ITmfLocation<?> location = context.getLocation().clone();
429 fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
430 // System.out.println(getName() + "[" + (fCheckpoints.size()
431 // - 1) + "] " + timestamp + ", " + location.toString());
432 }
433 }
434 }
435 }
4e3aa37d 436
e31e01e8 437 /**
12c155f5
FC
438 * Hook for special processing by the concrete class (called by getNextEvent())
439 *
440 * @param event
441 */
442 protected void processEvent(TmfEvent event) {
443 // Do nothing by default
444 }
445
446 // ------------------------------------------------------------------------
447 // toString
448 // ------------------------------------------------------------------------
449
450 /* (non-Javadoc)
451 * @see java.lang.Object#toString()
4e3aa37d 452 */
d4011df2 453 @Override
12c155f5
FC
454 @SuppressWarnings("nls")
455 public String toString() {
456 return "[TmfTrace (" + getName() + ")]";
457 }
458
664902f7
FC
459 // ------------------------------------------------------------------------
460 // Indexing
461 // ------------------------------------------------------------------------
462
12c155f5
FC
463 /*
464 * The purpose of the index is to keep the information needed to rapidly
465 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
466 * event).
467 */
83e13355 468
8e31f2d2 469 @SuppressWarnings({ "unchecked" })
a79913eb 470 protected void indexTrace(boolean waitForCompletion) {
83e13355 471
12c155f5
FC
472 final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
473 @Override
474 protected IStatus run(IProgressMonitor monitor) {
475 while (!monitor.isCanceled()) {
476 try {
477 Thread.sleep(100);
478 } catch (InterruptedException e) {
479 return Status.OK_STATUS;
480 }
481 }
482 monitor.done();
483 return Status.OK_STATUS;
484 }
485 };
486 job.schedule();
487
488 fCheckpoints.clear();
489 ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, TmfDataRequest.ALL_DATA,
490 fIndexPageSize, ITmfDataRequest.ExecutionType.BACKGROUND) {
491
492 TmfTimestamp startTime = null;
493 TmfTimestamp lastTime = null;
83e13355
FC
494
495 @Override
496 public void handleData(TmfEvent event) {
497 super.handleData(event);
498 if (event != null) {
499 TmfTimestamp ts = event.getTimestamp();
500 if (startTime == null)
501 startTime = new TmfTimestamp(ts);
502 lastTime = new TmfTimestamp(ts);
503
64267c9d 504 if ((getNbRead() % fIndexPageSize) == 0) {
83e13355
FC
505 updateTrace();
506 }
507 }
508 }
509
510 @Override
511 public void handleSuccess() {
512 updateTrace();
513 }
514
05bd3318
FC
515 @Override
516 public void handleCompleted() {
12c155f5
FC
517 job.cancel();
518 super.handleCompleted();
05bd3318
FC
519 }
520
83e13355
FC
521 private void updateTrace() {
522 int nbRead = getNbRead();
523 if (nbRead != 0) {
524 fStartTime = startTime;
525 fEndTime = lastTime;
12c155f5 526 fNbEvents = nbRead;
83e13355
FC
527 notifyListeners();
528 }
529 }
530 };
531
532 sendRequest((ITmfDataRequest<T>) request);
533 if (waitForCompletion)
534 try {
535 request.waitForCompletion();
536 } catch (InterruptedException e) {
537 e.printStackTrace();
538 }
539 }
540
12c155f5
FC
541 protected void notifyListeners() {
542 broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime)));
543 }
8c8bf09f 544}
This page took 0.080027 seconds and 5 git commands to generate.