4 * Copyright(c) 2010-2012 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.
36 #include <sys/types.h>
44 #include <sys/queue.h>
46 #include "rte_memory.h"
47 #include "rte_memzone.h"
48 #include "rte_tailq.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";
64 get_num_hugepages(const char *subdir)
66 const char nr_hp_file[] = "nr_hugepages";
68 unsigned num_pages = 0;
70 rte_snprintf(path, sizeof(path), "%s/%s/%s",
71 sys_dir_path, subdir, nr_hp_file);
73 if (eal_parse_sysfs_value(path, &num_pages) < 0)
80 get_default_hp_size(void)
82 const char proc_meminfo[] = "/proc/meminfo";
83 const char str_hugepagesz[] = "Hugepagesize:";
84 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
86 unsigned long long size = 0;
88 FILE *fd = fopen(proc_meminfo, "r");
90 rte_panic("Cannot open %s\n", proc_meminfo);
91 while(fgets(buffer, sizeof(buffer), fd)){
92 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
93 size = rte_str_to_size(&buffer[hugepagesz_len]);
99 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
104 get_hugepage_dir(uint64_t hugepage_sz)
106 enum proc_mount_fieldnames {
113 static uint64_t default_size = 0;
114 const char proc_mounts[] = "/proc/mounts";
115 const char hugetlbfs_str[] = "hugetlbfs";
116 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
117 const char pagesize_opt[] = "pagesize=";
118 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
119 const char split_tok = ' ';
120 char *splitstr[_FIELDNAME_MAX];
124 FILE *fd = fopen(proc_mounts, "r");
126 rte_panic("Cannot open %s\n", proc_mounts);
128 if (default_size == 0)
129 default_size = get_default_hp_size();
131 while (fgets(buf, sizeof(buf), fd)){
132 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
133 split_tok) != _FIELDNAME_MAX) {
134 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
135 break; /* return NULL */
138 /* we have a specified --huge-dir option, only examine that dir */
139 if (internal_config.hugepage_dir != NULL &&
140 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
143 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
144 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
146 /* if no explicit page size, the default page size is compared */
147 if (pagesz_str == NULL){
148 if (hugepage_sz == default_size){
149 retval = strdup(splitstr[MOUNTPT]);
153 /* there is an explicit page size, so check it */
155 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
156 if (pagesz == hugepage_sz) {
157 retval = strdup(splitstr[MOUNTPT]);
161 } /* end if strncmp hugetlbfs */
162 } /* end while fgets */
169 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
171 char buf[sizeof(*a)];
172 memcpy(buf, a, sizeof(*a));
173 memcpy(a, b, sizeof(*a));
174 memcpy(b, buf, sizeof(*a));
178 eal_hugepage_info_init(void)
180 const char dirent_start_text[] = "hugepages-";
181 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
182 unsigned i, num_sizes = 0;
184 DIR *dir = opendir(sys_dir_path);
186 rte_panic("Cannot open directory %s to read system hugepage info\n",
189 struct dirent *dirent = readdir(dir);
190 while(dirent != NULL){
191 if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) == 0){
192 struct hugepage_info *hpi = \
193 &internal_config.hugepage_info[num_sizes];
194 hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]);
195 hpi->num_pages = get_num_hugepages(dirent->d_name);
196 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
197 if (hpi->hugedir == NULL){
198 RTE_LOG(INFO, EAL, "%u hugepages of size %llu reserved, "\
199 "but no mounted hugetlbfs found for that size\n",
201 (unsigned long long)hpi->hugepage_sz);
206 dirent = readdir(dir);
209 internal_config.num_hugepage_sizes = num_sizes;
211 /* sort the page directory entries by size, largest to smallest */
212 for (i = 0; i < num_sizes; i++){
214 for (j = i+1; j < num_sizes; j++)
215 if (internal_config.hugepage_info[j-1].hugepage_sz < \
216 internal_config.hugepage_info[j].hugepage_sz)
217 swap_hpi(&internal_config.hugepage_info[j-1],
218 &internal_config.hugepage_info[j]);
221 /* now we have all info, check we have at least one valid size */
222 for (i = 0; i < num_sizes; i++)
223 if (internal_config.hugepage_info[i].hugedir != NULL &&
224 internal_config.hugepage_info[i].num_pages > 0)
226 /* no valid hugepage mounts available, return error */