1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
;
15 import java
.util
.HashMap
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.CTFStrings
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.LexicalScope
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Declaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStream
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStreamInputReader
;
34 * Representation of one type of event. A bit like "int" or "long" but for trace
37 public class EventDeclaration
implements IEventDeclaration
{
39 /** Id of lost events */
40 public static final long LOST_EVENT_ID
= -1L;
42 /** Id of events when not set */
43 public static final long UNSET_EVENT_ID
= -2L;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
55 * Event context structure declaration
57 private StructDeclaration fContext
= null;
60 * Event fields structure declaration
62 private StructDeclaration fFields
= null;
65 * Event id (can be null if only event in the stream).
67 private Long fId
= UNSET_EVENT_ID
;
70 * Stream to which belongs this event.
72 private CTFStream fStream
= null;
75 * Loglevel of an event
77 private long fLogLevel
;
79 /** Map of this event type's custom CTF attributes */
80 private final Map
<String
, String
> fCustomAttributes
= new HashMap
<>();
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Default constructor. Use the setters afterwards to set the fields
90 public EventDeclaration() {
94 public EventDefinition
createDefinition(CTFStreamInputReader streamInputReader
, @NonNull BitBuffer input
, long timestamp
) throws CTFReaderException
{
95 StructDeclaration streamEventContextDecl
= streamInputReader
.getStreamEventContextDecl();
96 StructDefinition streamEventContext
= streamEventContextDecl
!= null ? streamEventContextDecl
.createDefinition(fStream
.getTrace(), LexicalScope
.STREAM_EVENT_CONTEXT
, input
) : null;
97 StructDefinition packetContext
= streamInputReader
.getPacketReader().getCurrentPacketEventHeader();
98 StructDefinition eventContext
= fContext
!= null ? fContext
.createDefinition(fStream
.getTrace(), LexicalScope
.CONTEXT
, input
) : null;
99 StructDefinition eventPayload
= fFields
!= null ? fFields
.createDefinition(fStream
.getTrace(), LexicalScope
.FIELDS
, input
) : null;
101 // a bit lttng specific
102 // CTF doesn't require a timestamp,
103 // but it's passed to us
104 return new EventDefinition(
115 * Creates a "lost" event. This is a synthetic event that is there to show
116 * that there should be something there.
118 * @return the lost event
120 public static synchronized EventDeclaration
getLostEventDeclaration() {
121 EventDeclaration lostEvent
= new EventDeclaration();
122 String
[] fieldNames
= new String
[] { CTFStrings
.LOST_EVENTS_FIELD
, CTFStrings
.LOST_EVENTS_DURATION
};
123 Declaration
[] fieldDeclarations
= new Declaration
[] { IntegerDeclaration
.UINT_32B_DECL
, IntegerDeclaration
.UINT_64B_DECL
};
124 lostEvent
.fFields
= new StructDeclaration(fieldNames
, fieldDeclarations
);
125 lostEvent
.fId
= LOST_EVENT_ID
;
126 lostEvent
.fName
= CTFStrings
.LOST_EVENT_NAME
;
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
135 * Sets a name for an event Declaration
140 public void setName(String name
) {
145 public String
getName() {
150 * Sets the context for an event declaration (see CTF specification)
153 * the context in structdeclaration format
155 public void setContext(StructDeclaration context
) {
160 * Sets the fields of an event declaration
163 * the fields in structdeclaration format
165 public void setFields(StructDeclaration fields
) {
170 public StructDeclaration
getFields() {
175 public StructDeclaration
getContext() {
180 * Sets the id of an event declaration
185 public void setId(long id
) {
190 public Long
getId() {
195 * Sets the stream of an event declaration
201 public void setStream(CTFStream stream
) {
206 public CTFStream
getStream() {
211 * Is the name of the event declaration set
213 * @return is the name set?
215 public boolean nameIsSet() {
216 return fName
!= null;
222 * @return is the context set
224 public boolean contextIsSet() {
225 return fContext
!= null;
231 * @return Is the field set?
233 public boolean fieldsIsSet() {
234 return fFields
!= null;
240 * @return is the id set?
242 public boolean idIsSet() {
243 return (fId
!= null && fId
!= UNSET_EVENT_ID
);
249 * @return is the stream set?
251 public boolean streamIsSet() {
252 return fStream
!= null;
256 public long getLogLevel() {
266 public void setLogLevel(long level
) {
271 public Set
<String
> getCustomAttributes() {
272 return fCustomAttributes
.keySet();
276 public String
getCustomAttribute(String key
) {
277 return fCustomAttributes
.get(key
);
281 * Sets a custom attribute value.
284 * the key of the attribute
286 * the value of the attribute
289 public void setCustomAttribute(String key
, String value
) {
290 fCustomAttributes
.put(key
, value
);
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 public boolean equals(Object obj
) {
305 if (!(obj
instanceof EventDeclaration
)) {
308 EventDeclaration other
= (EventDeclaration
) obj
;
309 if (fContext
== null) {
310 if (other
.fContext
!= null) {
313 } else if (!fContext
.equals(other
.fContext
)) {
316 if (fFields
== null) {
317 if (other
.fFields
!= null) {
320 } else if (!fFields
.equals(other
.fFields
)) {
324 if (other
.fId
!= null) {
327 } else if (!fId
.equals(other
.fId
)) {
331 if (other
.fName
!= null) {
334 } else if (!fName
.equals(other
.fName
)) {
337 if (fStream
== null) {
338 if (other
.fStream
!= null) {
341 } else if (!fStream
.equals(other
.fStream
)) {
344 if (!fCustomAttributes
.equals(other
.fCustomAttributes
)) {
351 public int hashCode() {
352 final int prime
= 31;
354 result
= (prime
* result
)
355 + ((fContext
== null) ?
0 : fContext
.hashCode());
356 result
= (prime
* result
) + ((fFields
== null) ?
0 : fFields
.hashCode());
357 result
= (prime
* result
) + ((fId
== null) ?
0 : fId
.hashCode());
358 result
= (prime
* result
) + ((fName
== null) ?
0 : fName
.hashCode());
359 result
= (prime
* result
) + ((fStream
== null) ?
0 : fStream
.hashCode());
360 result
= (prime
* result
) + fCustomAttributes
.hashCode();