1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
#! /usr/perl5/bin/perl
#
# 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
# 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# pgstat - tool for displaying Processor Group statistics
#
use warnings;
use strict;
use File::Basename;
use List::Util qw(first max min);
use Errno;
use POSIX qw(locale_h strftime);
use Getopt::Long qw(:config no_ignore_case bundling auto_version);
use Sun::Solaris::Utils qw(textdomain gettext);
use Sun::Solaris::Pg;
#
# 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
#
my @sort_keys = qw(pg hwload swload user sys idle depth breadth);
# Set message locale
setlocale(LC_ALL, "");
textdomain(TEXT_DOMAIN);
# Get script name for error messages
our $cmdname = basename($0, ".pl");
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
GetOptions("aggregate|A" => \$do_aggregate,
"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,
) || usage(E_USAGE);
# Print usage message when -h is given
usage(E_SUCCESS) if $do_usage;
if ($do_version) {
printf gettext("%s version %s\n"), $cmdname, VERSION;
exit(E_SUCCESS);
}
#
# Verify options
#
# -T should have either u or d argument
if (defined($do_timestamp) && !($do_timestamp eq 'u' || $do_timestamp eq 'd')) {
printf STDERR gettext("%s: Invalid -T %s argument\n"),
$cmdname, $do_timestamp;
usage(E_USAGE);
}
if ($sort_order_a && $sort_order_d) {
printf STDERR gettext("%s: -S and -s flags can not be used together\n"),
$cmdname;
usage(E_USAGE);
}
if (defined ($show_top) && $show_top <= 0) {
printf STDERR gettext("%s: -t should specify positive integer\n"),
$cmdname;
usage(E_USAGE);
}
#
# 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_key = $sort_order_d || $sort_order_a;
$sort_reverse = defined($sort_order_d);
}
#
# Make sure sort key is valid
#
if (!list_match($sort_key, \@sort_keys, 1)) {
printf STDERR gettext("%s: invalid sort key %s\n"),
$cmdname, $sort_key;
usage(E_USAGE);
}
#
# 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.
#
my $p = Sun::Solaris::Pg->new(-cpudata => $do_cpu_utilization,
-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;
my $p_dup = Sun::Solaris::Pg->new(-cpudata => $do_cpu_utilization,
-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) {
$interval = shift @ARGV;
if (scalar @ARGV > 0) {
$count = $ARGV[0];
} else {
$count = 0;
}
}
if (! ($interval=~ m/^\d+\.?\d*$/)) {
printf STDERR
gettext("%s: Invalid interval %s - should be numeric\n"),
$cmdname, $interval;
usage(E_USAGE);
}
if ($count && ! ($count=~ m/^\d+$/)) {
printf STDERR
gettext("%s: Invalid count %s - should be numeric\n"),
$cmdname, $count;
usage(E_USAGE);
}
my $infinite = 1 unless $count;
#
# 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
#
my @all_cpus = $p->cpus($p->root());
# PGs to work with
my @pgs = @all_pgs;
my $rc = E_SUCCESS;
#
# 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 { $_ eq 'all' ? @all_cpus : $_ } # all -> (cpu1, cpu2, ...)
map { split /,/ } @cpu_list; # x,y -> (x, y)
@cpu_list = $p->expand(@cpu_list); # 1-3 -> 1 2 3
# Same drill for PGs
@pg_list =
map { $_ eq 'all' ? @all_pgs : $_ }
map { split /,/ } @pg_list;
@pg_list = $p->expand(@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
#
my @bad_cpus = $p->set_subtract(\@all_cpus, \@cpu_list);
if (scalar @bad_cpus) {
printf STDERR
gettext("%s: Invalid processor IDs %s\n"),
$cmdname, $p->id_collapse(@bad_cpus);
$rc = E_ERROR;
}
#
# Find all PGs which have at least some CPUs from @cpu_list
#
my @pgs_from_cpus = grep {
my @cpus = $p->cpus($_);
scalar($p->intersect(\@cpus, \@cpu_list));
} @all_pgs;
# Combine PGs from @pg_list (if any) with PGs we found
@pg_list = (@pg_list, @pgs_from_cpus);
}
#
# If there are any PGs specified by the user, complain about invalid ones
#
@pgs = get_pg_list($p, \@pg_list, \@sharing_filter, \@sharing_filter_neg);
if (scalar @pg_list > 0) {
#
# Warn about any invalid PG
# @bad_pgs is a list of invalid CPUs in the arguments
#
my @bad_pgs = $p->set_subtract(\@all_pgs, \@pg_list);
if (scalar @bad_pgs) {
printf STDERR
gettext("%s: warning: invalid PG IDs %s\n"),
$cmdname, $p->id_collapse(@bad_pgs);
}
}
# 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.
#
my $do_levels = ($sort_key eq 'breadth' || $sort_key eq 'depth');
#
# %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) {
my @levels = map { $p->level($_) } @pgs; # Levels for each PG
$maxlevel = max(@levels);
$minlevel = min(@levels);
}
#
# Walk over all PGs and find out the string length that we need to represent
# sharing name + physical tags + indentation level.
#
foreach my $pg (@pgs) {
my $name = $p->sh_name ($pg) || "unknown";
my $level = $p->level($pg) || 0 if $do_levels;
if ($do_physical) {
my $tags = $p->tags($pg);
$name = "$name [$tags]" if $tags;
$name_of_pg{$pg} = $name;
}
$name_of_pg{$pg} = $name;
my $length = length($name);
$length += $level - $minlevel if $do_levels;
$max_sharename_len = $length if $length > $max_sharename_len;
}
# Maximum length of PG ID field
my $max_pg_len = length(max(@pgs)) + 1;
$max_pg_len = length('PG') if ($max_pg_len) < length('PG');
#
#
# %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
#
$SIG{'INT'} = \&print_totals if $do_aggregate;
######################################################################
# Main loop
###########
while ($infinite || $count--) {
#
# Print timestamp if -T is specified
#
if ($do_timestamp) {
if ($do_timestamp eq 'u') {
print time(), "\n";
} else {
my $date_str = strftime "%A, %B %e, %Y %r %Z",
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
#
$p_dup->update();
#
# Do not show offlined CPUs
#
my @online_cpus = $p->online_cpus();
#
# Check whether both snapshots belong to the same generation
#
if ($p->generation() != $p_dup->generation()) {
printf gettext("Configuration changed!\n");
# Swap $p and $p_dup;
$p = $p_dup;
$p_dup = Sun::Solaris::Pg->new(
-cpudata => $do_cpu_utilization,
-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);
}
#
# Recreate @pg_list since it may have changed
#
@pgs = get_pg_list($p, \@pg_list,
\@sharing_filter, \@sharing_filter_neg);
next;
}
%pgs = ();
#
# Go over each PG and gets its utilization data
#
foreach my $pg (@pgs) {
my ($hwload, $utilization, $capacity, $accuracy) =
get_load($p, $p_dup, $pg);
my @cpus = $p->cpus ($pg);
my ($user, $sys, $idle, $swload) =
$p->sw_utilization($p_dup, $pg);
# Adjust idle and swload based on rounding
($swload, $idle) = get_swload($user, $sys);
$pgs{$pg}->{pg} = $pg;
$pgs{$pg}->{hwload} = $hwload;
$pgs{$pg}->{swload} = $swload;
$pgs{$pg}->{user} = $user;
$pgs{$pg}->{sys} = $sys;
$pgs{$pg}->{idle} = $idle;
$pgs{$pg}->{utilization} = $utilization;
$pgs{$pg}->{capacity} = $capacity;
#
# Record history
#
$history->{$pg}->{hwload} += $hwload if $hwload && $hwload >= 0;
$history->{$pg}->{swload} += $swload if $swload;
$history->{$pg}->{user} += $user if $user;
$history->{$pg}->{sys} += $sys if $sys;
$history->{$pg}->{idle} += $idle if $idle;
$history->{$pg}->{maxhwload} = $hwload if
!defined($history->{$pg}->{maxhwload}) ||
$hwload > $history->{$pg}->{maxhwload};
$history->{$pg}->{minhwload} = $hwload if
!defined($history->{$pg}->{minhwload}) ||
$hwload < $history->{$pg}->{minhwload};
$history->{$pg}->{maxswload} = $swload if
!defined($history->{$pg}->{maxswload}) ||
$swload > $history->{$pg}->{maxswload};
$history->{$pg}->{minswload} = $swload if
!defined($history->{$pg}->{minswload}) ||
$swload < $history->{$pg}->{minswload};
}
#
# Sort the output
#
my @sorted_pgs;
my $npgs = scalar @pgs;
@sorted_pgs = pg_sort_by_key(\%pgs, $sort_key, $sort_reverse, @pgs);
#
# Should only top N be displayed?
#
if ($show_top) {
$npgs = $show_top if $show_top < $npgs;
@sorted_pgs = @sorted_pgs[0..$npgs - 1];
}
#
# Now print everything
#
foreach my $pg (@sorted_pgs) {
my $shname = $name_of_pg{$pg};
my $level;
if ($do_levels) {
$level = $p->level($pg) - $minlevel;
$shname = (' ' x (LEVEL_OFFSET * $level)) . $shname;
}
my $hwload = $pgs{$pg}->{hwload} || 0;
my $swload = $pgs{$pg}->{swload};
my @cpus = $p->cpus($pg);
@cpus = $p->intersect(\@cpus, \@online_cpus);
my $cpus = $p->id_collapse(@cpus);
my $user = $pgs{$pg}->{user};
my $sys = $pgs{$pg}->{sys};
my $idle = $pgs{$pg}->{idle};
my $utilization = $pgs{$pg}->{utilization};
my $capacity = $pgs{$pg}->{capacity};
if (!$verbose) {
printf "%${max_pg_len}d %-${max_sharename_len}s " .
"%s %s %s\n",
$pg, $shname,
load2str($hwload),
load2str($swload),
$cpus;
} else {
printf
"%${max_pg_len}d %-${max_sharename_len}s " .
"%4s %4s %4s %4s %4s %4s %4s %s\n",
$pg, $shname,
load2str($hwload),
number_to_scaled_string($utilization),
number_to_scaled_string($capacity),
load2str($swload),
load2str($user),
load2str($sys),
load2str($idle),
$cpus;
}
#
# If per-CPU utilization is requested, print it after each
# corresponding PG
#
if ($do_cpu_utilization) {
my $w = ${max_sharename_len} - length ('CPU');
foreach my $cpu (sort {$a <=> $b } @cpus) {
my ($cpu_utilization,
$accuracy, $hw_utilization,
$swload) =
$p->cpu_utilization($p_dup, $pg, $cpu);
next unless defined $cpu_utilization;
my $cpuname = "CPU$cpu";
if ($do_levels) {
$cpuname =
(' ' x (LEVEL_OFFSET * $level)) .
$cpuname;
}
printf "%-${max_pg_len}s " .
"%-${max_sharename_len}s ",
' ', $cpuname;
if ($verbose) {
printf "%s %4s %4s\n",
load2str($cpu_utilization),
number_to_scaled_string($hw_utilization),
number_to_scaled_string($capacity);
} else {
printf "%s %s\n",
load2str($cpu_utilization),
load2str($swload);
}
}
}
}
#
# Swap $p and $p_dup
#
($p, $p_dup) = ($p_dup, $p);
$total_iterations++;
}
print_totals() if $do_aggregate;
####################################
# End of main loop
####################################
#
# Support Subroutines
#
#
# Print aggregated information in the end
#
sub print_totals
{
exit ($rc) unless $total_iterations > 1;
printf gettext("\n%s SUMMARY: UTILIZATION OVER %d SECONDS\n\n"),
' ' x 10,
$total_iterations * $interval;
my @sorted_pgs;
my $npgs = scalar @pgs;
%pgs = ();
#
# Collect data per PG
#
foreach my $pg (@pgs) {
$pgs{$pg}->{pg} = $pg;
my ($hwload, $utilization, $capacity, $accuracy) =
get_load($p_initial, $p_dup, $pg);
my @cpus = $p->cpus ($pg);
my ($user, $sys, $idle, $swload) =
$p_dup->sw_utilization($p_initial, $pg);
# Adjust idle and swload based on rounding
($swload, $idle) = get_swload($user, $sys);
$pgs{$pg}->{pg} = $pg;
$pgs{$pg}->{swload} = $swload;
$pgs{$pg}->{user} = $user;
$pgs{$pg}->{sys} = $sys;
$pgs{$pg}->{idle} = $idle;
$pgs{$pg}->{hwload} = $hwload;
$pgs{$pg}->{utilization} = number_to_scaled_string($utilization);
$pgs{$pg}->{capacity} = number_to_scaled_string($capacity);
$pgs{$pg}->{minhwload} = $history->{$pg}->{minhwload};
$pgs{$pg}->{maxhwload} = $history->{$pg}->{maxhwload};
$pgs{$pg}->{minswload} = $history->{$pg}->{minswload} || 0;
$pgs{$pg}->{maxswload} = $history->{$pg}->{maxswload} || 0;
}
#
# Sort PGs according to the sorting options
#
@sorted_pgs = pg_sort_by_key(\%pgs, $sort_key, $sort_reverse, @pgs);
#
# Trim to top N if needed
#
if ($show_top) {
$npgs = $show_top if $show_top < $npgs;
@sorted_pgs = @sorted_pgs[0..$npgs - 1];
}
#
# 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) {
my $cpus = $p->cpus($pg);
my $shname = $name_of_pg{$pg};
if ($sort_key eq 'breadth' || $sort_key eq 'depth') {
my $level = $p->level($pg) - $minlevel;
$shname = (' ' x (LEVEL_OFFSET * $level)) . $shname;
}
printf "%${max_pg_len}d %-${max_sharename_len}s ",
$pg, $shname;
if ($verbose) {
printf "%4s %4s ",
number_to_scaled_string($pgs{$pg}->{utilization}),
number_to_scaled_string($pgs{$pg}->{capacity});
}
if (!defined($pgs{$pg}->{hwload}) ||
$pgs{$pg}->{hwload} == HWLOAD_UNDEF) {
printf "$pcnt_fmt $pcnt_fmt $pcnt_fmt ",
'-', '-', '-';
} else {
printf "%s %s %s ",
load2str($pgs{$pg}->{minhwload}),
load2str($pgs{$pg}->{hwload}),
load2str($pgs{$pg}->{maxhwload});
}
printf "%s %s %s",
load2str($pgs{$pg}->{minswload}),
load2str($pgs{$pg}->{swload}),
load2str($pgs{$pg}->{maxswload});
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;
if ($key eq 'depth' || $key eq 'breadth') {
my $root = $p->root;
my @pgs = $key eq 'depth' ?
$p->all_depth_first() :
$p->all_breadth_first();
@sorted = reverse(grep { exists($pgs{$_}) } @pgs);
} else {
@sorted = sort { $pgs{$a}->{$key} <=> $pgs{$b}->{$key} } @_;
}
return ($inverse ? reverse(@sorted) : @sorted);
}
#
# Convert numeric load to formatted string
#
sub load2str
{
my $load = shift;
return (sprintf "$pcnt_fmt", '-') if
!defined($load) || $load == HWLOAD_UNDEF;
return (sprintf "$pcnt_fmt", '?') if $load == HWLOAD_UNKNOWN;
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;
return HWLOAD_UNDEF if !$p->has_utilization($pg);
my ($capacity, $utilization, $accuracy, $tdelta);
$accuracy = 100;
$utilization = 0;
$utilization = $p->utilization($p_dup, $pg) || 0;
$capacity = $p_dup->capacity($pg);
$accuracy = $p->accuracy($p_dup, $pg) || 0;
$tdelta = $p->tdelta($p_dup, $pg);
my $utilization_per_second = $utilization;
$utilization_per_second /= $tdelta if $tdelta;
my $load;
if ($accuracy != 100) {
$load = HWLOAD_UNKNOWN;
} else {
$load = $capacity ?
$utilization_per_second * 100 / $capacity :
HWLOAD_UNKNOWN;
$capacity *= $tdelta if $tdelta;
}
return (wantarray() ?
($load, $utilization, $capacity, $accuracy) :
$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;
$user = sprintf "$pcnt", $user;
$sys = sprintf "$pcnt", $sys;
$swload = $user + $sys;
$idle = 100 - $swload;
return ($swload, $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;
my @all = $p->all();
my @pg_list = scalar @$pg_list ? @$pg_list : @all;
my @pgs = $p->intersect(\@all_pgs, \@pg_list);
#
# Now we have list of PGs to work with. Now apply filtering. First list
# only those matching -R
#
@pgs = grep { list_match($p->sh_name($_), \@sharing_filter, 0) } @pgs if
@sharing_filter;
my @sharing_filter = @$sharing_filter;
my @sharing_filter_neg = @$sharing_filter_neg;
# Remove any that doesn't match -r
@pgs = grep {
!list_match($p->sh_name($_), \@sharing_filter_neg, 0)
} @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
{
my $rc = shift || E_SUCCESS;
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;
return first { $arg eq $_ } @$list if $strict;
return first { $arg =~ m/$_/i } @$list;
}
#
# 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.
#
sub number_to_scaled_string
{
my $number = shift;
return '-' unless defined ($number);
# Remove any trailing spaces
$number =~ s/ //g;
return $number unless $number =~ /^[.\d]+$/;
my $scale = 1000;
return sprintf("%4d", $number) if $number < $scale;
my @measurement = ('K', 'M', 'B', 'T');
my $uom = shift(@measurement);
my $result;
my $save = $number;
# Get size in K.
$number /= $scale;
while (($number >= $scale) && $uom ne 'B') {
$uom = shift(@measurement);
$save = $number;
$number /= $scale;
}
# check if we should output a decimal place after the point
if ($save && (($save / $scale) < 10)) {
$result = sprintf("%3.1f$uom", $save / $scale);
} else {
$result = sprintf("%3d$uom", $number);
}
return ("$result");
}
__END__
|