forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iostat.c
2168 lines (1944 loc) · 60.2 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-2019 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 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 "ioconf.h"
#include "rd_stats.h"
#include "count.h"
#include <locale.h> /* For setlocale() */
#ifdef USE_NLS
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
#ifdef USE_SCCSID
#define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
char *sccsid(void) { return (SCCSID); }
#endif
#ifdef TEST
extern int __env;
#endif
struct stats_cpu *st_cpu[2];
unsigned long long uptime_cs[2] = {0, 0};
unsigned long long tot_jiffies[2] = {0, 0};
struct io_device *dev_list;
/* Number of decimal places */
int dplaces_nr = -1;
int group_nr = 0; /* Nb of device groups */
int cpu_nr = 0; /* Nb of processors on the machine */
int flags = 0; /* Flag for common options and system state */
unsigned int dm_major; /* Device-mapper major number */
long interval = 0;
char timestamp[TIMESTAMP_LEN];
struct sigaction alrm_act, int_act;
int sigint_caught = 0;
/*
***************************************************************************
* 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 ] [ -s ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]\n"
"[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
"[ --dec={ 0 | 1 | 2 } ] [ --human ] [ -o JSON ]\n"
"[ [ -H ] -g <group_name> ] [ -p [ <device> [,...] | ALL ] ]\n"
"[ <device> [...] | ALL ] [ --debuginfo ]\n"));
#else
fprintf(stderr, _("Options are:\n"
"[ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -s ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]\n"
"[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
"[ --dec={ 0 | 1 | 2 } ] [ --human ] [ -o JSON ]\n"
"[ [ -H ] -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)
{
if (DISPLAY_KILOBYTES(flags) || DISPLAY_MEGABYTES(flags))
return;
/* Check POSIXLY_CORRECT environment variable */
if (__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);
}
/*
***************************************************************************
* SIGINT signal handler.
*
* IN:
* @sig Signal number.
**************************************************************************
*/
void int_handler(int sig)
{
sigint_caught = 1;
}
/*
***************************************************************************
* 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 nonexistent status.
*
* IN:
* @dlist Pointer on the start of the linked list.
***************************************************************************
*/
void set_devices_nonexistent(struct io_device *dlist)
{
while (dlist != NULL) {
dlist->exist = FALSE;
dlist = dlist->next;
}
}
/*
***************************************************************************
* Check if a device is present in the list, and add it if requested.
* Also look for its type (device or partition) and save it.
*
* IN:
* @dlist Address of pointer on the start of the linked list.
* @name Device name.
* @dtype T_PART_DEV (=2) if the device and all its partitions should
* also be read (option -p used), T_GROUP (=3) if it's a group
* name, and 0 otherwise.
*
* RETURNS:
* Pointer on the io_device structure in the list where the device is located
* (whether it was already in the list or if it has been added).
* NULL if the device name is too long or if the device doesn't exist and we
* don't want to add it.
***************************************************************************
*/
struct io_device *add_list_device(struct io_device **dlist, char *name, int dtype)
{
struct io_device *d, *ds;
int i;
if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN)
/* Device name is too long */
return NULL;
while (*dlist != NULL) {
d = *dlist;
if ((i = strcmp(d->name, name)) == 0) {
/* Device found in list */
if ((dtype == T_PART_DEV) && (d->dev_tp == T_DEV)) {
d->dev_tp = dtype;
}
d->exist = TRUE;
return d;
}
if (!GROUP_DEFINED(flags) && !DISPLAY_EVERYTHING(flags) && (i > 0))
/*
* If no group defined and we don't use /proc/diskstats,
* insert current device in alphabetical order.
* NB: Using /proc/diskstats ("iostat -p ALL") is a bit better than
* using alphabetical order because sda10 comes after sda9...
*/
break;
dlist = &(d->next);
}
/* Device not found */
ds = *dlist;
/* Add device to the list */
if ((*dlist = (struct io_device *) malloc(sizeof(struct io_device))) == NULL) {
perror("malloc");
exit(4);
}
memset(*dlist, 0, sizeof(struct io_device));
d = *dlist;
for (i = 0; i < 2; i++) {
if ((d->dev_stats[i] = (struct io_stats *) malloc(sizeof(struct io_stats))) == NULL) {
perror("malloc");
exit(4);
}
memset(d->dev_stats[i], 0, sizeof(struct io_stats));
}
strncpy(d->name, name, MAX_NAME_LEN);
d->name[MAX_NAME_LEN - 1] = '\0';
d->exist = TRUE;
d->next = ds;
if (dtype == T_GROUP) {
d->dev_tp = dtype;
}
else if (is_device(name, ACCEPT_VIRTUAL_DEVICES)) {
d->dev_tp = (dtype == T_PART_DEV ? T_PART_DEV : T_DEV);
}
else {
/* This is a partition (T_PART) */
d->dev_tp = T_PART;
}
return d;
}
/*
***************************************************************************
* Get device major and minor numbers.
*
* IN:
* @filename Name of the device ("sda", "/dev/sdb1"...)
*
* OUT:
* @major Major number of the device.
* @minor Minor number of the device.
*
* RETURNS:
* 0 on success, and -1 otherwise.
***************************************************************************
*/
int get_major_minor_nr(char filename[], int *major, int *minor)
{
struct stat statbuf;
char *bang;
char dfile[MAX_PF_NAME];
snprintf(dfile, sizeof(dfile), "%s%s", filename[0] == '/' ? "" : SLASH_DEV, filename);
dfile[sizeof(dfile) - 1] = '\0';
while ((bang = strchr(dfile, '!'))) {
/*
* Some devices may have had a slash replaced with a bang character (eg. cciss!c0d0...)
* Restore their original names so that they can be found in /dev directory.
*/
*bang = '/';
}
if (__stat(dfile, &statbuf) < 0)
return -1;
*major = major(statbuf.st_rdev);
*minor = minor(statbuf.st_rdev);
return 0;
}
/*
***************************************************************************
* Read sysfs stat for current block device or partition.
*
* IN:
* @filename File name where stats will be read.
* @ios Structure where stats will be saved.
*
* OUT:
* @ios Structure where stats have been saved.
*
* RETURNS:
* 0 on success, -1 otherwise.
***************************************************************************
*/
int read_sysfs_file_stat(char *filename, struct io_stats *ios)
{
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;
unsigned long dc_ios, dc_merges, dc_sec, dc_ticks;
/* Try to read given stat file */
if ((fp = fopen(filename, "r")) == NULL)
return -1;
i = fscanf(fp, "%lu %lu %lu %lu %lu %lu %lu %u %u %u %u %lu %lu %lu %lu",
&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,
&dc_ios, &dc_merges, &dc_sec, &dc_ticks);
memset(&sdev, 0, sizeof(struct io_stats));
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;
if (i == 15) {
/* Discard I/O */
sdev.dc_ios = dc_ios;
sdev.dc_merges = dc_merges;
sdev.dc_sectors = dc_sec;
sdev.dc_ticks = dc_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;
}
*ios = sdev;
fclose(fp);
return 0;
}
/*
***************************************************************************
* Read sysfs stats for all the partitions of a whole device. Devices are
* saved in the linked list.
*
* IN:
* @curr Index in array for current sample statistics.
* @dname Whole device name.
*
* RETURNS:
* 0 on success, -1 otherwise.
***************************************************************************
*/
int read_sysfs_device_part_stat(int curr, char *dname)
{
DIR *dir;
struct dirent *drd;
struct io_stats sdev;
struct io_device *d;
char dfile[MAX_PF_NAME], filename[MAX_PF_NAME + 512];
int major, minor;
snprintf(dfile, sizeof(dfile), "%s/%s", SYSFS_BLOCK, dname);
dfile[sizeof(dfile) - 1] = '\0';
/* Open current device directory in /sys/block */
if ((dir = __opendir(dfile)) == NULL)
return -1;
/* Get current entry */
while ((drd = __readdir(dir)) != NULL) {
if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
continue;
snprintf(filename, sizeof(filename), "%s/%s/%s", dfile, drd->d_name, S_STAT);
filename[sizeof(filename) - 1] = '\0';
/* Read current partition stats */
if (read_sysfs_file_stat(filename, &sdev) < 0)
continue;
d = add_list_device(&dev_list, drd->d_name, 0);
if (d != NULL) {
*(d->dev_stats[curr]) = sdev;
if (!d->major) {
/* Get major and minor numbers for given device */
if (get_major_minor_nr(d->name, &major, &minor) == 0) {
d->major = major;
d->minor = minor;
}
}
}
}
/* Close device directory */
__closedir(dir);
return 0;
}
/*
***************************************************************************
* Read sysfs stats for every whole device. Devices are saved in the linked
* list.
*
* IN:
* @curr Index in array for current sample statistics.
*
* RETURNS:
* 0 on success, -1 otherwise.
***************************************************************************
*/
int read_sysfs_all_devices_stat(int curr)
{
DIR *dir;
struct dirent *drd;
struct io_stats sdev;
struct io_device *d;
char dfile[MAX_PF_NAME];
int major, minor;
/* Open /sys/block directory */
if ((dir = __opendir(SYSFS_BLOCK)) == NULL)
return -1;
/* Get current entry */
while ((drd = __readdir(dir)) != NULL) {
if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
continue;
snprintf(dfile, sizeof(dfile), "%s/%s/%s", SYSFS_BLOCK, drd->d_name, S_STAT);
dfile[sizeof(dfile) - 1] = '\0';
/* Read current whole device stats */
if (read_sysfs_file_stat(dfile, &sdev) < 0)
continue;
d = add_list_device(&dev_list, drd->d_name, 0);
if (d != NULL) {
*(d->dev_stats[curr]) = sdev;
if (!d->major) {
/* Get major and minor numbers for given device */
if (get_major_minor_nr(d->name, &major, &minor) == 0) {
d->major = major;
d->minor = minor;
}
}
}
}
/* Close device directory */
__closedir(dir);
return 0;
}
/*
***************************************************************************
* Read sysfs stats for a partition using /sys/dev/block/M:m/ directory.
*
* IN:
* @curr Index in array for current sample statistics.
* @d Device structure.
*
* RETURNS:
* 0 on success, and -1 otherwise.
***************************************************************************
*/
int read_sysfs_part_stat(int curr, struct io_device *d)
{
char dfile[MAX_PF_NAME];
int major, minor;
if (!d->major) {
/* Get major and minor numbers for given device */
if (get_major_minor_nr(d->name, &major, &minor) < 0)
return -1;
d->major = major;
d->minor = minor;
}
/* Read stats for device */
snprintf(dfile, sizeof(dfile), "%s/%d:%d/%s",
SYSFS_DEV_BLOCK, d->major, d->minor, S_STAT);
dfile[sizeof(dfile) - 1] = '\0';
return read_sysfs_file_stat(dfile, d->dev_stats[curr]);
}
/*
***************************************************************************
* 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)
{
struct io_device *dlist;
char dfile[MAX_PF_NAME];
for (dlist = dev_list; dlist != NULL; dlist = dlist->next) {
if (dlist->exist)
/* Device stats already read */
continue;
else if (dlist->dev_tp == T_PART) {
/*
* This is a partition.
* Read its stats using /sys/dev/block/M:n/ directory.
*/
if (read_sysfs_part_stat(curr, dlist) == 0) {
dlist->exist = TRUE;
}
}
else if ((dlist->dev_tp == T_PART_DEV) || (dlist->dev_tp == T_DEV)) {
/* Read stats for current whole device using /sys/block/ directory */
snprintf(dfile, sizeof(dfile), "%s/%s/%s",
SYSFS_BLOCK, dlist->name, S_STAT);
dfile[sizeof(dfile) - 1] = '\0';
if (read_sysfs_file_stat(dfile, dlist->dev_stats[curr]) == 0) {
dlist->exist = TRUE;
}
if (dlist->dev_tp == T_PART_DEV) {
/* Also read all its partitions now */
read_sysfs_device_part_stat(curr, dlist->name);
}
}
}
/* Read all whole devices stats if requested ("iostat ALL ...") */
if (DISPLAY_ALL_DEVICES(flags)) {
read_sysfs_all_devices_stat(curr);
}
}
/*
***************************************************************************
* Read stats from /proc/diskstats. Only used when "-p ALL" has been entered
* on the command line.
*
* 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];
struct io_device *d;
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;
unsigned long dc_ios, dc_merges, dc_sec, dc_ticks;
unsigned int major, minor;
memset(&sdev, 0, sizeof(struct io_stats));
if ((fp = fopen(DISKSTATS, "r")) == NULL)
return;
while (fgets(line, sizeof(line), fp) != NULL) {
/* major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq dcio dcmerge dcsect dcuse*/
i = sscanf(line, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u %u %u %u %lu %lu %lu %lu",
&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,
&dc_ios, &dc_merges, &dc_sec, &dc_ticks);
if (i >= 14) {
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;
if (i == 18) {
/* Discard I/O */
sdev.dc_ios = dc_ios;
sdev.dc_merges = dc_merges;
sdev.dc_sectors = dc_sec;
sdev.dc_ticks = dc_ticks;
}
}
else if (i == 7) {
/* Partition without extended statistics */
if (DISPLAY_EXTENDED(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;
d = add_list_device(&dev_list, dev_name, 0);
if (d != NULL) {
*d->dev_stats[curr] = sdev;
d->major = major;
d->minor = minor;
}
}
fclose(fp);
}
/*
***************************************************************************
* Add current device statistics to corresponding group.
*
* IN:
* @curr Index in array for current sample statistics.
* @iodev_nr Number of devices and partitions.
***************************************************************************
*/
void compute_device_groups_stats(int curr, struct io_device *d, struct io_device *g)
{
if (!DISPLAY_UNFILTERED(flags)) {
if (!d->dev_stats[curr]->rd_ios &&
!d->dev_stats[curr]->wr_ios &&
!d->dev_stats[curr]->dc_ios)
return;
}
g->dev_stats[curr]->rd_ios += d->dev_stats[curr]->rd_ios;
g->dev_stats[curr]->rd_merges += d->dev_stats[curr]->rd_merges;
g->dev_stats[curr]->rd_sectors += d->dev_stats[curr]->rd_sectors;
g->dev_stats[curr]->rd_ticks += d->dev_stats[curr]->rd_ticks;
g->dev_stats[curr]->wr_ios += d->dev_stats[curr]->wr_ios;
g->dev_stats[curr]->wr_merges += d->dev_stats[curr]->wr_merges;
g->dev_stats[curr]->wr_sectors += d->dev_stats[curr]->wr_sectors;
g->dev_stats[curr]->wr_ticks += d->dev_stats[curr]->wr_ticks;
g->dev_stats[curr]->dc_ios += d->dev_stats[curr]->dc_ios;
g->dev_stats[curr]->dc_merges += d->dev_stats[curr]->dc_merges;
g->dev_stats[curr]->dc_sectors += d->dev_stats[curr]->dc_sectors;
g->dev_stats[curr]->dc_ticks += d->dev_stats[curr]->dc_ticks;
g->dev_stats[curr]->ios_pgr += d->dev_stats[curr]->ios_pgr;
g->dev_stats[curr]->tot_ticks += d->dev_stats[curr]->tot_ticks;
g->dev_stats[curr]->rq_ticks += d->dev_stats[curr]->rq_ticks;
}
/*
***************************************************************************
* Write current sample's timestamp, either in plain or JSON format.
*
* IN:
* @tab Number of tabs to print.
* @rectime Current date and time.
***************************************************************************
*/
void write_sample_timestamp(int tab, struct tm *rectime)
{
if (DISPLAY_ISO(flags)) {
strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
}
else {
strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
}
if (DISPLAY_JSON_OUTPUT(flags)) {
xprintf(tab, "\"timestamp\": \"%s\",", timestamp);
}
else {
printf("%s\n", timestamp);
}
}
/*
***************************************************************************
* Display CPU utilization in plain format.
*
* IN:
* @curr Index in array for current sample statistics.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
***************************************************************************
*/
void write_plain_cpu_stat(int curr, unsigned long long deltot_jiffies)
{
printf("avg-cpu: %%user %%nice %%system %%iowait %%steal %%idle\n");
printf(" ");
cprintf_pc(DISPLAY_UNIT(flags), 6, 7, 2,
ll_sp_value(st_cpu[!curr]->cpu_user, st_cpu[curr]->cpu_user, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_nice, st_cpu[curr]->cpu_nice, deltot_jiffies),
/*
* 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, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_iowait, st_cpu[curr]->cpu_iowait, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_steal, st_cpu[curr]->cpu_steal, deltot_jiffies),
(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, deltot_jiffies));
printf("\n\n");
}
/*
***************************************************************************
* Display CPU utilization in JSON format.
*
* IN:
* @tab Number of tabs to print.
* @curr Index in array for current sample statistics.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
***************************************************************************
*/
void write_json_cpu_stat(int tab, int curr, unsigned long long deltot_jiffies)
{
xprintf0(tab, "\"avg-cpu\": {\"user\": %.2f, \"nice\": %.2f, \"system\": %.2f,"
" \"iowait\": %.2f, \"steal\": %.2f, \"idle\": %.2f}",
ll_sp_value(st_cpu[!curr]->cpu_user, st_cpu[curr]->cpu_user, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_nice, st_cpu[curr]->cpu_nice, deltot_jiffies),
/*
* 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, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_iowait, st_cpu[curr]->cpu_iowait, deltot_jiffies),
ll_sp_value(st_cpu[!curr]->cpu_steal, st_cpu[curr]->cpu_steal, deltot_jiffies),
(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, deltot_jiffies));
}
/*
***************************************************************************
* Display CPU utilization in plain or JSON format.
*
* IN:
* @curr Index in array for current sample statistics.
* @tab Number of tabs to print (JSON format only).
***************************************************************************
*/
void write_cpu_stat(int curr, int tab)
{
unsigned long long deltot_jiffies;
/* Total number of jiffies spent on the interval */
deltot_jiffies = get_interval(tot_jiffies[!curr], tot_jiffies[curr]);
#ifdef DEBUG
if (DISPLAY_DEBUG(flags)) {
/* Debug output */
fprintf(stderr, "deltot_jiffies=%llu st_cpu[curr]{ cpu_user=%llu cpu_nice=%llu "
"cpu_sys=%llu cpu_idle=%llu cpu_iowait=%llu cpu_steal=%llu "
"cpu_hardirq=%llu cpu_softirq=%llu cpu_guest=%llu "
"cpu_guest_nice=%llu }\n",
deltot_jiffies,
st_cpu[curr]->cpu_user,
st_cpu[curr]->cpu_nice,
st_cpu[curr]->cpu_sys,
st_cpu[curr]->cpu_idle,
st_cpu[curr]->cpu_iowait,
st_cpu[curr]->cpu_steal,
st_cpu[curr]->cpu_hardirq,
st_cpu[curr]->cpu_softirq,
st_cpu[curr]->cpu_guest,
st_cpu[curr]->cpu_guest_nice);
}
#endif
if (DISPLAY_JSON_OUTPUT(flags)) {
write_json_cpu_stat(tab, curr, deltot_jiffies);
}
else {
write_plain_cpu_stat(curr, deltot_jiffies);
}
}
/*
***************************************************************************
* Display disk stats header in plain or JSON format.
*
* OUT:
* @fctr Conversion factor.
* @tab Number of tabs to print (JSON format only).
* @hpart Indicate which part of the report should be displayed in
* human mode. A value of 0 indicates that output should not be
* broken in several parts.
***************************************************************************
*/
void write_disk_stat_header(int *fctr, int *tab, int hpart)
{
char *units, *spc;
if (DISPLAY_KILOBYTES(flags)) {
*fctr = 2;
units = "kB";
spc = " ";
}
else if (DISPLAY_MEGABYTES(flags)) {
*fctr = 2048;
units = "MB";
spc = " ";
}
else if (DISPLAY_EXTENDED(flags)) {
units = "sec";
spc = "";
}
else {
units = "Blk";
spc = "";
}
if (DISPLAY_JSON_OUTPUT(flags)) {
xprintf((*tab)++, "\"disk\": [");
return;
}
if (!DISPLAY_HUMAN_READ(flags)) {
printf("Device ");
}
if (DISPLAY_EXTENDED(flags)) {
/* Extended stats */
if (DISPLAY_SHORT_OUTPUT(flags)) {
printf(" tps %s%s/s rqm/s await areq-sz aqu-sz %%util",
spc, units);
}
else {
if ((hpart == 1) || !hpart) {
printf(" r/s %sr%s/s rrqm/s %%rrqm r_await rareq-sz",
spc, units);
}
if ((hpart == 2) || !hpart) {
printf(" w/s %sw%s/s wrqm/s %%wrqm w_await wareq-sz",
spc, units);
}
if ((hpart == 3) || !hpart) {
printf(" d/s %sd%s/s drqm/s %%drqm d_await dareq-sz",
spc, units);
}
if ((hpart == 4) || !hpart) {
printf(" aqu-sz %%util");
}
}
}
else {
/* Basic stats */
if (DISPLAY_SHORT_OUTPUT(flags)) {
printf(" tps %s%s_read/s %s%s_w+d/s %s%s_read %s%s_w+d",
spc, units, spc, units, spc, units, spc, units);
}
else {
printf(" tps %s%s_read/s %s%s_wrtn/s %s%s_dscd/s %s%s_read %s%s_wrtn %s%s_dscd",
spc, units, spc, units, spc, units, spc, units, spc, units, spc, units);
}
}
if (DISPLAY_HUMAN_READ(flags)) {
printf(" Device");
}
printf("\n");
}
/*
***************************************************************************
* Display extended stats, read from /proc/{diskstats,partitions} or /sys,
* in plain format.
*
* IN:
* @itv Interval of time.
* @fctr Conversion factor.
* @hpart Indicate which part of the report should be displayed in
* human mode. A value of 0 indicates that output should not be
* broken in several parts.
* @d Structure containing device description.
* @ioi Current sample statistics.
* @ioj Previous sample statistics.
* @devname Current device name.
* @xds Extended stats for current device.
* @xios Additional extended statistics for current device.
***************************************************************************
*/
void write_plain_ext_stat(unsigned long long itv, int fctr, int hpart,
struct io_device *d, struct io_stats *ioi,
struct io_stats *ioj, char *devname, struct ext_disk_stats *xds,
struct ext_io_stats *xios)
{
int n;
/* If this is a group with no devices, skip it */
if (d->dev_tp == T_GROUP)
return;
if (!DISPLAY_HUMAN_READ(flags)) {
cprintf_in(IS_STR, "%-13s", devname, 0);
}
/* Compute number of devices in group */
if (d->dev_tp > T_GROUP) {
n = d->dev_tp - T_GROUP;
}
else {
n = 1;
}
if (DISPLAY_SHORT_OUTPUT(flags)) {
/* tps */
cprintf_f(NO_UNIT, 1, 8, 2,
S_VALUE(ioj->rd_ios + ioj->wr_ios + ioj->dc_ios,
ioi->rd_ios + ioi->wr_ios + ioi->dc_ios, itv));
/* kB/s */
if (!DISPLAY_UNIT(flags)) {
xios->sectors /= fctr;
}
cprintf_f(DISPLAY_UNIT(flags) ? UNIT_SECTOR : NO_UNIT, 1, 9, 2,
xios->sectors);
/* rqm/s */
cprintf_f(NO_UNIT, 1, 8, 2,
S_VALUE(ioj->rd_merges + ioj->wr_merges + ioj->dc_merges,
ioi->rd_merges + ioi->wr_merges + ioi->dc_merges, itv));
/* await */
cprintf_f(NO_UNIT, 1, 7, 2,
xds->await);
/* areq-sz (in kB, not sectors) */
cprintf_f(DISPLAY_UNIT(flags) ? UNIT_KILOBYTE : NO_UNIT, 1, 8, 2,
xds->arqsz / 2);
/* aqu-sz */
cprintf_f(NO_UNIT, 1, 7, 2,
S_VALUE(ioj->rq_ticks, ioi->rq_ticks, itv) / 1000.0);
/*
* %util
* Again: Ticks in milliseconds.
*/
cprintf_pc(DISPLAY_UNIT(flags), 1, 6, 2, xds->util / 10.0 / (double) n);
}
else {