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