Penguin
Blame: perldebguts(1)
EditPageHistoryDiffInfoLikePages
Annotated edit history of perldebguts(1) version 1, including all changes. View license author blame.
Rev Author # Line
1 perry 1 PERLDEBGUTS
2 !!!PERLDEBGUTS
3 NAME
4 DESCRIPTION
5 Debugger Internals
6 Frame Listing Output Examples
7 Debugging regular expressions
8 Debugging Perl memory usage
9 SEE ALSO
10 ----
11 !!NAME
12
13
14 perldebguts - Guts of Perl debugging
15 !!DESCRIPTION
16
17
18 This is not the perldebug(1) manpage, which tells you
19 how to use the debugger. This manpage describes low-level
20 details ranging between difficult and impossible for anyone
21 who isn't incredibly intimate with Perl's guts to
22 understand. Caveat lector.
23 !!Debugger Internals
24
25
26 Perl has special debugging hooks at compile-time and
27 run-time used to create debugging environments. These hooks
28 are not to be confused with the ''perl -Dxxx'' command
29 described in perlrun, which is usable only if a special Perl
30 is built per the instructions in the
31 ''INSTALL'' podpage in the Perl source
32 tree.
33
34
35 For example, whenever you call Perl's built-in
36 caller function from the package DB
37 , the arguments that the corresponding stack frame was
38 called with are copied to the @DB::args array. The
39 general mechanisms is enabled by calling Perl with the
40 __-d__ switch, the following additional features are
41 enabled (cf. ``$^P'' in perlvar):
42
43
44 Perl inserts the contents of $ENV{PERL5DB} (or
45 BEGIN {require 'perl5db.pl'} if not present) before
46 the first line of your program.
47
48
49 Each array @{ holds the
50 lines of $filename for a file compiled by Perl. The
51 same for evaled strings that contain subroutines,
52 or which are currently being executed. The
53 $filename for evaled strings looks like
54 (eval 34). Code assertions in regexes look like
55 (re_eval 19).
56
57
58 Values in this array are magical in numeric context: they
59 compare equal to zero only if the line is not
60 breakable.
61
62
63 Each hash %{ contains
64 breakpoints and actions keyed by line number. Individual
65 entries (as opposed to the whole hash) are settable. Perl
66 only cares about Boolean true here, although the values used
67 by ''perl5db.pl'' have the form
68 .
69
70
71 The same holds for evaluated strings that contain
72 subroutines, or which are currently being executed. The
73 $filename for evaled strings looks like
74 (eval 34) or (re_eval 19).
75
76
77 Each scalar ${ contains
78 . This is also the case
79 for evaluated strings that contain subroutines, or which are
80 currently being executed. The $filename for
81 evaled strings looks like (eval 34) or
82 (re_eval 19).
83
84
85 After each required file is compiled, but before it
86 is executed,
87 DB::postponed(*{ is
88 called if the subroutine DB::postponed exists.
89 Here, the $filename is the expanded name of the
90 required file, as found in the values of
91 %INC.
92
93
94 After each subroutine subname is compiled, the
95 existence of $DB::postponed{subname} is checked. If
96 this key exists, DB::postponed(subname) is called
97 if the DB::postponed subroutine also
98 exists.
99
100
101 A hash %DB::sub is maintained, whose keys are
102 subroutine names and whose values have the form
103 filename:startline-endline. filename has
104 the form (eval 34) for subroutines defined inside
105 evals, or (re_eval 19) for those within
106 regex code assertions.
107
108
109 When the execution of your program reaches a point that can
110 hold a breakpoint, the DB::DB() subroutine is
111 called any of the variables $DB::trace,
112 $DB::single, or $DB::signal is true. These
113 variables are not localizable. This feature is
114 disabled when executing inside DB::DB(), including
115 functions called from it unless $^D
116 is true.
117
118
119 When execution of the program reaches a subroutine call, a
120 call to (''args'') is made instead,
121 with $DB::sub holding the name of the called
122 subroutine. This doesn't happen if the subroutine was
123 compiled in the DB package.)
124
125
126 Note that if needs external data for
127 it to work, no subroutine call is possible until this is
128 done. For the standard debugger, the $DB::deep
129 variable (how many levels of recursion deep into the
130 debugger you can go before a mandatory break) gives an
131 example of such a dependency.
132
133
134 __Writing Your Own Debugger__
135
136
137 The minimal working debugger consists of one
138 line
139
140
141 sub DB::DB {}
142 which is quite handy as contents of PERL5DB environment variable:
143
144
145 $ PERL5DB=
146 Another brief debugger, slightly more useful, could be created with only the line:
147
148
149 sub DB::DB {print ++$i; scalar
150 This debugger would print the sequential number of encountered statement, and would wait for you to hit a newline before continuing.
151
152
153 The following debugger is quite functional:
154
155
156 {
157 package DB;
158 sub DB {}
159 sub sub {print ++$i,
160 It prints the sequential number of subroutine call and the name of the called subroutine. Note that should be compiled into the package DB.
161
162
163 At the start, the debugger reads your rc file
164 (''./.perldb'' or ''~/.perldb'' under Unix), which can
165 set important options. This file may define a subroutine
166 to be executed after the debugger is
167 initialized.
168
169
170 After the rc file is read, the debugger reads the
171 PERLDB_OPTS environment variable and parses
172 this as the remainder of a O ... line as one might
173 enter at the debugger prompt.
174
175
176 The debugger also maintains magical internal variables, such
177 as @DB::dbline, %DB::dbline, which are
178 aliases for @{
179 . Here
180 current_file is the currently selected file, either
181 explicitly chosen with the debugger's f command, or
182 implicitly by flow of execution.
183
184
185 Some functions are provided to simplify customization. See
186 ``Options'' in perldebug for description of options parsed
187 by DB::parse_options(string). The function
188 DB::dump_trace(skip[[, count]) skips the specified
189 number of frames and returns a list containing information
190 about the calling frames (all of them, if count is
191 missing). Each entry is reference to a hash with keys
192 context (either ., $, or
193 @), sub (subroutine name, or info about
194 eval), args (undef or a reference
195 to an array), file, and line.
196
197
198 The function DB::print_trace(FH, skip[[, count[[,
199 short]]) prints formatted info about caller frames. The
200 last two functions may be convenient as arguments to
201 , commands.
202
203
204 Note that any variables and functions that are not
205 documented in this manpages (or in perldebug) are considered
206 for internal use only, and as such are subject to change
207 without notice.
208 !!Frame Listing Output Examples
209
210
211 The frame option can be used to control the output
212 of frame information. For example, contrast this expression
213 trace:
214
215
216 $ perl -de 42
217 Stack dump during die enabled outside of evals.
218 Loading DB routines from perl5db.pl patch level 0.94
219 Emacs support available.
220 Enter h or `h h' for help.
221 main::(-e:1): 0
222 DB
223 DB
224 DB
225 with this one, once the Option frame=2 has been set:
226
227
228 DB
229 By way of demonstration, we present below a laborious listing resulting from setting your PERLDB_OPTS environment variable to the value f=n N, and running ''perl -d -V'' from the command line. Examples use various values of n are shown to give you a feel for the difference between settings. Long those it may be, this is not a complete listing, but only excerpts.
230
231
232 1
233
234
235 entering main::BEGIN
236 entering Config::BEGIN
237 Package lib/Exporter.pm.
238 Package lib/Carp.pm.
239 Package lib/Config.pm.
240 entering Config::TIEHASH
241 entering Exporter::import
242 entering Exporter::export
243 entering Config::myconfig
244 entering Config::FETCH
245 entering Config::FETCH
246 entering Config::FETCH
247 entering Config::FETCH
248
249
250 2
251
252
253 entering main::BEGIN
254 entering Config::BEGIN
255 Package lib/Exporter.pm.
256 Package lib/Carp.pm.
257 exited Config::BEGIN
258 Package lib/Config.pm.
259 entering Config::TIEHASH
260 exited Config::TIEHASH
261 entering Exporter::import
262 entering Exporter::export
263 exited Exporter::export
264 exited Exporter::import
265 exited main::BEGIN
266 entering Config::myconfig
267 entering Config::FETCH
268 exited Config::FETCH
269 entering Config::FETCH
270 exited Config::FETCH
271 entering Config::FETCH
272
273
274 4
275
276
277 in $=main::BEGIN() from /dev/null:0
278 in $=Config::BEGIN() from lib/Config.pm:2
279 Package lib/Exporter.pm.
280 Package lib/Carp.pm.
281 Package lib/Config.pm.
282 in $=Config::TIEHASH('Config') from lib/Config.pm:644
283 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
284 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from li
285 in @=Config::myconfig() from /dev/null:0
286 in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
287 in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
288 in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
289 in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
290 in $=Config::FETCH(ref(Config), 'osname') from lib/Config.pm:574
291 in $=Config::FETCH(ref(Config), 'osvers') from lib/Config.pm:574
292
293
294 6
295
296
297 in $=main::BEGIN() from /dev/null:0
298 in $=Config::BEGIN() from lib/Config.pm:2
299 Package lib/Exporter.pm.
300 Package lib/Carp.pm.
301 out $=Config::BEGIN() from lib/Config.pm:0
302 Package lib/Config.pm.
303 in $=Config::TIEHASH('Config') from lib/Config.pm:644
304 out $=Config::TIEHASH('Config') from lib/Config.pm:644
305 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
306 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
307 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
308 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
309 out $=main::BEGIN() from /dev/null:0
310 in @=Config::myconfig() from /dev/null:0
311 in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
312 out $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
313 in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
314 out $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
315 in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
316 out $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
317 in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
318
319
320 14
321
322
323 in $=main::BEGIN() from /dev/null:0
324 in $=Config::BEGIN() from lib/Config.pm:2
325 Package lib/Exporter.pm.
326 Package lib/Carp.pm.
327 out $=Config::BEGIN() from lib/Config.pm:0
328 Package lib/Config.pm.
329 in $=Config::TIEHASH('Config') from lib/Config.pm:644
330 out $=Config::TIEHASH('Config') from lib/Config.pm:644
331 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
332 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
333 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
334 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
335 out $=main::BEGIN() from /dev/null:0
336 in @=Config::myconfig() from /dev/null:0
337 in $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
338 out $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
339 in $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
340 out $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
341
342
343 30
344
345
346 in $=CODE(0x15eca4)() from /dev/null:0
347 in $=CODE(0x182528)() from lib/Config.pm:2
348 Package lib/Exporter.pm.
349 out $=CODE(0x182528)() from lib/Config.pm:0
350 scalar context return from CODE(0x182528): undef
351 Package lib/Config.pm.
352 in $=Config::TIEHASH('Config') from lib/Config.pm:628
353 out $=Config::TIEHASH('Config') from lib/Config.pm:628
354 scalar context return from Config::TIEHASH: empty hash
355 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
356 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
357 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
358 scalar context return from Exporter::export: ''
359 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
360 scalar context return from Exporter::import: ''
361
362
363 In all cases shown above, the line indentation shows the
364 call tree. If bit 2 of frame is set, a line is
365 printed on exit from a subroutine as well. If bit 4 is set,
366 the arguments are printed along with the caller info. If bit
367 8 is set, the arguments are printed even if they are tied or
368 references. If bit 16 is set, the return value is printed,
369 too.
370
371
372 When a package is compiled, a line like this
373
374
375 Package lib/Carp.pm.
376 is printed with proper indentation.
377 !!Debugging regular expressions
378
379
380 There are two ways to enable debugging output for regular
381 expressions.
382
383
384 If your perl is compiled with -DDEBUGGING, you may
385 use the __-Dr__ flag on the command line.
386
387
388 Otherwise, one can use re 'debug', which has
389 effects at compile time and run time. It is not lexically
390 scoped.
391
392
393 __Compile-time output__
394
395
396 The debugging output at compile time looks like
397 this:
398
399
400 compiling RE `[[bc]d(ef*g)+h[[ij]k$'
401 size 43 first at 1
402 1: ANYOF(11)
403 11: EXACT
404 The first line shows the pre-compiled form of the regex. The second shows the size of the compiled form (in arbitrary units, usually 4-byte words) and the label ''id'' of the first node that does a match.
405
406
407 The last line (split into two lines above) contains
408 optimizer information. In the example shown, the optimizer
409 found that the match should contain a substring de
410 at offset 1, plus substring gh at some offset
411 between 3 and infinity. Moreover, when checking for these
412 substrings (to abandon impossible matches quickly), Perl
413 will check for the substring gh before checking for
414 the substring de. The optimizer may also use the
415 knowledge that the match starts (at the first
416 ''id'') with a character class, and the match cannot be
417 shorter than 7 chars.
418
419
420 The fields of interest which may appear in the last line
421 are
422
423
424 anchored ''STRING'' at
425 ''POS''
426
427
428 floating ''STRING'' at
429 ''POS1 ..POS2''
430
431
432 See above.
433
434
435 matching floating/anchored
436
437
438 Which substring to check first.
439
440
441 minlen
442
443
444 The minimal length of the match.
445
446
447 stclass ''TYPE''
448
449
450 Type of first matching node.
451
452
453 noscan
454
455
456 Don't scan for the found substrings.
457
458
459 isall
460
461
462 Means that the optimizer info is all that the regular
463 expression contains, and thus one does not need to enter the
464 regex engine at all.
465
466
467 GPOS
468
469
470 Set if the pattern contains G.
471
472
473 plus
474
475
476 Set if the pattern starts with a repeated char (as in
477 x+y).
478
479
480 implicit
481
482
483 Set if the pattern starts with .*.
484
485
486 with eval
487
488
489 Set if the pattern contain eval-groups, such as (?{ code
490 }) and (??{ code }).
491
492
493 anchored(TYPE)
494
495
496 If the pattern may match only at a handful of places, (with
497 TYPE being BOL, MBOL, or
498 GPOS. See the table below.
499
500
501 If a substring is known to match at end-of-line only, it may
502 be followed by $, as in floating
503 `k'$.
504
505
506 The optimizer-specific info is used to avoid entering (a
507 slow) regex engine on strings that will not definitely
508 match. If isall flag is set, a call to the regex
509 engine may be avoided even when the optimizer found an
510 appropriate place for the match.
511
512
513 The rest of the output contains the list of ''nodes'' of
514 the compiled form of the regex. Each line has
515 format
516
517
518 ''id'': ''TYPE OPTIONAL-INFO''
519 (''next-id'')
520
521
522 __Types of nodes__
523
524
525 Here are the possible types, with short
526 descriptions:
527
528
529 # TYPE arg-description [[num-args] [[longjump-len] DESCRIPTION
530 # Exit points
531 END no End of program.
532 SUCCEED no Return from a subroutine, basically.
533 # Anchors:
534 BOL no Match
535 # [[Special] alternatives
536 ANY no Match any one character (except newline).
537 SANY no Match any one character.
538 ANYOF sv Match character in (or not in) this class.
539 ALNUM no Match any alphanumeric character
540 ALNUML no Match any alphanumeric char in locale
541 NALNUM no Match any non-alphanumeric character
542 NALNUML no Match any non-alphanumeric char in locale
543 SPACE no Match any whitespace character
544 SPACEL no Match any whitespace char in locale
545 NSPACE no Match any non-whitespace character
546 NSPACEL no Match any non-whitespace char in locale
547 DIGIT no Match any numeric character
548 NDIGIT no Match any non-numeric character
549 # BRANCH The set of branches constituting a single choice are hooked
550 # together with their
551 # BACK Normal
552 # Literals
553 EXACT sv Match this string (preceded by length).
554 EXACTF sv Match this string, folded (prec. by length).
555 EXACTFL sv Match this string, folded in locale (w/len).
556 # Do nothing
557 NOTHING no Match empty string.
558 # A variant of above which delimits a group, thus stops optimizations
559 TAIL no Match empty string. Can jump here from outside.
560 # STAR,PLUS '?', and complex '*' and '+', are implemented as circular
561 # BRANCH structures using BACK. Simple cases (one character
562 # per match) are implemented with STAR and PLUS for speed
563 # and to minimize recursive plunges.
564 #
565 STAR node Match this (simple) thing 0 or more times.
566 PLUS node Match this (simple) thing 1 or more times.
567 CURLY sv 2 Match this simple thing {n,m} times.
568 CURLYN no 2 Match next-after-this simple thing
569 # {n,m} times, set parens.
570 CURLYM no 2 Match this medium-complex thing {n,m} times.
571 CURLYX sv 2 Match this complex thing {n,m} times.
572 # This terminator creates a loop structure for CURLYX
573 WHILEM no Do curly processing and see if rest matches.
574 # OPEN,CLOSE,GROUPP ...are numbered at compile time.
575 OPEN num 1 Mark this point in input as start of #n.
576 CLOSE num 1 Analogous to OPEN.
577 REF num 1 Match some already matched string
578 REFF num 1 Match already matched string, folded
579 REFFL num 1 Match already matched string, folded in loc.
580 # grouping assertions
581 IFMATCH off 1 2 Succeeds if the following matches.
582 UNLESSM off 1 2 Fails if the following matches.
583 SUSPEND off 1 1
584 # Support for long regex
585 LONGJMP off 1 1 Jump far away.
586 BRANCHJ off 1 1 BRANCH with long offset.
587 # The heavy worker
588 EVAL evl 1 Execute some Perl code.
589 # Modifiers
590 MINMOD no Next operator is not greedy.
591 LOGICAL no Next opcode should set the flag only.
592 # This is not used yet
593 RENUM off 1 1 Group with independently numbered parens.
594 # This is not really a node, but an optimized away piece of a
595
596
597 __Run-time output__
598
599
600 First of all, when doing a match, one may get no run-time
601 output even if debugging is enabled. This means that the
602 regex engine was never entered and that all of the job was
603 therefore done by the optimizer.
604
605
606 If the regex engine was entered, the output may look like
607 this:
608
609
610 Matching `[[bc]d(ef*g)+h[[ij]k$' against `abcdefg__gh__'
611 Setting an EVAL scope, savestack=3
612 2
613 The most significant information in the output is about the particular ''node'' of the compiled regex that is currently being tested against the target string. The format of these lines is
614
615
616 ''STRING-OFFSET'' ''PRE-STRING''
617 ''POST-STRING''''ID'' :
618 ''TYPE''
619
620
621 The ''TYPE'' info is indented with respect
622 to the backtracking level. Other incidental information
623 appears interspersed within.
624 !!Debugging Perl memory usage
625
626
627 Perl is a profligate wastrel when it comes to memory use.
628 There is a saying that to estimate memory usage of Perl,
629 assume a reasonable algorithm for memory allocation,
630 multiply that estimate by 10, and while you still may miss
631 the mark, at least you won't be quite so astonished. This is
632 not absolutely true, but may provide a good grasp of what
633 happens.
634
635
636 Assume that an integer cannot take less than 20 bytes of
637 memory, a float cannot take less than 24 bytes, a string
638 cannot take less than 32 bytes (all these examples assume
639 32-bit architectures, the result are quite a bit worse on
640 64-bit architectures). If a variable is accessed in two of
641 three different ways (which require an integer, a float, or
642 a string), the memory footprint may increase yet another 20
643 bytes. A sloppy malloc(3) implementation can inflate
644 these numbers dramatically.
645
646
647 On the opposite end of the scale, a declaration
648 like
649
650
651 sub foo;
652 may take up to 500 bytes of memory, depending on which release of Perl you're running.
653
654
655 Anecdotal estimates of source-to-compiled code bloat suggest
656 an eightfold increase. This means that the compiled form of
657 reasonable (normally commented, properly indented etc.) code
658 will take about eight times more space in memory than the
659 code took on disk.
660
661
662 There are two Perl-specific ways to analyze memory usage:
663 $ENV{ PERL_DEBUG_MSTATS } and
664 __-DL__ command-line switch. The first is available only
665 if Perl is compiled with Perl's ''malloc()''; the second
666 only if Perl was built with -DDEBUGGING. See the
667 instructions for how to do this in the
668 ''INSTALL'' podpage at the top level of
669 the Perl source tree.
670
671
672 __Using__ $ENV{PERL_DEBUG_MSTATS}
673
674
675 If your perl is using Perl's ''malloc()'' and was
676 compiled with the necessary switches (this is the default),
677 then it will print memory usage statistics after compiling
678 your code when $ENV{PERL_DEBUG_MSTATS} , and
679 before termination of the program when
680 $ENV{PERL_DEBUG_MSTATS} . The report format
681 is similar to the following example:
682
683
684 $ PERL_DEBUG_MSTATS=2 perl -e
685 It is possible to ask for such a statistic at arbitrary points in your execution using the ''mstat()'' function out of the standard Devel::Peek module.
686
687
688 Here is some explanation of that format:
689
690
691 buckets
692 SMALLEST(APPROX)..GREATEST(APPROX)
693
694
695 Perl's ''malloc()'' uses bucketed allocations. Every
696 request is rounded up to the closest bucket size available,
697 and a bucket is taken from the pool of buckets of that
698 size.
699
700
701 The line above describes the limits of buckets currently in
702 use. Each bucket has two sizes: memory footprint and the
703 maximal size of user data that can fit into this bucket.
704 Suppose in the above example that the smallest bucket were
705 size 4. The biggest bucket would have usable size 8188, and
706 the memory footprint would be 8192.
707
708
709 In a Perl built for debugging, some buckets may have
710 negative usable size. This means that these buckets cannot
711 (and will not) be used. For larger buckets, the memory
712 footprint may be one page greater than a power of 2. If so,
713 case the corresponding power of two is printed in the
714 APPROX field above.
715
716
717 Free/Used
718
719
720 The 1 or 2 rows of numbers following that correspond to the
721 number of buckets of each size between SMALLEST and
722 GREATEST. In the first row, the sizes (memory
723 footprints) of buckets are powers of two--or possibly one
724 page greater. In the second row, if present, the memory
725 footprints of the buckets are between the memory footprints
726 of two buckets ``above''.
727
728
729 For example, suppose under the previous example, the memory
730 footprints were
731
732
733 free: 8 16 32 64 128 256 512 1024 2048 4096 8192
734 4 12 24 48 80
735 With non-DEBUGGING perl, the buckets starting from 128 have a 4-byte overhead, and thus a 8192-long bucket may take up to 8188-byte allocations.
736
737
738 Total sbrk(): SBRKed/SBRKs:CONTINUOUS
739
740
741 The first two fields give the total amount of memory perl
742 sbrk(2)ed (ess-broken? :-) and number of
743 sbrk(2)s used. The third number is what perl thinks
744 about continuity of returned chunks. So long as this number
745 is positive, ''malloc()'' will assume that it is probable
746 that sbrk(2) will provide continuous
747 memory.
748
749
750 Memory allocated by external libraries is not
751 counted.
752
753
754 pad: 0
755
756
757 The amount of sbrk(2)ed memory needed to keep buckets
758 aligned.
759
760
761 heads: 2192
762
763
764 Although memory overhead of bigger buckets is kept inside
765 the bucket, for smaller buckets, it is kept in separate
766 areas. This field gives the total size of these
767 areas.
768
769
770 chain: 0
771
772
773 ''malloc()'' may want to subdivide a bigger bucket into
774 smaller buckets. If only a part of the deceased bucket is
775 left unsubdivided, the rest is kept as an element of a
776 linked list. This field gives the total size of these
777 chunks.
778
779
780 tail: 6144
781
782
783 To minimize the number of sbrk(2)s, ''malloc()''
784 asks for more memory. This field gives the size of the yet
785 unused part, which is sbrk(2)ed, but never
786 touched.
787
788
789 __Example of using -DL switch__
790
791
792 Below we show how to analyse memory usage by
793
794
795 do 'lib/auto/POSIX/autosplit.ix';
796 The file in question contains a header and 146 lines similar to
797
798
799 sub getcwd;
800 __WARNING__ : The discussion below supposes 32-bit architecture. In newer releases of Perl, memory usage of the constructs discussed here is greatly improved, but the story discussed below is a real-life story. This story is mercilessly terse, and assumes rather more than cursory knowledge of Perl internals. Type space to continue, `q' to quit. (Actually, you just want to skip to the next section.)
801
802
803 Here is the itemized list of Perl allocations performed
804 during parsing of this file:
805
806
807 !!!
808 To see this list, insert two warn('!...') statements around the call:
809
810
811 warn('!');
812 do 'lib/auto/POSIX/autosplit.ix';
813 warn('!!!
814 and run it with Perl's __-DL__ option. The first ''warn()'' will print memory allocation info before parsing the file and will memorize the statistics at this point (we ignore what it prints). The second ''warn()'' prints increments with respect to these memorized data. This is the printout shown above.
815
816
817 Different ''Id''s on the left correspond to different
818 subsystems of the perl interpreter. They are just the first
819 argument given to the perl memory allocation
820 API named ''New()''. To find what 9
821 03 means, just __grep__ the perl source for
822 903. You'll find it in ''util.c'', function
823 ''savepvn()''. (I know, you wonder why we told you to
824 __grep__ and then gave away the answer. That's because
825 grepping the source is good for the soul.) This function is
826 used to store a copy of an existing chunk of memory. Using a
827 C debugger, one can see that the function was called either
828 directly from ''gv_init()'' or via ''sv_magic()'', and
829 that ''gv_init()'' is called from
830 ''gv_fetchpv()''--which was itself called from
831 ''newSUB()''. Please stop to catch your breath
832 now.
833
834
835 __NOTE__ : To reach this point in the
836 debugger and skip the calls to ''savepvn()'' during the
837 compilation of the main program, you should set a C
838 breakpoint in ''Perl_warn()'', continue until this point
839 is reached, and ''then'' set a C breakpoint in
840 ''Perl_savepvn()''. Note that you may need to skip a
841 handful of ''Perl_savepvn()'' calls that do not
842 correspond to mass production of CVs (there are more
843 903 allocations than 146 similar lines of
844 ''lib/auto/POSIX/autosplit.ix''). Note also that
845 Perl_ prefixes are added by macroization code in
846 perl header files to avoid conflicts with external
847 libraries.
848
849
850 Anyway, we see that 903 ids correspond to creation
851 of globs, twice per glob - for glob name, and glob
852 stringification magic.
853
854
855 Here are explanations for other ''Id''s
856 above:
857
858
859 717
860
861
862 Creates bigger XPV* structures. In the case above,
863 it creates 3 AVs per subroutine, one for a list of
864 lexical variable names, one for a scratchpad (which contains
865 lexical variables and targets), and one for the
866 array of scratchpads needed for recursion.
867
868
869 It also creates a GV and a CV per
870 subroutine, all called from
871 ''start_subparse()''.
872
873
874 002
875
876
877 Creates a C array corresponding to the AV of
878 scratchpads and the scratchpad itself. The first fake entry
879 of this scratchpad is created though the subroutine itself
880 is not defined yet.
881
882
883 It also creates C arrays to keep data for the stash. This is
884 one HV , but it grows; thus, there are 4 big
885 allocations: the big chunks are not freed, but are kept as
886 additional arenas for SV allocations.
887
888
889 054
890
891
892 Creates a HEK for the name of the glob for the
893 subroutine. This name is a key in a
894 ''stash''.
895
896
897 Big allocations with this ''Id'' correspond to
898 allocations of new arenas to keep HE.
899
900
901 602
902
903
904 Creates a GP for the glob for the
905 subroutine.
906
907
908 702
909
910
911 Creates the MAGIC for the glob for the
912 subroutine.
913
914
915 704
916
917
918 Creates ''arenas'' which keep SVs.
919
920
921 __-DL details__
922
923
924 If Perl is run with __-DL__ option, then ''warn()''s
925 that start with `!' behave specially. They print a list of
926 ''categories'' of memory allocations, and statistics of
927 allocations of different sizes for these
928 categories.
929
930
931 If ''warn()'' string starts with
932
933
934 !!!
935
936
937 print changed categories only, print the differences in
938 counts of allocations.
939
940
941 !!
942
943
944 print grown categories only; print the absolute values of
945 counts, and totals.
946
947
948 !
949
950
951 print nonempty categories, print the absolute values of
952 counts and totals.
953
954
955 __Limitations of -DL statistics__
956
957
958 If an extension or external library does not use the Perl
959 API to allocate memory, such allocations are
960 not counted.
961 !!SEE ALSO
962
963
964 perldebug, perlguts, perlrun re, and
965 Devel::Dprof.
966 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.