49
|
1 package IMPL::Profiler::Memory;
|
|
2
|
|
3 use strict;
|
|
4 use Carp qw(longmess shortmess);
|
|
5 use Scalar::Util qw(refaddr weaken isweak);
|
|
6
|
188
|
7 my %listeners;
|
|
8 my $trapped;
|
49
|
9
|
|
10 BEGIN {
|
188
|
11 $trapped = 0;
|
|
12 }
|
49
|
13
|
188
|
14 sub import {
|
|
15 if (not $trapped) {
|
|
16 *CORE::GLOBAL::bless = sub {
|
|
17 $_[1] |= caller unless $_[1];
|
|
18 my $ref = CORE::bless $_[0],$_[1];
|
|
19
|
|
20 $_->track($ref) foreach values %listeners;
|
|
21
|
|
22 return $ref;
|
|
23 };
|
|
24 $trapped = 1;
|
|
25 }
|
|
26 }
|
49
|
27
|
188
|
28 sub _ConnectListener {
|
|
29 my ($self,$listener) = @_;
|
|
30
|
|
31 die "Invalid listener" unless ref $listener;
|
|
32
|
|
33 $listeners{refaddr($listener)} = $listener;
|
|
34 }
|
49
|
35
|
188
|
36 sub _RemoveListener {
|
|
37 my ($self,$listener) = @_;
|
|
38
|
|
39 die "Invalid listener" unless ref $listener;
|
|
40
|
|
41 delete $listeners{refaddr($listener)};
|
|
42 }
|
49
|
43
|
188
|
44 sub Monitor {
|
|
45 my ($self,$code) = @_;
|
|
46
|
|
47 my $data = IMPL::Profiler::Memory::Data->new();
|
|
48
|
|
49 $data->Monitor($code);
|
|
50
|
|
51 return $data;
|
49
|
52 }
|
|
53
|
188
|
54 package IMPL::Profiler::Memory::Data;
|
|
55 use parent qw(IMPL::Object::Fields);
|
|
56
|
|
57 use Data::Dumper();
|
|
58 use Scalar::Util qw(refaddr weaken isweak);
|
|
59
|
|
60 use fields qw( objects );
|
|
61
|
|
62 sub CTOR {
|
|
63 my $this = shift;
|
|
64 $this->{objects} = [];
|
|
65 }
|
|
66
|
|
67 sub track {
|
|
68 my $i = scalar @{$_[0]->{objects}};
|
|
69 $_[0]->{objects}[$i] = $_[1];
|
|
70 weaken($_[0]->{objects}[$i]);
|
|
71 }
|
|
72
|
|
73 sub Purge {
|
|
74 my $this = shift;
|
|
75
|
|
76 return $this->{objects} = [ grep defined($_), @{$this->{objects}}];
|
49
|
77 }
|
|
78
|
188
|
79 sub Dump {
|
|
80 my $this = shift;
|
|
81 return Data::Dumper->Dump($this->{objects});
|
|
82 }
|
49
|
83
|
188
|
84 sub isLeak {
|
|
85 my ($this) = @_;
|
|
86 $this->Purge();
|
|
87 return ( scalar(@{$this->{objects}}) > 0);
|
49
|
88 }
|
|
89
|
188
|
90 sub Monitor {
|
|
91 my ($this,$code) = @_;
|
|
92
|
|
93 die "A reference to a subroutine is required" unless ref $code;
|
|
94
|
|
95 IMPL::Profiler::Memory->_ConnectListener($this);
|
|
96 eval {
|
|
97 $code->();
|
|
98 };
|
|
99 my $err = $@;
|
|
100 IMPL::Profiler::Memory->_RemoveListener($this);
|
|
101
|
|
102 die $err if $err;
|
|
103
|
|
104 return;
|
49
|
105 }
|
|
106
|
188
|
107
|
|
108
|
49
|
109 1;
|