File: Usage.pm

package info (click to toggle)
libmemory-usage-perl 0.201-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 1,361; makefile: 2
file content (267 lines) | stat: -rw-r--r-- 5,017 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package Memory::Usage;
use warnings;
use strict;

=head1 NAME

Memory::Usage - Tools to determine actual memory usage

=head1 VERSION

Version 0.201

=cut

our $VERSION = '0.201';


=head1 SYNOPSIS

    use Memory::Usage;
    my $mu = Memory::Usage->new();

    # Record amount of memory used by current process
    $mu->record('starting work');

    # Do the thing you want to measure
    $object->something_memory_intensive();

    # Record amount in use afterwards
    $mu->record('after something_memory_intensive()');

    # Spit out a report
    $mu->dump();


=head1 DESCRIPTION

This module lets you attempt to measure, from your operating system's
perspective, how much memory a process is using at any given time.

=head1 METHODS

=head2 Class Methods

=over 4

=item new ( )

Creates a new Memory::Usage object.  This object will contain usage state as
recorded along the way.

=back

=cut

sub new
{
	my ($class) = @_;

	# TODO: allow pre-sizing of array at construct time so auto-growing
	# doesn't affect memory usage later on.  Probably not a big deal given
	# that our granularity is pages (4k) and our logged messages are small.
	my $self = [];

	return bless $self, $class;
}

# Returns
#  (vsize, rss, shared pages, code pages, data+stack pages)
# in kilobytes.  Precision is to nearest 4k page.
# TODO: Proc::ProcessTable so that we can support non-Linux?
my $page_size_in_kb = 4;
sub _get_mem_data
{
	my ($class, $pid) = @_;

	sysopen(my $fh, "/proc/$pid/statm", 0) or die $!;
	sysread($fh, my $line, 255) or die $!;
	close($fh);
	my ($vsz, $rss, $share, $text, $crap, $data, $crap2) = split(/\s+/, $line,  7);
	return map { $_ * $page_size_in_kb } ($vsz, $rss, $share, $text, $data);
}

=head2 Instance Methods

=over 4

=item record ( $message [, $pid ])

Record the memory usage at call time, logging it internally with the provided
message.  Optionally takes a process ID to record memory usage for, defaulting
to the current process.

=cut

sub record
{
	my ($self, $message, $pid) = @_;

	$pid ||= $$;

	push @$self, [
		time(),
		$message,
		$self->_get_mem_data($pid)
	];
}

=item report ( )

Generates report on memory usage.

=cut

sub report
{
	my ($self) = @_;

	my $report = sprintf "%6s %6s (%6s) %6s (%6s) %6s (%6s) %6s (%6s) %6s (%6s)\n",
		'time',
		'vsz',
		'diff',
		'rss',
		'diff',
		'shared',
		'diff',
		'code',
		'diff',
		'data',
		'diff';

	my $prev = [ undef, undef, 0, 0, 0, 0, 0 ];
	foreach (@$self) {
		$report .= sprintf "% 6d % 6d (% 6d) % 6d (% 6d) % 6d (% 6d) % 6d (% 6d) % 6d (% 6d) %s\n",
			($_->[0] - $self->[0][0]),
			$_->[2],
			($_->[2] - $prev->[2]),
			$_->[3],
			($_->[3] - $prev->[3]),
			$_->[4],
			($_->[4] - $prev->[4]),
			$_->[5],
			($_->[5] - $prev->[5]),
			$_->[6],
			($_->[6] - $prev->[6]),
			$_->[1];
		$prev = $_;
	}

	return $report;
}

=item dump ( )

Prints report on memory usage to stderr.


=cut

sub dump
{
	my ($self) = @_;

	print STDERR $self->report();
}

=item state ( )

Return arrayref of internal state.  Returned arrayref contains zero or more
references to arrays with the following columns (in order).  All sizes are in
kilobytes.

=over 4

=item timestamp (in seconds since epoch)

=item message (as passed to ->record())

=item virtual memory size

=item resident set size

=item shared memory size

=item text (aka code or exe) size

=item data and stack size

=back

=cut

sub state
{
	my ($self) = @_;

	return $self;
}

=pod

=back

=head1 AUTHOR

Dave O'Neill, C<< <dmo at dmo.ca> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-memory-usage at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Memory-Usage>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Memory::Usage

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Memory-Usage>

=item * Search CPAN

L<http://search.cpan.org/dist/Memory-Usage/>

=back


=head1 SEE ALSO

=over 4

=item * L<Memchmark>

Iteratively run coderefs (similar to L<Benchmark>) and compare their memory
usage.  Useful for optimizing specific subroutines with large memory usage once
you've identified them as memory hogs.  Uses same technique for gathering usage
information as this module, so accuracy and precision suffer similarly.

=item * L<Devel::Size>

Show the size of Perl variables.  Also useful for microoptimzations when you
know what you're looking for.  Looks at Perl internals, so accuracy is good.

=back


=head1 LICENSE AND COPYRIGHT

Copyright 2010 Dave O'Neill.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of Memory::Usage