forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sar.c
1702 lines (1483 loc) · 46.7 KB
/
sar.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
/*
* sar: report system activity
* (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 <stdint.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include "version.h"
#include "sa.h"
#ifdef USE_NLS
#include <locale.h>
#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 time_t __unix_time;
extern int __env;
#endif
/* Interval and count parameters */
long interval = -1, count = 0;
/* TRUE if a header line must be printed */
int dish = TRUE;
/* TRUE if data read from file don't match current machine's endianness */
int endian_mismatch = FALSE;
/* TRUE if file's data come from a 64 bit machine */
int arch_64 = FALSE;
/* Number of decimal places */
int dplaces_nr = -1;
uint64_t flags = 0;
unsigned int dm_major; /* Device-mapper major number */
char timestamp[2][TIMESTAMP_LEN];
extern unsigned int rec_types_nr[];
unsigned long avg_count = 0;
/* File header */
struct file_header file_hdr;
/* Current record header */
struct record_header record_hdr[3];
/*
* Activity sequence.
* This array must always be entirely filled (even with trailing zeros).
*/
unsigned int id_seq[NR_ACT];
struct tm rectime;
/* Contain the date specified by -s and -e options */
struct tstamp tm_start, tm_end;
char *args[MAX_ARGV_NR];
extern struct activity *act[];
extern struct report_format sar_fmt;
struct sigaction int_act;
int sigint_caught = 0;
/*
***************************************************************************
* Print usage title message.
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void print_usage_title(FILE *fp, char *progname)
{
fprintf(fp, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
progname);
}
/*
***************************************************************************
* Print usage and exit.
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void usage(char *progname)
{
print_usage_title(stderr, progname);
fprintf(stderr, _("Options are:\n"
"[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ] [ -h ]\n"
"[ -p ] [ -q ] [ -r [ ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ]\n"
"[ -v ] [ -W ] [ -w ] [ -y ] [ -z ]\n"
"[ -I { <int_list> | SUM | ALL } ] [ -P { <cpu_list> | ALL } ]\n"
"[ -m { <keyword> [,...] | ALL } ] [ -n { <keyword> [,...] | ALL } ]\n"
"[ --dev=<dev_list> ] [ --fs=<fs_list> ] [ --iface=<iface_list> ]\n"
"[ --dec={ 0 | 1 | 2 } ] [ --help ] [ --human ] [ --sadc ]\n"
"[ -j { SID | ID | LABEL | PATH | UUID | ... } ]\n"
"[ -f [ <filename> ] | -o [ <filename> ] | -[0-9]+ ]\n"
"[ -i <interval> ] [ -s [ <hh:mm[:ss]> ] ] [ -e [ <hh:mm[:ss]> ] ]\n"));
exit(1);
}
/*
***************************************************************************
* Display a short help message and exit.
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void display_help(char *progname)
{
print_usage_title(stdout, progname);
printf(_("Main options and reports (report name between square brackets):\n"));
printf(_("\t-B\tPaging statistics [A_PAGE]\n"));
printf(_("\t-b\tI/O and transfer rate statistics [A_IO]\n"));
printf(_("\t-d\tBlock devices statistics [A_DISK]\n"));
printf(_("\t-F [ MOUNT ]\n"));
printf(_("\t\tFilesystems statistics [A_FS]\n"));
printf(_("\t-H\tHugepages utilization statistics [A_HUGE]\n"));
printf(_("\t-I { <int_list> | SUM | ALL }\n"
"\t\tInterrupts statistics [A_IRQ]\n"));
printf(_("\t-m { <keyword> [,...] | ALL }\n"
"\t\tPower management statistics [A_PWR_...]\n"
"\t\tKeywords are:\n"
"\t\tCPU\tCPU instantaneous clock frequency\n"
"\t\tFAN\tFans speed\n"
"\t\tFREQ\tCPU average clock frequency\n"
"\t\tIN\tVoltage inputs\n"
"\t\tTEMP\tDevices temperature\n"
"\t\tUSB\tUSB devices plugged into the system\n"));
printf(_("\t-n { <keyword> [,...] | ALL }\n"
"\t\tNetwork statistics [A_NET_...]\n"
"\t\tKeywords are:\n"
"\t\tDEV\tNetwork interfaces\n"
"\t\tEDEV\tNetwork interfaces (errors)\n"
"\t\tNFS\tNFS client\n"
"\t\tNFSD\tNFS server\n"
"\t\tSOCK\tSockets\t(v4)\n"
"\t\tIP\tIP traffic\t(v4)\n"
"\t\tEIP\tIP traffic\t(v4) (errors)\n"
"\t\tICMP\tICMP traffic\t(v4)\n"
"\t\tEICMP\tICMP traffic\t(v4) (errors)\n"
"\t\tTCP\tTCP traffic\t(v4)\n"
"\t\tETCP\tTCP traffic\t(v4) (errors)\n"
"\t\tUDP\tUDP traffic\t(v4)\n"
"\t\tSOCK6\tSockets\t(v6)\n"
"\t\tIP6\tIP traffic\t(v6)\n"
"\t\tEIP6\tIP traffic\t(v6) (errors)\n"
"\t\tICMP6\tICMP traffic\t(v6)\n"
"\t\tEICMP6\tICMP traffic\t(v6) (errors)\n"
"\t\tUDP6\tUDP traffic\t(v6)\n"
"\t\tFC\tFibre channel HBAs\n"
"\t\tSOFT\tSoftware-based network processing\n"));
printf(_("\t-q\tQueue length and load average statistics [A_QUEUE]\n"));
printf(_("\t-r [ ALL ]\n"
"\t\tMemory utilization statistics [A_MEMORY]\n"));
printf(_("\t-S\tSwap space utilization statistics [A_MEMORY]\n"));
printf(_("\t-u [ ALL ]\n"
"\t\tCPU utilization statistics [A_CPU]\n"));
printf(_("\t-v\tKernel tables statistics [A_KTABLES]\n"));
printf(_("\t-W\tSwapping statistics [A_SWAP]\n"));
printf(_("\t-w\tTask creation and system switching statistics [A_PCSW]\n"));
printf(_("\t-y\tTTY devices statistics [A_SERIAL]\n"));
exit(0);
}
/*
***************************************************************************
* Give a hint to the user about where is located the data collector.
***************************************************************************
*/
void which_sadc(void)
{
struct stat buf;
if (stat(SADC_PATH, &buf) < 0) {
printf(_("Data collector will be sought in PATH\n"));
}
else {
printf(_("Data collector found: %s\n"), SADC_PATH);
}
exit(0);
}
/*
***************************************************************************
* SIGINT signal handler.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void int_handler(int sig)
{
sigint_caught = 1;
printf("\n"); /* Skip "^C" displayed on screen */
}
/*
***************************************************************************
* Init some structures.
***************************************************************************
*/
void init_structures(void)
{
int i;
for (i = 0; i < 3; i++)
memset(&record_hdr[i], 0, RECORD_HEADER_SIZE);
}
/*
***************************************************************************
* Allocate memory for sadc args.
*
* IN:
* @i Argument number.
* @ltemp Argument value.
***************************************************************************
*/
void salloc(int i, char *ltemp)
{
if ((args[i] = (char *) malloc(strlen(ltemp) + 1)) == NULL) {
perror("malloc");
exit(4);
}
strcpy(args[i], ltemp);
}
/*
***************************************************************************
* Display an error message.
*
* IN:
* @error_code Code of error message to display.
***************************************************************************
*/
void print_read_error(int error_code)
{
switch (error_code) {
case END_OF_DATA_UNEXPECTED:
/* Happens when the data collector doesn't send enough data */
fprintf(stderr, _("End of data collecting unexpected\n"));
break;
default:
/* Strange data sent by sadc...! */
fprintf(stderr, _("Inconsistent input data\n"));
break;
}
exit(3);
}
/*
***************************************************************************
* Check that every selected activity actually belongs to the sequence list.
* If not, then the activity should be unselected since it will not be sent
* by sadc. An activity can be not sent if its number of items is zero.
*
* IN:
* @act_nr Size of sequence list.
***************************************************************************
*/
void reverse_check_act(unsigned int act_nr)
{
int i, j;
for (i = 0; i < NR_ACT; i++) {
if (IS_SELECTED(act[i]->options)) {
for (j = 0; j < act_nr; j++) {
if (id_seq[j] == act[i]->id)
break;
}
if (j == act_nr)
act[i]->options &= ~AO_SELECTED;
}
}
}
/*
***************************************************************************
* Determine if a stat header line has to be displayed.
*
* RETURNS:
* TRUE if a header line has to be displayed.
***************************************************************************
*/
int check_line_hdr(void)
{
int i, rc = FALSE;
/* Get number of options entered on the command line */
if (get_activity_nr(act, AO_SELECTED, COUNT_OUTPUTS) > 1)
return TRUE;
for (i = 0; i < NR_ACT; i++) {
if (IS_SELECTED(act[i]->options)) {
/* Special processing for activities using a bitmap */
if (act[i]->bitmap) {
if (count_bits(act[i]->bitmap->b_array,
BITMAP_SIZE(act[i]->bitmap->b_size)) > 1) {
rc = TRUE;
}
}
else if (act[i]->nr_ini > 1) {
rc = TRUE;
}
/* Stop now since we have only one selected activity */
break;
}
}
return rc;
}
/*
***************************************************************************
* Print statistics average.
*
* IN:
* @curr Index in array for current sample statistics.
* @read_from_file Set to TRUE if stats are read from a system activity
* data file.
* @act_id Activity that can be displayed, or ~0 for all.
* Remember that when reading stats from a file, only
* one activity can be displayed at a time.
***************************************************************************
*/
void write_stats_avg(int curr, int read_from_file, unsigned int act_id)
{
int i;
unsigned long long itv;
/* Interval value in 1/100th of a second */
itv = get_interval(record_hdr[2].uptime_cs, record_hdr[curr].uptime_cs);
strncpy(timestamp[curr], _("Average:"), TIMESTAMP_LEN);
timestamp[curr][TIMESTAMP_LEN - 1] = '\0';
memcpy(timestamp[!curr], timestamp[curr], TIMESTAMP_LEN);
/* Test stdout */
TEST_STDOUT(STDOUT_FILENO);
for (i = 0; i < NR_ACT; i++) {
if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
continue;
if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) {
/* Display current average activity statistics */
(*act[i]->f_print_avg)(act[i], 2, curr, itv);
}
}
if (read_from_file) {
/*
* Reset number of lines printed only if we read stats
* from a system activity file.
*/
avg_count = 0;
}
}
/*
***************************************************************************
* Print system statistics.
* This is called when we read stats either from a file or from sadc.
*
* IN:
* @curr Index in array for current sample statistics.
* @read_from_file Set to TRUE if stats are read from a system activity
* data file.
* @use_tm_start Set to TRUE if option -s has been used.
* @use_tm_end Set to TRUE if option -e has been used.
* @reset Set to TRUE if last_uptime variable should be
* reinitialized (used in next_slice() function).
* @act_id Activity that can be displayed or ~0 for all.
* Remember that when reading stats from a file, only
* one activity can be displayed at a time.
* @reset_cd TRUE if static cross_day variable should be reset
* (see below).
*
* OUT:
* @cnt Number of remaining lines to display.
*
* RETURNS:
* 1 if stats have been successfully displayed, and 0 otherwise.
***************************************************************************
*/
int write_stats(int curr, int read_from_file, long *cnt, int use_tm_start,
int use_tm_end, int reset, unsigned int act_id, int reset_cd)
{
int i, prev_hour;
unsigned long long itv;
static int cross_day = 0;
if (reset_cd) {
/*
* cross_day is a static variable that is set to 1 when the first
* record of stats from a new day is read from a unique data file
* (in the case where the file contains data from two consecutive
* days). When set to 1, every following records timestamp will
* have its hour value increased by 24.
* Yet when a new activity (being read from the file) is going to
* be displayed, we start reading the file from the beginning
* again, and so cross_day should be reset in this case.
*/
cross_day = 0;
}
/* Check time (1) */
if (read_from_file) {
if (!next_slice(record_hdr[2].uptime_cs, record_hdr[curr].uptime_cs,
reset, interval))
/* Not close enough to desired interval */
return 0;
}
/* Get then set previous timestamp */
if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[!curr],
&rectime))
return 0;
prev_hour = rectime.tm_hour;
set_record_timestamp_string(flags, &record_hdr[!curr],
NULL, timestamp[!curr], TIMESTAMP_LEN, &rectime);
/* Get then set current timestamp */
if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
&rectime))
return 0;
set_record_timestamp_string(flags, &record_hdr[curr],
NULL, timestamp[curr], TIMESTAMP_LEN, &rectime);
/*
* Check if we are beginning a new day.
* Use rectime.tm_hour and prev_hour instead of record_hdr[].hour for comparison
* to take into account the current timezone (hours displayed will depend on the
* TZ variable value).
*/
if (use_tm_start && record_hdr[!curr].ust_time &&
(record_hdr[curr].ust_time > record_hdr[!curr].ust_time) &&
(rectime.tm_hour < prev_hour)) {
cross_day = 1;
}
/* Check time (2) */
if (use_tm_start && (datecmp(&rectime, &tm_start, cross_day) < 0))
/* it's too soon... */
return 0;
/* Get interval value in 1/100th of a second */
get_itv_value(&record_hdr[curr], &record_hdr[!curr], &itv);
/* Check time (3) */
if (use_tm_end && (datecmp(&rectime, &tm_end, cross_day) > 0)) {
/* It's too late... */
*cnt = 0;
return 0;
}
avg_count++;
/* Test stdout */
TEST_STDOUT(STDOUT_FILENO);
for (i = 0; i < NR_ACT; i++) {
if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
continue;
if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) {
/* Display current activity statistics */
(*act[i]->f_print)(act[i], !curr, curr, itv);
}
}
return 1;
}
/*
***************************************************************************
* Display stats since system startup.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void write_stats_startup(int curr)
{
int i;
/* Set to 0 previous structures corresponding to boot time */
memset(&record_hdr[!curr], 0, RECORD_HEADER_SIZE);
record_hdr[!curr].record_type = R_STATS;
record_hdr[!curr].hour = record_hdr[curr].hour;
record_hdr[!curr].minute = record_hdr[curr].minute;
record_hdr[!curr].second = record_hdr[curr].second;
record_hdr[!curr].ust_time = record_hdr[curr].ust_time;
for (i = 0; i < NR_ACT; i++) {
if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) {
/*
* Using nr[curr] and not nr[!curr] below because we initialize
* reference structures for each structure that has been
* currently read in memory.
* No problem with buffers allocation since they all have the
* same size.
*/
memset(act[i]->buf[!curr], 0,
(size_t) act[i]->msize * (size_t) act[i]->nr[curr] * (size_t) act[i]->nr2);
}
}
flags |= S_F_SINCE_BOOT;
dish = TRUE;
write_stats(curr, USE_SADC, &count, NO_TM_START, NO_TM_END, NO_RESET,
ALL_ACTIVITIES, TRUE);
exit(0);
}
/*
***************************************************************************
* Read data sent by the data collector.
*
* IN:
* @size Number of bytes of data to read.
*
* OUT:
* @buffer Buffer where data will be saved.
*
* RETURNS:
* 1 if end of file has been reached, 0 otherwise.
***************************************************************************
*/
int sa_read(void *buffer, size_t size)
{
ssize_t n;
while (size) {
if ((n = read(STDIN_FILENO, buffer, size)) < 0) {
perror("read");
exit(2);
}
if (!n)
return 1; /* EOF */
size -= n;
buffer = (char *) buffer + n;
}
return 0;
}
/*
***************************************************************************
* Display a restart message (contents of a R_RESTART record).
*
* IN:
* @tab Number of tabulations (unused here).
* @action Action expected from current function (unused here).
* @cur_date Date string of current restart message (unused here).
* @cur_time Time string of current restart message.
* @utc True if @cur_time is expressed in UTC (unused here).
* @file_hdr System activity file standard header.
* @record_hdr Current record header (unused here).
***************************************************************************
*/
__printf_funct_t print_sar_restart(int *tab, int action, char *cur_date, char *cur_time,
int utc, struct file_header *file_hdr,
struct record_header *record_hdr)
{
char restart[64];
printf("\n%-11s", cur_time);
sprintf(restart, " LINUX RESTART\t(%d CPU)\n",
file_hdr->sa_cpu_nr > 1 ? file_hdr->sa_cpu_nr - 1 : 1);
cprintf_s(IS_RESTART, "%s", restart);
}
/*
***************************************************************************
* Display a comment (contents of R_COMMENT record).
*
* IN:
* @tab Number of tabulations (unused here).
* @action Action expected from current function (unused here).
* @cur_date Date string of current comment (unused here).
* @cur_time Time string of current comment.
* @utc True if @cur_time is expressed in UTC (unused here).
* @comment Comment to display.
* @file_hdr System activity file standard header (unused here).
* @record_hdr Current record header (unused here).
***************************************************************************
*/
__print_funct_t print_sar_comment(int *tab, int action, char *cur_date, char *cur_time, int utc,
char *comment, struct file_header *file_hdr,
struct record_header *record_hdr)
{
printf("%-11s", cur_time);
cprintf_s(IS_COMMENT, " COM %s\n", comment);
}
/*
***************************************************************************
* Read the various statistics sent by the data collector (sadc).
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_sadc_stat_bunch(int curr)
{
int i, p;
/* Read record header (type is always R_STATS since it is read from sadc) */
if (sa_read(&record_hdr[curr], RECORD_HEADER_SIZE)) {
/*
* SIGINT (sent by sadc) is likely to be received
* while we are stuck in sa_read().
* If this happens then no data have to be read.
*/
if (sigint_caught)
return;
#ifdef DEBUG
fprintf(stderr, "%s: Record header\n", __FUNCTION__);
#endif
print_read_error(END_OF_DATA_UNEXPECTED);
}
for (i = 0; i < NR_ACT; i++) {
if (!id_seq[i])
continue;
p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
if (HAS_COUNT_FUNCTION(act[p]->options)) {
if (sa_read(&(act[p]->nr[curr]), sizeof(__nr_t))) {
#ifdef DEBUG
fprintf(stderr, "%s: Nb of items\n", __FUNCTION__);
#endif
print_read_error(END_OF_DATA_UNEXPECTED);
}
if ((act[p]->nr[curr] > act[p]->nr_max) || (act[p]->nr[curr] < 0)) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: nr=%d nr_max=%d\n",
__FUNCTION__, act[p]->name, act[p]->nr[curr], act[p]->nr_max);
#endif
print_read_error(INCONSISTENT_INPUT_DATA);
}
if (act[p]->nr[curr] > act[p]->nr_allocated) {
reallocate_all_buffers(act[p], act[p]->nr[curr]);
}
/*
* For persistent activities, we must make sure that no statistics
* from a previous iteration remain, especially if the number
* of structures read is smaller than @nr_ini.
*/
if (HAS_PERSISTENT_VALUES(act[p]->options)) {
memset(act[p]->buf[curr], 0,
(size_t) act[p]->fsize * (size_t) act[p]->nr_ini * (size_t) act[p]->nr2);
}
}
if (sa_read(act[p]->buf[curr],
(size_t) act[p]->fsize * (size_t) act[p]->nr[curr] * (size_t) act[p]->nr2)) {
#ifdef DEBUG
fprintf(stderr, "%s: Statistics\n", __FUNCTION__);
#endif
print_read_error(END_OF_DATA_UNEXPECTED);
}
}
}
/*
***************************************************************************
* Read stats for current activity from file and display them.
*
* IN:
* @ifd Input file descriptor.
* @fpos Position in file where reading must start.
* @curr Index in array for current sample statistics.
* @rows Number of rows of screen.
* @act_id Activity to display.
* @file_actlst List of activities in file.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.
* @rec_hdr_tmp Temporary buffer where current record header will be saved.
* @endian_mismatch
* TRUE if file's data don't match current machine's endianness.
* @arch_64 TRUE if file's data come from a 64 bit machine.
* @b_size Size of @rec_hdr_tmp buffer.
*
* OUT:
* @curr Index in array for next sample statistics.
* @cnt Number of remaining lines of stats to write.
* @eosaf Set to TRUE if EOF (end of file) has been reached.
* @reset Set to TRUE if last_uptime variable should be reinitialized
* (used in next_slice() function).
***************************************************************************
*/
void handle_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
int rows, unsigned int act_id, int *reset,
struct file_activity *file_actlst, char *file,
struct file_magic *file_magic, void *rec_hdr_tmp,
int endian_mismatch, int arch_64, size_t b_size)
{
int p, reset_cd;
unsigned long lines = 0;
unsigned char rtype;
int davg = 0, next, inc = 0;
if (lseek(ifd, fpos, SEEK_SET) < fpos) {
perror("lseek");
exit(2);
}
/*
* Restore the first stats collected.
* Used to compute the rate displayed on the first line.
*/
copy_structures(act, id_seq, record_hdr, !*curr, 2);
*cnt = count;
/* Assess number of lines printed when a bitmap is used */
p = get_activity_position(act, act_id, EXIT_IF_NOT_FOUND);
if (act[p]->bitmap) {
inc = count_bits(act[p]->bitmap->b_array,
BITMAP_SIZE(act[p]->bitmap->b_size));
}
reset_cd = 1;
do {
/*
* Display <count> lines of stats.
* Start with reading current sample's record header.
*/
*eosaf = read_record_hdr(ifd, rec_hdr_tmp, &record_hdr[*curr],
&file_hdr, arch_64, endian_mismatch, UEOF_STOP, b_size, flags);
rtype = record_hdr[*curr].record_type;
if (!*eosaf && (rtype != R_RESTART) && (rtype != R_COMMENT)) {
/* Read the extra fields since it's not a special record */
read_file_stat_bunch(act, *curr, ifd, file_hdr.sa_act_nr, file_actlst,
endian_mismatch, arch_64, file, file_magic, UEOF_STOP);
}
if ((lines >= rows) || !lines) {
lines = 0;
dish = 1;
}
else
dish = 0;
if (!*eosaf && (rtype != R_RESTART)) {
if (rtype == R_COMMENT) {
/* Display comment */
next = print_special_record(&record_hdr[*curr], flags + S_F_LOCAL_TIME,
&tm_start, &tm_end, R_COMMENT, ifd,
&rectime, file, 0,
file_magic, &file_hdr, act, &sar_fmt,
endian_mismatch, arch_64);
if (next && lines) {
/*
* A line of comment was actually displayed: Count it in the
* total number of displayed lines.
* If no lines of stats had been previously displayed, ignore it
* to make sure the header line will be displayed.
*/
lines++;
}
continue;
}
/* next is set to 1 when we were close enough to desired interval */
next = write_stats(*curr, USE_SA_FILE, cnt, tm_start.use, tm_end.use,
*reset, act_id, reset_cd);
reset_cd = 0;
if (next && (*cnt > 0)) {
(*cnt)--;
}
if (next) {
davg++;
*curr ^= 1;
if (inc) {
lines += inc;
}
else {
lines += act[p]->nr[*curr];
}
}
*reset = FALSE;
}
}
while (*cnt && !*eosaf && (rtype != R_RESTART));
/*
* At this moment, if we had a R_RESTART record, we still haven't read
* the number of CPU following it (nor the possible extra structures).
* But in this case, we always have @cnt != 0.
*/
if (davg) {
write_stats_avg(!*curr, USE_SA_FILE, act_id);
}
*reset = TRUE;
}
/*
***************************************************************************
* Read header data sent by sadc.
***************************************************************************
*/
void read_header_data(void)
{
struct file_magic file_magic;
struct file_activity file_act;
int rc, i, p;
char version[16];
/* Read magic header */
rc = sa_read(&file_magic, FILE_MAGIC_SIZE);
sprintf(version, "%d.%d.%d.%d",
file_magic.sysstat_version,
file_magic.sysstat_patchlevel,
file_magic.sysstat_sublevel,
file_magic.sysstat_extraversion);
if (!file_magic.sysstat_extraversion) {
version[strlen(version) - 2] = '\0';
}
if (rc || (file_magic.sysstat_magic != SYSSTAT_MAGIC) ||
(file_magic.format_magic != FORMAT_MAGIC) ||
strcmp(version, VERSION)) {
/* sar and sadc commands are not consistent */
if (!rc && (file_magic.sysstat_magic == SYSSTAT_MAGIC)) {
fprintf(stderr,
_("Using a wrong data collector from a different sysstat version\n"));
}
#ifdef DEBUG
fprintf(stderr, "%s: sysstat_magic=%x format_magic=%x version=%s\n",
__FUNCTION__, file_magic.sysstat_magic, file_magic.format_magic, version);
#endif
print_read_error(INCONSISTENT_INPUT_DATA);
}
/*
* Read header data.
* No need to take into account file_magic.header_size. We are sure that
* sadc and sar are from the same version (we have checked FORMAT_MAGIC
* but also VERSION above) and thus the size of file_header is FILE_HEADER_SIZE.
*/
if (sa_read(&file_hdr, FILE_HEADER_SIZE)) {
#ifdef DEBUG
fprintf(stderr, "%s: File header\n", __FUNCTION__);
#endif
print_read_error(END_OF_DATA_UNEXPECTED);
}
/* All activities are not necessarily selected, but NR_ACT is a max */
if (file_hdr.sa_act_nr > NR_ACT) {
#ifdef DEBUG
fprintf(stderr, "%s: sa_act_nr=%d\n", __FUNCTION__, file_hdr.sa_act_nr);
#endif
print_read_error(INCONSISTENT_INPUT_DATA);
}
if ((file_hdr.act_size != FILE_ACTIVITY_SIZE) ||
(file_hdr.rec_size != RECORD_HEADER_SIZE)) {
#ifdef DEBUG
fprintf(stderr, "%s: act_size=%u/%lu rec_size=%u/%lu\n", __FUNCTION__,
file_hdr.act_size, FILE_ACTIVITY_SIZE, file_hdr.rec_size, RECORD_HEADER_SIZE);
#endif
print_read_error(INCONSISTENT_INPUT_DATA);
}
/* Read activity list */
for (i = 0; i < file_hdr.sa_act_nr; i++) {
if (sa_read(&file_act, FILE_ACTIVITY_SIZE)) {
#ifdef DEBUG
fprintf(stderr, "%s: File activity (%d)\n", __FUNCTION__, i);
#endif
print_read_error(END_OF_DATA_UNEXPECTED);
}
p = get_activity_position(act, file_act.id, RESUME_IF_NOT_FOUND);
if ((p < 0) || (act[p]->fsize != file_act.size)
|| (act[p]->gtypes_nr[0] != file_act.types_nr[0])
|| (act[p]->gtypes_nr[1] != file_act.types_nr[1])
|| (act[p]->gtypes_nr[2] != file_act.types_nr[2])
|| (file_act.nr <= 0)
|| (file_act.nr2 <= 0)
|| (act[p]->magic != file_act.magic)) {
#ifdef DEBUG
if (p < 0) {
fprintf(stderr, "%s: p=%d\n", __FUNCTION__, p);
}
else {
fprintf(stderr, "%s: %s: size=%d/%d magic=%x/%x nr=%d nr2=%d types=%d,%d,%d/%d,%d,%d\n",
__FUNCTION__, act[p]->name, act[p]->fsize, file_act.size,
act[p]->magic, file_act.magic, file_act.nr, file_act.nr2,
act[p]->gtypes_nr[0], act[p]->gtypes_nr[1], act[p]->gtypes_nr[2],
file_act.types_nr[0], file_act.types_nr[1], file_act.types_nr[2]);
}
#endif
/* Remember that we are reading data from sadc and not from a file... */
print_read_error(INCONSISTENT_INPUT_DATA);
}
id_seq[i] = file_act.id; /* We necessarily have "i < NR_ACT" */
act[p]->nr_ini = file_act.nr;
act[p]->nr2 = file_act.nr2;
}
while (i < NR_ACT) {
id_seq[i++] = 0;
}
/* Check that all selected activties are actually sent by sadc */
reverse_check_act(file_hdr.sa_act_nr);
return;
}
/*
***************************************************************************
* Read statistics from a system activity data file.
*
* IN:
* @from_file Input file name.
***************************************************************************
*/
void read_stats_from_file(char from_file[])
{
struct file_magic file_magic;
struct file_activity *file_actlst = NULL;
char rec_hdr_tmp[MAX_RECORD_HEADER_SIZE];
int curr = 1, i, p;
int ifd, rtype;
int rows, eosaf = TRUE, reset = FALSE;
long cnt = 1;
off_t fpos;
/* Get window size */
rows = get_win_height();