1

我发现了这个解析 linux smaps 的 perl 脚本,但它似乎有点过时了:

#!/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 人,但我想知道这是否是一个简单的解决方法。另外,如果您知道类似的实用程序,我也很想听听。

4

1 回答 1

1

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

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

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

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

至于弃用消息,请参阅为什么我的 Perl 格式不起作用?

于 2012-07-20T21:11:24.787 回答