#
# 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 usr/src/OPENSOLARIS.LICENSE
# 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
#
#
#
#
# pgstat - tool for displaying Processor Group statistics
#
use warnings;
use strict;
use Errno;
#
# Constants section
#
# It is possible that wnen trying to parse PG kstats, PG generation changes
# which will cause PG new method to fail with errno set to EAGAIN In this case
# we retry open up to RETRY_COUNT times pausing RETRY_DELAY seconds between each
# retry.
#
# When printing PGs we print them as a little tree with each PG shifted by
# LEVEL_OFFSET from each parent. For example:
#
# PG RELATIONSHIP CPUs
# 0 System 0-7
# 3 Socket 0 2 4 6
# 2 Cache 0 2 4 6
#
#
# DEFAULT_INTERVAL - interval in seconds between snapshot if none is specified
# DEFAULT_COUNT - Number of iterations if none is specified
# HWLOAD_UNKNOWN - Value that we use to represent unknown hardware load
# HWLOAD_UNDEF - Value that we use to represent undefined hardware load
#
use constant {
VERSION => 1.1,
DEFAULT_INTERVAL => 1,
DEFAULT_COUNT => 1,
RETRY_COUNT => 4,
RETRY_DELAY => 0.25,
HWLOAD_UNKNOWN => -1,
HWLOAD_UNDEF => -2,
LEVEL_OFFSET => 1,
};
#
# Format for fields, showing percentage headers
#
my $pcnt_fmt = "%6s";
#
# Format for percentages field
#
my $pcnt = "%5.1f";
#
# Return codes
#
# 0 Successful completion.
#
# 1 An error occurred.
#
# 2 Invalid command-line options were specified.
#
use constant {
E_SUCCESS => 0,
E_ERROR => 1,
E_USAGE => 2,
};
#
# Valid sort keys for -s and -S options
#
# Set message locale
# Get script name for error messages
my @pg_list; # -P pg,... - PG arguments
my @cpu_list; # -c cpu,... - CPU arguments
my @sharing_filter_neg; # -R string,... - Prune PGs
my @sharing_filter; # -r string,... - Matching sharing names
my $do_aggregate; # -A - Show summary in the end
my $do_cpu_utilization; # -C - Show per-CPU utilization
my $do_physical; # -p - Show physical relationships
my $do_timestamp; # -T - Print timestamp
my $do_usage; # -h - Show usage
my $do_version; # -V - Verbose output
my $show_top; # -t - show top N
my $sort_order_a; # -S key - Ascending sort order
my $sort_order_d; # -s key - Descending sort order
my $verbose; # -v - Verbose output;
$verbose = 0;
# Parse options from the command line
"cpus|c=s" => \@cpu_list,
"showcpu|C" => \$do_cpu_utilization,
"help|h|?" => \$do_usage,
"pgs|P=s" => \@pg_list,
"physical|p" => \$do_physical,
"relationship|r=s" => \@sharing_filter,
"norelationship|R=s" => \@sharing_filter_neg,
"sort|s=s" => \$sort_order_d,
"Sort|S=s" => \$sort_order_a,
"top|t=i" => \$show_top,
"timestamp|T=s" => \$do_timestamp,
"version|V" => \$do_version,
"verbose+" => \$verbose,
"v+" => \$verbose,
# Print usage message when -h is given
if ($do_version) {
exit(E_SUCCESS);
}
#
# Verify options
#
# -T should have either u or d argument
$cmdname, $do_timestamp;
}
if ($sort_order_a && $sort_order_d) {
$cmdname;
}
$cmdname;
}
#
# Figure out requested sorting of the output
# By default 'depth-first' is used
#
my $sort_key;
my $sort_reverse;
if (!($sort_order_a || $sort_order_d)) {
$sort_key = 'depth';
$sort_reverse = 1;
} else {
$sort_reverse = defined($sort_order_d);
}
#
# Make sure sort key is valid
#
}
#
# Convert -[Rr] string1,string2,... into list (string1, string2, ...)
#
@sharing_filter = map { split /,/ } @sharing_filter;
@sharing_filter_neg = map { split /,/ } @sharing_filter_neg;
#
# We use two PG snapshot to compare utilization between them. One snapshot is
# kept behind another in time.
#
-swload => 1,
-tags => $do_physical,
-retry => RETRY_COUNT,
-delay => RETRY_DELAY);
if (!$p) {
printf STDERR
gettext("%s: can not obtain Processor Group information: $!\n"),
$cmdname;
exit(E_ERROR);
}
my $p_initial = $p;
-swload => 1,
-tags => $do_physical,
-retry => RETRY_COUNT,
-delay => RETRY_DELAY);
if (!$p_dup) {
printf STDERR
gettext("%s: can not obtain Processor Group information: $!\n"),
$cmdname;
exit(E_ERROR);
}
#
# Get interval and count
#
my $count = DEFAULT_COUNT;
my $interval = DEFAULT_INTERVAL;
if (scalar @ARGV > 0) {
if (scalar @ARGV > 0) {
} else {
$count = 0;
}
}
if (! ($interval=~ m/^\d+\.?\d*$/)) {
printf STDERR
gettext("%s: Invalid interval %s - should be numeric\n"),
}
printf STDERR
gettext("%s: Invalid count %s - should be numeric\n"),
}
#
# Get list of all PGs
#
my @all_pgs = $p->all_depth_first();
#
# get list of all CPUs in the system by looking at the root PG cpus
#
# PGs to work with
#
# Convert CPU and PG lists into proper Perl lists, converting things like
# 1-3,5 into (1, 2, 3, 5). Also convert 'all' into the list of all CPUs or PGs
#
@cpu_list =
map { split /,/ } @cpu_list; # x,y -> (x, y)
# Same drill for PGs
@pg_list =
map { $_ eq 'all' ? @all_pgs : $_ }
map { split /,/ } @pg_list;
#
# Convert CPU list to list of PGs
#
if (scalar @cpu_list) {
#
# Warn about any invalid CPU IDs in the arguments
# @bad_cpus is a list of invalid CPU IDs
#
if (scalar @bad_cpus) {
printf STDERR
gettext("%s: Invalid processor IDs %s\n"),
}
#
# Find all PGs which have at least some CPUs from @cpu_list
#
my @pgs_from_cpus = grep {
} @all_pgs;
# Combine PGs from @pg_list (if any) with PGs we found
}
#
# If there are any PGs specified by the user, complain about invalid ones
#
if (scalar @pg_list > 0) {
#
# Warn about any invalid PG
# @bad_pgs is a list of invalid CPUs in the arguments
#
if (scalar @bad_pgs) {
printf STDERR
gettext("%s: warning: invalid PG IDs %s\n"),
}
}
# Do we have any PGs left?
if (scalar(@pgs) == 0) {
printf STDERR
gettext("%s: No processor groups matching command line arguments\n"),
$cmdname;
exit(E_USAGE);
}
#
# Set $do_levels if we should provide output identation by level It doesn't make
# sense to provide identation if PGs are sorted not in topology order.
#
#
# %name_of_pg hash keeps sharing name, possibly with physical tags appended to
# it for each PG.
#
my %name_of_pg;
#
# For calculating proper offsets we need to know minimum and maximum level for
# all PGs
#
my $max_sharename_len = length('RELATIONSHIP');
my $maxlevel;
my $minlevel;
if ($do_levels) {
}
#
# Walk over all PGs and find out the string length that we need to represent
# sharing name + physical tags + indentation level.
#
if ($do_physical) {
}
}
# Maximum length of PG ID field
#
#
# %pgs hash contains various statistics per PG that is used for sorting.
my %pgs;
# Total number of main loop iterations we actually do
my $total_iterations = 0;
#
# For summary, keep track of minimum and maximum data per PG
#
my $history;
#
# Provide summary output when aggregation is requested and user hits ^C
#
######################################################################
# Main loop
###########
#
# Print timestamp if -T is specified
#
if ($do_timestamp) {
if ($do_timestamp eq 'u') {
print time(), "\n";
} else {
localtime;
print "$date_str\n";
}
}
#
# Wait for the requested interval
#
select(undef, undef, undef, $interval);
#
# Print headers
# There are two different output formats - one regular and one verbose
#
if (!$verbose) {
printf "%-${max_pg_len}s %-${max_sharename_len}s ".
"$pcnt_fmt $pcnt_fmt %-s\n",
'PG', 'RELATIONSHIP', 'HW', 'SW', 'CPUS';
} else {
printf "%-${max_pg_len}s %-${max_sharename_len}s" .
" $pcnt_fmt %4s %4s $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt %s\n",
'PG','RELATIONSHIP',
'HW', 'UTIL', 'CAP',
'SW', 'USR', 'SYS', 'IDLE', 'CPUS';
}
#
# Update the data in one of the snapshots
#
#
# Do not show offlined CPUs
#
my @online_cpus = $p->online_cpus();
#
# Check whether both snapshots belong to the same generation
#
printf gettext("Configuration changed!\n");
# Swap $p and $p_dup;
$p = $p_dup;
-cpudata => $do_cpu_utilization,
-swload => 1,
-tags => $do_physical,
-retry => RETRY_COUNT,
-delay => RETRY_DELAY);
if (!$p_dup) {
"%s: can not obtain Processor Group information: $!\n"),
$cmdname;
exit(E_ERROR);
}
#
# Recreate @pg_list since it may have changed
#
\@sharing_filter, \@sharing_filter_neg);
next;
}
%pgs = ();
#
# Go over each PG and gets its utilization data
#
# Adjust idle and swload based on rounding
#
# Record history
#
}
#
# Sort the output
#
my @sorted_pgs;
#
# Should only top N be displayed?
#
if ($show_top) {
}
#
# Now print everything
#
foreach my $pg (@sorted_pgs) {
my $level;
if ($do_levels) {
}
if (!$verbose) {
printf "%${max_pg_len}d %-${max_sharename_len}s " .
"%s %s %s\n",
$cpus;
} else {
printf
"%${max_pg_len}d %-${max_sharename_len}s " .
"%4s %4s %4s %4s %4s %4s %4s %s\n",
$cpus;
}
#
# If per-CPU utilization is requested, print it after each
# corresponding PG
#
if ($do_cpu_utilization) {
my $w = ${max_sharename_len} - length ('CPU');
my ($cpu_utilization,
$swload) =
next unless defined $cpu_utilization;
my $cpuname = "CPU$cpu";
if ($do_levels) {
$cpuname =
$cpuname;
}
printf "%-${max_pg_len}s " .
"%-${max_sharename_len}s ",
' ', $cpuname;
if ($verbose) {
printf "%s %4s %4s\n",
} else {
printf "%s %s\n",
}
}
}
}
#
# Swap $p and $p_dup
#
$total_iterations++;
}
print_totals() if $do_aggregate;
####################################
# End of main loop
####################################
#
# Support Subroutines
#
#
# Print aggregated information in the end
#
sub print_totals
{
printf gettext("\n%s SUMMARY: UTILIZATION OVER %d SECONDS\n\n"),
' ' x 10,
$total_iterations * $interval;
my @sorted_pgs;
%pgs = ();
#
# Collect data per PG
#
# Adjust idle and swload based on rounding
}
#
# Sort PGs according to the sorting options
#
#
# Trim to top N if needed
#
if ($show_top) {
}
#
# Print headers
#
my $d = ' ' . '-' x 4;
if ($verbose) {
printf "%${max_pg_len}s %-${max_sharename_len}s %s " .
" ------HARDWARE------ ------SOFTWARE------\n",
' ', ' ', ' ' x 8;
printf "%-${max_pg_len}s %-${max_sharename_len}s",
'PG', 'RELATIONSHIP';
printf " %4s %4s", 'UTIL', ' CAP';
printf " $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt %s\n",
'MIN', 'AVG', 'MAX', 'MIN', 'AVG', 'MAX', 'CPUS';
} else {
printf "%${max_pg_len}s %-${max_sharename_len}s " .
"------HARDWARE------" .
" ------SOFTWARE------\n", ' ', ' ';
printf "%-${max_pg_len}s %-${max_sharename_len}s",
'PG', 'RELATIONSHIP';
printf " $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt $pcnt_fmt %s\n",
'MIN', 'AVG', 'MAX', 'MIN', 'AVG', 'MAX', 'CPUS';
}
#
# Print information per PG
#
foreach my $pg (@sorted_pgs) {
}
printf "%${max_pg_len}d %-${max_sharename_len}s ",
if ($verbose) {
printf "%4s %4s ",
}
printf "$pcnt_fmt $pcnt_fmt $pcnt_fmt ",
'-', '-', '-';
} else {
printf "%s %s %s ",
}
printf "%s %s %s",
printf " %s\n", $cpus;
}
exit ($rc);
}
#
# pg_sort_by_key(pgs, key, inverse)
# Sort pgs according to the key specified
#
# Arguments:
# pgs hash indexed by PG ID
# sort keyword
# inverse - inverse sort result if this is T
#
sub pg_sort_by_key
{
my $pgs = shift;
my $key = shift;
my $inverse = shift;
my @sorted;
$p->all_depth_first() :
$p->all_breadth_first();
} else {
}
}
#
# Convert numeric load to formatted string
#
sub load2str
{
my $load = shift;
return (sprintf "$pcnt_fmt", '-') if
return (sprintf "$pcnt%%", $load);
}
#
# get_load(snapshot1, snapshot2, pg)
#
# Get various hardware load data for the given PG using two snapshots.
# Arguments: two PG snapshots and PG ID
#
# In scalar context returns the hardware load
# In list context returns a list
# (load, utilization, capacity, accuracy)
#
sub get_load
{
my $p = shift;
my $p_dup = shift;
my $pg = shift;
$accuracy = 100;
$utilization = 0;
my $utilization_per_second = $utilization;
my $load;
if ($accuracy != 100) {
$load = HWLOAD_UNKNOWN;
} else {
}
return (wantarray() ?
$load);
}
#
# Make sure that with the rounding used, user + system + swload add up to 100%.
#
#
sub get_swload
{
my $user = shift;
my $sys = shift;
my $swload;
my $idle;
}
#
# get_pg_list(cookie, pg_list, sharing_filter, sharing_filter_neg) Get list OF
# PGs to look at based on all PGs available, user-specified PGs and
# user-specified filters.
#
sub get_pg_list
{
my $p = shift;
my $pg_list = shift;
my $sharing_filter = shift;
my $sharing_filter_neg = shift;
#
# Now we have list of PGs to work with. Now apply filtering. First list
# only those matching -R
#
my @sharing_filter = @$sharing_filter;
my @sharing_filter_neg = @$sharing_filter_neg;
# Remove any that doesn't match -r
@pgs = grep {
} @pgs if
scalar @sharing_filter_neg;
return (@pgs);
}
#
# usage(rc)
#
# Print short usage message and exit with the given return code.
# If verbose is T, print a bit more information
#
sub usage
{
printf STDERR
gettext("Usage:\t%s [-A] [-C] [-p] [-s key | -S key] " .
"[-t number] [-T u | d]\n"), $cmdname;
print STDERR
gettext("\t\t[-r string] [-R string] [-P pg ...] [-c processor_id... ]\n");
print STDERR
gettext("\t\t[interval [count]]\n\n");
exit ($rc);
}
#
# list_match(val, list_ref, strict)
# Return T if argument matches any of the elements on the list, undef otherwise.
#
sub list_match
{
my $arg = shift;
my $list = shift;
my $strict = shift;
}
#
# Convert a number to a string representation
# The number is scaled down until it is small enough to be in a good
# human readable format i.e. in the range 0 thru 1000.
# If it's smaller than 10 there's room enough to provide one decimal place.
#
{
my $number = shift;
return '-' unless defined ($number);
# Remove any trailing spaces
$number =~ s/ //g;
my $scale = 1000;
my $uom = shift(@measurement);
my $result;
# Get size in K.
$uom = shift(@measurement);
}
# check if we should output a decimal place after the point
} else {
}
return ("$result");
}