package kaun-board

  1. Overview
  2. Docs

Module Proc.SelfSource

Current process (self) statistics.

Sourcetype t = {
  1. utime : float;
    (*

    Cumulative user-mode CPU time in seconds.

    *)
  2. stime : float;
    (*

    Cumulative system-mode CPU time in seconds.

    *)
  3. rss_bytes : int64;
    (*

    Resident set size (physical memory) in bytes.

    *)
  4. vsize_bytes : int64;
    (*

    Virtual memory size in bytes.

    *)
}

Raw process snapshot for delta calculation.

Contains cumulative CPU time and instantaneous memory usage for the current process. CPU times are in seconds (converted from platform-specific units by Unix.times).

Sourcetype stats = {
  1. cpu_percent : float;
    (*

    CPU usage percentage (0.0 to 100.0 per core, or total if num_cores provided).

    *)
  2. rss_bytes : int64;
    (*

    Resident set size in bytes.

    *)
  3. vsize_bytes : int64;
    (*

    Virtual memory size in bytes.

    *)
}

Computed process statistics.

Sourceval sample : unit -> t

sample () returns raw CPU times and memory usage for the current process.

Uses Unix.times for CPU times.

  • raises Sys_error

    if an error occurs during sampling.

Sourceval compute : prev:t -> next:t -> dt:float -> num_cores:int option -> stats

compute ~prev ~next ~dt ~num_cores computes CPU usage percentage between two samples.

CPU percentage is calculated as ((utime_delta + stime_delta) / dt) * 100. If num_cores is provided, the percentage is normalized by dividing by the number of cores, yielding a value in 0.0 to 100.0. Without normalization, the value can exceed 100.0 on multi-core systems.

The result is clamped to prevent spurious values from timing anomalies:

  • With num_cores: capped at 100.0
  • Without num_cores: capped at 800.0

If dt is outside the range (0.01, 10.0), returns 0.0 for cpu_percent to avoid division by near-zero or implausibly large intervals.