Observe a running program
ztcl top shows the interpreters and the threads of a program while it runs: the state of each one, its compute units each second and their trend, its memory against its limit, the proc that it runs, and its WebAssembly instances. The program serves the numbers when it runs with --observe. The service is off by default, it listens only on the local machine, and no script can reach it.
ztcl --allow thread,event --observe app.tcl &
ztcl top PID
On a terminal, ztcl top opens a panel that updates 10 times a second. Each picture on this page comes from the panel itself: ztcl top --shots DIR draws each screen of a fixed program, and a check of the CI compares them with the pictures here.
Start the service
Each of these turns the service on for a run of the ztcl program:
- The flag
--observe, or--observe=ADDRESSfor an address of your choice. - The environment variable
ZTCL_OBSERVE=1, orZTCL_OBSERVE=ADDRESS.
The program prints the address on stderr before the first script runs, and removes the socket and the token file when it ends. The default address depends on the system:
| System | Default address | Description |
|---|---|---|
| Linux | $XDG_RUNTIME_DIR/ztcl/observe-PID.sock |
A Unix socket of mode 0600. With no XDG_RUNTIME_DIR, the folder is ztcl-UID in TMPDIR or /tmp. |
| macOS | $TMPDIR/ztcl-UID/observe-PID.sock |
A Unix socket of mode 0600. |
| Windows | \\.\pipe\ztcl-observe-PID |
A named pipe that only the user can open, and no remote client. |
A build with -Dobserve=false has no service: --observe then gives the error --observe is not in this build.
The panel
ztcl top PID
ztcl top ADDRESS
PID is the process of the program, which finds the default address. ADDRESS is the address that --observe=ADDRESS named. With no service at the address, ztcl top prints no observe service: start the program with --observe and exits with 1.
The panel fills the terminal, and it is made for 80 columns and 24 rows or more. Only the cells that change go to the terminal, so it does not flicker, and a value eases from one sample to the next. It takes the colors that the terminal has: 24-bit color, 256 colors, 16 colors, or none with NO_COLOR. A terminal with a light background (COLORFGBG ends in 7 or 15) gets the light colors. Each state has a mark besides its color, such as ● for busy, ○ for idle, and ! for a memory use of 90 % and more. --ascii draws the panel with ASCII characters only.
The threads
The top band has the state of the connection, the program, its pid, and its uptime; the totals of the program at the right; and one meter of the memory of all the interpreters against their limits. Below it, each interpreter has a row. A child interpreter and a WebAssembly instance are under their interpreter in a tree, which folds. The cursor row has a bar at the left and a soft background, so its charts keep their colors.
| Column | Description |
|---|---|
NAME |
main for the interpreter of the program, the id of a thread such as tid0x2, or interpN for a child interpreter. ▾ or ▸ marks an interpreter with rows under it, open or folded. |
KIND |
At 110 columns and more: interp, thread, or child. |
STATE |
● busy when the interpreter used compute units since the last sample, and ○ idle otherwise. |
CU/S |
The compute units each second. A value that jumps flashes for a moment. |
TREND |
The compute units each second of the last samples, the newest at the right, scaled from their low to their high, with the peak in a bright color. |
MEM |
The memory of the interpreter. |
USE |
A meter of the memory against the limit, green, amber from 70 %, and red from 90 %, and its percent. none with no limit. |
PROC |
The innermost proc at the last sample. |
A row that is idle and has not changed for 5 seconds dims. s sorts the rows by compute units, memory, or name, in turn (c and m pick compute and memory), and ▼ marks the column of the sort. A click on the head of a column sorts by it too.
h or the left arrow folds the tree of the cursor row, and l or the right arrow opens it:
At 30 rows and more, a pane at the bottom has the detail of the cursor row: its stack of procs, its memory, its capabilities, its compute units, and its WebAssembly instances. At 140 columns and more, the pane is at the right.
With no color, the marks keep the meaning; --ascii draws the same screen with ASCII characters:
A thread
Enter opens the detail of the thread at the cursor: its state and capabilities, a trend of its compute units over the last 60 samples, its memory against its limit with its peak, and its stack of procs, innermost first, with the line of the innermost proc when the line is known. Code of the JIT has no line. j and k go to the next thread, and esc or h goes back.
An interpreter with WebAssembly instances has a table of them: the handle, the module, the pages of its memory, the calls of ztcl::wasm call, the calls of the module to Tcl commands (its imports of -imports), the traps, and the engine, jit or interp.
Memory and WebAssembly
Tab goes through the views, and 1, 2, and 3 pick one. The memory view has the memory of each interpreter, the most first: what it uses, its peak, its limit, and a meter.
The WebAssembly view has the instances of all the interpreters. A count of traps above 0 is red.
Search, filter, and pause
/ searches: the cursor goes to the first row whose name, kind, or proc has the text, with no case, as the text grows. The matches are in the accent color, and n and N go to the next and the last match. Esc ends the search.
f filters: only the rows that have the text stay. Enter keeps the filter, and esc clears it.
p or space pauses the view: the numbers stop, and the service still sends them. p again shows the newest data.
Keys
The keys of vim work beside the arrows. ? shows them, and each other key closes the help.
| Key | Description |
|---|---|
↑ ↓ j k |
Move the cursor. |
Ctrl-d Ctrl-u |
Move half a page. |
Ctrl-f Ctrl-b PgDn PgUp |
Move a page. |
gg G Home End |
Go to the first or the last row. |
enter |
Open the detail of the thread at the cursor. |
← → h l |
Fold or open the tree of the cursor row; h and ← go back from a view. |
za zo zc |
Toggle, open, or close the fold of the cursor row. |
zR zM |
Open all the folds, or close all of them. |
/ n N |
Search, go to the next match, go to the last match. |
f |
Filter the rows. |
s c m |
Sort: the next order, compute units, memory. |
tab 1 2 3 |
The views: threads, memory, WebAssembly. |
p space |
Pause the view, or show the newest data. |
esc |
Go back, and end a search. |
q Ctrl-c |
Quit. |
The mouse works too: a click picks a row, a second click opens its detail, a click on the head of a column sorts by it, and the wheel moves the cursor.
The states of the connection
Before the first data, the panel says where it connects. When the program ends, a line says so, and the panel keeps the last data until q.
One table or JSON
ztcl top --once PID
ztcl top --json PID
--once prints one table and ends, as ztcl top does when its output is no terminal, such as in a pipe. --json prints the snapshot as the service sends it.
$ ztcl --allow thread,event --observe app.tcl &
ztcl: observe at \\.\pipe\ztcl-observe-7976
$ ztcl top --once 7976
app.tcl pid 7976 protocol 1
ID NAME KIND STATE CU/S UNITS MEM/LIMIT MiB PROC
1 main interp busy 81 156 0.7/2048.0
wasm0 wasmmod0 pages 1 calls 16 to Tcl 16 traps 0 jit
2 tid0x2 thread busy 1.2G 2.4G 0.6/256.0 poll
This is a run on Windows of a program with two parts. The main interpreter calls a WebAssembly instance from a timer, and the instance calls a Tcl command at each call. A thread runs poll, a loop, from fetchAll. The table has the columns of the panel, with UNITS, the compute units since the service saw the interpreter start, in place of the trend.
Privacy and safety
- The service is off unless the person who starts the program turns it on. No script command turns it on or reads it, and a script sees nothing new.
- It listens on a Unix socket of mode 0600 or a named pipe with an access list of the user. It never listens on a network.
- A client first sends a token. The program writes the token to a file of mode 0600 next to the socket (on Windows, in
%LOCALAPPDATA%\ztcl, with the access list of the user). A wrong token closes the connection with no answer. - The service only reads. It cannot change the program, run a script, or pause a thread.
Cost
With the service off, a run does the same work as before: the interpreter has no hook on the path of a command. The hooks run when an interpreter starts or ends.
The service asks each interpreter for its stack at each sample. The interpreter writes it at its next check of the memory limit, a check that it makes anyway, so a busy interpreter answers within one sample and an idle one keeps its last stack. The counts of the WebAssembly instances are additions at each call of ztcl::wasm call and of an import, and they are there with the service off too.
With the service on, a thread of the service samples the counters 10 times a second. It reads the compute units and the memory of each interpreter, and the interpreters do not wait for it. Each client has a thread of its own, so a slow client gets fewer updates and the program never waits for it.
The protocol
A client can be any program that opens the socket or the pipe. Each message is one JSON object on one line.
- The client sends the token:
{"token":"HEX"}, with the text of the token file. - The service answers
{"hello":1,"level":"read","pid":PID,"program":"NAME"}. The number ofhellois the version of the protocol. - The client sends requests with an
id, and each answer has the sameid.
| Request | Description |
|---|---|
{"id":1,"op":"snapshot"} |
One answer {"id":1,"snapshot":{...}} with protocol, program, pid, t (ms since the start of the service), hz, and nodes, one object for each interpreter. |
{"id":2,"op":"subscribe","topic":"threads","hz":10} |
The answer {"id":2,"ok":true,"hz":10}, then at that rate (1 to 60) a message {"topic":"threads","t":MS,"nodes":[...],"gone":[...]} with the nodes that changed and the ids of the interpreters that ended. A message with no change is not sent. |
{"id":3,"op":"unsubscribe","topic":"threads"} |
Ends the messages of the topic. |
A node has the fields id, kind, name, parent (the id of the parent interpreter of a child, or 0), state, units, units_s, mem, mem_peak, mem_limit (null with no limit), gcs (the collections of the heap of the interpreter; a child shares the heap of its root), caps, the capabilities that are on, proc and line (the innermost proc and its line, or "" and 0), depth (the count of proc frames), stack (the names of the procs, innermost first), and wasm, one object for each WebAssembly instance with name, module, pages, max_pages, calls, host_calls, traps, and engine. A request that is no JSON object gets {"error":"bad request"}, and an unknown request or topic gets an error with its id. New fields can come with no change of the version; a removed or changed field gives a new version.
See also
- Debugging: probes,
ztcl debug, record and replay, and a debugger for an editor. - The
ztclprogram: each flag of the runner.