tmf: Fix remote test failing on Windows
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / trace / StreamInputPacketIndexEntry.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.ctf.core.trace;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
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;
30
31 /**
32 * <b><u>StreamInputPacketIndexEntry</u></b>
33 * <p>
34 * Represents an entry in the index of event packets.
35 */
36 public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
37
38 private static final Pattern NUMBER_PATTERN = Pattern.compile("\\D*(\\d+)"); //$NON-NLS-1$
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 /**
45 * Position of the start of the packet header in the file, in bits
46 */
47 private final long fOffsetBits;
48
49 /**
50 * Position of the start of the packet header in the file, in bytes
51 */
52 private final long fOffsetBytes;
53
54 /**
55 * Packet size, in bits
56 */
57 private final long fPacketSizeBits;
58
59 /**
60 * Content size, in bits
61 */
62 private final long fContentSizeBits;
63
64 /**
65 * Begin timestamp
66 */
67 private final long fTimestampBegin;
68
69 /**
70 * End timestamp
71 */
72 private final long fTimestampEnd;
73
74 /**
75 * How many lost events are there?
76 */
77 private final long fLostEvents;
78
79 /**
80 * Which target is being traced
81 */
82 private final String fTarget;
83 private final long fTargetID;
84
85 /**
86 * Attributes of this index entry
87 */
88 private final Map<String, Object> fAttributes = new HashMap<>();
89
90 private final long fEndPacketHeaderBits;
91
92 // ------------------------------------------------------------------------
93 // Constructors
94 // ------------------------------------------------------------------------
95
96 /**
97 * Constructs an index entry.
98 *
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
103 *
104 * TODO: Remove
105 */
106
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;
112 fLostEvents = 0;
113 fTarget = ""; //$NON-NLS-1$
114 fTargetID = 0;
115 fTimestampBegin = 0;
116 fTimestampEnd = Long.MAX_VALUE;
117 fEndPacketHeaderBits = dataOffsetBits;
118 }
119
120 /**
121 * full Constructor
122 *
123 * @param dataOffsetBits
124 * offset in the file for the start of data in bits
125 * @param streamPacketContextDef
126 * packet context
127 * @param fileSizeBytes
128 * number of bytes in a file
129 * @param lostSoFar
130 * number of lost events so far
131 *
132 * TODO: Remove
133 */
134 public StreamInputPacketIndexEntry(long dataOffsetBits, StructDefinition streamPacketContextDef, long fileSizeBytes, long lostSoFar) {
135 this(dataOffsetBits, streamPacketContextDef, fileSizeBytes, lostSoFar, dataOffsetBits);
136 }
137
138 /**
139 * full Constructor
140 *
141 * @param dataOffsetBits
142 * offset in the file for the start of data in bits
143 * @param streamPacketContextDef
144 * packet context
145 * @param fileSizeBytes
146 * number of bytes in a file
147 * @param lostSoFar
148 * number of lost events so far
149 * @param endPacketHeaderBits
150 * end of packet headers
151 */
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());
164 }
165 }
166
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);
172 // LTTng Specific
173 Long cpuId = (Long) fAttributes.get(CTFStrings.CPU_ID);
174 Long lostEvents = (Long) fAttributes.get(CTFStrings.EVENTS_DISCARDED);
175
176 /* Read the content size in bits */
177 if (contentSize != null) {
178 fContentSizeBits = (contentSize.longValue());
179 } else if (packetSize != null) {
180 fContentSizeBits = (packetSize.longValue());
181 } else {
182 fContentSizeBits = (fileSizeBytes * Byte.SIZE);
183 }
184
185 /* Read the packet size in bits */
186 if (packetSize != null) {
187 fPacketSizeBits = (packetSize.longValue());
188 } else if (this.getContentSizeBits() != 0) {
189 fPacketSizeBits = fContentSizeBits;
190 } else {
191 fPacketSizeBits = (fileSizeBytes * Byte.SIZE);
192 }
193
194 /* Read the begin timestamp */
195 if (tsBegin != null) {
196 fTimestampBegin = (tsBegin.longValue());
197 } else {
198 fTimestampBegin = 0;
199 }
200
201 /* Read the end timestamp */
202 if (tsEnd != null) {
203 // check if tsEnd == unsigned long max value
204 if (tsEnd == -1) {
205 tsEnd = Long.MAX_VALUE;
206 }
207 fTimestampEnd = (tsEnd.longValue());
208 } else {
209 fTimestampEnd = Long.MAX_VALUE;
210 }
211
212 Target target = lookupTarget(streamPacketContextDef, hasDevice, cpuId);
213 fTarget = target.string;
214 fTargetID = target.number;
215
216 if (lostEvents != null) {
217 fLostEvents = (lostEvents - lostSoFar);
218 } else {
219 fLostEvents = 0;
220 }
221
222 fOffsetBits = dataOffsetBits;
223 fOffsetBytes = dataOffsetBits / Byte.SIZE;
224 }
225
226 private static class Target {
227 public String string;
228 public long number;
229
230 public Target() {
231 string = null;
232 number = IPacketReader.UNKNOWN_CPU;
233 }
234 }
235
236 private static Target lookupTarget(StructDefinition streamPacketContextDef, boolean hasDevice, Long cpuId) {
237 Target ret = new Target();
238 if (hasDevice) {
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);
251 }
252 }
253 } else if (cpuId != null) {
254 ret.string = ("CPU" + cpuId.toString()); //$NON-NLS-1$
255 ret.number = cpuId;
256 }
257 return ret;
258 }
259
260 // ------------------------------------------------------------------------
261 // Operations
262 // ------------------------------------------------------------------------
263
264 @Override
265 public boolean includes(long ts) {
266 return (ts >= fTimestampBegin) && (ts <= fTimestampEnd);
267 }
268
269 @Override
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$
274 }
275
276 // ------------------------------------------------------------------------
277 // Getters and Setters
278 // ------------------------------------------------------------------------
279
280 @Override
281 public long getOffsetBits() {
282 return fOffsetBits;
283 }
284
285 @Override
286 public long getPacketSizeBits() {
287 return fPacketSizeBits;
288 }
289
290 @Override
291 public long getContentSizeBits() {
292 return fContentSizeBits;
293 }
294
295 @Override
296 public long getTimestampBegin() {
297 return fTimestampBegin;
298 }
299
300 @Override
301 public long getTimestampEnd() {
302 return fTimestampEnd;
303 }
304
305 @Override
306 public long getLostEvents() {
307 return fLostEvents;
308 }
309
310 /**
311 * Add an attribute to this index entry
312 *
313 * @param field
314 * The name of the attribute
315 * @param value
316 * The value to insert
317 */
318 public void addAttribute(String field, Object value) {
319 fAttributes.put(field, value);
320 }
321
322 @Override
323 public Object lookupAttribute(String field) {
324 return fAttributes.get(field);
325 }
326
327 @Override
328 public String getTarget() {
329 return fTarget;
330 }
331
332 @Override
333 public long getTargetId() {
334 return fTargetID;
335 }
336
337 @Override
338 public long getOffsetBytes() {
339 return fOffsetBytes;
340 }
341
342 @Override
343 public long getPayloadStartBits() {
344 return fEndPacketHeaderBits;
345 }
346 }
This page took 0.040207 seconds and 5 git commands to generate.