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) {
181 if (regexec(&tape_reg, &entry->d_name[0], 1, &match, 0) == 0) {
182 /* d_name[2] to skip the st at the front */
183 tmp = atoi(&entry->d_name[2]) + 1;
184 if (tmp > new_max_tape_drives) {
185 new_max_tape_drives = tmp;
188 snprintf(stats_dir, MAXPATHLEN, "%s/%s/%s",
189 SYSFS_CLASS_TAPE_DIR, &entry->d_name[0], "stats");
190 if (stat(stats_dir, &stat_buf) == 0) {
191 if (S_ISDIR(stat_buf.st_mode)) {
198 /* If there are no stats directories make the new number of tape drives 0 */
199 if (num_stats_dir == 0) {
200 new_max_tape_drives = 0;
203 return new_max_tape_drives;
207 ***************************************************************************
208 * Check if new tapes have been added and reallocate structures accordingly.
209 ***************************************************************************
211 void tape_check_tapes_and_realloc(void)
213 int new_max_tape_drives, i;
215 /* Count again number of tapes */
216 new_max_tape_drives = get_max_tape_drives();
218 if (new_max_tape_drives > max_tape_drives && new_max_tape_drives > 0) {
219 /* New tapes found: Realloc structures */
220 struct tape_stats *tape_old_stats_t = (struct tape_stats *)
221 realloc(tape_old_stats, sizeof(struct tape_stats) * new_max_tape_drives);
222 struct tape_stats *tape_new_stats_t = (struct tape_stats *)
223 realloc(tape_new_stats, sizeof(struct tape_stats) * new_max_tape_drives);
224 if ((tape_old_stats_t == NULL) || (tape_new_stats_t == NULL)) {
225 if (tape_old_stats_t != NULL) {
226 free(tape_old_stats_t);
227 tape_old_stats_t = NULL;
229 free(tape_old_stats);
230 tape_old_stats = NULL;
232 if (tape_new_stats_t != NULL) {
233 free(tape_new_stats_t);
234 tape_new_stats_t = NULL;
236 free(tape_new_stats);
237 tape_new_stats = NULL;
244 tape_old_stats = tape_old_stats_t;
245 tape_new_stats = tape_new_stats_t;
247 for (i = max_tape_drives; i < new_max_tape_drives; i++) {
248 tape_old_stats[i].valid = TAPE_STATS_INVALID;
249 tape_new_stats[i].valid = TAPE_STATS_INVALID;
251 max_tape_drives = new_max_tape_drives;
256 ***************************************************************************
257 * Collect initial statistics for all existing tapes in the system.
258 * This function should be called only once.
259 ***************************************************************************
261 void tape_gather_initial_stats(void)
263 int new_max_tape_drives, i;
265 char filename[MAXPATHLEN + 1];
267 /* Get number of tapes in the system */
268 new_max_tape_drives = get_max_tape_drives();
270 if (new_max_tape_drives == 0) {
272 fprintf(stderr, _("No tape drives with statistics found\n"));
276 /* Allocate structures */
277 if (tape_old_stats == NULL) {
278 tape_old_stats = (struct tape_stats *)
279 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
280 tape_new_stats = (struct tape_stats *)
281 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
282 for (i = 0; i < new_max_tape_drives; i++) {
283 tape_old_stats[i].valid = TAPE_STATS_INVALID;
284 tape_new_stats[i].valid = TAPE_STATS_INVALID;
286 max_tape_drives = new_max_tape_drives;
288 /* This should only be called once */
292 /* Read stats for each tape */
293 for (i = 0; i < max_tape_drives; i++) {
295 * Everything starts out valid but failing to open
296 * a file gets the tape drive marked invalid.
298 tape_new_stats[i].valid = TAPE_STATS_VALID;
299 tape_old_stats[i].valid = TAPE_STATS_VALID;
301 __gettimeofday(&tape_old_stats[i].tv, NULL);
303 tape_new_stats[i].tv.tv_sec = tape_old_stats[i].tv.tv_sec;
304 tape_new_stats[i].tv.tv_usec = tape_old_stats[i].tv.tv_usec;
306 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
307 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
308 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
309 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
310 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
311 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
312 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
313 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
314 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
316 tape_old_stats[i].read_time = 0;
317 tape_old_stats[i].write_time = 0;
318 tape_old_stats[i].other_time = 0;
319 tape_old_stats[i].read_bytes = 0;
320 tape_old_stats[i].write_bytes = 0;
321 tape_old_stats[i].read_count = 0;
322 tape_old_stats[i].write_count = 0;
323 tape_old_stats[i].other_count = 0;
324 tape_old_stats[i].resid_count = 0;
329 ***************************************************************************
330 * Collect a new sample of statistics for all existing tapes in the system.
331 ***************************************************************************
333 void tape_get_updated_stats(void)
337 char filename[MAXPATHLEN + 1] = { 0 };
339 /* Check tapes and realloc structures if needed */
340 tape_check_tapes_and_realloc();
342 for (i = 0; i < max_tape_drives; i++) {
344 * Everything starts out valid but failing
345 * to open a file gets the tape drive marked invalid.
347 tape_new_stats[i].valid = TAPE_STATS_VALID;
348 __gettimeofday(&tape_new_stats[i].tv, NULL);
350 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
351 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
352 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
353 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
354 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
355 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
356 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
357 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
358 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
360 if ((tape_new_stats[i].read_time < tape_old_stats[i].read_time) ||
361 (tape_new_stats[i].write_time < tape_old_stats[i].write_time) ||
362 (tape_new_stats[i].other_time < tape_old_stats[i].other_time)) {
363 tape_new_stats[i].valid = TAPE_STATS_INVALID;
369 ***************************************************************************
370 * Display tapes statistics headings.
371 ***************************************************************************
373 void tape_write_headings(void)
375 printf("Tape: r/s w/s ");
376 if (DISPLAY_MEGABYTES(flags)) {
377 printf("MB_read/s MB_wrtn/s");
379 printf("kB_read/s kB_wrtn/s");
381 printf(" %%Rd %%Wr %%Oa Rs/s Ot/s\n");
385 ***************************************************************************
386 * Calculate statistics for current tape.
389 * @i Index in array for current tape.
392 * @stats Statistics for current tape.
393 ***************************************************************************
395 void tape_calc_one_stats(struct calc_stats *stats, int i)
401 /* Duration in ms done in ms to prevent rounding issues with using seconds */
402 duration = (tape_new_stats[i].tv.tv_sec -
403 tape_old_stats[i].tv.tv_sec) * 1000;
404 duration -= tape_old_stats[i].tv.tv_usec / 1000;
405 duration += tape_new_stats[i].tv.tv_usec / 1000;
407 /* If duration is zero we need to calculate the ms since boot time */
409 fp = fopen(UPTIME, "r");
412 * Get uptime from /proc/uptime and if we can't then just set duration to
413 * be 0 - it will mean that we don't calculate stats.
418 if (fscanf(fp, "%lf", &temp) != 1) {
421 duration = (uint64_t) (temp * 1000);
426 /* The second value passed into the macro is the thing being calculated */
427 CALC_STAT_CNT(read_count, reads_per_second)
428 CALC_STAT_CNT(write_count, writes_per_second)
429 CALC_STAT_CNT(other_count, other_per_second)
430 CALC_STAT_KB(read_bytes, kbytes_read_per_second)
431 CALC_STAT_KB(write_bytes, kbytes_written_per_second)
432 CALC_STAT_PCT(read_time, read_pct_wait)
433 CALC_STAT_PCT(write_time, write_pct_wait)
434 CALC_STAT_PCT(other_time, all_pct_wait)
435 CALC_STAT_CNT(resid_count, resids_per_second)
439 ***************************************************************************
440 * Display statistics for current tape.
443 * @tape Statistics for current tape.
444 * @i Index in array for current tape.
445 ***************************************************************************
447 void tape_write_stats(struct calc_stats *tape, int i)
450 uint64_t divisor = 1;
452 if (DISPLAY_MEGABYTES(flags))
455 sprintf(buffer, "st%i ", i);
457 cprintf_in(IS_STR, "%s", buffer, 0);
458 cprintf_u64(NO_UNIT, 2, 7,
459 tape->reads_per_second,
460 tape->writes_per_second);
461 cprintf_u64(DISPLAY_UNIT(flags) ? UNIT_KILOBYTE : NO_UNIT, 2, 11,
462 DISPLAY_UNIT(flags) ? tape->kbytes_read_per_second
463 : tape->kbytes_read_per_second / divisor,
464 DISPLAY_UNIT(flags) ? tape->kbytes_written_per_second
465 : tape->kbytes_written_per_second / divisor);
466 cprintf_pc(DISPLAY_UNIT(flags), 3, 4, 0,
467 (double) tape->read_pct_wait,
468 (double) tape->write_pct_wait,
469 (double) tape->all_pct_wait);
470 cprintf_u64(NO_UNIT, 2, 7,
471 tape->resids_per_second,
472 tape->other_per_second);
477 ***************************************************************************
478 * Print everything now (stats and uptime).
481 * @rectime Current date and time.
482 ***************************************************************************
484 void write_stats(struct tm *rectime)
487 struct calc_stats tape;
488 struct tape_stats *tmp;
491 TEST_STDOUT(STDOUT_FILENO);
493 /* Print time stamp */
494 if (DISPLAY_TIMESTAMP(flags)) {
495 if (DISPLAY_ISO(flags)) {
496 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
499 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
501 printf("%s\n", timestamp);
504 /* Print the headings */
505 tape_write_headings();
508 * If either new or old is invalid or the I/Os per second is 0 and
509 * zero omit is true then we print nothing.
511 if (max_tape_drives > 0) {
513 for (i = 0; i < max_tape_drives; i++) {
514 if ((tape_new_stats[i].valid == TAPE_STATS_VALID) &&
515 (tape_old_stats[i].valid == TAPE_STATS_VALID)) {
516 tape_calc_one_stats(&tape, i);
517 if (!(DISPLAY_ZERO_OMIT(flags)
518 && (tape.other_per_second == 0)
519 && (tape.reads_per_second == 0)
520 && (tape.writes_per_second == 0)
521 && (tape.kbytes_read_per_second == 0)
522 && (tape.kbytes_written_per_second == 0)
523 && (tape.read_pct_wait == 0)
524 && (tape.write_pct_wait == 0)
525 && (tape.all_pct_wait == 0)
526 && (tape.resids_per_second == 0))) {
527 tape_write_stats(&tape, i);
532 * Swap new and old so next time we compare against the new old stats.
533 * If a new tape drive appears it won't appear in the output until after
534 * the second time we gather information about it.
536 tmp = tape_old_stats;
537 tape_old_stats = tape_new_stats;
538 tape_new_stats = tmp;
544 ***************************************************************************
545 * Main loop: Read tape stats from the relevant sources and display them.
548 * @count Number of lines of stats to print.
549 * @rectime Current date and time.
550 ***************************************************************************
552 void rw_tape_stat_loop(long int count, struct tm *rectime)
554 struct tape_stats *tmp;
557 /* Should we skip first report? */
558 if (DISPLAY_OMIT_SINCE_BOOT(flags) && interval > 0) {
564 if (tape_new_stats == NULL) {
565 tape_gather_initial_stats();
567 tape_get_updated_stats();
571 get_localtime(rectime, 0);
573 /* Check whether we should skip first report */
576 write_stats(rectime);
584 tmp = tape_old_stats;
585 tape_old_stats = tape_new_stats;
586 tape_new_stats = tmp;
597 ***************************************************************************
598 * Main entry to the tapestat program.
599 ***************************************************************************
601 int main(int argc, char **argv)
607 struct utsname header;
611 /* Init National Language Support */
615 /* Init color strings */
618 /* Process args... */
621 if (!strcmp(argv[opt], "--human")) {
626 else if (!strncmp(argv[opt], "-", 1)) {
627 for (i = 1; *(argv[opt] + i); i++) {
629 switch (*(argv[opt] + i)) {
632 if (DISPLAY_MEGABYTES(flags)) {
635 /* Display stats in kB/s */
636 flags |= T_D_KILOBYTES;
640 if (DISPLAY_KILOBYTES(flags)) {
643 /* Display stats in MB/s */
644 flags |= T_D_MEGABYTES;
648 /* Display timestamp */
649 flags |= T_D_TIMESTAMP;
653 /* Don't display stats since system restart */
654 flags |= T_D_OMIT_SINCE_BOOT;
658 /* Omit output for devices with no activity */
659 flags |= T_D_ZERO_OMIT;
663 /* Print version number and exit */
675 interval = atol(argv[opt++]);
684 count = atol(argv[opt++]);
685 if ((count < 1) || !interval) {
701 get_localtime(&rectime, 0);
704 * Don't buffer data if redirected to a pipe.
705 * Note: With musl-c, the behavior of this function is undefined except
706 * when it is the first operation on the stream.
708 setbuf(stdout, NULL);
710 /* Get system name, release number and hostname */
712 if (print_gal_header(&rectime, header.sysname, header.release,
713 header.nodename, header.machine, cpu_nr,
719 /* Set a handler for SIGALRM */
720 memset(&alrm_act, 0, sizeof(alrm_act));
721 alrm_act.sa_handler = alarm_handler;
722 sigaction(SIGALRM, &alrm_act, NULL);
726 rw_tape_stat_loop(count, &rectime);
728 /* Free structures */