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 resv_pages, num_pages = 0;
70 const char *nr_hp_file;
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);
77 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
80 /* if secondary process, just look at the number of hugepages,
81 * otherwise look at number of free hugepages */
82 if (internal_config.process_type == RTE_PROC_SECONDARY)
83 nr_hp_file = "nr_hugepages";
85 nr_hp_file = "free_hugepages";
87 snprintf(path, sizeof(path), "%s/%s/%s",
88 sys_dir_path, subdir, nr_hp_file);
90 if (eal_parse_sysfs_value(path, &num_pages) < 0)
94 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
97 /* adjust num_pages in case of primary process */
98 if (num_pages > 0 && internal_config.process_type == RTE_PROC_PRIMARY)
99 num_pages -= resv_pages;
101 return (int32_t)num_pages;
105 get_default_hp_size(void)
107 const char proc_meminfo[] = "/proc/meminfo";
108 const char str_hugepagesz[] = "Hugepagesize:";
109 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
111 unsigned long long size = 0;
113 FILE *fd = fopen(proc_meminfo, "r");
115 rte_panic("Cannot open %s\n", proc_meminfo);
116 while(fgets(buffer, sizeof(buffer), fd)){
117 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
118 size = rte_str_to_size(&buffer[hugepagesz_len]);
124 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
129 get_hugepage_dir(uint64_t hugepage_sz)
131 enum proc_mount_fieldnames {
138 static uint64_t default_size = 0;
139 const char proc_mounts[] = "/proc/mounts";
140 const char hugetlbfs_str[] = "hugetlbfs";
141 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
142 const char pagesize_opt[] = "pagesize=";
143 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
144 const char split_tok = ' ';
145 char *splitstr[_FIELDNAME_MAX];
149 FILE *fd = fopen(proc_mounts, "r");
151 rte_panic("Cannot open %s\n", proc_mounts);
153 if (default_size == 0)
154 default_size = get_default_hp_size();
156 while (fgets(buf, sizeof(buf), fd)){
157 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
158 split_tok) != _FIELDNAME_MAX) {
159 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
160 break; /* return NULL */
163 /* we have a specified --huge-dir option, only examine that dir */
164 if (internal_config.hugepage_dir != NULL &&
165 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
168 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
169 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
171 /* if no explicit page size, the default page size is compared */
172 if (pagesz_str == NULL){
173 if (hugepage_sz == default_size){
174 retval = strdup(splitstr[MOUNTPT]);
178 /* there is an explicit page size, so check it */
180 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
181 if (pagesz == hugepage_sz) {
182 retval = strdup(splitstr[MOUNTPT]);
186 } /* end if strncmp hugetlbfs */
187 } /* end while fgets */
194 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
196 char buf[sizeof(*a)];
197 memcpy(buf, a, sizeof(buf));
198 memcpy(a, b, sizeof(buf));
199 memcpy(b, buf, sizeof(buf));
203 * Clear the hugepage directory of whatever hugepage files
204 * there are. Checks if the file is locked (i.e.
205 * if it's in use by another DPDK process).
208 clear_hugedir(const char * hugedir)
211 struct dirent *dirent;
212 int dir_fd, fd, lck_result;
213 const char filter[] = "*map_*"; /* matches hugepage files */
216 dir = opendir(hugedir);
218 RTE_LOG(INFO, EAL, "Unable to open hugepage directory %s\n",
224 dirent = readdir(dir);
226 RTE_LOG(INFO, EAL, "Unable to read hugepage directory %s\n",
231 while(dirent != NULL){
232 /* skip files that don't match the hugepage pattern */
233 if (fnmatch(filter, dirent->d_name, 0) > 0) {
234 dirent = readdir(dir);
238 /* try and lock the file */
239 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
241 /* skip to next file */
243 dirent = readdir(dir);
247 /* non-blocking lock */
248 lck_result = flock(fd, LOCK_EX | LOCK_NB);
250 /* if lock succeeds, unlock and remove the file */
251 if (lck_result != -1) {
253 unlinkat(dir_fd, dirent->d_name, 0);
256 dirent = readdir(dir);
266 RTE_LOG(INFO, EAL, "Error while clearing hugepage dir: %s\n",
273 * when we initialize the hugepage info, everything goes
274 * to socket 0 by default. it will later get sorted by memory
275 * initialization procedure.
278 eal_hugepage_info_init(void)
280 const char dirent_start_text[] = "hugepages-";
281 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
282 unsigned i, num_sizes = 0;
284 DIR *dir = opendir(sys_dir_path);
286 rte_panic("Cannot open directory %s to read system hugepage info\n",
289 struct dirent *dirent = readdir(dir);
290 while(dirent != NULL){
291 if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) == 0){
292 struct hugepage_info *hpi = \
293 &internal_config.hugepage_info[num_sizes];
294 hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]);
295 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
297 /* first, check if we have a mountpoint */
298 if (hpi->hugedir == NULL){
300 if ((num_pages = get_num_hugepages(dirent->d_name)) > 0)
301 RTE_LOG(INFO, EAL, "%u hugepages of size %llu reserved, "\
302 "but no mounted hugetlbfs found for that size\n",
304 (unsigned long long)hpi->hugepage_sz);
306 /* try to obtain a writelock */
307 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
309 /* if blocking lock failed */
310 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
311 RTE_LOG(CRIT, EAL, "Failed to lock hugepage directory!\n");
315 /* clear out the hugepages dir from unused pages */
316 if (clear_hugedir(hpi->hugedir) == -1) {
321 /* for now, put all pages into socket 0,
322 * later they will be sorted */
323 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
326 /* for 32-bit systems, limit number of hugepages to 1GB per page size */
327 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
328 RTE_PGSIZE_1G / hpi->hugepage_sz);
334 dirent = readdir(dir);
337 internal_config.num_hugepage_sizes = num_sizes;
339 /* sort the page directory entries by size, largest to smallest */
340 for (i = 0; i < num_sizes; i++){
342 for (j = i+1; j < num_sizes; j++)
343 if (internal_config.hugepage_info[j-1].hugepage_sz < \
344 internal_config.hugepage_info[j].hugepage_sz)
345 swap_hpi(&internal_config.hugepage_info[j-1],
346 &internal_config.hugepage_info[j]);
349 /* now we have all info, check we have at least one valid size */
350 for (i = 0; i < num_sizes; i++)
351 if (internal_config.hugepage_info[i].hugedir != NULL &&
352 internal_config.hugepage_info[i].num_pages[0] > 0)
355 /* no valid hugepage mounts available, return error */