从Corestat的输出中保存值并保存在每个核心的列表中

时间:2015-03-18 20:30:43

标签: perl arraylist

我正在尝试修改" corestat v1.1" Perl中的代码。此代码的典型输出如下:

     Core Utilization          
CoreId     %Usr     %Sys     %Total 
------    -----     -----    ------ 
  5        4.91      0.01      4.92 
  6        0.06      0.02      0.08 
  7        0.04      0.04      0.08 
------    -----     -----    ------ 
 Avg       1.67      0.02      1.69

现在我想保存"%Total"每个" CoreId"如果它结束(例如)5。我需要将它放在一个列表中,如果列表大小变为4,那么我想执行一些操作。到目前为止,我只能为最后一个核心编写数据,而不是保存在列表中,而是每次只给我最后一个数字。 这是" Corestat_v1.1"代码:

    #!/bin/perl -w

#################################
#
#  CDDL HEADER START
#
#  The contents of this file are subject to the terms
#  of the Common Development and Distribution License
#  (the "License").  You may not use this file except
#  in compliance with the License.
#
#  You can obtain a copy of the license at
#  src/OPENSOLARIS.LICENSE
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing
#  permissions and limitations under the License.
#
#  When distributing Covered Code, include this CDDL
#  HEADER in each file and include the License file at
#  usr/src/OPENSOLARIS.LICENSE.  If applicable,
#  add the following below this CDDL HEADER, with the
#  fields enclosed by brackets "[]" replaced with your
#  own identifying information: Portions Copyright [yyyy]
#  [name of copyright owner]
#
#  CDDL HEADER END
#
#  Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
#  Use is subject to license terms.
#
#################################
#
#  Corestat version 1.1
#
#  Typical input line will look like :
#  20.024   1  tick    231640  53787286  # pic0=L2_imiss,pic1=Instr_cnt,nouser,sys
#  OR
#  18.006   0  tick     79783  27979245  # pic1=Instr_cnt,pic0=L2_dmiss_ld,nouser,sys
#
#  Note : 
#  1. Version 1.1 supports only UltraSPARC T1 processor
#  2. For offline analysis based on cpustat data, use following command
#     to capture the data : 
#     cpustat -n -c pic0=L2_dmiss_ld,pic1=Instr_cnt -c pic0=L2_dmiss_ld,pic1=Instr_cnt,nouser,sys 1
# 
#################################

##################################
# Define constants here 
*DEFAULT_FREQUENCY = \1400 ; # 1400 MHz
*DEFAULT_INTERVAL = \1 ; # 1 sec 
##################################

$frequency = $DEFAULT_FREQUENCY ;
$interval = $DEFAULT_INTERVAL ;

&clear_stats () ; # Initialize 

$flag = " " ;
$val = " " ;

if ($#ARGV >= 0) {
  while ($#ARGV > -1) {
    $arg = shift @ARGV;
    if ( $arg =~ /^-(.)(.*)$/ ) {
      $flag=$1; 
      if ( $flag eq "f" ) {
        $val = shift @ARGV;
        if ($val) { $fname = $val ;}
        else { &print_usage () ; exit ; } 
      } elsif ( $flag eq "i" ) {
        $val = shift @ARGV;
        if ($val) { $interval = $val ;}
        else { &print_usage () ; exit ; } 
      } elsif ( $flag eq "r" ) {
        $val = shift @ARGV;
        if ($val) { $frequency = $val ;}
        else { &print_usage () ; exit ; } 
      } elsif ( $flag eq "h" ) { 
        &print_usage () ; 
        exit ; 
      } elsif ($flag eq "v" ) {
        &print_version () ;
        exit ;
      } else {
        printf ("$0 : Invalid option -%s\n", $flag) ;
        &print_usage () ;
        exit ;
      }
    }
    else {
      printf ("$0 : Invalid option %s\n", $arg) ;
      &print_usage () ;
      exit ;
    }
  }
}

if (($frequency < 1000) || ($frequency > 1400)) {
  printf ("$0 : Invalid frequency - %d MHz \n", $frequency) ;
  &print_usage () ;
  exit ;
} else { $max_mips = $frequency*1000*1000 ;}

# if ($interval < 10) {
#  $interval = 10 ; # Minimum reporting interval

#}


if ($fname) { open (fd_in, $fname) || die ("Cannot open $fname") ; }
else {
  open (fd_in, "/bin/id |") || die "Can't fork : $!" ;
  $line = <fd_in> ;
  close (fd_in) ;
  if ($line =~ m/uid=0\(root\)/i) {
    open (fd_in, "priocntl -e -c RT cpustat -n -c pic0=L2_dmiss_ld,pic1=Instr_cnt -c pic0=L2_dmiss_ld,pic1=Instr_cnt,nouser,sys 1 2>&1  |") || die "Can't fork : $!" ;
  } else {
    printf ("$0 : Permission denied. Needs root privilege... \n") ;
    &print_usage () ;
    exit ;
  }
}

