Files
ThreadPilot/Services/CpuSelectionMigrationService.cs
PrimeBuild-pc 9875f7cc4f refactor: delete ReviewRequired, keep the check, point it at saved rules
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>
2026-08-27 00:43:52 +02:00

156 lines
6.9 KiB
C#

namespace ThreadPilot.Services
{
using System;
using ThreadPilot.Models;
public sealed class CpuSelectionMigrationService
{
public CpuSelectionMigrationResult MigrateFromLegacyAffinityMask(
long mask,
CpuTopologySnapshot topology)
{
ArgumentNullException.ThrowIfNull(topology);
var selection = CpuSelection.FromLegacyAffinityMask(mask, topology);
var reviewRequired = topology.Signature.LogicalProcessorCount > 64 ||
topology.Signature.ProcessorGroupCount > 1;
return new CpuSelectionMigrationResult(
selection,
new CpuSelectionMigrationMetadata
{
CreatedFromLegacyAffinityMask = true,
MigrationConfidence = reviewRequired ? "Medium" : "High",
Reason = reviewRequired
? "Migrated from a legacy affinity mask on a topology that may not be fully represented by legacy masks."
: "Migrated from a legacy affinity mask.",
TopologySignature = topology.Signature,
SourceLegacyAffinityMask = mask,
});
}
public CpuSelectionMigrationResult MigrateFromLegacyCoreMask(
IReadOnlyList<bool> coreMask,
CpuTopologySnapshot topology)
{
ArgumentNullException.ThrowIfNull(coreMask);
ArgumentNullException.ThrowIfNull(topology);
var orderedProcessors = topology.LogicalProcessors
.OrderBy(processor => processor.GlobalIndex)
.ThenBy(processor => processor.Group)
.ThenBy(processor => processor.LogicalProcessorNumber)
.ToList();
var selectedProcessors = orderedProcessors
.Take(Math.Min(coreMask.Count, orderedProcessors.Count))
.Where((_, index) => coreMask[index])
.ToList();
var reviewRequired = coreMask.Count != orderedProcessors.Count;
var selection = CpuSelection.FromProcessors(
selectedProcessors,
topology,
"Migrated from legacy core mask");
return new CpuSelectionMigrationResult(
selection,
new CpuSelectionMigrationMetadata
{
CreatedFromLegacyCoreMask = true,
MigrationConfidence = reviewRequired ? "Medium" : "High",
Reason = reviewRequired
? "Migrated from a legacy core mask whose length differs from the current topology."
: "Migrated from a legacy core mask.",
TopologySignature = topology.Signature,
});
}
public long? BuildLegacyAffinityMaskIfRepresentable(CpuSelection selection) =>
CpuSelection.ToLegacyAffinityMaskOrNull(selection);
public bool ShouldRequireReview(
CpuSelection selection,
CpuTopologySignature? savedSignature,
CpuTopologySnapshot currentTopology)
{
ArgumentNullException.ThrowIfNull(selection);
ArgumentNullException.ThrowIfNull(currentTopology);
// Deliberately not full record equality: the signature carries fields that can differ
// between two readings of the same machine, and flagging every saved selection on one
// of those would train the user to ignore the warning. What actually changes the
// meaning of "core 9" is a different chip or a different shape of one.
if (savedSignature != null &&
(!string.Equals(savedSignature.CpuBrand, currentTopology.Signature.CpuBrand, StringComparison.OrdinalIgnoreCase) ||
savedSignature.LogicalProcessorCount != currentTopology.Signature.LogicalProcessorCount ||
savedSignature.PhysicalCoreCount != currentTopology.Signature.PhysicalCoreCount ||
savedSignature.ProcessorGroupCount != currentTopology.Signature.ProcessorGroupCount))
{
return true;
}
var currentProcessors = currentTopology.LogicalProcessors.ToHashSet();
return selection.LogicalProcessors.Any(processor => !currentProcessors.Contains(processor));
}
public ProcessProfileSnapshot MigrateProcessProfile(
ProcessProfileSnapshot profile,
CpuTopologySnapshot topology)
{
ArgumentNullException.ThrowIfNull(profile);
ArgumentNullException.ThrowIfNull(topology);
if (profile.CpuSelection != null)
{
profile.ProfileSchemaVersion = CpuAffinityProfileSchemaVersions.CpuSelection;
profile.CpuSelectionMigration ??= new CpuSelectionMigrationMetadata
{
MigrationConfidence = "High",
Reason = "Profile already contains a CpuSelection.",
TopologySignature = profile.CpuSelection.Metadata.TopologySignature,
SourceLegacyAffinityMask = profile.ProcessorAffinity,
};
return profile;
}
var migrated = this.MigrateFromLegacyAffinityMask(profile.ProcessorAffinity, topology);
profile.ProfileSchemaVersion = CpuAffinityProfileSchemaVersions.CpuSelection;
profile.CpuSelection = migrated.Selection;
profile.CpuSelectionMigration = migrated.Metadata;
return profile;
}
public ProcessProfileSnapshot PrepareProcessProfileForSave(
ProcessProfileSnapshot profile,
CpuTopologySnapshot topology)
{
ArgumentNullException.ThrowIfNull(profile);
ArgumentNullException.ThrowIfNull(topology);
if (profile.CpuSelection == null)
{
this.MigrateProcessProfile(profile, topology);
}
profile.ProfileSchemaVersion = CpuAffinityProfileSchemaVersions.CpuSelection;
if (profile.CpuSelection != null)
{
var legacyMask = this.BuildLegacyAffinityMaskIfRepresentable(profile.CpuSelection);
if (legacyMask.HasValue)
{
profile.ProcessorAffinity = legacyMask.Value;
}
}
profile.CpuSelectionMigration ??= new CpuSelectionMigrationMetadata
{
MigrationConfidence = "High",
Reason = "Saved with CpuSelection profile schema.",
TopologySignature = topology.Signature,
SourceLegacyAffinityMask = profile.ProcessorAffinity,
};
return profile;
}
}
}