Linux Audio

Check our new training course

Loading...
v3.15
 
 1/*
 2 * hugepage-mmap:
 3 *
 4 * Example of using huge page memory in a user application using the mmap
 5 * system call.  Before running this application, make sure that the
 6 * administrator has mounted the hugetlbfs filesystem (on some directory
 7 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
 8 * example, the app is requesting memory of size 256MB that is backed by
 9 * huge pages.
10 *
11 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
12 * huge pages.  That means that if one requires a fixed address, a huge page
13 * aligned address starting with 0x800000... will be required.  If a fixed
14 * address is not required, the kernel will select an address in the proper
15 * range.
16 * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <sys/mman.h>
23#include <fcntl.h>
24
25#define FILE_NAME "huge/hugepagefile"
26#define LENGTH (256UL*1024*1024)
27#define PROTECTION (PROT_READ | PROT_WRITE)
28
29/* Only ia64 requires this */
30#ifdef __ia64__
31#define ADDR (void *)(0x8000000000000000UL)
32#define FLAGS (MAP_SHARED | MAP_FIXED)
33#else
34#define ADDR (void *)(0x0UL)
35#define FLAGS (MAP_SHARED)
36#endif
37
38static void check_bytes(char *addr)
39{
40	printf("First hex is %x\n", *((unsigned int *)addr));
41}
42
43static void write_bytes(char *addr)
44{
45	unsigned long i;
46
47	for (i = 0; i < LENGTH; i++)
48		*(addr + i) = (char)i;
49}
50
51static int read_bytes(char *addr)
52{
53	unsigned long i;
54
55	check_bytes(addr);
56	for (i = 0; i < LENGTH; i++)
57		if (*(addr + i) != (char)i) {
58			printf("Mismatch at %lu\n", i);
59			return 1;
60		}
61	return 0;
62}
63
64int main(void)
65{
66	void *addr;
67	int fd, ret;
68
69	fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
70	if (fd < 0) {
71		perror("Open failed");
72		exit(1);
73	}
74
75	addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
76	if (addr == MAP_FAILED) {
77		perror("mmap");
78		unlink(FILE_NAME);
79		exit(1);
80	}
81
82	printf("Returned address is %p\n", addr);
83	check_bytes(addr);
84	write_bytes(addr);
85	ret = read_bytes(addr);
86
87	munmap(addr, LENGTH);
88	close(fd);
89	unlink(FILE_NAME);
90
91	return ret;
92}
v5.4
 1// SPDX-License-Identifier: GPL-2.0
 2/*
 3 * hugepage-mmap:
 4 *
 5 * Example of using huge page memory in a user application using the mmap
 6 * system call.  Before running this application, make sure that the
 7 * administrator has mounted the hugetlbfs filesystem (on some directory
 8 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
 9 * example, the app is requesting memory of size 256MB that is backed by
10 * huge pages.
11 *
12 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
13 * huge pages.  That means that if one requires a fixed address, a huge page
14 * aligned address starting with 0x800000... will be required.  If a fixed
15 * address is not required, the kernel will select an address in the proper
16 * range.
17 * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <sys/mman.h>
24#include <fcntl.h>
25
26#define FILE_NAME "huge/hugepagefile"
27#define LENGTH (256UL*1024*1024)
28#define PROTECTION (PROT_READ | PROT_WRITE)
29
30/* Only ia64 requires this */
31#ifdef __ia64__
32#define ADDR (void *)(0x8000000000000000UL)
33#define FLAGS (MAP_SHARED | MAP_FIXED)
34#else
35#define ADDR (void *)(0x0UL)
36#define FLAGS (MAP_SHARED)
37#endif
38
39static void check_bytes(char *addr)
40{
41	printf("First hex is %x\n", *((unsigned int *)addr));
42}
43
44static void write_bytes(char *addr)
45{
46	unsigned long i;
47
48	for (i = 0; i < LENGTH; i++)
49		*(addr + i) = (char)i;
50}
51
52static int read_bytes(char *addr)
53{
54	unsigned long i;
55
56	check_bytes(addr);
57	for (i = 0; i < LENGTH; i++)
58		if (*(addr + i) != (char)i) {
59			printf("Mismatch at %lu\n", i);
60			return 1;
61		}
62	return 0;
63}
64
65int main(void)
66{
67	void *addr;
68	int fd, ret;
69
70	fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
71	if (fd < 0) {
72		perror("Open failed");
73		exit(1);
74	}
75
76	addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
77	if (addr == MAP_FAILED) {
78		perror("mmap");
79		unlink(FILE_NAME);
80		exit(1);
81	}
82
83	printf("Returned address is %p\n", addr);
84	check_bytes(addr);
85	write_bytes(addr);
86	ret = read_bytes(addr);
87
88	munmap(addr, LENGTH);
89	close(fd);
90	unlink(FILE_NAME);
91
92	return ret;
93}