namespace ThreadPilot.Core.Tests { using System.Diagnostics; using System.Runtime.InteropServices; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Win32.SafeHandles; using Moq; using ThreadPilot.Models; using ThreadPilot.Platforms.Windows; using ThreadPilot.Services; using Xunit.Abstractions; /// /// Real processes, real ThreadPilot services and real CPU Set / priority calls. Only the topology is /// simulated (this machine's CPU Set IDs split into two L3 groups), so the rollback can be exercised on /// CPUs where Game Boost itself would not act. The children are copies of PING.EXE. /// public sealed class GameSessionNativeIntegrationTests(ITestOutputHelper output) : IDisposable { private const ulong MB = 1024UL * 1024UL; private readonly string root = Path.Combine(Path.GetTempPath(), $"tp-gb-{Guid.NewGuid():N}"); private readonly List children = new(); private readonly ProcessMemoryPriorityService efficiency = new(ProcessMemoryPriorityNativeApi.Instance, NullLogger.Instance); public void Dispose() { foreach (var child in this.children) { try { child.Kill(); child.WaitForExit(5000); } catch { // already gone } child.Dispose(); } try { Directory.Delete(this.root, recursive: true); } catch { // best effort } } [Fact] public async Task RealProcesses_ApplyThenRestore_AndRecoverAfterCrash() { var real = await new WindowsCpuTopologyProvider().GetTopologySnapshotAsync(); var processors = real.LogicalProcessors.ToList(); var cores = processors.Select(p => real.TryGetCoreIndex(p, out var core) ? core : -1).ToList(); var gameCount = processors.Count * 3 / 4; if (processors.Any(p => !real.TryGetCpuSetId(p, out _)) || cores.Take(gameCount).Distinct().Count() < GameBoostPolicy.MinimumGamePhysicalCores || gameCount == processors.Count) { output.WriteLine($"Skipped: {processors.Count} logical CPUs are too few to split into a 6-core game group."); return; } var topology = CpuTopologySnapshot.Create( processors, cpuSetIds: processors.ToDictionary(p => p, p => real.TryGetCpuSetId(p, out var id) ? id : 0U), coreIndexes: processors.Select((p, i) => (p, i)).ToDictionary(x => x.p, x => cores[x.i]), lastLevelCacheIndexes: processors.Select((p, i) => (p, i)).ToDictionary(x => x.p, x => x.i < gameCount ? 0 : 1), lastLevelCacheSizes: new Dictionary { [0] = 96 * MB, [1] = 32 * MB }); var gameCpus = Enumerable.Range(0, gameCount).Select(i => processors[i].GlobalIndex).ToList(); var backgroundCpus = processors.Skip(gameCount).Select(p => p.GlobalIndex).ToList(); var game = this.StartCopyOfPing(Path.Combine(this.root, "steamapps", "common", "FakeGame"), "FakeGame.exe"); var browserPath = Path.Combine(this.root, "apps", "chrome.exe"); var browser = this.StartCopyOfPing(Path.GetDirectoryName(browserPath)!, "chrome.exe"); var journal = Path.Combine(this.root, "journal.json"); // The exact path, so a real browser running on this machine is never touched. var settings = new ApplicationSettingsModel { GameBoostEnabled = true, GameBoostAboveNormalPriority = true, GameBoostMode = GameBoostMode.Isolation, GameBoostBackgroundProcesses = [browserPath], }; // 1. Session: real CPU Sets and priority on both children. var first = this.CreateService(topology, settings, game.Id, journal); await first.TickAsync(); Assert.Equal(gameCpus, ReadDefaultCpuSetIndexes(game, topology)); Assert.Equal(backgroundCpus, ReadDefaultCpuSetIndexes(browser, topology)); Assert.Equal(ProcessPriorityClass.AboveNormal, ReadPriority(game)); Assert.Equal(ProcessEfficiencyMode.Off, await this.ReadEfficiencyAsync(game)); Assert.Equal(ProcessEfficiencyMode.On, await this.ReadEfficiencyAsync(browser)); Assert.True(File.Exists(journal)); // 2. Turned off: everything restored without a restart. settings.GameBoostEnabled = false; await first.ReapplyAsync(); Assert.Empty(ReadDefaultCpuSetIndexes(game, topology)); Assert.Empty(ReadDefaultCpuSetIndexes(browser, topology)); Assert.Equal(ProcessPriorityClass.Normal, ReadPriority(game)); Assert.Equal(ProcessEfficiencyMode.SystemManaged, await this.ReadEfficiencyAsync(game)); Assert.Equal(ProcessEfficiencyMode.SystemManaged, await this.ReadEfficiencyAsync(browser)); Assert.False(File.Exists(journal)); // 3. On again, then a "crash": the service is dropped without StopAsync. settings.GameBoostEnabled = true; await first.TickAsync(); Assert.Equal(backgroundCpus, ReadDefaultCpuSetIndexes(browser, topology)); first.Dispose(); // 4. Next start reads the journal and undoes the crashed run. using var second = this.CreateService(topology, new ApplicationSettingsModel(), game.Id, journal); await second.StartAsync(); Assert.Empty(ReadDefaultCpuSetIndexes(game, topology)); Assert.Empty(ReadDefaultCpuSetIndexes(browser, topology)); Assert.Equal(ProcessPriorityClass.Normal, ReadPriority(game)); Assert.Equal(ProcessEfficiencyMode.SystemManaged, await this.ReadEfficiencyAsync(browser)); Assert.False(File.Exists(journal)); await second.StopAsync(); // 5. The game exits: its held handle ends the session and restores without waiting for a tick. using var third = this.CreateService(topology, settings, game.Id, journal); await third.TickAsync(); Assert.Equal(backgroundCpus, ReadDefaultCpuSetIndexes(browser, topology)); game.Kill(); await WaitUntilAsync(() => !File.Exists(journal)); Assert.Empty(ReadDefaultCpuSetIndexes(browser, topology)); Assert.Null(third.ActiveGameName); } [Fact] public async Task Probe_HoldsTheSameProcess_ReportsItsExit_AndRejectsAnotherStartTime() { var game = this.StartCopyOfPing(Path.Combine(this.root, "game"), "FakeGame.exe"); var probe = new GameBoostProcessProbe(); Assert.True(probe.TryGetIdentity(game.Id, out var startTicks, out _)); var exited = new TaskCompletionSource(); Assert.Null(probe.TryOpen(game.Id, startTicks + 1, () => Task.CompletedTask)); // the PID now names another process using var handle = probe.TryOpen(game.Id, startTicks, () => { exited.TrySetResult(); return Task.CompletedTask; }); Assert.NotNull(handle); Assert.True(handle.TryGetCpuTime(out _)); Assert.False(exited.Task.IsCompleted); game.Kill(); await exited.Task.WaitAsync(TimeSpan.FromSeconds(10)); } [Fact] public void Probe_HoldsAProtectedProcess_WithLimitedRights() { // A protected (PPL) process refuses full access, as an anti-cheat-protected game may: only the minimal // rights an anti-cheat leaves to outside tools can open it, and only those are held for a session. var csrss = Process.GetProcessesByName("csrss").First(); var probe = new GameBoostProcessProbe(); if (!probe.TryGetIdentity(csrss.Id, out var startTicks, out _)) { output.WriteLine("Skipped: not elevated, csrss cannot be queried."); return; } using var handle = probe.TryOpen(csrss.Id, startTicks, () => Task.CompletedTask); Assert.NotNull(handle); Assert.True(handle.TryGetCpuTime(out var cpu)); Assert.True(cpu > TimeSpan.Zero); } private GameSessionService CreateService(CpuTopologySnapshot topology, ApplicationSettingsModel settings, int gamePid, string journal) { var settingsService = new Mock(); settingsService.SetupGet(s => s.Settings).Returns(settings); var topologyProvider = new Mock(); topologyProvider.Setup(p => p.GetTopologySnapshotAsync(It.IsAny())).ReturnsAsync(topology); var cpuTopologyService = new Mock(); cpuTopologyService.Setup(s => s.DetectTopologyAsync()).ReturnsAsync(new CpuTopologyModel { CpuBrand = "AMD (simulated dual L3)" }); var foreground = new Mock(); foreground.Setup(f => f.TryGetForegroundProcessId()).Returns(gamePid); var rules = new Mock(); rules.Setup(s => s.LoadAsync()).ReturnsAsync(Array.Empty()); var processService = new ProcessService(NullLogger.Instance); var affinityService = new AffinityApplyService( processService, new CpuTopologyService(NullLogger.Instance), NullLogger.Instance); return new GameSessionService( settingsService.Object, topologyProvider.Object, cpuTopologyService.Object, foreground.Object, processService, affinityService, rules.Object, new PersistentProcessRuleMatcher(), new ProcessFilterService(), new ProcessPolicyApplyGate(), new GameBoostProcessProbe(), this.efficiency, NullLogger.Instance, journal); } private Process StartCopyOfPing(string directory, string fileName) { Directory.CreateDirectory(directory); var path = Path.Combine(directory, fileName); File.Copy(Path.Combine(Environment.SystemDirectory, "PING.EXE"), path); var process = Process.Start(new ProcessStartInfo(path, "-n 120 127.0.0.1") { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, })!; this.children.Add(process); return process; } private static async Task WaitUntilAsync(Func condition) { for (var i = 0; i < 100 && !condition(); i++) { await Task.Delay(100); } Assert.True(condition()); } private Task ReadEfficiencyAsync(Process process) => this.efficiency.GetEfficiencyModeAsync(new ProcessModel { ProcessId = process.Id, Name = process.ProcessName }); private static ProcessPriorityClass ReadPriority(Process process) { process.Refresh(); return process.PriorityClass; } // Read straight from Windows, not through ThreadPilot's handler. private static List ReadDefaultCpuSetIndexes(Process process, CpuTopologySnapshot topology) { using var handle = OpenProcess(0x1000, false, process.Id); // PROCESS_QUERY_LIMITED_INFORMATION Assert.False(handle.IsInvalid); GetProcessDefaultCpuSets(handle, null, 0, out var count); var ids = new uint[count]; if (count > 0) { Assert.True(GetProcessDefaultCpuSets(handle, ids, count, out count)); } var byId = topology.LogicalProcessors.ToDictionary(p => topology.TryGetCpuSetId(p, out var id) ? id : 0U, p => p.GlobalIndex); return ids.Take((int)count).Select(id => byId[id]).OrderBy(i => i).ToList(); } [DllImport("kernel32.dll", SetLastError = true)] private static extern SafeProcessHandle OpenProcess(uint access, bool inherit, int processId); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool GetProcessDefaultCpuSets(SafeProcessHandle process, uint[]? cpuSetIds, uint cpuSetIdCount, out uint requiredIdCount); } }