]>
Commit | Line | Data |
---|---|---|
1e26d351 | 1 | /* Bug 22111: Test that threads do not leak their per thread cache. |
04277e02 | 2 | Copyright (C) 2015-2019 Free Software Foundation, Inc. |
1e26d351 CD |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | /* The point of this test is to start and exit a large number of | |
20 | threads, while at the same time looking to see if the used | |
21 | memory grows with each round of threads run. If the memory | |
22 | grows above some linear bound we declare the test failed and | |
23 | that the malloc implementation is leaking memory with each | |
24 | thread. This is a good indicator that the thread local cache | |
25 | is leaking chunks. */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <malloc.h> | |
30 | #include <pthread.h> | |
31 | #include <assert.h> | |
32 | ||
33 | #include <support/check.h> | |
34 | #include <support/support.h> | |
35 | #include <support/xthread.h> | |
36 | ||
37 | void * | |
38 | worker (void *data) | |
39 | { | |
40 | void *ret; | |
41 | /* Allocate an arbitrary amount of memory that is known to fit into | |
42 | the thread local cache (tcache). If we have at least 64 bins | |
43 | (default e.g. TCACHE_MAX_BINS) we should be able to allocate 32 | |
44 | bytes and force malloc to fill the tcache. We are assuming tcahce | |
45 | init happens at the first small alloc, but it might in the future | |
46 | be deferred to some other point. Therefore to future proof this | |
47 | test we include a full alloc/free/alloc cycle for the thread. We | |
48 | need a compiler barrier to avoid the removal of the useless | |
49 | alloc/free. We send some memory back to main to have the memory | |
50 | freed after the thread dies, as just another check that the chunks | |
51 | that were previously in the tcache are still OK to free after | |
52 | thread death. */ | |
53 | ret = xmalloc (32); | |
54 | __asm__ volatile ("" ::: "memory"); | |
55 | free (ret); | |
56 | return (void *) xmalloc (32); | |
57 | } | |
58 | ||
59 | static int | |
60 | do_test (void) | |
61 | { | |
62 | pthread_t *thread; | |
63 | struct mallinfo info_before, info_after; | |
64 | void *retval; | |
65 | ||
66 | /* This is an arbitrary choice. We choose a total of THREADS | |
67 | threads created and joined. This gives us enough iterations to | |
68 | show a leak. */ | |
69 | int threads = 100000; | |
70 | ||
71 | /* Avoid there being 0 malloc'd data at this point by allocating the | |
72 | pthread_t required to run the test. */ | |
73 | thread = (pthread_t *) xcalloc (1, sizeof (pthread_t)); | |
74 | ||
75 | info_before = mallinfo (); | |
76 | ||
77 | assert (info_before.uordblks != 0); | |
78 | ||
79 | printf ("INFO: %d (bytes) are in use before starting threads.\n", | |
80 | info_before.uordblks); | |
81 | ||
82 | for (int loop = 0; loop < threads; loop++) | |
83 | { | |
84 | *thread = xpthread_create (NULL, worker, NULL); | |
85 | retval = xpthread_join (*thread); | |
86 | free (retval); | |
87 | } | |
88 | ||
89 | info_after = mallinfo (); | |
90 | printf ("INFO: %d (bytes) are in use after all threads joined.\n", | |
91 | info_after.uordblks); | |
92 | ||
93 | /* We need to compare the memory in use before and the memory in use | |
94 | after starting and joining THREADS threads. We almost always grow | |
95 | memory slightly, but not much. Consider that if even 1-byte leaked | |
96 | per thread we'd have THREADS bytes of additional memory, and in | |
97 | general the in-use at the start of main is quite low. We will | |
98 | always leak a full malloc chunk, and never just 1-byte, therefore | |
99 | anything above "+ threads" from the start (constant offset) is a | |
100 | leak. Obviously this assumes no thread-related malloc'd internal | |
101 | libc data structures persist beyond the thread death, and any that | |
102 | did would limit the number of times you could call pthread_create, | |
103 | which is a QoI we'd want to detect and fix. */ | |
104 | if (info_after.uordblks > (info_before.uordblks + threads)) | |
105 | FAIL_EXIT1 ("Memory usage after threads is too high.\n"); | |
106 | ||
107 | /* Did not detect excessive memory usage. */ | |
108 | free (thread); | |
109 | exit (0); | |
110 | } | |
111 | ||
ba374b6d | 112 | #define TIMEOUT 50 |
1e26d351 | 113 | #include <support/test-driver.c> |