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.
- It only reads. It opens no file of the root folder, sends no signal, and changes no setting.
- It is off by default. Its values depend on the host, so a script with it is no longer deterministic (rule 4 of the sandbox).
- It shows other programs. The list of processes has the name and the user of each process of the machine, and with
-detailthe command line of the processes of the same user. A command line can hold a secret, such as a token in an argument.
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?
-partspicks the parts of the sample. The default is each part. The keyosis in each sample, so-parts osgives the OS of the host with no other read.-gpu 1adds the GPU time of each process to each GPU. The read is slow.-detail PIDSadds the command line of these processes, in the columncmd. The read is slow, so a monitor asks for the rows that it shows.-interval MSis the time between two samples of the caller. A backend that measures a rate itself uses it; the macOS backend reads only counters.
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 |
cpu totaland each item ofcpu coresare dicts of CPU time in ns:user,system,idle,iowait, andother. macOS counts no I/O wait, so itsiowaitstays 0.cpu kindsgives the indexes of the performance cores and of the efficiency cores, as{p {4 5 6 7} e {0 1 2 3}}; a CPU with one kind of core has nokinds.cpu loadis the load average of 1, 5, and 15 minutes.gpus NAME enginesis a dict of the busy percent of each engine.gpus NAME procs, with-gpu 1, maps each PID to{busy_ns NS}.power battery stateischarging,discharging,full, ornot charging.os_extra macos thermalis the thermal pressure:nominal,fair,serious, orcritical.- A key of
missingis a part, such asfs, or a key of a part, such ascpu.freq.
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 |