I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below:
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
Now I would like to save the "%Total" of each "CoreId" if it goes over (for example) 5. I need to put it in a list, and if the list size becomes 4, then I want to perform something. So far I am able to write the data only for the last core and it is not saving in the list, rather it is everytime just giving me the last number.
Here is the "Corestat_v1.1" code:
#!/bin/perl -w
#
# 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") ;
}
So, the code is running continuously. I am adding the list in the &print_stats() sub-function:
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:" ;
}
}
and the output I am getting is:
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
Notice the "size: " is always 1.
I think I am making some basic mistake in the concept.
Please help...