Skip to content

Commit

Permalink
Update native UltraNativeModuleTraceEvent with uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 17, 2024
1 parent 2b73666 commit a4cbd35
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 157 deletions.
83 changes: 83 additions & 0 deletions src/Ultra.Core/Parser/UltraNativeCallstackTraceEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using Microsoft.Diagnostics.Tracing;
using Ultra.Sampler;

namespace Ultra.Core;

internal sealed class UltraNativeCallstackTraceEvent : TraceEvent
{
private static readonly string[] _payloadNames =
[
nameof(FrameThreadId),
nameof(ThreadState),
nameof(ThreadCpuUsage),
nameof(PreviousFrameCount),
nameof(FrameSize),
nameof(FrameAddresses)
];

private Action<UltraNativeCallstackTraceEvent>? _target;

internal UltraNativeCallstackTraceEvent(Action<UltraNativeCallstackTraceEvent>? target, int eventID, int task, string taskName, Guid taskGuid, int opcode, string opcodeName, Guid providerGuid, string providerName) : base(eventID, task, taskName, taskGuid, opcode, opcodeName, providerGuid, providerName)
{
_target = target;
}

public ulong FrameThreadId => (ulong)GetInt64At(0);

public UltraSamplerThreadState ThreadState => (UltraSamplerThreadState)GetInt32At(8);

public double ThreadCpuUsage => GetInt32At(12) / 1000.0;

public int PreviousFrameCount => GetInt32At(16);

public int FrameSize => GetInt32At(20);

public unsafe ReadOnlySpan<ulong> FrameAddresses => new((byte*)DataStart + 24, FrameSize / sizeof(ulong));

/// <inheritdoc />

public override object PayloadValue(int index)
{
switch (index)
{
case 0:
return FrameThreadId;
case 1:
return (int)ThreadState;
case 2:
return GetInt32At(12);
case 3:
return PreviousFrameCount;
case 4:
return FrameSize;
case 5:
return FrameAddresses.ToArray();
default:
throw new ArgumentOutOfRangeException(nameof(index));
}
}

public override string[] PayloadNames => _payloadNames;

/// <inheritdoc />
protected override Delegate? Target
{
get => _target;
set => _target = (Action<UltraNativeCallstackTraceEvent>?)value;
}

/// <inheritdoc />
protected override void Dispatch()
{
_target?.Invoke(this);
}

/// <inheritdoc />
protected override void Validate()
{
}
}
92 changes: 92 additions & 0 deletions src/Ultra.Core/Parser/UltraNativeModuleTraceEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Text;
using Microsoft.Diagnostics.Tracing;

namespace Ultra.Core;

internal sealed class UltraNativeModuleTraceEvent : TraceEvent
{
private static readonly string[] _payloadNames =
[
nameof(NativeModuleEventKind),
nameof(LoadAddress),
nameof(Size),
nameof(TimestampUtc),
nameof(Uuid),
nameof(ModulePathUtf8Length),
nameof(ModulePath)
];

private Action<UltraNativeModuleTraceEvent>? _target;

internal UltraNativeModuleTraceEvent(Action<UltraNativeModuleTraceEvent>? target, int eventID, int task, string taskName, Guid taskGuid, int opcode, string opcodeName, Guid providerGuid, string providerName) : base(eventID, task, taskName, taskGuid, opcode, opcodeName,
providerGuid,
providerName)
{
_target = target;
}

public UltraSamplerNativeModuleEventKind NativeModuleEventKind => (UltraSamplerNativeModuleEventKind)GetInt32At(0);

public ulong LoadAddress => (ulong)GetInt64At(4);

public ulong Size => (ulong)GetInt64At(12);

public DateTime TimestampUtc => DateTime.FromFileTimeUtc(GetInt64At(20));

public unsafe Guid Uuid => *(Guid*)(DataStart + 28);

public unsafe int ModulePathUtf8Length => GetInt32At(44);

public unsafe ReadOnlySpan<byte> ModulePathUtf8 => ModulePathUtf8Length == 0 ? ReadOnlySpan<byte>.Empty : new((byte*)DataStart + 48, ModulePathUtf8Length);

public unsafe string? ModulePath => ModulePathUtf8Length == 0 ? null : Encoding.UTF8.GetString(ModulePathUtf8);

/// <inheritdoc />
public override object? PayloadValue(int index)
{
switch (index)
{
case 0:
return (int)NativeModuleEventKind;
case 1:
return LoadAddress;
case 2:
return Size;
case 3:
return TimestampUtc;
case 4:
return Uuid;
case 5:
return ModulePathUtf8Length;
case 6:
return ModulePathUtf8.ToArray();
default:
throw new ArgumentOutOfRangeException(nameof(index));
}
}

/// <inheritdoc />
public override string[] PayloadNames => _payloadNames;

/// <inheritdoc />
protected override Delegate? Target
{
get => _target;
set => _target = (Action<UltraNativeModuleTraceEvent>?)value;
}

/// <inheritdoc />
protected override void Dispatch()
{
_target?.Invoke(this);
}

/// <inheritdoc />
protected override void Validate()
{
}
}
2 changes: 1 addition & 1 deletion src/Ultra.Core/Parser/UltraSamplerNativeModuleEventKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ultra.Core;

