1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
12 * Simon Delisle - Replace LinkedList by TreeSet in callsitesByName attribute
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
18 import java
.io
.FileFilter
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.io
.Serializable
;
22 import java
.nio
.ByteOrder
;
23 import java
.nio
.MappedByteBuffer
;
24 import java
.nio
.channels
.FileChannel
;
25 import java
.nio
.channels
.FileChannel
.MapMode
;
26 import java
.util
.Arrays
;
27 import java
.util
.Comparator
;
28 import java
.util
.HashMap
;
29 import java
.util
.Iterator
;
30 import java
.util
.LinkedList
;
31 import java
.util
.List
;
33 import java
.util
.Map
.Entry
;
35 import java
.util
.TreeSet
;
36 import java
.util
.UUID
;
38 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
39 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
40 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
41 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
42 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
43 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
44 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
45 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
46 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
47 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
48 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
49 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.CTFCallsiteComparator
;
50 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
53 * A CTF trace on the file system.
55 * Represents a trace on the filesystem. It is responsible of parsing the
56 * metadata, creating declarations data structures, indexing the event packets
57 * (in other words, all the work that can be shared between readers), but the
58 * actual reading of events is left to TraceReader.
60 * @author Matthew Khouzam
61 * @version $Revision: 1.0 $
63 public class CTFTrace
implements IDefinitionScope
{
65 @SuppressWarnings("nls")
67 public String
toString() {
68 /* Only for debugging, shouldn't be externalized */
69 return "CTFTrace [path=" + path
+ ", major=" + major
+ ", minor="
70 + minor
+ ", uuid=" + uuid
+ "]";
74 * The trace directory on the filesystem.
76 private final File path
;
79 * Major CTF version number
84 * Minor CTF version number
96 private ByteOrder byteOrder
;
99 * Packet header structure declaration
101 private StructDeclaration packetHeaderDecl
= null;
104 * The clock of the trace
106 private CTFClock singleClock
;
109 * Packet header structure definition
111 * This is only used when opening the trace files, to read the first packet
112 * header and see if they are valid trace files.
114 private StructDefinition packetHeaderDef
;
117 * Collection of streams contained in the trace.
119 private final Map
<Long
, Stream
> streams
= new HashMap
<Long
, Stream
>();
122 * Collection of environment variables set by the tracer
124 private final Map
<String
, String
> environment
= new HashMap
<String
, String
>();
127 * Collection of all the clocks in a system.
129 private final Map
<String
, CTFClock
> clocks
= new HashMap
<String
, CTFClock
>();
131 /** FileInputStreams to the streams */
132 private final List
<FileInputStream
> fileInputStreams
= new LinkedList
<FileInputStream
>();
134 /** Handlers for the metadata files */
135 private static final FileFilter METADATA_FILE_FILTER
= new MetadataFileFilter();
136 private static final Comparator
<File
> METADATA_COMPARATOR
= new MetadataComparator();
138 /** map of all the event types */
139 private final Map
<Long
, HashMap
<Long
, IEventDeclaration
>> eventDecs
= new HashMap
<Long
, HashMap
<Long
, IEventDeclaration
>>();
141 /** Callsite helpers */
142 private CTFCallsiteComparator ctfCallsiteComparator
= new CTFCallsiteComparator();
144 private Map
<String
, TreeSet
<CTFCallsite
>> callsitesByName
= new HashMap
<String
, TreeSet
<CTFCallsite
>>();
146 /** Callsite helpers */
147 private TreeSet
<CTFCallsite
> callsitesByIP
= new TreeSet
<CTFCallsite
>();
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
157 * Filesystem path of the trace directory
158 * @throws CTFReaderException
159 * If no CTF trace was found at the path
161 public CTFTrace(String path
) throws CTFReaderException
{
162 this(new File(path
));
170 * Filesystem path of the trace directory.
171 * @throws CTFReaderException
172 * If no CTF trace was found at the path
174 public CTFTrace(File path
) throws CTFReaderException
{
176 final Metadata metadata
= new Metadata(this);
178 /* Set up the internal containers for this trace */
179 if (!this.path
.exists()) {
180 throw new CTFReaderException("Trace (" + path
.getPath() + ") doesn't exist. Deleted or moved?"); //$NON-NLS-1$ //$NON-NLS-2$
183 if (!this.path
.isDirectory()) {
184 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
187 /* Open and parse the metadata file */
190 /* Open all the trace files */
191 /* Create the definitions needed to read things from the files */
192 if (packetHeaderDecl
!= null) {
193 packetHeaderDef
= packetHeaderDecl
.createDefinition(this, "packet.header"); //$NON-NLS-1$
196 /* List files not called metadata and not hidden. */
197 File
[] files
= path
.listFiles(METADATA_FILE_FILTER
);
198 Arrays
.sort(files
, METADATA_COMPARATOR
);
199 /* Try to open each file */
200 for (File streamFile
: files
) {
201 openStreamInput(streamFile
);
204 /* Create their index */
205 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
206 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
207 for (StreamInput s
: inputs
) {
211 Iterator
<Entry
<Long
, IEventDeclaration
>> it
= s
.getStream()
212 .getEvents().entrySet().iterator();
213 while (it
.hasNext()) {
214 Entry
<Long
, IEventDeclaration
> pairs
= it
.next();
215 Long eventNum
= pairs
.getKey();
216 IEventDeclaration eventDec
= pairs
.getValue();
217 getEvents(s
.getStream().getId()).put(eventNum
, eventDec
);
233 public void dispose() {
234 for (FileInputStream fis
: fileInputStreams
) {
238 } catch (IOException e
) {
239 // do nothing it's ok, we tried to close it.
243 // Invoke GC to release MappedByteBuffer objects (Java bug JDK-4724038)
247 // ------------------------------------------------------------------------
248 // Getters/Setters/Predicates
249 // ------------------------------------------------------------------------
252 * Gets an event declaration hash map for a given streamID
255 * The ID of the stream from which to read
256 * @return The Hash map with the event declarations
259 public Map
<Long
, IEventDeclaration
> getEvents(Long streamId
) {
260 return eventDecs
.get(streamId
);
264 * Gets an event Declaration hashmap for a given StreamInput
268 * @return an empty hashmap, please see deprecated
270 * @deprecated You should be using
271 * {@link StreamInputReader#getEventDefinitions()} instead.
274 public Map
<Long
, EventDefinition
> getEventDefs(StreamInput id
) {
275 return new HashMap
<Long
, EventDefinition
>();
279 * Get an event by it's ID
282 * The ID of the stream from which to read
284 * the ID of the event
285 * @return the event declaration
288 public IEventDeclaration
getEventType(long streamId
, long id
) {
289 return getEvents(streamId
).get(id
);
293 * Method getStream gets the stream for a given id
296 * Long the id of the stream
297 * @return Stream the stream that we need
300 public Stream
getStream(Long id
) {
301 return streams
.get(id
);
305 * Method nbStreams gets the number of available streams
307 * @return int the number of streams
309 public int nbStreams() {
310 return streams
.size();
314 * Method setMajor sets the major version of the trace (DO NOT USE)
317 * long the major version
319 public void setMajor(long major
) {
324 * Method setMinor sets the minor version of the trace (DO NOT USE)
327 * long the minor version
329 public void setMinor(long minor
) {
334 * Method setUUID sets the UUID of a trace
339 public void setUUID(UUID uuid
) {
344 * Method setByteOrder sets the byte order
347 * ByteOrder of the trace, can be little-endian or big-endian
349 public void setByteOrder(ByteOrder byteOrder
) {
350 this.byteOrder
= byteOrder
;
354 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
356 * @param packetHeader
357 * StructDeclaration the header in structdeclaration form
359 public void setPacketHeader(StructDeclaration packetHeader
) {
360 this.packetHeaderDecl
= packetHeader
;
364 * Method majorIsSet is the major version number set?
366 * @return boolean is the major set?
369 public boolean majorIsSet() {
370 return major
!= null;
374 * Method minorIsSet. is the minor version number set?
376 * @return boolean is the minor set?
378 public boolean minorIsSet() {
379 return minor
!= null;
383 * Method UUIDIsSet is the UUID set?
385 * @return boolean is the UUID set?
388 public boolean uuidIsSet() {
393 * Method byteOrderIsSet is the byteorder set?
395 * @return boolean is the byteorder set?
397 public boolean byteOrderIsSet() {
398 return byteOrder
!= null;
402 * Method packetHeaderIsSet is the packet header set?
404 * @return boolean is the packet header set?
406 public boolean packetHeaderIsSet() {
407 return packetHeaderDecl
!= null;
411 * Method getUUID gets the trace UUID
413 * @return UUID gets the trace UUID
415 public UUID
getUUID() {
420 * Method getMajor gets the trace major version
422 * @return long gets the trace major version
424 public long getMajor() {
429 * Method getMinor gets the trace minor version
431 * @return long gets the trace minor version
433 public long getMinor() {
438 * Method getByteOrder gets the trace byte order
440 * @return ByteOrder gets the trace byte order
442 public final ByteOrder
getByteOrder() {
447 * Method getPacketHeader gets the trace packet header
449 * @return StructDeclaration gets the trace packet header
451 public StructDeclaration
getPacketHeader() {
452 return packetHeaderDecl
;
456 * Method getTraceDirectory gets the trace directory
458 * @return File the path in "File" format.
460 public File
getTraceDirectory() {
465 * Method getStreams get all the streams in a map format.
467 * @return Map<Long,Stream> a map of all the streams.
469 public Map
<Long
, Stream
> getStreams() {
474 * Method getPath gets the path of the trace directory
476 * @return String the path of the trace directory, in string format.
477 * @see java.io.File#getPath()
480 public String
getPath() {
481 return path
.getPath();
484 // ------------------------------------------------------------------------
486 // ------------------------------------------------------------------------
489 * Tries to open the given file, reads the first packet header of the file
490 * and check its validity.
493 * A trace file in the trace directory.
495 * Which index in the class' streamFileChannel array this file
497 * @throws CTFReaderException
499 private void openStreamInput(File streamFile
) throws CTFReaderException
{
500 MappedByteBuffer byteBuffer
;
501 BitBuffer streamBitBuffer
;
505 if (!streamFile
.canRead()) {
506 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
507 + streamFile
.getPath());
511 /* Open the file and get the FileChannel */
512 FileInputStream fis
= new FileInputStream(streamFile
);
513 fileInputStreams
.add(fis
);
514 fc
= fis
.getChannel();
516 /* Map one memory page of 4 kiB */
517 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, (int) Math
.min(fc
.size(), 4096L));
518 } catch (IOException e
) {
519 /* Shouldn't happen at this stage if every other check passed */
520 throw new CTFReaderException(e
);
523 /* Create a BitBuffer with this mapping and the trace byte order */
524 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
526 if (packetHeaderDef
!= null) {
527 /* Read the packet header */
528 packetHeaderDef
.read(streamBitBuffer
);
530 /* Check the magic number */
531 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
532 .lookupDefinition("magic"); //$NON-NLS-1$
533 int magic
= (int) magicDef
.getValue();
534 if (magic
!= Utils
.CTF_MAGIC
) {
535 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
539 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
540 .lookupDefinition("uuid"); //$NON-NLS-1$
541 if (uuidDef
!= null) {
542 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
544 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
545 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
547 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
550 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
552 if (!this.uuid
.equals(otheruuid
)) {
553 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
557 /* Read the stream ID */
558 Definition streamIDDef
= packetHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
560 if (streamIDDef
instanceof IntegerDefinition
) { // this doubles as a
562 long streamID
= ((IntegerDefinition
) streamIDDef
).getValue();
563 stream
= streams
.get(streamID
);
565 /* No stream_id in the packet header */
566 stream
= streams
.get(null);
570 /* No packet header, we suppose there is only one stream */
571 stream
= streams
.get(null);
574 if (stream
== null) {
575 throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$
578 /* Create the stream input */
579 StreamInput streamInput
= new StreamInput(stream
, fc
, streamFile
);
581 /* Add a reference to the streamInput in the stream */
582 stream
.addInput(streamInput
);
586 * Looks up a definition from packet
591 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
594 public Definition
lookupDefinition(String lookupPath
) {
595 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
596 return packetHeaderDef
;
602 * Adds a new stream to the trace.
606 * @throws ParseException
607 * If there was some problem reading the metadata
610 public void addStream(Stream stream
) throws ParseException
{
613 * If there is already a stream without id (the null key), it must be
616 if (streams
.get(null) != null) {
617 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
621 * If the stream we try to add has the null key, it must be the only
622 * one. Thus, if the streams container is not empty, it is not valid.
624 if ((stream
.getId() == null) && (streams
.size() != 0)) {
625 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
628 /* If a stream with the same ID already exists, it is not valid. */
629 if (streams
.get(stream
.getId()) != null) {
630 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
633 /* It should be ok now. */
634 streams
.put(stream
.getId(), stream
);
635 eventDecs
.put(stream
.getId(), new HashMap
<Long
, IEventDeclaration
>());
639 * gets the Environment variables from the trace metadata (See CTF spec)
641 * @return the environment variables in a map form (key value)
644 public Map
<String
, String
> getEnvironment() {
649 * Look up a specific environment variable
652 * the key to look for
653 * @return the value of the variable, can be null.
655 public String
lookupEnvironment(String key
) {
656 return environment
.get(key
);
660 * Add a variable to the environment variables
663 * the name of the variable
665 * the value of the variable
667 public void addEnvironmentVar(String varName
, String varValue
) {
668 environment
.put(varName
, varValue
);
672 * Add a clock to the clock list
675 * the name of the clock (full name with scope)
679 public void addClock(String nameValue
, CTFClock ctfClock
) {
680 clocks
.put(nameValue
, ctfClock
);
684 * gets the clock with a specific name
687 * the name of the clock.
690 public CTFClock
getClock(String name
) {
691 return clocks
.get(name
);
695 * gets the clock if there is only one. (this is 100% of the use cases as of
700 public final CTFClock
getClock() {
701 if (clocks
.size() == 1) {
702 singleClock
= clocks
.get(clocks
.keySet().iterator().next());
709 * gets the time offset of a clock with respect to UTC in nanoseconds
711 * @return the time offset of a clock with respect to UTC in nanoseconds
713 public final long getOffset() {
714 if (getClock() == null) {
717 return singleClock
.getClockOffset();
721 * gets the time offset of a clock with respect to UTC in nanoseconds
723 * @return the time offset of a clock with respect to UTC in nanoseconds
725 private double getTimeScale() {
726 if (getClock() == null) {
729 return singleClock
.getClockScale();
733 * Does the trace need to time scale?
735 * @return if the trace is in ns or cycles.
737 private boolean clockNeedsScale() {
738 if (getClock() == null) {
741 return singleClock
.isClockScaled();
745 * the inverse clock for returning to a scale.
747 * @return 1.0 / scale
749 private double getInverseTimeScale() {
750 if (getClock() == null) {
753 return singleClock
.getClockAntiScale();
758 * clock cycles since boot
759 * @return time in nanoseconds UTC offset
762 public long timestampCyclesToNanos(long cycles
) {
763 long retVal
= cycles
+ getOffset();
765 * this fix is since quite often the offset will be > than 53 bits and
766 * therefore the conversion will be lossy
768 if (clockNeedsScale()) {
769 retVal
= (long) (retVal
* getTimeScale());
776 * time in nanoseconds UTC offset
777 * @return clock cycles since boot.
780 public long timestampNanoToCycles(long nanos
) {
783 * this fix is since quite often the offset will be > than 53 bits and
784 * therefore the conversion will be lossy
786 if (clockNeedsScale()) {
787 retVal
= (long) (nanos
* getInverseTimeScale());
791 return retVal
- getOffset();
795 * Does a given stream contain any events?
799 * @return true if the stream has events.
801 public boolean hasEvents(Long id
) {
802 return eventDecs
.containsKey(id
);
806 * Add an event declaration map to the events map.
810 * @return the hashmap containing events.
813 public Map
<Long
, IEventDeclaration
> createEvents(Long id
) {
814 HashMap
<Long
, IEventDeclaration
> value
= eventDecs
.get(id
);
816 value
= new HashMap
<Long
, IEventDeclaration
>();
817 eventDecs
.put(id
, value
);
826 * the event name of the callsite
828 * the name of the callsite function
830 * the ip of the callsite
832 * the filename of the callsite
834 * the line number of the callsite
836 public void addCallsite(String eventName
, String funcName
, long ip
,
837 String fileName
, long lineNumber
) {
838 final CTFCallsite cs
= new CTFCallsite(eventName
, funcName
, ip
,
839 fileName
, lineNumber
);
840 TreeSet
<CTFCallsite
> csl
= callsitesByName
.get(eventName
);
842 csl
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
843 callsitesByName
.put(eventName
, csl
);
848 callsitesByIP
.add(cs
);
852 * Gets the set of callsites associated to an event name. O(1)
856 * @return the callsite set can be empty
859 public TreeSet
<CTFCallsite
> getCallsiteCandidates(String eventName
) {
860 TreeSet
<CTFCallsite
> retVal
= callsitesByName
.get(eventName
);
861 if (retVal
== null) {
862 retVal
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
868 * The I'm feeling lucky of getCallsiteCandidates O(1)
872 * @return the first callsite that has that event name, can be null
875 public CTFCallsite
getCallsite(String eventName
) {
876 TreeSet
<CTFCallsite
> callsites
= callsitesByName
.get(eventName
);
877 if (callsites
!= null) {
878 return callsites
.first();
884 * Gets a callsite from the instruction pointer O(log(n))
887 * the instruction pointer to lookup
888 * @return the callsite just before that IP in the list remember the IP is
889 * backwards on X86, can be null if no callsite is before the IP.
892 public CTFCallsite
getCallsite(long ip
) {
893 CTFCallsite cs
= new CTFCallsite(null, null, ip
, null, 0L);
894 return callsitesByIP
.ceiling(cs
);
898 * Gets a callsite using the event name and instruction pointer O(log(n))
901 * the name of the event
903 * the instruction pointer
904 * @return the closest matching callsite, can be null
906 public CTFCallsite
getCallsite(String eventName
, long ip
) {
907 final TreeSet
<CTFCallsite
> candidates
= callsitesByName
.get(eventName
);
908 final CTFCallsite dummyCs
= new CTFCallsite(null, null, ip
, null, -1);
909 final CTFCallsite callsite
= candidates
.ceiling(dummyCs
);
910 if (callsite
== null) {
911 return candidates
.floor(dummyCs
);
917 class MetadataFileFilter
implements FileFilter
{
920 public boolean accept(File pathname
) {
921 if (pathname
.isDirectory()) {
924 if (pathname
.isHidden()) {
927 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
935 class MetadataComparator
implements Comparator
<File
>, Serializable
{
937 private static final long serialVersionUID
= 1L;
940 public int compare(File o1
, File o2
) {
941 return o1
.getName().compareTo(o2
.getName());