beetlebug▾
/

Module ztcl::sysinfo

ztcl::sysinfo reads the numbers of the host: the processors, the memory, the processes, the disks, the volumes, the network, the GPUs, the battery, and the thermal state. One call gives one sample, as top reads one screen.

set before [ztcl::sysinfo sample -parts {cpu procs}]
after 1000
set now [ztcl::sysinfo sample -parts {cpu procs}]
set busy [expr {1.0 - double([dict get $now cpu total idle] - [dict get $before cpu total idle])
          / ([dict get $now cpu total user] + [dict get $now cpu total system] + [dict get $now cpu total idle]
             - [dict get $before cpu total user] - [dict get $before cpu total system] - [dict get $before cpu total idle])}]
puts [format "CPU %.1f%%" [expr {100 * $busy}]]

The command is not a package: it exists when the embedder turns on the capability sysinfo, and it is absent otherwise. The ztcl program turns it on with --allow sysinfo.

Dark Alley, a monitor of the processes in Tk, is the first user, and the sample has the shape of its record.

Commands

ztcl::sysinfo sample ?-parts LIST? ?-gpu BOOLEAN? ?-detail PIDS? ?-interval MS?
ztcl::sysinfo::sample ?-parts LIST? ?-gpu BOOLEAN? ?-detail PIDS? ?-interval MS?

The call charges one compute unit for each part and one for each process.

The sample

The sample is a dict. A counter stays raw and grows from the boot, so a rate needs two samples; a replay of saved samples then gives the same rates. A field that the OS cannot give is absent, and missing gives the reason in one line.

Key Keys inside Unit
os linux, macos, or windows
host name, kernel, cores, model, user text. user is the user of the process that calls.
uptime seconds since the boot
cpu total, cores, kinds, freq, load ns for the counters, MHz for freq
mem total, used, avail, cached, swap_total, swap_used, compressed bytes
procs cols, rows a table, below
disks for each disk: read_b, write_b, read_ops, write_ops, busy_ns bytes, operations, ns
fs for each mount point: size, free, type, dev bytes
net for each interface: rx_b, tx_b, rx_pk, tx_pk bytes, packets
gpus for each GPU: name, vendor, busy_pct, mem_used, mem_total, temp_mc, power_mw, engines, procs %, bytes, mC, mW
sensors for each sensor: kind (temp, fan, or power), value, unit, label mC, rpm, mW
power battery {pct state power_mw}, energy {NAME {uj max_uj}} %, mW, µJ
os_extra linux {...}, macos {thermal}, or windows {...}
missing KEY REASON pairs text

The process table

procs has the names of the columns in cols, and one list for each process in rows. A table costs much less memory than a dict for each process when a monitor keeps samples of 2,000 processes.

Column Unit
pid, ppid
name, user, state text. state is one letter: R running, S sleeping, I idle, T stopped, Z zombie.
cpu_ns ns of CPU time since the start of the process
rss, vsz bytes
threads, nice
start ns from the boot to the start of the process
energy_nj nJ of energy since the start of the process (macOS)
read_b, write_b bytes of disk I/O since the start of the process (macOS)
cmd the command line, for the processes of -detail

A cell that the OS refuses is empty. On macOS a process of another user gives its PID, parent, name, user, and state, and the start for each user; its CPU time, memory, threads, nice, energy, and disk I/O need root, and missing has the key procs.cpu_ns when a row lacks them.

Each OS

OS State Source of the numbers
macOS ztcl#1414 proc_pidinfo, proc_pid_rusage, host_processor_info, host_statistics64, sysctl, getfsstat, NET_RT_IFLIST2, IOKit for the disks, the GPUs, and the battery, and notify_get_state for the thermal pressure
Linux ztcl#1413 none yet: a sample gives os and a reason for each other part
Windows ztcl#1415 none yet, as on Linux

With no backend for the OS, a sample has only os and missing, and missing has a key for each part that -parts asked for. So a caller tests for a backend with one cheap call:

set has_backend [dict exists [ztcl::sysinfo sample -parts cpu] cpu]

On macOS these parts are in missing:

Key Reason
cpu.freq macOS has no public interface for the clock of a core.
gpus.mem_total The GPU of a Mac shares the memory of the system.
gpus.temp_mc, sensors.soc The temperatures of the chip are only in a private API.
power.energy The power of each part needs powermetrics, which needs root.
os_extra.launchd No public API lists the launchd jobs.
power.battery On a Mac with no battery.

Errors

Message -errorcode
bad option "-bogus": must be -parts, -gpu, -detail, or -interval TCL LOOKUP INDEX option
bad part "disk": must be os, host, uptime, ... ZTCL SYSINFO PART
bad interval "-1": must be 0 or more NONE
unknown or ambiguous subcommand "nope": must be sample TCL LOOKUP SUBCOMMAND nope