KSE, libpthread & libthr: almost newbie question

Robert Watson rwatson at FreeBSD.org
Fri Oct 27 13:02:59 UTC 2006


On Fri, 27 Oct 2006, Lev Serebryakov wrote:

>   I've was sure, that both libpthread and libthr use KSE to make 
> multithreading. They use KSE in different ways: libpthread uses N:M model 
> and libthr uses 1:1 model, but bot use KSE to work.
>   How will be possible to sue these libraries (read: multithreaded programs) 
> when KSE will be optional, on kernel without KSE?!

The answer to that rather critical question, not surprisingly, is complex. :-)

The FreeBSD kernel actually implements a good three different threading models 
(at least):

(1) Linux threads/rfork threads.  These are based on the idea of multiple
     "struct proc"s floating around with varying degrees of sharing.
     Typically, they share file descriptor array, address space, etc.  For
     those familiar with the Linux clone() system call, rfork() is notionally
     very similar.  This model implies 1:1 threading, and leaves quite a bit to
     be desired in terms of overhead, as well as having some odd implications
     for POSIX-like compliance (i.e., the implementation of getpid()).

(2) KSE.  This provides a kernel<->userspace threading framework allowing the
     implementation of a broad array of threading and scheduling models.  This
     is a fairly complex, but very flexible model, which has several levels of
     layering required to implement different scheduling policies using M:N
     threading.  Notice that 1:1 scheduling is a subset of M:N, so you can
     implement 1:1 in this manner.  One of the significant concerns about KSE
     is that it adds a great deal of complexity to the kernel scheduler
     architecture, making it quite difficult to optimize, further granularize
     the locking for, understand, etc.

(3) thr.  This is a simpler 1:1 threading API to the kernel, which make use of
     the same architectural structures present in the kernel for KSE, but
     without the full capability of M:N threading.  In particular, it has
     simplifying assumptions regarding how user threads enter the kernel and
     are mapped into kernel threads, so doesn't need an upcall mechanism, or
     create new threads when an existing thread sleeps.

The NO_KSE patch disables the code paths required only for (2), not for (1) or 
(3).  One of the current theories bouncing around the kernel developer 
community is that the complexity and overhead of (2) outweighs many of the 
benefits of KSE, and that by making it an option, we can better evaluate the 
impact.  Notice that this isn't just about code complexity, but also about 
scheduler overhead.  David Xu has reported a non-trivial performance change 
from the reduced overhead of the scheduler paths.  So now we're at a point 
where we can more fully evaluate the impact of KSE (since we can actually 
compile it out of the scheduler).  Before anything further can be done, we now 
need to do that evaluation.

Robert N M Watson
Computer Laboratory
University of Cambridge


More information about the freebsd-current mailing list