Debian Codegraph @

tests/direct.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM 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
20 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29
30 #include <hugetlbfs.h>
31
32 #include "hugetests.h"
33
34 #define P0 "ffffffff"
35 #define IOSZ 4096
36 char buf[IOSZ] __attribute__ ((aligned (IOSZ)));
37 #define TMPFILE "/tmp/direct"
38
39 int main(int argc, char *argv[])
40 {
41 long hpage_size;
42 int fd, dfd;
43 void *p;
44 size_t ret;
45
46 test_init(argc, argv);
47
48 hpage_size = check_hugepagesize();
49
50 fd = hugetlbfs_unlinked_fd();
51 if (fd < 0)
52 FAIL("hugetlbfs_unlinked_fd()");
53
54 dfd = open(TMPFILE, O_CREAT|O_EXCL|O_DIRECT|O_RDWR, 0600);
55 if (dfd < 0)
56 CONFIG("Failed to open direct-IO file: %s", strerror(errno));
57 unlink(TMPFILE);
58
59 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
60 if (p == MAP_FAILED)
61 FAIL("mmap hugetlbfs file: %s", strerror(errno));
62
63 memcpy(p, P0, 8);
64
65 /* Direct write from huge page */
66 ret = write(dfd, p, IOSZ);
67 if (ret == -1)
68 FAIL("Direct-IO write from huge page: %s", strerror(errno));
69 if (ret != IOSZ)
70 FAIL("Short direct-IO write from huge page");
71 if (lseek(dfd, 0, SEEK_SET) == -1)
72 FAIL("lseek: %s", strerror(errno));
73
74 /* Check for accuracy */
75 ret = read(dfd, buf, IOSZ);
76 if (ret == -1)
77 FAIL("Direct-IO read to normal memory: %s", strerror(errno));
78 if (ret != IOSZ)
79 FAIL("Short direct-IO read to normal memory");
80 if (memcmp(P0, buf, 8))
81 FAIL("Memory mismatch after Direct-IO write");
82 if (lseek(dfd, 0, SEEK_SET) == -1)
83 FAIL("lseek: %s", strerror(errno));
84
85 /* Direct read to huge page */
86 memset(p, 0, IOSZ);
87 ret = read(dfd, p, IOSZ);
88 if (ret == -1)
89 FAIL("Direct-IO read to huge page: %s\n", strerror(errno));
90 if (ret != IOSZ)
91 FAIL("Short direct-IO read to huge page");
92
93 /* Check for accuracy */
94 if (memcmp(p, P0, 8))
95 FAIL("Memory mismatch after Direct-IO read");
96
97 close(dfd);
98 unlink(TMPFILE);
99
100 PASS();
101 }