1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
19 import java
.util
.Map
.Entry
;
21 import org
.eclipse
.core
.runtime
.IAdaptable
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventPropertySource
;
31 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
34 * A wrapper class around CTF's Event Definition/Declaration that maps all
35 * types of Declaration to native Java types.
38 * @author Alexandre Montplaisir
41 public final class CtfTmfEvent
implements ITmfEvent
, IAdaptable
, Cloneable
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
48 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
50 /** Prefix for Context information stored as CtfTmfEventfield */
51 private static final String CONTEXT_FIELD_PREFIX
= "context."; //$NON-NLS-1$
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 private final CtfTmfTrace fTrace
;
58 private final ITmfTimestamp fTimestamp
;
59 private final int sourceCPU
;
60 private final long typeId
;
61 private final String eventName
;
62 private final String fileName
;
64 private final TmfEventField fContent
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * Usual CTFEvent constructor, where we read an event from the trace (via
72 * the StreamInputReader).
75 * CTF EventDefinition object corresponding to this trace event
77 * The path to the trace file
79 * The trace from which this event originates
81 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
82 CtfTmfTrace originTrace
) {
83 this.fTrace
= originTrace
;
85 if (eventDef
== null) {
86 this.fTimestamp
= new CtfTmfTimestamp(-1);
89 this.fileName
= NO_STREAM
;
90 this.eventName
= EMPTY_CTF_EVENT_NAME
;
95 /* Read the base event info */
96 long ts
= this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef
.getTimestamp());
97 this.fTimestamp
= new CtfTmfTimestamp(ts
);
98 this.sourceCPU
= eventDef
.getCPU();
99 this.typeId
= eventDef
.getDeclaration().getId();
100 this.eventName
= eventDef
.getDeclaration().getName();
101 this.fileName
= fileName
;
103 /* Read the fields */
104 this.fContent
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, parseFields(eventDef
));
108 * Extract the field information from the structDefinition haze-inducing
109 * mess, and put them into something ITmfEventField can cope with.
111 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
112 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
114 StructDefinition structFields
= eventDef
.getFields();
115 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
117 Definition curFieldDef
;
118 CtfTmfEventField curField
;
119 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
120 while(it
.hasNext()) {
121 Entry
<String
, Definition
> entry
= it
.next();
122 curFieldName
= entry
.getKey();
123 curFieldDef
= entry
.getValue();
124 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
125 fields
.add(curField
);
128 /* Add context information as CtfTmfEventField */
129 StructDefinition structContext
= eventDef
.getContext();
130 if (structContext
!= null) {
131 definitions
= structContext
.getDefinitions();
132 String curContextName
;
133 Definition curContextDef
;
134 CtfTmfEventField curContext
;
135 it
= definitions
.entrySet().iterator();
136 while(it
.hasNext()) {
137 Entry
<String
, Definition
> entry
= it
.next();
138 /* Prefix field name to */
139 curContextName
= CONTEXT_FIELD_PREFIX
+ entry
.getKey();
140 curContextDef
= entry
.getValue();
141 curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
142 fields
.add(curContext
);
146 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
153 * CtfTmfEvent to copy
155 public CtfTmfEvent(CtfTmfEvent other
) {
156 /* There is only one reference to the trace, so we can shallow-copy it */
157 this.fTrace
= other
.getTrace();
161 * FIXME This can be switched to a shallow-copy once timestamps are
164 this.fTimestamp
= new CtfTmfTimestamp(other
.fTimestamp
.getValue());
166 /* Primitives, those will be copied by value */
167 this.sourceCPU
= other
.sourceCPU
;
168 this.typeId
= other
.typeId
;
170 /* Strings are immutable, it's safe to shallow-copy them */
171 this.eventName
= other
.eventName
;
172 this.fileName
= other
.fileName
;
174 /* Copy the fields over */
175 this.fContent
= other
.fContent
.clone();
179 * Inner constructor to create "null" events. Don't use this directly in
180 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
183 * This needs to be public however because it's used in extension points,
184 * and the framework will use this constructor to get the class type.
186 public CtfTmfEvent() {
188 this.fTimestamp
= new CtfTmfTimestamp(-1);
191 this.fileName
= NO_STREAM
;
192 this.eventName
= EMPTY_CTF_EVENT_NAME
;
193 this.fContent
= new TmfEventField("", new CtfTmfEventField
[0]); //$NON-NLS-1$
196 // ------------------------------------------------------------------------
197 // Getters/Setters/Predicates
198 // ------------------------------------------------------------------------
200 private static CtfTmfEvent nullEvent
= new CtfTmfEvent();
205 * @return An empty event.
207 public static CtfTmfEvent
getNullEvent() {
212 * Gets the cpu core the event was recorded on.
214 * @return The cpu id for a given source. In lttng it's from CPUINFO
216 public int getCPU() {
217 return this.sourceCPU
;
221 * Return this event's ID, according to the trace's metadata.
223 * Watch out, this ID is not constant from one trace to another for the same
224 * event types! Use "getEventName()" for a constant reference.
226 * @return The event ID
228 public long getID() {
233 * Gets the name of a current event.
235 * @return The event name
237 public String
getEventName() {
242 * Gets the channel name of a field.
244 * @return The channel name.
246 public String
getChannelName() {
247 return this.fileName
;
251 public CtfTmfTrace
getTrace() {
256 public long getRank() {
257 // TODO Auto-generated method stub
262 public ITmfTimestamp
getTimestamp() {
267 public String
getSource() {
268 // TODO Returns CPU for now
269 return Integer
.toString(getCPU());
273 public ITmfEventType
getType() {
274 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
275 if( ctfTmfEventType
== null ){
276 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
278 return ctfTmfEventType
;
282 public ITmfEventField
getContent() {
287 public String
getReference() {
288 return getChannelName();
292 public CtfTmfEvent
clone() {
293 return new CtfTmfEvent(this);
297 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
300 public Object
getAdapter(Class adapter
) {
301 if (adapter
== IPropertySource
.class) {
302 return new TmfEventPropertySource(this);