📄 perldebguts.pod
字号:
The most significant information in the output is about the particular I<node>of the compiled regex that is currently being tested against the target string.The format of these lines isC< >I<STRING-OFFSET> <I<PRE-STRING>> <I<POST-STRING>> |I<ID>: I<TYPE>The I<TYPE> info is indented with respect to the backtracking level.Other incidental information appears interspersed within.=head1 Debugging Perl memory usagePerl is a profligate wastrel when it comes to memory use. Thereis a saying that to estimate memory usage of Perl, assume a reasonablealgorithm for memory allocation, multiply that estimate by 10, andwhile you still may miss the mark, at least you won't be quite soastonished. This is not absolutely true, but may provide a goodgrasp of what happens.Assume that an integer cannot take less than 20 bytes of memory, afloat cannot take less than 24 bytes, a string cannot take lessthan 32 bytes (all these examples assume 32-bit architectures, theresult are quite a bit worse on 64-bit architectures). If a variableis accessed in two of three different ways (which require an integer,a float, or a string), the memory footprint may increase yet another20 bytes. A sloppy malloc(3) implementation can inflate thesenumbers dramatically.On the opposite end of the scale, a declaration like sub foo;may take up to 500 bytes of memory, depending on which release of Perlyou're running.Anecdotal estimates of source-to-compiled code bloat suggest aneightfold increase. This means that the compiled form of reasonable(normally commented, properly indented etc.) code will takeabout eight times more space in memory than the code tookon disk.There are two Perl-specific ways to analyze memory usage:$ENV{PERL_DEBUG_MSTATS} and B<-DL> command-line switch. The firstis available only if Perl is compiled with Perl's malloc(); thesecond only if Perl was built with C<-DDEBUGGING>. See theinstructions for how to do this in the F<INSTALL> podpage at the top level of the Perl source tree.=head2 Using C<$ENV{PERL_DEBUG_MSTATS}>If your perl is using Perl's malloc() and was compiled with thenecessary switches (this is the default), then it will print memoryusage statistics after compiling your code when C<< $ENV{PERL_DEBUG_MSTATS}> 1 >>, and before termination of the program when C<<$ENV{PERL_DEBUG_MSTATS} >= 1 >>. The report format is similar tothe following example: $ PERL_DEBUG_MSTATS=2 perl -e "require Carp" Memory allocation statistics after compilation: (buckets 4(4)..8188(8192) 14216 free: 130 117 28 7 9 0 2 2 1 0 0 437 61 36 0 5 60924 used: 125 137 161 55 7 8 6 16 2 0 1 74 109 304 84 20 Total sbrk(): 77824/21:119. Odd ends: pad+heads+chain+tail: 0+636+0+2048. Memory allocation statistics after execution: (buckets 4(4)..8188(8192) 30888 free: 245 78 85 13 6 2 1 3 2 0 1 315 162 39 42 11 175816 used: 265 176 1112 111 26 22 11 27 2 1 1 196 178 1066 798 39 Total sbrk(): 215040/47:145. Odd ends: pad+heads+chain+tail: 0+2192+0+6144.It is possible to ask for such a statistic at arbitrary points inyour execution using the mstat() function out of the standardDevel::Peek module.Here is some explanation of that format:=over 4=item C<buckets SMALLEST(APPROX)..GREATEST(APPROX)>Perl's malloc() uses bucketed allocations. Every request is roundedup to the closest bucket size available, and a bucket is taken fromthe pool of buckets of that size.The line above describes the limits of buckets currently in use.Each bucket has two sizes: memory footprint and the maximal sizeof user data that can fit into this bucket. Suppose in the aboveexample that the smallest bucket were size 4. The biggest bucketwould have usable size 8188, and the memory footprint would be 8192.In a Perl built for debugging, some buckets may have negative usablesize. This means that these buckets cannot (and will not) be used.For larger buckets, the memory footprint may be one page greaterthan a power of 2. If so, case the corresponding power of two isprinted in the C<APPROX> field above.=item Free/UsedThe 1 or 2 rows of numbers following that correspond to the numberof buckets of each size between C<SMALLEST> and C<GREATEST>. Inthe first row, the sizes (memory footprints) of buckets are powersof two--or possibly one page greater. In the second row, if present,the memory footprints of the buckets are between the memory footprintsof two buckets "above".For example, suppose under the previous example, the memory footprintswere free: 8 16 32 64 128 256 512 1024 2048 4096 8192 4 12 24 48 80With non-C<DEBUGGING> perl, the buckets starting from C<128> havea 4-byte overhead, and thus a 8192-long bucket may take up to8188-byte allocations.=item C<Total sbrk(): SBRKed/SBRKs:CONTINUOUS>The first two fields give the total amount of memory perl sbrk(2)ed(ess-broken? :-) and number of sbrk(2)s used. The third number iswhat perl thinks about continuity of returned chunks. So long asthis number is positive, malloc() will assume that it is probablethat sbrk(2) will provide continuous memory.Memory allocated by external libraries is not counted.=item C<pad: 0>The amount of sbrk(2)ed memory needed to keep buckets aligned.=item C<heads: 2192>Although memory overhead of bigger buckets is kept inside the bucket, forsmaller buckets, it is kept in separate areas. This field gives thetotal size of these areas.=item C<chain: 0>malloc() may want to subdivide a bigger bucket into smaller buckets.If only a part of the deceased bucket is left unsubdivided, the restis kept as an element of a linked list. This field gives the totalsize of these chunks.=item C<tail: 6144>To minimize the number of sbrk(2)s, malloc() asks for more memory. Thisfield gives the size of the yet unused part, which is sbrk(2)ed, butnever touched.=back=head2 Example of using B<-DL> switchBelow we show how to analyse memory usage by do 'lib/auto/POSIX/autosplit.ix';The file in question contains a header and 146 lines similar to sub getcwd;B<WARNING>: The discussion below supposes 32-bit architecture. In newer releases of Perl, memory usage of the constructs discussedhere is greatly improved, but the story discussed below is a real-lifestory. This story is mercilessly terse, and assumes rather more than cursoryknowledge of Perl internals. Type space to continue, `q' to quit. (Actually, you just want to skip to the next section.)Here is the itemized list of Perl allocations performed during parsingof this file: !!! "after" at test.pl line 3. Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+ 0 02 13752 . . . . 294 . . . . . . . . . . 4 0 54 5545 . . 8 124 16 . . . 1 1 . . . . . 3 5 05 32 . . . . . . . 1 . . . . . . . . 6 02 7152 . . . . . . . . . . 149 . . . . . 7 02 3600 . . . . . 150 . . . . . . . . . . 7 03 64 . -1 . 1 . . 2 . . . . . . . . . 7 04 7056 . . . . . . . . . . . . . . . 7 7 17 38404 . . . . . . . 1 . . 442 149 . . 147 . 9 03 2078 17 249 32 . . . . 2 . . . . . . . .To see this list, insert two C<warn('!...')> statements around the call: warn('!'); do 'lib/auto/POSIX/autosplit.ix'; warn('!!! "after"');and run it with Perl's B<-DL> option. The first warn() will printmemory allocation info before parsing the file and will memorizethe statistics at this point (we ignore what it prints). The secondwarn() prints increments with respect to these memorized data. Thisis the printout shown above.Different I<Id>s on the left correspond to different subsystems ofthe perl interpreter. They are just the first argument given tothe perl memory allocation API named New(). To find what C<9 03>means, just B<grep> the perl source for C<903>. You'll find it inF<util.c>, function savepvn(). (I know, you wonder why we told youto B<grep> and then gave away the answer. That's because greppingthe source is good for the soul.) This function is used to storea copy of an existing chunk of memory. Using a C debugger, one cansee that the function was called either directly from gv_init() orvia sv_magic(), and that gv_init() is called from gv_fetchpv()--whichwas itself called from newSUB(). Please stop to catch your breath now.B<NOTE>: To reach this point in the debugger and skip the calls tosavepvn() during the compilation of the main program, you shouldset a C breakpointin Perl_warn(), continue until this point is reached, and I<then> seta C breakpoint in Perl_savepvn(). Note that you may need to skip ahandful of Perl_savepvn() calls that do not correspond to mass productionof CVs (there are more C<903> allocations than 146 similar lines ofF<lib/auto/POSIX/autosplit.ix>). Note also that C<Perl_> prefixes areadded by macroization code in perl header files to avoid conflictswith external libraries.Anyway, we see that C<903> ids correspond to creation of globs, twiceper glob - for glob name, and glob stringification magic.Here are explanations for other I<Id>s above: =over 4=item C<717> Creates bigger C<XPV*> structures. In the case above, itcreates 3 C<AV>s per subroutine, one for a list of lexical variablenames, one for a scratchpad (which contains lexical variables andC<targets>), and one for the array of scratchpads needed forrecursion. It also creates a C<GV> and a C<CV> per subroutine, all called fromstart_subparse().=item C<002>Creates a C array corresponding to the C<AV> of scratchpads and thescratchpad itself. The first fake entry of this scratchpad iscreated though the subroutine itself is not defined yet.It also creates C arrays to keep data for the stash. This is one HV,but it grows; thus, there are 4 big allocations: the big chunks are notfreed, but are kept as additional arenas for C<SV> allocations.=item C<054>Creates a C<HEK> for the name of the glob for the subroutine. Thisname is a key in a I<stash>.Big allocations with this I<Id> correspond to allocations of newarenas to keep C<HE>.=item C<602>Creates a C<GP> for the glob for the subroutine.=item C<702>Creates the C<MAGIC> for the glob for the subroutine.=item C<704>Creates I<arenas> which keep SVs.=back=head2 B<-DL> detailsIf Perl is run with B<-DL> option, then warn()s that start with `!'behave specially. They print a list of I<categories> of memoryallocations, and statistics of allocations of different sizes forthese categories.If warn() string starts with=over 4=item C<!!!> print changed categories only, print the differences in counts of allocations.=item C<!!> print grown categories only; print the absolute values of counts, and totals.=item C<!>print nonempty categories, print the absolute values of counts and totals.=back=head2 Limitations of B<-DL> statisticsIf an extension or external library does not use the Perl API toallocate memory, such allocations are not counted.=head1 SEE ALSOL<perldebug>,L<perlguts>,L<perlrun>L<re>,andL<Devel::Dprof>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -