Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Anonymous
Friday, December 25th, 2009 at 9:46:51am UTC 

  1. /*
  2.  * fsync-tester.c
  3.  *
  4.  * Written by Theodore Ts'o, 3/21/09.  Updated by Chris Mason to include
  5.  * the random writer thread
  6.  *
  7.  * This file may be redistributed under the terms of the GNU Public
  8.  * License, version 2.
  9.  */
  10. #define _FILE_OFFSET_BITS 64
  11. #define _XOPEN_SOURCE 500
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/wait.h>
  19. #include <signal.h>
  20. #include <time.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23.  
  24. #define SIZE (32768*32)
  25. static char bigbuf[4096];
  26.  
  27. static float timeval_subtract(struct timeval *tv1, struct timeval *tv2)
  28. {
  29.         return ((tv1->tv_sec - tv2->tv_sec) +
  30.                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
  31. }
  32.  
  33. static void random_io(int fd, loff_t total)
  34. {
  35.         loff_t cur = 0;
  36.         int ret;
  37.  
  38.         /* just some constant so our runs are always the same */
  39.         srand(4096);
  40.         while(1) {
  41.                 /*
  42.                  * we want a random offset into the file,
  43.                  * but rand only returns max in.  So we make
  44.                  * it a random block number instead, and multiply
  45.                  * by 4096.
  46.                  */
  47.                 cur = rand();
  48.                 cur = (cur * 4096) % (total - 4096);
  49.  
  50.                 /* align our offset  to 4k */
  51.                 cur = cur / 4096;
  52.                 cur = cur * 4096;
  53.                 ret = pwrite(fd, bigbuf, 4096, cur);
  54.                 if (ret < 4096) {
  55.                         fprintf(stderr, "short write ret %d cur %llu\n",
  56.                                 ret, (unsigned long long)cur);
  57.                         exit(1);
  58.                 }
  59.         }
  60. }
  61.  
  62. int main(int argc, char **argv)
  63. {
  64.         int     fd;
  65.         struct timeval tv, tv2, start;
  66.         char buf[SIZE];
  67.         pid_t pid;
  68.         loff_t total = ((loff_t)8) * 1024 * 1024 * 1024;
  69.         loff_t cur = 0;
  70.         int rand_fd;
  71.         int ret;
  72.         int i;
  73.         int status;
  74.         struct stat st;
  75.  
  76.         memset(bigbuf, 0, 4096);
  77.  
  78.         rand_fd = open("fsync-tester.rnd-file", O_WRONLY|O_CREAT);
  79.         if (rand_fd < 0) {
  80.                 perror("open");
  81.                 exit(1);
  82.         }
  83.  
  84.         ret = fstat(rand_fd, &st);
  85.         if (ret < 0) {
  86.                 perror("fstat");
  87.                 exit(1);
  88.         }
  89.  
  90.         if (st.st_size < total) {
  91.                 printf("setting up random write file\n");
  92.                 while(cur < total) {
  93.                         ret = write(rand_fd, bigbuf, 4096);
  94.                         if (ret <= 0) {
  95.                                 fprintf(stderr, "short write\n");
  96.                                 exit(1);
  97.                         }
  98.                         cur += ret;
  99.                 }
  100.                 printf("done setting up random write file\n");
  101.         }
  102.  
  103.         fd = open("fsync-tester.tst-file", O_WRONLY|O_CREAT);
  104.         if (fd < 0) {
  105.                 perror("open");
  106.                 exit(1);
  107.         }
  108.         memset(buf, 'a', SIZE);
  109.  
  110.         pid = fork();
  111.         if (pid == 0) {
  112.                 printf("starting random io!\n");
  113.                 random_io(rand_fd, total);
  114.                 exit(0);
  115.         }
  116.  
  117.         close(rand_fd);
  118.  
  119.         gettimeofday(&start, NULL);
  120.         printf("starting fsync run\n");
  121.         for(i = 0; i < 60; i++) {
  122.                 pwrite(fd, buf, SIZE, 0);
  123.                 gettimeofday(&tv, NULL);
  124.                 fsync(fd);
  125.                 gettimeofday(&tv2, NULL);
  126.                 printf("fsync time: %5.4fs\n", timeval_subtract(&tv2, &tv));
  127.                 if (timeval_subtract(&tv2, &start) > 60)
  128.                         break;
  129.                 sleep(4);
  130.         }
  131.         printf("run done %d fsyncs total, killing random writer\n", i + 1);
  132.         fflush(stdout);
  133.         kill(pid, SIGTERM);
  134.         wait(&status);
  135.  
  136.         return 0;
  137. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right