1 package org
.eclipse
.linuxtools
.lttng
.jni
;
2 /*******************************************************************************
3 * Copyright (c) 2009 Ericsson
5 * All rights reserved. This program and the accompanying materials are
6 * made available under the terms of the Eclipse Public License v1.0 which
7 * accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
15 import java
.util
.HashMap
;
17 import org
.eclipse
.linuxtools
.lttng
.jni
.common
.JniTime
;
18 import org
.eclipse
.linuxtools
.lttng
.jni
.common
.Jni_C_Pointer
;
19 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniException
;
20 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniNoSuchEventException
;
21 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniTracefileException
;
22 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniTracefileWithoutEventException
;
25 * <b><u>JniTracefile</u></b>
27 * A tracefile own an event of a certain type.<br>
28 * Provides access to the LttTracefile C structure in java.
30 * Most important fields in the JniTracefile are :
32 * <li> a JniTracefile path (a tracefile <b>file</b> within a JniTrace directory)
33 * <li> a name (basically the name without the directory part)
34 * <li> a reference to a single event object
35 * <li> a HashMap of marker associated with this tracefile
38 public abstract class JniTracefile
extends Jni_C_Common
{
40 // Internal C pointer of the JniTracefile used in LTT
41 private Jni_C_Pointer thisTracefilePtr
= new Jni_C_Pointer();
43 // Reference to the parent trace
44 private JniTrace parentTrace
= null;
46 // Data we should populate from LTT
47 // Note that all type have been scaled up as there is no "unsigned" in java
48 // This might be a problem about "unsigned long" as there is no equivalent in java
49 private boolean isCpuOnline
= false;
50 private String tracefilePath
= "";
51 private String tracefileName
= "";
52 private long cpuNumber
= 0;
54 private long pgid
= 0;
55 private long creation
= 0;
57 // Internal C pointer for trace and marker
58 private Jni_C_Pointer tracePtr
= null;
59 private Jni_C_Pointer markerDataPtr
= null;
61 private int CFileDescriptor
= 0;
62 private long fileSize
= 0;
63 private long blocksNumber
= 0;
64 private boolean isBytesOrderReversed
= false;
65 private boolean isFloatWordOrdered
= false;
66 private long alignement
= 0;
67 private long bufferHeaderSize
= 0;
68 private int bitsOfCurrentTimestampCounter
= 0;
69 private int bitsOfEvent
= 0;
70 private long currentTimestampCounterMask
= 0;
71 private long currentTimestampCounterMaskNextBit
= 0;
72 private long eventsLost
= 0;
73 private long subBufferCorrupt
= 0;
74 private JniEvent currentEvent
= null;
76 // Internal C pointer for trace and marker
77 private Jni_C_Pointer bufferPtr
= null;
79 private long bufferSize
= 0;
81 // This map will hold markers_info owned by this tracefile
82 private HashMap
<Integer
, JniMarker
> tracefileMarkersMap
= null;
84 // Native access functions
85 protected native boolean ltt_getIsCpuOnline(long tracefilePtr
);
86 protected native String
ltt_getTracefilepath(long tracefilePtr
);
87 protected native String
ltt_getTracefilename(long tracefilePtr
);
88 protected native long ltt_getCpuNumber(long tracefilePtr
);
89 protected native long ltt_getTid(long tracefilePtr
);
90 protected native long ltt_getPgid(long tracefilePtr
);
91 protected native long ltt_getCreation(long tracefilePtr
);
92 protected native long ltt_getTracePtr(long tracefilePtr
);
93 protected native long ltt_getMarkerDataPtr(long tracefilePtr
);
94 protected native int ltt_getCFileDescriptor(long tracefilePtr
);
95 protected native long ltt_getFileSize(long tracefilePtr
);
96 protected native long ltt_getBlockNumber(long tracefilePtr
);
97 protected native boolean ltt_getIsBytesOrderReversed(long tracefilePtr
);
98 protected native boolean ltt_getIsFloatWordOrdered(long tracefilePtr
);
99 protected native long ltt_getAlignement(long tracefilePtr
);
100 protected native long ltt_getBufferHeaderSize(long tracefilePtr
);
101 protected native int ltt_getBitsOfCurrentTimestampCounter(long tracefilePtr
);
102 protected native int ltt_getBitsOfEvent(long tracefilePtr
);
103 protected native long ltt_getCurrentTimestampCounterMask(long tracefilePtr
);
104 protected native long ltt_getCurrentTimestampCounterMaskNextBit(long tracefilePtr
);
105 protected native long ltt_getEventsLost(long tracefilePtr
);
106 protected native long ltt_getSubBufferCorrupt(long tracefilePtr
);
107 protected native long ltt_getEventPtr(long tracefilePtr
);
108 protected native long ltt_getBufferPtr(long tracefilePtr
);
109 protected native long ltt_getBufferSize(long tracefilePtr
);
111 // Method to fill a map with marker object
112 protected native void ltt_feedAllMarkers(long tracefilePtr
);
114 // Debug native function, ask LTT to print tracefile structure
115 protected native void ltt_printTracefile(long tracefilePtr
);
118 // To uncomment as soon as the library will be able to load multiple version at once
120 // System.loadLibrary("lttvtraceread_loader");
124 * Default constructor is forbidden
126 protected JniTracefile() {
130 * Copy constructor.<p>
132 * @param oldTracefile Reference to the JniTracefile you want to copy.
134 public JniTracefile(JniTracefile oldTracefile
) {
135 thisTracefilePtr
= oldTracefile
.thisTracefilePtr
;
136 parentTrace
= oldTracefile
.parentTrace
;
137 tracefileMarkersMap
= oldTracefile
.tracefileMarkersMap
;
138 isCpuOnline
= oldTracefile
.isCpuOnline
;
139 tracefilePath
= oldTracefile
.tracefilePath
;
140 tracefileName
= oldTracefile
.tracefileName
;
141 cpuNumber
= oldTracefile
.cpuNumber
;
142 tid
= oldTracefile
.tid
;
143 pgid
= oldTracefile
.pgid
;
144 creation
= oldTracefile
.creation
;
145 tracePtr
= oldTracefile
.tracePtr
;
146 markerDataPtr
= oldTracefile
.markerDataPtr
;
147 CFileDescriptor
= oldTracefile
.CFileDescriptor
;
148 fileSize
= oldTracefile
.fileSize
;
149 blocksNumber
= oldTracefile
.blocksNumber
;
150 isBytesOrderReversed
= oldTracefile
.isBytesOrderReversed
;
151 isFloatWordOrdered
= oldTracefile
.isFloatWordOrdered
;
152 alignement
= oldTracefile
.alignement
;
153 bufferHeaderSize
= oldTracefile
.bufferHeaderSize
;
154 bitsOfCurrentTimestampCounter
= oldTracefile
.bitsOfCurrentTimestampCounter
;
155 bitsOfEvent
= oldTracefile
.bitsOfEvent
;
156 currentTimestampCounterMask
= oldTracefile
.currentTimestampCounterMask
;
157 currentTimestampCounterMaskNextBit
= oldTracefile
.currentTimestampCounterMaskNextBit
;
158 eventsLost
= oldTracefile
.eventsLost
;
159 subBufferCorrupt
= oldTracefile
.subBufferCorrupt
;
160 currentEvent
= oldTracefile
.currentEvent
;
161 bufferPtr
= oldTracefile
.bufferPtr
;
162 bufferSize
= oldTracefile
.bufferSize
;
166 * Constructor, using C pointer.<p>
168 * @param newPtr The pointer of an already opened LttTracefile C Structure
170 * @exception JniException
172 * @see org.eclipse.linuxtools.lttng.jni.eclipse.linuxtools.lttng.jni.JniTrace
173 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Pointer
175 public JniTracefile(Jni_C_Pointer newPtr
, JniTrace newParentTrace
) throws JniException
{
176 thisTracefilePtr
= newPtr
;
177 parentTrace
= newParentTrace
;
178 tracefileMarkersMap
= new HashMap
<Integer
, JniMarker
>();
180 // Retrieve the trace file information and load the first event.
182 populateTracefileInformation();
183 } catch (JniNoSuchEventException e
) {
184 throw new JniTracefileWithoutEventException(
185 "JniEvent constructor reported that no event of this type are usable. (Jaf_Tracefile)");
190 * Read the next event of this tracefile.<p>
192 * Note : If the read succeed, the event will be populated.<p>
194 * @return LTT read status, as defined in Jni_C_Common
196 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
198 public int readNextEvent() {
199 return currentEvent
.readNextEvent();
203 * Seek to the given time.<p>
205 * Note : If the seek succeed, the event will be populated.
207 * @param seekTime The timestamp where to seek.
209 * @return LTT read status, as defined in Jni_C_Common
211 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
213 public int seekToTime(JniTime seekTime
) {
214 return currentEvent
.seekToTime(seekTime
);
218 * This function populates the tracefile data with data from LTT
220 * @throws JniException
222 private void populateTracefileInformation() throws JniException
{
223 if (thisTracefilePtr
.getPointer() == NULL
) {
224 throw new JniTracefileException(
225 "Pointer is NULL, trace closed? (populateTracefileInformation)");
228 isCpuOnline
= ltt_getIsCpuOnline( thisTracefilePtr
.getPointer() );
229 tracefilePath
= ltt_getTracefilepath( thisTracefilePtr
.getPointer() );
230 tracefileName
= ltt_getTracefilename( thisTracefilePtr
.getPointer() );
231 cpuNumber
= ltt_getCpuNumber( thisTracefilePtr
.getPointer() );
232 tid
= ltt_getTid( thisTracefilePtr
.getPointer() );
233 pgid
= ltt_getPgid( thisTracefilePtr
.getPointer() );
234 creation
= ltt_getCreation( thisTracefilePtr
.getPointer() );
235 tracePtr
= new Jni_C_Pointer(ltt_getTracePtr( thisTracefilePtr
.getPointer()) );
236 markerDataPtr
= new Jni_C_Pointer(ltt_getMarkerDataPtr( thisTracefilePtr
.getPointer()) );
237 CFileDescriptor
= ltt_getCFileDescriptor( thisTracefilePtr
.getPointer() );
238 fileSize
= ltt_getFileSize( thisTracefilePtr
.getPointer() );
239 blocksNumber
= ltt_getBlockNumber( thisTracefilePtr
.getPointer() );
240 isBytesOrderReversed
= ltt_getIsBytesOrderReversed( thisTracefilePtr
.getPointer() );
241 isFloatWordOrdered
= ltt_getIsFloatWordOrdered( thisTracefilePtr
.getPointer() );
242 alignement
= ltt_getAlignement( thisTracefilePtr
.getPointer() );
243 bufferHeaderSize
= ltt_getBufferHeaderSize( thisTracefilePtr
.getPointer() );
244 bitsOfCurrentTimestampCounter
= ltt_getBitsOfCurrentTimestampCounter( thisTracefilePtr
.getPointer() );
245 bitsOfEvent
= ltt_getBitsOfEvent( thisTracefilePtr
.getPointer() );
246 currentTimestampCounterMask
= ltt_getCurrentTimestampCounterMask( thisTracefilePtr
.getPointer() );
247 currentTimestampCounterMaskNextBit
= ltt_getCurrentTimestampCounterMaskNextBit( thisTracefilePtr
.getPointer() );
248 eventsLost
= ltt_getEventsLost( thisTracefilePtr
.getPointer() );
249 subBufferCorrupt
= ltt_getSubBufferCorrupt( thisTracefilePtr
.getPointer() );
250 bufferPtr
= new Jni_C_Pointer(ltt_getBufferPtr( thisTracefilePtr
.getPointer()) );
251 bufferSize
= ltt_getBufferSize( thisTracefilePtr
.getPointer() );
253 // To fill the map is a bit different
254 ltt_feedAllMarkers( thisTracefilePtr
.getPointer() );
256 Jni_C_Pointer tmpEventPointer
= new Jni_C_Pointer(ltt_getEventPtr(thisTracefilePtr
.getPointer()));
257 currentEvent
= allocateNewJniEvent(tmpEventPointer
, tracefileMarkersMap
, this);
261 * Fills a map of all the markers associated with this tracefile.
263 * Note: This function is called from C and there is no way to propagate
264 * exception back to the caller without crashing JNI. Therefore, it MUST
265 * catch all exceptions.
267 * @param markerId Id of the marker (int)
268 * @param markerInfoPtr C Pointer to a marker_info C structure
270 @SuppressWarnings("unused")
271 private void addMarkersFromC(int markerId
, long markerInfoPtr
) {
272 // Create a new tracefile object and insert it in the map
273 // the tracefile fill itself with LTT data while being constructed
275 JniMarker newMarker
= allocateNewJniMarker( new Jni_C_Pointer(markerInfoPtr
) );
277 tracefileMarkersMap
.put(markerId
, newMarker
);
278 } catch (Exception e
) {
279 printlnC("Failed to add marker to tracefileMarkersMap!(addMarkersFromC)\n\tException raised : " + e
.toString());
283 // Access to class variable. Most of them doesn't have setter
284 public boolean getIsCpuOnline() {
288 public String
getTracefilePath() {
289 return tracefilePath
;
292 public String
getTracefileName() {
293 return tracefileName
;
296 public long getCpuNumber() {
300 public long getTid() {
304 public long getPgid() {
308 public long getCreation() {
312 public Jni_C_Pointer
getTracePtr() {
316 public Jni_C_Pointer
getMarkerDataPtr() {
317 return markerDataPtr
;
320 public int getCFileDescriptor() {
321 return CFileDescriptor
;
324 public long getFileSize() {
328 public long getBlocksNumber() {
332 public boolean getIsBytesOrderReversed() {
333 return isBytesOrderReversed
;
336 public boolean getIsFloatWordOrdered() {
337 return isFloatWordOrdered
;
340 public long getAlignement() {
344 public long getBufferHeaderSize() {
345 return bufferHeaderSize
;
348 public int getBitsOfCurrentTimestampCounter() {
349 return bitsOfCurrentTimestampCounter
;
352 public int getBitsOfEvent() {
356 public long getCurrentTimestampCounterMask() {
357 return currentTimestampCounterMask
;
360 public long getCurrentTimestampCounterMaskNextBit() {
361 return currentTimestampCounterMaskNextBit
;
364 public long getEventsLost() {
368 public long getSubBufferCorrupt() {
369 return subBufferCorrupt
;
372 public JniEvent
getCurrentEvent() {
376 public Jni_C_Pointer
getBufferPtr() {
380 public long getBufferSize() {
384 public HashMap
<Integer
, JniMarker
> getTracefileMarkersMap() {
385 return tracefileMarkersMap
;
389 * Parent trace of this tracefile.<p>
391 * @return The parent trace
393 * @see org.eclipse.linuxtools.lttng.jni.eclipse.linuxtools.lttng.jni.JniTrace
395 public JniTrace
getParentTrace() {
400 * Pointer to the LttTracefile C structure<p>
402 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
403 * know what you are doing.<p>
405 * @return The actual (long converted) pointer or NULL.
407 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Pointer
409 public Jni_C_Pointer
getTracefilePtr() {
410 return thisTracefilePtr
;
414 * Print information for this tracefile.
415 * <u>Intended to debug</u><p>
417 * This function will call Ltt to print, so information printed will be the
418 * one from the C structure, not the one populated in java.<p>
420 * This function will not throw but will complain loudly if pointer is NULL.
422 public void printTracefileInformation() {
424 // If null pointer, print a warning!
425 if (thisTracefilePtr
.getPointer() == NULL
) {
426 printlnC("Pointer is NULL, cannot print. (printTracefileInformation)");
429 ltt_printTracefile( thisTracefilePtr
.getPointer() );
435 * <u>Intended to debug</u><p>
437 * @return Attributes of the object concatenated in String
440 public String
toString() {
441 String returnData
= "";
443 returnData
+= "isCpuOnline : " + isCpuOnline
+ "\n";
444 returnData
+= "tracefilePath : " + tracefilePath
+ "\n";
445 returnData
+= "tracefileName : " + tracefileName
+ "\n";
446 returnData
+= "cpuNumber : " + cpuNumber
+ "\n";
447 returnData
+= "tid : " + tid
+ "\n";
448 returnData
+= "pgid : " + pgid
+ "\n";
449 returnData
+= "creation : " + creation
+ "\n";
450 returnData
+= "tracePtr : " + tracePtr
+ "\n";
451 returnData
+= "markerDataPtr : " + markerDataPtr
+ "\n";
452 returnData
+= "CFileDescriptor : " + CFileDescriptor
+ "\n";
453 returnData
+= "fileSize : " + fileSize
+ "\n";
454 returnData
+= "blocksNumber : " + blocksNumber
+ "\n";
455 returnData
+= "isBytesOrderReversed : " + isBytesOrderReversed
+ "\n";
456 returnData
+= "isFloatWordOrdered : " + isFloatWordOrdered
+ "\n";
457 returnData
+= "alignement : " + alignement
+ "\n";
458 returnData
+= "bufferHeaderSize : " + bufferHeaderSize
+ "\n";
459 returnData
+= "bitsOfCurrentTimestampCounter : " + bitsOfCurrentTimestampCounter
+ "\n";
460 returnData
+= "bitsOfEvent : " + bitsOfEvent
+ "\n";
461 returnData
+= "currentTimestampCounterMask : " + currentTimestampCounterMask
+ "\n";
462 returnData
+= "currentTimestampCounterMaskNextBit : " + currentTimestampCounterMaskNextBit
+ "\n";
463 returnData
+= "eventsLost : " + eventsLost
+ "\n";
464 returnData
+= "subBufferCorrupt : " + subBufferCorrupt
+ "\n";
465 returnData
+= "currentEvent : " + currentEvent
.getReferenceToString() + "\n"; // Hack to avoid ending up with event.toString()
466 returnData
+= "bufferPtr : " + bufferPtr
+ "\n";
467 returnData
+= "bufferSize : " + bufferSize
+ "\n";
468 returnData
+= "tracefileMarkersMap : " + tracefileMarkersMap
.keySet() + "\n"; // Hack to avoid ending up with tracefileMarkersMap.toString()
474 public abstract JniEvent
allocateNewJniEvent(Jni_C_Pointer newEventPtr
, HashMap
<Integer
, JniMarker
> newMarkersMap
, JniTracefile newParentTracefile
) throws JniException
;
475 public abstract JniMarker
allocateNewJniMarker(Jni_C_Pointer newMarkerPtr
) throws JniException
;