while ($line = <fd_in>) { 
  $line = " " . $line ;
  @list = split (/\s+/, $line) ;
#print "after_split:@li
  $len = $#list ;

  if (($len >= 7) && ($list[3] ne "total")) { # Ignore header and totals
    $cpu_id = $list[2] ;
  #  print "$list[0]\t$list[1]\t$list[2]\t$list[3]\t$list[4]\n";
  #  print "cpu_id: $cpu_id\n";
    $pic0 = $list[4] ;
  #  print "pic0: $pic0\n";
    $pic1 = $list[5] ;

    # Detect mode for which data is collected

    if ($list[7] =~ m/nouser,sys/i) {
      $mode = 1 ; # system mode
      $sys_mode = 1 ;
    }elsif ($list[7] =~ m/sys/i) {
      $mode = 2 ; # Total time
      $tot_mode = 1 ; 
    }else {
      $mode = 0 ; # User mode
      $usr_mode = 1 ;
    }

    # Detect which counter holds instruction count
    if ($list[7] =~ m/pic1=Instr_cnt/i) {
      $instr_ctr = $pic1 ;
    } else {
      $instr_ctr = $pic0 ;
    }
    if ($cpu_stat_pic1[$cpu_id][$mode] == 0) {
      $cpu_stat_pic1[$cpu_id][$mode] = $instr_ctr ;
      $unique_events ++ ;
    } 
    else { # Save minsamples
        if ($interval==1) {
      $minsamples=$unique_events * ($interval*1000)/($usr_mode+$sys_mode+$tot_mode) ;
         }
        else $minsamples = $unique_events * ($interval-($interval%($usr_mode+$sys_mode+$tot_mode))) / ($usr_mode+$sys_mode+$tot_mode) ;
      if ($nsamples == $minsamples) {
        &print_stats () ;
        &clear_stats () ;
        $cpu_stat_pic1[$cpu_id][$mode] = $instr_ctr ; # Save the current sample
        $unique_events++ ;
      } else {
         $cpu_stat_pic1[$cpu_id][$mode] = ($cpu_stat_pic1[$cpu_id][$mode] + $instr_ctr)/2 ;
      }
    }
    $nsamples++ ;
  }
}

&print_stats () ;

sub clear_stats () {
  $cpu = 0 ;
  $m = 0 ;
  $nsamples = 0 ;
  $minsamples = 0 ;
  $usr_mode = 0 ;
  $sys_mode = 0 ;
  $tot_mode = 0 ;
  $unique_events = 0 ;

  while ($cpu < 32) {
    while ($m < 3) {
      $cpu_stat_pic1[$cpu][$m] = 0 ;
      $core_stat_pic1[$cpu/4][$m] = 0 ;
      $m++ ;
    }
    $m = 0 ;
    $cpu++ ;
  }
  $core_avg_pic1[0] = 0 ;
  $core_avg_pic1[1] = 0 ;
  $core_avg_pic1[2] = 0 ;
}

sub print_stats () {
  $core_id = 0 ;
  $cpu = 0 ;
  $ncores = 0 ;
  $header = 1 ;

  # Process sequentially
  while ($cpu < 32) {
    $core_id = $cpu/4 ;
    $core_stat_pic1[$core_id][0] += $cpu_stat_pic1[$cpu][0] ;
    $core_stat_pic1[$core_id][1] += $cpu_stat_pic1[$cpu][1] ;
    if (($cpu+1) % 4 == 0) {
      if ($core_stat_pic1[$core_id][0] || $core_stat_pic1[$core_id][1]) {
        if ($header) {
          &print_header () ;
          $header = 0 ;
        }
        printf ("  %d       %5.2f     %5.2f     %5.2f \n", $core_id, $core_stat_pic1[$core_id][0]*100/$max_mips, $core_stat_pic1[$core_id][1]*100/$max_mips, $core_stat_pic1[$core_id][0]*100/$max_mips + $core_stat_pic1[$core_id][1]*100/$max_mips) ;

    $core_avg_pic1[0] += $core_stat_pic1[$core_id][0]*100/$max_mips ;   
    $core_avg_pic1[1] += $core_stat_pic1[$core_id][1]*100/$max_mips ;   
        if (util > 0.1) {
            while ($m <6)
                $x_core_util[$core_id][$m] = $core_stat_pic1[$core_id][0]*100/$max_mips + $core_stat_pic1[$core_id][1]*100/$max_mips;
                }
            else continue;
    $ncores++ ;
      }
    }
    $cpu++ ;
  }
  if ($core_avg_pic1[0] || $core_avg_pic1[1]) {
    printf ("------    -----     -----    ------ \n") ;
    printf (" Avg      %5.2f     %5.2f     %5.2f \n", $core_avg_pic1[0]/$ncores, $core_avg_pic1[1]/$ncores, $core_avg_pic1[0]/$ncores + $core_avg_pic1[1]/$ncores) ;
  }
}

