← Index
NYTProf Performance Profile   « line view »
For examples/benchmark4.pl
  Run on Thu Aug 28 19:01:43 2014
Reported on Thu Aug 28 19:02:42 2014

Filename/usr/share/perl/5.18/Carp.pm
StatementsExecuted 182 statements in 3.47ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11129µs29µsCarp::::BEGIN@3Carp::BEGIN@3
11125µs81µsCarp::::short_error_locCarp::short_error_loc
11123µs28µsCarp::::caller_infoCarp::caller_info
32120µs48µsCarp::::trustsCarp::trusts
42116µs28µsCarp::::get_statusCarp::get_status
11114µs146µsCarp::::shortmessCarp::shortmess
63114µs14µsCarp::::_cgcCarp::_cgc
11112µs40µsCarp::::ret_summaryCarp::ret_summary
31112µs12µsCarp::::trusts_directlyCarp::trusts_directly
11110µs156µsCarp::::croakCarp::croak
1118µs16µsCarp::::BEGIN@441Carp::BEGIN@441
1118µs22µsCarp::::BEGIN@433Carp::BEGIN@433
1118µs18µsCarp::::BEGIN@434Carp::BEGIN@434
1117µs18µsCarp::::BEGIN@67Carp::BEGIN@67
1117µs128µsCarp::::shortmess_heavyCarp::shortmess_heavy
1117µs10µsCarp::::BEGIN@5Carp::BEGIN@5
1117µs7µsCarp::::BEGIN@7Carp::BEGIN@7
1116µs16µsCarp::::BEGIN@448Carp::BEGIN@448
1116µs6µsCarp::::BEGIN@105Carp::BEGIN@105
1116µs16µsCarp::::BEGIN@18Carp::BEGIN@18
1116µs17µsCarp::::BEGIN@4Carp::BEGIN@4
1116µs6µsCarp::::BEGIN@17Carp::BEGIN@17
1115µs14µsCarp::::BEGIN@8Carp::BEGIN@8
1113µs3µsCarp::::get_subnameCarp::get_subname
0000s0sCarp::::__ANON__[:13]Carp::__ANON__[:13]
0000s0sCarp::::__ANON__[:23]Carp::__ANON__[:23]
0000s0sCarp::::carpCarp::carp
0000s0sCarp::::cluckCarp::cluck
0000s0sCarp::::confessCarp::confess
0000s0sCarp::::export_failCarp::export_fail
0000s0sCarp::::format_argCarp::format_arg
0000s0sCarp::::long_error_locCarp::long_error_loc
0000s0sCarp::::longmessCarp::longmess
0000s0sCarp::::longmess_heavyCarp::longmess_heavy
0000s0sCarp::::ret_backtraceCarp::ret_backtrace
0000s0sCarp::::str_len_trimCarp::str_len_trim
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Carp;
2
3360µs129µs
# spent 29µs within Carp::BEGIN@3 which was called: # once (29µs+0s) by Benchmark::BEGIN@432 at line 3
{ use 5.006; }
# spent 29µs making 1 call to Carp::BEGIN@3
4227µs227µs
# spent 17µs (6+11) within Carp::BEGIN@4 which was called: # once (6µs+11µs) by Benchmark::BEGIN@432 at line 4
use strict;
# spent 17µs making 1 call to Carp::BEGIN@4 # spent 11µs making 1 call to strict::import
5227µs214µs
# spent 10µs (7+4) within Carp::BEGIN@5 which was called: # once (7µs+4µs) by Benchmark::BEGIN@432 at line 5
use warnings;
# spent 10µs making 1 call to Carp::BEGIN@5 # spent 4µs making 1 call to warnings::import
6
7
# spent 7µs within Carp::BEGIN@7 which was called: # once (7µs+0s) by Benchmark::BEGIN@432 at line 15
BEGIN {
8281µs222µs
# spent 14µs (5+9) within Carp::BEGIN@8 which was called: # once (5µs+9µs) by Benchmark::BEGIN@432 at line 8
no strict "refs";
# spent 14µs making 1 call to Carp::BEGIN@8 # spent 9µs making 1 call to strict::unimport
917µs if(exists($::{"utf8::"}) && exists(*{$::{"utf8::"}}{HASH}->{"is_utf8"}) &&
10 defined(*{*{$::{"utf8::"}}{HASH}->{"is_utf8"}}{CODE})) {
11 *is_utf8 = \&{"utf8::is_utf8"};
12 } else {
13 *is_utf8 = sub { 0 };
14 }
15128µs17µs}
# spent 7µs making 1 call to Carp::BEGIN@7
16
17
# spent 6µs within Carp::BEGIN@17 which was called: # once (6µs+0s) by Benchmark::BEGIN@432 at line 25
BEGIN {
18284µs226µs
# spent 16µs (6+10) within Carp::BEGIN@18 which was called: # once (6µs+10µs) by Benchmark::BEGIN@432 at line 18
no strict "refs";
# spent 16µs making 1 call to Carp::BEGIN@18 # spent 10µs making 1 call to strict::unimport
1917µs if(exists($::{"utf8::"}) && exists(*{$::{"utf8::"}}{HASH}->{"downgrade"}) &&
20 defined(*{*{$::{"utf8::"}}{HASH}->{"downgrade"}}{CODE})) {
21 *downgrade = \&{"utf8::downgrade"};
22 } else {
23 *downgrade = sub {};
24 }
251236µs16µs}
# spent 6µs making 1 call to Carp::BEGIN@17
26
271500nsour $VERSION = '1.29';
28
291100nsour $MaxEvalLen = 0;
301100nsour $Verbose = 0;
311100nsour $CarpLevel = 0;
321100nsour $MaxArgLen = 64; # How much of each argument to print. 0 = all.
331100nsour $MaxArgNums = 8; # How many arguments to print. 0 = all.
34
351655µsrequire Exporter;
3617µsour @ISA = ('Exporter');
371700nsour @EXPORT = qw(confess croak carp);
381700nsour @EXPORT_OK = qw(cluck verbose longmess shortmess);
391300nsour @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
40
41# The members of %Internal are packages that are internal to perl.
42# Carp will not report errors from within these packages if it
43# can. The members of %CarpInternal are internal to Perl's warning
44# system. Carp will not report errors from within these packages
45# either, and will not report calls *to* these packages for carp and
46# croak. They replace $CarpLevel, which is deprecated. The
47# $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
48# text and function arguments should be formatted when printed.
49
501100nsour %CarpInternal;
511100nsour %Internal;
52
53# disable these by default, so they can live w/o require Carp
541800ns$CarpInternal{Carp}++;
551200ns$CarpInternal{warnings}++;
561400ns$Internal{Exporter}++;
571200ns$Internal{'Exporter::Heavy'}++;
58
59# if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
60# then the following method will be called by the Exporter which knows
61# to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
62# 'verbose'.
63
64sub export_fail { shift; $Verbose = shift if $_[0] eq 'verbose'; @_ }
65
66
# spent 14µs within Carp::_cgc which was called 6 times, avg 2µs/call: # 4 times (8µs+0s) by Carp::short_error_loc at line 354, avg 2µs/call # once (4µs+0s) by Carp::shortmess at line 93 # once (2µs+0s) by Carp::caller_info at line 117
sub _cgc {
672329µs229µs
# spent 18µs (7+11) within Carp::BEGIN@67 which was called: # once (7µs+11µs) by Benchmark::BEGIN@432 at line 67
no strict 'refs';
# spent 18µs making 1 call to Carp::BEGIN@67 # spent 11µs making 1 call to strict::unimport
68610µs return \&{"CORE::GLOBAL::caller"} if defined &{"CORE::GLOBAL::caller"};
69618µs return;
70}
71
72sub longmess {
73 # Icky backwards compatibility wrapper. :-(
74 #
75 # The story is that the original implementation hard-coded the
76 # number of call levels to go back, so calls to longmess were off
77 # by one. Other code began calling longmess and expecting this
78 # behaviour, so the replacement has to emulate that behaviour.
79 my $cgc = _cgc();
80 my $call_pack = $cgc ? $cgc->() : caller();
81 if ( $Internal{$call_pack} or $CarpInternal{$call_pack} ) {
82 return longmess_heavy(@_);
83 }
84 else {
85 local $CarpLevel = $CarpLevel + 1;
86 return longmess_heavy(@_);
87 }
88}
89
901100nsour @CARP_NOT;
91
92
# spent 146µs (14+132) within Carp::shortmess which was called: # once (14µs+132µs) by Carp::croak at line 100
sub shortmess {
9311µs14µs my $cgc = _cgc();
# spent 4µs making 1 call to Carp::_cgc
94
95 # Icky backwards compatibility wrapper. :-(
9612µs local @CARP_NOT = $cgc ? $cgc->() : caller();
9714µs1128µs shortmess_heavy(@_);
# spent 128µs making 1 call to Carp::shortmess_heavy
98}
99
100116µs1146µs
# spent 156µs (10+146) within Carp::croak which was called: # once (10µs+146µs) by AutoLoader::autoload_sub at line 54 of AutoLoader.pm
sub croak { die shortmess @_ }
# spent 146µs making 1 call to Carp::shortmess
101sub confess { die longmess @_ }
102sub carp { warn shortmess @_ }
103sub cluck { warn longmess @_ }
104
105
# spent 6µs within Carp::BEGIN@105 which was called: # once (6µs+0s) by Benchmark::BEGIN@432 at line 112
BEGIN {
10617µs if("$]" >= 5.015002 || ("$]" >= 5.014002 && "$]" < 5.015) ||
107 ("$]" >= 5.012005 && "$]" < 5.013)) {
108 *CALLER_OVERRIDE_CHECK_OK = sub () { 1 };
109 } else {
110 *CALLER_OVERRIDE_CHECK_OK = sub () { 0 };
111 }
11211.53ms16µs}
# spent 6µs making 1 call to Carp::BEGIN@105
113
114
# spent 28µs (23+5) within Carp::caller_info which was called: # once (23µs+5µs) by Carp::ret_summary at line 342
sub caller_info {
1151600ns my $i = shift(@_) + 1;
1161100ns my %call_info;
1171700ns12µs my $cgc = _cgc();
# spent 2µs making 1 call to Carp::_cgc
118 {
119 # Some things override caller() but forget to implement the
120 # @DB::args part of it, which we need. We check for this by
121 # pre-populating @DB::args with a sentinel which no-one else
122 # has the address of, so that we can detect whether @DB::args
123 # has been properly populated. However, on earlier versions
124 # of perl this check tickles a bug in CORE::caller() which
125 # leaks memory. So we only check on fixed perls.
12622µs @DB::args = \$i if CALLER_OVERRIDE_CHECK_OK;
127 package DB;
128
12916µs
- -
1331400ns unless ( defined $call_info{file} ) {
134 return ();
135 }
136
13711µs13µs my $sub_name = Carp::get_subname( \%call_info );
# spent 3µs making 1 call to Carp::get_subname
1381300ns if ( $call_info{has_args} ) {
1391300ns my @args;
1401500ns if (CALLER_OVERRIDE_CHECK_OK && @DB::args == 1
141 && ref $DB::args[0] eq ref \$i
142 && $DB::args[0] == \$i ) {
143 @DB::args = (); # Don't let anyone see the address of $i
144 local $@;
145 my $where = eval {
146 my $func = $cgc or return '';
147 my $gv =
148 *{
149 ( $::{"B::"} || return '') # B stash
150 ->{svref_2object} || return '' # entry in stash
151 }{CODE} # coderef in entry
152 ->($func)->GV;
153 my $package = $gv->STASH->NAME;
154 my $subname = $gv->NAME;
155 return unless defined $package && defined $subname;
156
157 # returning CORE::GLOBAL::caller isn't useful for tracing the cause:
158 return if $package eq 'CORE::GLOBAL' && $subname eq 'caller';
159 " in &${package}::$subname";
160 } || '';
161 @args
162 = "** Incomplete caller override detected$where; \@DB::args were not set **";
163 }
164 else {
1651500ns @args = @DB::args;
1661100ns my $overflow;
1671400ns if ( $MaxArgNums and @args > $MaxArgNums )
168 { # More than we want to show?
169 $#args = $MaxArgNums;
170 $overflow = 1;
171 }
172
1731700ns @args = map { Carp::format_arg($_) } @args;
174
1751300ns if ($overflow) {
176 push @args, '...';
177 }
178 }
179
180 # Push the args onto the subroutine
18111µs $sub_name .= '(' . join( ', ', @args ) . ')';
182 }
1831500ns $call_info{sub_name} = $sub_name;
18416µs return wantarray() ? %call_info : \%call_info;
185}
186
187# Transform an argument to a function into a string.
188sub format_arg {
189 my $arg = shift;
190 if ( ref($arg) ) {
191 $arg = defined($overload::VERSION) ? overload::StrVal($arg) : "$arg";
192 }
193 if ( defined($arg) ) {
194 $arg =~ s/'/\\'/g;
195 $arg = str_len_trim( $arg, $MaxArgLen );
196
197 # Quote it?
198 # Downgrade, and use [0-9] rather than \d, to avoid loading
199 # Unicode tables, which would be liable to fail if we're
200 # processing a syntax error.
201 downgrade($arg, 1);
202 $arg = "'$arg'" unless $arg =~ /^-?[0-9.]+\z/;
203 }
204 else {
205 $arg = 'undef';
206 }
207
208 # The following handling of "control chars" is direct from
209 # the original code - it is broken on Unicode though.
210 # Suggestions?
211 is_utf8($arg)
212 or $arg =~ s/([[:cntrl:]]|[[:^ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
213 return $arg;
214}
215
216# Takes an inheritance cache and a package and returns
217# an anon hash of known inheritances and anon array of
218# inheritances which consequences have not been figured
219# for.
220
# spent 28µs (16+12) within Carp::get_status which was called 4 times, avg 7µs/call: # 3 times (12µs+8µs) by Carp::trusts at line 415, avg 7µs/call # once (4µs+3µs) by Carp::trusts at line 422
sub get_status {
2214600ns my $cache = shift;
2224500ns my $pkg = shift;
22347µs312µs $cache->{$pkg} ||= [ { $pkg => $pkg }, [ trusts_directly($pkg) ] ];
# spent 12µs making 3 calls to Carp::trusts_directly, avg 4µs/call
22449µs return @{ $cache->{$pkg} };
225}
226
227# Takes the info from caller() and figures out the name of
228# the sub/require/eval
229
# spent 3µs within Carp::get_subname which was called: # once (3µs+0s) by Carp::caller_info at line 137
sub get_subname {
2301300ns my $info = shift;
2311500ns if ( defined( $info->{evaltext} ) ) {
232 my $eval = $info->{evaltext};
233 if ( $info->{is_require} ) {
234 return "require $eval";
235 }
236 else {
237 $eval =~ s/([\\\'])/\\$1/g;
238 return "eval '" . str_len_trim( $eval, $MaxEvalLen ) . "'";
239 }
240 }
241
242 # this can happen on older perls when the sub (or the stash containing it)
243 # has been deleted
2441200ns if ( !defined( $info->{sub} ) ) {
245 return '__ANON__::__ANON__';
246 }
247
24813µs return ( $info->{sub} eq '(eval)' ) ? 'eval {...}' : $info->{sub};
249}
250
251# Figures out what call (from the point of view of the caller)
252# the long error backtrace should start at.
253sub long_error_loc {
254 my $i;
255 my $lvl = $CarpLevel;
256 {
257 ++$i;
258 my $cgc = _cgc();
259 my @caller = $cgc ? $cgc->($i) : caller($i);
260 my $pkg = $caller[0];
261 unless ( defined($pkg) ) {
262
263 # This *shouldn't* happen.
264 if (%Internal) {
265 local %Internal;
266 $i = long_error_loc();
267 last;
268 }
269 elsif (defined $caller[2]) {
270 # this can happen when the stash has been deleted
271 # in that case, just assume that it's a reasonable place to
272 # stop (the file and line data will still be intact in any
273 # case) - the only issue is that we can't detect if the
274 # deleted package was internal (so don't do that then)
275 # -doy
276 redo unless 0 > --$lvl;
277 last;
278 }
279 else {
280 return 2;
281 }
282 }
283 redo if $CarpInternal{$pkg};
284 redo unless 0 > --$lvl;
285 redo if $Internal{$pkg};
286 }
287 return $i - 1;
288}
289
290sub longmess_heavy {
291 return @_ if ref( $_[0] ); # don't break references as exceptions
292 my $i = long_error_loc();
293 return ret_backtrace( $i, @_ );
294}
295
296# Returns a full stack backtrace starting from where it is
297# told.
298sub ret_backtrace {
299 my ( $i, @error ) = @_;
300 my $mess;
301 my $err = join '', @error;
302 $i++;
303
304 my $tid_msg = '';
305 if ( defined &threads::tid ) {
306 my $tid = threads->tid;
307 $tid_msg = " thread $tid" if $tid;
308 }
309
310 my %i = caller_info($i);
311 $mess = "$err at $i{file} line $i{line}$tid_msg";
312 if( defined $. ) {
313 local $@ = '';
314 local $SIG{__DIE__};
315 eval {
316 CORE::die;
317 };
318 if($@ =~ /^Died at .*(, <.*?> line \d+).$/ ) {
319 $mess .= $1;
320 }
321 }
322 $mess .= "\.\n";
323
324 while ( my %i = caller_info( ++$i ) ) {
325 $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
326 }
327
328 return $mess;
329}
330
331
# spent 40µs (12+28) within Carp::ret_summary which was called: # once (12µs+28µs) by Carp::shortmess_heavy at line 387
sub ret_summary {
33211µs my ( $i, @error ) = @_;
33311µs my $err = join '', @error;
3341300ns $i++;
335
3361200ns my $tid_msg = '';
3371300ns if ( defined &threads::tid ) {
338 my $tid = threads->tid;
339 $tid_msg = " thread $tid" if $tid;
340 }
341
34214µs128µs my %i = caller_info($i);
# spent 28µs making 1 call to Carp::caller_info
34315µs return "$err at $i{file} line $i{line}$tid_msg\.\n";
344}
345
346
# spent 81µs (25+56) within Carp::short_error_loc which was called: # once (25µs+56µs) by Carp::shortmess_heavy at line 386
sub short_error_loc {
347 # You have to create your (hash)ref out here, rather than defaulting it
348 # inside trusts *on a lexical*, as you want it to persist across calls.
349 # (You can default it on $_[2], but that gets messy)
3501500ns my $cache = {};
3511100ns my $i = 1;
3521200ns my $lvl = $CarpLevel;
353 {
35454µs48µs my $cgc = _cgc();
# spent 8µs making 4 calls to Carp::_cgc, avg 2µs/call
35542µs my $called = $cgc ? $cgc->($i) : caller($i);
3564300ns $i++;
35741µs my $caller = $cgc ? $cgc->($i) : caller($i);
358
3594400ns if (!defined($caller)) {
360 my @caller = $cgc ? $cgc->($i) : caller($i);
361 if (@caller) {
362 # if there's no package but there is other caller info, then
363 # the package has been deleted - treat this as a valid package
364 # in this case
365 redo if defined($called) && $CarpInternal{$called};
366 redo unless 0 > --$lvl;
367 last;
368 }
369 else {
370 return 0;
371 }
372 }
3734800ns redo if $Internal{$caller};
37441µs redo if $CarpInternal{$caller};
3753600ns redo if $CarpInternal{$called};
37622µs222µs redo if trusts( $called, $caller, $cache );
# spent 22µs making 2 calls to Carp::trusts, avg 11µs/call
3771900ns127µs redo if trusts( $caller, $called, $cache );
# spent 27µs making 1 call to Carp::trusts
3781600ns redo unless 0 > --$lvl;
379 }
38015µs return $i - 1;
381}
382
383
# spent 128µs (7+121) within Carp::shortmess_heavy which was called: # once (7µs+121µs) by Carp::shortmess at line 97
sub shortmess_heavy {
3841200ns return longmess_heavy(@_) if $Verbose;
3851200ns return @_ if ref( $_[0] ); # don't break references as exceptions
3861900ns181µs my $i = short_error_loc();
# spent 81µs making 1 call to Carp::short_error_loc
38714µs140µs if ($i) {
# spent 40µs making 1 call to Carp::ret_summary
388 ret_summary( $i, @_ );
389 }
390 else {
391 longmess_heavy(@_);
392 }
393}
394
395# If a string is too long, trims it with ...
396sub str_len_trim {
397 my $str = shift;
398 my $max = shift || 0;
399 if ( 2 < $max and $max < length($str) ) {
400 substr( $str, $max - 3 ) = '...';
401 }
402 return $str;
403}
404
405# Takes two packages and an optional cache. Says whether the
406# first inherits from the second.
407#
408# Recursive versions of this have to work to avoid certain
409# possible endless loops, and when following long chains of
410# inheritance are less efficient.
411
# spent 48µs (20+28) within Carp::trusts which was called 3 times, avg 16µs/call: # 2 times (9µs+12µs) by Carp::short_error_loc at line 376, avg 11µs/call # once (11µs+16µs) by Carp::short_error_loc at line 377
sub trusts {
4123800ns my $child = shift;
4133400ns my $parent = shift;
4143400ns my $cache = shift;
41533µs321µs my ( $known, $partial ) = get_status( $cache, $child );
# spent 21µs making 3 calls to Carp::get_status, avg 7µs/call
416
417 # Figure out consequences until we have an answer
41832µs while ( @$partial and not exists $known->{$parent} ) {
4191600ns my $anc = shift @$partial;
4201200ns next if exists $known->{$anc};
4211500ns $known->{$anc}++;
42211µs18µs my ( $anc_knows, $anc_partial ) = get_status( $cache, $anc );
# spent 8µs making 1 call to Carp::get_status
42311µs my @found = keys %$anc_knows;
4241700ns @$known{@found} = ();
42511µs push @$partial, @$anc_partial;
426 }
42737µs return exists $known->{$parent};
428}
429
430# Takes a package and gives a list of those trusted directly
431
# spent 12µs within Carp::trusts_directly which was called 3 times, avg 4µs/call: # 3 times (12µs+0s) by Carp::get_status at line 223, avg 4µs/call
sub trusts_directly {
4323600ns my $class = shift;
433229µs236µs
# spent 22µs (8+14) within Carp::BEGIN@433 which was called: # once (8µs+14µs) by Benchmark::BEGIN@432 at line 433
no strict 'refs';
# spent 22µs making 1 call to Carp::BEGIN@433 # spent 14µs making 1 call to strict::unimport
434268µs227µs
# spent 18µs (8+10) within Carp::BEGIN@434 which was called: # once (8µs+10µs) by Benchmark::BEGIN@432 at line 434
no warnings 'once';
# spent 18µs making 1 call to Carp::BEGIN@434 # spent 10µs making 1 call to warnings::unimport
435 return @{"$class\::CARP_NOT"}
436 ? @{"$class\::CARP_NOT"}
437315µs : @{"$class\::ISA"};
438}
439
4401900nsif(!defined($warnings::VERSION) ||
441344µs224µs
# spent 16µs (8+8) within Carp::BEGIN@441 which was called: # once (8µs+8µs) by Benchmark::BEGIN@432 at line 441
do { no warnings "numeric"; $warnings::VERSION < 1.03 }) {
# spent 16µs making 1 call to Carp::BEGIN@441 # spent 8µs making 1 call to warnings::unimport
442 # Very old versions of warnings.pm import from Carp. This can go
443 # wrong due to the circular dependency. If Carp is invoked before
444 # warnings, then Carp starts by loading warnings, then warnings
445 # tries to import from Carp, and gets nothing because Carp is in
446 # the process of loading and hasn't defined its import method yet.
447 # So we work around that by manually exporting to warnings here.
448259µs225µs
# spent 16µs (6+10) within Carp::BEGIN@448 which was called: # once (6µs+10µs) by Benchmark::BEGIN@432 at line 448
no strict "refs";
# spent 16µs making 1 call to Carp::BEGIN@448 # spent 10µs making 1 call to strict::unimport
449 *{"warnings::$_"} = \&$_ foreach @EXPORT;
450}
451
45219µs1;
453
454__END__