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.
33 * version: DPDK.L.1.2.3-3
37 #include <sys/types.h>
45 #include <sys/queue.h>
47 #include "rte_memory.h"
48 #include "rte_memzone.h"
49 #include "rte_tailq.h"
51 #include "rte_launch.h"
52 #include "rte_per_lcore.h"
53 #include "rte_lcore.h"
54 #include "rte_debug.h"
56 #include "rte_common.h"
57 #include "rte_string_fns.h"
58 #include "eal_internal_cfg.h"
59 #include "eal_hugepages.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);
72 FILE *fd = fopen(path, "r");
73 if (fd == NULL || fscanf(fd, "%u", &num_pages) != 1)
74 rte_panic("Error reading file '%s'\n", path);
81 get_default_hp_size(void)
83 const char proc_meminfo[] = "/proc/meminfo";
84 const char str_hugepagesz[] = "Hugepagesize:";
85 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
87 unsigned long long size = 0;
89 FILE *fd = fopen(proc_meminfo, "r");
91 rte_panic("Cannot open %s\n", proc_meminfo);
92 while(fgets(buffer, sizeof(buffer), fd)){
93 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
94 size = rte_str_to_size(&buffer[hugepagesz_len]);
100 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
105 get_hugepage_dir(uint64_t hugepage_sz)
107 enum proc_mount_fieldnames {
114 static uint64_t default_size = 0;
115 const char proc_mounts[] = "/proc/mounts";
116 const char hugetlbfs_str[] = "hugetlbfs";
117 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
118 const char pagesize_opt[] = "pagesize=";
119 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
120 const char split_tok = ' ';
121 char *splitstr[_FIELDNAME_MAX];
125 FILE *fd = fopen(proc_mounts, "r");
127 rte_panic("Cannot open %s\n", proc_mounts);
129 if (default_size == 0)
130 default_size = get_default_hp_size();
132 while (fgets(buf, sizeof(buf), fd)){
133 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
134 split_tok) != _FIELDNAME_MAX) {
135 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
136 break; /* return NULL */
139 /* we have a specified --huge-dir option, only examine that dir */
140 if (internal_config.hugepage_dir != NULL &&
141 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
144 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
145 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
147 /* if no explicit page size, the default page size is compared */
148 if (pagesz_str == NULL){
149 if (hugepage_sz == default_size){
150 retval = strdup(splitstr[MOUNTPT]);
154 /* there is an explicit page size, so check it */
156 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
157 if (pagesz == hugepage_sz) {
158 retval = strdup(splitstr[MOUNTPT]);
162 } /* end if strncmp hugetlbfs */
163 } /* end while fgets */
170 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
172 char buf[sizeof(*a)];
173 memcpy(buf, a, sizeof(*a));
174 memcpy(a, b, sizeof(*a));
175 memcpy(b, buf, sizeof(*a));
179 eal_hugepage_info_init(void)
181 const char dirent_start_text[] = "hugepages-";
182 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
183 unsigned i, num_sizes = 0;
185 DIR *dir = opendir(sys_dir_path);
187 rte_panic("Cannot open directory %s to read system hugepage info\n",
190 struct dirent *dirent = readdir(dir);
191 while(dirent != NULL){
192 if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) == 0){
193 struct hugepage_info *hpi = \
194 &internal_config.hugepage_info[num_sizes];
195 hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]);
196 hpi->num_pages = get_num_hugepages(dirent->d_name);
197 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
198 if (hpi->hugedir == NULL){
199 RTE_LOG(INFO, EAL, "%u hugepages of size %llu reserved, "\
200 "but no mounted hugetlbfs found for that size\n",
202 (unsigned long long)hpi->hugepage_sz);
207 dirent = readdir(dir);
210 internal_config.num_hugepage_sizes = num_sizes;
212 /* sort the page directory entries by size, largest to smallest */
213 for (i = 0; i < num_sizes; i++){
215 for (j = i+1; j < num_sizes; j++)
216 if (internal_config.hugepage_info[j-1].hugepage_sz < \
217 internal_config.hugepage_info[j].hugepage_sz)
218 swap_hpi(&internal_config.hugepage_info[j-1],
219 &internal_config.hugepage_info[j]);
222 /* now we have all info, check we have at least one valid size */
223 for (i = 0; i < num_sizes; i++)
224 if (internal_config.hugepage_info[i].hugedir != NULL &&
225 internal_config.hugepage_info[i].num_pages > 0)
227 /* no valid hugepage mounts available, return error */