[Y-devel] Adventures in threading

Erik erikhill at hawaii.rr.com
Mon Mar 1 03:53:08 GMT 2004


Woops, accidentally replied directly to Mark, when I meant to send this to the 
mailing list.  Wouldn't want to be accused, by some future SCOundrel, that 
"not all contributions to y-windows were public".

--------------------------------------------------------------------------------------------------

> > Just curious.  I'm trying to decipher the exact process by which the Y
> > window system does its thing (ladder diagrams and all that) in
> > preparation for threading it.
> >
> > Actually, an overview of the main process loop might save me some
> > code-exploring time, but I think I've got the basic idea anyway.
>
> OK.  One Y `iteration' is kicked off by one of:
>

Wow, that's a great overview, my understanding of the system has grown quite a 
bit.  I did notice that interesting "coincidence" that a single select() 
launched each iteration on its own.  It's a great design, very simple.  I'm 
sorry about my plans to complicate it :) Of course it's done in the spirit of 
the hope of specific improvements.

My goal isn't precisely to thread Y, although I do believe that that is the 
best approach to getting it to behave in the way I'm thinking.  What I really 
want to do is, in order of importance:

#1:  Reduce its latency.  I'm defining latency as the maximum amount of time 
that it can take between a user action, done with the expectation of visual 
response, and the visual response.  For example, I move the mouse, the mouse 
pointer tracks my movements.  I click a button, that button changes to the 
selected form, and so on.  Some actions, such as dragging a window, have an 
expectation of smoothness which is really just a form of latency.  
Specifically, for tracking actions (dragging an icon, dragging a window, 
resizing a window, moving the mouse) it should keep up with the refresh rate 
of the monitor, if that's possible in hardware.  This could also save CPU 
work, by eliminating "frames" that wouldn't be shown anyway, and the 
associated work.

#2:  Avoid inversion.  Most of the time, the Y server will be running locally.  
Therefore, it's possible for a low-priority client to be able to prempt a 
higher priority client just by causing the Y server to do more work.  Since 
the Y server itself will be running at normal priority, it makes it possible 
for a low priority task to get more CPU time than it otherwise should.  Linux 
2.6 and other kernels adjust priority based on "interactiveness" of a task, 
the Y server will always be interactive (in the sense that it follows a 
sleep/wake pattern as opposed to continously running at full throttle).  This 
worsens the situation with low-priority clients, as high priority, and highly 
interactive clients fight for CPU time against the Y server itself.  
Strangely, this situation is even worse over networks.  With a network, it's 
more difficult to get information about the priority or interactiveness of 
the client.  They don't come with any information other than, "Hi, I'd like 
to be a client.  Can I have an ID?"

#3:  Make some use of multiple CPU's.  This is the least important of what I'm 
trying to do, simply because so few machines are multi-CPU, and most are fast 
enough where sharing the Y server's duties between multiple CPU's won't 
change much.  But there are scenaries where that extra power could come in 
useful, in improving latency and in completing tasks faster (throughput).

Fundamentally, I'm shooting for a smooth, silky, responsive feel.  I know that 
simply adding CPU and graphics card power to the mix will not achieve it, but 
can of course improve things.

There are, of course, other ways than threading which can help.  For example, 
dynamically changing the order of the events in the queues can help 
significantly, though we would need to know what we can reorder.

Currently, my plan of attack is as follows, although I'm hoping that my plan 
gets attacked first.  Any weak points in it that you or anyone notice, please 
feel free to bring them up, I encourage alternative ideas and will implement 
anyone's ideas in this area if it's felt that they are superior.

Plan:

#1:  The main thread waits for client registrations.  During the client 
registration phase, create a new handler thread for each client.

#2:  Each client thread will wait for messages, like the main thread does now.  
It will adopt the (base) priority of the client which launched it, which will 
require a small change to the client registration message.  As messages come 
in, they will be handled as much as possible in that thread.  If the thread 
comes to a "dirty rectangle propogation" scenario where it needs to propogate 
a dirty rectangle to a window/widget which belongs to another client/thread, 
it will simply send one or more messages to cover that event, then drop out.  
From then on it will be handled by the new thread, as soon as it gets the 
message.  If it needs further propogation, it is handled by the new thread in 
exactly the same way.  This way, a dirty-rectangle-propogation event may span 
several threads, but is still done strictly in order.  Windows overlap all 
the time, but dirty rectangles tend to be unobstructed windows (that's where 
the user action usually is) which means that all this thread-passing may be 
rare.  Fully opaque windows, which I believe will be the most common case, 
can also skip some propogation.

#3:  The main thread also waits for viewport-ready-to-refresh messages.  If it 
has already had one since the last hardware refresh, it will drop that old 
one.  In any case it waits until the next hardware refresh event, then locks 
everything down, on the surface only.  Dirty rectangle messages can propogate 
through the ranks, on their various threads, but not to the top level.  The 
current buffers which make up the surface screen are then "harvested" and 
displayed.

To do this, I need of course some kind of message system between the threads 
only, and it has to be very fast.  Events that once were simply "do it now" 
scenarios become queued, message based events.

However, if the CPU's time is split up between multiple tasks, task-switching 
1000 times a second or so, rather than working solely in the order of 
request, then two things will happen.  First, as a general rule, the 
throughput will be less.  But also, and this I believe is desireable, the 
time between any given user input and some kind of visual response will be 
less, possibly substantially less, since the CPU is spending at least some 
portion of its time on it.  I believe that this is particularly important for 
Y, since Y acts also as the widget library.  In a sense, this makes Y more 
"monolithic" which has the potential of actually increasing the latency (as 
compared to X) for certain kinds of operations.  Imagine that you have two 
separate clients, under X.  Each hooks to, for the sake of argument, the same 
GUI toolkit.  But in this scenario, if they both decide to do widget update 
at the same time, the X server itself might get a mix of messages -- some 
update sequences from one client, some from the other.  Client A may be able 
to at least partially update it's widgets even if client B is also trying to 
do the same.  Under Y, one update must complete before another begins, even 
if, unfortunately, the one that's updating currently is blocking the one that 
the user is paying attention to.  Of course, your current timing tests 
suggest that doing widget updates in the server is so fast, that this effect 
is never actually observed.  It might be observable on particularly 
complicated and fancy themes, however, that do a lot of widget work, or on 
slower machines.

Another point - as CPU's and GPU's get faster and faster, we run into perhaps 
a more serious problem, where the CPU and GPU are doing more work and updates 
than are useful, thus wasting cycles that could be used on some other task.  
Imagine text in a terminal.  Most terminals have some kind of "jump" code in 
there so that if data comes streaming in too fast to be useful, it no longer 
scrolls on every line or draws every character, but rather, does the work in 
bigger chunks, less often.  But this logic belongs naturally in the visual 
server, in this case Y, because it's Y that stands between what the software 
wants to display and what is actually shown on the screen.  It could even be 
done such that dirty rectangles are merged at lower levels and "duplicate 
regions" are thrown out at each level, before propogation, if there still 
hasn't been a hardware refresh.

In a sense, I'm glad the current implementations are so slow (SDL, 
framebuffer) so that these effects are easier to see at this stage.

>   A file descriptor becoming ready for writing
>   A file descriptor becoming ready for reading
>   A timer expiring
>
> It's no coincidence that these are the properties of select(2) and
> family.
>
> These could mean one of:
>
>   An IPC channel is ready for a message to be sent
>     - in which case the message is sent from the queue and control returns
>
>   An IPC channel is ready for a message to be received
>     - in which case the message is received and despatched to the
>       appropriate part of the server (class, object, or special
>       interface).  This part can then do whatever it likes to
>       handle the message.  The final result will usually be
>       a combination of:
>
>        + one or more messages being sent.
>             these are queued for sending.
>
>        + part of the widget tree being modified,
>             in which case "dirty" rectangles are passed up through the
>             tree to the viewports that they are contained within, which
>             add them to their dirty region lists. On the way, buffered
>             widgets also take note of the parts of the dirty regions that
>             fall within their buffers.
>
>   An object timer expires
>     - the object's timer handling routine is called, this can do the same
>       as the previous item, and bring about the same results.
>
>   A viewport is ready to refresh
>     - in which case the viewport creates a "Renderer" object for each of
>       its dirty regions, and passes that to the root widget of the
>       widget hierarchy.  This renderer is propagated down the widget
>       hierarchy. Doing two things:
>
>        + buffers of buffered widgets, such as Window, whose contents may
>          have changed are refreshed by calling Paint on the child widgets
>          of the buffered widgets.
>
>       then
>
>        + the buffers are rendered onto to the viewport via the
>          Renderer.
>
> Personally, I am dubious as to whether this can be effectively threaded.
> Each event can have widespread effects on the objects in the server:
> you're going to have to do a lot of locking.  The simplicity and
> safety of a single-threaded model is very tempting to try and keep.
> You're welcome to try threading it, of course, and if there are noticable
> benefits, I'll certainly look at it.
>
> Regards,
>
>   Mark.



More information about the Y-devel mailing list









2026 © www.Y-Windows.Org - All Rights Reserved