From nobody@FreeBSD.org  Tue Mar 25 00:44:10 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 51AEF106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 Mar 2008 00:44:10 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 2E3BE8FC1F
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 Mar 2008 00:44:10 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m2P0hxFR001700
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 Mar 2008 00:43:59 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id m2P0hwBk001699;
	Tue, 25 Mar 2008 00:43:58 GMT
	(envelope-from nobody)
Message-Id: <200803250043.m2P0hwBk001699@www.freebsd.org>
Date: Tue, 25 Mar 2008 00:43:58 GMT
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: freebsd-gnats-submit@FreeBSD.org
Subject: dlsym() is very slow when a symbol cannot be found
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         122061
>Category:       kern
>Synopsis:       [libc] dlsym(3) is very slow when a symbol cannot be found [regression]
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 25 00:50:04 UTC 2008
>Closed-Date:    Sat Aug 28 10:54:25 UTC 2010
>Last-Modified:  Sat Aug 28 10:54:25 UTC 2010
>Originator:     Jacques Garrigue
>Release:        7.0-RELEASE
>Organization:
Nagoya University
>Environment:
FreeBSD tet4.kurims.kyoto-u.ac.jp 7.0-RELEASE FreeBSD 7.0-RELEASE #0: Fri Feb 29 17:32:31 JST 2008     garrigue@tet4.kurims.kyoto-u.ac.jp:/usr/src/sys/i386/compile/TET  i386

>Description:
Calling dlsym() on a shared object that doesn't contain a symbol is very slow
compared to 6.2-RELEASE.

I discovered the problem when compiling the port x11-toolkits/ocaml-lablgtk2.
Compilation works normally at first, then stalls on the command:

ocamlmktop -I +lablGL -thread  -o lablgtktop unix.cma threads.cma lablgl.cma \
           -I . lablgtk.cma lablgtkgl.cma lablglade.cma lablgnomecanvas.cma lablgnomeui.cma lablpanel.cma lablrsvg.cma lablgtkspell.cma gtkThread.cmo

It succeeds eventually, but takes more than 1 minute rather than the few seconds
it took before. To understand what is happening, you have to know the internals
of the ocaml compiler: each of the libraries (.cma) here are requiring a shared
object each, and try to lookup symbols into them (about 2000 total). But rather
than looking the symbol where it is known to be, all the shared objects, and
then dlsym is used to manually look for it in each shared object individually.

So if you're out of luck you end up doing about 20000 failing lookups for 2000

successes.
Note that with a simple patch on ocaml, which reorders the list of shared
objects so that the last successful object is looked at first, the above
command line takes only 1 second.
>How-To-Repeat:
/* Compile and execute the following program */

#include <dlfcn.h>

int main()
{
     int i;
     void *handle = dlopen("/usr/local/lib/libgtk-x11-2.0.so", RTLD_NOW);

     for (i=0; i<20000; i++)
          dlsym(handle, "zorglub"); // takes 5 seconds
     // dlsym(handle, "gtk_main"); // takes 0.1 seconds

     return 0;
}


>Fix:


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: vwe 
State-Changed-When: Wed Apr 9 19:47:27 UTC 2008 
State-Changed-Why:  

submitter has been asked for feedback. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=122061 

From: Volker <volker@vwsoft.com>
To: bug-followup@FreeBSD.org, garrigue@math.nagoya-u.ac.jp
Cc:  
Subject: Re: kern/122061: [libc] dlsym() is very slow when a symbol cannot
 be found (regression)
Date: Wed, 09 Apr 2008 21:42:03 +0200

 Jacques,
 
 I've compiled your code and this is what it gives me:
 
 %time ./dlsym-test
 2.102u 0.007s 0:02.12 99.0%	5+2531k 0+0io 0pf+0w
 
 Tested on 7-STABLE:
 FreeBSD cesar.sz.vwsoft.com 7.0-STABLE FreeBSD 7.0-STABLE #19: Sun Apr
 6 14:54:57 CEST 2008
 root@cesar.sz.vwsoft.com:/usr/obj/usr/src/sys/CESAR  i386
 
 Execution time was ~2 seconds.
 
 Are you, by any chance, in a low memory condition?
 
 Volker
State-Changed-From-To: feedback->closed 
State-Changed-By: vwe 
State-Changed-When: Sat Aug 28 10:54:17 UTC 2010 
State-Changed-Why:  
feedback timed out 

http://www.freebsd.org/cgi/query-pr.cgi?pr=122061 
>Unformatted:
