473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

FileStream.EndRead, Does it cause a context switch?

Hi,

I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause a context switch? What is the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed, usage of critical section prevent this switch from happening... what is the object used by EndRead? will it cause a context switch? My Guess is that the EndRead uses an Event as the OVERLAPPED structure used with IO Completion routines use an event for completion indications, if my assumption is true calling EndRead from the completion callback will cause an un-neccesary context switch, one that can be saved by querying the amount of bytes read in a way other then calling EndRead... ( is there such a way? Is there a non-explicit interface to IAsyncResponce? Can it be cast to somewhat more detailed structure? )

Note that I use EndRead in the completion callback merely to get the amount of bytes read, if I could get this amount by in a way other then calling EndRead It would solve the problem... Is there another way of getting the amount of read bytes? is there a way of extracting the internal OVERLAPPED structure from the IAsyncResult object?

Nadav
http://www.ddevel.com
Nov 16 '05 #1
5 3659
Nadav,

The FileStream does in fact use IO completion ports to handle async
operations. You can not cast the IAsyncResult to something more concrete to
get the result that you want. You will have to call the APIs through the
P/Invoke layer to do what you want.

Also, I am not quite sure what you are trying to do. You say that you
just want to get the number of bytes read. If you don't want to do anything
with the bytes, and you want to advance the stream pointer, then why not
just seek on the stream?

Finally, calling EndRead might call a context switch, depending on where
you call it. If you call it in a thread other than the callback thread,
then it might cause a context switch. However, if you call it in the thread
that fires the asynchronous notification, then you should not have a context
switch.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
Hi,

I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in
mind, I Wonder... does calling EndRead will cause a context switch? What is
the kernel object used for blocking EndRead calls? Event and mutex cause a
context switch even when the object is signaled and no wait is needed, usage
of critical section prevent this switch from happening... what is the object
used by EndRead? will it cause a context switch? My Guess is that the
EndRead uses an Event as the OVERLAPPED structure used with IO Completion
routines use an event for completion indications, if my assumption is true
calling EndRead from the completion callback will cause an un-neccesary
context switch, one that can be saved by querying the amount of bytes read
in a way other then calling EndRead... ( is there such a way? Is there a
non-explicit interface to IAsyncResponce? Can it be cast to somewhat more
detailed structure? )
Note that I use EndRead in the completion callback merely to get the amount of bytes read, if I could get this amount by in a way other then
calling EndRead It would solve the problem... Is there another way of
getting the amount of read bytes? is there a way of extracting the internal
OVERLAPPED structure from the IAsyncResult object?
Nadav
http://www.ddevel.com

Nov 16 '05 #2
Thanks for the immediate response, my remarks are given bellow
Nadav,

The FileStream does in fact use IO completion ports to handle async
operations. You can not cast the IAsyncResult to something more concrete to get the result that you want. You will have to call the APIs through the
P/Invoke layer to do what you want.

Also, I am not quite sure what you are trying to do. You say that you
just want to get the number of bytes read. If you don't want to do anything with the bytes, and you want to advance the stream pointer, then why not
just seek on the stream? Actually, I am analyzing the data that was read, to do that I have to know
how much of it was read.

Finally, calling EndRead might call a context switch, depending on where you call it. If you call it in a thread other than the callback thread,
then it might cause a context switch. However, if you call it in the thread that fires the asynchronous notification, then you should not have a context switch.

SOOooo...., From what you are saying could I conclude that the EndRead API
check to see if it was called in the context of the completion thread, in
this case no wait will be done and the OVERLAPPED.hEvent will not be waited
on?

Do you base what you are saying on common scenes or there is a formal
Microsoft documentation for this type of data, I would be happy to get any
pointer leading to this kind of stuff.


Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
Hi,

I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in
mind, I Wonder... does calling EndRead will cause a context switch? What

is the kernel object used for blocking EndRead calls? Event and mutex cause a
context switch even when the object is signaled and no wait is needed, usage of critical section prevent this switch from happening... what is the object used by EndRead? will it cause a context switch? My Guess is that the
EndRead uses an Event as the OVERLAPPED structure used with IO Completion
routines use an event for completion indications, if my assumption is true
calling EndRead from the completion callback will cause an un-neccesary
context switch, one that can be saved by querying the amount of bytes read
in a way other then calling EndRead... ( is there such a way? Is there a
non-explicit interface to IAsyncResponce? Can it be cast to somewhat more
detailed structure? )

Note that I use EndRead in the completion callback merely to get the amount of bytes read, if I could get this amount by in a way other then
calling EndRead It would solve the problem... Is there another way of
getting the amount of read bytes? is there a way of extracting the

internal OVERLAPPED structure from the IAsyncResult object?

Nadav
http://www.ddevel.com


