Many operating systems support limiting the amount of resources (for instance, memory or CPU time) that a given instance of a running program (“process”) may use. Processes that use more than the alloted resource may have requests denied (as in the case of memory or disk usage), or may be terminated. In some cases it is useful to apply these limits to Macaulay2.
In Unix-like operating systems (including Linux and OS X) these limits are known as “ulimits” or “rlimits”. Users typically set these limits using a built-in shell command called ulimit or similar; for help, consult the documentation for your shell (e.g., run man bash). In particular, the units of any limits can vary from system to system. There are two types of limits, hard and soft: soft levels are always less than or equal to the hard limits, and hard levels can only be adjusted down. When a process creates a child process, the child process receives the ulimits of the parent process.
As an example, we give commands for a Linux system using a Bash shell. To view the current limits, run:
ulimit -a
To set soft ulimits on your shell of 123456 kilobytes of memory and 5 seconds of CPU time, run:
ulimit -S -m 123456 -S -t 5
Because ulimits are inherited by child processes, any commands run later in your shell session (say, Macaulay2) will inherit these ulimits. Perhaps the best way to set the limits on a specific process is to run something like
(ulimit -S -m 123456 -S -t 5; M2)
which will run M2 with these limits while leaving the shell’s ulimits unchanged.
From within Macaulay2, you can view the ulimits currently available to the Macaulay2 process by using the run command:
i1 : run("ulimit -a") core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 40731 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) 850000 open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) 700 max user processes (-u) 40731 virtual memory (kbytes, -v) 850000 file locks (-x) unlimited o1 = 0 |
This starts a new shell and executes the command given, which in this case provides the list of ulimits of the shell. Since ulimits are inherited, this should be the same as the ulimits of Macaulay2 itself.
From within Macaulay2, it is not possible to set ulimits on the current Macaulay2 process because Macaulay2 does not provide access to the setrlimit system call (other than the limitFiles and limitProcesses commands). However, it is possible to set ulimits on child Macaulay2 processes that are started by runExternalM2, by using the PreRunScript option of runExternalM2.