From owner-freebsd-current@FreeBSD.ORG Tue Sep 23 10:02:03 2014 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 774FA2FB; Tue, 23 Sep 2014 10:02:03 +0000 (UTC) Received: from mail-we0-x234.google.com (mail-we0-x234.google.com [IPv6:2a00:1450:400c:c03::234]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DFC8FEEB; Tue, 23 Sep 2014 10:02:02 +0000 (UTC) Received: by mail-we0-f180.google.com with SMTP id u56so4257815wes.39 for ; Tue, 23 Sep 2014 03:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=ku6tY3bXOEEga3n9AEwK0QndVpZ8N/dCbEwXMCaKfrM=; b=M9k+WML0e85eL8MNNwdmrWQI6a2bA09NP60p3g96t/Djz5cL2ee08tyOBBBEEqKfQy 4LKXr25R8p6CgKVe7Zecc4ghyN9w7Zaf0wodkObnU3+0Ia6hBBhpJ0Z1BN+Gkvlix5xo Rookml/hEXBblTO5VWUPmOEwBzNGPBcOYDu287RJWvC+nXdPEq/M6kBmCONfxeazNR09 2tiTQHfAyBaAWwDMkQ+odGRFdyQAhbVCdmJdnU66hikLY4Heif3qgp0BVQxc5+Yoi8sb 5mWzffZv027pa5/kd8OE5OIr3JW6jwEP5OJGp6laKfdm+n2wUCqCqF27SAYbmpB+ojzn h0Kw== MIME-Version: 1.0 X-Received: by 10.180.74.227 with SMTP id x3mr1971277wiv.80.1411466521133; Tue, 23 Sep 2014 03:02:01 -0700 (PDT) Received: by 10.216.53.68 with HTTP; Tue, 23 Sep 2014 03:02:01 -0700 (PDT) In-Reply-To: <20140923092449.GB8334@kib.kiev.ua> References: <53E36E84.4060806@ivan-labs.com> <20140916081324.GQ2737@kib.kiev.ua> <5242716.s4iaScq0Bu@ralph.baldwin.cx> <541E31E0.8020108@freebsd.org> <20140923092449.GB8334@kib.kiev.ua> Date: Tue, 23 Sep 2014 14:02:01 +0400 Message-ID: Subject: Re: libthr and main thread stack size From: Sergey Kandaurov To: Konstantin Belousov Content-Type: text/plain; charset=ISO-8859-1 Cc: Daniel Eischen , "Ivan A. Kosarev" , FreeBSD Current X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.18-1 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: Tue, 23 Sep 2014 10:02:03 -0000 On 23 September 2014 13:24, Konstantin Belousov wrote: > In the patch, default behaviour is to provide RLIMIT_STACK sized stack > for the main thread. The knobs are there to restore the old AS layout > if my fears of the binary compatibility become real one day, and to > keep the interface compat with the stable/10, which already got a knob > merged. > > That said, below the patch with libthr.7 man page merged to libthr.3, > and with the editing applied. > > diff --git a/lib/libthr/libthr.3 b/lib/libthr/libthr.3 > index bfbebec..aa4572c 100644 > --- a/lib/libthr/libthr.3 > +++ b/lib/libthr/libthr.3 > @@ -1,6 +1,11 @@ > .\" Copyright (c) 2005 Robert N. M. Watson > +.\" Copyright (c) 2014 The FreeBSD Foundation, Inc. > .\" All rights reserved. > .\" > +.\" Part of this documentation was written by > +.\" Konstantin Belousov under sponsorship > +.\" from the FreeBSD Foundation. > +.\" > .\" Redistribution and use in source and binary forms, with or without > .\" modification, are permitted provided that the following conditions > .\" are met: > @@ -24,7 +29,7 @@ > .\" > .\" $FreeBSD$ > .\" > -.Dd October 19, 2007 > +.Dd September 20, 2014 > .Dt LIBTHR 3 > .Os > .Sh NAME > @@ -45,8 +50,216 @@ has been optimized for use by applications expecting system scope thread > semantics, and can provide significant performance improvements > compared to > .Lb libkse . > +.Pp > +The library is tightly integrated with the run-time link editor > +.Xr ld-elf.so.1 1 > +and > +.Lb libc ; > +all three components must be built from the same source tree. > +Mixing > +.Li libc > +and > +.Nm > +libraries from different versions of > +.Fx > +is not supported. > +The run-time linker > +.Xr ld-elf.so.1 1 > +has some code to ensure backward-compatibility with older versions of > +.Nm . > +.Pp > +The man page documents the quirks and tunables of the > +.Nm . > +When linking with > +.Li -lpthread , > +the run-time dependency > +.Li libthr.so.3 > +is recorded in the produced object. > +.Sh MUTEX ACQUISITION > +A locked mutex (see > +.Xr pthread_mutex_lock 3 ) > +is represented by a volatile variable of type > +.Dv lwpid_t , > +which records the global system identifier of the thread > +owning the lock. > +.Nm > +performs a contested mutex acquisition in three stages, each of which > +is more resource-consuming than the previous. > +.Pp > +First, a spin loop > +is performed, where the library attempts to acquire the lock by > +.Xr atomic 9 > +operations. > +The loop count is controlled by the > +.Ev LIBPTHREAD_SPINLOOPS > +environment variable, with a default value of 2000. > +.Pp > +If the spin loop > +was unable to acquire the mutex, a yeild loop typo: yield [...] > .Sh SEE ALSO > -.Xr pthread 3 > +.Xr ktrace 1 , > +.Xr ld-elf.so.1 1 , > +.Xr getrlimit 2 , > +.Xr umtx 2 , > +.Xr dlclose 3 , > +.Xr dlopen 3 , > +.Xr errno 3 , > +.Xr getenv 3 , > +.Xr libc 3 , > +.Xr pthread_attr 3 , > +.Xr pthread_attr_setstacksize 3 , > +.Xr pthread_create 3 , > +.Xr signal 3 , > +.Xr atomic 9 . no pediod there per mdoc > .Sh AUTHORS > .An -nosplit > The > diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c > index 9bf0e29..72a067a 100644 > --- a/lib/libthr/thread/thr_init.c > +++ b/lib/libthr/thread/thr_init.c > @@ -445,7 +445,7 @@ init_private(void) > struct rlimit rlim; > size_t len; > int mib[2]; > - char *env; > + char *env, *env_bigstack, *env_splitstack; > > _thr_umutex_init(&_mutex_static_lock); > _thr_umutex_init(&_cond_static_lock); > @@ -473,8 +473,9 @@ init_private(void) > len = sizeof (_usrstack); > if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) > PANIC("Cannot get kern.usrstack from sysctl"); > - env = getenv("LIBPTHREAD_BIGSTACK_MAIN"); > - if (env != NULL) { > + env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); > + env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); > + if (bigstack != NULL || env_splitstack == NULL) { looks like a typo: s/bigstack/env_bigstack/ > if (getrlimit(RLIMIT_STACK, &rlim) == -1) > PANIC("Cannot get stack rlimit"); > _thr_stack_initial = rlim.rlim_cur; -- wbr, pluknet