46eaa665b1cdd34677869e0db00c5026f0feb183
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFTraceWriter.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.trace;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.tracecompass.ctf.core.CTFException;
24
25 /**
26 * A CTF trace reader. Reads the events of a trace.
27 *
28 * @author Bernd Hufmann
29 * @since 1.0
30 */
31 public class CTFTraceWriter {
32
33 // ------------------------------------------------------------------------
34 // Attributes
35 // ------------------------------------------------------------------------
36
37 /**
38 * The trace to read from.
39 */
40 @Nullable private final CTFTrace fInTrace;
41
42 // ------------------------------------------------------------------------
43 // Constructors
44 // ------------------------------------------------------------------------
45
46 /**
47 * Constructs a TraceReader to read a trace.
48 *
49 * @param trace
50 * The trace to read from.
51 * @throws CTFException
52 * if an error occurs
53 */
54 public CTFTraceWriter(@NonNull CTFTrace trace) throws CTFException {
55 fInTrace = trace;
56 try (CTFTraceReader fTraceReader = new CTFTraceReader(fInTrace)) {
57 fTraceReader.populateIndex();
58 }
59 }
60
61 /**
62 * Copies packets from the relevant input to the output trace based
63 * on a given time range. The following condition has to be met so that
64 * a packet is written to the output trace:
65 *
66 * startTime <= packet.getTimestampBegin() <= endTime
67 *
68 * @param startTime
69 * start time of packets to be included in output trace
70 * @param endTime
71 * end time of packets to be included in the output trace
72 * @param newTracePath
73 * the path of the new trace to be written
74 * @throws CTFException
75 * If a reading or writing error occurs
76 */
77 public void copyPackets(long startTime, long endTime, String newTracePath) throws CTFException {
78 CTFTrace trace = fInTrace;
79 if (trace != null) {
80 long adjustedStart = startTime - trace.getClock().getClockOffset();
81 long adjustedEnd = endTime - trace.getClock().getClockOffset();
82 File out = new File(newTracePath);
83 if (out.exists()) {
84 throw new CTFIOException("Trace segment cannot be created since trace already exists: " + newTracePath); //$NON-NLS-1$
85 }
86
87 // create new directory
88 boolean isSuccess = out.mkdir();
89 if (!isSuccess) {
90 throw new CTFIOException("Creating trace directory failed: " + newTracePath); //$NON-NLS-1$
91 }
92
93 // copy metadata
94 Metadata metadata = new Metadata(fInTrace);
95 try {
96 metadata.copyTo(out);
97 } catch (IOException e) {
98 throw new CTFIOException("Error copying metadata: " + e.toString(), e); //$NON-NLS-1$
99 }
100
101 // Copy packets
102 for (CTFStream stream : trace.getStreams()) {
103 Set<CTFStreamInput> inputs = stream.getStreamInputs();
104 for (CTFStreamInput s : inputs) {
105 CTFStreamOutputWriter streamOutputwriter = new CTFStreamOutputWriter(checkNotNull(s), out);
106 streamOutputwriter.copyPackets(adjustedStart, adjustedEnd);
107 }
108 }
109 }
110 }
111 }
This page took 0.033411 seconds and 4 git commands to generate.