1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
;
15 import java
.util
.HashMap
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.CTFStrings
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDefinition
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.SimpleDatatypeDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.ICTFPacketDescriptor
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.IPacketReader
;
32 * <b><u>StreamInputPacketIndexEntry</u></b>
34 * Represents an entry in the index of event packets.
36 public class StreamInputPacketIndexEntry
implements ICTFPacketDescriptor
{
38 private static final Pattern NUMBER_PATTERN
= Pattern
.compile("\\D*(\\d+)"); //$NON-NLS-1$
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Position of the start of the packet header in the file, in bits
47 private final long fOffsetBits
;
50 * Position of the start of the packet header in the file, in bytes
52 private final long fOffsetBytes
;
55 * Packet size, in bits
57 private final long fPacketSizeBits
;
60 * Content size, in bits
62 private final long fContentSizeBits
;
67 private final long fTimestampBegin
;
72 private final long fTimestampEnd
;
75 * How many lost events are there?
77 private final long fLostEvents
;
80 * Which target is being traced
82 private final String fTarget
;
83 private final long fTargetID
;
86 * Attributes of this index entry
88 private final Map
<String
, Object
> fAttributes
= new HashMap
<>();
90 private final long fEndPacketHeaderBits
;
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Constructs an index entry.
99 * @param dataOffsetBits
100 * offset in the file for the start of data in bits
101 * @param fileSizeBytes
102 * number of bytes in a file
107 public StreamInputPacketIndexEntry(long dataOffsetBits
, long fileSizeBytes
) {
108 fContentSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
109 fPacketSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
110 fOffsetBits
= dataOffsetBits
;
111 fOffsetBytes
= dataOffsetBits
/ Byte
.SIZE
;
113 fTarget
= ""; //$NON-NLS-1$
116 fTimestampEnd
= Long
.MAX_VALUE
;
117 fEndPacketHeaderBits
= dataOffsetBits
;
123 * @param dataOffsetBits
124 * offset in the file for the start of data in bits
125 * @param streamPacketContextDef
127 * @param fileSizeBytes
128 * number of bytes in a file
130 * number of lost events so far
134 public StreamInputPacketIndexEntry(long dataOffsetBits
, StructDefinition streamPacketContextDef
, long fileSizeBytes
, long lostSoFar
) {
135 this(dataOffsetBits
, streamPacketContextDef
, fileSizeBytes
, lostSoFar
, dataOffsetBits
);
141 * @param dataOffsetBits
142 * offset in the file for the start of data in bits
143 * @param streamPacketContextDef
145 * @param fileSizeBytes
146 * number of bytes in a file
148 * number of lost events so far
149 * @param endPacketHeaderBits
150 * end of packet headers
152 public StreamInputPacketIndexEntry(long dataOffsetBits
, StructDefinition streamPacketContextDef
, long fileSizeBytes
, long lostSoFar
, long endPacketHeaderBits
) {
153 fEndPacketHeaderBits
= endPacketHeaderBits
;
154 for (String field
: streamPacketContextDef
.getDeclaration().getFieldsList()) {
155 IDefinition id
= streamPacketContextDef
.lookupDefinition(field
);
156 if (id
instanceof IntegerDefinition
) {
157 fAttributes
.put(field
, ((IntegerDefinition
) id
).getValue());
158 } else if (id
instanceof FloatDefinition
) {
159 fAttributes
.put(field
, ((FloatDefinition
) id
).getValue());
160 } else if (id
instanceof EnumDefinition
) {
161 fAttributes
.put(field
, ((EnumDefinition
) id
).getValue());
162 } else if (id
instanceof StringDefinition
) {
163 fAttributes
.put(field
, ((StringDefinition
) id
).getValue());
167 Long contentSize
= (Long
) fAttributes
.get(CTFStrings
.CONTENT_SIZE
);
168 Long packetSize
= (Long
) fAttributes
.get(CTFStrings
.PACKET_SIZE
);
169 Long tsBegin
= (Long
) fAttributes
.get(CTFStrings
.TIMESTAMP_BEGIN
);
170 Long tsEnd
= (Long
) fAttributes
.get(CTFStrings
.TIMESTAMP_END
);
171 boolean hasDevice
= fAttributes
.containsKey(CTFStrings
.DEVICE
);
173 Long cpuId
= (Long
) fAttributes
.get(CTFStrings
.CPU_ID
);
174 Long lostEvents
= (Long
) fAttributes
.get(CTFStrings
.EVENTS_DISCARDED
);
176 /* Read the content size in bits */
177 if (contentSize
!= null) {
178 fContentSizeBits
= (contentSize
.longValue());
179 } else if (packetSize
!= null) {
180 fContentSizeBits
= (packetSize
.longValue());
182 fContentSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
185 /* Read the packet size in bits */
186 if (packetSize
!= null) {
187 fPacketSizeBits
= (packetSize
.longValue());
188 } else if (this.getContentSizeBits() != 0) {
189 fPacketSizeBits
= fContentSizeBits
;
191 fPacketSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
194 /* Read the begin timestamp */
195 if (tsBegin
!= null) {
196 fTimestampBegin
= (tsBegin
.longValue());
201 /* Read the end timestamp */
203 // check if tsEnd == unsigned long max value
205 tsEnd
= Long
.MAX_VALUE
;
207 fTimestampEnd
= (tsEnd
.longValue());
209 fTimestampEnd
= Long
.MAX_VALUE
;
212 Target target
= lookupTarget(streamPacketContextDef
, hasDevice
, cpuId
);
213 fTarget
= target
.string
;
214 fTargetID
= target
.number
;
216 if (lostEvents
!= null) {
217 fLostEvents
= (lostEvents
- lostSoFar
);
222 fOffsetBits
= dataOffsetBits
;
223 fOffsetBytes
= dataOffsetBits
/ Byte
.SIZE
;
226 private static class Target
{
227 public String string
;
232 number
= IPacketReader
.UNKNOWN_CPU
;
236 private static Target
lookupTarget(StructDefinition streamPacketContextDef
, boolean hasDevice
, Long cpuId
) {
237 Target ret
= new Target();
239 IDefinition def
= streamPacketContextDef
.lookupDefinition(CTFStrings
.DEVICE
);
240 if (def
instanceof SimpleDatatypeDefinition
) {
241 SimpleDatatypeDefinition simpleDefinition
= (SimpleDatatypeDefinition
) def
;
242 ret
.string
= simpleDefinition
.getStringValue();
243 ret
.number
= simpleDefinition
.getIntegerValue();
244 } else if (def
instanceof StringDefinition
) {
245 StringDefinition stringDefinition
= (StringDefinition
) def
;
246 ret
.string
= stringDefinition
.getValue();
247 final Matcher matcher
= NUMBER_PATTERN
.matcher(ret
.string
);
248 if (matcher
.matches()) {
249 String number
= matcher
.group(1);
250 ret
.number
= Integer
.parseInt(number
);
253 } else if (cpuId
!= null) {
254 ret
.string
= ("CPU" + cpuId
.toString()); //$NON-NLS-1$
260 // ------------------------------------------------------------------------
262 // ------------------------------------------------------------------------
265 public boolean includes(long ts
) {
266 return (ts
>= fTimestampBegin
) && (ts
<= fTimestampEnd
);
270 public String
toString() {
271 return "StreamInputPacketIndexEntry [offsetBits=" + fOffsetBits
//$NON-NLS-1$
272 + ", timestampBegin=" + fTimestampBegin
+ ", timestampEnd=" //$NON-NLS-1$ //$NON-NLS-2$
273 + fTimestampEnd
+ "]"; //$NON-NLS-1$
276 // ------------------------------------------------------------------------
277 // Getters and Setters
278 // ------------------------------------------------------------------------
281 public long getOffsetBits() {
286 public long getPacketSizeBits() {
287 return fPacketSizeBits
;
291 public long getContentSizeBits() {
292 return fContentSizeBits
;
296 public long getTimestampBegin() {
297 return fTimestampBegin
;
301 public long getTimestampEnd() {
302 return fTimestampEnd
;
306 public long getLostEvents() {
311 * Add an attribute to this index entry
314 * The name of the attribute
316 * The value to insert
318 public void addAttribute(String field
, Object value
) {
319 fAttributes
.put(field
, value
);
323 public Object
lookupAttribute(String field
) {
324 return fAttributes
.get(field
);
328 public String
getTarget() {
333 public long getTargetId() {
338 public long getOffsetBytes() {
343 public long getPayloadStartBits() {
344 return fEndPacketHeaderBits
;