Nov 16 '05 #3
Thanks for your responce, my remarks are bellow
Remember that the code is JIT compiled, the compiler may generate different code for pinned references than unpinned references. Still I would expect that the Assembler code provided by the unmanged
debugger while debugging the application will resemble the code AFTER it was
JITed so the corresponding GC '__pin' manipulations will have it's asm
signature...
If performance is that critical, I would consider using something else than .NET. .NET provides a level of abstraction that comes with a cost. Just out of interest, if you wrote this application in native C++, how would you
prevent Windows from moving the memory containing the buffers to the swap
file? I would use the Win32 VirtualLock/Unlock API, this will prevent page faults
while accessing the 'locked' range.
If you allocate the objects at the start of program execution, they would be located at the start of the managed heap. Objects are allocated continuously from the start of the managed heap, as objects become disposed the GC will
compact the heap to reduce the number of actual memory pages used. This
still wouldn't prevent Windows from paging all the memory to the swap file.
Chris

"Nadav" <not@today> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
Thanks for your immediate response, concerning the '__pin' keyword causes
the GC to prevent moving the buffer in memory ( and not manipulate any
refcount ), how does it do that? there must be some assembler code to
instruct the GC that this is a '__pined' variable so it would know not to move it, looking at the assembler code produced by the compiler (provided
at
the root query) I can't see anything as such... It seems as the '__pin'
keyword has no effect on the assembler code being generated by the compiler,
why is that? how can it be guaranteed that the memory pointed to by
'pbtArray' will stay valid for the duration it is being used...

(*)Another thing: Taking in mind what you have said concerning the way

GC is
operating, Is it possible to prevent the GC from moving memory in the

first
place? e.g. lets say I have a performance critical application, I

allocate (apriory) a collection of buffers to be used by the App, those buffers
should be moved as less as possible during runtime, how can I achieve that using the .NET Framework????

Nadav.
"Christopher Kimbell" <a@b.c> wrote in message
news:40********@news.broadpark.no...
The garbage collection is done by the runtime, not the application,

therefor
there is no GC code in your application code.
If Microsoft embedded GC manipulation code in the apllication code, it

would
be impossible to change GC policy in the future.

Pinning an object prevents the GC from moving the reference in memory,

it has nothing to do with reference counting. The GC periodically moves

things
around so that the memory used is located together in memory.

Chris

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
> Hi,
>
> I am writing a mixed mode application, I have a mixed mode Assembly
manipulating a managed byte[] array, to access this array in unmanaged

