tst-malloc_info.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Smoke test for malloc_info.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* The purpose of this test is to provide a quick way to run
  16. malloc_info in a multi-threaded process. */
  17. #include <array_length.h>
  18. #include <malloc.h>
  19. #include <stdlib.h>
  20. #include <support/support.h>
  21. #include <support/xthread.h>
  22. /* This barrier is used to have the main thread wait until the helper
  23. threads have performed their allocations. */
  24. static pthread_barrier_t barrier;
  25. enum
  26. {
  27. /* Number of threads performing allocations. */
  28. thread_count = 4,
  29. /* Amount of memory allocation per thread. This should be large
  30. enough to cause the allocation of multiple heaps per arena. */
  31. per_thread_allocations
  32. = sizeof (void *) == 4 ? 16 * 1024 * 1024 : 128 * 1024 * 1024,
  33. };
  34. static void *
  35. allocation_thread_function (void *closure)
  36. {
  37. struct list
  38. {
  39. struct list *next;
  40. long dummy[4];
  41. };
  42. struct list *head = NULL;
  43. size_t allocated = 0;
  44. while (allocated < per_thread_allocations)
  45. {
  46. struct list *new_head = xmalloc (sizeof (*new_head));
  47. allocated += sizeof (*new_head);
  48. new_head->next = head;
  49. head = new_head;
  50. }
  51. xpthread_barrier_wait (&barrier);
  52. /* Main thread prints first statistics here. */
  53. xpthread_barrier_wait (&barrier);
  54. while (head != NULL)
  55. {
  56. struct list *next_head = head->next;
  57. free (head);
  58. head = next_head;
  59. }
  60. return NULL;
  61. }
  62. static int
  63. do_test (void)
  64. {
  65. xpthread_barrier_init (&barrier, NULL, thread_count + 1);
  66. pthread_t threads[thread_count];
  67. for (size_t i = 0; i < array_length (threads); ++i)
  68. threads[i] = xpthread_create (NULL, allocation_thread_function, NULL);
  69. xpthread_barrier_wait (&barrier);
  70. puts ("info: After allocation:");
  71. malloc_info (0, stdout);
  72. xpthread_barrier_wait (&barrier);
  73. for (size_t i = 0; i < array_length (threads); ++i)
  74. xpthread_join (threads[i]);
  75. puts ("\ninfo: After deallocation:");
  76. malloc_info (0, stdout);
  77. return 0;
  78. }
  79. #include <support/test-driver.c>