Use systemd unit to create AMD uprof device

This commit is contained in:
Rodrigo Arias 2025-09-05 12:41:27 +02:00
parent de006f5cf0
commit 51a77777f1

View File

@ -22,23 +22,28 @@
# Make the userspace tools available in $PATH.
environment.systemPackages = with pkgs; [ amd-uprof ];
# Add extra udev rules to setup the /dev/AMDPowerProfiler device. They
# forgot to add these.
services.udev.extraRules = let
# To create the device node we need to read the device number from the
# /proc directory, so the module must have been loaded first.
addDevice = pkgs.writeShellScript "add-amd-uprof-dev.sh" ''
set -x
echo date > /tmp/uprof.log
mknod /dev/AMDPowerProfiler -m 666 c $(< /proc/AMDPowerProfiler/device) 0
'';
removeDevice = pkgs.writeShellScript "remove-amd-uprof-dev.sh" ''
rm /dev/AMDPowerProfiler
'';
in
''
KERNEL=="AMDPowerProfiler", SUBSYSTEM=="module", ACTION=="add", RUN+="${addDevice}"
KERNEL=="AMDPowerProfiler", SUBSYSTEM=="module", ACTION=="remove", RUN+="${removeDevice}"
'';
# The AMDPowerProfiler module doesn't create the /dev device nor it emits
# any uevents, so we cannot use udev rules to automatically create the
# device. Instead, we run a systemd unit that does it after loading the
# modules.
systemd.services.amd-uprof-device = {
description = "Create /dev/AMDPowerProfiler device";
after = [ "systemd-modules-load.service" ];
wantedBy = [ "multi-user.target" ];
unitConfig.ConditionPathExists = [
"/proc/AMDPowerProfiler/device"
"!/dev/AMDPowerProfiler"
];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "add-amd-uprof-dev.sh" ''
mknod /dev/AMDPowerProfiler -m 666 c $(< /proc/AMDPowerProfiler/device) 0
'';
ExecStop = pkgs.writeShellScript "remove-amd-uprof-dev.sh" ''
rm -f /dev/AMDPowerProfiler
'';
};
};
};
}