1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
.HashMap
;
16 import java
.util
.List
;
17 import java
.util
.Map
.Entry
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInputReader
;
26 * Representation of a particular instance of an event.
28 public class EventDefinition
implements IDefinitionScope
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
35 * The corresponding event declaration.
37 private final EventDeclaration declaration
;
40 * The timestamp of the current event.
42 private long timestamp
;
45 * The event context structure definition.
47 private StructDefinition context
;
50 * The event fields structure definition.
52 private StructDefinition fields
;
55 * The StreamInputReader that reads this event definition.
57 private final StreamInputReader streamInputReader
;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * Constructs an event definition.
67 * The corresponding event declaration
68 * @param streamInputReader
69 * The SIR from where this EventDef was read
71 public EventDefinition(EventDeclaration declaration
,
72 StreamInputReader streamInputReader
) {
73 this.declaration
= declaration
;
74 this.streamInputReader
= streamInputReader
;
77 // ------------------------------------------------------------------------
78 // Getters/Setters/Predicates
79 // ------------------------------------------------------------------------
82 public String
getPath() {
83 return "event"; //$NON-NLS-1$
87 * Gets the declaration (the form) of the data
89 * @return the event declaration
91 public EventDeclaration
getDeclaration() {
96 * Gets the fields of a definition
98 * @return the fields of a definition in struct form. Can be null.
100 public StructDefinition
getFields() {
105 * Gets the context of this event without the context of the stream
107 * @return the context in struct form
110 public StructDefinition
getEventContext() {
115 * Gets the context of this event within a stream
117 * @return the context in struct form
119 public StructDefinition
getContext() {
120 final StructDefinition streamContext
=
121 streamInputReader
.getPacketReader().getStreamEventContextDef();
123 /* Most common case so far */
124 if (streamContext
== null) {
128 /* streamContext is not null, but the context of the event is null */
129 if (context
== null) {
130 return streamContext
;
133 /* The stream context and event context are assigned. */
134 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
136 /* Add fields from the stream */
137 HashMap
<String
, Definition
> defs
= streamContext
.getDefinitions();
138 for (Entry
<String
, Definition
> entry
: defs
.entrySet()) {
139 mergedDeclaration
.addField(entry
.getKey(), entry
.getValue().getDeclaration());
142 /* Add fields from the event context, overwrite the stream ones if needed. */
143 for (Entry
<String
, Definition
> entry
: context
.getDefinitions().entrySet()) {
144 mergedDeclaration
.addField(entry
.getKey(), entry
.getValue().getDeclaration());
147 StructDefinition mergedContext
= mergedDeclaration
.createDefinition(null, "context"); //$NON-NLS-1$
148 for (String key
: mergedContext
.getDefinitions().keySet()) {
149 final Definition lookupDefinition
= context
.lookupDefinition(key
);
151 * If the key is in the event context, add it from there, if it is
152 * not, then it's in the stream. There is a priority with scoping so
153 * if there is a field like "context" in both stream and context,
154 * you display the context.
156 if (lookupDefinition
!= null) {
157 mergedContext
.getDefinitions().put(key
, lookupDefinition
);
159 mergedContext
.getDefinitions().put(key
, streamContext
.lookupDefinition(key
));
162 return mergedContext
;
166 * Gets the stream input reader that this event was made by
170 public StreamInputReader
getStreamInputReader() {
171 return streamInputReader
;
175 * Gets the context of packet the event is in.
177 * @return the packet context
179 public StructDefinition
getPacketContext() {
180 return streamInputReader
.getCurrentPacketContext();
184 * gets the CPU the event was generated by. Slightly LTTng specific
186 * @return The CPU the event was generated by
188 public int getCPU() {
189 return streamInputReader
.getCPU();
193 * @return the timestamp
195 public long getTimestamp() {
201 * the timestamp to set
203 public void setTimestamp(long timestamp
) {
204 this.timestamp
= timestamp
;
211 public void setContext(StructDefinition context
) {
212 this.context
= context
;
219 public void setFields(StructDefinition fields
) {
220 this.fields
= fields
;
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
228 public Definition
lookupDefinition(String lookupPath
) {
229 if (lookupPath
.equals("context")) { //$NON-NLS-1$
231 } else if (lookupPath
.equals("fields")) { //$NON-NLS-1$
239 public String
toString() {
240 HashMap
<String
, Definition
> definitions
;
242 StringBuilder retString
= new StringBuilder();
243 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
245 retString
.append("Event type: " + declaration
.getName() + cr
); //$NON-NLS-1$
246 retString
.append("Timestamp: " + Long
.toString(timestamp
) + cr
); //$NON-NLS-1$
248 if (context
!= null) {
249 definitions
= context
.getDefinitions();
250 list
= context
.getDeclaration().getFieldsList();
252 for (String field
: list
) {
253 retString
.append(field
254 + " : " + definitions
.get(field
).toString() + cr
); //$NON-NLS-1$
258 if (fields
!= null) {
259 definitions
= fields
.getDefinitions();
260 list
= fields
.getDeclaration().getFieldsList();
262 for (String field
: list
) {
263 retString
.append(field
264 + " : " + definitions
.get(field
).toString() + cr
); //$NON-NLS-1$
268 return retString
.toString();