d6f86daaa6f285da0cf7ddf0e031664258d7ed1c
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / JniTracefile.java
1 package org.eclipse.linuxtools.lttng.jni;
2 /*******************************************************************************
3 * Copyright (c) 2009 Ericsson
4 *
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
9 *
10 * Contributors:
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
13
14
15 import java.util.HashMap;
16
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;
23
24 /**
25 * <b><u>JniTracefile</u></b>
26 * <p>
27 * A tracefile own an event of a certain type.<br>
28 * Provides access to the LttTracefile C structure in java.
29 * <p>
30 * Most important fields in the JniTracefile are :
31 * <ul>
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
36 * </ul>
37 */
38 public abstract class JniTracefile extends Jni_C_Common {
39
40 // Internal C pointer of the JniTracefile used in LTT
41 private Jni_C_Pointer thisTracefilePtr = new Jni_C_Pointer();
42
43 // Reference to the parent trace
44 private JniTrace parentTrace = null;
45
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;
53 private long tid = 0;
54 private long pgid = 0;
55 private long creation = 0;
56
57 // Internal C pointer for trace and marker
58 private Jni_C_Pointer tracePtr = null;
59 private Jni_C_Pointer markerDataPtr = null;
60
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;
75
76 // Internal C pointer for trace and marker
77 private Jni_C_Pointer bufferPtr = null;
78
79 private long bufferSize = 0;
80
81 // This map will hold markers_info owned by this tracefile
82 private HashMap<Integer, JniMarker> tracefileMarkersMap = null;
83
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);
110
111 // Method to fill a map with marker object
112 protected native void ltt_feedAllMarkers(long tracefilePtr);
113
114 // Debug native function, ask LTT to print tracefile structure
115 protected native void ltt_printTracefile(long tracefilePtr);
116
117 // *** FIXME ***
118 // To uncomment as soon as the library will be able to load multiple version at once
119 // static {
120 // System.loadLibrary("lttvtraceread_loader");
121 //}
122
123 /*
124 * Default constructor is forbidden
125 */
126 @SuppressWarnings("unused")
127 protected JniTracefile() {
128 }
129
130 /**
131 * Copy constructor.<p>
132 *
133 * @param oldTracefile Reference to the JniTracefile you want to copy.
134 */
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;
164 }
165
166 /**
167 * Constructor, using C pointer.<p>
168 *
169 * @param newPtr The pointer of an already opened LttTracefile C Structure
170 *
171 * @exception JniException
172 *
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
175 */
176 public JniTracefile(Jni_C_Pointer newPtr, JniTrace newParentTrace) throws JniException {
177 thisTracefilePtr = newPtr;
178 parentTrace = newParentTrace;
179 tracefileMarkersMap = new HashMap<Integer, JniMarker>();
180
181 // Retrieve the trace file information and load the first event.
182 try {
183 populateTracefileInformation();
184 } catch (JniNoSuchEventException e) {
185 throw new JniTracefileWithoutEventException(
186 "JniEvent constructor reported that no event of this type are usable. (Jaf_Tracefile)");
187 }
188 }
189
190 /**
191 * Read the next event of this tracefile.<p>
192 *
193 * Note : If the read succeed, the event will be populated.<p>
194 *
195 * @return LTT read status, as defined in Jni_C_Common
196 *
197 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
198 */
199 public int readNextEvent() {
200 return currentEvent.readNextEvent();
201 }
202
203 /**
204 * Seek to the given time.<p>
205 *
206 * Note : If the seek succeed, the event will be populated.
207 *
208 * @param seekTime The timestamp where to seek.
209 *
210 * @return LTT read status, as defined in Jni_C_Common
211 *
212 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Common
213 */
214 public int seekToTime(JniTime seekTime) {
215 return currentEvent.seekToTime(seekTime);
216 }
217
218 /*
219 * This function populates the tracefile data with data from LTT
220 *
221 * @throws JniException
222 */
223 private void populateTracefileInformation() throws JniException {
224 if (thisTracefilePtr.getPointer() == NULL) {
225 throw new JniTracefileException(
226 "Pointer is NULL, trace closed? (populateTracefileInformation)");
227 }
228
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() );
253
254 // To fill the map is a bit different
255 ltt_feedAllMarkers( thisTracefilePtr.getPointer() );
256
257 Jni_C_Pointer tmpEventPointer = new Jni_C_Pointer(ltt_getEventPtr(thisTracefilePtr.getPointer()));
258 currentEvent = allocateNewJniEvent(tmpEventPointer , tracefileMarkersMap, this);
259 }
260
261 /*
262 * Fills a map of all the markers associated with this tracefile.
263 *
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.
267 *
268 * @param markerId Id of the marker (int)
269 * @param markerInfoPtr C Pointer to a marker_info C structure
270 */
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
275 try {
276 JniMarker newMarker = allocateNewJniMarker( new Jni_C_Pointer(markerInfoPtr) );
277
278 tracefileMarkersMap.put(markerId, newMarker);
279 } catch (Exception e) {
280 printlnC("Failed to add marker to tracefileMarkersMap!(addMarkersFromC)\n\tException raised : " + e.toString());
281 }
282 }
283
284 // Access to class variable. Most of them doesn't have setter
285 public boolean getIsCpuOnline() {
286 return isCpuOnline;
287 }
288
289 public String getTracefilePath() {
290 return tracefilePath;
291 }
292
293 public String getTracefileName() {
294 return tracefileName;
295 }
296
297 public long getCpuNumber() {
298 return cpuNumber;
299 }
300
301 public long getTid() {
302 return tid;
303 }
304
305 public long getPgid() {
306 return pgid;
307 }
308
309 public long getCreation() {
310 return creation;
311 }
312
313 public Jni_C_Pointer getTracePtr() {
314 return tracePtr;
315 }
316
317 public Jni_C_Pointer getMarkerDataPtr() {
318 return markerDataPtr;
319 }
320
321 public int getCFileDescriptor() {
322 return CFileDescriptor;
323 }
324
325 public long getFileSize() {
326 return fileSize;
327 }
328
329 public long getBlocksNumber() {
330 return blocksNumber;
331 }
332
333 public boolean getIsBytesOrderReversed() {
334 return isBytesOrderReversed;
335 }
336
337 public boolean getIsFloatWordOrdered() {
338 return isFloatWordOrdered;
339 }
340
341 public long getAlignement() {
342 return alignement;
343 }
344
345 public long getBufferHeaderSize() {
346 return bufferHeaderSize;
347 }
348
349 public int getBitsOfCurrentTimestampCounter() {
350 return bitsOfCurrentTimestampCounter;
351 }
352
353 public int getBitsOfEvent() {
354 return bitsOfEvent;
355 }
356
357 public long getCurrentTimestampCounterMask() {
358 return currentTimestampCounterMask;
359 }
360
361 public long getCurrentTimestampCounterMaskNextBit() {
362 return currentTimestampCounterMaskNextBit;
363 }
364
365 public long getEventsLost() {
366 return eventsLost;
367 }
368
369 public long getSubBufferCorrupt() {
370 return subBufferCorrupt;
371 }
372
373 public JniEvent getCurrentEvent() {
374 return currentEvent;
375 }
376
377 public Jni_C_Pointer getBufferPtr() {
378 return bufferPtr;
379 }
380
381 public long getBufferSize() {
382 return bufferSize;
383 }
384
385 public HashMap<Integer, JniMarker> getTracefileMarkersMap() {
386 return tracefileMarkersMap;
387 }
388
389 /**
390 * Parent trace of this tracefile.<p>
391 *
392 * @return The parent trace
393 *
394 * @see org.eclipse.linuxtools.lttng.jni.eclipse.linuxtools.lttng.jni.JniTrace
395 */
396 public JniTrace getParentTrace() {
397 return parentTrace;
398 }
399
400 /**
401 * Pointer to the LttTracefile C structure<p>
402 *
403 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
404 * know what you are doing.<p>
405 *
406 * @return The actual (long converted) pointer or NULL.
407 *
408 * @see org.eclipse.linuxtools.lttng.jni.common.eclipse.linuxtools.lttng.jni.Jni_C_Pointer
409 */
410 public Jni_C_Pointer getTracefilePtr() {
411 return thisTracefilePtr;
412 }
413
414 /**
415 * Print information for this tracefile.
416 * <u>Intended to debug</u><p>
417 *
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>
420 *
421 * This function will not throw but will complain loudly if pointer is NULL.
422 */
423 public void printTracefileInformation() {
424
425 // If null pointer, print a warning!
426 if (thisTracefilePtr.getPointer() == NULL) {
427 printlnC("Pointer is NULL, cannot print. (printTracefileInformation)");
428 }
429 else {
430 ltt_printTracefile( thisTracefilePtr.getPointer() );
431 }
432 }
433
434 /**
435 * toString() method.
436 * <u>Intended to debug</u><p>
437 *
438 * @return Attributes of the object concatenated in String
439 */
440 @Override
441 public String toString() {
442 String returnData = "";
443
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()
470
471 return returnData;
472 }
473
474
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;
477
478 }
479
This page took 0.043857 seconds and 5 git commands to generate.