Hello,
I am going to ask a question regarding
write and lseek. I will provide code at the end of this, but first
some background.
I am trying to identify the cause of some latency in writing to disk.
My user claims that performance is much slower on SAN than on local
disk. The developer provided me a C++ program that performed a write
test that confirmed his suspicions. I modified the code to better
fit
my needs which it does now.
What I found during the test is that fsync is an expensive operation
and will block waiting for a confirmation from the disk device. What
I am trying to understand is the lseek function.
From what I read, it simply moves the pointer in the file descriptor
as directed. When I use this lseek function, writes are faster.
My question is why? When I use the write command, does the pointer
get reset and on each write, it will search for EOF?
This is running Linux sytem.
Thanks in advance:
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
struct timeval start, end;
double usecs;
long val;
int ch, fd, idx, ops, numThreads;
char *fname= "";
int filesize = 40000000;
int bytes = 0;
bool dosync = true, doSeek=false;
bytes = 0;
ops = 0;
char *buf = new char[bytes];
fname = argv[1];
while (( ch = getopt(argc,argv, "b:o:f:sl")) != EOF)
switch (ch) {
case 'b' :
bytes = atoi(optarg);
break;
case 'o' :
ops = atoi(optarg);
break;
case 'f' :
fname = (optarg);
break;
case 's' :
dosync = false;
break;
case 'l' :
doSeek = true;
break;
}
argc -= optind;
argv += optind;
gettimeofday(&start,NULL);
memset(buf,0,bytes);
if ( dosync ) {
printf("Processing %d bytes with %d Operations of fsync :
\t", bytes,ops);
} else {
printf("Processing %d bytes with %d Operations of fsync :
\t", bytes,1);
}
// unlink(fname);
if ((fd = open(fname, O_RDWR | O_CREAT, 0666)) == -1)
{
int errNum = errno;
printf("ERROR: failed to open %s: n",fname);
return(0);
}
for ( int idx(0) ; idx < ops ; idx++)
{
if (write(fd, buf, bytes) != bytes)
{
printf("write: \n");
exit (1);
}
if ( dosync ) {
if (fsync(fd) != 0)
{
printf("fsync: \n");
exit (1);
}
}
if ( doSeek )
{
if (lseek(fd, (off_t)0, SEEK_SET) == -1)
{
printf("lseek: %s\n",
strerror(errno));
exit (1);
}
}
}
// One last sync
if (fsync(fd) != 0)
{
printf("fsync: \n");
exit (1);
}
gettimeofday(&end,NULL);
int totalSec = 0;
long totalUSec = 0;
if (start.tv_usec end.tv_usec) {
end.tv_usec += 1000000;
end.tv_sec--;
}
totalSec = end.tv_sec - start.tv_sec;
totalUSec = end.tv_usec - start.tv_usec;
int t = totalSec + (totalUSec / 1000000);
printf("%ld Hours ",t / ( 60 * 60));
t %= (60*60);
printf("%ld Minutes ",t / 60);
t %= 60;
printf("%ld.%ld Seconds ",t ,totalUSec);
printf("%ld.%ld Seconds\n ",totalSec ,totalUSec);
} 3 5243
golden wrote:
I am going to ask a question regarding
write and lseek. I will provide code at the end of this, but first
some background.
[..]
What I found during the test is that fsync is an expensive operation
and will block waiting for a confirmation from the disk device. What
I am trying to understand is the lseek function.
From what I read, it simply moves the pointer in the file descriptor
as directed. When I use this lseek function, writes are faster.
My question is why? When I use the write command, does the pointer
get reset and on each write, it will search for EOF?
This is running Linux sytem.
[..]
First a nit pick: 'write' is not a command. It's a function. IIRC,
it's a POSIX function, which isn't really on topic here. Now that's
out of the way, second, in C++ we'd use the 'fwrite' function (from
the C Standard Library). Have you tried switching to using 'fwrite'
instead?
And the last point: you might want to consider asking in the Linux
newsgroup since I/O performance depends greatly on the platform, and
there is no real explanation from the language point of view why
'write' is so slow without 'lseek'.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Nov 16, 3:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
golden wrote:
I am going to ask a question regarding
write and lseek. I will provide code at the end of this, but first
some background.
[..]
What I found during the test is that fsync is an expensive operation
and will block waiting for a confirmation from the disk device. What
I am trying to understand is the lseek function.
From what I read, it simply moves the pointer in the file descriptor
as directed. When I use this lseek function, writes are faster.
My question is why? When I use the write command, does the pointer
get reset and on each write, it will search for EOF?
This is running Linux sytem.
[..]
First a nit pick: 'write' is not a command. It's a function. IIRC,
it's a POSIX function, which isn't really on topic here. Now that's
out of the way, second, in C++ we'd use the 'fwrite' function (from
the C Standard Library). Have you tried switching to using 'fwrite'
instead?
And the last point: you might want to consider asking in the Linux
newsgroup since I/O performance depends greatly on the platform, and
there is no real explanation from the language point of view why
'write' is so slow without 'lseek'.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks... the nitpicking will make me better, so I welcome that. I am
so used to programming in perl the "command" seems automatic. I will
try the fwrite and visit the linux group. Thanks for the reply.
On Nov 16, 9:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
golden wrote:
I am going to ask a question regarding
write and lseek. I will provide code at the end of this, but first
some background.
[..]
What I found during the test is that fsync is an expensive operation
and will block waiting for a confirmation from the disk device. What
I am trying to understand is the lseek function.
From what I read, it simply moves the pointer in the file descriptor
as directed. When I use this lseek function, writes are faster.
My question is why? When I use the write command, does the pointer
get reset and on each write, it will search for EOF?
This is running Linux sytem.
[..]
First a nit pick: 'write' is not a command. It's a function. IIRC,
it's a POSIX function, which isn't really on topic here. Now that's
out of the way, second, in C++ we'd use the 'fwrite' function (from
the C Standard Library). Have you tried switching to using 'fwrite'
instead?
It won't work. He's using a functionality (synchronized
writing) which isn't available in the standard library. The
most you can ever guarantee with the standard library (either
FILE* or iostream) is that the data has been transfered to the
OS; his call to fsych guarantees that it has been physically
written on the medium.
And the last point: you might want to consider asking in the Linux
newsgroup since I/O performance depends greatly on the platform, and
there is no real explanation from the language point of view why
'write' is so slow without 'lseek'.
With regards to his particular question, the answer seems
obvious (and will probably be the same on any system, anytime he
doesn't use synchronized writes): because of the seek, he's
always writing the data at the same place on the disk, which
means that the system can always reuse the same sector cache,
and never has to go to disk. Without the seek, he's writing a
fairly large file, and the system probably won't keep all of the
cached data around, but will write to disk.
Is it really surprising that writing a file with one record is
significantly faster than writing one with ops records (where
ops is probably fairly large)?
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Zheng Da |
last post by:
I don't know where should I ask the question, so send the email to this
group.
I choose this group, because I want to write the program with c++ :)
I want to write a program which support...
|
by: Stumped and Confused |
last post by:
Hello,
I really, really, need some help here - I've spent hours trying to find a
solution.
In a nutshell, I'm trying to have a user input a value in form's
textfield. The value should then be...
|
by: Andrea |
last post by:
Hi there -
I'm hoping someone can help me; I've been struggling with this for a
few days! :-)
I have a webpage that is comprised of many forms containing questions.
As the user answers one...
|
by: Elephant |
last post by:
I know what lseek does, but i need an example with simple positioning and
reading the file after positioning for 5 bytes from the beggining(the whole
code) not a line like : lseek(........) and...
|
by: sonnystarks |
last post by:
I am taking a course in writing javascript and it (and all the books I
have been reading) tell me that if I will use the document.write
syntax, I will be able to "place text on the page."
None...
|
by: Newbie Coder |
last post by:
MFC Application VC++.NET 2003
I have a certain registry key (HKCU\Software\MyKey) that contains between 30
& 64 string values
I need to write a '*' to all those 30 - 64 string values under...
|
by: venkat |
last post by:
Hi,
I am learing Unix internals. I have come across a problem where
i am not able to understand what is happening. As i gone through the
book i found that lseek will give the physical descriptor...
|
by: cjard |
last post by:
I have a client and server that enjoy the following simple dialogue:
Client connects
Client sends request
Server sends response
Client disconnects
This is the way it must be. The response...
|
by: pal |
last post by:
Hi,
I want to move file pointer to the end of the file through any
funciton ( like lseek() )
Could anyone help me out to dothis .
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |