19bd9bf18c37a6abbc405856d23f2065594e89e1
[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.Iterator;
17 import java.util.List;
18 import java.util.Map.Entry;
19
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;
27 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
28
29 /**
30 * A wrapper class around CTF's Event Definition/Declaration that maps all
31 * types of Declaration to native Java types.
32 *
33 * @version 1.0
34 * @author Alexandre Montplaisir
35 */
36 public final class CtfTmfEvent implements ITmfEvent, Cloneable {
37
38 // ------------------------------------------------------------------------
39 // Constants
40 // ------------------------------------------------------------------------
41
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$
44
45 /** Prefix for Context information stored as CtfTmfEventfield */
46 private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51
52 private final CtfTmfTrace fTrace;
53 private final long timestamp;
54 private final int sourceCPU;
55 private final long typeId;
56 private final String eventName;
57 private final String fileName;
58
59 private final TmfEventField fContent;
60
61 // ------------------------------------------------------------------------
62 // Constructors
63 // ------------------------------------------------------------------------
64
65 /**
66 * Usual CTFEvent constructor, where we read an event from the trace (via
67 * the StreamInputReader).
68 *
69 * @param eventDef
70 * CTF EventDefinition object corresponding to this trace event
71 * @param fileName
72 * The path to the trace file
73 * @param originTrace
74 * The trace from which this event originates
75 */
76 public CtfTmfEvent(EventDefinition eventDef, String fileName,
77 CtfTmfTrace originTrace) {
78 this.fTrace = originTrace;
79
80 if (eventDef == null) {
81 this.timestamp = -1;
82 this.sourceCPU = -1;
83 this.typeId = -1;
84 this.fileName = NO_STREAM;
85 this.eventName = EMPTY_CTF_EVENT_NAME;
86 this.fContent = null;
87 return;
88 }
89
90 /* Read the base event info */
91 this.timestamp = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp());
92 this.sourceCPU = eventDef.getCPU();
93 this.typeId = eventDef.getDeclaration().getId();
94 this.eventName = eventDef.getDeclaration().getName();
95 this.fileName = fileName;
96
97 /* Read the fields */
98 this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef));
99 }
100
101 /**
102 * Extract the field information from the structDefinition haze-inducing
103 * mess, and put them into something ITmfEventField can cope with.
104 *
105 * @param eventDef
106 * CTF EventDefinition to read
107 * @return CtfTmfEventField[] The array of fields that were read
108 */
109 public static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
110 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
111
112 StructDefinition structFields = eventDef.getFields();
113 HashMap<String, Definition> definitions = structFields.getDefinitions();
114 String curFieldName;
115 Definition curFieldDef;
116 CtfTmfEventField curField;
117 Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
118 while(it.hasNext()) {
119 Entry<String, Definition> entry = it.next();
120 curFieldName = entry.getKey();
121 curFieldDef = entry.getValue();
122 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
123 fields.add(curField);
124 }
125
126 /* Add context information as CtfTmfEventField */
127 StructDefinition structContext = eventDef.getContext();
128 if (structContext != null) {
129 definitions = structContext.getDefinitions();
130 String curContextName;
131 Definition curContextDef;
132 CtfTmfEventField curContext;
133 it = definitions.entrySet().iterator();
134 while(it.hasNext()) {
135 Entry<String, Definition> entry = it.next();
136 /* Prefix field name to */
137 curContextName = CONTEXT_FIELD_PREFIX + entry.getKey();
138 curContextDef = entry.getValue();
139 curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
140 fields.add(curContext);
141 }
142 }
143
144 return fields.toArray(new CtfTmfEventField[fields.size()]);
145 }
146
147 /**
148 * Copy constructor
149 *
150 * @param other
151 * CtfTmfEvent to copy
152 */
153 public CtfTmfEvent(CtfTmfEvent other) {
154 this.fTrace = other.getTrace();
155 /* Primitives, those will be copied by value */
156 this.timestamp = other.timestamp;
157 this.sourceCPU = other.sourceCPU;
158 this.typeId = other.typeId;
159
160 /* Strings are immutable, it's safe to shallow-copy them */
161 this.eventName = other.eventName;
162 this.fileName = other.fileName;
163
164 /* Copy the fields over */
165 this.fContent = other.fContent.clone();
166 this.fTimestamp = other.fTimestamp.clone();
167 }
168
169 /**
170 * Inner constructor to create "null" events. Don't use this directly in
171 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
172 * empty event.
173 *
174 * This needs to be public however because it's used in extension points,
175 * and the framework will use this constructor to get the class type.
176 */
177 public CtfTmfEvent() {
178 this.fTrace = null;
179 this.timestamp = -1;
180 this.sourceCPU = -1;
181 this.typeId = -1;
182 this.fileName = NO_STREAM;
183 this.eventName = EMPTY_CTF_EVENT_NAME;
184 this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$
185 }
186
187 // ------------------------------------------------------------------------
188 // Getters/Setters/Predicates
189 // ------------------------------------------------------------------------
190
191 private static CtfTmfEvent nullEvent = null;
192
193 /**
194 * Get a null event
195 *
196 * @return an empty event. */
197 public static CtfTmfEvent getNullEvent() {
198 if (nullEvent == null) {
199 nullEvent = new CtfTmfEvent();
200 }
201 return nullEvent;
202 }
203
204 /**
205 * Gets the current timestamp of the event
206 *
207 * @return the current timestamp (long) */
208 public long getTimestampValue() {
209 return this.timestamp;
210 }
211
212 /**
213 * Gets the cpu core the event was recorded on.
214 *
215 * @return the cpu id for a given source. In lttng it's from CPUINFO */
216 public int getCPU() {
217 return this.sourceCPU;
218 }
219
220 /**
221 * Return this event's ID, according to the trace's metadata. Watch out,
222 * this ID is not constant from one trace to another for the same event
223 * types! Use "getEventName()" for a constant reference.
224 *
225
226 * @return the event ID */
227 public long getID() {
228 return this.typeId;
229 }
230
231 /**
232 * Gets the name of a current event.
233 *
234 * @return the event name */
235 public String getEventName() {
236 return eventName;
237 }
238
239 /**
240 * Gets the channel name of a field.
241 *
242 * @return the channel name. */
243 public String getChannelName() {
244 return this.fileName;
245 }
246
247 /**
248 * Method getTrace.
249 * @return CtfTmfTrace
250 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
251 */
252 @Override
253 public CtfTmfTrace getTrace() {
254 return fTrace;
255 }
256
257 /**
258 * Method getRank.
259 * @return long
260 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
261 */
262 @Override
263 public long getRank() {
264 // TODO Auto-generated method stub
265 return 0;
266 }
267
268 private ITmfTimestamp fTimestamp = null;
269
270 // TODO Benchmark if the singleton approach is faster than just
271 // instantiating a final fTimestramp right away at creation time
272 /**
273 * Method getTimestamp.
274 * @return ITmfTimestamp
275 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
276 */
277 @Override
278 public ITmfTimestamp getTimestamp() {
279 if (fTimestamp == null) {
280 fTimestamp = new CtfTmfTimestamp(timestamp);
281 }
282 return fTimestamp;
283 }
284
285 String fSource = null;
286 /**
287 * Method getSource.
288 * @return String
289 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
290 */
291 @Override
292 public String getSource() {
293 // TODO Returns CPU for now
294 if(fSource == null) {
295 fSource= Integer.toString(getCPU());
296 }
297 return fSource;
298 }
299
300 /**
301 * Method getType.
302 * @return ITmfEventType
303 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
304 */
305 @Override
306 public ITmfEventType getType() {
307 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
308 if( ctfTmfEventType == null ){
309 ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent());
310 }
311 return ctfTmfEventType;
312 }
313
314 /**
315 * Method getContent.
316 * @return ITmfEventField
317 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
318 */
319 @Override
320 public ITmfEventField getContent() {
321 return fContent;
322 }
323
324 String fReference = null;
325 /**
326 * Method getReference.
327 * @return String
328 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
329 */
330 @Override
331 public String getReference() {
332 if( fReference == null){
333 fReference = getChannelName();
334 }
335 return fReference;
336 }
337
338 /**
339 * Method clone.
340 * @return CtfTmfEvent
341 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
342 */
343 @Override
344 public CtfTmfEvent clone() {
345 return new CtfTmfEvent(this);
346 }
347 }
This page took 0.038788 seconds and 5 git commands to generate.