1 /*******************************************************************************
2 * Copyright (c) 2011-2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.event
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
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
.LexicalScope
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
25 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.EventDeclaration
;
27 import com
.google
.common
.collect
.ImmutableList
;
28 import com
.google
.common
.collect
.ImmutableList
.Builder
;
31 * Representation of a particular instance of an event.
33 public final class EventDefinition
implements IDefinitionScope
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 * A null event, can be used for testing or poison pilling
43 public static final EventDefinition NULL_EVENT
= new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
46 * The corresponding event declaration.
48 private final IEventDeclaration fDeclaration
;
51 * The timestamp of the current event.
53 private final long fTimestamp
;
56 * The event context structure definition.
58 private final StructDefinition fEventContext
;
60 private final StructDefinition fStreamContext
;
62 private final StructDefinition fPacketContext
;
65 * The event fields structure definition.
67 private final StructDefinition fFields
;
70 * The StreamInputReader that reads this event definition.
72 private final CTFStreamInputReader fStreamInputReader
;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Constructs an event definition.
82 * The corresponding event declaration
83 * @param streamInputReader
84 * The SIR from where this EventDef was read
89 * @param packetContext
91 * @param streamContext
96 public EventDefinition(IEventDeclaration declaration
,
97 CTFStreamInputReader streamInputReader
,
99 StructDefinition streamContext
,
100 StructDefinition eventContext
,
101 StructDefinition packetContext
,
102 StructDefinition fields
) {
103 fDeclaration
= declaration
;
104 fStreamInputReader
= streamInputReader
;
105 fTimestamp
= timestamp
;
107 fEventContext
= eventContext
;
108 fPacketContext
= packetContext
;
109 fStreamContext
= streamContext
;
112 // ------------------------------------------------------------------------
113 // Getters/Setters/Predicates
114 // ------------------------------------------------------------------------
117 public LexicalScope
getScopePath() {
118 String eventName
= fDeclaration
.getName();
119 if (eventName
== null) {
122 LexicalScope myScope
= LexicalScope
.EVENT
.getChild(eventName
);
123 if (myScope
== null) {
124 myScope
= new LexicalScope(LexicalScope
.EVENT
, eventName
);
130 * Gets the declaration (the form) of the data
132 * @return the event declaration
134 public IEventDeclaration
getDeclaration() {
139 * Gets the fields of a definition
141 * @return the fields of a definition in struct form. Can be null.
143 public StructDefinition
getFields() {
148 * Gets the context of this event without the context of the stream
150 * @return the context in struct form
152 public StructDefinition
getEventContext() {
153 return fEventContext
;
157 * Gets the context of this event within a stream
159 * @return the context in struct form
161 public StructDefinition
getContext() {
163 /* Most common case so far */
164 if (fStreamContext
== null) {
165 return fEventContext
;
168 /* streamContext is not null, but the context of the event is null */
169 if (fEventContext
== null) {
170 return fStreamContext
;
173 // TODO: cache if this is a performance issue
175 /* The stream context and event context are assigned. */
176 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
178 Builder
<String
> builder
= ImmutableList
.<String
> builder();
179 List
<Definition
> fieldValues
= new ArrayList
<>();
181 /* Add fields from the stream */
182 for (String fieldName
: fStreamContext
.getFieldNames()) {
183 Definition definition
= fStreamContext
.getDefinition(fieldName
);
184 mergedDeclaration
.addField(fieldName
, definition
.getDeclaration());
185 builder
.add(fieldName
);
186 fieldValues
.add(definition
);
189 ImmutableList
<String
> fieldNames
= builder
.build();
191 * Add fields from the event context, overwrite the stream ones if
194 for (String fieldName
: fEventContext
.getFieldNames()) {
195 Definition definition
= fEventContext
.getDefinition(fieldName
);
196 mergedDeclaration
.addField(fieldName
, definition
.getDeclaration());
197 if (fieldNames
.contains(fieldName
)) {
198 fieldValues
.set((fieldNames
.indexOf(fieldName
)), definition
);
200 builder
.add(fieldName
);
201 fieldValues
.add(definition
);
204 fieldNames
= builder
.build();
205 StructDefinition mergedContext
= new StructDefinition(mergedDeclaration
, this, "context", //$NON-NLS-1$
207 fieldValues
.toArray(new Definition
[fieldValues
.size()]));
208 return mergedContext
;
212 * Gets the stream input reader that this event was made by
216 public CTFStreamInputReader
getStreamInputReader() {
217 return fStreamInputReader
;
221 * Gets the context of packet the event is in.
223 * @return the packet context
225 public StructDefinition
getPacketContext() {
226 return fPacketContext
;
230 * gets the CPU the event was generated by. Slightly LTTng specific
232 * @return The CPU the event was generated by
234 public int getCPU() {
235 return fStreamInputReader
.getCPU();
239 * @return the timestamp
241 public long getTimestamp() {
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 public Definition
lookupDefinition(String lookupPath
) {
251 if (lookupPath
.equals("context")) { //$NON-NLS-1$
252 return fEventContext
;
253 } else if (lookupPath
.equals("fields")) { //$NON-NLS-1$
261 public String
toString() {
262 Iterable
<String
> list
;
263 StringBuilder retString
= new StringBuilder();
264 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
266 retString
.append("Event type: " + fDeclaration
.getName() + cr
); //$NON-NLS-1$
267 retString
.append("Timestamp: " + Long
.toString(fTimestamp
) + cr
); //$NON-NLS-1$
269 if (fEventContext
!= null) {
270 list
= fEventContext
.getDeclaration().getFieldsList();
272 for (String field
: list
) {
273 retString
.append(field
274 + " : " + fEventContext
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
278 if (fFields
!= null) {
279 list
= fFields
.getDeclaration().getFieldsList();
281 for (String field
: list
) {
282 retString
.append(field
283 + " : " + fFields
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
287 return retString
.toString();