code
I '__pin' the array, As I understand, pining an object guarantee that it will not be collected by the GC ( by increasing it's refcount or so ),
Taking that in mind, looking at the code generated by the compiler I can't see anything taking care of the GC refcount... following is the pinned
variable with the corresponding dis-assembly section:
>
> ****************** Mixed mode C++ code line ******************
> BYTE __pin *pbtArray = &btArray[0];
>
> ******************** The dis-assembly ***********************
> 00000019 cmp dword ptr [esi+4],0 // makes sure that NULL !=
&btArray[0]
> 0000001d ja 00000026 // if it is not NULL go

three
lines bellow
> 0000001f xor ecx,ecx
> 00000021 call 7227F90B // call some exception
handling procedure...
> 00000026 lea eax,[esi+8] // get the address of
&btArray[0] to eax
> 00000029 mov dword ptr [ebp-14h],eax // pbtArray = content of eax > ************************************************** ******
> Why there is no GC manipulation code? isn't it needed?
>
> The original code:
> ************************************************** ******
> virtual int NotArray(System::Byte btArray __gc[])
> {
> int i = 0;
> BYTE __pin *pbtArray = &btArray[0];
> for(i; i < btArray->Length; i++)
> pbtArray[i] <<= 1;
> return 0;
> }
>
> Nadav
> http://www.ddevel.com



Nov 16 '05 #4
Nadav,

See inline:
Actually, I am analyzing the data that was read, to do that I have to know
how much of it was read.
This number is returned to you on the call to EndRead.

SOOooo...., From what you are saying could I conclude that the EndRead API
check to see if it was called in the context of the completion thread, in
this case no wait will be done and the OVERLAPPED.hEvent will not be waited on?
If you wait on the WaitHandle returned by the AsyncWaitHandle property
on the IAsyncResult interface, then the event will be waited on.

However, you don't necessarily have to do this. What you can do is
provide a callback to the call to BeginRead, and then when the callback is
completed, call EndRead. Calling EndRead at this point will not cause a
wait on the handle to be performed, because the operation is complete at
that point. The code that executes at that point is called in the thread
that received the IO Completion notification.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com



Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
Hi,

I am using FileStream's Async API: BeginRead/EndRead, upon completion

callback execution I use the read data and call EndRead, Taking that in
mind, I Wonder... does calling EndRead will cause a context switch? What

is
the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed,

usage
of critical section prevent this switch from happening... what is the

object
used by EndRead? will it cause a context switch? My Guess is that the
EndRead uses an Event as the OVERLAPPED structure used with IO Completion routines use an event for completion indications, if my assumption is true calling EndRead from the completion callback will cause an un-neccesary
context switch, one that can be saved by querying the amount of bytes read in a way other then calling EndRead... ( is there such a way? Is there a
non-explicit interface to IAsyncResponce? Can it be cast to somewhat more detailed structure? )

Note that I use EndRead in the completion callback merely to get the

amount of bytes read, if I could get this amount by in a way other then
calling EndRead It would solve the problem... Is there another way of
getting the amount of read bytes? is there a way of extracting the

internal
OVERLAPPED structure from the IAsyncResult object?

Nadav
http://www.ddevel.com



Nov 16 '05 #5
Well, Concerning I am calling the EndRead from within the callback context: win32 use the OVERLAPPED structure for IO completion operation, this struct has an event variable that could be waited on for the IO Completion call is completed ( similar to calling Stream.EndRead ).
Now, taking in mind what was just said, I guess the .NET Framework use the same language to communicate with the kerenl as win32 use, this means that the completion event of the Win32 OVERLAPPED structure used by Win32 may be used by the .NET Framework ( this is a part of the interface with the kernel ), I assume that EndRead internally waits on this event ( managed by the kernel ).
Waiting on an Event object will cause a context switch weather it is signaled or not ( in contract to a wait on a critical section ), some kernel implementation of the wait functionality should be executed in a kernel thread, to achive that a context switch is requiered. Taking that in mind, EndRead MUST ( and probabely do ) wait on this event when it is called in a context other then the AsyncCallback, however, it is possible that before waiting on the event a context check is done and the wait is called only for threads other then the one who called the AsyncCallback. I just want to make sure weather EndRead waits on the event and by doing so causes a context switch, if so I want to figure out a different way of extracting the amount of bytes read, a way that will not cause a context switch.

Nadav
http://www.ddevel.com
"Nicholas Paldino [.NET/C# MVP]" wrote:
Nadav,

See inline:
Actually, I am analyzing the data that was read, to do that I have to know
how much of it was read.


This number is returned to you on the call to EndRead.

SOOooo...., From what you are saying could I conclude that the EndRead API
check to see if it was called in the context of the completion thread, in
this case no wait will be done and the OVERLAPPED.hEvent will not be

waited
on?


If you wait on the WaitHandle returned by the AsyncWaitHandle property
on the IAsyncResult interface, then the event will be waited on.

However, you don't necessarily have to do this. What you can do is
provide a callback to the call to BeginRead, and then when the callback is
completed, call EndRead. Calling EndRead at this point will not cause a
wait on the handle to be performed, because the operation is complete at
that point. The code that executes at that point is called in the thread
that received the IO Completion notification.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com



Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
> Hi,
>
> I am using FileStream's Async API: BeginRead/EndRead, upon completion
callback execution I use the read data and call EndRead, Taking that in
mind, I Wonder... does calling EndRead will cause a context switch? What

is
the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed,

usage
of critical section prevent this switch from happening... what is the

object
used by EndRead? will it cause a context switch? My Guess is that the
EndRead uses an Event as the OVERLAPPED structure used with IO Completion routines use an event for completion indications, if my assumption is true calling EndRead from the completion callback will cause an un-neccesary
context switch, one that can be saved by querying the amount of bytes read in a way other then calling EndRead... ( is there such a way? Is there a
non-explicit interface to IAsyncResponce? Can it be cast to somewhat more detailed structure? )
>
> Note that I use EndRead in the completion callback merely to get the
amount of bytes read, if I could get this amount by in a way other then
calling EndRead It would solve the problem... Is there another way of
getting the amount of read bytes? is there a way of extracting the

internal
OVERLAPPED structure from the IAsyncResult object?
>
> Nadav
> http://www.ddevel.com



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
0
by: Martyn Wynne | last post by:
Hi, Can anyone please tell me if there is any reason why when i am streaming from a webrequest (decompressing on route) to a file on the hard drive, i would be getting an exception of Filestream...
5
by: Mike Robinson | last post by:
The Win32 SDK had a great function called "ReadFile" that was a model of simplicity. Now I'm starting to use the new .NET Framework, which is supposed to be an improvement but the file reading is...
9
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was...
13
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
15
by: Lou | last post by:
I can't get it to work. Please help... -louie hPipe = CreateFile(connectionString, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,...
11
by: Brian W | last post by:
Yet another editor problem To reproduce do the following 1) Open a Webform and switch to HTML edit mode 2) Enter the Following (include spaces) This is some text before <asp:hyperlink...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
20
by: Ryan Liu | last post by:
Hi, I have a client/server application, using one thread/client approach. I see very high context switch/sec. What are the ways to reduce it? Each thread sleep longer in its endless loop if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.