1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.trace
;
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
17 import java
.util
.Vector
;
19 import org
.eclipse
.linuxtools
.lttng
.LttngException
;
20 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEvent
;
21 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventContent
;
22 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventReference
;
23 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventSource
;
24 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventType
;
25 import org
.eclipse
.linuxtools
.lttng
.event
.LttngLocation
;
26 import org
.eclipse
.linuxtools
.lttng
.event
.LttngTimestamp
;
27 import org
.eclipse
.linuxtools
.lttng
.jni
.JniEvent
;
28 import org
.eclipse
.linuxtools
.lttng
.jni
.JniMarker
;
29 import org
.eclipse
.linuxtools
.lttng
.jni
.JniTrace
;
30 import org
.eclipse
.linuxtools
.lttng
.jni
.JniTracefile
;
31 import org
.eclipse
.linuxtools
.lttng
.jni
.common
.JniTime
;
32 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniException
;
33 import org
.eclipse
.linuxtools
.lttng
.jni
.factory
.JniTraceFactory
;
34 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
35 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
36 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfLocation
;
37 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTrace
;
41 class LTTngTraceException
extends LttngException
{
42 static final long serialVersionUID
= -1636648737081868146L;
44 public LTTngTraceException(String errMsg
) {
50 * <b><u>LTTngTrace</u></b><p>
52 * LTTng trace implementation. It accesses the C trace handling library
53 * (seeking, reading and parsing) through the JNI component.
55 public class LTTngTrace
extends TmfTrace
<LttngEvent
> {
58 // private long lastTime = 0;
60 public static boolean printDebug
= false;
61 public static boolean uniqueEvent
= false;
63 private final static boolean SHOW_LTT_DEBUG_DEFAULT
= false;
64 private final static boolean IS_PARSING_NEEDED_DEFAULT
= true;
65 private final static int CHECKPOINT_PAGE_SIZE
= 1000;
67 // Reference to our JNI trace
68 private JniTrace currentJniTrace
= null;
71 // UNHACKED : We can no longer do that because TCF need to maintain several events at once.
72 // This is very slow to do so in LTTng, this has to be temporary.
74 // To save time, we will declare all component of the LttngEvent during the construction of the trace
75 // Then, while reading the trace, we will just SET the values instead of declaring new object
77 LttngTimestamp eventTimestamp
= null;
78 LttngEventSource eventSource
= null;
79 LttngEventContent eventContent
= null;
80 LttngEventReference eventReference
= null;
84 LttngEvent currentLttngEvent
= null;
86 // The current location
87 LttngLocation previousLocation
= null;
89 LttngEventType eventType
= null;
90 // Hashmap of the possible types of events (Tracefile/CPU/Marker in the JNI)
91 HashMap
<String
, LttngEventType
> traceTypes
= null;
92 // This vector will be used to quickly find a marker name from a position
93 Vector
<String
> traceTypeNames
= null;
96 * Default Constructor.<p>
98 * @param path Path to a <b>directory</b> that contain an LTTng trace.
100 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
102 public LTTngTrace(String path
) throws Exception
{
103 // Call with "wait for completion" true and "skip indexing" false
104 this(path
, true, false);
108 * Constructor, with control over the indexing.
110 * @param path Path to a <b>directory</b> that contain an LTTng trace.
111 * @param waitForCompletion Should we wait for indexign to complete before moving on.
113 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
115 public LTTngTrace(String path
, boolean waitForCompletion
) throws Exception
{
116 // Call with "skip indexing" false
117 this(path
, waitForCompletion
, false);
121 * Default constructor, with control over the indexing and possibility to bypass indexation
123 * @param path Path to a <b>directory</b> that contain an LTTng trace.
124 * @param waitForCompletion Should we wait for indexign to complete before moving on.
125 * @param bypassIndexing Should we bypass indexing completly? This is should only be useful for unit testing.
127 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
130 public LTTngTrace(String path
, boolean waitForCompletion
, boolean bypassIndexing
) throws Exception
{
131 super(path
, LttngEvent
.class, path
, CHECKPOINT_PAGE_SIZE
);
133 currentJniTrace
= JniTraceFactory
.getJniTrace(path
, SHOW_LTT_DEBUG_DEFAULT
);
135 catch (Exception e
) {
136 throw new LTTngTraceException(e
.getMessage());
139 // Export all the event types from the JNI side
140 traceTypes
= new HashMap
<String
, LttngEventType
>();
141 traceTypeNames
= new Vector
<String
>();
142 initialiseEventTypes(currentJniTrace
);
145 // Verify that all those "default constructor" are safe to use
146 eventTimestamp
= new LttngTimestamp();
147 eventSource
= new LttngEventSource();
148 eventType
= new LttngEventType();
149 eventContent
= new LttngEventContent(currentLttngEvent
);
150 eventReference
= new LttngEventReference(this.getName());
152 // Create the skeleton event
153 currentLttngEvent
= new LttngEvent(this, eventTimestamp
, eventSource
, eventType
, eventContent
, eventReference
, null);
155 // Create a new current location
156 previousLocation
= new LttngLocation();
159 // Set the currentEvent to the eventContent
160 eventContent
.setEvent(currentLttngEvent
);
162 // // Bypass indexing if asked
163 // if ( bypassIndexing == false ) {
167 // Even if we don't have any index, set ONE checkpoint
168 // fCheckpoints.add(new TmfCheckpoint(new LttngTimestamp(0L) , new LttngLocation() ) );
170 // Set the start time of the trace
171 setTimeRange( new TmfTimeRange( new LttngTimestamp(currentJniTrace
.getStartTime().getTime()),
172 new LttngTimestamp(currentJniTrace
.getEndTime().getTime())
178 * Copy constructor is forbidden for LttngEvenmStream
181 public LTTngTrace(LTTngTrace oldTrace
) throws Exception
{
182 this(oldTrace
.getPath(), false, true);
186 this.fCheckpoints
= oldTrace
.fCheckpoints
;
189 // This would only work if the index is already done
190 this.fCheckpoints = new Vector<TmfCheckpoint>( oldTrace.fCheckpoints.size() );
191 for (int x = 0; x<oldTrace.fCheckpoints.size(); x++){
192 TmfCheckpoint tmpCheckPoint = oldTrace.fCheckpoints.get(x);
193 this.fCheckpoints.add( new TmfCheckpoint(tmpCheckPoint.getTimestamp(), tmpCheckPoint.getLocation()) );
197 // Set the start time of the trace
198 setTimeRange( new TmfTimeRange( new LttngTimestamp(oldTrace
.getStartTime()),
199 new LttngTimestamp(oldTrace
.getEndTime()))
203 public LTTngTrace
createTraceCopy() {
204 LTTngTrace returnedTrace
= null;
207 returnedTrace
= new LTTngTrace(this);
209 catch (Exception e
) {
210 System
.out
.println("ERROR : Could not create LTTngTrace copy (createTraceCopy).\nError is : " + e
.getStackTrace());
213 return returnedTrace
;
217 public LTTngTrace
clone() {
218 LTTngTrace clone
= null;
220 clone
= (LTTngTrace
) super.clone();
222 clone
.currentJniTrace
= JniTraceFactory
.getJniTrace(getPath(), SHOW_LTT_DEBUG_DEFAULT
);
223 } catch (JniException e
) {
224 // e.printStackTrace();
227 // Export all the event types from the JNI side
228 clone
.traceTypes
= new HashMap
<String
, LttngEventType
>();
229 clone
.traceTypeNames
= new Vector
<String
>();
230 clone
.initialiseEventTypes(clone
.currentJniTrace
);
232 // Verify that all those "default constructor" are safe to use
233 clone
.eventTimestamp
= new LttngTimestamp();
234 clone
.eventSource
= new LttngEventSource();
235 clone
.eventType
= new LttngEventType();
236 clone
.eventContent
= new LttngEventContent(clone
.currentLttngEvent
);
237 clone
.eventReference
= new LttngEventReference(this.getName());
239 // Create the skeleton event
240 clone
.currentLttngEvent
= new LttngEvent(this, clone
.eventTimestamp
, clone
.eventSource
, clone
.eventType
, clone
.eventContent
, clone
.eventReference
, null);
242 // Create a new current location
243 clone
.previousLocation
= new LttngLocation();
245 // Set the currentEvent to the eventContent
246 clone
.eventContent
.setEvent(clone
.currentLttngEvent
);
248 // Set the start time of the trace
249 setTimeRange(new TmfTimeRange(
250 new LttngTimestamp(clone
.currentJniTrace
.getStartTime().getTime()),
251 new LttngTimestamp(clone
.currentJniTrace
.getEndTime().getTime())
254 catch (CloneNotSupportedException e
) {
262 * Fill out the HashMap with "Type" (Tracefile/Marker)
264 * This should be called at construction once the trace is open
266 private void initialiseEventTypes(JniTrace trace
) {
268 LttngEventType tmpType
= null;
269 String
[] markerFieldsLabels
= null;
271 String newTracefileKey
= null;
272 Integer newMarkerKey
= null;
274 JniTracefile newTracefile
= null;
275 JniMarker newMarker
= null;
277 // First, obtain an iterator on TRACEFILES of owned by the TRACE
278 Iterator
<String
> tracefileItr
= trace
.getTracefilesMap().keySet().iterator();
279 while ( tracefileItr
.hasNext() ) {
280 newTracefileKey
= tracefileItr
.next();
281 newTracefile
= trace
.getTracefilesMap().get(newTracefileKey
);
283 // From the TRACEFILE read, obtain its MARKER
284 Iterator
<Integer
> markerItr
= newTracefile
.getTracefileMarkersMap().keySet().iterator();
285 while ( markerItr
.hasNext() ) {
286 newMarkerKey
= markerItr
.next();
287 newMarker
= newTracefile
.getTracefileMarkersMap().get(newMarkerKey
);
289 // From the MARKER we can obtain the MARKERFIELDS keys (i.e. labels)
290 markerFieldsLabels
= newMarker
.getMarkerFieldsHashMap().keySet().toArray( new String
[newMarker
.getMarkerFieldsHashMap().size()] );
291 tmpType
= new LttngEventType(newTracefile
.getTracefileName(), newTracefile
.getCpuNumber(), newMarker
.getName(), markerFieldsLabels
);
293 // Add the type to the map/vector
294 addEventTypeToMap(tmpType
);
300 * Add a new type to the HashMap
302 * As the hashmap use a key format that is a bit dangerous to use, we should always add using this function.
304 private void addEventTypeToMap(LttngEventType newEventType
) {
305 String newTypeKey
= EventTypeKey
.getEventTypeKey(newEventType
);
307 this.traceTypes
.put(newTypeKey
, newEventType
);
308 this.traceTypeNames
.add(newTypeKey
);
312 // * Index the current trace.
314 // * @param useless This boolean is only to comply to the interface and will be ignored.
317 // public synchronized void indexTrace(boolean useless) {
321 // // Start time need to be null to detect none have been set
322 // // LastTime need to exist so we can ajust it as we go
323 // LttngTimestamp startTime = null;
324 // LttngTimestamp lastTime = new LttngTimestamp();
326 // // Position the trace at the beginning
327 // TmfContext context = seekEvent( new LttngTimestamp(0L) );
329 // // Read the first event and extract the location
330 // LttngEvent tmpEvent = (LttngEvent)getNextEvent(context);
332 // // If we read the first event, define the start time.
333 // if ( tmpEvent != null ) {
334 // startTime = new LttngTimestamp( tmpEvent.getTimestamp() );
335 // lastTime.setValue(tmpEvent.getTimestamp().getValue());
338 // // Now, we read each event until we hit the end of the trace
339 // // We will create a new checkpoint every "getCacheSize()" event
340 // while ( tmpEvent != null) {
341 // // Update the last time each time we read a new event
342 // lastTime.setValue(tmpEvent.getTimestamp().getValue());
344 // // Save a check point if needed
345 // if ((nbEvents++ % getCacheSize()) == 0) {
347 // // We need to NEW each stuff we put in checkpoint
348 // // Otherwise everything will be the same!
349 // LttngTimestamp tmpTimestamp = new LttngTimestamp( (LttngTimestamp)tmpEvent.getTimestamp() );
350 // LttngLocation newLocation = new LttngLocation( (LttngTimestamp)tmpEvent.getTimestamp() );
352 // fCheckpoints.add(new TmfCheckpoint(tmpTimestamp, newLocation ) );
354 // // Read the next event
355 // tmpEvent = (LttngEvent)getNextEvent(context);
358 // // If we have a start time, we should have an end time as well
359 // // Issue the new range
360 // if (startTime != null) {
361 // setTimeRange( new TmfTimeRange(startTime, lastTime) );
362 // notifyListeners(getTimeRange() );
365 // // Ajust the total number of event in the trace
366 // fNbEvents = nbEvents;
367 // //printCheckpointsVector();
368 // //printDebug = true;
372 * Return the latest saved location.
373 * Note : Modifying the returned location may result in buggy positionning!
375 * @return The LttngLocation as it was after the last operation.
377 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
380 public synchronized ITmfLocation
<?
> getCurrentLocation() {
381 return previousLocation
;
385 * Position the trace to the event at the given location.<p>
386 * NOTE : Seeking by location is very fast compare to seeking by position
387 * but is still slower than "ReadNext", avoid using it for small interval.
389 * @param location Location of the event in the trace.
390 * If no event available at this exact location, we will position ourself to the next one.
392 * @return The TmfContext that point to this event
394 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
395 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
398 public synchronized TmfContext
seekLocation(ITmfLocation
<?
> location
) {
403 if ( printDebug
== true ) {
404 System
.out
.println("seekLocation(location) location -> " + location
);
407 // If the location in context is null, create a new one
408 LttngLocation curLocation
= null;
409 if ( location
== null ) {
410 curLocation
= new LttngLocation();
413 curLocation
= (LttngLocation
)location
;
417 // Update to location should (and will) be done in SeekEvent.
419 // The only seek valid in LTTng is with the time, we call seekEvent(timestamp)
420 return seekEvent( curLocation
.getOperationTime() );
424 * Position the trace to the event at the given time.<p>
425 * NOTE : Seeking by time is very fast compare to seeking by position
426 * but is still slower than "ReadNext", avoid using it for small interval.
428 * @param timestamp Time of the event in the trace.
429 * If no event available at this exact time, we will position ourself to the next one.
431 * @return The TmfContext that point to this event
433 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
434 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
437 public synchronized TmfContext
seekEvent(TmfTimestamp timestamp
) {
442 if ( printDebug
== true ) {
443 System
.out
.println("seekEvent(timestamp) timestamp -> " + timestamp
);
447 currentJniTrace
.seekToTime(new JniTime(timestamp
.getValue()));
449 // Save the time at which we seeked
450 previousLocation
.setOperationTime(timestamp
.getValue());
451 // Set the operation marker as seek, to be able to detect we did "seek" this event
452 previousLocation
.setLastOperationSeek();
455 // Is that too paranoid?
457 // We don't trust what upper level could do with our internal location
458 // so we create a new one to return instead
459 LttngLocation curLocation
= new LttngLocation(previousLocation
);
461 return new TmfContext( curLocation
);
465 * Position the trace to the event at the given position (rank).<p>
466 * NOTE : Seeking by position is very slow in LTTng, consider seeking by timestamp.
468 * @param position Position (or rank) of the event in the trace, starting at 0.
470 * @return The TmfContext that point to this event
472 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
473 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
476 public synchronized TmfContext
seekEvent(long position
) {
481 if ( printDebug
== true ) {
482 System
.out
.println("seekEvent(position) position -> " + position
);
485 TmfTimestamp timestamp
= null;
486 long index
= position
/ getCacheSize();
488 // Get the timestamp of the closest check point to the given position
489 if (fCheckpoints
.size() > 0) {
490 if (index
>= fCheckpoints
.size()) {
491 index
= fCheckpoints
.size() - 1;
493 timestamp
= (TmfTimestamp
)fCheckpoints
.elementAt((int)index
).getTimestamp();
495 // If none, take the start time of the trace
497 timestamp
= getStartTime();
500 // Seek to the found time
501 TmfContext tmpContext
= seekEvent(timestamp
);
502 previousLocation
= (LttngLocation
)tmpContext
.getLocation();
504 // Ajust the index of the event we found at this check point position
505 Long currentPosition
= index
* getCacheSize();
507 Long lastTimeValueRead
= 0L;
509 // Get the event at current position. This won't move to the next one
510 JniEvent tmpJniEvent
= currentJniTrace
.findNextEvent();
511 // Now that we are positionned at the checkpoint,
512 // we need to "readNext" (Position - CheckpointPosition) times or until trace "run out"
513 while ( (tmpJniEvent
!= null) && ( currentPosition
< position
) ) {
514 tmpJniEvent
= currentJniTrace
.readNextEvent();
518 // If we found our event, save its timestamp
519 if ( tmpJniEvent
!= null ) {
520 lastTimeValueRead
= tmpJniEvent
.getEventTime().getTime();
523 // Set the operation marker as seek, to be able to detect we did "seek" this event
524 previousLocation
.setLastOperationSeek();
525 // Save read event time
526 previousLocation
.setOperationTime(lastTimeValueRead
);
529 // Is that too paranoid?
531 // We don't trust what upper level could do with our internal location
532 // so we create a new one to return instead
533 LttngLocation curLocation
= new LttngLocation(previousLocation
);
535 return new TmfContext( curLocation
);
539 * Return the event in the trace according to the given context. Read it if necessary.<p>
540 * Similar (same?) as ParseEvent except that calling GetNext twice read the next one the second time.
542 * @param context Current TmfContext where to get the event
544 * @return The LttngEvent we read of null if no event are available
546 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
547 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
550 public int nbEventsRead
= 0;
552 public synchronized LttngEvent
getNextEvent(TmfContext context
) {
554 if ( printDebug
== true ) {
555 System
.out
.println("getNextEvent(context) context.getLocation() -> " + context
.getLocation());
558 LttngEvent returnedEvent
= null;
559 LttngLocation curLocation
= null;
561 // If the location in context is null, create a new one
562 if ( context
.getLocation() == null ) {
563 curLocation
= new LttngLocation();
564 context
.setLocation(curLocation
);
567 // Otherwise, we use the one in context; it should be a valid LttngLocation
568 curLocation
= (LttngLocation
)context
.getLocation();
572 // TMF assumes it is possible to read (GetNextEvent) to the next Event once ParseEvent() is called
573 // In LTTNG, there is not difference between "Parsing" and "Reading" an event.
574 // Since parsing/reading invalidate the previous event,
575 // we need to make sure the sequence ParseEvent() -> GetNextEvent() will not actually move to the next event.
576 // To do so, we avoid moving for call to "GetNextEvent()" that follow call to a call to "ParseEvent()".
577 // However, calling ParseEvent() -> GetNextEvent() -> GetNextEvent() will only move next by one.
579 // *** Positioning trick :
580 // GetNextEvent only read the trace if :
581 // 1- The last operation was NOT a ParseEvent --> A read is required
583 // 2- The time of the previous location is different from the current one --> A seek + a read is required
584 if ( (curLocation
.isLastOperationParse() != true) ||
585 (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) )
587 if ( previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) {
588 if ( printDebug
== true ) {
589 System
.out
.println("\t\tSeeking in getNextEvent. [ LastTime : " + previousLocation
.getOperationTimeValue() + " CurrentTime" + curLocation
.getOperationTimeValue() + " ]");
591 seekEvent( curLocation
.getOperationTime() );
593 // Read the next event from the trace. The last one will NO LONGER BE VALID.
594 returnedEvent
= readEvent(curLocation
);
597 // Set the operation marker as read to both location, to be able to detect we did "read" this event
598 previousLocation
.setLastOperationReadNext();
599 curLocation
.setLastOperationReadNext();
602 // No event was read, just return the one currently loaded (the last one we read)
603 returnedEvent
= currentLttngEvent
;
606 // Reset (erase) the operation marker to both location, to be able to detect we did NOT "read" this event
607 previousLocation
.resetLocationState();
608 curLocation
.resetLocationState();
611 // If we read an event, set it's time to the locations (both previous and current)
612 if ( returnedEvent
!= null ) {
613 previousLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
614 curLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
616 updateIndex(context
, context
.getRank(), returnedEvent
.getTimestamp());
617 context
.updateRank(1);
620 // long newTime = returnedEvent.getOriginalTimestamp().getValue();
621 // if ((newTime-lastTime) <= 0)
622 // System.out.println("Going back in time (or not moving): " + newTime + ", " + lastTime);
623 // lastTime = newTime;
624 // System.out.println(getName() + Thread.currentThread() + ", ts=" + lastTime);
627 return returnedEvent
;
632 * Return the event in the trace according to the given context. Read it if necessary.<p>
633 * Similar (same?) as GetNextEvent except that calling ParseEvent twice will return the same event
635 * @param context Current TmfContext where to get the event
637 * @return The LttngEvent we read of null if no event are available
639 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
640 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
643 public synchronized LttngEvent
parseEvent(TmfContext context
) {
645 if ( printDebug
== true ) {
646 System
.out
.println("parseEvent(context) context.getLocation() -> " + context
.getLocation());
649 LttngEvent returnedEvent
= null;
650 LttngLocation curLocation
= null;
652 // If the location in context is null, create a new one
653 if ( context
.getLocation() == null ) {
654 curLocation
= new LttngLocation();
655 context
.setLocation(curLocation
);
657 // Otherwise, we use the one in context; it should be a valid LttngLocation
659 curLocation
= (LttngLocation
)context
.getLocation();
663 // TMF assumes it is possible to read (GetNextEvent) to the next Event once ParseEvent() is called
664 // In LTTNG, there is not difference between "Parsing" and "Reading" an event.
665 // So, before "Parsing" an event, we have to make sure we didn't "Read" it alreafy.
666 // Also, "Reading" invalidate the previous Event in LTTNG and seek back is very costly,
667 // so calling twice "Parse" will return the same event, giving a way to get the "Currently loaded" event
669 // *** Positionning trick :
670 // ParseEvent only read the trace if :
671 // 1- The last operation was NOT a ParseEvent or a GetNextEvent --> A read is required
673 // 2- The time of the previous location is different from the current one --> A seek + a read is required
674 if ( ( (curLocation
.isLastOperationParse() != true) && ((curLocation
.isLastOperationReadNext() != true)) ) ||
675 (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) )
677 // Previous time != Current time : We need to reposition to the current time
678 if (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) {
679 if ( printDebug
== true ) {
680 System
.out
.println("\t\tSeeking in getNextEvent. [ LastTime : " + previousLocation
.getOperationTimeValue() + " CurrentTime" + curLocation
.getOperationTimeValue() + " ]");
682 seekEvent( curLocation
.getOperationTime() );
685 // Read the next event from the trace. The last one will NO LONGER BE VALID.
686 returnedEvent
= readEvent(curLocation
);
689 // No event was read, just return the one currently loaded (the last one we read)
690 returnedEvent
= currentLttngEvent
;
693 // If we read an event, set it's time to the locations (both previous and current)
694 if ( returnedEvent
!= null ) {
695 previousLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
696 curLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
699 // Set the operation marker as parse to both location, to be able to detect we already "read" this event
700 previousLocation
.setLastOperationParse();
701 curLocation
.setLastOperationParse();
703 return returnedEvent
;
707 * Read the next event from the JNI and convert it as Lttng Event<p>
709 * @param location Current LttngLocation that to be updated with the event timestamp
711 * @return The LttngEvent we read of null if no event are available
713 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
714 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
716 private synchronized LttngEvent
readEvent(LttngLocation location
) {
717 LttngEvent returnedEvent
= null;
718 JniEvent tmpEvent
= null;
720 // Read the next event from JNI. THIS WILL INVALIDATE THE CURRENT LTTNG EVENT.
721 tmpEvent
= currentJniTrace
.readNextEvent();
723 if ( tmpEvent
!= null ) {
725 // Convert will update the currentLttngEvent
726 returnedEvent
= convertJniEventToTmf(tmpEvent
);
728 location
.setOperationTime( (LttngTimestamp
)returnedEvent
.getTimestamp() );
731 // If the read failed (likely the last event in the trace), set the LastReadTime to the JNI time
732 // That way, even if we try to read again, we will step over the bogus seek and read
734 location
.setOperationTime( getEndTime().getValue() + 1 );
737 return returnedEvent
;
741 * Method to convert a JniEvent into a LttngEvent.<p>
743 * Note : This method will call LttngEvent convertEventJniToTmf(JniEvent, boolean)
744 * with a default value for isParsingNeeded
746 * @param newEvent The JniEvent to convert into LttngEvent
748 * @return The converted LttngEvent
750 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
751 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
753 public synchronized LttngEvent
convertJniEventToTmf(JniEvent newEvent
) {
754 currentLttngEvent
= convertJniEventToTmf(newEvent
, IS_PARSING_NEEDED_DEFAULT
);
756 return currentLttngEvent
;
760 * Method to convert a JniEvent into a LttngEvent
762 * @param jniEvent The JniEvent to convert into LttngEvent
763 * @param isParsingNeeded A boolean value telling if the event should be parsed or not.
765 * @return The converted LttngEvent
767 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
768 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
770 public synchronized LttngEvent
convertJniEventToTmf(JniEvent jniEvent
, boolean isParsingNeeded
) {
772 if ( uniqueEvent
== true ) {
775 // UNHACKED : We can no longer do that because TCF need to maintain several events at once.
776 // This is very slow to do so in LTTng, this has to be temporary.
778 // To save time here, we only set value instead of allocating new object
779 // This give an HUGE performance improvement
780 // all allocation done in the LttngTrace constructor
782 eventTimestamp
.setValue(jniEvent
.getEventTime().getTime());
783 eventSource
.setSourceId(jniEvent
.requestEventSource());
785 eventType
= traceTypes
.get( EventTypeKey
.getEventTypeKey(jniEvent
) );
787 eventReference
.setValue(jniEvent
.getParentTracefile().getTracefilePath());
788 eventReference
.setTracepath(this.getName());
790 eventContent
.emptyContent();
792 currentLttngEvent
.setType(eventType
);
793 // Save the jni reference
794 currentLttngEvent
.updateJniEventReference(jniEvent
);
796 // Parse now if was asked
797 // Warning : THIS IS SLOW
798 if (isParsingNeeded
== true ) {
799 eventContent
.getFields();
802 return currentLttngEvent
;
805 return convertJniEventToTmfMultipleEventEvilFix(jniEvent
, isParsingNeeded
);
811 * This method is a temporary fix to support multiple events at once in TMF
812 * This is expected to be slow and should be fixed in another way.
813 * See comment in convertJniEventToTmf();
815 * @param jniEvent The current JNI Event
816 * @return Current Lttng Event fully parsed
818 private synchronized LttngEvent
convertJniEventToTmfMultipleEventEvilFix(JniEvent jniEvent
, boolean isParsingNeeded
) {
820 // Below : the "fix" with all the new and the full-parse
821 // Allocating new memory is slow.
822 // Parsing every events is very slow.
823 eventTimestamp
= new LttngTimestamp(jniEvent
.getEventTime().getTime());
824 eventSource
= new LttngEventSource(jniEvent
.requestEventSource());
825 eventReference
= new LttngEventReference(jniEvent
.getParentTracefile().getTracefilePath(), this.getName());
826 eventType
= new LttngEventType(traceTypes
.get( EventTypeKey
.getEventTypeKey(jniEvent
) ));
827 eventContent
= new LttngEventContent(currentLttngEvent
);
828 currentLttngEvent
= new LttngEvent(this, eventTimestamp
, eventSource
, eventType
, eventContent
, eventReference
, null);
830 // The jni reference is no longer reliable but we will keep it anyhow
831 currentLttngEvent
.updateJniEventReference(jniEvent
);
832 // Ensure that the content is correctly set
833 eventContent
.setEvent(currentLttngEvent
);
835 // Parse the event if it was needed
837 // ONLY for testing, NOT parsing events with non-unique events WILL result in segfault in the JVM
838 if ( isParsingNeeded
== true ) {
839 eventContent
.getFields();
842 return currentLttngEvent
;
848 * Reference to the current LttngTrace we are reading from.<p>
850 * Note : This bypass the framework and should not be use, except for testing!
852 * @return Reference to the current LttngTrace
854 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
856 public JniTrace
getCurrentJniTrace() {
857 return currentJniTrace
;
862 * Return a reference to the current LttngEvent we have in memory.
864 * @return The current (last read) LttngEvent
866 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
868 public synchronized LttngEvent
getCurrentEvent() {
869 return currentLttngEvent
;
873 * Get the major version number for the current trace
875 * @return Version major or -1 if unknown
877 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
880 public short getVersionMajor() {
881 if ( currentJniTrace
!= null ) {
882 return currentJniTrace
.getLttMajorVersion();
890 * Get the minor version number for the current trace
892 * @return Version minor or -1 if unknown
894 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
897 public short getVersionMinor() {
898 if ( currentJniTrace
!= null ) {
899 return currentJniTrace
.getLttMinorVersion();
907 * Get the number of CPU for this trace
909 * @return Number of CPU or -1 if unknown
911 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
914 public int getCpuNumber() {
915 if ( currentJniTrace
!= null ) {
916 return currentJniTrace
.getCpuNumber();
924 * Print the content of the checkpoint vector.<p>
926 * This is intended for debug purpose only.
928 public void printCheckpointsVector() {
929 System
.out
.println("StartTime : " + getTimeRange().getStartTime().getValue());
930 System
.out
.println("EndTime : " + getTimeRange().getEndTime().getValue());
932 for ( int pos
=0; pos
< fCheckpoints
.size(); pos
++) {
933 System
.out
.print(pos
+ ": " + "\t");
934 System
.out
.print( fCheckpoints
.get(pos
).getTimestamp() + "\t" );
935 System
.out
.println( fCheckpoints
.get(pos
).getLocation() );
940 * Return a String identifying this trace.
942 * @return String that identify this trace
945 public String
toString() {
946 String returnedData
="";
948 returnedData
+= "Path :" + getPath() + " ";
949 returnedData
+= "Trace:" + currentJniTrace
+ " ";
950 returnedData
+= "Event:" + currentLttngEvent
;
956 public void queueResult(LttngEvent data
) throws InterruptedException
{
957 super.queueResult(data
);
962 * EventTypeKey inner class
964 * This class is used to make the process of generating the HashMap key more transparent and so less error prone to use
969 // These two getEventTypeKey() functions should ALWAYS construct the key the same ways!
970 // Otherwise, every type search will fail!
972 static public String
getEventTypeKey(LttngEventType newEventType
) {
973 String key
= newEventType
.getTracefileName() + "/" + newEventType
.getCpuId().toString() + "/" + newEventType
.getMarkerName();
978 static public String
getEventTypeKey(JniEvent newEvent
) {
979 String key
= newEvent
.getParentTracefile().getTracefileName() + "/" + newEvent
.getParentTracefile().getCpuNumber() + "/" + newEvent
.requestEventMarker().getName();