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));
241 if ((blks = writeblocks(fd, size)) <= (off_t)0 || tapefd_weof(fd, (off_t)1) != 0)
247 show_progress(blocks, files);
254 if (*blocks == (off_t)0) {
255 fprintf(stderr, "%s: could not write any data in this pass: %s\n",
256 sProgName, short_write ? "short write" : strerror(save_errno));
262 * Just in case time warped backward or the device is really, really
263 * fast (e.g. /dev/null testing).
267 *seconds = end - start;
272 show_progress(blocks, files);
273 fprintf(stderr, " in " TIME_T_FMT " second%s (%s)\n",
274 (TIME_T_FMT_TYPE)*seconds, (*seconds == 1) ? "" : "s",
275 short_write ? "short write" : strerror(save_errno));
289 if (dorewind && tapefd_rewind(fd) == -1) {
290 fprintf(stderr, "%s: could not rewind %s: %s\n",
291 sProgName, tapedev, strerror(errno));
297 blks = writeblocks(fd, size);
298 tapefd_weof(fd, (off_t)1);
304 if (blks <= (off_t)0) {
305 fprintf(stderr, "%s: could not write any data in this pass: %s\n",
306 sProgName, short_write ? "short write" : strerror(save_errno));
312 * Just in case time warped backward or the device is really, really
313 * fast (e.g. /dev/null testing).
317 *seconds = end - start;
327 off_t pass1blocks = (off_t)0;
328 off_t pass2blocks = (off_t)0;
332 off_t pass1files = (off_t)0;
333 off_t pass2files = (off_t)0;
349 int comprtstonly = 0;
350 int overwrite_label = 0;
353 char *datestamp = NULL;
357 if ((sProgName = strrchr(*argv, '/')) == NULL) {
363 /* Don't die when child closes pipe */
364 signal(SIGPIPE, SIG_IGN);
367 tapedev = getenv("TAPE");
368 typename = "unknown-tapetype";
370 while ((ch = getopt(argc, argv, "b:e:f:t:hco")) != EOF) {
373 blockkb = (size_t)strtol(optarg, &suffix, 0);
374 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
375 if (*suffix == 'm' || *suffix == 'M') {
377 } else if (*suffix == 'g' || *suffix == 'G') {
378 blockkb *= (1024 * 1024);
380 fprintf(stderr, "%s: unknown size suffix \'%c\'\n",
387 estsize = OFF_T_STRTOL(optarg, &suffix, 0);
388 if (!(*suffix == '\0' || *suffix == 'k' || *suffix == 'K')) {
389 if (*suffix == 'm' || *suffix == 'M') {
390 estsize *= (off_t)1024;
391 } else if (*suffix == 'g' || *suffix == 'G') {
392 estsize *= (off_t)(1024 * 1024);
394 fprintf(stderr, "%s: unknown size suffix \'%c\'\n",
402 tapedev = stralloc(optarg);
406 typename = stralloc(optarg);
422 fprintf(stderr, "%s: unknown option \'%c\'\n", sProgName, ch);
430 blocksize = blockkb * 1024;
432 if (tapedev == NULL) {
433 fprintf(stderr, "%s: No tapedev specified\n", sProgName);
444 estsize = (off_t)(1024 * 1024); /* assume 1 GByte for now */
446 fprintf(stderr, "%s: please specify estimated tape capacity (e.g. '-e 4g')\n", sProgName);
456 fd = tape_open(tapedev, O_RDONLY, 0);
458 fprintf(stderr, "%s: could not open %s: %s\n",
459 sProgName, tapedev, strerror(errno));
463 if((result = tapefd_rdlabel(fd, &datestamp, &label)) == NULL) {
466 else if (strcmp(result,"not an amanda tape") == 0) {
470 if(tapefd_rewind(fd) == -1) {
471 fprintf(stderr, "%s: could not rewind %s: %s\n",
472 sProgName, tapedev, strerror(errno));
479 if(is_labeled == 1 && overwrite_label == 0) {
480 fprintf(stderr, "%s: The tape is an amanda tape, use -o to overwrite the tape\n",
484 else if(is_labeled == 2 && overwrite_label == 0) {
485 fprintf(stderr, "%s: The tape is already used, use -o to overwrite the tape\n",
490 fd = tape_open(tapedev, O_RDWR, 0);
492 fprintf(stderr, "%s: could not open %s: %s\n",
493 sProgName, tapedev, strerror(errno));
497 do_tty = isatty(fileno(stderr));
500 * Estimate pass: write twice a small file, once with compressable
501 * data and once with uncompressable data.
502 * The theory is that if the drive is in hardware compression mode
503 * we notice a significant difference in writing speed between the two
504 * (at least if we can provide data as fast the tape streams).
507 initnotrandombytes();
509 fprintf(stderr, "Estimate phase 1...");
510 pass0size = (off_t)(8 * 1024 / blockkb);
514 * To get accurate results, we should write enough data
515 * so that rewind/start/stop time is small compared to
516 * the total time; let's take 10%.
517 * The timer has a 1 sec granularity, so the test
518 * should take at least 10 seconds to measure a
519 * difference with 10% accuracy; let's take 25 seconds.
521 while (pass1time < 25 || ((100*(pass2time-pass1time)/pass2time) >= 10) ) {
522 if (pass1time != 0) {
523 time_t t = pass1time;
525 pass0size *= (off_t)2;
530 * first a dummy pass to rewind, stop, start and
531 * get drive streaming, then do the real timing
533 do_pass0(pass0size, &pass2time, 1);
534 do_pass0(pass0size, &pass1time, 0);
535 if (pass0size >= (off_t)(10 * 1024 * 1024)) {
537 "\rTape device is too fast to detect hardware compression...\n");
538 break; /* avoid loops if tape is superfast or broken */
542 "\rWriting " OFF_T_FMT " Mbyte compresseable data: "
544 (OFF_T_FMT_TYPE)((off_t)blockkb * pass0size / (off_t)1024),
545 (TIME_T_FMT_TYPE)pass1time);
548 * now generate uncompressable data and try again
551 srandom((unsigned)now);
554 fprintf(stderr, "Estimate phase 2...");
555 do_pass0(pass0size, &pass2time, 1); /* rewind and get drive streaming */
556 do_pass0(pass0size, &pass2time, 0);
557 fprintf(stderr, "\rWriting " OFF_T_FMT
558 " Mbyte uncompresseable data: " TIME_T_FMT " sec\n",
559 (OFF_T_FMT_TYPE)((off_t)blockkb * pass0size / (off_t)1024),
560 (TIME_T_FMT_TYPE)pass2time);
563 * Compute the time difference between writing the compressable and
564 * uncompressable data. If it differs more than 20%, then warn
565 * user that the tape drive has probably hardware compression enabled.
567 if (pass1time > pass2time) {
569 * Strange! I would expect writing compresseable data to be
570 * much faster (or about equal, if hardware compression is disabled)
574 timediff = pass2time - pass1time;
576 if (((100 * timediff) / pass2time) >= 20) { /* 20% faster? */
577 fprintf(stderr, "WARNING: Tape drive has hardware compression enabled\n");
582 * Inform about estimated time needed to run the remaining of this program
584 fprintf(stderr, "Estimated time to write 2 * %lu Mbyte: ", (unsigned long) (estsize / (off_t)1024));
585 pass1time = (time_t)(2.0 * (double)pass2time * (double)estsize /
586 (1.0 * (double)pass0size * (double)blockkb));
587 /* avoid overflow and underflow by doing math in floating point */
588 fprintf(stderr, TIME_T_FMT " sec = ",
589 (TIME_T_FMT_TYPE)pass1time);
590 fprintf(stderr, TIME_T_FMT " h " TIME_T_FMT " min\n",
591 (TIME_T_FMT_TYPE)(pass1time/(time_t)3600),
592 (TIME_T_FMT_TYPE)((pass1time%(time_t)3600) / (time_t)60));
600 * Do pass 1 -- write files that are 1% of the estimated size until error.
602 pass1size = (off_t)(((double)estsize * 0.01) / (double)blockkb); /* 1% of estimate */
603 if(pass1size <= (off_t)0) {
604 pass1size = (off_t)2; /* strange end case */
606 do_pass(pass1size, &pass1blocks, &pass1files, &pass1time);
609 * Do pass 2 -- write smaller files until error.
611 pass2size = pass1size / (off_t)2;
612 do_pass(pass2size, &pass2blocks, &pass2files, &pass2time);
615 * Compute the size of a filemark as the difference in data written
616 * between pass 1 and pass 2 divided by the difference in number of
617 * file marks written between pass 1 and pass 2. Note that we have
618 * to be careful in case size_t is unsigned (i.e. do not subtract
619 * things and then check for less than zero).
621 if (pass1blocks <= pass2blocks) {
623 * If tape marks take up space, there should be fewer blocks in pass
624 * 2 than in pass 1 since we wrote twice as many tape marks. But
625 * odd things happen, so make sure the result does not go negative.
627 blockdiff = (off_t)0;
629 blockdiff = pass1blocks - pass2blocks;
631 if (pass2files <= pass1files) {
633 * This should not happen, but just in case ...
637 filediff = pass2files - pass1files;
639 filemark = (size_t)((blockdiff * (off_t)blockkb) / filediff);
642 * Compute the length as the average of the two pass sizes including
645 size = ((pass1blocks * (off_t)blockkb + (off_t)filemark * pass1files)
646 + (pass2blocks * (off_t)blockkb + (off_t)filemark * pass2files))
648 if (size >= (off_t)(1024 * 1024 * 1000)) {
649 size /= (off_t)(1024 * 1024);
650 sizeunits = "gbytes";
651 } else if (size >= (off_t)(1024 * 1000)) {
653 sizeunits = "mbytes";
655 sizeunits = "kbytes";
659 * Compute the speed as the average of the two passes.
661 speed = (unsigned long)((((double)pass1blocks
662 * (double)blockkb / (double)pass1time)
663 + ((double)pass2blocks * (double)blockkb / (double)pass2time)) / 2.0);
668 printf("define tapetype %s {\n", typename);
669 printf(" comment \"just produced by tapetype prog (hardware compression %s)\"\n",
670 hwcompr ? "on" : "off");
671 printf(" length " OFF_T_FMT " %s\n", (OFF_T_FMT_TYPE)size, sizeunits);
672 printf(" filemark " SIZE_T_FMT " kbytes\n", (SIZE_T_FMT_TYPE)filemark);
673 printf(" speed %lu kps\n", speed);
676 if (tapefd_rewind(fd) == -1) {
677 fprintf(stderr, "%s: could not rewind %s: %s\n",
678 sProgName, tapedev, strerror(errno));
682 if (tapefd_close(fd) == -1) {
683 fprintf(stderr, "%s: could not close %s: %s\n",
684 sProgName, tapedev, strerror(errno));