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