ttrace(n)

πŸ“¦
version 8.6.17 Β· 2 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, epel 9, fedora 43, fedora 44, opensuse-leap 16.0

hey bestie πŸ’– you just opened ttrace(n) β€” the Tcl Threading manual page for trace-based interpreter initialization. it's giving on-demand replication of interpreter state and honestly? iconic. ✨

✨ NAME ✨

ttrace β€” Trace-based interpreter initialization πŸ¦‹


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so first, the package vibes:

package require Tcl  8.4

package require Thread  ?2.8?

ttrace::eval arg ?arg ...?

ttrace::enable

ttrace::disable

ttrace::cleanup

ttrace::update ?epoch?

ttrace::getscript

ttrace::atenable cmd arglist body

ttrace::atdisable cmd arglist body

ttrace::addtrace cmd arglist body

ttrace::addscript name body

ttrace::addresolver cmd arglist body

ttrace::addcleanup body

ttrace::addentry cmd var val

ttrace::getentry cmd var

ttrace::getentries cmd ?pattern?

ttrace::delentry cmd

ttrace::preload cmd

πŸ’– DESCRIPTION πŸ’–

so this package creates a whole framework for on-demand replication of the interpreter state accross threads in a multithreading application. it relies on the mechanics of Tcl command tracing and the Tcl unknown command and mechanism. 🧚

the package requires the Tcl threading extension, but it can alternatively be used stand-alone within the AOLserver, a scalable webserver from America Online. 🌸

in a nutshell, here's a short sample illustrating how ttrace works with the Tcl threading extension:

% package require Ttrace
2.8.0

% set t1 [thread::create {package require Ttrace; thread::wait}]
tid0x1802800

% ttrace::eval {proc test args {return test-[thread::id]}}
% thread::send $t1 test
test-tid0x1802800

% set t2 [thread::create {package require Ttrace; thread::wait}]
tid0x1804000

% thread::send $t2 test
test-tid0x1804000

as seen from above, the ttrace::eval and ttrace::update commands are used to create a thread-wide definition of a simple Tcl procedure and replicate that definition to all, already existing or later created, threads. πŸ‘‘


πŸ‘‘ USER COMMANDS πŸ‘‘

this section describes user-level commands. those commands can be used by script writers to control the execution of the tracing framework. πŸ’…

  • ttrace::eval arg ?arg ...? β€” this command concatenates the given arguments and evaluates the resulting Tcl command with the trace framework enabled. if the command execution was ok, it takes the necessary steps to automatically propagate the trace epoch change to all threads in the application. for AOLserver, only newly created threads actually receive the epoch change. for the Tcl threading extension, all threads created by the extension are automatically updated. if the command execution resulted in a Tcl error, no state propagation takes place. 😭

this is the most important user-level command of the package as it wraps most of the commands described below. this greatly simplifies things, because the user needs to learn just this (one) command in order to effectively use the package. other commands, as described below, are included mostly for the sake of completeness. 🫢

  • ttrace::enable β€” activates all registered callbacks in the framework and starts a new trace epoch. the trace epoch encapsulates all changes done to the interpreter during the time traces are activated. ✨
  • ttrace::disable β€” deactivates all registered callbacks in the framework and closes the current trace epoch. πŸ’€
  • ttrace::cleanup β€” used to clean-up all on-demand loaded resources in the interpreter. it effectively brings the Tcl interpreter to its pristine state. 🧼
  • ttrace::update ?epoch? β€” used to refresh the state of the interpreter to match the optional trace ?epoch?. if the optional ?epoch? is not given, it takes the most recent trace epoch. πŸ”„
  • ttrace::getscript β€” returns a synthetized Tcl script which may be sourced in any interpreter. this script sets the stage for the Tcl unknown command so it can load traced resources from the in-memory database. normally, this command is automatically invoked by other higher-level commands like ttrace::eval and ttrace::update. πŸ“œ

🌸 CALLBACK COMMANDS 🌸

a word upfront: the package already includes callbacks for tracing the following Tcl commands: proc, namespace, variable, load, and rename. additionaly, a set of callbacks for tracing resources (object, clasess) for the XOTcl v1.3.8+, an OO-extension to Tcl, is also provided. this gives a solid base for solving most of the real-life needs and serves as an example for people wanting to customize the package to cover their specific needs. πŸ¦‹

