Debian Codegraph @

tests/heapshrink.c

raw symbols SCIP
1 /*
2 * Test heap shrinking for libhugetlbfs.
3 * Copyright 2007 Cray Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This 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
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "hugetests.h"
24
25 /*
26 * We cannot test mapping size against huge page size because we are not linked
27 * against libhugetlbfs so gethugepagesize() won't work. So instead we define
28 * our MIN_PAGE_SIZE as 64 kB (the largest base page available) and make sure
29 * the mapping page size is larger than this.
30 */
31 #define MIN_PAGE_SIZE 65536
32 #define MAX(a, b) a > b ? a : b
33
34 int main(int argc, char **argv)
35 {
36 int is_huge, have_env, shrink_ok, have_helper, tcache_enabled;
37 unsigned long long mapping_size;
38 void *p;
39 long size = MAX(32*1024*1024, kernel_default_hugepage_size());
40
41 test_init(argc, argv);
42
43 have_env = getenv("HUGETLB_MORECORE") != NULL;
44 shrink_ok = getenv("HUGETLB_MORECORE_SHRINK") != NULL;
45 p = getenv("LD_PRELOAD");
46 have_helper = p != NULL && strstr(p, "heapshrink") != NULL;
47
48 /*
49 * After upstream commit: (glibc-2.25.90-688-gd5c3fafc43) glibc has a
50 * new per-thread caching mechanism that will NOT allow this test to
51 * successfully measure if heap has shrunk or not due to the fact that
52 * heap won't have its sized reduced right away.
53 *
54 * In order to disable it you need to have the tunable GLIBC in place.
55 * Unfortunately, it requires to be set before program is loaded, as an
56 * environment variable, since we can't re-initialize malloc() from the
57 * program context (not even with a constructor function), and the
58 * tunable is only evaluated during malloc() initialization.
59 *
60 * GLIBC_TUNABLES=glibc.malloc.tcache_count=0
61 */
62 p = getenv("GLIBC_TUNABLES");
63 tcache_enabled = p != NULL && strstr(p, "malloc.tcache_count=0");
64
65 p = malloc(size);
66 if (!p) {
67 if (shrink_ok && have_helper) {
68 /* Hitting unexpected behavior in malloc() */
69 PASS_INCONCLUSIVE();
70 } else
71 FAIL("malloc(%ld) failed\n", size);
72 }
73 memset(p, 0, size);
74 mapping_size = get_mapping_page_size(p);
75 is_huge = (mapping_size > MIN_PAGE_SIZE);
76 if (have_env && !is_huge) {
77 if (shrink_ok && have_helper) {
78 /* Hitting unexpected behavior in malloc() */
79 PASS_INCONCLUSIVE();
80 } else
81 FAIL("Heap not on hugepages");
82 }
83 if (!have_env && is_huge)
84 FAIL("Heap unexpectedly on hugepages");
85
86 free(p);
87 mapping_size = get_mapping_page_size(p+size-1);
88 if (shrink_ok && mapping_size > MIN_PAGE_SIZE) {
89 if (tcache_enabled)
90 FAIL("Heap did not shrink");
91 else
92 FAIL("Heap didn't shrink. Check malloc.tcache_count=0");
93 }
94
95 PASS();
96 }