/****************************************************************************** ******************************************************************************* ** ** Copyright 2004 Intel, Inc. ** ** This is free software released under the GNU General Public License. ** There is no warranty for this software. See the file COPYING for ** details. ** ** ** This file is maintained by: ** LingXiaofeng ** ******************************************************************************* ******************************************************************************/ #include #include #include #include #include #include #include #include #include char filename[256]; #define SIZE 256 int bufsize = SIZE; char buf1[1024 * 64]; char buf2[1024 * 64]; int isread = 1; int iswrite; int count = 128; int record = 4; int repeat = 1; int issleep = 0; int showall = 0; int iscreate = 0; rateprint(struct timeval *tv1, struct timeval *tv2, double total) { long tm; double utm; tm = (tv2->tv_sec - tv1->tv_sec); utm = tm * 1000000 + (tv2->tv_usec - tv1->tv_usec); printf("total:%.3lfK Time:%.3lf Rate:%'.0fKB\n", total, utm / 1.e3, 1.e6 * total / utm); } createfile(char *filename, int count) { int fd; int i; int err; fd = open(filename, O_CREAT | O_WRONLY); if (fd < 0) { fprintf(stderr, " can't open file %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } lseek(fd, 0, SEEK_SET); for (i = 0; i < count; i++) { err = write(fd, buf2, 1024); if (!err) perror("write error"); } if (close(fd) < 0) { fprintf(stderr, " close failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } printf("create file %s of size %dK successfully\n", filename, count); } showhelp(char *argv[]) { printf("%s [-01rasfp] [--sleep ] [--create]\n", argv[0]); printf(" 0 write\n" " 1 read\n" " r record size\n" " a print each\n" " s file size\n" " f \n" " p repeate times\n" ); } int main(int argc, char *argv[]) { int fd; int i; int err; int c; int j; char file[255]; char *ocfsroot; int options_index; struct timeval tv1, tv2, tv3, tv4; file[0] = 0; ocfsroot = getenv("OCFSROOT"); if (ocfsroot == NULL) ocfsroot = "/ocfs"; while (1) { options_index = 0; static struct option long_options[] = { {"sleep", 1, 0, 0}, {"create", 0, &iscreate, 1}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "01r:as:f:p:h", long_options, &options_index); if (c == EOF) break; switch (c) { case 0: if (options_index == 0) issleep = atoi(optarg); break; case '0': iswrite = 1; break; case '1': isread = 1; break; case 's': count = atoi(optarg); break; case 'r': record = atoi(optarg); break; case 'p': repeat = atoi(optarg); break; case 'f': strncpy(file, optarg, 254); break; case 'a': showall = 1; break; case 'h': default: showhelp(argv); exit(0); break; } } if (!file[0]) snprintf(filename, 255, "%s/ocfs_test%d", ocfsroot, getpid()); else snprintf(filename, 255, "%s", file); filename[255] = 0; count /= record; if (iscreate) createfile(filename, record * count); for (i = 0; i < bufsize; i++) buf1[i] = 'A' + (i + getpid()) % 50; gettimeofday(&tv1, NULL); for (j = 0; j < repeat; j++) { gettimeofday(&tv4, NULL); fd = open(filename, O_CREAT | O_RDONLY); if (fd < 0) { fprintf(stderr, " can't open file %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } lseek(fd, 0, SEEK_SET); if (isread) for (i = 0; i < count; i++) err = read(fd, buf2, record * 1024); lseek(fd, 0, SEEK_SET); if (iswrite) for (i = 0; i < count; i++) err = write(fd, buf2, record * 1024); if (close(fd) < 0) { fprintf(stderr, " close failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } gettimeofday(&tv2, NULL); if (j < 2 || showall) rateprint(&tv4, &tv2, record * count); if (j == 5 && issleep) sleep(issleep); } /*end for j */ gettimeofday(&tv3, NULL); rateprint(&tv1, &tv3, record * count * repeat); printf("PASS\n"); exit(EXIT_SUCCESS); }