Devel::Symdump allows us to access Perl's symbol table. This package is object oriented. To instantiate an object, you should provide the name of the package to traverse. If no package is provided as an argument, the main package is used. If the object is created with new( ), Devel::Symdump analyzes only the packages that are given as arguments; if rnew( ) is used, nested modules are analyzed recursively.
Once the object is instantiated, the methods packages( ), scalars( ), arrays( ), hashes( ), functions( ), ios( ), and unknowns( ) can be used. Each method returns an array of fully qualified symbols of the specified type in all packages that are held within a Devel::Symdump object, but without the leading "$", "@", or "%". In a scalar context, they will return the number of such symbols. Unknown symbols are usually either formats or variables that don't yet have defined values.
require Devel::Symdump; @packs = qw(Devel::Symdump); $obj = Devel::Symdump->new(@packs); print join "\n", $obj->scalars; Devel::Symdump::rnew Devel::Symdump::inh_tree Devel::Symdump::_partdump Devel::Symdump::DESTROY ...more symbols stripped
You may find this package useful to see what symbols are defined, traverse trees of symbols from inherited packages, and more. See the package's manpage for more information.
Apache::Symdump uses Devel::Symdump to record snapshots of the Perl symbol table in ServerRoot/logs/symdump.$$.$n. Here $$ is the process ID and $n is incremented each time the handler is run.
To enable this module, add the following to httpd.conf:
This module is useful for watching the growth of the processes and hopefully, by taking steps against the growth, reducing it. One of the reasons for process growth is the definition of new symbols. You can use the diff utility to compare snapshots and get an idea of what might be making a process grow. For example:
panic% diff -u symdump.1892.0 symdump.1892.1
where 1892 is PID. Normally, new symbols come from modules or scripts that were not preloaded, the Perl method cache, and so on. Let's write a simple script that uses DB_File, which wasn't preloaded (see Example 21-13).
use strict; require DB_File; my $r = shift; $r->send_http_header("text/plain"); $r->print("Hello $$\n");
If we issue a few requests and then compare two consecutive request dumps for the same process, nothing happens. That's because the module is loaded on the first request, and therefore from now on the symbol table will be the same. So in order to help Apache::Symdump to help us detect the load, we will require the module only on the second reload (see Example 21-14).
use strict; use vars qw($loaded); require DB_File if defined $loaded; $loaded = 1; my $r = shift; $r->send_http_header("text/plain"); $r->print("Hello $$\n");
Running the diff:
panic% diff symdump.9909.1 symdump.9909.2 |wc -l 301
reveals that there were 301 symbols defined, mostly from the DB_File and Fcntl packages. We can also see what new files were loaded, by applying diff on the incdump.$$.$n files, which dump the contents of %INC after each request:
panic% diff incdump.9909.1 incdump.9909.2 1a2 > /usr/lib/perl5/5.6.1/i386-linux/auto/DB_File/autosplit.ix = /usr/lib/perl5/5.6.1/i386-linux/auto/DB_File/autosplit.ix 21a23 > DB_File.pm = /usr/lib/perl5/5.6.1/i386-linux/DB_File.pm
Remember that Apache::Symdump does not clean up its snapshot files, so you have to do it yourself:
panic% rm logs/symdump.* logs/incdump.*
Apache::Status also uses Devel::Symdump to allow you to inspect symbol tables through your browser.
Eric Cholet (Logilune) and
Stas Bekman (StasoSphere & Free Books).