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>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
52 #include <rte_launch.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_debug.h>
57 #include <rte_common.h>
58 #include "rte_string_fns.h"
59 #include "eal_internal_cfg.h"
60 #include "eal_hugepages.h"
61 #include "eal_filesystem.h"
63 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
66 get_num_hugepages(const char *subdir)
69 long unsigned num_pages = 0;
70 const char *nr_hp_file;
72 /* if secondary process, just look at the number of hugepages,
73 * otherwise look at number of free hugepages */
74 if (internal_config.process_type == RTE_PROC_SECONDARY)
75 nr_hp_file = "nr_hugepages";
77 nr_hp_file = "free_hugepages";
79 rte_snprintf(path, sizeof(path), "%s/%s/%s",
80 sys_dir_path, subdir, nr_hp_file);
82 if (eal_parse_sysfs_value(path, &num_pages) < 0)
86 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
89 return (int32_t)num_pages;
93 get_default_hp_size(void)
95 const char proc_meminfo[] = "/proc/meminfo";
96 const char str_hugepagesz[] = "Hugepagesize:";
97 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
99 unsigned long long size = 0;
101 FILE *fd = fopen(proc_meminfo, "r");
103 rte_panic("Cannot open %s\n", proc_meminfo);
104 while(fgets(buffer, sizeof(buffer), fd)){
105 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
106 size = rte_str_to_size(&buffer[hugepagesz_len]);
112 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
117 get_hugepage_dir(uint64_t hugepage_sz)
119 enum proc_mount_fieldnames {
126 static uint64_t default_size = 0;
127 const char proc_mounts[] = "/proc/mounts";
128 const char hugetlbfs_str[] = "hugetlbfs";
129 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
130 const char pagesize_opt[] = "pagesize=";
131 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
132 const char split_tok = ' ';
133 char *splitstr[_FIELDNAME_MAX];
137 FILE *fd = fopen(proc_mounts, "r");
139 rte_panic("Cannot open %s\n", proc_mounts);
141 if (default_size == 0)
142 default_size = get_default_hp_size();
144 while (fgets(buf, sizeof(buf), fd)){
145 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
146 split_tok) != _FIELDNAME_MAX) {
147 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
148 break; /* return NULL */
151 /* we have a specified --huge-dir option, only examine that dir */
152 if (internal_config.hugepage_dir != NULL &&
153 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
156 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
157 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
159 /* if no explicit page size, the default page size is compared */
160 if (pagesz_str == NULL){
161 if (hugepage_sz == default_size){
162 retval = strdup(splitstr[MOUNTPT]);
166 /* there is an explicit page size, so check it */
168 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
169 if (pagesz == hugepage_sz) {
170 retval = strdup(splitstr[MOUNTPT]);
174 } /* end if strncmp hugetlbfs */
175 } /* end while fgets */
182 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
184 char buf[sizeof(*a)];
185 memcpy(buf, a, sizeof(buf));
186 memcpy(a, b, sizeof(buf));
187 memcpy(b, buf, sizeof(buf));
191 * Clear the hugepage directory of whatever hugepage files
192 * there are. Checks if the file is locked (i.e.
193 * if it's in use by another DPDK process).
196 clear_hugedir(const char * hugedir)
199 struct dirent *dirent;
200 int dir_fd, fd, lck_result;
201 const char filter[] = "*map_*"; /* matches hugepage files */
204 dir = opendir(hugedir);
206 RTE_LOG(INFO, EAL, "Unable to open hugepage directory %s\n",
212 dirent = readdir(dir);
214 RTE_LOG(INFO, EAL, "Unable to read hugepage directory %s\n",
219 while(dirent != NULL){
220 /* skip files that don't match the hugepage pattern */
221 if (fnmatch(filter, dirent->d_name, 0) > 0) {
222 dirent = readdir(dir);
226 /* try and lock the file */
227 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
229 /* skip to next file */
231 dirent = readdir(dir);
235 /* non-blocking lock */
236 lck_result = flock(fd, LOCK_EX | LOCK_NB);
238 /* if lock succeeds, unlock and remove the file */
239 if (lck_result != -1) {
241 unlinkat(dir_fd, dirent->d_name, 0);
244 dirent = readdir(dir);
254 RTE_LOG(INFO, EAL, "Error while clearing hugepage dir: %s\n",
261 * when we initialize the hugepage info, everything goes
262 * to socket 0 by default. it will later get sorted by memory
263 * initialization procedure.
266 eal_hugepage_info_init(void)
268 const char dirent_start_text[] = "hugepages-";
269 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
270 unsigned i, num_sizes = 0;
272 DIR *dir = opendir(sys_dir_path);
274 rte_panic("Cannot open directory %s to read system hugepage info\n",
277 struct dirent *dirent = readdir(dir);
278 while(dirent != NULL){
279 if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) == 0){
280 struct hugepage_info *hpi = \
281 &internal_config.hugepage_info[num_sizes];
282 hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]);
283 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
285 /* first, check if we have a mountpoint */
286 if (hpi->hugedir == NULL){
288 if ((num_pages = get_num_hugepages(dirent->d_name)) > 0)
289 RTE_LOG(INFO, EAL, "%u hugepages of size %llu reserved, "\
290 "but no mounted hugetlbfs found for that size\n",
292 (unsigned long long)hpi->hugepage_sz);
294 /* try to obtain a writelock */
295 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
297 /* if blocking lock failed */
298 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
299 RTE_LOG(CRIT, EAL, "Failed to lock hugepage directory!\n");
302 /* clear out the hugepages dir from unused pages */
303 if (clear_hugedir(hpi->hugedir) == -1)
306 /* for now, put all pages into socket 0,
307 * later they will be sorted */
308 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
310 #ifndef RTE_ARCH_X86_64
311 /* for 32-bit systems, limit number of hugepages to 1GB per page size */
312 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
313 RTE_PGSIZE_1G / hpi->hugepage_sz);
319 dirent = readdir(dir);
322 internal_config.num_hugepage_sizes = num_sizes;
324 /* sort the page directory entries by size, largest to smallest */
325 for (i = 0; i < num_sizes; i++){
327 for (j = i+1; j < num_sizes; j++)
328 if (internal_config.hugepage_info[j-1].hugepage_sz < \
329 internal_config.hugepage_info[j].hugepage_sz)
330 swap_hpi(&internal_config.hugepage_info[j-1],
331 &internal_config.hugepage_info[j]);
334 /* now we have all info, check we have at least one valid size */
335 for (i = 0; i < num_sizes; i++)
336 if (internal_config.hugepage_info[i].hugedir != NULL &&
337 internal_config.hugepage_info[i].num_pages[0] > 0)
340 /* no valid hugepage mounts available, return error */