1 /* Smoke test for malloc_info.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* The purpose of this test is to provide a quick way to run
20 malloc_info in a multi-threaded process. */
22 #include <array_length.h>
25 #include <support/support.h>
26 #include <support/xthread.h>
28 /* This barrier is used to have the main thread wait until the helper
29 threads have performed their allocations. */
30 static pthread_barrier_t barrier
;
34 /* Number of threads performing allocations. */
37 /* Amount of memory allocation per thread. This should be large
38 enough to cause the allocation of multiple heaps per arena. */
39 per_thread_allocations
40 = sizeof (void *) == 4 ? 16 * 1024 * 1024 : 128 * 1024 * 1024,
44 allocation_thread_function (void *closure
)
52 struct list
*head
= NULL
;
54 while (allocated
< per_thread_allocations
)
56 struct list
*new_head
= xmalloc (sizeof (*new_head
));
57 allocated
+= sizeof (*new_head
);
58 new_head
->next
= head
;
62 xpthread_barrier_wait (&barrier
);
64 /* Main thread prints first statistics here. */
66 xpthread_barrier_wait (&barrier
);
70 struct list
*next_head
= head
->next
;
81 xpthread_barrier_init (&barrier
, NULL
, thread_count
+ 1);
83 pthread_t threads
[thread_count
];
84 for (size_t i
= 0; i
< array_length (threads
); ++i
)
85 threads
[i
] = xpthread_create (NULL
, allocation_thread_function
, NULL
);
87 xpthread_barrier_wait (&barrier
);
88 puts ("info: After allocation:");
89 malloc_info (0, stdout
);
91 xpthread_barrier_wait (&barrier
);
92 for (size_t i
= 0; i
< array_length (threads
); ++i
)
93 xpthread_join (threads
[i
]);
95 puts ("\ninfo: After deallocation:");
96 malloc_info (0, stdout
);
101 #include <support/test-driver.c>