perldebguts.html

来自「perl教程」· HTML 代码 · 共 928 行 · 第 1/4 页

HTML
928 行
字号
        0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 18[1] 0[0] 19[1] 20[0]</pre>
<p>The first line here indicates that the offset/length table contains 45
entries.  Each entry is a pair of integers, denoted by <code>offset[length]</code>.
Entries are numbered starting with 1, so entry #1 here is <code>1[4]</code> and
entry #12 is <code>5[1]</code>.  <code>1[4]</code> indicates that the node labeled <code>1:</code>
(the <code>1: ANYOF[bc]</code>) begins at character position 1 in the
pre-compiled form of the regex, and has a length of 4 characters.
<code>5[1]</code> in position 12 
indicates that the node labeled <code>12:</code>
(the <code>12: EXACT &lt;d&gt;</code>) begins at character position 5 in the
pre-compiled form of the regex, and has a length of 1 character.
<code>12[1]</code> in position 14 
indicates that the node labeled <code>14:</code>
(the <code>14: CURLYX[0] {1,32767}</code>) begins at character position 12 in the
pre-compiled form of the regex, and has a length of 1 character---that
is, it corresponds to the <code>+</code> symbol in the precompiled regex.</p>
<p><code>0[0]</code> items indicate that there is no corresponding node.</p>
<p>
</p>
<h2><a name="runtime_output">Run-time output</a></h2>
<p>First of all, when doing a match, one may get no run-time output even
if debugging is enabled.  This means that the regex engine was never
entered and that all of the job was therefore done by the optimizer.</p>
<p>If the regex engine was entered, the output may look like this:</p>
<pre>
  Matching `[bc]d(ef*g)+h[ij]k$' against `abcdefg__gh__'
    Setting an EVAL scope, savestack=3
     2 &lt;ab&gt; &lt;cdefg__gh_&gt;    |  1: ANYOF
     3 &lt;abc&gt; &lt;defg__gh_&gt;    | 11: EXACT &lt;d&gt;
     4 &lt;abcd&gt; &lt;efg__gh_&gt;    | 13: CURLYX {1,32767}
     4 &lt;abcd&gt; &lt;efg__gh_&gt;    | 26:   WHILEM
                                0 out of 1..32767  cc=effff31c
     4 &lt;abcd&gt; &lt;efg__gh_&gt;    | 15:     OPEN1
     4 &lt;abcd&gt; &lt;efg__gh_&gt;    | 17:     EXACT &lt;e&gt;
     5 &lt;abcde&gt; &lt;fg__gh_&gt;    | 19:     STAR
                             EXACT &lt;f&gt; can match 1 times out of 32767...
    Setting an EVAL scope, savestack=3
     6 &lt;bcdef&gt; &lt;g__gh__&gt;    | 22:       EXACT &lt;g&gt;
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 24:       CLOSE1
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 26:       WHILEM
                                    1 out of 1..32767  cc=effff31c
    Setting an EVAL scope, savestack=12
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 15:         OPEN1
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 17:         EXACT &lt;e&gt;
       restoring \1 to 4(4)..7
                                    failed, try continuation...
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 27:         NOTHING
     7 &lt;bcdefg&gt; &lt;__gh__&gt;    | 28:         EXACT &lt;h&gt;
                                    failed...
                                failed...</pre>
<p>The most significant information in the output is about the particular <em>node</em>
of the compiled regex that is currently being tested against the target string.
The format of these lines is</p>
<p><code>    </code><em>STRING-OFFSET</em> &lt;<em>PRE-STRING</em>&gt; &lt;<em>POST-STRING</em>&gt;   |<em>ID</em>:  <em>TYPE</em></p>
<p>The <em>TYPE</em> info is indented with respect to the backtracking level.
Other incidental information appears interspersed within.</p>
<p>
</p>
<hr />
<h1><a name="debugging_perl_memory_usage">Debugging Perl memory usage</a></h1>
<p>Perl is a profligate wastrel when it comes to memory use.  There
is a saying that to estimate memory usage of Perl, assume a reasonable
algorithm for memory allocation, multiply that estimate by 10, and
while you still may miss the mark, at least you won't be quite so
astonished.  This is not absolutely true, but may provide a good
grasp of what happens.</p>
<p>Assume that an integer cannot take less than 20 bytes of memory, a
float cannot take less than 24 bytes, a string cannot take less
than 32 bytes (all these examples assume 32-bit architectures, the
result are quite a bit worse on 64-bit architectures).  If a variable
is accessed in two of three different ways (which require an integer,
a float, or a string), the memory footprint may increase yet another
20 bytes.  A sloppy <code>malloc(3)</code> implementation can inflate these
numbers dramatically.</p>
<p>On the opposite end of the scale, a declaration like</p>
<pre>
  <span class="keyword">sub</span><span class="variable"> foo</span><span class="operator">;</span>
