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