forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpstat.c
2431 lines (2139 loc) · 71.1 KB
/
mpstat.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
/*
* mpstat: per-processor statistics
* (C) 2000-2024 by Sebastien GODARD (sysstat <at> orange.fr)
* Copyright (C) 2022 Oracle and/or its affiliates.
*
***************************************************************************
* 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 <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/utsname.h>
#include "version.h"
#include "mpstat.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
unsigned long long uptime_cs[3] = {0, 0, 0};
/* NOTE: Use array of _char_ for bitmaps to avoid endianness problems...*/
unsigned char *cpu_bitmap = NULL; /* Bit 0: Global; Bit 1: 1st proc; etc. */
unsigned char *node_bitmap = NULL; /* Bit 0: Global; Bit 1: 1st NUMA node; etc. */
/* Structures used to save CPU and NUMA nodes CPU stats */
struct stats_cpu *st_cpu[3] = {NULL, NULL, NULL};
struct stats_cpu *st_node[3] = {NULL, NULL, NULL};
/*
* Structure used to save total number of interrupts received
* among all CPU and for each CPU.
*/
struct stats_global_irq *st_irq[3] = {NULL, NULL, NULL};
/*
* Structures used to save, for each interrupt, the number
* received by each CPU.
*/
struct stats_irqcpu *st_irqcpu[3] = {NULL, NULL, NULL};
struct stats_irqcpu *st_softirqcpu[3] = {NULL, NULL, NULL};
/*
* Number of CPU per node, e.g.:
* cpu_per_node[0]: total nr of CPU (this is node "all")
* cpu_per_node[1]: nr of CPU for node 0
* etc.
*/
int *cpu_per_node = NULL;
/*
* Node number the CPU belongs to, e.g.:
* cpu2node[0]: node nr for CPU 0
*/
int *cpu2node = NULL;
/* CPU topology */
struct cpu_topology *st_cpu_topology = NULL;
struct tm mp_tstamp[3];
/* Activity flag */
unsigned int actflags = 0;
unsigned int flags = 0;
/* Interval and count parameters */
long interval = -1, count = 0;
/* Number of decimal places */
int dplaces_nr = -1;
/*
* Nb of processors on the machine.
* A value of 2 means there are 2 processors (0 and 1).
*/
int cpu_nr = 0;
/*
* Highest NUMA node number found on the machine.
* A value of 0 means node 0 (one node).
* A value of -1 means no nodes found.
* We have: node_nr < cpu_nr (see get_node_placement() function).
*/
int node_nr = -1;
/* Nb of interrupts per processor */
int irqcpu_nr = 0;
/* Nb of soft interrupts per processor */
int softirqcpu_nr = 0;
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);
fprintf(stderr, _("Options are:\n"
"[ -A ] [ -H ] [ -n ] [ -T ] [ -u ] [ -V ]\n"
"[ -I { SUM | CPU | SCPU | ALL } ] [ -N { <node_list> | ALL } ]\n"
"[ --dec={ 0 | 1 | 2 } ] [ -o JSON ] [ -P { <cpu_list> | ALL } ]\n"));
exit(1);
}
/*
***************************************************************************
* 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;
}
/*
***************************************************************************
* Allocate stats structures and cpu bitmap. Also do it for NUMA nodes
* (although the machine may not be a NUMA one). Assume that the number of
* nodes is lower or equal than that of CPU.
*
* IN:
* @nr_cpus Number of CPUs. This is the real number of available CPUs + 1
* because we also have to allocate a structure for CPU 'all'.
* @pos Indicate which structures should be initialized. When @pos is
* non zero, it means that only the additional, newly allocated
* structures should be initialized.
***************************************************************************
*/
void salloc_mp_struct(int nr_cpus, int pos)
{
int i;
for (i = 0; i < 3; i++) {
SREALLOC(st_cpu[i], struct stats_cpu, STATS_CPU_SIZE * nr_cpus);
SREALLOC(st_node[i], struct stats_cpu, STATS_CPU_SIZE * nr_cpus);
SREALLOC(st_irq[i], struct stats_global_irq, STATS_GLOBAL_IRQ_SIZE * nr_cpus);
SREALLOC(st_irqcpu[i], struct stats_irqcpu,
STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr);
SREALLOC(st_softirqcpu[i], struct stats_irqcpu,
STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr);
}
SREALLOC(cpu_bitmap, unsigned char, (nr_cpus >> 3) + 1);
SREALLOC(node_bitmap, unsigned char, (nr_cpus >> 3) + 1);
SREALLOC(cpu_per_node, int, sizeof(int) * nr_cpus);
SREALLOC(cpu2node, int, sizeof(int) * nr_cpus);
SREALLOC(st_cpu_topology, struct cpu_topology, sizeof(struct cpu_topology) * nr_cpus);
if (pos) {
/* Init already done in SREALLOC macro if @pos == 0 */
for (i = 0; i < 3; i++) {
memset(st_cpu[i] + pos, 0, STATS_CPU_SIZE * (nr_cpus - pos));
memset(st_node[i] + pos, 0, STATS_CPU_SIZE * (nr_cpus - pos));
memset(st_irq[i] + pos, 0, STATS_GLOBAL_IRQ_SIZE * (nr_cpus - pos));
memset(st_irqcpu[i] + pos, 0, STATS_IRQCPU_SIZE * (nr_cpus - pos) * irqcpu_nr);
memset(st_softirqcpu[i] + pos, 0, STATS_IRQCPU_SIZE * (nr_cpus - pos) * softirqcpu_nr);
}
}
else {
memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1);
memset(node_bitmap, 0, (nr_cpus >> 3) + 1);
}
}
/*
***************************************************************************
* Free structures and bitmap.
***************************************************************************
*/
void sfree_mp_struct(void)
{
int i;
for (i = 0; i < 3; i++) {
free(st_cpu[i]);
free(st_node[i]);
free(st_irq[i]);
free(st_irqcpu[i]);
free(st_softirqcpu[i]);
}
free(cpu_bitmap);
free(node_bitmap);
free(cpu_per_node);
free(cpu2node);
}
/*
***************************************************************************
* Set interrupt values for current sample to those of previous sample.
*
* IN:
* @st_ic Array for per-CPU interrupts statistics.
* @c Fist CPU to process.
* @last Last CPU to process.
* @ic_nr Number of interrupts (hard or soft) per CPU.
* @curr Position in array where current statistics will be saved.
**************************************************************************
*/
void fwd_irq_values(struct stats_irqcpu *st_ic[], unsigned int c,
unsigned int last, int ic_nr, int curr)
{
struct stats_global_irq *st_irq_i, *st_irq_j;
struct stats_irqcpu *p, *q;
int j;
while (c < last) {
st_irq_i = st_irq[curr] + c + 1;
st_irq_j = st_irq[!curr] + c + 1;
st_irq_i->irq_nr = st_irq_j->irq_nr;
for (j = 0; j < ic_nr; j++) {
p = st_ic[curr] + c * ic_nr + j;
q = st_ic[!curr] + c * ic_nr + j;
p->interrupt = q->interrupt;
}
c++;
}
}
/*
***************************************************************************
* Get node placement (which node each CPU belongs to, and total number of
* CPU that each node has).
*
* IN:
* @nr_cpus Number of CPU on this machine.
*
* OUT:
* @cpu_per_node Number of CPU per node.
* @cpu2node The node the CPU belongs to.
*
* RETURNS:
* Highest node number found (e.g., 0 means node 0).
* A value of -1 means no nodes have been found.
***************************************************************************
*/
int get_node_placement(int nr_cpus, int cpu_per_node[], int cpu2node[])
{
DIR *dir;
struct dirent *drd;
char line[MAX_PF_NAME];
int cpu, node, hi_node_nr = -1;
/* Init number of CPU per node */
memset(cpu_per_node, 0, sizeof(int) * (nr_cpus + 1));
/* CPU belongs to no node by default */
memset(cpu2node, -1, sizeof(int) * nr_cpus);
/* This is node "all" */
cpu_per_node[0] = nr_cpus;
for (cpu = 0; cpu < nr_cpus; cpu++) {
snprintf(line, sizeof(line), "%s/cpu%d", SYSFS_DEVCPU, cpu);
line[sizeof(line) - 1] = '\0';
/* Open relevant /sys directory */
if ((dir = opendir(line)) == NULL)
return -1;
/* Get current file entry */
while ((drd = readdir(dir)) != NULL) {
if (!strncmp(drd->d_name, "node", 4) && isdigit(drd->d_name[4])) {
node = atoi(drd->d_name + 4);
if ((node >= nr_cpus) || (node < 0)) {
/* Assume we cannot have more nodes than CPU */
closedir(dir);
return -1;
}
cpu_per_node[node + 1]++;
cpu2node[cpu] = node;
if (node > hi_node_nr) {
hi_node_nr = node;
}
/* Node placement found for current CPU: Go to next CPU directory */
break;
}
}
/* Close directory */
closedir(dir);
}
return hi_node_nr;
}
/*
***************************************************************************
* Read system logical topology: Socket number for each logical core is read
* from the /sys/devices/system/cpu/cpu{N}/topology/physical_package_id file,
* and the logical core id number is the first number read from the
* /sys/devices/system/cpu/cpu{N}/topology/thread_siblings_list file.
* Don't use /sys/devices/system/cpu/cpu{N}/topology/core_id as this is the
* physical core id (seems to be different from the number displayed by lscpu).
*
* IN:
* @nr_cpus Number of CPU on this machine.
* @cpu_topo Structures where socket and core id numbers will be saved.
*
* OUT:
* @cpu_topo Structures where socket and core id numbers have been saved.
***************************************************************************
*/
void read_topology(int nr_cpus, struct cpu_topology *cpu_topo)
{
struct cpu_topology *cpu_topo_i;
FILE *fp;
char filename[MAX_PF_NAME];
int cpu, rc;
/* Init system topology */
memset(st_cpu_topology, 0, sizeof(struct cpu_topology) * nr_cpus);
for (cpu = 0; cpu < nr_cpus; cpu++) {
cpu_topo_i = cpu_topo + cpu;
/* Read current CPU's socket number */
snprintf(filename, sizeof(filename), "%s/cpu%d/%s", SYSFS_DEVCPU, cpu, PHYS_PACK_ID);
filename[sizeof(filename) - 1] = '\0';
if ((fp = fopen(filename, "r")) != NULL) {
rc = fscanf(fp, "%d", &cpu_topo_i->phys_package_id);
fclose(fp);
if (rc < 1) {
cpu_topo_i->phys_package_id = -1;
}
}
/* Read current CPU's logical core id number */
snprintf(filename, sizeof(filename), "%s/cpu%d/%s", SYSFS_DEVCPU, cpu, THREAD_SBL_LST);
filename[sizeof(filename) - 1] = '\0';
if ((fp = fopen(filename, "r")) != NULL) {
rc = fscanf(fp, "%d", &cpu_topo_i->logical_core_id);
fclose(fp);
if (rc < 1) {
cpu_topo_i->logical_core_id = -1;
}
}
}
}
/*
***************************************************************************
* Compute node statistics: Split CPU statistics among nodes.
*
* IN:
* @src Structure containing CPU stats to add.
*
* OUT:
* @dest Structure containing global CPU stats.
***************************************************************************
*/
void add_cpu_stats(struct stats_cpu *dest, struct stats_cpu *src)
{
dest->cpu_user += src->cpu_user;
dest->cpu_nice += src->cpu_nice;
dest->cpu_sys += src->cpu_sys;
dest->cpu_idle += src->cpu_idle;
dest->cpu_iowait += src->cpu_iowait;
dest->cpu_hardirq += src->cpu_hardirq;
dest->cpu_softirq += src->cpu_softirq;
dest->cpu_steal += src->cpu_steal;
dest->cpu_guest += src->cpu_guest;
dest->cpu_guest_nice += src->cpu_guest_nice;
}
/*
***************************************************************************
* Compute node statistics: Split CPU statistics among nodes.
*
* IN:
* @prev Index in array where stats used as reference are.
* @curr Index in array for current sample statistics.
*
* OUT:
* @st_node Array where CPU stats for each node have been saved.
***************************************************************************
*/
void set_node_cpu_stats(int prev, int curr)
{
int cpu;
unsigned long long tot_jiffies_p;
struct stats_cpu *scp, *scc, *snp, *snc;
struct stats_cpu *scc_all = st_cpu[curr];
struct stats_cpu *scp_all = st_cpu[prev];
struct stats_cpu *snc_all = st_node[curr];
struct stats_cpu *snp_all = st_node[prev];
/* Reset structures */
memset(st_node[prev], 0, STATS_CPU_SIZE * (cpu_nr + 1));
memset(st_node[curr], 0, STATS_CPU_SIZE * (cpu_nr + 1));
/* Node 'all' is the same as CPU 'all' */
*snp_all = *scp_all;
*snc_all = *scc_all;
/* Individual nodes */
for (cpu = 0; cpu < cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu + 1;
scp = st_cpu[prev] + cpu + 1;
snp = st_node[prev] + cpu2node[cpu] + 1;
snc = st_node[curr] + cpu2node[cpu] + 1;
tot_jiffies_p = scp->cpu_user + scp->cpu_nice +
scp->cpu_sys + scp->cpu_idle +
scp->cpu_iowait + scp->cpu_hardirq +
scp->cpu_steal + scp->cpu_softirq;
if ((tot_jiffies_p == 0) && (interval != 0))
/*
* CPU has just come back online with no ref from
* previous iteration: Skip it.
*/
continue;
add_cpu_stats(snp, scp);
add_cpu_stats(snc, scc);
}
}
/*
***************************************************************************
* Compute global CPU statistics as the sum of individual CPU ones, and
* calculate interval for global CPU.
* Also identify offline CPU.
*
* IN:
* @prev Index in array where stats used as reference are.
* @curr Index in array for current sample statistics.
* @offline_cpu_bitmap
* CPU bitmap for offline CPU.
*
* OUT:
* @offline_cpu_bitmap
* CPU bitmap with offline CPU.
*
* RETURNS:
* Interval for global CPU.
***************************************************************************
*/
unsigned long long get_global_cpu_mpstats(int prev, int curr,
unsigned char offline_cpu_bitmap[])
{
int i;
unsigned long long tot_jiffies_c, tot_jiffies_p;
unsigned long long deltot_jiffies = 0;
struct stats_cpu *scc, *scp;
struct stats_cpu *scc_all = st_cpu[curr];
struct stats_cpu *scp_all = st_cpu[prev];
/*
* For UP machines we keep the values read from global CPU line in /proc/stat.
* Also look for offline CPU: They won't be displayed, and some of their values may
* have to be modified.
*/
if (cpu_nr > 1) {
memset(scc_all, 0, sizeof(struct stats_cpu));
memset(scp_all, 0, sizeof(struct stats_cpu));
}
else {
/* This is a UP machine */
return get_per_cpu_interval(st_cpu[curr], st_cpu[prev]);
}
for (i = 1; i <= cpu_nr; i++) {
scc = st_cpu[curr] + i;
scp = st_cpu[prev] + i;
/*
* Compute the total number of jiffies spent by current processor.
* NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
* already include them.
*/
tot_jiffies_c = scc->cpu_user + scc->cpu_nice +
scc->cpu_sys + scc->cpu_idle +
scc->cpu_iowait + scc->cpu_hardirq +
scc->cpu_steal + scc->cpu_softirq;
tot_jiffies_p = scp->cpu_user + scp->cpu_nice +
scp->cpu_sys + scp->cpu_idle +
scp->cpu_iowait + scp->cpu_hardirq +
scp->cpu_steal + scp->cpu_softirq;
/*
* If the CPU is offline then it is omitted from /proc/stat:
* All the fields couldn't have been read and the sum of them is zero.
*/
if (tot_jiffies_c == 0) {
/*
* CPU is currently offline.
* Set current struct fields (which have been set to zero)
* to values from previous iteration. Hence their values won't
* jump from zero when the CPU comes back online.
* Note that this workaround no longer fully applies with recent kernels,
* as I have noticed that when a CPU comes back online, some fields
* restart from their previous value (e.g. user, nice, system)
* whereas others restart from zero (idle, iowait)! To deal with this,
* the get_per_cpu_interval() function will set these previous values
* to zero if necessary.
*/
*scc = *scp;
/*
* Mark CPU as offline to not display it
* (and thus it will not be confused with a tickless CPU).
*/
MARK_CPU_OFFLINE(offline_cpu_bitmap, i);
}
if ((tot_jiffies_p == 0) && (interval != 0)) {
/*
* CPU has just come back online.
* Unfortunately, no reference values are available
* from a previous iteration, probably because it was
* already offline when the first sample has been taken.
* So don't display that CPU to prevent "jump-from-zero"
* output syndrome, and don't take it into account for CPU "all".
* NB: Test for interval != 0 to make sure we don't want stats
* since boot time.
*/
MARK_CPU_OFFLINE(offline_cpu_bitmap, i);
continue;
}
/*
* Get interval for current CPU and add it to global CPU.
* Note: Previous idle and iowait values (saved in scp) may be modified here.
*/
deltot_jiffies += get_per_cpu_interval(scc, scp);
add_cpu_stats(scc_all, scc);
add_cpu_stats(scp_all, scp);
}
return deltot_jiffies;
}
/*
***************************************************************************
* Display CPU statistics in plain format.
*
* IN:
* @dis TRUE if a header line must be printed.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
* @offline_cpu_bitmap
* CPU bitmap for offline CPU.
***************************************************************************
*/
void write_plain_cpu_stats(int dis, unsigned long long deltot_jiffies, int prev, int curr,
char *prev_string, char *curr_string,
const unsigned char offline_cpu_bitmap[])
{
int i;
struct stats_cpu *scc, *scp;
struct cpu_topology *cpu_topo_i;
if (dis) {
printf("\n%-11s CPU", prev_string);
if (DISPLAY_TOPOLOGY(flags)) {
printf(" CORE SOCK NODE");
}
printf(" %%usr %%nice %%sys %%iowait %%irq "
"%%soft %%steal %%guest %%gnice %%idle\n");
}
/*
* Now display CPU statistics (including CPU "all"),
* except for offline CPU or CPU that the user doesn't want to see.
*/
for (i = 0; i <= cpu_nr; i++) {
/* Check if we want stats about this proc */
if (!(*(cpu_bitmap + (i >> 3)) & (1 << (i & 0x07))) ||
IS_CPU_OFFLINE(offline_cpu_bitmap, i))
continue;
scc = st_cpu[curr] + i;
scp = st_cpu[prev] + i;
printf("%-11s", curr_string);
if (i == 0) {
/* This is CPU "all" */
cprintf_in(IS_STR, " %s", " all", 0);
if (DISPLAY_TOPOLOGY(flags)) {
printf(" ");
}
}
else {
cprintf_in(IS_INT, " %4d", "", i - 1);
if (DISPLAY_TOPOLOGY(flags)) {
cpu_topo_i = st_cpu_topology + i - 1;
cprintf_in(IS_INT, " %4d", "", cpu_topo_i->logical_core_id);
cprintf_in(IS_INT, " %4d", "", cpu_topo_i->phys_package_id);
cprintf_in(IS_INT, " %4d", "", cpu2node[i - 1]);
}
/* Recalculate itv for current proc */
deltot_jiffies = get_per_cpu_interval(scc, scp);
if (!deltot_jiffies) {
/*
* If the CPU is tickless then there is no change in CPU values
* but the sum of values is not zero.
*/
cprintf_xpc(NO_UNIT, FALSE, 10, 7, 2,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
printf("\n");
continue;
}
}
cprintf_xpc(NO_UNIT, XHIGH, 9, 7, 2,
(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_hardirq,
scc->cpu_hardirq, deltot_jiffies),
ll_sp_value(scp->cpu_softirq,
scc->cpu_softirq, deltot_jiffies),
ll_sp_value(scp->cpu_steal,
scc->cpu_steal, 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));
cprintf_xpc(NO_UNIT, XLOW, 1, 7, 2,
(scc->cpu_idle < scp->cpu_idle) ?
0.0 :
ll_sp_value(scp->cpu_idle,
scc->cpu_idle, deltot_jiffies));
printf("\n");
}
}
/*
***************************************************************************
* Display CPU statistics in JSON format.
*
* IN:
* @tab Number of tabs to print.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @offline_cpu_bitmap
* CPU bitmap for offline CPU.
***************************************************************************
*/
void write_json_cpu_stats(int tab, unsigned long long deltot_jiffies, int prev, int curr,
const unsigned char offline_cpu_bitmap[])
{
int i, next = FALSE;
char cpu_name[16], topology[1024] = "";
struct stats_cpu *scc, *scp;
struct cpu_topology *cpu_topo_i;
xprintf(tab++, "\"cpu-load\": [");
/*
* Now display CPU statistics (including CPU "all"),
* except for offline CPU or CPU that the user doesn't want to see.
*/
for (i = 0; i <= cpu_nr; i++) {
/* Check if we want stats about this proc */
if (!(*(cpu_bitmap + (i >> 3)) & (1 << (i & 0x07))) ||
IS_CPU_OFFLINE(offline_cpu_bitmap, i))
continue;
scc = st_cpu[curr] + i;
scp = st_cpu[prev] + i;
if (next) {
printf(",\n");
}
next = TRUE;
if (i == 0) {
/* This is CPU "all" */
strcpy(cpu_name, K_LOWERALL);
if (DISPLAY_TOPOLOGY(flags)) {
snprintf(topology, sizeof(topology),
", \"core\": \"\", \"socket\": \"\", \"node\": \"\"");
}
}
else {
snprintf(cpu_name, sizeof(cpu_name), "%d", i - 1);
cpu_name[sizeof(cpu_name) - 1] = '\0';
if (DISPLAY_TOPOLOGY(flags)) {
cpu_topo_i = st_cpu_topology + i - 1;
snprintf(topology, sizeof(topology),
", \"core\": \"%d\", \"socket\": \"%d\", \"node\": \"%d\"",
cpu_topo_i->logical_core_id, cpu_topo_i->phys_package_id, cpu2node[i - 1]);
}
/* Recalculate itv for current proc */
deltot_jiffies = get_per_cpu_interval(scc, scp);
if (!deltot_jiffies) {
/*
* If the CPU is tickless then there is no change in CPU values
* but the sum of values is not zero.
*/
xprintf0(tab, "{\"cpu\": \"%d\"%s, \"usr\": 0.00, \"nice\": 0.00, "
"\"sys\": 0.00, \"iowait\": 0.00, \"irq\": 0.00, "
"\"soft\": 0.00, \"steal\": 0.00, \"guest\": 0.00, "
"\"gnice\": 0.00, \"idle\": 100.00}", i - 1, topology);
printf("\n");
continue;
}
}
xprintf0(tab, "{\"cpu\": \"%s\"%s, \"usr\": %.2f, \"nice\": %.2f, \"sys\": %.2f, "
"\"iowait\": %.2f, \"irq\": %.2f, \"soft\": %.2f, \"steal\": %.2f, "
"\"guest\": %.2f, \"gnice\": %.2f, \"idle\": %.2f}",
cpu_name, topology,
(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_hardirq,
scc->cpu_hardirq, deltot_jiffies),
ll_sp_value(scp->cpu_softirq,
scc->cpu_softirq, deltot_jiffies),
ll_sp_value(scp->cpu_steal,
scc->cpu_steal, 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 CPU statistics in plain or JSON format.
*
* IN:
* @dis TRUE if a header line must be printed.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
* @tab Number of tabs to print (JSON format only).
* @next TRUE is a previous activity has been displayed (JSON format
* only).
* @offline_cpu_bitmap
* CPU bitmap for offline CPU.
***************************************************************************
*/
void write_cpu_stats(int dis, unsigned long long deltot_jiffies, int prev, int curr,
char *prev_string, char *curr_string, int tab, int *next,
unsigned char offline_cpu_bitmap[])
{
if (!deltot_jiffies) {
/* CPU "all" cannot be tickless */
deltot_jiffies = 1;
}
if (DISPLAY_JSON_OUTPUT(flags)) {
if (*next) {
printf(",\n");
}
*next = TRUE;
write_json_cpu_stats(tab, deltot_jiffies, prev, curr,
offline_cpu_bitmap);
}
else {
write_plain_cpu_stats(dis, deltot_jiffies, prev, curr,
prev_string, curr_string, offline_cpu_bitmap);
}
}
/*
***************************************************************************
* Display CPU statistics for NUMA nodes in plain format.
*
* IN:
* @dis TRUE if a header line must be printed.
* @deltot_jiffies
* Number of jiffies spent on the interval by all processors.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
***************************************************************************
*/
void write_plain_node_stats(int dis, unsigned long long deltot_jiffies,
int prev, int curr, char *prev_string, char *curr_string)
{
struct stats_cpu *snc, *snp, *scc, *scp;
int cpu, node;
if (dis) {
printf("\n%-11s NODE %%usr %%nice %%sys %%iowait %%irq "
"%%soft %%steal %%guest %%gnice %%idle\n",
prev_string);
}
for (node = 0; node <= node_nr + 1; node++) {
snc = st_node[curr] + node;
snp = st_node[prev] + node;
/* Check if we want stats about this node */
if (!(*(node_bitmap + (node >> 3)) & (1 << (node & 0x07))))
continue;
if (!cpu_per_node[node])
/* No CPU in this node */
continue;
printf("%-11s", curr_string);
if (node == 0) {
/* This is node "all", i.e. CPU "all" */
cprintf_in(IS_STR, " %s", " all", 0);
}
else {
cprintf_in(IS_INT, " %4d", "", node - 1);
/* Recalculate interval for current node */
deltot_jiffies = 0;
for (cpu = 1; cpu <= cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu;
scp = st_cpu[prev] + cpu;
if ((scp->cpu_user + scp->cpu_nice + scp->cpu_sys +
scp->cpu_idle + scp->cpu_iowait + scp->cpu_hardirq +
scp->cpu_steal + scp->cpu_softirq == 0) && (interval != 0))
continue;
if (cpu2node[cpu - 1] == node - 1) {
deltot_jiffies += get_per_cpu_interval(scc, scp);
}
}
if (!deltot_jiffies) {
/* All CPU in node are tickless and/or offline */
cprintf_xpc(NO_UNIT, FALSE, 10, 7, 2,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
printf("\n");
continue;
}
}
cprintf_xpc(NO_UNIT, XHIGH, 9, 7, 2,
(snc->cpu_user - snc->cpu_guest) < (snp->cpu_user - snp->cpu_guest) ?
0.0 :
ll_sp_value(snp->cpu_user - snp->cpu_guest,
snc->cpu_user - snc->cpu_guest, deltot_jiffies),
(snc->cpu_nice - snc->cpu_guest_nice) < (snp->cpu_nice - snp->cpu_guest_nice) ?
0.0 :
ll_sp_value(snp->cpu_nice - snp->cpu_guest_nice,
snc->cpu_nice - snc->cpu_guest_nice, deltot_jiffies),
ll_sp_value(snp->cpu_sys,
snc->cpu_sys, deltot_jiffies),
ll_sp_value(snp->cpu_iowait,
snc->cpu_iowait, deltot_jiffies),
ll_sp_value(snp->cpu_hardirq,
snc->cpu_hardirq, deltot_jiffies),
ll_sp_value(snp->cpu_softirq,
snc->cpu_softirq, deltot_jiffies),
ll_sp_value(snp->cpu_steal,
snc->cpu_steal, deltot_jiffies),
ll_sp_value(snp->cpu_guest,
snc->cpu_guest, deltot_jiffies),
ll_sp_value(snp->cpu_guest_nice,
snc->cpu_guest_nice, deltot_jiffies));
cprintf_xpc(NO_UNIT, XLOW, 1, 7, 2,
(snc->cpu_idle < snp->cpu_idle) ?
0.0 :
ll_sp_value(snp->cpu_idle,
snc->cpu_idle, deltot_jiffies));