LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   fsync() is slow on some systems (http://www.linuxquestions.org/questions/linux-general-1/fsync-is-slow-on-some-systems-178569/)

brian_p_sully 05-06-2004 10:58 AM

fsync() is slow on some systems
 
I have code that writes to a specified file and after each line (around 300 bytes) is written it calls fsync() on the file to force the changes to disk. The file in question is fairly small, under 10MB. On some servers the program takes about 1 second to run with it writing about 1200 lines. On other systems though it takes around 70 seconds. If I take out the fsync at the end of each write then the program runs in under a second on all systems. All the servers are only a couple years old and are mostly idle. They are all different model Compaq/HP servers with various 2.4 kernel revisions (RedHat 7.3).

I assume there must be some configuration parameter somewhere on the servers that is different and causing the long delays since its the same program on all the boxes. Anyone have any suggestions where to look? Or any other theories to the cause of the time difference?

I'm pretty sure the size of the file is irrelevant, I modified my test program to rewrite the same line of the file over and over (so file was only 317 bytes big) and it took the same amount of time.

Here is my test program that times creating the file with and without fsync():


#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

int createFile( char *fname, int doSync );

char *NAME1 = "slowsync1.txt";
char *NAME2 = "slowsync2.txt";
int NUMLINES = 1296;
char *LINE = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAA\n"; // 317 BYTES LONG

int main( int argc, char *argv[] )
{

if ( createFile( NAME1, 0 ) < 0 ) {
printf( "ERROR: Couldn't create file1, aborting...\n" );
exit( 0 );
}

if ( createFile( NAME2, 1 ) < 0 ) {
printf( "ERROR: Couldn't create file2, aborting...\n" );
exit( 0 );
}

exit( 0 );

} // main


int createFile( char *fname, int doSync )
{
int fd;
int x;
struct timeval start, end, diff;

gettimeofday( &start, NULL );

fd = open( fname, O_CREAT | O_WRONLY , 0660 );
if ( fd < 0 ) {
printf( "ERROR: Can't open %s, errno=%d\n", fname, errno );
return( -1 );
}

for ( x = 0; x < NUMLINES; x++ )
{

if ( write( fd, LINE, strlen(LINE) ) < 0 ) {
printf( "ERROR: Can't write to %s, errno=%d\n", fname, errno );
return( -2 );
}

if ( doSync == 1 ) {
if ( fsync( fd ) < 0 ) {
printf( "ERROR: Can't fsync %s, errno=%d\n", fname, errno );
return( -3 );
}
}
}

close( fd );

gettimeofday( &end, NULL );
if ( end.tv_usec < start.tv_usec ) {
end.tv_sec--;
end.tv_usec += 1000000;
}

diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_usec = end.tv_usec - start.tv_usec;

printf( "%s created in %d.%06d seconds", fname, diff.tv_sec, diff.tv_usec);
if ( doSync == 1 )
printf( " with fsync\n" );
else
printf( " \n" );

return( 0 );

} // createFile


All times are GMT -5. The time now is 02:38 PM.