pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post e1ff71 by Anonymous on Tue 6th Sep 2022 15:27
original post adc by Anonymous on Tue 6th Sep 2022 15:19
Download diff
Show old version | new version | both versions

    
11
/*
22
 * hugepage_alloc_test1.c - test the availabilty of
33
 * largepages/hugepages
44
 *
55
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
66
 */
77
88
#include <stdlib.h>
99
#include <stdio.h>
1010
#include <stdbool.h>
1111
#include <errno.h>
1212
#include <fcntl.h>
1313
#include <unistd.h>
1414
1515
#include <sys/mman.h>
1616
#include <sys/stat.h>
1717
1818
#include <linux/mman.h>
1919
2020
/*
2121
 * Before using this Linux 5.10.0-13-rt-686-pae requires that "root"
2222
 * reserves hugepages like this:
2323
 *
2424
 * $ printf '128' > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
2525
 * $ cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
2626
 * 128
2727
 *
2828
 * Notes:
2929
 * - Terminology: UNIX uses the term "largepages", Linux defiantly
3030
 * uses "hugepages"
3131
 * - the value is the number of MMU (large-/huge-)pages, NOT the
3232
 * number of bytes.
3333
 * - $ /usr/bin/pagesize -a # can inform you which pagesizes are
3434
 * available (i686 usually has { 4k, 2M } pagesizes, SPARCv9 has
3535
 * { 8k, 64k, 4M, 32M, ... } )
3636
 */
3737
3838
/* Linux-specific version */
3939
ssize_t get_number_of_free_hugepages(void)
4040
{
4141
	int	dirfd;
4242
	int	fd;
4343
	/*
4444
	 * |round(log10(18446744073709551615))| is |19| on a 64bit
4545
	 * system, so |128| should be enough (including sign)
4646
	 */
4747
	char	buf[128];
4848
	ssize_t	numpages;
4949
	ssize_t	rs; /* read size */
5050
5151
	/*
5252
	 * extra step here, since real-world applications likely
5353
	 * want to look at "nr_hugepages" etc, too
5454
	 */
5555
	dirfd = open("/sys/kernel/mm/hugepages/hugepages-2048kB/",
5656
		O_DIRECTORY);
5757
	if (dirfd < 0) {
5858
		perror("open sysfs hugepage dir");
5959
		return -1;
6060
	}
6161
6262
	fd = openat(dirfd, "free_hugepages", O_RDONLY);
6363
	if (fd < 0) {
6464
		perror("open sysfs hugepage free pages file");
6565
		(void)close(dirfd);
6666
		return -1;
6767
	}
6868
	rs = read(fd, buf, sizeof(buf));
6969
	if (rs < 0) {
7070
		perror("read error sysfs hugepage free pages file");
7171
		(void)close(dirfd);
7272
		(void)close(fd);
7373
		return -1;
7474
	}
7575
7676
	(void)close(dirfd);
7777
	(void)close(fd);
7878
7979
	buf[rs] = '\0';
8080
#if MYDEBUG
8181
	if ((rs > 0) && (buf[rs-1] == '\n'))
8282
		buf[rs-1] = '\0';
8383
	(void)printf("string returned: |%s|\n", buf);
8484
#endif /* MYDEBUG */
8585
8686
	errno = 0;
8787
	numpages = strtol(buf, NULL, 10);
8888
	if (errno != 0) {
8989
		perror("error parsing value of free hugepages");
9090
		return -1;
9191
	}
9292
9393
	return numpages;
9494
}
9595
9696
9797
int main(int ac, char *av[])
9898
{
9999
	void		*p;
100100
	int		saved_errno;
101101
#define TEST_NUM_ALLOC_PAGES (32)
102102
	const size_t	size = TEST_NUM_ALLOC_PAGES*1024*1024;
103103
	ssize_t		free_numpages;
104104
105105
	(void)puts("#start.");
106106
107107
	free_numpages = get_number_of_free_hugepages();
108108
109109
	if (free_numpages < (TEST_NUM_ALLOC_PAGES+8)) {
110110
		(void)fprintf(stderr,
111111
			"not enough hugepages, currently free %ld\n",
112112
			(long)free_numpages);
113113
		return EXIT_FAILURE;
114114
	}
115115
116116
	/* get mmap()'ed memory with hugepage attribute */
117117
	p = mmap(0,
118118
		size,
119119
		PROT_READ|PROT_WRITE,
120120
		MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
121121
	saved_errno = errno;
122122
	if (p == MAP_FAILED) {
123123
		perror("mmap");
124124
		return EXIT_FAILURE;
125125
	}
126126
127127
	(void)printf("map address = 0x%lx, errno=%d\n",
128128
		(unsigned long)p, (int)saved_errno);
129129
130130
	/* use memory */
131131
	volatile unsigned char *c = p;
132132
	for (size_t i = 0 ; i < size ; i++) {
133133
		c[i] = (unsigned char)(i % 255);
134134
	}
135135
136136
#if MYDEBUG_USE_PROC_SMAPS
137137
	/*
138138
	 * Wait here so we can check out
139139
	 * $ cat /proc/$mypid/smaps | fgrep -i huge #
140140
	 */
141141
	sleep(1000);
142142
#endif /* MYDEBUG_USE_PROC_SMAPS */
143143
144144
	if (munmap(p, size) == -1)
145145
		perror("munmap");
146146
147147
	(void)puts("#done.");
148148
149149
	return EXIT_SUCCESS;
150150
}
151151

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at