diff mbox series

[v3,1/2] malloc: avoid global locks in tst-aligned_alloc-lib.c

Message ID 34dfe9283ec91c98d6a6c721251d5f1546e7c06c.1721142537.git.mmartinv@redhat.com
State New
Headers show
Series malloc: add multi-threaded tests for aligned_alloc/calloc/malloc | expand

Commit Message

Miguel Martín July 16, 2024, 3:14 p.m. UTC
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(-)

Comments

Arjun Shankar July 17, 2024, 3:32 p.m. UTC | #1
> Make sure the DSO used by aligned_alloc/calloc/malloc tests does not get
> a global lock on multithreaded tests.

This is the same as patch #1 from the last version that I already reviewed.

Reviewed-by: Arjun Shankar <arjun@redhat.com>

> ---
>  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 <https://www.gnu.org/licenses/>.  */
>
> -#include <array_length.h>
>  #include <libc-symbols.h>
>  #include <stdlib.h>
> +#include <time.h>
>
>  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);
>  }
> --
> Miguel Martín
> mmartinv@redhat.com
>
diff mbox series

Patch

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 <https://www.gnu.org/licenses/>.  */
 
-#include <array_length.h>
 #include <libc-symbols.h>
 #include <stdlib.h>
+#include <time.h>
 
 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);
 }