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 @SuppressWarnings("unused")
127 protected JniTracefile() {
131 * Copy constructor.<p>
133 * @param oldTracefile Reference to the JniTracefile you want to copy.
135 public JniTracefile(JniTracefile oldTracefile
) {
136 thisTracefilePtr
= oldTracefile
.thisTracefilePtr
;
137 parentTrace
= oldTracefile
.parentTrace
;
138 tracefileMarkersMap
= oldTracefile
.tracefileMarkersMap
;
139 isCpuOnline
= oldTracefile
.isCpuOnline
;
140 tracefilePath
= oldTracefile
.tracefilePath
;
141 tracefileName
= oldTracefile
.tracefileName
;
142 cpuNumber
= oldTracefile
.cpuNumber
;
143 tid
= oldTracefile
.tid
;
144 pgid
= oldTracefile
.pgid
;
145 creation
= oldTracefile
.creation
;
146 tracePtr
= oldTracefile
.tracePtr
;
147 markerDataPtr
= oldTracefile
.markerDataPtr
;
148 CFileDescriptor
= oldTracefile
.CFileDescriptor
;
149 fileSize
= oldTracefile
.fileSize
;
150 blocksNumber
= oldTracefile
.blocksNumber
;
151 isBytesOrderReversed
= oldTracefile
.isBytesOrderReversed
;
152 isFloatWordOrdered
= oldTracefile
.isFloatWordOrdered
;
153 alignement
= oldTracefile
.alignement
;
154 bufferHeaderSize
= oldTracefile
.bufferHeaderSize
;
155 bitsOfCurrentTimestampCounter
= oldTracefile
.bitsOfCurrentTimestampCounter
;
156 bitsOfEvent
= oldTracefile
.bitsOfEvent
;
157 currentTimestampCounterMask
= oldTracefile
.currentTimestampCounterMask
;
158 currentTimestampCounterMaskNextBit
= oldTracefile
.currentTimestampCounterMaskNextBit
;
159 eventsLost
= oldTracefile
.eventsLost
;
160 subBufferCorrupt
= oldTracefile
.subBufferCorrupt
;
161 currentEvent
= oldTracefile
.currentEvent
;
162 bufferPtr
= oldTracefile
.bufferPtr
;
163 bufferSize
= oldTracefile
.bufferSize
;
167 * Constructor, using C pointer.<p>
169 * @param newPtr The pointer of an already opened LttTracefile C Structure
171 * @exception JniException
173 * @see org.eclipse.linuxtools.lttng.jni.eclipse.linuxtools.lttng.jni.JniTrace
174 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Pointer
176 public JniTracefile(Jni_C_Pointer newPtr
, JniTrace newParentTrace
) throws JniException
{
177 thisTracefilePtr
= newPtr
;
178 parentTrace
= newParentTrace
;
179 tracefileMarkersMap
= new HashMap
<Integer
, JniMarker
>();
181 // Retrieve the trace file information and load the first event.
183 populateTracefileInformation();
184 } catch (JniNoSuchEventException e
) {
185 throw new JniTracefileWithoutEventException(
186 "JniEvent constructor reported that no event of this type are usable. (Jaf_Tracefile)");
191 * Read the next event of this tracefile.<p>
193 * Note : If the read succeed, the event will be populated.<p>
195 * @return LTT read status, as defined in Jni_C_Common
197 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
199 public int readNextEvent() {
200 return currentEvent
.readNextEvent();
204 * Seek to the given time.<p>
206 * Note : If the seek succeed, the event will be populated.
208 * @param seekTime The timestamp where to seek.
210 * @return LTT read status, as defined in Jni_C_Common
212 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
214 public int seekToTime(JniTime seekTime
) {
215 return currentEvent
.seekToTime(seekTime
);
219 * This function populates the tracefile data with data from LTT
221 * @throws JniException
223 private void populateTracefileInformation() throws JniException
{
224 if (thisTracefilePtr
.getPointer() == NULL
) {
225 throw new JniTracefileException(
226 "Pointer is NULL, trace closed? (populateTracefileInformation)");
229 isCpuOnline
= ltt_getIsCpuOnline( thisTracefilePtr
.getPointer() );
230 tracefilePath
= ltt_getTracefilepath( thisTracefilePtr
.getPointer() );
231 tracefileName
= ltt_getTracefilename( thisTracefilePtr
.getPointer() );
232 cpuNumber
= ltt_getCpuNumber( thisTracefilePtr
.getPointer() );
233 tid
= ltt_getTid( thisTracefilePtr
.getPointer() );
234 pgid
= ltt_getPgid( thisTracefilePtr
.getPointer() );
235 creation
= ltt_getCreation( thisTracefilePtr
.getPointer() );
236 tracePtr
= new Jni_C_Pointer(ltt_getTracePtr( thisTracefilePtr
.getPointer()) );
237 markerDataPtr
= new Jni_C_Pointer(ltt_getMarkerDataPtr( thisTracefilePtr
.getPointer()) );
238 CFileDescriptor
= ltt_getCFileDescriptor( thisTracefilePtr
.getPointer() );
239 fileSize
= ltt_getFileSize( thisTracefilePtr
.getPointer() );
240 blocksNumber
= ltt_getBlockNumber( thisTracefilePtr
.getPointer() );
241 isBytesOrderReversed
= ltt_getIsBytesOrderReversed( thisTracefilePtr
.getPointer() );
242 isFloatWordOrdered
= ltt_getIsFloatWordOrdered( thisTracefilePtr
.getPointer() );
243 alignement
= ltt_getAlignement( thisTracefilePtr
.getPointer() );
244 bufferHeaderSize
= ltt_getBufferHeaderSize( thisTracefilePtr
.getPointer() );
245 bitsOfCurrentTimestampCounter
= ltt_getBitsOfCurrentTimestampCounter( thisTracefilePtr
.getPointer() );
246 bitsOfEvent
= ltt_getBitsOfEvent( thisTracefilePtr
.getPointer() );
247 currentTimestampCounterMask
= ltt_getCurrentTimestampCounterMask( thisTracefilePtr
.getPointer() );
248 currentTimestampCounterMaskNextBit
= ltt_getCurrentTimestampCounterMaskNextBit( thisTracefilePtr
.getPointer() );
249 eventsLost
= ltt_getEventsLost( thisTracefilePtr
.getPointer() );
250 subBufferCorrupt
= ltt_getSubBufferCorrupt( thisTracefilePtr
.getPointer() );
251 bufferPtr
= new Jni_C_Pointer(ltt_getBufferPtr( thisTracefilePtr
.getPointer()) );
252 bufferSize
= ltt_getBufferSize( thisTracefilePtr
.getPointer() );
254 // To fill the map is a bit different
255 ltt_feedAllMarkers( thisTracefilePtr
.getPointer() );
257 Jni_C_Pointer tmpEventPointer
= new Jni_C_Pointer(ltt_getEventPtr(thisTracefilePtr
.getPointer()));
258 currentEvent
= allocateNewJniEvent(tmpEventPointer
, tracefileMarkersMap
, this);
262 * Fills a map of all the markers associated with this tracefile.
264 * Note: This function is called from C and there is no way to propagate
265 * exception back to the caller without crashing JNI. Therefore, it MUST
266 * catch all exceptions.
268 * @param markerId Id of the marker (int)
269 * @param markerInfoPtr C Pointer to a marker_info C structure
271 @SuppressWarnings("unused")
272 private void addMarkersFromC(int markerId
, long markerInfoPtr
) {
273 // Create a new tracefile object and insert it in the map
274 // the tracefile fill itself with LTT data while being constructed
276 JniMarker newMarker
= allocateNewJniMarker( new Jni_C_Pointer(markerInfoPtr
) );
278 tracefileMarkersMap
.put(markerId
, newMarker
);
279 } catch (Exception e
) {
280 printlnC("Failed to add marker to tracefileMarkersMap!(addMarkersFromC)\n\tException raised : " + e
.toString());
284 // Access to class variable. Most of them doesn't have setter
285 public boolean getIsCpuOnline() {
289 public String
getTracefilePath() {
290 return tracefilePath
;
293 public String
getTracefileName() {
294 return tracefileName
;
297 public long getCpuNumber() {
301 public long getTid() {
305 public long getPgid() {
309 public long getCreation() {
313 public Jni_C_Pointer
getTracePtr() {
317 public Jni_C_Pointer
getMarkerDataPtr() {
318 return markerDataPtr
;
321 public int getCFileDescriptor() {
322 return CFileDescriptor
;
325 public long getFileSize() {
329 public long getBlocksNumber() {
333 public boolean getIsBytesOrderReversed() {
334 return isBytesOrderReversed
;
337 public boolean getIsFloatWordOrdered() {
338 return isFloatWordOrdered
;
341 public long getAlignement() {
345 public long getBufferHeaderSize() {
346 return bufferHeaderSize
;
349 public int getBitsOfCurrentTimestampCounter() {
350 return bitsOfCurrentTimestampCounter
;
353 public int getBitsOfEvent() {
357 public long getCurrentTimestampCounterMask() {
358 return currentTimestampCounterMask
;
361 public long getCurrentTimestampCounterMaskNextBit() {
362 return currentTimestampCounterMaskNextBit
;
365 public long getEventsLost() {
369 public long getSubBufferCorrupt() {
370 return subBufferCorrupt
;
373 public JniEvent
getCurrentEvent() {
377 public Jni_C_Pointer
getBufferPtr() {
381 public long getBufferSize() {
385 public HashMap
<Integer
, JniMarker
> getTracefileMarkersMap() {
386 return tracefileMarkersMap
;
390 * Parent trace of this tracefile.<p>
392 * @return The parent trace
394 * @see org.eclipse.linuxtools.lttng.jni.eclipse.linuxtools.lttng.jni.JniTrace
396 public JniTrace
getParentTrace() {
401 * Pointer to the LttTracefile C structure<p>
403 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
404 * know what you are doing.<p>
406 * @return The actual (long converted) pointer or NULL.
408 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Pointer
410 public Jni_C_Pointer
getTracefilePtr() {
411 return thisTracefilePtr
;
415 * Print information for this tracefile.
416 * <u>Intended to debug</u><p>
418 * This function will call Ltt to print, so information printed will be the
419 * one from the C structure, not the one populated in java.<p>
421 * This function will not throw but will complain loudly if pointer is NULL.
423 public void printTracefileInformation() {
425 // If null pointer, print a warning!
426 if (thisTracefilePtr
.getPointer() == NULL
) {
427 printlnC("Pointer is NULL, cannot print. (printTracefileInformation)");
430 ltt_printTracefile( thisTracefilePtr
.getPointer() );
436 * <u>Intended to debug</u><p>
438 * @return Attributes of the object concatenated in String
441 public String
toString() {
442 String returnData
= "";
444 returnData
+= "isCpuOnline : " + isCpuOnline
+ "\n";
445 returnData
+= "tracefilePath : " + tracefilePath
+ "\n";
446 returnData
+= "tracefileName : " + tracefileName
+ "\n";
447 returnData
+= "cpuNumber : " + cpuNumber
+ "\n";
448 returnData
+= "tid : " + tid
+ "\n";
449 returnData
+= "pgid : " + pgid
+ "\n";
450 returnData
+= "creation : " + creation
+ "\n";
451 returnData
+= "tracePtr : " + tracePtr
+ "\n";
452 returnData
+= "markerDataPtr : " + markerDataPtr
+ "\n";
453 returnData
+= "CFileDescriptor : " + CFileDescriptor
+ "\n";
454 returnData
+= "fileSize : " + fileSize
+ "\n";
455 returnData
+= "blocksNumber : " + blocksNumber
+ "\n";
456 returnData
+= "isBytesOrderReversed : " + isBytesOrderReversed
+ "\n";
457 returnData
+= "isFloatWordOrdered : " + isFloatWordOrdered
+ "\n";
458 returnData
+= "alignement : " + alignement
+ "\n";
459 returnData
+= "bufferHeaderSize : " + bufferHeaderSize
+ "\n";
460 returnData
+= "bitsOfCurrentTimestampCounter : " + bitsOfCurrentTimestampCounter
+ "\n";
461 returnData
+= "bitsOfEvent : " + bitsOfEvent
+ "\n";
462 returnData
+= "currentTimestampCounterMask : " + currentTimestampCounterMask
+ "\n";
463 returnData
+= "currentTimestampCounterMaskNextBit : " + currentTimestampCounterMaskNextBit
+ "\n";
464 returnData
+= "eventsLost : " + eventsLost
+ "\n";
465 returnData
+= "subBufferCorrupt : " + subBufferCorrupt
+ "\n";
466 returnData
+= "currentEvent : " + currentEvent
.getReferenceToString() + "\n"; // Hack to avoid ending up with event.toString()
467 returnData
+= "bufferPtr : " + bufferPtr
+ "\n";
468 returnData
+= "bufferSize : " + bufferSize
+ "\n";
469 returnData
+= "tracefileMarkersMap : " + tracefileMarkersMap
.keySet() + "\n"; // Hack to avoid ending up with tracefileMarkersMap.toString()
475 public abstract JniEvent
allocateNewJniEvent(Jni_C_Pointer newEventPtr
, HashMap
<Integer
, JniMarker
> newMarkersMap
, JniTracefile newParentTracefile
) throws JniException
;
476 public abstract JniMarker
allocateNewJniMarker(Jni_C_Pointer newMarkerPtr
) throws JniException
;