Module cpu

Linux CPU Lua interface.

This module provides access to Linux's CPU abstractions.

Functions

foreach_online (callback) Iterates over all online CPUs and calls a function for each.
foreach_possible (callback) Iterates over all possible CPUs and calls a function for each.
foreach_present (callback) Iterates over all present CPUs and calls a function for each.
num_online () Returns the number of online CPUs in the system.
num_possible () Returns the number of possible CPUs in the system.
num_present () Returns the number of CPUs present in the system.
stats (cpu) Gets CPU statistics for a specific CPU.


Functions

foreach_online (callback)
Iterates over all online CPUs and calls a function for each.

Parameters:

  • callback function Function to call for each online CPU

Usage:

    cpu.foreach_online(function(cpu_num)
        print("CPU", cpu.stats(cpu_num))
    end)
foreach_possible (callback)
Iterates over all possible CPUs and calls a function for each.

Parameters:

  • callback function Function to call for each possible CPU

Usage:

    cpu.foreach_possible(function(cpu_num)
        print("CPU", cpu.stats(cpu_num))
    end)
foreach_present (callback)
Iterates over all present CPUs and calls a function for each.

Parameters:

  • callback function Function to call for each present CPU

Usage:

    cpu.foreach_present(function(cpu_num)
        print("CPU", cpu.stats(cpu_num))
    end)
num_online ()
Returns the number of online CPUs in the system.

Returns:

    integer The number of online CPUs in the system
num_possible ()
Returns the number of possible CPUs in the system.

Returns:

    integer The number of possible CPUs in the system
num_present ()
Returns the number of CPUs present in the system.

Returns:

    integer The number of CPUs present in the system
stats (cpu)
Gets CPU statistics for a specific CPU. Fetches kernel CPU statistics including user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice and forceidle times.

Parameters:

  • cpu integer The CPU number (0-based) to query.

Returns:

    table A table containing CPU time statistics with the following fields:

Raises:

Error if CPU is offline

Usage:

    local cpu0 = cpu.stats(0)
    print("CPU 0 user time:", cpu0.user)
    print("CPU 0 idle time:", cpu0.idle)
generated by LDoc 1.5.0 Last updated 2026-02-16 16:00:29