Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A pwrite() loop, for testing NFS random writes.
- * Run it under strace -tt -T, giving as argument a path located on a NFS server
- * It will try to write at random locations inside that file (4 GB by default,
- * see filesize variable)
- * Bare with me for the possible coding errors ;)
- */
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <time.h>
- #define BUFSIZE 32*1024
- #define _FILE_OFFSET_BITS 64
- int main (int argc, char **argv)
- {
- unsigned char buf[BUFSIZE];
- ssize_t filesize = 4000000000;
- ssize_t res;
- size_t blocksize = 1024;
- off_t off = 0;
- unsigned long val;
- int fd;
- int i;
- int seq = 0;
- if (!argv[1]) {
- fprintf(stderr, "Usage: %s <tmpfile> [blocksize] [seq|rnd]\n", argv[0]);
- exit(5);
- }
- if (argc >= 3 && sscanf(argv[2], "%lu", &val) > 0) {
- if (val > BUFSIZE)
- val = BUFSIZE;
- blocksize = (size_t)val;
- }
- if (argc >= 4 && !strcmp(argv[3], "seq"))
- seq = 1;
- memset(buf, 'a', BUFSIZE);
- if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC)) == -1) {
- perror("open");
- exit(5);
- }
- fchmod(fd, S_IWUSR|S_IRUSR);
- srand(time(NULL));
- /* for (i = 0; ; i++) */
- for (i = 0; i < 100000; i++)
- {
- if (seq) {
- off += BUFSIZE;
- if (off > filesize)
- off = 0;
- } else {
- off = (off_t) (filesize * (rand() / (RAND_MAX + 1.0)));
- off = off % filesize;
- }
- res = pwrite(fd, buf, blocksize, off);
- if (res != blocksize) {
- perror("pwrite");
- break;
- }
- /* usleep(50); */
- }
- close(fd);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement