X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.tmf.core%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Ftmf%2Fcore%2Fctfadaptor%2FCtfTmfEvent.java;h=8cab950ec05c60a4eb65b5453e6dd9cdad63f538;hb=e693075df9ab61d18493abaf9db9726efc254c17;hp=b1d5ec0cf7c4051dd2388bae906b945e0541897e;hpb=1d7277f317fe1a75f274719fc9a87eaf33fada84;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java index b1d5ec0cf7..8cab950ec0 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java @@ -1,326 +1,214 @@ /******************************************************************************* - * Copyright (c) 2011 Ericsson + * Copyright (c) 2011, 2014 Ericsson * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Alexandre Montplaisir - Initial API and implementation + * Contributors: + * Alexandre Montplaisir - Initial API and implementation + * Bernd Hufmann - Updated for source and model lookup interfaces *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.ctfadaptor; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; +import java.util.HashSet; +import java.util.Set; -import org.eclipse.linuxtools.ctf.core.event.EventDefinition; -import org.eclipse.linuxtools.ctf.core.event.types.Definition; -import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; -import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; +import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; +import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; +import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes; import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; -import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; +import org.eclipse.linuxtools.tmf.core.event.TmfEvent; +import org.eclipse.linuxtools.tmf.core.event.TmfEventField; +import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup; +import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup; +import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; /** - * A wrapper class around CTF's Event Definition/Declaration that maps all - * types of Declaration to native Java types. + * A wrapper class around CTF's Event Definition/Declaration that maps all types + * of Declaration to native Java types. * * @version 1.0 * @author Alexandre Montplaisir + * @since 2.0 */ -public final class CtfTmfEvent implements ITmfEvent, Cloneable { +public class CtfTmfEvent extends TmfEvent + implements ITmfSourceLookup, ITmfModelLookup, ITmfCustomAttributes { // ------------------------------------------------------------------------ // Constants // ------------------------------------------------------------------------ - private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ + static final String NO_STREAM = "No stream"; //$NON-NLS-1$ private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ - // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final CtfTmfTrace fTrace; - private final long timestamp; - private final int sourceCPU; - private final long typeId; - private final String eventName; - private final String fileName; - - private final CtfTmfContent fContent; + private final int fSourceCPU; + private final long fTypeId; + private final String fEventName; + private final IEventDeclaration fDeclaration; // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ /** - * Usual CTFEvent constructor, where we read an event from the trace (via - * the StreamInputReader). - * - * @param eventDef - * CTF EventDefinition object corresponding to this trace event - * @param fileName - * The path to the trace file - * @param originTrace - * The trace from which this event originates - */ - public CtfTmfEvent(EventDefinition eventDef, String fileName, - CtfTmfTrace originTrace) { - this.fTrace = originTrace; - - if (eventDef == null) { - this.timestamp = -1; - this.sourceCPU = -1; - this.typeId = -1; - this.fileName = NO_STREAM; - this.eventName = EMPTY_CTF_EVENT_NAME; - this.fContent = null; - return; - } - - /* Read the base event info */ - this.timestamp = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); - this.sourceCPU = eventDef.getCPU(); - this.typeId = eventDef.getDeclaration().getId(); - this.eventName = eventDef.getDeclaration().getName(); - this.fileName = fileName; - - /* Read the fields */ - this.fContent = new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID, - parseFields(eventDef)); - } - - /** - * Extract the field information from the structDefinition haze-inducing - * mess, and put them into something ITmfEventField can cope with. - * - * @param eventDef - * CTF EventDefinition to read - * @return CtfTmfEventField[] The array of fields that were read - */ - public static CtfTmfEventField[] parseFields(EventDefinition eventDef) { - List fields = new ArrayList(); - - StructDefinition structFields = eventDef.getFields(); - HashMap definitions = structFields.getDefinitions(); - String curFieldName; - Definition curFieldDef; - CtfTmfEventField curField; - Iterator> it = definitions.entrySet().iterator(); - while(it.hasNext()) { - Entry entry = it.next(); - curFieldName = entry.getKey(); - curFieldDef = entry.getValue(); - curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); - fields.add(curField); - } - - return fields.toArray(new CtfTmfEventField[fields.size()]); - } - - /** - * Copy constructor - * - * @param other - * CtfTmfEvent to copy + * Constructor used by {@link CtfTmfEventFactory#createEvent} */ - public CtfTmfEvent(CtfTmfEvent other) { - this.fTrace = other.getTrace(); - /* Primitives, those will be copied by value */ - this.timestamp = other.timestamp; - this.sourceCPU = other.sourceCPU; - this.typeId = other.typeId; + CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp, + ITmfEventField content, String fileName, int cpu, + IEventDeclaration declaration) { + super(trace, + rank, + timestamp, + String.valueOf(cpu), // Source + null, // Event type. We don't use TmfEvent's field here, we re-implement getType() + content, + fileName // Reference + ); - /* Strings are immutable, it's safe to shallow-copy them */ - this.eventName = other.eventName; - this.fileName = other.fileName; + fDeclaration = declaration; + fSourceCPU = cpu; + fTypeId = declaration.getId(); + fEventName = declaration.getName(); - /* Copy the fields over */ - this.fContent = (CtfTmfContent) other.fContent.clone(); } /** * Inner constructor to create "null" events. Don't use this directly in - * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an - * empty event. + * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an + * instance of an empty event. * * This needs to be public however because it's used in extension points, * and the framework will use this constructor to get the class type. */ public CtfTmfEvent() { - this.fTrace = null; - this.timestamp = -1; - this.sourceCPU = -1; - this.typeId = -1; - this.fileName = NO_STREAM; - this.eventName = EMPTY_CTF_EVENT_NAME; - this.fContent = new CtfTmfContent("", new CtfTmfEventField[0]); //$NON-NLS-1$ + super(null, + ITmfContext.UNKNOWN_RANK, + new CtfTmfTimestamp(-1), + null, + null, + new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$ + NO_STREAM); + fSourceCPU = -1; + fTypeId = -1; + fEventName = EMPTY_CTF_EVENT_NAME; + fDeclaration = null; } // ------------------------------------------------------------------------ // Getters/Setters/Predicates // ------------------------------------------------------------------------ - private static CtfTmfEvent nullEvent = null; - - /** - * Get a null event - * - * @return an empty event. */ - public static CtfTmfEvent getNullEvent() { - if (nullEvent == null) { - nullEvent = new CtfTmfEvent(); - } - return nullEvent; - } - - /** - * Gets the current timestamp of the event - * - * @return the current timestamp (long) */ - public long getTimestampValue() { - return this.timestamp; - } - /** * Gets the cpu core the event was recorded on. * - * @return the cpu id for a given source. In lttng it's from CPUINFO */ + * @return The cpu id for a given source. In lttng it's from CPUINFO + */ public int getCPU() { - return this.sourceCPU; + return fSourceCPU; } /** - * Return this event's ID, according to the trace's metadata. Watch out, - * this ID is not constant from one trace to another for the same event - * types! Use "getEventName()" for a constant reference. + * Return this event's ID, according to the trace's metadata. * - - * @return the event ID */ - public long getID() { - return this.typeId; - } - - /** - * Gets the name of a current event. + * Watch out, this ID is not constant from one trace to another for the same + * event types! Use "getEventName()" for a constant reference. * - * @return the event name */ - public String getEventName() { - return eventName; - } - - /** - * Gets the channel name of a field. - * - * @return the channel name. */ - public String getChannelName() { - return this.fileName; + * @return The event ID + */ + public long getID() { + return fTypeId; } - /** - * Method getTrace. - * @return CtfTmfTrace - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace() - */ @Override public CtfTmfTrace getTrace() { - return fTrace; + /* + * Should be of the right type, since we take a CtfTmfTrace at the + * constructor + */ + return (CtfTmfTrace) super.getTrace(); } - /** - * Method getRank. - * @return long - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank() - */ @Override - public long getRank() { - // TODO Auto-generated method stub - return 0; - } - - private ITmfTimestamp fTimestamp = null; - - // TODO Benchmark if the singleton approach is faster than just - // instantiating a final fTimestramp right away at creation time - /** - * Method getTimestamp. - * @return ITmfTimestamp - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp() - */ - @Override - public ITmfTimestamp getTimestamp() { - if (fTimestamp == null) { - fTimestamp = new CtfTmfTimestamp(timestamp); + public ITmfEventType getType() { + CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(getTrace(), fEventName); + if (ctfTmfEventType == null) { + /* Should only return null the first time */ + ctfTmfEventType = new CtfTmfEventType(fEventName, getTrace(), getContent()); } - return fTimestamp; + return ctfTmfEventType; } - String fSource = null; /** - * Method getSource. - * @return String - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource() + * @since 2.0 */ @Override - public String getSource() { - // TODO Returns CPU for now - if(fSource == null) { - fSource= Integer.toString(getCPU()); + public Set listCustomAttributes() { + if (fDeclaration == null) { + return new HashSet<>(); } - return fSource; + return fDeclaration.getCustomAttributes(); } /** - * Method getType. - * @return ITmfEventType - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType() + * @since 2.0 */ @Override - public ITmfEventType getType() { - CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); - if( ctfTmfEventType == null ){ - ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); + public String getCustomAttribute(String name) { + if (fDeclaration == null) { + return null; } - return ctfTmfEventType; - } - - /** - * Method getContent. - * @return ITmfEventField - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent() - */ - @Override - public ITmfEventField getContent() { - return fContent; + return fDeclaration.getCustomAttribute(name); } - String fReference = null; /** - * Method getReference. - * @return String - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference() + * Get the call site for this event. + * + * @return the call site information, or null if there is none + * @since 2.0 */ @Override - public String getReference() { - if( fReference == null){ - fReference = getChannelName(); + public CtfTmfCallsite getCallsite() { + CTFCallsite callsite = null; + CtfTmfTrace trace = getTrace(); + if (trace == null) { + return null; + } + CTFTrace ctfTrace = trace.getCTFTrace(); + /* Should not happen, but it is a good check */ + if (ctfTrace == null) { + return null; } - return fReference; + if (getContent() != null) { + ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY); + if (ipField != null && ipField.getValue() instanceof Long) { + long ip = (Long) ipField.getValue(); + callsite = ctfTrace.getCallsite(fEventName, ip); + } + } + if (callsite == null) { + callsite = ctfTrace.getCallsite(fEventName); + } + if (callsite != null) { + return new CtfTmfCallsite(callsite); + } + return null; } /** - * Method clone. - * @return CtfTmfEvent - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone() + * @since 2.0 */ @Override - public CtfTmfEvent clone() { - return new CtfTmfEvent(this); + public String getModelUri() { + return getCustomAttribute(CtfConstants.MODEL_URI_KEY); } + }