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
;
51 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndex
;
54 * A CTF trace on the file system.
56 * Represents a trace on the filesystem. It is responsible of parsing the
57 * metadata, creating declarations data structures, indexing the event packets
58 * (in other words, all the work that can be shared between readers), but the
59 * actual reading of events is left to TraceReader.
61 * @author Matthew Khouzam
62 * @version $Revision: 1.0 $
64 public class CTFTrace
implements IDefinitionScope
{
66 @SuppressWarnings("nls")
68 public String
toString() {
69 /* Only for debugging, shouldn't be externalized */
70 return "CTFTrace [path=" + path
+ ", major=" + major
+ ", minor="
71 + minor
+ ", uuid=" + uuid
+ "]";
75 * The trace directory on the filesystem.
77 private final File path
;
80 * Major CTF version number
85 * Minor CTF version number
97 private ByteOrder byteOrder
;
100 * Packet header structure declaration
102 private StructDeclaration packetHeaderDecl
= null;
105 * The clock of the trace
107 private CTFClock singleClock
;
110 * Packet header structure definition
112 * This is only used when opening the trace files, to read the first packet
113 * header and see if they are valid trace files.
115 private StructDefinition packetHeaderDef
;
118 * Collection of streams contained in the trace.
120 private final Map
<Long
, Stream
> streams
= new HashMap
<Long
, Stream
>();
123 * Collection of environment variables set by the tracer
125 private final Map
<String
, String
> environment
= new HashMap
<String
, String
>();
128 * Collection of all the clocks in a system.
130 private final Map
<String
, CTFClock
> clocks
= new HashMap
<String
, CTFClock
>();
132 /** FileInputStreams to the streams */
133 private final List
<FileInputStream
> fileInputStreams
= new LinkedList
<FileInputStream
>();
135 /** Handlers for the metadata files */
136 private static final FileFilter METADATA_FILE_FILTER
= new MetadataFileFilter();
137 private static final Comparator
<File
> METADATA_COMPARATOR
= new MetadataComparator();
139 /** map of all the event types */
140 private final Map
<Long
, HashMap
<Long
, IEventDeclaration
>> eventDecs
= new HashMap
<Long
, HashMap
<Long
, IEventDeclaration
>>();
142 /** map of all the indexes */
143 private final Map
<StreamInput
, StreamInputPacketIndex
> indexes
= new HashMap
<StreamInput
, StreamInputPacketIndex
>();
145 /** Callsite helpers */
146 private CTFCallsiteComparator ctfCallsiteComparator
= new CTFCallsiteComparator();
148 private Map
<String
, TreeSet
<CTFCallsite
>> callsitesByName
= new HashMap
<String
, TreeSet
<CTFCallsite
>>();
150 /** Callsite helpers */
151 private TreeSet
<CTFCallsite
> callsitesByIP
= new TreeSet
<CTFCallsite
>();
153 // ------------------------------------------------------------------------
155 // ------------------------------------------------------------------------
161 * Filesystem path of the trace directory
162 * @throws CTFReaderException
163 * If no CTF trace was found at the path
165 public CTFTrace(String path
) throws CTFReaderException
{
166 this(new File(path
));
174 * Filesystem path of the trace directory.
175 * @throws CTFReaderException
176 * If no CTF trace was found at the path
178 public CTFTrace(File path
) throws CTFReaderException
{
180 final Metadata metadata
= new Metadata(this);
182 /* Set up the internal containers for this trace */
183 if (!this.path
.exists()) {
184 throw new CTFReaderException("Trace (" + path
.getPath() + ") doesn't exist. Deleted or moved?"); //$NON-NLS-1$ //$NON-NLS-2$
187 if (!this.path
.isDirectory()) {
188 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
191 /* Open and parse the metadata file */
194 /* Open all the trace files */
195 /* Create the definitions needed to read things from the files */
196 if (packetHeaderDecl
!= null) {
197 packetHeaderDef
= packetHeaderDecl
.createDefinition(this, "packet.header"); //$NON-NLS-1$
200 /* List files not called metadata and not hidden. */
201 File
[] files
= path
.listFiles(METADATA_FILE_FILTER
);
202 Arrays
.sort(files
, METADATA_COMPARATOR
);
203 /* Try to open each file */
204 for (File streamFile
: files
) {
205 openStreamInput(streamFile
);
208 /* Create their index */
209 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
210 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
211 for (StreamInput s
: inputs
) {
215 Iterator
<Entry
<Long
, IEventDeclaration
>> it
= s
.getStream()
216 .getEvents().entrySet().iterator();
217 while (it
.hasNext()) {
218 Entry
<Long
, IEventDeclaration
> pairs
= it
.next();
219 Long eventNum
= pairs
.getKey();
220 IEventDeclaration eventDec
= pairs
.getValue();
221 getEvents(s
.getStream().getId()).put(eventNum
, eventDec
);
237 public void dispose() {
238 for (FileInputStream fis
: fileInputStreams
) {
242 } catch (IOException e
) {
243 // do nothing it's ok, we tried to close it.
247 // Invoke GC to release MappedByteBuffer objects (Java bug JDK-4724038)
251 // ------------------------------------------------------------------------
252 // Getters/Setters/Predicates
253 // ------------------------------------------------------------------------
256 * Gets an event declaration hash map for a given streamID
259 * The ID of the stream from which to read
260 * @return The Hash map with the event declarations
263 public Map
<Long
, IEventDeclaration
> getEvents(Long streamId
) {
264 return eventDecs
.get(streamId
);
268 * Gets an index for a given StreamInput
274 StreamInputPacketIndex
getIndex(StreamInput id
) {
275 if (!indexes
.containsKey(id
)) {
276 indexes
.put(id
, new StreamInputPacketIndex());
278 return indexes
.get(id
);
282 * Gets an event Declaration hashmap for a given StreamInput
286 * @return an empty hashmap, please see deprecated
288 * @deprecated You should be using
289 * {@link StreamInputReader#getEventDefinitions()} instead.
292 public Map
<Long
, EventDefinition
> getEventDefs(StreamInput id
) {
293 return new HashMap
<Long
, EventDefinition
>();
297 * Get an event by it's ID
300 * The ID of the stream from which to read
302 * the ID of the event
303 * @return the event declaration
306 public IEventDeclaration
getEventType(long streamId
, long id
) {
307 return getEvents(streamId
).get(id
);
311 * Method getStream gets the stream for a given id
314 * Long the id of the stream
315 * @return Stream the stream that we need
318 public Stream
getStream(Long id
) {
319 return streams
.get(id
);
323 * Method nbStreams gets the number of available streams
325 * @return int the number of streams
327 public int nbStreams() {
328 return streams
.size();
332 * Method setMajor sets the major version of the trace (DO NOT USE)
335 * long the major version
337 public void setMajor(long major
) {
342 * Method setMinor sets the minor version of the trace (DO NOT USE)
345 * long the minor version
347 public void setMinor(long minor
) {
352 * Method setUUID sets the UUID of a trace
357 public void setUUID(UUID uuid
) {
362 * Method setByteOrder sets the byte order
365 * ByteOrder of the trace, can be little-endian or big-endian
367 public void setByteOrder(ByteOrder byteOrder
) {
368 this.byteOrder
= byteOrder
;
372 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
374 * @param packetHeader
375 * StructDeclaration the header in structdeclaration form
377 public void setPacketHeader(StructDeclaration packetHeader
) {
378 this.packetHeaderDecl
= packetHeader
;
382 * Method majortIsSet is the major version number set?
384 * @return boolean is the major set?
386 public boolean majortIsSet() {
387 return major
!= null;
391 * Method minorIsSet. is the minor version number set?
393 * @return boolean is the minor set?
395 public boolean minorIsSet() {
396 return minor
!= null;
400 * Method UUIDIsSet is the UUID set?
402 * @return boolean is the UUID set?
405 public boolean uuidIsSet() {
410 * Method byteOrderIsSet is the byteorder set?
412 * @return boolean is the byteorder set?
414 public boolean byteOrderIsSet() {
415 return byteOrder
!= null;
419 * Method packetHeaderIsSet is the packet header set?
421 * @return boolean is the packet header set?
423 public boolean packetHeaderIsSet() {
424 return packetHeaderDecl
!= null;
428 * Method getUUID gets the trace UUID
430 * @return UUID gets the trace UUID
432 public UUID
getUUID() {
437 * Method getMajor gets the trace major version
439 * @return long gets the trace major version
441 public long getMajor() {
446 * Method getMinor gets the trace minor version
448 * @return long gets the trace minor version
450 public long getMinor() {
455 * Method getByteOrder gets the trace byte order
457 * @return ByteOrder gets the trace byte order
459 public final ByteOrder
getByteOrder() {
464 * Method getPacketHeader gets the trace packet header
466 * @return StructDeclaration gets the trace packet header
468 public StructDeclaration
getPacketHeader() {
469 return packetHeaderDecl
;
473 * Method getTraceDirectory gets the trace directory
475 * @return File the path in "File" format.
477 public File
getTraceDirectory() {
482 * Method getStreams get all the streams in a map format.
484 * @return Map<Long,Stream> a map of all the streams.
486 public Map
<Long
, Stream
> getStreams() {
491 * Method getPath gets the path of the trace directory
493 * @return String the path of the trace directory, in string format.
494 * @see java.io.File#getPath()
497 public String
getPath() {
498 return path
.getPath();
501 // ------------------------------------------------------------------------
503 // ------------------------------------------------------------------------
506 * Tries to open the given file, reads the first packet header of the file
507 * and check its validity.
510 * A trace file in the trace directory.
512 * Which index in the class' streamFileChannel array this file
514 * @throws CTFReaderException
516 private void openStreamInput(File streamFile
) throws CTFReaderException
{
517 MappedByteBuffer byteBuffer
;
518 BitBuffer streamBitBuffer
;
522 if (!streamFile
.canRead()) {
523 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
524 + streamFile
.getPath());
528 /* Open the file and get the FileChannel */
529 FileInputStream fis
= new FileInputStream(streamFile
);
530 fileInputStreams
.add(fis
);
531 fc
= fis
.getChannel();
533 /* Map one memory page of 4 kiB */
534 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, (int) Math
.min(fc
.size(), 4096L));
535 } catch (IOException e
) {
536 /* Shouldn't happen at this stage if every other check passed */
537 throw new CTFReaderException(e
);
540 /* Create a BitBuffer with this mapping and the trace byte order */
541 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
543 if (packetHeaderDef
!= null) {
544 /* Read the packet header */
545 packetHeaderDef
.read(streamBitBuffer
);
547 /* Check the magic number */
548 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
549 .lookupDefinition("magic"); //$NON-NLS-1$
550 int magic
= (int) magicDef
.getValue();
551 if (magic
!= Utils
.CTF_MAGIC
) {
552 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
556 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
557 .lookupDefinition("uuid"); //$NON-NLS-1$
558 if (uuidDef
!= null) {
559 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
561 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
562 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
564 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
567 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
569 if (!this.uuid
.equals(otheruuid
)) {
570 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
574 /* Read the stream ID */
575 Definition streamIDDef
= packetHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
577 if (streamIDDef
instanceof IntegerDefinition
) { // this doubles as a
579 long streamID
= ((IntegerDefinition
) streamIDDef
).getValue();
580 stream
= streams
.get(streamID
);
582 /* No stream_id in the packet header */
583 stream
= streams
.get(null);
587 /* No packet header, we suppose there is only one stream */
588 stream
= streams
.get(null);
591 if (stream
== null) {
592 throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$
595 /* Create the stream input */
596 StreamInput streamInput
= new StreamInput(stream
, fc
, streamFile
);
598 /* Add a reference to the streamInput in the stream */
599 stream
.addInput(streamInput
);
603 * Looks up a definition from packet
608 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
611 public Definition
lookupDefinition(String lookupPath
) {
612 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
613 return packetHeaderDef
;
619 * Adds a new stream to the trace.
623 * @throws ParseException
624 * If there was some problem reading the metadata
627 public void addStream(Stream stream
) throws ParseException
{
630 * If there is already a stream without id (the null key), it must be
633 if (streams
.get(null) != null) {
634 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
638 * If the stream we try to add has the null key, it must be the only
639 * one. Thus, if the streams container is not empty, it is not valid.
641 if ((stream
.getId() == null) && (streams
.size() != 0)) {
642 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
645 /* If a stream with the same ID already exists, it is not valid. */
646 if (streams
.get(stream
.getId()) != null) {
647 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
650 /* It should be ok now. */
651 streams
.put(stream
.getId(), stream
);
652 eventDecs
.put(stream
.getId(), new HashMap
<Long
, IEventDeclaration
>());
656 * gets the Environment variables from the trace metadata (See CTF spec)
658 * @return the environment variables in a map form (key value)
661 public Map
<String
, String
> getEnvironment() {
666 * Look up a specific environment variable
669 * the key to look for
670 * @return the value of the variable, can be null.
672 public String
lookupEnvironment(String key
) {
673 return environment
.get(key
);
677 * Add a variable to the environment variables
680 * the name of the variable
682 * the value of the variable
684 public void addEnvironmentVar(String varName
, String varValue
) {
685 environment
.put(varName
, varValue
);
689 * Add a clock to the clock list
692 * the name of the clock (full name with scope)
696 public void addClock(String nameValue
, CTFClock ctfClock
) {
697 clocks
.put(nameValue
, ctfClock
);
701 * gets the clock with a specific name
704 * the name of the clock.
707 public CTFClock
getClock(String name
) {
708 return clocks
.get(name
);
712 * gets the clock if there is only one. (this is 100% of the use cases as of
717 public final CTFClock
getClock() {
718 if (clocks
.size() == 1) {
719 singleClock
= clocks
.get(clocks
.keySet().iterator().next());
726 * gets the time offset of a clock with respect to UTC in nanoseconds
728 * @return the time offset of a clock with respect to UTC in nanoseconds
730 public final long getOffset() {
731 if (getClock() == null) {
734 return singleClock
.getClockOffset();
738 * gets the time offset of a clock with respect to UTC in nanoseconds
740 * @return the time offset of a clock with respect to UTC in nanoseconds
742 private double getTimeScale() {
743 if (getClock() == null) {
746 return singleClock
.getClockScale();
750 * Does the trace need to time scale?
752 * @return if the trace is in ns or cycles.
754 private boolean clockNeedsScale() {
755 if (getClock() == null) {
758 return singleClock
.isClockScaled();
762 * the inverse clock for returning to a scale.
764 * @return 1.0 / scale
766 private double getInverseTimeScale() {
767 if (getClock() == null) {
770 return singleClock
.getClockAntiScale();
775 * clock cycles since boot
776 * @return time in nanoseconds UTC offset
779 public long timestampCyclesToNanos(long cycles
) {
780 long retVal
= cycles
+ getOffset();
782 * this fix is since quite often the offset will be > than 53 bits and
783 * therefore the conversion will be lossy
785 if (clockNeedsScale()) {
786 retVal
= (long) (retVal
* getTimeScale());
793 * time in nanoseconds UTC offset
794 * @return clock cycles since boot.
797 public long timestampNanoToCycles(long nanos
) {
800 * this fix is since quite often the offset will be > than 53 bits and
801 * therefore the conversion will be lossy
803 if (clockNeedsScale()) {
804 retVal
= (long) (nanos
* getInverseTimeScale());
808 return retVal
- getOffset();
812 * Does a given stream contain any events?
816 * @return true if the stream has events.
818 public boolean hasEvents(Long id
) {
819 return eventDecs
.containsKey(id
);
823 * Add an event declaration map to the events map.
827 * @return the hashmap containing events.
830 public Map
<Long
, IEventDeclaration
> createEvents(Long id
) {
831 HashMap
<Long
, IEventDeclaration
> value
= eventDecs
.get(id
);
833 value
= new HashMap
<Long
, IEventDeclaration
>();
834 eventDecs
.put(id
, value
);
843 * the event name of the callsite
845 * the name of the callsite function
847 * the ip of the callsite
849 * the filename of the callsite
851 * the line number of the callsite
853 public void addCallsite(String eventName
, String funcName
, long ip
,
854 String fileName
, long lineNumber
) {
855 final CTFCallsite cs
= new CTFCallsite(eventName
, funcName
, ip
,
856 fileName
, lineNumber
);
857 TreeSet
<CTFCallsite
> csl
= callsitesByName
.get(eventName
);
859 csl
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
860 callsitesByName
.put(eventName
, csl
);
865 callsitesByIP
.add(cs
);
869 * Gets the set of callsites associated to an event name. O(1)
873 * @return the callsite set can be empty
876 public TreeSet
<CTFCallsite
> getCallsiteCandidates(String eventName
) {
877 TreeSet
<CTFCallsite
> retVal
= callsitesByName
.get(eventName
);
878 if (retVal
== null) {
879 retVal
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
885 * The I'm feeling lucky of getCallsiteCandidates O(1)
889 * @return the first callsite that has that event name, can be null
892 public CTFCallsite
getCallsite(String eventName
) {
893 TreeSet
<CTFCallsite
> callsites
= callsitesByName
.get(eventName
);
894 if (callsites
!= null) {
895 return callsites
.first();
901 * Gets a callsite from the instruction pointer O(log(n))
904 * the instruction pointer to lookup
905 * @return the callsite just before that IP in the list remember the IP is
906 * backwards on X86, can be null if no callsite is before the IP.
909 public CTFCallsite
getCallsite(long ip
) {
910 CTFCallsite cs
= new CTFCallsite(null, null, ip
, null, 0L);
911 return callsitesByIP
.ceiling(cs
);
915 * Gets a callsite using the event name and instruction pointer O(log(n))
918 * the name of the event
920 * the instruction pointer
921 * @return the closest matching callsite, can be null
923 public CTFCallsite
getCallsite(String eventName
, long ip
) {
924 final TreeSet
<CTFCallsite
> candidates
= callsitesByName
.get(eventName
);
925 final CTFCallsite dummyCs
= new CTFCallsite(null, null, ip
, null, -1);
926 final CTFCallsite callsite
= candidates
.ceiling(dummyCs
);
927 if (callsite
== null) {
928 return candidates
.floor(dummyCs
);
934 class MetadataFileFilter
implements FileFilter
{
937 public boolean accept(File pathname
) {
938 if (pathname
.isDirectory()) {
941 if (pathname
.isHidden()) {
944 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
952 class MetadataComparator
implements Comparator
<File
>, Serializable
{
954 private static final long serialVersionUID
= 1L;
957 public int compare(File o1
, File o2
) {
958 return o1
.getName().compareTo(o2
.getName());