1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
;
15 import org
.eclipse
.jdt
.annotation
.NonNull
;
16 import org
.eclipse
.tracecompass
.ctf
.core
.CTFStrings
;
17 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
24 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
27 * Factory for CtfTmfEvent's.
29 * This code was moved out of CtfTmfEvent to provide better separation between
30 * the parsing/instantiation of events, and the usual TMF API implementations.
32 * @author Alexandre Montplaisir
34 public class CtfTmfEventFactory
{
36 private static final @NonNull CtfTmfEventFactory INSTANCE
= new CtfTmfEventFactory();
39 * The file name to use when none is specified.
45 protected static final @NonNull String NO_STREAM
= "No stream"; //$NON-NLS-1$
48 * Protected constructor, only for use by sub-classes. Users should call
49 * the {@link #instance()} method instead.
53 protected CtfTmfEventFactory() {}
56 * Get the singleton factory instance
58 * @return The instance
61 public static @NonNull CtfTmfEventFactory
instance() {
66 * Factory method to instantiate new CTF events.
69 * The trace to which the new event will belong
71 * CTF EventDefinition object corresponding to this trace event
73 * The path to the trace file
74 * @return The newly-built CtfTmfEvent
77 public CtfTmfEvent
createEvent(CtfTmfTrace trace
, EventDefinition eventDef
, String fileName
) {
79 /* Prepare what to pass to CtfTmfEvent's constructor */
80 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
81 final long ts
= eventDef
.getTimestamp();
82 final TmfNanoTimestamp timestamp
= trace
.createTimestamp(trace
.timestampCyclesToNanos(ts
));
84 int sourceCPU
= eventDef
.getCPU();
86 String reference
= fileName
== null ? NO_STREAM
: fileName
;
88 /* Handle the special case of lost events */
89 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
90 return createLostEvent(trace
, eventDef
, eventDecl
, ts
, timestamp
, sourceCPU
, reference
);
93 /* Handle standard event types */
94 return new CtfTmfEvent(trace
,
95 ITmfContext
.UNKNOWN_RANK
,
97 reference
, // filename
104 * Create a new CTF lost event.
107 * The trace to which the new event will belong
109 * The CTF event definition
111 * The CTF event declaration
113 * The event's timestamp
115 * The event's timestamp (FIXME again??)
120 * @return The new lost event
123 protected static CtfTmfEvent
createLostEvent(CtfTmfTrace trace
,
124 EventDefinition eventDef
,
125 final IEventDeclaration eventDecl
,
127 final TmfNanoTimestamp timestamp
,
131 IDefinition nbLostEventsDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_FIELD
);
132 IDefinition durationDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_DURATION
);
133 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
135 * One or both of these fields doesn't exist, or is not of the right
136 * type. The event claims to be a "lost event", but is malformed.
137 * Log it and return a null event instead.
139 return getNullEvent(trace
);
141 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
142 long duration
= ((IntegerDefinition
) durationDef
).getValue();
143 TmfNanoTimestamp timestampEnd
= new TmfNanoTimestamp(
144 trace
.timestampCyclesToNanos(ts
) + duration
);
146 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(trace
,
147 ITmfContext
.UNKNOWN_RANK
,
151 new TmfTimeRange(timestamp
, timestampEnd
),
158 * Get an instance of a null event.
161 * The trace to which the new null event will belong
162 * @return An empty event
165 public static CtfTmfEvent
getNullEvent(CtfTmfTrace trace
) {
166 return new CtfTmfEvent(trace
);