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
;
17 import static org
.junit
.Assert
.fail
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
23 import java
.util
.Collection
;
24 import java
.util
.HashSet
;
26 import javax
.xml
.XMLConstants
;
27 import javax
.xml
.transform
.Source
;
28 import javax
.xml
.transform
.stream
.StreamSource
;
29 import javax
.xml
.validation
.Schema
;
30 import javax
.xml
.validation
.SchemaFactory
;
31 import javax
.xml
.validation
.Validator
;
33 import org
.eclipse
.core
.resources
.IProject
;
34 import org
.eclipse
.core
.resources
.IResource
;
35 import org
.eclipse
.core
.runtime
.IPath
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Status
;
38 import org
.eclipse
.jdt
.annotation
.DefaultLocation
;
39 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
40 import org
.eclipse
.jdt
.annotation
.Nullable
;
41 import org
.eclipse
.osgi
.util
.NLS
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfBaseAspects
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfContentFieldAspect
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
64 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
65 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
66 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
67 import org
.xml
.sax
.SAXException
;
69 import com
.google
.common
.collect
.ImmutableList
;
72 * An XML development trace using a custom XML trace definition and schema.
74 * This class will typically be used to build custom traces to unit test more
75 * complex functionalities like analyzes or to develop and test data-driven
78 * This class wraps a custom XML trace and rewrites the returned events in the
79 * getNext() method so that event's fields are the ones defined in <field ... />
80 * elements instead of those defined in the custom XML parser. This way, each
81 * event can have a different set of fields. This class can, for example, mimic
84 * @author Geneviève Bastien
86 public class TmfXmlTraceStub
extends TmfTrace
{
88 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
89 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
90 private static final String EMPTY
= ""; //$NON-NLS-1$
92 /* XML elements and attributes names */
93 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
94 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
95 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
96 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
97 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
98 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
99 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
100 private static final String ASPECT_CPU
= "cpu";
102 private static final Long SECONDS_TO_NS
= 1000000000L;
104 private final CustomXmlTraceDefinition fDefinition
;
105 private CustomXmlTrace fTrace
;
107 private Collection
<ITmfEventAspect
<?
>> fAspects
= TmfTrace
.BASE_ASPECTS
;
108 private final Collection
<ITmfEventAspect
<?
>> fAdditionalAspects
= new HashSet
<>();
111 * Validate and initialize a {@link TmfXmlTraceStub} object
113 * @param absolutePath
114 * The absolute file path of the trace file
117 public static TmfXmlTraceStub
setupTrace(IPath absolutePath
) {
118 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
119 IStatus status
= trace
.validate(null, absolutePath
.toOSString());
120 if (!status
.isOK()) {
121 fail(status
.getException().getMessage());
124 trace
.initTrace(null, absolutePath
.toOSString(), TmfEvent
.class);
125 } catch (TmfTraceException e
) {
126 fail(e
.getMessage());
132 * Constructor. Constructs the custom XML trace with the appropriate
135 public TmfXmlTraceStub() {
137 /* Load custom XML definition */
138 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
139 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
140 if (definitions
.length
< 2) {
141 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
143 /* The first definition parses the 'set_aspects' event */
144 fTrace
= new CustomXmlTrace(definitions
[0]) {
146 protected ITmfTraceIndexer
createIndexer(int interval
) {
147 /* Use the in-memory checkpoint indexer */
148 return new TmfCheckpointIndexer(this, interval
);
151 /* The second definition parses 'event' trace events */
152 fDefinition
= checkNotNull(definitions
[1]);
153 } catch (IOException e
) {
154 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
160 @NonNullByDefault({DefaultLocation
.TYPE_ARGUMENT
})
161 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
162 super.initTrace(resource
, path
, type
);
165 /* Initialize and read the trace with the 'set_aspects' definition */
166 TmfSignalManager
.deregister(fTrace
);
167 fTrace
.initTrace(resource
, path
, type
);
169 /* If a set_aspects event exists, getNext() will process it */
174 /* Initialize a new trace with the trace events definition */
175 fTrace
= new CustomXmlTrace(fDefinition
);
176 TmfSignalManager
.deregister(fTrace
);
177 fTrace
.initTrace(resource
, path
, type
);
178 /* Set the start and (current) end times for this trace */
183 ITmfEvent event
= getNext(ctx
);
185 final ITmfTimestamp curTime
= event
.getTimestamp();
186 this.setStartTime(curTime
);
187 this.setEndTime(curTime
);
193 public synchronized void dispose() {
199 public @Nullable ITmfEvent
parseEvent(@Nullable ITmfContext context
) {
200 return fTrace
.parseEvent(context
);
204 public @Nullable ITmfLocation
getCurrentLocation() {
205 return fTrace
.getCurrentLocation();
209 public double getLocationRatio(@Nullable ITmfLocation location
) {
210 return fTrace
.getLocationRatio(location
);
214 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
215 return fTrace
.seekEvent(location
);
219 public @Nullable ITmfContext
seekEvent(double ratio
) {
220 return fTrace
.seekEvent(ratio
);
224 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
225 File xmlFile
= new File(path
);
226 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
227 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
229 /* Does the XML file validate with the XSD */
230 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
231 Source xmlSource
= new StreamSource(xmlFile
);
234 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
235 Schema schema
= schemaFactory
.newSchema(url
);
237 Validator validator
= schema
.newValidator();
238 validator
.validate(xmlSource
);
239 } catch (SAXException e
) {
240 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
241 } catch (IOException e
) {
242 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
244 return Status
.OK_STATUS
;
247 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
248 ITmfEventField field
= content
.getField(fieldName
);
252 Object val
= field
.getValue();
253 if (!(val
instanceof String
)) {
260 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
261 if (context
== null) {
264 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
265 CustomXmlEvent event
= fTrace
.getNext(context
);
270 /* Translate the content of the event */
271 /* The "fields" field contains a | separated list of field names */
272 /* The "values" field contains a | separated list of field values */
273 /* the "type" field contains a | separated list of field types */
274 ITmfEventField content
= event
.getContent();
275 if (content
== null) {
279 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
280 String valueString
= getStringValue(content
, VALUES_FIELD
);
281 String typeString
= getStringValue(content
, TYPES_FIELD
);
283 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
284 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
285 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
286 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
288 for (int i
= 0; i
< fields
.length
; i
++) {
289 String value
= EMPTY
;
290 if (values
.length
> i
) {
294 if (types
.length
> i
) {
302 val
= Integer
.valueOf(value
);
303 } catch (NumberFormatException e
) {
304 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
311 val
= Long
.valueOf(value
);
312 } catch (NumberFormatException e
) {
313 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
322 fieldsArray
[i
] = new TmfEventField(checkNotNull(fields
[i
]), val
, null);
326 * Generate the aspects for this trace if it is the 'set_aspects'
329 if (fTrace
.getDefinition() != fDefinition
) {
330 generateAspects(fieldsArray
);
334 /* Create a new event with new fields and name */
335 ITmfEventType customEventType
= event
.getType();
336 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
337 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
338 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
340 * TODO: Timestamps for these traces are in nanos, but since the
341 * CustomXmlTrace does not support this format, the timestamp of the
342 * original is in second and we need to convert it. We should do that at
343 * the source when it is supported
345 ITmfTimestamp timestamp
= TmfTimestamp
.fromNanos(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
346 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
347 updateAttributes(savedContext
, event
);
351 private void generateAspects(ITmfEventField
[] fieldsArray
) {
352 ImmutableList
.Builder
<ITmfEventAspect
<?
>> builder
= new ImmutableList
.Builder
<>();
354 /* Initialize the first default trace aspects */
355 builder
.add(TmfBaseAspects
.getTimestampAspect());
356 builder
.add(TmfBaseAspects
.getEventTypeAspect());
358 /* Add custom aspects in between */
359 for (ITmfEventField field
: fieldsArray
) {
360 String name
= field
.getName();
361 final ITmfEventAspect
<?
> aspect
= new TmfContentFieldAspect(name
, name
);
362 if (name
.equals(ASPECT_CPU
)) {
363 builder
.add(new TmfCpuAspect() {
365 public @Nullable Integer
resolve(ITmfEvent event
) {
366 Object result
= aspect
.resolve(event
);
367 if (result
instanceof Number
) {
368 return ((Number
) result
).intValue();
378 /* Add the big content aspect */
379 builder
.add(TmfBaseAspects
.getContentsAspect());
380 /* Add the additional aspects */
381 builder
.addAll(fAdditionalAspects
);
382 fAspects
= builder
.build();
386 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
391 * Adds a new event aspect to this type of trace. Since this trace type is
392 * used to build custom traces that mimic the behavior of real traces, the
393 * required aspects may be missing and this method allows to add them. This
394 * method should be called before calling
395 * {@link #initTrace(IResource, String, Class)} otherwise the additional
396 * aspects will not be picked up when generating the aspects.
401 public void addEventAspect(ITmfEventAspect
<?
> aspect
) {
402 fAdditionalAspects
.add(aspect
);