2 * sar: report system activity
3 * (C) 1999-2015 by Sebastien GODARD (sysstat <at> orange.fr)
5 ***************************************************************************
6 * This program is free software; you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation; either version 2 of the License, or (at your *
9 * option) any later version. *
11 * This program is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * You should have received a copy of the GNU General Public License along *
17 * with this program; if not, write to the Free Software Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************
40 #define _(string) gettext(string)
42 #define _(string) (string)
45 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
46 char *sccsid(void) { return (SCCSID); }
48 /* Interval and count parameters */
49 long interval = -1, count = 0;
51 /* TRUE if a header line must be printed */
54 unsigned int flags = 0;
55 unsigned int dm_major; /* Device-mapper major number */
57 char timestamp[2][TIMESTAMP_LEN];
59 unsigned long avg_count = 0;
62 struct file_header file_hdr;
64 /* Current record header */
65 struct record_header record_hdr[3];
69 * This array must always be entirely filled (even with trailing zeros).
71 unsigned int id_seq[NR_ACT];
75 /* Contain the date specified by -s and -e options */
76 struct tstamp tm_start, tm_end;
78 char *args[MAX_ARGV_NR];
80 extern struct activity *act[];
81 extern struct report_format sar_fmt;
83 struct sigaction int_act;
84 int sigint_caught = 0;
87 ***************************************************************************
88 * Print usage title message.
91 * @progname Name of sysstat command
92 ***************************************************************************
94 void print_usage_title(FILE *fp, char *progname)
96 fprintf(fp, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
101 ***************************************************************************
102 * Print usage and exit.
105 * @progname Name of sysstat command
106 ***************************************************************************
108 void usage(char *progname)
110 print_usage_title(stderr, progname);
111 fprintf(stderr, _("Options are:\n"
112 "[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ] [ -h ]\n"
113 "[ -p ] [ -q ] [ -R ] [ -r [ ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ]\n"
114 "[ -v ] [ -W ] [ -w ] [ -y ] [ --sadc ]\n"
115 "[ -I { <int> [,...] | SUM | ALL | XALL } ] [ -P { <cpu> [,...] | ALL } ]\n"
116 "[ -m { <keyword> [,...] | ALL } ] [ -n { <keyword> [,...] | ALL } ]\n"
117 "[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
118 "[ -f [ <filename> ] | -o [ <filename> ] | -[0-9]+ ]\n"
119 "[ -i <interval> ] [ -s [ <hh:mm[:ss]> ] ] [ -e [ <hh:mm[:ss]> ] ]\n"));
124 ***************************************************************************
125 * Display a short help message and exit.
128 * @progname Name of sysstat command
129 ***************************************************************************
131 void display_help(char *progname)
133 print_usage_title(stdout, progname);
134 printf(_("Main options and reports:\n"));
135 printf(_("\t-B\tPaging statistics\n"));
136 printf(_("\t-b\tI/O and transfer rate statistics\n"));
137 printf(_("\t-d\tBlock devices statistics\n"));
138 printf(_("\t-F [ MOUNT ]\n"));
139 printf(_("\t\tFilesystems statistics\n"));
140 printf(_("\t-H\tHugepages utilization statistics\n"));
141 printf(_("\t-I { <int> | SUM | ALL | XALL }\n"
142 "\t\tInterrupts statistics\n"));
143 printf(_("\t-m { <keyword> [,...] | ALL }\n"
144 "\t\tPower management statistics\n"
145 "\t\tKeywords are:\n"
146 "\t\tCPU\tCPU instantaneous clock frequency\n"
147 "\t\tFAN\tFans speed\n"
148 "\t\tFREQ\tCPU average clock frequency\n"
149 "\t\tIN\tVoltage inputs\n"
150 "\t\tTEMP\tDevices temperature\n"
151 "\t\tUSB\tUSB devices plugged into the system\n"));
152 printf(_("\t-n { <keyword> [,...] | ALL }\n"
153 "\t\tNetwork statistics\n"
154 "\t\tKeywords are:\n"
155 "\t\tDEV\tNetwork interfaces\n"
156 "\t\tEDEV\tNetwork interfaces (errors)\n"
157 "\t\tNFS\tNFS client\n"
158 "\t\tNFSD\tNFS server\n"
159 "\t\tSOCK\tSockets\t(v4)\n"
160 "\t\tIP\tIP traffic\t(v4)\n"
161 "\t\tEIP\tIP traffic\t(v4) (errors)\n"
162 "\t\tICMP\tICMP traffic\t(v4)\n"
163 "\t\tEICMP\tICMP traffic\t(v4) (errors)\n"
164 "\t\tTCP\tTCP traffic\t(v4)\n"
165 "\t\tETCP\tTCP traffic\t(v4) (errors)\n"
166 "\t\tUDP\tUDP traffic\t(v4)\n"
167 "\t\tSOCK6\tSockets\t(v6)\n"
168 "\t\tIP6\tIP traffic\t(v6)\n"
169 "\t\tEIP6\tIP traffic\t(v6) (errors)\n"
170 "\t\tICMP6\tICMP traffic\t(v6)\n"
171 "\t\tEICMP6\tICMP traffic\t(v6) (errors)\n"
172 "\t\tUDP6\tUDP traffic\t(v6)\n"
173 "\t\tFC\tFibre channel HBAs\n"));
174 printf(_("\t-q\tQueue length and load average statistics\n"));
175 printf(_("\t-R\tMemory statistics\n"));
176 printf(_("\t-r [ ALL ]\n"
177 "\t\tMemory utilization statistics\n"));
178 printf(_("\t-S\tSwap space utilization statistics\n"));
179 printf(_("\t-u [ ALL ]\n"
180 "\t\tCPU utilization statistics\n"));
181 printf(_("\t-v\tKernel tables statistics\n"));
182 printf(_("\t-W\tSwapping statistics\n"));
183 printf(_("\t-w\tTask creation and system switching statistics\n"));
184 printf(_("\t-y\tTTY devices statistics\n"));
189 ***************************************************************************
190 * Give a hint to the user about where is located the data collector.
191 ***************************************************************************
193 void which_sadc(void)
197 if (stat(SADC_PATH, &buf) < 0) {
198 printf(_("Data collector will be sought in PATH\n"));
201 printf(_("Data collector found: %s\n"), SADC_PATH);
207 ***************************************************************************
208 * SIGINT signal handler.
211 * @sig Signal number.
212 ***************************************************************************
214 void int_handler(int sig)
217 printf("\n"); /* Skip "^C" displayed on screen */
222 ***************************************************************************
223 * Init some structures.
224 ***************************************************************************
226 void init_structures(void)
230 for (i = 0; i < 3; i++)
231 memset(&record_hdr[i], 0, RECORD_HEADER_SIZE);
235 ***************************************************************************
236 * Allocate memory for sadc args.
239 * @i Argument number.
240 * @ltemp Argument value.
241 ***************************************************************************
243 void salloc(int i, char *ltemp)
245 if ((args[i] = (char *) malloc(strlen(ltemp) + 1)) == NULL) {
249 strcpy(args[i], ltemp);
253 ***************************************************************************
254 * Display an error message. Happens when the data collector doesn't send
256 ***************************************************************************
258 void print_read_error(void)
260 fprintf(stderr, _("End of data collecting unexpected\n"));
265 ***************************************************************************
266 * Check that every selected activity actually belongs to the sequence list.
267 * If not, then the activity should be unselected since it will not be sent
268 * by sadc. An activity can be not sent if its number of items is null.
271 * @act_nr Size of sequence list.
272 ***************************************************************************
274 void reverse_check_act(unsigned int act_nr)
278 for (i = 0; i < NR_ACT; i++) {
280 if (IS_SELECTED(act[i]->options)) {
282 for (j = 0; j < act_nr; j++) {
283 if (id_seq[j] == act[i]->id)
287 act[i]->options &= ~AO_SELECTED;
293 ***************************************************************************
294 * Determine if a stat header line has to be displayed.
297 * TRUE if a header line has to be displayed.
298 ***************************************************************************
300 int check_line_hdr(void)
304 /* Get number of options entered on the command line */
305 if (get_activity_nr(act, AO_SELECTED, COUNT_OUTPUTS) > 1)
308 for (i = 0; i < NR_ACT; i++) {
309 if (IS_SELECTED(act[i]->options)) {
310 /* Special processing for activities using a bitmap */
311 if (act[i]->bitmap) {
312 if (count_bits(act[i]->bitmap->b_array,
313 BITMAP_SIZE(act[i]->bitmap->b_size)) > 1) {
317 else if (act[i]->nr > 1) {
320 /* Stop now since we have only one selected activity */
329 ***************************************************************************
330 * Print statistics average.
333 * @curr Index in array for current sample statistics.
334 * @read_from_file Set to TRUE if stats are read from a system activity
336 * @act_id Activity that can be displayed, or ~0 for all.
337 * Remember that when reading stats from a file, only
338 * one activity can be displayed at a time.
339 ***************************************************************************
341 void write_stats_avg(int curr, int read_from_file, unsigned int act_id)
344 unsigned long long itv, g_itv;
345 static __nr_t cpu_nr = -1;
348 cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
350 /* Interval value in jiffies */
351 g_itv = get_interval(record_hdr[2].uptime, record_hdr[curr].uptime);
354 itv = get_interval(record_hdr[2].uptime0, record_hdr[curr].uptime0);
358 strncpy(timestamp[curr], _("Average:"), TIMESTAMP_LEN);
359 timestamp[curr][TIMESTAMP_LEN - 1] = '\0';
360 strcpy(timestamp[!curr], timestamp[curr]);
363 TEST_STDOUT(STDOUT_FILENO);
365 for (i = 0; i < NR_ACT; i++) {
367 if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
370 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
371 /* Display current average activity statistics */
372 (*act[i]->f_print_avg)(act[i], 2, curr,
373 NEED_GLOBAL_ITV(act[i]->options) ? g_itv : itv);
377 if (read_from_file) {
379 * Reset number of lines printed only if we read stats
380 * from a system activity file.
387 ***************************************************************************
388 * Print system statistics.
391 * @curr Index in array for current sample statistics.
392 * @read_from_file Set to TRUE if stats are read from a system activity
394 * @use_tm_start Set to TRUE if option -s has been used.
395 * @use_tm_end Set to TRUE if option -e has been used.
396 * @reset Set to TRUE if last_uptime variable should be
397 * reinitialized (used in next_slice() function).
398 * @act_id Activity that can be displayed or ~0 for all.
399 * Remember that when reading stats from a file, only
400 * one activity can be displayed at a time.
401 * @reset_cd TRUE if static cross_day variable should be reset
405 * @cnt Number of remaining lines to display.
408 * 1 if stats have been successfully displayed, and 0 otherwise.
409 ***************************************************************************
411 int write_stats(int curr, int read_from_file, long *cnt, int use_tm_start,
412 int use_tm_end, int reset, unsigned int act_id, int reset_cd)
415 unsigned long long itv, g_itv;
416 static int cross_day = 0;
417 static __nr_t cpu_nr = -1;
420 cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
424 * cross_day is a static variable that is set to 1 when the first
425 * record of stats from a new day is read from a unique data file
426 * (in the case where the file contains data from two consecutive
427 * days). When set to 1, every following records timestamp will
428 * have its hour value increased by 24.
429 * Yet when a new activity (being read from the file) is going to
430 * be displayed, we start reading the file from the beginning
431 * again, and so cross_day should be reset in this case.
437 if (read_from_file) {
438 if (!next_slice(record_hdr[2].uptime0, record_hdr[curr].uptime0,
440 /* Not close enough to desired interval */
444 /* Get then set previous timestamp */
445 if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[!curr],
448 set_record_timestamp_string(S_F_PREFD_TIME_OUTPUT, &record_hdr[!curr],
449 NULL, timestamp[!curr], 16, &rectime);
451 /* Get then set current timestamp */
452 if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
455 set_record_timestamp_string(S_F_PREFD_TIME_OUTPUT, &record_hdr[curr],
456 NULL, timestamp[curr], 16, &rectime);
458 /* Check if we are beginning a new day */
459 if (use_tm_start && record_hdr[!curr].ust_time &&
460 (record_hdr[curr].ust_time > record_hdr[!curr].ust_time) &&
461 (record_hdr[curr].hour < record_hdr[!curr].hour)) {
467 * This is necessary if we want to properly handle something like:
468 * sar -s time_start -e time_end with
469 * time_start(day D) > time_end(day D+1)
471 rectime.tm_hour +=24;
475 if (use_tm_start && (datecmp(&rectime, &tm_start) < 0))
476 /* it's too soon... */
479 /* Get interval values */
480 get_itv_value(&record_hdr[curr], &record_hdr[!curr],
481 cpu_nr, &itv, &g_itv);
484 if (use_tm_end && (datecmp(&rectime, &tm_end) > 0)) {
485 /* It's too late... */
493 TEST_STDOUT(STDOUT_FILENO);
495 for (i = 0; i < NR_ACT; i++) {
497 if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
500 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
501 /* Display current activity statistics */
502 (*act[i]->f_print)(act[i], !curr, curr,
503 NEED_GLOBAL_ITV(act[i]->options) ? g_itv : itv);
511 ***************************************************************************
512 * Display stats since system startup.
515 * @curr Index in array for current sample statistics.
516 ***************************************************************************
518 void write_stats_startup(int curr)
522 /* Set to 0 previous structures corresponding to boot time */
523 memset(&record_hdr[!curr], 0, RECORD_HEADER_SIZE);
524 record_hdr[!curr].record_type = R_STATS;
525 record_hdr[!curr].hour = record_hdr[curr].hour;
526 record_hdr[!curr].minute = record_hdr[curr].minute;
527 record_hdr[!curr].second = record_hdr[curr].second;
528 record_hdr[!curr].ust_time = record_hdr[curr].ust_time;
530 for (i = 0; i < NR_ACT; i++) {
531 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
532 memset(act[i]->buf[!curr], 0,
533 (size_t) act[i]->msize * (size_t) act[i]->nr * (size_t) act[i]->nr2);
537 flags |= S_F_SINCE_BOOT;
540 write_stats(curr, USE_SADC, &count, NO_TM_START, NO_TM_END, NO_RESET,
541 ALL_ACTIVITIES, TRUE);
547 ***************************************************************************
548 * Read data sent by the data collector.
551 * @size Number of bytes of data to read.
554 * @buffer Buffer where data will be saved.
557 * 1 if end of file has been reached, 0 otherwise.
558 ***************************************************************************
560 int sa_read(void *buffer, int size)
566 if ((n = read(STDIN_FILENO, buffer, size)) < 0) {
575 buffer = (char *) buffer + n;
582 ***************************************************************************
583 * Display a restart message (contents of a R_RESTART record).
586 * @tab Number of tabulations (unused here).
587 * @action Action expected from current function (unused here).
588 * @cur_date Date string of current restart message (unused here).
589 * @cur_time Time string of current restart message.
590 * @utc True if @cur_time is expressed in UTC (unused here).
591 * @file_hdr System activity file standard header (unused here).
592 * @cpu_nr CPU count associated with restart mark.
593 ***************************************************************************
595 __print_funct_t print_sar_restart(int *tab, int action, char *cur_date, char *cur_time, int utc,
596 struct file_header *file_hdr, unsigned int cpu_nr)
600 printf("\n%-11s", cur_time);
601 sprintf(restart, " LINUX RESTART\t(%d CPU)\n",
602 cpu_nr > 1 ? cpu_nr - 1 : 1);
603 cprintf_s(IS_RESTART, "%s", restart);
608 ***************************************************************************
609 * Display a comment (contents of R_COMMENT record).
612 * @tab Number of tabulations (unused here).
613 * @action Action expected from current function (unused here).
614 * @cur_date Date string of current comment (unused here).
615 * @cur_time Time string of current comment.
616 * @utc True if @cur_time is expressed in UTC (unused here).
617 * @comment Comment to display.
618 * @file_hdr System activity file standard header (unused here).
619 ***************************************************************************
621 __print_funct_t print_sar_comment(int *tab, int action, char *cur_date, char *cur_time, int utc,
622 char *comment, struct file_header *file_hdr)
624 printf("%-11s", cur_time);
625 cprintf_s(IS_COMMENT, " COM %s\n", comment);
629 ***************************************************************************
630 * Read the various statistics sent by the data collector (sadc).
633 * @curr Index in array for current sample statistics.
634 ***************************************************************************
636 void read_sadc_stat_bunch(int curr)
640 /* Read record header (type is always R_STATS since it is read from sadc) */
641 if (sa_read(&record_hdr[curr], RECORD_HEADER_SIZE)) {
643 * SIGINT (sent by sadc) is likely to be received
644 * while we are stuck in sa_read().
645 * If this happens then no data have to be read.
653 for (i = 0; i < NR_ACT; i++) {
657 p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
659 if (sa_read(act[p]->buf[curr], act[p]->fsize * act[p]->nr * act[p]->nr2)) {
666 ***************************************************************************
667 * Read stats for current activity from file and display them.
670 * @ifd Input file descriptor.
671 * @fpos Position in file where reading must start.
672 * @curr Index in array for current sample statistics.
673 * @rows Number of rows of screen.
674 * @act_id Activity to display.
675 * @file_actlst List of activities in file.
676 * @file Name of file being read.
677 * @file_magic file_magic structure filled with file magic header data.
680 * @curr Index in array for next sample statistics.
681 * @cnt Number of remaining lines of stats to write.
682 * @eosaf Set to TRUE if EOF (end of file) has been reached.
683 * @reset Set to TRUE if last_uptime variable should be
684 * reinitialized (used in next_slice() function).
685 ***************************************************************************
687 void handle_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
688 int rows, unsigned int act_id, int *reset,
689 struct file_activity *file_actlst, char *file,
690 struct file_magic *file_magic)
693 unsigned long lines = 0;
695 int davg = 0, next, inc;
697 if (lseek(ifd, fpos, SEEK_SET) < fpos) {
703 * Restore the first stats collected.
704 * Used to compute the rate displayed on the first line.
706 copy_structures(act, id_seq, record_hdr, !*curr, 2);
710 /* Assess number of lines printed */
711 p = get_activity_position(act, act_id, EXIT_IF_NOT_FOUND);
712 if (act[p]->bitmap) {
713 inc = count_bits(act[p]->bitmap->b_array,
714 BITMAP_SIZE(act[p]->bitmap->b_size));
723 /* Display count lines of stats */
724 *eosaf = sa_fread(ifd, &record_hdr[*curr],
725 RECORD_HEADER_SIZE, SOFT_SIZE);
726 rtype = record_hdr[*curr].record_type;
728 if (!*eosaf && (rtype != R_RESTART) && (rtype != R_COMMENT)) {
729 /* Read the extra fields since it's not a special record */
730 read_file_stat_bunch(act, *curr, ifd, file_hdr.sa_act_nr, file_actlst);
733 if ((lines >= rows) || !lines) {
740 if (!*eosaf && (rtype != R_RESTART)) {
742 if (rtype == R_COMMENT) {
743 /* Display comment */
744 next = print_special_record(&record_hdr[*curr], flags + S_F_LOCAL_TIME,
745 &tm_start, &tm_end, R_COMMENT, ifd,
746 &rectime, NULL, file, 0,
747 file_magic, &file_hdr, act, &sar_fmt);
749 /* A line of comment was actually displayed */
755 /* next is set to 1 when we were close enough to desired interval */
756 next = write_stats(*curr, USE_SA_FILE, cnt, tm_start.use, tm_end.use,
757 *reset, act_id, reset_cd);
759 if (next && (*cnt > 0)) {
770 while (*cnt && !*eosaf && (rtype != R_RESTART));
773 write_stats_avg(!*curr, USE_SA_FILE, act_id);
780 ***************************************************************************
781 * Read header data sent by sadc.
782 ***************************************************************************
784 void read_header_data(void)
786 struct file_magic file_magic;
787 struct file_activity file_act;
791 /* Read magic header */
792 rc = sa_read(&file_magic, FILE_MAGIC_SIZE);
794 sprintf(version, "%d.%d.%d.%d",
795 file_magic.sysstat_version,
796 file_magic.sysstat_patchlevel,
797 file_magic.sysstat_sublevel,
798 file_magic.sysstat_extraversion);
799 if (!file_magic.sysstat_extraversion) {
800 version[strlen(version) - 2] = '\0';
803 if (rc || (file_magic.sysstat_magic != SYSSTAT_MAGIC) ||
804 (file_magic.format_magic != FORMAT_MAGIC) ||
805 strcmp(version, VERSION)) {
807 /* sar and sadc commands are not consistent */
808 if (!rc && (file_magic.sysstat_magic == SYSSTAT_MAGIC)) {
810 _("Using a wrong data collector from a different sysstat version\n"));
818 * No need to take into account file_magic.header_size. We are sure that
819 * sadc and sar are from the same version (we have checked FORMAT_MAGIC
820 * but also VERSION above) and thus the size of file_header is FILE_HEADER_SIZE.
822 if (sa_read(&file_hdr, FILE_HEADER_SIZE)) {
826 if (file_hdr.sa_act_nr > NR_ACT)
829 /* Read activity list */
830 for (i = 0; i < file_hdr.sa_act_nr; i++) {
832 if (sa_read(&file_act, FILE_ACTIVITY_SIZE)) {
836 p = get_activity_position(act, file_act.id, RESUME_IF_NOT_FOUND);
838 if ((p < 0) || (act[p]->fsize != file_act.size)
839 || (file_act.nr <= 0)
840 || (file_act.nr2 <= 0)
841 || (act[p]->magic != file_act.magic))
842 /* Remember that we are reading data from sadc and not from a file... */
845 id_seq[i] = file_act.id; /* We necessarily have "i < NR_ACT" */
846 act[p]->nr = file_act.nr;
847 act[p]->nr2 = file_act.nr2;
854 /* Check that all selected activties are actually sent by sadc */
855 reverse_check_act(file_hdr.sa_act_nr);
861 /* Strange data sent by sadc...! */
862 fprintf(stderr, _("Inconsistent input data\n"));
868 ***************************************************************************
869 * Read statistics from a system activity data file.
872 * @from_file Input file name.
873 ***************************************************************************
875 void read_stats_from_file(char from_file[])
877 struct file_magic file_magic;
878 struct file_activity *file_actlst = NULL;
881 int rows, eosaf = TRUE, reset = FALSE;
885 /* Get window size */
886 rows = get_win_height();
888 /* Read file headers and activity list */
889 check_file_actlst(&ifd, from_file, act, &file_magic, &file_hdr,
890 &file_actlst, id_seq, FALSE);
892 /* Perform required allocations */
893 allocate_structures(act);
895 /* Print report header */
896 print_report_hdr(flags, &rectime, &file_hdr,
897 act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr);
899 /* Read system statistics from file */
902 * If this record is a special (RESTART or COMMENT) one, print it and
903 * (try to) get another one.
906 if (sa_fread(ifd, &record_hdr[0], RECORD_HEADER_SIZE, SOFT_SIZE))
907 /* End of sa data file */
910 rtype = record_hdr[0].record_type;
911 if ((rtype == R_RESTART) || (rtype == R_COMMENT)) {
912 print_special_record(&record_hdr[0], flags + S_F_LOCAL_TIME,
913 &tm_start, &tm_end, rtype, ifd,
914 &rectime, NULL, from_file, 0, &file_magic,
915 &file_hdr, act, &sar_fmt);
919 * OK: Previous record was not a special one.
920 * So read now the extra fields.
922 read_file_stat_bunch(act, 0, ifd, file_hdr.sa_act_nr,
924 if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME,
928 * An error was detected.
929 * The timestamp hasn't been updated.
934 while ((rtype == R_RESTART) || (rtype == R_COMMENT) ||
935 (tm_start.use && (datecmp(&rectime, &tm_start) < 0)) ||
936 (tm_end.use && (datecmp(&rectime, &tm_end) >=0)));
938 /* Save the first stats collected. Will be used to compute the average */
939 copy_structures(act, id_seq, record_hdr, 2, 0);
941 reset = TRUE; /* Set flag to reset last_uptime variable */
943 /* Save current file position */
944 if ((fpos = lseek(ifd, 0, SEEK_CUR)) < 0) {
950 * Read and write stats located between two possible Linux restarts.
951 * Activities that should be displayed are saved in id_seq[] array.
953 for (i = 0; i < NR_ACT; i++) {
958 p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
959 if (!IS_SELECTED(act[p]->options))
962 if (!HAS_MULTIPLE_OUTPUTS(act[p]->options)) {
963 handle_curr_act_stats(ifd, fpos, &curr, &cnt, &eosaf, rows,
964 act[p]->id, &reset, file_actlst,
965 from_file, &file_magic);
968 unsigned int optf, msk;
970 optf = act[p]->opt_flags;
972 for (msk = 1; msk < 0x100; msk <<= 1) {
973 if ((act[p]->opt_flags & 0xff) & msk) {
974 act[p]->opt_flags &= (0xffffff00 + msk);
976 handle_curr_act_stats(ifd, fpos, &curr, &cnt,
977 &eosaf, rows, act[p]->id,
979 from_file, &file_magic);
980 act[p]->opt_flags = optf;
987 /* Go to next Linux restart, if possible */
989 eosaf = sa_fread(ifd, &record_hdr[curr], RECORD_HEADER_SIZE,
991 rtype = record_hdr[curr].record_type;
992 if (!eosaf && (rtype != R_RESTART) && (rtype != R_COMMENT)) {
993 read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr,
996 else if (!eosaf && (rtype == R_COMMENT)) {
997 /* This was a COMMENT record: print it */
998 print_special_record(&record_hdr[curr], flags + S_F_LOCAL_TIME,
999 &tm_start, &tm_end, R_COMMENT, ifd,
1000 &rectime, NULL, from_file, 0,
1001 &file_magic, &file_hdr, act, &sar_fmt);
1004 while (!eosaf && (rtype != R_RESTART));
1007 /* The last record we read was a RESTART one: Print it */
1008 if (!eosaf && (record_hdr[curr].record_type == R_RESTART)) {
1009 print_special_record(&record_hdr[curr], flags + S_F_LOCAL_TIME,
1010 &tm_start, &tm_end, R_RESTART, ifd,
1011 &rectime, NULL, from_file, 0,
1012 &file_magic, &file_hdr, act, &sar_fmt);
1023 ***************************************************************************
1024 * Read statistics sent by sadc, the data collector.
1025 ***************************************************************************
1027 void read_stats(void)
1030 unsigned long lines;
1034 /* Don't buffer data if redirected to a pipe... */
1035 setbuf(stdout, NULL);
1037 /* Read stats header */
1040 if (!get_activity_nr(act, AO_SELECTED, COUNT_ACTIVITIES)) {
1041 fprintf(stderr, _("Requested activities not available\n"));
1045 /* Determine if a stat line header has to be displayed */
1046 dis_hdr = check_line_hdr();
1048 lines = rows = get_win_height();
1050 /* Perform required allocations */
1051 allocate_structures(act);
1053 /* Print report header */
1054 print_report_hdr(flags, &rectime, &file_hdr,
1055 act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr);
1057 /* Read system statistics sent by the data collector */
1058 read_sadc_stat_bunch(0);
1061 /* Display stats since boot time and exit */
1062 write_stats_startup(0);
1065 /* Save the first stats collected. Will be used to compute the average */
1066 copy_structures(act, id_seq, record_hdr, 2, 0);
1068 /* Set a handler for SIGINT */
1069 memset(&int_act, 0, sizeof(int_act));
1070 int_act.sa_handler = int_handler;
1071 int_act.sa_flags = SA_RESTART;
1072 sigaction(SIGINT, &int_act, NULL);
1078 read_sadc_stat_bunch(curr);
1079 if (sigint_caught) {
1081 * SIGINT signal caught (it is sent by sadc).
1082 * => Display average stats.
1084 curr ^= 1; /* No data retrieved from last read */
1096 write_stats(curr, USE_SADC, &count, NO_TM_START, tm_end.use,
1097 NO_RESET, ALL_ACTIVITIES, TRUE);
1099 if (record_hdr[curr].record_type == R_LAST_STATS) {
1100 /* File rotation is happening: Re-read header data sent by sadc */
1102 allocate_structures(act);
1115 * Print statistics average.
1116 * At least one line of stats must have been displayed for this.
1117 * (There may be no lines at all if we press Ctrl/C immediately).
1121 write_stats_avg(curr, USE_SADC, ALL_ACTIVITIES);
1126 ***************************************************************************
1127 * Main entry to the sar program.
1128 ***************************************************************************
1130 int main(int argc, char **argv)
1132 int i, rc, opt = 1, args_idx = 2;
1135 char from_file[MAX_FILE_LEN], to_file[MAX_FILE_LEN];
1141 /* Compute page shift in kB */
1144 from_file[0] = to_file[0] = '\0';
1147 /* Init National Language Support */
1151 /* Init color strings */
1154 tm_start.use = tm_end.use = FALSE;
1156 /* Allocate and init activity bitmaps */
1157 allocate_bitmaps(act);
1161 /* Process options */
1162 while (opt < argc) {
1164 if (!strcmp(argv[opt], "--sadc")) {
1169 else if (!strcmp(argv[opt], "-I")) {
1171 /* Parse -I option */
1172 if (parse_sar_I_opt(argv, &opt, act)) {
1181 else if (!strcmp(argv[opt], "-D")) {
1182 /* Option to tell sar to write to saYYYYMMDD data files */
1183 flags |= S_F_SA_YYYYMMDD;
1187 else if (!strcmp(argv[opt], "-P")) {
1188 /* Parse -P option */
1189 if (parse_sa_P_opt(argv, &opt, &flags, act)) {
1194 else if (!strcmp(argv[opt], "-o")) {
1196 /* Output file already specified */
1199 /* Save stats to a file */
1200 if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
1201 (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1202 strncpy(to_file, argv[opt++], MAX_FILE_LEN);
1203 to_file[MAX_FILE_LEN - 1] = '\0';
1206 strcpy(to_file, "-");
1210 else if (!strcmp(argv[opt], "-f")) {
1211 if (from_file[0] || day_offset) {
1212 /* Input file already specified */
1215 /* Read stats from a file */
1216 if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
1217 (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1218 strncpy(from_file, argv[opt++], MAX_FILE_LEN);
1219 from_file[MAX_FILE_LEN - 1] = '\0';
1220 /* Check if this is an alternate directory for sa files */
1221 check_alt_sa_dir(from_file, day_offset, -1);
1224 set_default_file(from_file, day_offset, -1);
1228 else if (!strcmp(argv[opt], "-s")) {
1229 /* Get time start */
1230 if (parse_timestamp(argv, &opt, &tm_start, DEF_TMSTART)) {
1235 else if (!strcmp(argv[opt], "-e")) {
1237 if (parse_timestamp(argv, &opt, &tm_end, DEF_TMEND)) {
1242 else if (!strcmp(argv[opt], "-h")) {
1243 /* Display help message */
1244 display_help(argv[0]);
1247 else if (!strcmp(argv[opt], "-i")) {
1248 if (!argv[++opt] || (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1251 interval = atol(argv[opt++]);
1255 flags |= S_F_INTERVAL_SET;
1258 else if (!strcmp(argv[opt], "-m")) {
1260 /* Parse option -m */
1261 if (parse_sar_m_opt(argv, &opt, act)) {
1270 else if (!strcmp(argv[opt], "-n")) {
1272 /* Parse option -n */
1273 if (parse_sar_n_opt(argv, &opt, act)) {
1282 else if ((strlen(argv[opt]) > 1) &&
1283 (strlen(argv[opt]) < 4) &&
1284 !strncmp(argv[opt], "-", 1) &&
1285 (strspn(argv[opt] + 1, DIGITS) == (strlen(argv[opt]) - 1))) {
1286 if (from_file[0] || day_offset) {
1287 /* Input file already specified */
1290 day_offset = atoi(argv[opt++] + 1);
1293 else if (!strncmp(argv[opt], "-", 1)) {
1294 /* Other options not previously tested */
1295 if ((rc = parse_sar_opt(argv, &opt, act, &flags, C_SAR)) != 0) {
1304 else if (interval < 0) {
1306 if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
1309 interval = atol(argv[opt++]);
1316 /* Get count value */
1317 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
1322 /* Count parameter already set */
1325 count = atol(argv[opt++]);
1332 /* 'sar' is equivalent to 'sar -f' */
1334 ((interval < 0) && !from_file[0] && !to_file[0])) {
1335 set_default_file(from_file, day_offset, -1);
1338 if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
1339 tm_end.tm_hour += 24;
1343 * Check option dependencies.
1345 /* You read from a file OR you write to it... */
1346 if (from_file[0] && to_file[0]) {
1347 fprintf(stderr, _("-f and -o options are mutually exclusive\n"));
1350 /* Use time start or option -i only when reading stats from a file */
1351 if ((tm_start.use || INTERVAL_SET(flags)) && !from_file[0]) {
1353 _("Not reading from a system activity file (use -f option)\n"));
1356 /* Don't print stats since boot time if -o or -f options are used */
1357 if (!interval && (from_file[0] || to_file[0])) {
1361 /* Cannot enter a day shift with -o option */
1362 if (to_file[0] && day_offset) {
1366 if (USE_PRETTY_OPTION(flags)) {
1367 dm_major = get_devmap_major();
1372 * count parameter not set: Display all the contents of the file
1373 * or generate a report continuously.
1378 /* Default is CPU activity... */
1379 select_default_activity(act);
1381 /* Reading stats from file: */
1387 /* Read stats from file */
1388 read_stats_from_file(from_file);
1390 /* Free stuctures and activity bitmaps */
1392 free_structures(act);
1397 /* Reading stats from sadc: */
1399 /* Create anonymous pipe */
1400 if (pipe(fd) == -1) {
1413 if (dup2(fd[1], STDOUT_FILENO) < 0) {
1420 * Prepare options for sadc.
1425 /* Interval value */
1429 else if (!interval) {
1433 sprintf(ltemp, "%ld", interval);
1439 sprintf(ltemp, "%ld", count + 1);
1440 salloc(args_idx++, ltemp);
1443 /* Flags to be passed to sadc */
1444 salloc(args_idx++, "-z");
1446 /* Writing data to a file (option -o) */
1448 /* Set option -D if entered */
1449 if (USE_SA_YYYYMMDD(flags)) {
1450 salloc(args_idx++, "-D");
1452 /* Collect all possible activities (option -S XALL for sadc) */
1453 salloc(args_idx++, "-S");
1454 salloc(args_idx++, K_XALL);
1456 salloc(args_idx++, to_file);
1460 * If option -o hasn't been used, then tell sadc
1461 * to collect only activities that will be displayed.
1465 for (i = 0; i < NR_ACT; i++) {
1466 if (IS_SELECTED(act[i]->options)) {
1467 act_id |= act[i]->group;
1472 snprintf(ltemp, 19, "%d", act_id);
1474 salloc(args_idx++, "-S");
1475 salloc(args_idx++, ltemp);
1479 /* Last arg is NULL */
1480 args[args_idx] = NULL;
1482 /* Call now the data collector */
1483 execv(SADC_PATH, args);
1486 * Note: Don't use execl/execlp since we don't have a fixed number of
1487 * args to give to sadc.
1489 fprintf(stderr, _("Cannot find the data collector (%s)\n"), SADC);
1494 default: /* Parent */
1495 if (dup2(fd[0], STDIN_FILENO) < 0) {
1501 /* Get now the statistics */
1507 /* Free structures and activity bitmaps */
1509 free_structures(act);