tmf.ui: Fix dead store in TimeAlignmentSynchronizer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / EventDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2014 Ericsson, Ecole Polytechnique de Montreal and others
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
13 package org.eclipse.tracecompass.ctf.core.event;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
23 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
24 import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
25 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
26 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
27 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
28 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
29 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
30 import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;
31 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
32
33 /**
34 * Representation of a particular instance of an event.
35 */
36 public final class EventDefinition implements IDefinitionScope {
37
38 // ------------------------------------------------------------------------
39 // Attributes
40 // ------------------------------------------------------------------------
41
42 /**
43 * Value representing an unknown cpu number.
44 *
45 * @since 2.0
46 */
47 public static final int UNKNOWN_CPU = -1;
48
49 /**
50 * A null event, can be used for testing or poison pilling
51 */
52 public static final @NonNull EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), UNKNOWN_CPU, -1L, null, null, null, null, null, null);
53
54 /**
55 * The corresponding event declaration.
56 */
57 private final IEventDeclaration fDeclaration;
58
59 /**
60 * The timestamp of the current event.
61 */
62 private final long fTimestamp;
63
64 private final ICompositeDefinition fEventHeaderDefinition;
65
66 /**
67 * The event context structure definition.
68 */
69 private final ICompositeDefinition fEventContext;
70
71 private final ICompositeDefinition fStreamContext;
72
73 private final ICompositeDefinition fPacketContext;
74
75 /**
76 * The event fields structure definition.
77 */
78 private final ICompositeDefinition fFields;
79
80 /**
81 * The current cpu, could be @link {@link IPacketHeader#UNKNOWN_CPU}
82 */
83 private final int fCpu;
84
85 private final @NonNull Map<String, Object> fPacketAttributes;
86
87 // ------------------------------------------------------------------------
88 // Constructors
89 // ------------------------------------------------------------------------
90
91 /**
92 * Constructs an event definition.
93 *
94 * @param declaration
95 * The corresponding event declaration
96 * @param cpu
97 * The cpu source of the event. You can use UNKNOWN_CPU if it is
98 * not known.
99 * @param timestamp
100 * event timestamp
101 * @param eventHeaderDefinition
102 * The event header definition, can be null if there is no header
103 * definition
104 * @param eventContext
105 * The event context
106 * @param packetContext
107 * the packet context (the one with content size, not magic
108 * number)
109 * @param streamContext
110 * the stream context
111 * @param fields
112 * The event fields
113 * @param packetDescriptor
114 * descriptor of the packet containing this event
115 * @since 2.0
116 */
117 public EventDefinition(IEventDeclaration declaration,
118 int cpu,
119 long timestamp,
120 ICompositeDefinition eventHeaderDefinition,
121 ICompositeDefinition streamContext,
122 ICompositeDefinition eventContext,
123 ICompositeDefinition packetContext,
124 ICompositeDefinition fields,
125 @Nullable ICTFPacketDescriptor packetDescriptor) {
126 fDeclaration = declaration;
127 fEventHeaderDefinition = eventHeaderDefinition;
128 fCpu = cpu;
129 fTimestamp = timestamp;
130 fFields = fields;
131 fEventContext = eventContext;
132 fPacketContext = packetContext;
133 fStreamContext = streamContext;
134 fPacketAttributes = packetDescriptor != null ? packetDescriptor.getAttributes() : Collections.EMPTY_MAP;
135 }
136
137 // ------------------------------------------------------------------------
138 // Getters/Setters/Predicates
139 // ------------------------------------------------------------------------
140
141 /**
142 * @since 1.0
143 */
144 @Override
145 public ILexicalScope getScopePath() {
146 String eventName = fDeclaration.getName();
147 if (eventName == null) {
148 return null;
149 }
150 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
151 if (myScope == null) {
152 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
153 }
154 return myScope;
155 }
156
157 /**
158 * Gets the declaration (the form) of the data
159 *
160 * @return the event declaration
161 */
162 public IEventDeclaration getDeclaration() {
163 return fDeclaration;
164 }
165
166 /**
167 * Get the event header
168 *
169 * @return the event header
170 * @since 1.1
171 */
172 public ICompositeDefinition getEventHeader() {
173 return fEventHeaderDefinition;
174 }
175
176 /**
177 * Gets the fields of a definition
178 *
179 * @return the fields of a definition in struct form. Can be null.
180 * @since 1.0
181 */
182 public ICompositeDefinition getFields() {
183 return fFields;
184 }
185
186 /**
187 * Gets the context of this event without the context of the stream
188 *
189 * @return the context in struct form
190 * @since 1.0
191 */
192 public ICompositeDefinition getEventContext() {
193 return fEventContext;
194 }
195
196 /**
197 * Gets the context of this event within a stream
198 *
199 * @return the context in struct form
200 * @since 1.0
201 */
202 public ICompositeDefinition getContext() {
203
204 /* Most common case so far */
205 if (fStreamContext == null) {
206 return fEventContext;
207 }
208
209 /* streamContext is not null, but the context of the event is null */
210 if (fEventContext == null) {
211 return fStreamContext;
212 }
213
214 // TODO: cache if this is a performance issue
215
216 /* The stream context and event context are assigned. */
217 StructDeclaration mergedDeclaration = new StructDeclaration(1);
218
219 List<Definition> fieldValues = new ArrayList<>();
220
221 /* Add fields from the stream */
222 List<@NonNull String> fieldNames = fStreamContext.getFieldNames();
223 for (String fieldName : fieldNames) {
224 Definition definition = fStreamContext.getDefinition(fieldName);
225 mergedDeclaration.addField(fieldName, definition.getDeclaration());
226 fieldValues.add(definition);
227 }
228
229 /*
230 * Add fields from the event context, overwrite the stream ones if
231 * needed.
232 */
233 for (String fieldName : fEventContext.getFieldNames()) {
234 Definition definition = fEventContext.getDefinition(fieldName);
235 mergedDeclaration.addField(fieldName, definition.getDeclaration());
236 if (fieldNames.contains(fieldName)) {
237 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
238 } else {
239 fieldValues.add(definition);
240 }
241 }
242 return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
243 fieldValues.toArray(new Definition[fieldValues.size()]));
244 }
245
246 /**
247 * Gets the context of packet the event is in.
248 *
249 * @return the packet context
250 * @since 1.0
251 */
252 public ICompositeDefinition getPacketContext() {
253 return fPacketContext;
254 }
255
256 /**
257 * gets the CPU the event was generated by. Slightly LTTng specific
258 *
259 * @return The CPU the event was generated by
260 */
261 public int getCPU() {
262 return fCpu;
263 }
264
265 /**
266 * @return the timestamp
267 */
268 public long getTimestamp() {
269 return fTimestamp;
270 }
271
272 /**
273 * Get the packet attributes.
274 *
275 * @return the packet attributes, such as "device" and "timestamp_begin"
276 * @since 2.0
277 */
278 public @NonNull Map<String, Object> getPacketAttributes() {
279 return fPacketAttributes;
280 }
281 // ------------------------------------------------------------------------
282 // Operations
283 // ------------------------------------------------------------------------
284
285 /**
286 * @since 1.0
287 */
288 @Override
289 public IDefinition lookupDefinition(String lookupPath) {
290 if (lookupPath.equals("context")) { //$NON-NLS-1$
291 return fEventContext;
292 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
293 return fFields;
294 } else {
295 return null;
296 }
297 }
298
299 @Override
300 public String toString() {
301 Iterable<String> list;
302 StringBuilder retString = new StringBuilder();
303 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
304
305 retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$
306 retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$
307
308 if (fEventContext != null) {
309 list = fEventContext.getFieldNames();
310
311 for (String field : list) {
312 retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
313 }
314 }
315
316 if (fFields != null) {
317 list = fFields.getFieldNames();
318
319 for (String field : list) {
320 retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
321 }
322 }
323
324 return retString.toString();
325 }
326
327 }
This page took 0.038616 seconds and 5 git commands to generate.