4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
46 #include <sys/queue.h>
48 #include <rte_memory.h>
50 #include <rte_launch.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_debug.h>
55 #include <rte_common.h>
56 #include "rte_string_fns.h"
57 #include "eal_internal_cfg.h"
58 #include "eal_hugepages.h"
59 #include "eal_filesystem.h"
61 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
63 /* this function is only called from eal_hugepage_info_init which itself
64 * is only called from a primary process */
66 get_num_hugepages(const char *subdir)
69 long unsigned resv_pages, num_pages = 0;
70 const char *nr_hp_file = "free_hugepages";
71 const char *nr_rsvd_file = "resv_hugepages";
73 /* first, check how many reserved pages kernel reports */
74 snprintf(path, sizeof(path), "%s/%s/%s",
75 sys_dir_path, subdir, nr_rsvd_file);
76 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
79 snprintf(path, sizeof(path), "%s/%s/%s",
80 sys_dir_path, subdir, nr_hp_file);
81 if (eal_parse_sysfs_value(path, &num_pages) < 0)
85 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
88 /* adjust num_pages */
89 if (num_pages >= resv_pages)
90 num_pages -= resv_pages;
94 /* we want to return a uint32_t and more than this looks suspicious
96 if (num_pages > UINT32_MAX)
97 num_pages = UINT32_MAX;
103 get_default_hp_size(void)
105 const char proc_meminfo[] = "/proc/meminfo";
106 const char str_hugepagesz[] = "Hugepagesize:";
107 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
109 unsigned long long size = 0;
111 FILE *fd = fopen(proc_meminfo, "r");
113 rte_panic("Cannot open %s\n", proc_meminfo);
114 while(fgets(buffer, sizeof(buffer), fd)){
115 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
116 size = rte_str_to_size(&buffer[hugepagesz_len]);
122 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
127 get_hugepage_dir(uint64_t hugepage_sz)
129 enum proc_mount_fieldnames {
136 static uint64_t default_size = 0;
137 const char proc_mounts[] = "/proc/mounts";
138 const char hugetlbfs_str[] = "hugetlbfs";
139 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
140 const char pagesize_opt[] = "pagesize=";
141 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
142 const char split_tok = ' ';
143 char *splitstr[_FIELDNAME_MAX];
147 FILE *fd = fopen(proc_mounts, "r");
149 rte_panic("Cannot open %s\n", proc_mounts);
151 if (default_size == 0)
152 default_size = get_default_hp_size();
154 while (fgets(buf, sizeof(buf), fd)){
155 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
156 split_tok) != _FIELDNAME_MAX) {
157 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
158 break; /* return NULL */
161 /* we have a specified --huge-dir option, only examine that dir */
162 if (internal_config.hugepage_dir != NULL &&
163 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
166 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
167 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
169 /* if no explicit page size, the default page size is compared */
170 if (pagesz_str == NULL){
171 if (hugepage_sz == default_size){
172 retval = strdup(splitstr[MOUNTPT]);
176 /* there is an explicit page size, so check it */
178 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
179 if (pagesz == hugepage_sz) {
180 retval = strdup(splitstr[MOUNTPT]);
184 } /* end if strncmp hugetlbfs */
185 } /* end while fgets */
192 * Clear the hugepage directory of whatever hugepage files
193 * there are. Checks if the file is locked (i.e.
194 * if it's in use by another DPDK process).
197 clear_hugedir(const char * hugedir)
200 struct dirent *dirent;
201 int dir_fd, fd, lck_result;
202 const char filter[] = "*map_*"; /* matches hugepage files */
205 dir = opendir(hugedir);
207 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
213 dirent = readdir(dir);
215 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
220 while(dirent != NULL){
221 /* skip files that don't match the hugepage pattern */
222 if (fnmatch(filter, dirent->d_name, 0) > 0) {
223 dirent = readdir(dir);
227 /* try and lock the file */
228 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
230 /* skip to next file */
232 dirent = readdir(dir);
236 /* non-blocking lock */
237 lck_result = flock(fd, LOCK_EX | LOCK_NB);
239 /* if lock succeeds, unlock and remove the file */
240 if (lck_result != -1) {
242 unlinkat(dir_fd, dirent->d_name, 0);
245 dirent = readdir(dir);
255 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
262 compare_hpi(const void *a, const void *b)
264 const struct hugepage_info *hpi_a = a;
265 const struct hugepage_info *hpi_b = b;
267 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
271 * when we initialize the hugepage info, everything goes
272 * to socket 0 by default. it will later get sorted by memory
273 * initialization procedure.
276 eal_hugepage_info_init(void)
278 const char dirent_start_text[] = "hugepages-";
279 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
280 unsigned i, num_sizes = 0;
282 struct dirent *dirent;
284 dir = opendir(sys_dir_path);
287 "Cannot open directory %s to read system hugepage info\n",
292 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
293 struct hugepage_info *hpi;
295 if (strncmp(dirent->d_name, dirent_start_text,
296 dirent_start_len) != 0)
299 if (num_sizes >= MAX_HUGEPAGE_SIZES)
302 hpi = &internal_config.hugepage_info[num_sizes];
304 rte_str_to_size(&dirent->d_name[dirent_start_len]);
305 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
307 /* first, check if we have a mountpoint */
308 if (hpi->hugedir == NULL) {
311 num_pages = get_num_hugepages(dirent->d_name);
314 "%" PRIu32 " hugepages of size "
315 "%" PRIu64 " reserved, but no mounted "
316 "hugetlbfs found for that size\n",
317 num_pages, hpi->hugepage_sz);
321 /* try to obtain a writelock */
322 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
324 /* if blocking lock failed */
325 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
327 "Failed to lock hugepage directory!\n");
330 /* clear out the hugepages dir from unused pages */
331 if (clear_hugedir(hpi->hugedir) == -1)
334 /* for now, put all pages into socket 0,
335 * later they will be sorted */
336 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
339 /* for 32-bit systems, limit number of hugepages to
340 * 1GB per page size */
341 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
342 RTE_PGSIZE_1G / hpi->hugepage_sz);
349 /* something went wrong, and we broke from the for loop above */
353 internal_config.num_hugepage_sizes = num_sizes;
355 /* sort the page directory entries by size, largest to smallest */
356 qsort(&internal_config.hugepage_info[0], num_sizes,
357 sizeof(internal_config.hugepage_info[0]), compare_hpi);
359 /* now we have all info, check we have at least one valid size */
360 for (i = 0; i < num_sizes; i++)
361 if (internal_config.hugepage_info[i].hugedir != NULL &&
362 internal_config.hugepage_info[i].num_pages[0] > 0)
365 /* no valid hugepage mounts available, return error */