b145b58ebe3b4890e82b3fb77ccb58f70ee19e1e
[deliverable/tracecompass.git] / 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.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
20 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
21 import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
22 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
23 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
24 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
25 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
26 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
27 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
28
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.ImmutableList.Builder;
31
32 /**
33 * Representation of a particular instance of an event.
34 */
35 public final class EventDefinition implements IDefinitionScope {
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
41 /**
42 * A null event, can be used for testing or poison pilling
43 */
44 @NonNull
45 public static final EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
46
47 /**
48 * The corresponding event declaration.
49 */
50 private final IEventDeclaration fDeclaration;
51
52 /**
53 * The timestamp of the current event.
54 */
55 private final long fTimestamp;
56
57 /**
58 * The event context structure definition.
59 */
60 private final StructDefinition fEventContext;
61
62 private final StructDefinition fStreamContext;
63
64 private final StructDefinition fPacketContext;
65
66 /**
67 * The event fields structure definition.
68 */
69 private final StructDefinition fFields;
70
71 /**
72 * The StreamInputReader that reads this event definition.
73 */
74 private final CTFStreamInputReader fStreamInputReader;
75
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79
80 /**
81 * Constructs an event definition.
82 *
83 * @param declaration
84 * The corresponding event declaration
85 * @param streamInputReader
86 * The SIR from where this EventDef was read
87 * @param timestamp
88 * event timestamp
89 * @param eventContext
90 * The event context
91 * @param packetContext
92 * the packet context
93 * @param streamContext
94 * the stream context
95 * @param fields
96 * The event fields
97 */
98 public EventDefinition(IEventDeclaration declaration,
99 CTFStreamInputReader streamInputReader,
100 long timestamp,
101 StructDefinition streamContext,
102 StructDefinition eventContext,
103 StructDefinition packetContext,
104 StructDefinition fields) {
105 fDeclaration = declaration;
106 fStreamInputReader = streamInputReader;
107 fTimestamp = timestamp;
108 fFields = fields;
109 fEventContext = eventContext;
110 fPacketContext = packetContext;
111 fStreamContext = streamContext;
112 }
113
114 // ------------------------------------------------------------------------
115 // Getters/Setters/Predicates
116 // ------------------------------------------------------------------------
117
118 /**
119 * @since 1.0
120 */
121 @Override
122 public ILexicalScope getScopePath() {
123 String eventName = fDeclaration.getName();
124 if (eventName == null) {
125 return null;
126 }
127 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
128 if (myScope == null) {
129 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
130 }
131 return myScope;
132 }
133
134 /**
135 * Gets the declaration (the form) of the data
136 *
137 * @return the event declaration
138 */
139 public IEventDeclaration getDeclaration() {
140 return fDeclaration;
141 }
142
143 /**
144 * Gets the fields of a definition
145 *
146 * @return the fields of a definition in struct form. Can be null.
147 */
148 public StructDefinition getFields() {
149 return fFields;
150 }
151
152 /**
153 * Gets the context of this event without the context of the stream
154 *
155 * @return the context in struct form
156 */
157 public StructDefinition getEventContext() {
158 return fEventContext;
159 }
160
161 /**
162 * Gets the context of this event within a stream
163 *
164 * @return the context in struct form
165 */
166 public StructDefinition getContext() {
167
168 /* Most common case so far */
169 if (fStreamContext == null) {
170 return fEventContext;
171 }
172
173 /* streamContext is not null, but the context of the event is null */
174 if (fEventContext == null) {
175 return fStreamContext;
176 }
177
178 // TODO: cache if this is a performance issue
179
180 /* The stream context and event context are assigned. */
181 StructDeclaration mergedDeclaration = new StructDeclaration(1);
182
183 Builder<String> builder = ImmutableList.<String> builder();
184 List<Definition> fieldValues = new ArrayList<>();
185
186 /* Add fields from the stream */
187 for (String fieldName : fStreamContext.getFieldNames()) {
188 Definition definition = fStreamContext.getDefinition(fieldName);
189 mergedDeclaration.addField(fieldName, definition.getDeclaration());
190 builder.add(fieldName);
191 fieldValues.add(definition);
192 }
193
194 ImmutableList<String> fieldNames = builder.build();
195 /*
196 * Add fields from the event context, overwrite the stream ones if
197 * needed.
198 */
199 for (String fieldName : fEventContext.getFieldNames()) {
200 Definition definition = fEventContext.getDefinition(fieldName);
201 mergedDeclaration.addField(fieldName, definition.getDeclaration());
202 if (fieldNames.contains(fieldName)) {
203 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
204 } else {
205 builder.add(fieldName);
206 fieldValues.add(definition);
207 }
208 }
209 fieldNames = builder.build();
210 StructDefinition mergedContext = new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
211 fieldNames,
212 fieldValues.toArray(new Definition[fieldValues.size()]));
213 return mergedContext;
214 }
215
216 /**
217 * Gets the stream input reader that this event was made by
218 *
219 * @return the parent
220 */
221 public CTFStreamInputReader getStreamInputReader() {
222 return fStreamInputReader;
223 }
224
225 /**
226 * Gets the context of packet the event is in.
227 *
228 * @return the packet context
229 */
230 public StructDefinition getPacketContext() {
231 return fPacketContext;
232 }
233
234 /**
235 * gets the CPU the event was generated by. Slightly LTTng specific
236 *
237 * @return The CPU the event was generated by
238 */
239 public int getCPU() {
240 return fStreamInputReader.getCPU();
241 }
242
243 /**
244 * @return the timestamp
245 */
246 public long getTimestamp() {
247 return fTimestamp;
248 }
249
250 // ------------------------------------------------------------------------
251 // Operations
252 // ------------------------------------------------------------------------
253
254 /**
255 * @since 1.0
256 */
257 @Override
258 public IDefinition lookupDefinition(String lookupPath) {
259 if (lookupPath.equals("context")) { //$NON-NLS-1$
260 return fEventContext;
261 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
262 return fFields;
263 } else {
264 return null;
265 }
266 }
267
268 @Override
269 public String toString() {
270 Iterable<String> list;
271 StringBuilder retString = new StringBuilder();
272 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
273
274 retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$
275 retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$
276
277 if (fEventContext != null) {
278 list = fEventContext.getDeclaration().getFieldsList();
279
280 for (String field : list) {
281 retString.append(field
282 + " : " + fEventContext.getDefinition(field).toString() + cr); //$NON-NLS-1$
283 }
284 }
285
286 if (fFields != null) {
287 list = fFields.getDeclaration().getFieldsList();
288
289 for (String field : list) {
290 retString.append(field
291 + " : " + fFields.getDefinition(field).toString() + cr); //$NON-NLS-1$
292 }
293 }
294
295 return retString.toString();
296 }
297
298 }
This page took 0.03802 seconds and 4 git commands to generate.