xmrig's RandomX pollutes the L3 cache, making other processes appear ~3-8% busier. With a single 5% threshold for both stopping and resuming, the script oscillates: start xmrig -> cache pressure inflates CPU -> stop xmrig -> CPU drops -> restart -> repeat. Split into CPU_STOP_THRESHOLD (15%) and CPU_RESUME_THRESHOLD (5%). The stop threshold sits above xmrig's indirect pressure, so only genuine workloads trigger a pause. The resume threshold confirms the system is truly idle before restarting.
36 lines
950 B
Nix
36 lines
950 B
Nix
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
lib.mkIf config.services.xmrig.enable {
|
|
systemd.services.xmrig-auto-pause = {
|
|
description = "Auto-pause xmrig when other services need CPU";
|
|
after = [ "xmrig.service" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
serviceConfig = {
|
|
ExecStart = "${pkgs.python3}/bin/python3 ${./xmrig-auto-pause.py}";
|
|
Restart = "always";
|
|
RestartSec = "10s";
|
|
NoNewPrivileges = true;
|
|
ProtectHome = true;
|
|
ProtectSystem = "strict";
|
|
PrivateTmp = true;
|
|
RestrictAddressFamilies = [
|
|
"AF_UNIX" # systemctl talks to systemd over D-Bus unix socket
|
|
];
|
|
MemoryDenyWriteExecute = true;
|
|
StateDirectory = "xmrig-auto-pause";
|
|
};
|
|
environment = {
|
|
POLL_INTERVAL = "3";
|
|
GRACE_PERIOD = "15";
|
|
CPU_STOP_THRESHOLD = "15";
|
|
CPU_RESUME_THRESHOLD = "5";
|
|
STARTUP_COOLDOWN = "10";
|
|
STATE_DIR = "/var/lib/xmrig-auto-pause";
|
|
};
|
|
};
|
|
}
|