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 Long offset
= originTrace
.getCTFTrace().getOffset();
89 this.timestamp
= eventDef
.getTimestamp() + offset
;
90 this.sourceCPU
= eventDef
.getCPU();
91 this.typeId
= eventDef
.getDeclaration().getId();
92 this.eventName
= eventDef
.getDeclaration().getName();
93 this.fileName
= fileName
;
96 this.fContent
= new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
97 parseFields(eventDef
));
101 * Extract the field information from the structDefinition haze-inducing
102 * mess, and put them into something ITmfEventField can cope with.
105 * CTF EventDefinition to read
106 * @return CtfTmfEventField[] The array of fields that were read
108 public static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
109 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
111 StructDefinition structFields
= eventDef
.getFields();
112 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
114 Definition curFieldDef
;
115 CtfTmfEventField curField
;
116 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
117 while(it
.hasNext()) {
118 Entry
<String
, Definition
> entry
= it
.next();
119 curFieldName
= entry
.getKey();
120 curFieldDef
= entry
.getValue();
121 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
122 fields
.add(curField
);
125 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
132 * CtfTmfEvent to copy
134 public CtfTmfEvent(CtfTmfEvent other
) {
135 this.fTrace
= other
.getTrace();
136 /* Primitives, those will be copied by value */
137 this.timestamp
= other
.timestamp
;
138 this.sourceCPU
= other
.sourceCPU
;
139 this.typeId
= other
.typeId
;
141 /* Strings are immutable, it's safe to shallow-copy them */
142 this.eventName
= other
.eventName
;
143 this.fileName
= other
.fileName
;
145 /* Copy the fields over */
146 this.fContent
= (CtfTmfContent
) other
.fContent
.clone();
150 * Inner constructor to create "null" events. Don't use this directly in
151 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
154 * This needs to be public however because it's used in extension points,
155 * and the framework will use this constructor to get the class type.
157 public CtfTmfEvent() {
162 this.fileName
= NO_STREAM
;
163 this.eventName
= EMPTY_CTF_EVENT_NAME
;
164 this.fContent
= new CtfTmfContent("", new CtfTmfEventField
[0]); //$NON-NLS-1$
167 // ------------------------------------------------------------------------
168 // Getters/Setters/Predicates
169 // ------------------------------------------------------------------------
171 private static CtfTmfEvent nullEvent
= null;
176 * @return an empty event. */
177 public static CtfTmfEvent
getNullEvent() {
178 if (nullEvent
== null) {
179 nullEvent
= new CtfTmfEvent();
185 * Gets the current timestamp of the event
187 * @return the current timestamp (long) */
188 public long getTimestampValue() {
189 return this.timestamp
;
193 * Gets the cpu core the event was recorded on.
195 * @return the cpu id for a given source. In lttng it's from CPUINFO */
196 public int getCPU() {
197 return this.sourceCPU
;
201 * Return this event's ID, according to the trace's metadata. Watch out,
202 * this ID is not constant from one trace to another for the same event
203 * types! Use "getEventName()" for a constant reference.
206 * @return the event ID */
207 public long getID() {
212 * Gets the name of a current event.
214 * @return the event name */
215 public String
getEventName() {
220 * Gets the channel name of a field.
222 * @return the channel name. */
223 public String
getChannelName() {
224 return this.fileName
;
229 * @return CtfTmfTrace
230 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
233 public CtfTmfTrace
getTrace() {
240 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
243 public long getRank() {
244 // TODO Auto-generated method stub
248 private ITmfTimestamp fTimestamp
= null;
250 // TODO Benchmark if the singleton approach is faster than just
251 // instantiating a final fTimestramp right away at creation time
253 * Method getTimestamp.
254 * @return ITmfTimestamp
255 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
258 public ITmfTimestamp
getTimestamp() {
259 if (fTimestamp
== null) {
260 fTimestamp
= new CtfTmfTimestamp(timestamp
);
265 String fSource
= null;
269 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
272 public String
getSource() {
273 // TODO Returns CPU for now
274 if(fSource
== null) {
275 fSource
= Integer
.toString(getCPU());
282 * @return ITmfEventType
283 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
286 public ITmfEventType
getType() {
287 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
288 if( ctfTmfEventType
== null ){
289 ctfTmfEventType
= new CtfTmfEventType( this.getEventName(), this.getContent());
291 return ctfTmfEventType
;
296 * @return ITmfEventField
297 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
300 public ITmfEventField
getContent() {
304 String fReference
= null;
306 * Method getReference.
308 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
311 public String
getReference() {
312 if( fReference
== null){
313 fReference
= getChannelName();
320 * @return CtfTmfEvent
321 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
324 public CtfTmfEvent
clone() {
325 return new CtfTmfEvent(this);