Debian Codegraph @

tests/fallocate_basic.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 20015 Mike Kravetz, Oracle Corporation
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.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * 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 this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #define _GNU_SOURCE
20
21 #include <linux/falloc.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <linux/falloc.h>
29
30 #include <hugetlbfs.h>
31
32 #include "hugetests.h"
33
34 #define P "fallocate-basic"
35 #define DESC \
36 "* Test basic fallocate functionality in hugetlbfs. Preallocate *\n"\
37 "* huge pages to a file in hugetlbfs, and then remove the pages *\n"\
38 "* via hole punch. *"
39
40 #define min(a,b) (((a) < (b)) ? (a) : (b))
41
42 #define MAX_PAGES_TO_USE 5
43
44 int main(int argc, char *argv[])
45 {
46 long hpage_size;
47 long nr_hpages_free;
48 int fd;
49 int err;
50 int max_iterations;
51 unsigned long free_before, free_after;
52
53 test_init(argc, argv);
54
55 hpage_size = check_hugepagesize();
56 nr_hpages_free = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
57 max_iterations = min(nr_hpages_free, MAX_PAGES_TO_USE);
58
59 fd = hugetlbfs_unlinked_fd();
60 if (fd < 0)
61 FAIL("hugetlbfs_unlinked_fd()");
62
63 free_before = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
64
65 /* First preallocate file with max_iterations pages */
66 err = fallocate(fd, 0, 0, hpage_size * max_iterations);
67 if (err) {
68 if (errno == EOPNOTSUPP)
69 IRRELEVANT();
70 if (err)
71 FAIL("fallocate(): %s", strerror(errno));
72 }
73
74 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
75 if (free_before - free_after != max_iterations)
76 FAIL("fallocate did not preallocate %u huge pages\n",
77 max_iterations);
78
79 /* Now punch a hole of the same size */
80 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
81 0, hpage_size * max_iterations);
82 if (err)
83 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
84
85 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
86 if (free_after != free_before)
87 FAIL("fallocate hole punch did not release %u huge pages\n",
88 max_iterations);
89
90 PASS();
91 }