forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa_common.c
3837 lines (3448 loc) · 111 KB
/
sa_common.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 and sadf common routines.
* (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 <time.h>
#include <errno.h>
#include <unistd.h> /* For STDOUT_FILENO, among others */
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <float.h>
#include "version.h"
#include "sa.h"
#include "ioconf.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
int default_file_used = FALSE;
extern struct act_bitmap cpu_bitmap;
extern unsigned int dm_major;
unsigned int hdr_types_nr[] = {FILE_HEADER_ULL_NR, FILE_HEADER_UL_NR, FILE_HEADER_U_NR};
unsigned int act_types_nr[] = {FILE_ACTIVITY_ULL_NR, FILE_ACTIVITY_UL_NR, FILE_ACTIVITY_U_NR};
unsigned int rec_types_nr[] = {RECORD_HEADER_ULL_NR, RECORD_HEADER_UL_NR, RECORD_HEADER_U_NR};
unsigned int extra_desc_types_nr[] = {EXTRA_DESC_ULL_NR, EXTRA_DESC_UL_NR, EXTRA_DESC_U_NR};
/*
***************************************************************************
* Look for activity in array.
*
* IN:
* @act Array of activities.
* @act_flag Activity flag to look for.
* @stop TRUE if sysstat should exit when activity is not found.
*
* RETURNS:
* Position of activity in array, or -1 if not found (this may happen when
* reading data from a system activity file created by another version of
* sysstat).
***************************************************************************
*/
int get_activity_position(struct activity *act[], unsigned int act_flag, int stop)
{
int i;
for (i = 0; i < NR_ACT; i++) {
if (act[i]->id == act_flag)
return i;
}
if (stop) {
PANIC((int) act_flag);
}
return -1;
}
/*
***************************************************************************
* Count number of activities with given option.
*
* IN:
* @act Array of activities.
* @option Option that activities should have to be counted
* (eg. AO_COLLECTED...)
* @count Set to COUNT_OUTPUTS if each output should be counted for
* activities with multiple outputs.
*
* RETURNS:
* Number of selected activities
***************************************************************************
*/
int get_activity_nr(struct activity *act[], unsigned int option, enum count_mode count)
{
int i, n = 0;
unsigned int msk;
for (i = 0; i < NR_ACT; i++) {
if ((act[i]->options & option) == option) {
if (HAS_MULTIPLE_OUTPUTS(act[i]->options) && (count == COUNT_OUTPUTS)) {
for (msk = 1; msk < 0x100; msk <<= 1) {
if ((act[i]->opt_flags & 0xff) & msk) {
n++;
}
}
}
else {
n++;
}
}
}
return n;
}
/*
***************************************************************************
* Look for the most recent of saDD and saYYYYMMDD to decide which one to
* use. If neither exists then use saDD by default.
*
* IN:
* @sa_dir Directory where standard daily data files are saved.
* @rectime Structure containing the current date.
*
* OUT:
* @sa_name 0 to use saDD data files,
* 1 to use saYYYYMMDD data files.
***************************************************************************
*/
void guess_sa_name(char *sa_dir, struct tm *rectime, int *sa_name)
{
char filename[MAX_FILE_LEN];
struct stat sb;
time_t sa_mtime;
long nsec;
/* Use saDD by default */
*sa_name = 0;
/* Look for saYYYYMMDD */
snprintf(filename, sizeof(filename),
"%s/sa%04d%02d%02d", sa_dir,
rectime->tm_year + 1900,
rectime->tm_mon + 1,
rectime->tm_mday);
filename[sizeof(filename) - 1] = '\0';
if (stat(filename, &sb) < 0)
/* Cannot find or access saYYYYMMDD, so use saDD */
return;
sa_mtime = sb.st_mtime;
nsec = sb.st_mtim.tv_nsec;
/* Look for saDD */
snprintf(filename, sizeof(filename),
"%s/sa%02d", sa_dir,
rectime->tm_mday);
filename[sizeof(filename) - 1] = '\0';
if (stat(filename, &sb) < 0) {
/* Cannot find or access saDD, so use saYYYYMMDD */
*sa_name = 1;
return;
}
if ((sa_mtime > sb.st_mtime) ||
((sa_mtime == sb.st_mtime) && (nsec > sb.st_mtim.tv_nsec))) {
/* saYYYYMMDD is more recent than saDD, so use it */
*sa_name = 1;
}
}
/*
***************************************************************************
* Set current daily data file name.
*
* IN:
* @datafile If not an empty string then this is the alternate directory
* location where daily data files will be saved.
* @d_off Day offset (number of days to go back in the past).
* @sa_name 0 for saDD data files,
* 1 for saYYYYMMDD data files,
* -1 if unknown. In this case, will look for the most recent
* of saDD and saYYYYMMDD and use it.
*
* OUT:
* @datafile Name of daily data file.
***************************************************************************
*/
void set_default_file(char *datafile, int d_off, int sa_name)
{
char sa_dir[MAX_FILE_LEN];
struct tm rectime = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
int err = 0;
/* Set directory where daily data files will be saved */
if (datafile[0]) {
strncpy(sa_dir, datafile, sizeof(sa_dir));
}
else {
strncpy(sa_dir, SA_DIR, sizeof(sa_dir));
}
sa_dir[sizeof(sa_dir) - 1] = '\0';
get_time(&rectime, d_off);
if (sa_name < 0) {
/*
* Look for the most recent of saDD and saYYYYMMDD
* and use it. If neither exists then use saDD.
* sa_name is set accordingly.
*/
guess_sa_name(sa_dir, &rectime, &sa_name);
}
if (sa_name) {
/* Using saYYYYMMDD data files */
err = snprintf(datafile, MAX_FILE_LEN,
"%s/sa%04d%02d%02d", sa_dir,
rectime.tm_year + 1900,
rectime.tm_mon + 1,
rectime.tm_mday);
}
else {
/* Using saDD data files */
err = snprintf(datafile, MAX_FILE_LEN,
"%s/sa%02d", sa_dir,
rectime.tm_mday);
}
datafile[MAX_FILE_LEN - 1] = '\0';
if ((err < 0) || (err >= MAX_FILE_LEN)) {
fprintf(stderr, "%s: %s\n", __FUNCTION__, datafile);
exit(1);
}
default_file_used = TRUE;
#ifdef DEBUG
fprintf(stderr, "%s: Datafile: %s\n", __FUNCTION__, datafile);
#endif
}
/*
***************************************************************************
* Check data file type. If it is a directory then this is the alternate
* location where daily data files will be saved.
*
* IN:
* @datafile Name of the daily data file. May be a directory.
* @d_off Day offset (number of days to go back in the past).
* @sa_name 0 for saDD data files,
* 1 for saYYYYMMDD data files,
* -1 if unknown. In this case, will look for the most recent
* of saDD and saYYYYMMDD and use it.
*
*
* OUT:
* @datafile Name of the daily data file. This is now a plain file, not
* a directory.
*
* RETURNS:
* 1 if @datafile was a directory, and 0 otherwise.
***************************************************************************
*/
int check_alt_sa_dir(char *datafile, int d_off, int sa_name)
{
if (check_dir(datafile)) {
/*
* This is a directory: So append
* the default file name to it.
*/
set_default_file(datafile, d_off, sa_name);
return 1;
}
return 0;
}
/*
***************************************************************************
* Display sysstat version used to create system activity data file.
*
* IN:
* @st Output stream (stderr or stdout).
* @file_magic File magic header.
***************************************************************************
*/
void display_sa_file_version(FILE *st, struct file_magic *file_magic)
{
fprintf(st, _("File created by sar/sadc from sysstat version %d.%d.%d"),
file_magic->sysstat_version,
file_magic->sysstat_patchlevel,
file_magic->sysstat_sublevel);
if (file_magic->sysstat_extraversion) {
fprintf(st, ".%d", file_magic->sysstat_extraversion);
}
fprintf(st, "\n");
}
/*
***************************************************************************
* An invalid system activity file has been opened for reading.
* If this file was created by an old version of sysstat, tell it to the
* user...
*
* IN:
* @fd Descriptor of the file that has been opened.
* @file_magic file_magic structure filled with file magic header data.
* May contain invalid data.
* @file Name of the file being read.
* @n Number of bytes read while reading file magic header.
* This function may also be called after failing to read file
* standard header, or if CPU activity has not been found in
* file. In this case, n is set to 0.
***************************************************************************
*/
void handle_invalid_sa_file(int fd, struct file_magic *file_magic, char *file,
int n)
{
fprintf(stderr, _("Invalid system activity file: %s\n"), file);
if (n == FILE_MAGIC_SIZE) {
if ((file_magic->sysstat_magic == SYSSTAT_MAGIC) || (file_magic->sysstat_magic == SYSSTAT_MAGIC_SWAPPED)) {
unsigned short fmt_magic;
/* This is a sysstat file, but this file has an old format */
display_sa_file_version(stderr, file_magic);
fmt_magic = file_magic->sysstat_magic == SYSSTAT_MAGIC ?
file_magic->format_magic : __builtin_bswap16(file_magic->format_magic);
fprintf(stderr,
_("Current sysstat version cannot read the format of this file (%#x)\n"),
fmt_magic);
if (fmt_magic >= FORMAT_MAGIC_2171) {
fprintf(stderr,
_("Try to convert it to current format. Enter:\n\n"));
fprintf(stderr, "sadf -c %s > %s.new\n\n", file, file);
fprintf(stderr,
_("You should then be able to read the new file created (%s.new)\n"),
file);
}
}
}
close (fd);
exit(3);
}
/*
***************************************************************************
* Display an error message then exit.
***************************************************************************
*/
void print_collect_error(void)
{
fprintf(stderr, _("Requested activities not available\n"));
exit(1);
}
/*
***************************************************************************
* Fill system activity file magic header.
*
* IN:
* @file_magic System activity file magic header.
***************************************************************************
*/
void enum_version_nr(struct file_magic *fm)
{
char *v;
char version[16];
fm->sysstat_extraversion = 0;
strcpy(version, VERSION);
/* Get version number */
if ((v = strtok(version, ".")) == NULL)
return;
fm->sysstat_version = atoi(v) & 0xff;
/* Get patchlevel number */
if ((v = strtok(NULL, ".")) == NULL)
return;
fm->sysstat_patchlevel = atoi(v) & 0xff;
/* Get sublevel number */
if ((v = strtok(NULL, ".")) == NULL)
return;
fm->sysstat_sublevel = atoi(v) & 0xff;
/* Get extraversion number. Don't necessarily exist */
if ((v = strtok(NULL, ".")) == NULL)
return;
fm->sysstat_extraversion = atoi(v) & 0xff;
}
/*
***************************************************************************
* Write data to file. If the write() call was interrupted by a signal, try
* again so that the whole buffer can be written.
*
* IN:
* @fd Output file descriptor.
* @buf Data buffer.
* @nr_bytes Number of bytes to write.
*
* RETURNS:
* Number of bytes written to file, or -1 on error.
***************************************************************************
*/
int write_all(int fd, const void *buf, int nr_bytes)
{
int block, offset = 0;
char *buffer = (char *) buf;
while (nr_bytes > 0) {
block = write(fd, &buffer[offset], nr_bytes);
if (block < 0) {
if (errno == EINTR)
continue;
return block;
}
if (block == 0)
return offset;
offset += block;
nr_bytes -= block;
}
return offset;
}
#ifndef SOURCE_SADC
/*
* **************************************************************************
* Init buffers for min and max values.
*
* IN:
* @a Activity for which buffers are to be initialized.
* @start_slot First slot to init.
* @nr_alloc Number of slots to init.
***************************************************************************
*/
void init_minmax_buf(struct activity *a, size_t start_slot, size_t nr_alloc)
{
int j;
double *val;
for (j = start_slot * a->nr2 * a->xnr;
j < nr_alloc * a->nr2 * a->xnr; j++) {
val = (double *) (a->spmin + j);
*val = DBL_MAX;
val = (double *) (a->spmax + j);
*val = -DBL_MAX;
}
}
/*
* **************************************************************************
* Allocate buffers for min and max values.
*
* IN:
* @a Activity for which buffers are to be initialized.
* @nr_alloc Number of slots to allocate.
* @flags Flags for common options and system state.
***************************************************************************
*/
void allocate_minmax_buf(struct activity *a, size_t nr_alloc, uint64_t flags)
{
/* nr_alloc should be greater than a->nr_spalloc */
if (nr_alloc <= a->nr_spalloc) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: alloc=%zu allocated=%d\n",
__FUNCTION__, a->name, nr_alloc, a->nr_spalloc);
#endif
return;
}
#ifdef DEBUG
if (nr_alloc < a->nr_allocated) {
/* Should never happen */
fprintf(stderr, "%s: %s: spalloc=%zu allocated=%d\n",
__FUNCTION__, a->name, nr_alloc, a->nr_allocated);
exit(4);
}
#endif
if (DISPLAY_MINMAX(flags) && a->xnr) {
/* Look for a possible overflow */
check_overflow((unsigned int) a->xnr,
(unsigned int) nr_alloc,
(unsigned int) a->nr2);
/* Allocate arrays for min and max values... */
SREALLOC(a->spmin, void,
nr_alloc * (size_t) a->nr2 * (size_t) a->xnr * sizeof(double));
SREALLOC(a->spmax, void,
nr_alloc * (size_t) a->nr2 * (size_t) a->xnr * sizeof(double));
/* ... and init them */
init_minmax_buf(a, a->nr_spalloc, nr_alloc);
a->nr_spalloc = nr_alloc;
}
}
/*
***************************************************************************
* Allocate buffers for one activity.
*
* IN:
* @a Activity for which buffers are to be initialized.
* @nr_alloc Number of structures to allocate.
* @flags Flags for common options and system state.
***************************************************************************
*/
void allocate_buffers(struct activity *a, size_t nr_alloc, uint64_t flags)
{
int j;
/* nr_alloc should always be greater than a->nr_allocated */
if (nr_alloc <= a->nr_allocated) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: alloc=%zu allocated=%d\n",
__FUNCTION__, a->name, nr_alloc, a->nr_allocated);
#endif
return;
}
/* Look for a possible overflow */
check_overflow((unsigned int) a->msize,
(unsigned int) nr_alloc,
(unsigned int) a->nr2);
for (j = 0; j < 3; j++) {
SREALLOC(a->buf[j], void,
(size_t) a->msize * nr_alloc * (size_t) a->nr2);
/* If its a reallocation then init additional space which has been allocated */
if (a->nr_allocated) {
memset((char *) a->buf[j] + a->msize * a->nr_allocated * a->nr2, 0,
(size_t) a->msize * (size_t) (nr_alloc - a->nr_allocated) * (size_t) a->nr2);
}
}
a->nr_allocated = nr_alloc;
/* Allocate buffers for min and max values if necessary */
allocate_minmax_buf(a, nr_alloc, flags);
}
/*
* **************************************************************************
* Allocate structures for all activities.
*
* IN:
* @act Array of activities.
* @flags Flags for common options and system state.
***************************************************************************
*/
void allocate_structures(struct activity *act[], uint64_t flags)
{
int i;
for (i = 0; i < NR_ACT; i++) {
if (act[i]->nr_ini > 0) {
allocate_buffers(act[i], (size_t) act[i]->nr_ini, flags);
}
}
}
/*
***************************************************************************
* Free structures.
*
* IN:
* @act Array of activities.
***************************************************************************
*/
void free_structures(struct activity *act[])
{
int i, j;
for (i = 0; i < NR_ACT; i++) {
if (act[i]->nr_allocated > 0) {
for (j = 0; j < 3; j++) {
if (act[i]->buf[j]) {
free(act[i]->buf[j]);
act[i]->buf[j] = NULL;
}
}
act[i]->nr_allocated = 0;
}
if (act[i]->nr_spalloc > 0) {
if (act[i]->spmin) {
free(act[i]->spmin);
act[i]->spmin = NULL;
}
if (act[i]->spmax) {
free(act[i]->spmax);
act[i]->spmax = NULL;
}
act[i]->nr_spalloc = 0;
}
}
}
/*
* **************************************************************************
* Reallocate buffers for min/max values.
*
* IN:
* @a Activity whose buffers need to be reallocated.
* @nr_min Minimum number of items that the new buffers should be able
* to receive.
* @flags Flags for common options and system state.
***************************************************************************
*/
void reallocate_minmax_buf(struct activity *a, __nr_t nr_min, uint64_t flags)
{
size_t nr_realloc;
if (nr_min <= 0) {
nr_min = 1;
}
if (!a->nr_spalloc) {
nr_realloc = nr_min;
}
else {
nr_realloc = a->nr_spalloc;
do {
nr_realloc = nr_realloc * 2;
}
while (nr_realloc < nr_min);
}
/* Reallocate buffers for current activity */
allocate_minmax_buf(a, nr_realloc, flags);
}
/*
***************************************************************************
* Reallocate all the buffers for a given activity (main buffers and
* spmin/spmax buffers).
*
* IN:
* @a Activity whose buffers need to be reallocated.
* @nr_min Minimum number of items that the new buffers should be able
* to receive.
* @flags Flags for common options and system state.
***************************************************************************
*/
void reallocate_buffers(struct activity *a, __nr_t nr_min, uint64_t flags)
{
size_t nr_realloc;
if (nr_min <= 0) {
nr_min = 1;
}
if (!a->nr_allocated) {
nr_realloc = nr_min;
}
else {
nr_realloc = a->nr_allocated;
do {
nr_realloc = nr_realloc * 2;
}
while (nr_realloc < nr_min);
}
/* Reallocate buffers for current activity */
allocate_buffers(a, nr_realloc, flags);
}
/*
***************************************************************************
* Check if we are close enough to desired interval.
*
* IN:
* @uptime_ref Uptime used as reference. This is the system uptime for the
* first sample statistics, or the first system uptime after a
* LINUX RESTART (in 1/100th of a second).
* @uptime Current system uptime (in 1/100th of a second).
* @reset TRUE if @last_uptime should be reset with @uptime_ref.
* @interval Interval of time.
*
* RETURNS:
* TRUE if we are actually close enough to desired interval, FALSE otherwise.
***************************************************************************
*/
int next_slice(unsigned long long uptime_ref, unsigned long long uptime,
int reset, long interval)
{
unsigned long file_interval, entry;
static unsigned long long last_uptime = 0;
int min, max, pt1, pt2;
double f;
/* uptime is expressed in 1/100th of a second */
if (!last_uptime || reset) {
last_uptime = uptime_ref;
}
/* Interval cannot be greater than 0xffffffff here */
f = ((double) ((uptime - last_uptime) & 0xffffffff)) / 100;
file_interval = (unsigned long) f;
if ((f * 10) - (file_interval * 10) >= 5) {
file_interval++; /* Rounding to correct value */
}
last_uptime = uptime;
if (interval == 1)
/* Smallest time interval: Always close enough to desired interval */
return TRUE;
/*
* A few notes about the "algorithm" used here to display selected entries
* from the system activity file (option -f with -i flag):
* Let Iu be the interval value given by the user on the command line,
* In the interval between current and previous sample,
* and En the current sample (identified by its time stamp) in the file.
* En will ne displayed if there is an integer p so that:
* p * Iu belongs to [En - In/2, En + In/2[.
*/
f = ((double) ((uptime - uptime_ref) & 0xffffffff)) / 100;
entry = (unsigned long) f;
if ((f * 10) - (entry * 10) >= 5) {
entry++;
}
min = entry - (file_interval / 2);
max = entry + (file_interval / 2) + (file_interval & 0x1);
pt1 = (entry / interval) * interval;
pt2 = ((entry / interval) + 1) * interval;
return (((pt1 >= min) && (pt1 < max)) || ((pt2 >= min) && (pt2 < max)));
}
/*
***************************************************************************
* Use time stamp to fill tstamp_ext structure.
*
* IN:
* @timestamp Timestamp to decode (format: HH:MM:SS).
*
* OUT:
* @tse Structure containing the decoded timestamp.
*
* RETURNS:
* 0 if the timestamp has been successfully decoded, 1 otherwise.
***************************************************************************
*/
int decode_timestamp(char timestamp[], struct tstamp_ext *tse)
{
timestamp[2] = timestamp[5] = '\0';
if ((strspn(timestamp, DIGITS) != 2) ||
(strspn(×tamp[3], DIGITS) != 2) ||
(strspn(×tamp[6], DIGITS) != 2))
return 1;
tse->tm_time.tm_sec = atoi(×tamp[6]);
tse->tm_time.tm_min = atoi(×tamp[3]);
tse->tm_time.tm_hour = atoi(timestamp);
if ((tse->tm_time.tm_sec < 0) || (tse->tm_time.tm_sec > 59) ||
(tse->tm_time.tm_min < 0) || (tse->tm_time.tm_min > 59) ||
(tse->tm_time.tm_hour < 0) || (tse->tm_time.tm_hour > 23)) {
tse->use = NO_TIME;
return 1;
}
tse->use = USE_HHMMSS_T;
return 0;
}
/*
***************************************************************************
* Use time stamp to fill tstamp_ext structure.
*
* IN:
* @timestamp Epoch time to decode (format: number of seconds since
* Januray 1st 1970 00:00:00 UTC).
* @flags Flags for common options and system state.
*
* OUT:
* @tse Structure containing the decoded epoch time.
*
* RETURNS:
* 0 if the epoch time has been successfully decoded, 1 otherwise.
***************************************************************************
*/
int decode_epoch(char timestamp[], struct tstamp_ext *tse, uint64_t flags)
{
tse->epoch_time = atol(timestamp);
if (!tse->epoch_time) {
tse->use = NO_TIME;
return 1;
}
tse->use = USE_EPOCH_T;
return 0;
}
/*
***************************************************************************
* Compare two timestamps.
*
* IN:
* @rectime Date and time for current sample.
* @tse Timestamp used as reference.
* @cross_day TRUE if a new day has been started.
*
* RETURNS:
* A positive value if @rectime is greater than @tse,
* a negative one otherwise.
* Also returns 0 if no valid time is saved in @tse.
***************************************************************************
*/
int datecmp(struct tstamp_ext *rectime, struct tstamp_ext *tse, int cross_day)
{
int tm_hour;
switch (tse->use) {
case USE_HHMMSS_T:
/*
* This is necessary if we want to properly handle something like:
* sar -s time_start -e time_end with
* time_start(day D) > time_end(day D+1)
*/
tm_hour = rectime->tm_time.tm_hour + (24 * (cross_day != 0));
if (tm_hour == tse->tm_time.tm_hour) {
if (rectime->tm_time.tm_min == tse->tm_time.tm_min)
return (rectime->tm_time.tm_sec - tse->tm_time.tm_sec);
else
return (rectime->tm_time.tm_min - tse->tm_time.tm_min);
}
else
return (tm_hour - tse->tm_time.tm_hour);
case USE_EPOCH_T:
return (rectime->epoch_time - tse->epoch_time);
default: /* NO_TIME */
return 0;
}
}
/*
***************************************************************************
* Parse a timestamp entered on the command line (hh:mm[:ss] or number of
* seconds since the Epoch) and decode it.
*
* IN:
* @argv Arguments list.
* @opt Index in the arguments list.
* @def_timestamp Default timestamp to use.
* @flags Flags for common options and system state.
*
* OUT:
* @tse Structure containing the decoded timestamp.
*
* RETURNS:
* 0 if the timestamp has been successfully decoded, 1 otherwise.
***************************************************************************
*/
int parse_timestamp(char *argv[], int *opt, struct tstamp_ext *tse,
const char *def_timestamp, uint64_t flags)
{
char timestamp[11];
int ok = FALSE;
if (argv[++(*opt)] && strncmp(argv[*opt], "-", 1)) {
switch (strlen(argv[*opt])) {
case 5:
if (argv[*opt][2] != ':')
break;
strncpy(timestamp, argv[(*opt)++], 5);
timestamp[5] = '\0';
strcat(timestamp, ":00");
ok = TRUE;
break;
case 8:
if ((argv[*opt][2] != ':') || (argv[*opt][5] != ':'))
break;
strncpy(timestamp, argv[(*opt)++], 8);
ok = TRUE;
break;
case 10:
if (strspn(argv[*opt], DIGITS) == 10) {
/* This is actually a timestamp */
strncpy(timestamp, argv[(*opt)++], 10);
timestamp[10] = '\0';
return decode_epoch(timestamp, tse, flags);
}
break;
}
}
if (!ok) {
strncpy(timestamp, def_timestamp, 8);
}
timestamp[8] = '\0';
return decode_timestamp(timestamp, tse);
}
/*
***************************************************************************
* Set interval value.
*
* IN:
* @record_hdr_curr Record with current sample statistics.
* @record_hdr_prev Record with previous sample statistics.
*
* OUT:
* @itv Interval of time in 1/100th of a second.
***************************************************************************
*/
void get_itv_value(struct record_header *record_hdr_curr,
struct record_header *record_hdr_prev,
unsigned long long *itv)
{
/* Interval value in jiffies */
*itv = get_interval(record_hdr_prev->uptime_cs,
record_hdr_curr->uptime_cs);
}
/*
***************************************************************************
* Fill the tm_time structure with the file's creation date, based on file's
* time data saved in file header.
* The resulting timestamp is expressed in the locale of the file creator or
* in the user's own locale, depending on whether option -t has been used
* or not.
*
* IN:
* @flags Flags for common options and system state.
* @file_hdr System activity file standard header.
*
* OUT:
* @tm_time Date (and possibly time) from file header. Only the date,
* not the time, should be used by the caller.
***************************************************************************
*/
void get_file_timestamp_struct(uint64_t flags, struct tm *tm_time,
struct file_header *file_hdr)
{
time_t t = file_hdr->sa_ust_time;
if (PRINT_TRUE_TIME(flags)) {
/* Get local time. This is just to fill fields with a default value. */
get_time(tm_time, 0);
tm_time->tm_mday = file_hdr->sa_day;
tm_time->tm_mon = file_hdr->sa_month;
tm_time->tm_year = file_hdr->sa_year;
/*
* Call mktime() to set DST (Daylight Saving Time) flag.
* Has anyone a better way to do it?
*/
tm_time->tm_hour = tm_time->tm_min = tm_time->tm_sec = 0;
mktime(tm_time);
}
else {
localtime_r(&t, tm_time);
}
}
/*
***************************************************************************
* Print report header.