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
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
31 * A wrapper class around CTF's Event Definition/Declaration that maps all
32 * types of Declaration to native Java types.
35 * @author Alexandre Montplaisir
37 public final class CtfTmfEvent
implements ITmfEvent
, Cloneable
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
44 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
46 /** Prefix for Context information stored as CtfTmfEventfield */
47 private static final String CONTEXT_FIELD_PREFIX
= "context."; //$NON-NLS-1$
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private final CtfTmfTrace fTrace
;
54 private final ITmfTimestamp fTimestamp
;
55 private final int sourceCPU
;
56 private final long typeId
;
57 private final String eventName
;
58 private final String fileName
;
60 private final TmfEventField fContent
;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Usual CTFEvent constructor, where we read an event from the trace (via
68 * the StreamInputReader).
71 * CTF EventDefinition object corresponding to this trace event
73 * The path to the trace file
75 * The trace from which this event originates
77 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
78 CtfTmfTrace originTrace
) {
79 this.fTrace
= originTrace
;
81 if (eventDef
== null) {
82 this.fTimestamp
= new CtfTmfTimestamp(-1);
85 this.fileName
= NO_STREAM
;
86 this.eventName
= EMPTY_CTF_EVENT_NAME
;
91 /* Read the base event info */
92 long ts
= this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef
.getTimestamp());
93 this.fTimestamp
= new CtfTmfTimestamp(ts
);
94 this.sourceCPU
= eventDef
.getCPU();
95 this.typeId
= eventDef
.getDeclaration().getId();
96 this.eventName
= eventDef
.getDeclaration().getName();
97 this.fileName
= fileName
;
100 this.fContent
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, parseFields(eventDef
));
104 * Extract the field information from the structDefinition haze-inducing
105 * mess, and put them into something ITmfEventField can cope with.
107 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
108 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
110 StructDefinition structFields
= eventDef
.getFields();
111 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
113 Definition curFieldDef
;
114 CtfTmfEventField curField
;
115 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
116 while(it
.hasNext()) {
117 Entry
<String
, Definition
> entry
= it
.next();
118 curFieldName
= entry
.getKey();
119 curFieldDef
= entry
.getValue();
120 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
121 fields
.add(curField
);
124 /* Add context information as CtfTmfEventField */
125 StructDefinition structContext
= eventDef
.getContext();
126 if (structContext
!= null) {
127 definitions
= structContext
.getDefinitions();
128 String curContextName
;
129 Definition curContextDef
;
130 CtfTmfEventField curContext
;
131 it
= definitions
.entrySet().iterator();
132 while(it
.hasNext()) {
133 Entry
<String
, Definition
> entry
= it
.next();
134 /* Prefix field name to */
135 curContextName
= CONTEXT_FIELD_PREFIX
+ entry
.getKey();
136 curContextDef
= entry
.getValue();
137 curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
138 fields
.add(curContext
);
142 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
149 * CtfTmfEvent to copy
151 public CtfTmfEvent(CtfTmfEvent other
) {
152 /* There is only one reference to the trace, so we can shallow-copy it */
153 this.fTrace
= other
.getTrace();
157 * FIXME This can be switched to a shallow-copy once timestamps are
160 this.fTimestamp
= new CtfTmfTimestamp(other
.fTimestamp
.getValue());
162 /* Primitives, those will be copied by value */
163 this.sourceCPU
= other
.sourceCPU
;
164 this.typeId
= other
.typeId
;
166 /* Strings are immutable, it's safe to shallow-copy them */
167 this.eventName
= other
.eventName
;
168 this.fileName
= other
.fileName
;
170 /* Copy the fields over */
171 this.fContent
= other
.fContent
.clone();
175 * Inner constructor to create "null" events. Don't use this directly in
176 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
179 * This needs to be public however because it's used in extension points,
180 * and the framework will use this constructor to get the class type.
182 public CtfTmfEvent() {
184 this.fTimestamp
= new CtfTmfTimestamp(-1);
187 this.fileName
= NO_STREAM
;
188 this.eventName
= EMPTY_CTF_EVENT_NAME
;
189 this.fContent
= new TmfEventField("", new CtfTmfEventField
[0]); //$NON-NLS-1$
192 // ------------------------------------------------------------------------
193 // Getters/Setters/Predicates
194 // ------------------------------------------------------------------------
196 private static CtfTmfEvent nullEvent
= new CtfTmfEvent();
201 * @return An empty event.
203 public static CtfTmfEvent
getNullEvent() {
208 * Gets the cpu core the event was recorded on.
210 * @return The cpu id for a given source. In lttng it's from CPUINFO
212 public int getCPU() {
213 return this.sourceCPU
;
217 * Return this event's ID, according to the trace's metadata.
219 * Watch out, this ID is not constant from one trace to another for the same
220 * event types! Use "getEventName()" for a constant reference.
222 * @return The event ID
224 public long getID() {
229 * Gets the name of a current event.
231 * @return The event name
233 public String
getEventName() {
238 * Gets the channel name of a field.
240 * @return The channel name.
242 public String
getChannelName() {
243 return this.fileName
;
247 public CtfTmfTrace
getTrace() {
252 public long getRank() {
253 // TODO Auto-generated method stub
258 public ITmfTimestamp
getTimestamp() {
263 public String
getSource() {
264 // TODO Returns CPU for now
265 return Integer
.toString(getCPU());
269 public ITmfEventType
getType() {
270 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
271 if( ctfTmfEventType
== null ){
272 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
274 return ctfTmfEventType
;
278 public ITmfEventField
getContent() {
283 public String
getReference() {
284 return getChannelName();
288 public CtfTmfEvent
clone() {
289 return new CtfTmfEvent(this);