2

Edit: there is now an open bug for this.

Edit 2: the workaround for this issue is to use chrt as pointed out below.

Edit 3: this is now fixed as of v252 via https://github.com/systemd/systemd/commit/40c05a34595ed769ce676206f3c5de874f9a9234

Running debian buster with 5.4.91 with PREEMT_RT patch on an embedded system. systemd version is 247.

Attempting to set a process to realtime priority. I'm unable to natively set realtime priority to a process from a systemd.service unit.

There are several links out there, one aptly titled My Service Can't Get Realtime! that seem to point in the right direction but I'm unable to get it to work.

This is what I have in my service file:

[Service]
RestrictRealtime=false
LimitRTPRIO=infinity
CPUSchedulingPriority=99
CPUSchedulingPolicy=rr
IOSchedulingClass=realtime
IOSchedulingPriority=3
ExecStart=/usr/bin/local/foo.bar
Type=notify
....

Vexingly, if I set

[Service]
LimitRTPRIO=infinity
ExecStart=/usr/bin/chrt --rr 99 /usr/bin/local/foo.bar

I get the desired result - even with a non root user.

Why is systemd not natively letting me do this?

Edit: dump of systemctl show as requested:

Type=notify
Restart=always
NotifyAccess=main
RestartUSec=3s
TimeoutStartUSec=3s
TimeoutStopUSec=2s
TimeoutAbortUSec=2s
TimeoutStartFailureMode=terminate
TimeoutStopFailureMode=terminate
RuntimeMaxUSec=infinity
WatchdogUSec=3s
WatchdogTimestamp=Thu 2021-01-28 18:42:01 UTC
WatchdogTimestampMonotonic=426381837441
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
MainPID=4758
ControlPID=0
FileDescriptorStoreMax=0
NFileDescriptorStore=0
StatusErrno=0
Result=success
ReloadResult=success
CleanResult=success
UID=107
GID=113
NRestarts=0
OOMPolicy=stop
ExecMainStartTimestamp=Thu 2021-01-28 18:40:40 UTC
ExecMainStartTimestampMonotonic=426300331189
ExecMainExitTimestampMonotonic=0
ExecMainPID=4758
ExecMainCode=0
ExecMainStatus=0
ExecStart={ path=/usr/local/bin/foo.bar ; argv[]=/usr/local/bin/foo.bar --daemonize ; ignore_errors=no ; start_time=[Thu 2021-01-28 18:40:40 UTC] ; stop_time=[n/a] ; pid=4758 ; code=(null) ; status=0/0 }
ExecStartEx={ path=/usr/local/bin/foo.bar; argv[]=/usr/local/bin/foo.bar --daemonize ; flags= ; start_time=[Thu 2021-01-28 18:40:40 UTC] ; stop_time=[n/a] ; pid=4758 ; code=(null) ; status=0/0 }
ExecReload={ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
ExecReloadEx={ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; flags= ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
Slice=system.slice
ControlGroup=/system.slice/foo.bar.service
MemoryCurrent=1081344
CPUUsageNSec=6725421000
EffectiveCPUs=
EffectiveMemoryNodes=
TasksCurrent=5
IPIngressBytes=[no data]
IPIngressPackets=[no data]
IPEgressBytes=[no data]
IPEgressPackets=[no data]
IOReadBytes=18446744073709551615
IOReadOperations=18446744073709551615
IOWriteBytes=18446744073709551615
IOWriteOperations=18446744073709551615
Delegate=no
CPUAccounting=yes
CPUWeight=[not set]
StartupCPUWeight=[not set]
CPUShares=[not set]
StartupCPUShares=[not set]
CPUQuotaPerSecUSec=infinity
CPUQuotaPeriodUSec=infinity
AllowedCPUs=
AllowedMemoryNodes=
IOAccounting=no
IOWeight=[not set]
StartupIOWeight=[not set]
BlockIOAccounting=no
BlockIOWeight=[not set]
StartupBlockIOWeight=[not set]
MemoryAccounting=yes
DefaultMemoryLow=0
DefaultMemoryMin=0
MemoryMin=0
MemoryLow=0
MemoryHigh=infinity
MemoryMax=infinity
MemorySwapMax=infinity
MemoryLimit=infinity
DevicePolicy=auto
TasksAccounting=yes
TasksMax=1061
IPAccounting=no
ManagedOOMSwap=auto
ManagedOOMMemoryPressure=auto
ManagedOOMMemoryPressureLimitPercent=0%
Environment=PATH=/usr/local/bin:/usr/bin:/bin
EnvironmentFiles=/etc/environment (ignore_errors=no)
UMask=0022
LimitCPU=infinity
LimitCPUSoft=infinity
LimitFSIZE=infinity
LimitFSIZESoft=infinity
LimitDATA=infinity
LimitDATASoft=infinity
LimitSTACK=infinity
LimitSTACKSoft=8388608
LimitCORE=infinity
LimitCORESoft=0
LimitRSS=infinity
LimitRSSSoft=infinity
LimitNOFILE=524288
LimitNOFILESoft=1024
LimitAS=infinity
LimitASSoft=infinity
LimitNPROC=3539
LimitNPROCSoft=3539
LimitMEMLOCK=65536
LimitMEMLOCKSoft=65536
LimitLOCKS=infinity
LimitLOCKSSoft=infinity
LimitSIGPENDING=3539
LimitSIGPENDINGSoft=3539
LimitMSGQUEUE=819200
LimitMSGQUEUESoft=819200
LimitNICE=0
LimitNICESoft=0
LimitRTPRIO=infinity
LimitRTPRIOSoft=infinity
LimitRTTIME=infinity
LimitRTTIMESoft=infinity
WorkingDirectory=/opt/foo
RootHashSignature=
OOMScoreAdjust=0
CoredumpFilter=0x33
Nice=0
IOSchedulingClass=1
IOSchedulingPriority=3
CPUSchedulingPolicy=0
CPUSchedulingPriority=0
CPUAffinity=
CPUAffinityFromNUMA=no
NUMAPolicy=n/a
NUMAMask=
TimerSlackNSec=50000
CPUSchedulingResetOnFork=no
NonBlocking=no
StandardInput=null
StandardInputData=
StandardOutput=journal
StandardError=journal
TTYReset=no
TTYVHangup=no
TTYVTDisallocate=no
SyslogPriority=30
SyslogLevelPrefix=yes
SyslogLevel=6
SyslogFacility=3
LogLevelMax=-1
LogRateLimitIntervalUSec=0
LogRateLimitBurst=0
SecureBits=0
CapabilityBoundingSet=cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap
AmbientCapabilities=
User=foo
Group=foo
DynamicUser=no
RemoveIPC=no
MountFlags=
PrivateTmp=yes
PrivateDevices=no
ProtectClock=no
ProtectKernelTunables=no
ProtectKernelModules=no
ProtectKernelLogs=no
ProtectControlGroups=no
PrivateNetwork=no
PrivateUsers=no
PrivateMounts=no
ProtectHome=read-only
ProtectSystem=full
SameProcessGroup=no
UtmpMode=init
IgnoreSIGPIPE=yes
NoNewPrivileges=no
SystemCallErrorNumber=2147483646
LockPersonality=no
RuntimeDirectoryPreserve=no
RuntimeDirectoryMode=0755
RuntimeDirectory=foo
StateDirectoryMode=0755
CacheDirectoryMode=0755
LogsDirectoryMode=0755
LogsDirectory=foo
ConfigurationDirectoryMode=0755
TimeoutCleanUSec=infinity
MemoryDenyWriteExecute=no
RestrictRealtime=no
RestrictSUIDSGID=no
RestrictNamespaces=no
MountAPIVFS=no
KeyringMode=private
ProtectProc=default
ProcSubset=all
ProtectHostname=no
KillMode=mixed
KillSignal=15
RestartKillSignal=15
FinalKillSignal=9
SendSIGKILL=yes
SendSIGHUP=no
WatchdogSignal=6
Id=foo.bar.service
Names=foo.bar.service
Requires=sysinit.target var.mount system.slice -.mount tmp.mount
PartOf=foo.service
Conflicts=shutdown.target
Before=shutdown.target
After=systemd-journald.socket sysinit.target var.mount -.mount systemd-remount-fs.service tmp.mount sys-subsystem-net-devices-can1.device systemd-tmpfiles-setup.service sys-subsystem-net-devices-eth0.2.device system.slice basic.target
RequiresMountsFor=/run/foo /var/log/foo /opt/foo /tmp /var/tmp
Description=foo bar
LoadState=loaded
ActiveState=active
FreezerState=running
SubState=running
FragmentPath=/lib/systemd/system/foo.bar.service
UnitFileState=disabled
UnitFilePreset=enabled
StateChangeTimestamp=Thu 2021-01-28 18:40:40 UTC
StateChangeTimestampMonotonic=426300816780
InactiveExitTimestamp=Thu 2021-01-28 18:40:40 UTC
InactiveExitTimestampMonotonic=426300357891
ActiveEnterTimestamp=Thu 2021-01-28 18:40:40 UTC
ActiveEnterTimestampMonotonic=426300816780
ActiveExitTimestamp=Thu 2021-01-28 18:40:39 UTC
ActiveExitTimestampMonotonic=426300199732
InactiveEnterTimestamp=Thu 2021-01-28 18:40:40 UTC
InactiveEnterTimestampMonotonic=426300244778
CanStart=yes
CanStop=yes
CanReload=yes
CanIsolate=no
CanClean=runtime logs
CanFreeze=yes
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=no
NeedDaemonReload=no
JobTimeoutUSec=infinity
JobRunningTimeoutUSec=infinity
JobTimeoutAction=none
ConditionResult=yes
AssertResult=yes
ConditionTimestamp=Thu 2021-01-28 18:40:40 UTC
ConditionTimestampMonotonic=426300306353
AssertTimestamp=Thu 2021-01-28 18:40:40 UTC
AssertTimestampMonotonic=426300306377
Transient=no
Perpetual=no
StartLimitIntervalUSec=1s
StartLimitBurst=3
StartLimitAction=none
FailureAction=none
SuccessAction=none
InvocationID=3250e79f3cdc43268f1de989ad58b14b
CollectMode=inactive
7
  • what is the output of systemctl show <service> ? Jul 23, 2021 at 3:42
  • That's a pretty large dump. Do you have anything in particular you're looking for?
    – user247243
    Jul 23, 2021 at 3:50
  • 1
    I wanted to see what the effective values for the service in the dump show for CPUSchedulingPolicy and CPUSchedulingPriority (plus others). In your dump, the values are CPUSchedulingPolicy=0 & CPUSchedulingPriority=0. That's not what you've defined in the service. This is a known bug in older versions of systemd. Jul 23, 2021 at 15:13
  • 1
    I suspected this may be involved, github.com/systemd/systemd/pull/10292 Jul 23, 2021 at 15:19
  • Thanks. I initially did a bunch of investigation into slices and cgroups etc thinking that maybe I was hitting a quota, but the successful call to chrt was my proof negative of that. I just checked out the link you provided and followed through to this issue github.com/systemd/systemd/issues/10290 , but unfortunately, the systemd-run minimal test passes on my system (i.e. it correctly gives the rr value). So it may be that this is a slightly different bug and that I need to file it.
    – user247243
    Jul 24, 2021 at 14:14

0

You must log in to answer this question.

Browse other questions tagged .