forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iostat.c
1617 lines (1438 loc) · 43.6 KB
/
iostat.c
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
/*
* iostat: report CPU and I/O statistics
* (C) 1998-2013 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include "version.h"
#include "iostat.h"
#include "common.h"
#include "ioconf.h"
#include "rd_stats.h"
#include "count.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
#define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
char *sccsid(void) { return (SCCSID); }
struct stats_cpu *st_cpu[2];
unsigned long long uptime[2] = {0, 0};
unsigned long long uptime0[2] = {0, 0};
struct io_stats *st_iodev[2];
struct io_hdr_stats *st_hdr_iodev;
struct io_dlist *st_dev_list;
/* Last group name entered on the command line */
char group_name[MAX_NAME_LEN];
int iodev_nr = 0; /* Nb of devices and partitions found. Includes nb of device groups */
int group_nr = 0; /* Nb of device groups */
int cpu_nr = 0; /* Nb of processors on the machine */
int dlist_idx = 0; /* Nb of devices entered on the command line */
int flags = 0; /* Flag for common options and system state */
unsigned int dm_major; /* Device-mapper major number */
long interval = 0;
char timestamp[64];
struct sigaction alrm_act;
/*
***************************************************************************
* Print usage and exit.
*
* IN:
* @progname Name of sysstat command.
***************************************************************************
*/
void usage(char *progname)
{
fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
progname);
#ifdef DEBUG
fprintf(stderr, _("Options are:\n"
"[ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]\n"
"[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
"[ [ -T ] -g <group_name> ] [ -p [ <device> [,...] | ALL ] ]\n"
"[ <device> [...] | ALL ] [ --debuginfo ]\n"));
#else
fprintf(stderr, _("Options are:\n"
"[ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]\n"
"[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
"[ [ -T ] -g <group_name> ] [ -p [ <device> [,...] | ALL ] ]\n"
"[ <device> [...] | ALL ]\n"));
#endif
exit(1);
}
/*
***************************************************************************
* Set disk output unit. Unit will be kB/s unless POSIXLY_CORRECT
* environment variable has been set, in which case the output will be
* expressed in blocks/s.
***************************************************************************
*/
void set_disk_output_unit(void)
{
char *e;
if (DISPLAY_KILOBYTES(flags) || DISPLAY_MEGABYTES(flags))
return;
/* Check POSIXLY_CORRECT environment variable */
if ((e = getenv(ENV_POSIXLY_CORRECT)) == NULL) {
/* Variable not set: Unit is kB/s and not blocks/s */
flags |= I_D_KILOBYTES;
}
}
/*
***************************************************************************
* SIGALRM signal handler. No need to reset the handler here.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void alarm_handler(int sig)
{
alarm(interval);
}
/*
***************************************************************************
* Initialize stats common structures.
***************************************************************************
*/
void init_stats(void)
{
int i;
/* Allocate structures for CPUs "all" and 0 */
for (i = 0; i < 2; i++) {
if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * 2)) == NULL) {
perror("malloc");
exit(4);
}
memset(st_cpu[i], 0, STATS_CPU_SIZE * 2);
}
}
/*
***************************************************************************
* Set every device entry to unregistered status. But don't change status
* for group entries (whose status is DISK_GROUP).
*
* IN:
* @iodev_nr Number of devices and partitions.
* @st_hdr_iodev Pointer on first structure describing a device/partition.
***************************************************************************
*/
void set_entries_unregistered(int iodev_nr, struct io_hdr_stats *st_hdr_iodev)
{
int i;
struct io_hdr_stats *shi = st_hdr_iodev;
for (i = 0; i < iodev_nr; i++, shi++) {
if (shi->status == DISK_REGISTERED) {
shi->status = DISK_UNREGISTERED;
}
}
}
/*
***************************************************************************
* Free unregistered entries (mark them as unused).
*
* IN:
* @iodev_nr Number of devices and partitions.
* @st_hdr_iodev Pointer on first structure describing a device/partition.
***************************************************************************
*/
void free_unregistered_entries(int iodev_nr, struct io_hdr_stats *st_hdr_iodev)
{
int i;
struct io_hdr_stats *shi = st_hdr_iodev;
for (i = 0; i < iodev_nr; i++, shi++) {
if (shi->status == DISK_UNREGISTERED) {
shi->used = FALSE;
}
}
}
/*
***************************************************************************
* Allocate and init I/O device structures.
*
* IN:
* @dev_nr Number of devices and partitions (also including groups
* if option -g has been used).
***************************************************************************
*/
void salloc_device(int dev_nr)
{
int i;
for (i = 0; i < 2; i++) {
if ((st_iodev[i] =
(struct io_stats *) malloc(IO_STATS_SIZE * dev_nr)) == NULL) {
perror("malloc");
exit(4);
}
memset(st_iodev[i], 0, IO_STATS_SIZE * dev_nr);
}
if ((st_hdr_iodev =
(struct io_hdr_stats *) malloc(IO_HDR_STATS_SIZE * dev_nr)) == NULL) {
perror("malloc");
exit(4);
}
memset(st_hdr_iodev, 0, IO_HDR_STATS_SIZE * dev_nr);
}
/*
***************************************************************************
* Allocate structures for devices entered on the command line.
*
* IN:
* @list_len Number of arguments on the command line.
***************************************************************************
*/
void salloc_dev_list(int list_len)
{
if ((st_dev_list = (struct io_dlist *) malloc(IO_DLIST_SIZE * list_len)) == NULL) {
perror("malloc");
exit(4);
}
memset(st_dev_list, 0, IO_DLIST_SIZE * list_len);
}
/*
***************************************************************************
* Free structures used for devices entered on the command line.
***************************************************************************
*/
void sfree_dev_list(void)
{
if (st_dev_list) {
free(st_dev_list);
}
}
/*
***************************************************************************
* Look for the device in the device list and store it if not found.
*
* IN:
* @dlist_idx Length of the device list.
* @device_name Name of the device.
*
* OUT:
* @dlist_idx Length of the device list.
*
* RETURNS:
* Position of the device in the list.
***************************************************************************
*/
int update_dev_list(int *dlist_idx, char *device_name)
{
int i;
struct io_dlist *sdli = st_dev_list;
for (i = 0; i < *dlist_idx; i++, sdli++) {
if (!strcmp(sdli->dev_name, device_name))
break;
}
if (i == *dlist_idx) {
/*
* Device not found: Store it.
* Group names will be distinguished from real device names
* thanks to their names which begin with a space.
*/
(*dlist_idx)++;
strncpy(sdli->dev_name, device_name, MAX_NAME_LEN - 1);
}
return i;
}
/*
***************************************************************************
* Allocate and init structures, according to system state.
***************************************************************************
*/
void io_sys_init(void)
{
/* Allocate and init stat common counters */
init_stats();
/* How many processors on this machine? */
cpu_nr = get_cpu_nr(~0);
/* Get number of block devices and partitions in /proc/diskstats */
if ((iodev_nr = get_diskstats_dev_nr(CNT_PART, CNT_ALL_DEV)) > 0) {
flags |= I_F_HAS_DISKSTATS;
iodev_nr += NR_DEV_PREALLOC;
}
if (!HAS_DISKSTATS(flags) ||
(DISPLAY_PARTITIONS(flags) && !DISPLAY_PART_ALL(flags))) {
/*
* If /proc/diskstats exists but we also want stats for the partitions
* of a particular device, stats will have to be found in /sys. So we
* need to know if /sys is mounted or not, and set flags accordingly.
*/
/* Get number of block devices (and partitions) in sysfs */
if ((iodev_nr = get_sysfs_dev_nr(DISPLAY_PARTITIONS(flags))) > 0) {
flags |= I_F_HAS_SYSFS;
iodev_nr += NR_DEV_PREALLOC;
}
else {
fprintf(stderr, _("Cannot find disk data\n"));
exit(2);
}
}
/* Also allocate stat structures for "group" devices */
iodev_nr += group_nr;
/*
* Allocate structures for number of disks found, but also
* for groups of devices if option -g has been entered on the command line.
* iodev_nr must be <> 0.
*/
salloc_device(iodev_nr);
}
/*
***************************************************************************
* When group stats are to be displayed (option -g entered on the command
* line), save devices and group names in the io_hdr_stats structures. This
* is normally done later when stats are actually read from /proc or /sys
* files (via a call to save_stats() function), but here we want to make
* sure that the structures are ordered and that each device belongs to its
* proper group.
* Note that we can still have an unexpected device that gets attached to a
* group as devices can be registered or unregistered dynamically.
***************************************************************************
*/
void presave_device_list(void)
{
int i;
struct io_hdr_stats *shi = st_hdr_iodev;
struct io_dlist *sdli = st_dev_list;
if (dlist_idx>0) {
/* First, save the last group name entered on the command line in the list */
update_dev_list(&dlist_idx, group_name);
/* Now save devices and group names in the io_hdr_stats structures */
for (i = 0; (i < dlist_idx) && (i < iodev_nr); i++, shi++, sdli++) {
strcpy(shi->name, sdli->dev_name);
shi->used = TRUE;
if (shi->name[0] == ' ') {
/* Current device name is in fact the name of a group */
shi->status = DISK_GROUP;
}
else {
shi->status = DISK_REGISTERED;
}
}
}
else {
/*
* No device names have been entered on the command line but
* the name of a group. Save that name at the end of the
* data table so that all devices that will be read will be
* included in that group.
*/
shi += iodev_nr - 1;
strcpy(shi->name, group_name);
shi->used = TRUE;
shi->status = DISK_GROUP;
}
}
/*
***************************************************************************
* Free various structures.
***************************************************************************
*/
void io_sys_free(void)
{
int i;
for (i = 0; i < 2; i++) {
/* Free CPU structures */
if (st_cpu[i]) {
free(st_cpu[i]);
}
/* Free I/O device structures */
if (st_iodev[i]) {
free(st_iodev[i]);
}
}
if (st_hdr_iodev) {
free(st_hdr_iodev);
}
}
/*
***************************************************************************
* Save stats for current device or partition.
*
* IN:
* @name Name of the device/partition.
* @curr Index in array for current sample statistics.
* @st_io Structure with device or partition to save.
* @iodev_nr Number of devices and partitions.
* @st_hdr_iodev Pointer on structures describing a device/partition.
*
* OUT:
* @st_hdr_iodev Pointer on structures describing a device/partition.
***************************************************************************
*/
void save_stats(char *name, int curr, void *st_io, int iodev_nr,
struct io_hdr_stats *st_hdr_iodev)
{
int i;
struct io_hdr_stats *st_hdr_iodev_i;
struct io_stats *st_iodev_i;
/* Look for device in data table */
for (i = 0; i < iodev_nr; i++) {
st_hdr_iodev_i = st_hdr_iodev + i;
if (!strcmp(st_hdr_iodev_i->name, name)) {
break;
}
}
if (i == iodev_nr) {
/*
* This is a new device: Look for an unused entry to store it.
* Thus we are able to handle dynamically registered devices.
*/
for (i = 0; i < iodev_nr; i++) {
st_hdr_iodev_i = st_hdr_iodev + i;
if (!st_hdr_iodev_i->used) {
/* Unused entry found... */
st_hdr_iodev_i->used = TRUE; /* Indicate it is now used */
strcpy(st_hdr_iodev_i->name, name);
st_iodev_i = st_iodev[!curr] + i;
memset(st_iodev_i, 0, IO_STATS_SIZE);
break;
}
}
}
if (i < iodev_nr) {
st_hdr_iodev_i = st_hdr_iodev + i;
if (st_hdr_iodev_i->status == DISK_UNREGISTERED) {
st_hdr_iodev_i->status = DISK_REGISTERED;
}
st_iodev_i = st_iodev[curr] + i;
*st_iodev_i = *((struct io_stats *) st_io);
}
/*
* else it was a new device
* but there was no free structure to store it.
*/
}
/*
***************************************************************************
* Read sysfs stat for current block device or partition.
*
* IN:
* @curr Index in array for current sample statistics.
* @filename File name where stats will be read.
* @dev_name Device or partition name.
*
* RETURNS:
* 0 if file couldn't be opened, 1 otherwise.
***************************************************************************
*/
int read_sysfs_file_stat(int curr, char *filename, char *dev_name)
{
FILE *fp;
struct io_stats sdev;
int i;
unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks;
unsigned long rd_ios, rd_merges_or_rd_sec, wr_ios, wr_merges;
unsigned long rd_sec_or_wr_ios, wr_sec, rd_ticks_or_wr_sec;
/* Try to read given stat file */
if ((fp = fopen(filename, "r")) == NULL)
return 0;
i = fscanf(fp, "%lu %lu %lu %lu %lu %lu %lu %u %u %u %u",
&rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios, &rd_ticks_or_wr_sec,
&wr_ios, &wr_merges, &wr_sec, &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks);
if (i == 11) {
/* Device or partition */
sdev.rd_ios = rd_ios;
sdev.rd_merges = rd_merges_or_rd_sec;
sdev.rd_sectors = rd_sec_or_wr_ios;
sdev.rd_ticks = (unsigned int) rd_ticks_or_wr_sec;
sdev.wr_ios = wr_ios;
sdev.wr_merges = wr_merges;
sdev.wr_sectors = wr_sec;
sdev.wr_ticks = wr_ticks;
sdev.ios_pgr = ios_pgr;
sdev.tot_ticks = tot_ticks;
sdev.rq_ticks = rq_ticks;
}
else if (i == 4) {
/* Partition without extended statistics */
sdev.rd_ios = rd_ios;
sdev.rd_sectors = rd_merges_or_rd_sec;
sdev.wr_ios = rd_sec_or_wr_ios;
sdev.wr_sectors = rd_ticks_or_wr_sec;
}
if ((i == 11) || !DISPLAY_EXTENDED(flags)) {
/*
* In fact, we _don't_ save stats if it's a partition without
* extended stats and yet we want to display ext stats.
*/
save_stats(dev_name, curr, &sdev, iodev_nr, st_hdr_iodev);
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read sysfs stats for all the partitions of a device.
*
* IN:
* @curr Index in array for current sample statistics.
* @dev_name Device name.
***************************************************************************
*/
void read_sysfs_dlist_part_stat(int curr, char *dev_name)
{
DIR *dir;
struct dirent *drd;
char dfile[MAX_PF_NAME], filename[MAX_PF_NAME];
snprintf(dfile, MAX_PF_NAME, "%s/%s", SYSFS_BLOCK, dev_name);
dfile[MAX_PF_NAME - 1] = '\0';
/* Open current device directory in /sys/block */
if ((dir = opendir(dfile)) == NULL)
return;
/* Get current entry */
while ((drd = readdir(dir)) != NULL) {
if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
continue;
snprintf(filename, MAX_PF_NAME, "%s/%s/%s", dfile, drd->d_name, S_STAT);
filename[MAX_PF_NAME - 1] = '\0';
/* Read current partition stats */
read_sysfs_file_stat(curr, filename, drd->d_name);
}
/* Close device directory */
closedir(dir);
}
/*
***************************************************************************
* Read stats from the sysfs filesystem for the devices entered on the
* command line.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_sysfs_dlist_stat(int curr)
{
int dev, ok;
char filename[MAX_PF_NAME];
char *slash;
struct io_dlist *st_dev_list_i;
/* Every I/O device (or partition) is potentially unregistered */
set_entries_unregistered(iodev_nr, st_hdr_iodev);
for (dev = 0; dev < dlist_idx; dev++) {
st_dev_list_i = st_dev_list + dev;
/* Some devices may have a slash in their name (eg. cciss/c0d0...) */
while ((slash = strchr(st_dev_list_i->dev_name, '/'))) {
*slash = '!';
}
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
SYSFS_BLOCK, st_dev_list_i->dev_name, S_STAT);
filename[MAX_PF_NAME - 1] = '\0';
/* Read device stats */
ok = read_sysfs_file_stat(curr, filename, st_dev_list_i->dev_name);
if (ok && st_dev_list_i->disp_part) {
/* Also read stats for its partitions */
read_sysfs_dlist_part_stat(curr, st_dev_list_i->dev_name);
}
}
/* Free structures corresponding to unregistered devices */
free_unregistered_entries(iodev_nr, st_hdr_iodev);
}
/*
***************************************************************************
* Read stats from the sysfs filesystem for every block devices found.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_sysfs_stat(int curr)
{
DIR *dir;
struct dirent *drd;
char filename[MAX_PF_NAME];
int ok;
/* Every I/O device entry is potentially unregistered */
set_entries_unregistered(iodev_nr, st_hdr_iodev);
/* Open /sys/block directory */
if ((dir = opendir(SYSFS_BLOCK)) != NULL) {
/* Get current entry */
while ((drd = readdir(dir)) != NULL) {
if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
continue;
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
SYSFS_BLOCK, drd->d_name, S_STAT);
filename[MAX_PF_NAME - 1] = '\0';
/* If current entry is a directory, try to read its stat file */
ok = read_sysfs_file_stat(curr, filename, drd->d_name);
/*
* If '-p ALL' was entered on the command line,
* also try to read stats for its partitions
*/
if (ok && DISPLAY_PART_ALL(flags)) {
read_sysfs_dlist_part_stat(curr, drd->d_name);
}
}
/* Close /sys/block directory */
closedir(dir);
}
/* Free structures corresponding to unregistered devices */
free_unregistered_entries(iodev_nr, st_hdr_iodev);
}
/*
***************************************************************************
* Read stats from /proc/diskstats.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_diskstats_stat(int curr)
{
FILE *fp;
char line[256], dev_name[MAX_NAME_LEN];
char *dm_name;
struct io_stats sdev;
int i;
unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks;
unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_ios;
unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec;
char *ioc_dname;
unsigned int major, minor;
/* Every I/O device entry is potentially unregistered */
set_entries_unregistered(iodev_nr, st_hdr_iodev);
if ((fp = fopen(DISKSTATS, "r")) == NULL)
return;
while (fgets(line, 256, fp) != NULL) {
/* major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq */
i = sscanf(line, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u %u %u %u",
&major, &minor, dev_name,
&rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios, &rd_ticks_or_wr_sec,
&wr_ios, &wr_merges, &wr_sec, &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks);
if (i == 14) {
/* Device or partition */
if (!dlist_idx && !DISPLAY_PARTITIONS(flags) &&
!is_device(dev_name, ACCEPT_VIRTUAL_DEVICES))
continue;
sdev.rd_ios = rd_ios;
sdev.rd_merges = rd_merges_or_rd_sec;
sdev.rd_sectors = rd_sec_or_wr_ios;
sdev.rd_ticks = (unsigned int) rd_ticks_or_wr_sec;
sdev.wr_ios = wr_ios;
sdev.wr_merges = wr_merges;
sdev.wr_sectors = wr_sec;
sdev.wr_ticks = wr_ticks;
sdev.ios_pgr = ios_pgr;
sdev.tot_ticks = tot_ticks;
sdev.rq_ticks = rq_ticks;
}
else if (i == 7) {
/* Partition without extended statistics */
if (DISPLAY_EXTENDED(flags) ||
(!dlist_idx && !DISPLAY_PARTITIONS(flags)))
continue;
sdev.rd_ios = rd_ios;
sdev.rd_sectors = rd_merges_or_rd_sec;
sdev.wr_ios = rd_sec_or_wr_ios;
sdev.wr_sectors = rd_ticks_or_wr_sec;
}
else
/* Unknown entry: Ignore it */
continue;
if ((ioc_dname = ioc_name(major, minor)) != NULL) {
if (strcmp(dev_name, ioc_dname) && strcmp(ioc_dname, K_NODEV)) {
/*
* No match: Use name generated from sysstat.ioconf data
* (if different from "nodev") works around known issues
* with EMC PowerPath.
*/
strncpy(dev_name, ioc_dname, MAX_NAME_LEN);
}
}
if ((DISPLAY_DEVMAP_NAME(flags)) && (major == dm_major)) {
/*
* If the device is a device mapper device, try to get its
* assigned name of its logical device.
*/
dm_name = transform_devmapname(major, minor);
if (dm_name) {
strncpy(dev_name, dm_name, MAX_NAME_LEN);
}
}
save_stats(dev_name, curr, &sdev, iodev_nr, st_hdr_iodev);
}
fclose(fp);
/* Free structures corresponding to unregistered devices */
free_unregistered_entries(iodev_nr, st_hdr_iodev);
}
/*
***************************************************************************
* Compute stats for device groups using stats of every device belonging
* to each of these groups.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void compute_device_groups_stats(int curr)
{
struct io_stats gdev, *ioi;
struct io_hdr_stats *shi = st_hdr_iodev;
int i, nr_disks;
memset(&gdev, 0, IO_STATS_SIZE);
nr_disks = 0;
for (i = 0; i < iodev_nr; i++, shi++) {
if (shi->used && (shi->status == DISK_REGISTERED)) {
ioi = st_iodev[curr] + i;
if (!DISPLAY_UNFILTERED(flags)) {
if (!ioi->rd_ios && !ioi->wr_ios)
continue;
}
gdev.rd_ios += ioi->rd_ios;
gdev.rd_merges += ioi->rd_merges;
gdev.rd_sectors += ioi->rd_sectors;
gdev.rd_ticks += ioi->rd_ticks;
gdev.wr_ios += ioi->wr_ios;
gdev.wr_merges += ioi->wr_merges;
gdev.wr_sectors += ioi->wr_sectors;
gdev.wr_ticks += ioi->wr_ticks;
gdev.ios_pgr += ioi->ios_pgr;
gdev.tot_ticks += ioi->tot_ticks;
gdev.rq_ticks += ioi->rq_ticks;
nr_disks++;
}
else if (shi->status == DISK_GROUP) {
save_stats(shi->name, curr, &gdev, iodev_nr, st_hdr_iodev);
shi->used = nr_disks;
nr_disks = 0;
memset(&gdev, 0, IO_STATS_SIZE);
}
}
}
/*
***************************************************************************
* Display CPU utilization.
*
* IN:
* @curr Index in array for current sample statistics.
* @itv Interval of time.
***************************************************************************
*/
void write_cpu_stat(int curr, unsigned long long itv)
{
printf("avg-cpu: %%user %%nice %%system %%iowait %%steal %%idle\n");
printf(" %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n\n",
ll_sp_value(st_cpu[!curr]->cpu_user, st_cpu[curr]->cpu_user, itv),
ll_sp_value(st_cpu[!curr]->cpu_nice, st_cpu[curr]->cpu_nice, itv),
/*
* Time spent in system mode also includes time spent servicing
* hard and soft interrupts.
*/
ll_sp_value(st_cpu[!curr]->cpu_sys + st_cpu[!curr]->cpu_softirq +
st_cpu[!curr]->cpu_hardirq,
st_cpu[curr]->cpu_sys + st_cpu[curr]->cpu_softirq +
st_cpu[curr]->cpu_hardirq, itv),
ll_sp_value(st_cpu[!curr]->cpu_iowait, st_cpu[curr]->cpu_iowait, itv),
ll_sp_value(st_cpu[!curr]->cpu_steal, st_cpu[curr]->cpu_steal, itv),
(st_cpu[curr]->cpu_idle < st_cpu[!curr]->cpu_idle) ?
0.0 :
ll_sp_value(st_cpu[!curr]->cpu_idle, st_cpu[curr]->cpu_idle, itv));
}
/*
***************************************************************************
* Display disk stats header.
*
* OUT:
* @fctr Conversion factor.
***************************************************************************
*/
void write_disk_stat_header(int *fctr)
{
if (DISPLAY_EXTENDED(flags)) {
/* Extended stats */
printf("Device: rrqm/s wrqm/s r/s w/s");
if (DISPLAY_MEGABYTES(flags)) {
printf(" rMB/s wMB/s");
*fctr = 2048;
}
else if (DISPLAY_KILOBYTES(flags)) {
printf(" rkB/s wkB/s");
*fctr = 2;
}
else {
printf(" rsec/s wsec/s");
}
printf(" avgrq-sz avgqu-sz await r_await w_await svctm %%util\n");
}
else {
/* Basic stats */
printf("Device: tps");
if (DISPLAY_KILOBYTES(flags)) {
printf(" kB_read/s kB_wrtn/s kB_read kB_wrtn\n");
*fctr = 2;
}
else if (DISPLAY_MEGABYTES(flags)) {
printf(" MB_read/s MB_wrtn/s MB_read MB_wrtn\n");
*fctr = 2048;
}
else {
printf(" Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn\n");
}
}
}
/*
***************************************************************************
* Display extended stats, read from /proc/{diskstats,partitions} or /sys.
*
* IN:
* @curr Index in array for current sample statistics.
* @itv Interval of time.
* @fctr Conversion factor.
* @shi Structures describing the devices and partitions.
* @ioi Current sample statistics.
* @ioj Previous sample statistics.
***************************************************************************
*/
void write_ext_stat(int curr, unsigned long long itv, int fctr,
struct io_hdr_stats *shi, struct io_stats *ioi,
struct io_stats *ioj)
{
char *devname = NULL;
struct stats_disk sdc, sdp;
struct ext_disk_stats xds;
double r_await, w_await;
/*
* Counters overflows are possible, but don't need to be handled in
* a special way: The difference is still properly calculated if the
* result is of the same type as the two values.
* Exception is field rq_ticks which is incremented by the number of
* I/O in progress times the number of milliseconds spent doing I/O.
* But the number of I/O in progress (field ios_pgr) happens to be
* sometimes negative...
*/
sdc.nr_ios = ioi->rd_ios + ioi->wr_ios;
sdp.nr_ios = ioj->rd_ios + ioj->wr_ios;
sdc.tot_ticks = ioi->tot_ticks;
sdp.tot_ticks = ioj->tot_ticks;
sdc.rd_ticks = ioi->rd_ticks;
sdp.rd_ticks = ioj->rd_ticks;
sdc.wr_ticks = ioi->wr_ticks;
sdp.wr_ticks = ioj->wr_ticks;
sdc.rd_sect = ioi->rd_sectors;
sdp.rd_sect = ioj->rd_sectors;
sdc.wr_sect = ioi->wr_sectors;
sdp.wr_sect = ioj->wr_sectors;
compute_ext_disk_stats(&sdc, &sdp, itv, &xds);
r_await = (ioi->rd_ios - ioj->rd_ios) ?
(ioi->rd_ticks - ioj->rd_ticks) /
((double) (ioi->rd_ios - ioj->rd_ios)) : 0.0;
w_await = (ioi->wr_ios - ioj->wr_ios) ?
(ioi->wr_ticks - ioj->wr_ticks) /
((double) (ioi->wr_ios - ioj->wr_ios)) : 0.0;
/* Print device name */
if (DISPLAY_PERSIST_NAME_I(flags)) {
devname = get_persistent_name_from_pretty(shi->name);
}
if (!devname) {
devname = shi->name;
}
if (DISPLAY_HUMAN_READ(flags)) {
printf("%s\n%13s", devname, "");
}
else {
printf("%-13s", devname);
}
/* rrq/s wrq/s r/s w/s rsec wsec rqsz qusz await r_await w_await svctm %util */
printf(" %8.2f %8.2f %7.2f %7.2f %8.2f %8.2f %8.2f %8.2f %7.2f %7.2f %7.2f %6.2f %6.2f\n",
S_VALUE(ioj->rd_merges, ioi->rd_merges, itv),
S_VALUE(ioj->wr_merges, ioi->wr_merges, itv),
S_VALUE(ioj->rd_ios, ioi->rd_ios, itv),
S_VALUE(ioj->wr_ios, ioi->wr_ios, itv),
ll_s_value(ioj->rd_sectors, ioi->rd_sectors, itv) / fctr,
ll_s_value(ioj->wr_sectors, ioi->wr_sectors, itv) / fctr,
xds.arqsz,
S_VALUE(ioj->rq_ticks, ioi->rq_ticks, itv) / 1000.0,
xds.await,
r_await,
w_await,
/* The ticks output is biased to output 1000 ticks per second */
xds.svctm,
/*
* Again: Ticks in milliseconds.
* In the case of a device group (option -g), shi->used is the number of
* devices in the group. Else shi->used equals 1.
*/
shi->used ? xds.util / 10.0 / (double) shi->used
: xds.util / 10.0); /* shi->used should never be null here */
}
/*
***************************************************************************
* Write basic stats, read from /proc/diskstats or from sysfs.
*
* IN:
* @curr Index in array for current sample statistics.
* @itv Interval of time.
* @fctr Conversion factor.
* @shi Structures describing the devices and partitions.