forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sadc.c
1461 lines (1274 loc) · 38.8 KB
/
sadc.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
/*
* sadc: system activity data collector
* (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 <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <dirent.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/utsname.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
#if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
#include "sensors/sensors.h"
#include "sensors/error.h"
#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
extern char *tzname[2];
long interval = -1;
uint64_t flags = 0;
int optz = 0;
char timestamp[2][TIMESTAMP_LEN];
struct file_header file_hdr;
struct record_header record_hdr;
char comment[MAX_COMMENT_LEN];
unsigned int id_seq[NR_ACT];
extern unsigned int hdr_types_nr[];
extern unsigned int act_types_nr[];
extern unsigned int rec_types_nr[];
extern struct activity *act[];
extern __nr_t (*f_count[]) (struct activity *);
struct sigaction alrm_act, int_act;
int sigint_caught = 0;
/*
***************************************************************************
* Print usage and exit.
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void usage(char *progname)
{
fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ] [ <outfile> ]\n"),
progname);
fprintf(stderr, _("Options are:\n"
"[ -C <comment> ] [ -D ] [ -F ] [ -f ] [ -L ] [ -V ]\n"
"[ -S { INT | DISK | IPV6 | POWER | SNMP | XDISK | ALL | XALL } ]\n"));
exit(1);
}
/*
***************************************************************************
* Collect all activities belonging to a group.
*
* IN:
* @group_id Group identification number.
* @opt_f Optional flag to set.
***************************************************************************
*/
void collect_group_activities(unsigned int group_id, unsigned int opt_f)
{
int i;
for (i = 0; i < NR_ACT; i++) {
if (act[i]->group & group_id) {
act[i]->options |= AO_COLLECTED;
if (opt_f) {
act[i]->opt_flags |= opt_f;
}
}
}
}
/*
***************************************************************************
* Parse option -S, indicating which activities are to be collected.
*
* IN:
* @argv Arguments list.
* @opt Index in list of arguments.
***************************************************************************
*/
void parse_sadc_S_option(char *argv[], int opt)
{
char *p;
int i;
for (p = strtok(argv[opt], ","); p; p = strtok(NULL, ",")) {
if (!strcmp(p, K_INT)) {
/* Select group of interrupt activities */
collect_group_activities(G_INT, AO_F_NULL);
}
else if (!strcmp(p, K_DISK)) {
/* Select group of disk activities */
collect_group_activities(G_DISK, AO_F_NULL);
}
else if (!strcmp(p, K_XDISK)) {
/* Select group of disk and partition/filesystem activities */
collect_group_activities(G_DISK + G_XDISK, AO_F_DISK_PART);
}
else if (!strcmp(p, K_SNMP)) {
/* Select group of SNMP activities */
collect_group_activities(G_SNMP, AO_F_NULL);
}
else if (!strcmp(p, K_IPV6)) {
/* Select group of IPv6 activities */
collect_group_activities(G_IPV6, AO_F_NULL);
}
else if (!strcmp(p, K_POWER)) {
/* Select group of activities related to power management */
collect_group_activities(G_POWER, AO_F_NULL);
}
else if (!strcmp(p, K_ALL) || !strcmp(p, K_XALL)) {
/* Select all activities */
for (i = 0; i < NR_ACT; i++) {
if (!strcmp(p, K_ALL) && (act[i]->group & G_XDISK))
/*
* Don't select G_XDISK activities
* when option -S ALL is used.
*/
continue;
act[i]->options |= AO_COLLECTED;
}
if (!strcmp(p, K_XALL)) {
/* Tell sadc to also collect partition statistics */
collect_group_activities(G_DISK + G_XDISK, AO_F_DISK_PART);
}
}
else if (!strcmp(p, K_A_NULL)) {
/* Unselect all activities */
for (i = 0; i < NR_ACT; i++) {
act[i]->options &= ~AO_COLLECTED;
}
}
else if (!strncmp(p, "A_", 2)) {
/* Select activity by name */
for (i = 0; i < NR_ACT; i++) {
if (!strcmp(p, act[i]->name)) {
act[i]->options |= AO_COLLECTED;
break;
}
}
if (i == NR_ACT) {
usage(argv[0]);
}
}
else if (!strncmp(p, "-A_", 3)) {
/* Unselect activity by name */
for (i = 0; i < NR_ACT; i++) {
if (!strcmp(p + 1, act[i]->name)) {
act[i]->options &= ~AO_COLLECTED;
break;
}
}
if (i == NR_ACT) {
usage(argv[0]);
}
}
else {
usage(argv[0]);
}
}
}
/*
***************************************************************************
* SIGALRM signal handler. No need to reset handler here.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void alarm_handler(int sig)
{
__alarm(interval);
}
/*
***************************************************************************
* SIGINT signal handler.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void int_handler(int sig)
{
pid_t ppid = getppid();
sigint_caught = 1;
if (!optz || (ppid == 1)) {
/* sadc hasn't been called by sar or sar process is already dead */
exit(1);
}
/*
* When starting sar then pressing ctrl/c, SIGINT is received
* by sadc, not sar. So send SIGINT to sar so that average stats
* can be displayed.
*/
if (kill(ppid, SIGINT) < 0) {
exit(1);
}
}
/*
***************************************************************************
* Display an error message.
***************************************************************************
*/
void p_write_error(void)
{
fprintf(stderr, _("Cannot write data to system activity file: %s\n"),
strerror(errno));
exit(2);
}
/*
***************************************************************************
* Init structures. All of them are init'ed first when they are allocated
* (done by SREALLOC() macro in sa_sys_init() function).
* Then, they are init'ed again each time before reading the various system
* stats to make sure that no stats from a previous reading will remain.
* This is useful mainly for non sequential activities where some structures
* may remain unchanged. Such an activity is A_CPU, for which statistics
* for offline CPU won't be read and their corresponding stats structure
* won't be overwritten, giving the idea they are still online if we don't
* reset their structures to zero.
* Other activities may also assume that structure's fields are initialized
* when their stats are read.
***************************************************************************
*/
void reset_stats(void)
{
int i;
for (i = 0; i < NR_ACT; i++) {
if ((act[i]->_nr0 > 0) && act[i]->_buf0) {
memset(act[i]->_buf0, 0,
(size_t) act[i]->msize * (size_t) act[i]->nr_allocated * (size_t) act[i]->nr2);
}
}
}
/*
***************************************************************************
* Count activities items then allocate and init corresponding structures.
* Activities such as A_CPU with AO_ALWAYS_COUNTED flag set are always
* counted (thus the number of CPU will always be counted even if CPU
* activity is not collected), but ONLY those that will be collected have
* allocated structures.
* This function is called when sadc is started, and when a file is rotated.
* If a file is rotated and structures are reallocated with a larger size,
* additional space is not initialized: It doesn't matter as reset_stats()
* will do it later.
***************************************************************************
*/
void sa_sys_init(void)
{
int i, idx;
__nr_t f_count_results[NR_F_COUNT];
/* Init array. Means that no items have been counted yet */
for (i = 0; i < NR_F_COUNT; i++) {
f_count_results[i] = -1;
}
for (i = 0; i < NR_ACT; i++) {
if ((HAS_COUNT_FUNCTION(act[i]->options) && IS_COLLECTED(act[i]->options)) ||
ALWAYS_COUNT_ITEMS(act[i]->options)) {
idx = act[i]->f_count_index;
/* Number of items is not a constant and should be calculated */
if (f_count_results[idx] >= 0) {
act[i]->nr_ini = f_count_results[idx];
}
else {
act[i]->nr_ini = (f_count[idx])(act[i]);
f_count_results[idx] = act[i]->nr_ini;
}
}
if (act[i]->nr_ini > 0) {
if (act[i]->f_count2_index >= 0) {
idx = act[i]->f_count2_index;
if (f_count_results[idx] >= 0) {
act[i]->nr2 = f_count_results[idx];
}
else {
act[i]->nr2 = (f_count[idx])(act[i]);
f_count_results[idx] = act[i]->nr2;
}
}
/* else act[i]->nr2 is a constant and doesn't need to be calculated */
if (!act[i]->nr2) {
act[i]->nr_ini = 0;
}
}
if (IS_COLLECTED(act[i]->options) && (act[i]->nr_ini > 0)) {
/* Look for a possible overflow */
check_overflow((unsigned int) act[i]->msize,
(unsigned int) act[i]->nr_ini,
(unsigned int) act[i]->nr2);
/* Allocate structures for current activity (using nr_ini and nr2 results) */
SREALLOC(act[i]->_buf0, void,
(size_t) act[i]->msize * (size_t) act[i]->nr_ini * (size_t) act[i]->nr2);
act[i]->nr_allocated = act[i]->nr_ini;
}
if (act[i]->nr_ini <= 0) {
/* No items found: Invalidate current activity */
act[i]->options &= ~AO_COLLECTED;
}
if (HAS_DETECT_FUNCTION(act[i]->options) && IS_COLLECTED(act[i]->options)) {
idx = act[i]->f_count_index;
/* Detect if files needed by activity exist */
if (f_count_results[idx] < 0) {
f_count_results[idx] = (f_count[idx])(act[i]);
}
if (f_count_results[idx] == 0) {
/* Files not present */
act[i]->options &= ~AO_COLLECTED;
}
}
/* Set default activity list */
id_seq[i] = act[i]->id;
}
}
/*
***************************************************************************
* Free structures.
***************************************************************************
*/
void sa_sys_free(void)
{
int i;
for (i = 0; i < NR_ACT; i++) {
if (act[i]->nr_allocated > 0) {
if (act[i]->_buf0) {
free(act[i]->_buf0);
act[i]->_buf0 = NULL;
act[i]->nr_allocated = 0;
}
}
}
}
/*
***************************************************************************
* If -L option used, request a non-blocking, exclusive lock on the file.
* If lock would block, then another process (possibly sadc) has already
* opened that file => exit.
*
* IN:
* @fd Output file descriptor.
* @fatal Indicate if failing to lock file should be fatal or not.
* If it's not fatal then we'll wait for next iteration and
* try again.
*
* RETURNS:
* 0 on success, or 1 if file couldn't be locked.
***************************************************************************
*/
int ask_for_flock(int fd, int fatal)
{
/* Option -L may be used only if an outfile was specified on the command line */
if (LOCK_FILE(flags)) {
/*
* Yes: Try to lock file. To make code portable, check for both EWOULDBLOCK
* and EAGAIN return codes, and treat them the same (glibc documentation).
* Indeed, some Linux ports (e.g. hppa-linux) do not equate EWOULDBLOCK and
* EAGAIN like every other Linux port.
*/
if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
if ((((errno == EWOULDBLOCK) || (errno == EAGAIN)) && (fatal == FATAL)) ||
((errno != EWOULDBLOCK) && (errno != EAGAIN))) {
perror("flock");
exit(1);
}
/* Was unable to lock file: Lock would have blocked... */
return 1;
}
else {
/* File successfully locked */
flags |= S_F_FILE_LOCKED;
}
}
return 0;
}
/*
***************************************************************************
* Fill system activity file magic header.
*
* IN:
* @file_magic System activity file magic header.
***************************************************************************
*/
void fill_magic_header(struct file_magic *file_magic)
{
int i;
memset(file_magic, 0, FILE_MAGIC_SIZE);
file_magic->sysstat_magic = SYSSTAT_MAGIC;
file_magic->format_magic = FORMAT_MAGIC;
enum_version_nr(file_magic);
file_magic->header_size = FILE_HEADER_SIZE;
for (i = 0; i < 3; i++) {
file_magic->hdr_types_nr[i] = hdr_types_nr[i];
}
}
/*
***************************************************************************
* Fill system activity file header, then write it (or print it if stdout).
*
* IN:
* @fd Output file descriptor. May be stdout.
***************************************************************************
*/
void setup_file_hdr(int fd)
{
int i, j, p;
struct tm rectime;
struct utsname header;
struct file_magic file_magic;
struct file_activity file_act;
/* Fill then write file magic header */
fill_magic_header(&file_magic);
if (write_all(fd, &file_magic, FILE_MAGIC_SIZE) != FILE_MAGIC_SIZE) {
p_write_error();
}
/* First reset the structure */
memset(&file_hdr, 0, FILE_HEADER_SIZE);
/* Then get current date */
file_hdr.sa_ust_time = (unsigned long long) get_time(&rectime, 0);
/* OK, now fill the header */
file_hdr.sa_act_nr = get_activity_nr(act, AO_COLLECTED, COUNT_ACTIVITIES);
file_hdr.sa_day = rectime.tm_mday;
file_hdr.sa_month = rectime.tm_mon;
file_hdr.sa_year = rectime.tm_year;
file_hdr.sa_sizeof_long = sizeof(long);
file_hdr.sa_hz = HZ;
for (i = 0; i < 3; i++) {
file_hdr.act_types_nr[i] = act_types_nr[i];
file_hdr.rec_types_nr[i] = rec_types_nr[i];
}
file_hdr.act_size = FILE_ACTIVITY_SIZE;
file_hdr.rec_size = RECORD_HEADER_SIZE;
/*
* This is a new file (or stdout): Set sa_cpu_nr field to the number
* of CPU of the machine (1 .. CPU_NR + 1). This is the number of CPU, whether
* online or offline, when sadc was started.
* A_CPU activity is always counted in sa_sys_init(), even if it's not collected.
*/
file_hdr.sa_cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr_ini;
/* Get system name, release number, hostname and machine architecture */
__uname(&header);
strncpy(file_hdr.sa_sysname, header.sysname, sizeof(file_hdr.sa_sysname));
file_hdr.sa_sysname[sizeof(file_hdr.sa_sysname) - 1] = '\0';
strncpy(file_hdr.sa_nodename, header.nodename, sizeof(file_hdr.sa_nodename));
file_hdr.sa_nodename[sizeof(file_hdr.sa_nodename) - 1] = '\0';
strncpy(file_hdr.sa_release, header.release, sizeof(file_hdr.sa_release));
file_hdr.sa_release[sizeof(file_hdr.sa_release) - 1] = '\0';
strncpy(file_hdr.sa_machine, header.machine, sizeof(file_hdr.sa_machine));
file_hdr.sa_machine[sizeof(file_hdr.sa_machine) - 1] = '\0';
/* Get timezone value and save it */
tzset();
strncpy(file_hdr.sa_tzname, tzname[0], TZNAME_LEN);
file_hdr.sa_tzname[TZNAME_LEN - 1] = '\0';
/* Write file header */
if (write_all(fd, &file_hdr, FILE_HEADER_SIZE) != FILE_HEADER_SIZE) {
p_write_error();
}
/* Reset file_activity structure (in case some unknown extra fields exist) */
memset(&file_act, 0, FILE_ACTIVITY_SIZE);
/* Write activity list */
for (i = 0; i < NR_ACT; i++) {
/*
* Activity sequence given by id_seq array.
* Sequence must be the same for stdout as for output file.
*/
if (!id_seq[i])
continue;
if ((p = get_activity_position(act, id_seq[i], RESUME_IF_NOT_FOUND)) < 0)
continue;
if (IS_COLLECTED(act[p]->options)) {
file_act.id = act[p]->id;
file_act.magic = act[p]->magic;
file_act.nr = act[p]->nr_ini;
file_act.nr2 = act[p]->nr2;
file_act.size = act[p]->fsize;
for (j = 0; j < 3; j++) {
file_act.types_nr[j] = act[p]->gtypes_nr[j];
}
file_act.has_nr = HAS_COUNT_FUNCTION(act[p]->options);
if (write_all(fd, &file_act, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) {
p_write_error();
}
}
}
return;
}
/*
***************************************************************************
* Write the new number of CPU after the RESTART record in file.
*
* IN:
* @ofd Output file descriptor.
***************************************************************************
*/
void write_new_cpu_nr(int ofd)
{
int p;
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
if (write_all(ofd, &(act[p]->nr_ini), sizeof(__nr_t)) != sizeof(__nr_t)) {
p_write_error();
}
}
/*
***************************************************************************
* sadc called with interval and count parameters not set:
* Write a dummy record notifying a system restart, or insert a comment in
* binary data file if option -C has been used.
* Writing a dummy record should typically be done at boot time,
* before the cron daemon is started to avoid conflict with sa1/sa2 scripts.
*
* IN:
* @ofd Output file descriptor.
* @rtype Record type to write (restart or comment).
***************************************************************************
*/
void write_special_record(int ofd, int rtype)
{
struct tm rectime = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
/* Check if file is locked */
if (!FILE_LOCKED(flags)) {
ask_for_flock(ofd, FATAL);
}
/* Reset the structure (sane to do it, as other fields may be added in the future) */
memset(&record_hdr, 0, RECORD_HEADER_SIZE);
/* Set record type */
record_hdr.record_type = rtype;
/* Save time */
record_hdr.ust_time = (unsigned long long) get_time(&rectime, 0);
record_hdr.hour = rectime.tm_hour;
record_hdr.minute = rectime.tm_min;
record_hdr.second = rectime.tm_sec;
/* Write record now */
if (write_all(ofd, &record_hdr, RECORD_HEADER_SIZE) != RECORD_HEADER_SIZE) {
p_write_error();
}
if (rtype == R_RESTART) {
/* Also write the new number of CPU */
write_new_cpu_nr(ofd);
}
else if (rtype == R_COMMENT) {
/* Also write the comment */
if (write_all(ofd, comment, MAX_COMMENT_LEN) != MAX_COMMENT_LEN) {
p_write_error();
}
}
}
/*
***************************************************************************
* Write stats (or print them if stdout).
*
* IN:
* @ofd Output file descriptor. May be stdout.
***************************************************************************
*/
void write_stats(int ofd)
{
int i, p;
/* Try to lock file */
if (!FILE_LOCKED(flags)) {
if (ask_for_flock(ofd, NON_FATAL))
/*
* Unable to lock file:
* Wait for next iteration to try again to save data.
*/
return;
}
/* Write record header */
if (write_all(ofd, &record_hdr, RECORD_HEADER_SIZE) != RECORD_HEADER_SIZE) {
p_write_error();
}
/* Then write all statistics */
for (i = 0; i < NR_ACT; i++) {
if (!id_seq[i])
continue;
if ((p = get_activity_position(act, id_seq[i], RESUME_IF_NOT_FOUND)) < 0)
continue;
if (IS_COLLECTED(act[p]->options)) {
if (HAS_COUNT_FUNCTION(act[p]->options) && (act[p]->f_count_index >= 0)) {
if (write_all(ofd, &(act[p]->_nr0), sizeof(__nr_t)) != sizeof(__nr_t)) {
p_write_error();
}
}
if (write_all(ofd, act[p]->_buf0, act[p]->fsize * act[p]->_nr0 * act[p]->nr2) !=
(act[p]->fsize * act[p]->_nr0 * act[p]->nr2)) {
p_write_error();
}
}
}
}
/*
***************************************************************************
* Create a system activity daily data file.
*
* IN:
* @ofile Name of output file.
*
* OUT:
* @ofd Output file descriptor.
***************************************************************************
*/
void create_sa_file(int *ofd, char *ofile)
{
if ((*ofd = open(ofile, O_CREAT | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
goto create_error;
/* Try to lock file */
ask_for_flock(*ofd, FATAL);
/* Truncate file */
if (ftruncate(*ofd, 0) >= 0) {
/* Write file header */
setup_file_hdr(*ofd);
return;
}
create_error:
fprintf(stderr, _("Cannot open %s: %s\n"), ofile, strerror(errno));
exit(2);
}
/*
***************************************************************************
* Get descriptor for stdout.
*
* IN:
* @stdfd A value >= 0 indicates that stats data should also
* be written to stdout.
*
* OUT:
* @stdfd Stdout file descriptor.
***************************************************************************
*/
void open_stdout(int *stdfd)
{
if (*stdfd >= 0) {
if ((*stdfd = dup(STDOUT_FILENO)) < 0) {
perror("dup");
exit(4);
}
/* Write file header on STDOUT */
setup_file_hdr(*stdfd);
}
}
/*
***************************************************************************
* Get descriptor for output file and write its header.
* We may enter this function several times (when we rotate a file).
* NB: If data are appended to an existing file then the format must be
* strictly that expected by current version.
*
* IN:
* @ofile Name of output file.
* @restart_mark TRUE if sadc called with interval (and count) not
* set, and no comment given (so we are going to insert
* a restart mark into the file).
*
* OUT:
* @ofd Output file descriptor.
***************************************************************************
*/
void open_ofile(int *ofd, char ofile[], int restart_mark)
{
struct file_magic file_magic;
struct file_activity file_act[NR_ACT];
struct tm rectime = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
ssize_t sz;
int i, p;
if (!ofile[0])
return;
/* Try to open file and check that data can be appended to it */
if ((*ofd = open(ofile, O_APPEND | O_RDWR)) < 0) {
if (errno == ENOENT) {
/* File doesn't exist: Create it */
create_sa_file(ofd, ofile);
return;
}
fprintf(stderr, _("Cannot open %s: %s\n"), ofile, strerror(errno));
exit(2);
}
/* Read file magic header */
sz = read(*ofd, &file_magic, FILE_MAGIC_SIZE);
if (!sz) {
close(*ofd);
/* This is an empty file: Create it again */
create_sa_file(ofd, ofile);
return;
}
/* Test various values ("strict writing" rule) */
if ((sz != FILE_MAGIC_SIZE) ||
(file_magic.sysstat_magic != SYSSTAT_MAGIC) ||
(file_magic.format_magic != FORMAT_MAGIC) ||
(file_magic.header_size != FILE_HEADER_SIZE) ||
(file_magic.hdr_types_nr[0] != FILE_HEADER_ULL_NR) ||
(file_magic.hdr_types_nr[1] != FILE_HEADER_UL_NR) ||
(file_magic.hdr_types_nr[2] != FILE_HEADER_U_NR)) {
if (FORCE_FILE(flags)) {
close(*ofd);
/* -F option used: Truncate file */
create_sa_file(ofd, ofile);
return;
}
#ifdef DEBUG
fprintf(stderr, "%s: Size read=%zd sysstat_magic=%x format_magic=%x header_size=%u header=%u,%u,%u\n",
__FUNCTION__, sz, file_magic.sysstat_magic, file_magic.format_magic, file_magic.header_size,
file_magic.hdr_types_nr[0], file_magic.hdr_types_nr[1], file_magic.hdr_types_nr[2]);
#endif
/* Display error message and exit */
handle_invalid_sa_file(*ofd, &file_magic, ofile, sz);
}
/* Read file standard header */
if ((sz = read(*ofd, &file_hdr, FILE_HEADER_SIZE)) != FILE_HEADER_SIZE) {
#ifdef DEBUG
fprintf(stderr, "%s: Size read=%zd\n",
__FUNCTION__, sz);
#endif
goto append_error;
}
/*
* If we are using the standard daily data file (file specified
* as "-" on the command line) and it is from a past month,
* then overwrite (truncate) it.
*/
get_time(&rectime, 0);
if (((file_hdr.sa_month != rectime.tm_mon) ||
(file_hdr.sa_year != rectime.tm_year)) &&
WANT_SA_ROTAT(flags)) {
close(*ofd);
create_sa_file(ofd, ofile);
return;
}
/* OK: It's a true system activity file */
if (!file_hdr.sa_act_nr || (file_hdr.sa_act_nr > NR_ACT)) {
#ifdef DEBUG
fprintf(stderr, "%s: sa_act_nr=%u\n",
__FUNCTION__, file_hdr.sa_act_nr);
#endif
/*
* No activities at all or at least one unknown activity:
* Cannot append data to such a file.
*/
goto append_error;
}
/* Other sanity checks ("strict writing" rule) */
if ((file_hdr.act_size != FILE_ACTIVITY_SIZE) ||
(file_hdr.act_types_nr[0] != FILE_ACTIVITY_ULL_NR) ||
(file_hdr.act_types_nr[1] != FILE_ACTIVITY_UL_NR) ||
(file_hdr.act_types_nr[2] != FILE_ACTIVITY_U_NR) ||
(file_hdr.rec_size != RECORD_HEADER_SIZE) ||
(file_hdr.rec_types_nr[0] != RECORD_HEADER_ULL_NR) ||
(file_hdr.rec_types_nr[1] != RECORD_HEADER_UL_NR) ||
(file_hdr.rec_types_nr[2] != RECORD_HEADER_U_NR)) {
#ifdef DEBUG
fprintf(stderr, "%s: act_size=%u act=%u,%u,%u rec_size=%u rec=%u,%u,%u\n",
__FUNCTION__, file_hdr.act_size,
file_hdr.act_types_nr[0], file_hdr.act_types_nr[1], file_hdr.act_types_nr[2],
file_hdr.rec_size,
file_hdr.rec_types_nr[0], file_hdr.rec_types_nr[1], file_hdr.rec_types_nr[2]);
#endif
goto append_error;
}
for (i = 0; i < file_hdr.sa_act_nr; i++) {
/* Read current activity in list */
if (read(*ofd, &file_act[i], FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) {
#ifdef DEBUG
fprintf(stderr, "%s: Wrong size for file_activity\n",
__FUNCTION__);
#endif
handle_invalid_sa_file(*ofd, &file_magic, ofile, 0);
}
p = get_activity_position(act, file_act[i].id, RESUME_IF_NOT_FOUND);
if ((p < 0) || (act[p]->fsize != file_act[i].size) ||
(act[p]->magic != file_act[i].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\n",
__FUNCTION__, act[p]->name, act[p]->fsize, file_act[i].size,
act[p]->magic, file_act[i].magic);
}
#endif
/*
* Unknown activity in list or item size has changed or
* unknown activity format: Cannot append data to such a file
* ("strict writing" rule).
*/
goto append_error;
}
if ((file_act[i].nr <= 0) || (file_act[i].nr2 <= 0) ||
(file_act[i].nr > act[p]->nr_max) ||
(file_act[i].nr2 > NR2_MAX)) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: nr=%d nr_max=%d nr2=%d\n",
__FUNCTION__, act[p]->name, file_act[i].nr,
act[p]->nr_max, file_act[i].nr2);
#endif
/*
* Number of items and subitems should never be zero (or negative)
* or greater than their upper limit.
*/
goto append_error;
}
if ((file_act[i].types_nr[0] != act[p]->gtypes_nr[0]) ||
(file_act[i].types_nr[1] != act[p]->gtypes_nr[1]) ||
(file_act[i].types_nr[2] != act[p]->gtypes_nr[2])) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: types=%u,%u,%u/%u,%u,%u\n",
__FUNCTION__, act[p]->name,
file_act[i].types_nr[0], file_act[i].types_nr[1], file_act[i].types_nr[2],
act[p]->gtypes_nr[0], act[p]->gtypes_nr[1], act[p]->gtypes_nr[2]);
#endif
/*
* Composition of structure containing statsitics cannot
* be different from that known by current version.
*/
goto append_error;
}
if ((file_act[i].has_nr && (act[p]->f_count_index < 0)) ||
(!file_act[i].has_nr && (act[p]->f_count_index >= 0) && HAS_COUNT_FUNCTION(act[p]->options))) {
#ifdef DEBUG
fprintf(stderr, "%s: %s: has_nr=%d count_index=%d\n",
__FUNCTION__, act[p]->name, file_act[i].has_nr, act[p]->f_count_index);
#endif
/*
* For every activity whose number of items is not a constant,
* a value giving the number of structures to read should exist.
*/
goto append_error;
}
}
/*
* OK: (Almost) all tests successfully passed.
* List of activities from the file prevails over that of the user.
* So unselect all of them. And reset activity sequence.
*/
for (i = 0; i < NR_ACT; i++) {
act[i]->options &= ~AO_COLLECTED;
id_seq[i] = 0;
}
for (i = 0; i < file_hdr.sa_act_nr; i++) {
p = get_activity_position(act, file_act[i].id, EXIT_IF_NOT_FOUND);
/*
* sar doesn't expect a number of items equal to 0.
* Yet @nr_ini may be 0 if no items have been found on current machine.
* Since we are appending data to a file, set @nr_ini to the value of the file.
* Stats saved in file will all be 0 for that activity if no items exist on
* the machine.
* NB: We must preserve the value read for A_CPU when a LINUX RESTART is inserted.