b1d5ec0cf7c4051dd2388bae906b945e0541897e
[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
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
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;
55
56 private final CtfTmfContent fContent;
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61
62 /**
63 * Usual CTFEvent constructor, where we read an event from the trace (via
64 * the StreamInputReader).
65 *
66 * @param eventDef
67 * CTF EventDefinition object corresponding to this trace event
68 * @param fileName
69 * The path to the trace file
70 * @param originTrace
71 * The trace from which this event originates
72 */
73 public CtfTmfEvent(EventDefinition eventDef, String fileName,
74 CtfTmfTrace originTrace) {
75 this.fTrace = originTrace;
76
77 if (eventDef == null) {
78 this.timestamp = -1;
79 this.sourceCPU = -1;
80 this.typeId = -1;
81 this.fileName = NO_STREAM;
82 this.eventName = EMPTY_CTF_EVENT_NAME;
83 this.fContent = null;
84 return;
85 }
86
87 /* Read the base event info */
88 this.timestamp = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp());
89 this.sourceCPU = eventDef.getCPU();
90 this.typeId = eventDef.getDeclaration().getId();
91 this.eventName = eventDef.getDeclaration().getName();
92 this.fileName = fileName;
93
94 /* Read the fields */
95 this.fContent = new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
96 parseFields(eventDef));
97 }
98
99 /**
100 * Extract the field information from the structDefinition haze-inducing
101 * mess, and put them into something ITmfEventField can cope with.
102 *
103 * @param eventDef
104 * CTF EventDefinition to read
105 * @return CtfTmfEventField[] The array of fields that were read
106 */
107 public static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
108 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
109
110 StructDefinition structFields = eventDef.getFields();
111 HashMap<String, Definition> definitions = structFields.getDefinitions();
112 String curFieldName;
113 Definition curFieldDef;
114 CtfTmfEventField curField;
115 Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
116 while(it.hasNext()) {
117 Entry<String, Definition> entry = it.next();
118 curFieldName = entry.getKey();
119 curFieldDef = entry.getValue();
120 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
121 fields.add(curField);
122 }
123
124 return fields.toArray(new CtfTmfEventField[fields.size()]);
125 }
126
127 /**
128 * Copy constructor
129 *
130 * @param other
131 * CtfTmfEvent to copy
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 in
150 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
151 * empty event.
152 *
153 * This needs to be public however because it's used in extension points,
154 * and the framework will use this constructor to get the class type.
155 */
156 public CtfTmfEvent() {
157 this.fTrace = null;
158 this.timestamp = -1;
159 this.sourceCPU = -1;
160 this.typeId = -1;
161 this.fileName = NO_STREAM;
162 this.eventName = EMPTY_CTF_EVENT_NAME;
163 this.fContent = new CtfTmfContent("", new CtfTmfEventField[0]); //$NON-NLS-1$
164 }
165
166 // ------------------------------------------------------------------------
167 // Getters/Setters/Predicates
168 // ------------------------------------------------------------------------
169
170 private static CtfTmfEvent nullEvent = null;
171
172 /**
173 * Get a null event
174 *
175 * @return an empty event. */
176 public static CtfTmfEvent getNullEvent() {
177 if (nullEvent == null) {
178 nullEvent = new CtfTmfEvent();
179 }
180 return nullEvent;
181 }
182
183 /**
184 * Gets the current timestamp of the event
185 *
186 * @return the current timestamp (long) */
187 public long getTimestampValue() {
188 return this.timestamp;
189 }
190
191 /**
192 * Gets the cpu core the event was recorded on.
193 *
194 * @return the cpu id for a given source. In lttng it's from CPUINFO */
195 public int getCPU() {
196 return this.sourceCPU;
197 }
198
199 /**
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.
203 *
204
205 * @return the event ID */
206 public long getID() {
207 return this.typeId;
208 }
209
210 /**
211 * Gets the name of a current event.
212 *
213 * @return the event name */
214 public String getEventName() {
215 return eventName;
216 }
217
218 /**
219 * Gets the channel name of a field.
220 *
221 * @return the channel name. */
222 public String getChannelName() {
223 return this.fileName;
224 }
225
226 /**
227 * Method getTrace.
228 * @return CtfTmfTrace
229 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
230 */
231 @Override
232 public CtfTmfTrace getTrace() {
233 return fTrace;
234 }
235
236 /**
237 * Method getRank.
238 * @return long
239 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
240 */
241 @Override
242 public long getRank() {
243 // TODO Auto-generated method stub
244 return 0;
245 }
246
247 private ITmfTimestamp fTimestamp = null;
248
249 // TODO Benchmark if the singleton approach is faster than just
250 // instantiating a final fTimestramp right away at creation time
251 /**
252 * Method getTimestamp.
253 * @return ITmfTimestamp
254 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
255 */
256 @Override
257 public ITmfTimestamp getTimestamp() {
258 if (fTimestamp == null) {
259 fTimestamp = new CtfTmfTimestamp(timestamp);
260 }
261 return fTimestamp;
262 }
263
264 String fSource = null;
265 /**
266 * Method getSource.
267 * @return String
268 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
269 */
270 @Override
271 public String getSource() {
272 // TODO Returns CPU for now
273 if(fSource == null) {
274 fSource= Integer.toString(getCPU());
275 }
276 return fSource;
277 }
278
279 /**
280 * Method getType.
281 * @return ITmfEventType
282 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
283 */
284 @Override
285 public ITmfEventType getType() {
286 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
287 if( ctfTmfEventType == null ){
288 ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent());
289 }
290 return ctfTmfEventType;
291 }
292
293 /**
294 * Method getContent.
295 * @return ITmfEventField
296 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
297 */
298 @Override
299 public ITmfEventField getContent() {
300 return fContent;
301 }
302
303 String fReference = null;
304 /**
305 * Method getReference.
306 * @return String
307 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
308 */
309 @Override
310 public String getReference() {
311 if( fReference == null){
312 fReference = getChannelName();
313 }
314 return fReference;
315 }
316
317 /**
318 * Method clone.
319 * @return CtfTmfEvent
320 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone()
321 */
322 @Override
323 public CtfTmfEvent clone() {
324 return new CtfTmfEvent(this);
325 }
326 }
This page took 0.037797 seconds and 4 git commands to generate.