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 | ||
8e964be1 | 13 | package org.eclipse.linuxtools.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; |
c26d0fe0 | 20 | import org.eclipse.linuxtools.ctf.core.CTFStrings; |
8e964be1 MK |
21 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
22 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; | |
a4fa4e36 MK |
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; | |
c26d0fe0 | 26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
866e5b51 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
a4fa4e36 MK |
28 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
29 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
d84419e1 AM |
30 | import org.eclipse.linuxtools.ctf.core.trace.CTFStream; |
31 | import 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 | 37 | public 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 | 63 | |
866e5b51 FC |
64 | /** |
65 | * Stream to which belongs this event. | |
66 | */ | |
d84419e1 | 67 | private CTFStream fStream = null; |
866e5b51 | 68 | |
53047a66 MK |
69 | /** |
70 | * Loglevel of an event | |
71 | */ | |
75259c16 | 72 | private long fLogLevel; |
53047a66 | 73 | |
8e964be1 | 74 | /** Map of this event type's custom CTF attributes */ |
75259c16 | 75 | private final Map<String, String> fCustomAttributes = new HashMap<>(); |
8e964be1 | 76 | |
5f715709 MK |
77 | private int fId = (int) UNSET_EVENT_ID; |
78 | ||
866e5b51 FC |
79 | // ------------------------------------------------------------------------ |
80 | // Constructors | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
be6df2d8 AM |
83 | /** |
84 | * Default constructor. Use the setters afterwards to set the fields | |
85 | * accordingly. | |
86 | */ | |
8e964be1 MK |
87 | public EventDeclaration() { |
88 | } | |
be6df2d8 | 89 | |
8e964be1 | 90 | @Override |
d84419e1 | 91 | public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException { |
a4fa4e36 | 92 | StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl(); |
70f60307 | 93 | StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), LexicalScope.STREAM_EVENT_CONTEXT, input) : null; |
a4fa4e36 | 94 | StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader(); |
70f60307 MK |
95 | StructDefinition eventContext = fContext != null ? fContext.createDefinition(fStream.getTrace(), LexicalScope.CONTEXT, input) : null; |
96 | StructDefinition eventPayload = fFields != null ? fFields.createDefinition(fStream.getTrace(), LexicalScope.FIELDS, input) : null; | |
a4fa4e36 MK |
97 | |
98 | // a bit lttng specific | |
99 | // CTF doesn't require a timestamp, | |
100 | // but it's passed to us | |
101 | return new EventDefinition( | |
102 | this, | |
103 | streamInputReader, | |
104 | timestamp, | |
105 | streamEventContext, | |
106 | eventContext, | |
107 | packetContext, | |
108 | eventPayload); | |
866e5b51 FC |
109 | } |
110 | ||
33656d8e MK |
111 | /** |
112 | * Creates a "lost" event. This is a synthetic event that is there to show | |
113 | * that there should be something there. | |
8e964be1 | 114 | * |
9ac2eb62 | 115 | * @return the lost event |
33656d8e | 116 | */ |
0594c61c | 117 | public static synchronized EventDeclaration getLostEventDeclaration() { |
debcffff | 118 | EventDeclaration lostEvent = new EventDeclaration(); |
a4fa4e36 MK |
119 | String[] fieldNames = new String[] { CTFStrings.LOST_EVENTS_FIELD, CTFStrings.LOST_EVENTS_DURATION }; |
120 | Declaration[] fieldDeclarations = new Declaration[] { IntegerDeclaration.UINT_32B_DECL, IntegerDeclaration.UINT_64B_DECL }; | |
121 | lostEvent.fFields = new StructDeclaration(fieldNames, fieldDeclarations); | |
5f715709 | 122 | lostEvent.fId = (int) LOST_EVENT_ID; |
75259c16 | 123 | lostEvent.fName = CTFStrings.LOST_EVENT_NAME; |
33656d8e MK |
124 | return lostEvent; |
125 | } | |
126 | ||
866e5b51 FC |
127 | // ------------------------------------------------------------------------ |
128 | // Getters/Setters/Predicates | |
129 | // ------------------------------------------------------------------------ | |
130 | ||
9ac2eb62 MK |
131 | /** |
132 | * Sets a name for an event Declaration | |
8e964be1 MK |
133 | * |
134 | * @param name | |
135 | * the name | |
9ac2eb62 | 136 | */ |
866e5b51 | 137 | public void setName(String name) { |
75259c16 | 138 | fName = name; |
866e5b51 FC |
139 | } |
140 | ||
8e964be1 | 141 | @Override |
866e5b51 | 142 | public String getName() { |
75259c16 | 143 | return fName; |
866e5b51 FC |
144 | } |
145 | ||
9ac2eb62 MK |
146 | /** |
147 | * Sets the context for an event declaration (see CTF specification) | |
8e964be1 MK |
148 | * |
149 | * @param context | |
150 | * the context in structdeclaration format | |
9ac2eb62 | 151 | */ |
866e5b51 | 152 | public void setContext(StructDeclaration context) { |
75259c16 | 153 | fContext = context; |
866e5b51 FC |
154 | } |
155 | ||
9ac2eb62 MK |
156 | /** |
157 | * Sets the fields of an event declaration | |
8e964be1 MK |
158 | * |
159 | * @param fields | |
160 | * the fields in structdeclaration format | |
9ac2eb62 | 161 | */ |
866e5b51 | 162 | public void setFields(StructDeclaration fields) { |
75259c16 | 163 | fFields = fields; |
866e5b51 FC |
164 | } |
165 | ||
8e964be1 | 166 | @Override |
866e5b51 | 167 | public StructDeclaration getFields() { |
75259c16 | 168 | return fFields; |
866e5b51 FC |
169 | } |
170 | ||
8e964be1 | 171 | @Override |
866e5b51 | 172 | public StructDeclaration getContext() { |
75259c16 | 173 | return fContext; |
866e5b51 FC |
174 | } |
175 | ||
9ac2eb62 | 176 | /** |
ecb12461 | 177 | * Sets the id of an event declaration |
8e964be1 MK |
178 | * |
179 | * @param id | |
180 | * the id | |
9ac2eb62 | 181 | */ |
866e5b51 | 182 | public void setId(long id) { |
5f715709 MK |
183 | if (id < 0 || id > Integer.MAX_VALUE) { |
184 | throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$ | |
185 | } | |
186 | fId = (int) id; | |
866e5b51 FC |
187 | } |
188 | ||
8e964be1 | 189 | @Override |
866e5b51 | 190 | public Long getId() { |
5f715709 MK |
191 | return Long.valueOf(fId); |
192 | } | |
193 | ||
194 | /** | |
195 | * Faster get id assuming you have less than a billion event types | |
196 | * | |
197 | * @return the event id | |
198 | */ | |
199 | public int id() { | |
75259c16 | 200 | return fId; |
866e5b51 FC |
201 | } |
202 | ||
9ac2eb62 | 203 | /** |
ecb12461 | 204 | * Sets the stream of an event declaration |
8e964be1 MK |
205 | * |
206 | * @param stream | |
207 | * the stream | |
486efb2e | 208 | * @since 2.0 |
9ac2eb62 | 209 | */ |
d84419e1 | 210 | public void setStream(CTFStream stream) { |
75259c16 | 211 | fStream = stream; |
866e5b51 FC |
212 | } |
213 | ||
8e964be1 | 214 | @Override |
d84419e1 | 215 | public CTFStream getStream() { |
75259c16 | 216 | return fStream; |
866e5b51 FC |
217 | } |
218 | ||
9ac2eb62 MK |
219 | /** |
220 | * Is the name of the event declaration set | |
8e964be1 | 221 | * |
9ac2eb62 MK |
222 | * @return is the name set? |
223 | */ | |
866e5b51 | 224 | public boolean nameIsSet() { |
75259c16 | 225 | return fName != null; |
866e5b51 FC |
226 | } |
227 | ||
9ac2eb62 MK |
228 | /** |
229 | * Is the context set | |
8e964be1 | 230 | * |
9ac2eb62 MK |
231 | * @return is the context set |
232 | */ | |
866e5b51 | 233 | public boolean contextIsSet() { |
75259c16 | 234 | return fContext != null; |
866e5b51 FC |
235 | } |
236 | ||
9ac2eb62 MK |
237 | /** |
238 | * Is a field set? | |
8e964be1 | 239 | * |
9ac2eb62 MK |
240 | * @return Is the field set? |
241 | */ | |
866e5b51 | 242 | public boolean fieldsIsSet() { |
75259c16 | 243 | return fFields != null; |
866e5b51 FC |
244 | } |
245 | ||
9ac2eb62 MK |
246 | /** |
247 | * Is the id set? | |
8e964be1 | 248 | * |
9ac2eb62 MK |
249 | * @return is the id set? |
250 | */ | |
866e5b51 | 251 | public boolean idIsSet() { |
5f715709 | 252 | return (fId != UNSET_EVENT_ID); |
866e5b51 FC |
253 | } |
254 | ||
9ac2eb62 MK |
255 | /** |
256 | * Is the stream set? | |
8e964be1 | 257 | * |
9ac2eb62 MK |
258 | * @return is the stream set? |
259 | */ | |
866e5b51 | 260 | public boolean streamIsSet() { |
75259c16 | 261 | return fStream != null; |
866e5b51 FC |
262 | } |
263 | ||
8e964be1 | 264 | @Override |
53047a66 | 265 | public long getLogLevel() { |
75259c16 | 266 | return fLogLevel; |
53047a66 MK |
267 | } |
268 | ||
9ac2eb62 MK |
269 | /** |
270 | * Sets the log level | |
8e964be1 MK |
271 | * |
272 | * @param level | |
273 | * the log level | |
9ac2eb62 | 274 | */ |
8e964be1 | 275 | public void setLogLevel(long level) { |
75259c16 | 276 | fLogLevel = level; |
53047a66 MK |
277 | } |
278 | ||
8e964be1 MK |
279 | @Override |
280 | public Set<String> getCustomAttributes() { | |
75259c16 | 281 | return fCustomAttributes.keySet(); |
8e964be1 MK |
282 | } |
283 | ||
284 | @Override | |
285 | public String getCustomAttribute(String key) { | |
75259c16 | 286 | return fCustomAttributes.get(key); |
8e964be1 MK |
287 | } |
288 | ||
289 | /** | |
290 | * Sets a custom attribute value. | |
291 | * | |
292 | * @param key | |
293 | * the key of the attribute | |
294 | * @param value | |
295 | * the value of the attribute | |
296 | * @since 2.0 | |
297 | */ | |
298 | public void setCustomAttribute(String key, String value) { | |
75259c16 | 299 | fCustomAttributes.put(key, value); |
8e964be1 MK |
300 | } |
301 | ||
866e5b51 FC |
302 | // ------------------------------------------------------------------------ |
303 | // Operations | |
304 | // ------------------------------------------------------------------------ | |
305 | ||
306 | @Override | |
307 | public boolean equals(Object obj) { | |
308 | if (this == obj) { | |
309 | return true; | |
310 | } | |
311 | if (obj == null) { | |
312 | return false; | |
313 | } | |
314 | if (!(obj instanceof EventDeclaration)) { | |
315 | return false; | |
316 | } | |
317 | EventDeclaration other = (EventDeclaration) obj; | |
75259c16 MK |
318 | if (fContext == null) { |
319 | if (other.fContext != null) { | |
866e5b51 FC |
320 | return false; |
321 | } | |
75259c16 | 322 | } else if (!fContext.equals(other.fContext)) { |
866e5b51 FC |
323 | return false; |
324 | } | |
75259c16 MK |
325 | if (fFields == null) { |
326 | if (other.fFields != null) { | |
866e5b51 FC |
327 | return false; |
328 | } | |
75259c16 | 329 | } else if (!fFields.equals(other.fFields)) { |
866e5b51 FC |
330 | return false; |
331 | } | |
5f715709 | 332 | if (fId != (other.fId)) { |
866e5b51 FC |
333 | return false; |
334 | } | |
75259c16 MK |
335 | if (fName == null) { |
336 | if (other.fName != null) { | |
866e5b51 FC |
337 | return false; |
338 | } | |
75259c16 | 339 | } else if (!fName.equals(other.fName)) { |
866e5b51 FC |
340 | return false; |
341 | } | |
75259c16 MK |
342 | if (fStream == null) { |
343 | if (other.fStream != null) { | |
866e5b51 FC |
344 | return false; |
345 | } | |
75259c16 | 346 | } else if (!fStream.equals(other.fStream)) { |
866e5b51 FC |
347 | return false; |
348 | } | |
75259c16 | 349 | if (!fCustomAttributes.equals(other.fCustomAttributes)) { |
8e964be1 MK |
350 | return false; |
351 | } | |
866e5b51 FC |
352 | return true; |
353 | } | |
354 | ||
355 | @Override | |
356 | public int hashCode() { | |
357 | final int prime = 31; | |
358 | int result = 1; | |
359 | result = (prime * result) | |
75259c16 MK |
360 | + ((fContext == null) ? 0 : fContext.hashCode()); |
361 | result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode()); | |
5f715709 | 362 | result = (prime * result) + fId; |
75259c16 MK |
363 | result = (prime * result) + ((fName == null) ? 0 : fName.hashCode()); |
364 | result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode()); | |
365 | result = (prime * result) + fCustomAttributes.hashCode(); | |
866e5b51 FC |
366 | return result; |
367 | } | |
368 | ||
369 | } |