annotate Lib/IMPL/Profiler.pm @ 17:7f88e01b58f8

dom
author Sergey
date Wed, 09 Sep 2009 17:43:31 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::Profiler;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 our $Enabled;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 our %TrappedModules;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 our %InvokeInfo;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 our $InvokeTime = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 my $level;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 $level = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 if ($Enabled) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 warn "profiler enabled";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 no warnings 'once';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 *CORE::GLOBAL::caller = sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 my $target = (shift || 0)+1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 my $realFrame = 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 for (my $i = 1; $i<$target; $i++) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 $realFrame ++;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 my $caller = CORE::caller($realFrame-1) or return;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 $realFrame ++ if $caller eq 'IMPL::Profiler::Proxy'; #current frame is proxy
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 my @frame = CORE::caller($realFrame) or return;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 if ( $frame[0] eq 'IMPL::Profiler::Proxy' ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 my @next = CORE::caller($realFrame+1) or return;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 @frame[0..2] = @next[0..2];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 #warn "\t"x$level,"$frame[0] - $frame[3]";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 return wantarray ? @frame : $frame[0];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 use warnings;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 use Time::HiRes;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 require Scalar::Util;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 sub trap_all {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 return if not $Enabled;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 foreach my $class (@_) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 next if $TrappedModules{$class};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 $TrappedModules{$class} = 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 eval "warn 'load $class'; require $class;" if not %{"${class}::"};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 die $@ if $@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 my $table = \%{"${class}::"};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 trap($class,$_) foreach (grep *{$table->{$_}}{CODE}, keys %$table);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 sub trap {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 my ($class,$method) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 return if not $Enabled;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 my $prevCode = \&{"${class}::${method}"};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 my $proto = prototype $prevCode;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 if (defined $proto and not $proto) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 return;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 package IMPL::Profiler::Proxy;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 no warnings 'redefine';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 my $sub = sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 my $t0 = [Time::HiRes::gettimeofday];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 my @arr;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my $scalar;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 my $entry = $prevCode;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 my ($timeOwn,$timeTotal);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 my $context = wantarray;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 local $InvokeTime = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 #warn "\t"x$level,"enter ${class}::$method";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 $level ++;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 if ($context) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 @arr = &$entry(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 if (defined $context) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 $scalar = &$entry(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 &$entry(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 $timeTotal = Time::HiRes::tv_interval($t0);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 $timeOwn = $timeTotal - $InvokeTime;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 $InvokeInfo{"${class}::${method}"}{Count} ++;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 $InvokeInfo{"${class}::${method}"}{Total} += $timeTotal;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 $InvokeInfo{"${class}::${method}"}{Own} += $timeOwn;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 $InvokeTime += $timeTotal;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 $level --;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 #warn "\t"x$level,"leave ${class}::$method";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 return $context ? @arr : $scalar;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 if ($proto) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 Scalar::Util::set_prototype($sub => $proto);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 *{"${class}::${method}"} = $sub;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 sub PrintStatistics {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 my $hout = shift || *STDERR;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 print $hout "-- modules --\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 print $hout "$_\n" foreach sort keys %TrappedModules;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 print $hout "\n-- stats --\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 print $hout
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 pad($_,50),
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 pad("$InvokeInfo{$_}{Count}",10),
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 pad(sprintf("%.3f",$InvokeInfo{$_}{Own}),10),
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121 pad(sprintf("%.3f",$InvokeInfo{$_}{Total}),10),
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 "\n"
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 foreach sort { $InvokeInfo{$b}{Own} <=> $InvokeInfo{$a}{Own} } keys %InvokeInfo;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 sub ResetStatistics {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 $InvokeTime = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 %InvokeInfo = ();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131 sub pad {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 my ($str,$len) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133 if (length $str < $len) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134 return $str.(' 'x ($len- length $str));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136 return $str;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 1;