tmf.ctf: Move each CtfIteratorManager into its own trace object
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / CtfTmfEventFactory.java
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
13 package org.eclipse.tracecompass.tmf.ctf.core;
14
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.TmfTimeRange;
22 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
23
24 /**
25 * Factory for CtfTmfEvent's.
26 *
27 * This code was moved out of CtfTmfEvent to provide better separation between
28 * the parsing/instantiation of events, and the usual TMF API implementations.
29 *
30 * @author Alexandre Montplaisir
31 * @since 2.0
32 */
33 public final class CtfTmfEventFactory {
34
35 private static final String NO_STREAM = "No stream"; //$NON-NLS-1$
36
37 /**
38 * Don't let anyone instantiate this class.
39 */
40 private CtfTmfEventFactory() {}
41
42 /**
43 * Factory method to instantiate new {@link CtfTmfEvent}'s.
44 *
45 * @param eventDef
46 * CTF EventDefinition object corresponding to this trace event
47 * @param fileName
48 * The path to the trace file
49 * @param originTrace
50 * The trace from which this event originates
51 * @return The newly-built CtfTmfEvent
52 */
53 public static CtfTmfEvent createEvent(EventDefinition eventDef,
54 String fileName, CtfTmfTrace originTrace) {
55
56 /* Prepare what to pass to CtfTmfEvent's constructor */
57 final IEventDeclaration eventDecl = eventDef.getDeclaration();
58 final long ts = eventDef.getTimestamp();
59 final CtfTmfTimestamp timestamp = originTrace.createTimestamp(
60 originTrace.getCTFTrace().timestampCyclesToNanos(ts));
61
62 int sourceCPU = eventDef.getCPU();
63
64 String reference = fileName == null ? NO_STREAM : fileName;
65
66 /* Handle the special case of lost events */
67 if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
68 IDefinition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD);
69 IDefinition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION);
70 if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) {
71 /*
72 * One or both of these fields doesn't exist, or is not of the
73 * right type. The event claims to be a "lost event", but is
74 * malformed. Log it and return a null event instead.
75 */
76 return getNullEvent(originTrace);
77 }
78 long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue();
79 long duration = ((IntegerDefinition) durationDef).getValue();
80 CtfTmfTimestamp timestampEnd = new CtfTmfTimestamp(
81 originTrace.getCTFTrace().timestampCyclesToNanos(ts) + duration);
82
83 CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace,
84 ITmfContext.UNKNOWN_RANK,
85 reference, // filename
86 sourceCPU,
87 eventDecl,
88 new TmfTimeRange(timestamp, timestampEnd),
89 nbLostEvents,
90 eventDef);
91 return lostEvent;
92 }
93
94 /* Handle standard event types */
95 CtfTmfEvent event = new CtfTmfEvent(
96 originTrace,
97 ITmfContext.UNKNOWN_RANK,
98 timestamp,
99 reference, // filename
100 sourceCPU,
101 eventDecl,
102 eventDef);
103 return event;
104 }
105
106 /* Singleton instance of a null event */
107 private static CtfTmfEvent nullEvent = null;
108
109 /**
110 * Get an instance of a null event.
111 *
112 * @param trace
113 * A trace to associate with this null event
114 * @return An empty event
115 */
116 public static CtfTmfEvent getNullEvent(@NonNull CtfTmfTrace trace) {
117 if (nullEvent == null) {
118 nullEvent = new CtfTmfEvent(trace);
119 }
120 return nullEvent;
121 }
122
123
124 }
This page took 0.03984 seconds and 5 git commands to generate.