2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: tapetype.c,v 1.27 2006/08/24 01:57:16 paddy_s Exp $
30 * tests a tape in a given tape unit and prints a tapetype entry for
36 #define NBLOCKS 32 /* number of random blocks */
40 static char *sProgName;
44 static size_t blockkb = 32;
45 static size_t blocksize;
47 static char *randombytes = (char *) NULL;
50 /* If the C library does not define random(), try to use rand() by
51 defining USE_RAND, but then make sure you are not using hardware
52 compression, because the low-order bits of rand() may not be that
54 #define random() rand()
55 #define srandom(seed) srand(seed)
58 static char *getrandombytes(void);
59 static int writeblock(int fd);
60 static off_t writeblocks(int fd, off_t nblks);
61 static void allocrandombytes(void);
62 static void do_pass0(off_t size, time_t *seconds, int dorewind);
63 static void do_pass(off_t size, off_t *blocks, off_t *files, time_t *seconds);
64 static void help(void);
65 static void initnotrandombytes(void);
66 static void initrandombytes(void);
67 static void show_progress(off_t *blocks, off_t *files);
68 static void usage(void);
70 int main(int argc, char **argv);
73 allocrandombytes(void)
80 if (randombytes == (char *)NULL) {
81 #if defined(HAVE_GETPAGESIZE)
82 page_size = (size_t)getpagesize();
84 page_size = (size_t)1024;
86 j = (NBLOCKS * blocksize) + page_size; /* buffer space plus one page */
87 j = am_round(j, page_size); /* even number of pages */
89 i = (size_t)(p - (char *)0) & (page_size - 1);/* page boundary offset */
91 randombytes = p + (page_size - i); /* round up to page boundary */
93 randombytes = p; /* alloc already on boundary */
99 initnotrandombytes(void)
106 j = NBLOCKS * blocksize;
108 for(i=0; i < j; ++i) {
109 *p++ = (char) (i % 256);
114 initrandombytes(void)
120 j = NBLOCKS * blocksize;
122 for(i=0; i < j; ++i) {
123 *p++ = (char)random();
130 static unsigned long counter = 0;
132 return randombytes + ((counter++ % NBLOCKS) * blocksize);
135 static int short_write;
143 if ((w = tapefd_write(fd, getrandombytes(), blocksize)) == (ssize_t)blocksize) {
155 /* returns number of blocks actually written */
161 off_t blks = (off_t)0;
163 while (blks < nblks) {
164 if (! writeblock(fd)) {
177 fputs(_("usage: "), stderr);
178 fputs(sProgName, stderr);
179 fputs(_(" [-h]"), stderr);
180 fputs(_(" [-c]"), stderr);
181 fputs(_(" [-o]"), stderr);
182 fputs(_(" [-b blocksize]"), stderr);
183 fputs(_(" -e estsize"), stderr);
184 fputs(_(" [-f tapedev]"), stderr);
185 fputs(_(" [-t typename]"), stderr);
193 fputs(_("-h display this message\n"
194 "-c run hardware compression detection test only\n"
195 "-o overwrite amanda tape\n"
196 "-b blocksize record block size (default: 32k)\n"
197 "-e estsize estimated tape size (No default!)\n"
198 "-f tapedev tape device name (default: $TAPE)\n"
199 "-t typename tapetype name (default: unknown-tapetype)\n"
201 "Note: disable hardware compression when running this program.\n"),
214 plural(_("wrote %lld %zu Kb block"),
215 _("wrote %lld %zu Kb blocks"),
220 plural(_(" in %lld file"),
238 if (tapefd_rewind(fd) == -1) {
239 g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),
240 sProgName, tapedev, strerror(errno));
243 if (((-1 == tapefd_close(fd)) ||
244 (-1 == (fd = tape_open(tapedev, O_RDWR))))) {
245 g_fprintf(stderr, "%s: could not re-open %s: %s\n",
246 sProgName, tapedev, strerror(errno));
254 if ((blks = writeblocks(fd, size)) <= (off_t)0 || tapefd_weof(fd, (off_t)1) != 0)
260 show_progress(blocks, files);
267 if (*blocks == (off_t)0) {
268 g_fprintf(stderr, _("%s: could not write any data in this pass: %s\n"),
269 sProgName, short_write ? _("short write") : strerror(save_errno));
275 * Just in case time warped backward or the device is really, really
276 * fast (e.g. /dev/null testing).
280 *seconds = end - start;
285 show_progress(blocks, files);
287 plural(_(" in %jd second (%s)\n"),
288 _(" in %jd seconds (%s)\n"),
291 short_write ? _("short write") : strerror(save_errno));
305 if (dorewind && tapefd_rewind(fd) == -1) {
306 g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),
307 sProgName, tapedev, strerror(errno));
312 ((-1 == tapefd_close(fd)) ||
313 (-1 == (fd = tape_open(tapedev, O_RDWR))))) {
314 g_fprintf(stderr, "%s: could not re-open %s: %s\n",
315 sProgName, tapedev, strerror(errno));
322 blks = writeblocks(fd, size);
323 tapefd_weof(fd, (off_t)1);
329 if (blks <= (off_t)0) {
330 g_fprintf(stderr, _("%s: could not write any data in this pass: %s\n"),
331 sProgName, short_write ? _("short write") : strerror(save_errno));
337 * Just in case time warped backward or the device is really, really
338 * fast (e.g. /dev/null testing).
342 *seconds = end - start;
352 off_t pass1blocks = (off_t)0;
353 off_t pass2blocks = (off_t)0;
357 off_t pass1files = (off_t)0;
358 off_t pass2files = (off_t)0;
374 int comprtstonly = 0;
375 int overwrite_label = 0;
378 char *datestamp = NULL;
382 * Configure program for internationalization:
383 * 1) Only set the message locale for now.
384 * 2) Set textdomain for all amanda related programs to "amanda"
385 * We don't want to be forced to support dozens of message catalogs.
387 setlocale(LC_MESSAGES, "C");
388 textdomain("amanda");
390 if ((sProgName = strrchr(*argv, '/')) == NULL) {
396 /* Don't die when child closes pipe */
397 signal(SIGPIPE, SIG_IGN);
400 tapedev = getenv("TAPE");
401 typename = "unknown-tapetype";
403 while ((ch = getopt(argc, argv, "b:e:f:t:hco")) != EOF) {
406 blockkb = (size_t)strtol(optarg, &suffix, 0);
407 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
408 if (*suffix == 'm' || *suffix == 'M') {
410 } else if (*suffix == 'g' || *suffix == 'G') {
411 blockkb *= (1024 * 1024);
413 g_fprintf(stderr, _("%s: unknown size suffix \'%c\'\n"),
420 estsize = OFF_T_STRTOL(optarg, &suffix, 0);
421 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
422 if (*suffix == 'm' || *suffix == 'M') {
423 estsize *= (off_t)1024;
424 } else if (*suffix == 'g' || *suffix == 'G') {
425 estsize *= (off_t)(1024 * 1024);
427 g_fprintf(stderr, _("%s: unknown size suffix \'%c\'\n"),
435 tapedev = stralloc(optarg);
439 typename = stralloc(optarg);
455 g_fprintf(stderr, _("%s: unknown option \'%c\'\n"), sProgName, ch);
463 blocksize = blockkb * 1024;
465 if (tapedev == NULL) {
466 g_fprintf(stderr, _("%s: No tapedev specified\n"), sProgName);
477 estsize = (off_t)(1024 * 1024); /* assume 1 GByte for now */
479 g_fprintf(stderr, _("%s: please specify estimated tape capacity (e.g. '-e 4g')\n"), sProgName);
489 fd = tape_open(tapedev, O_RDONLY, 0);
491 g_fprintf(stderr, _("%s: could not open %s: %s\n"),
492 sProgName, tapedev, strerror(errno));
496 if((result = tapefd_rdlabel(fd, &datestamp, &label)) == NULL) {
499 else if (strcmp(result,_("not an amanda tape")) == 0) {
503 if(tapefd_rewind(fd) == -1) {
504 g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),
505 sProgName, tapedev, strerror(errno));
512 if(is_labeled == 1 && overwrite_label == 0) {
513 g_fprintf(stderr, _("%s: The tape is an amanda tape, use -o to overwrite the tape\n"),
517 else if(is_labeled == 2 && overwrite_label == 0) {
518 g_fprintf(stderr, _("%s: The tape is already used, use -o to overwrite the tape\n"),
523 fd = tape_open(tapedev, O_RDWR, 0);
525 g_fprintf(stderr, _("%s: could not open %s: %s\n"),
526 sProgName, tapedev, strerror(errno));
530 do_tty = isatty(fileno(stderr));
533 * Estimate pass: write twice a small file, once with compressable
534 * data and once with uncompressable data.
535 * The theory is that if the drive is in hardware compression mode
536 * we notice a significant difference in writing speed between the two
537 * (at least if we can provide data as fast the tape streams).
540 initnotrandombytes();
542 g_fprintf(stderr, _("Estimate phase 1..."));
543 pass0size = (off_t)(8 * 1024 / blockkb);
547 * To get accurate results, we should write enough data
548 * so that rewind/start/stop time is small compared to
549 * the total time; let's take 10%.
550 * The timer has a 1 sec granularity, so the test
551 * should take at least 10 seconds to measure a
552 * difference with 10% accuracy; let's take 25 seconds.
554 while (pass1time < 25 || ((100*(pass2time-pass1time)/pass2time) >= 10) ) {
555 if (pass1time != 0) {
556 time_t t = pass1time;
558 pass0size *= (off_t)2;
563 * first a dummy pass to rewind, stop, start and
564 * get drive streaming, then do the real timing
566 do_pass0(pass0size, &pass2time, 1);
567 do_pass0(pass0size, &pass1time, 0);
568 if (pass0size >= (off_t)(10 * 1024 * 1024)) {
569 g_fprintf(stderr, "\r");
571 _("Tape device is too fast to detect hardware compression...\n"));
572 break; /* avoid loops if tape is superfast or broken */
575 g_fprintf(stderr, "\r");
577 _("Writing %lld Mbyte compresseable data: %jd sec\n"),
578 (long long)((off_t)blockkb * pass0size / (off_t)1024),
579 (intmax_t)pass1time);
582 * now generate uncompressable data and try again
585 srandom((unsigned)now);
588 g_fprintf(stderr, _("Estimate phase 2..."));
589 do_pass0(pass0size, &pass2time, 1); /* rewind and get drive streaming */
590 do_pass0(pass0size, &pass2time, 0);
591 g_fprintf(stderr, "\r");
592 g_fprintf(stderr, _("Writing %lld Mbyte uncompresseable data: %jd sec\n"),
593 (long long)((off_t)blockkb * pass0size / (off_t)1024),
594 (intmax_t)pass2time);
597 * Compute the time difference between writing the compressable and
598 * uncompressable data. If it differs more than 20%, then warn
599 * user that the tape drive has probably hardware compression enabled.
601 if (pass1time > pass2time) {
603 * Strange! I would expect writing compresseable data to be
604 * much faster (or about equal, if hardware compression is disabled)
608 timediff = pass2time - pass1time;
610 if (((100 * timediff) / pass2time) >= 20) { /* 20% faster? */
611 g_fprintf(stderr, _("WARNING: Tape drive has hardware compression enabled\n"));
616 * Inform about estimated time needed to run the remaining of this program
618 g_fprintf(stderr, _("Estimated time to write 2 * %lu Mbyte: "), (unsigned long) (estsize / (off_t)1024));
619 pass1time = (time_t)(2.0 * (double)pass2time * (double)estsize /
620 (1.0 * (double)pass0size * (double)blockkb));
621 /* avoid overflow and underflow by doing math in floating point */
622 g_fprintf(stderr, _("%jd sec = %jd h %jd min\n"),
624 (intmax_t)(pass1time/(time_t)3600),
625 (intmax_t)((pass1time%(time_t)3600) / (time_t)60));
633 * Do pass 1 -- write files that are 1% of the estimated size until error.
635 pass1size = (off_t)(((double)estsize * 0.01) / (double)blockkb); /* 1% of estimate */
636 if(pass1size <= (off_t)0) {
637 pass1size = (off_t)2; /* strange end case */
639 do_pass(pass1size, &pass1blocks, &pass1files, &pass1time);
642 * Do pass 2 -- write smaller files until error.
644 pass2size = pass1size / (off_t)2;
645 do_pass(pass2size, &pass2blocks, &pass2files, &pass2time);
648 * Compute the size of a filemark as the difference in data written
649 * between pass 1 and pass 2 divided by the difference in number of
650 * file marks written between pass 1 and pass 2. Note that we have
651 * to be careful in case size_t is unsigned (i.e. do not subtract
652 * things and then check for less than zero).
654 if (pass1blocks <= pass2blocks) {
656 * If tape marks take up space, there should be fewer blocks in pass
657 * 2 than in pass 1 since we wrote twice as many tape marks. But
658 * odd things happen, so make sure the result does not go negative.
660 blockdiff = (off_t)0;
662 blockdiff = pass1blocks - pass2blocks;
664 if (pass2files <= pass1files) {
666 * This should not happen, but just in case ...
670 filediff = pass2files - pass1files;
672 filemark = (size_t)((blockdiff * (off_t)blockkb) / filediff);
675 * Compute the length as the average of the two pass sizes including
678 size = ((pass1blocks * (off_t)blockkb + (off_t)filemark * pass1files)
679 + (pass2blocks * (off_t)blockkb + (off_t)filemark * pass2files))
681 if (size >= (off_t)(1024 * 1024 * 1000)) {
682 size /= (off_t)(1024 * 1024);
683 sizeunits = "gbytes";
684 } else if (size >= (off_t)(1024 * 1000)) {
686 sizeunits = "mbytes";
688 sizeunits = "kbytes";
692 * Compute the speed as the average of the two passes.
694 speed = (unsigned long)((((double)pass1blocks
695 * (double)blockkb / (double)pass1time)
696 + ((double)pass2blocks * (double)blockkb / (double)pass2time)) / 2.0);
701 g_printf("define tapetype %s {\n", typename);
702 g_printf(_(" comment \"just produced by tapetype prog (hardware compression %s)\"\n"),
703 hwcompr ? _("on") : _("off"));
704 g_printf(" length %lld %s\n", (long long)size, sizeunits);
705 g_printf(" filemark %zu kbytes\n", filemark);
706 g_printf(" speed %lu kps\n", speed);
709 if (tapefd_rewind(fd) == -1) {
710 g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),
711 sProgName, tapedev, strerror(errno));
715 if (tapefd_close(fd) == -1) {
716 g_fprintf(stderr, _("%s: could not close %s: %s\n"),
717 sProgName, tapedev, strerror(errno));