</pre>
<p>may take up to 500 bytes of memory, depending on which release of Perl
you're running.</p>
<p>Anecdotal estimates of source-to-compiled code bloat suggest an
eightfold increase.  This means that the compiled form of reasonable
(normally commented, properly indented etc.) code will take
about eight times more space in memory than the code took
on disk.</p>
<p>The <strong>-DL</strong> command-line switch is obsolete since circa Perl 5.6.0
(it was available only if Perl was built with <code>-DDEBUGGING</code>).
The switch was used to track Perl's memory allocations and possible
memory leaks.  These days the use of malloc debugging tools like
<em>Purify</em> or <em>valgrind</em> is suggested instead.</p>
<p>One way to find out how much memory is being used by Perl data
structures is to install the Devel::Size module from CPAN: it gives
you the minimum number of bytes required to store a particular data
structure.  Please be mindful of the difference between the <a href="../../lib/Pod/perlfunc.html#item_size"><code>size()</code></a>
and total_size().</p>
<p>If Perl has been compiled using Perl's malloc you can analyze Perl
memory usage by setting the $ENV{PERL_DEBUG_MSTATS}.</p>
<p>
</p>
<h2><a name="using__env_perl_debug_mstats_">Using <code>$ENV{PERL_DEBUG_MSTATS}</code></a></h2>
<p>If your perl is using Perl's <code>malloc()</code> and was compiled with the
necessary switches (this is the default), then it will print memory
usage statistics after compiling your code when <code>$ENV{PERL_DEBUG_MSTATS}
&gt; 1</code>, and before termination of the program when <code>&lt;
$ENV{PERL_DEBUG_MSTATS} </code>= 1 &gt;&gt;.  The report format is similar to
the following example:</p>
<pre>
  $ PERL_DEBUG_MSTATS=2 perl -e &quot;require Carp&quot;
  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.</pre>
<p>It is possible to ask for such a statistic at arbitrary points in
your execution using the <code>mstat()</code> function out of the standard
Devel::Peek module.</p>
<p>Here is some explanation of that format:</p>
<dl>
<dt><strong><a name="item_smallest"><code>buckets SMALLEST(APPROX)..GREATEST(APPROX)</code></a></strong>

<dd>
<p>Perl's <code>malloc()</code> uses bucketed allocations.  Every request is rounded
up to the closest bucket size available, and a bucket is taken from
the pool of buckets of that size.</p>
</dd>
<dd>
<p>The line above describes the limits of buckets currently in use.
Each bucket has two sizes: memory footprint and the maximal size
of user data that can fit into this bucket.  Suppose in the above
example that the smallest bucket were size 4.  The biggest bucket
would have usable size 8188, and the memory footprint would be 8192.</p>
</dd>
<dd>
<p>In a Perl built for debugging, some buckets may have negative usable
size.  This means that these buckets cannot (and will not) be used.
For larger buckets, the memory footprint may be one page greater
than a power of 2.  If so, case the corresponding power of two is
printed in the <code>APPROX</code> field above.</p>
</dd>
</li>
<dt><strong><a name="item_free_2fused">Free/Used</a></strong>

<dd>
<p>The 1 or 2 rows of numbers following that correspond to the number
of buckets of each size between <a href="#item_smallest"><code>SMALLEST</code></a> and <code>GREATEST</code>.  In
the first row, the sizes (memory footprints) of buckets are powers
of two--or possibly one page greater.  In the second row, if present,
the memory footprints of the buckets are between the memory footprints
of two buckets &quot;above&quot;.</p>
</dd>
<dd>
<p>For example, suppose under the previous example, the memory footprints
were</p>
</dd>
<dd>
<pre>
     free:    8     16    32    64    128  256 512 1024 2048 4096 8192
           4     12    24    48    80</pre>
</dd>
<dd>
<p>With non-<code>DEBUGGING</code> perl, the buckets starting from <code>128</code> have
a 4-byte overhead, and thus an 8192-long bucket may take up to
8188-byte allocations.</p>
</dd>
</li>
<dt><strong><a name="item_sbrk"><code>Total sbrk(): SBRKed/SBRKs:CONTINUOUS</code></a></strong>

<dd>
<p>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 is
what perl thinks about continuity of returned chunks.  So long as
this number is positive, <code>malloc()</code> will assume that it is probable
that <a href="#item_sbrk"><code>sbrk(2)</code></a> will provide continuous memory.</p>
</dd>
<dd>
<p>Memory allocated by external libraries is not counted.</p>
</dd>
</li>
<dt><strong><a name="item_pad_3a_0"><code>pad: 0</code></a></strong>

<dd>
<p>The amount of sbrk(2)ed memory needed to keep buckets aligned.</p>
</dd>
</li>
<dt><strong><a name="item_heads_3a_2192"><code>heads: 2192</code></a></strong>

<dd>
<p>Although memory overhead of bigger buckets is kept inside the bucket, for
smaller buckets, it is kept in separate areas.  This field gives the
total size of these areas.</p>
</dd>
</li>
<dt><strong><a name="item_chain_3a_0"><code>chain: 0</code></a></strong>

<dd>
<p><code>malloc()</code> may want to subdivide a bigger bucket into smaller buckets.
If only a part of the deceased bucket is left unsubdivided, the rest
is kept as an element of a linked list.  This field gives the total
size of these chunks.</p>
</dd>
</li>
<dt><strong><a name="item_tail_3a_6144"><code>tail: 6144</code></a></strong>

<dd>
<p>To minimize the number of sbrk(2)s, <code>malloc()</code> asks for more memory.  This
field gives the size of the yet unused part, which is sbrk(2)ed, but
never touched.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Pod/perldebug.html">the perldebug manpage</a>,
<a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>,
<a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>
<a href="../../lib/re.html">the re manpage</a>,
and
<a href="../../lib/Devel/DProf.html">the Devel::DProf manpage</a>.</p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?