|
Packit |
2d622a |
/*
|
|
Packit |
2d622a |
* libhugetlbfs - Easy use of Linux hugepages
|
|
Packit |
2d622a |
* Copyright (C) 20015 Mike Kravetz, Oracle Corporation
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* This library is free software; you can redistribute it and/or
|
|
Packit |
2d622a |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
2d622a |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
2d622a |
* the License, or (at your option) any later version.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* This library is distributed in the hope that it will be useful, but
|
|
Packit |
2d622a |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
2d622a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
2d622a |
* Lesser General Public License for more details.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
2d622a |
* License along with this library; if not, write to the Free Software
|
|
Packit |
2d622a |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
2d622a |
*/
|
|
Packit |
2d622a |
#define _GNU_SOURCE
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#include <linux/falloc.h>
|
|
Packit |
2d622a |
#include <stdio.h>
|
|
Packit |
2d622a |
#include <stdlib.h>
|
|
Packit |
2d622a |
#include <unistd.h>
|
|
Packit |
2d622a |
#include <signal.h>
|
|
Packit |
2d622a |
#include <sys/mman.h>
|
|
Packit |
2d622a |
#include <fcntl.h>
|
|
Packit |
2d622a |
#include <linux/falloc.h>
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#include <hugetlbfs.h>
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#include "hugetests.h"
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#define P "fallocate-basic"
|
|
Packit |
2d622a |
#define DESC \
|
|
Packit |
2d622a |
"* Test basic fallocate functionality in hugetlbfs. Preallocate *\n"\
|
|
Packit |
2d622a |
"* huge pages to a file in hugetlbfs, and then remove the pages *\n"\
|
|
Packit |
2d622a |
"* via hole punch. *"
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#define MAX_PAGES_TO_USE 5
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
int main(int argc, char *argv[])
|
|
Packit |
2d622a |
{
|
|
Packit |
2d622a |
long hpage_size;
|
|
Packit |
2d622a |
long nr_hpages_free;
|
|
Packit |
2d622a |
int fd;
|
|
Packit |
2d622a |
int err;
|
|
Packit |
2d622a |
int max_iterations;
|
|
Packit |
2d622a |
unsigned long free_before, free_after;
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
test_init(argc, argv);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
hpage_size = check_hugepagesize();
|
|
Packit |
2d622a |
nr_hpages_free = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
|
|
Packit |
2d622a |
max_iterations = min(nr_hpages_free, MAX_PAGES_TO_USE);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
fd = hugetlbfs_unlinked_fd();
|
|
Packit |
2d622a |
if (fd < 0)
|
|
Packit |
2d622a |
FAIL("hugetlbfs_unlinked_fd()");
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
free_before = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
/* First preallocate file with max_iterations pages */
|
|
Packit |
2d622a |
err = fallocate(fd, 0, 0, hpage_size * max_iterations);
|
|
Packit |
2d622a |
if (err) {
|
|
Packit |
2d622a |
if (errno == EOPNOTSUPP)
|
|
Packit |
2d622a |
IRRELEVANT();
|
|
Packit |
2d622a |
if (err)
|
|
Packit |
2d622a |
FAIL("fallocate(): %s", strerror(errno));
|
|
Packit |
2d622a |
}
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
|
|
Packit |
2d622a |
if (free_before - free_after != max_iterations)
|
|
Packit |
2d622a |
FAIL("fallocate did not preallocate %u huge pages\n",
|
|
Packit |
2d622a |
max_iterations);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
/* Now punch a hole of the same size */
|
|
Packit |
2d622a |
err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
|
|
Packit |
2d622a |
0, hpage_size * max_iterations);
|
|
Packit |
2d622a |
if (err)
|
|
Packit |
2d622a |
FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
|
|
Packit |
2d622a |
if (free_after != free_before)
|
|
Packit |
2d622a |
FAIL("fallocate hole punch did not release %u huge pages\n",
|
|
Packit |
2d622a |
max_iterations);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
PASS();
|
|
Packit |
2d622a |
}
|