sub print_header () {
  printf ("\n") ;
  printf ("        Core Utilization          \n") ;
  printf ("CoreId     %%Usr     %%Sys     %%Total \n") ;
  printf ("------    -----     -----    ------ \n") ;
}

sub print_version () {
  printf ("Corestat : Version 1.1 \n") ;
}

sub print_usage () {
  printf ("\n") ;
  printf ("Usage : corestat [-v] [[-f <infile>] [-i <interval>] [-r <freq>]] \n\n") ;
  printf ("                  -v          : Report version number \n") ;
  printf ("                  -f infile   : Filename containing sampled cpustat data \n") ;
  printf ("                  -i interval : Reporting interval in sec \(default = 10 sec\)\n") ;
  printf ("                  -r freq     : Processor frequency in MHz \(default = 1200 MHz\)\n") ;
  printf ("\n") ;
}

因此,代码不断运行。我在&amp; print_stats()子函数中添加了列表:

    sub print_stats () {
  $core_id = 0 ;
  $cpu = 0 ;
  $ncores = 0 ;
  $header = 1 ;

  # Process sequentially
  while ($cpu < 32) {
    $core_id = $cpu/4 ;
    $core_stat_pic1[$core_id][0] += $cpu_stat_pic1[$cpu][0] ;
    $core_stat_pic1[$core_id][1] += $cpu_stat_pic1[$cpu][1] ;
    if (($cpu+1) % 4 == 0) {
      if ($core_stat_pic1[$core_id][0] || $core_stat_pic1[$core_id][1]) {
        if ($header) {
          &print_header () ;
          $header = 0 ;
        }
        printf ("  %d       %5.2f     %5.2f     %5.2f \n", $core_id, $core_stat_pic1[$core_id][0]*100/$max_mips, $core_stat_pic1[$core_id][1]*100/$max_mips, $core_stat_pic1[$core_id][0]*100/$max_mips + $core_stat_pic1[$core_id][1]*100/$max_mips) ;
        $core_avg_pic1[0] += $core_stat_pic1[$core_id][0]*100/$max_mips ;
        $core_avg_pic1[1] += $core_stat_pic1[$core_id][1]*100/$max_mips ;
        $average_util = ($core_stat_pic1[$core_id][0]*100/$max_mips + $core_stat_pic1[$core_id][1]*100/$max_mips);
        $ncores++ ;
        if ($average_util > 0.1)
        {
        &decision ($average_util,$core_id);
                }

        }
    }
    $cpu++ ;
  }
  if ($core_avg_pic1[0] || $core_avg_pic1[1]) {
    printf ("------    -----     -----    ------ \n") ;
    printf (" Avg      %5.2f     %5.2f     %5.2f \n", $core_avg_pic1[0]/$ncores, $core_avg_pic1[1]/$ncores, $core_avg_pic1[0]/$ncores + $core_avg_pic1[1]/$ncores) ;


        }


}
sub decision () {
         @x_list = @_;
        print @x_list;
        @x_average_util = ($_[0]);
        @x_core_id = ($_[1]);
        $size_avg_util = scalar (@x_average_util);
        print "size: $size_avg_util\n";
        if ($size_avg_util>=  5)
        {
        printf "Utilization over: 5:"  ;

        }
        }

我得到的输出是:

 Core Utilization          
CoreId     %Usr     %Sys     %Total 
------    -----     -----    ------ 
  5        0.04      0.02      0.07 
  6        0.03      0.01      0.04 
  7        0.01      0.01      0.02 
------    -----     -----    ------ 
 Avg       0.03      0.01      0.04 
min_samples:48  unique_sample: 24

        Core Utilization          
CoreId     %Usr     %Sys     %Total 
------    -----     -----    ------ 
  5        0.01      0.02      0.03 
  6        0.02      0.04      0.06 
  7        0.03      0.01      0.04 
------    -----     -----    ------ 
 Avg       0.02      0.02      0.04 
min_samples:48  unique_sample: 24

        Core Utilization          
CoreId     %Usr     %Sys     %Total 
------    -----     -----    ------ 
  5        0.01      0.02      0.03 
  6        0.04      0.02      0.06 
  7        0.01      0.01      0.01 
------    -----     -----    ------ 
 Avg       0.02      0.01      0.03 
min_samples:48  unique_sample: 24

        Core Utilization          
CoreId     %Usr     %Sys     %Total 
------    -----     -----    ------ 
  5        0.01      0.02      0.03 
  6        0.04      0.31      0.35 
0.3451689285714296.75size: 1
  7        0.18      0.01      0.19 
0.1929574285714297.75size: 1
------    -----     -----    ------ 
 Avg       0.08      0.11      0.19 
^C

注意&#34;尺寸:&#34;永远是1.: - (

请帮忙......

0 个答案:

没有答案