| 1 | package TUI::Gadgets::HeapView::Linux; | ||
| 2 | # ABSTRACT: Use the data stored in /proc/self/statm to monitor memory usage | ||
| 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 POSIX qw( sysconf _SC_PAGESIZE ); | ||
| 13 | |||
| 14 | use TUI::toolkit qw( :utils ); | ||
| 15 | use TUI::toolkit::Types qw( Object ); | ||
| 16 | |||
| 17 | use constant PAGE_SIZE_KB => eval { sysconf( _SC_PAGESIZE ) / 1024 } || 4; | ||
| 18 | |||
| 19 | sub heapSize { | ||
| 20 | state $sig = signature( | ||
| 21 | method => Object, | ||
| 22 | pos => [], | ||
| 23 | ); | ||
| 24 | my ( $self ) = $sig->( @_ ); | ||
| 25 | alias: for my $totalStr ( $self->{heapStr} ) { | ||
| 26 | |||
| 27 | state $next_update = 0; | ||
| 28 | state $bytes = -1; | ||
| 29 | |||
| 30 | my $now = time; | ||
| 31 | if ( $now < $next_update ) { | ||
| 32 | $totalStr = sprintf( "%12d", $bytes ) | ||
| 33 | if $bytes >= 0; | ||
| 34 | return $bytes; | ||
| 35 | } | ||
| 36 | $next_update = $now + 1; | ||
| 37 | |||
| 38 | $totalStr = " No heap"; | ||
| 39 | |||
| 40 | open my $fh, '<', '/proc/self/statm' or return -1; | ||
| 41 | my ( undef, $resident ) = split ' ', <$fh>; | ||
| 42 | close $fh; | ||
| 43 | return -1 unless $resident; | ||
| 44 | |||
| 45 | $bytes = $resident * PAGE_SIZE_KB * 1024; | ||
| 46 | $totalStr = sprintf( "%12d", $bytes ); | ||
| 47 | |||
| 48 | return $bytes; | ||
| 49 | } #/ alias: | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | |||
| 54 | =pod | ||
| 55 | |||
| 56 | =head1 NAME | ||
| 57 | |||
| 58 | TUI::Gadgets::HeapView::Linux - Linux heap usage backend for HeapView | ||
| 59 | |||
| 60 | =head1 SYNOPSIS | ||
| 61 | |||
| 62 | use TUI::Gadgets::HeapView::Linux; | ||
| 63 | |||
| 64 | my $total = TUI::Gadgets::HeapView::Linux->heapSize(); | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | This module provides the Linux-specific implementation used by | ||
| 69 | L<THeapView|TUI::Gadgets::HeapView> to retrieve memory usage information. | ||
| 70 | |||
| 71 | On Linux systems, heap usage is derived from the process virtual memory | ||
| 72 | statistics provided by the operating system. This module encapsulates the | ||
| 73 | platform-specific logic required to obtain that information. | ||
| 74 | |||
| 75 | The module is not intended to be used directly by application code. | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =head2 heapSize | ||
| 80 | |||
| 81 | my $total = TUI::Gadgets::HeapView::Linux->heapSize(); | ||
| 82 | |||
| 83 | Returns the amount of memory currently used by the process. | ||
| 84 | |||
| 85 | On Linux, the value is obtained from C</proc/self/statm>. | ||
| 86 | |||
| 87 | The returned value is expressed in bytes. | ||
| 88 | |||
| 89 | =head1 SEE ALSO | ||
| 90 | |||
| 91 | L<THeapView|TUI::Gadgets::HeapView> | ||
| 92 | |||
| 93 | =head1 AUTHORS | ||
| 94 | |||
| 95 | =over | ||
| 96 | |||
| 97 | =item * Borland International (original Turbo Vision design) | ||
| 98 | |||
| 99 | =item * J. Schneider <brickpool@cpan.org> (Perl implementation and maintenance) | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =head1 COPYRIGHT AND LICENSE | ||
| 104 | |||
| 105 | Copyright (c) 1990-1994, 1997 by Borland International | ||
| 106 | |||
| 107 | Copyright (c) 2026 the L</AUTHORS> as listed above. | ||
| 108 | |||
| 109 | This software is licensed under the MIT license (see the LICENSE file, which is | ||
| 110 | part of the distribution). | ||
| 111 | |||
| 112 | =cut |