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