tmf: Use tabs in statistics view for each traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / JniEvent.java
CommitLineData
0152140d
ASL
1package org.eclipse.linuxtools.lttng.jni;
2/*******************************************************************************
3 * Copyright (c) 2009 Ericsson
84b2b1f0 4 *
0152140d
ASL
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
84b2b1f0 9 *
0152140d
ASL
10 * Contributors:
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
13
14
15import java.util.HashMap;
16
ce38c104
FC
17import org.eclipse.linuxtools.internal.lttng.jni.common.JniTime;
18import org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id;
19import org.eclipse.linuxtools.internal.lttng.jni.exception.JniEventException;
20import org.eclipse.linuxtools.internal.lttng.jni.exception.JniException;
21import org.eclipse.linuxtools.internal.lttng.jni.exception.JniNoSuchEventException;
0152140d
ASL
22
23/**
d37aaa7f 24 * The LTTng event in its native (C) format.<br>
0152140d 25 * Provides access to the LttEvent C structure in java. <p>
84b2b1f0 26 *
0152140d
ASL
27 * Most important fields in the JniEvent are :
28 * <ul>
29 * <li>an event time, which is a digested timestamp.
30 * </ul>
31 * Note that the JniEvent content is not directly accessibe and should be obtained
32 * using the parseAllFields() or parseFieldBy...() methods.
84b2b1f0 33 *
3b7509b0
WB
34 * <b>NOTE</b><p>
35 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<p>
84b2b1f0 36 *
d37aaa7f
FC
37 * @version 0.1
38 * @author William Bourque
0152140d 39 */
84b2b1f0 40public abstract class JniEvent extends Jni_C_Common implements Comparable<JniEvent>
c85e8cb2 41{
0152140d
ASL
42 // Variables to detect if the event have been filled at least once
43 // this make possible the detection of "uninitialized" struct in Ltt
34eff969 44 // Can be "EOK", "ERANGE" or "EPERM" (defined in Jni_C_Common)
0152140d
ASL
45 private int eventState = EPERM; // Start with EPERM to ensure sanity
46
47 // Internal C pointer of the JniEvent used in LTT
c85e8cb2 48 private Jni_C_Pointer_And_Library_Id thisEventPtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
49
50 // Reference to the parent tracefile
51 private JniTracefile parentTracefile = null;
52
53 // This map hold marker relative to the parent tracefile of this event
54 // They are "our" marker in this event
55 private HashMap<Integer, JniMarker> markersMap = null;
56
57 // Data we should populate from ltt
58 // Note that all type have been scaled up as there is no "unsigned" in java
59 // This might be a problem about "unsigned long" as there is no equivalent
60 // in java
c85e8cb2 61 private Jni_C_Pointer_And_Library_Id tracefilePtr = new Jni_C_Pointer_And_Library_Id();
0152140d
ASL
62 private JniTime eventTime = null;
63
64 // These methods need a tracefile pointer, instead of a event pointer
c85e8cb2
WB
65 protected native int ltt_readNextEvent(int libId, long tracefilePtr);
66 protected native int ltt_seekEvent(int libId, long tracefilePtr, JniTime givenTime);
67 protected native int ltt_positionToFirstEvent(int libId, long tracefilePtr);
84b2b1f0 68
0152140d 69 // Native access functions
c85e8cb2
WB
70 protected native long ltt_getTracefilePtr(int libId, long eventPtr);
71 protected native long ltt_getBlock(int libId, long eventPtr);
72 protected native long ltt_getOffset(int libId, long eventPtr);
73 protected native long ltt_getCurrentTimestampCounter(int libId, long eventPtr);
74 protected native long ltt_getTimestamp(int libId, long eventPtr);
75 protected native int ltt_getEventMarkerId(int libId, long eventPtr);
76 protected native long ltt_getNanosencondsTime(int libId, long eventPtr);
77 protected native void ltt_feedEventTime(int libId, long eventPtr, JniTime eventTime);
78 protected native long ltt_getEventDataSize(int libId, long eventPtr);
79 protected native long ltt_getEventSize(int libId, long eventPtr);
80 protected native int ltt_getCount(int libId, long eventPtr);
81 protected native long ltt_getOverflowNanoSeconds(int libId, long eventPtr);
84b2b1f0 82
c85e8cb2
WB
83 // This method can be use to obtain the content as byte array
84 // Warning : untested!
85 protected native void ltt_getDataContent(int libId, long eventPtr, long dataSize, byte[] returnedContent);
84b2b1f0 86
0152140d 87 // Debug native function, ask LTT to print event structure
c85e8cb2 88 protected native void ltt_printEvent(int libId, long eventPtr);
84b2b1f0 89
0152140d
ASL
90 /**
91 * Default constructor is forbidden
92 */
93 protected JniEvent() {
e31e01e8 94 }
0152140d
ASL
95
96 /**
97 * Copy constructor.<p>
84b2b1f0
AM
98 *
99 * @param oldEvent Reference to the JniEvent you want to copy.
0152140d
ASL
100 */
101 public JniEvent(JniEvent oldEvent) {
102 thisEventPtr = oldEvent.thisEventPtr;
103 markersMap = oldEvent.markersMap;
104 parentTracefile = oldEvent.parentTracefile;
105 eventState = oldEvent.eventState;
106
107 tracefilePtr = oldEvent.tracefilePtr;
108 eventTime = oldEvent.eventTime;
109 }
84b2b1f0 110
0152140d 111 /**
84b2b1f0
AM
112 * Constructor with parameters
113 * <p>
114 *
115 * This constructor could throw. It will happen if an event can not be
116 * populated on <u>first read</u>.<br>
117 * In that case, the parent tracefile is probably useless and should be
118 * deleted.
119 *
120 * @param newEventPtr
121 * C pointer (converted in long) of the LttEvent C structure.
122 * @param newMarkersMap
123 * Reference an already populated HashMap of JniMarker objects
124 * @param newParentTracefile
125 * Reference to the parent JniTracefile of this JniEvent
126 *
0152140d 127 * @exception JniException
84b2b1f0
AM
128 * If the JNI call fails
129 *
ce38c104 130 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id
c357e4b6
WB
131 * @see org.eclipse.linuxtools.lttng.jni.JniMarker
132 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
0152140d 133 */
84b2b1f0
AM
134 public JniEvent(Jni_C_Pointer_And_Library_Id newEventPtr,
135 HashMap<Integer, JniMarker> newMarkersMap,
136 JniTracefile newParentTracefile) throws JniException {
137
138 // Basic test to make sure we didn't get null/empty value
0152140d 139 if ((newEventPtr.getPointer() == NULL)
84b2b1f0 140 || (newMarkersMap == null)
0152140d
ASL
141 || (newMarkersMap.size() == 0)
142 || (newParentTracefile == null)) {
9c4eb5f7 143 throw new JniEventException("Null or empty value passed to constructor, object is invalid! (JniEvent)"); //$NON-NLS-1$
0152140d 144 }
84b2b1f0 145
0152140d
ASL
146 thisEventPtr = newEventPtr;
147 tracefilePtr = newParentTracefile.getTracefilePtr();
148 markersMap = newMarkersMap;
149 parentTracefile = newParentTracefile;
150
151 eventTime = new JniTime();
84b2b1f0 152
0152140d 153 // Try to move to the first event
84b2b1f0 154 // If the event is Out of Range (ERANGE) at the first read,
0152140d
ASL
155 // this event type will never be usable.
156 // In that case, throw JniNoSuchEventException to warn the tracefile.
157 eventState = positionToFirstEvent();
158 if (eventState != EOK) {
9c4eb5f7 159 throw new JniNoSuchEventException("Object not populated, unusable. There is probably no event of that type in the trace. (JniEvent)"); //$NON-NLS-1$
0152140d 160 }
84b2b1f0 161 populateEventInformation();
0152140d
ASL
162 }
163
164 /**
165 * Move to the next event and populate the java object with LttEvent structure.<p>
84b2b1f0 166 *
0152140d 167 * If the move fails, the event will not get populated and the last event data will still be available.
84b2b1f0 168 *
c357e4b6 169 * @return LTT read status, as defined in Jni_C_Constant.
84b2b1f0 170 *
ce38c104 171 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Constant
0152140d
ASL
172 */
173 public int readNextEvent() {
174 // Ask Ltt to read the next event for this particular tracefile
c85e8cb2 175 eventState = ltt_readNextEvent(tracefilePtr.getLibraryId(), tracefilePtr.getPointer() );
0152140d
ASL
176 // If the event state is sane populate it
177 if (eventState == EOK) {
178 populateEventInformation();
179 }
84b2b1f0 180
0152140d
ASL
181 return eventState;
182 }
183
184 /**
84b2b1f0
AM
185 * Seek to a certain time.
186 * <p>
187 *
188 * Seek to a certain time and read event at this exact time or the next one
189 * if there is no event there.
190 * <p>
191 *
192 * Note that this function can seek in an invalid position if the timestamp
193 * is after the last event.<br>
194 * In that case, a seek back would be required to get back to a consistent
195 * state.
196 * <p>
197 *
198 * If the seek fails, the event will not get populated and the last event
199 * data will still be available.
200 * <p>
201 *
202 * @param seekTime
203 * The timestamp to which we want to seek
c357e4b6 204 * @return LTT read status, as defined in Jni_C_Constant
84b2b1f0 205 *
ce38c104 206 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Constant
0152140d
ASL
207 */
208 public int seekToTime(JniTime seekTime) {
209 // Ask Ltt to read the next event for this particular tracefile
c85e8cb2 210 eventState = ltt_seekEvent(tracefilePtr.getLibraryId(), tracefilePtr.getPointer(), seekTime);
84b2b1f0 211
0152140d
ASL
212 // If the event state is sane populate it
213 if (eventState == EOK) {
214 populateEventInformation();
215 }
216
217 return eventState;
218 }
219
220 /**
84b2b1f0
AM
221 * Try to seek to a certain time and seek back if it failed.
222 * <p>
223 *
224 * Seek to a certain time and read event at this exact time or the next one
225 * if there is no event there.
226 * <p>
227 *
228 * If the seek fails, we will seek back to the previous position, so the
229 * event will stay in a consistent state.
230 * <p>
231 *
232 * @param seekTime
233 * The target timestamp we will try to seek to
c357e4b6 234 * @return LTT read status, as defined in Jni_C_Constant
84b2b1f0 235 *
ce38c104 236 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Constant
0152140d
ASL
237 */
238 public int seekOrFallBack(JniTime seekTime) {
239 // Save the old time
240 JniTime oldTime = new JniTime(eventTime);
241
242 // Call seek to move ahead
243 // Save the state for the return (eventState will be modified if we seek back)
244 int returnState = seekToTime(seekTime);
245
246 // If the event state is sane populate it
247 if (returnState == EOK) {
248 populateEventInformation();
249 }
250 else {
251 seekToTime(oldTime);
252 }
253
254 return returnState;
255 }
256
257 /**
258 * Position on the first event in the tracefile.<p>
84b2b1f0 259 *
0152140d 260 * The function return the read status after the first event.<p>
84b2b1f0 261 *
0152140d 262 * A status different of EOK probably means there is no event associated to this tracefile.
84b2b1f0 263 *
c357e4b6 264 * @return LTT read status, as defined in Jni_C_Constant
84b2b1f0 265 *
ce38c104 266 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Constant
0152140d 267 */
5a5c2fc7 268 protected int positionToFirstEvent() {
c85e8cb2 269 eventState = ltt_positionToFirstEvent(tracefilePtr.getLibraryId(), tracefilePtr.getPointer());
84b2b1f0 270
0152140d
ASL
271 return eventState;
272 }
84b2b1f0 273
0152140d
ASL
274 /**
275 * Obtain a marker associated with this tracefile's event.
84b2b1f0 276 *
0152140d 277 * @return Reference to the marker for this tracefile's event or null if none.
84b2b1f0 278 *
c357e4b6 279 * @see org.eclipse.linuxtools.lttng.jni.JniMarker
0152140d
ASL
280 */
281 public JniMarker requestEventMarker() {
282 return markersMap.get(getEventMarkerId());
283 }
284
285 /**
286 * Obtain the raw data of a LttEvent object.<p>
84b2b1f0 287 *
0152140d
ASL
288 * The data will be in raw C bytes, not java bytes.<br>
289 * Note : This function is mostly untested and provided "as is".
84b2b1f0 290 *
0152140d
ASL
291 * @return Bytes array of raw data (contain raw C bytes).
292 */
293 public byte[] requestEventContent() {
294 byte dataContent[] = new byte[(int) getEventDataSize()];
295
c85e8cb2 296 ltt_getDataContent(thisEventPtr.getLibraryId(), thisEventPtr.getPointer(), getEventDataSize(), dataContent);
0152140d
ASL
297
298 return dataContent;
299 }
84b2b1f0 300
0152140d
ASL
301 /**
302 * Obtain an event source.<p>
84b2b1f0 303 *
0152140d 304 * This is not implemented yet and will always return "Kernel core" for now.
84b2b1f0
AM
305 *
306 * @return Reference to the JniMarker object for this event or null if none.
307 *
c357e4b6 308 * @see org.eclipse.linuxtools.lttng.jni.JniMarker
0152140d
ASL
309 */
310 public String requestEventSource() {
311 // *** TODO ***
312 // No "Source" of event exists in Ltt so far
313 // It would be a good addition to have a way to detect where an event come
314 // from, like "kernel" or "userspace"
84b2b1f0 315 //
9c4eb5f7 316 return "Kernel Core"; //$NON-NLS-1$
0152140d 317 }
84b2b1f0 318
0152140d
ASL
319 /**
320 * Parse a particular field in the event payload, identified by its id (position).<p>
84b2b1f0 321 *
0152140d 322 * Note : Position are relative to an event marker (i.e. requestEventMarker().getMarkerFieldsArrayList() )
84b2b1f0 323 *
0152140d 324 * @param fieldId Position of the field to parse.
84b2b1f0 325 *
0152140d 326 * @return Object that contain the parsed payload
84b2b1f0 327 *
c357e4b6 328 * @see org.eclipse.linuxtools.lttng.jni.JniParser
0152140d
ASL
329 */
330 public Object parseFieldById(int fieldId) {
331 return JniParser.parseField(this, fieldId);
332 }
84b2b1f0 333
0152140d
ASL
334 /**
335 * Parse a particular field in the event payload, identified by its name.<p>
84b2b1f0 336 *
0152140d 337 * Note : Name are relative to an event marker (i.e. requestEventMarker().getMarkerFieldsHashMap() )
84b2b1f0 338 *
0152140d 339 * @param fieldName Position of the field to parse.
84b2b1f0 340 *
0152140d 341 * @return Object that contain the parsed payload
84b2b1f0 342 *
c357e4b6 343 * @see org.eclipse.linuxtools.lttng.jni.JniParser
0152140d
ASL
344 */
345 public Object parseFieldByName(String fieldName) {
346 return JniParser.parseField(this, fieldName);
347 }
84b2b1f0 348
0152140d
ASL
349 /**
350 * Method to parse all the event payload.<p>
84b2b1f0 351 *
0152140d 352 * @return HashMap<String, Object> which is the parsedContent objects and their name as key.
84b2b1f0
AM
353 *
354 * @see org.eclipse.linuxtools.lttng.jni.JniParser
0152140d
ASL
355 */
356 public HashMap<String, Object> parseAllFields() {
357 return JniParser.parseAllFields(this);
358 }
359
84b2b1f0 360 /*
0152140d 361 * This function populates the event data with data from LTT
84b2b1f0 362 *
0152140d 363 * NOTE : To get better performance, we copy very few data into memory here
84b2b1f0 364 *
0152140d
ASL
365 */
366 private void populateEventInformation() {
367 // We need to save the time, as it is not a primitive (can't be dynamically called in getter)
c85e8cb2 368 eventTime.setTime(ltt_getNanosencondsTime(thisEventPtr.getLibraryId(), thisEventPtr.getPointer()));
0152140d 369 }
84b2b1f0
AM
370
371 /**
372 * Get this event's timestamp
373 *
374 * @return The timestamp, as a JniTime
375 */
0152140d
ASL
376 public JniTime getEventTime() {
377 return eventTime;
378 }
84b2b1f0
AM
379
380 // *** To get better performance, all getters below call LTT directly ****
0152140d 381 // That way, we can avoid copying data into memory
84b2b1f0
AM
382
383 /**
384 * Get this event's marker ID. This is a direct call through JNI.
385 *
386 * @return The marker ID
387 */
0152140d 388 public int getEventMarkerId() {
c85e8cb2 389 return ltt_getEventMarkerId(thisEventPtr.getLibraryId(), thisEventPtr.getPointer());
0152140d
ASL
390 }
391
84b2b1f0
AM
392 /**
393 * Get this event's data size. This is a direct call through JNI.
394 *
395 * @return The data size
396 */
0152140d 397 public long getEventDataSize() {
84b2b1f0
AM
398 return ltt_getEventDataSize(thisEventPtr.getLibraryId(),
399 thisEventPtr.getPointer());
0152140d
ASL
400 }
401
84b2b1f0
AM
402 /**
403 * Get the markers for this event. This does NOT go through JNI.
404 *
405 * @return The (java) Map of markers
406 */
0152140d
ASL
407 public HashMap<Integer, JniMarker> getMarkersMap() {
408 return markersMap;
409 }
410
411 /**
412 * Pointer to the parent LTTTracefile C structure.<br>
413 * <br>
414 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
415 * know what you are doing.
84b2b1f0 416 *
0152140d 417 * @return The actual (long converted) pointer or NULL.
84b2b1f0 418 *
ce38c104 419 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id
0152140d 420 */
c85e8cb2
WB
421 public Jni_C_Pointer_And_Library_Id getTracefilePtr() {
422 return new Jni_C_Pointer_And_Library_Id(thisEventPtr.getLibraryId(), ltt_getTracefilePtr(thisEventPtr.getLibraryId(), thisEventPtr.getPointer()) );
0152140d
ASL
423 }
424
425 /**
426 * Pointer to the LttEvent C structure.<br>
427 * <br>
428 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
429 * know what you are doing.
84b2b1f0 430 *
0152140d 431 * @return The actual (long converted) pointer or NULL.
84b2b1f0 432 *
ce38c104 433 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id
0152140d 434 */
c85e8cb2 435 public Jni_C_Pointer_And_Library_Id getEventPtr() {
0152140d
ASL
436 return thisEventPtr;
437 }
438
84b2b1f0
AM
439 /**
440 * Get this event's state
441 *
442 * @return The integer value representing the state
443 */
0152140d
ASL
444 public int getEventState() {
445 return eventState;
446 }
447
448 /**
449 * Getter to the parent tracefile for this event.
450 *
84b2b1f0
AM
451 * @return The parent tracefile
452 *
c357e4b6 453 * @see org.eclipse.linuxtools.lttng.jni.JniTracefile
0152140d
ASL
454 */
455 public JniTracefile getParentTracefile() {
456 return parentTracefile;
457 }
5a5c2fc7
FC
458
459 /* (non-Javadoc)
460 * @see java.lang.Object#hashCode()
461 */
f9a8715c 462 @Override
5a5c2fc7
FC
463 public int hashCode() {
464 final int prime = 31;
465 int result = 1;
466 result = prime * result + ((eventTime == null) ? 0 : eventTime.hashCode());
467 result = prime * result + ((parentTracefile == null) ? 0 : parentTracefile.hashCode());
468 return result;
469 }
470 /* (non-Javadoc)
471 * @see java.lang.Object#equals(java.lang.Object)
472 */
473 @Override
474 public boolean equals(Object obj) {
475 if (this == obj) {
476 return true;
477 }
478 if (obj == null) {
f9a8715c 479 return false;
5a5c2fc7
FC
480 }
481 if (!(obj instanceof JniEvent)) {
482 return false;
483 }
484 JniEvent other = (JniEvent) obj;
485 if (eventTime == null) {
486 if (other.eventTime != null) {
487 return false;
488 }
489 } else if (!eventTime.equals(other.eventTime)) {
490 return false;
491 }
492 if (parentTracefile == null) {
493 if (other.parentTracefile != null) {
494 return false;
495 }
496 } else if (!parentTracefile.equals(other.parentTracefile)) {
497 return false;
498 }
499 return true;
500 }
501
0152140d
ASL
502 /**
503 * Compare fonction for JNIEvent.<p>
504 * <p>
505 * This will compare the current JNIEvent with a passed one by timestamp AND tracefile ("type").<br>
506 * If both are equal but type differs, current event is considered to be older (-1 returned).
84b2b1f0 507 *
0152140d
ASL
508 * @return -1 if given event happens before, 0 if equal, 1 if passed event happens after.
509 */
d4011df2 510 @Override
64fe8e8a
FC
511 public int compareTo(JniEvent other) {
512
513 // By default, we consider the current event to be older.
514 int eventComparaison = -1;
515
516 // Test against null before performing anything
517 if (other != null) {
518 // Compare the timestamp first
519 eventComparaison = this.getEventTime().compareTo(other.getEventTime());
520
521 // If timestamp is equal, compare the parent trace file ("event type")
522 if ((eventComparaison == 0)
523 && (!this.parentTracefile.equals(other.parentTracefile))) {
524 eventComparaison = 1;
525 }
526 }
527 return eventComparaison;
528 }
84b2b1f0 529
0152140d 530 /**
84b2b1f0 531 * Print information for this event.
0152140d 532 * <u>Intended to debug</u><br>
84b2b1f0
AM
533 *
534 * This function will call Ltt to print, so information printed will be
0152140d
ASL
535 * the one from the C structure, not the one populated in java.<p>
536 */
537 public void printEventInformation() {
c85e8cb2 538 ltt_printEvent(thisEventPtr.getLibraryId(), thisEventPtr.getPointer());
0152140d 539 }
84b2b1f0 540
0152140d 541 /**
84b2b1f0 542 * toString() method.
0152140d 543 * <u>Intended to debug.</u><p>
84b2b1f0 544 *
0152140d
ASL
545 * @return Attributes of the object concatenated in String
546 */
547 @Override
3b38ea61 548 @SuppressWarnings("nls")
0152140d
ASL
549 public String toString() {
550 String returnData = "";
551
552 returnData += "tracefilePtr : " + tracefilePtr + "\n";
553 returnData += "eventMarkerId : " + getEventMarkerId() + "\n";
554 returnData += "eventTime : " + eventTime.getReferenceToString() + "\n";
555 returnData += " seconds : " + eventTime.getSeconds() + "\n";
556 returnData += " nanoSeconds : " + eventTime.getNanoSeconds() + "\n";
557 returnData += "eventDataSize : " + getEventDataSize() + "\n";
558 returnData += "markersMap : " + markersMap.keySet() + "\n"; // Hack to avoid ending up with markersMap.toString()
559
560 return returnData;
561 }
84b2b1f0 562
0152140d 563}
This page took 0.075114 seconds and 5 git commands to generate.