Refactor TmfTrace and dependencies - finalize ITmfTraceIndexer
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / trace / LTTngTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 Ericsson, MontaVista Software
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 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 * Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.lttng.core.trace;
15
16 import java.io.FileNotFoundException;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Vector;
20
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.linuxtools.internal.lttng.core.TraceHelper;
24 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
25 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventContent;
26 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventType;
27 import org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation;
28 import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
29 import org.eclipse.linuxtools.internal.lttng.core.exceptions.LttngException;
30 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.utility.LiveTraceManager;
31 import org.eclipse.linuxtools.internal.lttng.jni.common.JniTime;
32 import org.eclipse.linuxtools.lttng.jni.JniEvent;
33 import org.eclipse.linuxtools.lttng.jni.JniMarker;
34 import org.eclipse.linuxtools.lttng.jni.JniTrace;
35 import org.eclipse.linuxtools.lttng.jni.JniTracefile;
36 import org.eclipse.linuxtools.lttng.jni.factory.JniTraceFactory;
37 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
38 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
39 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
40 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
41 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
42 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
43 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
44 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
45 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
46 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
47 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
48 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
49
50 class LTTngTraceException extends LttngException {
51
52 static final long serialVersionUID = -1636648737081868146L;
53
54 public LTTngTraceException(final String errMsg) {
55 super(errMsg);
56 }
57 }
58
59 /**
60 * <b><u>LTTngTrace</u></b>
61 * <p>
62 *
63 * LTTng trace implementation. It accesses the C trace handling library
64 * (seeking, reading and parsing) through the JNI component.
65 */
66 public class LTTngTrace extends TmfTrace<LttngEvent> {
67
68 public final static boolean PRINT_DEBUG = false;
69 public final static boolean UNIQUE_EVENT = true;
70
71 private final static boolean SHOW_LTT_DEBUG_DEFAULT = false;
72 private final static boolean IS_PARSING_NEEDED_DEFAULT = !UNIQUE_EVENT;
73 private final static int CHECKPOINT_PAGE_SIZE = 50000;
74 private final static long LTTNG_STREAMING_INTERVAL = 2000; // in ms
75
76 // Reference to our JNI trace
77 private JniTrace currentJniTrace;
78
79 LttngTimestamp eventTimestamp;
80 String eventSource;
81 LttngEventContent eventContent;
82 String eventReference;
83
84 // The actual event
85 LttngEvent currentLttngEvent;
86
87 // The current location
88 LttngLocation previousLocation;
89
90 LttngEventType eventType;
91
92 // Hashmap of the possible types of events (Tracefile/CPU/Marker in the JNI)
93 HashMap<Integer, LttngEventType> traceTypes;
94
95 // This vector will be used to quickly find a marker name from a position
96 Vector<Integer> traceTypeNames;
97
98 private String traceLibPath;
99
100 public LTTngTrace() {
101 }
102
103 @Override
104 public boolean validate(final IProject project, final String path) {
105 if (super.validate(project, path)) {
106 final String traceLibPath = TraceHelper.getTraceLibDirFromProject(project);
107 try {
108 final LTTngTraceVersion version = new LTTngTraceVersion(path, traceLibPath);
109 return version.isValidLttngTrace();
110 } catch (final LttngException e) {
111 }
112 }
113 return false;
114 }
115
116 @Override
117 public synchronized void initTrace(final IResource resource, final String path, final Class<LttngEvent> eventType)
118 throws FileNotFoundException {
119 super.initialize(resource, path, eventType);
120 initialize(resource, path, eventType);
121 }
122
123 @Override
124 protected synchronized void initialize(final IResource resource, final String path, final Class<LttngEvent> eventType)
125 throws FileNotFoundException {
126 try {
127 currentJniTrace = JniTraceFactory.getJniTrace(path, traceLibPath, SHOW_LTT_DEBUG_DEFAULT);
128 } catch (final Exception e) {
129 throw new FileNotFoundException(e.getMessage());
130 }
131
132 // Export all the event types from the JNI side
133 traceTypes = new HashMap<Integer, LttngEventType>();
134 traceTypeNames = new Vector<Integer>();
135 initialiseEventTypes(currentJniTrace);
136
137 // Build the re-used event structure
138 eventTimestamp = new LttngTimestamp();
139 eventSource = ""; //$NON-NLS-1$
140 this.eventType = new LttngEventType();
141 eventContent = new LttngEventContent(currentLttngEvent);
142 eventReference = getName();
143
144 // Create the skeleton event
145 currentLttngEvent = new LttngEvent(this, eventTimestamp, eventSource, this.eventType, eventContent,
146 eventReference, null);
147
148 // Create a new current location
149 previousLocation = new LttngLocation();
150
151 // Set the currentEvent to the eventContent
152 eventContent.setEvent(currentLttngEvent);
153
154 // // Bypass indexing if asked
155 // if ( bypassIndexing == false ) {
156 // indexTrace(true);
157 // }
158 // else {
159 // Even if we don't have any index, set ONE checkpoint
160 // fCheckpoints.add(new TmfCheckpoint(new LttngTimestamp(0L) , new
161 // LttngLocation() ) );
162
163 initializeStreamingMonitor();
164 }
165
166 private void initializeStreamingMonitor() {
167 final JniTrace jniTrace = getCurrentJniTrace();
168 if (jniTrace == null
169 || (!jniTrace.isLiveTraceSupported() || !LiveTraceManager.isLiveTrace(jniTrace.getTracepath()))) {
170 // Set the time range of the trace
171 final TmfContext context = seekLocation(null);
172 final LttngEvent event = getNextEvent(context);
173 final LttngTimestamp startTime = new LttngTimestamp(event.getTimestamp());
174 final LttngTimestamp endTime = new LttngTimestamp(currentJniTrace.getEndTime().getTime());
175 setTimeRange(new TmfTimeRange(startTime, endTime));
176 final TmfTraceUpdatedSignal signal = new TmfTraceUpdatedSignal(this, this, getTimeRange());
177 broadcast(signal);
178 return;
179 }
180
181 // Set the time range of the trace
182 final TmfContext context = seekLocation(null);
183 final LttngEvent event = getNextEvent(context);
184 setEndTime(TmfTimestamp.BIG_BANG);
185 final long startTime = event != null ? event.getTimestamp().getValue() : TmfTimestamp.BIG_BANG.getValue();
186 fStreamingInterval = LTTNG_STREAMING_INTERVAL;
187
188 final Thread thread = new Thread("Streaming Monitor for trace " + getName()) { //$NON-NLS-1$
189
190 LttngTimestamp safeTimestamp = null;
191 TmfTimeRange timeRange = null;
192
193 @SuppressWarnings("unchecked")
194 @Override
195 public void run() {
196 while (!fExecutor.isShutdown()) {
197 final TmfExperiment<?> experiment = TmfExperiment.getCurrentExperiment();
198 if (experiment != null) {
199 @SuppressWarnings("rawtypes")
200 final TmfEventRequest request = new TmfEventRequest<TmfEvent>(TmfEvent.class,
201 TmfTimeRange.ETERNITY, 0, ExecutionType.FOREGROUND) {
202
203 @Override
204 public void handleCompleted() {
205 updateJniTrace();
206 }
207 };
208 synchronized (experiment) {
209 experiment.sendRequest(request);
210 }
211 try {
212 request.waitForCompletion();
213 } catch (final InterruptedException e) {
214 e.printStackTrace();
215 }
216 } else
217 updateJniTrace();
218 try {
219 Thread.sleep(LTTNG_STREAMING_INTERVAL);
220 } catch (final InterruptedException e) {
221 e.printStackTrace();
222 }
223 }
224 }
225
226 private void updateJniTrace() {
227 final JniTrace jniTrace = getCurrentJniTrace();
228 currentJniTrace.updateTrace();
229 final long endTime = jniTrace.getEndTime().getTime();
230 final LttngTimestamp startTimestamp = new LttngTimestamp(startTime);
231 final LttngTimestamp endTimestamp = new LttngTimestamp(endTime);
232 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0)
233 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
234 else
235 timeRange = null;
236 safeTimestamp = endTimestamp;
237 if (timeRange != null)
238 setTimeRange(timeRange);
239 }
240 };
241 thread.start();
242 }
243
244 /**
245 * Default Constructor.
246 * <p>
247 *
248 * @param name Name of the trace
249 * @param path Path to a <b>directory</b> that contain an LTTng trace.
250 *
251 * @exception Exception (most likely LTTngTraceException or
252 * FileNotFoundException)
253 */
254 public LTTngTrace(final IResource resource, final String path) throws Exception {
255 // Call with "wait for completion" true and "skip indexing" false
256 this(resource, path, null, true, false);
257 }
258
259 /**
260 * Constructor, with control over the indexing.
261 * <p>
262 *
263 * @param name Name of the trace
264 * @param path Path to a <b>directory</b> that contain an LTTng trace.
265 * @param waitForCompletion Should we wait for indexign to complete before
266 * moving on.
267 *
268 * @exception Exception (most likely LTTngTraceException or
269 * FileNotFoundException)
270 */
271 public LTTngTrace(final IResource resource, final String path, final boolean waitForCompletion) throws Exception {
272 // Call with "skip indexing" false
273 this(resource, path, null, waitForCompletion, true);
274 }
275
276 /**
277 * Default constructor, with control over the indexing and possibility to
278 * bypass indexation
279 * <p>
280 *
281 * @param name Name of the trace
282 * @param path Path to a <b>directory</b> that contain an LTTng trace.
283 * @param traceLibPath Path to a <b>directory</b> that contains LTTng trace
284 * libraries.
285 * @param waitForCompletion Should we wait for indexign to complete before
286 * moving on.
287 * @param bypassIndexing Should we bypass indexing completly? This is should
288 * only be useful for unit testing.
289 *
290 * @exception Exception (most likely LTTngTraceException or
291 * FileNotFoundException)
292 *
293 */
294 public LTTngTrace(final IResource resource, final String path, final String traceLibPath, final boolean waitForCompletion,
295 final boolean bypassIndexing)
296 throws Exception {
297 // super(resource, LttngEvent.class, path, CHECKPOINT_PAGE_SIZE, false);
298 super(resource, LttngEvent.class, path, CHECKPOINT_PAGE_SIZE);
299 initialize(resource, path, LttngEvent.class);
300 // if (!bypassIndexing)
301 // indexTrace(false);
302 this.traceLibPath = traceLibPath;
303 }
304
305 /*
306 * Copy constructor is forbidden for LttngEvenmStream
307 */
308 public LTTngTrace(final LTTngTrace other) throws Exception {
309 this(other.getResource(), other.getPath(), other.getTraceLibPath(), false, true);
310 // this.fCheckpoints = other.fCheckpoints;
311 setTimeRange(new TmfTimeRange(new LttngTimestamp(other.getStartTime()), new LttngTimestamp(other.getEndTime())));
312 }
313
314 // @Override
315 // public synchronized LTTngTrace clone() {
316 // LTTngTrace clone = null;
317 // clone = (LTTngTrace) super.clone();
318 // try {
319 // clone.currentJniTrace = JniTraceFactory.getJniTrace(getPath(), getTraceLibPath(),
320 // SHOW_LTT_DEBUG_DEFAULT);
321 // } catch (final JniException e) {
322 // // e.printStackTrace();
323 // }
324 //
325 // // Export all the event types from the JNI side
326 // clone.traceTypes = new HashMap<Integer, LttngEventType>();
327 // clone.traceTypeNames = new Vector<Integer>();
328 // clone.initialiseEventTypes(clone.currentJniTrace);
329 //
330 // // Verify that all those "default constructor" are safe to use
331 // clone.eventTimestamp = new LttngTimestamp();
332 // clone.eventSource = ""; //$NON-NLS-1$
333 // clone.eventType = new LttngEventType();
334 // clone.eventContent = new LttngEventContent(clone.currentLttngEvent);
335 // clone.eventReference = getName();
336 //
337 // // Create the skeleton event
338 // clone.currentLttngEvent = new LttngEvent(this, clone.eventTimestamp, clone.eventSource, clone.eventType,
339 // clone.eventContent, clone.eventReference, null);
340 //
341 // // Create a new current location
342 // clone.previousLocation = new LttngLocation();
343 //
344 // // Set the currentEvent to the eventContent
345 // clone.eventContent.setEvent(clone.currentLttngEvent);
346 //
347 // // Set the start time of the trace
348 // setTimeRange(new TmfTimeRange(new LttngTimestamp(clone.currentJniTrace.getStartTime().getTime()),
349 // new LttngTimestamp(clone.currentJniTrace.getEndTime().getTime())));
350 //
351 // return clone;
352 // }
353
354 public String getTraceLibPath() {
355 return traceLibPath;
356 }
357
358 /*
359 * Fill out the HashMap with "Type" (Tracefile/Marker)
360 *
361 * This should be called at construction once the trace is open
362 */
363 private void initialiseEventTypes(final JniTrace trace) {
364 // Work variables
365 LttngEventType tmpType = null;
366 String[] markerFieldsLabels = null;
367
368 String newTracefileKey = null;
369 Integer newMarkerKey = null;
370
371 JniTracefile newTracefile = null;
372 JniMarker newMarker = null;
373
374 // First, obtain an iterator on TRACEFILES of owned by the TRACE
375 final Iterator<String> tracefileItr = trace.getTracefilesMap().keySet().iterator();
376
377 while (tracefileItr.hasNext()) {
378 newTracefileKey = tracefileItr.next();
379 newTracefile = trace.getTracefilesMap().get(newTracefileKey);
380
381 // From the TRACEFILE read, obtain its MARKER
382 final Iterator<Integer> markerItr = newTracefile.getTracefileMarkersMap().keySet().iterator();
383 while (markerItr.hasNext()) {
384 newMarkerKey = markerItr.next();
385 newMarker = newTracefile.getTracefileMarkersMap().get(newMarkerKey);
386
387 // From the MARKER we can obtain the MARKERFIELDS keys (i.e.
388 // labels)
389 markerFieldsLabels = newMarker.getMarkerFieldsHashMap().keySet()
390 .toArray(new String[newMarker.getMarkerFieldsHashMap().size()]);
391
392 tmpType = new LttngEventType(newTracefile.getTracefileName(), newTracefile.getCpuNumber(),
393 newMarker.getName(), newMarkerKey.intValue(), markerFieldsLabels);
394
395 // Add the type to the map/vector
396 addEventTypeToMap(tmpType);
397 }
398 }
399 }
400
401 /*
402 * Add a new type to the HashMap
403 *
404 * As the hashmap use a key format that is a bit dangerous to use, we should
405 * always add using this function.
406 */
407 private void addEventTypeToMap(final LttngEventType newEventType) {
408 final int newTypeKey = EventTypeKey.getEventTypeHash(newEventType);
409
410 this.traceTypes.put(newTypeKey, newEventType);
411 this.traceTypeNames.add(newTypeKey);
412 }
413
414 /**
415 * Return the latest saved location. Note : Modifying the returned location
416 * may result in buggy positionning!
417 *
418 * @return The LttngLocation as it was after the last operation.
419 *
420 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
421 */
422 @Override
423 public synchronized ITmfLocation<?> getCurrentLocation() {
424 return previousLocation;
425 }
426
427 /**
428 * Position the trace to the event at the given location.
429 * <p>
430 * NOTE : Seeking by location is very fast compare to seeking by position
431 * but is still slower than "ReadNext", avoid using it for small interval.
432 *
433 * @param location Location of the event in the trace. If no event available
434 * at this exact location, we will position ourself to the next
435 * one.
436 *
437 * @return The TmfContext that point to this event
438 *
439 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
440 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext
441 */
442 @Override
443 public synchronized TmfContext seekLocation(final ITmfLocation<?> location) {
444
445 // // [lmcfrch]
446 // lastTime = 0;
447
448 if (PRINT_DEBUG)
449 System.out.println("seekLocation(location) location -> " + location); //$NON-NLS-1$
450
451 // If the location in context is null, create a new one
452 LttngLocation curLocation = null;
453 if (location == null) {
454 curLocation = new LttngLocation();
455 final TmfContext context = seekEvent(curLocation.getOperationTime());
456 context.setRank(ITmfContext.INITIAL_RANK);
457 return context;
458 } else
459 curLocation = (LttngLocation) location;
460
461 // *** NOTE :
462 // Update to location should (and will) be done in SeekEvent.
463
464 // The only seek valid in LTTng is with the time, we call
465 // seekEvent(timestamp)
466 final TmfContext context = seekEvent(curLocation.getOperationTime());
467
468 // If the location is marked with the read next flag
469 // then it is pointing to the next event following the operation time
470 if (curLocation.isLastOperationReadNext())
471 getNextEvent(context);
472
473 return context;
474 }
475
476 /**
477 * Position the trace to the event at the given time.
478 * <p>
479 * NOTE : Seeking by time is very fast compare to seeking by position but is
480 * still slower than "ReadNext", avoid using it for small interval.
481 *
482 * @param timestamp Time of the event in the trace. If no event available at
483 * this exact time, we will position ourself to the next one.
484 *
485 * @return The TmfContext that point to this event
486 *
487 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
488 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext
489 */
490 @Override
491 public synchronized TmfContext seekEvent(final ITmfTimestamp timestamp) {
492
493 if (PRINT_DEBUG)
494 System.out.println("seekEvent(timestamp) timestamp -> " + timestamp); //$NON-NLS-1$
495
496 // Call JNI to seek
497 currentJniTrace.seekToTime(new JniTime(timestamp.getValue()));
498
499 // Save the time at which we seeked
500 previousLocation.setOperationTime(timestamp.getValue());
501 // Set the operation marker as seek, to be able to detect we did "seek"
502 // this event
503 previousLocation.setLastOperationSeek();
504
505 final LttngLocation curLocation = new LttngLocation(previousLocation);
506
507 return new TmfContext(curLocation);
508 }
509
510 /**
511 * Position the trace to the event at the given position (rank).
512 * <p>
513 * NOTE : Seeking by position is very slow in LTTng, consider seeking by
514 * timestamp.
515 *
516 * @param rank Position (or rank) of the event in the trace, starting at 0.
517 *
518 * @return The TmfContext that point to this event
519 *
520 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
521 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext
522 */
523 @Override
524 public synchronized TmfContext seekEvent(final long rank) {
525
526 if (PRINT_DEBUG)
527 System.out.println("seekEvent(rank) rank -> " + rank); //$NON-NLS-1$
528
529 // ITmfTimestamp timestamp = null;
530 // long index = rank / getCacheSize();
531 //
532 // // Get the timestamp of the closest check point to the given position
533 // if (fCheckpoints.size() > 0) {
534 // if (index >= fCheckpoints.size())
535 // index = fCheckpoints.size() - 1;
536 // timestamp = fCheckpoints.elementAt((int) index).getTimestamp();
537 // } else
538 // timestamp = getStartTime();
539
540 // Position the trace at the checkpoint
541 final ITmfContext checkpointContext = fIndexer.seekIndex(rank);
542 LttngLocation location = (LttngLocation) checkpointContext.getLocation();
543 ITmfTimestamp timestamp = location.getLocation();
544 long index = rank / getCacheSize();
545
546 // Seek to the found time
547 final TmfContext tmpContext = seekEvent(timestamp);
548 tmpContext.setRank((index + 1) * fCacheSize);
549 previousLocation = (LttngLocation) tmpContext.getLocation();
550
551 // Ajust the index of the event we found at this check point position
552 Long currentPosition = index * getCacheSize();
553
554 Long lastTimeValueRead = 0L;
555
556 // Get the event at current position. This won't move to the next one
557 JniEvent tmpJniEvent = currentJniTrace.findNextEvent();
558 // Now that we are positionned at the checkpoint,
559 // we need to "readNext" (Position - CheckpointPosition) times or until
560 // trace "run out"
561 while ((tmpJniEvent != null) && (currentPosition < rank)) {
562 tmpJniEvent = currentJniTrace.readNextEvent();
563 currentPosition++;
564 }
565
566 // If we found our event, save its timestamp
567 if (tmpJniEvent != null)
568 lastTimeValueRead = tmpJniEvent.getEventTime().getTime();
569
570 // Set the operation marker as seek, to be able to detect we did "seek"
571 // this event
572 previousLocation.setLastOperationSeek();
573 // Save read event time
574 previousLocation.setOperationTime(lastTimeValueRead);
575
576 // *** VERIFY ***
577 // Is that too paranoid?
578 //
579 // We don't trust what upper level could do with our internal location
580 // so we create a new one to return instead
581 final LttngLocation curLocation = new LttngLocation(previousLocation);
582
583 return new TmfContext(curLocation, rank);
584 }
585
586 @Override
587 public TmfContext seekLocation(final double ratio) {
588 // TODO Auto-generated method stub
589 return null;
590 }
591
592 @Override
593 public double getLocationRatio(final ITmfLocation<?> location) {
594 // TODO Auto-generated method stub
595 return 0;
596 }
597
598 /**
599 * Return the event in the trace according to the given context. Read it if
600 * necessary.
601 * <p>
602 * Similar (same?) as ParseEvent except that calling GetNext twice read the
603 * next one the second time.
604 *
605 * @param context Current TmfContext where to get the event
606 *
607 * @return The LttngEvent we read of null if no event are available
608 *
609 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
610 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext
611 */
612
613 public int nbEventsRead = 0;
614
615 @Override
616 public synchronized LttngEvent getNextEvent(final ITmfContext context) {
617
618 if (PRINT_DEBUG)
619 System.out.println("getNextEvent(context) context.getLocation() -> " //$NON-NLS-1$
620 + context.getLocation());
621
622 LttngEvent returnedEvent = null;
623 LttngLocation curLocation = null;
624
625 curLocation = (LttngLocation) context.getLocation();
626 // If the location in context is null, create a new one
627 if (curLocation == null)
628 curLocation = getCurrentLocation(context);
629
630 // *** Positioning trick :
631 // GetNextEvent only read the trace if :
632 // 1- The last operation was NOT a ParseEvent --> A read is required
633 // OR
634 // 2- The time of the previous location is different from the current
635 // one --> A seek + a read is required
636 if ((!(curLocation.isLastOperationParse()))
637 || (previousLocation.getOperationTimeValue() != curLocation.getOperationTimeValue())) {
638 if (previousLocation.getOperationTimeValue() != curLocation.getOperationTimeValue()) {
639 if (PRINT_DEBUG)
640 System.out.println("\t\tSeeking in getNextEvent. [ LastTime : " //$NON-NLS-1$
641 + previousLocation.getOperationTimeValue() + " CurrentTime" //$NON-NLS-1$
642 + curLocation.getOperationTimeValue() + " ]"); //$NON-NLS-1$
643 seekEvent(curLocation.getOperationTime());
644 }
645 // Read the next event from the trace. The last one will NO LONGER
646 // BE VALID.
647 returnedEvent = readNextEvent(curLocation);
648
649 } else {
650 // No event was read, just return the one currently loaded (the last
651 // one we read)
652 returnedEvent = currentLttngEvent;
653
654 // Set the operation marker as read to both locations, to be able to
655 // detect we need to read the next event
656 previousLocation.setLastOperationReadNext();
657 curLocation.setLastOperationReadNext();
658 }
659
660 // If we read an event, set it's time to the locations (both previous
661 // and current)
662 if (returnedEvent != null)
663 setPreviousAndCurrentTimes(context, returnedEvent, curLocation);
664
665 return returnedEvent;
666 }
667
668 // this method was extracted for profiling purposes
669 private synchronized void setPreviousAndCurrentTimes(final ITmfContext context, final LttngEvent returnedEvent,
670 final LttngLocation curLocation) {
671
672 final ITmfTimestamp eventTimestamp = returnedEvent.getTimestamp();
673 // long eventTime = eventTimestamp.getValue();
674 previousLocation.setOperationTime(eventTimestamp.getValue());
675 curLocation.setOperationTime(eventTimestamp.getValue());
676 updateAttributes(context, eventTimestamp);
677 context.increaseRank();
678 }
679
680 // protected void updateIndex(TmfContext context, long rank, ITmfTimestamp timestamp) {
681 //
682 // if (getStartTime().compareTo(timestamp, false) > 0)
683 // setStartTime(timestamp);
684 // if (getEndTime().compareTo(timestamp, false) < 0)
685 // setEndTime(timestamp);
686 // if (rank != ITmfContext.UNKNOWN_RANK) {
687 // if (fNbEvents <= rank)
688 // fNbEvents = rank + 1;
689 // // Build the index as we go along
690 // if ((rank % fIndexPageSize) == 0) {
691 // // Determine the table position
692 // long position = rank / fIndexPageSize;
693 // // Add new entry at proper location (if empty)
694 // if (fCheckpoints.size() == position) {
695 // addCheckPoint(context, timestamp);
696 // }
697 // }
698 // }
699 // }
700
701 // this method was extracted for profiling purposes
702 private synchronized LttngEvent readNextEvent(final LttngLocation curLocation) {
703 LttngEvent returnedEvent;
704 // Read the next event from the trace. The last one will NO LONGER BE
705 // VALID.
706 returnedEvent = readEvent(curLocation);
707 nbEventsRead++;
708
709 // Set the operation marker as read to both locations, to be able to
710 // detect we need to read the next event
711 previousLocation.setLastOperationReadNext();
712 curLocation.setLastOperationReadNext();
713 return returnedEvent;
714 }
715
716 // this method was extracted for profiling purposes
717 private LttngLocation getCurrentLocation(final ITmfContext context) {
718 LttngLocation curLocation;
719 curLocation = new LttngLocation();
720 context.setLocation(curLocation);
721 return curLocation;
722 }
723
724 /**
725 * Return the event in the trace according to the given context. Read it if
726 * necessary.
727 * <p>
728 * Similar (same?) as GetNextEvent except that calling ParseEvent twice will
729 * return the same event
730 *
731 * @param context Current TmfContext where to get the event
732 *
733 * @return The LttngEvent we read of null if no event are available
734 *
735 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngLocation
736 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext
737 */
738 @Override
739 public synchronized LttngEvent parseEvent(final ITmfContext context) {
740
741 if (PRINT_DEBUG)
742 System.out.println("parseEvent(context) context.getLocation() -> " //$NON-NLS-1$
743 + context.getLocation());
744
745 LttngEvent returnedEvent = null;
746 LttngLocation curLocation = null;
747
748 // If the location in context is null, create a new one
749 if (context.getLocation() == null) {
750 curLocation = new LttngLocation();
751 context.setLocation(curLocation);
752 } else
753 curLocation = (LttngLocation) context.getLocation();
754
755 // *** HACK ***
756 // TMF assumes it is possible to read (GetNextEvent) to the next Event
757 // once ParseEvent() is called
758 // In LTTNG, there is not difference between "Parsing" and "Reading" an
759 // event.
760 // So, before "Parsing" an event, we have to make sure we didn't "Read"
761 // it alreafy.
762 // Also, "Reading" invalidate the previous Event in LTTNG and seek back
763 // is very costly,
764 // so calling twice "Parse" will return the same event, giving a way to
765 // get the "Currently loaded" event
766
767 // *** Positionning trick :
768 // ParseEvent only read the trace if :
769 // 1- The last operation was NOT a ParseEvent --> A read is required
770 // OR
771 // 2- The time of the previous location is different from the current
772 // one --> A seek + a read is required
773 if (!curLocation.isLastOperationParse()
774 || (previousLocation.getOperationTimeValue() != curLocation.getOperationTimeValue())) {
775 // Previous time != Current time : We need to reposition to the
776 // current time
777 if (previousLocation.getOperationTimeValue() != curLocation.getOperationTimeValue()) {
778 if (PRINT_DEBUG)
779 System.out.println("\t\tSeeking in getNextEvent. [ LastTime : " //$NON-NLS-1$
780 + previousLocation.getOperationTimeValue() + " CurrentTime" //$NON-NLS-1$
781 + curLocation.getOperationTimeValue() + " ]"); //$NON-NLS-1$
782 seekEvent(curLocation.getOperationTime());
783 }
784
785 // Read the next event from the trace. The last one will NO LONGER
786 // BE VALID.
787 returnedEvent = readEvent(curLocation);
788 } else
789 // No event was read, just return the one currently loaded (the last
790 // one we read)
791 returnedEvent = currentLttngEvent;
792
793 // If we read an event, set it's time to the locations (both previous
794 // and current)
795 if (returnedEvent != null) {
796 previousLocation.setOperationTime((LttngTimestamp) returnedEvent.getTimestamp());
797 curLocation.setOperationTime((LttngTimestamp) returnedEvent.getTimestamp());
798 }
799
800 // Set the operation marker as parse to both location, to be able to
801 // detect we already "read" this event
802 previousLocation.setLastOperationParse();
803 curLocation.setLastOperationParse();
804
805 return returnedEvent;
806 }
807
808 /*
809 * Read the next event from the JNI and convert it as Lttng Event<p>
810 *
811 * @param location Current LttngLocation that to be updated with the event
812 * timestamp
813 *
814 * @return The LttngEvent we read of null if no event are available
815 *
816 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
817 *
818 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
819 */
820 private synchronized LttngEvent readEvent(final LttngLocation location) {
821 LttngEvent returnedEvent = null;
822 JniEvent tmpEvent = null;
823
824 // Read the next event from JNI. THIS WILL INVALIDATE THE CURRENT LTTNG
825 // EVENT.
826 tmpEvent = currentJniTrace.readNextEvent();
827
828 if (tmpEvent != null) {
829 // *** NOTE
830 // Convert will update the currentLttngEvent
831 returnedEvent = convertJniEventToTmf(tmpEvent);
832
833 location.setOperationTime((LttngTimestamp) returnedEvent.getTimestamp());
834 } else
835 location.setOperationTime(getEndTime().getValue() + 1);
836
837 return returnedEvent;
838 }
839
840 /**
841 * Method to convert a JniEvent into a LttngEvent.
842 * <p>
843 *
844 * Note : This method will call LttngEvent convertEventJniToTmf(JniEvent,
845 * boolean) with a default value for isParsingNeeded
846 *
847 * @param newEvent The JniEvent to convert into LttngEvent
848 *
849 * @return The converted LttngEvent
850 *
851 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
852 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent
853 */
854 public synchronized LttngEvent convertJniEventToTmf(final JniEvent newEvent) {
855 currentLttngEvent = convertJniEventToTmf(newEvent, IS_PARSING_NEEDED_DEFAULT);
856
857 return currentLttngEvent;
858 }
859
860 /**
861 * Method to convert a JniEvent into a LttngEvent
862 *
863 * @param jniEvent The JniEvent to convert into LttngEvent
864 * @param isParsingNeeded A boolean value telling if the event should be
865 * parsed or not.
866 *
867 * @return The converted LttngEvent
868 *
869 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
870 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent
871 */
872 public synchronized LttngEvent convertJniEventToTmf(final JniEvent jniEvent, final boolean isParsingNeeded) {
873
874 if (UNIQUE_EVENT) {
875
876 // ***
877 // UNHACKED : We can no longer do that because TCF need to maintain
878 // several events at once.
879 // This is very slow to do so in LTTng, this has to be temporary.
880 // *** HACK ***
881 // To save time here, we only set value instead of allocating new
882 // object
883 // This give an HUGE performance improvement
884 // all allocation done in the LttngTrace constructor
885 // ***
886 eventTimestamp.setValue(jniEvent.getEventTime().getTime());
887 eventSource = jniEvent.requestEventSource();
888
889 eventType = traceTypes.get(EventTypeKey.getEventTypeHash(jniEvent));
890
891 final String fullTracePath = getName();
892 final String reference = fullTracePath.substring(fullTracePath.lastIndexOf('/') + 1);
893 currentLttngEvent.setReference(reference);
894
895 eventContent.emptyContent();
896
897 currentLttngEvent.setType(eventType);
898 // Save the jni reference
899 currentLttngEvent.updateJniEventReference(jniEvent);
900
901 // Parse now if was asked
902 // Warning : THIS IS SLOW
903 if (isParsingNeeded)
904 eventContent.getFields();
905
906 return currentLttngEvent;
907 } else
908 return convertJniEventToTmfMultipleEventEvilFix(jniEvent, isParsingNeeded);
909
910 }
911
912 /**
913 * This method is a temporary fix to support multiple events at once in TMF
914 * This is expected to be slow and should be fixed in another way. See
915 * comment in convertJniEventToTmf();
916 *
917 * @param jniEvent The current JNI Event
918 * @return Current Lttng Event fully parsed
919 */
920 private synchronized LttngEvent convertJniEventToTmfMultipleEventEvilFix(final JniEvent jniEvent,
921 final boolean isParsingNeeded) {
922 // *** HACK ***
923 // Below : the "fix" with all the new and the full-parse
924 // Allocating new memory is slow.
925 // Parsing every events is very slow.
926 eventTimestamp = new LttngTimestamp(jniEvent.getEventTime().getTime());
927 eventSource = jniEvent.requestEventSource();
928 eventReference = getName();
929 eventType = new LttngEventType(traceTypes.get(EventTypeKey.getEventTypeHash(jniEvent)));
930 eventContent = new LttngEventContent(currentLttngEvent);
931 currentLttngEvent = new LttngEvent(this, eventTimestamp, eventSource, eventType, eventContent, eventReference,
932 null);
933
934 // The jni reference is no longer reliable but we will keep it anyhow
935 currentLttngEvent.updateJniEventReference(jniEvent);
936 // Ensure that the content is correctly set
937 eventContent.setEvent(currentLttngEvent);
938
939 // Parse the event if it was needed
940 // *** WARNING ***
941 // ONLY for testing, NOT parsing events with non-unique events WILL
942 // result in segfault in the JVM
943 if (isParsingNeeded)
944 eventContent.getFields();
945
946 return currentLttngEvent;
947 }
948
949 /**
950 * Reference to the current LttngTrace we are reading from.
951 * <p>
952 *
953 * Note : This bypass the framework and should not be use, except for
954 * testing!
955 *
956 * @return Reference to the current LttngTrace
957 *
958 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
959 */
960 public JniTrace getCurrentJniTrace() {
961 return currentJniTrace;
962 }
963
964 /**
965 * Return a reference to the current LttngEvent we have in memory.
966 *
967 * @return The current (last read) LttngEvent
968 *
969 * @see org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent
970 */
971 public synchronized LttngEvent getCurrentEvent() {
972 return currentLttngEvent;
973 }
974
975 /**
976 * Get the major version number for the current trace
977 *
978 * @return Version major or -1 if unknown
979 *
980 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
981 *
982 */
983 public short getVersionMajor() {
984 if (currentJniTrace != null)
985 return currentJniTrace.getLttMajorVersion();
986 else
987 return -1;
988 }
989
990 /**
991 * Get the minor version number for the current trace
992 *
993 * @return Version minor or -1 if unknown
994 *
995 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
996 *
997 */
998 public short getVersionMinor() {
999 if (currentJniTrace != null)
1000 return currentJniTrace.getLttMinorVersion();
1001 else
1002 return -1;
1003 }
1004
1005 /**
1006 * Get the number of CPU for this trace
1007 *
1008 * @return Number of CPU or -1 if unknown
1009 *
1010 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
1011 *
1012 */
1013 public int getCpuNumber() {
1014 if (currentJniTrace != null)
1015 return currentJniTrace.getCpuNumber();
1016 else
1017 return -1;
1018 }
1019
1020 // /**
1021 // * Print the content of the checkpoint vector.
1022 // * <p>
1023 // *
1024 // * This is intended for debug purpose only.
1025 // */
1026 // public void printCheckpointsVector() {
1027 // System.out.println("StartTime : " //$NON-NLS-1$
1028 // + getTimeRange().getStartTime().getValue());
1029 // System.out.println("EndTime : " //$NON-NLS-1$
1030 // + getTimeRange().getEndTime().getValue());
1031 //
1032 // for (int pos = 0; pos < fCheckpoints.size(); pos++) {
1033 // System.out.print(pos + ": " + "\t"); //$NON-NLS-1$ //$NON-NLS-2$
1034 // System.out.print(fCheckpoints.get(pos).getTimestamp() + "\t"); //$NON-NLS-1$
1035 // System.out.println(fCheckpoints.get(pos).getLocation());
1036 // }
1037 // }
1038
1039 @Override
1040 public synchronized void dispose() {
1041 if (currentJniTrace != null)
1042 currentJniTrace.closeTrace();
1043 super.dispose();
1044 }
1045
1046 /**
1047 * Return a String identifying this trace.
1048 *
1049 * @return String that identify this trace
1050 */
1051 @Override
1052 @SuppressWarnings("nls")
1053 public String toString() {
1054 String returnedData = "";
1055
1056 returnedData += "Path :" + getPath() + " ";
1057 returnedData += "Trace:" + currentJniTrace + " ";
1058 returnedData += "Event:" + currentLttngEvent;
1059
1060 return returnedData;
1061 }
1062
1063 }
1064
1065 /*
1066 * EventTypeKey inner class
1067 *
1068 * This class is used to make the process of generating the HashMap key more
1069 * transparent and so less error prone to use
1070 */
1071 final class EventTypeKey {
1072
1073 // *** WARNING ***
1074 // These two getEventTypeKey() functions should ALWAYS construct the key the
1075 // same ways!
1076 // Otherwise, every type search will fail!
1077
1078 // added final to encourage inlining.
1079
1080 // generating a hash code by hand to avoid a string creation
1081 final static public int getEventTypeHash(final LttngEventType newEventType) {
1082 return generateHash(newEventType.getTracefileName(), newEventType.getCpuId(), newEventType.getMarkerName());
1083 }
1084
1085 final private static int generateHash(final String traceFileName, final long cpuNumber, final String markerName) {
1086 // 0x1337 is a prime number. The number of CPUs is always under 8192 on
1087 // the current kernel, so this will work with the current linux kernel.
1088 final int cpuHash = (int) (cpuNumber * (0x1337));
1089 return traceFileName.hashCode() ^ (cpuHash) ^ markerName.hashCode();
1090 }
1091
1092 // generating a hash code by hand to avoid a string creation
1093 final static public int getEventTypeHash(final JniEvent newEvent) {
1094 return generateHash(newEvent.getParentTracefile().getTracefileName(), newEvent.getParentTracefile()
1095 .getCpuNumber(), newEvent.requestEventMarker().getName());
1096 }
1097
1098 }
This page took 0.055391 seconds and 5 git commands to generate.