1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Add trace type id handling
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.IOException
;
21 import java
.nio
.ByteBuffer
;
22 import java
.util
.HashMap
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
25 import java
.util
.Map
.Entry
;
26 import java
.util
.regex
.Matcher
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Status
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.parsers
.custom
.CustomEventAspects
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
58 * Base class for custom plain text traces.
60 * @author Patrick Tassé
62 public class CustomTxtTrace
extends TmfTrace
implements ITmfPersistentlyIndexable
{
64 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation(-1L);
65 private static final int DEFAULT_CACHE_SIZE
= 100;
66 private static final int MAX_LINES
= 100;
67 private static final int MAX_CONFIDENCE
= 100;
69 private final CustomTxtTraceDefinition fDefinition
;
70 private final ITmfEventField fRootField
;
71 private BufferedRandomAccessFile fFile
;
72 private final @NonNull String fTraceTypeId
;
74 private static final char SEPARATOR
= ':';
75 private static final String CUSTOM_TXT_TRACE_TYPE_PREFIX
= "custom.txt.trace" + SEPARATOR
; //$NON-NLS-1$
76 private static final String LINUX_TOOLS_CUSTOM_TXT_TRACE_TYPE_PREFIX
= "org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace" + SEPARATOR
; //$NON-NLS-1$
77 private static final String EARLY_TRACE_COMPASS_CUSTOM_TXT_TRACE_TYPE_PREFIX
= "org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace" + SEPARATOR
; //$NON-NLS-1$
83 * Text trace definition
85 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
86 fDefinition
= definition
;
87 fRootField
= CustomEventType
.getRootField(definition
);
88 fTraceTypeId
= buildTraceTypeId(definition
.categoryName
, definition
.definitionName
);
89 setCacheSize(DEFAULT_CACHE_SIZE
);
98 * Text trace definition
100 * Path to the trace file
103 * @throws TmfTraceException
104 * If we couldn't open the trace at 'path'
106 public CustomTxtTrace(final IResource resource
,
107 final CustomTxtTraceDefinition definition
, final String path
,
108 final int cacheSize
) throws TmfTraceException
{
110 setCacheSize((cacheSize
> 0) ? cacheSize
: DEFAULT_CACHE_SIZE
);
111 initTrace(resource
, path
, CustomTxtEvent
.class);
115 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
116 super.initTrace(resource
, path
, eventType
);
120 private void initFile() throws TmfTraceException
{
123 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
124 } catch (IOException e
) {
125 throw new TmfTraceException(e
.getMessage(), e
);
130 public synchronized void dispose() {
135 private void closeFile() {
139 } catch (IOException e
) {
147 public ITmfTraceIndexer
getIndexer() {
148 return super.getIndexer();
152 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
153 return CustomEventAspects
.generateAspects(fDefinition
);
157 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
158 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
159 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
163 if (location
== null) {
165 } else if (location
.getLocationInfo() instanceof Long
) {
166 fFile
.seek((Long
) location
.getLocationInfo());
168 long rawPos
= fFile
.getFilePointer();
169 String line
= fFile
.getNextLine();
170 while (line
!= null) {
171 for (final InputLine input
: getFirstLines()) {
172 final Matcher matcher
= input
.getPattern().matcher(line
);
173 if (matcher
.matches()) {
174 context
.setLocation(new TmfLongLocation(rawPos
));
175 context
.firstLineMatcher
= matcher
;
176 context
.firstLine
= line
;
177 context
.nextLineLocation
= fFile
.getFilePointer();
178 context
.inputLine
= input
;
182 rawPos
= fFile
.getFilePointer();
183 line
= fFile
.getNextLine();
186 } catch (final FileNotFoundException e
) {
187 Activator
.logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
189 } catch (final IOException e
) {
190 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
197 public synchronized TmfContext
seekEvent(final double ratio
) {
199 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
202 long pos
= Math
.round(ratio
* fFile
.length());
205 if (fFile
.read() == '\n') {
210 final ITmfLocation location
= new TmfLongLocation(pos
);
211 final TmfContext context
= seekEvent(location
);
212 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
214 } catch (final IOException e
) {
215 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
216 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
221 public synchronized double getLocationRatio(final ITmfLocation location
) {
226 if (location
.getLocationInfo() instanceof Long
) {
227 return ((Long
) location
.getLocationInfo()).doubleValue() / fFile
.length();
229 } catch (final IOException e
) {
230 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
236 public ITmfLocation
getCurrentLocation() {
237 // TODO Auto-generated method stub
242 public synchronized CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
243 ITmfContext context
= seekEvent(tmfContext
.getLocation());
244 return parse(context
);
248 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
249 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
250 final CustomTxtEvent event
= parse(context
);
252 updateAttributes(savedContext
, event
);
253 context
.increaseRank();
258 private synchronized CustomTxtEvent
parse(final ITmfContext tmfContext
) {
262 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
266 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
267 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
271 CustomTxtEvent event
= parseFirstLine(context
);
273 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<>();
274 InputLine currentInput
= null;
275 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
276 currentInput
= context
.inputLine
.childrenInputs
.get(0);
277 countMap
.put(currentInput
, 0);
281 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
282 fFile
.seek(context
.nextLineLocation
);
284 long rawPos
= fFile
.getFilePointer();
285 String line
= fFile
.getNextLine();
286 while (line
!= null) {
287 boolean processed
= false;
288 if (currentInput
== null) {
289 for (final InputLine input
: getFirstLines()) {
290 final Matcher matcher
= input
.getPattern().matcher(line
);
291 if (matcher
.matches()) {
292 context
.setLocation(new TmfLongLocation(rawPos
));
293 context
.firstLineMatcher
= matcher
;
294 context
.firstLine
= line
;
295 context
.nextLineLocation
= fFile
.getFilePointer();
296 context
.inputLine
= input
;
301 if (checkNotNull(countMap
.get(currentInput
)) >= currentInput
.getMinCount()) {
302 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
303 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
304 for (final InputLine input
: getFirstLines()) {
305 final Matcher matcher
= input
.getPattern().matcher(line
);
306 if (matcher
.matches()) {
307 context
.setLocation(new TmfLongLocation(rawPos
));
308 context
.firstLineMatcher
= matcher
;
309 context
.firstLine
= line
;
310 context
.nextLineLocation
= fFile
.getFilePointer();
311 context
.inputLine
= input
;
316 for (final InputLine input
: nextInputs
) {
317 final Matcher matcher
= input
.getPattern().matcher(line
);
318 if (matcher
.matches()) {
319 event
.processGroups(input
, matcher
);
320 currentInput
= input
;
321 if (countMap
.get(currentInput
) == null) {
322 countMap
.put(currentInput
, 1);
324 countMap
.put(currentInput
, checkNotNull(countMap
.get(currentInput
)) + 1);
326 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
327 while (iter
.hasNext()) {
328 final InputLine inputLine
= iter
.next();
329 if (inputLine
.level
> currentInput
.level
) {
333 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
334 currentInput
= currentInput
.childrenInputs
.get(0);
335 countMap
.put(currentInput
, 0);
336 } else if (checkNotNull(countMap
.get(currentInput
)) >= currentInput
.getMaxCount()) {
337 if (currentInput
.getNextInputs(countMap
).size() > 0) {
338 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
339 if (countMap
.get(currentInput
) == null) {
340 countMap
.put(currentInput
, 0);
342 iter
= countMap
.keySet().iterator();
343 while (iter
.hasNext()) {
344 final InputLine inputLine
= iter
.next();
345 if (inputLine
.level
> currentInput
.level
) {
358 if (!processed
&& currentInput
!= null) {
359 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
360 if (matcher
.matches()) {
361 event
.processGroups(currentInput
, matcher
);
362 countMap
.put(currentInput
, checkNotNull(countMap
.get(currentInput
)) + 1);
363 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
364 currentInput
= currentInput
.childrenInputs
.get(0);
365 countMap
.put(currentInput
, 0);
366 } else if (checkNotNull(countMap
.get(currentInput
)) >= currentInput
.getMaxCount()) {
367 if (currentInput
.getNextInputs(countMap
).size() > 0) {
368 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
369 if (countMap
.get(currentInput
) == null) {
370 countMap
.put(currentInput
, 0);
372 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
373 while (iter
.hasNext()) {
374 final InputLine inputLine
= iter
.next();
375 if (inputLine
.level
> currentInput
.level
) {
384 ((StringBuffer
) event
.getContentValue()).append("\n").append(line
); //$NON-NLS-1$
387 rawPos
= fFile
.getFilePointer();
388 line
= fFile
.getNextLine();
390 } catch (final IOException e
) {
391 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
393 for (final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
394 if (entry
.getValue() < entry
.getKey().getMinCount()) {
398 context
.setLocation(NULL_LOCATION
);
403 * @return The first few lines of the text file
405 public List
<InputLine
> getFirstLines() {
406 return fDefinition
.inputs
;
410 * Parse the first line of the trace (to recognize the type).
414 * @return The first event
416 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
417 CustomTxtEventType eventType
= new CustomTxtEventType(checkNotNull(fDefinition
.definitionName
), fRootField
);
418 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, eventType
);
419 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
420 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
425 * Get the trace definition.
427 * @return The trace definition
429 public CustomTraceDefinition
getDefinition() {
436 * The default implementation computes the confidence as the percentage of
437 * lines in the first 100 lines of the file which match any of the root
438 * input line patterns.
441 public IStatus
validate(IProject project
, String path
) {
442 File file
= new File(path
);
443 if (!file
.exists() || !file
.isFile() || !file
.canRead()) {
444 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CustomTrace_FileNotFound
+ ": " + path
); //$NON-NLS-1$
448 if (!TmfTraceUtils
.isText(file
)) {
449 return new TraceValidationStatus(confidence
, Activator
.PLUGIN_ID
);
451 } catch (IOException e
) {
452 Activator
.logError("Error validating file: " + path
, e
); //$NON-NLS-1$
453 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
455 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
457 double matches
= 0.0;
458 String line
= rafile
.getNextLine();
460 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
461 for (InputLine inputLine
: fDefinition
.inputs
) {
462 Matcher matcher
= inputLine
.getPattern().matcher(line
);
463 if (matcher
.matches()) {
464 int groupCount
= matcher
.groupCount();
465 matches
+= (1.0 + groupCount
/ ((double) groupCount
+ 1));
469 confidence
= (int) (MAX_CONFIDENCE
* matches
/ lineCount
);
470 line
= rafile
.getNextLine();
472 } catch (IOException e
) {
473 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
475 return new TraceValidationStatus(confidence
, Activator
.PLUGIN_ID
);
478 private static int fCheckpointSize
= -1;
481 public synchronized int getCheckpointSize() {
482 if (fCheckpointSize
== -1) {
483 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
484 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
487 fCheckpointSize
= b
.position();
490 return fCheckpointSize
;
494 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
495 return new TmfLongLocation(bufferIn
);
499 protected ITmfTraceIndexer
createIndexer(int interval
) {
500 return new TmfBTreeTraceIndexer(this, interval
);
504 public String
getTraceTypeId() {
509 * Build the trace type id for a custom text trace
513 * @param definitionName
514 * the definition name
515 * @return the trace type id
517 public static @NonNull String
buildTraceTypeId(String category
, String definitionName
) {
518 return CUSTOM_TXT_TRACE_TYPE_PREFIX
+ category
+ SEPARATOR
+ definitionName
;
522 * Checks whether the given trace type ID is a custom text trace type ID
525 * the trace type ID to check
526 * @return <code>true</code> if it's a custom text trace type ID else <code>false</code>
528 public static boolean isCustomTraceTypeId(@NonNull String traceTypeId
) {
529 return traceTypeId
.startsWith(CUSTOM_TXT_TRACE_TYPE_PREFIX
);
533 * This methods builds a trace type ID from a given ID taking into
534 * consideration any format changes that were done for the IDs of custom
535 * text traces. For example, such format change took place when moving to
536 * Trace Compass. Trace type IDs that are part of the plug-in extension for
537 * trace types won't be changed.
539 * This method is useful for IDs that were persisted in the workspace before
540 * the format changes (e.g. in the persistent properties of a trace
543 * It ensures backwards compatibility of the workspace for custom text
547 * the legacy trace type ID
548 * @return the trace type id in Trace Compass format
550 public static @NonNull String
buildCompatibilityTraceTypeId(@NonNull String traceTypeId
) {
551 // Handle early Trace Compass custom text trace type IDs
552 if (traceTypeId
.startsWith(EARLY_TRACE_COMPASS_CUSTOM_TXT_TRACE_TYPE_PREFIX
)) {
553 return CUSTOM_TXT_TRACE_TYPE_PREFIX
+ traceTypeId
.substring(EARLY_TRACE_COMPASS_CUSTOM_TXT_TRACE_TYPE_PREFIX
.length());
556 // Handle Linux Tools custom text trace type IDs (with and without category)
557 int index
= traceTypeId
.lastIndexOf(SEPARATOR
);
558 if ((index
!= -1) && (traceTypeId
.startsWith(LINUX_TOOLS_CUSTOM_TXT_TRACE_TYPE_PREFIX
))) {
559 String definitionName
= index
< traceTypeId
.length() ? traceTypeId
.substring(index
+ 1) : ""; //$NON-NLS-1$
560 if (traceTypeId
.contains(CustomTxtTrace
.class.getSimpleName() + SEPARATOR
) && traceTypeId
.indexOf(SEPARATOR
) == index
) {
561 return buildTraceTypeId(CustomTxtTraceDefinition
.CUSTOM_TXT_CATEGORY
, definitionName
);
563 return CUSTOM_TXT_TRACE_TYPE_PREFIX
+ traceTypeId
.substring(LINUX_TOOLS_CUSTOM_TXT_TRACE_TYPE_PREFIX
.length());
570 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal
) {
571 if (signal
.getTrace() == this) {
573 synchronized (this) {
574 // Reset the file handle in case it has reached the end of the
575 // file already. Otherwise, it will not be able to read new data
576 // pass the previous end.
579 } catch (TmfTraceException e
) {
580 Activator
.logError(e
.getLocalizedMessage(), e
);
583 super.traceRangeUpdated(signal
);