| 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: |
| 10 | * Alexandre Montplaisir - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.HashMap; |
| 17 | import java.util.Iterator; |
| 18 | import java.util.List; |
| 19 | import java.util.Map.Entry; |
| 20 | |
| 21 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 23 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 26 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; |
| 27 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
| 28 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 29 | |
| 30 | /** |
| 31 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
| 32 | * types of Declaration to native Java types. |
| 33 | * |
| 34 | * @version 1.0 |
| 35 | * @author Alexandre Montplaisir |
| 36 | */ |
| 37 | public final class CtfTmfEvent implements ITmfEvent, Cloneable { |
| 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$ |
| 45 | |
| 46 | /** Prefix for Context information stored as CtfTmfEventfield */ |
| 47 | private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$ |
| 48 | |
| 49 | // ------------------------------------------------------------------------ |
| 50 | // Attributes |
| 51 | // ------------------------------------------------------------------------ |
| 52 | |
| 53 | private final CtfTmfTrace fTrace; |
| 54 | private final ITmfTimestamp fTimestamp; |
| 55 | private final int sourceCPU; |
| 56 | private final long typeId; |
| 57 | private final String eventName; |
| 58 | private final String fileName; |
| 59 | |
| 60 | private final TmfEventField fContent; |
| 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 |
| 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 |
| 76 | */ |
| 77 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
| 78 | CtfTmfTrace originTrace) { |
| 79 | this.fTrace = originTrace; |
| 80 | |
| 81 | if (eventDef == null) { |
| 82 | this.fTimestamp = new CtfTmfTimestamp(-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 */ |
| 92 | long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); |
| 93 | this.fTimestamp = new CtfTmfTimestamp(ts); |
| 94 | this.sourceCPU = eventDef.getCPU(); |
| 95 | this.typeId = eventDef.getDeclaration().getId(); |
| 96 | this.eventName = eventDef.getDeclaration().getName(); |
| 97 | this.fileName = fileName; |
| 98 | |
| 99 | /* Read the fields */ |
| 100 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Extract the field information from the structDefinition haze-inducing |
| 105 | * mess, and put them into something ITmfEventField can cope with. |
| 106 | */ |
| 107 | private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
| 108 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); |
| 109 | |
| 110 | StructDefinition structFields = eventDef.getFields(); |
| 111 | HashMap<String, Definition> definitions = structFields.getDefinitions(); |
| 112 | String curFieldName; |
| 113 | Definition curFieldDef; |
| 114 | CtfTmfEventField curField; |
| 115 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
| 116 | while(it.hasNext()) { |
| 117 | Entry<String, Definition> entry = it.next(); |
| 118 | curFieldName = entry.getKey(); |
| 119 | curFieldDef = entry.getValue(); |
| 120 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
| 121 | fields.add(curField); |
| 122 | } |
| 123 | |
| 124 | /* Add context information as CtfTmfEventField */ |
| 125 | StructDefinition structContext = eventDef.getContext(); |
| 126 | if (structContext != null) { |
| 127 | definitions = structContext.getDefinitions(); |
| 128 | String curContextName; |
| 129 | Definition curContextDef; |
| 130 | CtfTmfEventField curContext; |
| 131 | it = definitions.entrySet().iterator(); |
| 132 | while(it.hasNext()) { |
| 133 | Entry<String, Definition> entry = it.next(); |
| 134 | /* Prefix field name to */ |
| 135 | curContextName = CONTEXT_FIELD_PREFIX + entry.getKey(); |
| 136 | curContextDef = entry.getValue(); |
| 137 | curContext = CtfTmfEventField.parseField(curContextDef, curContextName); |
| 138 | fields.add(curContext); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Copy constructor |
| 147 | * |
| 148 | * @param other |
| 149 | * CtfTmfEvent to copy |
| 150 | */ |
| 151 | public CtfTmfEvent(CtfTmfEvent other) { |
| 152 | /* There is only one reference to the trace, so we can shallow-copy it */ |
| 153 | this.fTrace = other.getTrace(); |
| 154 | |
| 155 | /* |
| 156 | * Copy the timestamp |
| 157 | * FIXME This can be switched to a shallow-copy once timestamps are |
| 158 | * made immutable. |
| 159 | */ |
| 160 | this.fTimestamp = new CtfTmfTimestamp(other.fTimestamp.getValue()); |
| 161 | |
| 162 | /* Primitives, those will be copied by value */ |
| 163 | this.sourceCPU = other.sourceCPU; |
| 164 | this.typeId = other.typeId; |
| 165 | |
| 166 | /* Strings are immutable, it's safe to shallow-copy them */ |
| 167 | this.eventName = other.eventName; |
| 168 | this.fileName = other.fileName; |
| 169 | |
| 170 | /* Copy the fields over */ |
| 171 | this.fContent = other.fContent.clone(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Inner constructor to create "null" events. Don't use this directly in |
| 176 | * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an |
| 177 | * empty event. |
| 178 | * |
| 179 | * This needs to be public however because it's used in extension points, |
| 180 | * and the framework will use this constructor to get the class type. |
| 181 | */ |
| 182 | public CtfTmfEvent() { |
| 183 | this.fTrace = null; |
| 184 | this.fTimestamp = new CtfTmfTimestamp(-1); |
| 185 | this.sourceCPU = -1; |
| 186 | this.typeId = -1; |
| 187 | this.fileName = NO_STREAM; |
| 188 | this.eventName = EMPTY_CTF_EVENT_NAME; |
| 189 | this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
| 190 | } |
| 191 | |
| 192 | // ------------------------------------------------------------------------ |
| 193 | // Getters/Setters/Predicates |
| 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | private static CtfTmfEvent nullEvent = new CtfTmfEvent(); |
| 197 | |
| 198 | /** |
| 199 | * Get a null event |
| 200 | * |
| 201 | * @return An empty event. |
| 202 | */ |
| 203 | public static CtfTmfEvent getNullEvent() { |
| 204 | return nullEvent; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Gets the cpu core the event was recorded on. |
| 209 | * |
| 210 | * @return The cpu id for a given source. In lttng it's from CPUINFO |
| 211 | */ |
| 212 | public int getCPU() { |
| 213 | return this.sourceCPU; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Return this event's ID, according to the trace's metadata. |
| 218 | * |
| 219 | * Watch out, this ID is not constant from one trace to another for the same |
| 220 | * event types! Use "getEventName()" for a constant reference. |
| 221 | * |
| 222 | * @return The event ID |
| 223 | */ |
| 224 | public long getID() { |
| 225 | return this.typeId; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Gets the name of a current event. |
| 230 | * |
| 231 | * @return The event name |
| 232 | */ |
| 233 | public String getEventName() { |
| 234 | return eventName; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Gets the channel name of a field. |
| 239 | * |
| 240 | * @return The channel name. |
| 241 | */ |
| 242 | public String getChannelName() { |
| 243 | return this.fileName; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public CtfTmfTrace getTrace() { |
| 248 | return fTrace; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public long getRank() { |
| 253 | // TODO Auto-generated method stub |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public ITmfTimestamp getTimestamp() { |
| 259 | return fTimestamp; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public String getSource() { |
| 264 | // TODO Returns CPU for now |
| 265 | return Integer.toString(getCPU()); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public ITmfEventType getType() { |
| 270 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
| 271 | if( ctfTmfEventType == null ){ |
| 272 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); |
| 273 | } |
| 274 | return ctfTmfEventType; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public ITmfEventField getContent() { |
| 279 | return fContent; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public String getReference() { |
| 284 | return getChannelName(); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public CtfTmfEvent clone() { |
| 289 | return new CtfTmfEvent(this); |
| 290 | } |
| 291 | } |