2010-11-10 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[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.common.Jni_C_Pointer_And_Library_Id;
20 import org.eclipse.linuxtools.lttng.jni.exception.JniException;
21 import org.eclipse.linuxtools.lttng.jni.exception.JniNoSuchEventException;
22 import org.eclipse.linuxtools.lttng.jni.exception.JniTracefileException;
23 import org.eclipse.linuxtools.lttng.jni.exception.JniTracefileWithoutEventException;
24
25 /**
26 * <b><u>JniTracefile</u></b>
27 * <p>
28 * A tracefile own an event of a certain type.<br>
29 * Provides access to the LttTracefile C structure in java.
30 * <p>
31 * Most important fields in the JniTracefile are :
32 * <ul>
33 * <li> a JniTracefile path (a tracefile <b>file</b> within a JniTrace directory)
34 * <li> a name (basically the name without the directory part)
35 * <li> a reference to a single event object
36 * <li> a HashMap of marker associated with this tracefile
37 * </ul>
38 * <b>NOTE</b><p>
39 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<br>
40 * Please look at the abstract functions to override at the bottom of this file.<p>
41 *
42 */
43 public abstract class JniTracefile extends Jni_C_Common
44 {
45 // Internal C pointer of the JniTracefile used in LTT
46 private Jni_C_Pointer_And_Library_Id thisTracefilePtr = new Jni_C_Pointer_And_Library_Id();
47
48 // Reference to the parent trace
49 private JniTrace parentTrace = null;
50
51 // Data we should populate from LTT
52 // Note that all type have been scaled up as there is no "unsigned" in java
53 // This might be a problem about "unsigned long" as there is no equivalent in java
54 private boolean isCpuOnline = false;
55 private String tracefilePath = ""; //$NON-NLS-1$
56 private String tracefileName = ""; //$NON-NLS-1$
57 private long cpuNumber = 0;
58 private long tid = 0;
59 private long pgid = 0;
60 private long creation = 0;
61
62 // Internal C pointer for trace and marker
63 // Note : These are real Jni_C_Pointer, not Jni_C_Pointer_And_Library_Id
64 private Jni_C_Pointer tracePtr = null;
65 private Jni_C_Pointer markerDataPtr = null;
66
67 private int CFileDescriptor = 0;
68 private long fileSize = 0;
69 private long blocksNumber = 0;
70 private boolean isBytesOrderReversed = false;
71 private boolean isFloatWordOrdered = false;
72 private long alignement = 0;
73 private long bufferHeaderSize = 0;
74 private int bitsOfCurrentTimestampCounter = 0;
75 private int bitsOfEvent = 0;
76 private long currentTimestampCounterMask = 0;
77 private long currentTimestampCounterMaskNextBit = 0;
78 private long eventsLost = 0;
79 private long subBufferCorrupt = 0;
80 private JniEvent currentEvent = null;
81
82 // Internal C pointer for trace and marker
83 // Note : This one is a real Jni_C_Pointer, not Jni_C_Pointer_And_Library_Id
84 private Jni_C_Pointer bufferPtr = null;
85
86 private long bufferSize = 0;
87
88 // This map will hold markers_info owned by this tracefile
89 private HashMap<Integer, JniMarker> tracefileMarkersMap = null;
90
91 // Native access functions
92 protected native boolean ltt_getIsCpuOnline(int libId, long tracefilePtr);
93 protected native String ltt_getTracefilepath(int libId, long tracefilePtr);
94 protected native String ltt_getTracefilename(int libId, long tracefilePtr);
95 protected native long ltt_getCpuNumber(int libId, long tracefilePtr);
96 protected native long ltt_getTid(int libId, long tracefilePtr);
97 protected native long ltt_getPgid(int libId, long tracefilePtr);
98 protected native long ltt_getCreation(int libId, long tracefilePtr);
99 protected native long ltt_getTracePtr(int libId, long tracefilePtr);
100 protected native long ltt_getMarkerDataPtr(int libId, long tracefilePtr);
101 protected native int ltt_getCFileDescriptor(int libId, long tracefilePtr);
102 protected native long ltt_getFileSize(int libId, long tracefilePtr);
103 protected native long ltt_getBlockNumber(int libId, long tracefilePtr);
104 protected native boolean ltt_getIsBytesOrderReversed(int libId, long tracefilePtr);
105 protected native boolean ltt_getIsFloatWordOrdered(int libId, long tracefilePtr);
106 protected native long ltt_getAlignement(int libId, long tracefilePtr);
107 protected native long ltt_getBufferHeaderSize(int libId, long tracefilePtr);
108 protected native int ltt_getBitsOfCurrentTimestampCounter(int libId, long tracefilePtr);
109 protected native int ltt_getBitsOfEvent(int libId, long tracefilePtr);
110 protected native long ltt_getCurrentTimestampCounterMask(int libId, long tracefilePtr);
111 protected native long ltt_getCurrentTimestampCounterMaskNextBit(int libId, long tracefilePtr);
112 protected native long ltt_getEventsLost(int libId, long tracefilePtr);
113 protected native long ltt_getSubBufferCorrupt(int libId, long tracefilePtr);
114 protected native long ltt_getEventPtr(int libId, long tracefilePtr);
115 protected native long ltt_getBufferPtr(int libId, long tracefilePtr);
116 protected native long ltt_getBufferSize(int libId, long tracefilePtr);
117
118 // Method to fill a map with marker object
119 protected native void ltt_feedAllMarkers(int libId, long tracefilePtr);
120
121 // Debug native function, ask LTT to print tracefile structure
122 protected native void ltt_printTracefile(int libId, long tracefilePtr);
123
124 /*
125 * Default constructor is forbidden
126 */
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 * @param newParentTrace The JniTrace parent of this tracefile.
171 *
172 * @exception JniException
173 *
174 * @see org.eclipse.linuxtools.lttng.jni.JniTrace
175 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
176 */
177 public JniTracefile(Jni_C_Pointer_And_Library_Id newPtr, JniTrace newParentTrace) throws JniException {
178 thisTracefilePtr = newPtr;
179 parentTrace = newParentTrace;
180 tracefileMarkersMap = new HashMap<Integer, JniMarker>();
181
182 // Retrieve the trace file information and load the first event.
183 try {
184 populateTracefileInformation();
185 }
186 catch (JniNoSuchEventException e) {
187 throw new JniTracefileWithoutEventException("JniEvent constructor reported that no event of this type are usable. (Jni_Tracefile)"); //$NON-NLS-1$
188 }
189 }
190
191 /**
192 * Read the next event of this tracefile.<p>
193 *
194 * Note : If the read succeed, the event will be populated.<p>
195 *
196 * @return LTT read status, as defined in Jni_C_Constant
197 *
198 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Constant
199 */
200 public int readNextEvent() {
201 return currentEvent.readNextEvent();
202 }
203
204 /**
205 * Seek to the given time.<p>
206 *
207 * Note : If the seek succeed, the event will be populated.
208 *
209 * @param seekTime The timestamp where to seek.
210 *
211 * @return LTT read status, as defined in Jni_C_Constant
212 *
213 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Constant
214 */
215 public int seekToTime(JniTime seekTime) {
216 return currentEvent.seekToTime(seekTime);
217 }
218
219 /*
220 * This function populates the tracefile data with data from LTT
221 *
222 * @throws JniException
223 */
224 private void populateTracefileInformation() throws JniException {
225 if (thisTracefilePtr.getPointer() == NULL) {
226 throw new JniTracefileException("Pointer is NULL, trace closed? (populateTracefileInformation)"); //$NON-NLS-1$
227 }
228
229 isCpuOnline = ltt_getIsCpuOnline(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer());
230 tracefilePath = ltt_getTracefilepath(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer());
231 tracefileName = ltt_getTracefilename(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer());
232 cpuNumber = ltt_getCpuNumber(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer());
233 tid = ltt_getTid(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
234 pgid = ltt_getPgid(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
235 creation = ltt_getCreation(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
236 tracePtr = new Jni_C_Pointer(ltt_getTracePtr(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer()) );
237 markerDataPtr = new Jni_C_Pointer(ltt_getMarkerDataPtr(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer()) );
238 CFileDescriptor = ltt_getCFileDescriptor(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
239 fileSize = ltt_getFileSize(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
240 blocksNumber = ltt_getBlockNumber(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
241 isBytesOrderReversed = ltt_getIsBytesOrderReversed(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
242 isFloatWordOrdered = ltt_getIsFloatWordOrdered(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
243 alignement = ltt_getAlignement(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
244 bufferHeaderSize = ltt_getBufferHeaderSize(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
245 bitsOfCurrentTimestampCounter = ltt_getBitsOfCurrentTimestampCounter(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
246 bitsOfEvent = ltt_getBitsOfEvent(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
247 currentTimestampCounterMask = ltt_getCurrentTimestampCounterMask(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
248 currentTimestampCounterMaskNextBit = ltt_getCurrentTimestampCounterMaskNextBit(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
249 eventsLost = ltt_getEventsLost(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
250 subBufferCorrupt = ltt_getSubBufferCorrupt(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
251 bufferPtr = new Jni_C_Pointer(ltt_getBufferPtr(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer()) );
252 bufferSize = ltt_getBufferSize(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer());
253
254 // To fill the map is a bit different
255 ltt_feedAllMarkers(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
256
257 Jni_C_Pointer_And_Library_Id tmpEventPointer = new Jni_C_Pointer_And_Library_Id(thisTracefilePtr.getLibraryId(), ltt_getEventPtr(thisTracefilePtr.getLibraryId(), 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 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
274 try {
275 JniMarker newMarker = allocateNewJniMarker( new Jni_C_Pointer_And_Library_Id(thisTracefilePtr.getLibraryId(), markerInfoPtr) );
276
277 tracefileMarkersMap.put(markerId, newMarker);
278 } catch (Exception e) {
279 printlnC(thisTracefilePtr.getLibraryId(), "Failed to add marker to tracefileMarkersMap!(addMarkersFromC)\n\tException raised : " + e.toString()); //$NON-NLS-1$
280 }
281 }
282
283 // Access to class variable. Most of them doesn't have setter
284 public boolean getIsCpuOnline() {
285 return isCpuOnline;
286 }
287
288 public String getTracefilePath() {
289 return tracefilePath;
290 }
291
292 public String getTracefileName() {
293 return tracefileName;
294 }
295
296 public long getCpuNumber() {
297 return cpuNumber;
298 }
299
300 public long getTid() {
301 return tid;
302 }
303
304 public long getPgid() {
305 return pgid;
306 }
307
308 public long getCreation() {
309 return creation;
310 }
311
312 public Jni_C_Pointer getTracePtr() {
313 return tracePtr;
314 }
315
316 public Jni_C_Pointer getMarkerDataPtr() {
317 return markerDataPtr;
318 }
319
320 public int getCFileDescriptor() {
321 return CFileDescriptor;
322 }
323
324 public long getFileSize() {
325 return fileSize;
326 }
327
328 public long getBlocksNumber() {
329 return blocksNumber;
330 }
331
332 public boolean getIsBytesOrderReversed() {
333 return isBytesOrderReversed;
334 }
335
336 public boolean getIsFloatWordOrdered() {
337 return isFloatWordOrdered;
338 }
339
340 public long getAlignement() {
341 return alignement;
342 }
343
344 public long getBufferHeaderSize() {
345 return bufferHeaderSize;
346 }
347
348 public int getBitsOfCurrentTimestampCounter() {
349 return bitsOfCurrentTimestampCounter;
350 }
351
352 public int getBitsOfEvent() {
353 return bitsOfEvent;
354 }
355
356 public long getCurrentTimestampCounterMask() {
357 return currentTimestampCounterMask;
358 }
359
360 public long getCurrentTimestampCounterMaskNextBit() {
361 return currentTimestampCounterMaskNextBit;
362 }
363
364 public long getEventsLost() {
365 return eventsLost;
366 }
367
368 public long getSubBufferCorrupt() {
369 return subBufferCorrupt;
370 }
371
372 public JniEvent getCurrentEvent() {
373 return currentEvent;
374 }
375
376 public Jni_C_Pointer getBufferPtr() {
377 return bufferPtr;
378 }
379
380 public long getBufferSize() {
381 return bufferSize;
382 }
383
384 public HashMap<Integer, JniMarker> getTracefileMarkersMap() {
385 return tracefileMarkersMap;
386 }
387
388 /**
389 * Parent trace of this tracefile.<p>
390 *
391 * @return The parent trace
392 *
393 * @see org.eclipse.linuxtools.lttng.jni.JniTrace
394 */
395 public JniTrace getParentTrace() {
396 return parentTrace;
397 }
398
399 /**
400 * Pointer to the LttTracefile C structure<p>
401 *
402 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
403 * know what you are doing.<p>
404 *
405 * @return The actual (long converted) pointer or NULL.
406 *
407 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
408 */
409 public Jni_C_Pointer_And_Library_Id getTracefilePtr() {
410 return thisTracefilePtr;
411 }
412
413 /**
414 * Print information for this tracefile.
415 * <u>Intended to debug</u><p>
416 *
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>
419 */
420 public void printTracefileInformation() {
421 ltt_printTracefile(thisTracefilePtr.getLibraryId(), thisTracefilePtr.getPointer() );
422 }
423
424 /**
425 * toString() method.
426 * <u>Intended to debug</u><p>
427 *
428 * @return Attributes of the object concatenated in String
429 */
430 @Override
431 @SuppressWarnings("nls")
432 public String toString() {
433 String returnData = "";
434
435 returnData += "isCpuOnline : " + isCpuOnline + "\n";
436 returnData += "tracefilePath : " + tracefilePath + "\n";
437 returnData += "tracefileName : " + tracefileName + "\n";
438 returnData += "cpuNumber : " + cpuNumber + "\n";
439 returnData += "tid : " + tid + "\n";
440 returnData += "pgid : " + pgid + "\n";
441 returnData += "creation : " + creation + "\n";
442 returnData += "tracePtr : " + tracePtr + "\n";
443 returnData += "markerDataPtr : " + markerDataPtr + "\n";
444 returnData += "CFileDescriptor : " + CFileDescriptor + "\n";
445 returnData += "fileSize : " + fileSize + "\n";
446 returnData += "blocksNumber : " + blocksNumber + "\n";
447 returnData += "isBytesOrderReversed : " + isBytesOrderReversed + "\n";
448 returnData += "isFloatWordOrdered : " + isFloatWordOrdered + "\n";
449 returnData += "alignement : " + alignement + "\n";
450 returnData += "bufferHeaderSize : " + bufferHeaderSize + "\n";
451 returnData += "bitsOfCurrentTimestampCounter : " + bitsOfCurrentTimestampCounter + "\n";
452 returnData += "bitsOfEvent : " + bitsOfEvent + "\n";
453 returnData += "currentTimestampCounterMask : " + currentTimestampCounterMask + "\n";
454 returnData += "currentTimestampCounterMaskNextBit : " + currentTimestampCounterMaskNextBit + "\n";
455 returnData += "eventsLost : " + eventsLost + "\n";
456 returnData += "subBufferCorrupt : " + subBufferCorrupt + "\n";
457 returnData += "currentEvent : " + currentEvent.getReferenceToString() + "\n"; // Hack to avoid ending up with event.toString()
458 returnData += "bufferPtr : " + bufferPtr + "\n";
459 returnData += "bufferSize : " + bufferSize + "\n";
460 returnData += "tracefileMarkersMap : " + tracefileMarkersMap.keySet() + "\n"; // Hack to avoid ending up with tracefileMarkersMap.toString()
461
462 return returnData;
463 }
464
465
466 // ****************************
467 // **** ABSTRACT FUNCTIONS ****
468 // You MUST override those in your version specific implementation
469
470
471 /**
472 * Function place holder to allocate a new JniEvent.<p>
473 * <br>
474 * JniEvent constructor is non overridable so we need another overridable function to return the correct version of JniEvent.<br>
475 * Effect of this function should be the same (allocate a fresh new JniEvent).<br>
476 * <br>
477 * <b>!! Override this with you version specific implementation.</b><br>
478 *
479 * @param newEventPtr The pointer of an already opened LttEvent C Structure
480 * @param newMarkersMap An already populated HashMap of JniMarker objects for this new event
481 * @param newParentTracefile The JniTrace parent of this tracefile.
482 *
483 * @return The newly allocated JniEvent of the correct version
484 *
485 * @throws JniException The construction (allocation) failed.
486 *
487 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
488 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
489 * @see org.eclipse.linuxtools.lttng.jni.JniMarker
490 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
491 */
492 public abstract JniEvent allocateNewJniEvent(Jni_C_Pointer_And_Library_Id newEventPtr, HashMap<Integer, JniMarker> newMarkersMap, JniTracefile newParentTracefile) throws JniException;
493
494
495 /**
496 * Function place holder to allocate a new JniMarker.<p>
497 * <br>
498 * JniMarker constructor is non overridable so we need another overridable function to return the correct version of JniMarker.<br>
499 * Effect of this function should be the same (allocate a fresh new JniMarker).<br>
500 * <br>
501 * <b>!! Override this with you version specific implementation.</b><br>
502 *
503 * @param newMarkerPtr The pointer of an already opened marker_info C Structure
504 *
505 * @return The newly allocated JniMarker of the correct version
506 *
507 * @throws JniException The construction (allocation) failed.
508 *
509 * @see org.eclipse.linuxtools.lttng.jni.JniMarker
510 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
511 */
512 public abstract JniMarker allocateNewJniMarker(Jni_C_Pointer_And_Library_Id newMarkerPtr) throws JniException;
513
514 }
This page took 0.236597 seconds and 5 git commands to generate.