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>
39 #include <sys/param.h>
40 #undef HZ /* sys/param.h defines HZ but needed for MAXPATHLEN */
50 #define _(string) gettext(string)
52 #define _(string) (string)
55 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
56 char *sccsid(void) { return (SCCSID); }
58 int cpu_nr = 0; /* Nb of processors on the machine */
59 int flags = 0; /* Flag for common options and system state */
64 struct sigaction alrm_act;
67 * For tape stats - it would be extremely rare for there to be a very large
68 * number of tape drives attached to a system. I wouldn't expect to see more
69 * than 20-30 in a very large configuration and discontinguous ones should
70 * be even more rare. Because of this we keep the old and new data in a
71 * simple data structure with the tape index being the number after the tape
72 * drive, st0 at index 0, etc.
74 int max_tape_drives = 0;
75 struct tape_stats *tape_new_stats = { NULL };
76 struct tape_stats *tape_old_stats = { NULL };
80 ***************************************************************************
81 * Print usage and exit.
84 * @progname Name of sysstat command.
85 ***************************************************************************
87 void usage(char *progname)
89 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
91 fprintf(stderr, _("Options are:\n"
92 "[ -k | -m ] [ -t ] [ -V ] [ -y ] [ -z ]\n"));
97 ***************************************************************************
98 * SIGALRM signal handler. No need to reset the handler here.
101 * @sig Signal number.
102 ***************************************************************************
104 void alarm_handler(int sig)
110 ***************************************************************************
112 ***************************************************************************
114 void tape_initialise(void)
116 /* How many processors on this machine? */
117 cpu_nr = get_cpu_nr(~0, FALSE);
119 /* Compile regular expression for tape names */
120 if (regcomp(&tape_reg, "^st[0-9]+$", REG_EXTENDED) != 0) {
126 ***************************************************************************
128 ***************************************************************************
130 void tape_uninitialise(void)
133 if (tape_old_stats != NULL) {
134 free(tape_old_stats);
136 if (tape_new_stats != NULL) {
137 free(tape_new_stats);
142 ***************************************************************************
143 * Get maximum number of tapes in the system.
146 * Number of tapes found.
147 ***************************************************************************
149 int get_max_tape_drives(void)
152 struct dirent *entry;
153 int new_max_tape_drives, tmp, num_stats_dir = 0;
155 char stats_dir[MAXPATHLEN + 1];
156 struct stat stat_buf;
158 new_max_tape_drives = max_tape_drives;
160 /* Open sysfs tree */
161 dir = opendir(SYSFS_CLASS_TAPE_DIR);
165 while ((entry = readdir(dir)) != NULL) {
166 if (regexec(&tape_reg, &entry->d_name[0], 1, &match, 0) == 0) {
167 /* d_name[2] to skip the st at the front */
168 tmp = atoi(&entry->d_name[2]) + 1;
169 if (tmp > new_max_tape_drives) {
170 new_max_tape_drives = tmp;
173 snprintf(stats_dir, MAXPATHLEN, "%s/%s/%s",
174 SYSFS_CLASS_TAPE_DIR, &entry->d_name[0], "stats");
175 if (stat(stats_dir, &stat_buf) == 0) {
176 if (S_ISDIR(stat_buf.st_mode)) {
183 /* If there are no stats directories make the new number of tape drives 0 */
184 if (num_stats_dir == 0) {
185 new_max_tape_drives = 0;
188 return new_max_tape_drives;
192 ***************************************************************************
193 * Check if new tapes have been added and reallocate structures accordingly.
194 ***************************************************************************
196 void tape_check_tapes_and_realloc(void)
198 int new_max_tape_drives, i;
200 /* Count again number of tapes */
201 new_max_tape_drives = get_max_tape_drives();
203 if (new_max_tape_drives > max_tape_drives) {
204 /* New tapes found: Realloc structures */
205 struct tape_stats *tape_old_stats_t = (struct tape_stats *)
206 realloc(tape_old_stats, sizeof(struct tape_stats) * new_max_tape_drives);
207 struct tape_stats *tape_new_stats_t = (struct tape_stats *)
208 realloc(tape_new_stats, sizeof(struct tape_stats) * new_max_tape_drives);
209 if ((tape_old_stats_t == NULL) || (tape_new_stats_t == NULL)) {
210 if (tape_old_stats_t != NULL) {
211 free(tape_old_stats_t);
212 tape_old_stats_t = NULL;
214 if (tape_new_stats_t != NULL) {
215 free(tape_new_stats_t);
216 tape_new_stats_t = NULL;
218 free(tape_old_stats);
219 tape_old_stats = NULL;
220 free(tape_new_stats);
221 tape_new_stats = NULL;
227 for (i = max_tape_drives; i < new_max_tape_drives; i++) {
228 tape_old_stats[i].valid = TAPE_STATS_INVALID;
229 tape_new_stats[i].valid = TAPE_STATS_INVALID;
231 max_tape_drives = new_max_tape_drives;
236 ***************************************************************************
237 * Collect initial statistics for all existing tapes in the system.
238 * This function should be called only once.
239 ***************************************************************************
241 void tape_gather_initial_stats(void)
243 int new_max_tape_drives, i;
245 char filename[MAXPATHLEN + 1];
247 /* Get number of tapes in the system */
248 new_max_tape_drives = get_max_tape_drives();
250 if (new_max_tape_drives == 0) {
252 fprintf(stderr, _("No tape drives with statistics found\n"));
256 /* Allocate structures */
257 if (tape_old_stats == NULL) {
258 tape_old_stats = (struct tape_stats *)
259 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
260 tape_new_stats = (struct tape_stats *)
261 malloc(sizeof(struct tape_stats) * new_max_tape_drives);
262 for (i = 0; i < new_max_tape_drives; i++) {
263 tape_old_stats[i].valid = TAPE_STATS_INVALID;
264 tape_new_stats[i].valid = TAPE_STATS_INVALID;
266 max_tape_drives = new_max_tape_drives;
268 /* This should only be called once */
272 /* Read stats for each tape */
273 for (i = 0; i < max_tape_drives; i++) {
275 * Everything starts out valid but failing to open
276 * a file gets the tape drive marked invalid.
278 tape_new_stats[i].valid = TAPE_STATS_VALID;
279 tape_old_stats[i].valid = TAPE_STATS_VALID;
281 gettimeofday(&tape_old_stats[i].tv, NULL);
283 tape_new_stats[i].tv.tv_sec = tape_old_stats[i].tv.tv_sec;
284 tape_new_stats[i].tv.tv_usec = tape_old_stats[i].tv.tv_usec;
286 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
287 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
288 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
289 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
290 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
291 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
292 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
293 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
294 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
296 tape_old_stats[i].read_time = 0;
297 tape_old_stats[i].write_time = 0;
298 tape_old_stats[i].other_time = 0;
299 tape_old_stats[i].read_bytes = 0;
300 tape_old_stats[i].write_bytes = 0;
301 tape_old_stats[i].read_count = 0;
302 tape_old_stats[i].write_count = 0;
303 tape_old_stats[i].other_count = 0;
304 tape_old_stats[i].resid_count = 0;
309 ***************************************************************************
310 * Collect a new sample of statistics for all existing tapes in the system.
311 ***************************************************************************
313 void tape_get_updated_stats(void)
317 char filename[MAXPATHLEN + 1] = { 0 };
319 /* Check tapes and realloc structures if needed */
320 tape_check_tapes_and_realloc();
322 for (i = 0; i < max_tape_drives; i++) {
324 * Everything starts out valid but failing
325 * to open a file gets the tape drive marked invalid.
327 tape_new_stats[i].valid = TAPE_STATS_VALID;
328 gettimeofday(&tape_new_stats[i].tv, NULL);
330 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_ns", read_time)
331 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_ns", write_time)
332 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "io_ns", other_time)
333 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_byte_cnt", read_bytes)
334 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_byte_cnt", write_bytes)
335 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "read_cnt", read_count)
336 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "write_cnt", write_count)
337 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "other_cnt", other_count)
338 TAPE_STAT_FILE_VAL(TAPE_STAT_PATH "resid_cnt", resid_count)
340 if ((tape_new_stats[i].read_time < tape_old_stats[i].read_time) ||
341 (tape_new_stats[i].write_time < tape_old_stats[i].write_time) ||
342 (tape_new_stats[i].other_time < tape_old_stats[i].other_time)) {
343 tape_new_stats[i].valid = TAPE_STATS_INVALID;
349 ***************************************************************************
350 * Display tapes statistics headings.
351 ***************************************************************************
353 void tape_write_headings(void)
355 printf("Tape: r/s w/s ");
356 if (DISPLAY_MEGABYTES(flags)) {
357 printf("MB_read/s MB_wrtn/s");
359 printf("kB_read/s kB_wrtn/s");
361 printf(" %%Rd %%Wr %%Oa Rs/s Ot/s\n");
365 ***************************************************************************
366 * Calculate statistics for current tape.
369 * @i Index in array for current tape.
372 * @stats Statistics for current tape.
373 ***************************************************************************
375 void tape_calc_one_stats(struct calc_stats *stats, int i)
381 /* Duration in ms done in ms to prevent rounding issues with using seconds */
382 duration = (tape_new_stats[i].tv.tv_sec -
383 tape_old_stats[i].tv.tv_sec) * 1000;
384 duration -= tape_old_stats[i].tv.tv_usec / 1000;
385 duration += tape_new_stats[i].tv.tv_usec / 1000;
387 /* If duration is zero we need to calculate the ms since boot time */
389 fp = fopen("/proc/uptime", "r");
392 * Get uptime from /proc/uptime and if we can't then just set duration to
393 * be 0 - it will mean that we don't calculate stats.
398 if (fscanf(fp, "%lf", &temp) != 1) {
401 duration = (uint64_t) (temp * 1000);
406 /* The second value passed into the macro is the thing being calculated */
407 CALC_STAT_CNT(read_count, reads_per_second)
408 CALC_STAT_CNT(write_count, writes_per_second)
409 CALC_STAT_CNT(other_count, other_per_second)
410 CALC_STAT_KB(read_bytes, kbytes_read_per_second)
411 CALC_STAT_KB(write_bytes, kbytes_written_per_second)
412 CALC_STAT_PCT(read_time, read_pct_wait)
413 CALC_STAT_PCT(write_time, write_pct_wait)
414 CALC_STAT_PCT(other_time, all_pct_wait)
415 CALC_STAT_CNT(resid_count, resids_per_second)
419 ***************************************************************************
420 * Display statistics for current tape.
423 * @tape Statistics for current tape.
424 * @i Index in array for current tape.
425 ***************************************************************************
427 void tape_write_stats(struct calc_stats *tape, int i)
430 uint64_t divisor = 1;
432 if (DISPLAY_MEGABYTES(flags))
435 sprintf(buffer, "st%i ", i);
437 cprintf_in(IS_STR, "%s", buffer, 0);
439 tape->reads_per_second,
440 tape->writes_per_second);
442 tape->kbytes_read_per_second / divisor,
443 tape->kbytes_written_per_second / divisor);
445 (double) tape->read_pct_wait,
446 (double) tape->write_pct_wait,
447 (double) tape->all_pct_wait);
449 tape->resids_per_second,
450 tape->other_per_second);
455 ***************************************************************************
456 * Print everything now (stats and uptime).
459 * @rectime Current date and time.
460 ***************************************************************************
462 void write_stats(struct tm *rectime)
465 struct calc_stats tape;
466 struct tape_stats *tmp;
469 TEST_STDOUT(STDOUT_FILENO);
471 /* Print time stamp */
472 if (DISPLAY_TIMESTAMP(flags)) {
473 if (DISPLAY_ISO(flags)) {
474 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
477 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
479 printf("%s\n", timestamp);
482 /* Print the headings */
483 tape_write_headings();
486 * If either new or old is invalid or the I/Os per second is 0 and
487 * zero omit is true then we print nothing.
489 if (max_tape_drives > 0) {
491 for (i = 0; i < max_tape_drives; i++) {
492 if ((tape_new_stats[i].valid == TAPE_STATS_VALID) &&
493 (tape_old_stats[i].valid == TAPE_STATS_VALID)) {
494 tape_calc_one_stats(&tape, i);
495 if (!(DISPLAY_ZERO_OMIT(flags)
496 && (tape.other_per_second == 0)
497 && (tape.reads_per_second == 0)
498 && (tape.writes_per_second == 0)
499 && (tape.kbytes_read_per_second == 0)
500 && (tape.kbytes_written_per_second == 0)
501 && (tape.read_pct_wait == 0)
502 && (tape.write_pct_wait == 0)
503 && (tape.all_pct_wait == 0)
504 && (tape.resids_per_second == 0))) {
505 tape_write_stats(&tape, i);
510 * Swap new and old so next time we compare against the new old stats.
511 * If a new tape drive appears it won't appear in the output until after
512 * the second time we gather information about it.
514 tmp = tape_old_stats;
515 tape_old_stats = tape_new_stats;
516 tape_new_stats = tmp;
522 ***************************************************************************
523 * Main loop: Read tape stats from the relevant sources and display them.
526 * @count Number of lines of stats to print.
527 * @rectime Current date and time.
528 ***************************************************************************
530 void rw_tape_stat_loop(long int count, struct tm *rectime)
532 struct tape_stats *tmp;
535 /* Should we skip first report? */
536 if (DISPLAY_OMIT_SINCE_BOOT(flags) && interval > 0) {
540 /* Don't buffer data if redirected to a pipe */
541 setbuf(stdout, NULL);
545 if (tape_new_stats == NULL) {
546 tape_gather_initial_stats();
548 tape_get_updated_stats();
552 get_localtime(rectime, 0);
554 /* Check whether we should skip first report */
557 write_stats(rectime);
565 tmp = tape_old_stats;
566 tape_old_stats = tape_new_stats;
567 tape_new_stats = tmp;
578 ***************************************************************************
579 * Main entry to the tapestat program.
580 ***************************************************************************
582 int main(int argc, char **argv)
588 struct utsname header;
592 /* Init National Language Support */
596 /* Init color strings */
602 /* Process args... */
604 if (!strncmp(argv[opt], "-", 1)) {
605 for (i = 1; *(argv[opt] + i); i++) {
607 switch (*(argv[opt] + i)) {
610 if (DISPLAY_MEGABYTES(flags)) {
613 /* Display stats in kB/s */
614 flags |= T_D_KILOBYTES;
618 if (DISPLAY_KILOBYTES(flags)) {
621 /* Display stats in MB/s */
622 flags |= T_D_MEGABYTES;
626 /* Display timestamp */
627 flags |= T_D_TIMESTAMP;
631 /* Don't display stats since system restart */
632 flags |= T_D_OMIT_SINCE_BOOT;
636 /* Omit output for devices with no activity */
637 flags |= T_D_ZERO_OMIT;
641 /* Print version number and exit */
653 interval = atol(argv[opt++]);
662 count = atol(argv[opt++]);
663 if ((count < 1) || !interval) {
679 get_localtime(&rectime, 0);
681 /* Get system name, release number and hostname */
683 if (print_gal_header(&rectime, header.sysname, header.release,
684 header.nodename, header.machine, cpu_nr)) {
689 /* Set a handler for SIGALRM */
690 memset(&alrm_act, 0, sizeof(alrm_act));
691 alrm_act.sa_handler = alarm_handler;
692 sigaction(SIGALRM, &alrm_act, NULL);
696 rw_tape_stat_loop(count, &rectime);
698 /* Free structures */