| 1 | package TUI::Gadgets::HeapView::Win32; | ||
| 2 | # ABSTRACT: on Windows, display the virtual memory used by the process | ||
| 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 Config; | ||
| 13 | use TUI::toolkit qw( :utils ); | ||
| 14 | use TUI::toolkit::Types qw( Object ); | ||
| 15 | use Win32::API; | ||
| 16 | |||
| 17 | use constant PTR_SIZE => $Config{ptrsize}; | ||
| 18 | use constant SIZE_T => | ||
| 19 | PTR_SIZE == 8 ? 'Q' | ||
| 20 | : PTR_SIZE == 4 ? 'L' | ||
| 21 | : die "Unrecognized ptrsize\n"; | ||
| 22 | |||
| 23 | # Set the size large enough for Windows 32 and 64 bit versions | ||
| 24 | use constant PROCESS_MEMORY_COUNTERS_EX_SIZE => ( 2 * 4 + 9 * PTR_SIZE ); | ||
| 25 | |||
| 26 | use constant { | ||
| 27 | # cb => 0, # DWORD | ||
| 28 | # PageFaultCount => 1, # DWORD | ||
| 29 | # PeakWorkingSetSize => 2, # SIZE_T | ||
| 30 | # WorkingSetSize => 3, # SIZE_T | ||
| 31 | # QuotaPeakPagedPoolUsage => 4, # SIZE_T | ||
| 32 | # QuotaPagedPoolUsage => 5, # SIZE_T | ||
| 33 | # QuotaPeakNonPagedPoolUsage => 6, # SIZE_T | ||
| 34 | # QuotaNonPagedPoolUsage => 7, # SIZE_T | ||
| 35 | # PagefileUsage => 8, # SIZE_T | ||
| 36 | # PeakPagefileUsage => 9, # SIZE_T | ||
| 37 | PrivateUsage => 10, # SIZE_T | ||
| 38 | }; | ||
| 39 | |||
| 40 | BEGIN { | ||
| 41 | # Load required Windows API functions | ||
| 42 | Win32::API::More->Import( 'kernel32', | ||
| 43 | 'HANDLE GetCurrentProcess()' | ||
| 44 | ) or die "Import GetCurrentProcess failed: $^E"; | ||
| 45 | |||
| 46 | Win32::API::More->Import( 'psapi', | ||
| 47 | 'BOOL GetProcessMemoryInfo( | ||
| 48 | HANDLE Process, | ||
| 49 | LPVOID ppsmemCounters, | ||
| 50 | DWORD cb | ||
| 51 | )' | ||
| 52 | ) or die "Import GetProcessMemoryInfo failed: $^E"; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub heapSize { # $total () | ||
| 56 | state $sig = signature( | ||
| 57 | method => Object, | ||
| 58 | pos => [], | ||
| 59 | ); | ||
| 60 | my ( $self ) = $sig->( @_ ); | ||
| 61 | alias: for my $totalStr ( $self->{heapStr} ) { | ||
| 62 | $totalStr = " No heap"; | ||
| 63 | |||
| 64 | # Get process handle | ||
| 65 | my $hProcess = GetCurrentProcess(); | ||
| 66 | return -1 unless defined $hProcess && $hProcess != -1; | ||
| 67 | |||
| 68 | # Create pmc buffer for PROCESS_MEMORY_COUNTERS_EX | ||
| 69 | my $buf = "\0" x PROCESS_MEMORY_COUNTERS_EX_SIZE; | ||
| 70 | |||
| 71 | # Call WinAPI to fill memory info | ||
| 72 | my $r = GetProcessMemoryInfo( $hProcess, $buf, | ||
| 73 | PROCESS_MEMORY_COUNTERS_EX_SIZE ); | ||
| 74 | return -1 unless $r; | ||
| 75 | |||
| 76 | # Unpack PROCESS_MEMORY_COUNTERS_EX | ||
| 77 | my @pmc = unpack( 'LL' . SIZE_T . '*', $buf ); | ||
| 78 | |||
| 79 | # Prepare display string similar to setw(12) | ||
| 80 | $totalStr = sprintf( "%12d", $pmc[PrivateUsage] ); | ||
| 81 | |||
| 82 | return $pmc[PrivateUsage]; | ||
| 83 | } | ||
| 84 | } #/ sub heapSize | ||
| 85 | |||
| 86 | 1 | ||
| 87 | |||
| 88 | __END__ |