Add support for displaying of CTF context info in TMF (Bug 385494)
[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
28 /**
29 * A wrapper class around CTF's Event Definition/Declaration that maps all
30 * types of Declaration to native Java types.
31 *
32 * @version 1.0
33 * @author Alexandre Montplaisir
34 */
35 public final class CtfTmfEvent implements ITmfEvent, Cloneable {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
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$
43
44 /** Prefix for Context information stored as CtfTmfEventfield */
45 private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50
51 private final CtfTmfTrace fTrace;
52 private final long timestamp;
53 private final int sourceCPU;
54 private final long typeId;
55 private final String eventName;
56 private final String fileName;
57
58 private final CtfTmfContent fContent;
59
60 // ------------------------------------------------------------------------
61 // Constructors
62 // ------------------------------------------------------------------------
63
64 /**
65 * Usual CTFEvent constructor, where we read an event from the trace (via
66 * the StreamInputReader).
67 *
68 * @param eventDef
69 * CTF EventDefinition object corresponding to this trace event
70 * @param fileName
71 * The path to the trace file
72 * @param originTrace
73 * The trace from which this event originates
74 */
75 public CtfTmfEvent(EventDefinition eventDef, String fileName,
76 CtfTmfTrace originTrace) {
77 this.fTrace = originTrace;
78
79 if (eventDef == null) {
80 this.timestamp = -1;
81 this.sourceCPU = -1;
82 this.typeId = -1;
83 this.fileName = NO_STREAM;
84 this.eventName = EMPTY_CTF_EVENT_NAME;
85 this.fContent = null;
86 return;
87 }
88
89 /* Read the base event info */
90 Long offset = originTrace.getCTFTrace().getOffset();
91 this.timestamp = eventDef.getTimestamp() + offset;
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 CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
99 parseFields(eventDef));
100 }
101
102 /**
103 * Extract the field information from the structDefinition haze-inducing
104 * mess, and put them into something ITmfEventField can cope with.
105 *
106 * @param eventDef
107 * CTF EventDefinition to read
108 * @return CtfTmfEventField[] The array of fields that were read
109 */
110 public static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
111 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
112
113 StructDefinition structFields = eventDef.getFields();
114 HashMap<String, Definition> definitions = structFields.getDefinitions();
115 String curFieldName;
116 Definition curFieldDef;
117 CtfTmfEventField curField;
118 Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
119 while(it.hasNext()) {
120 Entry<String, Definition> entry = it.next();
121 curFieldName = entry.getKey();
122 curFieldDef = entry.getValue();
123 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
124 fields.add(curField);
125 }
126
127 /* Add context information as CtfTmfEventField */
128 StructDefinition structContext = eventDef.getContext();
129 if (structContext != null) {
130 definitions = structContext.getDefinitions();
131 String curContextName;
132 Definition curContextDef;
133 CtfTmfEventField curContext;
134 it = definitions.entrySet().iterator();
135 while(it.hasNext()) {
136 Entry<String, Definition> entry = it.next();
137 /* Prefix field name to */
138 curContextName = CONTEXT_FIELD_PREFIX + entry.getKey();
139 curContextDef = entry.getValue();
140 curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
141 fields.add(curContext);
142 }
143 }
144
145 return fields.toArray(new CtfTmfEventField[fields.size()]);
146 }
147
148 /**
149 * Copy constructor
150 *
151 * @param other
152 * CtfTmfEvent to copy
153 */
154 public CtfTmfEvent(CtfTmfEvent other) {
155 this.fTrace = other.getTrace();
156 /* Primitives, those will be copied by value */
157 this.timestamp = other.timestamp;
158 this.sourceCPU = other.sourceCPU;
159 this.typeId = other.typeId;
160
161 /* Strings are immutable, it's safe to shallow-copy them */
162 this.eventName = other.eventName;
163 this.fileName = other.fileName;
164
165 /* Copy the fields over */
166 this.fContent = (CtfTmfContent) other.fContent.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 CtfTmfContent("", 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.039795 seconds and 6 git commands to generate.