首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Win32::PerfLib的Perl

使用Win32::PerfLib的Perl
EN

Stack Overflow用户
提问于 2014-05-24 23:00:41
回答 1查看 357关注 0票数 1

我正在尝试更好地理解Win32::PerfLib,并且我不能使用Win32::PerfMon。有两个我有疑问的例子:

第一个示例是来自CPAN的经典示例:

代码语言:javascript
运行
复制
use Win32::PerfLib;
  my $server = "";`enter code here`
  Win32::PerfLib::GetCounterNames($server, \%counter);
  %r_counter = map { $counter{$_} => $_ } keys %counter;
  # retrieve the id for process object
  $process_obj = $r_counter{Process};
  # retrieve the id for the process ID counter
  $process_id = $r_counter{'ID Process'};

  # create connection to $server
  $perflib = new Win32::PerfLib($server);
  $proc_ref = {};
  # get the performance data for the process object
  $perflib->GetObjectList($process_obj, $proc_ref);
  $perflib->Close();
  $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances};
  foreach $p (sort keys %{$instance_ref})
  {
      $counter_ref = $instance_ref->{$p}->{Counters};
      foreach $i (keys %{$counter_ref})
      {
          if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id)
          {
              printf( "% 6d %s\n", $counter_ref->{$i}->{Counter},
                      $instance_ref->{$p}->{Name}
                    );
          }
      }
  }

有没有人能深入解释一下第四行?我不明白我们为什么使用$_,也不明白它代表了什么,虽然我读到过,但在这种情况下,我不知道。此外,$counter{$_} => $_的含义是什么?

第二个问题来自这段代码,它从perfmon获取cpu %:

代码语言:javascript
运行
复制
use Win32::PerfLib;
($server) = @ARGV;
# only needed for PrintHash subroutine
#Win32::PerfLib::GetCounterNames($server, \%counter);

$processor = 238;
$proctime = 6;

$perflib = new Win32::PerfLib($server);
$proc_ref0 = {};
$proc_ref1 = {};
$perflib->GetObjectList($processor, $proc_ref0);
sleep 5;
$perflib->GetObjectList($processor, $proc_ref1);
$perflib->Close();
$instance_ref0 = $proc_ref0->{Objects}->{$processor}->{Instances};
$instance_ref1 = $proc_ref1->{Objects}->{$processor}->{Instances};
foreach $p (keys %{$instance_ref0})
{
    $counter_ref0 = $instance_ref0->{$p}->{Counters};
    $counter_ref1 = $instance_ref1->{$p}->{Counters};
    foreach $i (keys %{$counter_ref0})
    {
    next if $instance_ref0->{$p}->{Name} eq "_Total";
    if($counter_ref0->{$i}->{CounterNameTitleIndex} == $proctime)
    {
        $Numerator0 = $counter_ref0->{$i}->{Counter};
        $Denominator0 = $proc_ref0->{PerfTime100nSec};
        $Numerator1 = $counter_ref1->{$i}->{Counter};
        $Denominator1 = $proc_ref1->{PerfTime100nSec};
        $proc_time{$p} = (1- (($Numerator1 - $Numerator0) /
                  ($Denominator1 - $Denominator0 ))) * 100;
        printf "Instance $p: %.2f\%\n", $proc_time{$p};
    }
    }
}

为什么程序员必须使用两次"GetObjectList“方法,并将睡眠方法放在它们之间?为什么我们不能像perfmon展示的那样,只取cpu百分比,然后我们必须进行所有这些计算?

提前谢谢你,Fam Pam。

EN

回答 1

Stack Overflow用户

发布于 2014-05-26 19:24:20

在此代码中:

代码语言:javascript
运行
复制
  Win32::PerfLib::GetCounterNames($server, \%counter);
  %r_counter = map { $counter{$_} => $_ } keys %counter;

您正在%counter哈希中存储性能数据。本例中的映射创建了一个反向散列,其中较早的值成为键。

示例:

代码语言:javascript
运行
复制
from apple => 'fruit' to fruit => 'apple
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23846460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档