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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* rewritten from UCB 4.13 83/09/25
* rewritten from SunOS 4.1 SID 1.18 89/10/06
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <memory.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <sys/sysinfo.h>
#include <inttypes.h>
#include <strings.h>
#include <sys/systeminfo.h>
#include <kstat.h>
#include "dsr.h"
#include "statcommon.h"
#define DISK_OLD 0x0001
#define DISK_NEW 0x0002
#define DISK_EXTENDED 0x0004
#define DISK_ERRORS 0x0008
#define DISK_EXTENDED_ERRORS 0x0010
#define DISK_IOPATH_LI 0x0020 /* LunInitiator */
#define DISK_IOPATH_LTI 0x0040 /* LunTargetInitiator */
#define DISK_NORMAL (DISK_OLD | DISK_NEW)
#define DISK_IO_MASK (DISK_OLD | DISK_NEW | DISK_EXTENDED)
#define DISK_ERROR_MASK (DISK_ERRORS | DISK_EXTENDED_ERRORS)
#define PRINT_VERTICAL (DISK_ERROR_MASK | DISK_EXTENDED)
#define REPRINT 19
/*
* It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
* labels don't look bad. 1GB is really 1073741824 bytes.
*/
#define DISK_GIGABYTE 1000000000.0
/*
* Function desciptor to be called when extended
* headers are used.
*/
typedef struct formatter {
void (*nfunc)(void);
struct formatter *next;
} format_t;
/*
* Used to get formatting right when printing tty/cpu
* data to the right of disk data
*/
enum show_disk_mode {
SHOW_FIRST_ONLY,
SHOW_SECOND_ONWARDS,
SHOW_ALL
};
enum show_disk_mode show_disk_mode = SHOW_ALL;
char *cmdname = "iostat";
int caught_cont = 0;
static char one_blank[] = " ";
static char two_blanks[] = " ";
/*
* count for number of lines to be emitted before a header is
* shown again. Only used for the basic format.
*/
static uint_t tohdr = 1;
/*
* If we're in raw format, have we printed a header? We only do it
* once for raw but we emit it every REPRINT lines in non-raw format.
* This applies only for the basic header. The extended header is
* done only once in both formats.
*/
static uint_t hdr_out;
/*
* Flags representing arguments from command line
*/
static uint_t do_tty; /* show tty info (-t) */
static uint_t do_disk; /* show disk info per selected */
/* format (-d, -D, -e, -E, -x -X -Y) */
static uint_t do_cpu; /* show cpu info (-c) */
static uint_t do_interval; /* do intervals (-I) */
static int do_partitions; /* per-partition stats (-p) */
static int do_partitions_only; /* per-partition stats only (-P) */
/* no per-device stats for disks */
static uint_t do_conversions; /* display disks as cXtYdZ (-n) */
static uint_t do_megabytes; /* display data in MB/sec (-M) */
static uint_t do_controller; /* display controller info (-C) */
static uint_t do_raw; /* emit raw format (-r) */
static uint_t do_timestamp; /* timestamp each display (-T) */
static uint_t do_devid; /* -E should show devid */
/*
* Definition of allowable types of timestamps
*/
#define CDATE 1
#define UDATE 2
/*
* Default number of disk drives to be displayed in basic format
*/
#define DEFAULT_LIMIT 4
struct iodev_filter df;
static uint_t suppress_state; /* skip state change messages */
static uint_t suppress_zero; /* skip zero valued lines */
static uint_t show_mountpts; /* show mount points */
static int interval; /* interval (seconds) to output */
static int iter; /* iterations from command line */
#define SMALL_SCRATCH_BUFLEN MAXNAMELEN
static int iodevs_nl; /* name field width */
#define IODEVS_NL_MIN 6 /* not too thin for "device" */
#define IODEVS_NL_MAX 24 /* but keep full width under 80 */
static char disk_header[132];
static uint_t dh_len; /* disk header length for centering */
static int lineout; /* data waiting to be printed? */
static struct snapshot *newss;
static struct snapshot *oldss;
static double getime; /* elapsed time */
static double percent; /* 100 / etime */
/*
* List of functions to be called which will construct the desired output
*/
static format_t *formatter_list;
static format_t *formatter_end;
static u_longlong_t ull_delta(u_longlong_t, u_longlong_t);
static uint_t u32_delta(uint_t, uint_t);
static void setup(void (*nfunc)(void));
static void print_timestamp(void);
static void print_tty_hdr1(void);
static void print_tty_hdr2(void);
static void print_cpu_hdr1(void);
static void print_cpu_hdr2(void);
static void print_tty_data(void);
static void print_cpu_data(void);
static void print_err_hdr(void);
static void print_disk_header(void);
static void hdrout(void);
static void disk_errors(void);
static void do_newline(void);
static void push_out(const char *, ...);
static void printhdr(int);
static void printxhdr(void);
static void usage(void);
static void do_args(int, char **);
static void do_format(void);
static void show_all_disks(void);
static void show_first_disk(void);
static void show_other_disks(void);
static void show_disk_errors(void *, void *, void *);
static void write_core_header(void);
static int fzero(double value);
static int safe_strtoi(char const *val, char *errmsg);
int
main(int argc, char **argv)
{
enum snapshot_types types = SNAP_SYSTEM;
kstat_ctl_t *kc;
long hz;
int forever;
hrtime_t start_n;
hrtime_t period_n;
do_args(argc, argv);
/*
* iostat historically showed CPU changes, even though
* it doesn't provide much useful information
*/
types |= SNAP_CPUS;
if (do_disk)
types |= SNAP_IODEVS;
if (do_disk && !do_partitions_only)
df.if_allowed_types |= IODEV_DISK;
if (do_disk & DISK_IOPATH_LI) {
df.if_allowed_types |= IODEV_IOPATH_LTI;
types |= SNAP_IOPATHS_LI;
}
if (do_disk & DISK_IOPATH_LTI) {
df.if_allowed_types |= IODEV_IOPATH_LTI;
types |= SNAP_IOPATHS_LTI;
}
if (do_disk & DISK_ERROR_MASK)
types |= SNAP_IODEV_ERRORS;
if (do_partitions || do_partitions_only)
df.if_allowed_types |= IODEV_PARTITION;
if (do_conversions)
types |= SNAP_IODEV_PRETTY;
if (do_devid)
types |= SNAP_IODEV_DEVID;
if (do_controller) {
if (!(do_disk & PRINT_VERTICAL) ||
(do_disk & DISK_EXTENDED_ERRORS))
fail(0, "-C can only be used with -e or -x.");
types |= SNAP_CONTROLLERS;
df.if_allowed_types |= IODEV_CONTROLLER;
}
hz = sysconf(_SC_CLK_TCK);
/*
* Undocumented behavior - sending a SIGCONT will result
* in a new header being emitted. Used only if we're not
* doing extended headers. This is a historical
* artifact.
*/
if (!(do_disk & PRINT_VERTICAL))
(void) signal(SIGCONT, printhdr);
if (interval)
period_n = (hrtime_t)interval * NANOSEC;
kc = open_kstat();
if (interval)
start_n = gethrtime();
newss = acquire_snapshot(kc, types, &df);
/* compute width of "device" field */
iodevs_nl = newss->s_iodevs_is_name_maxlen;
iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
IODEVS_NL_MIN : iodevs_nl;
iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
IODEVS_NL_MAX : iodevs_nl;
do_format();
forever = (iter == 0);
do {
if (do_conversions && show_mountpts)
do_mnttab();
if (do_tty || do_cpu) {
kstat_t *oldks;
oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
getime = cpu_ticks_delta(oldks,
&newss->s_sys.ss_agg_sys);
percent = (getime > 0.0) ? 100.0 / getime : 0.0;
getime = (getime / nr_active_cpus(newss)) / hz;
if (getime == 0.0)
getime = (double)interval;
if (getime == 0.0 || do_interval)
getime = 1.0;
}
if (formatter_list) {
format_t *tmp;
tmp = formatter_list;
while (tmp) {
(tmp->nfunc)();
tmp = tmp->next;
}
(void) fflush(stdout);
}
/* only remaining/doing a single iteration, we are done */
if (iter == 1)
continue;
if (interval > 0)
/* Have a kip */
sleep_until(&start_n, period_n, forever, &caught_cont);
free_snapshot(oldss);
oldss = newss;
newss = acquire_snapshot(kc, types, &df);
iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
newss->s_iodevs_is_name_maxlen : iodevs_nl;
iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
IODEVS_NL_MIN : iodevs_nl;
iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
IODEVS_NL_MAX : iodevs_nl;
if (!suppress_state)
snapshot_report_changes(oldss, newss);
/* if config changed, show stats from boot */
if (snapshot_has_changed(oldss, newss)) {
free_snapshot(oldss);
oldss = NULL;
}
} while (--iter);
free_snapshot(oldss);
free_snapshot(newss);
(void) kstat_close(kc);
free(df.if_names);
return (0);
}
/*
* Some magic numbers used in header formatting.
*
* DISK_LEN = length of either "kps tps serv" or "wps rps util"
* using 0 as the first position
*
* DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
* either side. Does not use zero as first pos.
*
* DEVICE_LEN = length of "device" + 1 character.
*/
#define DISK_LEN 11
#define DISK_ERROR_LEN 16
#define DEVICE_LEN 7
/*ARGSUSED*/
static void
show_disk_name(void *v1, void *v2, void *data)
{
struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
size_t slen;
char *name;
char fbuf[SMALL_SCRATCH_BUFLEN];
if (dev == NULL)
return;
name = do_conversions ? dev->is_pretty : dev->is_name;
name = name ? name : dev->is_name;
if (!do_raw) {
uint_t width;
slen = strlen(name);
/*
* The length is less
* than the section
* which will be displayed
* on the next line.
* Center the entry.
*/
width = (DISK_LEN + 1)/2 + (slen / 2);
(void) snprintf(fbuf, sizeof (fbuf),
"%*s", width, name);
name = fbuf;
push_out("%-13.13s ", name);
} else {
push_out(name);
}
}
/*ARGSUSED*/
static void
show_disk_header(void *v1, void *v2, void *data)
{
push_out(disk_header);
}
/*
* Write out a two line header. What is written out depends on the flags
* selected but in the worst case consists of a tty header, a disk header
* providing information for 4 disks and a cpu header.
*
* The tty header consists of the word "tty" on the first line above the
* words "tin tout" on the next line. If present the tty portion consumes
* the first 10 characters of each line since "tin tout" is surrounded
* by single spaces.
*
* Each of the disk sections is a 14 character "block" in which the name of
* the disk is centered in the first 12 characters of the first line.
*
* The cpu section is an 11 character block with "cpu" centered over the
* section.
*
* The worst case should look as follows:
*
* 0---------1--------2---------3---------4---------5---------6---------7-------
* tty sd0 sd1 sd2 sd3 cpu
* tin tout kps tps serv kps tps serv kps tps serv kps tps serv us sy wt id
* NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NN NN NN NN
*
* When -D is specified, the disk header looks as follows (worst case):
*
* 0---------1--------2---------3---------4---------5---------6---------7-------
* tty sd0 sd1 sd2 sd3 cpu
* tin tout rps wps util rps wps util rps wps util rps wps util us sy wt id
* NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NN NN NN NN
*/
static void
printhdr(int sig)
{
/*
* If we're here because a signal fired, reenable the
* signal.
*/
if (sig)
(void) signal(SIGCONT, printhdr);
if (sig == SIGCONT)
caught_cont = 1;
/*
* Horizontal mode headers
*
* First line
*/
if (do_tty)
print_tty_hdr1();
if (do_disk & DISK_NORMAL) {
(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
show_disk_name, NULL);
}
if (do_cpu)
print_cpu_hdr1();
do_newline();
/*
* Second line
*/
if (do_tty)
print_tty_hdr2();
if (do_disk & DISK_NORMAL) {
(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
show_disk_header, NULL);
}
if (do_cpu)
print_cpu_hdr2();
do_newline();
tohdr = REPRINT;
}
/*
* Write out the extended header centered over the core information.
*/
static void
write_core_header(void)
{
char *edev = "extended device statistics";
uint_t lead_space_ct;
uint_t follow_space_ct;
size_t edevlen;
if (do_raw == 0) {
/*
* The things we do to look nice...
*
* Center the core output header. Make sure we have the
* right number of trailing spaces for follow-on headers
* (i.e., cpu and/or tty and/or errors).
*/
edevlen = strlen(edev);
lead_space_ct = dh_len - edevlen;
lead_space_ct /= 2;
if (lead_space_ct > 0) {
follow_space_ct = dh_len - (lead_space_ct + edevlen);
if (do_disk & DISK_ERRORS)
follow_space_ct -= DISK_ERROR_LEN;
if ((do_disk & DISK_EXTENDED) && do_conversions)
follow_space_ct -= DEVICE_LEN;
push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
lead_space_ct, edev, one_blank, follow_space_ct);
} else
push_out("%56s", edev);
} else
push_out(edev);
}
/*
* In extended mode headers, we don't want to reprint the header on
* signals as they are printed every time anyways.
*/
static void
printxhdr(void)
{
/*
* Vertical mode headers
*/
if (do_disk & DISK_EXTENDED)
setup(write_core_header);
if (do_disk & DISK_ERRORS)
setup(print_err_hdr);
if (do_conversions) {
setup(do_newline);
if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
setup(print_disk_header);
setup(do_newline);
} else {
if (do_tty)
setup(print_tty_hdr1);
if (do_cpu)
setup(print_cpu_hdr1);
setup(do_newline);
if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
setup(print_disk_header);
if (do_tty)
setup(print_tty_hdr2);
if (do_cpu)
setup(print_cpu_hdr2);
setup(do_newline);
}
}
/*
* Write out a line for this disk - note that show_disk writes out
* full lines or blocks for each selected disk.
*/
static void
show_disk(void *v1, void *v2, void *data)
{
struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
int *count = (int *)data;
double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
double wserv, rserv, serv;
double iosize; /* kb/sec or MB/sec */
double etime, hr_etime;
char *disk_name;
u_longlong_t ldeltas;
uint_t udeltas;
uint64_t t_delta;
uint64_t w_delta;
uint64_t r_delta;
int doit = 1;
int i;
uint_t toterrs;
char *fstr;
if (new == NULL)
return;
switch (show_disk_mode) {
case SHOW_FIRST_ONLY:
if (count != NULL && *count)
return;
break;
case SHOW_SECOND_ONWARDS:
if (count != NULL && !*count) {
(*count)++;
return;
}
break;
default:
break;
}
disk_name = do_conversions ? new->is_pretty : new->is_name;
disk_name = disk_name ? disk_name : new->is_name;
/*
* Only do if we want IO stats - Avoids errors traveling this
* section if that's all we want to see.
*/
if (do_disk & DISK_IO_MASK) {
if (old) {
t_delta = hrtime_delta(old->is_snaptime,
new->is_snaptime);
} else {
t_delta = hrtime_delta(new->is_crtime,
new->is_snaptime);
}
if (new->is_nr_children) {
if (new->is_type == IODEV_CONTROLLER) {
t_delta /= new->is_nr_children;
} else if ((new->is_type == IODEV_IOPATH_LT) ||
(new->is_type == IODEV_IOPATH_LI)) {
/* synthetic path */
if (!old) {
t_delta = new->is_crtime;
}
t_delta /= new->is_nr_children;
}
}
hr_etime = (double)t_delta;
if (hr_etime == 0.0)
hr_etime = (double)NANOSEC;
etime = hr_etime / (double)NANOSEC;
/* reads per second */
udeltas = u32_delta(old ? old->is_stats.reads : 0,
new->is_stats.reads);
rps = (double)udeltas;
rps /= etime;
/* writes per second */
udeltas = u32_delta(old ? old->is_stats.writes : 0,
new->is_stats.writes);
wps = (double)udeltas;
wps /= etime;
tps = rps + wps;
/* transactions per second */
/*
* report throughput as either kb/sec or MB/sec
*/
if (!do_megabytes)
iosize = 1024.0;
else
iosize = 1048576.0;
ldeltas = ull_delta(old ? old->is_stats.nread : 0,
new->is_stats.nread);
if (ldeltas) {
krps = (double)ldeltas;
krps /= etime;
krps /= iosize;
} else
krps = 0.0;
ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
new->is_stats.nwritten);
if (ldeltas) {
kwps = (double)ldeltas;
kwps /= etime;
kwps /= iosize;
} else
kwps = 0.0;
/*
* Blocks transferred per second
*/
kps = krps + kwps;
/*
* Average number of wait transactions waiting
*/
w_delta = hrtime_delta((u_longlong_t)
(old ? old->is_stats.wlentime : 0),
new->is_stats.wlentime);
if (w_delta) {
avw = (double)w_delta;
avw /= hr_etime;
} else
avw = 0.0;
/*
* Average number of run transactions waiting
*/
r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
new->is_stats.rlentime);
if (r_delta) {
avr = (double)r_delta;
avr /= hr_etime;
} else
avr = 0.0;
/*
* Average wait service time in milliseconds
*/
if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
mtps = 1000.0 / tps;
if (avw != 0.0)
wserv = avw * mtps;
else
wserv = 0.0;
if (avr != 0.0)
rserv = avr * mtps;
else
rserv = 0.0;
serv = rserv + wserv;
} else {
rserv = 0.0;
wserv = 0.0;
serv = 0.0;
}
/* % of time there is a transaction waiting for service */
t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
new->is_stats.wtime);
if (t_delta) {
w_pct = (double)t_delta;
w_pct /= hr_etime;
w_pct *= 100.0;
/*
* Average the wait queue utilization over the
* the controller's devices, if this is a controller.
*/
if (new->is_type == IODEV_CONTROLLER)
w_pct /= new->is_nr_children;
} else
w_pct = 0.0;
/* % of time there is a transaction running */
t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
new->is_stats.rtime);
if (t_delta) {
r_pct = (double)t_delta;
r_pct /= hr_etime;
r_pct *= 100.0;
/*
* Average the percent busy over the controller's
* devices, if this is a controller.
*/
if (new->is_type == IODEV_CONTROLLER)
w_pct /= new->is_nr_children;
} else {
r_pct = 0.0;
}
/* % of time there is a transaction running */
if (do_interval) {
rps *= etime;
wps *= etime;
tps *= etime;
krps *= etime;
kwps *= etime;
kps *= etime;
}
}
if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
if ((!do_conversions) && ((suppress_zero == 0) ||
((do_disk & DISK_EXTENDED) == 0))) {
if (do_raw == 0) {
push_out("%-*.*s",
iodevs_nl, iodevs_nl, disk_name);
} else {
push_out(disk_name);
}
}
}
switch (do_disk & DISK_IO_MASK) {
case DISK_OLD:
if (do_raw == 0)
fstr = "%3.0f %3.0f %4.0f ";
else
fstr = "%.0f,%.0f,%.0f";
push_out(fstr, kps, tps, serv);
break;
case DISK_NEW:
if (do_raw == 0)
fstr = "%3.0f %3.0f %4.1f ";
else
fstr = "%.0f,%.0f,%.1f";
push_out(fstr, rps, wps, r_pct);
break;
case DISK_EXTENDED:
if (suppress_zero) {
if (fzero(rps) && fzero(wps) && fzero(krps) &&
fzero(kwps) && fzero(avw) && fzero(avr) &&
fzero(serv) && fzero(w_pct) && fzero(r_pct)) {
doit = 0;
} else if (do_conversions == 0) {
if (do_raw == 0) {
push_out("%-*.*s",
iodevs_nl, iodevs_nl, disk_name);
} else {
push_out(disk_name);
}
}
}
if (doit) {
if (!do_conversions) {
if (do_raw == 0) {
fstr = " %6.1f %6.1f %6.1f %6.1f "
"%4.1f %4.1f %6.1f %3.0f "
"%3.0f ";
} else {
fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
"%.1f,%.0f,%.0f";
}
push_out(fstr, rps, wps, krps, kwps, avw, avr,
serv, w_pct, r_pct);
} else {
if (do_raw == 0) {
fstr = " %6.1f %6.1f %6.1f %6.1f "
"%4.1f %4.1f %6.1f %6.1f "
"%3.0f %3.0f ";
} else {
fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
"%.1f,%.1f,%.0f,%.0f";
}
push_out(fstr, rps, wps, krps, kwps, avw, avr,
wserv, rserv, w_pct, r_pct);
}
}
break;
}
if (do_disk & DISK_ERRORS) {
if ((do_disk == DISK_ERRORS)) {
if (do_raw == 0)
push_out(two_blanks);
}
if (new->is_errors.ks_data) {
kstat_named_t *knp;
char *efstr;
if (do_raw == 0)
efstr = "%3u ";
else
efstr = "%u";
toterrs = 0;
knp = KSTAT_NAMED_PTR(&new->is_errors);
for (i = 0; i < 3; i++) {
switch (knp[i].data_type) {
case KSTAT_DATA_ULONG:
push_out(efstr,
knp[i].value.ui32);
toterrs += knp[i].value.ui32;
break;
case KSTAT_DATA_ULONGLONG:
/*
* We're only set up to
* write out the low
* order 32-bits so
* just grab that.
*/
push_out(efstr,
knp[i].value.ui32);
toterrs += knp[i].value.ui32;
break;
default:
break;
}
}
push_out(efstr, toterrs);
} else {
if (do_raw == 0)
push_out(" 0 0 0 0 ");
else
push_out("0,0,0,0");
}
}
if (suppress_zero == 0 || doit == 1) {
if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
do_conversions) {
push_out("%s", disk_name);
if (show_mountpts && new->is_dname) {
mnt_t *mount_pt;
char *lu;
char *dnlu;
char lub[SMALL_SCRATCH_BUFLEN];
lu = strrchr(new->is_dname, '/');
if (lu) {
/* only the part after a possible '/' */
dnlu = strrchr(disk_name, '/');
if (dnlu != NULL &&
strcmp(dnlu, lu) == 0)
lu = new->is_dname;
else {
*lu = 0;
(void) strcpy(lub,
new->is_dname);
*lu = '/';
(void) strcat(lub, "/");
(void) strcat(lub,
disk_name);
lu = lub;
}
} else
lu = disk_name;
mount_pt = lookup_mntent_byname(lu);
if (mount_pt) {
if (do_raw == 0)
push_out(" (%s)",
mount_pt->mount_point);
else
push_out("(%s)",
mount_pt->mount_point);
}
}
}
}
if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
do_newline();
if (count != NULL)
(*count)++;
}
static void
usage(void)
{
(void) fprintf(stderr,
"Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
" [-l n] [-T d|u] [disk ...] [interval [count]]\n"
"\t\t-c: report percentage of time system has spent\n"
"\t\t\tin user/system/wait/idle mode\n"
"\t\t-C: report disk statistics by controller\n"
"\t\t-d: display disk Kb/sec, transfers/sec, avg. \n"
"\t\t\tservice time in milliseconds \n"
"\t\t-D: display disk reads/sec, writes/sec, \n"
"\t\t\tpercentage disk utilization \n"
"\t\t-e: report device error summary statistics\n"
"\t\t-E: report extended device error statistics\n"
"\t\t-i: show device IDs for -E output\n"
"\t\t-I: report the counts in each interval,\n"
"\t\t\tinstead of rates, where applicable\n"
"\t\t-l n: Limit the number of disks to n\n"
"\t\t-m: Display mount points (most useful with -p)\n"
"\t\t-M: Display data throughput in MB/sec "
"instead of Kb/sec\n"
"\t\t-n: convert device names to cXdYtZ format\n"
"\t\t-p: report per-partition disk statistics\n"
"\t\t-P: report per-partition disk statistics only,\n"
"\t\t\tno per-device disk statistics\n"
"\t\t-r: Display data in comma separated format\n"
"\t\t-s: Suppress state change messages\n"
"\t\t-T d|u Display a timestamp in date (d) or unix "
"time_t (u)\n"
"\t\t-t: display chars read/written to terminals\n"
"\t\t-x: display extended disk statistics\n"
"\t\t-X: display I/O path statistics\n"
"\t\t-Y: display I/O path (I/T/L) statistics\n"
"\t\t-z: Suppress entries with all zero values\n");
exit(1);
}
/*ARGSUSED*/
static void
show_disk_errors(void *v1, void *v2, void *d)
{
struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
kstat_named_t *knp;
size_t col;
int i, len;
char *dev_name;
if (disk->is_errors.ks_ndata == 0)
return;
if (disk->is_type == IODEV_CONTROLLER)
return;
dev_name = do_conversions ? disk->is_pretty : disk->is_name;
dev_name = dev_name ? dev_name : disk->is_name;
len = strlen(dev_name);
if (len > 20)
push_out("%s ", dev_name);
else if (len > 16)
push_out("%-20.20s ", dev_name);
else {
if (do_conversions)
push_out("%-16.16s ", dev_name);
else
push_out("%-9.9s ", dev_name);
}
col = 0;
knp = KSTAT_NAMED_PTR(&disk->is_errors);
for (i = 0; i < disk->is_errors.ks_ndata; i++) {
/* skip kstats that the driver did not kstat_named_init */
if (knp[i].name[0] == 0)
continue;
col += strlen(knp[i].name);
switch (knp[i].data_type) {
case KSTAT_DATA_CHAR:
if ((strcmp(knp[i].name, "Serial No") == 0) &&
do_devid) {
if (disk->is_devid) {
push_out("Device Id: %s ",
disk->is_devid);
col += strlen(disk->is_devid);
} else
push_out("Device Id: ");
} else {
push_out("%s: %-.16s ", knp[i].name,
&knp[i].value.c[0]);
col += strlen(&knp[i].value.c[0]);
}
break;
case KSTAT_DATA_ULONG:
push_out("%s: %u ", knp[i].name,
knp[i].value.ui32);
col += 4;
break;
case KSTAT_DATA_ULONGLONG:
if (strcmp(knp[i].name, "Size") == 0) {
push_out("%s: %2.2fGB <%llu bytes>\n",
knp[i].name,
(float)knp[i].value.ui64 /
DISK_GIGABYTE,
knp[i].value.ui64);
col = 0;
break;
}
push_out("%s: %u ", knp[i].name,
knp[i].value.ui32);
col += 4;
break;
}
if ((col >= 62) || (i == 2)) {
do_newline();
col = 0;
}
}
if (col > 0) {
do_newline();
}
do_newline();
}
void
do_args(int argc, char **argv)
{
int c;
int errflg = 0;
extern char *optarg;
extern int optind;
while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
switch (c) {
case 't':
do_tty++;
break;
case 'd':
do_disk |= DISK_OLD;
break;
case 'D':
do_disk |= DISK_NEW;
break;
case 'x':
do_disk |= DISK_EXTENDED;
break;
case 'X':
if (do_disk & DISK_IOPATH_LTI)
errflg++; /* -Y already used */
else
do_disk |= DISK_IOPATH_LI;
break;
case 'Y':
if (do_disk & DISK_IOPATH_LI)
errflg++; /* -X already used */
else
do_disk |= DISK_IOPATH_LTI;
break;
case 'C':
do_controller++;
break;
case 'c':
do_cpu++;
break;
case 'I':
do_interval++;
break;
case 'p':
do_partitions++;
break;
case 'P':
do_partitions_only++;
break;
case 'n':
do_conversions++;
break;
case 'M':
do_megabytes++;
break;
case 'e':
do_disk |= DISK_ERRORS;
break;
case 'E':
do_disk |= DISK_EXTENDED_ERRORS;
break;
case 'i':
do_devid = 1;
break;
case 's':
suppress_state = 1;
break;
case 'z':
suppress_zero = 1;
break;
case 'm':
show_mountpts = 1;
break;
case 'T':
if (optarg) {
if (*optarg == 'u')
do_timestamp = UDATE;
else if (*optarg == 'd')
do_timestamp = CDATE;
else
errflg++;
} else
errflg++;
break;
case 'r':
do_raw = 1;
break;
case 'l':
df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
if (df.if_max_iodevs < 1)
usage();
break;
case '?':
errflg++;
}
if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
(void) fprintf(stderr, "-d and -D are incompatible.\n");
usage();
}
if (errflg) {
usage();
}
/* if no output classes explicity specified, use defaults */
if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
do_tty = do_cpu = 1, do_disk = DISK_OLD;
/*
* multi-path options (-X, -Y) without a specific vertical
* output format (-x, -e, -E) imply extended -x format
*/
if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
!(do_disk & PRINT_VERTICAL))
do_disk |= DISK_EXTENDED;
/*
* If conflicting options take the preferred
* -D and -x result in -x
* -d or -D and -e or -E gives only whatever -d or -D was specified
*/
if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
do_disk &= ~DISK_NORMAL;
if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
do_disk &= ~DISK_ERROR_MASK;
/* nfs, tape, always shown */
df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
/*
* If limit == 0 then no command line limit was set, else if any of
* the flags that cause unlimited disks were not set,
* use the default of 4
*/
if (df.if_max_iodevs == 0) {
df.if_max_iodevs = DEFAULT_LIMIT;
df.if_skip_floppy = 1;
if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
DISK_EXTENDED_ERRORS)) {
df.if_max_iodevs = UNLIMITED_IODEVS;
df.if_skip_floppy = 0;
}
}
if (do_disk) {
size_t count = 0;
size_t i = optind;
while (i < argc && !isdigit(argv[i][0])) {
count++;
i++;
}
/*
* "Note: disks explicitly requested
* are not subject to this disk limit"
*/
if ((count > df.if_max_iodevs) ||
(count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
df.if_max_iodevs = count;
df.if_names = safe_alloc(count * sizeof (char *));
(void) memset(df.if_names, 0, count * sizeof (char *));
df.if_nr_names = 0;
while (optind < argc && !isdigit(argv[optind][0]))
df.if_names[df.if_nr_names++] = argv[optind++];
}
if (optind < argc) {
interval = safe_strtoi(argv[optind], "invalid interval");
if (interval < 1)
fail(0, "invalid interval");
optind++;
if (optind < argc) {
iter = safe_strtoi(argv[optind], "invalid count");
if (iter < 1)
fail(0, "invalid count");
optind++;
}
}
if (interval == 0)
iter = 1;
if (optind < argc)
usage();
}
/*
* Driver for doing the extended header formatting. Will produce
* the function stack needed to output an extended header based
* on the options selected.
*/
void
do_format(void)
{
char header[SMALL_SCRATCH_BUFLEN];
char ch;
char iosz;
const char *fstr;
disk_header[0] = 0;
ch = (do_interval ? 'i' : 's');
iosz = (do_megabytes ? 'M' : 'k');
if (do_disk & DISK_ERRORS) {
if (do_raw == 0) {
(void) sprintf(header, "s/w h/w trn tot ");
} else
(void) sprintf(header, "s/w,h/w,trn,tot");
} else
*header = NULL;
switch (do_disk & DISK_IO_MASK) {
case DISK_OLD:
if (do_raw == 0)
fstr = "%cp%c tp%c serv ";
else
fstr = "%cp%c,tp%c,serv";
(void) snprintf(disk_header, sizeof (disk_header),
fstr, iosz, ch, ch);
break;
case DISK_NEW:
if (do_raw == 0)
fstr = "rp%c wp%c util ";
else
fstr = "%rp%c,wp%c,util";
(void) snprintf(disk_header, sizeof (disk_header),
fstr, ch, ch);
break;
case DISK_EXTENDED:
/* This is -x option */
if (!do_conversions) {
/* without -n option */
if (do_raw == 0) {
/* without -r option */
(void) snprintf(disk_header,
sizeof (disk_header),
"%-*.*s r/%c w/%c "
"%cr/%c %cw/%c wait actv "
"svc_t %%%%w %%%%b %s",
iodevs_nl, iodevs_nl, "device",
ch, ch, iosz, ch, iosz, ch, header);
} else {
/* with -r option */
(void) snprintf(disk_header,
sizeof (disk_header),
"device,r/%c,w/%c,%cr/%c,%cw/%c,"
"wait,actv,svc_t,%%%%w,"
"%%%%b,%s",
ch, ch, iosz, ch, iosz, ch, header);
}
} else {
/* with -n option */
if (do_raw == 0) {
fstr = " r/%c w/%c %cr/%c "
"%cw/%c wait actv wsvc_t asvc_t "
"%%%%w %%%%b %sdevice";
} else {
fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
"wait,actv,wsvc_t,asvc_t,"
"%%%%w,%%%%b,%sdevice";
}
(void) snprintf(disk_header,
sizeof (disk_header),
fstr, ch, ch, iosz, ch, iosz,
ch, header);
}
break;
default:
break;
}
/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
if ((do_disk & DISK_ERRORS) &&
((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
if (!do_conversions) {
if (do_raw == 0)
(void) snprintf(disk_header,
sizeof (disk_header), "%-*.*s %s",
iodevs_nl, iodevs_nl, "device", header);
else
(void) snprintf(disk_header,
sizeof (disk_header), "device,%s", header);
} else {
if (do_raw == 0) {
(void) snprintf(disk_header,
sizeof (disk_header),
" %sdevice", header);
} else {
(void) snprintf(disk_header,
sizeof (disk_header),
"%s,device", header);
}
}
} else {
/*
* Need to subtract two characters for the % escape in
* the string.
*/
dh_len = strlen(disk_header) - 2;
}
if (do_timestamp)
setup(print_timestamp);
/*
* -n *and* (-E *or* -e *or* -x)
*/
if (do_conversions && (do_disk & PRINT_VERTICAL)) {
if (do_tty)
setup(print_tty_hdr1);
if (do_cpu)
setup(print_cpu_hdr1);
if (do_tty || do_cpu)
setup(do_newline);
if (do_tty)
setup(print_tty_hdr2);
if (do_cpu)
setup(print_cpu_hdr2);
if (do_tty || do_cpu)
setup(do_newline);
if (do_tty)
setup(print_tty_data);
if (do_cpu)
setup(print_cpu_data);
if (do_tty || do_cpu)
setup(do_newline);
printxhdr();
setup(show_all_disks);
} else {
/*
* These unholy gymnastics are necessary to place CPU/tty
* data to the right of the disks/errors for the first
* line in vertical mode.
*/
if (do_disk & PRINT_VERTICAL) {
printxhdr();
setup(show_first_disk);
if (do_tty)
setup(print_tty_data);
if (do_cpu)
setup(print_cpu_data);
setup(do_newline);
setup(show_other_disks);
} else {
setup(hdrout);
if (do_tty)
setup(print_tty_data);
setup(show_all_disks);
if (do_cpu)
setup(print_cpu_data);
}
setup(do_newline);
}
if (do_disk & DISK_EXTENDED_ERRORS)
setup(disk_errors);
}
/*
* Add a new function to the list of functions
* for this invocation. Once on the stack the
* function is never removed nor does its place
* change.
*/
void
setup(void (*nfunc)(void))
{
format_t *tmp;
tmp = safe_alloc(sizeof (format_t));
tmp->nfunc = nfunc;
tmp->next = 0;
if (formatter_end)
formatter_end->next = tmp;
else
formatter_list = tmp;
formatter_end = tmp;
}
/*
* The functions after this comment are devoted to printing
* various parts of the header. They are selected based on the
* options provided when the program was invoked. The functions
* are either directly invoked in printhdr() or are indirectly
* invoked by being placed on the list of functions used when
* extended headers are used.
*/
void
print_tty_hdr1(void)
{
char *fstr;
char *dstr;
if (do_raw == 0) {
fstr = "%10.10s";
dstr = "tty ";
} else {
fstr = "%s";
dstr = "tty";
}
push_out(fstr, dstr);
}
void
print_tty_hdr2(void)
{
if (do_raw == 0)
push_out("%-10.10s", " tin tout");
else
push_out("tin,tout");
}
void
print_cpu_hdr1(void)
{
char *dstr;
if (do_raw == 0)
dstr = " cpu";
else
dstr = "cpu";
push_out(dstr);
}
void
print_cpu_hdr2(void)
{
char *dstr;
if (do_raw == 0)
dstr = " us sy wt id";
else
dstr = "us,sy,wt,id";
push_out(dstr);
}
/*
* Assumption is that tty data is always first - no need for raw mode leading
* comma.
*/
void
print_tty_data(void)
{
char *fstr;
uint64_t deltas;
double raw;
double outch;
kstat_t *oldks = NULL;
if (oldss)
oldks = &oldss->s_sys.ss_agg_sys;
if (do_raw == 0)
fstr = " %3.0f %4.0f ";
else
fstr = "%.0f,%.0f";
deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
raw = deltas;
raw /= getime;
deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
outch = deltas;
outch /= getime;
push_out(fstr, raw, outch);
}
/*
* Write out CPU data
*/
void
print_cpu_data(void)
{
char *fstr;
uint64_t idle;
uint64_t user;
uint64_t kern;
uint64_t wait;
kstat_t *oldks = NULL;
if (oldss)
oldks = &oldss->s_sys.ss_agg_sys;
if (do_raw == 0)
fstr = " %2.0f %2.0f %2.0f %2.0f";
else
fstr = "%.0f,%.0f,%.0f,%.0f";
idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
push_out(fstr, user * percent, kern * percent,
wait * percent, idle * percent);
}
/*
* Emit the appropriate header.
*/
void
hdrout(void)
{
if (do_raw == 0) {
if (--tohdr == 0)
printhdr(0);
} else if (hdr_out == 0) {
printhdr(0);
hdr_out = 1;
}
}
/*
* Write out disk errors when -E is specified.
*/
void
disk_errors(void)
{
(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
}
void
show_first_disk(void)
{
int count = 0;
show_disk_mode = SHOW_FIRST_ONLY;
(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
}
void
show_other_disks(void)
{
int count = 0;
show_disk_mode = SHOW_SECOND_ONWARDS;
(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
}
void
show_all_disks(void)
{
int count = 0;
show_disk_mode = SHOW_ALL;
(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
}
/*
* Write a newline out and clear the lineout flag.
*/
static void
do_newline(void)
{
if (lineout) {
(void) putchar('\n');
lineout = 0;
}
}
/*
* Generalized printf function that determines what extra
* to print out if we're in raw mode. At this time we
* don't care about errors.
*/
static void
push_out(const char *message, ...)
{
va_list args;
va_start(args, message);
if (do_raw && lineout == 1)
(void) putchar(',');
(void) vprintf(message, args);
va_end(args);
lineout = 1;
}
/*
* Emit the header string when -e is specified.
*/
static void
print_err_hdr(void)
{
char obuf[SMALL_SCRATCH_BUFLEN];
if (do_raw) {
push_out("errors");
return;
}
if (do_conversions == 0) {
if (!(do_disk & DISK_EXTENDED)) {
(void) snprintf(obuf, sizeof (obuf),
"%11s", one_blank);
push_out(obuf);
}
} else if (do_disk == DISK_ERRORS)
push_out(two_blanks);
else
push_out(one_blank);
push_out("---- errors --- ");
}
/*
* Emit the header string when -e is specified.
*/
static void
print_disk_header(void)
{
push_out(disk_header);
}
/*
* Write out a timestamp. Format is all that goes out on
* the line so no use of push_out.
*
* Write out as decimal reprentation of time_t value
* (-T u was specified) or the string returned from
* ctime() (-T d was specified).
*/
static void
print_timestamp(void)
{
time_t t;
if (time(&t) != -1) {
if (do_timestamp == UDATE) {
(void) printf("%ld\n", t);
} else if (do_timestamp == CDATE) {
char *cpt;
cpt = ctime(&t);
if (cpt) {
(void) fputs(cpt, stdout);
}
}
}
}
/*
* No, UINTMAX_MAX isn't the right thing here since
* it is #defined to be either INT32_MAX or INT64_MAX
* depending on the whether _LP64 is defined.
*
* We want to handle the odd future case of having
* ulonglong_t be more than 64 bits but we have
* no nice #define MAX value we can drop in place
* without having to change this code in the future.
*/
u_longlong_t
ull_delta(u_longlong_t old, u_longlong_t new)
{
if (new >= old)
return (new - old);
else
return ((UINT64_MAX - old) + new + 1);
}
/*
* Take the difference of an unsigned 32
* bit int attempting to cater for
* overflow.
*/
uint_t
u32_delta(uint_t old, uint_t new)
{
if (new >= old)
return (new - old);
else
return ((UINT32_MAX - old) + new + 1);
}
/*
* This is exactly what is needed for standard iostat output,
* but make sure to use it only for that
*/
#define EPSILON (0.1)
static int
fzero(double value)
{
return (value >= 0.0 && value < EPSILON);
}
static int
safe_strtoi(char const *val, char *errmsg)
{
char *end;
long tmp;
errno = 0;
tmp = strtol(val, &end, 10);
if (*end != '\0' || errno)
fail(0, "%s %s", errmsg, val);
return ((int)tmp);
}
|