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
.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
;
29 import com
.google
.common
.collect
.ImmutableList
;
30 import com
.google
.common
.collect
.ImmutableList
.Builder
;
33 * Representation of a particular instance of an event.
35 public final class EventDefinition
implements IDefinitionScope
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * A null event, can be used for testing or poison pilling
45 public static final EventDefinition NULL_EVENT
= new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
48 * The corresponding event declaration.
50 private final IEventDeclaration fDeclaration
;
53 * The timestamp of the current event.
55 private final long fTimestamp
;
58 * The event context structure definition.
60 private final StructDefinition fEventContext
;
62 private final StructDefinition fStreamContext
;
64 private final StructDefinition fPacketContext
;
67 * The event fields structure definition.
69 private final StructDefinition fFields
;
72 * The StreamInputReader that reads this event definition.
74 private final CTFStreamInputReader fStreamInputReader
;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 * Constructs an event definition.
84 * The corresponding event declaration
85 * @param streamInputReader
86 * The SIR from where this EventDef was read
91 * @param packetContext
93 * @param streamContext
98 public EventDefinition(IEventDeclaration declaration
,
99 CTFStreamInputReader streamInputReader
,
101 StructDefinition streamContext
,
102 StructDefinition eventContext
,
103 StructDefinition packetContext
,
104 StructDefinition fields
) {
105 fDeclaration
= declaration
;
106 fStreamInputReader
= streamInputReader
;
107 fTimestamp
= timestamp
;
109 fEventContext
= eventContext
;
110 fPacketContext
= packetContext
;
111 fStreamContext
= streamContext
;
114 // ------------------------------------------------------------------------
115 // Getters/Setters/Predicates
116 // ------------------------------------------------------------------------
122 public ILexicalScope
getScopePath() {
123 String eventName
= fDeclaration
.getName();
124 if (eventName
== null) {
127 ILexicalScope myScope
= ILexicalScope
.EVENT
.getChild(eventName
);
128 if (myScope
== null) {
129 myScope
= new LexicalScope(ILexicalScope
.EVENT
, eventName
);
135 * Gets the declaration (the form) of the data
137 * @return the event declaration
139 public IEventDeclaration
getDeclaration() {
144 * Gets the fields of a definition
146 * @return the fields of a definition in struct form. Can be null.
148 public StructDefinition
getFields() {
153 * Gets the context of this event without the context of the stream
155 * @return the context in struct form
157 public StructDefinition
getEventContext() {
158 return fEventContext
;
162 * Gets the context of this event within a stream
164 * @return the context in struct form
166 public StructDefinition
getContext() {
168 /* Most common case so far */
169 if (fStreamContext
== null) {
170 return fEventContext
;
173 /* streamContext is not null, but the context of the event is null */
174 if (fEventContext
== null) {
175 return fStreamContext
;
178 // TODO: cache if this is a performance issue
180 /* The stream context and event context are assigned. */
181 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
183 Builder
<String
> builder
= ImmutableList
.<String
> builder();
184 List
<Definition
> fieldValues
= new ArrayList
<>();
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
);
194 ImmutableList
<String
> fieldNames
= builder
.build();
196 * Add fields from the event context, overwrite the stream ones if
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
);
205 builder
.add(fieldName
);
206 fieldValues
.add(definition
);
209 fieldNames
= builder
.build();
210 StructDefinition mergedContext
= new StructDefinition(mergedDeclaration
, this, "context", //$NON-NLS-1$
212 fieldValues
.toArray(new Definition
[fieldValues
.size()]));
213 return mergedContext
;
217 * Gets the stream input reader that this event was made by
221 public CTFStreamInputReader
getStreamInputReader() {
222 return fStreamInputReader
;
226 * Gets the context of packet the event is in.
228 * @return the packet context
230 public StructDefinition
getPacketContext() {
231 return fPacketContext
;
235 * gets the CPU the event was generated by. Slightly LTTng specific
237 * @return The CPU the event was generated by
239 public int getCPU() {
240 return fStreamInputReader
.getCPU();
244 * @return the timestamp
246 public long getTimestamp() {
250 // ------------------------------------------------------------------------
252 // ------------------------------------------------------------------------
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$
269 public String
toString() {
270 Iterable
<String
> list
;
271 StringBuilder retString
= new StringBuilder();
272 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
274 retString
.append("Event type: " + fDeclaration
.getName() + cr
); //$NON-NLS-1$
275 retString
.append("Timestamp: " + Long
.toString(fTimestamp
) + cr
); //$NON-NLS-1$
277 if (fEventContext
!= null) {
278 list
= fEventContext
.getDeclaration().getFieldsList();
280 for (String field
: list
) {
281 retString
.append(field
282 + " : " + fEventContext
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
286 if (fFields
!= null) {
287 list
= fFields
.getDeclaration().getFieldsList();
289 for (String field
: list
) {
290 retString
.append(field
291 + " : " + fFields
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
295 return retString
.toString();