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$
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private final CtfTmfTrace fTrace
;
50 private final long timestamp
;
51 private final int sourceCPU
;
52 private final long typeId
;
53 private final String eventName
;
54 private final String fileName
;
56 private final CtfTmfContent fContent
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Usual CTFEvent constructor, where we read an event from the trace (via
64 * the StreamInputReader).
67 * CTF EventDefinition object corresponding to this trace event
69 * The path to the trace file
71 * The trace from which this event originates
73 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
74 CtfTmfTrace originTrace
) {
75 this.fTrace
= originTrace
;
77 if (eventDef
== null) {
81 this.fileName
= NO_STREAM
;
82 this.eventName
= EMPTY_CTF_EVENT_NAME
;
87 /* Read the base event info */
88 this.timestamp
= this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef
.getTimestamp());
89 this.sourceCPU
= eventDef
.getCPU();
90 this.typeId
= eventDef
.getDeclaration().getId();
91 this.eventName
= eventDef
.getDeclaration().getName();
92 this.fileName
= fileName
;
95 this.fContent
= new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
96 parseFields(eventDef
));
100 * Extract the field information from the structDefinition haze-inducing
101 * mess, and put them into something ITmfEventField can cope with.
104 * CTF EventDefinition to read
105 * @return CtfTmfEventField[] The array of fields that were read
107 public 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 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
131 * CtfTmfEvent to copy
133 public CtfTmfEvent(CtfTmfEvent other
) {
134 this.fTrace
= other
.getTrace();
135 /* Primitives, those will be copied by value */
136 this.timestamp
= other
.timestamp
;
137 this.sourceCPU
= other
.sourceCPU
;
138 this.typeId
= other
.typeId
;
140 /* Strings are immutable, it's safe to shallow-copy them */
141 this.eventName
= other
.eventName
;
142 this.fileName
= other
.fileName
;
144 /* Copy the fields over */
145 this.fContent
= (CtfTmfContent
) other
.fContent
.clone();
149 * Inner constructor to create "null" events. Don't use this directly in
150 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
153 * This needs to be public however because it's used in extension points,
154 * and the framework will use this constructor to get the class type.
156 public CtfTmfEvent() {
161 this.fileName
= NO_STREAM
;
162 this.eventName
= EMPTY_CTF_EVENT_NAME
;
163 this.fContent
= new CtfTmfContent("", new CtfTmfEventField
[0]); //$NON-NLS-1$
166 // ------------------------------------------------------------------------
167 // Getters/Setters/Predicates
168 // ------------------------------------------------------------------------
170 private static CtfTmfEvent nullEvent
= null;
175 * @return an empty event. */
176 public static CtfTmfEvent
getNullEvent() {
177 if (nullEvent
== null) {
178 nullEvent
= new CtfTmfEvent();
184 * Gets the current timestamp of the event
186 * @return the current timestamp (long) */
187 public long getTimestampValue() {
188 return this.timestamp
;
192 * Gets the cpu core the event was recorded on.
194 * @return the cpu id for a given source. In lttng it's from CPUINFO */
195 public int getCPU() {
196 return this.sourceCPU
;
200 * Return this event's ID, according to the trace's metadata. Watch out,
201 * this ID is not constant from one trace to another for the same event
202 * types! Use "getEventName()" for a constant reference.
205 * @return the event ID */
206 public long getID() {
211 * Gets the name of a current event.
213 * @return the event name */
214 public String
getEventName() {
219 * Gets the channel name of a field.
221 * @return the channel name. */
222 public String
getChannelName() {
223 return this.fileName
;
228 * @return CtfTmfTrace
229 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
232 public CtfTmfTrace
getTrace() {
239 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
242 public long getRank() {
243 // TODO Auto-generated method stub
247 private ITmfTimestamp fTimestamp
= null;
249 // TODO Benchmark if the singleton approach is faster than just
250 // instantiating a final fTimestramp right away at creation time
252 * Method getTimestamp.
253 * @return ITmfTimestamp
254 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
257 public ITmfTimestamp
getTimestamp() {
258 if (fTimestamp
== null) {
259 fTimestamp
= new CtfTmfTimestamp(timestamp
);
264 String fSource
= null;
268 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
271 public String
getSource() {
272 // TODO Returns CPU for now
273 if(fSource
== null) {
274 fSource
= Integer
.toString(getCPU());
281 * @return ITmfEventType
282 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
285 public ITmfEventType
getType() {
286 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
287 if( ctfTmfEventType
== null ){
288 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
290 return ctfTmfEventType
;
295 * @return ITmfEventField
296 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
299 public ITmfEventField
getContent() {
303 String fReference
= null;
305 * Method getReference.
307 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
310 public String
getReference() {
311 if( fReference
== null){
312 fReference
= getChannelName();
319 * @return CtfTmfEvent
320 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
323 public CtfTmfEvent
clone() {
324 return new CtfTmfEvent(this);