Crate rlimit

source ·
Expand description

rlimit - Resource limits.

Set resource limit

use rlimit::{setrlimit, Resource};

const DEFAULT_SOFT_LIMIT: u64 = 4 * 1024 * 1024;
const DEFAULT_HARD_LIMIT: u64 = 8 * 1024 * 1024;
assert!(Resource::FSIZE.set(DEFAULT_SOFT_LIMIT, DEFAULT_HARD_LIMIT).is_ok());

let soft = 16384;
let hard = soft * 2;
assert!(setrlimit(Resource::NOFILE, soft, hard).is_ok());

Get resource limit

use rlimit::{getrlimit, Resource};

assert!(Resource::NOFILE.get().is_ok());
assert_eq!(getrlimit(Resource::CPU).unwrap(), (rlimit::INFINITY, rlimit::INFINITY));

Windows

Windows does not have Unix-like resource limits. It only supports changing the number of simultaneously open files currently permitted at the stdio level.

See the official documentation of _getmaxstdio and _setmaxstdio.

println!("{}", rlimit::getmaxstdio()); // 512
rlimit::setmaxstdio(2048).unwrap();
println!("{}", rlimit::getmaxstdio()); // 2048

Increase NOFILE limit

See the example nofile.

You can also use the tool function rlimit::increase_nofile_limit

rlimit::increase_nofile_limit(10240).unwrap();
rlimit::increase_nofile_limit(u64::MAX).unwrap();

Troubleshoot

Failed to increase NOFILE to hard limit on macOS

On macOS, getrlimit by default reports that the hard limit is unlimited, but there is usually a stricter hard limit discoverable via sysctl (kern.maxfilesperproc). Failing to discover this secret stricter hard limit will cause the call to setrlimit to fail.

rlimit::increase_nofile_limit respects kern.maxfilesperproc.

Structs

  • ProcLimitLinux or Android
    A process’s resource limit field.
  • ProcLimitsLinux or Android
    A process’s resource limits. It is parsed from the proc filesystem.
  • A kind of resource.

Constants

Functions

  • Returns the number of simultaneously open files permitted at the stream I/O level.
  • Get resource limits.
  • Try to increase NOFILE limit and return the current soft limit.
  • prlimitLinux or Android
    Set and get the resource limits of an arbitrary process.
  • Sets a maximum for the number of simultaneously open files at the stream I/O level.
  • Set resource limits.

Type Definitions

  • pid_tLinux or Android
    The type of a process ID