Advertisement
Guest User

random pwrite test

a guest
Oct 27th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. /* A pwrite() loop, for testing NFS random writes.
  2.  * Run it under strace -tt -T, giving as argument a path located on a NFS server
  3.  * It will try to write at random locations inside that file (4 GB by default,
  4.  *  see filesize variable)
  5.  * Bare with me for the possible coding errors ;)
  6.  */
  7.  
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #define BUFSIZE 32*1024
  18. #define _FILE_OFFSET_BITS 64
  19.  
  20. int main (int argc, char **argv)
  21. {
  22.    unsigned char buf[BUFSIZE];
  23.    ssize_t filesize = 4000000000;
  24.    ssize_t res;
  25.    size_t  blocksize = 1024;
  26.    off_t off = 0;
  27.    unsigned long val;
  28.    int fd;
  29.    int i;
  30.    int seq = 0;
  31.  
  32.    if (!argv[1]) {
  33.       fprintf(stderr, "Usage: %s <tmpfile> [blocksize] [seq|rnd]\n", argv[0]);
  34.       exit(5);
  35.    }
  36.  
  37.    if (argc >= 3 && sscanf(argv[2], "%lu", &val) > 0) {
  38.       if (val > BUFSIZE)
  39.          val = BUFSIZE;
  40.       blocksize = (size_t)val;
  41.    }
  42.  
  43.    if (argc >= 4 && !strcmp(argv[3], "seq"))
  44.       seq = 1;
  45.  
  46.    memset(buf, 'a', BUFSIZE);
  47.  
  48.    if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC)) == -1) {
  49.       perror("open");
  50.       exit(5);
  51.    }
  52.  
  53.    fchmod(fd, S_IWUSR|S_IRUSR);
  54.    srand(time(NULL));
  55.    /* for (i = 0; ; i++) */
  56.    for (i = 0; i < 100000; i++)
  57.    {
  58.       if (seq) {
  59.          off += BUFSIZE;
  60.          if (off > filesize)
  61.             off = 0;
  62.       } else {
  63.          off = (off_t) (filesize * (rand() / (RAND_MAX + 1.0)));
  64.          off = off % filesize;
  65.       }
  66.       res = pwrite(fd, buf, blocksize, off);
  67.       if (res != blocksize) {
  68.          perror("pwrite");
  69.          break;
  70.       }
  71.  
  72.       /* usleep(50); */
  73.    }
  74.  
  75.    close(fd);
  76.    exit(0);
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement