1 /*******************************************************************************
2 * Copyright (c) 2011-2013 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
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventPropertySource
;
35 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
38 * A wrapper class around CTF's Event Definition/Declaration that maps all
39 * types of Declaration to native Java types.
42 * @author Alexandre Montplaisir
45 public final class CtfTmfEvent
implements ITmfEvent
, Cloneable
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
52 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
54 /** Prefix for Context information stored as CtfTmfEventfield */
55 private static final String CONTEXT_FIELD_PREFIX
= "context."; //$NON-NLS-1$
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private final CtfTmfTrace fTrace
;
62 private final ITmfTimestamp fTimestamp
;
63 private final int sourceCPU
;
64 private final long typeId
;
65 private final String eventName
;
66 private final String fileName
;
68 private final TmfEventField fContent
;
69 private final IEventDeclaration fDeclaration
;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * Usual CTFEvent constructor, where we read an event from the trace (via
77 * the StreamInputReader).
80 * CTF EventDefinition object corresponding to this trace event
82 * The path to the trace file
84 * The trace from which this event originates
86 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
87 CtfTmfTrace originTrace
) {
88 this.fTrace
= originTrace
;
90 if (eventDef
== null) {
91 this.fTimestamp
= new CtfTmfTimestamp(-1);
94 this.fileName
= NO_STREAM
;
95 this.eventName
= EMPTY_CTF_EVENT_NAME
;
97 this.fDeclaration
= null;
101 /* Read the base event info */
102 long ts
= this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef
.getTimestamp());
103 this.fTimestamp
= new CtfTmfTimestamp(ts
);
104 this.sourceCPU
= eventDef
.getCPU();
105 this.typeId
= eventDef
.getDeclaration().getId();
106 this.eventName
= eventDef
.getDeclaration().getName();
107 this.fileName
= fileName
;
109 /* Read the fields */
110 this.fContent
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, parseFields(eventDef
));
112 /* Keep a reference to this event's CTF declaration */
113 this.fDeclaration
= eventDef
.getDeclaration();
117 * Extract the field information from the structDefinition haze-inducing
118 * mess, and put them into something ITmfEventField can cope with.
120 private CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
121 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
123 StructDefinition structFields
= eventDef
.getFields();
124 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
125 String curFieldName
= null;
126 Definition curFieldDef
;
127 CtfTmfEventField curField
;
128 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
129 while(it
.hasNext()) {
130 Entry
<String
, Definition
> entry
= it
.next();
131 curFieldName
= entry
.getKey();
132 curFieldDef
= entry
.getValue();
133 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
134 fields
.add(curField
);
137 /* Add context information as CtfTmfEventField */
139 StructDefinition structContext
= eventDef
.getContext();
140 if (structContext
!= null) {
141 definitions
= structContext
.getDefinitions();
142 String curContextName
;
143 Definition curContextDef
;
144 CtfTmfEventField curContext
;
145 it
= definitions
.entrySet().iterator();
146 while(it
.hasNext()) {
147 Entry
<String
, Definition
> entry
= it
.next();
148 /* This is to get the instruction pointer if available */
149 if (entry
.getKey().equals("_ip") && //$NON-NLS-1$
150 (entry
.getValue() instanceof IntegerDefinition
)) {
151 ip
= ((IntegerDefinition
) entry
.getValue()).getValue();
153 /* Prefix field name to */
154 curContextName
= CONTEXT_FIELD_PREFIX
+ entry
.getKey();
155 curContextDef
= entry
.getValue();
156 curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
157 fields
.add(curContext
);
161 final String name
= eventDef
.getDeclaration().getName();
162 List
<CTFCallsite
> eventList
= fTrace
.getCTFTrace().getCallsiteCandidates(name
);
163 if (!eventList
.isEmpty()) {
164 final String callsite
= "callsite"; //$NON-NLS-1$
165 if (eventList
.size() == 1 || ip
== -1) {
166 CTFCallsite cs
= eventList
.get(0);
167 fields
.add(new CTFStringField(cs
.toString(), callsite
));
169 fields
.add(new CTFStringField(
170 fTrace
.getCTFTrace().getCallsite(name
, ip
).toString(),
175 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
182 * CtfTmfEvent to copy
184 public CtfTmfEvent(CtfTmfEvent other
) {
185 /* There is only one reference to the trace, so we can shallow-copy it */
186 this.fTrace
= other
.getTrace();
188 /* Copy the timestamp (immutable) */
189 this.fTimestamp
= other
.fTimestamp
;
191 /* Primitives, those will be copied by value */
192 this.sourceCPU
= other
.sourceCPU
;
193 this.typeId
= other
.typeId
;
195 /* Strings are immutable, it's safe to shallow-copy them */
196 this.eventName
= other
.eventName
;
197 this.fileName
= other
.fileName
;
199 /* Copy the fields over (immutable) */
200 this.fContent
= other
.fContent
;
203 * Copy the reference to the custom attributes (should be the same
204 * object for all events of this type)
206 this.fDeclaration
= other
.fDeclaration
;
210 * Inner constructor to create "null" events. Don't use this directly in
211 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
214 * This needs to be public however because it's used in extension points,
215 * and the framework will use this constructor to get the class type.
217 public CtfTmfEvent() {
219 this.fTimestamp
= new CtfTmfTimestamp(-1);
222 this.fileName
= NO_STREAM
;
223 this.eventName
= EMPTY_CTF_EVENT_NAME
;
224 this.fContent
= new TmfEventField("", new CtfTmfEventField
[0]); //$NON-NLS-1$
225 this.fDeclaration
= null;
228 // ------------------------------------------------------------------------
229 // Getters/Setters/Predicates
230 // ------------------------------------------------------------------------
232 private static CtfTmfEvent nullEvent
= new CtfTmfEvent();
237 * @return An empty event.
239 public static CtfTmfEvent
getNullEvent() {
244 * Gets the cpu core the event was recorded on.
246 * @return The cpu id for a given source. In lttng it's from CPUINFO
248 public int getCPU() {
249 return this.sourceCPU
;
253 * Return this event's ID, according to the trace's metadata.
255 * Watch out, this ID is not constant from one trace to another for the same
256 * event types! Use "getEventName()" for a constant reference.
258 * @return The event ID
260 public long getID() {
265 * Gets the name of a current event.
267 * @return The event name
269 public String
getEventName() {
274 * Gets the channel name of a field.
276 * @return The channel name.
278 public String
getChannelName() {
279 return this.fileName
;
283 public CtfTmfTrace
getTrace() {
288 public long getRank() {
289 // TODO Auto-generated method stub
294 public ITmfTimestamp
getTimestamp() {
299 public String
getSource() {
300 // TODO Returns CPU for now
301 return Integer
.toString(getCPU());
305 public ITmfEventType
getType() {
306 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
307 if( ctfTmfEventType
== null ){
308 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
310 return ctfTmfEventType
;
314 public ITmfEventField
getContent() {
319 public String
getReference() {
320 return getChannelName();
324 * List the custom CTF attributes for events of this type.
326 * @return The list of custom attribute names. Should not be null, but could
330 public Set
<String
> listCustomAttributes() {
331 if (fDeclaration
== null) {
332 return new HashSet
<String
>();
334 return fDeclaration
.getCustomAttributes();
338 * Get the value of a custom CTF attributes for this event's type.
341 * Name of the the custom attribute
342 * @return Value of this attribute, or null if there is no attribute with
346 public String
getCustomAttribute(String name
) {
347 if (fDeclaration
== null) {
350 return fDeclaration
.getCustomAttribute(name
);
354 public CtfTmfEvent
clone() {
355 return new CtfTmfEvent(this);
362 public Object
getAdapter(Class adapter
) {
363 if (adapter
== IPropertySource
.class) {
364 return new TmfEventPropertySource(this);