From patchwork Thu Jun 27 14:20:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Miguel_Mart=C3=ADn?= X-Patchwork-Id: 92966 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6D4238323F2 for ; Thu, 27 Jun 2024 14:21:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id CFD563858C53 for ; Thu, 27 Jun 2024 14:20:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CFD563858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CFD563858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719498045; cv=none; b=GP/jlCzU58y0hua+8jTgft6avZodioHJMPKdlhdi40CvPlkqrCN35xprvv0n1eyenPIvwsoqajP5/RTmLLyFr2ZVaz0t3EUPDrTiecX4XewI6kwtpnFcpjo9FSIAG+Kj7v9/oOLbPGtGOCQeN2Sd9kTtPOictqI3fLhHs3epo1M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719498045; c=relaxed/simple; bh=Ykm/jIOi8N+yaxZIpxMDHY+PWGBOR79htTrqJgJ7fko=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=elwExK3VIUqbg8lR2nLZPTUrig0Hp8AfYfG7NiDpxsV/j9jb1dAZIsIm0lTfLlx8BxAQXvJ08cgLn1AP49UiCE15jT1/KOE3yyDmrqA6X08tlkgrYM6c0+vEGjgq8qwid5nqU17lLxe/oogpkTY2Tu6rFDArRHe89b6y4zjkXjU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719498037; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8C0g0w0urwqLiSip0lXyDWiKGIdExIowLuBWQDGtgJU=; b=FrAO4/sQJ3lVMV/AUSCOAlqaelDw2LQYqPHKTFpmkca2GYmT4fPbXXiJCQ9TFZEWm8QRvE b/ydYUSRnkUo4d/0hugVR6WjiVwLCCYNrSD9ZPZlfS5qAgF2Ta1QEAQEkyY11gP6z5b8pm MXyBZrmb7I4BF3mB7yftsvaGfdhuEWQ= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-153-Kb7arygXN2G6gGZsAnTJqw-1; Thu, 27 Jun 2024 10:20:36 -0400 X-MC-Unique: Kb7arygXN2G6gGZsAnTJqw-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6b07ef34bfcso40879116d6.1 for ; Thu, 27 Jun 2024 07:20:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719498035; x=1720102835; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8C0g0w0urwqLiSip0lXyDWiKGIdExIowLuBWQDGtgJU=; b=cKSRgTG/3kyE8nHwxV61yl5dP1ml7n7cKPLdJ1NWVYsBYuhhE9oRnLKMyaE7ci1xPD P8l4b6G4PSEVsdJqqFrYNKEhN7Sr+nSqY+biF8F9OnQEPRWIAm9Y3dyFwd/autk0TPgU Yq6casFGTIKfnQdtJFNg9bVPpmyyaDQNuVgw379cHMOiko8wI4OsoAXWmq2cvQvnD7ep 1gsDM3iCFq2oy+Til8NK42l3Ln29sOPNZikofOztEIdhFQqCd/dVgwET0FC5o14a1oXc tBXoXsgOnU3Zqn0QoBB2JOZDJ4FGh0AiKuDxvv+c0AV8PlJNJl3iAo4WpL6koTM+SJuz XikQ== X-Gm-Message-State: AOJu0Yy9SjF7uV1Y06/v+MELC57b5//nIzq6b/w+6D+qAHk5+zn+mbBc GfpXt7VMWcO2b03135jVXxw5ih5MiVqFaVcYmEz0Qc4yc5WZgZLyuMiuLKr+wdIK5f3Q7TNHGd3 3XICJC+olPdUAfZoQaRn4L6uF8Q9H5Zy7Tvq59GqjtB+/tq52IW5U7sEJS/UHtlyVsGRA4Xtlqq k0zpBBlBbK+eAF+do8cC9qsMbDtQWp5WaRkPhdS3AlQA== X-Received: by 2002:a05:6214:240b:b0:6b5:1d2f:1d3 with SMTP id 6a1803df08f44-6b592f9882dmr34218296d6.0.1719498035123; Thu, 27 Jun 2024 07:20:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2ErRhajQgtDzeNZOXWTZgJdHKTl4rD+Y+9TZy4nr0PYSmZj0fXB3twlKQxo0K/IyenzSmIg== X-Received: by 2002:a05:6214:240b:b0:6b5:1d2f:1d3 with SMTP id 6a1803df08f44-6b592f9882dmr34217886d6.0.1719498034709; Thu, 27 Jun 2024 07:20:34 -0700 (PDT) Received: from lenovo-p1.mamux.org.com ([213.229.137.162]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b59241b5a5sm5442306d6.28.2024.06.27.07.20.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 07:20:33 -0700 (PDT) From: =?utf-8?q?Miguel_Mart=C3=ADn?= To: libc-alpha@sourceware.org Subject: [PATCH 1/2] malloc: avoid global locks in tst-aligned_alloc-lib.c Date: Thu, 27 Jun 2024 16:20:22 +0200 Message-ID: <20240627142028.134709-2-mmartinv@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240627142028.134709-1-mmartinv@redhat.com> References: <20240627142028.134709-1-mmartinv@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Make sure the DSO used by aligned_alloc/calloc/malloc tests does not get a global lock on multithreaded tests. --- malloc/tst-aligned_alloc-lib.c | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/malloc/tst-aligned_alloc-lib.c b/malloc/tst-aligned_alloc-lib.c index 0205df5acf..9ef1f839c1 100644 --- a/malloc/tst-aligned_alloc-lib.c +++ b/malloc/tst-aligned_alloc-lib.c @@ -17,37 +17,38 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ -#include #include #include +#include extern void *__libc_malloc (size_t size); extern void *__libc_calloc (size_t n, size_t size); +__thread unsigned int seed = 0; + int aligned_alloc_count = 0; int libc_malloc_count = 0; int libc_calloc_count = 0; -/* Get a random alignment value. Biased towards the smaller values. Must be - a power of 2. */ -static size_t get_random_alignment (void) -{ - size_t aligns[] = { - 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 - }; - - return aligns[random () % array_length (aligns)]; -} - -static void *get_random_alloc (size_t size) +static void * +get_random_alloc (size_t size) { void *retval; size_t align; + struct timespec tp; + + if (seed == 0) + { + clock_gettime (CLOCK_REALTIME, &tp); + seed = tp.tv_nsec; + } - switch (random() % 3) - { + switch (rand_r (&seed) % 3) + { case 1: - align = get_random_alignment (); + /* Get a random alignment value. Biased towards the smaller + * values up to 16384. Must be a power of 2. */ + align = 1 << rand_r (&seed) % 15; retval = aligned_alloc (align, size); aligned_alloc_count++; break; @@ -59,13 +60,13 @@ static void *get_random_alloc (size_t size) retval = __libc_malloc (size); libc_malloc_count++; break; - } + } return retval; } - -void * __random_malloc (size_t size) +void * +__random_malloc (size_t size) { return get_random_alloc (size); } From patchwork Thu Jun 27 14:20:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Miguel_Mart=C3=ADn?= X-Patchwork-Id: 92967 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B08D385E44D for ; Thu, 27 Jun 2024 14:21:21 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3BFB63832E4D for ; Thu, 27 Jun 2024 14:20:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3BFB63832E4D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3BFB63832E4D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719498046; cv=none; b=eC9e2yDNF3F/gvQIc85NAgSqcmf8JlhIEEEi3wqAfKNa0qqTi2YF8w/KCLzyUgi7LXgazzhFl5W402aA6DQ64eCFxGTAcpcTQ0F9GJdM3r3QulAMQp+Tk6v2qGB38yQ01WcagaMTr87FP8iQOOhDPGX0T+HiZUnDWTcB+/dsFB4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719498046; c=relaxed/simple; bh=EtGEr+JdolfPs3Q3FB7KDthxMWhrBnsOQs+AUUCbU/8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ur8PBRLR95TTP94vvl+mJFdKCQpgKIfPM5g0yvDTUaZi/5HRSj6quZq9Cpp2RE1hPx6Ru9Q5uOW1ZrWyYPwoMl5Nk1ZXuwkzg/0e0m2grEXjwxVTcAu6fajXyoYcyfF1g1D+PGXKZkQL/JWOyuGVxGXQ5PAsEeIvwgq6AOUjbHI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719498039; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vnlQTv7PXqFxvg5xKtZs+AM5AqGKcCBV5zkjGBuBLS4=; b=HutBioX54n3msWUWiOcdLguV9+HnCAvelZTLoiRzyDuwSBeYZkAWjdzuQv4nNyijOTAke9 0Bkwipv72pKxSuAkqryI4mHXZ6n7zgoz+FvACYeUO7ZpebY+IX6R07slpm5+z/pT0LlU8D uqJLsgwJt3AOZQjfrzIAWvxCPQEuXIg= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-194-6cE3ooxTN7miWqjh7C_PkA-1; Thu, 27 Jun 2024 10:20:38 -0400 X-MC-Unique: 6cE3ooxTN7miWqjh7C_PkA-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4460f0951e8so35084091cf.3 for ; Thu, 27 Jun 2024 07:20:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719498037; x=1720102837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vnlQTv7PXqFxvg5xKtZs+AM5AqGKcCBV5zkjGBuBLS4=; b=mteAZIPE0l464oHRDvLr/yby5/zFyC1zzVS4HozorHRiUfDjwHx3TeloW+QbPM1H63 YBNZdWMXvoLp9Ideblz9fPwMGkUsXhLdqnQ1i1P/vzKC5dj1zJ60p8oCXftOpWg2UV9E 5s3A0wpvnbQiAzwUkn0btTGLwaT9FywPaH1zzGzC/o/Nm0Iu+3tpeP9z6Q98tJfj7oFK NHZG4Gl2jFSDSjmmWgfzyOz58BqziZUG1t0vfZI0bqev7g0QatWKaUVJuXcgaYBbFhNA utrwB8u8ZEWAtFhMkJiknyvXwKq58FHgj+JrHYcoc6eTCIXSDAWUAxE3BA6eLWuITEHX btUA== X-Gm-Message-State: AOJu0YzZbtdHjgtGVRhln1cD3oNXSiB1FSsIgoUmyEYykih7fJd+zVoC D+WQwNhWdLOrN7Iq9IPq03IsoDXR3n81pJwYQcQD4TTNSoBFd7gvs1aVgaYHRbR59R+6vy++Buh izpI8kEbTJ7hr8botCWxrJwTcjo8vy8Wtgg0zC8huEwQddvFudYGMWzZZtSlQuK3EUPN9+ODllm TXw0gAxQkxKZyfinA8azwTQBG/rcW+K/t9o97mmM9qjw== X-Received: by 2002:ad4:4448:0:b0:6b5:f75:4a0c with SMTP id 6a1803df08f44-6b540cf70ccmr133573016d6.64.1719498037319; Thu, 27 Jun 2024 07:20:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IENljIUB2mXvBF5QYdT6/kcvdn0tjFMD6bo+b8JP2QRWJ+5T6dWuT6f7r4ez8ty0KGTIlA/Kw== X-Received: by 2002:ad4:4448:0:b0:6b5:f75:4a0c with SMTP id 6a1803df08f44-6b540cf70ccmr133572646d6.64.1719498036764; Thu, 27 Jun 2024 07:20:36 -0700 (PDT) Received: from lenovo-p1.mamux.org.com ([213.229.137.162]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b59241b5a5sm5442306d6.28.2024.06.27.07.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 07:20:35 -0700 (PDT) From: =?utf-8?q?Miguel_Mart=C3=ADn?= To: libc-alpha@sourceware.org Subject: [PATCH 2/2] malloc: add multi-threaded tests for aligned_alloc/calloc/malloc Date: Thu, 27 Jun 2024 16:20:23 +0200 Message-ID: <20240627142028.134709-3-mmartinv@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240627142028.134709-1-mmartinv@redhat.com> References: <20240627142028.134709-1-mmartinv@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Improve aligned_alloc/calloc/malloc test coverage by adding multi-threaded tests with random memory allocations and with/without cross-thread memory deallocations. Perform a number of memory allocation calls with a randomly sized, but limited to 0xffff, requests. Use the existing DSO ('malloc/tst-aligned_alloc-lib.c') to randomize allocator selection. The multi-threaded allocation/deallocation is staged as described below: - Stage 1: Half of the threads will be allocating memory and the other half will be waiting for them to finish the allocation. - Stage 2: Half of the threads will be allocating memory and the other half will be deallocating memory. - Stage 3: Half of the threads will be deallocating memory and the second half waiting on the to finish. Add 'malloc/tst-aligned-alloc-random-thread.c' where each thread will deallocate only the memory that was previously allocated by itself. Add 'malloc/tst-aligned-alloc-random-thread-cross.c' where each thread will deallocate memory that was previously allocated by another thread. The intention is to be able to utilize existing malloc testing to ensure that similar allocation APIs are also exposed to the same rigors. --- malloc/Makefile | 8 + .../tst-aligned-alloc-random-thread-cross.c | 19 ++ malloc/tst-aligned-alloc-random-thread.c | 170 ++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 malloc/tst-aligned-alloc-random-thread-cross.c create mode 100644 malloc/tst-aligned-alloc-random-thread.c diff --git a/malloc/Makefile b/malloc/Makefile index 02aff1bd1d..98d507a6eb 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -28,6 +28,8 @@ tests := \ mallocbug \ tst-aligned-alloc \ tst-aligned-alloc-random \ + tst-aligned-alloc-random-thread \ + tst-aligned-alloc-random-thread-cross \ tst-alloc_buffer \ tst-calloc \ tst-free-errno \ @@ -151,6 +153,8 @@ ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes) # the tests expect specific internal behavior that is changed due to linking to # libmcheck.a. tests-exclude-mcheck = \ + tst-aligned-alloc-random-thread \ + tst-aligned-alloc-random-thread-cross \ tst-compathooks-off \ tst-compathooks-on \ tst-malloc-backtrace \ @@ -415,7 +419,11 @@ $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so $(objpfx)tst-aligned-alloc-random.out: $(objpfx)tst-aligned_alloc-lib.so +$(objpfx)tst-aligned-alloc-random-thread.out: $(objpfx)tst-aligned_alloc-lib.so +$(objpfx)tst-aligned-alloc-random-thread-cross.out: $(objpfx)tst-aligned_alloc-lib.so $(objpfx)tst-malloc-random.out: $(objpfx)tst-aligned_alloc-lib.so tst-aligned-alloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so +tst-aligned-alloc-random-thread-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so +tst-aligned-alloc-random-thread-cross-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so tst-malloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so diff --git a/malloc/tst-aligned-alloc-random-thread-cross.c b/malloc/tst-aligned-alloc-random-thread-cross.c new file mode 100644 index 0000000000..360ecc56ee --- /dev/null +++ b/malloc/tst-aligned-alloc-random-thread-cross.c @@ -0,0 +1,19 @@ +/* multi-threaded memory allocation and cross-thread deallocation test. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see . */ +#define CROSS_THREAD_DEALLOC +#include "tst-aligned-alloc-random-thread.c" diff --git a/malloc/tst-aligned-alloc-random-thread.c b/malloc/tst-aligned-alloc-random-thread.c new file mode 100644 index 0000000000..dfc29c0da4 --- /dev/null +++ b/malloc/tst-aligned-alloc-random-thread.c @@ -0,0 +1,170 @@ +/* multi-threaded memory allocation/deallocation test. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef ITERATIONS +# define ITERATIONS 16 +#endif + +#ifndef NUM_THREADS +# define NUM_THREADS 8 +#endif + +#ifndef NUM_ALLOCATIONS +# define NUM_ALLOCATIONS (16 * 1024) +#endif + +static pthread_barrier_t barrier; + +__thread unsigned int seed; + +typedef struct +{ + int id; + pthread_t thread; +} thread; + +thread threads[NUM_THREADS]; + +typedef struct +{ + void *ptr; + size_t size; +} allocation; + +allocation allocations[NUM_ALLOCATIONS]; + +void +run_thread_dealloc (int id) +{ + int actual_thread = id; +#ifdef CROSS_THREAD_DEALLOC + if (id < NUM_THREADS / 2) + actual_thread = (id + NUM_THREADS / 2); +#endif + for (int i = id; i < NUM_ALLOCATIONS; i += NUM_THREADS) + { + free (allocations[i].ptr); + verbose_printf ( + "info: thread %.2d dealloc %lu bytes at %p (allocations[%d])\n", + actual_thread, allocations[i].size, allocations[i].ptr, i); + allocations[i].ptr = NULL; + allocations[i].size = 0; + } +} + +void +run_thread_alloc (int id) +{ + int actual_thread = id; +#ifdef CROSS_THREAD_DEALLOC + if (id >= NUM_THREADS / 2) + actual_thread = (id - NUM_THREADS / 2); +#endif + for (int i = id; i < NUM_ALLOCATIONS; i += NUM_THREADS) + { + allocations[i].size = rand_r (&seed) & 0xffff; + allocations[i].ptr = malloc (allocations[i].size); + TEST_VERIFY_EXIT (allocations[i].ptr != NULL); + verbose_printf ( + "info: thread %.2d alloc %lu bytes at %p (allocations[%d])\n", + actual_thread, allocations[i].size, allocations[i].ptr, i); + } +} + +void * +run_allocations (void *arg) +{ + int id = *((int *) arg); + seed = time (NULL) + id; + + /* Stage 1: Half o the threads allocating memory */ + if (id < NUM_THREADS / 2) + run_thread_alloc (id); + + verbose_printf ("info: thread %.2d waiting for stage 1 barrier\n", id); + xpthread_barrier_wait (&barrier); + + /* Stage 2: Half of the threads allocationg memory and the other + * half deallocating + */ + if (id < NUM_THREADS / 2) +#ifndef CROSS_THREAD_DEALLOC + run_thread_dealloc (id); +#else + run_thread_alloc (id + NUM_THREADS / 2); +#endif + else +#ifndef CROSS_THREAD_DEALLOC + run_thread_alloc (id); +#else + run_thread_dealloc (id - NUM_THREADS / 2); +#endif + + verbose_printf ("info: thread %.2d waiting for stage 2 barrier\n", id); + xpthread_barrier_wait (&barrier); + + // Stage 3: Half of the threads deallocating and the other waiting for + // them to finish. + if (id >= NUM_THREADS / 2) + run_thread_dealloc (id); + + verbose_printf ("info: thread %.2d waiting for stage 3 barrier\n", id); + xpthread_barrier_wait (&barrier); + + return NULL; +} + +static int +do_test (void) +{ + memset (allocations, 0, sizeof (allocations)); + memset (threads, 0, sizeof (threads)); + xpthread_barrier_init (&barrier, NULL, NUM_THREADS); + + verbose_printf ("info: running %d iterations with %d threads\n", ITERATIONS, NUM_THREADS); + for (int j = 0; j < ITERATIONS; j++) + { + for (int i = 0; i < NUM_THREADS; i++) + { + threads[i].id = i; + threads[i].thread + = xpthread_create (NULL, run_allocations, &threads[i].id); + } + + for (int i = 0; i < NUM_THREADS; i++) + xpthread_join (threads[i].thread); + + // Check everything was freed + for (int i = 0; i < NUM_ALLOCATIONS; i++) + TEST_VERIFY_EXIT (allocations[i].ptr == NULL); + } + + return 0; +} + +#include