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