sys_getloadavg

sys_getloadavg(): array|false

System load-average introspection: returns [1min, 5min, 15min] samples (Unix load = exponentially damped average of runnable plus uninterruptible tasks), false on failure; complements getrusage (process-level) — this one is machine-level; values float with machine state, so only the shape can be asserted.

C C23

C23
/* getloadavg(3): BSD/glibc - the sys_getloadavg original (needs
   _GNU_SOURCE on Linux glibc; macOS declares it in stdlib.h out of the box) */
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double avg[3];
    int n = getloadavg(avg, 3);
    printf("%d\n", n == 3); /* three samples */
    printf("%d\n", avg[0] >= 0.0);
    return 0;
}
Output
1
1

C++ C++26

C++26
#include <cstdlib>
#include <iostream>

// getloadavg(3): the BSD/glibc original behind sys_getloadavg
int main() {
    double avg[3];
    int n = getloadavg(avg, 3);
    std::cout << (n == 3) << "\n"; // three samples
    std::cout << (avg[0] >= 0.0) << "\n";
    return 0;
}
Output
1
1

C# v11.0.100

v11.0.100
using System;

// no loadavg API in .NET: P/Invoke getloadavg(3) is the real route - the std
// self-metrics that exist are process-shaped
Console.WriteLine(Environment.ProcessorCount > 0); // capacity side
Console.WriteLine(Environment.WorkingSet > 0);     // this process' memory
Output
True
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"runtime"
)

// std has no getloadavg: read /proc/loadavg (Linux) or call getloadavg(3)
// via cgo; the std-side companion is core count - load vs capacity
func main() {
	fmt.Println(runtime.NumCPU() > 0)
}
Output
true

Java v27.0.1

v27.0.1
import java.lang.management.ManagementFactory;

public class Main {
    public static void main(String[] args) {
        com.sun.management.OperatingSystemMXBean os =
            (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double load = os.getSystemLoadAverage(); // -1.0 where unsupported (Windows)
        System.out.println(load >= 0); // true on Unix-like systems - the sys_getloadavg analog
        System.out.println(os.getAvailableProcessors() > 0); // capacity side
    }
}
Output
true
true

JavaScript v26.13.1

v26.13.1
// Node os.loadavg(): [1m, 5m, 15m] - zeros on Windows, real numbers on Unix
const os = require("node:os");
const load = os.loadavg();
console.log(load.length === 3);
console.log(load[0] >= 0);
Output
true
true

PHP v8.6.0

v8.6.0
// sys_getloadavg(): [1min, 5min, 15min] load averages - Unix-only
// introspection (getrusage is the process-level sibling); values are
// machine-state, so only the shape is asserted (derived facts)
$load = sys_getloadavg();
var_dump(is_array($load) && count($load) === 3);
var_dump($load[0] >= 0.0);
var_dump(is_numeric($load[1]) && is_numeric($load[2]));
Output
bool(true)
bool(true)
bool(true)

Python v3.15.2

v3.15.2
import os

# os.getloadavg(): the 1:1 analog (missing on Windows - guard with os.name)
load = os.getloadavg()
print(len(load) == 3)
print(load[0] >= 0)
Output
True
True

Rust v1.99.1

v1.99.1
// std is silent; getloadavg(3) is one extern away - the BSD/glibc libc
// call, no crate needed (Windows has no such symbol - notes cover the guard)
use std::os::raw::{c_double, c_int};

extern "C" {
    fn getloadavg(loadavg: *mut c_double, nelem: c_int) -> c_int;
}

fn main() {
    let mut avg = [0.0f64; 3];
    let n = unsafe { getloadavg(avg.as_mut_ptr(), 3) };
    println!("{}", n == 3); // three samples returned
    println!("{}", avg[0] >= 0.0);
}
Output
true
true

TypeScript v26.13.1

v26.13.1
// Node os.loadavg(): [1m, 5m, 15m] - zeros on Windows, real numbers on Unix
import { loadavg } from "node:os";
const load: number[] = loadavg();
console.log(load.length === 3);
console.log(load[0] >= 0);
Output
true
true

Language differences

Machine-level vs process-level: the existing getrusage entry reports this process's resource usage, this one reports the whole machine's load level — the first signal of capacity planning (load/cores > 1 means queueing); the deterministic assertions use derived facts: a three-element array, first element >= 0, the other two numeric — the load values float at second granularity with machine state, so no concrete number is asserted (a textbook case of environment-independent output).

PHP: sys_getloadavg() returns a [1m, 5m, 15m] float array, false on failure; Unix load is the exponentially damped average of runnable plus uninterruptible tasks (not a CPU percentage); Windows lacks the function.

Python: os.getloadavg() is the 1:1 analog (likewise Unix-only, AttributeError on Windows); os.cpu_count() is the capacity-side companion.

Go: no std analog — read /proc/loadavg on Linux (three fields) or call getloadavg(3) via cgo; runtime.NumCPU() is the capacity-side companion.

Java: com.sun.management.OperatingSystemMXBean.getSystemLoadAverage() is the canonical analog (returns -1.0 where unsupported — which the >= 0 assertion turns into a platform tell); note the two interfaces: the load method lives only on the com.sun version, not java.lang.management.OperatingSystemMXBean.

C#: nothing built in — P/Invoke getloadavg(3) or read /proc; Environment.ProcessorCount and Environment.WorkingSet are the std capacity/process-side metrics.

JavaScript/TypeScript: Node os.loadavg() has the same shape (on Windows it returns [0,0,0] instead of erroring — friendlier than Python but a monitoring misread risk, so guard by platform).

C/C++: getloadavg(double[], int) (stdlib.h; Linux glibc needs _GNU_SOURCE, which the validator's compile command already carries) is the prototype and returns the number of samples actually fetched (<= nelem); on Linux parsing the first three fields of /proc/loadavg works equally well.

Rust: std has no such API — an extern "C" declaration of getloadavg is enough (no crate, the libc symbol is linked anyway); Windows lacks the symbol, so cross-platform crates are sysinfo or nix::sys::load (Unix).