1 /*******************************************************************************
2 * Copyright (c) 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ctf
.core
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.CTFStrings
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
24 * Factory for CtfTmfEvent's.
26 * This code was moved out of CtfTmfEvent to provide better separation between
27 * the parsing/instantiation of events, and the usual TMF API implementations.
29 * @author Alexandre Montplaisir
32 public final class CtfTmfEventFactory
{
34 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
37 * Don't let anyone instantiate this class.
39 private CtfTmfEventFactory() {}
42 * Factory method to instantiate new {@link CtfTmfEvent}'s.
45 * CTF EventDefinition object corresponding to this trace event
47 * The path to the trace file
49 * The trace from which this event originates
50 * @return The newly-built CtfTmfEvent
52 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
53 String fileName
, CtfTmfTrace originTrace
) {
55 /* Prepare what to pass to CtfTmfEvent's constructor */
56 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
57 final long ts
= eventDef
.getTimestamp();
58 final CtfTmfTimestamp timestamp
= originTrace
.createTimestamp(
59 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
));
61 int sourceCPU
= eventDef
.getCPU();
63 String reference
= fileName
== null ? NO_STREAM
: fileName
;
65 /* Handle the special case of lost events */
66 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
67 IDefinition nbLostEventsDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_FIELD
);
68 IDefinition durationDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_DURATION
);
69 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
71 * One or both of these fields doesn't exist, or is not of the
72 * right type. The event claims to be a "lost event", but is
73 * malformed. Log it and return a null event instead.
75 return getNullEvent();
77 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
78 long duration
= ((IntegerDefinition
) durationDef
).getValue();
79 CtfTmfTimestamp timestampEnd
= new CtfTmfTimestamp(
80 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
) + duration
);
82 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(originTrace
,
83 ITmfContext
.UNKNOWN_RANK
,
84 reference
, // filename
87 new TmfTimeRange(timestamp
, timestampEnd
),
93 /* Handle standard event types */
94 CtfTmfEvent event
= new CtfTmfEvent(
96 ITmfContext
.UNKNOWN_RANK
,
98 reference
, // filename
105 /* Singleton instance of a null event */
106 private static CtfTmfEvent nullEvent
= null;
109 * Get an instance of a null event.
111 * @return An empty event
113 public static CtfTmfEvent
getNullEvent() {
114 if (nullEvent
== null) {
115 nullEvent
= new CtfTmfEvent();