aboutsummaryrefslogtreecommitdiff
path: root/tests/heapshrink.c
blob: 0644c7859b108be5dbc705fc71b2c680cd0eae36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Test heap shrinking for libhugetlbfs.
 * Copyright 2007 Cray Inc.  All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hugetests.h"

/*
 * We cannot test mapping size against huge page size because we are not linked
 * against libhugetlbfs so gethugepagesize() won't work.  So instead we define
 * our MIN_PAGE_SIZE as 64 kB (the largest base page available) and make sure
 * the mapping page size is larger than this.
 */
#define MIN_PAGE_SIZE 65536

#define	SIZE	(32 * 1024 * 1024)

int main(int argc, char **argv)
{
	int is_huge, have_env, shrink_ok, have_helper;
	unsigned long long mapping_size;
	void *p;

	test_init(argc, argv);

	have_env = getenv("HUGETLB_MORECORE") != NULL;
	shrink_ok = getenv("HUGETLB_MORECORE_SHRINK") != NULL;
	p = getenv("LD_PRELOAD");
	have_helper = p != NULL && strstr(p, "heapshrink") != NULL;

	p = malloc(SIZE);
	if (!p) {
		if (shrink_ok && have_helper) {
			/* Hitting unexpected behavior in malloc() */
			PASS_INCONCLUSIVE();
		} else
			FAIL("malloc(%d) failed\n", SIZE);
	}
	memset(p, 0, SIZE);
	mapping_size = get_mapping_page_size(p);
	is_huge = (mapping_size > MIN_PAGE_SIZE);
	if (have_env && !is_huge) {
		if (shrink_ok && have_helper) {
			/* Hitting unexpected behavior in malloc() */
			PASS_INCONCLUSIVE();
		} else
			FAIL("Heap not on hugepages");
	}
	if (!have_env && is_huge)
		FAIL("Heap unexpectedly on hugepages");

	free(p);
	mapping_size = get_mapping_page_size(p+SIZE-1);
	if (shrink_ok && mapping_size > MIN_PAGE_SIZE)
		FAIL("Heap did not shrink");
	PASS();
}