2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / JniTrace.java
CommitLineData
0152140d
ASL
1package 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
14import java.util.HashMap;
15import java.util.Iterator;
16import java.util.PriorityQueue;
17
18import org.eclipse.linuxtools.lttng.jni.common.JniTime;
c85e8cb2 19import org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id;
0152140d
ASL
20import org.eclipse.linuxtools.lttng.jni.exception.JniException;
21import org.eclipse.linuxtools.lttng.jni.exception.JniOpenTraceFailedException;
22import org.eclipse.linuxtools.lttng.jni.exception.JniTraceException;
23import org.eclipse.linuxtools.lttng.jni.exception.JniTracefileWithoutEventException;
24
25/**
26 * <b><u>JniTrace</u></b>
27 * <p>
28 * This is the top level class in the JNI. It provides access to the
29 * LttTrace C structure in java.
30 * <p>
31 * Most important fields in the JniTrace are :
32 * <ul>
33 * <li>a JniTrace path (a trace <b>directory</b>)
34 * <li>a HashMap of tracefiles that exists in this trace
35 * </ul>
3b7509b0
WB
36 * <p>
37 * <b>NOTE</b><p>
38 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<br>
39 * Please look at the abstract functions to override at the bottom of this file.<p>
0152140d
ASL
40 */
41public abstract class JniTrace extends Jni_C_Common {
42
43 private final static boolean DEFAULT_LTT_DEBUG = false;
44
45 // Internal C pointer of the JniTrace used in LTT
c85e8cb2 46 private Jni_C_Pointer_And_Library_Id thisTracePtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
47
48 // Data we should populate from LTT
49 // Note that all type have been scaled up as there is no "unsigned" in java
50 // This might be a problem about "unsigned long" as there is no equivalent
51 // in java
c357e4b6 52
0152140d
ASL
53 private String tracepath = ""; // Path of the trace. Should be a directory (like : /tmp/traceX)
54 private int cpuNumber = 0;
55 private long archType = 0;
56 private long archVariant = 0;
57 private short archSize = 0;
58 private short lttMajorVersion = 0;
59 private short lttMinorVersion = 0;
60 private short flightRecorder = 0;
61 private long freqScale = 0;
62 private long startFreq = 0;
63 private long startTimestampCurrentCounter = 0;
64 private long startMonotonic = 0;
65 private JniTime startTimeNoAdjustement = null;
66 private JniTime startTime = null;
67 private JniTime endTime = null;
68
69 // This Map holds a reference to the tracefiles owned by this trace
70 private HashMap<String, JniTracefile> tracefilesMap = null;
71 // The priority queue (similar to heap) hold events
72 private PriorityQueue<JniEvent> eventsHeap = null;
73
74 // This variable will hold the content of the "last" event we read
75 private JniEvent currentEvent = null;
76
77 // Should we print debug in the C library or not?
78 private boolean printLttDebug = DEFAULT_LTT_DEBUG;
79
c85e8cb2 80
0152140d 81 // This need to be called prior to any operation
c85e8cb2
WB
82 protected native int ltt_initializeHandle(String libname);
83
0152140d 84 // This need to be called at the very end (destructor)
c85e8cb2 85 protected native boolean ltt_freeHandle(int libId);
0152140d
ASL
86
87 // Open/close native functions
c85e8cb2
WB
88 protected native long ltt_openTrace(int libId, String pathname, boolean printDebug);
89 protected native void ltt_closeTrace(int libId, long tracePtr);
0152140d
ASL
90
91 // Native access functions
c85e8cb2
WB
92 protected native String ltt_getTracepath(int libId, long tracePtr);
93 protected native int ltt_getCpuNumber(int libId, long tracePtr);
94 protected native long ltt_getArchType(int libId, long tracePtr);
95 protected native long ltt_getArchVariant(int libId, long tracePtr);
96 protected native short ltt_getArchSize(int libId, long tracePtr);
97 protected native short ltt_getLttMajorVersion(int libId, long tracePtr);
98 protected native short ltt_getLttMinorVersion(int libId, long tracePtr);
99 protected native short ltt_getFlightRecorder(int libId, long tracePtr);
100 protected native long ltt_getFreqScale(int libId, long tracePtr);
101 protected native long ltt_getStartFreq(int libId, long tracePtr);
102 protected native long ltt_getStartTimestampCurrentCounter(int libId, long tracePtr);
103 protected native long ltt_getStartMonotonic(int libId, long tracePtr);
0152140d
ASL
104
105 // Native function to fill out startTime
c85e8cb2 106 protected native void ltt_feedStartTime(int libId, long tracePtr, JniTime startTime);
0152140d
ASL
107
108 // Native function to fill out startTimeFromTimestampCurrentCounter
c85e8cb2 109 protected native void ltt_feedStartTimeFromTimestampCurrentCounter(int libId, long tracePtr, JniTime startTime);
0152140d
ASL
110
111 // Native function to fill out tracefilesMap
c85e8cb2 112 protected native void ltt_feedAllTracefiles(int libId, long tracePtr);
0152140d
ASL
113
114 // Native function to fill out the start and end time of the trace
c85e8cb2 115 protected native void ltt_feedTracefileTimeRange(int libId, long tracePtr, JniTime startTime, JniTime endTime);
0152140d
ASL
116
117 // Debug native function, ask LTT to print trace structure
c85e8cb2 118 protected native void ltt_printTrace(int libId, long tracePtr);
0152140d
ASL
119
120 /*
121 * Default constructor is forbidden
122 */
123 protected JniTrace() {
124 }
125
126 /**
127 * Constructor that takes a tracepath parameter.<p>
128 *
129 * This constructor also opens the trace.
130 *
131 * @param newpath The <b>directory</b> of the trace to be opened
132 *
133 * @exception JniException
134 */
135 public JniTrace(String newpath) throws JniException {
136 this(newpath, DEFAULT_LTT_DEBUG);
137 }
138
139 /**
140 * Constructor that takes a tracepath parameter and a debug value.<p>
141 *
142 * This constructor also opens the trace.
143 *
144 * @param newpath The <b>directory</b> of the trace to be opened
145 * @param newPrintDebug Should the debug information be printed in the LTT C library
146 *
147 * @exception JniException
148 */
149 public JniTrace(String newpath, boolean newPrintDebug) throws JniException {
150 tracepath = newpath;
c85e8cb2 151 thisTracePtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
152 printLttDebug = newPrintDebug;
153
154 openTrace(newpath);
155 }
156
157 /**
158 * Copy constructor.
159 *
160 * @param oldTrace A reference to the JniTrace to copy.
161 */
162 public JniTrace(JniTrace oldTrace) {
163 thisTracePtr = oldTrace.thisTracePtr;
164
165 tracepath = oldTrace.tracepath;
166 cpuNumber = oldTrace.cpuNumber;
167 archType = oldTrace.archType;
168 archVariant = oldTrace.archVariant;
169 archSize = oldTrace.archSize;
170 lttMajorVersion = oldTrace.lttMajorVersion;
171 lttMinorVersion = oldTrace.lttMinorVersion;
172 flightRecorder = oldTrace.flightRecorder;
173 freqScale = oldTrace.freqScale;
174 startFreq = oldTrace.startFreq;
175 startTimestampCurrentCounter = oldTrace.startTimestampCurrentCounter;
176 startMonotonic = oldTrace.startMonotonic;
177 startTimeNoAdjustement = oldTrace.startTimeNoAdjustement;
178 startTime = oldTrace.startTime;
179 endTime = oldTrace.endTime;
180
181 tracefilesMap = new HashMap<String, JniTracefile>(oldTrace.tracefilesMap.size());
e0ea56dd 182 ltt_feedAllTracefiles(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
0152140d
ASL
183
184 eventsHeap = new PriorityQueue<JniEvent>( oldTrace.eventsHeap.size());
e0ea56dd 185 populateEventHeap();
0152140d
ASL
186
187 printLttDebug = oldTrace.printLttDebug;
188 }
189
190 /**
191 * Constructor, using C pointer.<p>
192 *
193 * @param newPtr The pointer to an already opened LttTrace C structure.
194 * @param newPrintDebug Should the debug information be printed in the LTT C library
195 *
196 * @exception JniException
197 *
c85e8cb2 198 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
0152140d 199 */
c85e8cb2 200 public JniTrace(Jni_C_Pointer_And_Library_Id newPtr, boolean newPrintDebug) throws JniException {
0152140d
ASL
201 thisTracePtr = newPtr;
202 printLttDebug = newPrintDebug;
203
204 // Populate our trace
205 populateTraceInformation();
206 }
207
208
b9fb2d51
FC
209 @Override
210 public void finalize() {
0152140d
ASL
211 // If the trace is open, close it
212 if (thisTracePtr.getPointer() != NULL) {
213 closeTrace();
214 }
215
687cc7e7 216 // Tell the C to free the allocated memory
0152140d
ASL
217 freeLibrary();
218 }
219
220 /**
221 * Open an existing trace.<p>
222 *
223 * The tracepath is a directory and needs to exist, otherwise
34eff969 224 * a JniOpenTraceFailedException is throwed.
0152140d
ASL
225 *
226 * @param newPath The <b>directory</b> of the trace to be opened
227 *
228 * @exception JniOpenTraceFailedException Thrown if the open failed
229 */
230 public void openTrace(String newPath) throws JniException {
231 // If open is called while a trace is already opened, we will try to close it first
232 if (thisTracePtr.getPointer() != NULL) {
233 closeTrace();
234 }
235
236 // Set the tracepath and open it
237 tracepath = newPath;
238 openTrace();
239 }
240
241 /**
242 * Open an existing trace.<p>
243 *
244 * The tracepath should have been set already,
245 *
246 * @exception JniOpenTraceFailedException Thrown if the open failed
247 */
248 public void openTrace() throws JniException {
249
250 // Raise an exception if the tracepath is empty, otherwise open the trace
251 if (tracepath == "") {
252 throw new JniTraceException("Tracepath is not set. (openTrace)");
253 }
254
255 // If the file is already opened, close it first
256 if (thisTracePtr.getPointer() != NULL) {
257 closeTrace();
258 }
259
c85e8cb2
WB
260 // Initialization of the library is made here
261 // It is very important that the id is kept!
262 int newLibraryId = initializeLibrary();
263 if ( newLibraryId != -1 ) {
0152140d 264 // Call the LTT to open the trace
c85e8cb2
WB
265 // Note that the libraryId is not yet and the pointer
266 long newPtr = ltt_openTrace(newLibraryId, tracepath, printLttDebug);
0152140d
ASL
267
268 if (newPtr == NULL) {
c85e8cb2 269 thisTracePtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
270 throw new JniOpenTraceFailedException("Error while opening trace. Is the tracepath correct? (openTrace)");
271 }
272
273 // This is OUR pointer
c85e8cb2 274 thisTracePtr = new Jni_C_Pointer_And_Library_Id(newLibraryId, newPtr);
0152140d
ASL
275
276 // Populate the trace with LTT information
277 populateTraceInformation();
278 }
279 else {
c85e8cb2 280 thisTracePtr = new Jni_C_Pointer_And_Library_Id();
687cc7e7
WB
281 throw new JniTraceException("Failed to initialize library! Is the trace version supported?\n" +
282 "Make sure you have the correct LTTv library compiled. (openTrace)");
0152140d
ASL
283 }
284 }
285
286 /**
287 * Close a trace.<p>
288 *
289 * If the trace is already closed, will silently do nothing.
290 */
291 public void closeTrace() {
292
293 if (thisTracePtr.getPointer() != NULL) {
c85e8cb2 294 ltt_closeTrace(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
0152140d
ASL
295
296 // Clear the tracefile map
297 tracefilesMap.clear();
298 tracefilesMap = null;
299
300 // Clear the eventsHeap and make it points to null
301 eventsHeap.clear();
302 eventsHeap = null;
303
304 // Nullify the pointer
c85e8cb2 305 thisTracePtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
306 }
307 }
308
309 /**
310 * This function force the library to free its memory.<p>
311 *
312 * Note : No call to the library will work after this until ltt_initializeHandle is called again
313 */
314 public void freeLibrary() {
c85e8cb2 315 ltt_freeHandle(thisTracePtr.getLibraryId());
0152140d
ASL
316 }
317
318 /*
319 * This function populates the trace data with data from LTT
320 *
321 * @throws JniException
322 */
323 private void populateTraceInformation() throws JniException {
324 if (thisTracePtr.getPointer() == NULL) {
325 throw new JniTraceException("Pointer is NULL, trace not opened/already closed? (populateTraceInformation)");
326 }
327
328 // Populate from the LTT library
c85e8cb2
WB
329 tracepath = ltt_getTracepath(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
330 cpuNumber = ltt_getCpuNumber(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
331 archType = ltt_getArchType(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
332 archVariant = ltt_getArchVariant(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
333 archSize = ltt_getArchSize(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
334 lttMajorVersion = ltt_getLttMajorVersion(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
335 lttMinorVersion = ltt_getLttMinorVersion(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
336 flightRecorder = ltt_getFlightRecorder(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
337 freqScale = ltt_getFreqScale(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
338 startFreq = ltt_getStartFreq(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
339 startTimestampCurrentCounter = ltt_getStartTimestampCurrentCounter(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
340 startMonotonic = ltt_getStartMonotonic(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
0152140d
ASL
341
342 // Creation of time is a bit different, we need to pass the object reference to C
343 //
344 // *** NOTE : LTTv consider "raw startTime" (time without any frequency adjustement) to be default startTime
345 // So "startTimeNoAdjustement" is obtain throught "ltt_feedStartTime()" and
346 // "startTime" is obtained from ltt_feedStartTimeFromTimestampCurrentCounter()
347 startTimeNoAdjustement = new JniTime();
c85e8cb2 348 ltt_feedStartTime(thisTracePtr.getLibraryId(), thisTracePtr.getPointer(), startTimeNoAdjustement);
0152140d
ASL
349
350 startTime = new JniTime();
c85e8cb2 351 ltt_feedStartTimeFromTimestampCurrentCounter(thisTracePtr.getLibraryId(), thisTracePtr.getPointer(), startTime);
0152140d
ASL
352
353 // Call the fill up function for the tracefiles map
354 if ( tracefilesMap== null ) {
355 tracefilesMap = new HashMap<String, JniTracefile>();
356 }
c85e8cb2 357 ltt_feedAllTracefiles(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
0152140d
ASL
358
359 // Now, obtain the trace "endTime"
360 // Note that we discard "startTime" right away, as we already have it
361 endTime = new JniTime();
c85e8cb2 362 ltt_feedTracefileTimeRange(thisTracePtr.getLibraryId(), thisTracePtr.getPointer(), new JniTime(), endTime);
0152140d
ASL
363
364 if (eventsHeap == null) {
365 eventsHeap = new PriorityQueue<JniEvent>(tracefilesMap.size());
366 }
367
368 // Populate the heap with events
369 populateEventHeap();
370 }
371
372 /*
373 * This function populates the event heap with one event from each tracefile
374 * It should be called after each seek or when the object is constructed
375 */
376 private void populateEventHeap() {
377 currentEvent = null;
378 eventsHeap.clear();
379
380 Object new_key = null;
381 JniTracefile tmpTracefile = null;
382
383 Iterator<String> iterator = tracefilesMap.keySet().iterator();
384 while( iterator.hasNext() ) {
385 new_key = iterator.next();
386
387 tmpTracefile = tracefilesMap.get(new_key);
388 if ( tmpTracefile.getCurrentEvent().getEventState() == EOK ) {
389 eventsHeap.add( tmpTracefile.getCurrentEvent() );
390 }
391 }
392 }
393
394 /*
395 * Fills a map of all the trace files.
396 *
397 * Note: This function is called from C and there is no way to propagate
398 * exception back to the caller without crashing JNI. Therefore, it MUST
399 * catch all exceptions.
400 *
401 * @param tracefileName
402 * @param tracefilePtr
403 */
404 protected void addTracefileFromC(String tracefileName, long tracefilePtr) {
405
406 JniTracefile newTracefile = null;
407
408 // Create a new tracefile object and insert it in the map
409 // the tracefile fill itself with LTT data while being constructed
410 try {
c85e8cb2 411 newTracefile = allocateNewJniTracefile(new Jni_C_Pointer_And_Library_Id(thisTracePtr.getLibraryId(), tracefilePtr), this);
0152140d
ASL
412 getTracefilesMap().put( (tracefileName + newTracefile.getCpuNumber()), newTracefile);
413 }
414 catch(JniTracefileWithoutEventException e) {
415 if ( printLttDebug == true ) {
c85e8cb2 416 printlnC(thisTracePtr.getLibraryId(), "JniTracefile " + tracefileName + " has no event (addTracefileFromC). Ignoring.");
0152140d
ASL
417 }
418 }
419 catch(Exception e) {
420 if ( printLttDebug == true ) {
c85e8cb2 421 printlnC(thisTracePtr.getLibraryId(), "Failed to add tracefile " + tracefileName + " to tracefilesMap!(addTracefileFromC)\n\tException raised : " + e.toString() );
0152140d
ASL
422 }
423 }
424 }
425
426 /**
427 * Return the top event in the events stack, determined by timestamp, in the trace (all the tracefiles).<p>
428 *
429 * Note : If the events were read before, the top event and the event currently loaded (currentEvent) are most likely the same.
430 *
431 * @return The top event in the stack or null if no event is available.
432 *
c357e4b6 433 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
0152140d
ASL
434 */
435 public JniEvent findNextEvent() {
436 return eventsHeap.peek();
437 }
438
439 /**
440 * Return the next event in the events stack, determined by timestamp, in the trace (all the tracefiles).<p>
441 *
442 * @return The next event in the trace or null if no event is available.
443 *
c357e4b6 444 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
0152140d
ASL
445 */
446 public JniEvent readNextEvent() {
447 // Get the "next" event on the top of the heap but DO NOT remove it
448 JniEvent tmpEvent = eventsHeap.peek();
449
450 // If the event is null, it was the last one in the trace we can leave the function
451 if (tmpEvent == null) {
452 return null;
453 }
454
455 // Otherwise, we need to make sure the timestamp of the event we got is not the same as the last "NextEvent" we requested
456 // NOTE : JniEvent.compareTo() compare by timestamp AND type, as 2 events of different type could have the same timestamp.
64fe8e8a
FC
457// if( currentEvent != null ){
458 if (tmpEvent.equals(currentEvent)) {
0152140d
ASL
459 // Remove the event on top as it is the same currentEventTimestamp
460 eventsHeap.poll();
461
462 // Read the next event for this particular event type
463 tmpEvent.readNextEvent();
464
465 // If the event state is sane (not Out of Range), put it back in the heap
466 if ( tmpEvent.getEventState() == EOK ) {
467 eventsHeap.add(tmpEvent);
468 }
469
470 // Pick the top event again
471 tmpEvent = eventsHeap.peek();
472
473 // Save the event we just read as the "current event"
474 currentEvent = tmpEvent;
475 }
476 // If the event on top has different timestamp than the currentTimestamp, just save this timestamp as current
477 else {
478 currentEvent = tmpEvent;
479 }
480
481 return tmpEvent;
482 }
483
484 /**
485 * Read the next event on a certain tracefile.<p>
486 *
487 * By calling this function make sure the "global" readNextEvent() stay synchronised.
488 * Calling readNextEvent() after this function will consider this tracefile moved and is then consistent.
489 *
490 * @param targetTracefile The tracefile object to read from
491 *
492 * @return The next event in the tracefile or null if no event is available.
493 *
c357e4b6
WB
494 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
495 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
0152140d
ASL
496 */
497 public JniEvent readNextEvent(JniTracefile targetTracefile) {
498 JniEvent returnedEvent = null;
499
500 // There is 2 special cases where we should read the CURRENT event, not the next one
501 // 1- The currentEvent is null --> We never read or we just seeked
502 // 2- The currentEvent is of another type --> We last read on a DIFFERENT tracefile
503 if ( (currentEvent == null) ||
504 (currentEvent.getParentTracefile().equals(targetTracefile) == false)
505 ) {
506 returnedEvent = targetTracefile.getCurrentEvent();
507 // Save the event we read
508 currentEvent = returnedEvent;
509 }
510 else {
511 // Remove from the event related to this tracefile from the event heap, if it exists.
512 // WARNING : This only safe as long getCurrentEvent() never return "null" in any case.
513 eventsHeap.remove(targetTracefile.getCurrentEvent() );
514
515 // If status EOK, we can return the event, otherwise something wrong happen (out of range, read error, etc...)
516 if ( targetTracefile.readNextEvent() == EOK) {
517 returnedEvent = targetTracefile.getCurrentEvent();
518 // Add back to the heap the read event
519 eventsHeap.add(returnedEvent);
520 }
521 // Save the event we read...
522 // Note : might be null if the read failed and it's ok
523 currentEvent = targetTracefile.getCurrentEvent();
524 }
525
526 return returnedEvent;
527 }
528
529 /**
530 * Seek to a certain time but <b>do not</b> read the next event.<p>
531 *
532 * This only position the trace, it will not return anything.<p>
533 *
534 * @param seekTime The time where we want to seek to
535 *
c357e4b6 536 * @see org.eclipse.linuxtools.lttng.jni.common.JniTime
0152140d
ASL
537 */
538 public void seekToTime(JniTime seekTime) {
539
540 // Invalidate the last read event
541 currentEvent = null;
542
543 Object tracefile_name = null;
544 Iterator<String> iterator = tracefilesMap.keySet().iterator();
545
546 while (iterator.hasNext() ) {
547 // We seek to the given event for ALL tracefiles
548 tracefile_name = iterator.next();
549 seekToTime(seekTime, tracefilesMap.get(tracefile_name));
550 }
551
552 populateEventHeap();
553 }
554
555 /**
556 * Seek to a certain time on a certain tracefile but <b>do not</b> read the next event.<p>
557 *
558 * This only position the trace, it will not return anything.<p>
559 *
560 * @param targetTracefile The tracefile object to read from
561 * @param seekTime The time where we want to seek to
562 *
c357e4b6
WB
563 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
564 * @see org.eclipse.linuxtools.lttng.jni.common.JniTime
0152140d
ASL
565 */
566 public void seekToTime(JniTime seekTime, JniTracefile targetTracefile) {
567 // Invalidate the current read event
568 currentEvent = null;
569
570 // Remove from the event related to this tracefile from the event heap, if it exists.
571 // WARNING : This is only safe as long getCurrentEvent() never return "null" in any case.
572 eventsHeap.remove(targetTracefile.getCurrentEvent() );
573
574 // Perform the actual seek on the tracefile
575 // Add the event to the heap if it succeed
576 if ( targetTracefile.seekToTime(seekTime) == EOK) {
577 // Add back to the heap the read event
578 eventsHeap.add(targetTracefile.getCurrentEvent());
579 }
580 }
581
582 /**
583 * Seek to a certain timestamp and read the next event.
584 * <p>
585 * If no more events are available or an error happen, null will be returned.
586 *
587 * @param seekTime The time where we want to seek to.
588 *
589 * @return The event just after the seeked time or null if none available.
590 *
c357e4b6
WB
591 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
592 * @see org.eclipse.linuxtools.lttng.jni.common.JniTime
0152140d
ASL
593 */
594 public JniEvent seekAndRead(JniTime seekTime) {
595 JniEvent returnedEvent = null;
596 seekToTime(seekTime);
597
598 // The trace should be correctly positionned, let's get the event
599 returnedEvent = readNextEvent();
600
601 return returnedEvent;
602 }
603
604 /**
605 * Seek to a certain timestamp on a certain tracefile and read the next event.<p>
606 *
607 * If no more events are available or an error happen, null will be returned.
608 *
609 * Calling readNextEvent() after this function will consider this tracefile moved and is then consistent.<br>
610 *
c357e4b6 611 * @param targetTracefile The tracefile object to read from
0152140d
ASL
612 * @param seekTime The time where we want to seek to
613 *
614 * @return The event just after the seeked time or null if none available.
615 *
c357e4b6
WB
616 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
617 * @see org.eclipse.linuxtools.lttng.jni.common.JniTime
618 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
0152140d
ASL
619 */
620 public JniEvent seekAndRead(JniTime seekTime, JniTracefile targetTracefile) {
621 seekToTime(seekTime, targetTracefile);
622 return readNextEvent(targetTracefile);
623 }
624
625 /**
626 * Get a certain tracefile from its given name.<p>
627 *
628 * @param tracefileName The name of the tracefile.
629 *
630 * @return The tracefile found or null if none.
631 *
c357e4b6 632 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
0152140d
ASL
633 */
634 public JniTracefile requestTracefileByName(String tracefileName) {
635 return tracefilesMap.get(tracefileName);
636 }
637
638 /**
639 * Get a certain event associated to a tracefile from the tracefile name.<p>
640 *
641 * @param tracefileName The name of the trace file.
642 *
643 * @return Event of the tracefile or null if none found.
644 *
c357e4b6 645 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
0152140d
ASL
646 */
647 public JniEvent requestEventByName(String tracefileName) {
648 JniEvent returnValue = null;
649
650 JniTracefile tmpTracefile = tracefilesMap.get(tracefileName);
651
652 // If the tracefile is found, return the current event
653 // There should always be an event linked to a tracefile
654 if (tmpTracefile != null) {
655 returnValue = tmpTracefile.getCurrentEvent();
656 }
657
658 return returnValue;
659 }
660
661 // Access to class variable. Most of them doesn't have setter
662 public String getTracepath() {
663 return tracepath;
664 }
665
666 public int getCpuNumber() {
667 return cpuNumber;
668 }
669
670 public long getArchType() {
671 return archType;
672 }
673
674 public long getArchVariant() {
675 return archVariant;
676 }
677
678 public short getArchSize() {
679 return archSize;
680 }
681
682 public short getLttMajorVersion() {
683 return lttMajorVersion;
684 }
685
686 public short getLttMinorVersion() {
687 return lttMinorVersion;
688 }
689
690 public short getFlightRecorder() {
691 return flightRecorder;
692 }
693
694 public long getFreqScale() {
695 return freqScale;
696 }
697
698 public long getStartFreq() {
699 return startFreq;
700 }
701
702 public long getStartTimestampCurrentCounter() {
703 return startTimestampCurrentCounter;
704 }
705
706 public long getStartMonotonic() {
707 return startMonotonic;
708 }
709
710 public JniTime getStartTime() {
711 return startTime;
712 }
713
714 public JniTime getEndTime() {
715 return endTime;
716 }
717
718 public JniTime getStartTimeNoAdjustement() {
719 return startTimeNoAdjustement;
720 }
721
722 public HashMap<String, JniTracefile> getTracefilesMap() {
723 return tracefilesMap;
724 }
725
726 /**
727 * The timestamp of the last read event.<p>
728 *
729 * Note : If no event is available, Long.MAX_VALUE is returned.
730 *
731 * @return Time of the last event read
732 *
c357e4b6 733 * @see org.eclipse.linuxtools.lttng.jni.common.JniTime
0152140d
ASL
734 */
735 public JniTime getCurrentEventTimestamp() {
736 JniTime returnedTime = null;
737
738 // If no event were read or we reach the last event in the trace,
739 // currentEvent will be null
740 if ( currentEvent != null ) {
741 returnedTime = currentEvent.getEventTime();
742 }
743 else {
744 returnedTime = new JniTime(Long.MAX_VALUE);
745 }
746 return returnedTime;
747 }
748
749 /**
750 * Pointer to the LttTrace C structure.<p>
751 *
752 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
753 * know what you are doing.
754 *
755 * @return The actual (long converted) pointer or NULL.
756 *
c85e8cb2 757 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
0152140d 758 */
c85e8cb2 759 public Jni_C_Pointer_And_Library_Id getTracePtr() {
0152140d
ASL
760 return thisTracePtr;
761 }
762
763 /**
764 * Return boolean value saying if the debug is enabled in LTT or not.<p>
765 *
766 * Note : this need to be set at construction.
767 *
768 * @return If the debug is set or not
769 */
770 public boolean isPrintingLttDebug() {
771 return printLttDebug;
772 }
773
774 /**
775 * Print information for all the tracefiles associated with this trace.
776 * <u>Intended to debug</u><p>
777 *
778 * This function will call Ltt to print, so information printed will be the
779 * one from the C structure, not the one populated in java.
780 *
c357e4b6 781 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
0152140d
ASL
782 */
783 public void printAllTracefilesInformation() {
784 JniTracefile tracefile = null;
785
786 Iterator<String> iterator = tracefilesMap.keySet().iterator();
787 while (iterator.hasNext()) {
788 tracefile = tracefilesMap.get(iterator.next());
789 tracefile.printTracefileInformation();
790 }
791 }
792
793 /**
794 * Print information for this trace.
795 * <u>Intended to debug</u><p>
796 *
797 * This function will call Ltt to print, so information printed will be the
798 * one from the C structure, not the one populated in java.<p>
799 */
800 public void printTraceInformation() {
c85e8cb2 801 ltt_printTrace(thisTracePtr.getLibraryId(), thisTracePtr.getPointer());
0152140d
ASL
802 }
803
804 /**
805 * toString() method.
806 * <u>Intended to debug</u><br>
807 *
808 * @return Attributes of the object concatenated in String
809 */
810 @Override
3b38ea61 811 @SuppressWarnings("nls")
0152140d
ASL
812 public String toString() {
813 String returnData = "";
814 returnData += "tracepath : " + tracepath + "\n";
815 returnData += "cpuNumber : " + cpuNumber + "\n";
816 returnData += "archType : " + archType + "\n";
817 returnData += "archVariant : " + archVariant + "\n";
818 returnData += "archSize : " + archSize + "\n";
819 returnData += "lttMajorVersion : " + lttMajorVersion + "\n";
820 returnData += "lttMinorVersion : " + lttMinorVersion + "\n";
821 returnData += "flightRecorder : " + flightRecorder + "\n";
822 returnData += "freqScale : " + freqScale + "\n";
823 returnData += "startFreq : " + startFreq + "\n";
824 returnData += "startTimestampCurrentCounter : " + startTimestampCurrentCounter + "\n";
825 returnData += "startMonotonic : " + startMonotonic + "\n";
826 returnData += "startTimeNoAdjustement : " + startTimeNoAdjustement.getReferenceToString() + "\n";
827 returnData += " seconds : " + startTimeNoAdjustement.getSeconds() + "\n";
828 returnData += " nanoSeconds : " + startTimeNoAdjustement.getNanoSeconds() + "\n";
829 returnData += "startTime : " + startTime.getReferenceToString() + "\n";
830 returnData += " seconds : " + startTime.getSeconds() + "\n";
831 returnData += " nanoSeconds : " + startTime.getNanoSeconds() + "\n";
832 returnData += "endTime : " + endTime.getReferenceToString() + "\n";
833 returnData += " seconds : " + endTime.getSeconds() + "\n";
834 returnData += " nanoSeconds : " + endTime.getNanoSeconds() + "\n";
835 returnData += "tracefilesMap : " + tracefilesMap.keySet() + "\n"; // Hack to avoid ending up with tracefilesMap.toString()
836
837 return returnData;
838 }
839
c357e4b6
WB
840
841 // ****************************
842 // **** ABSTRACT FUNCTIONS ****
843 // You MUST override those in your version specific implementation
844
845 /**
846 * Function place holder to load the correct C library.<p>
847 * <br>
848 * Can be as simple as calling ltt_initializeHandle(LIBRARY_NAME) with the correct .so instead of LIBRARY_NAME.<br>
849 * You may also want to perform some check or some additionnal validations.<br>
850 * <br>
851 * <b>!! Override this with you version specific implementation.</b><br>
852 *
c85e8cb2 853 * @return integer that is the library id, or -1 if the load was unsuccessful
c357e4b6 854 */
c85e8cb2 855 public abstract int initializeLibrary();
0152140d 856
c357e4b6
WB
857
858 /**
859 * Function place holder to allocate a new JniTracefile.<p>
860 *
861 * JniTracefile constructor is non overridable so we need another overridable function to return the correct version of JniTracefile.<br>
862 * Effect of this function should be the same (allocate a fresh new JniTracefile)<br>
863 * <br>
864 * <b>!! Override this with you version specific implementation.</b><br>
865 *
866 * @param newPtr The pointer of an already opened LttTracefile C Structure
867 * @param newParentTrace The JniTrace parent of this tracefile.
868 *
869 * @return The newly allocated JniTracefile of the correct version
870 *
871 * @throws JniException The construction (allocation) failed.
872 *
873 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
c85e8cb2 874 * @see org.eclipse.linuxtools.lttng.jni.common.Jni_C_Pointer_And_Library_Id
c357e4b6
WB
875 * @see org.eclipse.linuxtools.lttng.jni.JniTrace
876 */
c85e8cb2 877 public abstract JniTracefile allocateNewJniTracefile(Jni_C_Pointer_And_Library_Id newPtr, JniTrace newParentTrace) throws JniException;
0152140d 878
c357e4b6 879}
This page took 0.066448 seconds and 5 git commands to generate.