line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Linux::DVB::DVBT::Advert::Mem ;
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Linux::DVB::DVBT::Advert::Mem
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Linux::DVB::DVBT::Advert::Mem ;
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$Linux::DVB::DVBT::Advert::Mem::MEM_PROFILE = 1 ;
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# set baseline memory usage start value - all reports are relative to this
|
14
|
|
|
|
|
|
|
set_used() ;
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# display memory used from baseline (and also relative to last call)
|
17
|
|
|
|
|
|
|
print_used() ;
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# show size of variables
|
20
|
|
|
|
|
|
|
var_size("message string", $var1, @var2) ;
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
For debug/developer use.
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This module contains memory profiling tools that I used while developing the advert removal modules
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut
|
29
|
|
|
|
|
|
|
|
30
|
12
|
|
|
12
|
|
68
|
use strict ;
|
|
12
|
|
|
|
|
20
|
|
|
12
|
|
|
|
|
6911
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $VERSION = '1.00' ;
|
33
|
|
|
|
|
|
|
our $MEM_PROFILE ;
|
34
|
|
|
|
|
|
|
our $DEBUG ;
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $HAS_MEM ;
|
37
|
|
|
|
|
|
|
our %mHash ;
|
38
|
|
|
|
|
|
|
our $first_mem ;
|
39
|
|
|
|
|
|
|
our $prev_mem ;
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#============================================================================================
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 Mem
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=over 4
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=back
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
BEGIN {
|
54
|
12
|
|
|
12
|
|
27
|
$MEM_PROFILE = 0 ;
|
55
|
12
|
|
|
|
|
24
|
$DEBUG = 0 ;
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
12
|
|
|
|
|
55
|
*set_used = \&set_used_null ;
|
59
|
12
|
|
|
|
|
36
|
*get_used = \&get_used_null ;
|
60
|
12
|
|
|
|
|
35
|
*print_used = \&print_used_null ;
|
61
|
12
|
|
|
|
|
35
|
*varsize = \&varsize_null ;
|
62
|
|
|
|
|
|
|
|
63
|
12
|
|
|
|
|
67
|
my %PROCLIST = (
|
64
|
|
|
|
|
|
|
'print_used_win32' => ["Win32::SystemInfo"],
|
65
|
|
|
|
|
|
|
'print_used_proc' => ["Proc::ProcessTable"],
|
66
|
|
|
|
|
|
|
) ;
|
67
|
|
|
|
|
|
|
|
68
|
12
|
50
|
|
|
|
65
|
print STDERR "Mem.pm\n" if $DEBUG ;
|
69
|
|
|
|
|
|
|
|
70
|
12
|
|
|
|
|
46
|
foreach my $func (keys %PROCLIST)
|
71
|
|
|
|
|
|
|
{
|
72
|
24
|
50
|
|
|
|
65
|
print STDERR " + check $func... \n" if $DEBUG ;
|
73
|
|
|
|
|
|
|
|
74
|
24
|
|
|
|
|
36
|
my $ok =1 ;
|
75
|
24
|
|
|
|
|
32
|
foreach my $mod (@{$PROCLIST{$func}})
|
|
24
|
|
|
|
|
61
|
|
76
|
|
|
|
|
|
|
{
|
77
|
24
|
50
|
|
|
|
61
|
print STDERR " + import $mod... " if $DEBUG ;
|
78
|
24
|
50
|
|
|
|
1796
|
if (eval "require $mod")
|
79
|
|
|
|
|
|
|
{
|
80
|
0
|
|
|
|
|
0
|
$mod->import() ;
|
81
|
0
|
0
|
|
|
|
0
|
print STDERR "ok\n" if $DEBUG ;
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
else
|
84
|
|
|
|
|
|
|
{
|
85
|
24
|
|
|
|
|
46
|
$ok = 0 ;
|
86
|
24
|
50
|
|
|
|
166
|
print STDERR "fail\n" if $DEBUG ;
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
|
90
|
24
|
50
|
|
|
|
93
|
if ($ok)
|
91
|
|
|
|
|
|
|
{
|
92
|
0
|
|
|
|
|
0
|
$HAS_MEM = 1 ;
|
93
|
0
|
|
|
|
|
0
|
eval {
|
94
|
0
|
|
|
|
|
0
|
*print_used = \&$func ;
|
95
|
|
|
|
|
|
|
} ;
|
96
|
0
|
|
|
|
|
0
|
last ;
|
97
|
0
|
0
|
|
|
|
0
|
print STDERR "MEM: got $func\n" if $DEBUG ;
|
98
|
|
|
|
|
|
|
}
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
|
101
|
12
|
|
|
|
|
34
|
my $mod = "Devel::Size" ;
|
102
|
12
|
50
|
|
|
|
45
|
print STDERR " + import $mod... " if $DEBUG ;
|
103
|
12
|
50
|
|
|
|
726
|
if (eval "require $mod")
|
104
|
|
|
|
|
|
|
{
|
105
|
0
|
|
|
|
|
0
|
$mod->import() ;
|
106
|
0
|
|
|
|
|
0
|
*varsize = \&varsize_devel ;
|
107
|
0
|
0
|
|
|
|
0
|
print STDERR "ok\n" if $DEBUG ;
|
108
|
0
|
0
|
|
|
|
0
|
print STDERR "MEM: got $mod\n" if $DEBUG ;
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
else
|
111
|
|
|
|
|
|
|
{
|
112
|
12
|
50
|
|
|
|
11558
|
print STDERR "fail\n" if $DEBUG ;
|
113
|
|
|
|
|
|
|
}
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
117
|
|
|
|
|
|
|
sub get_used_null
|
118
|
0
|
|
|
0
|
0
|
|
{
|
119
|
|
|
|
|
|
|
}
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
122
|
|
|
|
|
|
|
sub get_used_win32
|
123
|
|
|
|
|
|
|
{
|
124
|
0
|
0
|
|
0
|
0
|
|
return 0 unless $MEM_PROFILE ;
|
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $avail ;
|
127
|
0
|
0
|
|
|
|
|
if (Win32::SystemInfo::MemoryStatus(%mHash, "MB"))
|
128
|
|
|
|
|
|
|
{
|
129
|
0
|
|
|
|
|
|
$avail = $mHash{'AvailPhys'} * 1.0 ;
|
130
|
|
|
|
|
|
|
}
|
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
return $avail ;
|
133
|
|
|
|
|
|
|
}
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
136
|
|
|
|
|
|
|
sub get_used_proc
|
137
|
|
|
|
|
|
|
{
|
138
|
0
|
0
|
|
0
|
0
|
|
return 0 unless $MEM_PROFILE ;
|
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $t = new Proc::ProcessTable;
|
141
|
0
|
|
|
|
|
|
my $total = 0 ;
|
142
|
0
|
|
|
|
|
|
foreach my $got ( @{$t->table} )
|
|
0
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
{
|
144
|
0
|
|
|
|
|
|
my $pid = $got->pid ;
|
145
|
0
|
0
|
|
|
|
|
next if not $pid eq $$;
|
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
if ($got->can("size"))
|
148
|
|
|
|
|
|
|
{
|
149
|
0
|
|
|
|
|
|
$total += $got->size;
|
150
|
|
|
|
|
|
|
}
|
151
|
|
|
|
|
|
|
else
|
152
|
|
|
|
|
|
|
{
|
153
|
0
|
|
|
|
|
|
my @lines = `cat /proc/$pid/statm 2>/dev/null` ;
|
154
|
0
|
|
|
|
|
|
my ($size_pages,
|
155
|
|
|
|
|
|
|
$resident_pages,
|
156
|
|
|
|
|
|
|
$share_pages,
|
157
|
|
|
|
|
|
|
$trs_pages,
|
158
|
|
|
|
|
|
|
$lrs_pages,
|
159
|
|
|
|
|
|
|
$drs_pages,
|
160
|
|
|
|
|
|
|
$dt_pages) = split /\s+/, $lines[0] ;
|
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if ($resident_pages)
|
163
|
|
|
|
|
|
|
{
|
164
|
|
|
|
|
|
|
# 4k pages to bytes
|
165
|
0
|
|
|
|
|
|
my $resident = $resident_pages << 12 ;
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
#print " + pmap $pid : $resident\n" ;
|
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
$total += $resident ;
|
170
|
|
|
|
|
|
|
}
|
171
|
|
|
|
|
|
|
}
|
172
|
|
|
|
|
|
|
}
|
173
|
0
|
0
|
|
|
|
|
if ($total)
|
174
|
|
|
|
|
|
|
{
|
175
|
0
|
|
|
|
|
|
$total = int($total / (1024*1024)) ;
|
176
|
|
|
|
|
|
|
}
|
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
return $total ;
|
179
|
|
|
|
|
|
|
}
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
183
|
|
|
|
|
|
|
sub set_used_null
|
184
|
0
|
|
|
0
|
0
|
|
{
|
185
|
|
|
|
|
|
|
}
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
188
|
|
|
|
|
|
|
sub set_used_win32
|
189
|
|
|
|
|
|
|
{
|
190
|
0
|
0
|
|
0
|
0
|
|
return 0 unless $MEM_PROFILE ;
|
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
my $avail = get_used_win32();
|
193
|
0
|
0
|
|
|
|
|
if ($avail)
|
194
|
|
|
|
|
|
|
{
|
195
|
0
|
|
|
|
|
|
$prev_mem = $avail ;
|
196
|
0
|
|
0
|
|
|
|
$first_mem ||= $avail ;
|
197
|
|
|
|
|
|
|
}
|
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
return $avail ;
|
200
|
|
|
|
|
|
|
}
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
203
|
|
|
|
|
|
|
sub set_used_proc
|
204
|
|
|
|
|
|
|
{
|
205
|
0
|
0
|
|
0
|
0
|
|
return 0 unless $MEM_PROFILE ;
|
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
my $total = get_used_proc() ;
|
208
|
0
|
0
|
|
|
|
|
if ($total)
|
209
|
|
|
|
|
|
|
{
|
210
|
0
|
|
|
|
|
|
$total = int($total / (1024*1024)) ;
|
211
|
|
|
|
|
|
|
|
212
|
0
|
|
|
|
|
|
$prev_mem = $total ;
|
213
|
0
|
|
0
|
|
|
|
$first_mem ||= $total ;
|
214
|
|
|
|
|
|
|
}
|
215
|
|
|
|
|
|
|
|
216
|
0
|
|
|
|
|
|
return $total ;
|
217
|
|
|
|
|
|
|
}
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
220
|
|
|
|
|
|
|
sub print_used_null
|
221
|
0
|
|
|
0
|
0
|
|
{
|
222
|
|
|
|
|
|
|
}
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
225
|
|
|
|
|
|
|
sub print_used_win32
|
226
|
|
|
|
|
|
|
{
|
227
|
0
|
|
|
0
|
0
|
|
my ($msg) = @_ ;
|
228
|
|
|
|
|
|
|
|
229
|
0
|
0
|
|
|
|
|
return unless $MEM_PROFILE ;
|
230
|
|
|
|
|
|
|
|
231
|
0
|
|
0
|
|
|
|
$msg ||= "" ;
|
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
my $avail = get_used_win32() ;
|
234
|
0
|
0
|
|
|
|
|
if ($avail)
|
235
|
|
|
|
|
|
|
{
|
236
|
0
|
0
|
|
|
|
|
if ($first_mem)
|
237
|
|
|
|
|
|
|
{
|
238
|
0
|
|
|
|
|
|
my $used = $first_mem - $avail ;
|
239
|
0
|
|
|
|
|
|
my $diff = $prev_mem - $avail ;
|
240
|
0
|
0
|
|
|
|
|
$msg .= ": " if $msg ;
|
241
|
0
|
|
|
|
|
|
print "${msg}Memory used $used MB (since last call $diff MB)\n" ;
|
242
|
|
|
|
|
|
|
}
|
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
$prev_mem = $avail ;
|
245
|
0
|
|
0
|
|
|
|
$first_mem ||= $avail ;
|
246
|
|
|
|
|
|
|
}
|
247
|
|
|
|
|
|
|
}
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
250
|
|
|
|
|
|
|
sub print_used_proc
|
251
|
|
|
|
|
|
|
{
|
252
|
0
|
|
|
0
|
0
|
|
my ($msg) = @_ ;
|
253
|
|
|
|
|
|
|
|
254
|
0
|
0
|
|
|
|
|
return unless $MEM_PROFILE ;
|
255
|
|
|
|
|
|
|
|
256
|
0
|
|
0
|
|
|
|
$msg ||= "" ;
|
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
my $total = get_used_proc() ;
|
259
|
0
|
0
|
|
|
|
|
if ($total)
|
260
|
|
|
|
|
|
|
{
|
261
|
0
|
0
|
|
|
|
|
if ($first_mem < $total)
|
262
|
|
|
|
|
|
|
{
|
263
|
0
|
|
|
|
|
|
my $used = $total - $first_mem ;
|
264
|
0
|
|
|
|
|
|
my $diff = $total - $prev_mem ;
|
265
|
0
|
0
|
|
|
|
|
$msg .= ": " if $msg ;
|
266
|
0
|
|
|
|
|
|
print "${msg}Memory used $used MB (since last call $diff MB)\n" ;
|
267
|
|
|
|
|
|
|
}
|
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
$prev_mem = $total ;
|
270
|
0
|
|
0
|
|
|
|
$first_mem ||= $total ;
|
271
|
|
|
|
|
|
|
}
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
}
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------
|
278
|
|
|
|
|
|
|
sub varsize_null
|
279
|
0
|
|
|
0
|
0
|
|
{
|
280
|
|
|
|
|
|
|
}
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------
|
283
|
|
|
|
|
|
|
sub varsize_devel
|
284
|
|
|
|
|
|
|
{
|
285
|
0
|
|
|
0
|
0
|
|
my ($msg, @vars) = @_ ;
|
286
|
|
|
|
|
|
|
|
287
|
0
|
0
|
|
|
|
|
return unless $MEM_PROFILE ;
|
288
|
|
|
|
|
|
|
|
289
|
0
|
|
|
|
|
|
print "\n-------------------------------------------------------------------------\n" ;
|
290
|
0
|
|
|
|
|
|
my @collect ;
|
291
|
|
|
|
|
|
|
|
292
|
0
|
|
|
|
|
|
foreach (@vars)
|
293
|
|
|
|
|
|
|
{
|
294
|
0
|
|
|
|
|
|
push @collect, $_ ;
|
295
|
|
|
|
|
|
|
}
|
296
|
0
|
|
|
|
|
|
my $size = int(Devel::Size::total_size(\@collect) / (1024 * 1024)) ;
|
297
|
0
|
|
|
|
|
|
print "$msg : Variables size = $size MB\n" ;
|
298
|
|
|
|
|
|
|
|
299
|
0
|
|
|
|
|
|
print "\n-------------------------------------------------------------------------\n" ;
|
300
|
|
|
|
|
|
|
}
|
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# ============================================================================================
|
304
|
|
|
|
|
|
|
# END OF PACKAGE
|
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
1;
|
307
|
|
|
|
|
|
|
|