Files
ThreadPilot/Services/CpuSelectionMigrationService.cs
T
Prime Build 731a537015 CPU topology v2 phase 6: migrate profiles and core masks to CpuSelection (#12)
Migrate profiles and core masks to CpuSelection

Add a conservative compatibility layer for moving persisted profiles and core masks toward topology-aware CpuSelection while preserving legacy fields.

- Add profile schema version constants and CpuSelection migration metadata
- Add ProcessProfileSnapshot with legacy ProcessorAffinity plus optional CpuSelection
- Extend ProfileModel and CoreMask with CpuSelection fields without removing legacy data
- Add CpuSelectionMigrationService for legacy affinity mask and bool core mask conversion
- Preserve legacy mask build-back only when CpuSelection is safely representable
- Wire profile save/load to read and persist CpuSelection when topology is available
- Wire core mask load/save to attach CpuSelection metadata while keeping BoolMask
- Return failure when CpuSelection profile application fails
- Add tests for CPU64 alias protection, multi-group safety, topology signature review, legacy/v2 JSON compatibility and profile load behavior

No UI, preset generator, registry/IFEO, priority guardrail, X3D detection, version or tag changes.
2026-05-20 15:32:52 +02:00

169 lines
6.9 KiB
C#

/*
* ThreadPilot - Advanced Windows Process and Power Plan Manager
* Copyright (C) 2025 Prime Build
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace ThreadPilot.Services
{
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,
ReviewRequired = reviewRequired,
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,
ReviewRequired = reviewRequired,
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);
if (savedSignature == null || savedSignature != currentTopology.Signature)
{
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
{
ReviewRequired = this.ShouldRequireReview(
profile.CpuSelection,
profile.CpuSelection.Metadata.TopologySignature,
topology),
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;
}
}
}