9875f7cc4f
ReviewRequired was not merely unread - it had the wrong shape. It is a bit persisted at save time recording a judgement that can only be made at load time, against whatever CPU is present then. Three writers, no readers: the mask path, MigrateProcessProfile (whose Save/Load Profile commands are bound in no XAML), and CpuPresetGenerator (registered in DI with no consumers). The field goes. The question it was asking does not, so ShouldRequireReview stays and finally gets a caller - saved rules, which nothing compared against the running machine. A rule pinning cores that no longer exist did not announce itself; it failed at apply time through a path that reports it as the process having exited. The Saved process rules tab now names those rules, and their cores can be set again from a mask without leaving the page. ShouldRequireReview no longer compares topology signatures by record equality. That included fields which can differ between two readings of the same machine, and a warning that fires on everything teaches people to ignore it. It now compares what actually changes the meaning of "core 9": the chip and the shape of it. Old files keep the serialized flag; it is ignored on read and dropped on the next save. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
263 lines
11 KiB
C#
263 lines
11 KiB
C#
namespace ThreadPilot.Core.Tests
|
|
{
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
using ThreadPilot.Models;
|
|
using ThreadPilot.Services;
|
|
|
|
public sealed class CpuSelectionMigrationServiceTests
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
ReadCommentHandling = JsonCommentHandling.Skip,
|
|
AllowTrailingCommas = true,
|
|
};
|
|
|
|
[Fact]
|
|
public void MigrateFromLegacyAffinityMask_WithSingleGroupBelow64_SelectsExpectedProcessors()
|
|
{
|
|
var topology = CreateTopology(8);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var result = service.MigrateFromLegacyAffinityMask(0b0101, topology);
|
|
|
|
Assert.Equal([0, 2], result.Selection.GlobalLogicalProcessorIndexes);
|
|
Assert.True(result.Metadata.CreatedFromLegacyAffinityMask);
|
|
Assert.False(service.ShouldRequireReview(result.Selection, result.Metadata.TopologySignature, topology));
|
|
Assert.Equal(0b0101, service.BuildLegacyAffinityMaskIfRepresentable(result.Selection));
|
|
}
|
|
|
|
[Fact]
|
|
public void MigrateFromLegacyAffinityMask_OnTopologyAbove64_DoesNotAliasCpu64ToCpu0()
|
|
{
|
|
var topology = CreateTopology(65);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var result = service.MigrateFromLegacyAffinityMask(1, topology);
|
|
var cpu64Selection = CpuSelection.FromProcessors(
|
|
[topology.LogicalProcessors.Single(processor => processor.GlobalIndex == 64)],
|
|
topology);
|
|
|
|
Assert.Equal([0], result.Selection.GlobalLogicalProcessorIndexes);
|
|
Assert.DoesNotContain(result.Selection.LogicalProcessors, processor => processor.GlobalIndex == 64);
|
|
Assert.Null(service.BuildLegacyAffinityMaskIfRepresentable(cpu64Selection));
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildLegacyAffinityMaskIfRepresentable_WithGroupOneCpuZero_ReturnsNull()
|
|
{
|
|
var group1Cpu0 = new ProcessorRef(1, 0, 64);
|
|
var topology = CpuTopologySnapshot.Create([new ProcessorRef(0, 0, 0), group1Cpu0]);
|
|
var selection = CpuSelection.FromProcessors([group1Cpu0], topology);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var legacyMask = service.BuildLegacyAffinityMaskIfRepresentable(selection);
|
|
|
|
Assert.Null(legacyMask);
|
|
}
|
|
|
|
[Fact]
|
|
public void MigrateFromLegacyCoreMask_WhenShorterThanTopology_SelectsPresentIndexesAndRequiresReview()
|
|
{
|
|
var topology = CreateTopology(4);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var result = service.MigrateFromLegacyCoreMask([true, false], topology);
|
|
|
|
Assert.Equal([0], result.Selection.GlobalLogicalProcessorIndexes);
|
|
Assert.True(result.Metadata.CreatedFromLegacyCoreMask);
|
|
Assert.Contains("length differs", result.Metadata.Reason, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void MigrateFromLegacyCoreMask_WhenLongerThanTopology_IgnoresExtrasAndRequiresReview()
|
|
{
|
|
var topology = CreateTopology(2);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var result = service.MigrateFromLegacyCoreMask([false, true, true, true], topology);
|
|
|
|
Assert.Equal([1], result.Selection.GlobalLogicalProcessorIndexes);
|
|
Assert.Contains("length differs", result.Metadata.Reason, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void MigrateProcessProfile_WithExistingCpuSelection_DoesNotOverwriteFromLegacyMask()
|
|
{
|
|
var topology = CreateTopology(4);
|
|
var existingSelection = CpuSelection.FromProcessors([topology.LogicalProcessors[1]], topology);
|
|
var profile = new ProcessProfileSnapshot
|
|
{
|
|
ProcessName = "game.exe",
|
|
Priority = ProcessPriorityClass.High,
|
|
ProcessorAffinity = 0b0101,
|
|
CpuSelection = existingSelection,
|
|
};
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var migrated = service.MigrateProcessProfile(profile, topology);
|
|
|
|
Assert.Equal([1], migrated.CpuSelection!.GlobalLogicalProcessorIndexes);
|
|
Assert.Equal(0b0101, migrated.ProcessorAffinity);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldRequireReview_TracksTopologySignatureChanges()
|
|
{
|
|
var topology = CreateTopology(4);
|
|
var changedTopology = CreateTopology(6);
|
|
var selection = CpuSelection.FromProcessors([topology.LogicalProcessors[0]], topology);
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
Assert.False(service.ShouldRequireReview(selection, topology.Signature, topology));
|
|
Assert.True(service.ShouldRequireReview(selection, topology.Signature, changedTopology));
|
|
}
|
|
|
|
[Fact]
|
|
public void PrepareProcessProfileForSave_WithSingleGroupBelow64_SavesLegacyMask()
|
|
{
|
|
var topology = CreateTopology(4);
|
|
var selection = CpuSelection.FromProcessors([topology.LogicalProcessors[0], topology.LogicalProcessors[2]], topology);
|
|
var profile = new ProcessProfileSnapshot
|
|
{
|
|
ProcessName = "game.exe",
|
|
Priority = ProcessPriorityClass.Normal,
|
|
ProcessorAffinity = 0,
|
|
CpuSelection = selection,
|
|
};
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var prepared = service.PrepareProcessProfileForSave(profile, topology);
|
|
|
|
Assert.Equal(CpuAffinityProfileSchemaVersions.CpuSelection, prepared.ProfileSchemaVersion);
|
|
Assert.Equal(0b0101, prepared.ProcessorAffinity);
|
|
Assert.NotNull(prepared.CpuSelection);
|
|
}
|
|
|
|
[Fact]
|
|
public void PrepareProcessProfileForSave_WithCpu64_DoesNotProduceLegacyMask()
|
|
{
|
|
var topology = CreateTopology(65);
|
|
var selection = CpuSelection.FromProcessors([topology.LogicalProcessors[64]], topology);
|
|
var profile = new ProcessProfileSnapshot
|
|
{
|
|
ProcessName = "game.exe",
|
|
Priority = ProcessPriorityClass.Normal,
|
|
ProcessorAffinity = 0b11,
|
|
CpuSelection = selection,
|
|
};
|
|
var service = new CpuSelectionMigrationService();
|
|
|
|
var prepared = service.PrepareProcessProfileForSave(profile, topology);
|
|
|
|
Assert.Equal(0b11, prepared.ProcessorAffinity);
|
|
Assert.Null(service.BuildLegacyAffinityMaskIfRepresentable(prepared.CpuSelection!));
|
|
}
|
|
|
|
[Fact]
|
|
public void LegacyProcessProfileWithoutSchemaVersion_DeserializesAsVersionOne()
|
|
{
|
|
const string json = """
|
|
{
|
|
"processName": "game.exe",
|
|
"priority": 2,
|
|
"processorAffinity": 5
|
|
}
|
|
""";
|
|
|
|
var profile = JsonSerializer.Deserialize<ProcessProfileSnapshot>(json, JsonOptions);
|
|
|
|
Assert.NotNull(profile);
|
|
Assert.Equal(CpuAffinityProfileSchemaVersions.Legacy, profile.ProfileSchemaVersion);
|
|
Assert.Equal(5, profile.ProcessorAffinity);
|
|
}
|
|
|
|
[Fact]
|
|
public void ProcessProfileWithCpuSelection_DeserializesAsVersionTwo()
|
|
{
|
|
var topology = CreateTopology(2);
|
|
var profile = new ProcessProfileSnapshot
|
|
{
|
|
ProcessName = "game.exe",
|
|
Priority = ProcessPriorityClass.High,
|
|
ProcessorAffinity = 1,
|
|
ProfileSchemaVersion = CpuAffinityProfileSchemaVersions.CpuSelection,
|
|
CpuSelection = CpuSelection.FromProcessors([topology.LogicalProcessors[0]], topology),
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(profile);
|
|
var deserialized = JsonSerializer.Deserialize<ProcessProfileSnapshot>(json, JsonOptions);
|
|
|
|
Assert.NotNull(deserialized);
|
|
Assert.Equal(CpuAffinityProfileSchemaVersions.CpuSelection, deserialized.ProfileSchemaVersion);
|
|
Assert.NotNull(deserialized.CpuSelection);
|
|
Assert.Equal([0], deserialized.CpuSelection!.GlobalLogicalProcessorIndexes);
|
|
}
|
|
|
|
[Fact]
|
|
public void LegacyCoreMaskWithoutSchemaVersion_DeserializesAsVersionOne()
|
|
{
|
|
const string json = """
|
|
{
|
|
"id": "mask-1",
|
|
"name": "Legacy mask",
|
|
"description": "legacy",
|
|
"boolMask": [true, false, true],
|
|
"isDefault": false,
|
|
"isEnabled": true
|
|
}
|
|
""";
|
|
|
|
var mask = JsonSerializer.Deserialize<CoreMask>(json, JsonOptions);
|
|
|
|
Assert.NotNull(mask);
|
|
Assert.Equal(CpuAffinityProfileSchemaVersions.Legacy, mask.ProfileSchemaVersion);
|
|
Assert.Equal([true, false, true], mask.BoolMask.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void CoreMaskWithCpuSelection_DeserializesAsVersionTwo()
|
|
{
|
|
var topology = CreateTopology(2);
|
|
var mask = new CoreMask
|
|
{
|
|
Name = "V2 mask",
|
|
ProfileSchemaVersion = CpuAffinityProfileSchemaVersions.CpuSelection,
|
|
CpuSelection = CpuSelection.FromProcessors([topology.LogicalProcessors[1]], topology),
|
|
};
|
|
mask.BoolMask.Add(false);
|
|
mask.BoolMask.Add(true);
|
|
|
|
var json = JsonSerializer.Serialize(mask);
|
|
var deserialized = JsonSerializer.Deserialize<CoreMask>(json, JsonOptions);
|
|
|
|
Assert.NotNull(deserialized);
|
|
Assert.Equal(CpuAffinityProfileSchemaVersions.CpuSelection, deserialized.ProfileSchemaVersion);
|
|
Assert.NotNull(deserialized.CpuSelection);
|
|
Assert.Equal([1], deserialized.CpuSelection!.GlobalLogicalProcessorIndexes);
|
|
Assert.Equal([false, true], deserialized.BoolMask.ToArray());
|
|
}
|
|
|
|
private static CpuTopologySnapshot CreateTopology(int processorCount)
|
|
{
|
|
var processors = Enumerable
|
|
.Range(0, processorCount)
|
|
.Select(index => new ProcessorRef((ushort)(index / 64), (byte)(index % 64), index))
|
|
.ToList();
|
|
|
|
return CpuTopologySnapshot.Create(
|
|
processors,
|
|
signature: new CpuTopologySignature
|
|
{
|
|
CpuBrand = "Synthetic CPU",
|
|
LogicalProcessorCount = processorCount,
|
|
PhysicalCoreCount = processorCount,
|
|
ProcessorGroupCount = Math.Max(1, (processorCount + 63) / 64),
|
|
Source = "Test",
|
|
});
|
|
}
|
|
}
|
|
}
|