| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Geneviève Bastien - Initial implementation |
| 11 | * Patrick Tasse - Dispose wrapped trace |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml; |
| 15 | |
| 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | import java.net.URL; |
| 23 | import java.util.Collection; |
| 24 | import java.util.HashSet; |
| 25 | |
| 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; |
| 32 | |
| 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; |
| 68 | |
| 69 | import com.google.common.collect.ImmutableList; |
| 70 | |
| 71 | /** |
| 72 | * An XML development trace using a custom XML trace definition and schema. |
| 73 | * |
| 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 |
| 76 | * analyzes. |
| 77 | * |
| 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 |
| 82 | * a CTF trace. |
| 83 | * |
| 84 | * @author Geneviève Bastien |
| 85 | */ |
| 86 | public class TmfXmlTraceStub extends TmfTrace { |
| 87 | |
| 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$ |
| 91 | |
| 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"; |
| 101 | |
| 102 | private static final Long SECONDS_TO_NS = 1000000000L; |
| 103 | |
| 104 | private final CustomXmlTraceDefinition fDefinition; |
| 105 | private CustomXmlTrace fTrace; |
| 106 | |
| 107 | private Collection<ITmfEventAspect<?>> fAspects = TmfTrace.BASE_ASPECTS; |
| 108 | private final Collection<ITmfEventAspect<?>> fAdditionalAspects = new HashSet<>(); |
| 109 | |
| 110 | /** |
| 111 | * Validate and initialize a {@link TmfXmlTraceStub} object |
| 112 | * |
| 113 | * @param absolutePath |
| 114 | * The absolute file path of the trace file |
| 115 | * @return The trace |
| 116 | */ |
| 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()); |
| 122 | } |
| 123 | try { |
| 124 | trace.initTrace(null, absolutePath.toOSString(), TmfEvent.class); |
| 125 | } catch (TmfTraceException e) { |
| 126 | fail(e.getMessage()); |
| 127 | } |
| 128 | return trace; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Constructor. Constructs the custom XML trace with the appropriate |
| 133 | * definition. |
| 134 | */ |
| 135 | public TmfXmlTraceStub() { |
| 136 | |
| 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$ |
| 142 | } |
| 143 | /* The first definition parses the 'set_aspects' event */ |
| 144 | fTrace = new CustomXmlTrace(definitions[0]) { |
| 145 | @Override |
| 146 | protected ITmfTraceIndexer createIndexer(int interval) { |
| 147 | /* Use the in-memory checkpoint indexer */ |
| 148 | return new TmfCheckpointIndexer(this, interval); |
| 149 | } |
| 150 | }; |
| 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$ |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | @Override |
| 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); |
| 163 | ITmfContext ctx; |
| 164 | |
| 165 | /* Initialize and read the trace with the 'set_aspects' definition */ |
| 166 | TmfSignalManager.deregister(fTrace); |
| 167 | fTrace.initTrace(resource, path, type); |
| 168 | ctx = seekEvent(0L); |
| 169 | /* If a set_aspects event exists, getNext() will process it */ |
| 170 | getNext(ctx); |
| 171 | ctx.dispose(); |
| 172 | fTrace.dispose(); |
| 173 | |
| 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 */ |
| 179 | ctx = seekEvent(0L); |
| 180 | if (ctx == null) { |
| 181 | return; |
| 182 | } |
| 183 | ITmfEvent event = getNext(ctx); |
| 184 | if (event != null) { |
| 185 | final ITmfTimestamp curTime = event.getTimestamp(); |
| 186 | this.setStartTime(curTime); |
| 187 | this.setEndTime(curTime); |
| 188 | } |
| 189 | ctx.dispose(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public synchronized void dispose() { |
| 194 | super.dispose(); |
| 195 | fTrace.dispose(); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public @Nullable ITmfEvent parseEvent(@Nullable ITmfContext context) { |
| 200 | return fTrace.parseEvent(context); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public @Nullable ITmfLocation getCurrentLocation() { |
| 205 | return fTrace.getCurrentLocation(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public double getLocationRatio(@Nullable ITmfLocation location) { |
| 210 | return fTrace.getLocationRatio(location); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) { |
| 215 | return fTrace.seekEvent(location); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public @Nullable ITmfContext seekEvent(double ratio) { |
| 220 | return fTrace.seekEvent(ratio); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 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)); |
| 228 | } |
| 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); |
| 232 | |
| 233 | try { |
| 234 | URL url = TmfXmlTraceStub.class.getResource(DEVELOPMENT_TRACE_XSD); |
| 235 | Schema schema = schemaFactory.newSchema(url); |
| 236 | |
| 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); |
| 243 | } |
| 244 | return Status.OK_STATUS; |
| 245 | } |
| 246 | |
| 247 | private static String getStringValue(ITmfEventField content, String fieldName) { |
| 248 | ITmfEventField field = content.getField(fieldName); |
| 249 | if (field == null) { |
| 250 | return EMPTY; |
| 251 | } |
| 252 | Object val = field.getValue(); |
| 253 | if (!(val instanceof String)) { |
| 254 | return EMPTY; |
| 255 | } |
| 256 | return (String) val; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) { |
| 261 | if (context == null) { |
| 262 | return null; |
| 263 | } |
| 264 | final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank()); |
| 265 | CustomXmlEvent event = fTrace.getNext(context); |
| 266 | if (event == null) { |
| 267 | return null; |
| 268 | } |
| 269 | |
| 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) { |
| 276 | return null; |
| 277 | } |
| 278 | |
| 279 | String fieldString = getStringValue(content, FIELD_NAMES_FIELD); |
| 280 | String valueString = getStringValue(content, VALUES_FIELD); |
| 281 | String typeString = getStringValue(content, TYPES_FIELD); |
| 282 | |
| 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]; |
| 287 | |
| 288 | for (int i = 0; i < fields.length; i++) { |
| 289 | String value = EMPTY; |
| 290 | if (values.length > i) { |
| 291 | value = values[i]; |
| 292 | } |
| 293 | String type = null; |
| 294 | if (types.length > i) { |
| 295 | type = types[i]; |
| 296 | } |
| 297 | Object val = value; |
| 298 | if (type != null) { |
| 299 | switch (type) { |
| 300 | case TYPE_INTEGER: { |
| 301 | try { |
| 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$ |
| 305 | val = 0; |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | case TYPE_LONG: { |
| 310 | try { |
| 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$ |
| 314 | val = 0L; |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | fieldsArray[i] = new TmfEventField(checkNotNull(fields[i]), val, null); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Generate the aspects for this trace if it is the 'set_aspects' |
| 327 | * definition |
| 328 | */ |
| 329 | if (fTrace.getDefinition() != fDefinition) { |
| 330 | generateAspects(fieldsArray); |
| 331 | return null; |
| 332 | } |
| 333 | |
| 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); |
| 339 | /* |
| 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 |
| 344 | */ |
| 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); |
| 348 | return newEvent; |
| 349 | } |
| 350 | |
| 351 | private void generateAspects(ITmfEventField[] fieldsArray) { |
| 352 | ImmutableList.Builder<ITmfEventAspect<?>> builder = new ImmutableList.Builder<>(); |
| 353 | |
| 354 | /* Initialize the first default trace aspects */ |
| 355 | builder.add(TmfBaseAspects.getTimestampAspect()); |
| 356 | builder.add(TmfBaseAspects.getEventTypeAspect()); |
| 357 | |
| 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() { |
| 364 | @Override |
| 365 | public @Nullable Integer resolve(ITmfEvent event) { |
| 366 | Object result = aspect.resolve(event); |
| 367 | if (result instanceof Number) { |
| 368 | return ((Number) result).intValue(); |
| 369 | } |
| 370 | return null; |
| 371 | } |
| 372 | }); |
| 373 | } else { |
| 374 | builder.add(aspect); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /* Add the big content aspect */ |
| 379 | builder.add(TmfBaseAspects.getContentsAspect()); |
| 380 | /* Add the additional aspects */ |
| 381 | builder.addAll(fAdditionalAspects); |
| 382 | fAspects = builder.build(); |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public Iterable<ITmfEventAspect<?>> getEventAspects() { |
| 387 | return fAspects; |
| 388 | } |
| 389 | |
| 390 | /** |
| 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. |
| 397 | * |
| 398 | * @param aspect |
| 399 | * The aspect to have |
| 400 | */ |
| 401 | public void addEventAspect(ITmfEventAspect<?> aspect) { |
| 402 | fAdditionalAspects.add(aspect); |
| 403 | } |
| 404 | |
| 405 | } |