2 * sar: report system activity
3 * (C) 1999-2014 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 ***************************************************************************
39 #define _(string) gettext(string)
41 #define _(string) (string)
44 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
45 char *sccsid(void) { return (SCCSID); }
47 /* Interval and count parameters */
48 long interval = -1, count = 0;
50 /* TRUE if a header line must be printed */
53 unsigned int flags = 0;
54 unsigned int dm_major; /* Device-mapper major number */
56 char timestamp[2][TIMESTAMP_LEN];
58 unsigned long avg_count = 0;
61 struct file_header file_hdr;
63 /* Current record header */
64 struct record_header record_hdr[3];
68 * This array must always be entirely filled (even with trailing zeros).
70 unsigned int id_seq[NR_ACT];
74 /* Contain the date specified by -s and -e options */
75 struct tstamp tm_start, tm_end;
77 char *args[MAX_ARGV_NR];
79 extern struct activity *act[];
81 struct sigaction int_act;
82 int sigint_caught = 0;
85 ***************************************************************************
86 * Print usage title message.
89 * @progname Name of sysstat command
90 ***************************************************************************
92 void print_usage_title(FILE *fp, char *progname)
94 fprintf(fp, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
99 ***************************************************************************
100 * Print usage and exit.
103 * @progname Name of sysstat command
104 ***************************************************************************
106 void usage(char *progname)
108 print_usage_title(stderr, progname);
109 fprintf(stderr, _("Options are:\n"
110 "[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F ] [ -H ] [ -h ] [ -p ] [ -q ]\n"
111 "[ -R ] [ -r [ ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ] [ -v ] [ -W ]\n"
113 "[ -I { <int> [,...] | SUM | ALL | XALL } ] [ -P { <cpu> [,...] | ALL } ]\n"
114 "[ -m { <keyword> [,...] | ALL } ] [ -n { <keyword> [,...] | ALL } ]\n"
115 "[ -j { ID | LABEL | PATH | UUID | ... } ]\n"
116 "[ -f [ <filename> ] | -o [ <filename> ] | -[0-9]+ ]\n"
117 "[ -i <interval> ] [ -s [ <hh:mm:ss> ] ] [ -e [ <hh:mm:ss> ] ]\n"));
122 ***************************************************************************
123 * Display a short help message and exit.
126 * @progname Name of sysstat command
127 ***************************************************************************
129 void display_help(char *progname)
131 print_usage_title(stdout, progname);
132 printf(_("Main options and reports:\n"));
133 printf(_("\t-B\tPaging statistics\n"));
134 printf(_("\t-b\tI/O and transfer rate statistics\n"));
135 printf(_("\t-d\tBlock devices statistics\n"));
136 printf(_("\t-F\tFilesystems statistics\n"));
137 printf(_("\t-H\tHugepages utilization statistics\n"));
138 printf(_("\t-I { <int> | SUM | ALL | XALL }\n"
139 "\t\tInterrupts statistics\n"));
140 printf(_("\t-m { <keyword> [,...] | ALL }\n"
141 "\t\tPower management statistics\n"
142 "\t\tKeywords are:\n"
143 "\t\tCPU\tCPU instantaneous clock frequency\n"
144 "\t\tFAN\tFans speed\n"
145 "\t\tFREQ\tCPU average clock frequency\n"
146 "\t\tIN\tVoltage inputs\n"
147 "\t\tTEMP\tDevices temperature\n"
148 "\t\tUSB\tUSB devices plugged into the system\n"));
149 printf(_("\t-n { <keyword> [,...] | ALL }\n"
150 "\t\tNetwork statistics\n"
151 "\t\tKeywords are:\n"
152 "\t\tDEV\tNetwork interfaces\n"
153 "\t\tEDEV\tNetwork interfaces (errors)\n"
154 "\t\tNFS\tNFS client\n"
155 "\t\tNFSD\tNFS server\n"
156 "\t\tSOCK\tSockets\t(v4)\n"
157 "\t\tIP\tIP traffic\t(v4)\n"
158 "\t\tEIP\tIP traffic\t(v4) (errors)\n"
159 "\t\tICMP\tICMP traffic\t(v4)\n"
160 "\t\tEICMP\tICMP traffic\t(v4) (errors)\n"
161 "\t\tTCP\tTCP traffic\t(v4)\n"
162 "\t\tETCP\tTCP traffic\t(v4) (errors)\n"
163 "\t\tUDP\tUDP traffic\t(v4)\n"
164 "\t\tSOCK6\tSockets\t(v6)\n"
165 "\t\tIP6\tIP traffic\t(v6)\n"
166 "\t\tEIP6\tIP traffic\t(v6) (errors)\n"
167 "\t\tICMP6\tICMP traffic\t(v6)\n"
168 "\t\tEICMP6\tICMP traffic\t(v6) (errors)\n"
169 "\t\tUDP6\tUDP traffic\t(v6)\n"));
170 printf(_("\t-q\tQueue length and load average statistics\n"));
171 printf(_("\t-R\tMemory statistics\n"));
172 printf(_("\t-r [ ALL ]\n"
173 "\tMemory utilization statistics\n"));
174 printf(_("\t-S\tSwap space utilization statistics\n"));
175 printf(_("\t-u [ ALL ]\n"
176 "\t\tCPU utilization statistics\n"));
177 printf(_("\t-v\tKernel tables statistics\n"));
178 printf(_("\t-W\tSwapping statistics\n"));
179 printf(_("\t-w\tTask creation and system switching statistics\n"));
180 printf(_("\t-y\tTTY devices statistics\n"));
185 ***************************************************************************
186 * SIGINT signal handler.
189 * @sig Signal number.
190 ***************************************************************************
192 void int_handler(int sig)
195 printf("\n"); /* Skip "^C" displayed on screen */
200 ***************************************************************************
201 * Init some structures.
202 ***************************************************************************
204 void init_structures(void)
208 for (i = 0; i < 3; i++)
209 memset(&record_hdr[i], 0, RECORD_HEADER_SIZE);
213 ***************************************************************************
214 * Allocate memory for sadc args.
217 * @i Argument number.
218 * @ltemp Argument value.
219 ***************************************************************************
221 void salloc(int i, char *ltemp)
223 if ((args[i] = (char *) malloc(strlen(ltemp) + 1)) == NULL) {
227 strcpy(args[i], ltemp);
231 ***************************************************************************
232 * Display an error message. Happens when the data collector doesn't send
234 ***************************************************************************
236 void print_read_error(void)
238 fprintf(stderr, _("End of data collecting unexpected\n"));
243 ***************************************************************************
244 * Check that every selected activity actually belongs to the sequence list.
245 * If not, then the activity should be unselected since it will not be sent
246 * by sadc. An activity can be not sent if its number of items is null.
249 * @act_nr Size of sequence list.
250 ***************************************************************************
252 void reverse_check_act(unsigned int act_nr)
256 for (i = 0; i < NR_ACT; i++) {
258 if (IS_SELECTED(act[i]->options)) {
260 for (j = 0; j < act_nr; j++) {
261 if (id_seq[j] == act[i]->id)
265 act[i]->options &= ~AO_SELECTED;
271 ***************************************************************************
272 * Fill the (struct tm) rectime structure with current record's time,
273 * based on current record's time data saved in file.
274 * The resulting timestamp is expressed in the locale of the file creator
275 * or in the user's own locale depending on whether option -t has been used
279 * @curr Index in array for current sample statistics.
282 * 1 if an error was detected, or 0 otherwise.
283 ***************************************************************************
285 int sar_get_record_timestamp_struct(int curr)
289 /* Check if option -t was specified on the command line */
290 if (PRINT_TRUE_TIME(flags)) {
292 rectime.tm_hour = record_hdr[curr].hour;
293 rectime.tm_min = record_hdr[curr].minute;
294 rectime.tm_sec = record_hdr[curr].second;
297 if ((ltm = localtime((const time_t *) &record_hdr[curr].ust_time)) == NULL)
299 * An error was detected.
300 * The rectime structure has NOT been updated.
311 ***************************************************************************
312 * Determine if a stat header line has to be displayed.
315 * TRUE if a header line has to be displayed.
316 ***************************************************************************
318 int check_line_hdr(void)
322 /* Get number of options entered on the command line */
323 if (get_activity_nr(act, AO_SELECTED, COUNT_OUTPUTS) > 1)
326 for (i = 0; i < NR_ACT; i++) {
327 if (IS_SELECTED(act[i]->options)) {
328 /* Special processing for activities using a bitmap */
329 if (act[i]->bitmap) {
330 if (count_bits(act[i]->bitmap->b_array,
331 BITMAP_SIZE(act[i]->bitmap->b_size)) > 1) {
335 else if (act[i]->nr > 1) {
338 /* Stop now since we have only one selected activity */
347 ***************************************************************************
348 * Set current record's timestamp string.
351 * @curr Index in array for current sample statistics.
352 * @len Maximum length of timestamp string.
355 * @cur_time Timestamp string.
358 * 1 if an error was detected, or 0 otherwise.
359 ***************************************************************************
361 int set_record_timestamp_string(int curr, char *cur_time, int len)
363 /* Fill timestamp structure */
364 if (sar_get_record_timestamp_struct(curr))
368 /* Set cur_time date value */
369 strftime(cur_time, len, "%X", &rectime);
375 ***************************************************************************
376 * Print statistics average.
379 * @curr Index in array for current sample statistics.
380 * @read_from_file Set to TRUE if stats are read from a system activity
382 * @act_id Activity that can be displayed, or ~0 for all.
383 * Remember that when reading stats from a file, only
384 * one activity can be displayed at a time.
385 ***************************************************************************
387 void write_stats_avg(int curr, int read_from_file, unsigned int act_id)
390 unsigned long long itv, g_itv;
391 static __nr_t cpu_nr = -1;
394 cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
396 /* Interval value in jiffies */
397 g_itv = get_interval(record_hdr[2].uptime, record_hdr[curr].uptime);
400 itv = get_interval(record_hdr[2].uptime0, record_hdr[curr].uptime0);
404 strncpy(timestamp[curr], _("Average:"), TIMESTAMP_LEN);
405 timestamp[curr][TIMESTAMP_LEN - 1] = '\0';
406 strcpy(timestamp[!curr], timestamp[curr]);
409 TEST_STDOUT(STDOUT_FILENO);
411 for (i = 0; i < NR_ACT; i++) {
413 if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
416 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
417 /* Display current average activity statistics */
418 (*act[i]->f_print_avg)(act[i], 2, curr,
419 NEED_GLOBAL_ITV(act[i]->options) ? g_itv : itv);
423 if (read_from_file) {
425 * Reset number of lines printed only if we read stats
426 * from a system activity file.
433 ***************************************************************************
434 * Print system statistics.
437 * @curr Index in array for current sample statistics.
438 * @read_from_file Set to TRUE if stats are read from a system activity
440 * @use_tm_start Set to TRUE if option -s has been used.
441 * @use_tm_end Set to TRUE if option -e has been used.
442 * @reset Set to TRUE if last_uptime variable should be
443 * reinitialized (used in next_slice() function).
444 * @act_id Activity that can be displayed or ~0 for all.
445 * Remember that when reading stats from a file, only
446 * one activity can be displayed at a time.
447 * @reset_cd TRUE if static cross_day variable should be reset
451 * @cnt Number of remaining lines to display.
454 * 1 if stats have been successfully displayed, and 0 otherwise.
455 ***************************************************************************
457 int write_stats(int curr, int read_from_file, long *cnt, int use_tm_start,
458 int use_tm_end, int reset, unsigned int act_id, int reset_cd)
461 unsigned long long itv, g_itv;
462 static int cross_day = 0;
463 static __nr_t cpu_nr = -1;
466 cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
470 * cross_day is a static variable that is set to 1 when the first
471 * record of stats from a new day is read from a unique data file
472 * (in the case where the file contains data from two consecutive
473 * days). When set to 1, every following records timestamp will
474 * have its hour value increased by 24.
475 * Yet when a new activity (being read from the file) is going to
476 * be displayed, we start reading the file from the beginning
477 * again, and so cross_day should be reset in this case.
483 if (read_from_file) {
484 if (!next_slice(record_hdr[2].uptime0, record_hdr[curr].uptime0,
486 /* Not close enough to desired interval */
490 /* Set previous timestamp */
491 if (set_record_timestamp_string(!curr, timestamp[!curr], 16))
493 /* Set current timestamp */
494 if (set_record_timestamp_string(curr, timestamp[curr], 16))
497 /* Check if we are beginning a new day */
498 if (use_tm_start && record_hdr[!curr].ust_time &&
499 (record_hdr[curr].ust_time > record_hdr[!curr].ust_time) &&
500 (record_hdr[curr].hour < record_hdr[!curr].hour)) {
506 * This is necessary if we want to properly handle something like:
507 * sar -s time_start -e time_end with
508 * time_start(day D) > time_end(day D+1)
510 rectime.tm_hour +=24;
514 if (use_tm_start && (datecmp(&rectime, &tm_start) < 0))
515 /* it's too soon... */
518 /* Get interval values */
519 get_itv_value(&record_hdr[curr], &record_hdr[!curr],
520 cpu_nr, &itv, &g_itv);
523 if (use_tm_end && (datecmp(&rectime, &tm_end) > 0)) {
524 /* It's too late... */
532 TEST_STDOUT(STDOUT_FILENO);
534 for (i = 0; i < NR_ACT; i++) {
536 if ((act_id != ALL_ACTIVITIES) && (act[i]->id != act_id))
539 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
540 /* Display current activity statistics */
541 (*act[i]->f_print)(act[i], !curr, curr,
542 NEED_GLOBAL_ITV(act[i]->options) ? g_itv : itv);
550 ***************************************************************************
551 * Display stats since system startup.
554 * @curr Index in array for current sample statistics.
555 ***************************************************************************
557 void write_stats_startup(int curr)
561 /* Set to 0 previous structures corresponding to boot time */
562 memset(&record_hdr[!curr], 0, RECORD_HEADER_SIZE);
563 record_hdr[!curr].record_type = R_STATS;
564 record_hdr[!curr].hour = record_hdr[curr].hour;
565 record_hdr[!curr].minute = record_hdr[curr].minute;
566 record_hdr[!curr].second = record_hdr[curr].second;
567 record_hdr[!curr].ust_time = record_hdr[curr].ust_time;
569 for (i = 0; i < NR_ACT; i++) {
570 if (IS_SELECTED(act[i]->options) && (act[i]->nr > 0)) {
571 memset(act[i]->buf[!curr], 0,
572 (size_t) act[i]->msize * (size_t) act[i]->nr * (size_t) act[i]->nr2);
576 flags |= S_F_SINCE_BOOT;
579 write_stats(curr, USE_SADC, &count, NO_TM_START, NO_TM_END, NO_RESET,
580 ALL_ACTIVITIES, TRUE);
586 ***************************************************************************
587 * Read data sent by the data collector.
590 * @size Number of bytes of data to read.
593 * @buffer Buffer where data will be saved.
596 * 1 if end of file has been reached, 0 otherwise.
597 ***************************************************************************
599 int sa_read(void *buffer, int size)
605 if ((n = read(STDIN_FILENO, buffer, size)) < 0) {
614 buffer = (char *) buffer + n;
621 ***************************************************************************
622 * Print a Linux restart message (contents of a RESTART record) or a
623 * comment (contents of a COMMENT record).
626 * @curr Index in array for current sample statistics.
627 * @use_tm_start Set to TRUE if option -s has been used.
628 * @use_tm_end Set to TRUE if option -e has been used.
629 * @rtype Record type to display.
630 * @ifd Input file descriptor.
631 * @file Name of file being read.
632 * @file_magic file_magic structure filled with file magic header
636 * 1 if the record has been successfully displayed, and 0 otherwise.
637 ***************************************************************************
639 int sar_print_special(int curr, int use_tm_start, int use_tm_end, int rtype,
640 int ifd, char *file, struct file_magic *file_magic)
644 unsigned int new_cpu_nr;
646 if (set_record_timestamp_string(curr, cur_time, 26))
649 /* The record must be in the interval specified by -s/-e options */
650 if ((use_tm_start && (datecmp(&rectime, &tm_start) < 0)) ||
651 (use_tm_end && (datecmp(&rectime, &tm_end) > 0))) {
655 if (rtype == R_RESTART) {
656 /* Don't forget to read the volatile activities structures */
657 new_cpu_nr = read_vol_act_structures(ifd, act, file, file_magic,
658 file_hdr.sa_vol_act_nr);
661 printf("\n%-11s LINUX RESTART\t(%d CPU)\n",
662 cur_time, new_cpu_nr > 1 ? new_cpu_nr - 1 : 1);
666 else if (rtype == R_COMMENT) {
667 char file_comment[MAX_COMMENT_LEN];
669 /* Don't forget to read comment record even if it won't be displayed... */
670 sa_fread(ifd, file_comment, MAX_COMMENT_LEN, HARD_SIZE);
671 file_comment[MAX_COMMENT_LEN - 1] = '\0';
673 if (dp && DISPLAY_COMMENT(flags)) {
674 printf("%-11s COM %s\n", cur_time, file_comment);
683 ***************************************************************************
684 * Read the various statistics sent by the data collector (sadc).
687 * @curr Index in array for current sample statistics.
688 ***************************************************************************
690 void read_sadc_stat_bunch(int curr)
694 /* Read record header (type is always R_STATS since it is read from sadc) */
695 if (sa_read(&record_hdr[curr], RECORD_HEADER_SIZE)) {
699 for (i = 0; i < NR_ACT; i++) {
703 p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
705 if (sa_read(act[p]->buf[curr], act[p]->fsize * act[p]->nr * act[p]->nr2)) {
712 ***************************************************************************
713 * Read stats for current activity from file and display them.
716 * @ifd Input file descriptor.
717 * @fpos Position in file where reading must start.
718 * @curr Index in array for current sample statistics.
719 * @rows Number of rows of screen.
720 * @act_id Activity to display.
721 * @file_actlst List of activities in file.
722 * @file Name of file being read.
723 * @file_magic file_magic structure filled with file magic header data.
726 * @curr Index in array for next sample statistics.
727 * @cnt Number of remaining lines of stats to write.
728 * @eosaf Set to TRUE if EOF (end of file) has been reached.
729 * @reset Set to TRUE if last_uptime variable should be
730 * reinitialized (used in next_slice() function).
731 ***************************************************************************
733 void handle_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
734 int rows, unsigned int act_id, int *reset,
735 struct file_activity *file_actlst, char *file,
736 struct file_magic *file_magic)
739 unsigned long lines = 0;
741 int davg = 0, next, inc;
743 if (lseek(ifd, fpos, SEEK_SET) < fpos) {
749 * Restore the first stats collected.
750 * Used to compute the rate displayed on the first line.
752 copy_structures(act, id_seq, record_hdr, !*curr, 2);
756 /* Assess number of lines printed */
757 p = get_activity_position(act, act_id, EXIT_IF_NOT_FOUND);
758 if (act[p]->bitmap) {
759 inc = count_bits(act[p]->bitmap->b_array,
760 BITMAP_SIZE(act[p]->bitmap->b_size));
769 /* Display count lines of stats */
770 *eosaf = sa_fread(ifd, &record_hdr[*curr],
771 RECORD_HEADER_SIZE, SOFT_SIZE);
772 rtype = record_hdr[*curr].record_type;
774 if (!*eosaf && (rtype != R_RESTART) && (rtype != R_COMMENT)) {
775 /* Read the extra fields since it's not a special record */
776 read_file_stat_bunch(act, *curr, ifd, file_hdr.sa_act_nr, file_actlst);
779 if ((lines >= rows) || !lines) {
786 if (!*eosaf && (rtype != R_RESTART)) {
788 if (rtype == R_COMMENT) {
789 /* Display comment */
790 next = sar_print_special(*curr, tm_start.use, tm_end.use,
791 R_COMMENT, ifd, file, file_magic);
793 /* A line of comment was actually displayed */
799 /* next is set to 1 when we were close enough to desired interval */
800 next = write_stats(*curr, USE_SA_FILE, cnt, tm_start.use, tm_end.use,
801 *reset, act_id, reset_cd);
803 if (next && (*cnt > 0)) {
814 while (*cnt && !*eosaf && (rtype != R_RESTART));
817 write_stats_avg(!*curr, USE_SA_FILE, act_id);
824 ***************************************************************************
825 * Read header data sent by sadc.
826 ***************************************************************************
828 void read_header_data(void)
830 struct file_magic file_magic;
831 struct file_activity file_act;
835 /* Read magic header */
836 rc = sa_read(&file_magic, FILE_MAGIC_SIZE);
838 sprintf(version, "%d.%d.%d.%d",
839 file_magic.sysstat_version,
840 file_magic.sysstat_patchlevel,
841 file_magic.sysstat_sublevel,
842 file_magic.sysstat_extraversion);
843 if (!file_magic.sysstat_extraversion) {
844 version[strlen(version) - 2] = '\0';
847 if (rc || (file_magic.sysstat_magic != SYSSTAT_MAGIC) ||
848 (file_magic.format_magic != FORMAT_MAGIC) ||
849 strcmp(version, VERSION)) {
851 /* sar and sadc commands are not consistent */
852 fprintf(stderr, _("Invalid data format\n"));
854 if (!rc && (file_magic.sysstat_magic == SYSSTAT_MAGIC)) {
856 _("Using a wrong data collector from a different sysstat version\n"));
863 * No need to take into account file_magic.header_size. We are sure that
864 * sadc and sar are from the same version (we have checked FORMAT_MAGIC
865 * but also VERSION above) and thus the size of file_header is FILE_HEADER_SIZE.
867 if (sa_read(&file_hdr, FILE_HEADER_SIZE)) {
871 /* Read activity list */
872 for (i = 0; i < file_hdr.sa_act_nr; i++) {
874 if (sa_read(&file_act, FILE_ACTIVITY_SIZE)) {
878 p = get_activity_position(act, file_act.id, RESUME_IF_NOT_FOUND);
880 if ((p < 0) || (act[p]->fsize != file_act.size)
883 || (act[p]->magic != file_act.magic)) {
884 /* Remember that we are reading data from sadc and not from a file... */
885 fprintf(stderr, _("Inconsistent input data\n"));
889 id_seq[i] = file_act.id; /* We necessarily have "i < NR_ACT" */
890 act[p]->nr = file_act.nr;
891 act[p]->nr2 = file_act.nr2;
898 /* Check that all selected activties are actually sent by sadc */
899 reverse_check_act(file_hdr.sa_act_nr);
903 ***************************************************************************
904 * Read statistics from a system activity data file.
907 * @from_file Input file name.
908 ***************************************************************************
910 void read_stats_from_file(char from_file[])
912 struct file_magic file_magic;
913 struct file_activity *file_actlst = NULL;
916 int rows, eosaf = TRUE, reset = FALSE;
920 /* Get window size */
921 rows = get_win_height();
923 /* Read file headers and activity list */
924 check_file_actlst(&ifd, from_file, act, &file_magic, &file_hdr,
925 &file_actlst, id_seq, FALSE);
927 /* Perform required allocations */
928 allocate_structures(act);
930 /* Print report header */
931 print_report_hdr(flags, &rectime, &file_hdr,
932 act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr);
934 /* Read system statistics from file */
937 * If this record is a special (RESTART or COMMENT) one, print it and
938 * (try to) get another one.
941 if (sa_fread(ifd, &record_hdr[0], RECORD_HEADER_SIZE, SOFT_SIZE))
942 /* End of sa data file */
945 rtype = record_hdr[0].record_type;
946 if ((rtype == R_RESTART) || (rtype == R_COMMENT)) {
947 sar_print_special(0, tm_start.use, tm_end.use, rtype,
948 ifd, from_file, &file_magic);
952 * OK: Previous record was not a special one.
953 * So read now the extra fields.
955 read_file_stat_bunch(act, 0, ifd, file_hdr.sa_act_nr,
957 if (sar_get_record_timestamp_struct(0))
959 * An error was detected.
960 * The timestamp hasn't been updated.
965 while ((rtype == R_RESTART) || (rtype == R_COMMENT) ||
966 (tm_start.use && (datecmp(&rectime, &tm_start) < 0)) ||
967 (tm_end.use && (datecmp(&rectime, &tm_end) >=0)));
969 /* Save the first stats collected. Will be used to compute the average */
970 copy_structures(act, id_seq, record_hdr, 2, 0);
972 reset = TRUE; /* Set flag to reset last_uptime variable */
974 /* Save current file position */
975 if ((fpos = lseek(ifd, 0, SEEK_CUR)) < 0) {
981 * Read and write stats located between two possible Linux restarts.
982 * Activities that should be displayed are saved in id_seq[] array.
984 for (i = 0; i < NR_ACT; i++) {
989 p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
990 if (!IS_SELECTED(act[p]->options))
993 if (!HAS_MULTIPLE_OUTPUTS(act[p]->options)) {
994 handle_curr_act_stats(ifd, fpos, &curr, &cnt, &eosaf, rows,
995 act[p]->id, &reset, file_actlst,
996 from_file, &file_magic);
999 unsigned int optf, msk;
1001 optf = act[p]->opt_flags;
1003 for (msk = 1; msk < 0x100; msk <<= 1) {
1004 if ((act[p]->opt_flags & 0xff) & msk) {
1005 act[p]->opt_flags &= (0xffffff00 + msk);
1007 handle_curr_act_stats(ifd, fpos, &curr, &cnt,
1008 &eosaf, rows, act[p]->id,
1009 &reset, file_actlst,
1010 from_file, &file_magic);
1011 act[p]->opt_flags = optf;
1018 /* Go to next Linux restart, if possible */
1020 eosaf = sa_fread(ifd, &record_hdr[curr], RECORD_HEADER_SIZE,
1022 rtype = record_hdr[curr].record_type;
1023 if (!eosaf && (rtype != R_RESTART) && (rtype != R_COMMENT)) {
1024 read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr,
1027 else if (!eosaf && (rtype == R_COMMENT)) {
1028 /* This was a COMMENT record: print it */
1029 sar_print_special(curr, tm_start.use, tm_end.use, R_COMMENT,
1030 ifd, from_file, &file_magic);
1033 while (!eosaf && (rtype != R_RESTART));
1036 /* The last record we read was a RESTART one: Print it */
1037 if (!eosaf && (record_hdr[curr].record_type == R_RESTART)) {
1038 sar_print_special(curr, tm_start.use, tm_end.use, R_RESTART,
1039 ifd, from_file, &file_magic);
1050 ***************************************************************************
1051 * Read statistics sent by sadc, the data collector.
1052 ***************************************************************************
1054 void read_stats(void)
1057 unsigned long lines;
1061 /* Don't buffer data if redirected to a pipe... */
1062 setbuf(stdout, NULL);
1064 /* Read stats header */
1067 if (!get_activity_nr(act, AO_SELECTED, COUNT_ACTIVITIES)) {
1068 fprintf(stderr, _("Requested activities not available\n"));
1072 /* Determine if a stat line header has to be displayed */
1073 dis_hdr = check_line_hdr();
1075 lines = rows = get_win_height();
1077 /* Perform required allocations */
1078 allocate_structures(act);
1080 /* Print report header */
1081 print_report_hdr(flags, &rectime, &file_hdr,
1082 act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr);
1084 /* Read system statistics sent by the data collector */
1085 read_sadc_stat_bunch(0);
1088 /* Display stats since boot time and exit */
1089 write_stats_startup(0);
1092 /* Save the first stats collected. Will be used to compute the average */
1093 copy_structures(act, id_seq, record_hdr, 2, 0);
1095 /* Set a handler for SIGINT */
1096 memset(&int_act, 0, sizeof(int_act));
1097 int_act.sa_handler = int_handler;
1098 int_act.sa_flags = SA_RESTART;
1099 sigaction(SIGINT, &int_act, NULL);
1105 read_sadc_stat_bunch(curr);
1115 write_stats(curr, USE_SADC, &count, NO_TM_START, tm_end.use,
1116 NO_RESET, ALL_ACTIVITIES, TRUE);
1118 if (record_hdr[curr].record_type == R_LAST_STATS) {
1119 /* File rotation is happening: Re-read header data sent by sadc */
1121 allocate_structures(act);
1128 if (sigint_caught) {
1129 /* SIGINT signal caught => Display average stats */
1139 /* Print statistics average */
1141 write_stats_avg(curr, USE_SADC, ALL_ACTIVITIES);
1145 ***************************************************************************
1146 * Main entry to the sar program.
1147 ***************************************************************************
1149 int main(int argc, char **argv)
1151 int i, rc, opt = 1, args_idx = 2;
1154 char from_file[MAX_FILE_LEN], to_file[MAX_FILE_LEN];
1160 /* Compute page shift in kB */
1163 from_file[0] = to_file[0] = '\0';
1166 /* Init National Language Support */
1170 tm_start.use = tm_end.use = FALSE;
1172 /* Allocate and init activity bitmaps */
1173 allocate_bitmaps(act);
1177 /* Process options */
1178 while (opt < argc) {
1180 if (!strcmp(argv[opt], "-I")) {
1182 /* Parse -I option */
1183 if (parse_sar_I_opt(argv, &opt, act)) {
1192 else if (!strcmp(argv[opt], "-D")) {
1193 /* Option to tell sar to write to saYYYYMMDD data files */
1194 flags |= S_F_SA_YYYYMMDD;
1198 else if (!strcmp(argv[opt], "-P")) {
1199 /* Parse -P option */
1200 if (parse_sa_P_opt(argv, &opt, &flags, act)) {
1205 else if (!strcmp(argv[opt], "-o")) {
1207 /* Output file already specified */
1210 /* Save stats to a file */
1211 if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
1212 (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1213 strncpy(to_file, argv[opt++], MAX_FILE_LEN);
1214 to_file[MAX_FILE_LEN - 1] = '\0';
1217 strcpy(to_file, "-");
1221 else if (!strcmp(argv[opt], "-f")) {
1222 if (from_file[0] || day_offset) {
1223 /* Input file already specified */
1226 /* Read stats from a file */
1227 if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
1228 (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1229 strncpy(from_file, argv[opt++], MAX_FILE_LEN);
1230 from_file[MAX_FILE_LEN - 1] = '\0';
1231 /* Check if this is an alternate directory for sa files */
1232 check_alt_sa_dir(from_file, day_offset, -1);
1235 set_default_file(from_file, day_offset, -1);
1239 else if (!strcmp(argv[opt], "-s")) {
1240 /* Get time start */
1241 if (parse_timestamp(argv, &opt, &tm_start, DEF_TMSTART)) {
1246 else if (!strcmp(argv[opt], "-e")) {
1248 if (parse_timestamp(argv, &opt, &tm_end, DEF_TMEND)) {
1253 else if (!strcmp(argv[opt], "-h")) {
1254 /* Display help message */
1255 display_help(argv[0]);
1258 else if (!strcmp(argv[opt], "-i")) {
1259 if (!argv[++opt] || (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
1262 interval = atol(argv[opt++]);
1266 flags |= S_F_INTERVAL_SET;
1269 else if (!strcmp(argv[opt], "-m")) {
1271 /* Parse option -m */
1272 if (parse_sar_m_opt(argv, &opt, act)) {
1281 else if (!strcmp(argv[opt], "-n")) {
1283 /* Parse option -n */
1284 if (parse_sar_n_opt(argv, &opt, act)) {
1293 else if ((strlen(argv[opt]) > 1) &&
1294 (strlen(argv[opt]) < 4) &&
1295 !strncmp(argv[opt], "-", 1) &&
1296 (strspn(argv[opt] + 1, DIGITS) == (strlen(argv[opt]) - 1))) {
1297 if (from_file[0] || day_offset) {
1298 /* Input file already specified */
1301 day_offset = atoi(argv[opt++] + 1);
1304 else if (!strncmp(argv[opt], "-", 1)) {
1305 /* Other options not previously tested */
1306 if ((rc = parse_sar_opt(argv, &opt, act, &flags, C_SAR)) != 0) {
1315 else if (interval < 0) {
1317 if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
1320 interval = atol(argv[opt++]);
1327 /* Get count value */
1328 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
1333 /* Count parameter already set */
1336 count = atol(argv[opt++]);
1343 /* 'sar' is equivalent to 'sar -f' */
1345 ((interval < 0) && !from_file[0] && !to_file[0])) {
1346 set_default_file(from_file, day_offset, -1);
1349 if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
1350 tm_end.tm_hour += 24;
1354 * Check option dependencies.
1356 /* You read from a file OR you write to it... */
1357 if (from_file[0] && to_file[0]) {
1358 fprintf(stderr, _("-f and -o options are mutually exclusive\n"));
1361 /* Use time start or option -i only when reading stats from a file */
1362 if ((tm_start.use || INTERVAL_SET(flags)) && !from_file[0]) {
1364 _("Not reading from a system activity file (use -f option)\n"));
1367 /* Don't print stats since boot time if -o or -f options are used */
1368 if (!interval && (from_file[0] || to_file[0])) {
1372 /* Cannot enter a day shift with -o option */
1373 if (to_file[0] && day_offset) {
1377 if (USE_PRETTY_OPTION(flags)) {
1378 dm_major = get_devmap_major();
1383 * count parameter not set: Display all the contents of the file
1384 * or generate a report continuously.
1389 /* Default is CPU activity... */
1390 select_default_activity(act);
1392 /* Reading stats from file: */
1398 /* Read stats from file */
1399 read_stats_from_file(from_file);
1401 /* Free stuctures and activity bitmaps */
1403 free_structures(act);
1408 /* Reading stats from sadc: */
1410 /* Create anonymous pipe */
1411 if (pipe(fd) == -1) {
1424 if (dup2(fd[1], STDOUT_FILENO) < 0) {
1431 * Prepare options for sadc.
1436 /* Interval value */
1440 else if (!interval) {
1444 sprintf(ltemp, "%ld", interval);
1450 sprintf(ltemp, "%ld", count + 1);
1451 salloc(args_idx++, ltemp);
1454 /* Flags to be passed to sadc */
1455 salloc(args_idx++, "-z");
1457 /* Writing data to a file (option -o) */
1459 /* Set option -D if entered */
1460 if (USE_SA_YYYYMMDD(flags)) {
1461 salloc(args_idx++, "-D");
1463 /* Collect all possible activities (option -S XALL for sadc) */
1464 salloc(args_idx++, "-S");
1465 salloc(args_idx++, K_XALL);
1467 salloc(args_idx++, to_file);
1471 * If option -o hasn't been used, then tell sadc
1472 * to collect only activities that will be displayed.
1476 for (i = 0; i < NR_ACT; i++) {
1477 if (IS_SELECTED(act[i]->options)) {
1478 act_id |= act[i]->group;
1483 snprintf(ltemp, 19, "%d", act_id);
1485 salloc(args_idx++, "-S");
1486 salloc(args_idx++, ltemp);
1490 /* Last arg is NULL */
1491 args[args_idx] = NULL;
1493 /* Call now the data collector */
1494 execv(SADC_PATH, args);
1497 * Note: Don't use execl/execlp since we don't have a fixed number of
1498 * args to give to sadc.
1500 fprintf(stderr, _("Cannot find the data collector (%s)\n"), SADC);
1505 default: /* Parent */
1506 if (dup2(fd[0], STDIN_FILENO) < 0) {
1512 /* Get now the statistics */
1518 /* Free structures and activity bitmaps */
1520 free_structures(act);