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
.tracecompass
.internal
.ctf
.core
.event
;
15 import java
.util
.HashMap
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.ILexicalScope
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStream
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
32 * Representation of one type of event. A bit like "int" or "long" but for trace
35 public class EventDeclaration
implements IEventDeclaration
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
47 * Event context structure declaration
49 private StructDeclaration fContext
= null;
52 * Event fields structure declaration
54 private StructDeclaration fFields
= null;
57 * Stream to which belongs this event.
59 private CTFStream fStream
= null;
62 * Loglevel of an event
64 private long fLogLevel
;
66 /** Map of this event type's custom CTF attributes */
67 private final Map
<String
, String
> fCustomAttributes
= new HashMap
<>();
69 private int fId
= (int) UNSET_EVENT_ID
;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * Default constructor. Use the setters afterwards to set the fields
79 public EventDeclaration() {
83 * Creates an instance of EventDefinition corresponding to this declaration.
85 * @param streamInputReader
86 * The StreamInputReader for which this definition is created.
87 * @param eventHeaderDef
88 * The event header definition
90 * the bitbuffer input source
92 * The timestamp when the event was taken
93 * @return A new EventDefinition.
94 * @throws CTFException
95 * As a bitbuffer is used to read, it could have wrapped
98 public EventDefinition
createDefinition(CTFStreamInputReader streamInputReader
, ICompositeDefinition eventHeaderDef
, @NonNull BitBuffer input
, long timestamp
) throws CTFException
{
99 StructDeclaration streamEventContextDecl
= streamInputReader
.getStreamEventContextDecl();
100 StructDefinition streamEventContext
= streamEventContextDecl
!= null ? streamEventContextDecl
.createDefinition(fStream
.getTrace(), ILexicalScope
.STREAM_EVENT_CONTEXT
, input
) : null;
101 ICompositeDefinition packetContext
= streamInputReader
.getPacketReader().getCurrentPacketEventHeader();
102 StructDefinition eventContext
= fContext
!= null ? fContext
.createFieldDefinition(eventHeaderDef
, fStream
.getTrace(), ILexicalScope
.CONTEXT
, input
) : null;
103 StructDefinition eventPayload
= fFields
!= null ? fFields
.createFieldDefinition(eventHeaderDef
, fStream
.getTrace(), ILexicalScope
.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(
120 public EventDefinition
createDefinition(CTFStreamInputReader streamInputReader
, @NonNull BitBuffer input
, long timestamp
) throws CTFException
{
121 StructDeclaration streamEventContextDecl
= streamInputReader
.getStreamEventContextDecl();
122 StructDefinition streamEventContext
= streamEventContextDecl
!= null ? streamEventContextDecl
.createDefinition(fStream
.getTrace(), ILexicalScope
.STREAM_EVENT_CONTEXT
, input
) : null;
123 ICompositeDefinition packetContext
= streamInputReader
.getPacketReader().getCurrentPacketEventHeader();
124 StructDefinition eventContext
= fContext
!= null ? fContext
.createDefinition(fStream
.getTrace(), ILexicalScope
.CONTEXT
, input
) : null;
125 StructDefinition eventPayload
= fFields
!= null ? fFields
.createDefinition(fStream
.getTrace(), ILexicalScope
.FIELDS
, input
) : null;
127 // a bit lttng specific
128 // CTF doesn't require a timestamp,
129 // but it's passed to us
130 return new EventDefinition(
140 // ------------------------------------------------------------------------
141 // Getters/Setters/Predicates
142 // ------------------------------------------------------------------------
145 * Sets a name for an event Declaration
150 public void setName(String name
) {
155 public String
getName() {
160 * Sets the context for an event declaration (see CTF specification)
163 * the context in structdeclaration format
165 public void setContext(StructDeclaration context
) {
170 * Sets the fields of an event declaration
173 * the fields in structdeclaration format
175 public void setFields(StructDeclaration fields
) {
180 public StructDeclaration
getFields() {
185 public StructDeclaration
getContext() {
190 * Sets the id of an event declaration
195 public void setId(long id
) {
196 if (id
< 0 || id
> Integer
.MAX_VALUE
) {
197 throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$
203 public Long
getId() {
204 return Long
.valueOf(fId
);
208 * Faster get id assuming you have less than a billion event types
210 * @return the event id
217 * Sets the stream of an event declaration
222 public void setStream(CTFStream stream
) {
227 public CTFStream
getStream() {
232 * Is the name of the event declaration set
234 * @return is the name set?
236 public boolean nameIsSet() {
237 return fName
!= null;
243 * @return is the context set
245 public boolean contextIsSet() {
246 return fContext
!= null;
252 * @return Is the field set?
254 public boolean fieldsIsSet() {
255 return fFields
!= null;
261 * @return is the id set?
263 public boolean idIsSet() {
264 return (fId
!= UNSET_EVENT_ID
);
270 * @return is the stream set?
272 public boolean streamIsSet() {
273 return fStream
!= null;
277 public long getLogLevel() {
287 public void setLogLevel(long level
) {
292 public Set
<String
> getCustomAttributes() {
293 return fCustomAttributes
.keySet();
297 public String
getCustomAttribute(String key
) {
298 return fCustomAttributes
.get(key
);
302 * Sets a custom attribute value.
305 * the key of the attribute
307 * the value of the attribute
309 public void setCustomAttribute(String key
, String value
) {
310 fCustomAttributes
.put(key
, value
);
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
318 public boolean equals(Object obj
) {
325 if (!(obj
instanceof EventDeclaration
)) {
328 EventDeclaration other
= (EventDeclaration
) obj
;
329 if (fContext
== null) {
330 if (other
.fContext
!= null) {
333 } else if (!fContext
.equals(other
.fContext
)) {
336 if (fFields
== null) {
337 if (other
.fFields
!= null) {
340 } else if (!fFields
.equals(other
.fFields
)) {
343 if (fId
!= (other
.fId
)) {
347 if (other
.fName
!= null) {
350 } else if (!fName
.equals(other
.fName
)) {
353 if (fStream
== null) {
354 if (other
.fStream
!= null) {
357 } else if (!fStream
.equals(other
.fStream
)) {
360 if (!fCustomAttributes
.equals(other
.fCustomAttributes
)) {
367 public int hashCode() {
368 final int prime
= 31;
370 result
= (prime
* result
)
371 + ((fContext
== null) ?
0 : fContext
.hashCode());
372 result
= (prime
* result
) + ((fFields
== null) ?
0 : fFields
.hashCode());
373 result
= (prime
* result
) + fId
;
374 result
= (prime
* result
) + ((fName
== null) ?
0 : fName
.hashCode());
375 result
= (prime
* result
) + ((fStream
== null) ?
0 : fStream
.hashCode());
376 result
= (prime
* result
) + fCustomAttributes
.hashCode();