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