| 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; |
| 16 | import java.util.Iterator; |
| 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; |
| 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; |
| 27 | |
| 28 | /** |
| 29 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
| 30 | * types of Declaration to native Java types. |
| 31 | * |
| 32 | * @version 1.0 |
| 33 | * @author Alexandre Montplaisir |
| 34 | */ |
| 35 | public final class CtfTmfEvent implements ITmfEvent, Cloneable { |
| 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$ |
| 43 | |
| 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 | |
| 56 | private final CtfTmfContent fContent; |
| 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 |
| 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 |
| 72 | */ |
| 73 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
| 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 */ |
| 88 | Long offset = originTrace.getCTFTrace().getOffset(); |
| 89 | this.timestamp = eventDef.getTimestamp() + offset; |
| 90 | this.sourceCPU = eventDef.getCPU(); |
| 91 | this.typeId = eventDef.getDeclaration().getId(); |
| 92 | this.eventName = eventDef.getDeclaration().getName(); |
| 93 | this.fileName = fileName; |
| 94 | |
| 95 | /* Read the fields */ |
| 96 | this.fContent = new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID, |
| 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 |
| 105 | * CTF EventDefinition to read |
| 106 | * @return CtfTmfEventField[] The array of fields that were read |
| 107 | */ |
| 108 | public static CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
| 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; |
| 116 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
| 117 | while(it.hasNext()) { |
| 118 | Entry<String, Definition> entry = it.next(); |
| 119 | curFieldName = entry.getKey(); |
| 120 | curFieldDef = entry.getValue(); |
| 121 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
| 122 | fields.add(curField); |
| 123 | } |
| 124 | |
| 125 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Copy constructor |
| 130 | * |
| 131 | * @param other |
| 132 | * CtfTmfEvent to copy |
| 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 */ |
| 146 | this.fContent = (CtfTmfContent) other.fContent.clone(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 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. |
| 153 | * |
| 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. |
| 156 | */ |
| 157 | public CtfTmfEvent() { |
| 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; |
| 164 | this.fContent = new CtfTmfContent("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
| 165 | } |
| 166 | |
| 167 | // ------------------------------------------------------------------------ |
| 168 | // Getters/Setters/Predicates |
| 169 | // ------------------------------------------------------------------------ |
| 170 | |
| 171 | private static CtfTmfEvent nullEvent = null; |
| 172 | |
| 173 | /** |
| 174 | * Get a null event |
| 175 | * |
| 176 | * @return an empty event. */ |
| 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 | * |
| 187 | * @return the current timestamp (long) */ |
| 188 | public long getTimestampValue() { |
| 189 | return this.timestamp; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Gets the cpu core the event was recorded on. |
| 194 | * |
| 195 | * @return the cpu id for a given source. In lttng it's from CPUINFO */ |
| 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 | * |
| 205 | |
| 206 | * @return the event ID */ |
| 207 | public long getID() { |
| 208 | return this.typeId; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Gets the name of a current event. |
| 213 | * |
| 214 | * @return the event name */ |
| 215 | public String getEventName() { |
| 216 | return eventName; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Gets the channel name of a field. |
| 221 | * |
| 222 | * @return the channel name. */ |
| 223 | public String getChannelName() { |
| 224 | return this.fileName; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Method getTrace. |
| 229 | * @return CtfTmfTrace |
| 230 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace() |
| 231 | */ |
| 232 | @Override |
| 233 | public CtfTmfTrace getTrace() { |
| 234 | return fTrace; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Method getRank. |
| 239 | * @return long |
| 240 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank() |
| 241 | */ |
| 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 |
| 252 | /** |
| 253 | * Method getTimestamp. |
| 254 | * @return ITmfTimestamp |
| 255 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp() |
| 256 | */ |
| 257 | @Override |
| 258 | public ITmfTimestamp getTimestamp() { |
| 259 | if (fTimestamp == null) { |
| 260 | fTimestamp = new CtfTmfTimestamp(timestamp); |
| 261 | } |
| 262 | return fTimestamp; |
| 263 | } |
| 264 | |
| 265 | String fSource = null; |
| 266 | /** |
| 267 | * Method getSource. |
| 268 | * @return String |
| 269 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource() |
| 270 | */ |
| 271 | @Override |
| 272 | public String getSource() { |
| 273 | // TODO Returns CPU for now |
| 274 | if(fSource == null) { |
| 275 | fSource= Integer.toString(getCPU()); |
| 276 | } |
| 277 | return fSource; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Method getType. |
| 282 | * @return ITmfEventType |
| 283 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType() |
| 284 | */ |
| 285 | @Override |
| 286 | public ITmfEventType getType() { |
| 287 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
| 288 | if( ctfTmfEventType == null ){ |
| 289 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); |
| 290 | } |
| 291 | return ctfTmfEventType; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Method getContent. |
| 296 | * @return ITmfEventField |
| 297 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent() |
| 298 | */ |
| 299 | @Override |
| 300 | public ITmfEventField getContent() { |
| 301 | return fContent; |
| 302 | } |
| 303 | |
| 304 | String fReference = null; |
| 305 | /** |
| 306 | * Method getReference. |
| 307 | * @return String |
| 308 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference() |
| 309 | */ |
| 310 | @Override |
| 311 | public String getReference() { |
| 312 | if( fReference == null){ |
| 313 | fReference = getChannelName(); |
| 314 | } |
| 315 | return fReference; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Method clone. |
| 320 | * @return CtfTmfEvent |
| 321 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone() |
| 322 | */ |
| 323 | @Override |
| 324 | public CtfTmfEvent clone() { |
| 325 | return new CtfTmfEvent(this); |
| 326 | } |
| 327 | } |