fix: one save path for a saved rule's cores

The rules editor's core mask went through a new update path that reported
success without saving when no affinity payload was built, and kept a
legacy mask next to a CPU selection. Merge it with the old single-purpose
method (now unused), keep its checks, and localize the undo message.
This commit is contained in:
PrimeBuild-pc
2026-10-14 17:08:41 +02:00
parent 2eeec21702
commit 7700a2ae17
9 changed files with 20 additions and 85 deletions
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Löschen</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Regel bearbeiten</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Kerne</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Kerne aus dieser Maske übernehmen</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">CPU-Priorität</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Speicherpriorität</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">E/A-Priorität</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Letzte Prioritätsänderung rückgängig machen</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">Prozesse angezeigt</sys:String>
<sys:String x:Key="Rules_PendingChanges">Nicht gespeicherte Änderungen</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Vorherige Priorität wiederhergestellt.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Delete</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Edit rule</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Cores</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Set cores from this mask</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">CPU priority</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Memory priority</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">I/O priority</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Undo last priority change</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">processes shown</sys:String>
<sys:String x:Key="Rules_PendingChanges">Unsaved changes</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Previous priority restored.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Eliminar</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Editar regla</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Núcleos</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Establecer núcleos desde esta máscara</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">Prioridad de CPU</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Prioridad de memoria</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">Prioridad de E/S</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Deshacer el último cambio de prioridad</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">procesos mostrados</sys:String>
<sys:String x:Key="Rules_PendingChanges">Cambios sin guardar</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Prioridad anterior restaurada.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Supprimer</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Modifier la règle</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Cœurs</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Définir les cœurs depuis ce masque</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">Priorité CPU</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Priorité mémoire</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">Priorité E/S</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Annuler la dernière modification de priorité</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">processus affichés</sys:String>
<sys:String x:Key="Rules_PendingChanges">Modifications non enregistrées</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Priorité précédente restaurée.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Elimina</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Modifica regola</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Core</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Imposta i core da questa maschera</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">Priorità CPU</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Priorità memoria</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">Priorità I/O</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Annulla ultima modifica di priorità</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">processi visibili</sys:String>
<sys:String x:Key="Rules_PendingChanges">Modifiche non salvate</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Priorità precedente ripristinata.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">Удалить</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">Изменить правило</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">Ядра</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">Задать ядра из этой маски</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">Приоритет ЦП</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">Приоритет памяти</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">Приоритет ввода-вывода</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">Отменить последнее изменение приоритета</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">процессов показано</sys:String>
<sys:String x:Key="Rules_PendingChanges">Несохранённые изменения</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">Предыдущий приоритет восстановлен.</sys:String>
</ResourceDictionary>
+1 -1
View File
@@ -247,7 +247,6 @@
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleDelete">删除</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleEditor">编辑规则</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCores">核心</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleApplyMask">从此掩码设置核心</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleCpuPriority">CPU 优先级</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleMemoryPriority">内存优先级</sys:String>
<sys:String x:Key="ProcessPowerPlanAssociationView_SavedRuleIoPriority">I/O 优先级</sys:String>
@@ -776,4 +775,5 @@
<sys:String x:Key="ProcessView_UndoPriority">撤销上次优先级更改</sys:String>
<sys:String x:Key="ProcessView_VisibleCountSuffix">个进程已显示</sys:String>
<sys:String x:Key="Rules_PendingChanges">未保存的更改</sys:String>
<sys:String x:Key="ProcessView_PriorityRestored">已恢复之前的优先级。</sys:String>
</ResourceDictionary>
+7 -72
View File
@@ -40,11 +40,6 @@ namespace ThreadPilot.Services
IReadOnlyList<bool>? coreSelection,
CancellationToken cancellationToken = default);
Task<ProcessRuleCreationResult> UpdateRuleCoreSelectionAsync(
string ruleId,
IReadOnlyList<bool> coreSelection,
CancellationToken cancellationToken = default);
Task<IReadOnlyList<string>> GetRulesNeedingTopologyReviewAsync(
CancellationToken cancellationToken = default);
}
@@ -256,15 +251,20 @@ namespace ThreadPilot.Services
if (coreSelection != null)
{
var affinity = await this.BuildAffinityPayloadFromCoreSelectionAsync(coreSelection, "Saved rule editor", cancellationToken).ConfigureAwait(false);
if (!affinity.Success || affinity.Payload == null)
if (!affinity.Success)
{
return affinity;
}
if (affinity.Payload == null)
{
return ProcessRuleCreationResult.Failed("NoActionableRulePayload", NoCurrentSettingsMessage);
}
updated = updated with
{
CpuSelection = affinity.Payload.CpuSelection,
LegacyAffinityMask = affinity.Payload.LegacyAffinityMask,
LegacyAffinityMask = HasSelectionPayload(affinity.Payload.CpuSelection) ? null : affinity.Payload.LegacyAffinityMask,
ApplyAffinityOnStart = true,
};
}
@@ -288,71 +288,6 @@ namespace ThreadPilot.Services
};
}
// Setting a rule's cores without the process being around to select. The bits come from a
// saved CPU mask, and go through the same migration path the Process tab uses, so a rule
// edited here is indistinguishable from one saved there.
public async Task<ProcessRuleCreationResult> UpdateRuleCoreSelectionAsync(
string ruleId,
IReadOnlyList<bool> coreSelection,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(coreSelection);
if (string.IsNullOrWhiteSpace(ruleId))
{
return ProcessRuleCreationResult.Failed("NoSavedRuleToUpdate", NoSavedRuleMessage);
}
var affinityPayload = await this.BuildAffinityPayloadFromCoreSelectionAsync(
coreSelection,
"Set from a saved CPU mask in the Rules tab",
cancellationToken).ConfigureAwait(false);
if (!affinityPayload.Success)
{
return affinityPayload;
}
if (affinityPayload.Payload == null)
{
return ProcessRuleCreationResult.Failed("NoActionableRulePayload", NoCurrentSettingsMessage);
}
var rules = (await this.ruleStore.LoadAsync().ConfigureAwait(false)).ToList();
var existingIndex = rules.FindIndex(candidate => string.Equals(candidate.Id, ruleId, StringComparison.Ordinal));
if (existingIndex < 0)
{
return ProcessRuleCreationResult.Failed("NoSavedRuleToUpdate", NoSavedRuleMessage);
}
var selection = affinityPayload.Payload.CpuSelection;
var existing = rules[existingIndex];
var updated = existing with
{
CpuSelection = selection,
LegacyAffinityMask = HasSelectionPayload(selection) ? null : affinityPayload.Payload.LegacyAffinityMask,
ApplyAffinityOnStart = true,
UpdatedAt = DateTime.UtcNow,
};
rules[existingIndex] = updated;
cancellationToken.ThrowIfCancellationRequested();
await this.ruleStore.SaveAsync(rules).ConfigureAwait(false);
var processName = string.IsNullOrWhiteSpace(updated.ProcessName)
? "process"
: updated.ProcessName.Trim();
this.logger.LogInformation("Updated the CPU selection of saved rule {RuleId} for {ProcessName}", updated.Id, processName);
return new ProcessRuleCreationResult
{
Success = true,
Updated = true,
Rule = updated,
UserMessage = $"Updated the cores of the saved rule for {processName}.",
};
}
// Nothing checked saved rules against the machine they are running on. A rule that pins
// cores which no longer exist does not announce itself: it fails at apply time, inside a
// path that reports it as the process having exited. Ask the question where the rules are
@@ -645,7 +645,7 @@ namespace ThreadPilot.Core.Tests
}
[Fact]
public async Task UpdateRuleCoreSelectionAsync_SetsTheCoresWithoutTheProcessRunning()
public async Task UpdateRuleWithCoreSelectionAsync_SetsTheCoresWithoutTheProcessRunning()
{
var existing = new PersistentProcessRule
{
@@ -663,7 +663,7 @@ namespace ThreadPilot.Core.Tests
]));
var service = CreateService(store, topologyProvider);
var result = await service.UpdateRuleCoreSelectionAsync("rule-1", [false, true]);
var result = await service.UpdateRuleWithCoreSelectionAsync(existing, [false, true]);
Assert.True(result.Success);
var rule = Assert.Single(store.SavedRules);
@@ -674,25 +674,25 @@ namespace ThreadPilot.Core.Tests
}
[Fact]
public async Task UpdateRuleCoreSelectionAsync_RefusesASelectionWithNoCores()
public async Task UpdateRuleWithCoreSelectionAsync_RefusesASelectionWithNoCores()
{
var existing = new PersistentProcessRule { Id = "rule-1", ProcessName = "cs2", IsEnabled = true };
var store = new CapturingRuleStore([existing]);
var service = CreateService(store);
var result = await service.UpdateRuleCoreSelectionAsync("rule-1", [false, false]);
var result = await service.UpdateRuleWithCoreSelectionAsync(existing, [false, false]);
Assert.False(result.Success);
Assert.Empty(store.SavedRules);
}
[Fact]
public async Task UpdateRuleCoreSelectionAsync_ReportsAControlledFailureForAnUnknownRule()
public async Task UpdateRuleWithCoreSelectionAsync_ReportsAControlledFailureForAnUnknownRule()
{
var store = new CapturingRuleStore([]);
var service = CreateService(store);
var result = await service.UpdateRuleCoreSelectionAsync("missing", [true, false]);
var result = await service.UpdateRuleWithCoreSelectionAsync(new PersistentProcessRule { Id = "missing", ProcessName = "cs2" }, [true, false]);
Assert.False(result.Success);
Assert.Equal("NoSavedRuleToUpdate", result.ErrorCode);