below, you can find commands for registering callbacks in the framework and for writing callback scripts. these callbacks are invoked by the framework in order to gather interpreter state changes, build the in-memory database, perform custom-cleanups and various other tasks. πŸ’–

  • ttrace::atenable cmd arglist body β€” registers a Tcl callback to be activated at ttrace::enable. registered callbacks are activated on FIFO basis. the callback definition includes the name of the callback, cmd, a list of callback arguments, arglist and the body of the callback. effectively, this actually resembles the call interface of the standard Tcl proc command. 🌟
  • ttrace::atdisable cmd arglist body β€” registers a Tcl callback to be activated at ttrace::disable. registered callbacks are activated on FIFO basis. the callback definition includes the name of the callback, cmd, a list of callback arguments, arglist and the body of the callback. effectively, this actually resembles the call interface of the standard Tcl proc command. πŸŒ™
  • ttrace::addtrace cmd arglist body β€” registers a Tcl callback to be activated for tracing the Tcl cmd command. the callback definition includes the name of the Tcl command to trace, cmd, a list of callback arguments, arglist and the body of the callback. effectively, this actually resembles the call interface of the standard Tcl proc command. πŸ”
  • ttrace::addscript name body β€” registers a Tcl callback to be activated for building a Tcl script to be passed to other interpreters. this script is used to set the stage for the Tcl unknown command. registered callbacks are activated on FIFO basis. the callback definition includes the name of the callback, name and the body of the callback. πŸ“œ
  • ttrace::addresolver cmd arglist body β€” registers a Tcl callback to be activated by the overloaded Tcl unknown command. registered callbacks are activated on FIFO basis. this callback is used to resolve the resource and load the resource in the current interpreter. 🧚
  • ttrace::addcleanup body β€” registers a Tcl callback to be activated by the trace::cleanup. registered callbacks are activated on FIFO basis. 🧹
  • ttrace::addentry cmd var val β€” adds one entry to the named in-memory database. πŸ“₯
  • ttrace::getentry cmd var β€” returns the value of the entry from the named in-memory database. πŸ“€
  • ttrace::getentries cmd ?pattern? β€” returns names of all entries from the named in-memory database. πŸ—‚οΈ
  • ttrace::delentry cmd β€” deletes an entry from the named in-memory database. πŸ—‘οΈ
  • ttrace::preload cmd β€” registers the Tcl command to be loaded in the interpreter. commands registered this way will always be the part of the interpreter and not be on-demand loaded by the Tcl unknown command. ⚑

πŸ”₯ DISCUSSION πŸ”₯

common introspective state-replication approaches use a custom Tcl script to introspect the running interpreter and synthesize another Tcl script to replicate this state in some other interpreter. this package, on the contrary, uses Tcl command traces. command traces are registered on selected Tcl commands, like proc, namespace, load and other standard (and/or user-defined) Tcl commands. when activated, those traces build an in-memory database of created resources. this database is used as a resource repository for the (overloaded) Tcl unknown command which creates the requested resource in the interpreter on demand. this way, users can update just one interpreter (master) in one thread and replicate that interpreter state (or part of it) to other threads/interpreters in the process. πŸ‘‘

immediate benefit of such approach is the much smaller memory footprint of the application and much faster thread creation. by not actually loading all necessary procedures (and other resources) in every thread at the thread initialization time, but by deferring this to the time the resource is actually referenced, significant improvements in both memory consumption and thread initialization time can be achieved. some tests have shown that memory footprint of a multithreading Tcl application went down more than three times and thread startup time was reduced for about 50 times. πŸ”₯

note: your mileage may vary. 😌

other benefits include much finer control about what (and when) gets replicated from the master to other Tcl thread/interpreters. πŸ’…


🌈 SEE ALSO 🌈

  • thread
  • tpool
  • tsv

⭐ KEYWORDS ⭐

  • command tracing
  • introspection

source: Tcl Threading, version: 2.8, manual: ttrace(n) β€” the footer didn't give a date, bestie, so that's the whole tea β˜•οΈβœ¨πŸ’–