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
.ICompositeDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
28 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.EventDeclaration
;
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 ICompositeDefinition fEventContext
;
60 private final ICompositeDefinition fStreamContext
;
62 private final ICompositeDefinition fPacketContext
;
65 * The event fields structure definition.
67 private final ICompositeDefinition 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
97 public EventDefinition(IEventDeclaration declaration
,
98 CTFStreamInputReader streamInputReader
,
100 ICompositeDefinition streamContext
,
101 ICompositeDefinition eventContext
,
102 ICompositeDefinition packetContext
,
103 ICompositeDefinition fields
) {
104 fDeclaration
= declaration
;
105 fStreamInputReader
= streamInputReader
;
106 fTimestamp
= timestamp
;
108 fEventContext
= eventContext
;
109 fPacketContext
= packetContext
;
110 fStreamContext
= streamContext
;
113 // ------------------------------------------------------------------------
114 // Getters/Setters/Predicates
115 // ------------------------------------------------------------------------
121 public ILexicalScope
getScopePath() {
122 String eventName
= fDeclaration
.getName();
123 if (eventName
== null) {
126 ILexicalScope myScope
= ILexicalScope
.EVENT
.getChild(eventName
);
127 if (myScope
== null) {
128 myScope
= new LexicalScope(ILexicalScope
.EVENT
, eventName
);
134 * Gets the declaration (the form) of the data
136 * @return the event declaration
138 public IEventDeclaration
getDeclaration() {
143 * Gets the fields of a definition
145 * @return the fields of a definition in struct form. Can be null.
148 public ICompositeDefinition
getFields() {
153 * Gets the context of this event without the context of the stream
155 * @return the context in struct form
158 public ICompositeDefinition
getEventContext() {
159 return fEventContext
;
163 * Gets the context of this event within a stream
165 * @return the context in struct form
168 public ICompositeDefinition
getContext() {
170 /* Most common case so far */
171 if (fStreamContext
== null) {
172 return fEventContext
;
175 /* streamContext is not null, but the context of the event is null */
176 if (fEventContext
== null) {
177 return fStreamContext
;
180 // TODO: cache if this is a performance issue
182 /* The stream context and event context are assigned. */
183 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
185 List
<Definition
> fieldValues
= new ArrayList
<>();
187 /* Add fields from the stream */
188 List
<String
> fieldNames
= fStreamContext
.getFieldNames();
189 for (String fieldName
: fieldNames
) {
190 Definition definition
= fStreamContext
.getDefinition(fieldName
);
191 mergedDeclaration
.addField(fieldName
, definition
.getDeclaration());
192 fieldValues
.add(definition
);
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 fieldValues
.add(definition
);
208 return new StructDefinition(mergedDeclaration
, this, "context", //$NON-NLS-1$
209 fieldValues
.toArray(new Definition
[fieldValues
.size()]));
213 * Gets the stream input reader that this event was made by
217 public CTFStreamInputReader
getStreamInputReader() {
218 return fStreamInputReader
;
222 * Gets the context of packet the event is in.
224 * @return the packet context
227 public ICompositeDefinition
getPacketContext() {
228 return fPacketContext
;
232 * gets the CPU the event was generated by. Slightly LTTng specific
234 * @return The CPU the event was generated by
236 public int getCPU() {
237 return fStreamInputReader
.getCPU();
241 * @return the timestamp
243 public long getTimestamp() {
247 // ------------------------------------------------------------------------
249 // ------------------------------------------------------------------------
255 public IDefinition
lookupDefinition(String lookupPath
) {
256 if (lookupPath
.equals("context")) { //$NON-NLS-1$
257 return fEventContext
;
258 } else if (lookupPath
.equals("fields")) { //$NON-NLS-1$
266 public String
toString() {
267 Iterable
<String
> list
;
268 StringBuilder retString
= new StringBuilder();
269 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
271 retString
.append("Event type: ").append(fDeclaration
.getName()).append(cr
); //$NON-NLS-1$
272 retString
.append("Timestamp: ").append(Long
.toString(fTimestamp
)).append(cr
); //$NON-NLS-1$
274 if (fEventContext
!= null) {
275 list
= fEventContext
.getFieldNames();
277 for (String field
: list
) {
278 retString
.append(field
).append(" : ").append(fEventContext
.getDefinition(field
).toString()).append(cr
); //$NON-NLS-1$
282 if (fFields
!= null) {
283 list
= fFields
.getFieldNames();
285 for (String field
: list
) {
286 retString
.append(field
).append(" : ").append(fFields
.getDefinition(field
).toString()).append(cr
); //$NON-NLS-1$
290 return retString
.toString();