From patchwork Tue Jul 16 15:14:56 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: 1961129 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=E6DQwim2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNjLd42BFz1xqc for ; Wed, 17 Jul 2024 01:15:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B08CF38708C8 for ; Tue, 16 Jul 2024 15:15:35 +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 E1BE03846099 for ; Tue, 16 Jul 2024 15:15:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1BE03846099 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 E1BE03846099 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=1721142912; cv=none; b=NYIuDzs6KbI0z+z36Gd6LoPWFAQkN31DXHZYRjfWjpybsM/PXWkfFUVLbxyDc21J1A6oqWgl1BpTzIU2oSW1O+90pnhdE6vmIyI1kVWGrcdsHjgElcd03gEr0Ytrltwag2eGgoBqeQnnqPdUQx2AAvf7tPWgp7pTRnZ+5GEdtMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721142912; c=relaxed/simple; bh=Ykm/jIOi8N+yaxZIpxMDHY+PWGBOR79htTrqJgJ7fko=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ABuUFqIzz/r1K+gFn4mW2j3IGWqIes1Jb3ayLHUixUciq66SakHavERrfLV0uS5FxnNVKgN2UPSaRiPUpuLIjZPZq66SVfQ1S+de9ShUSUelvI2DVFq5t1pk1HvhRf2TWvnhv3taehDXG1VPSSYjay/D7Bpn3ewgipq0wYNENZQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721142910; 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=E6DQwim20OJN1rA/AY/SdypAOYU4u1Xha2jis3w03/z894hRCoUp/3jWRJwFYf2F5TXG/D 88/y7n+Tl45hZADXCKGyjgH7NB7PGQszqdgubtz8wQGcIH2A+8y8jO5jNNA524cr62SQJQ KNFNQq2TS+x1MouQTtWoQo8T/4S2KyE= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-205-FlK8J-7QNr-dXFt27Vm6OA-1; Tue, 16 Jul 2024 11:15:09 -0400 X-MC-Unique: FlK8J-7QNr-dXFt27Vm6OA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3679e7eeda6so3711724f8f.3 for ; Tue, 16 Jul 2024 08:15:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721142908; x=1721747708; 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=M2LV7+ExukOz1YOufRWvSVEx4iEBYAP543oJTPjFbu/aEOSdephWvkyHaRk9Bg3LMY NyzzHMY9JB45RwmKJ3GoM+PiTh5/6/pvdx/C6oCO6WMOZmfohlELSQMJyx/zJMDt1A4k d4uzzqXVjjBuTzkcGXlx9ECCLfInXC2+x06H6+beacNGTJO0AHHQaUN6nEPQtMe2eur/ EnpoT3lZMCv+6oLVHHR/RFQL1prIr1RipQ10srF9jjloXL+Ydo2vghpsVH5MilY6dAAk Bd6HMrzh9X6vjSRZzFCiq8HBCC2vwscMQs77/w4U0nfl+KFQh+udvjo2YjJiLsJIgbtq uwsw== X-Gm-Message-State: AOJu0YxVkgSwiYK+ztLjS/9Ufo1k0cTTJsa7X81R/QAyJ+bp4wNX/u2H nQF4AtksQMTUDLbQuP9ie++LSNejUu3+u2D+jIzpYcJXOyqm9GXWn+spkNOGeccaYZot7RBYVk/ odGkaOOnNe5/aCuB7qnB7wxsr6FJmCk/cp8iewjjZVQ7UHumhrWzNZKJZuhKtQnzmBG5B8+w2tW /hswGBF0PzNk5a8ppgI9+xRbSKPPpxrMiAYUqXP+fLMLZ1 X-Received: by 2002:a5d:47c4:0:b0:366:f8e7:d898 with SMTP id ffacd0b85a97d-36826313455mr1953038f8f.50.1721142907784; Tue, 16 Jul 2024 08:15:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHjx8vaxKEWPzztkrJwemVi1R+yQvPYbEfgSp/+f6zz1G+SGs7lzbTJUt+RIC48NTLxo1/wYQ== X-Received: by 2002:a5d:47c4:0:b0:366:f8e7:d898 with SMTP id ffacd0b85a97d-36826313455mr1953026f8f.50.1721142907453; Tue, 16 Jul 2024 08:15:07 -0700 (PDT) Received: from lenovo-p1.mamux.org ([82.213.230.188]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3680dab3f13sm9401185f8f.4.2024.07.16.08.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jul 2024 08:15:07 -0700 (PDT) From: =?utf-8?q?Miguel_Mart=C3=ADn?= To: libc-alpha@sourceware.org Subject: [PATCH v3 1/2] malloc: avoid global locks in tst-aligned_alloc-lib.c Date: Tue, 16 Jul 2024 17:14:56 +0200 Message-ID: <34dfe9283ec91c98d6a6c721251d5f1546e7c06c.1721142537.git.mmartinv@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.3 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_H3, 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~incoming=patchwork.ozlabs.org@sourceware.org Make sure the DSO used by aligned_alloc/calloc/malloc tests does not get a global lock on multithreaded tests. Reviewed-by: Arjun Shankar --- 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 Tue Jul 16 15:14:57 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: 1961130 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gOBwPemD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNjMS3bLsz1xqc for ; Wed, 17 Jul 2024 01:16:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5BEBA384A077 for ; Tue, 16 Jul 2024 15:16: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 DDE9C384A05B for ; Tue, 16 Jul 2024 15:15:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDE9C384A05B 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 DDE9C384A05B 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=1721142915; cv=none; b=N/+8OyTMW67pLEHzq+iih3O6Vkgujiu9W9TIdGDzU5M9leWo9vi1HNaU+vJnjU5Z+3oNLRh6VG9G8WEOVI+ORjYuIlqJg0MWS7DH03LUNOGF/1dBf9obx5ojJ6blyTs1jZPeIyIjczVtEn+wDdMtgKk06try3idfEjx7+YdPVos= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721142915; c=relaxed/simple; bh=/c4tzzWORf2eROfVZySooDvUPXQcHXLhRumyB8LxA5U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lQ9Hsx6pvFGj4+yOIEcVr5EFvptXgXQ48+/6k99PIe4umYJow+2+KkXF4KVSuMDPIv+0B4i/mYZ+3PMsnFoCdsYuEr8C1oAD2BaPOR6QSOVhL7Kul7gcIK2siKd31LWZvuGShokqS5UnZW+6VWfsxD5hfwmE9QJV8bVQ/WoM8qA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721142912; 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=PVv2CmmvVEE234JrAPme+H5f15HSQMizqyuyw7Xl4lU=; b=gOBwPemDimhtPlvbFS5dd3vKb4gazT6k+WvOQ3l6Gl7i3nZ3KyVtbro9SgucHeOBZWstQb IuPi3eCiAllHHCQVBvwHn5NDyiUWTL5cPBy6PVFA63NTh8amPRWajZUjybm3lx7mEC5Jjh MplyoPRN3j9UPXyyBD5PbXzfnd84La0= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-aa43-ruMPOeikXzfGSDBzw-1; Tue, 16 Jul 2024 11:15:11 -0400 X-MC-Unique: aa43-ruMPOeikXzfGSDBzw-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-367a058fa21so3255538f8f.1 for ; Tue, 16 Jul 2024 08:15:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721142909; x=1721747709; 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=PVv2CmmvVEE234JrAPme+H5f15HSQMizqyuyw7Xl4lU=; b=KCOTRIOsC1BRD+wilnRBCekma50sDnPQcm6x9SFX7ofJWUzzzEIa7EUWBwOt1RXiBh b7n3I6zo6dZI6QIscMiGKwb+sH4eLPuTD3f+6SnP/Jat4odoq91qbifUALwgjGki2734 BuxrzLPDBMhiJfPjnUrkv/GSDK18g3DyOu8c4p231lybynonwsVpqebmUp/x5AFzD7wZ ah4lMlC8bppsCaxdMi1iwiXUXN8EQQNnyiCMiUKzIzrLYFR21qhTWRl7+vOTTYnE2Fw5 I6xTa+zw+lJfMav2yhAHcfpKfAshSqQgjqwgSw6fO6Hs4dsg6xQ4Wl+E0SoZJH/rg2oV cSbA== X-Gm-Message-State: AOJu0YwVv7apPkjM0xlofe9yQR/KD1DxgW/f3wvGA/nObEHLhDVi7Jzx dahRAJ/XBsetsdS0tEHyg4WHfDY7d8n0FGoxwcEHyCHYCmjzUJR9A6z+fhy60XR6U6Nvc1qslv0 fru7EFRYk+DoQvpBayDpRRWZS6Zv190BQN7D1gMhb3U74r8RA4S6jXZWkYqgplmWkorJx4Jj8H2 AtfEp4k8WVBigpPg9EB8uTjLm8eR6HfNSj5WujNVoyhDe1 X-Received: by 2002:adf:f14a:0:b0:362:d7ac:ae70 with SMTP id ffacd0b85a97d-3682737c357mr1913196f8f.5.1721142909430; Tue, 16 Jul 2024 08:15:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuXUE/yGi0Sl4j2/tYl8lVntUZelZr6Mo56GgpVf4/Hf2g03TE6prTUxSqZPjc/iaZsCoHew== X-Received: by 2002:adf:f14a:0:b0:362:d7ac:ae70 with SMTP id ffacd0b85a97d-3682737c357mr1913167f8f.5.1721142908873; Tue, 16 Jul 2024 08:15:08 -0700 (PDT) Received: from lenovo-p1.mamux.org ([82.213.230.188]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3680dab3f13sm9401185f8f.4.2024.07.16.08.15.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jul 2024 08:15:07 -0700 (PDT) From: =?utf-8?q?Miguel_Mart=C3=ADn?= To: libc-alpha@sourceware.org Subject: [PATCH v3 2/2] malloc: add multi-threaded tests for aligned_alloc/calloc/malloc Date: Tue, 16 Jul 2024 17:14:57 +0200 Message-ID: <38d564025c76128664911cfaf0f5a5cdd02442f1.1721142537.git.mmartinv@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.2 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_H3, 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~incoming=patchwork.ozlabs.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 random sizes limited to 0xffff. 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 them 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. Reviewed-by: Arjun Shankar --- malloc/Makefile | 8 + .../tst-aligned-alloc-random-thread-cross.c | 19 +++ malloc/tst-aligned-alloc-random-thread.c | 145 ++++++++++++++++++ 3 files changed, 172 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..e95f79250a --- /dev/null +++ b/malloc/tst-aligned-alloc-random-thread.c @@ -0,0 +1,145 @@ +/* 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 2048 +#endif + +static pthread_barrier_t barrier; + +__thread unsigned int seed; + +typedef struct +{ + int id; + pthread_t thread; +} thread; + +thread threads[NUM_THREADS]; + +void *allocations[NUM_THREADS][NUM_ALLOCATIONS]; + +void +run_thread_dealloc (int id) +{ + for (int i = 0; i < NUM_ALLOCATIONS; i++) + { + free (allocations[id][i]); + allocations[id][i] = NULL; + } +} + +void +run_thread_alloc (int id) +{ + size_t msb, size; + for (int i = 0; i < NUM_ALLOCATIONS; i++) + { + msb = 1 << rand_r (&seed) % 16; + size = msb + rand_r (&seed) % msb; + allocations[id][i] = malloc (size); + TEST_VERIFY_EXIT (allocations[id][i] != NULL); + } +} + +void * +run_allocations (void *arg) +{ + int id = *((int *) arg); + seed = time (NULL) + id; + + /* Stage 1: First half o the threads allocating memory and the second + * half waiting for them to finish + */ + if (id < NUM_THREADS / 2) + run_thread_alloc (id); + + xpthread_barrier_wait (&barrier); + + /* Stage 2: Half of the threads allocationg memory and the other + * half deallocating: + * - In the non cross-thread dealloc scenario the first half will be + * deallocating the memory allocated by themselves in stage 1 and the + * second half will be allocating memory. + * - In the cross-thread dealloc scenario the first half will continue + * to allocate memory and the second half will deallocate the memory + * allocated by the first half in stage 1. + */ + 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 + + xpthread_barrier_wait (&barrier); + + // Stage 3: Second half of the threads deallocating and the first half + // waiting for them to finish. + if (id >= NUM_THREADS / 2) + run_thread_dealloc (id); + + return NULL; +} + +static int +do_test (void) +{ + xpthread_barrier_init (&barrier, NULL, NUM_THREADS); + + for (int i = 0; i < ITERATIONS; i++) + { + for (int t = 0; t < NUM_THREADS; t++) + { + threads[t].id = t; + threads[t].thread + = xpthread_create (NULL, run_allocations, &threads[t].id); + } + + for (int t = 0; t < NUM_THREADS; t++) + xpthread_join (threads[t].thread); + } + + return 0; +} + +#include