ade605afe5d40c1aba494e1113da0cb904375bbe
[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 fContentSizeBits = computeContentSize(fileSizeBytes);
168 fPacketSizeBits = computePacketSize(fileSizeBytes);
169 fTimestampBegin = computeTsBegin();
170 fTimestampEnd = computeTsEnd();
171 fOffsetBits = dataOffsetBits;
172 fOffsetBytes = dataOffsetBits / Byte.SIZE;
173
174 // LTTng Specific
175 Target target = lookupTarget(streamPacketContextDef);
176 fTarget = target.string;
177 fTargetID = target.number;
178 fLostEvents = computeLostEvents(lostSoFar);
179 }
180
181 private Long getPacketSize() {
182 return (Long) fAttributes.get(CTFStrings.PACKET_SIZE);
183 }
184
185 private long computeContentSize(long fileSizeBytes) {
186 Long contentSize = (Long) fAttributes.get(CTFStrings.CONTENT_SIZE);
187 /* Read the content size in bits */
188 if (contentSize != null) {
189 return contentSize.longValue();
190 }
191 Long packetSize = getPacketSize();
192 if (packetSize != null) {
193 return packetSize.longValue();
194 }
195 return fileSizeBytes * Byte.SIZE;
196 }
197
198 private long computePacketSize(long fileSizeBytes) {
199 Long packetSize = getPacketSize();
200 /* Read the packet size in bits */
201 if (packetSize != null) {
202 return packetSize.longValue();
203 }
204 long contentSizeBits = computeContentSize(fileSizeBytes);
205 if (contentSizeBits != 0) {
206 return contentSizeBits;
207 }
208 return fileSizeBytes * Byte.SIZE;
209 }
210
211 private long computeTsBegin() {
212 Long tsBegin = (Long) fAttributes.get(CTFStrings.TIMESTAMP_BEGIN);
213 /* Read the begin timestamp */
214 if (tsBegin != null) {
215 return tsBegin.longValue();
216 }
217 return 0;
218 }
219
220 private long computeTsEnd() {
221 Long tsEnd = (Long) fAttributes.get(CTFStrings.TIMESTAMP_END);
222 /* Read the end timestamp */
223 if (tsEnd != null) {
224 // check if tsEnd == unsigned long max value
225 if (tsEnd == -1) {
226 return Long.MAX_VALUE;
227 }
228 return tsEnd.longValue();
229 }
230 return Long.MAX_VALUE;
231 }
232
233 private long computeLostEvents(long lostSoFar) {
234 Long lostEvents = (Long) fAttributes.get(CTFStrings.EVENTS_DISCARDED);
235 if (lostEvents != null) {
236 return lostEvents - lostSoFar;
237 }
238 return 0;
239 }
240
241 private static class Target {
242 public String string;
243 public long number;
244
245 public Target() {
246 string = null;
247 number = IPacketReader.UNKNOWN_CPU;
248 }
249 }
250
251 private Target lookupTarget(StructDefinition streamPacketContextDef) {
252 Target ret = new Target();
253 boolean hasDevice = fAttributes.containsKey(CTFStrings.DEVICE);
254 if (hasDevice) {
255 IDefinition def = streamPacketContextDef.lookupDefinition(CTFStrings.DEVICE);
256 if (def instanceof SimpleDatatypeDefinition) {
257 SimpleDatatypeDefinition simpleDefinition = (SimpleDatatypeDefinition) def;
258 ret.string = simpleDefinition.getStringValue();
259 ret.number = simpleDefinition.getIntegerValue();
260 } else if (def instanceof StringDefinition) {
261 StringDefinition stringDefinition = (StringDefinition) def;
262 ret.string = stringDefinition.getValue();
263 final Matcher matcher = NUMBER_PATTERN.matcher(ret.string);
264 if (matcher.matches()) {
265 String number = matcher.group(1);
266 ret.number = Integer.parseInt(number);
267 }
268 }
269 } else {
270 Long cpuId = (Long) fAttributes.get(CTFStrings.CPU_ID);
271 if (cpuId != null) {
272 ret.string = ("CPU" + cpuId.toString()); //$NON-NLS-1$
273 ret.number = cpuId;
274 }
275 }
276 return ret;
277 }
278
279 // ------------------------------------------------------------------------
280 // Operations
281 // ------------------------------------------------------------------------
282
283 @Override
284 public boolean includes(long ts) {
285 return (ts >= fTimestampBegin) && (ts <= fTimestampEnd);
286 }
287
288 @Override
289 public String toString() {
290 return "StreamInputPacketIndexEntry [offsetBits=" + fOffsetBits //$NON-NLS-1$
291 + ", timestampBegin=" + fTimestampBegin + ", timestampEnd=" //$NON-NLS-1$ //$NON-NLS-2$
292 + fTimestampEnd + "]"; //$NON-NLS-1$
293 }
294
295 // ------------------------------------------------------------------------
296 // Getters and Setters
297 // ------------------------------------------------------------------------
298
299 @Override
300 public long getOffsetBits() {
301 return fOffsetBits;
302 }
303
304 @Override
305 public long getPacketSizeBits() {
306 return fPacketSizeBits;
307 }
308
309 @Override
310 public long getContentSizeBits() {
311 return fContentSizeBits;
312 }
313
314 @Override
315 public long getTimestampBegin() {
316 return fTimestampBegin;
317 }
318
319 @Override
320 public long getTimestampEnd() {
321 return fTimestampEnd;
322 }
323
324 @Override
325 public long getLostEvents() {
326 return fLostEvents;
327 }
328
329 /**
330 * Add an attribute to this index entry
331 *
332 * @param field
333 * The name of the attribute
334 * @param value
335 * The value to insert
336 */
337 public void addAttribute(String field, Object value) {
338 fAttributes.put(field, value);
339 }
340
341 @Override
342 public Object lookupAttribute(String field) {
343 return fAttributes.get(field);
344 }
345
346 @Override
347 public String getTarget() {
348 return fTarget;
349 }
350
351 @Override
352 public long getTargetId() {
353 return fTargetID;
354 }
355
356 @Override
357 public long getOffsetBytes() {
358 return fOffsetBytes;
359 }
360
361 @Override
362 public long getPayloadStartBits() {
363 return fEndPacketHeaderBits;
364 }
365 }
This page took 0.038492 seconds and 4 git commands to generate.