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