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
.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 IEventDeclaration fDeclaration
;
40 * The timestamp of the current event.
42 private long fTimestamp
;
45 * The event context structure definition.
47 private StructDefinition fContext
;
50 * The event fields structure definition.
52 private StructDefinition fFields
;
55 * The StreamInputReader that reads this event definition.
57 private final StreamInputReader fStreamInputReader
;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * Constructs an event definition.
67 * The corresponding event declaration
68 * @param streamInputReader
69 * The SIR from where this EventDef was read
72 public EventDefinition(IEventDeclaration declaration
,
73 StreamInputReader streamInputReader
) {
74 fDeclaration
= declaration
;
75 fStreamInputReader
= streamInputReader
;
78 // ------------------------------------------------------------------------
79 // Getters/Setters/Predicates
80 // ------------------------------------------------------------------------
83 public String
getPath() {
84 return "event"; //$NON-NLS-1$
88 * Gets the declaration (the form) of the data
90 * @return the event declaration
93 public IEventDeclaration
getDeclaration() {
98 * Gets the fields of a definition
100 * @return the fields of a definition in struct form. Can be null.
102 public StructDefinition
getFields() {
107 * Gets the context of this event without the context of the stream
109 * @return the context in struct form
112 public StructDefinition
getEventContext() {
117 * Gets the context of this event within a stream
119 * @return the context in struct form
121 public StructDefinition
getContext() {
122 final StructDefinition streamContext
=
123 fStreamInputReader
.getPacketReader().getStreamEventContextDef();
125 /* Most common case so far */
126 if (streamContext
== null) {
130 /* streamContext is not null, but the context of the event is null */
131 if (fContext
== null) {
132 return streamContext
;
135 /* The stream context and event context are assigned. */
136 StructDeclaration mergedDeclaration
= new StructDeclaration(1);
138 /* Add fields from the stream */
139 Map
<String
, Definition
> defs
= streamContext
.getDefinitions();
140 for (Entry
<String
, Definition
> entry
: defs
.entrySet()) {
141 mergedDeclaration
.addField(entry
.getKey(), entry
.getValue().getDeclaration());
144 /* Add fields from the event context, overwrite the stream ones if needed. */
145 for (Entry
<String
, Definition
> entry
: fContext
.getDefinitions().entrySet()) {
146 mergedDeclaration
.addField(entry
.getKey(), entry
.getValue().getDeclaration());
149 StructDefinition mergedContext
= mergedDeclaration
.createDefinition(null, "context"); //$NON-NLS-1$
150 for (String key
: mergedContext
.getDefinitions().keySet()) {
151 final Definition lookupDefinition
= fContext
.lookupDefinition(key
);
153 * If the key is in the event context, add it from there, if it is
154 * not, then it's in the stream. There is a priority with scoping so
155 * if there is a field like "context" in both stream and context,
156 * you display the context.
158 if (lookupDefinition
!= null) {
159 mergedContext
.getDefinitions().put(key
, lookupDefinition
);
161 mergedContext
.getDefinitions().put(key
, streamContext
.lookupDefinition(key
));
164 return mergedContext
;
168 * Gets the stream input reader that this event was made by
172 public StreamInputReader
getStreamInputReader() {
173 return fStreamInputReader
;
177 * Gets the context of packet the event is in.
179 * @return the packet context
181 public StructDefinition
getPacketContext() {
182 return fStreamInputReader
.getCurrentPacketContext();
186 * gets the CPU the event was generated by. Slightly LTTng specific
188 * @return The CPU the event was generated by
190 public int getCPU() {
191 return fStreamInputReader
.getCPU();
195 * @return the timestamp
197 public long getTimestamp() {
203 * the timestamp to set
205 public void setTimestamp(long timestamp
) {
206 fTimestamp
= timestamp
;
213 public void setContext(StructDefinition context
) {
221 public void setFields(StructDefinition fields
) {
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
230 public Definition
lookupDefinition(String lookupPath
) {
231 if (lookupPath
.equals("context")) { //$NON-NLS-1$
233 } else if (lookupPath
.equals("fields")) { //$NON-NLS-1$
241 public String
toString() {
242 Map
<String
, Definition
> definitions
;
244 StringBuilder retString
= new StringBuilder();
245 final String cr
= System
.getProperty("line.separator");//$NON-NLS-1$
247 retString
.append("Event type: " + fDeclaration
.getName() + cr
); //$NON-NLS-1$
248 retString
.append("Timestamp: " + Long
.toString(fTimestamp
) + cr
); //$NON-NLS-1$
250 if (fContext
!= null) {
251 definitions
= fContext
.getDefinitions();
252 list
= fContext
.getDeclaration().getFieldsList();
254 for (String field
: list
) {
255 retString
.append(field
256 + " : " + definitions
.get(field
).toString() + cr
); //$NON-NLS-1$
260 if (fFields
!= null) {
261 definitions
= fFields
.getDefinitions();
262 list
= fFields
.getDeclaration().getFieldsList();
264 for (String field
: list
) {
265 retString
.append(field
266 + " : " + definitions
.get(field
).toString() + cr
); //$NON-NLS-1$
270 return retString
.toString();