Wednesday, January 25, 2012

prlimit(1)

prlimit(1) is a new util that will be available in util-linux-2.21 (now -rc1). This new util is very nice and flexible command line interface to prlimit(2) Linux syscall (supported since Linux 2.6.36).

prlimit(1) allows to get or set one or more process resource limits for given PID. When a command is given instead of PID, prlimit(1) will run this command with the given resources.

The output is flexible like output from lsblk(8) or findmnt(8). You can define the output columns, use parsable output etc.

See the default output:
  $ prlimit --pid $$
RESOURCE DESCRIPTION SOFT HARD UNITS
AS address space limit unlimited unlimited bytes
CORE max core file size 0 unlimited blocks
CPU CPU time unlimited unlimited seconds
DATA max data size unlimited unlimited bytes
FSIZE max file size unlimited unlimited blocks
LOCKS max number of file locks held unlimited unlimited
MEMLOCK max locked-in-memory address space 65536 65536 bytes
MSGQUEUE max bytes in POSIX mqueues 819200 819200 bytes
NICE max nice prio allowed to raise 0 0
NOFILE max number of open files 1024 4096
NPROC max number of processes 1024 62809
RSS max resident set size unlimited unlimited pages
RTPRIO max real-time priority 0 0
RTTIME timeout for real-time tasks unlimited unlimited microsecs
SIGPENDING max number of pending signals 62809 62809
STACK max stack size 8388608 unlimited bytes
or redefine the output and ask for max number of open files only:
  $ prlimit  --nofile --output RESOURCE,SOFT,HARD --pid $$
RESOURCE SOFT HARD
NOFILE 1024 4096
and now let's modify the soft limit of maximal core file size and maximal number of open files:
  $ prlimit --core=1000000: --nofile=100: --pid $$
the notation used for the limits is:
  soft:hard    specify both limits
soft: specify only the soft limit
:hard specify only the hard limit
value specify both soft and hard limits to the same value
and check the result:
  $ prlimit  --nofile --core --pid $$
RESOURCE DESCRIPTION SOFT HARD UNITS
NOFILE max number of open files 100 1024
CORE max core file size 1000000 unlimited blocks
and revert the core file soft limit:
  $ prlimit --core=unlimited: --pid $$

$ prlimit --core --pid $$
RESOURCE DESCRIPTION SOFT HARD UNITS
CORE max core file size unlimited unlimited blocks
Do you want to restrict CPU time for given command (sort(1) in this example):
   $ prlimit --cpu=10 sort -u hugefile
I think prlimit(1) is much better than the shell built-in command ulimit.

Thanks to Davidlohr Bueso who found time to implement prlimit(1) for util-linux 2.21.
-- In memory of Dennis M. Ritchie