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
> {
57 public static boolean printDebug
= false;
58 public static boolean uniqueEvent
= false;
60 private final static boolean SHOW_LTT_DEBUG_DEFAULT
= false;
61 private final static boolean IS_PARSING_NEEDED_DEFAULT
= true;
62 private final static int CHECKPOINT_PAGE_SIZE
= 1000;
64 // Reference to our JNI trace
65 private JniTrace currentJniTrace
= null;
68 // UNHACKED : We can no longer do that because TCF need to maintain several events at once.
69 // This is very slow to do so in LTTng, this has to be temporary.
71 // To save time, we will declare all component of the LttngEvent during the construction of the trace
72 // Then, while reading the trace, we will just SET the values instead of declaring new object
74 LttngTimestamp eventTimestamp
= null;
75 LttngEventSource eventSource
= null;
76 LttngEventContent eventContent
= null;
77 LttngEventReference eventReference
= null;
81 LttngEvent currentLttngEvent
= null;
83 // The current location
84 LttngLocation previousLocation
= null;
86 LttngEventType eventType
= null;
87 // Hashmap of the possible types of events (Tracefile/CPU/Marker in the JNI)
88 HashMap
<String
, LttngEventType
> traceTypes
= null;
89 // This vector will be used to quickly find a marker name from a position
90 Vector
<String
> traceTypeNames
= null;
93 * Default Constructor.<p>
95 * @param path Path to a <b>directory</b> that contain an LTTng trace.
97 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
99 public LTTngTrace(String path
) throws Exception
{
100 // Call with "wait for completion" true and "skip indexing" false
101 this(path
, true, false);
105 * Constructor, with control over the indexing.
107 * @param path Path to a <b>directory</b> that contain an LTTng trace.
108 * @param waitForCompletion Should we wait for indexign to complete before moving on.
110 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
112 public LTTngTrace(String path
, boolean waitForCompletion
) throws Exception
{
113 // Call with "skip indexing" false
114 this(path
, waitForCompletion
, false);
118 * Default constructor, with control over the indexing and possibility to bypass indexation
120 * @param path Path to a <b>directory</b> that contain an LTTng trace.
121 * @param waitForCompletion Should we wait for indexign to complete before moving on.
122 * @param bypassIndexing Should we bypass indexing completly? This is should only be useful for unit testing.
124 * @exception Exception (most likely LTTngTraceException or FileNotFoundException)
127 public LTTngTrace(String path
, boolean waitForCompletion
, boolean bypassIndexing
) throws Exception
{
128 super(path
, LttngEvent
.class, path
, CHECKPOINT_PAGE_SIZE
);
130 currentJniTrace
= JniTraceFactory
.getJniTrace(path
, SHOW_LTT_DEBUG_DEFAULT
);
132 catch (Exception e
) {
133 throw new LTTngTraceException(e
.getMessage());
136 // Export all the event types from the JNI side
137 traceTypes
= new HashMap
<String
, LttngEventType
>();
138 traceTypeNames
= new Vector
<String
>();
139 initialiseEventTypes(currentJniTrace
);
142 // Verify that all those "default constructor" are safe to use
143 eventTimestamp
= new LttngTimestamp();
144 eventSource
= new LttngEventSource();
145 eventType
= new LttngEventType();
146 eventContent
= new LttngEventContent(currentLttngEvent
);
147 eventReference
= new LttngEventReference(this.getName());
149 // Create the skeleton event
150 currentLttngEvent
= new LttngEvent(this, eventTimestamp
, eventSource
, eventType
, eventContent
, eventReference
, null);
152 // Create a new current location
153 previousLocation
= new LttngLocation();
156 // Set the currentEvent to the eventContent
157 eventContent
.setEvent(currentLttngEvent
);
159 // // Bypass indexing if asked
160 // if ( bypassIndexing == false ) {
164 // Even if we don't have any index, set ONE checkpoint
165 // fCheckpoints.add(new TmfCheckpoint(new LttngTimestamp(0L) , new LttngLocation() ) );
167 // Set the start time of the trace
168 setTimeRange( new TmfTimeRange( new LttngTimestamp(currentJniTrace
.getStartTime().getTime()),
169 new LttngTimestamp(currentJniTrace
.getEndTime().getTime())
175 * Copy constructor is forbidden for LttngEvenmStream
178 public LTTngTrace(LTTngTrace oldTrace
) throws Exception
{
179 this(oldTrace
.getPath(), false, true);
183 this.fCheckpoints
= oldTrace
.fCheckpoints
;
186 // This would only work if the index is already done
187 this.fCheckpoints = new Vector<TmfCheckpoint>( oldTrace.fCheckpoints.size() );
188 for (int x = 0; x<oldTrace.fCheckpoints.size(); x++){
189 TmfCheckpoint tmpCheckPoint = oldTrace.fCheckpoints.get(x);
190 this.fCheckpoints.add( new TmfCheckpoint(tmpCheckPoint.getTimestamp(), tmpCheckPoint.getLocation()) );
194 // Set the start time of the trace
195 setTimeRange( new TmfTimeRange( new LttngTimestamp(oldTrace
.getStartTime()),
196 new LttngTimestamp(oldTrace
.getEndTime()))
200 public LTTngTrace
createTraceCopy() {
201 LTTngTrace returnedTrace
= null;
204 returnedTrace
= new LTTngTrace(this);
206 catch (Exception e
) {
207 System
.out
.println("ERROR : Could not create LTTngTrace copy (createTraceCopy).\nError is : " + e
.getStackTrace());
210 return returnedTrace
;
214 public LTTngTrace
clone() {
215 LTTngTrace clone
= null;
217 clone
= (LTTngTrace
) super.clone();
219 clone
.currentJniTrace
= JniTraceFactory
.getJniTrace(getPath(), SHOW_LTT_DEBUG_DEFAULT
);
220 } catch (JniException e
) {
224 // Export all the event types from the JNI side
225 clone
.traceTypes
= new HashMap
<String
, LttngEventType
>();
226 clone
.traceTypeNames
= new Vector
<String
>();
227 clone
.initialiseEventTypes(clone
.currentJniTrace
);
229 // Verify that all those "default constructor" are safe to use
230 clone
.eventTimestamp
= new LttngTimestamp();
231 clone
.eventSource
= new LttngEventSource();
232 clone
.eventType
= new LttngEventType();
233 clone
.eventContent
= new LttngEventContent(clone
.currentLttngEvent
);
234 clone
.eventReference
= new LttngEventReference(this.getName());
236 // Create the skeleton event
237 clone
.currentLttngEvent
= new LttngEvent(this, clone
.eventTimestamp
, clone
.eventSource
, clone
.eventType
, clone
.eventContent
, clone
.eventReference
, null);
239 // Create a new current location
240 clone
.previousLocation
= new LttngLocation();
242 // Set the currentEvent to the eventContent
243 clone
.eventContent
.setEvent(clone
.currentLttngEvent
);
245 // Set the start time of the trace
246 setTimeRange(new TmfTimeRange(
247 new LttngTimestamp(clone
.currentJniTrace
.getStartTime().getTime()),
248 new LttngTimestamp(clone
.currentJniTrace
.getEndTime().getTime())
251 catch (CloneNotSupportedException e
) {
259 * Fill out the HashMap with "Type" (Tracefile/Marker)
261 * This should be called at construction once the trace is open
263 private void initialiseEventTypes(JniTrace trace
) {
265 LttngEventType tmpType
= null;
266 String
[] markerFieldsLabels
= null;
268 String newTracefileKey
= null;
269 Integer newMarkerKey
= null;
271 JniTracefile newTracefile
= null;
272 JniMarker newMarker
= null;
274 // First, obtain an iterator on TRACEFILES of owned by the TRACE
275 Iterator
<String
> tracefileItr
= trace
.getTracefilesMap().keySet().iterator();
276 while ( tracefileItr
.hasNext() ) {
277 newTracefileKey
= tracefileItr
.next();
278 newTracefile
= trace
.getTracefilesMap().get(newTracefileKey
);
280 // From the TRACEFILE read, obtain its MARKER
281 Iterator
<Integer
> markerItr
= newTracefile
.getTracefileMarkersMap().keySet().iterator();
282 while ( markerItr
.hasNext() ) {
283 newMarkerKey
= markerItr
.next();
284 newMarker
= newTracefile
.getTracefileMarkersMap().get(newMarkerKey
);
286 // From the MARKER we can obtain the MARKERFIELDS keys (i.e. labels)
287 markerFieldsLabels
= newMarker
.getMarkerFieldsHashMap().keySet().toArray( new String
[newMarker
.getMarkerFieldsHashMap().size()] );
288 tmpType
= new LttngEventType(newTracefile
.getTracefileName(), newTracefile
.getCpuNumber(), newMarker
.getName(), markerFieldsLabels
);
290 // Add the type to the map/vector
291 addEventTypeToMap(tmpType
);
297 * Add a new type to the HashMap
299 * As the hashmap use a key format that is a bit dangerous to use, we should always add using this function.
301 private void addEventTypeToMap(LttngEventType newEventType
) {
302 String newTypeKey
= EventTypeKey
.getEventTypeKey(newEventType
);
304 this.traceTypes
.put(newTypeKey
, newEventType
);
305 this.traceTypeNames
.add(newTypeKey
);
309 // * Index the current trace.
311 // * @param useless This boolean is only to comply to the interface and will be ignored.
314 // public synchronized void indexTrace(boolean useless) {
318 // // Start time need to be null to detect none have been set
319 // // LastTime need to exist so we can ajust it as we go
320 // LttngTimestamp startTime = null;
321 // LttngTimestamp lastTime = new LttngTimestamp();
323 // // Position the trace at the beginning
324 // TmfContext context = seekEvent( new LttngTimestamp(0L) );
326 // // Read the first event and extract the location
327 // LttngEvent tmpEvent = (LttngEvent)getNextEvent(context);
329 // // If we read the first event, define the start time.
330 // if ( tmpEvent != null ) {
331 // startTime = new LttngTimestamp( tmpEvent.getTimestamp() );
332 // lastTime.setValue(tmpEvent.getTimestamp().getValue());
335 // // Now, we read each event until we hit the end of the trace
336 // // We will create a new checkpoint every "getCacheSize()" event
337 // while ( tmpEvent != null) {
338 // // Update the last time each time we read a new event
339 // lastTime.setValue(tmpEvent.getTimestamp().getValue());
341 // // Save a check point if needed
342 // if ((nbEvents++ % getCacheSize()) == 0) {
344 // // We need to NEW each stuff we put in checkpoint
345 // // Otherwise everything will be the same!
346 // LttngTimestamp tmpTimestamp = new LttngTimestamp( (LttngTimestamp)tmpEvent.getTimestamp() );
347 // LttngLocation newLocation = new LttngLocation( (LttngTimestamp)tmpEvent.getTimestamp() );
349 // fCheckpoints.add(new TmfCheckpoint(tmpTimestamp, newLocation ) );
351 // // Read the next event
352 // tmpEvent = (LttngEvent)getNextEvent(context);
355 // // If we have a start time, we should have an end time as well
356 // // Issue the new range
357 // if (startTime != null) {
358 // setTimeRange( new TmfTimeRange(startTime, lastTime) );
359 // notifyListeners(getTimeRange() );
362 // // Ajust the total number of event in the trace
363 // fNbEvents = nbEvents;
364 // //printCheckpointsVector();
365 // //printDebug = true;
369 * Return the latest saved location.
370 * Note : Modifying the returned location may result in buggy positionning!
372 * @return The LttngLocation as it was after the last operation.
374 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
377 public synchronized ITmfLocation
<?
> getCurrentLocation() {
378 return previousLocation
;
382 * Position the trace to the event at the given location.<p>
383 * NOTE : Seeking by location is very fast compare to seeking by position
384 * but is still slower than "ReadNext", avoid using it for small interval.
386 * @param location Location of the event in the trace.
387 * If no event available at this exact location, we will position ourself to the next one.
389 * @return The TmfContext that point to this event
391 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
392 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
395 public synchronized TmfContext
seekLocation(ITmfLocation
<?
> location
) {
397 if ( printDebug
== true ) {
398 System
.out
.println("seekLocation(location) location -> " + location
);
401 // If the location in context is null, create a new one
402 LttngLocation curLocation
= null;
403 if ( location
== null ) {
404 curLocation
= new LttngLocation();
407 curLocation
= (LttngLocation
)location
;
411 // Update to location should (and will) be done in SeekEvent.
413 // The only seek valid in LTTng is with the time, we call seekEvent(timestamp)
414 return seekEvent( curLocation
.getOperationTime() );
418 * Position the trace to the event at the given time.<p>
419 * NOTE : Seeking by time is very fast compare to seeking by position
420 * but is still slower than "ReadNext", avoid using it for small interval.
422 * @param timestamp Time of the event in the trace.
423 * If no event available at this exact time, we will position ourself to the next one.
425 * @return The TmfContext that point to this event
427 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
428 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
431 public synchronized TmfContext
seekEvent(TmfTimestamp timestamp
) {
433 if ( printDebug
== true ) {
434 System
.out
.println("seekEvent(timestamp) timestamp -> " + timestamp
);
438 currentJniTrace
.seekToTime(new JniTime(timestamp
.getValue()));
440 // Save the time at which we seeked
441 previousLocation
.setOperationTime(timestamp
.getValue());
442 // Set the operation marker as seek, to be able to detect we did "seek" this event
443 previousLocation
.setLastOperationSeek();
446 // Is that too paranoid?
448 // We don't trust what upper level could do with our internal location
449 // so we create a new one to return instead
450 LttngLocation curLocation
= new LttngLocation(previousLocation
);
452 return new TmfContext( curLocation
);
456 * Position the trace to the event at the given position (rank).<p>
457 * NOTE : Seeking by position is very slow in LTTng, consider seeking by timestamp.
459 * @param position Position (or rank) of the event in the trace, starting at 0.
461 * @return The TmfContext that point to this event
463 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
464 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
467 public synchronized TmfContext
seekEvent(long position
) {
469 if ( printDebug
== true ) {
470 System
.out
.println("seekEvent(position) position -> " + position
);
473 TmfTimestamp timestamp
= null;
474 long index
= position
/ getCacheSize();
476 // Get the timestamp of the closest check point to the given position
477 if (fCheckpoints
.size() > 0) {
478 if (index
>= fCheckpoints
.size()) {
479 index
= fCheckpoints
.size() - 1;
481 timestamp
= (TmfTimestamp
)fCheckpoints
.elementAt((int)index
).getTimestamp();
483 // If none, take the start time of the trace
485 timestamp
= getStartTime();
488 // Seek to the found time
489 TmfContext tmpContext
= seekEvent(timestamp
);
490 previousLocation
= (LttngLocation
)tmpContext
.getLocation();
492 // Ajust the index of the event we found at this check point position
493 Long currentPosition
= index
* getCacheSize();
495 Long lastTimeValueRead
= 0L;
497 // Get the event at current position. This won't move to the next one
498 JniEvent tmpJniEvent
= currentJniTrace
.findNextEvent();
499 // Now that we are positionned at the checkpoint,
500 // we need to "readNext" (Position - CheckpointPosition) times or until trace "run out"
501 while ( (tmpJniEvent
!= null) && ( currentPosition
< position
) ) {
502 tmpJniEvent
= currentJniTrace
.readNextEvent();
506 // If we found our event, save its timestamp
507 if ( tmpJniEvent
!= null ) {
508 lastTimeValueRead
= tmpJniEvent
.getEventTime().getTime();
511 // Set the operation marker as seek, to be able to detect we did "seek" this event
512 previousLocation
.setLastOperationSeek();
513 // Save read event time
514 previousLocation
.setOperationTime(lastTimeValueRead
);
517 // Is that too paranoid?
519 // We don't trust what upper level could do with our internal location
520 // so we create a new one to return instead
521 LttngLocation curLocation
= new LttngLocation(previousLocation
);
523 return new TmfContext( curLocation
);
527 * Return the event in the trace according to the given context. Read it if necessary.<p>
528 * Similar (same?) as ParseEvent except that calling GetNext twice read the next one the second time.
530 * @param context Current TmfContext where to get the event
532 * @return The LttngEvent we read of null if no event are available
534 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
535 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
538 public int nbEventsRead
= 0;
540 public synchronized LttngEvent
getNextEvent(TmfContext context
) {
542 if ( printDebug
== true ) {
543 System
.out
.println("getNextEvent(context) context.getLocation() -> " + context
.getLocation());
546 LttngEvent returnedEvent
= null;
547 LttngLocation curLocation
= null;
549 // If the location in context is null, create a new one
550 if ( context
.getLocation() == null ) {
551 curLocation
= new LttngLocation();
552 context
.setLocation(curLocation
);
555 // Otherwise, we use the one in context; it should be a valid LttngLocation
556 curLocation
= (LttngLocation
)context
.getLocation();
560 // TMF assumes it is possible to read (GetNextEvent) to the next Event once ParseEvent() is called
561 // In LTTNG, there is not difference between "Parsing" and "Reading" an event.
562 // Since parsing/reading invalidate the previous event,
563 // we need to make sure the sequence ParseEvent() -> GetNextEvent() will not actually move to the next event.
564 // To do so, we avoid moving for call to "GetNextEvent()" that follow call to a call to "ParseEvent()".
565 // However, calling ParseEvent() -> GetNextEvent() -> GetNextEvent() will only move next by one.
567 // *** Positioning trick :
568 // GetNextEvent only read the trace if :
569 // 1- The last operation was NOT a ParseEvent --> A read is required
571 // 2- The time of the previous location is different from the current one --> A seek + a read is required
572 if ( (curLocation
.isLastOperationParse() != true) ||
573 (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) )
575 if ( previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) {
576 if ( printDebug
== true ) {
577 System
.out
.println("\t\tSeeking in getNextEvent. [ LastTime : " + previousLocation
.getOperationTimeValue() + " CurrentTime" + curLocation
.getOperationTimeValue() + " ]");
579 seekEvent( curLocation
.getOperationTime() );
581 // Read the next event from the trace. The last one will NO LONGER BE VALID.
582 returnedEvent
= readEvent(curLocation
);
585 // Set the operation marker as read to both location, to be able to detect we did "read" this event
586 previousLocation
.setLastOperationReadNext();
587 curLocation
.setLastOperationReadNext();
590 // No event was read, just return the one currently loaded (the last one we read)
591 returnedEvent
= currentLttngEvent
;
594 // Reset (erase) the operation marker to both location, to be able to detect we did NOT "read" this event
595 previousLocation
.resetLocationState();
596 curLocation
.resetLocationState();
599 // If we read an event, set it's time to the locations (both previous and current)
600 if ( returnedEvent
!= null ) {
601 previousLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
602 curLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
604 updateIndex(context
, context
.getRank(), returnedEvent
.getTimestamp());
605 context
.updateRank(1);
608 return returnedEvent
;
613 * Return the event in the trace according to the given context. Read it if necessary.<p>
614 * Similar (same?) as GetNextEvent except that calling ParseEvent twice will return the same event
616 * @param context Current TmfContext where to get the event
618 * @return The LttngEvent we read of null if no event are available
620 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
621 * @see org.eclipse.linuxtools.tmf.trace.TmfContext
624 public synchronized LttngEvent
parseEvent(TmfContext context
) {
626 if ( printDebug
== true ) {
627 System
.out
.println("parseEvent(context) context.getLocation() -> " + context
.getLocation());
630 LttngEvent returnedEvent
= null;
631 LttngLocation curLocation
= null;
633 // If the location in context is null, create a new one
634 if ( context
.getLocation() == null ) {
635 curLocation
= new LttngLocation();
636 context
.setLocation(curLocation
);
638 // Otherwise, we use the one in context; it should be a valid LttngLocation
640 curLocation
= (LttngLocation
)context
.getLocation();
644 // TMF assumes it is possible to read (GetNextEvent) to the next Event once ParseEvent() is called
645 // In LTTNG, there is not difference between "Parsing" and "Reading" an event.
646 // So, before "Parsing" an event, we have to make sure we didn't "Read" it alreafy.
647 // Also, "Reading" invalidate the previous Event in LTTNG and seek back is very costly,
648 // so calling twice "Parse" will return the same event, giving a way to get the "Currently loaded" event
650 // *** Positionning trick :
651 // ParseEvent only read the trace if :
652 // 1- The last operation was NOT a ParseEvent or a GetNextEvent --> A read is required
654 // 2- The time of the previous location is different from the current one --> A seek + a read is required
655 if ( ( (curLocation
.isLastOperationParse() != true) && ((curLocation
.isLastOperationReadNext() != true)) ) ||
656 (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) )
658 // Previous time != Current time : We need to reposition to the current time
659 if (previousLocation
.getOperationTimeValue() != curLocation
.getOperationTimeValue() ) {
660 if ( printDebug
== true ) {
661 System
.out
.println("\t\tSeeking in getNextEvent. [ LastTime : " + previousLocation
.getOperationTimeValue() + " CurrentTime" + curLocation
.getOperationTimeValue() + " ]");
663 seekEvent( curLocation
.getOperationTime() );
666 // Read the next event from the trace. The last one will NO LONGER BE VALID.
667 returnedEvent
= readEvent(curLocation
);
670 // No event was read, just return the one currently loaded (the last one we read)
671 returnedEvent
= currentLttngEvent
;
674 // If we read an event, set it's time to the locations (both previous and current)
675 if ( returnedEvent
!= null ) {
676 previousLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
677 curLocation
.setOperationTime((LttngTimestamp
)returnedEvent
.getTimestamp());
680 // Set the operation marker as parse to both location, to be able to detect we already "read" this event
681 previousLocation
.setLastOperationParse();
682 curLocation
.setLastOperationParse();
684 return returnedEvent
;
688 * Read the next event from the JNI and convert it as Lttng Event<p>
690 * @param location Current LttngLocation that to be updated with the event timestamp
692 * @return The LttngEvent we read of null if no event are available
694 * @see org.eclipse.linuxtools.lttng.event.LttngLocation
695 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
697 private synchronized LttngEvent
readEvent(LttngLocation location
) {
698 LttngEvent returnedEvent
= null;
699 JniEvent tmpEvent
= null;
701 // Read the next event from JNI. THIS WILL INVALIDATE THE CURRENT LTTNG EVENT.
702 tmpEvent
= currentJniTrace
.readNextEvent();
704 if ( tmpEvent
!= null ) {
706 // Convert will update the currentLttngEvent
707 returnedEvent
= convertJniEventToTmf(tmpEvent
);
709 location
.setOperationTime( (LttngTimestamp
)returnedEvent
.getTimestamp() );
712 // If the read failed (likely the last event in the trace), set the LastReadTime to the JNI time
713 // That way, even if we try to read again, we will step over the bogus seek and read
715 location
.setOperationTime( getEndTime().getValue() + 1 );
718 return returnedEvent
;
722 * Method to convert a JniEvent into a LttngEvent.<p>
724 * Note : This method will call LttngEvent convertEventJniToTmf(JniEvent, boolean)
725 * with a default value for isParsingNeeded
727 * @param newEvent The JniEvent to convert into LttngEvent
729 * @return The converted LttngEvent
731 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
732 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
734 public synchronized LttngEvent
convertJniEventToTmf(JniEvent newEvent
) {
735 currentLttngEvent
= convertJniEventToTmf(newEvent
, IS_PARSING_NEEDED_DEFAULT
);
737 return currentLttngEvent
;
741 * Method to convert a JniEvent into a LttngEvent
743 * @param jniEvent The JniEvent to convert into LttngEvent
744 * @param isParsingNeeded A boolean value telling if the event should be parsed or not.
746 * @return The converted LttngEvent
748 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniEvent
749 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
751 public synchronized LttngEvent
convertJniEventToTmf(JniEvent jniEvent
, boolean isParsingNeeded
) {
753 if ( uniqueEvent
== true ) {
756 // UNHACKED : We can no longer do that because TCF need to maintain several events at once.
757 // This is very slow to do so in LTTng, this has to be temporary.
759 // To save time here, we only set value instead of allocating new object
760 // This give an HUGE performance improvement
761 // all allocation done in the LttngTrace constructor
763 eventTimestamp
.setValue(jniEvent
.getEventTime().getTime());
764 eventSource
.setSourceId(jniEvent
.requestEventSource());
766 eventType
= traceTypes
.get( EventTypeKey
.getEventTypeKey(jniEvent
) );
768 eventReference
.setValue(jniEvent
.getParentTracefile().getTracefilePath());
769 eventReference
.setTracepath(this.getName());
771 eventContent
.emptyContent();
773 currentLttngEvent
.setType(eventType
);
774 // Save the jni reference
775 currentLttngEvent
.updateJniEventReference(jniEvent
);
777 // Parse now if was asked
778 // Warning : THIS IS SLOW
779 if (isParsingNeeded
== true ) {
780 eventContent
.getFields();
783 return currentLttngEvent
;
786 return convertJniEventToTmfMultipleEventEvilFix(jniEvent
, isParsingNeeded
);
792 * This method is a temporary fix to support multiple events at once in TMF
793 * This is expected to be slow and should be fixed in another way.
794 * See comment in convertJniEventToTmf();
796 * @param jniEvent The current JNI Event
797 * @return Current Lttng Event fully parsed
799 private synchronized LttngEvent
convertJniEventToTmfMultipleEventEvilFix(JniEvent jniEvent
, boolean isParsingNeeded
) {
801 // Below : the "fix" with all the new and the full-parse
802 // Allocating new memory is slow.
803 // Parsing every events is very slow.
804 eventTimestamp
= new LttngTimestamp(jniEvent
.getEventTime().getTime());
805 eventSource
= new LttngEventSource(jniEvent
.requestEventSource());
806 eventReference
= new LttngEventReference(jniEvent
.getParentTracefile().getTracefilePath(), this.getName());
807 eventType
= new LttngEventType(traceTypes
.get( EventTypeKey
.getEventTypeKey(jniEvent
) ));
808 eventContent
= new LttngEventContent(currentLttngEvent
);
809 currentLttngEvent
= new LttngEvent(this, eventTimestamp
, eventSource
, eventType
, eventContent
, eventReference
, null);
811 // The jni reference is no longer reliable but we will keep it anyhow
812 currentLttngEvent
.updateJniEventReference(jniEvent
);
813 // Ensure that the content is correctly set
814 eventContent
.setEvent(currentLttngEvent
);
816 // Parse the event if it was needed
818 // ONLY for testing, NOT parsing events with non-unique events WILL result in segfault in the JVM
819 if ( isParsingNeeded
== true ) {
820 eventContent
.getFields();
823 return currentLttngEvent
;
829 * Reference to the current LttngTrace we are reading from.<p>
831 * Note : This bypass the framework and should not be use, except for testing!
833 * @return Reference to the current LttngTrace
835 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
837 public JniTrace
getCurrentJniTrace() {
838 return currentJniTrace
;
843 * Return a reference to the current LttngEvent we have in memory.
845 * @return The current (last read) LttngEvent
847 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
849 public synchronized LttngEvent
getCurrentEvent() {
850 return currentLttngEvent
;
854 * Get the major version number for the current trace
856 * @return Version major or -1 if unknown
858 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
861 public short getVersionMajor() {
862 if ( currentJniTrace
!= null ) {
863 return currentJniTrace
.getLttMajorVersion();
871 * Get the minor version number for the current trace
873 * @return Version minor or -1 if unknown
875 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
878 public short getVersionMinor() {
879 if ( currentJniTrace
!= null ) {
880 return currentJniTrace
.getLttMinorVersion();
888 * Get the number of CPU for this trace
890 * @return Number of CPU or -1 if unknown
892 * @see org.eclipse.linuxtools.org.eclipse.linuxtools.lttng.jni.JniTrace
895 public int getCpuNumber() {
896 if ( currentJniTrace
!= null ) {
897 return currentJniTrace
.getCpuNumber();
905 * Print the content of the checkpoint vector.<p>
907 * This is intended for debug purpose only.
909 public void printCheckpointsVector() {
910 System
.out
.println("StartTime : " + getTimeRange().getStartTime().getValue());
911 System
.out
.println("EndTime : " + getTimeRange().getEndTime().getValue());
913 for ( int pos
=0; pos
< fCheckpoints
.size(); pos
++) {
914 System
.out
.print(pos
+ ": " + "\t");
915 System
.out
.print( fCheckpoints
.get(pos
).getTimestamp() + "\t" );
916 System
.out
.println( fCheckpoints
.get(pos
).getLocation() );
921 * Return a String identifying this trace.
923 * @return String that identify this trace
926 public String
toString() {
927 String returnedData
="";
929 returnedData
+= "Path :" + getPath() + " ";
930 returnedData
+= "Trace:" + currentJniTrace
+ " ";
931 returnedData
+= "Event:" + currentLttngEvent
;
938 * EventTypeKey inner class
940 * This class is used to make the process of generating the HashMap key more transparent and so less error prone to use
945 // These two getEventTypeKey() functions should ALWAYS construct the key the same ways!
946 // Otherwise, every type search will fail!
948 static public String
getEventTypeKey(LttngEventType newEventType
) {
949 String key
= newEventType
.getTracefileName() + "/" + newEventType
.getCpuId().toString() + "/" + newEventType
.getMarkerName();
954 static public String
getEventTypeKey(JniEvent newEvent
) {
955 String key
= newEvent
.getParentTracefile().getTracefileName() + "/" + newEvent
.getParentTracefile().getCpuNumber() + "/" + newEvent
.requestEventMarker().getName();