5d19a46fc1
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.
476 lines
22 KiB
C#
476 lines
22 KiB
C#
/*
|
|
* ThreadPilot - persistent rules engine foundation.
|
|
*/
|
|
namespace ThreadPilot.Services
|
|
{
|
|
using System.Diagnostics;
|
|
using Microsoft.Extensions.Logging;
|
|
using ThreadPilot.Models;
|
|
|
|
public interface IPersistentRulesEngine
|
|
{
|
|
Task<IReadOnlyList<PersistentRuleApplyResult>> ApplyMatchingRulesAsync(
|
|
ProcessModel process,
|
|
Predicate<PersistentProcessRule>? ruleFilter = null,
|
|
CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
public sealed class PersistentRulesEngine : IPersistentRulesEngine
|
|
{
|
|
private const string MissingAffinityErrorCode = "PersistentRuleMissingAffinity";
|
|
private const string MissingMemoryPriorityErrorCode = "PersistentRuleMissingMemoryPriority";
|
|
private const string MissingPriorityErrorCode = "PersistentRuleMissingPriority";
|
|
private const string MemoryPriorityApplyFailedErrorCode = "MemoryPriorityApplyFailed";
|
|
private const string NoActionsErrorCode = "PersistentRuleNoActions";
|
|
private const string PriorityApplyFailedErrorCode = "PriorityApplyFailed";
|
|
private const string PriorityNotObservedErrorCode = "PriorityNotObserved";
|
|
private const string PriorityVerificationUnavailableErrorCode = "PriorityVerificationUnavailable";
|
|
private const string RealtimePriorityBlockedErrorCode = "RealtimePriorityBlocked";
|
|
|
|
private readonly IPersistentProcessRuleStore ruleStore;
|
|
private readonly IPersistentProcessRuleMatcher matcher;
|
|
private readonly IAffinityApplyService affinityApplyService;
|
|
private readonly IProcessService processService;
|
|
private readonly IProcessMemoryPriorityService memoryPriorityService;
|
|
private readonly ProcessPowerRequestService? powerRequestService;
|
|
private readonly ILogger<PersistentRulesEngine> logger;
|
|
|
|
public PersistentRulesEngine(
|
|
IPersistentProcessRuleStore ruleStore,
|
|
IPersistentProcessRuleMatcher matcher,
|
|
IAffinityApplyService affinityApplyService,
|
|
IProcessService processService,
|
|
IProcessMemoryPriorityService memoryPriorityService,
|
|
ILogger<PersistentRulesEngine> logger,
|
|
ProcessPowerRequestService? powerRequestService = null)
|
|
{
|
|
this.ruleStore = ruleStore ?? throw new ArgumentNullException(nameof(ruleStore));
|
|
this.matcher = matcher ?? throw new ArgumentNullException(nameof(matcher));
|
|
this.affinityApplyService = affinityApplyService ?? throw new ArgumentNullException(nameof(affinityApplyService));
|
|
this.processService = processService ?? throw new ArgumentNullException(nameof(processService));
|
|
this.memoryPriorityService = memoryPriorityService ?? throw new ArgumentNullException(nameof(memoryPriorityService));
|
|
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
this.powerRequestService = powerRequestService;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<PersistentRuleApplyResult>> ApplyMatchingRulesAsync(
|
|
ProcessModel process,
|
|
Predicate<PersistentProcessRule>? ruleFilter = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(process);
|
|
|
|
var rules = await this.ruleStore.LoadAsync().ConfigureAwait(false);
|
|
var results = new List<PersistentRuleApplyResult>();
|
|
|
|
foreach (var rule in PersistentRuleResolution.MatchInApplyOrder(rules, process, this.matcher)
|
|
.Where(rule => ruleFilter == null || ruleFilter(rule)))
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
results.Add(await this.ApplyRuleAsync(rule, process, cancellationToken).ConfigureAwait(false));
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
private async Task<PersistentRuleApplyResult> ApplyRuleAsync(
|
|
PersistentProcessRule rule,
|
|
ProcessModel process,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = CreateSuccessResult(rule, process);
|
|
var success = true;
|
|
|
|
if (!rule.ApplyAffinityOnStart &&
|
|
!rule.ApplyPriorityOnStart &&
|
|
!rule.ApplyMemoryPriorityOnStart &&
|
|
!rule.ApplyIoPriorityOnStart &&
|
|
!rule.ApplyEfficiencyModeOnStart &&
|
|
!rule.PreventSystemSleepWhileRunning)
|
|
{
|
|
return MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
NoActionsErrorCode,
|
|
"This saved rule has no actions to apply.");
|
|
}
|
|
|
|
if (rule.ApplyAffinityOnStart)
|
|
{
|
|
if (rule.CpuSelection == null && !rule.LegacyAffinityMask.HasValue)
|
|
{
|
|
success = false;
|
|
result = MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
MissingAffinityErrorCode,
|
|
"This saved rule has no affinity selection to apply.");
|
|
}
|
|
else
|
|
{
|
|
var affinityResult = await this.ApplyAffinityAsync(rule, process).ConfigureAwait(false);
|
|
if (affinityResult.Success)
|
|
{
|
|
result = result with { AffinityApplied = true };
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
result = MergeAffinityFailure(result, affinityResult);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (rule.ApplyPriorityOnStart && !result.IsProcessExited)
|
|
{
|
|
if (!rule.Priority.HasValue)
|
|
{
|
|
success = false;
|
|
result = MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
MissingPriorityErrorCode,
|
|
"This saved rule has no priority value to apply.");
|
|
}
|
|
else
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
try
|
|
{
|
|
var requestedPriority = rule.Priority.Value;
|
|
await this.processService.SetProcessPriority(process, requestedPriority, ProcessPriorityWriteSource.PersistentRuleInitialApply).ConfigureAwait(false);
|
|
result = await this.VerifyPriorityAsync(result, process, requestedPriority).ConfigureAwait(false);
|
|
success = success && result.PriorityVerified;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
success = false;
|
|
result = this.MergePriorityFailure(result, ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (rule.ApplyMemoryPriorityOnStart && !result.IsProcessExited)
|
|
{
|
|
if (!rule.MemoryPriority.HasValue)
|
|
{
|
|
success = false;
|
|
result = MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
MissingMemoryPriorityErrorCode,
|
|
"This saved rule has no memory priority value to apply.");
|
|
}
|
|
else
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
var memoryPriorityResult = await this.memoryPriorityService
|
|
.SetMemoryPriorityAsync(process, rule.MemoryPriority.Value)
|
|
.ConfigureAwait(false);
|
|
if (memoryPriorityResult.Success)
|
|
{
|
|
result = result with { MemoryPriorityApplied = true };
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
result = this.MergeMemoryPriorityFailure(result, memoryPriorityResult);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (rule.ApplyIoPriorityOnStart && !result.IsProcessExited)
|
|
{
|
|
if (!rule.IoPriority.HasValue)
|
|
{
|
|
success = false;
|
|
result = MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
"PersistentRuleMissingIoPriority",
|
|
"This saved rule has no I/O priority value to apply.");
|
|
}
|
|
else
|
|
{
|
|
var ioResult = await this.memoryPriorityService
|
|
.SetIoPriorityAsync(process, rule.IoPriority.Value)
|
|
.ConfigureAwait(false);
|
|
if (ioResult.Success)
|
|
{
|
|
result = result with { IoPriorityApplied = true };
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
result = this.MergeMemoryPriorityFailure(result, ioResult);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (rule.ApplyEfficiencyModeOnStart && !result.IsProcessExited)
|
|
{
|
|
if (!rule.EfficiencyMode.HasValue)
|
|
{
|
|
success = false;
|
|
result = MarkRuleConfigurationFailure(
|
|
result,
|
|
rule,
|
|
"PersistentRuleMissingEfficiencyMode",
|
|
"This saved rule has no efficiency mode value to apply.");
|
|
}
|
|
else
|
|
{
|
|
var efficiencyResult = await this.memoryPriorityService
|
|
.SetEfficiencyModeAsync(process, rule.EfficiencyMode.Value)
|
|
.ConfigureAwait(false);
|
|
if (efficiencyResult.Success)
|
|
{
|
|
result = result with { EfficiencyModeApplied = true };
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
result = this.MergeMemoryPriorityFailure(result, efficiencyResult);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (rule.PreventSystemSleepWhileRunning && !result.IsProcessExited)
|
|
{
|
|
var applied = this.powerRequestService != null &&
|
|
await this.powerRequestService.SetPreventSleepAsync(process, enabled: true).ConfigureAwait(false);
|
|
result = result with { PreventSleepApplied = applied };
|
|
success = success && applied;
|
|
if (!applied)
|
|
{
|
|
result = result with
|
|
{
|
|
ErrorCode = "PowerRequestFailed",
|
|
UserMessage = "ThreadPilot could not prevent system sleep for this process.",
|
|
};
|
|
}
|
|
}
|
|
|
|
return result with
|
|
{
|
|
Success = success,
|
|
UserMessage = success ? "Persistent rule applied." : result.UserMessage,
|
|
TechnicalMessage = success ? $"Persistent rule '{rule.Name}' applied to process {process.Name}." : result.TechnicalMessage,
|
|
};
|
|
}
|
|
|
|
private PersistentRuleApplyResult MergeMemoryPriorityFailure(
|
|
PersistentRuleApplyResult result,
|
|
ProcessOperationResult memoryPriorityResult)
|
|
{
|
|
this.logger.LogWarning(
|
|
"Persistent rule memory priority apply failed for rule {RuleId} on process {ProcessName} (PID: {ProcessId}): {Message}",
|
|
result.RuleId,
|
|
result.ProcessName,
|
|
result.ProcessId,
|
|
memoryPriorityResult.TechnicalMessage);
|
|
|
|
return result with
|
|
{
|
|
ErrorCode = string.IsNullOrWhiteSpace(memoryPriorityResult.ErrorCode)
|
|
? MemoryPriorityApplyFailedErrorCode
|
|
: memoryPriorityResult.ErrorCode,
|
|
UserMessage = string.IsNullOrWhiteSpace(memoryPriorityResult.UserMessage)
|
|
? "ThreadPilot could not apply the saved memory priority rule."
|
|
: memoryPriorityResult.UserMessage,
|
|
TechnicalMessage = memoryPriorityResult.TechnicalMessage,
|
|
IsAccessDenied = result.IsAccessDenied || memoryPriorityResult.IsAccessDenied,
|
|
IsAntiCheatLikely = result.IsAntiCheatLikely || memoryPriorityResult.IsAntiCheatLikely,
|
|
IsProcessExited = result.IsProcessExited || memoryPriorityResult.IsProcessExited,
|
|
};
|
|
}
|
|
|
|
private async Task<PersistentRuleApplyResult> VerifyPriorityAsync(
|
|
PersistentRuleApplyResult result,
|
|
ProcessModel process,
|
|
ProcessPriorityClass requestedPriority)
|
|
{
|
|
try
|
|
{
|
|
await this.processService.RefreshProcessInfo(process).ConfigureAwait(false);
|
|
var observedPriority = process.Priority;
|
|
if (observedPriority == requestedPriority)
|
|
{
|
|
return result with
|
|
{
|
|
PriorityApplied = true,
|
|
RequestedPriority = requestedPriority,
|
|
ObservedPriority = observedPriority,
|
|
PriorityVerified = true,
|
|
PriorityVerificationPhase = "immediate",
|
|
};
|
|
}
|
|
|
|
this.logger.LogWarning(
|
|
"Persistent rule priority requested but not observed for rule {RuleId} on process {ProcessName} (PID: {ProcessId}). Requested: {RequestedPriority}; observed: {ObservedPriority}; phase: immediate",
|
|
result.RuleId,
|
|
result.ProcessName,
|
|
result.ProcessId,
|
|
requestedPriority,
|
|
observedPriority);
|
|
|
|
return result with
|
|
{
|
|
Success = false,
|
|
PriorityApplied = true,
|
|
RequestedPriority = requestedPriority,
|
|
ObservedPriority = observedPriority,
|
|
PriorityVerificationPhase = "immediate",
|
|
ErrorCode = PriorityNotObservedErrorCode,
|
|
UserMessage = "ThreadPilot requested the saved priority, but the process reported a different priority.",
|
|
TechnicalMessage = $"Requested priority {requestedPriority}; observed {observedPriority} during immediate verification.",
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogWarning(
|
|
ex,
|
|
"Persistent rule priority verification unavailable for rule {RuleId} on process {ProcessName} (PID: {ProcessId}). Requested: {RequestedPriority}; phase: immediate",
|
|
result.RuleId,
|
|
result.ProcessName,
|
|
result.ProcessId,
|
|
requestedPriority);
|
|
|
|
var isProcessExited = AffinityApplyExceptionClassifier.IsProcessExited(ex);
|
|
var isAccessDenied = AffinityApplyExceptionClassifier.IsAccessDenied(ex);
|
|
var isAntiCheatLikely = AffinityApplyExceptionClassifier.IsAntiCheatLikely(ex);
|
|
return result with
|
|
{
|
|
Success = false,
|
|
PriorityApplied = true,
|
|
RequestedPriority = requestedPriority,
|
|
PriorityVerificationUnavailable = true,
|
|
PriorityVerificationPhase = "immediate",
|
|
ErrorCode = isProcessExited
|
|
? AffinityApplyErrorCodes.ProcessExited
|
|
: isAntiCheatLikely
|
|
? AffinityApplyErrorCodes.AntiCheatOrProtectedProcessLikely
|
|
: isAccessDenied
|
|
? AffinityApplyErrorCodes.AccessDenied
|
|
: PriorityVerificationUnavailableErrorCode,
|
|
UserMessage = isProcessExited
|
|
? ProcessOperationUserMessages.ProcessExited
|
|
: isAntiCheatLikely
|
|
? ProcessOperationUserMessages.PersistentRulesProtectedProcessWarning
|
|
: isAccessDenied
|
|
? ProcessOperationUserMessages.AccessDenied
|
|
: "ThreadPilot requested the saved priority, but could not verify the process priority.",
|
|
TechnicalMessage = ex.Message,
|
|
IsAccessDenied = result.IsAccessDenied || isAccessDenied,
|
|
IsAntiCheatLikely = result.IsAntiCheatLikely || isAntiCheatLikely,
|
|
IsProcessExited = result.IsProcessExited || isProcessExited,
|
|
};
|
|
}
|
|
}
|
|
|
|
private Task<AffinityApplyResult> ApplyAffinityAsync(PersistentProcessRule rule, ProcessModel process)
|
|
{
|
|
if (rule.CpuSelection != null)
|
|
{
|
|
return rule.CpuAssignmentMode == CpuAssignmentMode.Automatic
|
|
? this.affinityApplyService.ApplyAsync(process, rule.CpuSelection)
|
|
: this.affinityApplyService.ApplyAsync(process, rule.CpuSelection, rule.CpuAssignmentMode);
|
|
}
|
|
|
|
if (rule.LegacyAffinityMask.HasValue)
|
|
{
|
|
return rule.CpuAssignmentMode == CpuAssignmentMode.Automatic
|
|
? this.affinityApplyService.ApplyAsync(process, rule.LegacyAffinityMask.Value)
|
|
: this.affinityApplyService.ApplyAsync(process, rule.LegacyAffinityMask.Value, rule.CpuAssignmentMode);
|
|
}
|
|
|
|
return Task.FromResult(AffinityApplyResult.Succeeded(0, process.ProcessorAffinity));
|
|
}
|
|
|
|
private static PersistentRuleApplyResult MarkRuleConfigurationFailure(
|
|
PersistentRuleApplyResult result,
|
|
PersistentProcessRule rule,
|
|
string errorCode,
|
|
string userMessage) =>
|
|
result with
|
|
{
|
|
Success = false,
|
|
ErrorCode = errorCode,
|
|
UserMessage = userMessage,
|
|
TechnicalMessage = $"Persistent rule '{rule.Name}' ({rule.Id}) is incomplete: {userMessage}",
|
|
};
|
|
|
|
private PersistentRuleApplyResult MergePriorityFailure(PersistentRuleApplyResult result, Exception ex)
|
|
{
|
|
this.logger.LogWarning(
|
|
ex,
|
|
"Persistent rule priority apply failed for rule {RuleId} on process {ProcessName} (PID: {ProcessId})",
|
|
result.RuleId,
|
|
result.ProcessName,
|
|
result.ProcessId);
|
|
|
|
var isProcessExited = AffinityApplyExceptionClassifier.IsProcessExited(ex);
|
|
var isAccessDenied = AffinityApplyExceptionClassifier.IsAccessDenied(ex);
|
|
var isAntiCheatLikely = AffinityApplyExceptionClassifier.IsAntiCheatLikely(ex);
|
|
var isRealtimeBlocked = string.Equals(
|
|
ex.Message,
|
|
ProcessOperationUserMessages.RealtimePriorityBlocked,
|
|
StringComparison.Ordinal);
|
|
|
|
return result with
|
|
{
|
|
ErrorCode = isRealtimeBlocked
|
|
? RealtimePriorityBlockedErrorCode
|
|
: isProcessExited
|
|
? AffinityApplyErrorCodes.ProcessExited
|
|
: isAntiCheatLikely
|
|
? AffinityApplyErrorCodes.AntiCheatOrProtectedProcessLikely
|
|
: isAccessDenied
|
|
? AffinityApplyErrorCodes.AccessDenied
|
|
: PriorityApplyFailedErrorCode,
|
|
UserMessage = isRealtimeBlocked
|
|
? ProcessOperationUserMessages.RealtimePriorityBlocked
|
|
: isProcessExited
|
|
? ProcessOperationUserMessages.ProcessExited
|
|
: isAntiCheatLikely
|
|
? ProcessOperationUserMessages.PersistentRulesProtectedProcessWarning
|
|
: isAccessDenied
|
|
? ProcessOperationUserMessages.AccessDenied
|
|
: "ThreadPilot could not apply the saved priority rule.",
|
|
TechnicalMessage = ex.Message,
|
|
IsAccessDenied = result.IsAccessDenied || isAccessDenied,
|
|
IsAntiCheatLikely = result.IsAntiCheatLikely || isAntiCheatLikely,
|
|
IsProcessExited = result.IsProcessExited || isProcessExited,
|
|
};
|
|
}
|
|
|
|
private static PersistentRuleApplyResult CreateSuccessResult(PersistentProcessRule rule, ProcessModel process) =>
|
|
new()
|
|
{
|
|
Success = true,
|
|
RuleId = rule.Id,
|
|
ProcessId = process.ProcessId,
|
|
ProcessName = process.Name,
|
|
UserMessage = "Persistent rule applied.",
|
|
TechnicalMessage = $"Persistent rule '{rule.Name}' matched process {process.Name}.",
|
|
};
|
|
|
|
private static PersistentRuleApplyResult MergeAffinityFailure(
|
|
PersistentRuleApplyResult result,
|
|
AffinityApplyResult affinityResult) =>
|
|
result with
|
|
{
|
|
ErrorCode = affinityResult.ErrorCode,
|
|
UserMessage = affinityResult.IsAntiCheatLikely
|
|
? ProcessOperationUserMessages.PersistentRulesProtectedProcessWarning
|
|
: affinityResult.UserMessage,
|
|
TechnicalMessage = affinityResult.TechnicalMessage,
|
|
IsAccessDenied = result.IsAccessDenied || affinityResult.IsAccessDenied,
|
|
IsAntiCheatLikely = result.IsAntiCheatLikely || affinityResult.IsAntiCheatLikely,
|
|
IsProcessExited = result.IsProcessExited ||
|
|
affinityResult.ErrorCode == AffinityApplyErrorCodes.ProcessExited ||
|
|
affinityResult.FailureReason == AffinityApplyFailureReason.ProcessTerminated,
|
|
};
|
|
}
|
|
}
|