More docs and other polish for release.
[p5sagit/Devel-Size.git] / lib / Devel / SizeMe.pm
CommitLineData
0e977dbc 1package Devel::SizeMe;
2
eda23e24 3# As a handy convenience, make perl -d:SizeMe automatically call heap_size
4# in an END block, and also set some $^P flags to get more detail.
5my $do_size_at_end; # set true below for "perl -d:SizeMe ..."
6BEGIN {
7 if ($^P) {
8 warn "Note: Devel::SizeMe currently disables perl debugger mode\n";
9 warn scalar keys %INC;
10 # default $^P set by "perl -d" is 0x73f
11 $^P = 0x10 # Keep info about source lines on which a sub is defined
12 | 0x100 # Provide informative "file" names for evals
13 | 0x200 # Provide informative names to anonymous subroutines;
14 ;
15 $do_size_at_end = 1;
16 }
0e977dbc 17}
18
eda23e24 19use strict;
20use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS $warn $dangle);
21
22require 5.005;
23require Exporter;
24require XSLoader;
25
26$VERSION = '0.02';
27@ISA = qw(Exporter);
28
29@EXPORT_OK = qw(size total_size perl_size heap_size);
30%EXPORT_TAGS = ( 'all' => \@EXPORT_OK ); # for use Devel::SizeMe ':all';
31
32$warn = 1;
33$dangle = 0; ## Set true to enable warnings about dangling pointers
34
12ae883b 35if (not defined $ENV{SIZEME}) {
36 $ENV{SIZEME} = "| sizeme_store.pl --db=sizeme.db";
37 warn qq{SIZEME env var not set, defaulting to "$ENV{SIZEME}"\n};
38}
eda23e24 39
40XSLoader::load( __PACKAGE__);
41
0e977dbc 42END {
12ae883b 43 Devel::SizeMe::perl_size() if $do_size_at_end; # heap_size()
0e977dbc 44}
45
461;
eda23e24 47__END__
48
49=pod
50
12ae883b 51Devel::SizeMe - Extension for extracting detailed memory usage information
eda23e24 52
53=head1 SYNOPSIS
54
12ae883b 55Manual usage:
56
57 use Devel::SizeMe qw(total_size perl_size);
eda23e24 58
eda23e24 59 my $total_size = total_size( $ref_to_data );
60
12ae883b 61 my $perl_size = perl_size();
62
63Quick automatic usage:
64
65 perl -d:SizeMe ...
66
eda23e24 67=head1 DESCRIPTION
68
12ae883b 69NOTE: This is all rather alpha and anything may change.
70
71The functions traverse memory structures and return the total memory size in
72bytes. See L<Devel::Size> for more information.
eda23e24 73
12ae883b 74If the C<SIZEME> env var is set then the functions also stream out detailed
75information about the individual data structures. This data can be written to a
76file or piped to a program for further processing.
eda23e24 77
78If SIZEME env var is set to an empty string then all the *_size functions
79dump a textual representation of the memory data to stderr.
80
81If SIZEME env var is set to a string that starts with "|" then the
82remainder of the string is taken to be a command name and popen() is used to
83start the command and the raw memory data is piped to it.
12ae883b 84See L<sizeme_store.pl>.
eda23e24 85
86If SIZEME env var is set to anything else it is treated as the name of a
87file the raw memory data should be written to.
88
89The sizeme_store.pl script can be used to process the raw memory data.
90Typically run via the SIZEME env var. For example:
91
92 export SIZEME='|./sizeme_store.pl --text'
93 export SIZEME='|./sizeme_store.pl --dot=sizeme.dot'
94 export SIZEME='|./sizeme_store.pl --db=sizeme.db'
95
96The --text output is similar to the textual representation output by the module
97when the SIZEME env var is set to an empty string.
98
99The --dot output is suitable for feeding to Graphviz.
100
101The --db output is a SQLite database. (Very subject to change.)
102
103Example usage:
104
105 SIZEME='|sizeme_store.pl --db=sizeme.db' perl -MDevel::SizeMe=:all -e 'total_size(sub { })'
106
107The sizeme_graph.pl script is a Mojolicious::Lite application that serves data to
108an interactive treemap visualization of the memory use. It can be run as:
109
110 sizeme_graph.pl daemon
111
112and then open http://127.0.0.1:3000
113
114Please report bugs to:
115
116 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Devel-SizeMe
117
12ae883b 118=head2 Automatic Mode
119
120If loaded using the perl C<-d> option (i.e., C<perl -d:SizeMe ...>)
121then perl memory usage data will be written to a C<sizeme.db> file in the
122current directory when the script ends.
123
eda23e24 124=head1 COPYRIGHT
125
126Copyright (C) 2005 Dan Sugalski,
127Copyright (C) 2007-2008 Tels,
128Copyright (C) 2008 BrowserUK,
129Copyright (C) 2011-2012 Nicholas Clark,
130Copyright (C) 2012 Tim Bunce.
131
132This module is free software; you can redistribute it and/or modify it
133under the same terms as Perl v5.8.8.
134
135=head1 SEE ALSO
136
137perl(1), L<Devel::Size>.
138
139=cut