1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
16 import java
.util
.HashSet
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfCustomAttributes
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfSourceLookup
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
32 * A wrapper class around CTF's Event Definition/Declaration that maps all types
33 * of Declaration to native Java types.
36 * @author Alexandre Montplaisir
39 public class CtfTmfEvent
extends TmfEvent
40 implements ITmfSourceLookup
, ITmfModelLookup
, ITmfCustomAttributes
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
46 static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
47 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private final int sourceCPU
;
54 private final long typeId
;
55 private final String eventName
;
56 private final IEventDeclaration fDeclaration
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Constructor used by {@link CtfTmfEventFactory#createEvent}
65 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
66 ITmfEventField content
, String fileName
, int cpu
,
67 IEventDeclaration declaration
) {
71 String
.valueOf(cpu
), // Source
72 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
77 fDeclaration
= declaration
;
79 typeId
= declaration
.getId();
80 eventName
= declaration
.getName();
85 * Inner constructor to create "null" events. Don't use this directly in
86 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
87 * instance of an empty event.
89 * This needs to be public however because it's used in extension points,
90 * and the framework will use this constructor to get the class type.
92 public CtfTmfEvent() {
94 ITmfContext
.UNKNOWN_RANK
,
95 new CtfTmfTimestamp(-1),
98 new TmfEventField("", null, new CtfTmfEventField
[0]), //$NON-NLS-1$
102 this.eventName
= EMPTY_CTF_EVENT_NAME
;
103 this.fDeclaration
= null;
106 // ------------------------------------------------------------------------
107 // Getters/Setters/Predicates
108 // ------------------------------------------------------------------------
111 * Gets the cpu core the event was recorded on.
113 * @return The cpu id for a given source. In lttng it's from CPUINFO
115 public int getCPU() {
116 return this.sourceCPU
;
120 * Return this event's ID, according to the trace's metadata.
122 * Watch out, this ID is not constant from one trace to another for the same
123 * event types! Use "getEventName()" for a constant reference.
125 * @return The event ID
127 public long getID() {
132 * Gets the name of a current event.
134 * @return The event name
136 public String
getEventName() {
141 public CtfTmfTrace
getTrace() {
143 * Should be of the right type, since we take a CtfTmfTrace at the
146 return (CtfTmfTrace
) super.getTrace();
150 public ITmfEventType
getType() {
151 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
152 if (ctfTmfEventType
== null) {
153 /* Should only return null the first time */
154 ctfTmfEventType
= new CtfTmfEventType(this.getEventName(), this.getContent());
156 return ctfTmfEventType
;
163 public Set
<String
> listCustomAttributes() {
164 if (fDeclaration
== null) {
165 return new HashSet
<>();
167 return fDeclaration
.getCustomAttributes();
174 public String
getCustomAttribute(String name
) {
175 if (fDeclaration
== null) {
178 return fDeclaration
.getCustomAttribute(name
);
182 * Get the call site for this event.
184 * @return the call site information, or null if there is none
188 public CtfTmfCallsite
getCallsite() {
189 CTFCallsite callsite
= null;
190 CtfTmfTrace trace
= getTrace();
194 CTFTrace ctfTrace
= trace
.getCTFTrace();
195 /* Should not happen, but it is a good check */
196 if (ctfTrace
== null) {
199 if (getContent() != null) {
200 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
201 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
202 long ip
= (Long
) ipField
.getValue();
203 callsite
= ctfTrace
.getCallsite(eventName
, ip
);
206 if (callsite
== null) {
207 callsite
= ctfTrace
.getCallsite(eventName
);
209 if (callsite
!= null) {
210 return new CtfTmfCallsite(callsite
);
219 public String
getModelUri() {
220 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);