self piping and context switching
Assume a thread is monitoring a set of fd's which include both ends of a pipe (using poll, for example). If the thread writes to the pipe (in order to notify itself for whatever reason) is it reasonable to expect that it will be able to return to its poll loop and get the event without a context switch? (provided it quickly returns to the poll loop).
Regards,
Iker
Quoted 7 lines
Assume a thread is monitoring a set of fd's which include both ends of a pipe (using poll, for example). If the thread writes to the pipe (in order to notify itself for whatever reason) is it reasonable to expect that it will be able to return to its poll loop and get the event without a context switch? (provided it quickly returns to the poll loop).
It's reasonable to expect that this will be the most common case and the
one to optimize. It is unreasonable to fail if this doesn't happen, since it's not guaranteed to happen. Note that if by "without a context switch" you really mean without another thread getting a chance to run, then it is totally unreasonable. On SMP systems and with hyper-threading, threads can run concurrently.
DS
More specifically, I was wondering if the write to the pipe or the call back into poll involved anything that might prompt the scheduler to replace the thread in this scenario.
Quoted 5 lines
It's reasonable to expect that this will be the most common case and the one to optimize. It is unreasonable to fail if this doesn't happen, since it's not guaranteed to happen. Note that if by "without a context switch" you really mean without another thread getting a chance to run, then it is totally unreasonable.
Are you referring to transitions to/from kernel space? If so, wouldn't the write
on the pipe and the call to poll both result in transitions?
Regards,
Iker
----- Original Message -----
From: "David Schwartz" <[email protected]>
To: "Iker" <[email protected]>; <[email protected]>
Sent: Friday, September 12, 2003 10:04 PM
Subject: [lkml] RE: self piping and context switching
Quoted 24 lines
Quoted 7 lines
Assume a thread is monitoring a set of fd's which include both ends of a pipe (using poll, for example). If the thread writes to the pipe (in order to notify itself for whatever reason) is it reasonable to expect that it will be able to return to its poll loop and get the event without a context switch? (provided it quickly returns to the poll loop).
It's reasonable to expect that this will be the most common case and the one to optimize. It is unreasonable to fail if this doesn't happen, since it's not guaranteed to happen. Note that if by "without a context switch" you really mean without another thread getting a chance to run, then it is totally unreasonable. On SMP systems and with hyper-threading, threads can run concurrently.
DS
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 5 lines
More specifically, I was wondering if the write to the pipe or the call back
into poll involved anything that might prompt the scheduler to replace the thread in this scenario.
Sure, it could, but there's no particular reason to expect it to. The one
exception would be if another thread was already blocked waiting for that particular pipe. In this case, your sending data on the pipe would unblock that thread, which could trigger a pre-emption.
Quoted 9 lines
Quoted 8 lines
It's reasonable to expect that this will be the most common case and the one to optimize. It is unreasonable to fail if this doesn't happen, since
it's not guaranteed to happen. Note that if by "without a context switch"
you really mean without another thread getting a chance to run, then it is
totally unreasonable.
Quoted 3 lines
Are you referring to transitions to/from kernel space? If so, wouldn't the write
on the pipe and the call to poll both result in transitions?
Yes, but there's no reason that transition should cause the kernel to want
to change processes. The kernel generally tries to let processes complete their timeslice. An exception could certainly be if the write to the pipe causes a new thread to unblock and that that thread has a higher dynamic priority (which it well might, since it was waiting).
Personally, I don't think the kernel should ever pre-empt threads/processes
with equivalent static priorities, but that's another whole argument. ;)
DS
Quoted 3 lines · Iker
More specifically, I was wondering if the write to the pipe or the call back into poll involved anything that might prompt the scheduler to replace the thread in this scenario.
Unless it happens to cause page faults or fill up the pipe nothing in paticular. Sending a message to yourself down a pipe is pretty standard in event based programs as a way of turning a signal from asynchronous event and thus nuisance to handle into a message.
Quoted 9 lines · Alan Cox
Quoted 3 lines · Iker
More specifically, I was wondering if the write to the pipe or the call back into poll involved anything that might prompt the scheduler to replace the thread in this scenario.
Unless it happens to cause page faults or fill up the pipe nothing in paticular. Sending a message to yourself down a pipe is pretty standard in event based programs as a way of turning a signal from asynchronous event and thus nuisance to handle into a message.
This "pretty standard" method is the reason Netscape 4.07 hangs on computers which are too fast: It writes too many events to a pipe before reading from the pipe, and the pipe fills.
Of course that is a programming error. Just a reminder to be careful :)
-- Jamie
Not to knarf at you, and not to pollute this list, but depending on what *else* you might be expecting on that pipe, this kind of code could deadlock with itself rather easily on the write() to the pipe. If nothing else is can write to the pipe, then the pipe has no use that could not be better served by a more local data item. A thread should "never" (trademark, all rights reserved 8-) control *itself* through this kind of mechanism.
It is far better, (and cheaper, and less likely to lose the CPU, etc.) to have a non-local thread-specific (managed, etc) data item that you check "just before" the poll.
(from a pure performance perspective) Because *any* kernel call is an opportunity to lose you your timslice, the sequence you mention is a write() a poll() and presumably a read() to clear the state. That is three chances to get "anti-scheduled" (8-) between your decision to "wake yourself" and the actual desired processing.
Consider the truth of each of the following:
1) If you can "find" the file descriptor for "this thread" then you could set up and "find" a simple boolean/integer flag that you could test before even calling the poll(). [This is a self-wake right? So why poll() if you can avoid it.]
2) If the pipe-special device/file is private to the thread, the same thing can be accomplished by a simple queue/stack data structure. You don't even need locking because it is private so there is no possibility of contention.
2a) In this private case, the thread can never wake itself inside the poll anyway, because it is waiting inside the poll() [if you catch my meaning.]
3) If the pipe-special device/file is *NOT* private, but this thread is its only reader, then it is possible that the pipe could be full, in which case the write() would block waiting for a read that, by definition, will never happen.
3a) see item 2a.
4) If the pipe-special device/file is *NOT* private AND there are multiple readers, then you don't know which thread you would be waking (so the loss-of-timeslice issue is insoluble) and it is *STILL* possible (though less likely) for all the threads to form a completely deadlocked dependency graph with blocking write() semantics.
5) If you do the write() in non-blocking mode, and it would block, you have no means to communicate what you wanted to communicate, so you would have to discard the event. This leads to an unstable model, or it leads to writing the queue/stack mentioned above to solve this case.
The short version is: If a thread is writing to itself using a pipe, you almost certainly have a semantic problem that could be solved better and faster using local data or something.
I can't get more specific without seeing your code.
Rob White.
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Iker
Sent: Friday, September 12, 2003 6:19 PM
To: [email protected]
Subject: self piping and context switching
Assume a thread is monitoring a set of fd's which include both ends of a pipe (using poll, for example). If the thread writes to the pipe (in order to notify itself for whatever reason) is it reasonable to expect that it will be able to return to its poll loop and get the event without a context switch? (provided it quickly returns to the poll loop).
Regards,
Iker
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/