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