简体   繁体   中英

How can I fix this perl script that parses linux smaps?

I found this perl script that parses linux smaps , but it seems a bit out of date:

#!/usr/bin/perl

# Copyright Ben Maurer
# you can distribute this under the MIT/X11 License

use Linux::Smaps;

my $pid=shift @ARGV;
unless ($pid) {
        print "./smem.pl <pid>\n";
        exit 1;
}
my $map=Linux::Smaps->new($pid);
my @VMAs = $map->vmas;

format STDOUT =
VMSIZE:  @######## kb
$map->size
RSS:     @######## kb total
$map->rss
         @######## kb shared
$map->shared_clean + $map->shared_dirty
         @######## kb private clean
$map->private_clean
         @######## kb private dirty
$map->private_dirty
.

write;

printPrivateMappings ();
printSharedMappings ();

sub sharedMappings () {
    return grep { ($_->shared_clean  + $_->shared_dirty) > 0 } @VMAs;
}

sub privateMappings () {
    return grep { ($_->private_clean  + $_->private_dirty) > 0 } @VMAs;
}

sub printPrivateMappings ()
{
    $TYPE = "PRIVATE MAPPINGS";
    $^ = 'SECTION_HEADER';
    $~ = 'SECTION_ITEM';
    $- = 0;
    $= = 100000000;
    foreach  $vma (sort {-($a->private_dirty <=> $b->private_dirty)}
                                   privateMappings ()) {
        $size  = $vma->size;
        $dirty = $vma->private_dirty;
        $clean = $vma->private_clean;
        $file  = $vma->file_name;
        write;
    }
}

sub printSharedMappings ()
{
    $TYPE = "SHARED MAPPINGS";
    $^ = 'SECTION_HEADER';
    $~ = 'SECTION_ITEM';
    $- = 0;
    $= = 100000000;

    foreach  $vma (sort {-(($a->shared_clean + $a->shared_dirty)
                           <=>
                           ($b->shared_clean + $b->shared_dirty))}
                   sharedMappings ()) {

        $size  = $vma->size;
        $dirty = $vma->shared_dirty;
        $clean = $vma->shared_clean;
        $file  = $vma->file_name;
        write;


    }
}

format SECTION_HEADER =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$TYPE
@>>>>>>>>>> @>>>>>>>>>>  @>>>>>>>>>   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"vmsize" "rss clean" "rss dirty" "file"
.

format SECTION_ITEM =
@####### kb @####### kb @####### kb   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$size $clean $dirty $file
.

When I run the script I get:

./smem.perl 121212
Use of comma-less variable list is deprecated at ./smem.perl line 86.
Use of comma-less variable list is deprecated at ./smem.perl line 86.
Use of comma-less variable list is deprecated at ./smem.perl line 86.
Use of comma-less variable list is deprecated at ./smem.perl line 91.
Use of comma-less variable list is deprecated at ./smem.perl line 91.
Use of comma-less variable list is deprecated at ./smem.perl line 91.
Can't locate object method "size" via package "Linux::Smaps" at ./smem.perl line 18.

I'm running perl 5 and the latest version of Linux::Smaps. I'm not a perl guy, but I'm wondering if this is an easy fix. Also if you know of a similar utility I'd love to hear about that, too.

The module's source has a comment that reveals what's going on:

It creates accessor methods dynamically depending on what the kernel reveals.

What that tells me is that even though you may have instantiated the object correctly, the accessor method size hasn't been dynamically created yet. That could be a result of the kernel not "revealing" everything needed to fully populate the object.

The format deprecation messages are non-fatal, but trying to invoke an object method from a module when the method hasn't been created yet is.

As for the deprecation messages, see Why isn't my Perl format working?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM