forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sadf.c
2100 lines (1850 loc) · 59.9 KB
/
sadf.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
/*
* sadf: system activity data formatter
* (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 <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include "version.h"
#include "sadf.h"
# include <locale.h> /* For setlocale() */
#ifdef USE_NLS
# include <libintl.h>
# define _(string) gettext(string)
#else
# define _(string) (string)
#endif
#ifdef HAVE_PCP
#include <pcp/pmapi.h>
#include <pcp/import.h>
#endif
#ifdef USE_SCCSID
#define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
char *sccsid(void) { return (SCCSID); }
#endif
#ifdef TEST
extern int __env;
void int_handler(int n) { return; }
#endif
extern char *tzname[2];
long interval = -1, count = 0;
/* 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;
/* Color palette number */
int palette = SVG_DEFAULT_COL_PALETTE;
uint64_t flags = 0;
unsigned int dm_major; /* Device-mapper major number */
unsigned int format = 0; /* Output format */
unsigned int f_position = 0; /* Output format position in array */
unsigned int canvas_height = 0; /* SVG canvas height value set with option -O */
unsigned int user_hz = 0; /* HZ value set with option -O */
/* File header */
struct file_header file_hdr;
/*
* Activity sequence.
* This array must always be entirely filled (even with trailing zeros).
*/
unsigned int id_seq[NR_ACT];
/* Current record header */
struct record_header record_hdr[3];
/* Contain the date specified by -s and -e options */
struct tstamp_ext tm_start, tm_end;
char *args[MAX_ARGV_NR];
/* Current timezone */
char my_tzname[TZNAME_LEN];
extern struct activity *act[];
extern struct report_format *fmt[];
/* Battery status */
char bat_status[][16] = {
"Unknown",
"Charging",
"Discharging",
"NotCharging",
"Full"
};
/*
***************************************************************************
* Print usage and exit.
*
* IN:
* @progname Name of sysstat command.
***************************************************************************
*/
void usage(char *progname)
{
fprintf(stderr,
_("Usage: %s [ options ] [ <interval> [ <count> ] ] [ <datafile> | -[0-9]+ ]\n"),
progname);
fprintf(stderr, _("Options are:\n"
"[ -C ] [ -c | -d | -g | -j | -l | -p | -r | -x ] [ -H ] [ -h ] [ -T | -t | -U ] [ -V ]\n"
"[ -O <opts> [,...] ] [ -P { <cpu> [,...] | ALL } ]\n"
"[ --dev=<dev_list> ] [ --fs=<fs_list> ] [ --iface=<iface_list> ] [ --int=<int_list> ]\n"
"[ -s [ <start_time> ] ] [ -e [ <end_time> ] ]\n"
"[ -- <sar_options> ]\n"));
exit(1);
}
/*
***************************************************************************
* Init structures.
***************************************************************************
*/
void init_structures(void)
{
int i;
for (i = 0; i < 3; i++) {
memset(&record_hdr[i], 0, RECORD_HEADER_SIZE);
}
}
/*
***************************************************************************
* Look for output format in array.
*
* IN:
* @oformat Output format to look for.
*
* RETURNS:
* Position of output format in array.
***************************************************************************
*/
int get_format_position(unsigned int oformat)
{
int i;
for (i = 0; i < NR_FMT; i++) {
if (fmt[i]->id == oformat)
break;
}
if (i == NR_FMT)
/* Should never happen */
return 0;
return i;
}
/*
***************************************************************************
* Check that options entered on the command line are consistent with
* selected output format. If no output format has been explicitly entered,
* then select a default one.
***************************************************************************
*/
void check_format_options(void)
{
if (!format) {
/* Select output format if none has been selected */
if (DISPLAY_HDR_ONLY(flags)) {
format = F_HEADER_OUTPUT;
}
else {
format = F_PPC_OUTPUT;
}
}
/* Get format position in array */
f_position = get_format_position(format);
/* Check options consistency wrt output format */
if (!ACCEPT_HEADER_ONLY(fmt[f_position]->options)) {
/* Remove option -H */
flags &= ~S_F_HDR_ONLY;
}
if (!ACCEPT_HORIZONTALLY(fmt[f_position]->options)) {
/* Remove option -h */
flags &= ~S_F_HORIZONTALLY;
}
if (!ACCEPT_LOCAL_TIME(fmt[f_position]->options)) {
/* Remove option -T */
flags &= ~S_F_LOCAL_TIME;
}
if (!ACCEPT_SEC_EPOCH(fmt[f_position]->options)) {
/* Remove option -U */
flags &= ~S_F_SEC_EPOCH;
}
if (REJECT_TRUE_TIME(fmt[f_position]->options)) {
/* Remove option -t */
flags &= ~S_F_TRUE_TIME;
}
}
/*
***************************************************************************
* Read next sample statistics. If it's a special record (R_RESTART or
* R_COMMENT) then display it if requested. Also fill timestamps structures.
*
* IN:
* @ifd File descriptor
* @action Flags indicating if special records should be displayed or
* not.
* @curr Index in array for current sample statistics.
* @file System activity data file name (name of file being read).
* @tab Number of tabulations to print.
* @file_magic System activity file magic header.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or in UTC
* depending on whether options -T/-t have been used or not) can
* be saved for current record.
* @oneof Set to UEOF_CONT if an unexpected end of file should not make
* sadf stop. Default behavior is to stop on unexpected EOF.
*
* OUT:
* @rtype Type of record read (R_RESTART, R_COMMENT, etc.)
* @rectime Structure where timestamp (expressed in local time or in UTC
* depending on options used) has been saved for current record.
* If current record was a special one (RESTART or COMMENT) and
* noted to be ignored, then the timestamp is saved only if
* explicitly told to do so with the SET_TIMESTAMPS action flag.
*
* RETURNS:
* 1 if EOF has been reached,
* 2 if an unexpected EOF has been reached, or an error occurred.
* 0 otherwise.
***************************************************************************
*/
int read_next_sample(int ifd, int action, int curr, char *file, int *rtype, int tab,
struct file_magic *file_magic, struct file_activity *file_actlst,
struct tstamp_ext *rectime, enum on_eof oneof)
{
int rc;
char rec_hdr_tmp[MAX_RECORD_HEADER_SIZE];
/* Read current record */
if ((rc = read_record_hdr(ifd, rec_hdr_tmp, &record_hdr[curr], &file_hdr,
arch_64, endian_mismatch, oneof, sizeof(rec_hdr_tmp), flags,
fmt[f_position])) != 0)
/* End of sa file */
return rc;
*rtype = record_hdr[curr].record_type;
if (*rtype == R_COMMENT) {
if (action & IGNORE_COMMENT) {
/* Ignore COMMENT record */
if (lseek(ifd, MAX_COMMENT_LEN, SEEK_CUR) < MAX_COMMENT_LEN) {
if (oneof == UEOF_CONT)
return 2;
close(ifd);
exit(2);
}
/* Ignore unknown extra structures if present */
if (record_hdr[curr].extra_next && (skip_extra_struct(ifd, endian_mismatch, arch_64) < 0))
return 2;
if (action & SET_TIMESTAMPS) {
if (sa_get_record_timestamp_struct(flags, &record_hdr[curr],
rectime))
return 2;
}
}
else {
/* Display COMMENT record */
print_special_record(&record_hdr[curr], flags, &tm_start, &tm_end,
*rtype, ifd, rectime, file, tab, my_tzname,
file_magic, &file_hdr, act, fmt[f_position],
endian_mismatch, arch_64);
}
}
else if (*rtype == R_RESTART) {
if (action & IGNORE_RESTART) {
/*
* Ignore RESTART record (don't display it)
* but anyway we have to read the CPU number that follows it
* (unless we don't want to do it now).
*/
if (!(action & DONT_READ_CPU_NR)) {
file_hdr.sa_cpu_nr = read_nr_value(ifd, file, file_magic,
endian_mismatch, arch_64, TRUE,
NR_CPUS + 1);
/* Ignore unknown extra structures if present */
if (record_hdr[curr].extra_next && (skip_extra_struct(ifd, endian_mismatch, arch_64) < 0))
return 2;
}
if (action & SET_TIMESTAMPS) {
if (sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime))
return 2;
}
}
else {
/* Display RESTART record */
print_special_record(&record_hdr[curr], flags, &tm_start, &tm_end,
*rtype, ifd, rectime, file, tab, my_tzname,
file_magic, &file_hdr, act, fmt[f_position],
endian_mismatch, arch_64);
}
}
else {
/*
* OK: Previous record was not a special one.
* So read now the extra fields.
*/
if (read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr, file_actlst,
endian_mismatch, arch_64, file, file_magic, oneof,
flags) > 0)
return 2;
if (sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime))
return 2;
}
return 0;
}
/*
***************************************************************************
* Display the field list (used eg. in database format).
*
* IN:
* @act_id Activity to display, or ~0 for all.
***************************************************************************
*/
void list_fields(unsigned int act_id)
{
int i, j;
unsigned int msk;
char *hl;
char hline[HEADER_LINE_LEN] = "";
char out[256];
cprintf_s(IS_COMMENT, "%s", "# hostname;interval;timestamp");
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_ini > 0)) {
if (!HAS_MULTIPLE_OUTPUTS(act[i]->options)) {
sprintf(out, ";%s", act[i]->hdr_line);
cprintf_s(IS_COMMENT, "%s", out);
if ((act[i]->nr_ini > 1) && DISPLAY_HORIZONTALLY(flags)) {
cprintf_s(IS_COMMENT, "%s", "[...]");
}
}
else {
msk = 1;
strncpy(hline, act[i]->hdr_line, sizeof(hline) - 1);
hline[sizeof(hline) - 1] = '\0';
for (hl = strtok(hline, "|"); hl; hl = strtok(NULL, "|"), msk <<= 1) {
if ((hl != NULL) && ((act[i]->opt_flags & 0xff) & msk)) {
if (strchr(hl, '&')) {
j = strcspn(hl, "&");
if ((act[i]->opt_flags & 0xff00) & (msk << 8)) {
/* Display whole header line */
*(hl + j) = ';';
sprintf(out, ";%s", hl);
cprintf_s(IS_COMMENT, "%s", out);
}
else {
/* Display only the first part of the header line */
*(hl + j) = '\0';
sprintf(out, ";%s", hl);
cprintf_s(IS_COMMENT, "%s", out);
}
*(hl + j) = '&';
}
else {
sprintf(out, ";%s", hl);
cprintf_s(IS_COMMENT, "%s", out);
}
if ((act[i]->nr_ini > 1) && DISPLAY_HORIZONTALLY(flags)) {
cprintf_s(IS_COMMENT, "%s", "[...]");
}
}
}
}
}
}
printf("\n");
}
/*
***************************************************************************
* Determine the time (expressed in seconds since the epoch) used as the
* origin on X axis for SVG graphs. If S_F_SVG_ONE_DAY is set, then origin
* will be the beginning of current day (00:00:00) else it will be the time
* of the first sample collected.
*
* RETURNS:
* Time origin on X axis (expressed in seconds since the epoch).
***************************************************************************
*/
time_t get_time_ref(void)
{
struct tm ltm;
time_t t = record_hdr[2].ust_time;
if (DISPLAY_ONE_DAY(flags)) {
localtime_r(&t, <m);
/* Move back to midnight */
ltm.tm_sec = ltm.tm_min = ltm.tm_hour = 0;
t = mktime(<m);
if (t != -1)
return t;
}
return (time_t) record_hdr[2].ust_time;
}
/*
***************************************************************************
* Save or restore position in file.
*
* IN:
* @ifd File descriptor of input file.
* @action DO_SAVE to save position or DO_RESTORE to restore it.
***************************************************************************
*/
void seek_file_position(int ifd, int action)
{
static off_t fpos = -1;
static unsigned int save_cpu_nr = 0;
if (action == DO_SAVE) {
/* Save current file position */
if ((fpos = lseek(ifd, 0, SEEK_CUR)) < 0) {
perror("lseek");
exit(2);
}
save_cpu_nr = file_hdr.sa_cpu_nr;
}
else if (action == DO_RESTORE) {
/* Rewind file */
if ((fpos < 0) || (lseek(ifd, fpos, SEEK_SET) < fpos)) {
perror("lseek");
exit(2);
}
file_hdr.sa_cpu_nr = save_cpu_nr;
}
}
/*
***************************************************************************
* Count number of different items in file. Save these numbers in fields
* @item_list_sz of structure activity, and create the corresponding list
* in field @item_list.
*
* IN:
* @ifd File descriptor of input file.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or
* in UTC depending on whether options -T/-t have been
* used or not) can be saved for current record.
*
* RETURNS:
* 0 if no records are concerned in file, and 1 otherwise.
***************************************************************************
*/
int count_file_items(int ifd, char *file, struct file_magic *file_magic,
struct file_activity *file_actlst, struct tstamp_ext *rectime)
{
int i, eosaf, rtype;
/* Save current file position */
seek_file_position(ifd, DO_SAVE);
/* Init maximum number of items for each activity */
for (i = 0; i < NR_ACT; i++) {
if (!HAS_LIST_ON_CMDLINE(act[i]->options)) {
act[i]->item_list_sz = 0;
}
}
/* Look for the first record that will be displayed */
do {
eosaf = read_next_sample(ifd, IGNORE_RESTART | IGNORE_COMMENT | SET_TIMESTAMPS,
0, file, &rtype, 0, file_magic, file_actlst,
rectime, UEOF_CONT);
if (eosaf)
/* No record to display */
return 0;
}
while ((datecmp(rectime, &tm_start, FALSE) < 0) ||
(datecmp(rectime, &tm_end, FALSE) > 0));
/*
* Read all the file and determine the maximum number
* of items for each activity.
*/
do {
for (i = 0; i < NR_ACT; i++) {
if (!HAS_LIST_ON_CMDLINE(act[i]->options)) {
if (act[i]->f_count_new) {
act[i]->item_list_sz += (*act[i]->f_count_new)(act[i], 0);
}
else if (act[i]->nr[0] > act[i]->item_list_sz) {
act[i]->item_list_sz = act[i]->nr[0];
}
if (HAS_PERSISTENT_VALUES(act[i]->options) &&
(act[i]->item_list_sz < act[i]->nr_ini)) {
/*
* For persistent (i.e. CPU related) activities,
* structures must be allocated for every installed CPU,
* even for offline CPU.
* So item_list_sz cannot be smaller than nr_ini.
*/
act[i]->item_list_sz = act[i]->nr_ini;
}
}
}
do {
eosaf = read_next_sample(ifd, IGNORE_RESTART | IGNORE_COMMENT | SET_TIMESTAMPS,
0, file, &rtype, 0, file_magic, file_actlst,
rectime, UEOF_CONT);
if (eosaf || (datecmp(rectime, &tm_end, FALSE) > 0))
/* End of data file or end time exceeded */
break;
}
while ((rtype == R_RESTART) || (rtype == R_COMMENT));
}
while (!eosaf && !(datecmp(rectime, &tm_end, FALSE) > 0));
/* Rewind file */
seek_file_position(ifd, DO_RESTORE);
return 1;
}
/*
***************************************************************************
* Compute the number of rows that will contain SVG views. Usually only one
* view is displayed on a row, unless the "packed" option has been entered.
* Each activity selected may have several views. Moreover some activities
* may have a number of items that varies within the file: In this case,
* the number of views will depend on the highest number of items saved in
* the file.
*
* IN:
* @ifd File descriptor of input file.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or
* in UTC depending on whether options -T/-t have been
* used or not) can be saved for current record.
* @views_per_row Default number of views displayed on a single row.
*
* OUT:
* @views_per_row Maximum number of views that will be displayed on a
* single row (useful only if "packed" option entered).
* @nr_act_dispd Number of activities that will be displayed.
* May be 0.
*
* RETURNS:
* Number of rows containing views, taking into account only activities
* to be displayed, and selected period of time (options -s/-e).
* Result may be 0.
***************************************************************************
*/
int get_svg_graph_nr(int ifd, char *file, struct file_magic *file_magic,
struct file_activity *file_actlst, struct tstamp_ext *rectime,
int *views_per_row, int *nr_act_dispd)
{
int i, n, p, tot_g_nr = 0;
*nr_act_dispd = 0;
/* Count items in file */
if (!count_file_items(ifd, file, file_magic, file_actlst, rectime))
/* No record to display => No graph */
return 0;
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 (!IS_SELECTED(act[p]->options) || !act[p]->g_nr)
continue;
(*nr_act_dispd)++;
if (PACK_VIEWS(flags)) {
/*
* One activity = one row with multiple views.
* Exception is A_MEMORY, for which one activity may be
* displayed in two rows if both memory *and* swap utilization
* have been selected.
*/
if ((act[p]->id == A_MEMORY) &&
(DISPLAY_MEMORY(act[p]->opt_flags) && DISPLAY_SWAP(act[p]->opt_flags))) {
n = 2;
}
else {
n = 1;
}
}
else {
/* One activity = multiple rows with only one view */
n = act[p]->g_nr;
}
if (ONE_GRAPH_PER_ITEM(act[p]->options)) {
n = n * act[p]->item_list_sz;
}
if (act[p]->g_nr > *views_per_row) {
*views_per_row = act[p]->g_nr;
}
tot_g_nr += n;
}
if (*views_per_row > MAX_VIEWS_ON_A_ROW) {
*views_per_row = MAX_VIEWS_ON_A_ROW;
}
return tot_g_nr;
}
/*
***************************************************************************
* Display *one* sample of statistics for one or several activities,
* checking that all conditions are met before printing (time start, time
* end, interval). Current record should be a record of statistics (R_STATS),
* not a special one (R_RESTART or R_COMMENT).
*
* IN:
* @curr Index in array for current sample statistics.
* @use_tm_start Set to non-zero if option -s has been used.
* @use_tm_end Set to non-zero if option -e has been used.
* @reset Set to TRUE if last_uptime should be reinitialized
* (used in next_slice() function).
* @parm Pointer on parameters depending on output format
* (eg.: number of tabulations to print).
* @rectime Structure where timestamp (expressed in local time
* or in UTC depending on whether options -T/-t have
* been used or not) has been saved for current record.
* @reset_cd TRUE if static cross_day variable should be reset.
* @act_id Activity to display (only for formats where
* activities are displayed one at a time) or
* ALL_ACTIVITIES for all.
*
* OUT:
* @cnt Set to 0 to indicate that no other lines of stats
* should be displayed.
*
* RETURNS:
* 1 if stats have been successfully displayed.
***************************************************************************
*/
int generic_write_stats(int curr, enum time_mode use_tm_start, enum time_mode use_tm_end,
int reset, long *cnt, void *parm, struct tstamp_ext *rectime,
int reset_cd, unsigned int act_id)
{
int i;
unsigned long long dt, itv;
char cur_date[TIMESTAMP_LEN], cur_time[TIMESTAMP_LEN], *pre = NULL;
static int cross_day = FALSE;
if (reset_cd) {
/*
* See note in sar.c.
* NB: Resetting cross_day is needed only if datafile
* may be rewinded (eg. in db or ppc output formats).
*/
cross_day = 0;
}
/*
* Check time (1).
* For this first check, we use the time interval entered on
* the command line. This is equivalent to sar's option -i which
* selects records at seconds as close as possible to the number
* specified by the interval parameter.
*/
if (!next_slice(record_hdr[2].uptime_cs, record_hdr[curr].uptime_cs,
reset, interval))
/* Not close enough to desired interval */
return 0;
/* Check if we are beginning a new day */
if (use_tm_start && record_hdr[!curr].ust_time &&
(record_hdr[curr].ust_time > record_hdr[!curr].ust_time) &&
(record_hdr[curr].hour < record_hdr[!curr].hour)) {
cross_day = TRUE;
}
/* Check time (2) */
if (use_tm_end && (datecmp(rectime, &tm_end, cross_day) > 0)) {
/* End time exceeded */
*cnt = 0;
return 0;
}
/* Get interval values in 1/100th of a second */
get_itv_value(&record_hdr[curr], &record_hdr[!curr], &itv);
dt = itv / 100;
/* Correct rounding error for dt */
if ((itv % 100) >= 50) {
dt++;
}
/* Set date and time strings for current record */
set_record_timestamp_string(flags, cur_date, cur_time, TIMESTAMP_LEN, rectime);
if (*fmt[f_position]->f_timestamp) {
pre = (char *) (*fmt[f_position]->f_timestamp)(parm, F_BEGIN, cur_date, cur_time,
my_tzname, dt, &record_hdr[curr],
&file_hdr, flags);
}
/* Display statistics */
for (i = 0; i < NR_ACT; i++) {
if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
continue;
if ((TEST_MARKUP(fmt[f_position]->options) && CLOSE_MARKUP(act[i]->options)) ||
(IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) ||
(format == F_RAW_OUTPUT)) {
if (format == F_JSON_OUTPUT) {
/* JSON output */
int *tab = (int *) parm;
if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) {
if (*fmt[f_position]->f_timestamp) {
(*fmt[f_position]->f_timestamp)(tab, F_MAIN, cur_date, cur_time, NULL,
dt, &record_hdr[curr],
&file_hdr, flags);
}
}
(*act[i]->f_json_print)(act[i], curr, *tab, itv);
}
else if (format == F_XML_OUTPUT) {
/* XML output */
int *tab = (int *) parm;
(*act[i]->f_xml_print)(act[i], curr, *tab, itv);
}
else if (format == F_SVG_OUTPUT) {
/* SVG output */
struct svg_parm *svg_p = (struct svg_parm *) parm;
svg_p->dt = (unsigned long) dt;
(*act[i]->f_svg_print)(act[i], curr, F_MAIN, svg_p, itv, &record_hdr[curr]);
}
else if (format == F_RAW_OUTPUT) {
/* Raw output */
if (DISPLAY_DEBUG_MODE(flags)) {
char out[128];
sprintf(out, "# name; %s; nr_curr; %d; nr_alloc; %d; nr_ini; %d\n",
act[i]->name, act[i]->nr[curr], act[i]->nr_allocated,
act[i]->nr_ini);
cprintf_s(IS_COMMENT, "%s", out);
}
if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) {
(*act[i]->f_raw_print)(act[i], pre, curr);
}
}
else if (format == F_PCP_OUTPUT) {
/* PCP archive */
if (*act[i]->f_pcp_print) {
(*act[i]->f_pcp_print)(act[i], curr);
}
}
else {
/* Other output formats: db, ppc */
(*act[i]->f_render)(act[i], (format == F_DB_OUTPUT), pre, curr, itv);
}
}
}
if (*fmt[f_position]->f_timestamp) {
(*fmt[f_position]->f_timestamp)(parm, F_END, cur_date, cur_time, NULL, dt,
&record_hdr[curr], &file_hdr, flags);
}
return 1;
}
/*
***************************************************************************
* Read stats for current activity from file and print them.
* Display at most <count> lines of stats (and possibly comments inserted
* in file) located between two LINUX RESTART messages.
*
* IN:
* @ifd File descriptor of input file.
* @curr Index in array for current sample statistics.
* @act_id Activity to display, or ~0 for all.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or in UTC
* depending on whether options -T/-t have been used or not) can
* be saved for current record.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.
*
* OUT:
* @curr Index in array for next sample statistics.
* @cnt Number of lines of stats remaining 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 rw_curr_act_stats(int ifd, int *curr, long *cnt, int *eosaf,
unsigned int act_id, int *reset, struct file_activity *file_actlst,
struct tstamp_ext *rectime, char *file,
struct file_magic *file_magic)
{
int rtype;
int next, reset_cd;
/* Rewind file */
seek_file_position(ifd, DO_RESTORE);
if (DISPLAY_FIELD_LIST(fmt[f_position]->options)) {
/* Print field list */
list_fields(act_id);
}
/*
* 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;
reset_cd = 1;
do {
/* Display <count> lines of stats */
*eosaf = read_next_sample(ifd, IGNORE_RESTART | DONT_READ_CPU_NR,
*curr, file, &rtype, 0, file_magic,
file_actlst, rectime, UEOF_STOP);
if (*eosaf || (rtype == R_RESTART))
break;
if (rtype != R_COMMENT) {
next = generic_write_stats(*curr, tm_start.use, tm_end.use, *reset, cnt,
NULL, rectime, reset_cd, act_id);
reset_cd = 0;
if (next) {
/*
* next is set to 1 when we were close enough to desired interval.
* In this case, the call to generic_write_stats() has actually
* displayed a line of stats.
*/
*curr ^= 1;
if (*cnt > 0) {
(*cnt)--;
}
}
*reset = FALSE;
}
}
while (*cnt);
*reset = TRUE;
}
/*
* **************************************************************************
* Read stats for current activity from file and fill the buffers with SVG
* code that will be output later.
* At most <count> lines of stats are taken into account.
*
* IN:
* @ifd File descriptor of input file.
* @curr Index in array for current sample statistics.
* @a Current activity.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or in UTC
* depending on whether options -T/-t have been used or not) can
* be saved for current record.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.
* @parm SVG specific parameters.
*
* OUT:
* @cnt Number of lines of stats remaining 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).
* @parm Some members are updated.
***************************************************************************
*/
void gen_curr_act_svg_code(int ifd, int *curr, long *cnt, int *eosaf, struct activity *a,
int *reset, struct file_activity *file_actlst,
struct tstamp_ext *rectime, char *file,
struct file_magic *file_magic, struct svg_parm *parm)
{
int rtype;
int next, reset_cd;
*cnt = count;
reset_cd = 1;
do {
*eosaf = read_next_sample(ifd, IGNORE_RESTART | IGNORE_COMMENT | SET_TIMESTAMPS,
*curr, file, &rtype, 0, file_magic,
file_actlst, rectime, UEOF_CONT);
if (*eosaf)
break;
if (rtype == R_RESTART) {
parm->restart = TRUE;
*reset = TRUE;
/* Go to next statistics record, if possible */
do {
*eosaf = read_next_sample(ifd, IGNORE_RESTART | IGNORE_COMMENT | SET_TIMESTAMPS,
*curr, file, &rtype, 0, file_magic,
file_actlst, rectime, UEOF_CONT);
}
while (!*eosaf && ((rtype == R_RESTART) || (rtype == R_COMMENT)));
*curr ^= 1;
}
else if (rtype != R_COMMENT) {
next = generic_write_stats(*curr, tm_start.use, tm_end.use, *reset, cnt,
parm, rectime, reset_cd, a->id);
reset_cd = 0;
if (next) {
/*
* next is set to 1 when we were close enough to desired interval.
* In this case, the call to generic_write_stats() has actually
* displayed a line of stats.
*/
parm->restart = FALSE;
parm->ust_time_end = record_hdr[*curr].ust_time;
*curr ^= 1;
if (*cnt > 0) {
(*cnt)--;
}
}
*reset = FALSE;
}
}
while (!*eosaf && *cnt);
*reset = TRUE;
}
/*
***************************************************************************
* Read stats for current activity from file and output its SVG code.
* At most <count> lines of stats are taken into account.
*
* IN:
* @ifd File descriptor of input file.
* @curr Index in array for current sample statistics.
* @a Current activity.
* @file_actlst List of (known or unknown) activities in file.
* @rectime Structure where timestamp (expressed in local time or in UTC
* depending on whether options -T/-t have been used or not) can
* be saved for current record.
* @file Name of file being read.
* @file_magic file_magic structure filled with file magic header data.