1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
16 import java
.io
.FileFilter
;
17 import java
.io
.FileInputStream
;
18 import java
.io
.IOException
;
19 import java
.nio
.ByteOrder
;
20 import java
.nio
.MappedByteBuffer
;
21 import java
.nio
.channels
.FileChannel
;
22 import java
.nio
.channels
.FileChannel
.MapMode
;
23 import java
.util
.Arrays
;
24 import java
.util
.Comparator
;
25 import java
.util
.HashMap
;
28 import java
.util
.UUID
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
36 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
37 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.Activator
;
38 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
39 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
40 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.Stream
;
41 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInput
;
44 * <b><u>CTFTrace</u></b>
46 * Represents a trace on the filesystem. It is responsible of parsing the
47 * metadata, creating declarations data structures, indexing the event packets
48 * (in other words, all the work that can be shared between readers), but the
49 * actual reading of events is left to TraceReader.
51 * @author Matthew Khouzam
52 * @version $Revision: 1.0 $
54 public class CTFTrace
implements IDefinitionScope
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
63 * @see java.lang.Object#toString()
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 * The metadata parsing object.
81 private final Metadata metadata
;
84 * Major CTF version number
89 * Minor CTF version number
101 private ByteOrder byteOrder
;
104 * Packet header structure declaration
106 private StructDeclaration packetHeaderDecl
;
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 HashMap
<Long
, Stream
> streams
;
122 * Collection of environment variables set by the tracer
124 private final HashMap
<String
, String
> environment
;
127 * Collection of all the clocks in a system.
129 private final HashMap
<String
, CTFClock
> clocks
;
131 /** FileChannels to the streams */
132 private final FileChannel
[] streamFileChannels
;
134 /** Handlers for the metadata files */
135 private final static FileFilter metadataFileFilter
= new MetadataFileFilter();
136 private final static Comparator
<File
> metadataComparator
= new MetadataComparator();
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
146 * Filesystem path of the trace directory.
147 * @throws IOException
149 public CTFTrace(String path
) throws CTFReaderException
{
150 this(new File(path
));
157 * Filesystem path of the trace directory.
158 * @throws CTFReaderException
160 public CTFTrace(File path
) throws CTFReaderException
{
162 this.metadata
= new Metadata(this);
164 if (!this.path
.isDirectory()) {
165 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
168 /* Set up the internal containers for this trace */
169 streams
= new HashMap
<Long
, Stream
>();
170 environment
= new HashMap
<String
, String
>();
171 clocks
= new HashMap
<String
, CTFClock
>();
173 /* Open and parse the metadata file */
176 if (Activator
.getDefault() != null) {
177 Activator
.getDefault().log(metadata
.toString());
180 /* Open all the trace files */
181 /* Create the definitions needed to read things from the files */
182 if (packetHeaderDecl
!= null) {
183 packetHeaderDef
= packetHeaderDecl
.createDefinition(this,
184 "packet.header"); //$NON-NLS-1$
187 /* List files not called metadata and not hidden. */
188 File
[] files
= path
.listFiles(metadataFileFilter
);
189 Arrays
.sort(files
, metadataComparator
);
191 /* Try to open each file */
192 streamFileChannels
= new FileChannel
[files
.length
];
193 for (int i
= 0; i
< files
.length
; i
++) {
194 openStreamInput(files
[i
], i
);
197 /* Create their index */
198 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
199 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
200 for (StreamInput s
: inputs
) {
207 protected void finalize() {
208 /* If this trace gets closed, release the descriptors to the streams */
209 for (FileChannel fc
: streamFileChannels
) {
213 } catch (IOException e
) {
219 // ------------------------------------------------------------------------
220 // Getters/Setters/Predicates
221 // ------------------------------------------------------------------------
224 * Method getStream gets the stream for a given id
227 * Long the id of the stream
228 * @return Stream the stream that we need
230 public Stream
getStream(Long id
) {
231 return streams
.get(id
);
235 * Method nbStreams gets the number of available streams
237 * @return int the number of streams
239 public int nbStreams() {
240 return streams
.size();
244 * Method setMajor sets the major version of the trace (DO NOT USE)
247 * long the major version
249 public void setMajor(long major
) {
254 * Method setMinor sets the minor version of the trace (DO NOT USE)
257 * long the minor version
259 public void setMinor(long minor
) {
264 * Method setUUID sets the UUID of a trace
269 public void setUUID(UUID uuid
) {
274 * Method setByteOrder sets the byte order
277 * ByteOrder of the trace, can be little-endian or big-endian
279 public void setByteOrder(ByteOrder byteOrder
) {
280 this.byteOrder
= byteOrder
;
284 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
286 * @param packetHeader
287 * StructDeclaration the header in structdeclaration form
289 public void setPacketHeader(StructDeclaration packetHeader
) {
290 this.packetHeaderDecl
= packetHeader
;
294 * Method majortIsSet is the major version number set?
296 * @return boolean is the major set?
298 public boolean majortIsSet() {
299 return major
!= null;
303 * Method minorIsSet. is the minor version number set?
305 * @return boolean is the minor set?
307 public boolean minorIsSet() {
308 return minor
!= null;
312 * Method UUIDIsSet is the UUID set?
314 * @return boolean is the UUID set?
316 public boolean UUIDIsSet() {
321 * Method byteOrderIsSet is the byteorder set?
323 * @return boolean is the byteorder set?
325 public boolean byteOrderIsSet() {
326 return byteOrder
!= null;
330 * Method packetHeaderIsSet is the packet header set?
332 * @return boolean is the packet header set?
334 public boolean packetHeaderIsSet() {
335 return packetHeaderDecl
!= null;
339 * Method getUUID gets the trace UUID
341 * @return UUID gets the trace UUID
343 public UUID
getUUID() {
348 * Method getMajor gets the trace major version
350 * @return long gets the trace major version
352 public long getMajor() {
357 * Method getMinor gets the trace minor version
359 * @return long gets the trace minor version
361 public long getMinor() {
366 * Method getByteOrder gets the trace byte order
368 * @return ByteOrder gets the trace byte order
370 public ByteOrder
getByteOrder() {
375 * Method getPacketHeader gets the trace packet header
377 * @return StructDeclaration gets the trace packet header
379 public StructDeclaration
getPacketHeader() {
380 return packetHeaderDecl
;
384 * Method getTraceDirectory gets the trace directory
386 * @return File the path in "File" format.
388 public File
getTraceDirectory() {
393 * Method getStreams get all the streams in a map format.
395 * @return Map<Long,Stream> a map of all the streams.
397 public Map
<Long
, Stream
> getStreams() {
402 * Method getPath gets the path of the trace directory
404 * @return String the path of the trace directory, in string format.
405 * @see java.io.File#getPath()
408 public String
getPath() {
409 return path
.getPath();
412 // ------------------------------------------------------------------------
414 // ------------------------------------------------------------------------
417 * Tries to open the given file, reads the first packet header of the file
418 * and check its validity.
421 * A trace file in the trace directory.
423 * Which index in the class' streamFileChannel array this file
425 * @throws CTFReaderException
427 private void openStreamInput(File streamFile
, int index
)
428 throws CTFReaderException
{
429 MappedByteBuffer byteBuffer
;
430 BitBuffer streamBitBuffer
;
432 if (!streamFile
.canRead()) {
433 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
434 + streamFile
.getPath());
438 /* Open the file and get the FileChannel */
439 streamFileChannels
[index
] = new FileInputStream(streamFile
).getChannel();
441 /* Map one memory page of 4 kiB */
442 byteBuffer
= streamFileChannels
[index
].map(MapMode
.READ_ONLY
, 0,
444 } catch (IOException e
) {
445 /* Shouldn't happen at this stage if every other check passed */
446 throw new CTFReaderException();
449 /* Create a BitBuffer with this mapping and the trace byte order */
450 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
452 if (packetHeaderDef
!= null) {
453 /* Read the packet header */
454 packetHeaderDef
.read(streamBitBuffer
);
456 /* Check the magic number */
457 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
.lookupDefinition("magic"); //$NON-NLS-1$
458 int magic
= (int) magicDef
.getValue();
459 if (magic
!= Utils
.CTF_MAGIC
) {
460 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
464 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
.lookupDefinition("uuid"); //$NON-NLS-1$
465 assert ((uuidDef
!= null) && (uuidDef
.getDeclaration().getLength() == Utils
.UUID_LEN
));
466 if (uuidDef
!= null) {
467 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
469 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
470 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
.getElem(i
);
471 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
474 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
476 if (!this.uuid
.equals(otheruuid
)) {
477 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
482 // TODO: it hasn't been checked that the stream_id field exists and
485 IntegerDefinition streamIDDef
= (IntegerDefinition
) packetHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
486 assert (streamIDDef
!= null);
488 long streamID
= streamIDDef
.getValue();
490 /* Get the stream to which this trace file belongs to */
491 Stream stream
= streams
.get(streamID
);
493 /* Create the stream input */
494 StreamInput streamInput
= new StreamInput(stream
,
495 streamFileChannels
[index
], streamFile
);
497 /* Add a reference to the streamInput in the stream */
498 stream
.addInput(streamInput
);
500 /* No packet header, we suppose there is only one stream */
501 Stream stream
= streams
.get(null);
503 /* Create the stream input */
504 StreamInput streamInput
= new StreamInput(stream
,
505 streamFileChannels
[index
], streamFile
);
507 /* Add a reference to the streamInput in the stream */
508 stream
.addInput(streamInput
);
513 * Looks up a definition from packet
518 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
521 public Definition
lookupDefinition(String lookupPath
) {
522 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
523 return packetHeaderDef
;
529 * Adds a new stream to the trace.
534 * @throws ParseException
536 public void addStream(Stream stream
) throws ParseException
{
539 * If there is already a stream without id (the null key), it must be
542 if (streams
.get(null) != null) {
543 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
547 * If the stream we try to add has the null key, it must be the only
548 * one. Thus, if the streams container is not empty, it is not valid.
550 if ((stream
.getId() == null) && (streams
.size() != 0)) {
551 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
554 /* If a stream with the same ID already exists, it is not valid. */
555 if (streams
.get(stream
.getId()) != null) {
556 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
559 /* It should be ok now. */
560 streams
.put(stream
.getId(), stream
);
563 public HashMap
<String
, String
> getEnvironment() {
567 public String
lookupEnvironment(String key
) {
568 return environment
.get(key
);
571 public void addEnvironmentVar(String varName
, String varValue
) {
572 environment
.put(varName
, varValue
);
575 public void addClock(String nameValue
, CTFClock ctfClock
) {
576 clocks
.put(nameValue
, ctfClock
);
579 public CTFClock
getClock(String name
) {
580 return clocks
.get(name
);
583 public CTFClock
getClock() {
584 if (clocks
.size() == 1) {
585 String key
= (String
) clocks
.keySet().toArray()[0];
586 return clocks
.get(key
);
591 public long getOffset() {
592 if (getClock() == null) {
595 return (Long
) getClock().getProperty("offset"); //$NON-NLS-1$
600 class MetadataFileFilter
implements FileFilter
{
603 public boolean accept(File pathname
) {
604 if (pathname
.isDirectory()) {
607 if (pathname
.isHidden()) {
610 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
618 class MetadataComparator
implements Comparator
<File
> {
621 public int compare(File o1
, File o2
) {
622 return o1
.getName().compareTo(o2
.getName());