Skip to content

Commit

Permalink
Encapsulated MTStopwatch APIs further.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Jan 5, 2025
1 parent fe5ee5b commit 9f9c350
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
10 changes: 5 additions & 5 deletions ModTek/Util/Stopwatch/MTStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace ModTek.Util.Stopwatch;

internal class MTStopwatch
{
protected long _ticks;
protected long _count;
protected long _ticks;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static long GetTimestamp()
Expand All @@ -18,17 +18,17 @@ internal static long GetTimestamp()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal virtual void EndMeasurement(long start)
{
AddMeasurement(GetTimestamp() - start);
AddMeasurement(GetTimestamp() - start, 1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual void AddMeasurement(long elapsedTicks)
protected virtual void AddMeasurement(long elapsedTicks, long delta)
{
Interlocked.Increment(ref _count);
Interlocked.Add(ref _count, delta);
Interlocked.Add(ref _ticks, elapsedTicks);
}

internal MTStopwatchStats GetStats() => new(this, Interlocked.Read(ref _ticks), Interlocked.Read(ref _count));
internal MTStopwatchStats GetStats() => new(this, Volatile.Read(ref _count), Volatile.Read(ref _ticks));

internal static TimeSpan TimeSpanFromTicks(long elapsedTicks)
{
Expand Down
19 changes: 5 additions & 14 deletions ModTek/Util/Stopwatch/MTStopwatchStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,16 @@ namespace ModTek.Util.Stopwatch;
internal readonly struct MTStopwatchStats
{
private readonly string _verb;
internal readonly long Ticks;
internal readonly long Count;
internal readonly long Ticks;
internal TimeSpan TotalTime => MTStopwatch.TimeSpanFromTicks(Ticks);
internal long AverageNanoseconds => Count <= 0 ? 0 : (long)((double)Ticks / Count / System.Diagnostics.Stopwatch.Frequency * 1e+9);

internal MTStopwatchStats(MTStopwatch sw, long ticks, long count)
internal MTStopwatchStats(MTStopwatch sw, long count, long ticks)
{
if (sw is MTStopwatchWithSampling swWithSampling)
{
Ticks = ticks * swWithSampling.Sampling;
Count = count * swWithSampling.Sampling;
_verb = "estimated at";
}
else
{
Ticks = ticks;
Count = count;
_verb = "measured";
}
Count = count;
Ticks = ticks;
_verb = sw is MTStopwatchWithSampling ? "estimated at" : "measured";
}

public override string ToString()
Expand Down
6 changes: 3 additions & 3 deletions ModTek/Util/Stopwatch/MTStopwatchWithCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ internal MTStopwatchWithCallback(Action<MTStopwatchStats> callback)
private readonly Action<MTStopwatchStats> _callback;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void AddMeasurement(long elapsedTicks)
protected override void AddMeasurement(long elapsedTicks, long delta)
{
var count = Interlocked.Increment(ref _count);
var count = Interlocked.Add(ref _count, delta);
var ticks = Interlocked.Add(ref _ticks, elapsedTicks);
if ((count & CallbackFastModuloMask) == 0)
{
_callback.Invoke(new MTStopwatchStats(this, ticks, count));
_callback.Invoke(new MTStopwatchStats(this, count, ticks));
}
}
private const long CallbackEveryMeasurement = 1 << 14; // every 16k, FastModulo requires base 2
Expand Down
6 changes: 3 additions & 3 deletions ModTek/Util/Stopwatch/MTStopwatchWithSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ internal sealed class MTStopwatchWithSampling : MTStopwatch
{
internal MTStopwatchWithSampling(uint sampling)
{
Sampling = sampling;
_sampling = sampling;
_sampleIfRandomSmallerOrEqualsTo = ulong.MaxValue / sampling;
}
internal readonly uint Sampling;
private readonly uint _sampling;
private readonly ulong _sampleIfRandomSmallerOrEqualsTo;
private readonly FastRandom _random = new();

Expand All @@ -20,7 +20,7 @@ internal override void EndMeasurement(long start)
// fast random is much faster, runs unrolled and therefore in parallel on the CPU
if (_random.NextUInt64() <= _sampleIfRandomSmallerOrEqualsTo)
{
AddMeasurement(GetTimestamp() - start);
AddMeasurement((GetTimestamp() - start) * _sampling, _sampling);
}
}
}

0 comments on commit 9f9c350

Please sign in to comment.