Skip to content

Commit 7c84adb

Browse files
fixed math error and plumbed the needed metadata through (#51)
1 parent a8dfe39 commit 7c84adb

File tree

11 files changed

+86
-40
lines changed

11 files changed

+86
-40
lines changed

CtfPlayback/CtfPlayback.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public void Playback(
9191
streamPlayback.CurrentEvent,
9292
streamPlayback.CurrentPacket,
9393
this.streamToTrace[streamPlayback],
94-
streamPlayback.EventStream);
94+
streamPlayback.EventStream,
95+
streamPlayback.Metadata);
9596

9697
this.eventCount++;
9798

CtfPlayback/EventStreams/CtfEvent.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ internal class CtfEvent
1414
{
1515
private readonly PacketReader packetReader;
1616
private readonly ICtfPacket owningPacket;
17+
private readonly ICtfMetadata metadata;
1718

18-
public CtfEvent(PacketReader packetReader, ICtfPacket owningPacket)
19+
public CtfEvent(PacketReader packetReader, ICtfMetadata metadata, ICtfPacket owningPacket)
1920
{
2021
this.packetReader = packetReader;
2122
this.owningPacket = owningPacket;
23+
this.metadata = metadata;
2224
}
2325

2426
public ulong ByteOffsetWithinPacket { get; private set; }
@@ -49,7 +51,7 @@ public void ReadEventMetadata()
4951

5052
this.ReadStreamEventContext();
5153

52-
this.Timestamp = this.packetReader.PlaybackCustomization.GetTimestampFromEventHeader(this, this.owningPacket.CurrentEvent?.Timestamp);
54+
this.Timestamp = this.packetReader.PlaybackCustomization.GetTimestampFromEventHeader(this, this.metadata, this.owningPacket.CurrentEvent?.Timestamp);
5355

5456
this.DiscardedEvents = this.owningPacket.StreamPacketContext.ReadFieldAsUInt32("events_discarded");
5557

@@ -79,7 +81,7 @@ public void ReadEventMetadata()
7981

8082
public void Read()
8183
{
82-
this.EventDescriptor = this.packetReader.PlaybackCustomization.GetEventDescriptor(this);
84+
this.EventDescriptor = this.packetReader.PlaybackCustomization.GetEventDescriptor(this, this.metadata);
8385
if (this.EventDescriptor == null)
8486
{
8587
throw new CtfPlaybackException("Missing event descriptor for event.");

CtfPlayback/EventStreams/CtfPacket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public bool ReadPacketMetadata()
7373
this.PacketTimestampsAreValid = false;
7474

7575
if (this.packetReader.PlaybackCustomization.GetTimestampsFromPacketContext(
76-
this, out var startValue, out var endValue))
76+
this, this.Metadata, out var startValue, out var endValue))
7777
{
7878
this.Start = startValue;
7979
this.End = endValue;
@@ -92,7 +92,7 @@ public bool MoveToNextEvent()
9292
return false;
9393
}
9494

95-
var nextEvent = new CtfEvent(this.packetReader, this);
95+
var nextEvent = new CtfEvent(this.packetReader, this.Metadata, this);
9696
nextEvent.ReadEventMetadata();
9797

9898
this.CurrentEvent = nextEvent;

CtfPlayback/FieldValues/CtfTimestamp.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public class CtfTimestamp
3030
/// Constructor
3131
/// </summary>
3232
/// <param name="metadataCustomization">Extensibility points</param>
33+
/// <param name="metadata">The active metadata relevant to this timestamp</param>
3334
/// <param name="integerValue">integer representation of the timestamp</param>
34-
public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, CtfIntegerValue integerValue)
35+
public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, ICtfMetadata metadata, CtfIntegerValue integerValue)
3536
{
3637
this.BaseIntegerValue = integerValue;
3738

@@ -44,9 +45,9 @@ public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, CtfIntegerV
4445
throw new CtfPlaybackException($"Unable to parse integer map value as a clock: {integerValue.MapValue}");
4546
}
4647

47-
if (metadataCustomization.Metadata.Clocks?.Count == 1)
48+
if (metadata.Clocks?.Count == 1)
4849
{
49-
clockName = metadataCustomization.Metadata.Clocks[0].Name;
50+
clockName = metadata.Clocks[0].Name;
5051
}
5152
else
5253
{
@@ -55,7 +56,7 @@ public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, CtfIntegerV
5556
}
5657
}
5758

