Files
ThreadPilot/Platforms/Windows/IProcessMemoryPriorityNativeApi.cs
T
PrimeBuild-pc 5d19a46fc1 feat: per-process efficiency mode (EcoQoS) for live processes and rules
Closes #66.

- Native: SetProcessInformation/GetProcessInformation with
  ProcessPowerThrottling (EXECUTION_SPEED), three states: Windows decides,
  On (EcoQoS), Off (never throttle / HighQoS).
- Process tab: context menu entries, a picker with Apply in the details
  panel, current state in the summary, undo of the last change, included
  when saving current settings as a rule (only if explicitly set).
- Rules: EfficiencyMode + ApplyEfficiencyModeOnStart, applied by the rules
  engine, editable in the saved-rule editor, validated on import and part
  of conflict detection.
- The summary flags when the global Power Throttling tweak voids it. CPU
  priority is left alone, unlike Task Manager's efficiency mode.
- Localized in all seven languages.
2026-10-14 17:16:36 +02:00

142 lines
5.3 KiB
C#

/*
* ThreadPilot - Windows process memory priority native API abstraction.
*/
namespace ThreadPilot.Platforms.Windows
{
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public interface IProcessMemoryPriorityNativeApi
{
bool IsSupported { get; }
SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId);
bool GetProcessInformation(
SafeProcessHandle process,
ProcessInformationClass processInformationClass,
ref MemoryPriorityInformation processInformation,
uint processInformationSize);
bool SetProcessInformation(
SafeProcessHandle process,
ProcessInformationClass processInformationClass,
ref MemoryPriorityInformation processInformation,
uint processInformationSize);
int GetLastWin32Error();
int QueryIoPriority(SafeProcessHandle process, ref int priority, out uint returnLength)
{
returnLength = 0;
return unchecked((int)0xC0000002);
}
int SetIoPriority(SafeProcessHandle process, ref int priority) => unchecked((int)0xC0000002);
bool GetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => false;
bool SetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => false;
IDisposable? AcquirePrivilegeScope(string privilegeName) => null;
}
public sealed class ProcessMemoryPriorityNativeApi : IProcessMemoryPriorityNativeApi
{
public static ProcessMemoryPriorityNativeApi Instance { get; } = new();
private ProcessMemoryPriorityNativeApi()
{
}
public bool IsSupported => OperatingSystem.IsWindowsVersionAtLeast(6, 2);
public SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId)
{
return ProcessMemoryPriorityNativeMethods.OpenProcess(access, inheritHandle, processId);
}
public bool GetProcessInformation(
SafeProcessHandle process,
ProcessInformationClass processInformationClass,
ref MemoryPriorityInformation processInformation,
uint processInformationSize)
{
return ProcessMemoryPriorityNativeMethods.GetProcessInformation(
process,
processInformationClass,
ref processInformation,
processInformationSize);
}
public bool SetProcessInformation(
SafeProcessHandle process,
ProcessInformationClass processInformationClass,
ref MemoryPriorityInformation processInformation,
uint processInformationSize)
{
return ProcessMemoryPriorityNativeMethods.SetProcessInformation(
process,
processInformationClass,
ref processInformation,
processInformationSize);
}
public int GetLastWin32Error()
{
return Marshal.GetLastWin32Error();
}
public int QueryIoPriority(SafeProcessHandle process, ref int priority, out uint returnLength) =>
ProcessMemoryPriorityNativeMethods.NtQueryInformationProcess(
process,
ProcessMemoryPriorityNativeMethods.ProcessIoPriority,
ref priority,
sizeof(int),
out returnLength);
public int SetIoPriority(SafeProcessHandle process, ref int priority) =>
ProcessMemoryPriorityNativeMethods.NtSetInformationProcess(
process,
ProcessMemoryPriorityNativeMethods.ProcessIoPriority,
ref priority,
sizeof(int));
public bool GetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) =>
ProcessMemoryPriorityNativeMethods.GetProcessPowerThrottling(
process, ProcessInformationClass.ProcessPowerThrottling, ref state, (uint)Marshal.SizeOf<PowerThrottlingState>());
public bool SetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) =>
ProcessMemoryPriorityNativeMethods.SetProcessPowerThrottling(
process, ProcessInformationClass.ProcessPowerThrottling, ref state, (uint)Marshal.SizeOf<PowerThrottlingState>());
public IDisposable? AcquirePrivilegeScope(string privilegeName) =>
ProcessMemoryPriorityNativeMethods.AcquirePrivilegeScope(privilegeName);
}
public enum ProcessInformationClass
{
ProcessMemoryPriority = 0,
ProcessPowerThrottling = 4,
}
// PROCESS_POWER_THROTTLING_STATE: ControlMask picks the mechanism, StateMask turns it on or off.
[StructLayout(LayoutKind.Sequential)]
public struct PowerThrottlingState
{
public const uint CurrentVersion = 1;
public const uint ExecutionSpeed = 0x1;
public uint Version;
public uint ControlMask;
public uint StateMask;
}
[StructLayout(LayoutKind.Sequential)]
public struct MemoryPriorityInformation
{
public uint MemoryPriority;
}
}