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
.List
;
17 import java
.util
.Map
.Entry
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
28 * <b><u>CTFEvent</u></b>
30 * This is a wrapper class around CTF's Event Definition/Declaration so that we
31 * can map all types of Declaration to native Java types.
33 public final class CtfTmfEvent
implements ITmfEvent
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
40 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
41 private static final String CONTEXT_ID
= "Ctf Event"; //$NON-NLS-1$
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final CtfTmfTrace fTrace
;
48 private final long timestamp
;
49 private final int sourceCPU
;
50 private final long typeId
;
51 private final String eventName
;
52 private final String fileName
;
54 private final CtfTmfContent fContent
;
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * Usual CTFEvent constructor, where we read an event from the trace (via
62 * the StreamInputReader).
67 public CtfTmfEvent(EventDefinition eventDef
, String fileName
,
68 CtfTmfTrace originTrace
) {
69 this.fTrace
= originTrace
;
71 if (eventDef
== null) {
75 this.fileName
= NO_STREAM
;
76 this.eventName
= EMPTY_CTF_EVENT_NAME
;
81 /* Read the base event info */
82 Long offset
= originTrace
.getCTFTrace().getOffset();
83 this.timestamp
= eventDef
.timestamp
+ offset
;
84 this.sourceCPU
= eventDef
.getCPU();
85 this.typeId
= eventDef
.getDeclaration().getId();
86 this.eventName
= eventDef
.getDeclaration().getName();
87 this.fileName
= fileName
;
90 this.fContent
= new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
91 parseFields(eventDef
));
95 * Extract the field information from the structDefinition haze-inducing
96 * mess, and put them into something ITmfEventField can cope with.
101 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
102 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
104 StructDefinition structFields
= eventDef
.getFields();
105 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
107 Definition curFieldDef
;
108 CtfTmfEventField curField
;
110 for (Entry
<String
, Definition
> entry
: definitions
.entrySet()) {
111 curFieldName
= entry
.getKey();
112 curFieldDef
= entry
.getValue();
113 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
114 if (curField
== null) {
115 // TmfCorePlugin.getDefault().log(
116 // "We've parsed an unimplemented field type for event \"" + this.eventName //$NON-NLS-1$
117 // + "\", field \"" + curFieldName + "\" of type " + curFieldDef.getClass().toString()); //$NON-NLS-1$ //$NON-NLS-2$
119 fields
.add(curField
);
122 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
130 public CtfTmfEvent(CtfTmfEvent other
) {
131 this.fTrace
= other
.getTrace();
132 /* Primitives, those will be copied by value */
133 this.timestamp
= other
.timestamp
;
134 this.sourceCPU
= other
.sourceCPU
;
135 this.typeId
= other
.typeId
;
137 /* Strings are immutable, it's safe to shallow-copy them */
138 this.eventName
= other
.eventName
;
139 this.fileName
= other
.fileName
;
141 /* Copy the fields over */
142 this.fContent
= (CtfTmfContent
) other
.fContent
.clone();
146 * Inner constructor to create "null" events. Don't use this directly, use
147 * CTFEvent.getNullEvent();
149 public CtfTmfEvent() {
154 this.fileName
= NO_STREAM
;
155 this.eventName
= EMPTY_CTF_EVENT_NAME
;
156 this.fContent
= null;
159 // ------------------------------------------------------------------------
160 // Getters/Setters/Predicates
161 // ------------------------------------------------------------------------
163 private static CtfTmfEvent nullEvent
= null;
168 * @return an empty event.
170 public static CtfTmfEvent
getNullEvent() {
171 if (nullEvent
== null) {
172 nullEvent
= new CtfTmfEvent();
178 * Gets the current timestamp of the event
180 * @return the current timestamp (long)
182 public long getTimestampValue() {
183 return this.timestamp
;
187 * Gets the cpu core the event was recorded on.
189 * @return the cpu id for a given source. In lttng it's from CPUINFO
191 public int getCPU() {
192 return this.sourceCPU
;
196 * Return this event's ID, according to the trace's metadata. Watch out,
197 * this ID is not constant from one trace to another for the same event
198 * types! Use "getEventName()" for a constant reference.
200 * @return the event ID
202 public long getID() {
207 * Gets the name of a current event.
209 * @return the event name
211 public String
getEventName() {
216 * Gets the channel name of a field.
218 * @return the channel name.
220 public String
getChannelName() {
221 return this.fileName
;
225 public CtfTmfTrace
getTrace() {
230 public long getRank() {
231 // TODO Auto-generated method stub
235 private ITmfTimestamp fTimestamp
= null;
237 // TODO Benchmark if the singleton approach is faster than just
238 // instantiating a final fTimestramp right away at creation time
240 public ITmfTimestamp
getTimestamp() {
241 if (fTimestamp
== null) {
242 fTimestamp
= new CtfTmfTimestamp(timestamp
, fTrace
);
247 String fSource
= null;
249 public String
getSource() {
250 // TODO Returns CPU for now
251 if(fSource
== null) {
252 fSource
= Integer
.toString(getCPU());
257 private CtfTmfEventType type
= null;
259 public ITmfEventType
getType() {
261 type
= new CtfTmfEventType(CONTEXT_ID
, eventName
, fContent
);
267 public ITmfEventField
getContent() {
271 String fReference
= null;
273 public String
getReference() {
274 if( fReference
== null){
275 fReference
= getChannelName();
281 public CtfTmfEvent
clone() {
282 return new CtfTmfEvent(this);