49b150139e867b6c62fed60f7f4c9ca0eed014ca
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
3 *
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
8 *
9 * Contributors: Alexandre Montplaisir - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map.Entry;
18
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;
26
27 /**
28 * <b><u>CTFEvent</u></b>
29 * <p>
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.
32 */
33 public final class CtfTmfEvent implements ITmfEvent {
34
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38
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$
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
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;
53
54 private final CtfTmfContent fContent;
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 /**
61 * Usual CTFEvent constructor, where we read an event from the trace (via
62 * the StreamInputReader).
63 *
64 * @param eventDef
65
66 * @param fileName String
67 * @param originTrace CtfTmfTrace
68 */
69 public CtfTmfEvent(EventDefinition eventDef, String fileName,
70 CtfTmfTrace originTrace) {
71 this.fTrace = originTrace;
72
73 if (eventDef == null) {
74 this.timestamp = -1;
75 this.sourceCPU = -1;
76 this.typeId = -1;
77 this.fileName = NO_STREAM;
78 this.eventName = EMPTY_CTF_EVENT_NAME;
79 this.fContent = null;
80 return;
81 }
82
83 /* Read the base event info */
84 Long offset = originTrace.getCTFTrace().getOffset();
85 this.timestamp = eventDef.timestamp + offset;
86 this.sourceCPU = eventDef.getCPU();
87 this.typeId = eventDef.getDeclaration().getId();
88 this.eventName = eventDef.getDeclaration().getName();
89 this.fileName = fileName;
90
91 /* Read the fields */
92 this.fContent = new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
93 parseFields(eventDef));
94 }
95
96 /**
97 * Extract the field information from the structDefinition haze-inducing
98 * mess, and put them into something ITmfEventField can cope with.
99 *
100 * @param eventDef
101
102 * @return CtfTmfEventField[]
103 */
104 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
105 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
106
107 StructDefinition structFields = eventDef.getFields();
108 HashMap<String, Definition> definitions = structFields.getDefinitions();
109 String curFieldName;
110 Definition curFieldDef;
111 CtfTmfEventField curField;
112
113 for (Entry<String, Definition> entry : definitions.entrySet()) {
114 curFieldName = entry.getKey();
115 curFieldDef = entry.getValue();
116 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
117 if (curField == null) {
118 // TmfCorePlugin.getDefault().log(
119 // "We've parsed an unimplemented field type for event \"" + this.eventName //$NON-NLS-1$
120 // + "\", field \"" + curFieldName + "\" of type " + curFieldDef.getClass().toString()); //$NON-NLS-1$ //$NON-NLS-2$
121 }
122 fields.add(curField);
123 }
124
125 return fields.toArray(new CtfTmfEventField[fields.size()]);
126 }
127
128 /**
129 * Copy constructor
130 *
131 * @param other
132 */
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;
139
140 /* Strings are immutable, it's safe to shallow-copy them */
141 this.eventName = other.eventName;
142 this.fileName = other.fileName;
143
144 /* Copy the fields over */
145 this.fContent = (CtfTmfContent) other.fContent.clone();
146 }
147
148 /**
149 * Inner constructor to create "null" events. Don't use this directly, use
150 * CTFEvent.getNullEvent();
151 */
152 public CtfTmfEvent() {
153 this.fTrace = null;
154 this.timestamp = -1;
155 this.sourceCPU = -1;
156 this.typeId = -1;
157 this.fileName = NO_STREAM;
158 this.eventName = EMPTY_CTF_EVENT_NAME;
159 this.fContent = null;
160 }
161
162 // ------------------------------------------------------------------------
163 // Getters/Setters/Predicates
164 // ------------------------------------------------------------------------
165
166 private static CtfTmfEvent nullEvent = null;
167
168 /**
169 * Get a null event
170 *
171 * @return an empty event. */
172 public static CtfTmfEvent getNullEvent() {
173 if (nullEvent == null) {
174 nullEvent = new CtfTmfEvent();
175 }
176 return nullEvent;
177 }
178
179 /**
180 * Gets the current timestamp of the event
181 *
182 * @return the current timestamp (long) */
183 public long getTimestampValue() {
184 return this.timestamp;
185 }
186
187 /**
188 * Gets the cpu core the event was recorded on.
189 *
190 * @return the cpu id for a given source. In lttng it's from CPUINFO */
191 public int getCPU() {
192 return this.sourceCPU;
193 }
194
195 /**
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.
199 *
200
201 * @return the event ID */
202 public long getID() {
203 return this.typeId;
204 }
205
206 /**
207 * Gets the name of a current event.
208 *
209 * @return the event name */
210 public String getEventName() {
211 return eventName;
212 }
213
214 /**
215 * Gets the channel name of a field.
216 *
217 * @return the channel name. */
218 public String getChannelName() {
219 return this.fileName;
220 }
221
222 /**
223 * Method getTrace.
224 * @return CtfTmfTrace
225 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
226 */
227 @Override
228 public CtfTmfTrace getTrace() {
229 return fTrace;
230 }
231
232 /**
233 * Method getRank.
234 * @return long
235 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
236 */
237 @Override
238 public long getRank() {
239 // TODO Auto-generated method stub
240 return 0;
241 }
242
243 private ITmfTimestamp fTimestamp = null;
244
245 // TODO Benchmark if the singleton approach is faster than just
246 // instantiating a final fTimestramp right away at creation time
247 /**
248 * Method getTimestamp.
249 * @return ITmfTimestamp
250 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
251 */
252 @Override
253 public ITmfTimestamp getTimestamp() {
254 if (fTimestamp == null) {
255 fTimestamp = new CtfTmfTimestamp(timestamp);
256 }
257 return fTimestamp;
258 }
259
260 String fSource = null;
261 /**
262 * Method getSource.
263 * @return String
264 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
265 */
266 @Override
267 public String getSource() {
268 // TODO Returns CPU for now
269 if(fSource == null) {
270 fSource= Integer.toString(getCPU());
271 }
272 return fSource;
273 }
274
275 private CtfTmfEventType type = null;
276 /**
277 * Method getType.
278 * @return ITmfEventType
279 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
280 */
281 @Override
282 public ITmfEventType getType() {
283 if(type == null){
284 type = new CtfTmfEventType(CONTEXT_ID, eventName, fContent);
285 }
286 return type;
287 }
288
289 /**
290 * Method getContent.
291 * @return ITmfEventField
292 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
293 */
294 @Override
295 public ITmfEventField getContent() {
296 return fContent;
297 }
298
299 String fReference = null;
300 /**
301 * Method getReference.
302 * @return String
303 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
304 */
305 @Override
306 public String getReference() {
307 if( fReference == null){
308 fReference = getChannelName();
309 }
310 return fReference;
311 }
312
313 /**
314 * Method clone.
315 * @return CtfTmfEvent
316 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
317 */
318 @Override
319 public CtfTmfEvent clone() {
320 return new CtfTmfEvent(this);
321 }
322 }
This page took 0.036755 seconds and 5 git commands to generate.