lib/TUI/Gadgets/HeapView/Unix.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 48 CC 4 SCAR 23.0
CC
4
Cov
0%
heapSize
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 10.0
1 package TUI::Gadgets::HeapView::Unix;
2 # ABSTRACT: Determine memory usage based on the 'ps' command
3
4 use 5.010;
5 use strict;
6 use warnings;
7
8 our $VERSION = '2.000002';
9 $VERSION =~ tr/_//d;
10 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 use TUI::toolkit qw( :utils );
13 use TUI::toolkit::Types qw( Object );
14
15 sub heapSize {
16 state $sig = signature(
17 method => Object,
18 pos => [],
19 );
20 my ( $self ) = $sig->( @_ );
21 alias: for my $totalStr ( $self->{heapStr} ) {
22
23 state $next_update = 0;
24 state $bytes = -1;
25
26 my $now = time;
27 if ( $now < $next_update ) {
28 $totalStr = sprintf( "%12d", $bytes )
29 if $bytes >= 0;
30 return $bytes;
31 }
32 $next_update = $now + 1;
33
34 $totalStr = " No heap";
35
36 my $kb = qx(ps -o rss= -p $$ 2>/dev/null);
37 $kb =~ s/\D//g;
38 return -1 unless $kb;
39
40 $bytes = $kb * 1024;
41 $totalStr = sprintf( "%12d", $bytes );
42
43 return $bytes;
44 } #/ alias:
45 }
46
47 1;
48
49 =pod
50
51 =head1 NAME
52
53 TUI::Gadgets::HeapView::Unix - Unix heap usage backend for HeapView
54
55 =head1 SYNOPSIS
56
57 use TUI::Gadgets::HeapView::Unix;
58
59 my $total = TUI::Gadgets::HeapView::Unix->heapSize();
60
61 =head1 DESCRIPTION
62
63 This module provides the Unix-specific implementation used by
64 L<THeapView|TUI::Gadgets::HeapView> to retrieve memory usage information.
65
66 On Unix systems, memory usage is obtained from the operating system by
67 querying the resident set size (RSS) of the current process using the
68 C<ps> command.
69
70 The module is not intended to be used directly by application code.
71
72 =head1 METHODS
73
74 =head2 heapSize
75
76 my $total = TUI::Gadgets::HeapView::Unix->heapSize();
77
78 Returns the amount of resident memory currently used by the process.
79
80 The value is derived from the RSS (resident set size) reported by the
81 operating system and is returned in bytes.
82
83 To reduce system overhead, the value is cached for up to one second
84 before being refreshed.
85
86 =head1 SEE ALSO
87
88 L<THeapView|TUI::Gadgets::HeapView>
89
90 =head1 AUTHORS
91
92 =over
93
94 =item * Borland International (original Turbo Vision design)
95
96 =item * J. Schneider <brickpool@cpan.org> (Perl implementation and maintenance)
97
98 =back
99
100 =head1 COPYRIGHT AND LICENSE
101
102 Copyright (c) 1990-1994, 1997 by Borland International
103
104 Copyright (c) 2026 the L</AUTHORS> as listed above.
105
106 This software is licensed under the MIT license (see the LICENSE file, which is
107 part of the distribution).
108
109 =cut