| 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. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 18 | |
| 19 | /* The purpose of this test is to provide a quick way to run |
| 20 | malloc_info in a multi-threaded process. */ |
| 21 | |
| 22 | #include <array_length.h> |
| 23 | #include <malloc.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <support/support.h> |
| 26 | #include <support/xthread.h> |
| 27 | |
| 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; |
| 31 | |
| 32 | enum |
| 33 | { |
| 34 | /* Number of threads performing allocations. */ |
| 35 | thread_count = 4, |
| 36 | |
| 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, |
| 41 | }; |
| 42 | |
| 43 | static void * |
| 44 | allocation_thread_function (void *closure) |
| 45 | { |
| 46 | struct list |
| 47 | { |
| 48 | struct list *next; |
| 49 | long dummy[4]; |
| 50 | }; |
| 51 | |
| 52 | struct list *head = NULL; |
| 53 | size_t allocated = 0; |
| 54 | while (allocated < per_thread_allocations) |
| 55 | { |
| 56 | struct list *new_head = xmalloc (n: sizeof (*new_head)); |
| 57 | allocated += sizeof (*new_head); |
| 58 | new_head->next = head; |
| 59 | head = new_head; |
| 60 | } |
| 61 | |
| 62 | xpthread_barrier_wait (barrier: &barrier); |
| 63 | |
| 64 | /* Main thread prints first statistics here. */ |
| 65 | |
| 66 | xpthread_barrier_wait (barrier: &barrier); |
| 67 | |
| 68 | while (head != NULL) |
| 69 | { |
| 70 | struct list *next_head = head->next; |
| 71 | free (ptr: head); |
| 72 | head = next_head; |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | static int |
| 79 | do_test (void) |
| 80 | { |
| 81 | xpthread_barrier_init (barrier: &barrier, NULL, count: thread_count + 1); |
| 82 | |
| 83 | pthread_t threads[thread_count]; |
| 84 | for (size_t i = 0; i < array_length (threads); ++i) |
| 85 | threads[i] = xpthread_create (NULL, thread_func: allocation_thread_function, NULL); |
| 86 | |
| 87 | xpthread_barrier_wait (barrier: &barrier); |
| 88 | puts (s: "info: After allocation:" ); |
| 89 | malloc_info (options: 0, stdout); |
| 90 | |
| 91 | xpthread_barrier_wait (barrier: &barrier); |
| 92 | for (size_t i = 0; i < array_length (threads); ++i) |
| 93 | xpthread_join (thr: threads[i]); |
| 94 | |
| 95 | puts (s: "\ninfo: After deallocation:" ); |
| 96 | malloc_info (options: 0, stdout); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | #include <support/test-driver.c> |
| 102 | |