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
.ctf
.core
.trace
.StreamInputReader
;
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
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
31 * <b><u>CTFEvent</u></b>
33 * This is a wrapper class around CTF's Event Definition/Declaration so that we
34 * can map all types of Declaration to native Java types.
36 public final class CtfTmfEvent
implements ITmfEvent
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
43 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 TmfEventField fContent
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Usual CTFEvent constructor, where we read an event from the trace (via
64 * the StreamInputReader).
69 public CtfTmfEvent(EventDefinition eventDef
, StreamInputReader top
,
70 CtfTmfTrace originTrace
) {
71 this.fTrace
= originTrace
;
73 if (eventDef
== null) {
77 this.fileName
= NO_STREAM
;
78 this.eventName
= EMPTY_CTF_EVENT_NAME
;
83 /* Read the base event info */
84 // FIXME restore once the CTF parser with clocks gets merged
85 //Long offset = originTrace.getCTFTrace().getOffset();
87 this.timestamp
= eventDef
.timestamp
+ offset
;
88 this.sourceCPU
= eventDef
.getCPU();
89 this.typeId
= eventDef
.getDeclaration().getId();
90 this.eventName
= eventDef
.getDeclaration().getName();
91 this.fileName
= top
.getStreamInput().getFilename();
94 this.fContent
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
,
95 parseFields(eventDef
));
99 * Extract the field information from the structDefinition haze-inducing
100 * mess, and put them into something ITmfEventField can cope with.
105 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
106 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
108 StructDefinition structFields
= eventDef
.getFields();
109 HashMap
<String
, Definition
> definitions
= structFields
.getDefinitions();
111 Definition curFieldDef
;
112 CtfTmfEventField curField
;
114 for (Entry
<String
, Definition
> entry
: definitions
.entrySet()) {
115 curFieldName
= entry
.getKey();
116 curFieldDef
= entry
.getValue();
117 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
118 if (curField
== null) {
119 // TmfCorePlugin.getDefault().log(
120 // "We've parsed an unimplemented field type for event \"" + this.eventName //$NON-NLS-1$
121 // + "\", field \"" + curFieldName + "\" of type " + curFieldDef.getClass().toString()); //$NON-NLS-1$ //$NON-NLS-2$
123 fields
.add(curField
);
126 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);
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
= other
.fContent
.clone();
150 * Inner constructor to create "null" events. Don't use this directly, use
151 * CTFEvent.getNullEvent();
153 private CtfTmfEvent() {
158 this.fileName
= NO_STREAM
;
159 this.eventName
= EMPTY_CTF_EVENT_NAME
;
160 this.fContent
= null;
163 // ------------------------------------------------------------------------
164 // Getters/Setters/Predicates
165 // ------------------------------------------------------------------------
167 private static CtfTmfEvent nullEvent
= null;
172 * @return an empty event.
174 public static CtfTmfEvent
getNullEvent() {
175 if (nullEvent
== null) {
176 nullEvent
= new CtfTmfEvent();
182 * Gets the current timestamp of the event
184 * @return the current timestamp (long)
186 public long getTimestampValue() {
187 return this.timestamp
;
191 * Gets the cpu core the event was recorded on.
193 * @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.
206 public long getID() {
211 * Gets the name of a current event.
213 * @return the event name
215 public String
getEventName() {
220 * Gets the channel name of a field.
222 * @return the channel name.
224 public String
getChannelName() {
225 return this.fileName
;
229 public CtfTmfTrace
getTrace() {
234 public long getRank() {
235 // TODO Auto-generated method stub
239 private ITmfTimestamp fTimestamp
= null;
241 // TODO Benchmark if the singleton approach is faster than just
242 // instantiating a final fTimestramp right away at creation time
244 public ITmfTimestamp
getTimestamp() {
245 if (fTimestamp
== null) {
246 fTimestamp
= new TmfTimestamp(timestamp
);
252 public String
getSource() {
253 // TODO Returns eventName for now
258 public ITmfEventType
getType() {
259 // TODO Auto-generated method stub
264 public ITmfEventField
getContent() {
269 public String
getReference() {
270 // TODO Auto-generated method stub
275 public CtfTmfEvent
clone() {
276 return new CtfTmfEvent(this);