| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011-2013 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.HashSet; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.List; |
| 20 | import java.util.Map.Entry; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; |
| 24 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 25 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
| 26 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 27 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 28 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 29 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 31 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; |
| 32 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
| 33 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 34 | import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource; |
| 35 | import org.eclipse.ui.views.properties.IPropertySource; |
| 36 | |
| 37 | /** |
| 38 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
| 39 | * types of Declaration to native Java types. |
| 40 | * |
| 41 | * @version 1.0 |
| 42 | * @author Alexandre Montplaisir |
| 43 | * @since 2.0 |
| 44 | */ |
| 45 | public final class CtfTmfEvent implements ITmfEvent, Cloneable { |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Constants |
| 49 | // ------------------------------------------------------------------------ |
| 50 | |
| 51 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ |
| 52 | private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ |
| 53 | |
| 54 | /** Prefix for Context information stored as CtfTmfEventfield */ |
| 55 | private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$ |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // Attributes |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | private final CtfTmfTrace fTrace; |
| 62 | private final ITmfTimestamp fTimestamp; |
| 63 | private final int sourceCPU; |
| 64 | private final long typeId; |
| 65 | private final String eventName; |
| 66 | private final String fileName; |
| 67 | |
| 68 | private final TmfEventField fContent; |
| 69 | private final IEventDeclaration fDeclaration; |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Constructors |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Usual CTFEvent constructor, where we read an event from the trace (via |
| 77 | * the StreamInputReader). |
| 78 | * |
| 79 | * @param eventDef |
| 80 | * CTF EventDefinition object corresponding to this trace event |
| 81 | * @param fileName |
| 82 | * The path to the trace file |
| 83 | * @param originTrace |
| 84 | * The trace from which this event originates |
| 85 | */ |
| 86 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
| 87 | CtfTmfTrace originTrace) { |
| 88 | this.fTrace = originTrace; |
| 89 | |
| 90 | if (eventDef == null) { |
| 91 | this.fTimestamp = new CtfTmfTimestamp(-1); |
| 92 | this.sourceCPU = -1; |
| 93 | this.typeId = -1; |
| 94 | this.fileName = NO_STREAM; |
| 95 | this.eventName = EMPTY_CTF_EVENT_NAME; |
| 96 | this.fContent = null; |
| 97 | this.fDeclaration = null; |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | /* Read the base event info */ |
| 102 | long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); |
| 103 | this.fTimestamp = new CtfTmfTimestamp(ts); |
| 104 | this.sourceCPU = eventDef.getCPU(); |
| 105 | this.typeId = eventDef.getDeclaration().getId(); |
| 106 | this.eventName = eventDef.getDeclaration().getName(); |
| 107 | this.fileName = fileName; |
| 108 | |
| 109 | /* Read the fields */ |
| 110 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef)); |
| 111 | |
| 112 | /* Keep a reference to this event's CTF declaration */ |
| 113 | this.fDeclaration = eventDef.getDeclaration(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Extract the field information from the structDefinition haze-inducing |
| 118 | * mess, and put them into something ITmfEventField can cope with. |
| 119 | */ |
| 120 | private CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
| 121 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); |
| 122 | |
| 123 | StructDefinition structFields = eventDef.getFields(); |
| 124 | HashMap<String, Definition> definitions = structFields.getDefinitions(); |
| 125 | String curFieldName = null; |
| 126 | Definition curFieldDef; |
| 127 | CtfTmfEventField curField; |
| 128 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
| 129 | while(it.hasNext()) { |
| 130 | Entry<String, Definition> entry = it.next(); |
| 131 | curFieldName = entry.getKey(); |
| 132 | curFieldDef = entry.getValue(); |
| 133 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
| 134 | fields.add(curField); |
| 135 | } |
| 136 | |
| 137 | /* Add context information as CtfTmfEventField */ |
| 138 | long ip = -1; |
| 139 | StructDefinition structContext = eventDef.getContext(); |
| 140 | if (structContext != null) { |
| 141 | definitions = structContext.getDefinitions(); |
| 142 | String curContextName; |
| 143 | Definition curContextDef; |
| 144 | CtfTmfEventField curContext; |
| 145 | it = definitions.entrySet().iterator(); |
| 146 | while(it.hasNext()) { |
| 147 | Entry<String, Definition> entry = it.next(); |
| 148 | /* This is to get the instruction pointer if available */ |
| 149 | if (entry.getKey().equals("_ip") && //$NON-NLS-1$ |
| 150 | (entry.getValue() instanceof IntegerDefinition)) { |
| 151 | ip = ((IntegerDefinition) entry.getValue()).getValue(); |
| 152 | } |
| 153 | /* Prefix field name to */ |
| 154 | curContextName = CONTEXT_FIELD_PREFIX + entry.getKey(); |
| 155 | curContextDef = entry.getValue(); |
| 156 | curContext = CtfTmfEventField.parseField(curContextDef, curContextName); |
| 157 | fields.add(curContext); |
| 158 | } |
| 159 | } |
| 160 | /* Add callsite */ |
| 161 | final String name = eventDef.getDeclaration().getName(); |
| 162 | List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name); |
| 163 | if (!eventList.isEmpty()) { |
| 164 | final String callsite = "callsite"; //$NON-NLS-1$ |
| 165 | if (eventList.size() == 1 || ip == -1) { |
| 166 | CTFCallsite cs = eventList.get(0); |
| 167 | fields.add(new CTFStringField(cs.toString(), callsite)); |
| 168 | } else { |
| 169 | fields.add(new CTFStringField( |
| 170 | fTrace.getCTFTrace().getCallsite(name, ip).toString(), |
| 171 | callsite)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Copy constructor |
| 180 | * |
| 181 | * @param other |
| 182 | * CtfTmfEvent to copy |
| 183 | */ |
| 184 | public CtfTmfEvent(CtfTmfEvent other) { |
| 185 | /* There is only one reference to the trace, so we can shallow-copy it */ |
| 186 | this.fTrace = other.getTrace(); |
| 187 | |
| 188 | /* Copy the timestamp (immutable) */ |
| 189 | this.fTimestamp = other.fTimestamp; |
| 190 | |
| 191 | /* Primitives, those will be copied by value */ |
| 192 | this.sourceCPU = other.sourceCPU; |
| 193 | this.typeId = other.typeId; |
| 194 | |
| 195 | /* Strings are immutable, it's safe to shallow-copy them */ |
| 196 | this.eventName = other.eventName; |
| 197 | this.fileName = other.fileName; |
| 198 | |
| 199 | /* Copy the fields over (immutable) */ |
| 200 | this.fContent = other.fContent; |
| 201 | |
| 202 | /* |
| 203 | * Copy the reference to the custom attributes (should be the same |
| 204 | * object for all events of this type) |
| 205 | */ |
| 206 | this.fDeclaration = other.fDeclaration; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Inner constructor to create "null" events. Don't use this directly in |
| 211 | * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an |
| 212 | * empty event. |
| 213 | * |
| 214 | * This needs to be public however because it's used in extension points, |
| 215 | * and the framework will use this constructor to get the class type. |
| 216 | */ |
| 217 | public CtfTmfEvent() { |
| 218 | this.fTrace = null; |
| 219 | this.fTimestamp = new CtfTmfTimestamp(-1); |
| 220 | this.sourceCPU = -1; |
| 221 | this.typeId = -1; |
| 222 | this.fileName = NO_STREAM; |
| 223 | this.eventName = EMPTY_CTF_EVENT_NAME; |
| 224 | this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
| 225 | this.fDeclaration = null; |
| 226 | } |
| 227 | |
| 228 | // ------------------------------------------------------------------------ |
| 229 | // Getters/Setters/Predicates |
| 230 | // ------------------------------------------------------------------------ |
| 231 | |
| 232 | private static CtfTmfEvent nullEvent = new CtfTmfEvent(); |
| 233 | |
| 234 | /** |
| 235 | * Get a null event |
| 236 | * |
| 237 | * @return An empty event. |
| 238 | */ |
| 239 | public static CtfTmfEvent getNullEvent() { |
| 240 | return nullEvent; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Gets the cpu core the event was recorded on. |
| 245 | * |
| 246 | * @return The cpu id for a given source. In lttng it's from CPUINFO |
| 247 | */ |
| 248 | public int getCPU() { |
| 249 | return this.sourceCPU; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Return this event's ID, according to the trace's metadata. |
| 254 | * |
| 255 | * Watch out, this ID is not constant from one trace to another for the same |
| 256 | * event types! Use "getEventName()" for a constant reference. |
| 257 | * |
| 258 | * @return The event ID |
| 259 | */ |
| 260 | public long getID() { |
| 261 | return this.typeId; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Gets the name of a current event. |
| 266 | * |
| 267 | * @return The event name |
| 268 | */ |
| 269 | public String getEventName() { |
| 270 | return eventName; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Gets the channel name of a field. |
| 275 | * |
| 276 | * @return The channel name. |
| 277 | */ |
| 278 | public String getChannelName() { |
| 279 | return this.fileName; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public CtfTmfTrace getTrace() { |
| 284 | return fTrace; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public long getRank() { |
| 289 | // TODO Auto-generated method stub |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public ITmfTimestamp getTimestamp() { |
| 295 | return fTimestamp; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public String getSource() { |
| 300 | // TODO Returns CPU for now |
| 301 | return Integer.toString(getCPU()); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public ITmfEventType getType() { |
| 306 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
| 307 | if( ctfTmfEventType == null ){ |
| 308 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); |
| 309 | } |
| 310 | return ctfTmfEventType; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public ITmfEventField getContent() { |
| 315 | return fContent; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public String getReference() { |
| 320 | return getChannelName(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * List the custom CTF attributes for events of this type. |
| 325 | * |
| 326 | * @return The list of custom attribute names. Should not be null, but could |
| 327 | * be empty. |
| 328 | * @since 2.0 |
| 329 | */ |
| 330 | public Set<String> listCustomAttributes() { |
| 331 | if (fDeclaration == null) { |
| 332 | return new HashSet<String>(); |
| 333 | } |
| 334 | return fDeclaration.getCustomAttributes(); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Get the value of a custom CTF attributes for this event's type. |
| 339 | * |
| 340 | * @param name |
| 341 | * Name of the the custom attribute |
| 342 | * @return Value of this attribute, or null if there is no attribute with |
| 343 | * that name |
| 344 | * @since 2.0 |
| 345 | */ |
| 346 | public String getCustomAttribute(String name) { |
| 347 | if (fDeclaration == null) { |
| 348 | return null; |
| 349 | } |
| 350 | return fDeclaration.getCustomAttribute(name); |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public CtfTmfEvent clone() { |
| 355 | return new CtfTmfEvent(this); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @since 2.0 |
| 360 | */ |
| 361 | @Override |
| 362 | public Object getAdapter(Class adapter) { |
| 363 | if (adapter == IPropertySource.class) { |
| 364 | return new TmfEventPropertySource(this); |
| 365 | } |
| 366 | return null; |
| 367 | } |
| 368 | } |