58-
if (!metadataCustomization.Metadata.ClocksByName.TryGetValue(clockName, out var clockDescriptor))
59+
if (!metadata.ClocksByName.TryGetValue(clockName, out var clockDescriptor))
5960
{
6061
throw new CtfPlaybackException($"Unable to retrieve clock descriptor for timestamp value: {integerValue.MapValue}");
6162
}
@@ -77,10 +78,11 @@ public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, CtfIntegerV
7778
/// Constructor
7879
/// </summary>
7980
/// <param name="metadataCustomization">Extensibility points</param>
81+
/// <param name="metadata">The active metadata relevant to this timestamp</param>
8082
/// <param name="integerValue">Integer representation of the timestamp</param>
8183
/// <param name="timestampValue">Timestamp value in units specified by the ClockDescriptor</param>
82-
public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, CtfIntegerValue integerValue, long timestampValue)
83-
: this(metadataCustomization, integerValue)
84+
public CtfTimestamp(ICtfMetadataCustomization metadataCustomization, ICtfMetadata metadata, CtfIntegerValue integerValue, long timestampValue)
85+
: this(metadataCustomization, metadata, integerValue)
8486
{
8587
if (timestampValue < 0)
8688
{

CtfPlayback/ICtfPlaybackCustomization.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ public interface ICtfPlaybackCustomization
3030
/// Allows for customized parsing of a packet to determine its start and end timestamps.
3131
/// </summary>
3232
/// <param name="ctfPacket"></param>
33+
/// <param name="metadata"></param>
3334
/// <param name="start"></param>
3435
/// <param name="end"></param>
3536
/// <returns></returns>
3637
bool GetTimestampsFromPacketContext(
3738
ICtfPacket ctfPacket,
39+
ICtfMetadata metadata,
3840
out CtfTimestamp start,
3941
out CtfTimestamp end);
4042

4143
/// <summary>
4244
/// Allows for customized parsing of an event header data to determine a timestamp
4345
/// for the event.
4446
/// </summary>
45-
CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, CtfTimestamp previousTimestamp);
47+
CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, ICtfMetadata metadata, CtfTimestamp previousTimestamp);
4648

4749
/// <summary>
4850
/// Used to retrieve the total number of bits in a packet.
@@ -62,8 +64,9 @@ bool GetTimestampsFromPacketContext(
6264
/// Reads the event at the current location from packetReader
6365
/// </summary>
6466
/// <param name="ctfEvent">Event with its context and header filled in</param>
67+
/// <param name="metadata">The metadata for the event</param>
6568
/// <returns></returns>
66-
ICtfEventDescriptor GetEventDescriptor(ICtfEvent ctfEvent);
69+
ICtfEventDescriptor GetEventDescriptor(ICtfEvent ctfEvent, ICtfMetadata metadata);
6770

6871
/// <summary>
6972
/// Called during trace playback for the current event.
@@ -72,6 +75,7 @@ bool GetTimestampsFromPacketContext(
7275
/// <param name="eventPacket">Packet which contains the event</param>
7376
/// <param name="ctfTraceInput">Trace which contains the event</param>
7477
/// <param name="ctfEventStream">Stream which contains the event</param>
75-
void ProcessEvent(ICtfEvent ctfEvent, ICtfPacket eventPacket, ICtfTraceInput ctfTraceInput, ICtfInputStream ctfEventStream);
78+
/// <param name="metadata">The metadata for the event</param>
79+
void ProcessEvent(ICtfEvent ctfEvent, ICtfPacket eventPacket, ICtfTraceInput ctfTraceInput, ICtfInputStream ctfEventStream, ICtfMetadata metadata);
7680
}
7781
}

