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
9 * Contributors: Alexandre Montplaisir - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
29 * A wrapper class around CTF's Event Definition/Declaration that maps all
30 * types of Declaration to native Java types.
33 * @author Alexandre Montplaisir
35 public final class CtfTmfEvent
implements ITmfEvent
, Cloneable
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
42 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
44 /** Prefix for Context information stored as CtfTmfEventfield */
45 private static final String CONTEXT_FIELD_PREFIX
= "context."; //$NON-NLS-1$
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private final CtfTmfTrace fTrace
;
52 private final long timestamp
;
53 private final int sourceCPU
;
54 private final long typeId
;
55 private final String eventName
;
56 private final String fileName
;
58 private final CtfTmfContent fContent
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * Usual CTFEvent constructor, where we read an event from the trace (via
66 * the StreamInputReader).
69 * CTF EventDefinition object corresponding to this trace event
71 * The path to the trace file
73 * The trace from which this event originates
75 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
76 CtfTmfTrace originTrace
) {
77 this.fTrace
= originTrace
;
79 if (eventDef
== null) {
83 this.fileName
= NO_STREAM
;
84 this.eventName
= EMPTY_CTF_EVENT_NAME
;
89 /* Read the base event info */
90 this.timestamp
= this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef
.getTimestamp());
91 this.sourceCPU
= eventDef
.getCPU();
92 this.typeId
= eventDef
.getDeclaration().getId();
93 this.eventName
= eventDef
.getDeclaration().getName();
94 this.fileName
= fileName
;
97 this.fContent
= new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
98 parseFields(eventDef
));
102 * Extract the field information from the structDefinition haze-inducing
103 * mess, and put them into something ITmfEventField can cope with.
106 * CTF EventDefinition to read
107 * @return CtfTmfEventField[] The array of fields that were read
109 public static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
110 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
112 StructDefinition structFields
= eventDef
.getFields();
113 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
115 Definition curFieldDef
;
116 CtfTmfEventField curField
;
117 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
118 while(it
.hasNext()) {
119 Entry
<String
, Definition
> entry
= it
.next();
120 curFieldName
= entry
.getKey();
121 curFieldDef
= entry
.getValue();
122 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
123 fields
.add(curField
);
126 /* Add context information as CtfTmfEventField */
127 StructDefinition structContext
= eventDef
.getContext();
128 if (structContext
!= null) {
129 definitions
= structContext
.getDefinitions();
130 String curContextName
;
131 Definition curContextDef
;
132 CtfTmfEventField curContext
;
133 it
= definitions
.entrySet().iterator();
134 while(it
.hasNext()) {
135 Entry
<String
, Definition
> entry
= it
.next();
136 /* Prefix field name to */
137 curContextName
= CONTEXT_FIELD_PREFIX
+ entry
.getKey();
138 curContextDef
= entry
.getValue();
139 curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
140 fields
.add(curContext
);
144 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
151 * CtfTmfEvent to copy
153 public CtfTmfEvent(CtfTmfEvent other
) {
154 this.fTrace
= other
.getTrace();
155 /* Primitives, those will be copied by value */
156 this.timestamp
= other
.timestamp
;
157 this.sourceCPU
= other
.sourceCPU
;
158 this.typeId
= other
.typeId
;
160 /* Strings are immutable, it's safe to shallow-copy them */
161 this.eventName
= other
.eventName
;
162 this.fileName
= other
.fileName
;
164 /* Copy the fields over */
165 this.fContent
= (CtfTmfContent
) other
.fContent
.clone();
166 this.fTimestamp
= other
.fTimestamp
.clone();
170 * Inner constructor to create "null" events. Don't use this directly in
171 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
174 * This needs to be public however because it's used in extension points,
175 * and the framework will use this constructor to get the class type.
177 public CtfTmfEvent() {
182 this.fileName
= NO_STREAM
;
183 this.eventName
= EMPTY_CTF_EVENT_NAME
;
184 this.fContent
= new CtfTmfContent("", new CtfTmfEventField
[0]); //$NON-NLS-1$
187 // ------------------------------------------------------------------------
188 // Getters/Setters/Predicates
189 // ------------------------------------------------------------------------
191 private static CtfTmfEvent nullEvent
= null;
196 * @return an empty event. */
197 public static CtfTmfEvent
getNullEvent() {
198 if (nullEvent
== null) {
199 nullEvent
= new CtfTmfEvent();
205 * Gets the current timestamp of the event
207 * @return the current timestamp (long) */
208 public long getTimestampValue() {
209 return this.timestamp
;
213 * Gets the cpu core the event was recorded on.
215 * @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. Watch out,
222 * this ID is not constant from one trace to another for the same event
223 * types! Use "getEventName()" for a constant reference.
226 * @return the event ID */
227 public long getID() {
232 * Gets the name of a current event.
234 * @return the event name */
235 public String
getEventName() {
240 * Gets the channel name of a field.
242 * @return the channel name. */
243 public String
getChannelName() {
244 return this.fileName
;
249 * @return CtfTmfTrace
250 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
253 public CtfTmfTrace
getTrace() {
260 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
263 public long getRank() {
264 // TODO Auto-generated method stub
268 private ITmfTimestamp fTimestamp
= null;
270 // TODO Benchmark if the singleton approach is faster than just
271 // instantiating a final fTimestramp right away at creation time
273 * Method getTimestamp.
274 * @return ITmfTimestamp
275 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
278 public ITmfTimestamp
getTimestamp() {
279 if (fTimestamp
== null) {
280 fTimestamp
= new CtfTmfTimestamp(timestamp
);
285 String fSource
= null;
289 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
292 public String
getSource() {
293 // TODO Returns CPU for now
294 if(fSource
== null) {
295 fSource
= Integer
.toString(getCPU());
302 * @return ITmfEventType
303 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
306 public ITmfEventType
getType() {
307 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
308 if( ctfTmfEventType
== null ){
309 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
311 return ctfTmfEventType
;
316 * @return ITmfEventField
317 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
320 public ITmfEventField
getContent() {
324 String fReference
= null;
326 * Method getReference.
328 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
331 public String
getReference() {
332 if( fReference
== null){
333 fReference
= getChannelName();
340 * @return CtfTmfEvent
341 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
344 public CtfTmfEvent
clone() {
345 return new CtfTmfEvent(this);