我该如何修复这个解析linux smaps的perl脚本?

时间:2012-07-20 20:58:56

标签: linux perl memory

我找到this perl script that parses linux smaps,但似乎有些过时了:

#!/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
.

当我运行脚本时,我得到:

./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.

我正在运行perl 5和最新版本的Linux :: Smaps。我不是一个perl家伙,但我想知道这是否是一个简单的解决方案。此外,如果您知道类似的实用程序,我也很乐意听到。

1 个答案:

答案 0 :(得分:1)

该模块的来源有一条评论,揭示正在发生的事情:

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

这告诉我的是,即使您可能已正确实例化对象,仍未动态创建存取方法size。这可能是因为内核没有“揭示”完全填充对象所需的一切。

格式弃用消息是非致命的,但是当尚未创建方法时尝试从模块调用对象方法。

关于弃用邮件,请参阅Why isn't my Perl format working?