Meter and bill scripts
Build a service that runs the scripts of its customers and bills each customer for the work. ztcl counts the work in compute units, and the count of a script is the same on each machine and in each run.

The picture shows a usage report in a ztk window. Three tenants have a budget each. The host ran eight jobs: five ended, one stopped at its cap, one stopped at the memory limit, and the host refused one.
How it works
Each tenant has a budget of compute units. Each job asks for a cap: the most units that it can use. The host reserves the cap before the start, and refuses the job when the tenant has fewer units left. Each other job runs in a thread with a meter of its own:
ztcl::meter create $m -tenant $tenant -budget $cap
set t [thread::create -joinable -isolation $level -caps {} -meter $m]
set code [catch {thread::send $t $src result} err]
catch {thread::release $t}
catch {thread::join $t}
set units [dict get [ztcl::meter get $m] units]
lappend records [ztcl::meter record $m -tenant $tenant -script $src -key $host_seed]
- The meter has the cap as its budget, so the thread stops at the cap with "compute limit reached".
-caps {}gives the job no capability: no files, no clock, and no threads.ztcl::meter recordgives a usage record in JSON, with the SHA-256 of the script and an Ed25519 signature.ztcl::meter verifychecks a record with the public key of the host. A customer can check a record in another language.- The report has only counts that depend on the scripts. So a second run of the same jobs gives the same report, byte for byte. The wall time and the CPU time go to a second file.
The host prints one line for each job and the totals of each tenant:
acme acme-primes.tcl ok 195808 units ... 2262
globex globex-report.tcl ok 22016 units ... {"north":124000,"south":124500,"east":125
initech initech-memory.tcl memory-limit ... the script used too much memory
acme acme-sort.tcl ok 212509 units ... 13 999991
globex globex-strings.tcl ok 57 units ... 40000
initech initech-forever.tcl compute-limit 750000 units ... compute limit reached
initech initech-small.tcl refused cap 200000, left ...
acme acme-primes.tcl ok 195808 units ... 2262
records: 7, verified with the public key: 7
tenant acme budget 3000000 used 604125 left 2395875 jobs 3 refused 0
The job acme-primes.tcl uses 195,808 compute units each time it runs. The units of a job that ends by itself are the same at the levels interp and process.
Run it
cd examples/usecases/metering
mkdir -p out
ztcl --gc off --thread-mem 67108864 --allow thread,meter,file,channels --root . \
--tclsh host.tcl queue.txt out/report.json out/times.json -isolation process
ztk --root . dashboard.tcl out/report.json
queue.txt has one job on each line: the tenant, the cap, and the file of the script. --gc off makes the memory counts exact.
What is in ztcl
The meters, the budgets, the limits, the records, and the signatures are part of ztcl. The host is a prototype: the queue is a file, and the key seed is in the script. A real host gets its jobs over the network, keeps the budgets in a database, and signs with -sign, so the key stays in the embedder.
More
- Metering: what a meter counts, and a hosting example.
- ztcl::meter: the commands and the record format.
- Limits and capabilities: what a compute unit is.
- The source:
examples/usecases/metering.