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 | * | |
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; | |
aa572e22 | 16 | import java.util.Iterator; |
a3fc8213 AM |
17 | import java.util.List; |
18 | import java.util.Map.Entry; | |
19 | ||
4c9d2941 | 20 | import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; |
a3fc8213 AM |
21 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
4c9d2941 | 23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
a3fc8213 | 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; | |
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. | |
26859ddb | 33 | * |
d09f973b FC |
34 | * @version 1.0 |
35 | * @author Alexandre Montplaisir | |
a3fc8213 | 36 | */ |
0879b6b9 | 37 | public 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; | |
54 | private final long timestamp; | |
55 | private final int sourceCPU; | |
56 | private final long typeId; | |
57 | private final String eventName; | |
58 | private final String fileName; | |
59 | ||
ce2388e0 | 60 | private final CtfTmfContent 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) { | |
82 | this.timestamp = -1; | |
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 */ | |
ce2388e0 | 92 | Long offset = originTrace.getCTFTrace().getOffset(); |
aa572e22 | 93 | this.timestamp = eventDef.getTimestamp() + offset; |
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 */ | |
ce2388e0 | 100 | this.fContent = new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID, |
a3fc8213 AM |
101 | parseFields(eventDef)); |
102 | } | |
103 | ||
104 | /** | |
105 | * Extract the field information from the structDefinition haze-inducing | |
106 | * mess, and put them into something ITmfEventField can cope with. | |
107 | * | |
108 | * @param eventDef | |
063f0d27 AM |
109 | * CTF EventDefinition to read |
110 | * @return CtfTmfEventField[] The array of fields that were read | |
a3fc8213 | 111 | */ |
4c9d2941 | 112 | private CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
a3fc8213 AM |
113 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); |
114 | ||
115 | StructDefinition structFields = eventDef.getFields(); | |
116 | HashMap<String, Definition> definitions = structFields.getDefinitions(); | |
4c9d2941 | 117 | String curFieldName = null; |
a3fc8213 AM |
118 | Definition curFieldDef; |
119 | CtfTmfEventField curField; | |
aa572e22 MK |
120 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
121 | while(it.hasNext()) { | |
122 | Entry<String, Definition> entry = it.next(); | |
a3fc8213 AM |
123 | curFieldName = entry.getKey(); |
124 | curFieldDef = entry.getValue(); | |
125 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
a3fc8213 AM |
126 | fields.add(curField); |
127 | } | |
128 | ||
26859ddb | 129 | /* Add context information as CtfTmfEventField */ |
4c9d2941 | 130 | long ip = -1; |
26859ddb BH |
131 | StructDefinition structContext = eventDef.getContext(); |
132 | if (structContext != null) { | |
133 | definitions = structContext.getDefinitions(); | |
134 | String curContextName; | |
135 | Definition curContextDef; | |
136 | CtfTmfEventField curContext; | |
137 | it = definitions.entrySet().iterator(); | |
138 | while(it.hasNext()) { | |
139 | Entry<String, Definition> entry = it.next(); | |
4c9d2941 MK |
140 | /* This is to get the instruction pointer if available */ |
141 | if (entry.getKey().equals("_ip") && //$NON-NLS-1$ | |
142 | (entry.getValue() instanceof IntegerDefinition)) { | |
143 | ip = ((IntegerDefinition) entry.getValue()).getValue(); | |
144 | } | |
26859ddb BH |
145 | /* Prefix field name to */ |
146 | curContextName = CONTEXT_FIELD_PREFIX + entry.getKey(); | |
147 | curContextDef = entry.getValue(); | |
148 | curContext = CtfTmfEventField.parseField(curContextDef, curContextName); | |
149 | fields.add(curContext); | |
150 | } | |
151 | } | |
4c9d2941 MK |
152 | /* Add callsite */ |
153 | final String name = eventDef.getDeclaration().getName(); | |
154 | List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name); | |
155 | if (eventList != null) { | |
156 | final String callsite = "callsite"; //$NON-NLS-1$ | |
157 | if (eventList.size() == 1 || ip == -1) { | |
158 | CTFCallsite cs = eventList.get(0); | |
159 | fields.add(new CTFStringField(cs.toString(), callsite)); | |
160 | } else { | |
161 | fields.add(new CTFStringField( | |
162 | fTrace.getCTFTrace().getCallsite(name, ip).toString(), | |
163 | callsite)); | |
164 | } | |
165 | } | |
26859ddb | 166 | |
a3fc8213 AM |
167 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
168 | } | |
169 | ||
170 | /** | |
171 | * Copy constructor | |
172 | * | |
173 | * @param other | |
063f0d27 | 174 | * CtfTmfEvent to copy |
a3fc8213 AM |
175 | */ |
176 | public CtfTmfEvent(CtfTmfEvent other) { | |
177 | this.fTrace = other.getTrace(); | |
178 | /* Primitives, those will be copied by value */ | |
179 | this.timestamp = other.timestamp; | |
180 | this.sourceCPU = other.sourceCPU; | |
181 | this.typeId = other.typeId; | |
182 | ||
183 | /* Strings are immutable, it's safe to shallow-copy them */ | |
184 | this.eventName = other.eventName; | |
185 | this.fileName = other.fileName; | |
186 | ||
187 | /* Copy the fields over */ | |
ce2388e0 | 188 | this.fContent = (CtfTmfContent) other.fContent.clone(); |
a3fc8213 AM |
189 | } |
190 | ||
191 | /** | |
b8a6e46d AM |
192 | * Inner constructor to create "null" events. Don't use this directly in |
193 | * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an | |
194 | * empty event. | |
c26afeaf | 195 | * |
b8a6e46d AM |
196 | * This needs to be public however because it's used in extension points, |
197 | * and the framework will use this constructor to get the class type. | |
a3fc8213 | 198 | */ |
ce2388e0 | 199 | public CtfTmfEvent() { |
a3fc8213 AM |
200 | this.fTrace = null; |
201 | this.timestamp = -1; | |
202 | this.sourceCPU = -1; | |
203 | this.typeId = -1; | |
204 | this.fileName = NO_STREAM; | |
205 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
aa572e22 | 206 | this.fContent = new CtfTmfContent("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
a3fc8213 AM |
207 | } |
208 | ||
209 | // ------------------------------------------------------------------------ | |
210 | // Getters/Setters/Predicates | |
211 | // ------------------------------------------------------------------------ | |
212 | ||
213 | private static CtfTmfEvent nullEvent = null; | |
214 | ||
215 | /** | |
216 | * Get a null event | |
217 | * | |
b1baa808 | 218 | * @return an empty event. */ |
a3fc8213 AM |
219 | public static CtfTmfEvent getNullEvent() { |
220 | if (nullEvent == null) { | |
221 | nullEvent = new CtfTmfEvent(); | |
222 | } | |
223 | return nullEvent; | |
224 | } | |
225 | ||
226 | /** | |
227 | * Gets the current timestamp of the event | |
228 | * | |
b1baa808 | 229 | * @return the current timestamp (long) */ |
a3fc8213 AM |
230 | public long getTimestampValue() { |
231 | return this.timestamp; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Gets the cpu core the event was recorded on. | |
236 | * | |
b1baa808 | 237 | * @return the cpu id for a given source. In lttng it's from CPUINFO */ |
a3fc8213 AM |
238 | public int getCPU() { |
239 | return this.sourceCPU; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Return this event's ID, according to the trace's metadata. Watch out, | |
244 | * this ID is not constant from one trace to another for the same event | |
245 | * types! Use "getEventName()" for a constant reference. | |
246 | * | |
b1baa808 MK |
247 | |
248 | * @return the event ID */ | |
a3fc8213 AM |
249 | public long getID() { |
250 | return this.typeId; | |
251 | } | |
252 | ||
253 | /** | |
254 | * Gets the name of a current event. | |
255 | * | |
b1baa808 | 256 | * @return the event name */ |
a3fc8213 AM |
257 | public String getEventName() { |
258 | return eventName; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Gets the channel name of a field. | |
263 | * | |
b1baa808 | 264 | * @return the channel name. */ |
a3fc8213 AM |
265 | public String getChannelName() { |
266 | return this.fileName; | |
267 | } | |
268 | ||
b1baa808 MK |
269 | /** |
270 | * Method getTrace. | |
271 | * @return CtfTmfTrace | |
272 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace() | |
273 | */ | |
a3fc8213 AM |
274 | @Override |
275 | public CtfTmfTrace getTrace() { | |
276 | return fTrace; | |
277 | } | |
278 | ||
b1baa808 MK |
279 | /** |
280 | * Method getRank. | |
281 | * @return long | |
282 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank() | |
283 | */ | |
a3fc8213 AM |
284 | @Override |
285 | public long getRank() { | |
286 | // TODO Auto-generated method stub | |
287 | return 0; | |
288 | } | |
289 | ||
290 | private ITmfTimestamp fTimestamp = null; | |
291 | ||
292 | // TODO Benchmark if the singleton approach is faster than just | |
293 | // instantiating a final fTimestramp right away at creation time | |
b1baa808 MK |
294 | /** |
295 | * Method getTimestamp. | |
296 | * @return ITmfTimestamp | |
297 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp() | |
298 | */ | |
a3fc8213 AM |
299 | @Override |
300 | public ITmfTimestamp getTimestamp() { | |
301 | if (fTimestamp == null) { | |
b0f9e44d | 302 | fTimestamp = new CtfTmfTimestamp(timestamp); |
a3fc8213 AM |
303 | } |
304 | return fTimestamp; | |
305 | } | |
306 | ||
ce2388e0 | 307 | String fSource = null; |
b1baa808 MK |
308 | /** |
309 | * Method getSource. | |
310 | * @return String | |
311 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource() | |
312 | */ | |
a3fc8213 AM |
313 | @Override |
314 | public String getSource() { | |
ce2388e0 FC |
315 | // TODO Returns CPU for now |
316 | if(fSource == null) { | |
317 | fSource= Integer.toString(getCPU()); | |
318 | } | |
319 | return fSource; | |
a3fc8213 AM |
320 | } |
321 | ||
b1baa808 MK |
322 | /** |
323 | * Method getType. | |
324 | * @return ITmfEventType | |
325 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType() | |
326 | */ | |
a3fc8213 AM |
327 | @Override |
328 | public ITmfEventType getType() { | |
c26afeaf MD |
329 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
330 | if( ctfTmfEventType == null ){ | |
331 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); | |
332 | } | |
333 | return ctfTmfEventType; | |
a3fc8213 AM |
334 | } |
335 | ||
b1baa808 MK |
336 | /** |
337 | * Method getContent. | |
338 | * @return ITmfEventField | |
339 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent() | |
340 | */ | |
a3fc8213 AM |
341 | @Override |
342 | public ITmfEventField getContent() { | |
343 | return fContent; | |
344 | } | |
345 | ||
ce2388e0 | 346 | String fReference = null; |
b1baa808 MK |
347 | /** |
348 | * Method getReference. | |
349 | * @return String | |
350 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference() | |
351 | */ | |
a3fc8213 AM |
352 | @Override |
353 | public String getReference() { | |
ce2388e0 FC |
354 | if( fReference == null){ |
355 | fReference = getChannelName(); | |
356 | } | |
357 | return fReference; | |
a3fc8213 AM |
358 | } |
359 | ||
b1baa808 MK |
360 | /** |
361 | * Method clone. | |
362 | * @return CtfTmfEvent | |
363 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone() | |
364 | */ | |
a3fc8213 AM |
365 | @Override |
366 | public CtfTmfEvent clone() { | |
367 | return new CtfTmfEvent(this); | |
368 | } | |
369 | } |