473,544 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileStream.EndR ead, 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 3674
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.co m

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:9F******** *************** ***********@mic rosoft.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.hEve nt 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.co m

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:9F******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP12.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.
"Christophe r 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***@discussi ons.microsoft.c om> wrote in message
news:DE******** *************** ***********@mic rosoft.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.hEve nt 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.co m



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

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:9F******** *************** ***********@mic rosoft.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.hEve nt 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.co m



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

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:9F******** *************** ***********@mic rosoft.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
4603
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 values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int...
0
1804
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 error System.StackOverflowException every time the file gets to 8580Kbs. Code is basically a modified Microsoft example, it errors on the line ...
5
6409
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 (as far as I can tell) nowhere near as good. FileStream fs=new FileStream(FName); fs.BeginRead (Buffer,0,thisFile.Length,0,0); The last two...
9
5839
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 working with the StreamReader object I got a deadlock when I tried reading the file while my external application was writing to it. So far I am able...
13
7411
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
10775
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, FILE_ATTRIBUTE_NORMAL,HANDLE.Zero); if ((hPipe.ToInt32()) == INVALID_HANDLE_VALUE)
11
3186
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 id="hl1" runat="server " navigateurl="http://www.microsoft.com">This is my link</asp:hyperlink> And this is my text after the Hyperlink
34
2752
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
310
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 not reducing thread? On the same machine, I also run a mysql server. I can see same amount thread in mysqld, seems mysql is also using one...
0
7370
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7714
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5305
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3415
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
674
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.