9a4baba19a
* feat: back up persistent rules and resolve rule precedence deterministically
- Configuration export now includes persistent process rules (schema 3.0),
stays compatible with 2.0 bundles, validates before writing, previews
what an import replaces and rolls back on failure.
- Matching rules apply broad-to-specific (name, wildcard path, exact path,
then most recently edited), shared by the engine and the process summary,
which now lists the apply order and flags conflicting values.
- Localized the new strings in all seven languages.
* feat: clearer process and rule editors
- Process details: CPU, memory and I/O priority pickers with an explicit
Apply, undo of the last manual priority change (guarded against reused
PIDs), adaptive side/bottom details panel.
- Manual process control stays available while automation is paused, with
a persistent notice instead of an overlay.
- Saved-rule editor stages all edits (including the core mask) and saves
them together, showing pending changes.
- Localized the new strings in all seven languages.
* 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.
* 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.
* chore: keep CRLF line endings in SelectedProcessSummaryViewModel
* feat: clearer navigation, process page and rules editor
- Navigation pane open with short labels instead of seven bare icons.
- Processes: one toolbar row (search with placeholder, Filters and
Columns menus, sort, refresh); details split into Process, Priority,
CPU cores and Rule cards; pickers start from the process's current
values and stay empty when a value cannot be read; help paragraphs
moved into tooltips; core picker always visible.
- Rules: process rules first, clearer tab names, localized choices in the
saved-rule editor instead of internal enum names.
- Shorter page titles and subtitles in all seven languages; removed the
strings this made unused.
No data format changes.
* feat: settings and tweaks as Windows 11 rows, localized rule list
- Settings: one column of rows (what it is on the left, its switch on the
right), most used sections first, Save as the primary action.
- Tweaks: one tweak per row with its status dot and switch.
- Rule list: 'Applies' and 'Mode' columns use the same localized labels as
the editor and refresh on language change.
- Removed 76 strings no code referenced, from all seven languages.
* fix: issues found testing the beta in a clean Windows 11 VM
- Process counter could show more visible processes than the total
('157 / 151'): the batch total was a snapshot from the first load.
- Narrow windows (or 150% scaling): toolbar buttons move under the search
box instead of squeezing it to a few letters.
- Rules list keeps the 'Applies' column readable when narrow.
- Settings status message gets its own line instead of being cut.
Tested in a Hyper-V Windows 11 VM: clean install, upgrade, uninstall,
light and dark theme, 100% and 150% scaling.
* feat: remember layout choices; Windows 11 toggles in Tweaks
- The navigation pane, window size/position/maximized state, and the
process list filters, sort and columns now survive a restart. They live
in ui-state.json, apart from settings.json, so they never mark the
Settings page as unsaved or travel in exported configurations. A corrupt
file falls back to defaults; tests write to a temp file.
- Fixes the pane re-opening on every start (it was hard-coded open).
- Tweaks use a native-looking Windows 11 switch (40x20 track, sliding
thumb) with On/Off written beside it.
- New test: every on/off setting survives save and reload.
* docs: handoff for the 1.7.7 PR stack and beta test
* fix: P/E cores from the highest EfficiencyClass, AMD CCDs from real L3 groups
The legacy topology treated the lowest EfficiencyClass as the performance
core, swapping P- and E-cores on hybrid Intel CPUs. AMD CCDs were guessed
as 8 cores each, which split 6+6 parts (7900X3D, 9900X3D) into 8+4; they now
come from the L3 groups Windows reports, with the guess kept only as a
fallback. The topology snapshot also keeps each L3 group's size.
* feat: Game Boost, CPU Sets for the game and selected background apps
While a detected game runs, Game Boost sets process default CPU Sets from
the CPU topology: on AMD X3D with one larger L3 the game goes to the V-Cache
CCD, on Intel hybrid CPUs to the P-cores, and selected background apps
(browsers, sync clients, updaters, launchers) to the other cores. On dual-CCD
parts with equal L3 only the background moves, unless Isolation mode also
keeps the game on CCD0. Single-L3 and unreadable topologies are left to
Windows. Above Normal priority for the game is optional; never High.
Each change is written to a journal before it is made and undone when the
game exits, Game Boost is turned off or ThreadPilot closes, and on the next
start after a crash. Only values still exactly as Game Boost set them are
restored, never one a user rule or a manual change has taken over since.
Persistent rules and Game Boost share one apply gate, and a rule always
wins over Game Boost for the setting it covers.
The switch, mode and lists are on the Tweaks page, off by default, and take
effect without a restart.
* fix: Game Boost background entries with a full path never matched
The background list compared full-path entries against an empty path, so
only bare process names worked. Found by a new integration test that runs
the real services on two copies of PING.EXE: CPU Sets and priority are set
and read back through Windows, removed when Game Boost is turned off, and
undone from the journal after a simulated crash. The topology is simulated
from this machine's CPU Set IDs, so the test also runs on CPUs where Game
Boost would not act; it skips itself below 8 cores.
* feat: report Game Boost sessions and restore results in Activity
* test: diagnose EcoQoS read failures on Windows Server
* chore: prepare 1.7.7-beta.2 and keep prereleases out of stable channels
* test: use native full-query access mask in EcoQoS diagnostic
* docs: update beta handoff and prerelease publication policy
* ci: run native EcoQoS tests on a Windows 11-era kernel
Windows Server 2022 build 20348 rejects GetProcessInformation(ProcessPowerThrottling) with ERROR_INVALID_PARAMETER (87), with both limited and full query handles. Windows 11 local tests pass. Use Server 2025 for CI/release tests and smoke testing; keep the real round-trip assertions, remove temporary probes.
* test: lock beta publication and native runner contracts
* chore: separate stable 1.7.7 from experimental Game Boost
Stable retains the validated EcoQoS/rules/UI stack. Preserve the complete local Game Boost implementation at ef2ec24 for the separate 1.7.8-beta.1 prerelease; no experimental Game Boost/topology changes ship in stable. Keep channel-aware publication and Windows 11-era native CI tests.
840 lines
35 KiB
C#
840 lines
35 KiB
C#
/*
|
|
* ThreadPilot - persistent process rule creation tests.
|
|
*/
|
|
namespace ThreadPilot.Core.Tests
|
|
{
|
|
using System.Diagnostics;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ThreadPilot.Models;
|
|
using ThreadPilot.Services;
|
|
|
|
public sealed class ProcessRuleCreationServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task SaveRuleAsync_UsesExecutablePathWhenAvailable()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
var process = CreateProcess(path: @"C:\Games\Game.exe");
|
|
|
|
var result = await service.SaveRuleAsync(
|
|
process,
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.AboveNormal });
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(@"C:\Games\Game.exe", rule.ExecutablePath);
|
|
Assert.Equal("Game.exe", rule.ProcessName);
|
|
Assert.True(rule.IsEnabled);
|
|
Assert.Equal("Game.exe rule", rule.Name);
|
|
Assert.Equal("Created from Process tab action.", rule.Description);
|
|
Assert.True(result.Created);
|
|
Assert.False(result.Updated);
|
|
Assert.Equal("Saved rule for Game.exe.", result.UserMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_FallsBackToProcessNameWhenPathUnavailable()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveRuleAsync(
|
|
CreateProcess(path: string.Empty),
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.Normal });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Null(rule.ExecutablePath);
|
|
Assert.Equal("Game.exe", rule.ProcessName);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_UpdatesExistingPathMatchWithoutDuplicating()
|
|
{
|
|
var createdAt = DateTime.UtcNow.AddDays(-2);
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "existing-rule",
|
|
Name = "Old",
|
|
IsEnabled = true,
|
|
ProcessName = "Game.exe",
|
|
ExecutablePath = @"C:\Games\Game.exe",
|
|
Priority = ProcessPriorityClass.Normal,
|
|
ApplyPriorityOnStart = true,
|
|
CreatedAt = createdAt,
|
|
UpdatedAt = createdAt,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveRuleAsync(
|
|
CreateProcess(path: @"C:\Games\Game.exe"),
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.High });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.True(result.Updated);
|
|
Assert.False(result.Created);
|
|
Assert.Equal("Updated saved rule for Game.exe.", result.UserMessage);
|
|
Assert.Equal("existing-rule", rule.Id);
|
|
Assert.Equal(createdAt, rule.CreatedAt);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.True(rule.UpdatedAt > createdAt);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_UpdatesExistingPathlessNameMatchWhenNewPathIsAvailable()
|
|
{
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "pathless-rule",
|
|
Name = "Game.exe rule",
|
|
IsEnabled = true,
|
|
ProcessName = "Game.exe",
|
|
CreatedAt = DateTime.UtcNow.AddDays(-1),
|
|
UpdatedAt = DateTime.UtcNow.AddDays(-1),
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveRuleAsync(
|
|
CreateProcess(path: @"C:\Games\Game.exe"),
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.AboveNormal });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal("pathless-rule", rule.Id);
|
|
Assert.Equal(@"C:\Games\Game.exe", rule.ExecutablePath);
|
|
Assert.Equal(ProcessPriorityClass.AboveNormal, rule.Priority);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_SavesCpuSelectionWhenProvided()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
var selection = CreateCpuSelection();
|
|
|
|
await service.SaveRuleAsync(
|
|
CreateProcess(),
|
|
new ProcessRuleCreationPayload { CpuSelection = selection });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Same(selection, rule.CpuSelection);
|
|
Assert.Null(rule.LegacyAffinityMask);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_PrefersCpuSelectionWhenTopologyIsAvailable()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var topologyProvider = new FakeTopologyProvider(CpuTopologySnapshot.Create(
|
|
[
|
|
new ProcessorRef(0, 0, 0),
|
|
new ProcessorRef(0, 1, 1),
|
|
]));
|
|
var service = CreateService(store, topologyProvider);
|
|
|
|
await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0),
|
|
currentCoreSelection: [true, false],
|
|
currentMemoryPriority: null,
|
|
cpuAssignmentMode: CpuAssignmentMode.IdealProcessor);
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.NotNull(rule.CpuSelection);
|
|
Assert.Null(rule.LegacyAffinityMask);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.Equal(CpuAssignmentMode.IdealProcessor, rule.CpuAssignmentMode);
|
|
Assert.Equal(0, rule.CpuSelection.GlobalLogicalProcessorIndexes.Single());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_SavesLegacyMaskWhenSelectionIsSafelyRepresentable()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store, topologyProvider: null);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0x3),
|
|
currentCoreSelection: [true, true, false],
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(0x3, rule.LegacyAffinityMask);
|
|
Assert.Null(rule.CpuSelection);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.Null(rule.Priority);
|
|
Assert.False(rule.ApplyPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_BlocksUnsafeLegacyAffinity()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store, topologyProvider: null);
|
|
var unsafeSelection = Enumerable.Repeat(true, 65).ToArray();
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0),
|
|
unsafeSelection,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal(
|
|
"The current affinity selection cannot be saved safely on this CPU topology.",
|
|
result.UserMessage);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_BlocksRealtimePriority()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("There are no current settings to save as a rule.", result.UserMessage);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_WithNormalPriorityAndNoOtherPayload_ReturnsNoActionFailure()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.Normal, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("NoActionableRulePayload", result.ErrorCode);
|
|
Assert.Equal("There are no current settings to save as a rule.", result.UserMessage);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_WithNormalPriorityAndAffinity_SavesAffinityButDoesNotEnablePriority()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.Normal, affinity: 0x5),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(0x5, rule.LegacyAffinityMask);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.Null(rule.Priority);
|
|
Assert.False(rule.ApplyPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_WithAboveNormalPriority_SavesPriority()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.AboveNormal, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(ProcessPriorityClass.AboveNormal, rule.Priority);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_WithHighPriority_SavesPriority()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.High, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_WithRealtimePriority_OmitsPriorityWithoutSavingIt()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0x3),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(0x3, rule.LegacyAffinityMask);
|
|
Assert.Null(rule.Priority);
|
|
Assert.False(rule.ApplyPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_SavesMemoryPriorityWhenAvailable()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: ProcessMemoryPriority.BelowNormal);
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(ProcessMemoryPriority.BelowNormal, rule.MemoryPriority);
|
|
Assert.True(rule.ApplyMemoryPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_ReturnsControlledFailureWhenNoActionablePayloadExists()
|
|
{
|
|
var store = new CapturingRuleStore();
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.RealTime, affinity: 0),
|
|
currentCoreSelection: [],
|
|
currentMemoryPriority: null);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("There are no current settings to save as a rule.", result.UserMessage);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateRule_SaveThenReload_PreservesAllPersistedFields()
|
|
{
|
|
var filePath = CreateTemporaryFilePath();
|
|
var store = new PersistentProcessRuleJsonStore(() => filePath);
|
|
var service = CreateService(store);
|
|
var selection = CreateCpuSelection();
|
|
|
|
try
|
|
{
|
|
var result = await service.SaveRuleAsync(
|
|
CreateProcess(name: "PersistenceProbe.exe", path: @"C:\Probe\PersistenceProbe.exe"),
|
|
new ProcessRuleCreationPayload
|
|
{
|
|
CpuSelection = selection,
|
|
Priority = ProcessPriorityClass.High,
|
|
MemoryPriority = ProcessMemoryPriority.BelowNormal,
|
|
});
|
|
|
|
Assert.True(result.Success);
|
|
var loaded = await new PersistentProcessRuleJsonStore(() => filePath).LoadAsync();
|
|
var rule = Assert.Single(loaded);
|
|
Assert.Equal(result.Rule!.Id, rule.Id);
|
|
Assert.Equal("PersistenceProbe.exe rule", rule.Name);
|
|
Assert.True(rule.IsEnabled);
|
|
Assert.Equal("PersistenceProbe.exe", rule.ProcessName);
|
|
Assert.Equal(@"C:\Probe\PersistenceProbe.exe", rule.ExecutablePath);
|
|
Assert.NotNull(rule.CpuSelection);
|
|
Assert.Equal(selection.GlobalLogicalProcessorIndexes, rule.CpuSelection.GlobalLogicalProcessorIndexes);
|
|
Assert.Null(rule.LegacyAffinityMask);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.Equal(ProcessMemoryPriority.BelowNormal, rule.MemoryPriority);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
Assert.True(rule.ApplyMemoryPriorityOnStart);
|
|
Assert.Equal(result.Rule.CreatedAt, rule.CreatedAt);
|
|
Assert.Equal(result.Rule.UpdatedAt, rule.UpdatedAt);
|
|
Assert.Equal("Created from Process tab action.", rule.Description);
|
|
}
|
|
finally
|
|
{
|
|
DeleteTemporaryDirectory(filePath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRule_SaveThenReload_PreservesUpdatedValues()
|
|
{
|
|
var filePath = CreateTemporaryFilePath();
|
|
var store = new PersistentProcessRuleJsonStore(() => filePath);
|
|
var service = CreateService(store);
|
|
var process = CreateProcess(name: "PersistenceProbe.exe", path: @"C:\Probe\PersistenceProbe.exe");
|
|
|
|
try
|
|
{
|
|
var created = await service.SaveRuleAsync(
|
|
process,
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.AboveNormal });
|
|
var updated = await service.SaveRuleAsync(
|
|
process,
|
|
new ProcessRuleCreationPayload
|
|
{
|
|
LegacyAffinityMask = 0x3,
|
|
Priority = ProcessPriorityClass.High,
|
|
MemoryPriority = ProcessMemoryPriority.Low,
|
|
});
|
|
|
|
Assert.True(created.Success);
|
|
Assert.True(updated.Success);
|
|
Assert.True(updated.Updated);
|
|
var loaded = await new PersistentProcessRuleJsonStore(() => filePath).LoadAsync();
|
|
var rule = Assert.Single(loaded);
|
|
Assert.Equal(created.Rule!.Id, rule.Id);
|
|
Assert.Equal(created.Rule.CreatedAt, rule.CreatedAt);
|
|
Assert.Equal(0x3, rule.LegacyAffinityMask);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.Equal(ProcessMemoryPriority.Low, rule.MemoryPriority);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
Assert.True(rule.ApplyMemoryPriorityOnStart);
|
|
}
|
|
finally
|
|
{
|
|
DeleteTemporaryDirectory(filePath);
|
|
}
|
|
}
|
|
|
|
private static ProcessRuleCreationService CreateService(
|
|
IPersistentProcessRuleStore store,
|
|
ICpuTopologyProvider? topologyProvider = null) =>
|
|
new(
|
|
store,
|
|
topologyProvider,
|
|
new CpuSelectionMigrationService(),
|
|
NullLogger<ProcessRuleCreationService>.Instance);
|
|
|
|
private static CpuSelection CreateCpuSelection() =>
|
|
new()
|
|
{
|
|
LogicalProcessors = [new ProcessorRef(0, 0, 0)],
|
|
GlobalLogicalProcessorIndexes = [0],
|
|
};
|
|
|
|
[Theory]
|
|
[InlineData(ProcessEfficiencyMode.On, true)]
|
|
[InlineData(ProcessEfficiencyMode.SystemManaged, false)]
|
|
public async Task SaveCurrentSettingsAsRuleAsync_SavesOnlyAnExplicitEfficiencyMode(ProcessEfficiencyMode current, bool saved)
|
|
{
|
|
var store = new CapturingRuleStore([]);
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveCurrentSettingsAsRuleAsync(
|
|
CreateProcess(priority: ProcessPriorityClass.High, affinity: 0),
|
|
currentCoreSelection: null,
|
|
currentMemoryPriority: null,
|
|
currentEfficiencyMode: current);
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(saved, rule.ApplyEfficiencyModeOnStart);
|
|
Assert.Equal(saved ? current : null, rule.EfficiencyMode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_KeepsFieldsTheCallerDidNotCarry()
|
|
{
|
|
// The regression this covers: "Apply CPU assignment and save as rule" sent no memory or
|
|
// I/O priority, and the rewrite dropped both from a rule that already had them.
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "existing-rule",
|
|
ProcessName = "Game.exe",
|
|
ExecutablePath = @"C:\Games\Game.exe",
|
|
IsEnabled = true,
|
|
MemoryPriority = ProcessMemoryPriority.Low,
|
|
IoPriority = ProcessIoPriority.Low,
|
|
Priority = ProcessPriorityClass.High,
|
|
ApplyMemoryPriorityOnStart = true,
|
|
ApplyIoPriorityOnStart = true,
|
|
ApplyPriorityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.SaveRuleAsync(
|
|
CreateProcess(path: @"C:\Games\Game.exe"),
|
|
new ProcessRuleCreationPayload { CpuSelection = CreateCpuSelection() });
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(ProcessMemoryPriority.Low, rule.MemoryPriority);
|
|
Assert.Equal(ProcessIoPriority.Low, rule.IoPriority);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.True(rule.ApplyMemoryPriorityOnStart);
|
|
Assert.True(rule.ApplyIoPriorityOnStart);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_KeepsTheExistingAffinityWhenOnlyPrioritiesAreSaved()
|
|
{
|
|
var selection = CreateCpuSelection();
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "existing-rule",
|
|
ProcessName = "Game.exe",
|
|
ExecutablePath = @"C:\Games\Game.exe",
|
|
IsEnabled = true,
|
|
CpuSelection = selection,
|
|
CpuAssignmentMode = CpuAssignmentMode.CpuSets,
|
|
ApplyAffinityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveRuleAsync(
|
|
CreateProcess(path: @"C:\Games\Game.exe"),
|
|
new ProcessRuleCreationPayload { Priority = ProcessPriorityClass.High });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Same(selection, rule.CpuSelection);
|
|
Assert.Equal(CpuAssignmentMode.CpuSets, rule.CpuAssignmentMode);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveRuleAsync_StillReplacesAFieldTheCallerDoesCarry()
|
|
{
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "existing-rule",
|
|
ProcessName = "Game.exe",
|
|
ExecutablePath = @"C:\Games\Game.exe",
|
|
IsEnabled = true,
|
|
MemoryPriority = ProcessMemoryPriority.Low,
|
|
ApplyMemoryPriorityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
await service.SaveRuleAsync(
|
|
CreateProcess(path: @"C:\Games\Game.exe"),
|
|
new ProcessRuleCreationPayload { MemoryPriority = ProcessMemoryPriority.BelowNormal });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Equal(ProcessMemoryPriority.BelowNormal, rule.MemoryPriority);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteRuleAsync_RemovesTheRuleMatchingTheProcess()
|
|
{
|
|
var target = new PersistentProcessRule
|
|
{
|
|
Id = "target-rule",
|
|
ProcessName = "Game.exe",
|
|
ExecutablePath = @"C:\Games\Game.exe",
|
|
IsEnabled = true,
|
|
Priority = ProcessPriorityClass.High,
|
|
ApplyPriorityOnStart = true,
|
|
};
|
|
var other = new PersistentProcessRule
|
|
{
|
|
Id = "other-rule",
|
|
ProcessName = "Other.exe",
|
|
ExecutablePath = @"C:\Games\Other.exe",
|
|
IsEnabled = true,
|
|
};
|
|
var store = new CapturingRuleStore([target, other]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.DeleteRuleAsync(CreateProcess(path: @"C:\Games\Game.exe"));
|
|
|
|
Assert.True(result.Success);
|
|
Assert.Equal("Deleted saved rule for Game.exe.", result.UserMessage);
|
|
var remaining = Assert.Single(store.SavedRules);
|
|
Assert.Equal("other-rule", remaining.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteRuleAsync_ReportsAControlledFailureWhenNoRuleMatches()
|
|
{
|
|
var store = new CapturingRuleStore([]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.DeleteRuleAsync(CreateProcess());
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("NoSavedRuleToDelete", result.ErrorCode);
|
|
Assert.Equal(ProcessRuleCreationService.NoSavedRuleMessage, result.UserMessage);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleAsync_ChangesTheEditableFieldsAndKeepsTheAffinity()
|
|
{
|
|
var selection = CreateCpuSelection();
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
CpuSelection = selection,
|
|
ApplyAffinityOnStart = true,
|
|
CpuAssignmentMode = CpuAssignmentMode.AffinityMask,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.UpdateRuleAsync(existing with
|
|
{
|
|
IsEnabled = false,
|
|
MemoryPriority = ProcessMemoryPriority.Low,
|
|
Priority = ProcessPriorityClass.High,
|
|
});
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.False(rule.IsEnabled);
|
|
Assert.Equal(ProcessMemoryPriority.Low, rule.MemoryPriority);
|
|
Assert.True(rule.ApplyMemoryPriorityOnStart);
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
Assert.True(rule.ApplyPriorityOnStart);
|
|
Assert.Same(selection, rule.CpuSelection);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleAsync_ClearsAFieldTheEditorSetBackToNone()
|
|
{
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
MemoryPriority = ProcessMemoryPriority.Low,
|
|
ApplyMemoryPriorityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
await service.UpdateRuleAsync(existing with { MemoryPriority = null });
|
|
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.Null(rule.MemoryPriority);
|
|
Assert.False(rule.ApplyMemoryPriorityOnStart);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleAsync_RefusesRealtimeAndLeavesTheRuleAlone()
|
|
{
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
Priority = ProcessPriorityClass.High,
|
|
ApplyPriorityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.UpdateRuleAsync(existing with { Priority = ProcessPriorityClass.RealTime });
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("RealtimePriorityBlocked", result.ErrorCode);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleAsync_ReportsAControlledFailureForAnUnknownRule()
|
|
{
|
|
var store = new CapturingRuleStore([]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.UpdateRuleAsync(new PersistentProcessRule { Id = "missing", ProcessName = "cs2" });
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("NoSavedRuleToUpdate", result.ErrorCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleWithCoreSelectionAsync_SetsTheCoresWithoutTheProcessRunning()
|
|
{
|
|
var existing = new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
Priority = ProcessPriorityClass.High,
|
|
ApplyPriorityOnStart = true,
|
|
};
|
|
var store = new CapturingRuleStore([existing]);
|
|
var topologyProvider = new FakeTopologyProvider(CpuTopologySnapshot.Create(
|
|
[
|
|
new ProcessorRef(0, 0, 0),
|
|
new ProcessorRef(0, 1, 1),
|
|
]));
|
|
var service = CreateService(store, topologyProvider);
|
|
|
|
var result = await service.UpdateRuleWithCoreSelectionAsync(existing, [false, true]);
|
|
|
|
Assert.True(result.Success);
|
|
var rule = Assert.Single(store.SavedRules);
|
|
Assert.True(rule.ApplyAffinityOnStart);
|
|
Assert.NotNull(rule.CpuSelection);
|
|
Assert.Equal(1, rule.CpuSelection!.GlobalLogicalProcessorIndexes.Single());
|
|
Assert.Equal(ProcessPriorityClass.High, rule.Priority);
|
|
}
|
|
|
|
[Fact]
|
|
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.UpdateRuleWithCoreSelectionAsync(existing, [false, false]);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Empty(store.SavedRules);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRuleWithCoreSelectionAsync_ReportsAControlledFailureForAnUnknownRule()
|
|
{
|
|
var store = new CapturingRuleStore([]);
|
|
var service = CreateService(store);
|
|
|
|
var result = await service.UpdateRuleWithCoreSelectionAsync(new PersistentProcessRule { Id = "missing", ProcessName = "cs2" }, [true, false]);
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("NoSavedRuleToUpdate", result.ErrorCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRulesNeedingTopologyReviewAsync_NamesARuleSavedOnADifferentCpu()
|
|
{
|
|
var topology = CpuTopologySnapshot.Create(
|
|
[new ProcessorRef(0, 0, 0), new ProcessorRef(0, 1, 1)],
|
|
signature: new CpuTopologySignature { CpuBrand = "AMD Ryzen 7 5800X3D", LogicalProcessorCount = 2 });
|
|
var store = new CapturingRuleStore(
|
|
[
|
|
new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
ApplyAffinityOnStart = true,
|
|
CpuSelection = new CpuSelection
|
|
{
|
|
LogicalProcessors = [new ProcessorRef(0, 0, 0)],
|
|
GlobalLogicalProcessorIndexes = [0],
|
|
Metadata = new CpuSelectionMetadata
|
|
{
|
|
TopologySignature = new CpuTopologySignature { CpuBrand = "AMD Ryzen 7 5800X", LogicalProcessorCount = 2 },
|
|
},
|
|
},
|
|
},
|
|
]);
|
|
var service = CreateService(store, new FakeTopologyProvider(topology));
|
|
|
|
var needingReview = await service.GetRulesNeedingTopologyReviewAsync();
|
|
|
|
Assert.Equal(new[] { "cs2" }, needingReview);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRulesNeedingTopologyReviewAsync_SaysNothingWhenTheCpuIsTheSame()
|
|
{
|
|
var signature = new CpuTopologySignature { CpuBrand = "AMD Ryzen 7 5800X", LogicalProcessorCount = 2 };
|
|
var topology = CpuTopologySnapshot.Create(
|
|
[new ProcessorRef(0, 0, 0), new ProcessorRef(0, 1, 1)],
|
|
signature: signature);
|
|
var store = new CapturingRuleStore(
|
|
[
|
|
new PersistentProcessRule
|
|
{
|
|
Id = "rule-1",
|
|
ProcessName = "cs2",
|
|
IsEnabled = true,
|
|
ApplyAffinityOnStart = true,
|
|
CpuSelection = new CpuSelection
|
|
{
|
|
LogicalProcessors = [new ProcessorRef(0, 0, 0)],
|
|
GlobalLogicalProcessorIndexes = [0],
|
|
Metadata = new CpuSelectionMetadata { TopologySignature = signature },
|
|
},
|
|
},
|
|
]);
|
|
var service = CreateService(store, new FakeTopologyProvider(topology));
|
|
|
|
Assert.Empty(await service.GetRulesNeedingTopologyReviewAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRulesNeedingTopologyReviewAsync_SaysNothingWithoutATopologyProvider()
|
|
{
|
|
var store = new CapturingRuleStore(
|
|
[
|
|
new PersistentProcessRule { Id = "rule-1", ProcessName = "cs2", IsEnabled = true, ApplyAffinityOnStart = true },
|
|
]);
|
|
var service = CreateService(store);
|
|
|
|
Assert.Empty(await service.GetRulesNeedingTopologyReviewAsync());
|
|
}
|
|
|
|
private static ProcessModel CreateProcess(
|
|
string name = "Game.exe",
|
|
string path = @"C:\Games\Game.exe",
|
|
ProcessPriorityClass priority = ProcessPriorityClass.Normal,
|
|
long affinity = 0xF) =>
|
|
new()
|
|
{
|
|
ProcessId = 42,
|
|
Name = name,
|
|
ExecutablePath = path,
|
|
Priority = priority,
|
|
ProcessorAffinity = affinity,
|
|
};
|
|
|
|
private static string CreateTemporaryFilePath() =>
|
|
Path.Combine(Path.GetTempPath(), $"threadpilot-rule-service-{Guid.NewGuid():N}", "rules.json");
|
|
|
|
private static void DeleteTemporaryDirectory(string filePath)
|
|
{
|
|
var directory = Path.GetDirectoryName(filePath);
|
|
if (directory != null && Directory.Exists(directory))
|
|
{
|
|
Directory.Delete(directory, recursive: true);
|
|
}
|
|
}
|
|
|
|
private sealed class CapturingRuleStore(IReadOnlyList<PersistentProcessRule>? initialRules = null)
|
|
: IPersistentProcessRuleStore
|
|
{
|
|
public IReadOnlyList<PersistentProcessRule> SavedRules { get; private set; } = [];
|
|
|
|
public Task<IReadOnlyList<PersistentProcessRule>> LoadAsync() =>
|
|
Task.FromResult(initialRules ?? this.SavedRules);
|
|
|
|
public Task SaveAsync(IReadOnlyList<PersistentProcessRule> rules)
|
|
{
|
|
this.SavedRules = rules.ToList();
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private sealed class FakeTopologyProvider(CpuTopologySnapshot topology) : ICpuTopologyProvider
|
|
{
|
|
public Task<CpuTopologySnapshot> GetTopologySnapshotAsync(CancellationToken cancellationToken = default) =>
|
|
Task.FromResult(topology);
|
|
}
|
|
}
|
|
}
|