Skip to content

Commit efb3412

Browse files
committed
Improve model for sample and marker lists
1 parent 8508b67 commit efb3412

File tree

5 files changed

+93
-14
lines changed

5 files changed

+93
-14
lines changed

src/Ultra.Core/Model/UGenericList.cs

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
using System.Collections;
66
using System.ComponentModel;
7+
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
9+
using System.Linq;
10+
using System.Runtime.CompilerServices;
711
using XenoAtom.Collections;
812

913
namespace Ultra.Core.Model;
@@ -12,6 +16,8 @@ namespace Ultra.Core.Model;
1216
/// A generic list class used to store items of type <typeparamref name="T"/>.
1317
/// </summary>
1418
/// <typeparam name="T">The type of items in the list.</typeparam>
19+
[DebuggerDisplay("Count = {Count}")]
20+
[DebuggerTypeProxy(typeof(UGenericList<>.DebugListView))]
1521
public abstract class UGenericList<T>(int capacity) : IEnumerable<T>
1622
{
1723
/// <summary>
@@ -26,11 +32,29 @@ protected UGenericList() : this(0)
2632
{
2733
}
2834

35+
/// <summary>
36+
/// Gets the number of items in the list.
37+
/// </summary>
38+
public int Count => List.Count;
39+
40+
/// <summary>
41+
/// Gets the item at the specified index.
42+
/// </summary>
43+
/// <param name="index">The index of the item to get.</param>
44+
/// <returns>The item at the specified index</returns>
45+
public T this[int index]
46+
{
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
get => List[index];
49+
}
50+
2951
/// <summary>
3052
/// Gets the items in the list as a span.
3153
/// </summary>
3254
public ReadOnlySpan<T> Items => List.AsSpan();
3355

56+
/// <inheritdoc />
57+
public override string ToString() => $"{GetType().Name}[{Count}]";
3458

3559
/// <summary>
3660
/// Gets an enumerator for the items in the list.
@@ -42,4 +66,19 @@ protected UGenericList() : this(0)
4266
IEnumerator<T> IEnumerable<T>.GetEnumerator() => List.GetEnumerator();
4367

4468
IEnumerator IEnumerable.GetEnumerator() => List.GetEnumerator();
69+
70+
[ExcludeFromCodeCoverage]
71+
private sealed class DebugListView(UGenericList<T> genericList)
72+
{
73+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
74+
public T[] Items
75+
{
76+
get
77+
{
78+
T[] array = new T[genericList.Count];
79+
genericList.List.CopyTo((Span<T>)array);
80+
return array;
81+
}
82+
}
83+
}
4584
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace Ultra.Core.Model;
6+
7+
/// <summary>
8+
/// Represents a list of <see cref="UTraceMarker"/>.
9+
/// </summary>
10+
public sealed class UTraceMarkerList : UGenericList<UTraceMarker>
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="UTraceMarkerList"/> class.
14+
/// </summary>
15+
public UTraceMarkerList() : base()
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Adds a new marker to the list.
21+
/// </summary>
22+
/// <param name="marker">The sample to add.</param>
23+
public void Add(UTraceMarker marker) => List.Add(marker);
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace Ultra.Core.Model;
6+
7+
/// <summary>
8+
/// Represents a list of <see cref="UTraceSample"/>.
9+
/// </summary>
10+
public sealed class UTraceSampleList : UGenericList<UTraceSample>
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="UTraceSampleList"/> class.
14+
/// </summary>
15+
public UTraceSampleList() : base(1024)
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Adds a new sample to the list.
21+
/// </summary>
22+
/// <param name="sample">The sample to add.</param>
23+
public void Add(UTraceSample sample) => List.Add(sample);
24+
}

src/Ultra.Core/Model/UTraceThread.cs

+5-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace Ultra.Core.Model;
1111
/// </summary>
1212
public sealed record UTraceThread(ulong ThreadID)
1313
{
14-
private UnsafeList<UTraceSample> _samples = new(1024);
15-
1614
/// <summary>
1715
/// Gets or sets the start time of the thread.
1816
/// </summary>
@@ -31,21 +29,15 @@ public sealed record UTraceThread(ulong ThreadID)
3129
/// <summary>
3230
/// Gets the samples collected for the thread.
3331
/// </summary>
34-
public ReadOnlySpan<UTraceSample> Samples => _samples.AsSpan();
35-
36-
/// <summary>
37-
/// Gets or sets the CPU time for the thread.
38-
/// </summary>
39-
public UTimeSpan CpuTime { get; set; }
32+
public UTraceSampleList Samples { get; } = new();
4033

4134
/// <summary>
42-
/// Clears all collected samples.
35+
/// Gets the markers collected for the thread.
4336
/// </summary>
44-
public void ClearSamples() => _samples.Clear();
37+
public UTraceMarkerList Markers { get; } = new();
4538

4639
/// <summary>
47-
/// Adds a new sample to the thread.
40+
/// Gets or sets the CPU time for the thread.
4841
/// </summary>
49-
/// <param name="sample">The sample to add.</param>
50-
public void AddSample(UTraceSample sample) => _samples.Add(sample);
42+
public UTimeSpan CpuTime { get; set; }
5143
}

src/Ultra.Core/Parser/UltraEventPipeProcessor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void RecordStack(UTraceProcess process, UltraNativeCallstackTraceEvent ev
290290

291291
var callStackIndex = process.CallStacks.InsertCallStack(callStack);
292292

293-
_thread.AddSample(new(callStackIndex, UTimeSpan.FromMilliseconds(evt.TimeStampRelativeMSec), TimeSpan.Zero)); // TODO: cputime
293+
_thread.Samples.Add(new(callStackIndex, UTimeSpan.FromMilliseconds(evt.TimeStampRelativeMSec), TimeSpan.Zero)); // TODO: cputime
294294
}
295295
}
296296
}

0 commit comments

Comments
 (0)