2 * tapestat: report tape statistics
3 * (C) 2015 Hewlett-Packard Development Company, L.P.
5 * Initial revision by Shane M. SEYMOUR (shane.seymour <at> hpe.com)
6 * Modified for sysstat by Sebastien GODARD (sysstat <at> orange.fr)
8 ***************************************************************************
9 * This program is free software; you can redistribute it and/or modify it *
10 * under the terms of the GNU General Public License as published by the *
11 * Free Software Foundation; either version 2 of the License, or (at your *
12 * option) any later version. *
14 * This program is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * You should have received a copy of the GNU General Public License along *
20 * with this program; if not, write to the Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
22 ***************************************************************************
31 #define __DO_NOT_DEFINE_COMPILE
38 #include <sys/utsname.h>
40 #ifdef HAVE_SYS_PARAM_H
41 #include <sys/param.h>
42 #undef HZ /* sys/param.h defines HZ but needed for MAXPATHLEN */
45 #define MAXPATHLEN 256
56 #define _(string) gettext(string)
58 #define _(string) (string)
62 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
63 char *sccsid(void) { return (SCCSID); }
67 void int_handler(int n) { return; }
70 int cpu_nr = 0; /* Nb of processors on the machine */
71 int flags = 0; /* Flag for common options and system state */
74 char timestamp[TIMESTAMP_LEN];
76 struct sigaction alrm_act;
78 /* Number of decimal places */
82 * For tape stats - it would be extremely rare for there to be a very large
83 * number of tape drives attached to a system. I wouldn't expect to see more
84 * than 20-30 in a very large configuration and discontinguous ones should
85 * be even more rare. Because of this we keep the old and new data in a
86 * simple data structure with the tape index being the number after the tape
87 * drive, st0 at index 0, etc.
89 int max_tape_drives = 0;
90 struct tape_stats *tape_new_stats = { NULL };
91 struct tape_stats *tape_old_stats = { NULL };
95 ***************************************************************************
96 * Print usage and exit.
99 * @progname Name of sysstat command.
100 ***************************************************************************
102 void usage(char *progname)
104 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
106 fprintf(stderr, _("Options are:\n"
107 "[ --human ] [ -k | -m ] [ -t ] [ -V ] [ -y ] [ -z ]\n"));
112 ***************************************************************************
113 * SIGALRM signal handler. No need to reset the handler here.
116 * @sig Signal number.
117 ***************************************************************************
119 void alarm_handler(int sig)
125 ***************************************************************************
127 ***************************************************************************
129 void tape_initialise(void)
131 /* How many processors on this machine? */
132 cpu_nr = get_cpu_nr(~0, FALSE);
134 /* Compile regular expression for tape names */
135 if (regcomp(&tape_reg, "^st[0-9]+$", REG_EXTENDED) != 0) {
141 ***************************************************************************
143 ***************************************************************************
145 void tape_uninitialise(void)
148 if (tape_old_stats != NULL) {
149 free(tape_old_stats);
151 if (tape_new_stats != NULL) {
152 free(tape_new_stats);
157 ***************************************************************************
158 * Get maximum number of tapes in the system.
161 * Number of tapes found.
162 ***************************************************************************
164 int get_max_tape_drives(void)
167 struct dirent *entry;
168 int new_max_tape_drives, tmp, num_stats_dir = 0;
170 char stats_dir[MAXPATHLEN + 1];
171 struct stat stat_buf;
173 new_max_tape_drives = max_tape_drives;
175 /* Open sysfs tree */
176 dir = opendir(SYSFS_CLASS_TAPE_DIR);
180 while ((entry = readdir(dir)) != NULL) {
182 if (regexec(&tape_reg, &entry->d_name[0], 1, &match, 0) == 0) {
183 /* d_name[2] to skip the st at the front */
184 tmp = atoi(&entry->d_name[2]) + 1;
185 if (tmp > new_max_tape_drives) {
186 new_max_tape_drives = tmp;
189 snprintf(stats_dir, MAXPATHLEN, "%s/%s/%s",
190 SYSFS_CLASS_TAPE_DIR, &entry->d_name[0], "stats");
191 if (stat(stats_dir, &stat_buf) == 0) {
192 if (S_ISDIR(stat_buf.st_mode)) {
199 /* If there are no stats directories make the new number of tape drives 0 */
200 if (num_stats_dir == 0) {
201 new_max_tape_drives = 0;
204 return new_max_tape_drives;
208 ***************************************************************************
209 * Check if new tapes have been added and reallocate structures accordingly.
210 ***************************************************************************
212 void tape_check_tapes_and_realloc(void)
214 int new_max_tape_drives, i;
216 /* Count again number of tapes */
217 new_max_tape_drives = get_max_tape_drives();
219 if (new_max_tape_drives > max_tape_drives && new_max_tape_drives > 0) {
220 /* New tapes found: Realloc structures */
221 struct tape_stats *tape_old_stats_t = (struct tape_stats *)
222 realloc(tape_old_stats, sizeof(struct tape_stats) * new_max_tape_drives);
223 struct tape_stats *tape_new_stats_t = (struct tape_stats *)
224 realloc(tape_new_stats, sizeof(struct tape_stats) * new_max_tape_drives);
225 if ((tape_old_stats_t == NULL) || (tape_new_stats_t == NULL)) {
226 if (tape_old_stats_t != NULL) {
227 free(tape_old_stats_t);
228 tape_old_stats_t = NULL;
230 free(tape_old_stats);
231 tape_old_stats = NULL;
233 if (tape_new_stats_t != NULL) {
234 free(tape_new_stats_t);
235 tape_new_stats_t = NULL;
237 free(tape_new_stats);
238 tape_new_stats = NULL;
245 tape_old_stats = tape_old_stats_t;
246 tape_new_stats = tape_new_stats_t;
248 for (i = max_tape_drives; i < new_max_tape_drives; i++) {
249 tape_old_stats[i].valid = TAPE_STATS_INVALID;
250 tape_new_stats[i].valid = TAPE_STATS_INVALID;
252 max_tape_drives = new_max_tape_drives;
257 ***************************************************************************
258 * Collect initial statistics for all existing tapes in the system.
259 * This function should be called only once.
260 ***************************************************************************
262 void tape_gather_initial_stats(void)
264 int new_max_tape_drives, i;
266 char filename[MAXPATHLEN + 1];
268 /* Get number of tapes in the system */
269 new_max_tape_drives = get_max_tape_drives();
271 if (new_max_tape_drives == 0) {
273 fprintf(stderr, _("No tape drives with statistics found\n"));
277 /* Allocate structures */
278 if (tape_old_stats == NULL) {
279 tape_old_stats = (struct tape_stats *)
280 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
281 tape_new_stats = (struct tape_stats *)
282 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
283 for (i = 0; i < new_max_tape_drives; i++) {
284 tape_old_stats[i].valid = TAPE_STATS_INVALID;
285 tape_new_stats[i].valid = TAPE_STATS_INVALID;
287 max_tape_drives = new_max_tape_drives;
289 /* This should only be called once */
293 /* Read stats for each tape */
294 for (i = 0; i < max_tape_drives; i++) {
296 * Everything starts out valid but failing to open
297 * a file gets the tape drive marked invalid.
299 tape_new_stats[i].valid = TAPE_STATS_VALID;
300 tape_old_stats[i].valid = TAPE_STATS_VALID;
302 __gettimeofday(&tape_old_stats[i].tv, NULL);
304 tape_new_stats[i].tv.tv_sec = tape_old_stats[i].tv.tv_sec;
305 tape_new_stats[i].tv.tv_usec = tape_old_stats[i].tv.tv_usec;
307 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
308 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
309 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
310 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
311 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
312 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
313 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
314 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
315 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
317 tape_old_stats[i].read_time = 0;
318 tape_old_stats[i].write_time = 0;
319 tape_old_stats[i].other_time = 0;
320 tape_old_stats[i].read_bytes = 0;
321 tape_old_stats[i].write_bytes = 0;
322 tape_old_stats[i].read_count = 0;
323 tape_old_stats[i].write_count = 0;
324 tape_old_stats[i].other_count = 0;
325 tape_old_stats[i].resid_count = 0;
330 ***************************************************************************
331 * Collect a new sample of statistics for all existing tapes in the system.
332 ***************************************************************************
334 void tape_get_updated_stats(void)
338 char filename[MAXPATHLEN + 1] = { 0 };
340 /* Check tapes and realloc structures if needed */
341 tape_check_tapes_and_realloc();
343 for (i = 0; i < max_tape_drives; i++) {
345 * Everything starts out valid but failing
346 * to open a file gets the tape drive marked invalid.
348 tape_new_stats[i].valid = TAPE_STATS_VALID;
349 __gettimeofday(&tape_new_stats[i].tv, NULL);
351 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
352 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
353 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
354 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
355 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
356 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
357 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
358 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
359 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
361 if ((tape_new_stats[i].read_time < tape_old_stats[i].read_time) ||
362 (tape_new_stats[i].write_time < tape_old_stats[i].write_time) ||
363 (tape_new_stats[i].other_time < tape_old_stats[i].other_time)) {
364 tape_new_stats[i].valid = TAPE_STATS_INVALID;
370 ***************************************************************************
371 * Display tapes statistics headings.
372 ***************************************************************************
374 void tape_write_headings(void)
376 printf("Tape: r/s w/s ");
377 if (DISPLAY_MEGABYTES(flags)) {
378 printf("MB_read/s MB_wrtn/s");
380 printf("kB_read/s kB_wrtn/s");
382 printf(" %%Rd %%Wr %%Oa Rs/s Ot/s\n");
386 ***************************************************************************
387 * Calculate statistics for current tape.
390 * @i Index in array for current tape.
393 * @stats Statistics for current tape.
394 ***************************************************************************
396 void tape_calc_one_stats(struct calc_stats *stats, int i)
402 /* Duration in ms done in ms to prevent rounding issues with using seconds */
403 duration = (tape_new_stats[i].tv.tv_sec -
404 tape_old_stats[i].tv.tv_sec) * 1000;
405 duration -= tape_old_stats[i].tv.tv_usec / 1000;
406 duration += tape_new_stats[i].tv.tv_usec / 1000;
408 /* If duration is zero we need to calculate the ms since boot time */
410 fp = fopen(UPTIME, "r");
413 * Get uptime from /proc/uptime and if we can't then just set duration to
414 * be 0 - it will mean that we don't calculate stats.
419 if (fscanf(fp, "%lf", &temp) != 1) {
422 duration = (uint64_t) (temp * 1000);
427 /* The second value passed into the macro is the thing being calculated */
428 CALC_STAT_CNT(read_count, reads_per_second)
429 CALC_STAT_CNT(write_count, writes_per_second)
430 CALC_STAT_CNT(other_count, other_per_second)
431 CALC_STAT_KB(read_bytes, kbytes_read_per_second)
432 CALC_STAT_KB(write_bytes, kbytes_written_per_second)
433 CALC_STAT_PCT(read_time, read_pct_wait)
434 CALC_STAT_PCT(write_time, write_pct_wait)
435 CALC_STAT_PCT(other_time, all_pct_wait)
436 CALC_STAT_CNT(resid_count, resids_per_second)
440 ***************************************************************************
441 * Display statistics for current tape.
444 * @tape Statistics for current tape.
445 * @i Index in array for current tape.
446 ***************************************************************************
448 void tape_write_stats(struct calc_stats *tape, int i)
451 uint64_t divisor = 1;
453 if (DISPLAY_MEGABYTES(flags))
456 sprintf(buffer, "st%i ", i);
458 cprintf_in(IS_STR, "%s", buffer, 0);
459 cprintf_u64(NO_UNIT, 2, 7,
460 tape->reads_per_second,
461 tape->writes_per_second);
462 cprintf_u64(DISPLAY_UNIT(flags) ? UNIT_KILOBYTE : NO_UNIT, 2, 11,
463 DISPLAY_UNIT(flags) ? tape->kbytes_read_per_second
464 : tape->kbytes_read_per_second / divisor,
465 DISPLAY_UNIT(flags) ? tape->kbytes_written_per_second
466 : tape->kbytes_written_per_second / divisor);
467 cprintf_pc(DISPLAY_UNIT(flags), 3, 4, 0,
468 (double) tape->read_pct_wait,
469 (double) tape->write_pct_wait,
470 (double) tape->all_pct_wait);
471 cprintf_u64(NO_UNIT, 2, 7,
472 tape->resids_per_second,
473 tape->other_per_second);
478 ***************************************************************************
479 * Print everything now (stats and uptime).
482 * @rectime Current date and time.
483 ***************************************************************************
485 void write_stats(struct tm *rectime)
488 struct calc_stats tape;
489 struct tape_stats *tmp;
492 TEST_STDOUT(STDOUT_FILENO);
494 /* Print time stamp */
495 if (DISPLAY_TIMESTAMP(flags)) {
496 if (DISPLAY_ISO(flags)) {
497 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
500 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
502 printf("%s\n", timestamp);
505 /* Print the headings */
506 tape_write_headings();
509 * If either new or old is invalid or the I/Os per second is 0 and
510 * zero omit is true then we print nothing.
512 if (max_tape_drives > 0) {
514 for (i = 0; i < max_tape_drives; i++) {
515 if ((tape_new_stats[i].valid == TAPE_STATS_VALID) &&
516 (tape_old_stats[i].valid == TAPE_STATS_VALID)) {
517 tape_calc_one_stats(&tape, i);
518 if (!(DISPLAY_ZERO_OMIT(flags)
519 && (tape.other_per_second == 0)
520 && (tape.reads_per_second == 0)
521 && (tape.writes_per_second == 0)
522 && (tape.kbytes_read_per_second == 0)
523 && (tape.kbytes_written_per_second == 0)
524 && (tape.read_pct_wait == 0)
525 && (tape.write_pct_wait == 0)
526 && (tape.all_pct_wait == 0)
527 && (tape.resids_per_second == 0))) {
528 tape_write_stats(&tape, i);
533 * Swap new and old so next time we compare against the new old stats.
534 * If a new tape drive appears it won't appear in the output until after
535 * the second time we gather information about it.
537 tmp = tape_old_stats;
538 tape_old_stats = tape_new_stats;
539 tape_new_stats = tmp;
545 ***************************************************************************
546 * Main loop: Read tape stats from the relevant sources and display them.
549 * @count Number of lines of stats to print.
550 * @rectime Current date and time.
551 ***************************************************************************
553 void rw_tape_stat_loop(long int count, struct tm *rectime)
555 struct tape_stats *tmp;
558 /* Should we skip first report? */
559 if (DISPLAY_OMIT_SINCE_BOOT(flags) && interval > 0) {
565 if (tape_new_stats == NULL) {
566 tape_gather_initial_stats();
568 tape_get_updated_stats();
572 get_localtime(rectime, 0);
574 /* Check whether we should skip first report */
577 write_stats(rectime);
585 tmp = tape_old_stats;
586 tape_old_stats = tape_new_stats;
587 tape_new_stats = tmp;
598 ***************************************************************************
599 * Main entry to the tapestat program.
600 ***************************************************************************
602 int main(int argc, char **argv)
608 struct utsname header;
612 /* Init National Language Support */
616 /* Init color strings */
619 /* Process args... */
622 if (!strcmp(argv[opt], "--human")) {
627 else if (!strncmp(argv[opt], "-", 1)) {
628 for (i = 1; *(argv[opt] + i); i++) {
630 switch (*(argv[opt] + i)) {
633 if (DISPLAY_MEGABYTES(flags)) {
636 /* Display stats in kB/s */
637 flags |= T_D_KILOBYTES;
641 if (DISPLAY_KILOBYTES(flags)) {
644 /* Display stats in MB/s */
645 flags |= T_D_MEGABYTES;
649 /* Display timestamp */
650 flags |= T_D_TIMESTAMP;
654 /* Don't display stats since system restart */
655 flags |= T_D_OMIT_SINCE_BOOT;
659 /* Omit output for devices with no activity */
660 flags |= T_D_ZERO_OMIT;
664 /* Print version number and exit */
676 interval = atol(argv[opt++]);
685 count = atol(argv[opt++]);
686 if ((count < 1) || !interval) {
702 get_localtime(&rectime, 0);
705 * Don't buffer data if redirected to a pipe.
706 * Note: With musl-c, the behavior of this function is undefined except
707 * when it is the first operation on the stream.
709 setbuf(stdout, NULL);
711 /* Get system name, release number and hostname */
713 if (print_gal_header(&rectime, header.sysname, header.release,
714 header.nodename, header.machine, cpu_nr,
720 /* Set a handler for SIGALRM */
721 memset(&alrm_act, 0, sizeof(alrm_act));
722 alrm_act.sa_handler = alarm_handler;
723 sigaction(SIGALRM, &alrm_act, NULL);
727 rw_tape_stat_loop(count, &rectime);
729 /* Free structures */