Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.HashMap; | |
aa572e22 | 17 | import java.util.Iterator; |
a3fc8213 AM |
18 | import java.util.List; |
19 | import java.util.Map.Entry; | |
20 | ||
93bfd50a | 21 | import org.eclipse.core.runtime.IAdaptable; |
a3fc8213 AM |
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; | |
a3fc8213 AM |
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; | |
306dc902 | 29 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
93bfd50a PT |
30 | import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource; |
31 | import org.eclipse.ui.views.properties.IPropertySource; | |
a3fc8213 AM |
32 | |
33 | /** | |
d09f973b FC |
34 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
35 | * types of Declaration to native Java types. | |
6256d8ad | 36 | * |
d09f973b FC |
37 | * @version 1.0 |
38 | * @author Alexandre Montplaisir | |
93bfd50a | 39 | * @since 2.0 |
a3fc8213 | 40 | */ |
93bfd50a | 41 | public final class CtfTmfEvent implements ITmfEvent, IAdaptable, Cloneable { |
a3fc8213 AM |
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$ | |
aa572e22 | 49 | |
26859ddb BH |
50 | /** Prefix for Context information stored as CtfTmfEventfield */ |
51 | private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$ | |
a3fc8213 AM |
52 | |
53 | // ------------------------------------------------------------------------ | |
54 | // Attributes | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
57 | private final CtfTmfTrace fTrace; | |
58f3bc52 | 58 | private final ITmfTimestamp fTimestamp; |
a3fc8213 AM |
59 | private final int sourceCPU; |
60 | private final long typeId; | |
61 | private final String eventName; | |
62 | private final String fileName; | |
63 | ||
306dc902 | 64 | private final TmfEventField fContent; |
a3fc8213 AM |
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 | |
063f0d27 AM |
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 | |
a3fc8213 | 80 | */ |
ce2388e0 | 81 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
a3fc8213 AM |
82 | CtfTmfTrace originTrace) { |
83 | this.fTrace = originTrace; | |
84 | ||
85 | if (eventDef == null) { | |
58f3bc52 | 86 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
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 */ | |
58f3bc52 AM |
96 | long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); |
97 | this.fTimestamp = new CtfTmfTimestamp(ts); | |
a3fc8213 AM |
98 | this.sourceCPU = eventDef.getCPU(); |
99 | this.typeId = eventDef.getDeclaration().getId(); | |
100 | this.eventName = eventDef.getDeclaration().getName(); | |
ce2388e0 | 101 | this.fileName = fileName; |
a3fc8213 AM |
102 | |
103 | /* Read the fields */ | |
306dc902 | 104 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef)); |
a3fc8213 AM |
105 | } |
106 | ||
107 | /** | |
108 | * Extract the field information from the structDefinition haze-inducing | |
109 | * mess, and put them into something ITmfEventField can cope with. | |
a3fc8213 | 110 | */ |
58f3bc52 | 111 | private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
a3fc8213 AM |
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; | |
aa572e22 MK |
119 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
120 | while(it.hasNext()) { | |
121 | Entry<String, Definition> entry = it.next(); | |
a3fc8213 AM |
122 | curFieldName = entry.getKey(); |
123 | curFieldDef = entry.getValue(); | |
124 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
a3fc8213 AM |
125 | fields.add(curField); |
126 | } | |
127 | ||
26859ddb BH |
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 | ||
a3fc8213 AM |
146 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
147 | } | |
148 | ||
149 | /** | |
150 | * Copy constructor | |
151 | * | |
152 | * @param other | |
063f0d27 | 153 | * CtfTmfEvent to copy |
a3fc8213 AM |
154 | */ |
155 | public CtfTmfEvent(CtfTmfEvent other) { | |
58f3bc52 | 156 | /* There is only one reference to the trace, so we can shallow-copy it */ |
a3fc8213 | 157 | this.fTrace = other.getTrace(); |
58f3bc52 AM |
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 | ||
a3fc8213 | 166 | /* Primitives, those will be copied by value */ |
a3fc8213 AM |
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 */ | |
306dc902 | 175 | this.fContent = other.fContent.clone(); |
a3fc8213 AM |
176 | } |
177 | ||
178 | /** | |
b8a6e46d AM |
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. | |
c26afeaf | 182 | * |
b8a6e46d AM |
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. | |
a3fc8213 | 185 | */ |
ce2388e0 | 186 | public CtfTmfEvent() { |
a3fc8213 | 187 | this.fTrace = null; |
58f3bc52 | 188 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
189 | this.sourceCPU = -1; |
190 | this.typeId = -1; | |
191 | this.fileName = NO_STREAM; | |
192 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
306dc902 | 193 | this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
a3fc8213 AM |
194 | } |
195 | ||
196 | // ------------------------------------------------------------------------ | |
197 | // Getters/Setters/Predicates | |
198 | // ------------------------------------------------------------------------ | |
199 | ||
58f3bc52 | 200 | private static CtfTmfEvent nullEvent = new CtfTmfEvent(); |
a3fc8213 AM |
201 | |
202 | /** | |
203 | * Get a null event | |
204 | * | |
58f3bc52 AM |
205 | * @return An empty event. |
206 | */ | |
a3fc8213 | 207 | public static CtfTmfEvent getNullEvent() { |
a3fc8213 AM |
208 | return nullEvent; |
209 | } | |
210 | ||
a3fc8213 AM |
211 | /** |
212 | * Gets the cpu core the event was recorded on. | |
213 | * | |
58f3bc52 AM |
214 | * @return The cpu id for a given source. In lttng it's from CPUINFO |
215 | */ | |
a3fc8213 AM |
216 | public int getCPU() { |
217 | return this.sourceCPU; | |
218 | } | |
219 | ||
220 | /** | |
58f3bc52 | 221 | * Return this event's ID, according to the trace's metadata. |
a3fc8213 | 222 | * |
58f3bc52 AM |
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 | */ | |
a3fc8213 AM |
228 | public long getID() { |
229 | return this.typeId; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Gets the name of a current event. | |
234 | * | |
58f3bc52 AM |
235 | * @return The event name |
236 | */ | |
a3fc8213 AM |
237 | public String getEventName() { |
238 | return eventName; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Gets the channel name of a field. | |
243 | * | |
58f3bc52 AM |
244 | * @return The channel name. |
245 | */ | |
a3fc8213 AM |
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 | ||
a3fc8213 AM |
261 | @Override |
262 | public ITmfTimestamp getTimestamp() { | |
a3fc8213 AM |
263 | return fTimestamp; |
264 | } | |
265 | ||
266 | @Override | |
267 | public String getSource() { | |
ce2388e0 | 268 | // TODO Returns CPU for now |
58f3bc52 | 269 | return Integer.toString(getCPU()); |
a3fc8213 AM |
270 | } |
271 | ||
272 | @Override | |
273 | public ITmfEventType getType() { | |
c26afeaf MD |
274 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
275 | if( ctfTmfEventType == null ){ | |
276 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); | |
277 | } | |
278 | return ctfTmfEventType; | |
a3fc8213 AM |
279 | } |
280 | ||
281 | @Override | |
282 | public ITmfEventField getContent() { | |
283 | return fContent; | |
284 | } | |
285 | ||
286 | @Override | |
287 | public String getReference() { | |
58f3bc52 | 288 | return getChannelName(); |
a3fc8213 AM |
289 | } |
290 | ||
291 | @Override | |
292 | public CtfTmfEvent clone() { | |
293 | return new CtfTmfEvent(this); | |
294 | } | |
93bfd50a PT |
295 | |
296 | /* (non-Javadoc) | |
297 | * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) | |
298 | */ | |
531987c8 PT |
299 | /** |
300 | * @since 2.0 | |
301 | */ | |
93bfd50a PT |
302 | @Override |
303 | public Object getAdapter(Class adapter) { | |
304 | if (adapter == IPropertySource.class) { | |
305 | return new TmfEventPropertySource(this); | |
306 | } | |
307 | return null; | |
308 | } | |
a3fc8213 | 309 | } |