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
.Stream
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInputReader
;
34 * Representation of one type of event. A bit like "int" or "long" but for trace
37 public class EventDeclaration
implements IEventDeclaration
{
39 @NonNull private static final String FIELDS
= "fields"; //$NON-NLS-1$
41 @NonNull private static final String CONTEXT
= "context"; //$NON-NLS-1$
43 /** Id of lost events */
44 public static final long LOST_EVENT_ID
= -1L;
46 /** Id of events when not set */
47 public static final long UNSET_EVENT_ID
= -2L;
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
59 * Event context structure declaration
61 private StructDeclaration fContext
= null;
64 * Event fields structure declaration
66 private StructDeclaration fFields
= null;
69 * Event id (can be null if only event in the stream).
71 private Long fId
= UNSET_EVENT_ID
;
74 * Stream to which belongs this event.
76 private Stream fStream
= null;
79 * Loglevel of an event
81 private long fLogLevel
;
83 /** Map of this event type's custom CTF attributes */
84 private final Map
<String
, String
> fCustomAttributes
= new HashMap
<>();
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * Default constructor. Use the setters afterwards to set the fields
94 public EventDeclaration() {
98 public EventDefinition
createDefinition(StreamInputReader streamInputReader
, @NonNull BitBuffer input
, long timestamp
) throws CTFReaderException
{
99 StructDeclaration streamEventContextDecl
= streamInputReader
.getStreamEventContextDecl();
100 StructDefinition streamEventContext
= streamEventContextDecl
!= null ? streamEventContextDecl
.createDefinition(null, LexicalScope
.STREAM_EVENT_CONTEXT
.toString(), input
) : null;
101 StructDefinition packetContext
= streamInputReader
.getPacketReader().getCurrentPacketEventHeader();
102 StructDefinition eventContext
= fContext
!= null ? fContext
.createDefinition(null, CONTEXT
, input
) : null;
103 StructDefinition eventPayload
= fFields
!= null ? fFields
.createDefinition(null, FIELDS
, input
) : null;
105 // a bit lttng specific
106 // CTF doesn't require a timestamp,
107 // but it's passed to us
108 return new EventDefinition(
119 * Creates a "lost" event. This is a synthetic event that is there to show
120 * that there should be something there.
122 * @return the lost event
124 public static synchronized EventDeclaration
getLostEventDeclaration() {
125 EventDeclaration lostEvent
= new EventDeclaration();
126 String
[] fieldNames
= new String
[] { CTFStrings
.LOST_EVENTS_FIELD
, CTFStrings
.LOST_EVENTS_DURATION
};
127 Declaration
[] fieldDeclarations
= new Declaration
[] { IntegerDeclaration
.UINT_32B_DECL
, IntegerDeclaration
.UINT_64B_DECL
};
128 lostEvent
.fFields
= new StructDeclaration(fieldNames
, fieldDeclarations
);
129 lostEvent
.fId
= LOST_EVENT_ID
;
130 lostEvent
.fName
= CTFStrings
.LOST_EVENT_NAME
;
134 // ------------------------------------------------------------------------
135 // Getters/Setters/Predicates
136 // ------------------------------------------------------------------------
139 * Sets a name for an event Declaration
144 public void setName(String name
) {
149 public String
getName() {
154 * Sets the context for an event declaration (see CTF specification)
157 * the context in structdeclaration format
159 public void setContext(StructDeclaration context
) {
164 * Sets the fields of an event declaration
167 * the fields in structdeclaration format
169 public void setFields(StructDeclaration fields
) {
174 public StructDeclaration
getFields() {
179 public StructDeclaration
getContext() {
184 * Sets the id of an event declaration
189 public void setId(long id
) {
194 public Long
getId() {
199 * Sets the stream of an event declaration
205 public void setStream(Stream stream
) {
210 public Stream
getStream() {
215 * Is the name of the event declaration set
217 * @return is the name set?
219 public boolean nameIsSet() {
220 return fName
!= null;
226 * @return is the context set
228 public boolean contextIsSet() {
229 return fContext
!= null;
235 * @return Is the field set?
237 public boolean fieldsIsSet() {
238 return fFields
!= null;
244 * @return is the id set?
246 public boolean idIsSet() {
247 return (fId
!= null && fId
!= UNSET_EVENT_ID
);
253 * @return is the stream set?
255 public boolean streamIsSet() {
256 return fStream
!= null;
260 public long getLogLevel() {
270 public void setLogLevel(long level
) {
275 public Set
<String
> getCustomAttributes() {
276 return fCustomAttributes
.keySet();
280 public String
getCustomAttribute(String key
) {
281 return fCustomAttributes
.get(key
);
285 * Sets a custom attribute value.
288 * the key of the attribute
290 * the value of the attribute
293 public void setCustomAttribute(String key
, String value
) {
294 fCustomAttributes
.put(key
, value
);
297 // ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
302 public boolean equals(Object obj
) {
309 if (!(obj
instanceof EventDeclaration
)) {
312 EventDeclaration other
= (EventDeclaration
) obj
;
313 if (fContext
== null) {
314 if (other
.fContext
!= null) {
317 } else if (!fContext
.equals(other
.fContext
)) {
320 if (fFields
== null) {
321 if (other
.fFields
!= null) {
324 } else if (!fFields
.equals(other
.fFields
)) {
328 if (other
.fId
!= null) {
331 } else if (!fId
.equals(other
.fId
)) {
335 if (other
.fName
!= null) {
338 } else if (!fName
.equals(other
.fName
)) {
341 if (fStream
== null) {
342 if (other
.fStream
!= null) {
345 } else if (!fStream
.equals(other
.fStream
)) {
348 if (!fCustomAttributes
.equals(other
.fCustomAttributes
)) {
355 public int hashCode() {
356 final int prime
= 31;
358 result
= (prime
* result
)
359 + ((fContext
== null) ?
0 : fContext
.hashCode());
360 result
= (prime
* result
) + ((fFields
== null) ?
0 : fFields
.hashCode());
361 result
= (prime
* result
) + ((fId
== null) ?
0 : fId
.hashCode());
362 result
= (prime
* result
) + ((fName
== null) ?
0 : fName
.hashCode());
363 result
= (prime
* result
) + ((fStream
== null) ?
0 : fStream
.hashCode());
364 result
= (prime
* result
) + fCustomAttributes
.hashCode();