LinuxCommandLibrary

runlim

Limit resource consumption of a program

TLDR

Print the time and memory usage of a command

$ runlim [command] [command_arguments]
copy

Log statistics to a file instead of stdout
$ runlim --output-file=[path/to/file] [command] [command_arguments]
copy

Limit time to an upper bound (in seconds)
$ runlim --time-limit=[number] [command] [command_arguments]
copy

Limit real-time to an upper bound (in seconds)
$ runlim --real-time-limit=[number] [command] [command_arguments]
copy

Limit space to an upper bound (in MB)
$ runlim --space-limit=[number] [command] [command_arguments]
copy

SYNOPSIS

runlim [options] command [arg ...]

PARAMETERS

-t <seconds>, --time-limit=<seconds>
    Sets the maximum CPU time (in seconds) the command is allowed to use. If the limit is exceeded, the command is terminated.

-r <seconds>, --real-time-limit=<seconds>
    Sets the maximum wall-clock time (in seconds) the command is allowed to run. This measures the actual time elapsed, regardless of CPU usage.

-m <bytes>, --memory-limit=<bytes>
    Sets the maximum memory (in bytes) the command is allowed to allocate. This typically refers to virtual memory, but runlim often monitors resident set size (RSS) as well. Use suffixes like 'K', 'M', 'G' for kilobyte, megabyte, gigabyte.

-o <file>, --output-file=<file>
    Redirects the resource usage statistics and status information to the specified file instead of standard error.

-v, --verbose
    Enables verbose output, providing more detailed information about the execution and resource monitoring process.

-p <seconds>, --patience=<seconds>
    Specifies the time (in seconds) runlim will wait for the child process to exit cleanly after sending it a termination signal (due to a limit being exceeded).

--exit-code-if-timeout=<CODE>
    Sets a custom exit code for runlim itself if the executed command exceeds its time limit.

--exit-code-if-memoryout=<CODE>
    Sets a custom exit code for runlim itself if the executed command exceeds its memory limit.

--version
    Displays the version information of the runlim utility.

--help
    Displays a help message with available options and usage.

DESCRIPTION

runlim is a utility designed to execute a specified command while imposing and monitoring various resource limits. It can enforce limits on CPU time, wall-clock time, and memory consumption (both virtual and resident set size). Upon completion, it reports the actual resources used by the command and its children.

This tool is particularly useful in environments like competitive programming, automated assessment systems, or scientific computing where it's crucial to prevent programs from consuming excessive resources or running indefinitely. It provides a robust way to ensure that submitted solutions or experiments adhere to predefined constraints, preventing resource exhaustion on shared systems and facilitating fair evaluation based on resource efficiency.

CAVEATS

runlim relies on system calls like setrlimit() and getrusage(), which can have system-specific behaviors or limitations.

While it monitors both CPU time and wall-clock time, the precision of these measurements can be affected by system load and kernel scheduler activity.

Memory limits primarily refer to virtual memory; ensuring a strict limit on resident set size (RSS) can be more complex and might not always be perfectly enforced by runlim directly through system limits, though it usually reports RSS.

Be aware that if the monitored command spawns long-running background processes (daemons) that detach from the parent, runlim might not track their resource usage effectively after the parent process exits.

If the command being executed catches and handles termination signals (e.g., SIGTERM) and doesn't exit promptly, runlim might eventually resort to sending a more forceful SIGKILL (controlled by the --patience option).

EXIT CODES

runlim provides specific exit codes to indicate the outcome of the executed command.

If the command finishes successfully without exceeding any limits, runlim usually exits with the same exit code as the executed command.

If a limit (time, memory) is exceeded, runlim will terminate the command and exit with a non-zero status, often a specific code (e.g., 124 for timeout, 125 for memory limit, though this can be customized with --exit-code-if-timeout and --exit-code-if-memoryout). This allows scripts to differentiate between a program's normal failure and a resource-limit violation.

HISTORY

runlim is not a standard utility found in all Unix-like systems' default installations, but it is widely used in specific contexts. It was developed primarily for competitive programming platforms, automated judging systems, and academic research environments where precise resource monitoring and strict enforcement of limits are critical. Its design focuses on providing reliable statistics and control over a program's execution, addressing limitations of simpler tools like time(1) or shell ulimit commands, especially regarding real-time and memory tracking for child processes.

SEE ALSO

time(1), timeout(1), ulimit(1), getrusage(2), setrlimit(2)

Copied to clipboard