LTTng: CPU usage analysis from the LTTng kernel trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventFactory.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15import java.util.ArrayList;
6cfa0200 16import java.util.List;
843f986b 17import java.util.Map;
6cfa0200 18
c26d0fe0 19import org.eclipse.linuxtools.ctf.core.CTFStrings;
6cfa0200 20import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
c26d0fe0 21import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
6cfa0200 22import org.eclipse.linuxtools.ctf.core.event.types.Definition;
c26d0fe0 23import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
6cfa0200
AM
24import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
25import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
26import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
c26d0fe0 27import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
6cfa0200
AM
28import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
29
30/**
31 * Factory for CtfTmfEvent's.
32 *
33 * This code was moved out of CtfTmfEvent to provide better separation between
34 * the parsing/instantiation of events, and the usual TMF API implementations.
35 *
36 * @author Alexandre Montplaisir
37 * @since 2.0
38 */
60fb38b8 39public final class CtfTmfEventFactory {
6cfa0200 40
60fb38b8
PT
41 /**
42 * Don't let anyone instantiate this class.
43 */
44 private CtfTmfEventFactory() {}
6cfa0200
AM
45
46 /**
47 * Factory method to instantiate new {@link CtfTmfEvent}'s.
48 *
49 * @param eventDef
50 * CTF EventDefinition object corresponding to this trace event
51 * @param fileName
52 * The path to the trace file
53 * @param originTrace
54 * The trace from which this event originates
55 * @return The newly-built CtfTmfEvent
56 */
57 public static CtfTmfEvent createEvent(EventDefinition eventDef,
58 String fileName, CtfTmfTrace originTrace) {
59
60 /* Prepare what to pass to CtfTmfEvent's constructor */
c26d0fe0
AM
61 final IEventDeclaration eventDecl = eventDef.getDeclaration();
62 final long ts = eventDef.getTimestamp();
e3254d3a 63 final CtfTmfTimestamp timestamp = originTrace.createTimestamp(
c26d0fe0 64 originTrace.getCTFTrace().timestampCyclesToNanos(ts));
6cfa0200
AM
65
66 int sourceCPU = eventDef.getCPU();
67
68 ITmfEventField content = new TmfEventField(
214cc822 69 ITmfEventField.ROOT_FIELD_ID, null, parseFields(eventDef));
6cfa0200
AM
70
71 String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
72
c26d0fe0
AM
73 /* Handle the special case of lost events */
74 if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
75 Definition nbLostEventsDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD);
76 Definition durationDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION);
77 if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) {
78 /*
79 * One or both of these fields doesn't exist, or is not of the
80 * right type. The event claims to be a "lost event", but is
81 * malformed. Log it and return a null event instead.
82 */
83 return getNullEvent();
84 }
85 long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue();
86 long duration = ((IntegerDefinition) durationDef).getValue();
87 CtfTmfTimestamp timestampEnd = new CtfTmfTimestamp(
88 originTrace.getCTFTrace().timestampCyclesToNanos(ts) + duration);
89
90 CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace,
91 ITmfContext.UNKNOWN_RANK,
92 content,
93 reference, // filename
94 sourceCPU,
95 eventDecl,
96 new TmfTimeRange(timestamp, timestampEnd),
97 nbLostEvents);
98 return lostEvent;
99 }
100
101 /* Handle standard event types */
6cfa0200
AM
102 CtfTmfEvent event = new CtfTmfEvent(
103 originTrace,
104 ITmfContext.UNKNOWN_RANK,
105 timestamp,
106 content,
c26d0fe0 107 reference, // filename
6cfa0200 108 sourceCPU,
c26d0fe0 109 eventDecl);
6cfa0200
AM
110 return event;
111 }
112
113 /* Singleton instance of a null event */
114 private static CtfTmfEvent nullEvent = null;
115
116 /**
117 * Get an instance of a null event.
118 *
119 * @return An empty event
120 */
121 public static CtfTmfEvent getNullEvent() {
122 if (nullEvent == null) {
123 nullEvent = new CtfTmfEvent();
124 }
125 return nullEvent;
126 }
127
128 /**
129 * Extract the field information from the structDefinition haze-inducing
130 * mess, and put them into something ITmfEventField can cope with.
131 */
60fb38b8 132 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
a4524c1b 133 List<CtfTmfEventField> fields = new ArrayList<>();
6cfa0200
AM
134
135 StructDefinition structFields = eventDef.getFields();
437bb7c0
AM
136 for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) {
137 String curFieldName = entry.getKey();
138 Definition curFieldDef = entry.getValue();
139 CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
6cfa0200
AM
140 fields.add(curField);
141 }
142
143 /* Add context information as CtfTmfEventField */
6cfa0200
AM
144 StructDefinition structContext = eventDef.getContext();
145 if (structContext != null) {
437bb7c0 146 for (Map.Entry<String, Definition> entry : structContext.getDefinitions().entrySet()) {
60fb38b8 147 /* Prefix field name */
437bb7c0
AM
148 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey();
149 Definition curContextDef = entry.getValue();
150 CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
6cfa0200
AM
151 fields.add(curContext);
152 }
153 }
437bb7c0 154
6cfa0200
AM
155 return fields.toArray(new CtfTmfEventField[fields.size()]);
156 }
157}
This page took 0.042735 seconds and 5 git commands to generate.