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
= new HashMap
<Long
, Stream
>();
122 * Collection of environment variables set by the tracer
124 private final HashMap
<String
, String
> environment
= new HashMap
<String
, String
>();
127 * Collection of all the clocks in a system.
129 private final HashMap
<String
, CTFClock
> clocks
= new HashMap
<String
, CTFClock
>();
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
140 * Filesystem path of the trace directory.
141 * @throws IOException
143 public CTFTrace(String path
) throws CTFReaderException
{
144 this(new File(path
));
151 * Filesystem path of the trace directory.
152 * @throws CTFReaderException
154 @SuppressWarnings("unqualified-field-access")
155 public CTFTrace(File path
) throws CTFReaderException
{
158 metadata
= new Metadata(this);
160 if (!this.path
.isDirectory()) {
161 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
167 // ------------------------------------------------------------------------
168 // Getters/Setters/Predicates
169 // ------------------------------------------------------------------------
172 * Method getStream gets the stream for a given id
175 * Long the id of the stream
176 * @return Stream the stream that we need
178 public Stream
getStream(Long id
) {
179 return streams
.get(id
);
183 * Method nbStreams gets the number of available streams
185 * @return int the number of streams
187 public int nbStreams() {
188 return streams
.size();
192 * Method setMajor sets the major version of the trace (DO NOT USE)
195 * long the major version
197 public void setMajor(long major
) {
202 * Method setMinor sets the minor version of the trace (DO NOT USE)
205 * long the minor version
207 public void setMinor(long minor
) {
212 * Method setUUID sets the UUID of a trace
217 public void setUUID(UUID uuid
) {
222 * Method setByteOrder sets the byte order
225 * ByteOrder of the trace, can be little-endian or big-endian
227 public void setByteOrder(ByteOrder byteOrder
) {
228 this.byteOrder
= byteOrder
;
232 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
234 * @param packetHeader
235 * StructDeclaration the header in structdeclaration form
237 public void setPacketHeader(StructDeclaration packetHeader
) {
238 this.packetHeaderDecl
= packetHeader
;
242 * Method majortIsSet is the major version number set?
244 * @return boolean is the major set?
246 public boolean majortIsSet() {
247 return major
!= null;
251 * Method minorIsSet. is the minor version number set?
253 * @return boolean is the minor set?
255 public boolean minorIsSet() {
256 return minor
!= null;
260 * Method UUIDIsSet is the UUID set?
262 * @return boolean is the UUID set?
264 public boolean UUIDIsSet() {
269 * Method byteOrderIsSet is the byteorder set?
271 * @return boolean is the byteorder set?
273 public boolean byteOrderIsSet() {
274 return byteOrder
!= null;
278 * Method packetHeaderIsSet is the packet header set?
280 * @return boolean is the packet header set?
282 public boolean packetHeaderIsSet() {
283 return packetHeaderDecl
!= null;
287 * Method getUUID gets the trace UUID
289 * @return UUID gets the trace UUID
291 public UUID
getUUID() {
296 * Method getMajor gets the trace major version
298 * @return long gets the trace major version
300 public long getMajor() {
305 * Method getMinor gets the trace minor version
307 * @return long gets the trace minor version
309 public long getMinor() {
314 * Method getByteOrder gets the trace byte order
316 * @return ByteOrder gets the trace byte order
318 public ByteOrder
getByteOrder() {
323 * Method getPacketHeader gets the trace packet header
325 * @return StructDeclaration gets the trace packet header
327 public StructDeclaration
getPacketHeader() {
328 return packetHeaderDecl
;
332 * Method getTraceDirectory gets the trace directory
334 * @return File the path in "File" format.
336 public File
getTraceDirectory() {
341 * Method getStreams get all the streams in a map format.
343 * @return Map<Long,Stream> a map of all the streams.
345 public Map
<Long
, Stream
> getStreams() {
350 * Method getPath gets the path of the trace directory
352 * @return String the path of the trace directory, in string format.
353 * @see java.io.File#getPath()
356 public String
getPath() {
357 return path
.getPath();
360 // ------------------------------------------------------------------------
362 // ------------------------------------------------------------------------
365 * Opens the trace and creates the index.
367 * @throws CTFReaderException
369 private void open() throws CTFReaderException
{
370 /* Open and parse the metadata file */
373 if (Activator
.getDefault() != null) {
374 Activator
.getDefault().log(metadata
.toString());
376 /* Open all the trace files */
379 /* Create their index */
380 createStreamInputIndexes();
384 * Parses the metadata
386 * @throws CTFReaderException
388 private void openTraceMetadata() throws CTFReaderException
{
393 * Creates the definitions needed by the Trace class to open the trace
396 private void createDefinitions() {
397 if (packetHeaderDecl
!= null) {
398 packetHeaderDef
= packetHeaderDecl
.createDefinition(this,
399 "packet.header"); //$NON-NLS-1$
404 * Creates the indexes of all the trace files.
406 * @throws CTFReaderException
408 private void createStreamInputIndexes() throws CTFReaderException
{
409 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
410 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
411 for (StreamInput s
: inputs
) {
418 * Tries to open every file in the trace directory (except metadata).
420 * @throws CTFReaderException
422 private void openStreamInputs() throws CTFReaderException
{
423 /* Create the definitions needed to read things from the files */
426 /* List files not called metadata and not hidden. */
427 File
[] files
= path
.listFiles(new FileFilter() {
430 public boolean accept(File pathname
) {
432 if (pathname
.isDirectory()) {
436 if (pathname
.isHidden()) {
440 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
447 Arrays
.sort(files
, new Comparator
<File
>() {
450 public int compare(File o1
, File o2
) {
452 return o1
.getName().compareTo(o2
.getName());
457 /* Try to open each file */
458 for (File s
: files
) {
464 * Tries to open the given file, reads the first packet header of the file
465 * and check its validity.
468 * A trace file in the trace directory.
469 * @throws CTFReaderException
471 private void openStreamInput(File streamFile
) throws CTFReaderException
{
472 FileChannel streamFileChannel
;
473 MappedByteBuffer byteBuffer
;
474 BitBuffer streamBitBuffer
;
476 if (!streamFile
.canRead()) {
477 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
478 + streamFile
.getPath());
482 /* Open the file and get the FileChannel */
483 streamFileChannel
= new FileInputStream(streamFile
).getChannel();
485 /* Map one memory page of 4 kiB */
486 byteBuffer
= streamFileChannel
.map(MapMode
.READ_ONLY
, 0, 4096);
487 } catch (IOException e
) {
488 /* Shouldn't happen at this stage if every other check passed */
489 throw new CTFReaderException();
492 /* Create a BitBuffer with this mapping and the trace byte order */
493 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
495 if (packetHeaderDef
!= null) {
496 /* Read the packet header */
497 packetHeaderDef
.read(streamBitBuffer
);
499 /* Check the magic number */
500 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
.lookupDefinition("magic"); //$NON-NLS-1$
501 int magic
= (int) magicDef
.getValue();
502 if (magic
!= Utils
.CTF_MAGIC
) {
503 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
507 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
.lookupDefinition("uuid"); //$NON-NLS-1$
508 assert ((uuidDef
!= null) && (uuidDef
.getDeclaration().getLength() == Utils
.UUID_LEN
));
509 if (uuidDef
!= null) {
510 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
512 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
513 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
.getElem(i
);
514 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
517 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
519 if (!this.uuid
.equals(otheruuid
)) {
520 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
525 // TODO: it hasn't been checked that the stream_id field exists and
528 IntegerDefinition streamIDDef
= (IntegerDefinition
) packetHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
529 assert (streamIDDef
!= null);
531 long streamID
= streamIDDef
.getValue();
533 /* Get the stream to which this trace file belongs to */
534 Stream stream
= streams
.get(streamID
);
536 /* Create the stream input */
537 StreamInput streamInput
= new StreamInput(stream
,
538 streamFileChannel
, streamFile
);
540 /* Add a reference to the streamInput in the stream */
541 stream
.addInput(streamInput
);
543 /* No packet header, we suppose there is only one stream */
544 Stream stream
= streams
.get(null);
546 /* Create the stream input */
547 StreamInput streamInput
= new StreamInput(stream
,
548 streamFileChannel
, streamFile
);
550 /* Add a reference to the streamInput in the stream */
551 stream
.addInput(streamInput
);
556 * Looks up a definition from packet
561 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
564 public Definition
lookupDefinition(String lookupPath
) {
565 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
566 return packetHeaderDef
;
572 * Adds a new stream to the trace.
577 * @throws ParseException
579 public void addStream(Stream stream
) throws ParseException
{
582 * If there is already a stream without id (the null key), it must be
585 if (streams
.get(null) != null) {
586 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
590 * If the stream we try to add has the null key, it must be the only
591 * one. Thus, if the streams container is not empty, it is not valid.
593 if ((stream
.getId() == null) && (streams
.size() != 0)) {
594 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
597 /* If a stream with the same ID already exists, it is not valid. */
598 if (streams
.get(stream
.getId()) != null) {
599 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
602 /* It should be ok now. */
603 streams
.put(stream
.getId(), stream
);
606 public HashMap
<String
, String
> getEnvironment() {
610 public String
lookupEnvironment( String key
)
612 return environment
.get(key
);
615 public void addEnvironmentVar( String varName
, String varValue
)
617 environment
.put(varName
, varValue
);
620 public void addClock(String nameValue
, CTFClock ctfClock
) {
621 clocks
.put(nameValue
, ctfClock
);
624 public CTFClock
getClock(String name
){
625 return clocks
.get(name
);
628 public CTFClock
getClock(){
629 if( clocks
.size() == 1 )
631 String key
= (String
) clocks
.keySet().toArray()[0];
632 return clocks
.get(key
);
637 public long getOffset() {
638 if(getClock() == null )
642 return (Long
) getClock().getProperty("offset"); //$NON-NLS-1$