Files
poli0981andClaude Fable 5.2 5694ec91f7 fix(telemetry): the poller keeps its period, and sensor_blobs.hz is the rate the samples had (D146) (#452)
* feat(overlay): two more members of NVIDIA's latency report - the driver's active GPU time and its GPU frame time - and every frame-generation multiplier a session saw; four reserved words, layout still 5; a read left by the session before is dropped (D142, D144; in no release yet)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* feat(advanced): the driver's active GPU time and GPU frame time as two lines of their own; the GPU render label says what it includes; the multiplier row lists every value seen; the texts say two real games were read - Privacy Policy 2.21, Disclaimer 2.17 (D142-D145; in no release yet)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(telemetry): the poller keeps its period - it waited the whole interval after a poll's work, and on the owner's PC a second was 1.32 s; sensor_blobs.hz is the rate the samples had, not the constant 1 (D146; in no release yet)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 11:06:21 +07:00

302 lines
13 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FluentAssertions;
using FrameLedger.Application.Telemetry;
using FrameLedger.Infrastructure.Telemetry;
namespace FrameLedger.Infrastructure.Tests.Telemetry;
/// <summary>
/// The 1 Hz thread's contract: what a tick queues, what a drain returns, what a full queue does.
/// The thread itself is exercised once; every other case drives <see cref="TelemetryPoller.PollOnce"/>.
/// </summary>
public sealed class TelemetryPollerTests
{
private static readonly DateTimeOffset _t0 = new(2026, 9, 9, 12, 0, 0, TimeSpan.Zero);
private sealed class ManualTimeProvider : TimeProvider
{
public long Ticks { get; set; } = 1_000_000;
public override DateTimeOffset GetUtcNow() => _t0;
public override long GetTimestamp() => Ticks;
}
/// <summary>
/// beta.13: the adapter the game presents on reaches the source — the real L1 switches to a LUID DXGI listed — and a source
/// that cannot follow adapters answers false rather than pretending.
/// </summary>
[Fact]
public void TheGamesAdapterReachesALayerThatCanFollowIt()
{
using var memory = new FakeMemoryCounter(_ => () => 1024);
using var l1 = new BaselineTelemetrySource(new FakeDxgi(FakeDxgi.Identity("first", 0x10), FakeDxgi.Identity("second", 0x20)), memory, new ManualTimeProvider());
using var composite = new CompositeTelemetrySource([l1]);
using var poller = new TelemetryPoller(composite, new TelemetryPollerOptions(), new ManualTimeProvider());
poller.FollowAdapter(0x20).Should().BeTrue();
l1.Selected!.Name.Should().Be("second");
poller.FollowAdapter(0xDEAD).Should().BeFalse("DXGI never listed it");
using var layer = new FakeLayer(TelemetryLayer.Lhm);
using var plain = new TelemetryPoller(layer, new TelemetryPollerOptions(), new ManualTimeProvider());
plain.FollowAdapter(0x20).Should().BeFalse("a layer that reads its own GPU is told a vendor by the composite, not a LUID");
}
[Fact]
public void ATickStampsTheLayersSampleWithTheCallersTimestampClock()
{
var clock = new ManualTimeProvider { Ticks = 42_000 };
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), clock);
poller.PollOnce();
clock.Ticks = 52_000;
poller.PollOnce();
var drained = new List<TelemetrySample>();
poller.Drain(drained).Should().Be(2);
drained.Select(s => s.QpcTicks).Should().Equal(42_000, 52_000);
drained.Should().AllSatisfy(s => s.Sample.LoadPct.Should().Be(50), "every tick is queued, duplicates included");
poller.Queued.Should().Be(0);
poller.Drain(drained).Should().Be(0);
}
private sealed class FakeSystem(SystemReading? reading) : ISystemTelemetrySource
{
public bool Disposed { get; private set; }
public bool CpuTemperatureAvailable => reading?.CpuTempC is not null;
public bool TryRead(out SystemReading read)
{
read = reading ?? default;
return reading is not null;
}
public void Dispose() => Disposed = true;
}
[Fact]
public void TheMachinesReadingRidesTheSameTickAndATickOnlyItAnsweredIsStillQueued()
{
var clock = new ManualTimeProvider { Ticks = 7 };
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
using var system = new FakeSystem(new SystemReading(CpuLoadPct: 37.5, RamUsedMb: 9000, CpuTempC: null));
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), clock, ownsSource: true, system);
poller.PollOnce();
layer.Publish(null);
poller.PollOnce();
var drained = new List<TelemetrySample>();
poller.Drain(drained).Should().Be(2);
drained[0].Sample.LoadPct.Should().Be(50);
drained[0].System.CpuLoadPct.Should().Be(37.5);
drained[1].Sample.Layer.Should().Be(TelemetryLayer.None, "the GPU layers said nothing on this tick; the placeholder names no layer and no field");
drained[1].Sample.PresentFields.Should().Be(GpuCapabilities.None);
drained[1].System.RamUsedMb.Should().Be(9000);
poller.CpuTemperatureAvailable.Should().BeFalse();
poller.Dispose();
system.Disposed.Should().BeTrue("the poller owns the system source with the layers");
}
private sealed class FakeGame : IGameMemorySource
{
public List<GameProcess> Followed { get; } = [];
public ProcessReading? Next { get; set; }
public bool Disposed { get; private set; }
public void Follow(GameProcess target) => Followed.Add(target);
public bool TryRead(out ProcessReading reading)
{
reading = Next ?? default;
return Next is not null;
}
public void Dispose() => Disposed = true;
}
/// <summary>
/// beta.12 (D43): the game process's memory rides the same tick, a tick only it answered is still queued, the target the
/// capture loop names reaches the game source, and the poller owns it with the layers.
/// </summary>
[Fact]
public void TheGamesMemoryRidesTheSameTickAndTheFollowedProcessReachesItsSource()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
using var game = new FakeGame { Next = new ProcessReading(8192, 256, 6000, 6500, 7000, 1, ProcessReadingSources.Counters) };
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new ManualTimeProvider(), ownsSource: true, system: null, game: game);
GameProcess target = GameProcess.Unpinned([new GameProcessId(4242, DateTimeOffset.UnixEpoch)]);
poller.Follow(target);
layer.Publish(null);
poller.PollOnce();
game.Next = null;
poller.PollOnce();
var drained = new List<TelemetrySample>();
poller.Drain(drained).Should().Be(1, "the second tick: nothing answered at all, so nothing is queued");
drained[0].Game.VramDedicatedMb.Should().Be(8192);
drained[0].Sample.Layer.Should().Be(TelemetryLayer.None, "only the game answered; the placeholder names no GPU layer");
game.Followed.Should().Equal(target);
poller.Dispose();
game.Disposed.Should().BeTrue("the poller owns the game source with the layers");
}
[Fact]
public void WithNoSystemSourceEverySampleCarriesAnEmptyReading()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new ManualTimeProvider());
poller.PollOnce();
var drained = new List<TelemetrySample>();
poller.Drain(drained);
drained.Should().ContainSingle().Which.System.IsEmpty.Should().BeTrue();
}
[Fact]
public void ALayerWithNothingToSayQueuesNothing()
{
using var layer = new FakeLayer(TelemetryLayer.Baseline).Publish(null);
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new ManualTimeProvider());
poller.PollOnce();
poller.Queued.Should().Be(0);
layer.Reads.Should().Be(1);
}
[Fact]
public void AFullQueueDropsTheOldestAndCountsIt()
{
var clock = new ManualTimeProvider();
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 1));
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions { QueueCapacity = 3 }, clock);
for (int i = 1; i <= 5; i++)
{
clock.Ticks = i;
poller.PollOnce();
}
var drained = new List<TelemetrySample>();
poller.Drain(drained);
// The newest survive; a stalled consumer loses the oldest, like the ring.
drained.Select(s => s.QpcTicks).Should().Equal(3L, 4L, 5L);
poller.Dropped.Should().Be(2);
poller.Queued.Should().Be(0);
}
[Fact]
public void TheDescriptorIsTheCompositesOrTheSingleLayersName()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
using var l2 = new FakeLayer(TelemetryLayer.Lhm);
using var single = new TelemetryPoller(l2, new TelemetryPollerOptions(), new ManualTimeProvider());
using var merged = new CompositeTelemetrySource([l1, l2]);
using var composite = new TelemetryPoller(merged, new TelemetryPollerOptions(), new ManualTimeProvider());
single.Descriptor.Should().Be("lhm");
composite.Descriptor.Should().Be("l1+lhm");
l2.IsDisabled = true;
single.Descriptor.Should().BeEmpty();
composite.Descriptor.Should().Be("l1");
}
[Fact]
public void FasterThanTheLayersAnswerIsRefused()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
Action fast = () => new TelemetryPoller(layer,
new TelemetryPollerOptions { Interval = TimeSpan.FromMilliseconds(100) }, new ManualTimeProvider()).Dispose();
Action empty = () => new TelemetryPoller(layer,
new TelemetryPollerOptions { QueueCapacity = 0 }, new ManualTimeProvider()).Dispose();
fast.Should().Throw<ArgumentOutOfRangeException>();
empty.Should().Throw<ArgumentOutOfRangeException>();
}
[Fact]
public void TheNextTickIsDueAtAWholeIntervalFromTheStartHoweverLongThePollTook()
{
TimeSpan second = TimeSpan.FromSeconds(1);
// D146: a poll that took 320 ms is followed 680 ms later, not a whole second later.
TelemetryPoller.UntilNextTick(TimeSpan.FromMilliseconds(320), second).Should().Be(TimeSpan.FromMilliseconds(680));
TelemetryPoller.UntilNextTick(TimeSpan.FromMilliseconds(5320), second).Should().Be(TimeSpan.FromMilliseconds(680));
// A poll that overran two periods: the next tick still to come, and the two it missed are not made up.
TelemetryPoller.UntilNextTick(TimeSpan.FromMilliseconds(2500), second).Should().Be(TimeSpan.FromMilliseconds(500));
// On the boundary, and before any time has passed: a whole interval - never nothing, never more.
TelemetryPoller.UntilNextTick(second, second).Should().Be(second);
TelemetryPoller.UntilNextTick(TimeSpan.Zero, second).Should().Be(second);
TelemetryPoller.UntilNextTick(TimeSpan.FromSeconds(-3), second).Should().Be(second);
for (int ms = 0; ms < 5000; ms += 37)
{
TelemetryPoller.UntilNextTick(TimeSpan.FromMilliseconds(ms), TimeSpan.FromMilliseconds(500))
.Should().BeGreaterThan(TimeSpan.Zero).And.BeLessThanOrEqualTo(TimeSpan.FromMilliseconds(500));
}
Action none = () => TelemetryPoller.UntilNextTick(second, TimeSpan.Zero);
none.Should().Throw<ArgumentOutOfRangeException>();
}
[Fact]
public void TheThreadKeepsItsPeriodWhenAPollTakesTime()
{
// A layer that takes 300 ms to answer, polled every 500 ms: the samples are 500 ms apart, where waiting the
// whole interval after the work made them 800 ms apart.
using var layer = new FakeLayer(TelemetryLayer.Lhm);
GpuSample sample = layer.Sample(_t0, load: 1);
layer.Script(() =>
{
Thread.Sleep(300);
return sample;
});
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions { Interval = TelemetryPollerOptions.MinimumInterval }, TimeProvider.System);
poller.Start();
SpinWait.SpinUntil(() => poller.Queued >= 7, TimeSpan.FromSeconds(20)).Should().BeTrue("seven ticks, half a second apart");
poller.Dispose();
var samples = new List<TelemetrySample>();
poller.Drain(samples);
double apart = TimeProvider.System.GetElapsedTime(samples[0].QpcTicks, samples[6].QpcTicks).TotalMilliseconds / 6;
apart.Should().BeLessThan(700, "the period is the interval (500 ms), not the interval plus the work (800 ms)");
}
[Fact]
public void TheThreadPollsAtItsIntervalAndReturnsOnDispose()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 1));
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions { Interval = TelemetryPollerOptions.MinimumInterval }, TimeProvider.System);
poller.Start();
poller.Start();
SpinWait.SpinUntil(() => poller.Queued >= 2, TimeSpan.FromSeconds(10)).Should().BeTrue("500 ms cadence, two ticks well inside 10 s");
poller.Dispose();
int after = layer.Reads;
Thread.Sleep(TelemetryPollerOptions.MinimumInterval * 2);
layer.Reads.Should().Be(after, "the thread stopped reading");
layer.Disposed.Should().BeFalse("the poller does not own the source");
}
}