Files
ThreadPilot/Platforms/Windows/IProcessCpuSetNativeApi.cs
T
Prime Build 290826bd93 CPU topology v2 phase 3: make CPU Sets mapping group-aware (#9)
Make CPU Sets mapping group-aware

Replace legacy LogicalProcessorIndex-based CPU Set mapping with ProcessorRef-based mapping that preserves processor group and group-relative logical processor identity.

- Add CpuSetMapping with ProcessorRef -> CpuSetId and reverse lookup
- Add fakeable ProcessCpuSet native API seam
- Add ApplyCpuSelection for topology-aware CPU Set assignment
- Keep legacy ApplyCpuSetMask path for single-group <=64 compatibility
- Prevent CPU64 / Group 1 CPU0 from aliasing CPU0
- Add tests for group-aware mapping, explicit CpuSetIds, selection clearing and legacy behavior

No ProcessService wiring, UI, preset generator or profile migration changes.
2026-05-19 17:18:53 +02:00

90 lines
3.0 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.Platforms.Windows
{
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
internal interface IProcessCpuSetNativeApi
{
SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId);
bool SetProcessDefaultCpuSets(SafeProcessHandle process, uint[]? cpuSetIds, uint cpuSetIdCount);
bool GetProcessTimes(
SafeProcessHandle process,
out FILETIME creationTime,
out FILETIME exitTime,
out FILETIME kernelTime,
out FILETIME userTime);
bool GetSystemCpuSetInformation(
IntPtr information,
uint bufferLength,
ref uint returnedLength,
SafeProcessHandle process,
uint flags);
int GetLastWin32Error();
}
internal sealed class ProcessCpuSetNativeApi : IProcessCpuSetNativeApi
{
public static ProcessCpuSetNativeApi Instance { get; } = new();
private ProcessCpuSetNativeApi()
{
}
public SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId)
{
return CpuSetNativeMethods.OpenProcess(access, inheritHandle, processId);
}
public bool SetProcessDefaultCpuSets(SafeProcessHandle process, uint[]? cpuSetIds, uint cpuSetIdCount)
{
return CpuSetNativeMethods.SetProcessDefaultCpuSets(process, cpuSetIds, cpuSetIdCount);
}
public bool GetProcessTimes(
SafeProcessHandle process,
out FILETIME creationTime,
out FILETIME exitTime,
out FILETIME kernelTime,
out FILETIME userTime)
{
return CpuSetNativeMethods.GetProcessTimes(process, out creationTime, out exitTime, out kernelTime, out userTime);
}
public bool GetSystemCpuSetInformation(
IntPtr information,
uint bufferLength,
ref uint returnedLength,
SafeProcessHandle process,
uint flags)
{
return CpuSetNativeMethods.GetSystemCpuSetInformation(information, bufferLength, ref returnedLength, process, flags);
}
public int GetLastWin32Error()
{
return Marshal.GetLastWin32Error();
}
}
}