Module Posix_resource

POSIX resource usage and limits.

This module provides OCaml bindings to POSIX resource functions defined in sys/resource.h.

It includes functions for querying and setting resource limits, getting resource usage statistics, and managing process priorities.

Example

  (* Get resource usage for current process *)
  let usage = Posix_resource.getrusage rusage_self in
  Printf.printf "User CPU time: %s\n"
    (Posix_time2.Timeval.to_string usage.ru_utime);
  Printf.printf "Max RSS: %Ld KB\n" usage.ru_maxrss;

  (* Get file descriptor limit *)
  let limits = Posix_resource.getrlimit rlimit_nofile in
  Printf.printf "Max open files: %s\n"
    (Unsigned.UInt64.to_string limits.rlim_cur)

Resource Limit Constants

val rlimit_cpu : int

CPU time limit in seconds (RLIMIT_CPU).

val rlimit_fsize : int

Maximum file size in bytes (RLIMIT_FSIZE).

val rlimit_data : int

Maximum data segment size (RLIMIT_DATA).

val rlimit_stack : int

Maximum stack size (RLIMIT_STACK).

val rlimit_core : int

Maximum core file size (RLIMIT_CORE).

val rlimit_nofile : int

Maximum number of open files (RLIMIT_NOFILE).

val rlimit_as : int

Maximum address space size (RLIMIT_AS).

val rlim_infinity : Unsigned.uint64

Special value meaning no limit (RLIM_INFINITY).

Resource Usage Constants

val rusage_self : int

Current process (RUSAGE_SELF).

val rusage_children : int

All terminated and waited-for children (RUSAGE_CHILDREN).

Priority Constants

val prio_process : int

Process priority (PRIO_PROCESS).

val prio_pgrp : int

Process group priority (PRIO_PGRP).

val prio_user : int

User priority (PRIO_USER).

Resource Limit Types

type rlimit = {
  1. rlim_cur : Unsigned.uint64;
    (*

    Current (soft) limit

    *)
  2. rlim_max : Unsigned.uint64;
    (*

    Maximum (hard) limit

    *)
}

Resource Usage Types

type rusage = {
  1. ru_utime : Posix_time2.Timeval.t;
  2. ru_stime : Posix_time2.Timeval.t;
  3. ru_maxrss : int64;
  4. ru_ixrss : int64;
  5. ru_idrss : int64;
  6. ru_isrss : int64;
  7. ru_minflt : int64;
  8. ru_majflt : int64;
  9. ru_nswap : int64;
  10. ru_inblock : int64;
  11. ru_oublock : int64;
  12. ru_msgsnd : int64;
  13. ru_msgrcv : int64;
  14. ru_nsignals : int64;
  15. ru_nvcsw : int64;
  16. ru_nivcsw : int64;
}

Resource usage information returned by getrusage. Corresponds to POSIX struct rusage.

Resource Limit Functions

val getrlimit : int -> rlimit

Get resource limits.

See getrlimit(2).

  • parameter resource

    One of the rlimit_* constants.

  • returns

    The current soft and hard limits.

  • raises Unix.Unix_error

    on failure.

val setrlimit : int -> rlimit -> unit

Set resource limits.

See setrlimit(2).

  • parameter resource

    One of the rlimit_* constants.

  • parameter limits

    The new soft and hard limits.

  • raises Unix.Unix_error

    on failure.

Resource Usage Functions

val getrusage : int -> rusage

Get resource usage statistics.

See getrusage(2).

  • returns

    Resource usage information.

  • raises Unix.Unix_error

    on failure.

Priority Functions

val getpriority : int -> int -> int

Get process scheduling priority.

See getpriority(2).

  • parameter who

    Process ID, process group ID, or user ID (0 for current).

  • returns

    The priority value.

  • raises Unix.Unix_error

    on failure.

val setpriority : int -> int -> int -> unit

Set process scheduling priority.

See setpriority(2).

  • parameter who

    Process ID, process group ID, or user ID (0 for current).

  • parameter prio

    The new priority value.

  • raises Unix.Unix_error

    on failure.