public enum UltraSamplerNativeModuleEventKind
internal enum UltraSamplerNativeModuleEventKind
{
AlreadyLoaded = 0,

Expand Down
158 changes: 2 additions & 156 deletions src/Ultra.Core/Parser/UltraSamplerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.Diagnostics.Tracing;
using Ultra.Sampler;

namespace Ultra.Core;

public sealed class UltraSamplerParser : TraceEventParser
internal sealed class UltraSamplerParser : TraceEventParser
{
public static readonly Guid ProviderGuid = UltraSamplerConstants.ProviderGuid;

Expand Down Expand Up @@ -61,158 +61,4 @@ private static TraceEvent CreateUltraNativeCallstackTraceEvent(Action<UltraNativ

private static TraceEvent CreateUltraNativeModuleTraceEvent(Action<UltraNativeModuleTraceEvent>? value)
=> new UltraNativeModuleTraceEvent(value, UltraSamplerConstants.NativeModuleEventId, 0, "OnNativeModule", Guid.Empty, 0, "OnNativeModule", ProviderGuid, ProviderName);
}

public sealed class UltraNativeCallstackTraceEvent : TraceEvent
{
private static readonly string[] _payloadNames =
[
nameof(FrameThreadId),
nameof(ThreadState),
nameof(ThreadCpuUsage),
nameof(PreviousFrameCount),
nameof(FrameSize),
nameof(FrameAddresses)
];

private Action<UltraNativeCallstackTraceEvent>? _target;

internal UltraNativeCallstackTraceEvent(Action<UltraNativeCallstackTraceEvent>? target, int eventID, int task, string taskName, Guid taskGuid, int opcode, string opcodeName, Guid providerGuid, string providerName) : base(eventID, task, taskName, taskGuid, opcode, opcodeName, providerGuid, providerName)
{
_target = target;
}

public ulong FrameThreadId => (ulong)GetInt64At(0);

public UltraSamplerThreadState ThreadState => (UltraSamplerThreadState)GetInt32At(8);

public double ThreadCpuUsage => GetInt32At(12) / 1000.0;

public int PreviousFrameCount => GetInt32At(16);

public int FrameSize => GetInt32At(20);

public unsafe ReadOnlySpan<ulong> FrameAddresses => new((byte*)DataStart + 24, FrameSize / sizeof(ulong));

/// <inheritdoc />

public override object PayloadValue(int index)
{
switch (index)
{
case 0:
return FrameThreadId;
case 1:
return (int)ThreadState;
case 2:
return GetInt32At(12);
case 3:
return PreviousFrameCount;
case 4:
return FrameSize;
case 5:
return FrameAddresses.ToArray();
default:
throw new ArgumentOutOfRangeException(nameof(index));
}
}

public override string[] PayloadNames => _payloadNames;

/// <inheritdoc />
protected override Delegate? Target
{
get => _target;
set => _target = (Action<UltraNativeCallstackTraceEvent>?)value;
}

/// <inheritdoc />
protected override void Dispatch()
{
_target?.Invoke(this);
}

/// <inheritdoc />
protected override void Validate()
{
}
}

public sealed class UltraNativeModuleTraceEvent : TraceEvent
{
private static readonly string[] _payloadNames =
[
nameof(NativeModuleEventKind),
nameof(LoadAddress),
nameof(Size),
nameof(TimestampUtc),
nameof(ModulePathUtf8Length),
nameof(ModulePath)
];

private Action<UltraNativeModuleTraceEvent>? _target;

internal UltraNativeModuleTraceEvent(Action<UltraNativeModuleTraceEvent>? target, int eventID, int task, string taskName, Guid taskGuid, int opcode, string opcodeName, Guid providerGuid, string providerName) : base(eventID, task, taskName, taskGuid, opcode, opcodeName,
providerGuid,
providerName)
{
_target = target;
}

public UltraSamplerNativeModuleEventKind NativeModuleEventKind => (UltraSamplerNativeModuleEventKind)GetInt32At(0);

public ulong LoadAddress => (ulong)GetInt64At(4);

public ulong Size => (ulong)GetInt64At(12);

public DateTime TimestampUtc => DateTime.FromFileTimeUtc(GetInt64At(20));

private int ModulePathUtf8Length => GetInt32At(28);

private unsafe ReadOnlySpan<byte> ModulePathUtf8 => ModulePathUtf8Length == 0 ? ReadOnlySpan<byte>.Empty : new((byte*)DataStart + 32, ModulePathUtf8Length);

public unsafe string? ModulePath => ModulePathUtf8Length == 0 ? null : Encoding.UTF8.GetString(ModulePathUtf8);

/// <inheritdoc />
public override object? PayloadValue(int index)
{
switch (index)
{
case 0:
return (int)NativeModuleEventKind;
case 1:
return LoadAddress;
case 2:
return Size;
case 3:
return TimestampUtc;
case 4:
return ModulePathUtf8Length;
case 5:
return ModulePathUtf8.ToArray();
default:
throw new ArgumentOutOfRangeException(nameof(index));
}
}

/// <inheritdoc />
public override string[] PayloadNames => _payloadNames;

/// <inheritdoc />
protected override Delegate? Target
{
get => _target;
set => _target = (Action<UltraNativeModuleTraceEvent>?)value;
}

/// <inheritdoc />
protected override void Dispatch()
{
_target?.Invoke(this);
}

/// <inheritdoc />
protected override void Validate()
{
}
}

0 comments on commit a4cbd35

Please sign in to comment.