Commit | Line | Data |
---|---|---|
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 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.internal.ctf.core.event; |
866e5b51 | 14 | |
8e964be1 MK |
15 | import java.util.HashMap; |
16 | import java.util.Map; | |
17 | import java.util.Set; | |
18 | ||
a4fa4e36 | 19 | import org.eclipse.jdt.annotation.NonNull; |
680f9173 | 20 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
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; | |
fbe6fa6f | 24 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
778bce67 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; |
f357bcd4 AM |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; |
27 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
f357bcd4 AM |
28 | import org.eclipse.tracecompass.ctf.core.trace.CTFStream; |
29 | import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader; | |
866e5b51 FC |
30 | |
31 | /** | |
be6df2d8 AM |
32 | * Representation of one type of event. A bit like "int" or "long" but for trace |
33 | * events. | |
866e5b51 | 34 | */ |
8e964be1 | 35 | public class EventDeclaration implements IEventDeclaration { |
866e5b51 FC |
36 | |
37 | // ------------------------------------------------------------------------ | |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * Name of the event | |
43 | */ | |
75259c16 | 44 | private String fName; |
866e5b51 FC |
45 | |
46 | /** | |
47 | * Event context structure declaration | |
48 | */ | |
75259c16 | 49 | private StructDeclaration fContext = null; |
866e5b51 FC |
50 | |
51 | /** | |
52 | * Event fields structure declaration | |
53 | */ | |
75259c16 | 54 | private StructDeclaration fFields = null; |
866e5b51 | 55 | |
866e5b51 FC |
56 | /** |
57 | * Stream to which belongs this event. | |
58 | */ | |
d84419e1 | 59 | private CTFStream fStream = null; |
866e5b51 | 60 | |
53047a66 MK |
61 | /** |
62 | * Loglevel of an event | |
63 | */ | |
75259c16 | 64 | private long fLogLevel; |
53047a66 | 65 | |
8e964be1 | 66 | /** Map of this event type's custom CTF attributes */ |
75259c16 | 67 | private final Map<String, String> fCustomAttributes = new HashMap<>(); |
8e964be1 | 68 | |
5f715709 MK |
69 | private int fId = (int) UNSET_EVENT_ID; |
70 | ||
866e5b51 FC |
71 | // ------------------------------------------------------------------------ |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
be6df2d8 AM |
75 | /** |
76 | * Default constructor. Use the setters afterwards to set the fields | |
77 | * accordingly. | |
78 | */ | |
8e964be1 MK |
79 | public EventDeclaration() { |
80 | } | |
be6df2d8 | 81 | |
94c255ef MK |
82 | /** |
83 | * Creates an instance of EventDefinition corresponding to this declaration. | |
84 | * | |
85 | * @param streamInputReader | |
86 | * The StreamInputReader for which this definition is created. | |
87 | * @param eventHeaderDef | |
88 | * The event header definition | |
89 | * @param input | |
90 | * the bitbuffer input source | |
91 | * @param timestamp | |
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 | |
96 | * IOExceptions. | |
97 | */ | |
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; | |
104 | ||
105 | // a bit lttng specific | |
106 | // CTF doesn't require a timestamp, | |
107 | // but it's passed to us | |
108 | return new EventDefinition( | |
109 | this, | |
110 | streamInputReader, | |
111 | timestamp, | |
112 | eventHeaderDef, | |
113 | streamEventContext, | |
114 | eventContext, | |
115 | packetContext, | |
116 | eventPayload); | |
117 | } | |
118 | ||
8e964be1 | 119 | @Override |
680f9173 | 120 | public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFException { |
a4fa4e36 | 121 | StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl(); |
fbe6fa6f | 122 | StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), ILexicalScope.STREAM_EVENT_CONTEXT, input) : null; |
778bce67 | 123 | ICompositeDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader(); |
fbe6fa6f MK |
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; | |
a4fa4e36 MK |
126 | |
127 | // a bit lttng specific | |
128 | // CTF doesn't require a timestamp, | |
129 | // but it's passed to us | |
130 | return new EventDefinition( | |
131 | this, | |
132 | streamInputReader, | |
133 | timestamp, | |
134 | streamEventContext, | |
135 | eventContext, | |
136 | packetContext, | |
137 | eventPayload); | |
866e5b51 FC |
138 | } |
139 | ||
140 | // ------------------------------------------------------------------------ | |
141 | // Getters/Setters/Predicates | |
142 | // ------------------------------------------------------------------------ | |
143 | ||
9ac2eb62 MK |
144 | /** |
145 | * Sets a name for an event Declaration | |
8e964be1 MK |
146 | * |
147 | * @param name | |
148 | * the name | |
9ac2eb62 | 149 | */ |
866e5b51 | 150 | public void setName(String name) { |
75259c16 | 151 | fName = name; |
866e5b51 FC |
152 | } |
153 | ||
8e964be1 | 154 | @Override |
866e5b51 | 155 | public String getName() { |
75259c16 | 156 | return fName; |
866e5b51 FC |
157 | } |
158 | ||
9ac2eb62 MK |
159 | /** |
160 | * Sets the context for an event declaration (see CTF specification) | |
8e964be1 MK |
161 | * |
162 | * @param context | |
163 | * the context in structdeclaration format | |
9ac2eb62 | 164 | */ |
866e5b51 | 165 | public void setContext(StructDeclaration context) { |
75259c16 | 166 | fContext = context; |
866e5b51 FC |
167 | } |
168 | ||
9ac2eb62 MK |
169 | /** |
170 | * Sets the fields of an event declaration | |
8e964be1 MK |
171 | * |
172 | * @param fields | |
173 | * the fields in structdeclaration format | |
9ac2eb62 | 174 | */ |
866e5b51 | 175 | public void setFields(StructDeclaration fields) { |
75259c16 | 176 | fFields = fields; |
866e5b51 FC |
177 | } |
178 | ||
8e964be1 | 179 | @Override |
866e5b51 | 180 | public StructDeclaration getFields() { |
75259c16 | 181 | return fFields; |
866e5b51 FC |
182 | } |
183 | ||
8e964be1 | 184 | @Override |
866e5b51 | 185 | public StructDeclaration getContext() { |
75259c16 | 186 | return fContext; |
866e5b51 FC |
187 | } |
188 | ||
9ac2eb62 | 189 | /** |
ecb12461 | 190 | * Sets the id of an event declaration |
8e964be1 MK |
191 | * |
192 | * @param id | |
193 | * the id | |
9ac2eb62 | 194 | */ |
866e5b51 | 195 | public void setId(long id) { |
5f715709 MK |
196 | if (id < 0 || id > Integer.MAX_VALUE) { |
197 | throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$ | |
198 | } | |
199 | fId = (int) id; | |
866e5b51 FC |
200 | } |
201 | ||
8e964be1 | 202 | @Override |
866e5b51 | 203 | public Long getId() { |
5f715709 MK |
204 | return Long.valueOf(fId); |
205 | } | |
206 | ||
207 | /** | |
208 | * Faster get id assuming you have less than a billion event types | |
209 | * | |
210 | * @return the event id | |
211 | */ | |
212 | public int id() { | |
75259c16 | 213 | return fId; |
866e5b51 FC |
214 | } |
215 | ||
9ac2eb62 | 216 | /** |
ecb12461 | 217 | * Sets the stream of an event declaration |
8e964be1 MK |
218 | * |
219 | * @param stream | |
220 | * the stream | |
9ac2eb62 | 221 | */ |
d84419e1 | 222 | public void setStream(CTFStream stream) { |
75259c16 | 223 | fStream = stream; |
866e5b51 FC |
224 | } |
225 | ||
8e964be1 | 226 | @Override |
d84419e1 | 227 | public CTFStream getStream() { |
75259c16 | 228 | return fStream; |
866e5b51 FC |
229 | } |
230 | ||
9ac2eb62 MK |
231 | /** |
232 | * Is the name of the event declaration set | |
8e964be1 | 233 | * |
9ac2eb62 MK |
234 | * @return is the name set? |
235 | */ | |
866e5b51 | 236 | public boolean nameIsSet() { |
75259c16 | 237 | return fName != null; |
866e5b51 FC |
238 | } |
239 | ||
9ac2eb62 MK |
240 | /** |
241 | * Is the context set | |
8e964be1 | 242 | * |
9ac2eb62 MK |
243 | * @return is the context set |
244 | */ | |
866e5b51 | 245 | public boolean contextIsSet() { |
75259c16 | 246 | return fContext != null; |
866e5b51 FC |
247 | } |
248 | ||
9ac2eb62 MK |
249 | /** |
250 | * Is a field set? | |
8e964be1 | 251 | * |
9ac2eb62 MK |
252 | * @return Is the field set? |
253 | */ | |
866e5b51 | 254 | public boolean fieldsIsSet() { |
75259c16 | 255 | return fFields != null; |
866e5b51 FC |
256 | } |
257 | ||
9ac2eb62 MK |
258 | /** |
259 | * Is the id set? | |
8e964be1 | 260 | * |
9ac2eb62 MK |
261 | * @return is the id set? |
262 | */ | |
866e5b51 | 263 | public boolean idIsSet() { |
8e0c9d81 | 264 | return (fId != UNSET_EVENT_ID); |
866e5b51 FC |
265 | } |
266 | ||
9ac2eb62 MK |
267 | /** |
268 | * Is the stream set? | |
8e964be1 | 269 | * |
9ac2eb62 MK |
270 | * @return is the stream set? |
271 | */ | |
866e5b51 | 272 | public boolean streamIsSet() { |
75259c16 | 273 | return fStream != null; |
866e5b51 FC |
274 | } |
275 | ||
8e964be1 | 276 | @Override |
53047a66 | 277 | public long getLogLevel() { |
75259c16 | 278 | return fLogLevel; |
53047a66 MK |
279 | } |
280 | ||
9ac2eb62 MK |
281 | /** |
282 | * Sets the log level | |
8e964be1 MK |
283 | * |
284 | * @param level | |
285 | * the log level | |
9ac2eb62 | 286 | */ |
8e964be1 | 287 | public void setLogLevel(long level) { |
75259c16 | 288 | fLogLevel = level; |
53047a66 MK |
289 | } |
290 | ||
8e964be1 MK |
291 | @Override |
292 | public Set<String> getCustomAttributes() { | |
75259c16 | 293 | return fCustomAttributes.keySet(); |
8e964be1 MK |
294 | } |
295 | ||
296 | @Override | |
297 | public String getCustomAttribute(String key) { | |
75259c16 | 298 | return fCustomAttributes.get(key); |
8e964be1 MK |
299 | } |
300 | ||
301 | /** | |
302 | * Sets a custom attribute value. | |
303 | * | |
304 | * @param key | |
305 | * the key of the attribute | |
306 | * @param value | |
307 | * the value of the attribute | |
8e964be1 MK |
308 | */ |
309 | public void setCustomAttribute(String key, String value) { | |
75259c16 | 310 | fCustomAttributes.put(key, value); |
8e964be1 MK |
311 | } |
312 | ||
866e5b51 FC |
313 | // ------------------------------------------------------------------------ |
314 | // Operations | |
315 | // ------------------------------------------------------------------------ | |
316 | ||
317 | @Override | |
318 | public boolean equals(Object obj) { | |
319 | if (this == obj) { | |
320 | return true; | |
321 | } | |
322 | if (obj == null) { | |
323 | return false; | |
324 | } | |
325 | if (!(obj instanceof EventDeclaration)) { | |
326 | return false; | |
327 | } | |
328 | EventDeclaration other = (EventDeclaration) obj; | |
75259c16 MK |
329 | if (fContext == null) { |
330 | if (other.fContext != null) { | |
866e5b51 FC |
331 | return false; |
332 | } | |
75259c16 | 333 | } else if (!fContext.equals(other.fContext)) { |
866e5b51 FC |
334 | return false; |
335 | } | |
75259c16 MK |
336 | if (fFields == null) { |
337 | if (other.fFields != null) { | |
866e5b51 FC |
338 | return false; |
339 | } | |
75259c16 | 340 | } else if (!fFields.equals(other.fFields)) { |
866e5b51 FC |
341 | return false; |
342 | } | |
5f715709 | 343 | if (fId != (other.fId)) { |
866e5b51 FC |
344 | return false; |
345 | } | |
75259c16 MK |
346 | if (fName == null) { |
347 | if (other.fName != null) { | |
866e5b51 FC |
348 | return false; |
349 | } | |
75259c16 | 350 | } else if (!fName.equals(other.fName)) { |
866e5b51 FC |
351 | return false; |
352 | } | |
75259c16 MK |
353 | if (fStream == null) { |
354 | if (other.fStream != null) { | |
866e5b51 FC |
355 | return false; |
356 | } | |
75259c16 | 357 | } else if (!fStream.equals(other.fStream)) { |
866e5b51 FC |
358 | return false; |
359 | } | |
75259c16 | 360 | if (!fCustomAttributes.equals(other.fCustomAttributes)) { |
8e964be1 MK |
361 | return false; |
362 | } | |
866e5b51 FC |
363 | return true; |
364 | } | |
365 | ||
366 | @Override | |
367 | public int hashCode() { | |
368 | final int prime = 31; | |
369 | int result = 1; | |
370 | result = (prime * result) | |
75259c16 MK |
371 | + ((fContext == null) ? 0 : fContext.hashCode()); |
372 | result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode()); | |
5f715709 | 373 | result = (prime * result) + fId; |
75259c16 MK |
374 | result = (prime * result) + ((fName == null) ? 0 : fName.hashCode()); |
375 | result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode()); | |
376 | result = (prime * result) + fCustomAttributes.hashCode(); | |
866e5b51 FC |
377 | return result; |
378 | } | |
379 | ||
380 | } |