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: 94011 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 ACA13386C5A4 for ; Tue, 16 Jul 2024 15:15:46 +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~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. 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); }