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 final class CtfTmfEventFactory
{
36 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
39 * Don't let anyone instantiate this class.
41 private CtfTmfEventFactory() {}
44 * Factory method to instantiate new {@link CtfTmfEvent}'s.
47 * CTF EventDefinition object corresponding to this trace event
49 * The path to the trace file
51 * The trace from which this event originates
52 * @return The newly-built CtfTmfEvent
54 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
55 String fileName
, CtfTmfTrace originTrace
) {
57 /* Prepare what to pass to CtfTmfEvent's constructor */
58 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
59 final long ts
= eventDef
.getTimestamp();
60 final TmfNanoTimestamp timestamp
= originTrace
.createTimestamp(
61 originTrace
.timestampCyclesToNanos(ts
));
63 int sourceCPU
= eventDef
.getCPU();
65 String reference
= fileName
== null ? NO_STREAM
: fileName
;
67 /* Handle the special case of lost events */
68 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
69 IDefinition nbLostEventsDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_FIELD
);
70 IDefinition durationDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_DURATION
);
71 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
73 * One or both of these fields doesn't exist, or is not of the
74 * right type. The event claims to be a "lost event", but is
75 * malformed. Log it and return a null event instead.
77 return getNullEvent(originTrace
);
79 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
80 long duration
= ((IntegerDefinition
) durationDef
).getValue();
81 TmfNanoTimestamp timestampEnd
= new TmfNanoTimestamp(
82 originTrace
.timestampCyclesToNanos(ts
) + duration
);
84 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(originTrace
,
85 ITmfContext
.UNKNOWN_RANK
,
86 reference
, // filename
89 new TmfTimeRange(timestamp
, timestampEnd
),
95 /* Handle standard event types */
96 CtfTmfEvent event
= new CtfTmfEvent(
98 ITmfContext
.UNKNOWN_RANK
,
100 reference
, // filename
107 /* Singleton instance of a null event */
108 private static CtfTmfEvent nullEvent
= null;
111 * Get an instance of a null event.
114 * A trace to associate with this null event
115 * @return An empty event
117 public static CtfTmfEvent
getNullEvent(@NonNull CtfTmfTrace trace
) {
118 if (nullEvent
== null) {
119 nullEvent
= new CtfTmfEvent(trace
);