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