ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors:
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 *******************************************************************************/
14
15 package org.eclipse.linuxtools.ctf.core.trace;
16
17 import java.io.File;
18 import java.io.FileFilter;
19 import java.io.IOException;
20 import java.io.Serializable;
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 import java.nio.channels.FileChannel;
24 import java.nio.channels.FileChannel.MapMode;
25 import java.nio.file.StandardOpenOption;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeSet;
34 import java.util.UUID;
35
36 import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
37 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
38 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
39 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
40 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
41 import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
42 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
43 import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
44 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
45 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
46 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
47 import org.eclipse.linuxtools.internal.ctf.core.SafeMappedByteBuffer;
48 import org.eclipse.linuxtools.internal.ctf.core.event.CTFCallsiteComparator;
49 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
50 import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition;
51
52 /**
53 * A CTF trace on the file system.
54 *
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.
59 *
60 * @author Matthew Khouzam
61 * @version $Revision: 1.0 $
62 */
63 public class CTFTrace implements IDefinitionScope, AutoCloseable {
64
65 @Override
66 public String toString() {
67 /* Only for debugging, shouldn't be externalized */
68 return "CTFTrace [path=" + fPath + ", major=" + fMajor + ", minor=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69 + fMinor + ", uuid=" + fUuid + "]"; //$NON-NLS-1$ //$NON-NLS-2$
70 }
71
72 /**
73 * The trace directory on the filesystem.
74 */
75 private final File fPath;
76
77 /**
78 * Major CTF version number
79 */
80 private Long fMajor;
81
82 /**
83 * Minor CTF version number
84 */
85 private Long fMinor;
86
87 /**
88 * Trace UUID
89 */
90 private UUID fUuid;
91
92 /**
93 * Trace byte order
94 */
95 private ByteOrder fByteOrder;
96
97 /**
98 * Packet header structure declaration
99 */
100 private StructDeclaration fPacketHeaderDecl = null;
101
102 /**
103 * The clock of the trace
104 */
105 private CTFClock fSingleClock = null;
106
107 /**
108 * Packet header structure definition
109 *
110 * This is only used when opening the trace files, to read the first packet
111 * header and see if they are valid trace files.
112 */
113 private StructDefinition fPacketHeaderDef;
114
115 /**
116 * Collection of streams contained in the trace.
117 */
118 private final Map<Long, CTFStream> fStreams = new HashMap<>();
119
120 /**
121 * Collection of environment variables set by the tracer
122 */
123 private final Map<String, String> fEnvironment = new HashMap<>();
124
125 /**
126 * Collection of all the clocks in a system.
127 */
128 private final Map<String, CTFClock> fClocks = new HashMap<>();
129
130 /** Handlers for the metadata files */
131 private static final FileFilter METADATA_FILE_FILTER = new MetadataFileFilter();
132 private static final Comparator<File> METADATA_COMPARATOR = new MetadataComparator();
133
134 /** Callsite helpers */
135 private CTFCallsiteComparator fCtfCallsiteComparator = new CTFCallsiteComparator();
136
137 private Map<String, TreeSet<CTFCallsite>> fCallsitesByName = new HashMap<>();
138
139 /** Callsite helpers */
140 private TreeSet<CTFCallsite> fCallsitesByIP = new TreeSet<>();
141
142 // ------------------------------------------------------------------------
143 // Constructors
144 // ------------------------------------------------------------------------
145
146 /**
147 * Trace constructor.
148 *
149 * @param path
150 * Filesystem path of the trace directory
151 * @throws CTFReaderException
152 * If no CTF trace was found at the path
153 */
154 public CTFTrace(String path) throws CTFReaderException {
155 this(new File(path));
156
157 }
158
159 /**
160 * Trace constructor.
161 *
162 * @param path
163 * Filesystem path of the trace directory.
164 * @throws CTFReaderException
165 * If no CTF trace was found at the path
166 */
167 public CTFTrace(File path) throws CTFReaderException {
168 fPath = path;
169 final Metadata metadata = new Metadata(this);
170
171 /* Set up the internal containers for this trace */
172 if (!fPath.exists()) {
173 throw new CTFReaderException("Trace (" + path.getPath() + ") doesn't exist. Deleted or moved?"); //$NON-NLS-1$ //$NON-NLS-2$
174 }
175
176 if (!fPath.isDirectory()) {
177 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
178 }
179
180 /* Open and parse the metadata file */
181 metadata.parseFile();
182
183 init(path);
184 }
185
186 /**
187 * Streamed constructor
188 *
189 * @since 3.0
190 */
191 public CTFTrace() {
192 fPath = null;
193 }
194
195 private void init(File path) throws CTFReaderException {
196
197 /* Open all the trace files */
198
199 /* List files not called metadata and not hidden. */
200 File[] files = path.listFiles(METADATA_FILE_FILTER);
201 Arrays.sort(files, METADATA_COMPARATOR);
202
203 /* Try to open each file */
204 for (File streamFile : files) {
205 openStreamInput(streamFile);
206 }
207
208 /* Create their index */
209 for (CTFStream stream : getStreams()) {
210 Set<CTFStreamInput> inputs = stream.getStreamInputs();
211 for (CTFStreamInput s : inputs) {
212 addStream(s);
213 }
214 }
215 }
216
217 /**
218 * Dispose the trace
219 *
220 * FIXME Not needed anymore, class doesn't need to be AutoCloseable.
221 *
222 * @since 3.0
223 */
224 @Override
225 public void close() {
226 }
227
228 // ------------------------------------------------------------------------
229 // Getters/Setters/Predicates
230 // ------------------------------------------------------------------------
231
232 /**
233 * Gets an event declaration hash map for a given streamID
234 *
235 * @param streamId
236 * The ID of the stream from which to read
237 * @return The Hash map with the event declarations
238 * @since 2.0
239 * @deprecated use {@link CTFTrace#getEventDeclarations(Long)}
240 */
241 @Deprecated
242 public Map<Long, IEventDeclaration> getEvents(Long streamId) {
243 return fStreams.get(streamId).getEvents();
244 }
245
246 /**
247 * Gets an event declaration list for a given streamID
248 *
249 * @param streamId
250 * The ID of the stream from which to read
251 * @return The list of event declarations
252 * @since 3.2
253 */
254 public Collection<IEventDeclaration> getEventDeclarations(Long streamId) {
255 return fStreams.get(streamId).getEventDeclarations();
256 }
257
258 /**
259 * Get an event by it's ID
260 *
261 * @param streamId
262 * The ID of the stream from which to read
263 * @param id
264 * the ID of the event
265 * @return the event declaration
266 * @since 2.0
267 * @deprecated use {@link CTFTrace#getEventType(long, int)} instead
268 */
269 @Deprecated
270 public IEventDeclaration getEventType(long streamId, long id) {
271 return getStream(streamId).getEventDeclaration((int) id);
272 }
273
274 /**
275 * Get an event by it's ID
276 *
277 * @param streamId
278 * The ID of the stream from which to read
279 * @param id
280 * the ID of the event
281 * @return the event declaration
282 * @since 3.2
283 */
284 public IEventDeclaration getEventType(long streamId, int id) {
285 return getEvents(streamId).get(id);
286 }
287
288 /**
289 * Method getStream gets the stream for a given id
290 *
291 * @param id
292 * Long the id of the stream
293 * @return Stream the stream that we need
294 * @since 3.0
295 */
296 public CTFStream getStream(Long id) {
297 return fStreams.get(id);
298 }
299
300 /**
301 * Method nbStreams gets the number of available streams
302 *
303 * @return int the number of streams
304 */
305 public int nbStreams() {
306 return fStreams.size();
307 }
308
309 /**
310 * Method setMajor sets the major version of the trace (DO NOT USE)
311 *
312 * @param major
313 * long the major version
314 */
315 public void setMajor(long major) {
316 fMajor = major;
317 }
318
319 /**
320 * Method setMinor sets the minor version of the trace (DO NOT USE)
321 *
322 * @param minor
323 * long the minor version
324 */
325 public void setMinor(long minor) {
326 fMinor = minor;
327 }
328
329 /**
330 * Method setUUID sets the UUID of a trace
331 *
332 * @param uuid
333 * UUID
334 */
335 public void setUUID(UUID uuid) {
336 fUuid = uuid;
337 }
338
339 /**
340 * Method setByteOrder sets the byte order
341 *
342 * @param byteOrder
343 * ByteOrder of the trace, can be little-endian or big-endian
344 */
345 public void setByteOrder(ByteOrder byteOrder) {
346 fByteOrder = byteOrder;
347 }
348
349 /**
350 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
351 *
352 * @param packetHeader
353 * StructDeclaration the header in structdeclaration form
354 */
355 public void setPacketHeader(StructDeclaration packetHeader) {
356 fPacketHeaderDecl = packetHeader;
357 }
358
359 /**
360 * Method majorIsSet is the major version number set?
361 *
362 * @return boolean is the major set?
363 * @since 3.0
364 */
365 public boolean majorIsSet() {
366 return fMajor != null;
367 }
368
369 /**
370 * Method minorIsSet. is the minor version number set?
371 *
372 * @return boolean is the minor set?
373 */
374 public boolean minorIsSet() {
375 return fMinor != null;
376 }
377
378 /**
379 * Method UUIDIsSet is the UUID set?
380 *
381 * @return boolean is the UUID set?
382 * @since 2.0
383 */
384 public boolean uuidIsSet() {
385 return fUuid != null;
386 }
387
388 /**
389 * Method byteOrderIsSet is the byteorder set?
390 *
391 * @return boolean is the byteorder set?
392 */
393 public boolean byteOrderIsSet() {
394 return fByteOrder != null;
395 }
396
397 /**
398 * Method packetHeaderIsSet is the packet header set?
399 *
400 * @return boolean is the packet header set?
401 */
402 public boolean packetHeaderIsSet() {
403 return fPacketHeaderDecl != null;
404 }
405
406 /**
407 * Method getUUID gets the trace UUID
408 *
409 * @return UUID gets the trace UUID
410 */
411 public UUID getUUID() {
412 return fUuid;
413 }
414
415 /**
416 * Method getMajor gets the trace major version
417 *
418 * @return long gets the trace major version
419 */
420 public long getMajor() {
421 return fMajor;
422 }
423
424 /**
425 * Method getMinor gets the trace minor version
426 *
427 * @return long gets the trace minor version
428 */
429 public long getMinor() {
430 return fMinor;
431 }
432
433 /**
434 * Method getByteOrder gets the trace byte order
435 *
436 * @return ByteOrder gets the trace byte order
437 */
438 public final ByteOrder getByteOrder() {
439 return fByteOrder;
440 }
441
442 /**
443 * Method getPacketHeader gets the trace packet header
444 *
445 * @return StructDeclaration gets the trace packet header
446 */
447 public StructDeclaration getPacketHeader() {
448 return fPacketHeaderDecl;
449 }
450
451 /**
452 * Method getTraceDirectory gets the trace directory
453 *
454 * @return File the path in "File" format.
455 */
456 public File getTraceDirectory() {
457 return fPath;
458 }
459
460 /**
461 * Get all the streams as an iterable.
462 *
463 * @return Iterable&lt;Stream&gt; an iterable over streams.
464 * @since 3.0
465 */
466 public Iterable<CTFStream> getStreams() {
467 return fStreams.values();
468 }
469
470 /**
471 * Method getPath gets the path of the trace directory
472 *
473 * @return String the path of the trace directory, in string format.
474 * @see java.io.File#getPath()
475 */
476 public String getPath() {
477 return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$
478 }
479
480 // ------------------------------------------------------------------------
481 // Operations
482 // ------------------------------------------------------------------------
483
484 private void addStream(CTFStreamInput s) {
485
486 /*
487 * add the stream
488 */
489 CTFStream stream = s.getStream();
490 fStreams.put(stream.getId(), stream);
491
492 /*
493 * index the trace
494 */
495 s.setupIndex();
496 }
497
498 /**
499 * Tries to open the given file, reads the first packet header of the file
500 * and check its validity. This will add a file to a stream as a streaminput
501 *
502 * @param streamFile
503 * A trace file in the trace directory.
504 * @param index
505 * Which index in the class' streamFileChannel array this file
506 * must use
507 * @throws CTFReaderException
508 * if there is a file error
509 */
510 private CTFStream openStreamInput(File streamFile) throws CTFReaderException {
511 ByteBuffer byteBuffer;
512 BitBuffer streamBitBuffer;
513 CTFStream stream;
514
515 if (!streamFile.canRead()) {
516 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
517 + streamFile.getPath());
518 }
519
520 try (FileChannel fc = FileChannel.open(streamFile.toPath(), StandardOpenOption.READ)) {
521 /* Map one memory page of 4 kiB */
522 byteBuffer = SafeMappedByteBuffer.map(fc, MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
523 if (byteBuffer == null) {
524 throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
525 }
526 /* Create a BitBuffer with this mapping and the trace byte order */
527 streamBitBuffer = new BitBuffer(byteBuffer, this.getByteOrder());
528
529 if (fPacketHeaderDecl != null) {
530 /* Read the packet header */
531 fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, LexicalScope.PACKET_HEADER, streamBitBuffer);
532 }
533 } catch (IOException e) {
534 /* Shouldn't happen at this stage if every other check passed */
535 throw new CTFReaderException(e);
536 }
537 if (fPacketHeaderDef != null) {
538 validateMagicNumber(fPacketHeaderDef);
539
540 validateUUID(fPacketHeaderDef);
541
542 /* Read the stream ID */
543 IDefinition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
544
545 if (streamIDDef instanceof IntegerDefinition) {
546 /* This doubles as a null check */
547 long streamID = ((IntegerDefinition) streamIDDef).getValue();
548 stream = fStreams.get(streamID);
549 } else {
550 /* No stream_id in the packet header */
551 stream = fStreams.get(null);
552 }
553
554 } else {
555 /* No packet header, we suppose there is only one stream */
556 stream = fStreams.get(null);
557 }
558
559 if (stream == null) {
560 throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$
561 }
562
563 /*
564 * Create the stream input and add a reference to the streamInput in the
565 * stream.
566 */
567 stream.addInput(new CTFStreamInput(stream, streamFile));
568 return stream;
569 }
570
571 private void validateUUID(StructDefinition packetHeaderDef) throws CTFReaderException {
572 IDefinition lookupDefinition = packetHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
573 ArrayDefinition uuidDef = (ArrayDefinition) lookupDefinition;
574 if (uuidDef != null) {
575 UUID otheruuid = Utils.getUUIDfromDefinition(uuidDef);
576 if (!fUuid.equals(otheruuid)) {
577 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
578 }
579 }
580 }
581
582 private static void validateMagicNumber(StructDefinition packetHeaderDef) throws CTFReaderException {
583 IntegerDefinition magicDef = (IntegerDefinition) packetHeaderDef.lookupDefinition("magic"); //$NON-NLS-1$
584 int magic = (int) magicDef.getValue();
585 if (magic != Utils.CTF_MAGIC) {
586 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
587 }
588 }
589
590 // ------------------------------------------------------------------------
591 // IDefinitionScope
592 // ------------------------------------------------------------------------
593
594 /**
595 * @since 3.0
596 */
597 @Override
598 public LexicalScope getScopePath() {
599 return LexicalScope.TRACE;
600 }
601
602 /**
603 * Looks up a definition from packet
604 *
605 * @param lookupPath
606 * String
607 * @return Definition
608 * @see org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope#lookupDefinition(String)
609 */
610 @Override
611 public Definition lookupDefinition(String lookupPath) {
612 if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) {
613 return fPacketHeaderDef;
614 }
615 return null;
616 }
617
618 // ------------------------------------------------------------------------
619 // Live trace reading
620 // ------------------------------------------------------------------------
621
622 /**
623 * Add a new stream file to support new streams while the trace is being
624 * read.
625 *
626 * @param streamFile
627 * the file of the stream
628 * @throws CTFReaderException
629 * A stream had an issue being read
630 * @since 3.0
631 */
632 public void addStreamFile(File streamFile) throws CTFReaderException {
633 openStreamInput(streamFile);
634 }
635
636 /**
637 * Registers a new stream to the trace.
638 *
639 * @param stream
640 * A stream object.
641 * @throws ParseException
642 * If there was some problem reading the metadata
643 * @since 3.0
644 */
645 public void addStream(CTFStream stream) throws ParseException {
646 /*
647 * If there is already a stream without id (the null key), it must be
648 * the only one
649 */
650 if (fStreams.get(null) != null) {
651 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
652 }
653
654 /*
655 * If the stream we try to add has the null key, it must be the only
656 * one. Thus, if the streams container is not empty, it is not valid.
657 */
658 if ((stream.getId() == null) && (fStreams.size() != 0)) {
659 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
660 }
661
662 /*
663 * If a stream with the same ID already exists, it is not valid.
664 */
665 CTFStream existingStream = fStreams.get(stream.getId());
666 if (existingStream != null) {
667 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
668 }
669
670 /* This stream is valid and has a unique id. */
671 fStreams.put(stream.getId(), stream);
672 }
673
674 /**
675 * Gets the Environment variables from the trace metadata (See CTF spec)
676 *
677 * @return The environment variables in the form of an unmodifiable map
678 * (key, value)
679 * @since 2.0
680 */
681 public Map<String, String> getEnvironment() {
682 return Collections.unmodifiableMap(fEnvironment);
683 }
684
685 /**
686 * Add a variable to the environment variables
687 *
688 * @param varName
689 * the name of the variable
690 * @param varValue
691 * the value of the variable
692 */
693 public void addEnvironmentVar(String varName, String varValue) {
694 fEnvironment.put(varName, varValue);
695 }
696
697 /**
698 * Add a clock to the clock list
699 *
700 * @param nameValue
701 * the name of the clock (full name with scope)
702 * @param ctfClock
703 * the clock
704 */
705 public void addClock(String nameValue, CTFClock ctfClock) {
706 fClocks.put(nameValue, ctfClock);
707 }
708
709 /**
710 * gets the clock with a specific name
711 *
712 * @param name
713 * the name of the clock.
714 * @return the clock
715 */
716 public CTFClock getClock(String name) {
717 return fClocks.get(name);
718 }
719
720 /**
721 * gets the clock if there is only one. (this is 100% of the use cases as of
722 * June 2012)
723 *
724 * @return the clock
725 */
726 public final CTFClock getClock() {
727 if (fSingleClock != null && fClocks.size() == 1) {
728 return fSingleClock;
729 }
730 if (fClocks.size() == 1) {
731 fSingleClock = fClocks.get(fClocks.keySet().iterator().next());
732 return fSingleClock;
733 }
734 return null;
735 }
736
737 /**
738 * gets the time offset of a clock with respect to UTC in nanoseconds
739 *
740 * @return the time offset of a clock with respect to UTC in nanoseconds
741 */
742 public final long getOffset() {
743 if (getClock() == null) {
744 return 0;
745 }
746 return fSingleClock.getClockOffset();
747 }
748
749 /**
750 * gets the time offset of a clock with respect to UTC in nanoseconds
751 *
752 * @return the time offset of a clock with respect to UTC in nanoseconds
753 */
754 private double getTimeScale() {
755 if (getClock() == null) {
756 return 1.0;
757 }
758 return fSingleClock.getClockScale();
759 }
760
761 /**
762 * Gets the current first packet start time
763 *
764 * @return the current start time
765 * @since 3.0
766 */
767 public long getCurrentStartTime() {
768 long currentStart = Long.MAX_VALUE;
769 for (CTFStream stream : fStreams.values()) {
770 for (CTFStreamInput si : stream.getStreamInputs()) {
771 currentStart = Math.min(currentStart, si.getIndex().getEntries().get(0).getTimestampBegin());
772 }
773 }
774 return timestampCyclesToNanos(currentStart);
775 }
776
777 /**
778 * Gets the current last packet end time
779 *
780 * @return the current end time
781 * @since 3.0
782 */
783 public long getCurrentEndTime() {
784 long currentEnd = Long.MIN_VALUE;
785 for (CTFStream stream : fStreams.values()) {
786 for (CTFStreamInput si : stream.getStreamInputs()) {
787 currentEnd = Math.max(currentEnd, si.getTimestampEnd());
788 }
789 }
790 return timestampCyclesToNanos(currentEnd);
791 }
792
793 /**
794 * Does the trace need to time scale?
795 *
796 * @return if the trace is in ns or cycles.
797 */
798 private boolean clockNeedsScale() {
799 if (getClock() == null) {
800 return false;
801 }
802 return fSingleClock.isClockScaled();
803 }
804
805 /**
806 * the inverse clock for returning to a scale.
807 *
808 * @return 1.0 / scale
809 */
810 private double getInverseTimeScale() {
811 if (getClock() == null) {
812 return 1.0;
813 }
814 return fSingleClock.getClockAntiScale();
815 }
816
817 /**
818 * @param cycles
819 * clock cycles since boot
820 * @return time in nanoseconds UTC offset
821 * @since 2.0
822 */
823 public long timestampCyclesToNanos(long cycles) {
824 long retVal = cycles + getOffset();
825 /*
826 * this fix is since quite often the offset will be > than 53 bits and
827 * therefore the conversion will be lossy
828 */
829 if (clockNeedsScale()) {
830 retVal = (long) (retVal * getTimeScale());
831 }
832 return retVal;
833 }
834
835 /**
836 * @param nanos
837 * time in nanoseconds UTC offset
838 * @return clock cycles since boot.
839 * @since 2.0
840 */
841 public long timestampNanoToCycles(long nanos) {
842 long retVal;
843 /*
844 * this fix is since quite often the offset will be > than 53 bits and
845 * therefore the conversion will be lossy
846 */
847 if (clockNeedsScale()) {
848 retVal = (long) (nanos * getInverseTimeScale());
849 } else {
850 retVal = nanos;
851 }
852 return retVal - getOffset();
853 }
854
855 /**
856 * Adds a callsite
857 *
858 * @param eventName
859 * the event name of the callsite
860 * @param funcName
861 * the name of the callsite function
862 * @param ip
863 * the ip of the callsite
864 * @param fileName
865 * the filename of the callsite
866 * @param lineNumber
867 * the line number of the callsite
868 */
869 public void addCallsite(String eventName, String funcName, long ip,
870 String fileName, long lineNumber) {
871 final CTFCallsite cs = new CTFCallsite(eventName, funcName, ip,
872 fileName, lineNumber);
873 TreeSet<CTFCallsite> csl = fCallsitesByName.get(eventName);
874 if (csl == null) {
875 csl = new TreeSet<>(fCtfCallsiteComparator);
876 fCallsitesByName.put(eventName, csl);
877 }
878
879 csl.add(cs);
880
881 fCallsitesByIP.add(cs);
882 }
883
884 /**
885 * Gets the set of callsites associated to an event name. O(1)
886 *
887 * @param eventName
888 * the event name
889 * @return the callsite set can be empty
890 * @since 3.0
891 */
892 public TreeSet<CTFCallsite> getCallsiteCandidates(String eventName) {
893 TreeSet<CTFCallsite> retVal = fCallsitesByName.get(eventName);
894 if (retVal == null) {
895 retVal = new TreeSet<>(fCtfCallsiteComparator);
896 }
897 return retVal;
898 }
899
900 /**
901 * The I'm feeling lucky of getCallsiteCandidates O(1)
902 *
903 * @param eventName
904 * the event name
905 * @return the first callsite that has that event name, can be null
906 * @since 1.2
907 */
908 public CTFCallsite getCallsite(String eventName) {
909 TreeSet<CTFCallsite> callsites = fCallsitesByName.get(eventName);
910 if (callsites != null) {
911 return callsites.first();
912 }
913 return null;
914 }
915
916 /**
917 * Gets a callsite from the instruction pointer O(log(n))
918 *
919 * @param ip
920 * the instruction pointer to lookup
921 * @return the callsite just before that IP in the list remember the IP is
922 * backwards on X86, can be null if no callsite is before the IP.
923 * @since 1.2
924 */
925 public CTFCallsite getCallsite(long ip) {
926 CTFCallsite cs = new CTFCallsite(null, null, ip, null, 0L);
927 return fCallsitesByIP.ceiling(cs);
928 }
929
930 /**
931 * Gets a callsite using the event name and instruction pointer O(log(n))
932 *
933 * @param eventName
934 * the name of the event
935 * @param ip
936 * the instruction pointer
937 * @return the closest matching callsite, can be null
938 */
939 public CTFCallsite getCallsite(String eventName, long ip) {
940 final TreeSet<CTFCallsite> candidates = fCallsitesByName.get(eventName);
941 if (candidates == null) {
942 return null;
943 }
944 final CTFCallsite dummyCs = new CTFCallsite(null, null, ip, null, -1);
945 final CTFCallsite callsite = candidates.ceiling(dummyCs);
946 if (callsite == null) {
947 return candidates.floor(dummyCs);
948 }
949 return callsite;
950 }
951
952 /**
953 * Add a new stream
954 *
955 * @param id
956 * the ID of the stream
957 * @param streamFile
958 * new file in the stream
959 * @throws CTFReaderException
960 * The file must exist
961 * @since 3.0
962 */
963 // TODO: remove suppress warning
964 @SuppressWarnings("resource")
965 public void addStream(long id, File streamFile) throws CTFReaderException {
966 CTFStream stream = null;
967 final File file = streamFile;
968 if (file == null) {
969 throw new CTFReaderException("cannot create a stream with no file"); //$NON-NLS-1$
970 }
971 if (fStreams.containsKey(id)) {
972 stream = fStreams.get(id);
973 } else {
974 stream = new CTFStream(this);
975 fStreams.put(id, stream);
976 }
977 stream.addInput(new CTFStreamInput(stream, file));
978 }
979 }
980
981 class MetadataFileFilter implements FileFilter {
982
983 @Override
984 public boolean accept(File pathname) {
985 if (pathname.isDirectory()) {
986 return false;
987 }
988 if (pathname.isHidden()) {
989 return false;
990 }
991 if (pathname.getName().equals("metadata")) { //$NON-NLS-1$
992 return false;
993 }
994 return true;
995 }
996
997 }
998
999 class MetadataComparator implements Comparator<File>, Serializable {
1000
1001 private static final long serialVersionUID = 1L;
1002
1003 @Override
1004 public int compare(File o1, File o2) {
1005 return o1.getName().compareTo(o2.getName());
1006 }
1007 }
This page took 0.051985 seconds and 5 git commands to generate.