Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
8e964be1 | 2 | * Copyright (c) 2011-2013 Ericsson |
a3fc8213 AM |
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; | |
8e964be1 | 17 | import java.util.HashSet; |
aa572e22 | 18 | import java.util.Iterator; |
a3fc8213 AM |
19 | import java.util.List; |
20 | import java.util.Map.Entry; | |
8e964be1 | 21 | import java.util.Set; |
a3fc8213 | 22 | |
93bfd50a | 23 | import org.eclipse.core.runtime.IAdaptable; |
4c9d2941 | 24 | import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; |
a3fc8213 | 25 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
8e964be1 | 26 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
a3fc8213 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
4c9d2941 | 28 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
a3fc8213 | 29 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
a3fc8213 AM |
30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
31 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
32 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; | |
33 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
306dc902 | 34 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
93bfd50a PT |
35 | import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource; |
36 | import org.eclipse.ui.views.properties.IPropertySource; | |
a3fc8213 AM |
37 | |
38 | /** | |
d09f973b FC |
39 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
40 | * types of Declaration to native Java types. | |
6256d8ad | 41 | * |
d09f973b FC |
42 | * @version 1.0 |
43 | * @author Alexandre Montplaisir | |
93bfd50a | 44 | * @since 2.0 |
a3fc8213 | 45 | */ |
93bfd50a | 46 | public final class CtfTmfEvent implements ITmfEvent, IAdaptable, Cloneable { |
a3fc8213 AM |
47 | |
48 | // ------------------------------------------------------------------------ | |
49 | // Constants | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ | |
53 | private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ | |
aa572e22 | 54 | |
26859ddb BH |
55 | /** Prefix for Context information stored as CtfTmfEventfield */ |
56 | private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$ | |
a3fc8213 AM |
57 | |
58 | // ------------------------------------------------------------------------ | |
59 | // Attributes | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | private final CtfTmfTrace fTrace; | |
58f3bc52 | 63 | private final ITmfTimestamp fTimestamp; |
a3fc8213 AM |
64 | private final int sourceCPU; |
65 | private final long typeId; | |
66 | private final String eventName; | |
67 | private final String fileName; | |
68 | ||
306dc902 | 69 | private final TmfEventField fContent; |
8e964be1 | 70 | private final IEventDeclaration fDeclaration; |
a3fc8213 AM |
71 | |
72 | // ------------------------------------------------------------------------ | |
73 | // Constructors | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | /** | |
77 | * Usual CTFEvent constructor, where we read an event from the trace (via | |
78 | * the StreamInputReader). | |
79 | * | |
80 | * @param eventDef | |
063f0d27 AM |
81 | * CTF EventDefinition object corresponding to this trace event |
82 | * @param fileName | |
83 | * The path to the trace file | |
84 | * @param originTrace | |
85 | * The trace from which this event originates | |
a3fc8213 | 86 | */ |
ce2388e0 | 87 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
a3fc8213 AM |
88 | CtfTmfTrace originTrace) { |
89 | this.fTrace = originTrace; | |
90 | ||
91 | if (eventDef == null) { | |
58f3bc52 | 92 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
93 | this.sourceCPU = -1; |
94 | this.typeId = -1; | |
95 | this.fileName = NO_STREAM; | |
96 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
97 | this.fContent = null; | |
8e964be1 | 98 | this.fDeclaration = null; |
a3fc8213 AM |
99 | return; |
100 | } | |
101 | ||
102 | /* Read the base event info */ | |
58f3bc52 AM |
103 | long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); |
104 | this.fTimestamp = new CtfTmfTimestamp(ts); | |
a3fc8213 AM |
105 | this.sourceCPU = eventDef.getCPU(); |
106 | this.typeId = eventDef.getDeclaration().getId(); | |
107 | this.eventName = eventDef.getDeclaration().getName(); | |
ce2388e0 | 108 | this.fileName = fileName; |
a3fc8213 AM |
109 | |
110 | /* Read the fields */ | |
306dc902 | 111 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef)); |
8e964be1 MK |
112 | |
113 | /* Keep a reference to this event's CTF declaration */ | |
114 | this.fDeclaration = eventDef.getDeclaration(); | |
a3fc8213 AM |
115 | } |
116 | ||
117 | /** | |
118 | * Extract the field information from the structDefinition haze-inducing | |
119 | * mess, and put them into something ITmfEventField can cope with. | |
a3fc8213 | 120 | */ |
4c9d2941 | 121 | private CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
a3fc8213 AM |
122 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); |
123 | ||
124 | StructDefinition structFields = eventDef.getFields(); | |
125 | HashMap<String, Definition> definitions = structFields.getDefinitions(); | |
4c9d2941 | 126 | String curFieldName = null; |
a3fc8213 AM |
127 | Definition curFieldDef; |
128 | CtfTmfEventField curField; | |
aa572e22 MK |
129 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
130 | while(it.hasNext()) { | |
131 | Entry<String, Definition> entry = it.next(); | |
a3fc8213 AM |
132 | curFieldName = entry.getKey(); |
133 | curFieldDef = entry.getValue(); | |
134 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
a3fc8213 AM |
135 | fields.add(curField); |
136 | } | |
137 | ||
26859ddb | 138 | /* Add context information as CtfTmfEventField */ |
4c9d2941 | 139 | long ip = -1; |
26859ddb BH |
140 | StructDefinition structContext = eventDef.getContext(); |
141 | if (structContext != null) { | |
142 | definitions = structContext.getDefinitions(); | |
143 | String curContextName; | |
144 | Definition curContextDef; | |
145 | CtfTmfEventField curContext; | |
146 | it = definitions.entrySet().iterator(); | |
147 | while(it.hasNext()) { | |
148 | Entry<String, Definition> entry = it.next(); | |
4c9d2941 MK |
149 | /* This is to get the instruction pointer if available */ |
150 | if (entry.getKey().equals("_ip") && //$NON-NLS-1$ | |
151 | (entry.getValue() instanceof IntegerDefinition)) { | |
152 | ip = ((IntegerDefinition) entry.getValue()).getValue(); | |
153 | } | |
26859ddb BH |
154 | /* Prefix field name to */ |
155 | curContextName = CONTEXT_FIELD_PREFIX + entry.getKey(); | |
156 | curContextDef = entry.getValue(); | |
157 | curContext = CtfTmfEventField.parseField(curContextDef, curContextName); | |
158 | fields.add(curContext); | |
159 | } | |
160 | } | |
4c9d2941 MK |
161 | /* Add callsite */ |
162 | final String name = eventDef.getDeclaration().getName(); | |
163 | List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name); | |
84115199 | 164 | if (!eventList.isEmpty()) { |
4c9d2941 MK |
165 | final String callsite = "callsite"; //$NON-NLS-1$ |
166 | if (eventList.size() == 1 || ip == -1) { | |
167 | CTFCallsite cs = eventList.get(0); | |
168 | fields.add(new CTFStringField(cs.toString(), callsite)); | |
169 | } else { | |
170 | fields.add(new CTFStringField( | |
171 | fTrace.getCTFTrace().getCallsite(name, ip).toString(), | |
172 | callsite)); | |
173 | } | |
174 | } | |
26859ddb | 175 | |
a3fc8213 AM |
176 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
177 | } | |
178 | ||
179 | /** | |
180 | * Copy constructor | |
181 | * | |
182 | * @param other | |
063f0d27 | 183 | * CtfTmfEvent to copy |
a3fc8213 AM |
184 | */ |
185 | public CtfTmfEvent(CtfTmfEvent other) { | |
58f3bc52 | 186 | /* There is only one reference to the trace, so we can shallow-copy it */ |
a3fc8213 | 187 | this.fTrace = other.getTrace(); |
58f3bc52 | 188 | |
80349bf7 AM |
189 | /* Copy the timestamp (immutable) */ |
190 | this.fTimestamp = other.fTimestamp; | |
58f3bc52 | 191 | |
a3fc8213 | 192 | /* Primitives, those will be copied by value */ |
a3fc8213 AM |
193 | this.sourceCPU = other.sourceCPU; |
194 | this.typeId = other.typeId; | |
195 | ||
196 | /* Strings are immutable, it's safe to shallow-copy them */ | |
197 | this.eventName = other.eventName; | |
198 | this.fileName = other.fileName; | |
199 | ||
80349bf7 AM |
200 | /* Copy the fields over (immutable) */ |
201 | this.fContent = other.fContent; | |
8e964be1 MK |
202 | |
203 | /* | |
204 | * Copy the reference to the custom attributes (should be the same | |
205 | * object for all events of this type) | |
206 | */ | |
207 | this.fDeclaration = other.fDeclaration; | |
a3fc8213 AM |
208 | } |
209 | ||
210 | /** | |
b8a6e46d AM |
211 | * Inner constructor to create "null" events. Don't use this directly in |
212 | * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an | |
213 | * empty event. | |
c26afeaf | 214 | * |
b8a6e46d AM |
215 | * This needs to be public however because it's used in extension points, |
216 | * and the framework will use this constructor to get the class type. | |
a3fc8213 | 217 | */ |
ce2388e0 | 218 | public CtfTmfEvent() { |
a3fc8213 | 219 | this.fTrace = null; |
58f3bc52 | 220 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
221 | this.sourceCPU = -1; |
222 | this.typeId = -1; | |
223 | this.fileName = NO_STREAM; | |
224 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
306dc902 | 225 | this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
8e964be1 | 226 | this.fDeclaration = null; |
a3fc8213 AM |
227 | } |
228 | ||
229 | // ------------------------------------------------------------------------ | |
230 | // Getters/Setters/Predicates | |
231 | // ------------------------------------------------------------------------ | |
232 | ||
58f3bc52 | 233 | private static CtfTmfEvent nullEvent = new CtfTmfEvent(); |
a3fc8213 AM |
234 | |
235 | /** | |
236 | * Get a null event | |
237 | * | |
58f3bc52 AM |
238 | * @return An empty event. |
239 | */ | |
a3fc8213 | 240 | public static CtfTmfEvent getNullEvent() { |
a3fc8213 AM |
241 | return nullEvent; |
242 | } | |
243 | ||
a3fc8213 AM |
244 | /** |
245 | * Gets the cpu core the event was recorded on. | |
246 | * | |
58f3bc52 AM |
247 | * @return The cpu id for a given source. In lttng it's from CPUINFO |
248 | */ | |
a3fc8213 AM |
249 | public int getCPU() { |
250 | return this.sourceCPU; | |
251 | } | |
252 | ||
253 | /** | |
58f3bc52 | 254 | * Return this event's ID, according to the trace's metadata. |
a3fc8213 | 255 | * |
58f3bc52 AM |
256 | * Watch out, this ID is not constant from one trace to another for the same |
257 | * event types! Use "getEventName()" for a constant reference. | |
258 | * | |
259 | * @return The event ID | |
260 | */ | |
a3fc8213 AM |
261 | public long getID() { |
262 | return this.typeId; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Gets the name of a current event. | |
267 | * | |
58f3bc52 AM |
268 | * @return The event name |
269 | */ | |
a3fc8213 AM |
270 | public String getEventName() { |
271 | return eventName; | |
272 | } | |
273 | ||
274 | /** | |
275 | * Gets the channel name of a field. | |
276 | * | |
58f3bc52 AM |
277 | * @return The channel name. |
278 | */ | |
a3fc8213 AM |
279 | public String getChannelName() { |
280 | return this.fileName; | |
281 | } | |
282 | ||
283 | @Override | |
284 | public CtfTmfTrace getTrace() { | |
285 | return fTrace; | |
286 | } | |
287 | ||
288 | @Override | |
289 | public long getRank() { | |
290 | // TODO Auto-generated method stub | |
291 | return 0; | |
292 | } | |
293 | ||
a3fc8213 AM |
294 | @Override |
295 | public ITmfTimestamp getTimestamp() { | |
a3fc8213 AM |
296 | return fTimestamp; |
297 | } | |
298 | ||
299 | @Override | |
300 | public String getSource() { | |
ce2388e0 | 301 | // TODO Returns CPU for now |
58f3bc52 | 302 | return Integer.toString(getCPU()); |
a3fc8213 AM |
303 | } |
304 | ||
305 | @Override | |
306 | public ITmfEventType getType() { | |
c26afeaf MD |
307 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
308 | if( ctfTmfEventType == null ){ | |
309 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); | |
310 | } | |
311 | return ctfTmfEventType; | |
a3fc8213 AM |
312 | } |
313 | ||
314 | @Override | |
315 | public ITmfEventField getContent() { | |
316 | return fContent; | |
317 | } | |
318 | ||
319 | @Override | |
320 | public String getReference() { | |
58f3bc52 | 321 | return getChannelName(); |
a3fc8213 AM |
322 | } |
323 | ||
8e964be1 MK |
324 | /** |
325 | * List the custom CTF attributes for events of this type. | |
326 | * | |
327 | * @return The list of custom attribute names. Should not be null, but could | |
328 | * be empty. | |
329 | * @since 2.0 | |
330 | */ | |
331 | public Set<String> listCustomAttributes() { | |
332 | if (fDeclaration == null) { | |
333 | return new HashSet<String>(); | |
334 | } | |
335 | return fDeclaration.getCustomAttributes(); | |
336 | } | |
337 | ||
338 | /** | |
339 | * Get the value of a custom CTF attributes for this event's type. | |
340 | * | |
341 | * @param name | |
342 | * Name of the the custom attribute | |
343 | * @return Value of this attribute, or null if there is no attribute with | |
344 | * that name | |
345 | * @since 2.0 | |
346 | */ | |
347 | public String getCustomAttribute(String name) { | |
348 | if (fDeclaration == null) { | |
349 | return null; | |
350 | } | |
351 | return fDeclaration.getCustomAttribute(name); | |
352 | } | |
353 | ||
a3fc8213 AM |
354 | @Override |
355 | public CtfTmfEvent clone() { | |
356 | return new CtfTmfEvent(this); | |
357 | } | |
93bfd50a | 358 | |
531987c8 PT |
359 | /** |
360 | * @since 2.0 | |
361 | */ | |
93bfd50a PT |
362 | @Override |
363 | public Object getAdapter(Class adapter) { | |
364 | if (adapter == IPropertySource.class) { | |
365 | return new TmfEventPropertySource(this); | |
366 | } | |
367 | return null; | |
368 | } | |
a3fc8213 | 369 | } |