1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * Geneviève Bastien - Initial implementation
11 * Patrick Tasse - Dispose wrapped trace
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
22 import java
.util
.Collection
;
24 import javax
.xml
.XMLConstants
;
25 import javax
.xml
.transform
.Source
;
26 import javax
.xml
.transform
.stream
.StreamSource
;
27 import javax
.xml
.validation
.Schema
;
28 import javax
.xml
.validation
.SchemaFactory
;
29 import javax
.xml
.validation
.Validator
;
31 import org
.eclipse
.core
.resources
.IProject
;
32 import org
.eclipse
.core
.resources
.IResource
;
33 import org
.eclipse
.core
.runtime
.IStatus
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.jdt
.annotation
.Nullable
;
36 import org
.eclipse
.osgi
.util
.NLS
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfContentFieldAspect
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
61 import org
.xml
.sax
.SAXException
;
63 import com
.google
.common
.collect
.ImmutableList
;
66 * An XML development trace using a custom XML trace definition and schema.
68 * This class will typically be used to build custom traces to unit test more
69 * complex functionalities like analyzes or to develop and test data-driven
72 * This class wraps a custom XML trace and rewrites the returned events in the
73 * getNext() method so that event's fields are the ones defined in <field ... />
74 * elements instead of those defined in the custom XML parser. This way, each
75 * event can have a different set of fields. This class can, for example, mimic
78 * @author Geneviève Bastien
80 public class TmfXmlTraceStub
extends TmfTrace
{
82 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
83 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
84 private static final String EMPTY
= ""; //$NON-NLS-1$
86 /* XML elements and attributes names */
87 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
88 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
89 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
90 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
91 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
92 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
93 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
94 private static final String ASPECT_CPU
= "cpu";
96 private static final Long SECONDS_TO_NS
= 1000000000L;
98 private final CustomXmlTraceDefinition fDefinition
;
99 private CustomXmlTrace fTrace
;
101 private Collection
<ITmfEventAspect
> fAspects
= TmfTrace
.BASE_ASPECTS
;
104 * Constructor. Constructs the custom XML trace with the appropriate
107 public TmfXmlTraceStub() {
109 /* Load custom XML definition */
110 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
111 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
112 if (definitions
.length
< 2) {
113 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
115 /* The first definition parses the 'set_aspects' event */
116 fTrace
= new CustomXmlTrace(definitions
[0]) {
118 protected ITmfTraceIndexer
createIndexer(int interval
) {
119 /* Use the in-memory checkpoint indexer */
120 return new TmfCheckpointIndexer(this, interval
);
123 /* The second definition parses 'event' trace events */
124 fDefinition
= checkNotNull(definitions
[1]);
125 } catch (IOException e
) {
126 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
132 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
133 super.initTrace(resource
, path
, type
);
136 /* Initialize and read the trace with the 'set_aspects' definition */
137 TmfSignalManager
.deregister(fTrace
);
138 fTrace
.initTrace(resource
, path
, type
);
140 /* If a set_aspects event exists, getNext() will process it */
145 /* Initialize a new trace with the trace events definition */
146 fTrace
= new CustomXmlTrace(fDefinition
);
147 TmfSignalManager
.deregister(fTrace
);
148 fTrace
.initTrace(resource
, path
, type
);
149 /* Set the start and (current) end times for this trace */
154 ITmfEvent event
= getNext(ctx
);
156 final ITmfTimestamp curTime
= event
.getTimestamp();
157 this.setStartTime(curTime
);
158 this.setEndTime(curTime
);
164 public synchronized void dispose() {
170 public @Nullable ITmfEvent
parseEvent(@Nullable ITmfContext context
) {
171 return fTrace
.parseEvent(context
);
175 public @Nullable ITmfLocation
getCurrentLocation() {
176 return fTrace
.getCurrentLocation();
180 public double getLocationRatio(@Nullable ITmfLocation location
) {
181 return fTrace
.getLocationRatio(location
);
185 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
186 return fTrace
.seekEvent(location
);
190 public @Nullable ITmfContext
seekEvent(double ratio
) {
191 return fTrace
.seekEvent(ratio
);
195 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
196 File xmlFile
= new File(path
);
197 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
198 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
200 /* Does the XML file validate with the XSD */
201 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
202 Source xmlSource
= new StreamSource(xmlFile
);
205 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
206 Schema schema
= schemaFactory
.newSchema(url
);
208 Validator validator
= schema
.newValidator();
209 validator
.validate(xmlSource
);
210 } catch (SAXException e
) {
211 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
212 } catch (IOException e
) {
213 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
215 return Status
.OK_STATUS
;
218 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
219 ITmfEventField field
= content
.getField(fieldName
);
223 Object val
= field
.getValue();
224 if (!(val
instanceof String
)) {
231 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
232 if (context
== null) {
235 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
236 CustomXmlEvent event
= fTrace
.getNext(context
);
241 /* Translate the content of the event */
242 /* The "fields" field contains a | separated list of field names */
243 /* The "values" field contains a | separated list of field values */
244 /* the "type" field contains a | separated list of field types */
245 ITmfEventField content
= event
.getContent();
246 if (content
== null) {
250 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
251 String valueString
= getStringValue(content
, VALUES_FIELD
);
252 String typeString
= getStringValue(content
, TYPES_FIELD
);
254 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
255 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
256 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
257 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
259 for (int i
= 0; i
< fields
.length
; i
++) {
260 String value
= EMPTY
;
261 if (values
.length
> i
) {
265 if (types
.length
> i
) {
273 val
= Integer
.valueOf(value
);
274 } catch (NumberFormatException e
) {
275 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
282 val
= Long
.valueOf(value
);
283 } catch (NumberFormatException e
) {
284 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
293 fieldsArray
[i
] = new TmfEventField(checkNotNull(fields
[i
]), val
, null);
296 /* Generate the aspects for this trace if it is the 'set_aspects' definition */
297 if (fTrace
.getDefinition() != fDefinition
) {
298 generateAspects(fieldsArray
);
302 /* Create a new event with new fields and name */
303 ITmfEventType customEventType
= event
.getType();
304 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
305 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
306 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
308 * TODO: Timestamps for these traces are in nanos, but since the
309 * CustomXmlTrace does not support this format, the timestamp of the
310 * original is in second and we need to convert it. We should do that at
311 * the source when it is supported
313 ITmfTimestamp timestamp
= new TmfNanoTimestamp(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
314 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
315 updateAttributes(savedContext
, event
);
319 private void generateAspects(ITmfEventField
[] fieldsArray
) {
320 ImmutableList
.Builder
<ITmfEventAspect
> builder
= new ImmutableList
.Builder
<>();
322 /* Initialize the first default trace aspects */
323 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
324 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
326 /* Add custom aspects in between */
327 for (ITmfEventField field
: fieldsArray
) {
328 String name
= field
.getName();
329 final ITmfEventAspect aspect
= new TmfContentFieldAspect(name
, name
);
330 if (name
.equals(ASPECT_CPU
)) {
331 builder
.add(new TmfCpuAspect() {
333 public @Nullable Integer
resolve(ITmfEvent event
) {
334 Object result
= aspect
.resolve(event
);
335 if (result
instanceof Number
) {
336 return ((Number
) result
).intValue();
346 /* Add the big content aspect */
347 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
348 fAspects
= builder
.build();
352 public Iterable
<ITmfEventAspect
> getEventAspects() {