Hi

On Wed, Dec 17, 2014 at 2:39 PM, Alvaro Herrera
<alvhe...@2ndquadrant.com> wrote:
> didier wrote:
>
>> On many Linux systems it may not do that much (2.6.32 and 3.2 are bad,
>> 3.13 is better but still it slows the fsync).
>>
>> If there's a fsync in progress WALReceiver will:
>> 1- slow the fsync because its writes to the same file are grabbed by the 
>> fsync
>> 2- stall until the end of fsync.
>
> Is this behavior filesystem-dependent?
I don't know. I only tested  ext4

Attach the trivial code I used, there's a lot of junk in it.

Didier
/*
* Compile with: gcc testf.c -Wall -W -O0 
*/
 
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/file.h>
#include <errno.h>
 
static long long microseconds(void) {
   struct timeval tv;
   long long mst;
 
   gettimeofday(&tv, NULL);
   mst = ((long long)tv.tv_sec)*1000000;
   mst += tv.tv_usec;
   return mst;
}
int out= 0;
//#define FLOCK(a,b) flock(a,b)
#define FLOCK(a,b) (0)
 
//==================
// fsync  
void child(void) {
  int fd, retval;
  long long start;
 
  while(1) {
     fd = open("/tmp/foo.txt",O_RDONLY);
     //usleep(30000000);
     usleep(5000000);
     FLOCK(fd, LOCK_EX);
     if (out) {
       printf("Start sync\n");
       fflush(stdout);
       start = microseconds();
     }
     retval = fsync(fd);
     FLOCK(fd, LOCK_UN);
     if (out) {
       printf("Sync in %lld microseconds (%d)\n", microseconds()-start,retval);
       fflush(stdout);
     }  
     close(fd);
   }
   exit(0);
}

char buf[8*1024];
#define f_size (2lu*1024*1024*1024)

//==================
// read
void child2(void) {
   int fd, retval;
   long long start;
   off_t lfsr;

   fd = open("/tmp/foo.txt",O_RDWR /*|O_CREAT | O_SYNC*/,0644);
   srandom(2000 +time(NULL));
   while(1) {
      if (out) {
        start = microseconds();
      }
      lfsr = random()/sizeof(buf);
      if (pread (fd, buf, sizeof(buf), sizeof(buf)*lfsr) == -1) {
         perror("read");
         exit(1);
      }
      // posix_fadvise(fd, sizeof(buf)*lfsr, sizeof(buf), POSIX_FADV_DONTNEED);
      if (out) {
        printf("read %lu in %lld microseconds\n", lfsr *sizeof(buf), microseconds()-start);
        fflush(stdout);
      }
      usleep(500);
   }
   close(fd);
   exit(0);
}

//==================
void child3(int end) {
   int fd, retval;
   long long start;
   off_t lfsr;
   int i;
   int j = 2;

   fd = open("/tmp/foo.txt",O_RDWR /*|O_CREAT | O_SYNC*/,0644);
   for (i = 0; i < 131072/j; i++) {
      int u;
      lseek(fd, sizeof(buf)*(i*j), SEEK_SET);
      write(fd, buf , sizeof(buf));  
    }
   close(fd);
   if (end)
     exit(0);
   sleep(60);
}

 
int main(void) {
   int fd0 = open("/tmp/foo.txt",O_RDWR |O_CREAT /*| O_SYNC*/,0644);
   int fd1 = open("/tmp/foo1.txt",O_RDWR |O_CREAT /*| O_SYNC*/,0644);
   
   int fd;
   long long start;
   long long end = 0;
   off_t lfsr = 0;
   memset(buf, 'a', sizeof(buf));
   ftruncate(fd0, f_size);
   ftruncate(fd1, f_size);
   printf ("%d\n",RAND_MAX);
//   child3(0);
   
   if (!fork()) {
     child();
     exit(1);
   }
   
#if 0
   if (!fork()) {
     child2();
     exit(1);
   }
   if (!fork()) {
     child3(1);
     exit(1);
   }
#endif   
   srandom(1000+time(NULL));
   while(1) {
      fd = fd0;
      if (FLOCK(fd, LOCK_EX| LOCK_NB) == -1) {
         if (errno == EWOULDBLOCK)
            fd =fd1;        
      }
      lfsr = random()/sizeof(buf);
      if (out) {
         start = microseconds();
      }
//      if (pwrite(fd ,buf ,sizeof(buf), sizeof(buf)*lfsr) == -1) {
      lseek(fd, sizeof(buf)*lfsr, SEEK_SET);
      if (write(fd,buf,sizeof(buf)) == -1) {
         perror("write");
         exit(1);
      }
      if (out) {
        printf("Write %lu in %lld microseconds\n", lfsr *sizeof(buf), microseconds()-start);
        fflush(stdout);
      }
      if (fd == fd0) { 
         FLOCK(fd, LOCK_UN);
      }
      usleep(20000);
   }
   close(fd);
   exit(0);
}
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to