ttrace(3tcl) | ttrace(3tcl) |
ttrace - Trace-based interpreter initialization
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
This package creates a framework for on-demand replication of the interpreter state accross threads in an multithreading application. It relies on the mechanics of Tcl command tracing and the Tcl unknown command and mechanism.
The package requires Tcl threading extension but can be alternatively used stand-alone within the AOLserver, a scalable webserver from America Online.
In a nutshell, a short sample illustrating the usage of the ttrace 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.
This section describes user-level commands. Those commands can be used by script writers to control the execution of the tracing framework.
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 user need to learn just this (one) command in order to effectively use the package. Other commands, as desribed below, are included mostly for the sake of completeness.
A word upfront: the package already includes callbacks for tracing 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 in-memory database, perform custom-cleanups and various other tasks.
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 deffering 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 an multithreading Tcl application went down more than three times and thread startup time was reduced for about 50 times. Note that your mileage may vary. Other benefits include much finer control about what (and when) gets replicated from the master to other Tcl thread/interpreters.
thread, tpool, tsv
command tracing, introspection
2.8 | Tcl Threading |