Some thoughts about threads and deferred procedures
Posted: Tue Dec 04, 2007 8:54 pm
Here comes some fun things to think about...
Say, one has deferred procedures in kernel, and then one goes implementing stuff such that when some driver or subsystem wants to do some work in response to an interrupts, it sets a deferred procedure in order to not block futher interrupts... and life is good, right?
Pfft. There was a problem in Vista. The problem was that if you had a 1Gb network, and enough traffic, your media playback might not work reliably.
Why?
Well, obviously Windows handles the incoming network packets the way that's easiest: ack the interrupt, schedule a DPC for the packet, then run the network stack in that DPC... which is quite fine, until you need to have some lowish-latency stuff like audio or even video running at the same time..
Ok, MS fixed that by throttling network: it'll only handle a fixed amount of packets per given interval if there's real-time media going on..
But I could think of another solution: handle the networking in a separate thread instead... now it can be given a priority lower than the media playback real-time thread. Problem solved.
So mm.. I started thinking: DPCs should only be used for stuff that's higher priority than anything that ever runs in any thread, because only when something is given to a thread, it becomes possible to tweak the priorities.
In my case so far, there are not so many things.. the above mentioned network is going to be in a separate thread I guess. I'm a little scared though about the idea of having important kernel threads like network at priority lower than stuff that's running in userspace (say a real-time audio processing thread).. so guess it's a scheduler rewrite time as well (so I can have scheduler limit the amount of CPU the real-time userspace threads are allowed to spend, before they get temporarily demoted to lower-priority).
My VFS, which is currently in the middle of a gigantic rewrite anyway, is another potential place. Would there be any real advantage of putting the VFS into a thread so as to allow other stuff to run at priority higher? It's not like VFS does a lot of things without blocking on IO anyway, but what if it doesn't block long enough? Is it ok to allow VFS to starve other stuff?
Anyway, the bottom line would probably be: anything not running in a thread (but in DPCs) can potentially starve anything running in a thread. Which is not nice.
Where do you draw the line?
Say, one has deferred procedures in kernel, and then one goes implementing stuff such that when some driver or subsystem wants to do some work in response to an interrupts, it sets a deferred procedure in order to not block futher interrupts... and life is good, right?
Pfft. There was a problem in Vista. The problem was that if you had a 1Gb network, and enough traffic, your media playback might not work reliably.
Why?
Well, obviously Windows handles the incoming network packets the way that's easiest: ack the interrupt, schedule a DPC for the packet, then run the network stack in that DPC... which is quite fine, until you need to have some lowish-latency stuff like audio or even video running at the same time..
Ok, MS fixed that by throttling network: it'll only handle a fixed amount of packets per given interval if there's real-time media going on..
But I could think of another solution: handle the networking in a separate thread instead... now it can be given a priority lower than the media playback real-time thread. Problem solved.
So mm.. I started thinking: DPCs should only be used for stuff that's higher priority than anything that ever runs in any thread, because only when something is given to a thread, it becomes possible to tweak the priorities.
In my case so far, there are not so many things.. the above mentioned network is going to be in a separate thread I guess. I'm a little scared though about the idea of having important kernel threads like network at priority lower than stuff that's running in userspace (say a real-time audio processing thread).. so guess it's a scheduler rewrite time as well (so I can have scheduler limit the amount of CPU the real-time userspace threads are allowed to spend, before they get temporarily demoted to lower-priority).
My VFS, which is currently in the middle of a gigantic rewrite anyway, is another potential place. Would there be any real advantage of putting the VFS into a thread so as to allow other stuff to run at priority higher? It's not like VFS does a lot of things without blocking on IO anyway, but what if it doesn't block long enough? Is it ok to allow VFS to starve other stuff?
Anyway, the bottom line would probably be: anything not running in a thread (but in DPCs) can potentially starve anything running in a thread. Which is not nice.
Where do you draw the line?