forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rd_stats.c
2698 lines (2399 loc) · 70.9 KB
/
rd_stats.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
/*
* rd_stats.c: Read system statistics
* (C) 1999-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 <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include "common.h"
#include "rd_stats.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
/*
***************************************************************************
* Read CPU statistics.
* Remember that this function is used by several sysstat commands!
*
* IN:
* @st_cpu Buffer where structures containing stats will be saved.
* @nr_alloc Total number of structures allocated. Value is >= 1.
*
* OUT:
* @st_cpu Buffer with statistics.
*
* RETURNS:
* Highest CPU number(*) for which statistics have been read.
* 1 means CPU "all", 2 means CPU 0, 3 means CPU 1, etc.
* Or -1 if the buffer was too small and needs to be reallocated.
*
* (*)This doesn't account for all processors in the machine in the case
* where some CPU are offline and located at the end of the list.
***************************************************************************
*/
__nr_t read_stat_cpu(struct stats_cpu *st_cpu, __nr_t nr_alloc)
{
FILE *fp;
struct stats_cpu *st_cpu_i;
struct stats_cpu sc;
char line[8192];
int proc_nr;
__nr_t cpu_read = 0;
if ((fp = fopen(STAT, "r")) == NULL) {
fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
exit(2);
}
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "cpu ", 4)) {
/*
* All the fields don't necessarily exist,
* depending on the kernel version used.
*/
memset(st_cpu, 0, STATS_CPU_SIZE);
/*
* Read the number of jiffies spent in the different modes
* (user, nice, etc.) among all proc. CPU usage is not reduced
* to one processor to avoid rounding problems.
*/
sscanf(line + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&st_cpu->cpu_user,
&st_cpu->cpu_nice,
&st_cpu->cpu_sys,
&st_cpu->cpu_idle,
&st_cpu->cpu_iowait,
&st_cpu->cpu_hardirq,
&st_cpu->cpu_softirq,
&st_cpu->cpu_steal,
&st_cpu->cpu_guest,
&st_cpu->cpu_guest_nice);
if (!cpu_read) {
cpu_read = 1;
}
if (nr_alloc == 1)
/* We just want to read stats for CPU "all" */
break;
}
else if (!strncmp(line, "cpu", 3)) {
/* All the fields don't necessarily exist */
memset(&sc, 0, STATS_CPU_SIZE);
/*
* Read the number of jiffies spent in the different modes
* (user, nice, etc) for current proc.
* This is done only on SMP machines.
*/
sscanf(line + 3, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&proc_nr,
&sc.cpu_user,
&sc.cpu_nice,
&sc.cpu_sys,
&sc.cpu_idle,
&sc.cpu_iowait,
&sc.cpu_hardirq,
&sc.cpu_softirq,
&sc.cpu_steal,
&sc.cpu_guest,
&sc.cpu_guest_nice);
if (proc_nr + 2 > nr_alloc) {
cpu_read = -1;
break;
}
st_cpu_i = st_cpu + proc_nr + 1;
*st_cpu_i = sc;
if (proc_nr + 2 > cpu_read) {
cpu_read = proc_nr + 2;
}
}
}
fclose(fp);
return cpu_read;
}
/*
***************************************************************************
* Read interrupts statistics from /proc/stat.
* Remember that this function is used by several sysstat commands!
*
* IN:
* @st_irq Structure where stats will be saved.
* @nr_alloc Number of structures allocated. Value is >= 1.
*
* OUT:
* @st_irq Structure with statistics.
*
* RETURNS:
* Number of interrupts read, or -1 if the buffer was too small and
* needs to be reallocated.
***************************************************************************
*/
__nr_t read_stat_irq(struct stats_irq *st_irq, __nr_t nr_alloc)
{
FILE *fp;
struct stats_irq *st_irq_i;
char line[8192];
int i, pos;
unsigned long long irq_nr;
__nr_t irq_read = 0;
if ((fp = fopen(STAT, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "intr ", 5)) {
/* Read total number of interrupts received since system boot */
sscanf(line + 5, "%llu", &st_irq->irq_nr);
pos = strcspn(line + 5, " ") + 5;
irq_read++;
if (nr_alloc == 1)
/* We just want to read the total number of interrupts */
break;
do {
i = sscanf(line + pos, " %llu", &irq_nr);
if (i < 1)
break;
if (irq_read + 1 > nr_alloc) {
irq_read = -1;
break;
}
st_irq_i = st_irq + irq_read++;
st_irq_i->irq_nr = irq_nr;
i = strcspn(line + pos + 1, " ");
pos += i + 1;
}
while ((i > 0) && (pos < (sizeof(line) - 1)));
break;
}
}
fclose(fp);
return irq_read;
}
/*
***************************************************************************
* Read memory statistics from /proc/meminfo.
*
* IN:
* @st_memory Structure where stats will be saved.
*
* OUT:
* @st_memory Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_meminfo(struct stats_memory *st_memory)
{
FILE *fp;
char line[128];
if ((fp = fopen(MEMINFO, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "MemTotal:", 9)) {
/* Read the total amount of memory in kB */
sscanf(line + 9, "%llu", &st_memory->tlmkb);
}
else if (!strncmp(line, "MemFree:", 8)) {
/* Read the amount of free memory in kB */
sscanf(line + 8, "%llu", &st_memory->frmkb);
}
else if (!strncmp(line, "MemAvailable:", 13)) {
/* Read the amount of available memory in kB */
sscanf(line + 13, "%llu", &st_memory->availablekb);
}
else if (!strncmp(line, "Buffers:", 8)) {
/* Read the amount of buffered memory in kB */
sscanf(line + 8, "%llu", &st_memory->bufkb);
}
else if (!strncmp(line, "Cached:", 7)) {
/* Read the amount of cached memory in kB */
sscanf(line + 7, "%llu", &st_memory->camkb);
}
else if (!strncmp(line, "SwapCached:", 11)) {
/* Read the amount of cached swap in kB */
sscanf(line + 11, "%llu", &st_memory->caskb);
}
else if (!strncmp(line, "Active:", 7)) {
/* Read the amount of active memory in kB */
sscanf(line + 7, "%llu", &st_memory->activekb);
}
else if (!strncmp(line, "Inactive:", 9)) {
/* Read the amount of inactive memory in kB */
sscanf(line + 9, "%llu", &st_memory->inactkb);
}
else if (!strncmp(line, "SwapTotal:", 10)) {
/* Read the total amount of swap memory in kB */
sscanf(line + 10, "%llu", &st_memory->tlskb);
}
else if (!strncmp(line, "SwapFree:", 9)) {
/* Read the amount of free swap memory in kB */
sscanf(line + 9, "%llu", &st_memory->frskb);
}
else if (!strncmp(line, "Dirty:", 6)) {
/* Read the amount of dirty memory in kB */
sscanf(line + 6, "%llu", &st_memory->dirtykb);
}
else if (!strncmp(line, "Committed_AS:", 13)) {
/* Read the amount of commited memory in kB */
sscanf(line + 13, "%llu", &st_memory->comkb);
}
else if (!strncmp(line, "AnonPages:", 10)) {
/* Read the amount of pages mapped into userspace page tables in kB */
sscanf(line + 10, "%llu", &st_memory->anonpgkb);
}
else if (!strncmp(line, "Slab:", 5)) {
/* Read the amount of in-kernel data structures cache in kB */
sscanf(line + 5, "%llu", &st_memory->slabkb);
}
else if (!strncmp(line, "KernelStack:", 12)) {
/* Read the kernel stack utilization in kB */
sscanf(line + 12, "%llu", &st_memory->kstackkb);
}
else if (!strncmp(line, "PageTables:", 11)) {
/* Read the amount of memory dedicated to the lowest level of page tables in kB */
sscanf(line + 11, "%llu", &st_memory->pgtblkb);
}
else if (!strncmp(line, "VmallocUsed:", 12)) {
/* Read the amount of vmalloc area which is used in kB */
sscanf(line + 12, "%llu", &st_memory->vmusedkb);
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read machine uptime, independently of the number of processors.
*
* OUT:
* @uptime Uptime value in hundredths of a second.
***************************************************************************
*/
void read_uptime(unsigned long long *uptime)
{
FILE *fp = NULL;
char line[128];
unsigned long up_sec, up_cent;
int err = FALSE;
if ((fp = fopen(UPTIME, "r")) == NULL) {
err = TRUE;
}
else if (fgets(line, sizeof(line), fp) == NULL) {
err = TRUE;
}
else if (sscanf(line, "%lu.%lu", &up_sec, &up_cent) == 2) {
*uptime = (unsigned long long) up_sec * 100 +
(unsigned long long) up_cent;
}
else {
err = TRUE;
}
if (fp != NULL) {
fclose(fp);
}
if (err) {
fprintf(stderr, _("Cannot read %s\n"), UPTIME);
exit(2);
}
}
/*
***************************************************************************
* Compute "extended" device statistics (service time, etc.).
*
* IN:
* @sdc Structure with current device statistics.
* @sdp Structure with previous device statistics.
* @itv Interval of time in 1/100th of a second.
*
* OUT:
* @xds Structure with extended statistics.
***************************************************************************
*/
void compute_ext_disk_stats(struct stats_disk *sdc, struct stats_disk *sdp,
unsigned long long itv, struct ext_disk_stats *xds)
{
xds->util = S_VALUE(sdp->tot_ticks, sdc->tot_ticks, itv);
/*
* Kernel gives ticks already in milliseconds for all platforms
* => no need for further scaling.
*/
xds->await = (sdc->nr_ios - sdp->nr_ios) ?
((sdc->rd_ticks - sdp->rd_ticks) + (sdc->wr_ticks - sdp->wr_ticks) + (sdc->dc_ticks - sdp->dc_ticks)) /
((double) (sdc->nr_ios - sdp->nr_ios)) : 0.0;
xds->arqsz = (sdc->nr_ios - sdp->nr_ios) ?
((sdc->rd_sect - sdp->rd_sect) + (sdc->wr_sect - sdp->wr_sect) + (sdc->dc_sect - sdp->dc_sect)) /
((double) (sdc->nr_ios - sdp->nr_ios)) : 0.0;
}
/*
***************************************************************************
* Since ticks may vary slightly from CPU to CPU, we'll want
* to recalculate itv based on this CPU's tick count, rather
* than that reported by the "cpu" line. Otherwise we
* occasionally end up with slightly skewed figures, with
* the skew being greater as the time interval grows shorter.
*
* IN:
* @scc Current sample statistics for current CPU.
* @scp Previous sample statistics for current CPU.
*
* RETURNS:
* Interval of time based on current CPU, expressed in jiffies.
***************************************************************************
*/
unsigned long long get_per_cpu_interval(struct stats_cpu *scc,
struct stats_cpu *scp)
{
unsigned long long ishift = 0LL;
if ((scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest)) {
/*
* Sometimes the nr of jiffies spent in guest mode given by the guest
* counter in /proc/stat is slightly higher than that included in
* the user counter. Update the interval value accordingly.
*/
ishift += (scp->cpu_user - scp->cpu_guest) -
(scc->cpu_user - scc->cpu_guest);
}
if ((scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice)) {
/*
* Idem for nr of jiffies spent in guest_nice mode.
*/
ishift += (scp->cpu_nice - scp->cpu_guest_nice) -
(scc->cpu_nice - scc->cpu_guest_nice);
}
/*
* Workaround for CPU coming back online: With recent kernels
* some fields (user, nice, system) restart from their previous value,
* whereas others (idle, iowait) restart from zero.
* For the latter we need to set their previous value to zero to
* avoid getting an interval value < 0.
* (I don't know how the other fields like hardirq, steal... behave).
* Don't assume the CPU has come back from offline state if previous
* value was greater than ULLONG_MAX - 0x7ffff (the counter probably
* overflew).
*/
if ((scc->cpu_idle < scp->cpu_idle) && (scp->cpu_idle < (ULLONG_MAX - 0x7ffff))) {
scp->cpu_idle = 0;
}
if ((scc->cpu_iowait < scp->cpu_iowait) && (scp->cpu_iowait < (ULLONG_MAX - 0x7ffff))) {
scp->cpu_iowait = 0;
}
/*
* Don't take cpu_guest and cpu_guest_nice into account
* because cpu_user and cpu_nice already include them.
*/
return ((scc->cpu_user + scc->cpu_nice +
scc->cpu_sys + scc->cpu_iowait +
scc->cpu_idle + scc->cpu_steal +
scc->cpu_hardirq + scc->cpu_softirq) -
(scp->cpu_user + scp->cpu_nice +
scp->cpu_sys + scp->cpu_iowait +
scp->cpu_idle + scp->cpu_steal +
scp->cpu_hardirq + scp->cpu_softirq) +
ishift);
}
#ifdef SOURCE_SADC
/*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
/*
***************************************************************************
* Replace octal codes in string with their corresponding characters.
*
* IN:
* @str String to parse.
*
* OUT:
* @str String with octal codes replaced with characters.
***************************************************************************
*/
void oct2chr(char *str)
{
int i = 0;
int j, len;
len = strlen(str);
while (i < len - 3) {
if ((str[i] == '\\') &&
(str[i + 1] >= '0') && (str[i + 1] <= '3') &&
(str[i + 2] >= '0') && (str[i + 2] <= '7') &&
(str[i + 3] >= '0') && (str[i + 3] <= '7')) {
/* Octal code found */
str[i] = (str[i + 1] - 48) * 64 +
(str[i + 2] - 48) * 8 +
(str[i + 3] - 48);
for (j = i + 4; j <= len; j++) {
str[j - 3] = str[j];
}
len -= 3;
}
i++;
}
}
/*
***************************************************************************
* Read processes (tasks) creation and context switches statistics
* from /proc/stat.
*
* IN:
* @st_pcsw Structure where stats will be saved.
*
* OUT:
* @st_pcsw Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_stat_pcsw(struct stats_pcsw *st_pcsw)
{
FILE *fp;
char line[8192];
if ((fp = fopen(STAT, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "ctxt ", 5)) {
/* Read number of context switches */
sscanf(line + 5, "%llu", &st_pcsw->context_switch);
}
else if (!strncmp(line, "processes ", 10)) {
/* Read number of processes created since system boot */
sscanf(line + 10, "%lu", &st_pcsw->processes);
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read queue and load statistics from /proc/loadavg and /proc/stat.
*
* IN:
* @st_queue Structure where stats will be saved.
*
* OUT:
* @st_queue Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_loadavg(struct stats_queue *st_queue)
{
FILE *fp;
char line[8192];
unsigned int load_tmp[3];
int rc;
if ((fp = fopen(LOADAVG, "r")) == NULL)
return 0;
/* Read load averages and queue length */
rc = fscanf(fp, "%u.%u %u.%u %u.%u %llu/%llu %*d\n",
&load_tmp[0], &st_queue->load_avg_1,
&load_tmp[1], &st_queue->load_avg_5,
&load_tmp[2], &st_queue->load_avg_15,
&st_queue->nr_running,
&st_queue->nr_threads);
fclose(fp);
if (rc < 8)
return 0;
st_queue->load_avg_1 += load_tmp[0] * 100;
st_queue->load_avg_5 += load_tmp[1] * 100;
st_queue->load_avg_15 += load_tmp[2] * 100;
if (st_queue->nr_running) {
/* Do not take current process into account */
st_queue->nr_running--;
}
/* Read nr of tasks blocked from /proc/stat */
if ((fp = fopen(STAT, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "procs_blocked ", 14)) {
/* Read number of processes blocked */
sscanf(line + 14, "%llu", &st_queue->procs_blocked);
break;
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read swapping statistics from /proc/vmstat.
*
* IN:
* @st_swap Structure where stats will be saved.
*
* OUT:
* @st_swap Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_vmstat_swap(struct stats_swap *st_swap)
{
FILE *fp;
char line[128];
if ((fp = fopen(VMSTAT, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "pswpin ", 7)) {
/* Read number of swap pages brought in */
sscanf(line + 7, "%lu", &st_swap->pswpin);
}
else if (!strncmp(line, "pswpout ", 8)) {
/* Read number of swap pages brought out */
sscanf(line + 8, "%lu", &st_swap->pswpout);
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read paging statistics from /proc/vmstat.
*
* IN:
* @st_paging Structure where stats will be saved.
*
* OUT:
* @st_paging Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_vmstat_paging(struct stats_paging *st_paging)
{
FILE *fp;
char line[128];
unsigned long pgtmp;
if ((fp = fopen(VMSTAT, "r")) == NULL)
return 0;
st_paging->pgsteal = 0;
st_paging->pgscan_kswapd = st_paging->pgscan_direct = 0;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "pgpgin ", 7)) {
/* Read number of pages the system paged in */
sscanf(line + 7, "%lu", &st_paging->pgpgin);
}
else if (!strncmp(line, "pgpgout ", 8)) {
/* Read number of pages the system paged out */
sscanf(line + 8, "%lu", &st_paging->pgpgout);
}
else if (!strncmp(line, "pgfault ", 8)) {
/* Read number of faults (major+minor) made by the system */
sscanf(line + 8, "%lu", &st_paging->pgfault);
}
else if (!strncmp(line, "pgmajfault ", 11)) {
/* Read number of faults (major only) made by the system */
sscanf(line + 11, "%lu", &st_paging->pgmajfault);
}
else if (!strncmp(line, "pgfree ", 7)) {
/* Read number of pages freed by the system */
sscanf(line + 7, "%lu", &st_paging->pgfree);
}
else if (!strncmp(line, "pgsteal_", 8)) {
/* Read number of pages stolen by the system */
sscanf(strchr(line, ' '), "%lu", &pgtmp);
st_paging->pgsteal += pgtmp;
}
else if (!strncmp(line, "pgscan_kswapd", 13)) {
/* Read number of pages scanned by the kswapd daemon */
sscanf(strchr(line, ' '), "%lu", &pgtmp);
st_paging->pgscan_kswapd += pgtmp;
}
else if (!strncmp(line, "pgscan_direct", 13)) {
/* Read number of pages scanned directly */
sscanf(strchr(line, ' '), "%lu", &pgtmp);
st_paging->pgscan_direct += pgtmp;
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read I/O and transfer rates statistics from /proc/diskstats.
*
* IN:
* @st_io Structure where stats will be saved.
*
* OUT:
* @st_io Structure with statistics.
*
* RETURNS:
* 1 on success, 0 otherwise.
***************************************************************************
*/
__nr_t read_diskstats_io(struct stats_io *st_io)
{
FILE *fp;
char line[1024];
char dev_name[MAX_NAME_LEN];
unsigned int major, minor;
unsigned long rd_ios, wr_ios, dc_ios;
unsigned long rd_sec, wr_sec, dc_sec;
if ((fp = fopen(DISKSTATS, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
/* Discard I/O stats may be not available */
dc_ios = dc_sec = 0;
if (sscanf(line,
"%u %u %s "
"%lu %*u %lu %*u "
"%lu %*u %lu %*u "
"%*u %*u %*u "
"%lu %*u %lu",
&major, &minor, dev_name,
&rd_ios, &rd_sec,
&wr_ios, &wr_sec,
&dc_ios, &dc_sec) >= 7) {
if (is_device(dev_name, IGNORE_VIRTUAL_DEVICES)) {
/*
* OK: It's a (real) device and not a partition.
* Note: Structure should have been initialized first!
*/
st_io->dk_drive += (unsigned long long) rd_ios +
(unsigned long long) wr_ios +
(unsigned long long) dc_ios;
st_io->dk_drive_rio += rd_ios;
st_io->dk_drive_rblk += rd_sec;
st_io->dk_drive_wio += wr_ios;
st_io->dk_drive_wblk += wr_sec;
st_io->dk_drive_dio += dc_ios;
st_io->dk_drive_dblk += dc_sec;
}
}
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Read block devices statistics from /proc/diskstats.
*
* IN:
* @st_disk Structure where stats will be saved.
* @nr_alloc Total number of structures allocated. Value is >= 1.
* @read_part True if disks *and* partitions should be read; False if only
* disks are read.
*
* OUT:
* @st_disk Structure with statistics.
*
* RETURNS:
* Number of block devices read, or -1 if the buffer was too small and
* needs to be reallocated.
***************************************************************************
*/
__nr_t read_diskstats_disk(struct stats_disk *st_disk, __nr_t nr_alloc,
int read_part)
{
FILE *fp;
char line[1024];
char dev_name[MAX_NAME_LEN];
struct stats_disk *st_disk_i;
unsigned int major, minor, rd_ticks, wr_ticks, dc_ticks, tot_ticks, rq_ticks;
unsigned long rd_ios, wr_ios, dc_ios, rd_sec, wr_sec, dc_sec;
__nr_t dsk_read = 0;
if ((fp = fopen(DISKSTATS, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL) {
/* Discard I/O stats may be not available */
dc_ios = dc_sec = dc_ticks = 0;
if (sscanf(line,
"%u %u %s "
"%lu %*u %lu %u "
"%lu %*u %lu %u "
"%*u %u %u "
"%lu %*u %lu %u",
&major, &minor, dev_name,
&rd_ios, &rd_sec, &rd_ticks,
&wr_ios, &wr_sec, &wr_ticks,
&tot_ticks, &rq_ticks,
&dc_ios, &dc_sec, &dc_ticks) >= 11) {
if (!rd_ios && !wr_ios && !dc_ios)
/* Unused device: Ignore it */
continue;
if (read_part || is_device(dev_name, ACCEPT_VIRTUAL_DEVICES)) {
if (dsk_read + 1 > nr_alloc) {
dsk_read = -1;
break;
}
st_disk_i = st_disk + dsk_read++;
st_disk_i->major = major;
st_disk_i->minor = minor;
st_disk_i->nr_ios = (unsigned long long) rd_ios +
(unsigned long long) wr_ios +
(unsigned long long) dc_ios;
st_disk_i->rd_sect = rd_sec;
st_disk_i->wr_sect = wr_sec;
st_disk_i->dc_sect = dc_sec;
st_disk_i->rd_ticks = rd_ticks;
st_disk_i->wr_ticks = wr_ticks;
st_disk_i->dc_ticks = dc_ticks;
st_disk_i->tot_ticks = tot_ticks;
st_disk_i->rq_ticks = rq_ticks;
}
}
}
fclose(fp);
return dsk_read;
}
/*
***************************************************************************
* Read serial lines statistics from /proc/tty/driver/serial.
*
* IN:
* @st_serial Structure where stats will be saved.
* @nr_alloc Total number of structures allocated. Value is >= 1.
*
* OUT:
* @st_serial Structure with statistics.
*
* RETURNS:
* Number of serial lines read, or -1 if the buffer was too small and
* needs to be reallocated.
***************************************************************************
*/
__nr_t read_tty_driver_serial(struct stats_serial *st_serial, __nr_t nr_alloc)
{
FILE *fp;
struct stats_serial *st_serial_i;
char line[256];
char *p;
__nr_t sl_read = 0;
if ((fp = fopen(SERIAL, "r")) == NULL)
return 0;
while (fgets(line, sizeof(line), fp) != NULL ) {
if ((p = strstr(line, "tx:")) != NULL) {
if (sl_read + 1 > nr_alloc) {
sl_read = -1;
break;
}
st_serial_i = st_serial + sl_read++;
/* Read serial line number */
sscanf(line, "%u", &st_serial_i->line);
/*
* Read the number of chars transmitted and received by
* current serial line.
*/
sscanf(p + 3, "%u", &st_serial_i->tx);
if ((p = strstr(line, "rx:")) != NULL) {
sscanf(p + 3, "%u", &st_serial_i->rx);
}
if ((p = strstr(line, "fe:")) != NULL) {
sscanf(p + 3, "%u", &st_serial_i->frame);
}
if ((p = strstr(line, "pe:")) != NULL) {
sscanf(p + 3, "%u", &st_serial_i->parity);
}
if ((p = strstr(line, "brk:")) != NULL) {
sscanf(p + 4, "%u", &st_serial_i->brk);
}
if ((p = strstr(line, "oe:")) != NULL) {
sscanf(p + 3, "%u", &st_serial_i->overrun);
}
}
}
fclose(fp);
return sl_read;
}
/*
***************************************************************************
* Read kernel tables statistics from various system files.
*
* IN:
* @st_ktables Structure where stats will be saved.
*
* OUT:
* @st_ktables Structure with statistics.
*
* RETURNS:
* 1 (always success).
***************************************************************************
*/
__nr_t read_kernel_tables(struct stats_ktables *st_ktables)
{
FILE *fp;
unsigned long long parm;
int rc = 0;
/* Open /proc/sys/fs/dentry-state file */
if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {
rc = fscanf(fp, "%*d %llu",
&st_ktables->dentry_stat);
fclose(fp);
if (rc == 0) {
st_ktables->dentry_stat = 0;
}
}
/* Open /proc/sys/fs/file-nr file */
if ((fp = fopen(FFILE_NR, "r")) != NULL) {
rc = fscanf(fp, "%llu %llu",
&st_ktables->file_used, &parm);
fclose(fp);
/*
* The number of used handles is the number of allocated ones
* minus the number of free ones.
*/
if (rc == 2) {
st_ktables->file_used -= parm;
}
else {
st_ktables->file_used = 0;
}
}
/* Open /proc/sys/fs/inode-state file */
if ((fp = fopen(FINODE_STATE, "r")) != NULL) {
rc = fscanf(fp, "%llu %llu",
&st_ktables->inode_used, &parm);
fclose(fp);
/*
* The number of inuse inodes is the number of allocated ones
* minus the number of free ones.
*/
if (rc == 2) {
st_ktables->inode_used -= parm;
}
else {
st_ktables->inode_used = 0;
}
}
/* Open /proc/sys/kernel/pty/nr file */
if ((fp = fopen(PTY_NR, "r")) != NULL) {
rc = fscanf(fp, "%llu",
&st_ktables->pty_nr);
fclose(fp);
if (rc == 0) {
st_ktables->pty_nr = 0;
}
}
return 1;
}
/*
***************************************************************************
* Read network interfaces statistics from /proc/net/dev.
*
* IN:
* @st_net_dev Structure where stats will be saved.
* @nr_alloc Total number of structures allocated. Value is >= 1.
*