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