Blame malloc/tst-malloc_info.c

Packit 6c4009
/* Smoke test for malloc_info.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* The purpose of this test is to provide a quick way to run
Packit 6c4009
   malloc_info in a multi-threaded process.  */
Packit 6c4009
Packit 6c4009
#include <array_length.h>
Packit 6c4009
#include <malloc.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
Packit 6c4009
/* This barrier is used to have the main thread wait until the helper
Packit 6c4009
   threads have performed their allocations.  */
Packit 6c4009
static pthread_barrier_t barrier;
Packit 6c4009
Packit 6c4009
enum
Packit 6c4009
  {
Packit 6c4009
    /* Number of threads performing allocations.  */
Packit 6c4009
    thread_count  = 4,
Packit 6c4009
Packit 6c4009
    /* Amount of memory allocation per thread.  This should be large
Packit 6c4009
       enough to cause the allocation of multiple heaps per arena.  */
Packit 6c4009
    per_thread_allocations
Packit 6c4009
      = sizeof (void *) == 4 ? 16 * 1024 * 1024 : 128 * 1024 * 1024,
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
allocation_thread_function (void *closure)
Packit 6c4009
{
Packit 6c4009
  struct list
Packit 6c4009
  {
Packit 6c4009
    struct list *next;
Packit 6c4009
    long dummy[4];
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
  struct list *head = NULL;
Packit 6c4009
  size_t allocated = 0;
Packit 6c4009
  while (allocated < per_thread_allocations)
Packit 6c4009
    {
Packit 6c4009
      struct list *new_head = xmalloc (sizeof (*new_head));
Packit 6c4009
      allocated += sizeof (*new_head);
Packit 6c4009
      new_head->next = head;
Packit 6c4009
      head = new_head;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  xpthread_barrier_wait (&barrier);
Packit 6c4009
Packit 6c4009
  /* Main thread prints first statistics here.  */
Packit 6c4009
Packit 6c4009
  xpthread_barrier_wait (&barrier);
Packit 6c4009
Packit 6c4009
  while (head != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct list *next_head = head->next;
Packit 6c4009
      free (head);
Packit 6c4009
      head = next_head;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  xpthread_barrier_init (&barrier, NULL, thread_count + 1);
Packit 6c4009
Packit 6c4009
  pthread_t threads[thread_count];
Packit 6c4009
  for (size_t i = 0; i < array_length (threads); ++i)
Packit 6c4009
    threads[i] = xpthread_create (NULL, allocation_thread_function, NULL);
Packit 6c4009
Packit 6c4009
  xpthread_barrier_wait (&barrier);
Packit 6c4009
  puts ("info: After allocation:");
Packit 6c4009
  malloc_info (0, stdout);
Packit 6c4009
Packit 6c4009
  xpthread_barrier_wait (&barrier);
Packit 6c4009
  for (size_t i = 0; i < array_length (threads); ++i)
Packit 6c4009
    xpthread_join (threads[i]);
Packit 6c4009
Packit 6c4009
  puts ("\ninfo: After deallocation:");
Packit 6c4009
  malloc_info (0, stdout);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>