Files
ThreadPilot/Services/GameBoostProcessLists.cs
PrimeBuild-pc 604642598f perf: Game Boost holds the game open and rescans only listed names
During a session the game was looked up twice every 3 s (identity + CPU
time, each a GetProcessById and the identity a full process snapshot for
SessionId), and every 15 s a model was built for every process on the
machine to find a few listed background names.

- The game is held open for the session (Process + Exited): its exit ends
  the session at once, under the same lock as the tick; CPU time for the
  load sampling is read from the same handle. The identity is re-checked
  through the held handle, which also pins the PID.
- The handle is verified with one wait before the exit wait is registered,
  so a handle without SYNCHRONIZE (anti-cheat) falls into the polling
  fallback instead of failing on the runtime's wait thread. A denied or
  protected game keeps the previous polling.
- The rescan filters the GetProcesses snapshot by listed name before any
  model is built; exact paths are still checked afterwards.
- Session id read with ProcessIdToSessionId instead of Process.SessionId
  (a full snapshot per listed background process every 15 s).

Ownership, journal and restore unchanged. +8 tests (903/903).
2026-10-22 10:54:37 +02:00

116 lines
5.2 KiB
C#

namespace ThreadPilot.Services
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
/// <summary>
/// Deterministic lists Game Boost decides with. Audio and voice (Discord), capture (OBS) and
/// peripheral software are deliberately in none of the built-in lists.
/// </summary>
public static class GameBoostProcessLists
{
/// <summary>Background moved off the game's cores in Automatic; the user can edit this list.</summary>
public static readonly IReadOnlyList<string> DefaultBackgroundProcesses =
[
// Browsers
"chrome", "msedge", "firefox", "opera", "brave", "vivaldi",
// Sync clients
"OneDrive", "Dropbox", "GoogleDriveFS", "iCloudDrive", "MEGAsync", "nextcloud",
// Updaters
"GoogleUpdate", "MicrosoftEdgeUpdate", "AdobeARM",
// Launchers, once the game is running
"steam", "steamwebhelper", "EpicGamesLauncher", "EpicWebHelper", "EADesktop", "GalaxyClient", "upc", "Battle.net",
];
/// <summary>Added to the user's list in Isolation: processes that are certainly secondary while gaming.</summary>
public static readonly IReadOnlyList<string> IsolationExtraProcesses =
[
"Widgets", "WidgetService", "PhoneExperienceHost",
"Spotify", "vlc",
"WINWORD", "EXCEL", "POWERPNT", "OUTLOOK", "olk", "ONENOTE", "Acrobat", "AcroRd32",
"Code", "devenv", "Notion", "Obsidian",
];
private static readonly string[] GameLibraryPathFragments =
[
@"\steamapps\common\",
@"\Epic Games\",
@"\XboxGames\",
@"\GOG Games\",
@"\GOG Galaxy\Games\",
@"\EA Games\",
@"\Ubisoft Game Launcher\games\",
@"\Riot Games\",
];
// Executables that live in game folders but are not the game.
private static readonly string[] NonGameNameFragments =
[
"launcher", "update", "crash", "report", "setup", "install", "unins", "redist", "prereq",
"easyanticheat", "battleye", "beservice", "vanguard", "riotclient", "helper", "overlay",
"bootstrap", "cefsharp", "service",
];
public static bool IsGame(
string? processName,
string? executablePath,
IReadOnlyCollection<string>? manualGames,
IReadOnlyCollection<string>? excludedGames)
{
var name = Normalize(processName);
if (name.Length == 0)
{
return false;
}
if (Matches(excludedGames, name, executablePath))
{
return false;
}
if (Matches(manualGames, name, executablePath))
{
return true;
}
return !string.IsNullOrWhiteSpace(executablePath)
&& GameLibraryPathFragments.Any(fragment => executablePath.Contains(fragment, StringComparison.OrdinalIgnoreCase))
&& !NonGameNameFragments.Any(fragment => name.Contains(fragment, StringComparison.OrdinalIgnoreCase));
}
public static bool IsListedBackground(string? processName, string? executablePath, GameBoostMode mode, IReadOnlyCollection<string>? userBackground)
{
var name = Normalize(processName);
return name.Length > 0
&& (Matches(userBackground, name, executablePath)
|| (mode == GameBoostMode.Isolation && IsolationExtraProcesses.Any(entry => Normalize(entry).Equals(name, StringComparison.OrdinalIgnoreCase))));
}
/// <summary>Every name <see cref="IsListedBackground"/> can match (path entries by file name): a cheap prefilter, not the decision.</summary>
public static HashSet<string> BackgroundNames(GameBoostMode mode, IReadOnlyCollection<string>? userBackground) =>
new HashSet<string>(
(userBackground ?? []).Concat(mode == GameBoostMode.Isolation ? IsolationExtraProcesses : []).Select(Normalize).Where(name => name.Length > 0),
StringComparer.OrdinalIgnoreCase);
/// <summary>Process name without directory and ".exe", so "C:\x\Game.exe", "Game.exe" and "game" all match.</summary>
public static string Normalize(string? processName)
{
var name = Path.GetFileName((processName ?? string.Empty).Trim());
return name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) ? name[..^4] : name;
}
// An entry with a directory matches that exact executable; a bare name matches any path.
private static bool Matches(IReadOnlyCollection<string>? entries, string normalizedName, string? executablePath) =>
entries != null && entries.Any(entry =>
!string.IsNullOrWhiteSpace(entry)
&& (entry.Contains('\\')
? string.Equals(entry.Trim(), executablePath, StringComparison.OrdinalIgnoreCase)
: Normalize(entry).Equals(normalizedName, StringComparison.OrdinalIgnoreCase)));
}
}