1 /*******************************************************************************
2 * Copyright (c) 2011-2013 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
.linuxtools
.ctf
.core
.event
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.LexicalScope
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFStreamInputReader
;
25 import org
.eclipse
.linuxtools
.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
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
99 public EventDefinition(IEventDeclaration declaration
,
100 CTFStreamInputReader streamInputReader
,
102 StructDefinition streamContext
,
103 StructDefinition eventContext
,
104 StructDefinition packetContext
,
105 StructDefinition fields
) {
106 fDeclaration
= declaration
;
107 fStreamInputReader
= streamInputReader
;
108 fTimestamp
= timestamp
;
110 fEventContext
= eventContext
;
111 fPacketContext
= packetContext
;
112 fStreamContext
= streamContext
;
115 // ------------------------------------------------------------------------
116 // Getters/Setters/Predicates
117 // ------------------------------------------------------------------------
123 public LexicalScope
getScopePath() {
124 String eventName
= fDeclaration
.getName();
125 if (eventName
== null) {
128 LexicalScope myScope
= LexicalScope
.EVENT
.getChild(eventName
);
129 if (myScope
== null) {
130 myScope
= new LexicalScope(LexicalScope
.EVENT
, eventName
);
136 * Gets the declaration (the form) of the data
138 * @return the event declaration
141 public IEventDeclaration
getDeclaration() {
146 * Gets the fields of a definition
148 * @return the fields of a definition in struct form. Can be null.
150 public StructDefinition
getFields() {
155 * Gets the context of this event without the context of the stream
157 * @return the context in struct form
160 public StructDefinition
getEventContext() {
161 return fEventContext
;
165 * Gets the context of this event within a stream
167 * @return the context in struct form
169 public StructDefinition
getContext() {
171 /* Most common case so far */
172 if (fStreamContext
== null) {
173 return fEventContext
;
176 /* streamContext is not null, but the context of the event is null */
177 if (fEventContext
== null) {
178 return fStreamContext
;
181 // TODO: cache if this is a performance issue
183 /* The stream context and event context are assigned. */
184 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
186 Builder
<String
> builder
= ImmutableList
.<String
> builder();
187 List
<Definition
> fieldValues
= new ArrayList
<>();
189 /* Add fields from the stream */
190 for (String fieldName
: fStreamContext
.getFieldNames()) {
191 Definition definition
= fStreamContext
.getDefinition(fieldName
);
192 mergedDeclaration
.addField(fieldName
, definition
.getDeclaration());
193 builder
.add(fieldName
);
194 fieldValues
.add(definition
);
197 ImmutableList
<String
> fieldNames
= builder
.build();
199 * Add fields from the event context, overwrite the stream ones if
202 for (String fieldName
: fEventContext
.getFieldNames()) {
203 Definition definition
= fEventContext
.getDefinition(fieldName
);
204 mergedDeclaration
.addField(fieldName
, definition
.getDeclaration());
205 if (fieldNames
.contains(fieldName
)) {
206 fieldValues
.set((fieldNames
.indexOf(fieldName
)), definition
);
208 builder
.add(fieldName
);
209 fieldValues
.add(definition
);
212 fieldNames
= builder
.build();
213 StructDefinition mergedContext
= new StructDefinition(mergedDeclaration
, this, "context", //$NON-NLS-1$
215 fieldValues
.toArray(new Definition
[fieldValues
.size()]));
216 return mergedContext
;
220 * Gets the stream input reader that this event was made by
225 public CTFStreamInputReader
getStreamInputReader() {
226 return fStreamInputReader
;
230 * Gets the context of packet the event is in.
232 * @return the packet context
234 public StructDefinition
getPacketContext() {
235 return fPacketContext
;
239 * gets the CPU the event was generated by. Slightly LTTng specific
241 * @return The CPU the event was generated by
243 public int getCPU() {
244 return fStreamInputReader
.getCPU();
248 * @return the timestamp
250 public long getTimestamp() {
254 // ------------------------------------------------------------------------
256 // ------------------------------------------------------------------------
259 public Definition
lookupDefinition(String lookupPath
) {
260 if (lookupPath
.equals("context")) { //$NON-NLS-1$
261 return fEventContext
;
262 } else if (lookupPath
.equals("fields")) { //$NON-NLS-1$
270 public String
toString() {
271 Iterable
<String
> list
;
272 StringBuilder retString
= new StringBuilder();
273 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
275 retString
.append("Event type: " + fDeclaration
.getName() + cr
); //$NON-NLS-1$
276 retString
.append("Timestamp: " + Long
.toString(fTimestamp
) + cr
); //$NON-NLS-1$
278 if (fEventContext
!= null) {
279 list
= fEventContext
.getDeclaration().getFieldsList();
281 for (String field
: list
) {
282 retString
.append(field
283 + " : " + fEventContext
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
287 if (fFields
!= null) {
288 list
= fFields
.getDeclaration().getFieldsList();
290 for (String field
: list
) {
291 retString
.append(field
292 + " : " + fFields
.getDefinition(field
).toString() + cr
); //$NON-NLS-1$
296 return retString
.toString();