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",
213 fprintf(stderr, "wrote " OFF_T_FMT " " SIZE_T_FMT "Kb block%s in " OFF_T_FMT " file%s",
214 (OFF_T_FMT_TYPE)*blocks,
215 (SIZE_T_FMT_TYPE)blockkb, (*blocks == (off_t)1) ? "" : "s",
216 (OFF_T_FMT_TYPE)*files, (*files == (off_t)1) ? "" : "s");
231 if (tapefd_rewind(fd) == -1) {
232 fprintf(stderr, "%s: could not rewind %s: %s\n",
233 sProgName, tapedev, strerror(errno));
236 if (((-1 == tapefd_close(fd)) ||
237 (-1 == (fd = tape_open(tapedev, O_RDWR))))) {
238 fprintf(stderr, "%s: could not re-open %s: %s\n",
239 sProgName, tapedev, strerror(errno));
247 if ((blks = writeblocks(fd, size)) <= (off_t)0 || tapefd_weof(fd, (off_t)1) != 0)
253 show_progress(blocks, files);
260 if (*blocks == (off_t)0) {
261 fprintf(stderr, "%s: could not write any data in this pass: %s\n",
262 sProgName, short_write ? "short write" : strerror(save_errno));
268 * Just in case time warped backward or the device is really, really
269 * fast (e.g. /dev/null testing).
273 *seconds = end - start;
278 show_progress(blocks, files);
279 fprintf(stderr, " in " TIME_T_FMT " second%s (%s)\n",
280 (TIME_T_FMT_TYPE)*seconds, (*seconds == 1) ? "" : "s",
281 short_write ? "short write" : strerror(save_errno));
295 if (dorewind && tapefd_rewind(fd) == -1) {
296 fprintf(stderr, "%s: could not rewind %s: %s\n",
297 sProgName, tapedev, strerror(errno));
302 ((-1 == tapefd_close(fd)) ||
303 (-1 == (fd = tape_open(tapedev, O_RDWR))))) {
304 fprintf(stderr, "%s: could not re-open %s: %s\n",
305 sProgName, tapedev, strerror(errno));
312 blks = writeblocks(fd, size);
313 tapefd_weof(fd, (off_t)1);
319 if (blks <= (off_t)0) {
320 fprintf(stderr, "%s: could not write any data in this pass: %s\n",
321 sProgName, short_write ? "short write" : strerror(save_errno));
327 * Just in case time warped backward or the device is really, really
328 * fast (e.g. /dev/null testing).
332 *seconds = end - start;
342 off_t pass1blocks = (off_t)0;
343 off_t pass2blocks = (off_t)0;
347 off_t pass1files = (off_t)0;
348 off_t pass2files = (off_t)0;
364 int comprtstonly = 0;
365 int overwrite_label = 0;
368 char *datestamp = NULL;
372 if ((sProgName = strrchr(*argv, '/')) == NULL) {
378 /* Don't die when child closes pipe */
379 signal(SIGPIPE, SIG_IGN);
382 tapedev = getenv("TAPE");
383 typename = "unknown-tapetype";
385 while ((ch = getopt(argc, argv, "b:e:f:t:hco")) != EOF) {
388 blockkb = (size_t)strtol(optarg, &suffix, 0);
389 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
390 if (*suffix == 'm' || *suffix == 'M') {
392 } else if (*suffix == 'g' || *suffix == 'G') {
393 blockkb *= (1024 * 1024);
395 fprintf(stderr, "%s: unknown size suffix \'%c\'\n",
402 estsize = OFF_T_STRTOL(optarg, &suffix, 0);
403 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
404 if (*suffix == 'm' || *suffix == 'M') {
405 estsize *= (off_t)1024;
406 } else if (*suffix == 'g' || *suffix == 'G') {
407 estsize *= (off_t)(1024 * 1024);
409 fprintf(stderr, "%s: unknown size suffix \'%c\'\n",
417 tapedev = stralloc(optarg);
421 typename = stralloc(optarg);
437 fprintf(stderr, "%s: unknown option \'%c\'\n", sProgName, ch);
445 blocksize = blockkb * 1024;
447 if (tapedev == NULL) {
448 fprintf(stderr, "%s: No tapedev specified\n", sProgName);
459 estsize = (off_t)(1024 * 1024); /* assume 1 GByte for now */
461 fprintf(stderr, "%s: please specify estimated tape capacity (e.g. '-e 4g')\n", sProgName);
471 fd = tape_open(tapedev, O_RDONLY, 0);
473 fprintf(stderr, "%s: could not open %s: %s\n",
474 sProgName, tapedev, strerror(errno));
478 if((result = tapefd_rdlabel(fd, &datestamp, &label)) == NULL) {
481 else if (strcmp(result,"not an amanda tape") == 0) {
485 if(tapefd_rewind(fd) == -1) {
486 fprintf(stderr, "%s: could not rewind %s: %s\n",
487 sProgName, tapedev, strerror(errno));
494 if(is_labeled == 1 && overwrite_label == 0) {
495 fprintf(stderr, "%s: The tape is an amanda tape, use -o to overwrite the tape\n",
499 else if(is_labeled == 2 && overwrite_label == 0) {
500 fprintf(stderr, "%s: The tape is already used, use -o to overwrite the tape\n",
505 fd = tape_open(tapedev, O_RDWR, 0);
507 fprintf(stderr, "%s: could not open %s: %s\n",
508 sProgName, tapedev, strerror(errno));
512 do_tty = isatty(fileno(stderr));
515 * Estimate pass: write twice a small file, once with compressable
516 * data and once with uncompressable data.
517 * The theory is that if the drive is in hardware compression mode
518 * we notice a significant difference in writing speed between the two
519 * (at least if we can provide data as fast the tape streams).
522 initnotrandombytes();
524 fprintf(stderr, "Estimate phase 1...");
525 pass0size = (off_t)(8 * 1024 / blockkb);
529 * To get accurate results, we should write enough data
530 * so that rewind/start/stop time is small compared to
531 * the total time; let's take 10%.
532 * The timer has a 1 sec granularity, so the test
533 * should take at least 10 seconds to measure a
534 * difference with 10% accuracy; let's take 25 seconds.
536 while (pass1time < 25 || ((100*(pass2time-pass1time)/pass2time) >= 10) ) {
537 if (pass1time != 0) {
538 time_t t = pass1time;
540 pass0size *= (off_t)2;
545 * first a dummy pass to rewind, stop, start and
546 * get drive streaming, then do the real timing
548 do_pass0(pass0size, &pass2time, 1);
549 do_pass0(pass0size, &pass1time, 0);
550 if (pass0size >= (off_t)(10 * 1024 * 1024)) {
552 "\rTape device is too fast to detect hardware compression...\n");
553 break; /* avoid loops if tape is superfast or broken */
557 "\rWriting " OFF_T_FMT " Mbyte compresseable data: "
559 (OFF_T_FMT_TYPE)((off_t)blockkb * pass0size / (off_t)1024),
560 (TIME_T_FMT_TYPE)pass1time);
563 * now generate uncompressable data and try again
566 srandom((unsigned)now);
569 fprintf(stderr, "Estimate phase 2...");
570 do_pass0(pass0size, &pass2time, 1); /* rewind and get drive streaming */
571 do_pass0(pass0size, &pass2time, 0);
572 fprintf(stderr, "\rWriting " OFF_T_FMT
573 " Mbyte uncompresseable data: " TIME_T_FMT " sec\n",
574 (OFF_T_FMT_TYPE)((off_t)blockkb * pass0size / (off_t)1024),
575 (TIME_T_FMT_TYPE)pass2time);
578 * Compute the time difference between writing the compressable and
579 * uncompressable data. If it differs more than 20%, then warn
580 * user that the tape drive has probably hardware compression enabled.
582 if (pass1time > pass2time) {
584 * Strange! I would expect writing compresseable data to be
585 * much faster (or about equal, if hardware compression is disabled)
589 timediff = pass2time - pass1time;
591 if (((100 * timediff) / pass2time) >= 20) { /* 20% faster? */
592 fprintf(stderr, "WARNING: Tape drive has hardware compression enabled\n");
597 * Inform about estimated time needed to run the remaining of this program
599 fprintf(stderr, "Estimated time to write 2 * %lu Mbyte: ", (unsigned long) (estsize / (off_t)1024));
600 pass1time = (time_t)(2.0 * (double)pass2time * (double)estsize /
601 (1.0 * (double)pass0size * (double)blockkb));
602 /* avoid overflow and underflow by doing math in floating point */
603 fprintf(stderr, TIME_T_FMT " sec = ",
604 (TIME_T_FMT_TYPE)pass1time);
605 fprintf(stderr, TIME_T_FMT " h " TIME_T_FMT " min\n",
606 (TIME_T_FMT_TYPE)(pass1time/(time_t)3600),
607 (TIME_T_FMT_TYPE)((pass1time%(time_t)3600) / (time_t)60));
615 * Do pass 1 -- write files that are 1% of the estimated size until error.
617 pass1size = (off_t)(((double)estsize * 0.01) / (double)blockkb); /* 1% of estimate */
618 if(pass1size <= (off_t)0) {
619 pass1size = (off_t)2; /* strange end case */
621 do_pass(pass1size, &pass1blocks, &pass1files, &pass1time);
624 * Do pass 2 -- write smaller files until error.
626 pass2size = pass1size / (off_t)2;
627 do_pass(pass2size, &pass2blocks, &pass2files, &pass2time);
630 * Compute the size of a filemark as the difference in data written
631 * between pass 1 and pass 2 divided by the difference in number of
632 * file marks written between pass 1 and pass 2. Note that we have
633 * to be careful in case size_t is unsigned (i.e. do not subtract
634 * things and then check for less than zero).
636 if (pass1blocks <= pass2blocks) {
638 * If tape marks take up space, there should be fewer blocks in pass
639 * 2 than in pass 1 since we wrote twice as many tape marks. But
640 * odd things happen, so make sure the result does not go negative.
642 blockdiff = (off_t)0;
644 blockdiff = pass1blocks - pass2blocks;
646 if (pass2files <= pass1files) {
648 * This should not happen, but just in case ...
652 filediff = pass2files - pass1files;
654 filemark = (size_t)((blockdiff * (off_t)blockkb) / filediff);
657 * Compute the length as the average of the two pass sizes including
660 size = ((pass1blocks * (off_t)blockkb + (off_t)filemark * pass1files)
661 + (pass2blocks * (off_t)blockkb + (off_t)filemark * pass2files))
663 if (size >= (off_t)(1024 * 1024 * 1000)) {
664 size /= (off_t)(1024 * 1024);
665 sizeunits = "gbytes";
666 } else if (size >= (off_t)(1024 * 1000)) {
668 sizeunits = "mbytes";
670 sizeunits = "kbytes";
674 * Compute the speed as the average of the two passes.
676 speed = (unsigned long)((((double)pass1blocks
677 * (double)blockkb / (double)pass1time)
678 + ((double)pass2blocks * (double)blockkb / (double)pass2time)) / 2.0);
683 printf("define tapetype %s {\n", typename);
684 printf(" comment \"just produced by tapetype prog (hardware compression %s)\"\n",
685 hwcompr ? "on" : "off");
686 printf(" length " OFF_T_FMT " %s\n", (OFF_T_FMT_TYPE)size, sizeunits);
687 printf(" filemark " SIZE_T_FMT " kbytes\n", (SIZE_T_FMT_TYPE)filemark);
688 printf(" speed %lu kps\n", speed);
691 if (tapefd_rewind(fd) == -1) {
692 fprintf(stderr, "%s: could not rewind %s: %s\n",
693 sProgName, tapedev, strerror(errno));
697 if (tapefd_close(fd) == -1) {
698 fprintf(stderr, "%s: could not close %s: %s\n",
699 sProgName, tapedev, strerror(errno));