Visit Our UNIX and Linux User Community


Save value from output of Corestat and save in a list for each core


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save value from output of Corestat and save in a list for each core
# 1  
Old 03-18-2015
Sun Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below:

Code:
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:

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:



Code:
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:

Code:
 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...

Previous Thread | Next Thread
Test Your Knowledge in Computers #433
Difficulty: Easy
JSON, or JavaScript Object Notation, is a general-purpose data interchange format that is defined as a subset of JavaScript's object literal syntax.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Save output in text C++

Hi , i want to save the output of my c ++ code to a text file which is in a particular path : this is part of my code and I dunno where I am doing it wrong do { for( int i = 0; i < l; ++i ) { std::cout << 1 + k * i + index << ' '; } ... (2 Replies)
Discussion started by: siya@
2 Replies

2. Shell Programming and Scripting

Save output to file - inside a script ?

I'm using the following script to check cisco router health and I'd like to save output to a file, vty_runcmd.sh > /check/check-cisco-health script works and output is saved to a file. However using it in crontab file is created but output is not printed inside it. In crontab, */5 * * * *... (4 Replies)
Discussion started by: marmellata
4 Replies

3. UNIX for Dummies Questions & Answers

Solved: how to save an output to a variable

i want to save the output of /scripts/whoowns domain.com to a username like $user = /scripts/whoowns domain.com but I'm not sure how to do that This is inside a bash script how can I get the output of /scripts/whoowns then save that to a variable? thanks! ---------- Post updated at... (0 Replies)
Discussion started by: vanessafan99
0 Replies

4. Shell Programming and Scripting

how can save output?

I make shell script by use "if" statement, what should add it to shell script save which I enter it in output file txt? ---------- Post updated at 08:27 AM ---------- Previous update was at 05:59 AM ---------- I mean like this echo "enter your name" read name # now i when start... (1 Reply)
Discussion started by: Oman_Member
1 Replies

5. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. Shell Programming and Scripting

Save output in a variable

Hi, I have a 3rd party tool on Solaris 8. I am running it thorugh my script but I am not able to capture its output into a variable. Like, I tried, but did not work. output=`/usr/bin/myTool` echo $output Also, I tried saving in a file but when I run, output is always shown on the... (19 Replies)
Discussion started by: angshuman_ag
19 Replies

7. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

8. AIX

topas -P save output

hi i have server monitor script which totally depends upon the output of TOP which works fine on HP. now i am told to put same script on aix i found that topas -P produces same output but it doesnt redirect output to file i have to kill/terminate it. is there any other way to this. as the... (0 Replies)
Discussion started by: zedex
0 Replies

9. UNIX for Dummies Questions & Answers

Save TCL Procedure Output

Hello Friend, In TCL i wrote a procedure as power, accepting two arguments and it was saved as a file named TCLProc.tcl. Now from unix, using shell script i want to run this procedure power,the output of this procedure should be saved in a text file.How can i do this.Please help me as early... (1 Reply)
Discussion started by: gjsaravanan
1 Replies

10. Solaris

terminal output - save to file?

I have a window open on my ultra 10 - a terminal window connecting to a server. Is there any way I can log all output to this window to a log file on my ultra 10 ? (2 Replies)
Discussion started by: frustrated1
2 Replies

Featured Tech Videos