From owner-freebsd-current@FreeBSD.ORG Fri Oct 27 13:02:59 2006 Return-Path: X-Original-To: current@FreeBSD.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CFC7516A407; Fri, 27 Oct 2006 13:02:59 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from cyrus.watson.org (cyrus.watson.org [209.31.154.42]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7C3D043D53; Fri, 27 Oct 2006 13:02:59 +0000 (GMT) (envelope-from rwatson@FreeBSD.org) Received: from fledge.watson.org (fledge.watson.org [209.31.154.41]) by cyrus.watson.org (Postfix) with ESMTP id 57D8546D20; Fri, 27 Oct 2006 09:02:59 -0400 (EDT) Date: Fri, 27 Oct 2006 14:02:59 +0100 (BST) From: Robert Watson X-X-Sender: robert@fledge.watson.org To: Lev Serebryakov In-Reply-To: <917908193.20061027102647@serebryakov.spb.ru> Message-ID: <20061027103924.F79313@fledge.watson.org> References: <917908193.20061027102647@serebryakov.spb.ru> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: current@FreeBSD.org Subject: Re: KSE, libpthread & libthr: almost newbie question X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Oct 2006 13:02:59 -0000 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