Commit | Line | Data |
---|---|---|
6cfa0200 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
91e7f946 | 13 | package org.eclipse.linuxtools.tmf.ctf.core; |
6cfa0200 | 14 | |
c26d0fe0 | 15 | import org.eclipse.linuxtools.ctf.core.CTFStrings; |
6cfa0200 | 16 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
c26d0fe0 | 17 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
cc98c947 | 18 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinition; |
c26d0fe0 | 19 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
c26d0fe0 | 20 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
6cfa0200 AM |
21 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
22 | ||
23 | /** | |
24 | * Factory for CtfTmfEvent's. | |
25 | * | |
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. | |
28 | * | |
29 | * @author Alexandre Montplaisir | |
30 | * @since 2.0 | |
31 | */ | |
60fb38b8 | 32 | public final class CtfTmfEventFactory { |
6cfa0200 | 33 | |
a4fa4e36 MK |
34 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ |
35 | ||
60fb38b8 PT |
36 | /** |
37 | * Don't let anyone instantiate this class. | |
38 | */ | |
39 | private CtfTmfEventFactory() {} | |
6cfa0200 AM |
40 | |
41 | /** | |
42 | * Factory method to instantiate new {@link CtfTmfEvent}'s. | |
43 | * | |
44 | * @param eventDef | |
45 | * CTF EventDefinition object corresponding to this trace event | |
46 | * @param fileName | |
47 | * The path to the trace file | |
48 | * @param originTrace | |
49 | * The trace from which this event originates | |
50 | * @return The newly-built CtfTmfEvent | |
51 | */ | |
52 | public static CtfTmfEvent createEvent(EventDefinition eventDef, | |
53 | String fileName, CtfTmfTrace originTrace) { | |
54 | ||
55 | /* Prepare what to pass to CtfTmfEvent's constructor */ | |
c26d0fe0 AM |
56 | final IEventDeclaration eventDecl = eventDef.getDeclaration(); |
57 | final long ts = eventDef.getTimestamp(); | |
e3254d3a | 58 | final CtfTmfTimestamp timestamp = originTrace.createTimestamp( |
c26d0fe0 | 59 | originTrace.getCTFTrace().timestampCyclesToNanos(ts)); |
6cfa0200 AM |
60 | |
61 | int sourceCPU = eventDef.getCPU(); | |
62 | ||
a4fa4e36 | 63 | String reference = fileName == null ? NO_STREAM : fileName; |
6cfa0200 | 64 | |
c26d0fe0 AM |
65 | /* Handle the special case of lost events */ |
66 | if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) { | |
cc98c947 MK |
67 | IDefinition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD); |
68 | IDefinition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION); | |
c26d0fe0 AM |
69 | if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) { |
70 | /* | |
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. | |
74 | */ | |
75 | return getNullEvent(); | |
76 | } | |
77 | long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue(); | |
78 | long duration = ((IntegerDefinition) durationDef).getValue(); | |
79 | CtfTmfTimestamp timestampEnd = new CtfTmfTimestamp( | |
80 | originTrace.getCTFTrace().timestampCyclesToNanos(ts) + duration); | |
81 | ||
82 | CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace, | |
83 | ITmfContext.UNKNOWN_RANK, | |
c26d0fe0 AM |
84 | reference, // filename |
85 | sourceCPU, | |
86 | eventDecl, | |
87 | new TmfTimeRange(timestamp, timestampEnd), | |
a4fa4e36 MK |
88 | nbLostEvents, |
89 | eventDef); | |
c26d0fe0 AM |
90 | return lostEvent; |
91 | } | |
92 | ||
93 | /* Handle standard event types */ | |
6cfa0200 AM |
94 | CtfTmfEvent event = new CtfTmfEvent( |
95 | originTrace, | |
96 | ITmfContext.UNKNOWN_RANK, | |
97 | timestamp, | |
c26d0fe0 | 98 | reference, // filename |
6cfa0200 | 99 | sourceCPU, |
a4fa4e36 MK |
100 | eventDecl, |
101 | eventDef); | |
6cfa0200 AM |
102 | return event; |
103 | } | |
104 | ||
105 | /* Singleton instance of a null event */ | |
106 | private static CtfTmfEvent nullEvent = null; | |
107 | ||
108 | /** | |
109 | * Get an instance of a null event. | |
110 | * | |
111 | * @return An empty event | |
112 | */ | |
113 | public static CtfTmfEvent getNullEvent() { | |
114 | if (nullEvent == null) { | |
115 | nullEvent = new CtfTmfEvent(); | |
116 | } | |
117 | return nullEvent; | |
118 | } | |
119 | ||
437bb7c0 | 120 | |
6cfa0200 | 121 | } |