CtfPlayback/Metadata/Interfaces/ICtfEventDescriptor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace CtfPlayback.Metadata.Interfaces
1010
/// </summary>
1111
public interface ICtfEventDescriptor
1212
{
13+
/// <summary>
14+
/// The id of the event
15+
/// </summary>
16+
public uint Id { get; }
17+
1318
/// <summary>
1419
/// Event context type
1520
/// </summary>

CtfPlayback/PacketReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public void Align(uint bitCount)
323323

324324
// progress by entire bytes, we should be byte aligned at this point
325325
Debug.Assert(this.bitsConsumedInCurrentByte == 0);
326-
while (bitsToProgress > 8)
326+
while (bitsToProgress >= 8)
327327
{
328328
if (this.bufferByteIndex + 1 >= this.bufferByteCount)
329329
{

LTTngCds/CtfExtensions/LTTngPlaybackCustomization.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Linq;
78
using CtfPlayback;
89
using CtfPlayback.EventStreams.Interfaces;
910
using CtfPlayback.FieldValues;
@@ -45,6 +46,7 @@ public virtual ICtfMetadataParser CreateMetadataParser(
4546

4647
public bool GetTimestampsFromPacketContext(
4748
ICtfPacket ctfPacket,
49+
ICtfMetadata metadata,
4850
out CtfTimestamp start,
4951
out CtfTimestamp end)
5052
{
@@ -76,8 +78,8 @@ public bool GetTimestampsFromPacketContext(
7678
return false;
7779
}
7880

79-
start = new CtfTimestamp(this.MetadataCustomization, startFieldInteger);
80-
end = new CtfTimestamp(this.MetadataCustomization, endFieldInteger);
81+
start = new CtfTimestamp(this.MetadataCustomization, metadata, startFieldInteger);
82+
end = new CtfTimestamp(this.MetadataCustomization, metadata, endFieldInteger);
8183
return true;
8284
}
8385

@@ -86,7 +88,7 @@ public void RegisterEventCallback(Action<LTTngEvent, LTTngContext> eventCallback
8688
this.eventCallbacks.Add(eventCallback);
8789
}
8890

89-
public virtual CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, CtfTimestamp previousTimestamp)
91+
public virtual CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, ICtfMetadata metadata, CtfTimestamp previousTimestamp)
9092
{
9193
var variantStruct = GetStreamEventHeaderStructure(ctfEvent);
9294
Debug.Assert(variantStruct != null);
@@ -107,7 +109,7 @@ public virtual CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, CtfT
107109
// todo:I think we need to do something else for this case, especially if the integer size is < 64
108110
// not sure what yet. maybe base it on the clock's offset?
109111

110-
return new CtfTimestamp(this.MetadataCustomization, timestampInteger);
112+
return new CtfTimestamp(this.MetadataCustomization, metadata, timestampInteger);
111113
}
112114

113115
// Timestamps aren't actually absolute values. To quote from CTF spec 1.8.2 section 8:
@@ -139,10 +141,10 @@ public virtual CtfTimestamp GetTimestampFromEventHeader(ICtfEvent ctfEvent, CtfT
139141
Debug.Assert(newTimestamp > previous);
140142
}
141143

142-
return new CtfTimestamp(this.MetadataCustomization, timestampInteger, newTimestamp);
144+
return new CtfTimestamp(this.MetadataCustomization, metadata, timestampInteger, newTimestamp);
143145
}
144146

145-
return new CtfTimestamp(this.MetadataCustomization, timestampInteger);
147+
return new CtfTimestamp(this.MetadataCustomization, metadata, timestampInteger);
146148
}
147149

148150
/// <inheritdoc />
@@ -218,20 +220,31 @@ public ulong GetPacketContentBitCount(ICtfPacket ctfPacket)
218220
}
219221

220222
/// <inheritdoc />
221-
public ICtfEventDescriptor GetEventDescriptor(
222-
ICtfEvent ctfEvent)
223+
public ICtfEventDescriptor GetEventDescriptor(ICtfEvent ctfEvent, ICtfMetadata metadata)
223224
{
224225
uint id = this.GetEventId(ctfEvent);
225226

226-
if (!this.metadataCustomization.LTTngMetadata.EventByEventId.TryGetValue(id, out var eventDescriptor))
227+
ICtfEventDescriptor eventDescriptor;
228+
if (metadata is LTTngMetadata typedMetadata)
229+
{
230+
// optimization if we got our own typed metadata back
231+
// we check for success below
232+
typedMetadata.EventByEventId.TryGetValue(id, out eventDescriptor);
233+
}
234+
else
235+
{
236+
eventDescriptor = metadata.Events.FirstOrDefault(x => x.Id == id);
237+
}
238+
239+
if (eventDescriptor == null)
227240
{
228241
throw new LTTngPlaybackException($"Unable to find event descriptor for event id={id}.");
229242
}
230243

231244
return eventDescriptor;
232245
}
233246

234-
public void ProcessEvent(ICtfEvent ctfEvent, ICtfPacket eventPacket, ICtfTraceInput traceInput, ICtfInputStream ctfEventStream)
247+
public void ProcessEvent(ICtfEvent ctfEvent, ICtfPacket eventPacket, ICtfTraceInput traceInput, ICtfInputStream ctfEventStream, ICtfMetadata metadata)
235248
{
236249
var eventDescriptor = ctfEvent.EventDescriptor as EventDescriptor;
237250
Debug.Assert(eventDescriptor != null);
@@ -255,7 +268,7 @@ public void ProcessEvent(ICtfEvent ctfEvent, ICtfPacket eventPacket, ICtfTraceIn
255268

256269
if (!this.traceContexts.TryGetValue(traceInput, out var traceContext))
257270
{
258-
traceContext = new TraceContext(this.metadataCustomization.LTTngMetadata);
271+
traceContext = new TraceContext(metadata);
259272
this.traceContexts.Add(traceInput, traceContext);
260273
}
261274

LTTngCds/CtfExtensions/TraceContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using CtfPlayback.Metadata.Interfaces;
5+
46
namespace LTTngCds.CtfExtensions
57
{
68
internal class TraceContext
79
{
8-
internal TraceContext(LTTngMetadata metadata)
10+
internal TraceContext(ICtfMetadata metadata)
911
{
1012
if (metadata.EnvironmentDescriptor.Properties.TryGetValue("hostname", out string hostName))
1113
{

0 commit comments

Comments
 (0)