From owner-freebsd-hackers@FreeBSD.ORG Sat May 16 17:05:29 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CEDDE106564A for ; Sat, 16 May 2009 17:05:29 +0000 (UTC) (envelope-from to.my.trociny@gmail.com) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.152]) by mx1.freebsd.org (Postfix) with ESMTP id 514808FC08 for ; Sat, 16 May 2009 17:05:28 +0000 (UTC) (envelope-from to.my.trociny@gmail.com) Received: by fg-out-1718.google.com with SMTP id e12so286777fga.12 for ; Sat, 16 May 2009 10:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:to:cc:subject:references :organization:from:date:in-reply-to:message-id:user-agent :mime-version:content-type:content-transfer-encoding; bh=2+ZSjQJ642MIJkB8TDQWnWcV3KEZQhk8jf+XO9/qv/E=; b=akXst155L618Mjn6LXbgZvRblCke0MMz+Y4eyweailwurXszSJFeEhvqwGFFnXKqpn dEJUZ6QbgJHXerxD6p5IbIiYtJ7N9NEGunlX73JT/TPXnmGWb9EDJOBVov2DlndDsJf8 iLZDNN5oWzfxeaDi0zXfMedZGfi4lTSpg5n9A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=to:cc:subject:references:organization:from:date:in-reply-to :message-id:user-agent:mime-version:content-type :content-transfer-encoding; b=AEsdPf+yWxfYXA0UcYA4f8vfPb3mE1V0CIkf888sM5/ojAxeW511lc5bLKLyGbN/j5 dFDkwNTUTskMdlBYhmGXXzl5RC6XI4NqZewXIzIUiHNSsaW/zZeiFEgEC1kUX1zhUIrE /kZYKrKvUpH2oeui5hjBU1FSguimdxXSvPkYc= Received: by 10.86.68.10 with SMTP id q10mr4973012fga.78.1242493528172; Sat, 16 May 2009 10:05:28 -0700 (PDT) Received: from localhost ([95.69.165.249]) by mx.google.com with ESMTPS id d6sm4610917fga.2.2009.05.16.10.05.25 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 16 May 2009 10:05:26 -0700 (PDT) To: Marius =?iso-8859-1?Q?N=FCnnerich?= References: <814ovqn8dp.fsf@zhuzha.ua1> Organization: TOA Ukraine From: Mikolaj Golub Date: Sat, 16 May 2009 20:05:24 +0300 In-Reply-To: ("Marius =?iso-8859-1?Q?N=FCnnerich=22's?= message of "Fri\, 15 May 2009 13\:48\:51 +0200") Message-ID: <86k54hvuzv.fsf@kopusha.onet> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org, Mikolaj Golub Subject: Re: Memory leak on thread removal X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 May 2009 17:05:30 -0000 On Fri, 15 May 2009 13:48:51 +0200 Marius N=FCnnerich wrote: MN> On Tue, May 12, 2009 at 08:27, Mikolaj Golub = wrote: >> Hi, >> >> The code below is compiled with -fopenmp and run on FreeBSD6/7 (i386, a= md64): >> >> #include >> #include >> >> int n =3D 4, m =3D 2; >> >> int main () { >> =A0 =A0 =A0 =A0for (;;) { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0int i; >> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//sleep(2); >> #pragma omp parallel for num_threads(m) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0for(i =3D 0; i < 1; i++) {} >> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//sleep(2); >> #pragma omp parallel for num_threads(n) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0for(i =3D 0; i < 1; i++) {} >> >> =A0 =A0 =A0 =A0} >> >> =A0 =A0 =A0 =A0return 0; >> } >> >> During the run the program's virtual memory usage constantly grows. The= growth >> is observed only when n !=3D m. When running the program with uncomment= ed >> sleep() and observing the number of threads with 'top -H' I see in turn= 2 or 4 >> threads. So it looks like memory leak when thread is removed. Should I = fill >> PR? It looks like I have found the leak. The problem is in libgomp/team.c. gomp_thread_start() does sem_init() but sem_destroy() is never called. This patch solves the problem for me: --- contrib/gcclibs/libgomp/team.c.orig 2009-05-16 17:32:57.000000000 +0300 +++ contrib/gcclibs/libgomp/team.c 2009-05-16 19:16:37.000000000 +0300 @@ -164,9 +164,12 @@ new_team (unsigned nthreads, struct gomp static void free_team (struct gomp_team *team) { + int i; free (team->work_shares); gomp_mutex_destroy (&team->work_share_lock); gomp_barrier_destroy (&team->barrier); + for(i =3D 1; i < team->nthreads; i++) + gomp_sem_destroy (team->ordered_release[i]); gomp_sem_destroy (&team->master_release); free (team); } I am going to fill PR to gcc mainstream, but should I also register this in FreeBSD bugtrack as gcc is part of the base? BTW, the problem is not observed under Linux. I have not looked in Linux co= de but it looks like sem_init() implementation for Linux does not do memory allocation. The memory for the test program below grows under FreeBSD and d= oes not under Linux. #include int main(int argc, char *argv[]) { sem_t sem; for(;;) { sem_init(&sem, 0, 0);} return 0; } MN> I can confirm this. I briefly looked through the libgomp code but MN> didn't see the leak. Anybody knows good tools how to investigate this? http://freshmeat.net/projects/lmdbg This is a small memory leak debugger. It does not provide all functionality you can find in more sophisticated tools but is lightweight, portable and simple in use. It was very useful when I traced this bug. --=20 Mikolaj Golub