| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
|
| 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 |
} |