2ee397ac83
A user reported that the CPU affinity feature did nothing when applying a rule. Two distinct defects were behind it. ProcessService.SetProcessorAffinity(process, long) silently substituted SetProcessDefaultCpuSets for the hard affinity write. CPU Sets are only a soft scheduling hint and leave the mask returned by GetProcessAffinityMask untouched, so the single caller - AffinityApplyService.ApplyAsync, which reads the mask back and compares - always reported VerificationMismatch. The Rules tab path (ProcessMonitorManagerService association core masks) therefore failed while the affinity never changed. Reproduced on a 16-CPU machine: requesting 0x3 left the process at 0xFFFF and returned a failure. The overload now writes the affinity mask it was asked for; soft assignment keeps its own entry points (CpuAssignmentMode.CpuSets, CpuSelectionAffinityApplier). The shipped DefaultCpuAssignmentMode was Automatic, which is CPU-Sets-first, so out of the box "Apply CPU Assignment" produced no change visible in Task Manager while reporting success. The default is now AffinityMask, and the CPU Sets result message states that it is a soft preference rather than claiming "Affinity applied successfully". Save Current Settings as Rule only captured the core selection while edits were staged, otherwise falling back to process.ProcessorAffinity - which soft modes never change - saving a rule pinned to every CPU. It now captures what the core checkboxes show. Adds a regression test that spawns a real process and asserts the hard affinity mask actually changes; it fails on the previous implementation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AhS1xyuMPFvxrojERQ4NuY
69 lines
3.5 KiB
C#
69 lines
3.5 KiB
C#
namespace ThreadPilot.Services
|
|
{
|
|
using System.Diagnostics;
|
|
|
|
internal static class ProcessOperationUserMessages
|
|
{
|
|
public const string AccessDenied =
|
|
"Windows denied this change. The process may require administrator rights or may be protected.";
|
|
|
|
public const string AntiCheatProtectedLikely =
|
|
"The process appears protected by anti-cheat or process protection. ThreadPilot will not try to bypass it.";
|
|
|
|
public const string AdminClarification =
|
|
"Administrator mode may help with normal permission issues, but cannot bypass anti-cheat or protected process restrictions.";
|
|
|
|
public const string LegacyFallbackBlocked =
|
|
"This CPU selection cannot be safely represented by legacy affinity APIs on this topology. CPU Sets are required for this selection.";
|
|
|
|
public const string InvalidTopology =
|
|
"This CPU selection does not match the current CPU topology. Review or recreate the preset.";
|
|
|
|
public const string ProcessExited =
|
|
"The process exited before ThreadPilot could apply the change.";
|
|
|
|
public const string CpuSetsApplied =
|
|
"CPU Sets assignment applied. This is a soft scheduling preference: the affinity mask shown in Task Manager stays unchanged. Use the Affinity Mask mode for a hard, visible restriction.";
|
|
|
|
public const string CpuSetsUnavailable =
|
|
"Windows CPU Sets are unavailable or rejected this selection. ThreadPilot will use a safe fallback only when possible.";
|
|
|
|
public const string ProcessRestartRequired =
|
|
"This process has a conflicting hard affinity. Restart it before applying a soft CPU assignment; ThreadPilot will not widen external restrictions automatically.";
|
|
|
|
public const string HighPriorityWarning =
|
|
"High priority can improve responsiveness for some workloads but may reduce system responsiveness.";
|
|
|
|
public const string RealtimePriorityBlocked =
|
|
"Realtime priority is blocked by ThreadPilot because it can make Windows unstable or unresponsive.";
|
|
|
|
public const string PersistentLaunchTimePriorityNotice =
|
|
"Persistent launch-time priority may be supported for normal processes, but it does not bypass protected process or anti-cheat restrictions.";
|
|
|
|
public const string PersistentRulesDescription =
|
|
"Applies saved rules when a matching process starts. Some protected or anti-cheat processes may reject changes. Administrator mode can help with normal permission issues but cannot bypass protection.";
|
|
|
|
public const string PersistentRulesProtectedProcessWarning =
|
|
"The process appears protected by anti-cheat or process protection. ThreadPilot will not try to override it.";
|
|
}
|
|
|
|
internal static class ProcessPriorityGuardrails
|
|
{
|
|
public static string? GetWarning(ProcessPriorityClass priority) =>
|
|
priority == ProcessPriorityClass.High
|
|
? ProcessOperationUserMessages.HighPriorityWarning
|
|
: null;
|
|
|
|
public static bool IsBlocked(ProcessPriorityClass priority) =>
|
|
priority == ProcessPriorityClass.RealTime;
|
|
|
|
public static void ThrowIfBlocked(ProcessPriorityClass priority)
|
|
{
|
|
if (IsBlocked(priority))
|
|
{
|
|
throw new InvalidOperationException(ProcessOperationUserMessages.RealtimePriorityBlocked);
|
|
}
|
|
}
|
|
}
|
|
}
|