Hi Willy
('t is toch altijd weer plezant om hier landgenoten terug te vinden ;-) )
I tried implementing the class using the different solutions .NET offers,
but in the end the one using WriteFileEx was really the fastest (according to
my tests, please convince me that I'm wrong, cause I don't like it
either...). The speed I'm aiming at is btw 150Mb/s on a SCSI Raid System...
The way I implemented it:
1) The File handle:
mFHandle = SystemAPI.Creat eFile(FileName,
FileAccess.Writ e,
FileShare.Write ,
0,
FileMode.Create ,
FILE_FLAG_OVERL APPED|FILE_FLAG _SEQUENTIAL_SCA N |
FILE_FLAG_NO_BU FFERING,
IntPtr.Zero);
As you can see I use the NO_BUFFERING option, this resulted in a significant
speed improvement... but it requires you to take sector sizes into account
(that's why I don't like it...)
2) Using the FilePointer I give the file it's final size (rounded to the
next sector size), to prevent the system resizing after every write...
3) The actual write routine:
internal unsafe void WriteBufferToDi sk(int Length)
{
IOCompletionCal lback _IOComp = new IOCompletionCal lback(IOComplet e);
Overlapped _ovl = new Overlapped();
_ovl.OffsetLow= mWritePtr;
_ovl.AsyncResul t = new myAsyncResult(m Indx);
mOvrlBuf[mIndx]=_ovl;
SystemAPI.Write FileEx(mFHandle ,mStreamingBuff ers[mIndx].Bytes,Length,m OvrlBuf[mIndx].Pack(_IOComp), _IOComp);
mIndx++;
mActiveWrites++ ;
if(mIndx==8)
mIndx=0;
mWritePtr+=Leng th;
}
You'll see something weird with the mOvrlBuf[], I agree... basically I keep
an array of Overlapped close, so I can do upto 8 writes "at the same time"
without having to look for the overlapped when finished... the array is
organized as a FIFO... This way I make sure the overlapped is never destroyed
before it's not needed anymore
4) The completion routine
private unsafe void IOComplete(uint Lo, uint Hi, NativeOverlappe d * NatOvrl)
{
Overlapped _ovl = Overlapped.Unpa ck(NatOvrl);
//int _indx = (int)_ovl.Async Result.AsyncSta te;
//I don't unpin, since my Streaming buffer handles this...
// UnPinBuffer(mGC Handles[_indx]);
Overlapped.Free (NatOvrl);
mActiveWrites--;
}
5) The pacing of the System happens using:
internal bool ReadyForData
{
get
{
return mActiveWrites<4 ;
}
}
I use a Async reset event at first.... but this "don't do it like this!"
method proved to be faster...
Anyway, as I said in my first post... once you put this in a thread it's not
async anymore. Any ideas ??
Kindest regards,
Tom
"Willy Denoyette [MVP]" wrote:
Any reason why you aren't using FileStream.Begi nRead for asynchronous IO?
I guess you are expecting an asynchronous WriteFile to be faster than a
synchronous one, however, I'm afraid this is not the case and it's not the
purpose of the API. This API is useful whenever you need to execute some
code after the call returns, but before the IO completes.
Anyway, without more details and some code that illustrates the problem,
it's hard to tell you why you see this different behavior.
Willy.
"Tom Vandeplas" <to***********@ agilent.com> wrote in message
news:10******** ******@cswreg.c os.agilent.com. .. Hi all,
When using an asynchronous API call (in my case WriteFileEx) in a thread
it
seems to become blocking instead of non-blocking (see details below). Can
somebody explain me what is happening here...
Details:
I wrote a simple program that is able to do very fast async writes to
disk.
For that I used the WriteFileEx API call which is by nature asynchronous.
At
first I wrote a simple console app to verify the concept, it worked fine.
Once integrated it in my main app, it became a lot slower. After
investigating a little, it seemed that the call became blocking instead of
non-blocking. The main difference between the console app and my main app
is
that I put the calling method inside a worker thread. Does anybody know
how
to fix this, make it async again ?
Kindest regards,
Tom
PS: My API call goes like this:
[DllImport("kern el32.dll", SetLastError=tr ue)]
public unsafe static extern int WriteFileEx ( IntPtr hFile,
byte[] lpBuffer,
int nNumberOfBytesT oWrite,
NativeOverlappe d * lpOverlapped,
IOCompletionCal lback lpCompletionRou tine);