Annotation of Net2/tests/benchmarks/randrewrite.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Random I/O benchmark.
        !             3:  *
        !             4:  * Process writes blocks to a file in a
        !             5:  * random order.  Writes are constrained
        !             6:  * to overwrite existing blocks.  This
        !             7:  * test should be run after the seqio test
        !             8:  * so that a file is constructed.
        !             9:  */
        !            10: #include <sys/types.h>
        !            11: #include <sys/file.h>
        !            12: #include <sys/stat.h>
        !            13: 
        !            14: char   *malloc();
        !            15: 
        !            16: main(argc, argv)
        !            17:        char *argv[];
        !            18: {
        !            19:        char *buf;
        !            20:        int fd, bn, nblocks;
        !            21:        struct stat sb;
        !            22:        register int i, niter;
        !            23: 
        !            24:        if (argc < 3) {
        !            25:                printf("usage: %s file #writes\n", argv[0]);
        !            26:                exit(1);
        !            27:        }
        !            28:        fd = open(argv[1], O_WRONLY|O_CREAT, 0644);
        !            29:        if (fd < 0) {
        !            30:                perror(argv[1]);
        !            31:                exit(2);
        !            32:        }
        !            33:        fstat(fd, &sb);
        !            34:        buf = malloc(sb.st_blksize);
        !            35:        if (buf == (char *)0) {
        !            36:                printf("Couldn't allocate i/o buffer.\n");
        !            37:                exit(3);
        !            38:        }
        !            39:        nblocks = sb.st_size / sb.st_blksize;
        !            40:        niter = atoi(argv[2]);
        !            41:        printf("%d random writes in %d block file (block size %d)\n", 
        !            42:            niter, nblocks, sb.st_blksize);
        !            43:        for (i = 0; i < niter; i++) {
        !            44:                bn = random() % nblocks;
        !            45:                lseek(fd, bn * sb.st_blksize, L_SET);
        !            46:                write(fd, buf, sb.st_blksize);
        !            47:        }
        !            48: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.