forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_stats.c
2689 lines (2360 loc) · 76.9 KB
/
json_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
/*
* json_stats.c: Functions used by sadf to display statistics in JSON format.
* (C) 1999-2024 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 <stdarg.h>
#include "sa.h"
#include "ioconf.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
extern uint64_t flags;
extern char bat_status[][16];
/*
***************************************************************************
* Open or close "network" markup.
*
* IN:
* @tab Number of tabulations.
* @action Open or close action.
***************************************************************************
*/
void json_markup_network(int tab, enum json_action action)
{
static int markup_state = CLOSE_JSON_MARKUP;
if (action == markup_state)
return;
markup_state = action;
if (action == OPEN_JSON_MARKUP) {
/* Open markup */
xprintf(tab, "\"network\": {");
}
else {
/* Close markup */
printf("\n");
xprintf0(tab, "}");
}
}
/*
***************************************************************************
* Open or close "power-management" markup.
*
* IN:
* @tab Number of tabulations.
* @action Open or close action.
***************************************************************************
*/
void json_markup_power_management(int tab, enum json_action action)
{
static int markup_state = CLOSE_JSON_MARKUP;
if (action == markup_state)
return;
markup_state = action;
if (action == OPEN_JSON_MARKUP) {
/* Open markup */
xprintf(tab, "\"power-management\": {");
}
else {
/* Close markup */
printf("\n");
xprintf0(tab, "}");
}
}
/*
***************************************************************************
* Open or close "psi" markup.
*
* IN:
* @tab Number of tabulations.
* @action Open or close action.
***************************************************************************
*/
void json_markup_psi(int tab, enum json_action action)
{
static int markup_state = CLOSE_JSON_MARKUP;
if (action == markup_state)
return;
markup_state = action;
if (action == OPEN_JSON_MARKUP) {
/* Open markup */
xprintf(tab, "\"psi\": {");
}
else {
/* Close markup */
printf("\n");
xprintf0(tab, "}");
}
}
/*
***************************************************************************
* Display CPU statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second (independent of the
* number of processors). Unused here.
***************************************************************************
*/
__print_funct_t json_print_cpu_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i;
int sep = FALSE;
unsigned long long deltot_jiffies = 1;
struct stats_cpu *scc, *scp;
unsigned char offline_cpu_bitmap[BITMAP_SIZE(NR_CPUS)] = {0};
char cpuno[16];
xprintf(tab++, "\"cpu-load\": [");
/* @nr[curr] cannot normally be greater than @nr_ini */
if (a->nr[curr] > a->nr_ini) {
a->nr_ini = a->nr[curr];
}
/*
* Compute CPU "all" as sum of all individual CPU (on SMP machines)
* and look for offline CPU.
*/
if (a->nr_ini > 1) {
deltot_jiffies = get_global_cpu_statistics(a, !curr, curr,
flags, offline_cpu_bitmap);
}
for (i = 0; (i < a->nr_ini) && (i < a->bitmap->b_size + 1); i++) {
/* Should current CPU (including CPU "all") be displayed? */
if (!IS_CPU_SELECTED(a->bitmap->b_array, i) ||
IS_CPU_OFFLINE(offline_cpu_bitmap, i))
/* Don't display CPU */
continue;
scc = (struct stats_cpu *) ((char *) a->buf[curr] + i * a->msize);
scp = (struct stats_cpu *) ((char *) a->buf[!curr] + i * a->msize);
if (sep) {
printf(",\n");
}
sep = TRUE;
if (!i) {
/* This is CPU "all" */
strcpy(cpuno, K_LOWERALL);
if (a->nr_ini == 1) {
/*
* This is a UP machine. In this case
* interval has still not been calculated.
*/
deltot_jiffies = get_per_cpu_interval(scc, scp);
}
if (!deltot_jiffies) {
/* CPU "all" cannot be tickless */
deltot_jiffies = 1;
}
}
else {
sprintf(cpuno, "%d", i - 1);
/*
* Recalculate interval for current proc.
* If result is 0 then current CPU is a tickless one.
*/
deltot_jiffies = get_per_cpu_interval(scc, scp);
if (!deltot_jiffies) {
/* Current CPU is tickless */
if (DISPLAY_CPU_DEF(a->opt_flags)) {
xprintf0(tab, "{\"cpu\": \"%d\", "
"\"user\": %.2f, "
"\"nice\": %.2f, "
"\"system\": %.2f, "
"\"iowait\": %.2f, "
"\"steal\": %.2f, "
"\"idle\": %.2f}",
i - 1, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
}
else if (DISPLAY_CPU_ALL(a->opt_flags)) {
xprintf0(tab, "{\"cpu\": \"%d\", "
"\"usr\": %.2f, "
"\"nice\": %.2f, "
"\"sys\": %.2f, "
"\"iowait\": %.2f, "
"\"steal\": %.2f, "
"\"irq\": %.2f, "
"\"soft\": %.2f, "
"\"guest\": %.2f, "
"\"gnice\": %.2f, "
"\"idle\": %.2f}",
i - 1, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
}
continue;
}
}
if (DISPLAY_CPU_DEF(a->opt_flags)) {
xprintf0(tab, "{\"cpu\": \"%s\", "
"\"user\": %.2f, "
"\"nice\": %.2f, "
"\"system\": %.2f, "
"\"iowait\": %.2f, "
"\"steal\": %.2f, "
"\"idle\": %.2f}",
cpuno,
ll_sp_value(scp->cpu_user, scc->cpu_user, deltot_jiffies),
ll_sp_value(scp->cpu_nice, scc->cpu_nice, deltot_jiffies),
ll_sp_value(scp->cpu_sys + scp->cpu_hardirq + scp->cpu_softirq,
scc->cpu_sys + scc->cpu_hardirq + scc->cpu_softirq,
deltot_jiffies),
ll_sp_value(scp->cpu_iowait, scc->cpu_iowait, deltot_jiffies),
ll_sp_value(scp->cpu_steal, scc->cpu_steal, deltot_jiffies),
scc->cpu_idle < scp->cpu_idle ?
0.0 :
ll_sp_value(scp->cpu_idle, scc->cpu_idle, deltot_jiffies));
}
else if (DISPLAY_CPU_ALL(a->opt_flags)) {
xprintf0(tab, "{\"cpu\": \"%s\", "
"\"usr\": %.2f, "
"\"nice\": %.2f, "
"\"sys\": %.2f, "
"\"iowait\": %.2f, "
"\"steal\": %.2f, "
"\"irq\": %.2f, "
"\"soft\": %.2f, "
"\"guest\": %.2f, "
"\"gnice\": %.2f, "
"\"idle\": %.2f}",
cpuno,
(scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
0.0 :
ll_sp_value(scp->cpu_user - scp->cpu_guest,
scc->cpu_user - scc->cpu_guest, deltot_jiffies),
(scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice) ?
0.0 :
ll_sp_value(scp->cpu_nice - scp->cpu_guest_nice,
scc->cpu_nice - scc->cpu_guest_nice, deltot_jiffies),
ll_sp_value(scp->cpu_sys, scc->cpu_sys, deltot_jiffies),
ll_sp_value(scp->cpu_iowait, scc->cpu_iowait, deltot_jiffies),
ll_sp_value(scp->cpu_steal, scc->cpu_steal, deltot_jiffies),
ll_sp_value(scp->cpu_hardirq, scc->cpu_hardirq, deltot_jiffies),
ll_sp_value(scp->cpu_softirq, scc->cpu_softirq, deltot_jiffies),
ll_sp_value(scp->cpu_guest, scc->cpu_guest, deltot_jiffies),
ll_sp_value(scp->cpu_guest_nice, scc->cpu_guest_nice, deltot_jiffies),
scc->cpu_idle < scp->cpu_idle ?
0.0 :
ll_sp_value(scp->cpu_idle, scc->cpu_idle, deltot_jiffies));
}
}
printf("\n");
xprintf0(--tab, "]");
}
/*
***************************************************************************
* Display task creation and context switch statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_pcsw_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_pcsw
*spc = (struct stats_pcsw *) a->buf[curr],
*spp = (struct stats_pcsw *) a->buf[!curr];
/* proc/s and cswch/s */
xprintf0(tab, "\"process-and-context-switch\": {"
"\"proc\": %.2f, "
"\"cswch\": %.2f}",
S_VALUE(spp->processes, spc->processes, itv),
S_VALUE(spp->context_switch, spc->context_switch, itv));
}
/*
***************************************************************************
* Display interrupts statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_irq_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i, c;
struct stats_irq *stc_cpu_irq, *stp_cpu_irq, *stc_cpuall_irq;
unsigned char masked_cpu_bitmap[BITMAP_SIZE(NR_CPUS)] = {0};
int sep = FALSE, first;
xprintf(tab++, "\"interrupts\": [");
/* @nr[curr] cannot normally be greater than @nr_ini */
if (a->nr[curr] > a->nr_ini) {
a->nr_ini = a->nr[curr];
}
/* Identify offline and unselected CPU, and keep persistent statistics values */
get_global_int_statistics(a, !curr, curr, flags, masked_cpu_bitmap);
for (i = 0; i < a->nr2; i++) {
stc_cpuall_irq = (struct stats_irq *) ((char *) a->buf[curr] + i * a->msize);
if (a->item_list != NULL) {
/* A list of devices has been entered on the command line */
if (!search_list_item(a->item_list, stc_cpuall_irq->irq_name))
/* Device not found */
continue;
}
first = TRUE;
if (sep) {
printf(",\n");
}
for (c = 0; (c < a->nr[curr]) && (c < a->bitmap->b_size + 1); c++) {
stc_cpu_irq = (struct stats_irq *) ((char *) a->buf[curr] + c * a->msize * a->nr2
+ i * a->msize);
stp_cpu_irq = (struct stats_irq *) ((char *) a->buf[!curr] + c * a->msize * a->nr2
+ i * a->msize);
/* Should current CPU (including CPU "all") be displayed? */
if (IS_CPU_SET(masked_cpu_bitmap, c))
/* No */
continue;
/* Yes: Display it */
if (first) {
xprintf0(tab, "{\"intr\": \"%s\"", stc_cpuall_irq->irq_name);
first = FALSE;
}
if (!c) {
printf(", \"all\": %.2f",
(stc_cpu_irq->irq_nr < stp_cpu_irq->irq_nr) ? 0.0 :
S_VALUE(stp_cpu_irq->irq_nr, stc_cpu_irq->irq_nr, itv));
}
else {
printf(", \"CPU%d\": %.2f", c - 1,
S_VALUE(stp_cpu_irq->irq_nr, stc_cpu_irq->irq_nr, itv));
}
}
if (!first) {
printf("}");
sep = TRUE;
}
}
printf("\n");
xprintf0(--tab, "]");
}
/*
***************************************************************************
* Display swapping statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_swap_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_swap
*ssc = (struct stats_swap *) a->buf[curr],
*ssp = (struct stats_swap *) a->buf[!curr];
xprintf0(tab, "\"swap-pages\": {"
"\"pswpin\": %.2f, "
"\"pswpout\": %.2f}",
S_VALUE(ssp->pswpin, ssc->pswpin, itv),
S_VALUE(ssp->pswpout, ssc->pswpout, itv));
}
/*
***************************************************************************
* Display paging statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_paging_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_paging
*spc = (struct stats_paging *) a->buf[curr],
*spp = (struct stats_paging *) a->buf[!curr];
xprintf0(tab, "\"paging\": {"
"\"pgpgin\": %.2f, "
"\"pgpgout\": %.2f, "
"\"fault\": %.2f, "
"\"majflt\": %.2f, "
"\"pgfree\": %.2f, "
"\"pgscank\": %.2f, "
"\"pgscand\": %.2f, "
"\"pgsteal\": %.2f, "
"\"pgprom\": %.2f, "
"\"pgdem\": %.2f}",
S_VALUE(spp->pgpgin, spc->pgpgin, itv),
S_VALUE(spp->pgpgout, spc->pgpgout, itv),
S_VALUE(spp->pgfault, spc->pgfault, itv),
S_VALUE(spp->pgmajfault, spc->pgmajfault, itv),
S_VALUE(spp->pgfree, spc->pgfree, itv),
S_VALUE(spp->pgscan_kswapd, spc->pgscan_kswapd, itv),
S_VALUE(spp->pgscan_direct, spc->pgscan_direct, itv),
S_VALUE(spp->pgsteal, spc->pgsteal, itv),
S_VALUE(spp->pgpromote, spc->pgpromote, itv),
S_VALUE(spp->pgdemote, spc->pgdemote, itv));
}
/*
***************************************************************************
* Display I/O and transfer rate statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_io_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_io
*sic = (struct stats_io *) a->buf[curr],
*sip = (struct stats_io *) a->buf[!curr];
xprintf0(tab, "\"io\": {"
"\"tps\": %.2f, "
"\"io-reads\": {"
"\"rtps\": %.2f, "
"\"bread\": %.2f}, "
"\"io-writes\": {"
"\"wtps\": %.2f, "
"\"bwrtn\": %.2f}, "
"\"io-discard\": {"
"\"dtps\": %.2f, "
"\"bdscd\": %.2f}}",
/*
* If we get negative values, this is probably because
* one or more devices/filesystems have been unmounted.
* We display 0.0 in this case though we should rather tell
* the user that the value cannot be calculated here.
*/
sic->dk_drive < sip->dk_drive ? 0.0 :
S_VALUE(sip->dk_drive, sic->dk_drive, itv),
sic->dk_drive_rio < sip->dk_drive_rio ? 0.0 :
S_VALUE(sip->dk_drive_rio, sic->dk_drive_rio, itv),
sic->dk_drive_rblk < sip->dk_drive_rblk ? 0.0 :
S_VALUE(sip->dk_drive_rblk, sic->dk_drive_rblk, itv),
sic->dk_drive_wio < sip->dk_drive_wio ? 0.0 :
S_VALUE(sip->dk_drive_wio, sic->dk_drive_wio, itv),
sic->dk_drive_wblk < sip->dk_drive_wblk ? 0.0 :
S_VALUE(sip->dk_drive_wblk, sic->dk_drive_wblk, itv),
sic->dk_drive_dio < sip->dk_drive_dio ? 0.0 :
S_VALUE(sip->dk_drive_dio, sic->dk_drive_dio, itv),
sic->dk_drive_dblk < sip->dk_drive_dblk ? 0.0 :
S_VALUE(sip->dk_drive_dblk, sic->dk_drive_dblk, itv));
}
/*
* **************************************************************************
* Display RAM memory utilization in JSON.
*
* IN:
* @smc Structure with statistics.
* @dispall TRUE if all memory fields should be displayed.
***************************************************************************
*/
void json_print_ram_memory_stats(struct stats_memory *smc, int dispall)
{
unsigned long long nousedmem;
nousedmem = smc->frmkb + smc->bufkb + smc->camkb + smc->slabkb;
if (nousedmem > smc->tlmkb) {
nousedmem = smc->tlmkb;
}
printf("\"memfree\": %llu, "
"\"avail\": %llu, "
"\"memused\": %llu, "
"\"memused-percent\": %.2f, "
"\"buffers\": %llu, "
"\"cached\": %llu, "
"\"commit\": %llu, "
"\"commit-percent\": %.2f, "
"\"active\": %llu, "
"\"inactive\": %llu, "
"\"dirty\": %llu",
smc->frmkb,
smc->availablekb,
smc->tlmkb - nousedmem,
smc->tlmkb ? SP_VALUE(nousedmem, smc->tlmkb, smc->tlmkb)
: 0.0,
smc->bufkb,
smc->camkb,
smc->comkb,
(smc->tlmkb + smc->tlskb) ? SP_VALUE(0, smc->comkb, smc->tlmkb + smc->tlskb)
: 0.0,
smc->activekb,
smc->inactkb,
smc->dirtykb);
if (dispall) {
/* Display extended memory stats */
printf(", \"anonpg\": %llu, "
"\"slab\": %llu, "
"\"kstack\": %llu, "
"\"pgtbl\": %llu, "
"\"vmused\": %llu",
smc->anonpgkb,
smc->slabkb,
smc->kstackkb,
smc->pgtblkb,
smc->vmusedkb);
}
}
/*
* **************************************************************************
* Display swap memory utilization in JSON.
*
* IN:
* @smc Structure with statistics.
***************************************************************************
*/
void json_print_swap_memory_stats(struct stats_memory *smc)
{
printf("\"swpfree\": %llu, "
"\"swpused\": %llu, "
"\"swpused-percent\": %.2f, "
"\"swpcad\": %llu, "
"\"swpcad-percent\": %.2f",
smc->frskb,
smc->tlskb - smc->frskb,
smc->tlskb ? SP_VALUE(smc->frskb, smc->tlskb, smc->tlskb)
: 0.0,
smc->caskb,
(smc->tlskb - smc->frskb) ? SP_VALUE(0, smc->caskb, smc->tlskb - smc->frskb)
: 0.0);
}
/*
***************************************************************************
* Display memory statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_memory_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_memory
*smc = (struct stats_memory *) a->buf[curr];
int sep = FALSE;
xprintf0(tab, "\"memory\": {");
if (DISPLAY_MEMORY(a->opt_flags)) {
sep = TRUE;
json_print_ram_memory_stats(smc, DISPLAY_MEM_ALL(a->opt_flags));
}
if (DISPLAY_SWAP(a->opt_flags)) {
if (sep) {
printf(", ");
}
json_print_swap_memory_stats(smc);
}
printf("}");
}
/*
***************************************************************************
* Display kernel tables statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_ktables_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_ktables
*skc = (struct stats_ktables *) a->buf[curr];
xprintf0(tab, "\"kernel\": {"
"\"dentunusd\": %llu, "
"\"file-nr\": %llu, "
"\"inode-nr\": %llu, "
"\"pty-nr\": %llu}",
skc->dentry_stat,
skc->file_used,
skc->inode_used,
skc->pty_nr);
}
/*
***************************************************************************
* Display queue and load statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_queue_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
struct stats_queue
*sqc = (struct stats_queue *) a->buf[curr];
xprintf0(tab, "\"queue\": {"
"\"runq-sz\": %llu, "
"\"plist-sz\": %llu, "
"\"ldavg-1\": %.2f, "
"\"ldavg-5\": %.2f, "
"\"ldavg-15\": %.2f, "
"\"blocked\": %llu}",
sqc->nr_running,
sqc->nr_threads,
(double) sqc->load_avg_1 / 100,
(double) sqc->load_avg_5 / 100,
(double) sqc->load_avg_15 / 100,
sqc->procs_blocked);
}
/*
***************************************************************************
* Display serial lines statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_serial_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i, j, j0, found;
struct stats_serial *ssc, *ssp;
int sep = FALSE;
xprintf(tab++, "\"serial\": [");
for (i = 0; i < a->nr[curr]; i++) {
found = FALSE;
if (a->nr[!curr] > 0) {
ssc = (struct stats_serial *) ((char *) a->buf[curr] + i * a->msize);
/* Look for corresponding serial line in previous iteration */
j = i;
if (j >= a->nr[!curr]) {
j = a->nr[!curr] - 1;
}
j0 = j;
do {
ssp = (struct stats_serial *) ((char *) a->buf[!curr] + j * a->msize);
if (ssc->line == ssp->line) {
found = TRUE;
break;
}
if (++j >= a->nr[!curr]) {
j = 0;
}
}
while (j != j0);
}
if (!found)
continue;
if (sep) {
printf(",\n");
}
sep = TRUE;
xprintf0(tab, "{\"line\": %d, "
"\"rcvin\": %.2f, "
"\"xmtin\": %.2f, "
"\"framerr\": %.2f, "
"\"prtyerr\": %.2f, "
"\"brk\": %.2f, "
"\"ovrun\": %.2f}",
ssc->line,
S_VALUE(ssp->rx, ssc->rx, itv),
S_VALUE(ssp->tx, ssc->tx, itv),
S_VALUE(ssp->frame, ssc->frame, itv),
S_VALUE(ssp->parity, ssc->parity, itv),
S_VALUE(ssp->brk, ssc->brk, itv),
S_VALUE(ssp->overrun, ssc->overrun, itv));
}
printf("\n");
xprintf0(--tab, "]");
}
/*
***************************************************************************
* Display disks statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_disk_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i, j;
struct stats_disk *sdc, *sdp, sdpzero;
struct ext_disk_stats xds;
int sep = FALSE;
char *dev_name;
memset(&sdpzero, 0, STATS_DISK_SIZE);
xprintf(tab++, "\"disk\": [");
for (i = 0; i < a->nr[curr]; i++) {
sdc = (struct stats_disk *) ((char *) a->buf[curr] + i * a->msize);
j = check_disk_reg(a, curr, !curr, i);
if (j < 0) {
/* This is a newly registered interface. Previous stats are zero */
sdp = &sdpzero;
}
else {
sdp = (struct stats_disk *) ((char *) a->buf[!curr] + j * a->msize);
}
/* Get device name */
dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr,
DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags),
USE_STABLE_ID(flags), NULL);
if (a->item_list != NULL) {
/* A list of devices has been entered on the command line */
if (!search_list_item(a->item_list, dev_name))
/* Device not found */
continue;
}
/* Compute extended statistics values */
compute_ext_disk_stats(sdc, sdp, itv, &xds);
if (sep) {
printf(",\n");
}
sep = TRUE;
xprintf0(tab, "{\"disk-device\": \"%s\", "
"\"tps\": %.2f, "
"\"rd_sec\": %.2f, "
"\"wr_sec\": %.2f, "
"\"dc_sec\": %.2f, "
"\"rkB\": %.2f, "
"\"wkB\": %.2f, "
"\"dkB\": %.2f, "
"\"avgrq-sz\": %.2f, "
"\"areq-sz\": %.2f, "
"\"avgqu-sz\": %.2f, "
"\"aqu-sz\": %.2f, "
"\"await\": %.2f, "
"\"util-percent\": %.2f}",
/* Confusion possible here between index and minor numbers */
dev_name,
S_VALUE(sdp->nr_ios, sdc->nr_ios, itv),
S_VALUE(sdp->rd_sect, sdc->rd_sect, itv), /* Unit = sectors (for backward compatibility) */
S_VALUE(sdp->wr_sect, sdc->wr_sect, itv),
S_VALUE(sdp->dc_sect, sdc->dc_sect, itv),
S_VALUE(sdp->rd_sect, sdc->rd_sect, itv) / 2,
S_VALUE(sdp->wr_sect, sdc->wr_sect, itv) / 2,
S_VALUE(sdp->dc_sect, sdc->dc_sect, itv) / 2,
/* See iostat for explanations */
xds.arqsz, /* Unit = sectors (for backward compatibility) */
xds.arqsz / 2,
S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0, /* For backward compatibility */
S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0,
xds.await,
xds.util / 10.0);
}
printf("\n");
xprintf0(--tab, "]");
}
/*
***************************************************************************
* Display network interfaces statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_net_dev_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i, j;
struct stats_net_dev *sndc, *sndp, sndzero;
int sep = FALSE;
double rxkb, txkb, ifutil;
memset(&sndzero, 0, STATS_NET_DEV_SIZE);
if (!IS_SELECTED(a->options) || (a->nr[curr] <= 0))
goto close_json_markup;
json_markup_network(tab, OPEN_JSON_MARKUP);
tab++;
xprintf(tab++, "\"net-dev\": [");
for (i = 0; i < a->nr[curr]; i++) {
sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize);
if (a->item_list != NULL) {
/* A list of devices has been entered on the command line */
if (!search_list_item(a->item_list, sndc->interface))
/* Device not found */
continue;
}
j = check_net_dev_reg(a, curr, !curr, i);
if (j < 0) {
/* This is a newly registered interface. Previous stats are zero */
sndp = &sndzero;
}
else {
sndp = (struct stats_net_dev *) ((char *) a->buf[!curr] + j * a->msize);
}
if (sep) {
printf(",\n");
}
sep = TRUE;
rxkb = S_VALUE(sndp->rx_bytes, sndc->rx_bytes, itv);
txkb = S_VALUE(sndp->tx_bytes, sndc->tx_bytes, itv);
ifutil = compute_ifutil(sndc, rxkb, txkb);
xprintf0(tab, "{\"iface\": \"%s\", "
"\"rxpck\": %.2f, "
"\"txpck\": %.2f, "
"\"rxkB\": %.2f, "
"\"txkB\": %.2f, "
"\"rxcmp\": %.2f, "
"\"txcmp\": %.2f, "
"\"rxmcst\": %.2f, "
"\"ifutil-percent\": %.2f}",
sndc->interface,
S_VALUE(sndp->rx_packets, sndc->rx_packets, itv),
S_VALUE(sndp->tx_packets, sndc->tx_packets, itv),
rxkb / 1024,
txkb / 1024,
S_VALUE(sndp->rx_compressed, sndc->rx_compressed, itv),
S_VALUE(sndp->tx_compressed, sndc->tx_compressed, itv),
S_VALUE(sndp->multicast, sndc->multicast, itv),
ifutil);
}
printf("\n");
xprintf0(--tab, "]");
tab--;
close_json_markup:
if (CLOSE_MARKUP(a->options)) {
json_markup_network(tab, CLOSE_JSON_MARKUP);
}
}
/*
***************************************************************************
* Display network interfaces errors statistics in JSON.
*
* IN:
* @a Activity structure with statistics.
* @curr Index in array for current sample statistics.
* @tab Indentation in output.
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
__print_funct_t json_print_net_edev_stats(struct activity *a, int curr, int tab,
unsigned long long itv)
{
int i, j;
struct stats_net_edev *snedc, *snedp, snedzero;
int sep = FALSE;
if (!IS_SELECTED(a->options) || (a->nr[curr] <= 0))
goto close_json_markup;
memset(&snedzero, 0, STATS_NET_EDEV_SIZE);
json_markup_network(tab, OPEN_JSON_MARKUP);
tab++;
xprintf(tab++, "\"net-edev\": [");
for (i = 0; i < a->nr[curr]; i++) {
snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize);
if (a->item_list != NULL) {
/* A list of devices has been entered on the command line */
if (!search_list_item(a->item_list, snedc->interface))
/* Device not found */
continue;
}
j = check_net_edev_reg(a, curr, !curr, i);
if (j < 0) {
/* This is a newly registered interface. Previous stats are zero */
snedp = &snedzero;
}
else {
snedp = (struct stats_net_edev *) ((char *) a->buf[!curr] + j * a->msize);
}