ctf: accelerate n-grams for ctf scopes
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / EventDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
8e964be1 13package org.eclipse.linuxtools.internal.ctf.core.event;
866e5b51 14
8e964be1
MK
15import java.util.HashMap;
16import java.util.Map;
17import java.util.Set;
18
a4fa4e36 19import org.eclipse.jdt.annotation.NonNull;
c26d0fe0 20import org.eclipse.linuxtools.ctf.core.CTFStrings;
8e964be1
MK
21import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
22import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
a4fa4e36
MK
23import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
24import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
25import org.eclipse.linuxtools.ctf.core.event.types.Declaration;
c26d0fe0 26import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
866e5b51 27import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
a4fa4e36
MK
28import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
29import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
d84419e1
AM
30import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
31import org.eclipse.linuxtools.ctf.core.trace.CTFStreamInputReader;
866e5b51
FC
32
33/**
be6df2d8
AM
34 * Representation of one type of event. A bit like "int" or "long" but for trace
35 * events.
866e5b51 36 */
8e964be1 37public class EventDeclaration implements IEventDeclaration {
866e5b51 38
b73145e2
JCK
39 /** Id of lost events */
40 public static final long LOST_EVENT_ID = -1L;
41
42 /** Id of events when not set */
43 public static final long UNSET_EVENT_ID = -2L;
44
866e5b51
FC
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
49 /**
50 * Name of the event
51 */
75259c16 52 private String fName;
866e5b51
FC
53
54 /**
55 * Event context structure declaration
56 */
75259c16 57 private StructDeclaration fContext = null;
866e5b51
FC
58
59 /**
60 * Event fields structure declaration
61 */
75259c16 62 private StructDeclaration fFields = null;
866e5b51
FC
63
64 /**
65 * Event id (can be null if only event in the stream).
66 */
75259c16 67 private Long fId = UNSET_EVENT_ID;
866e5b51
FC
68
69 /**
70 * Stream to which belongs this event.
71 */
d84419e1 72 private CTFStream fStream = null;
866e5b51 73
53047a66
MK
74 /**
75 * Loglevel of an event
76 */
75259c16 77 private long fLogLevel;
53047a66 78
8e964be1 79 /** Map of this event type's custom CTF attributes */
75259c16 80 private final Map<String, String> fCustomAttributes = new HashMap<>();
8e964be1 81
866e5b51
FC
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85
be6df2d8
AM
86 /**
87 * Default constructor. Use the setters afterwards to set the fields
88 * accordingly.
89 */
8e964be1
MK
90 public EventDeclaration() {
91 }
be6df2d8 92
8e964be1 93 @Override
d84419e1 94 public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException {
a4fa4e36 95 StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl();
70f60307 96 StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), LexicalScope.STREAM_EVENT_CONTEXT, input) : null;
a4fa4e36 97 StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader();
70f60307
MK
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;
a4fa4e36
MK
100
101 // a bit lttng specific
102 // CTF doesn't require a timestamp,
103 // but it's passed to us
104 return new EventDefinition(
105 this,
106 streamInputReader,
107 timestamp,
108 streamEventContext,
109 eventContext,
110 packetContext,
111 eventPayload);
866e5b51
FC
112 }
113
33656d8e
MK
114 /**
115 * Creates a "lost" event. This is a synthetic event that is there to show
116 * that there should be something there.
8e964be1 117 *
9ac2eb62 118 * @return the lost event
33656d8e 119 */
0594c61c 120 public static synchronized EventDeclaration getLostEventDeclaration() {
debcffff 121 EventDeclaration lostEvent = new EventDeclaration();
a4fa4e36
MK
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);
75259c16
MK
125 lostEvent.fId = LOST_EVENT_ID;
126 lostEvent.fName = CTFStrings.LOST_EVENT_NAME;
33656d8e
MK
127 return lostEvent;
128 }
129
866e5b51
FC
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
133
9ac2eb62
MK
134 /**
135 * Sets a name for an event Declaration
8e964be1
MK
136 *
137 * @param name
138 * the name
9ac2eb62 139 */
866e5b51 140 public void setName(String name) {
75259c16 141 fName = name;
866e5b51
FC
142 }
143
8e964be1 144 @Override
866e5b51 145 public String getName() {
75259c16 146 return fName;
866e5b51
FC
147 }
148
9ac2eb62
MK
149 /**
150 * Sets the context for an event declaration (see CTF specification)
8e964be1
MK
151 *
152 * @param context
153 * the context in structdeclaration format
9ac2eb62 154 */
866e5b51 155 public void setContext(StructDeclaration context) {
75259c16 156 fContext = context;
866e5b51
FC
157 }
158
9ac2eb62
MK
159 /**
160 * Sets the fields of an event declaration
8e964be1
MK
161 *
162 * @param fields
163 * the fields in structdeclaration format
9ac2eb62 164 */
866e5b51 165 public void setFields(StructDeclaration fields) {
75259c16 166 fFields = fields;
866e5b51
FC
167 }
168
8e964be1 169 @Override
866e5b51 170 public StructDeclaration getFields() {
75259c16 171 return fFields;
866e5b51
FC
172 }
173
8e964be1 174 @Override
866e5b51 175 public StructDeclaration getContext() {
75259c16 176 return fContext;
866e5b51
FC
177 }
178
9ac2eb62 179 /**
ecb12461 180 * Sets the id of an event declaration
8e964be1
MK
181 *
182 * @param id
183 * the id
9ac2eb62 184 */
866e5b51 185 public void setId(long id) {
75259c16 186 fId = id;
866e5b51
FC
187 }
188
8e964be1 189 @Override
866e5b51 190 public Long getId() {
75259c16 191 return fId;
866e5b51
FC
192 }
193
9ac2eb62 194 /**
ecb12461 195 * Sets the stream of an event declaration
8e964be1
MK
196 *
197 * @param stream
198 * the stream
486efb2e 199 * @since 2.0
9ac2eb62 200 */
d84419e1 201 public void setStream(CTFStream stream) {
75259c16 202 fStream = stream;
866e5b51
FC
203 }
204
8e964be1 205 @Override
d84419e1 206 public CTFStream getStream() {
75259c16 207 return fStream;
866e5b51
FC
208 }
209
9ac2eb62
MK
210 /**
211 * Is the name of the event declaration set
8e964be1 212 *
9ac2eb62
MK
213 * @return is the name set?
214 */
866e5b51 215 public boolean nameIsSet() {
75259c16 216 return fName != null;
866e5b51
FC
217 }
218
9ac2eb62
MK
219 /**
220 * Is the context set
8e964be1 221 *
9ac2eb62
MK
222 * @return is the context set
223 */
866e5b51 224 public boolean contextIsSet() {
75259c16 225 return fContext != null;
866e5b51
FC
226 }
227
9ac2eb62
MK
228 /**
229 * Is a field set?
8e964be1 230 *
9ac2eb62
MK
231 * @return Is the field set?
232 */
866e5b51 233 public boolean fieldsIsSet() {
75259c16 234 return fFields != null;
866e5b51
FC
235 }
236
9ac2eb62
MK
237 /**
238 * Is the id set?
8e964be1 239 *
9ac2eb62
MK
240 * @return is the id set?
241 */
866e5b51 242 public boolean idIsSet() {
75259c16 243 return (fId != null && fId != UNSET_EVENT_ID);
866e5b51
FC
244 }
245
9ac2eb62
MK
246 /**
247 * Is the stream set?
8e964be1 248 *
9ac2eb62
MK
249 * @return is the stream set?
250 */
866e5b51 251 public boolean streamIsSet() {
75259c16 252 return fStream != null;
866e5b51
FC
253 }
254
8e964be1 255 @Override
53047a66 256 public long getLogLevel() {
75259c16 257 return fLogLevel;
53047a66
MK
258 }
259
9ac2eb62
MK
260 /**
261 * Sets the log level
8e964be1
MK
262 *
263 * @param level
264 * the log level
9ac2eb62 265 */
8e964be1 266 public void setLogLevel(long level) {
75259c16 267 fLogLevel = level;
53047a66
MK
268 }
269
8e964be1
MK
270 @Override
271 public Set<String> getCustomAttributes() {
75259c16 272 return fCustomAttributes.keySet();
8e964be1
MK
273 }
274
275 @Override
276 public String getCustomAttribute(String key) {
75259c16 277 return fCustomAttributes.get(key);
8e964be1
MK
278 }
279
280 /**
281 * Sets a custom attribute value.
282 *
283 * @param key
284 * the key of the attribute
285 * @param value
286 * the value of the attribute
287 * @since 2.0
288 */
289 public void setCustomAttribute(String key, String value) {
75259c16 290 fCustomAttributes.put(key, value);
8e964be1
MK
291 }
292
866e5b51
FC
293 // ------------------------------------------------------------------------
294 // Operations
295 // ------------------------------------------------------------------------
296
297 @Override
298 public boolean equals(Object obj) {
299 if (this == obj) {
300 return true;
301 }
302 if (obj == null) {
303 return false;
304 }
305 if (!(obj instanceof EventDeclaration)) {
306 return false;
307 }
308 EventDeclaration other = (EventDeclaration) obj;
75259c16
MK
309 if (fContext == null) {
310 if (other.fContext != null) {
866e5b51
FC
311 return false;
312 }
75259c16 313 } else if (!fContext.equals(other.fContext)) {
866e5b51
FC
314 return false;
315 }
75259c16
MK
316 if (fFields == null) {
317 if (other.fFields != null) {
866e5b51
FC
318 return false;
319 }
75259c16 320 } else if (!fFields.equals(other.fFields)) {
866e5b51
FC
321 return false;
322 }
75259c16
MK
323 if (fId == null) {
324 if (other.fId != null) {
866e5b51
FC
325 return false;
326 }
75259c16 327 } else if (!fId.equals(other.fId)) {
866e5b51
FC
328 return false;
329 }
75259c16
MK
330 if (fName == null) {
331 if (other.fName != null) {
866e5b51
FC
332 return false;
333 }
75259c16 334 } else if (!fName.equals(other.fName)) {
866e5b51
FC
335 return false;
336 }
75259c16
MK
337 if (fStream == null) {
338 if (other.fStream != null) {
866e5b51
FC
339 return false;
340 }
75259c16 341 } else if (!fStream.equals(other.fStream)) {
866e5b51
FC
342 return false;
343 }
75259c16 344 if (!fCustomAttributes.equals(other.fCustomAttributes)) {
8e964be1
MK
345 return false;
346 }
866e5b51
FC
347 return true;
348 }
349
350 @Override
351 public int hashCode() {
352 final int prime = 31;
353 int result = 1;
354 result = (prime * result)
75259c16
MK
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();
866e5b51
FC
361 return result;
362 }
363
364}
This page took 0.061518 seconds and 5 git commands to generate.