1

Rewrite game mode: two-pass scan, priority restoration, LimitedInformation access

- Use PROCESS_QUERY_LIMITED_INFORMATION instead of PROCESS_QUERY_INFORMATION
  to open processes (fixes Access Denied for KeePassXC, HD-Player, etc.)
- Separate query handle from set handle: trySetPrio reopens with
  PROCESS_SET_INFORMATION only when needed
- Two-pass scan: pass 1 collects RSS and detects game processes,
  pass 2 applies priorities with correct hasGame state
- Game mode saves original priorities in gameSaved map, restores them
  on GAME MODE OFF (not just NORMAL)
- Demote ALL non-game processes to IDLE in game mode (not just promoted)
- 50ms delay between priority changes for smooth transitions
- Replace isAlive/procName with pidMap from allProcs snapshot
- Replace mutex/MessageBox single-instance with killOtherInstances
- Defaults: -mem=512M, -game-mem=4G
- Fix double CloseHandle and handle leak in trySetPrio
- Update README with new defaults and game mode restoration behavior
This commit is contained in:
2026-06-22 17:24:21 +03:00
parent b5885791fa
commit 565b751195
2 changed files with 335 additions and 116 deletions
+37 -11
View File
@@ -1,23 +1,39 @@
# autoPriority
Monitors process memory usage on Windows and automatically adjusts CPU priority: promotes memory-heavy processes to HIGH and demotes everything else to NORMAL.
Monitors process memory usage on Windows and automatically adjusts CPU priority: promotes memory-heavy processes to HIGH and demotes everything else to NORMAL. Optionally enters **Game Mode** when a process exceeds a higher threshold, boosting it to HIGH and setting all other processes to IDLE.
**Windows only.**
## How it works
### Normal mode
Every scan interval the program iterates over all running processes:
| RSS vs threshold | Current priority | Action |
| RSS vs `-mem` | Current priority | Action |
|---|---|---|
| ≥ threshold | anything except HIGH | → HIGH (logged as `PROMOTE`) |
| ≥ threshold | already HIGH | skip |
| < threshold | ABOVE_NORMAL, HIGH, or REALTIME | → NORMAL (logged as `DEMOTE`) |
| < threshold | NORMAL, BELOW_NORMAL, or IDLE | skip |
If `SetPriorityClass` fails for a process (e.g., anti-cheat protection), the process is added to an in-memory exclusion list and never touched again (logged as `BLOCK`). When the process exits, it is automatically removed from the list.
### Game mode (`-game-mem`)
On shutdown, all promoted processes are restored to NORMAL.
When any process reaches or exceeds the `-game-mem` threshold:
1. That process → **HIGH** (logged as `GAME`)
2. All other processes → **IDLE**
3. Log: `GAME MODE ON`
When all such processes close:
1. All processes that were demoted during game mode are **restored to their original priority** (logged as `RESTORE`)
2. `GAME MODE OFF` logged
3. Normal mode resumes — priorities recalculated by `-mem` rules
If `SetPriorityClass` or `OpenProcess` fails for a process (e.g., anti-cheat protection, system processes), the process is added to an in-memory exclusion list and never touched again (logged as `BLOCK`). When the process exits, it is automatically removed from the list.
On shutdown, all processes are restored: game-mode demoted processes to their saved original priority, promoted and game processes to NORMAL.
## Build
@@ -39,15 +55,22 @@ autopriority [flags]
| Flag | Default | Description |
|-------------|---------------|----------------------------------|
| `-mem` | 536870912 | Memory threshold in bytes (512 MB) |
| `-mem` | 512M | Memory threshold (e.g. 512M, 1G, 2048M) |
| `-game-mem` | 4G | Game threshold (e.g. 3G, 4G). Must be > `-mem`. 0 = disabled |
| `-interval` | 1 minute | Scan interval (min 10s) |
| `-dry-run` | false | Log only, don't change priority |
Examples:
```
# Run with 1 GB memory threshold, scanning every 30 seconds
autopriority -mem=1073741824 -interval=30s
# Run with 1 GB threshold, scanning every 30 seconds
autopriority -mem=1G -interval=30s
# Run with defaults (512M threshold, 4G game threshold)
autopriority
# Game mode: normal threshold 512M, game threshold 4G
autopriority -mem=512M -game-mem=4G
# Dry run — log decisions without changing anything
autopriority -dry-run
@@ -61,10 +84,13 @@ Log entries:
| Prefix | Meaning |
|---|---|
| `PROMOTE` | Priority raised to HIGH |
| `DEMOTE` | Priority lowered to NORMAL |
| `BLOCK` | SetPriorityClass failed; process added to exclusion list |
| `RESTORE` | Promoted process restored to NORMAL on shutdown |
| `PROMOTE` | Priority raised to HIGH (normal mode) |
| `DEMOTE` | Priority lowered to NORMAL (normal mode) |
| `GAME` | Process set to HIGH or IDLE (game mode) |
| `GAME MODE ON` | Game mode activated |
| `GAME MODE OFF` | Game mode deactivated — processes restored |
| `BLOCK` | OpenProcess or SetPriorityClass failed; process added to exclusion list |
| `RESTORE` | Process restored to original/saved priority (game mode exit or shutdown) |
| `SKIP RESTORE` | PID reused by a different process; restore skipped |
| `[DRY-RUN]` | Would change priority (dry-run mode) |