Subject: Re: [linux-audio-dev] info point on linux hdr
From: Jussi Laako (jussi_AT_jlaako.pp.fi)
Date: Fri Apr 21 2000 - 19:00:27 EEST
Paul Barton-Davis wrote:
>
> also, in the code you show, the ftruncate is redundant, because it
> doesn't allocate any blocks, and the point of preallocation (if there
> is one) is to get the blocks allocated in a certain way.
It depends on where in the block we write the data. If we write data to
start of the block and without ftruncate() we get file size
filesize = requested_file_size - blocksize + write_data_size
Allocated block count is same...
I got little better preallocation performance with
ftruncate()/lseek()/write() compared to write() of full blocks.
Test source follows
--- 8< ---
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#define TEST_FILE_SIZE 33554432l
#define TEST_BLOCK_SIZE 4096l
double ConvTime(const struct timeval *spTime)
{
double dTime;
dTime = (double) spTime->tv_sec + (double) spTime->tv_usec / 1000000.0;
return dTime;
}
int main()
{
int iHandle;
long lFilePos;
double dStartTime;
#ifdef TEST_WRITE_ONLY
long lWriteRes;
char cpData[TEST_BLOCK_SIZE];
#endif
struct timeval sCurrentTime;
iHandle = open("test.file", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (iHandle < 0)
{
printf("open() failed: %s\n", strerror(errno));
return 1;
}
gettimeofday(&sCurrentTime, NULL);
dStartTime = ConvTime(&sCurrentTime);
#ifndef TEST_WRITE_ONLY
if (ftruncate(iHandle, TEST_FILE_SIZE) < 0)
{
printf("ftruncate() failed: %s\n", strerror(errno));
return 1;
}
for (lFilePos = 0l;
lFilePos < TEST_FILE_SIZE;
lFilePos += TEST_BLOCK_SIZE)
{
if (lseek(iHandle, lFilePos, SEEK_SET) < lFilePos)
{
printf("lseek() failed: %s\n", strerror(errno));
return 1;
}
if (write(iHandle, &lFilePos, sizeof(long)) < sizeof(long))
{
printf("write() failed: %s\n", strerror(errno));
return 1;
}
}
#else
lFilePos = 0l;
while (lFilePos < TEST_FILE_SIZE)
{
lWriteRes = write(iHandle, cpData, TEST_BLOCK_SIZE);
if (lWriteRes < TEST_BLOCK_SIZE)
{
printf("write() failed: %s\n", strerror(errno));
return 1;
}
lFilePos += lWriteRes;
}
#endif
fsync(iHandle);
close(iHandle);
gettimeofday(&sCurrentTime, NULL);
printf("Operation took %.2f seconds\n",
ConvTime(&sCurrentTime) - dStartTime);
return 0;
}
--- 8< ---
- Jussi Laako
-- PGP key fingerprint: 161D 6FED 6A92 39E2 EB5B 39DD A4DE 63EB C216 1E4B Available at: ldap://certserver.pgp.com, http://keys.pgp.com:11371
This archive was generated by hypermail 2b28 : Fri Apr 21 2000 - 19:53:54 EEST