first public release
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_hugepage_info.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <string.h>
37 #include <sys/types.h>
38 #include <dirent.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <inttypes.h>
43 #include <stdarg.h>
44 #include <errno.h>
45 #include <sys/queue.h>
46
47 #include "rte_memory.h"
48 #include "rte_memzone.h"
49 #include "rte_tailq.h"
50 #include "rte_eal.h"
51 #include "rte_launch.h"
52 #include "rte_per_lcore.h"
53 #include "rte_lcore.h"
54 #include "rte_debug.h"
55 #include "rte_log.h"
56 #include "rte_common.h"
57 #include "rte_string_fns.h"
58 #include "eal_internal_cfg.h"
59 #include "eal_hugepages.h"
60
61 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
62
63 static int32_t
64 get_num_hugepages(const char *subdir)
65 {
66         const char nr_hp_file[] = "nr_hugepages";
67         char path[BUFSIZ];
68         unsigned num_pages = 0;
69
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);
75         fclose(fd);
76
77         return num_pages;
78 }
79
80 static uint64_t
81 get_default_hp_size(void)
82 {
83         const char proc_meminfo[] = "/proc/meminfo";
84         const char str_hugepagesz[] = "Hugepagesize:";
85         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
86         char buffer[256];
87         unsigned long long size = 0;
88
89         FILE *fd = fopen(proc_meminfo, "r");
90         if (fd == NULL)
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]);
95                         break;
96                 }
97         }
98         fclose(fd);
99         if (size == 0)
100                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
101         return size;
102 }
103
104 static const char *
105 get_hugepage_dir(uint64_t hugepage_sz)
106 {
107         enum proc_mount_fieldnames {
108                 DEVICE = 0,
109                 MOUNTPT,
110                 FSTYPE,
111                 OPTIONS,
112                 _FIELDNAME_MAX
113         };
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];
122         char buf[BUFSIZ];
123         char *retval = NULL;
124
125         FILE *fd = fopen(proc_mounts, "r");
126         if (fd == NULL)
127                 rte_panic("Cannot open %s\n", proc_mounts);
128
129         if (default_size == 0)
130                 default_size = get_default_hp_size();
131
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 */
137                 }
138
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)
142                         continue;
143
144                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
145                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
146
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]);
151                                         break;
152                                 }
153                         }
154                         /* there is an explicit page size, so check it */
155                         else {
156                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
157                                 if (pagesz == hugepage_sz) {
158                                         retval = strdup(splitstr[MOUNTPT]);
159                                         break;
160                                 }
161                         }
162                 } /* end if strncmp hugetlbfs */
163         } /* end while fgets */
164
165         fclose(fd);
166         return retval;
167 }
168
169 static inline void
170 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
171 {
172         char buf[sizeof(*a)];
173         memcpy(buf, a, sizeof(*a));
174         memcpy(a, b, sizeof(*a));
175         memcpy(b, buf, sizeof(*a));
176 }
177
178 int
179 eal_hugepage_info_init(void)
180 {
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;
184
185         DIR *dir = opendir(sys_dir_path);
186         if (dir == NULL)
187                 rte_panic("Cannot open directory %s to read system hugepage info\n",
188                                 sys_dir_path);
189
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",
201                                                 hpi->num_pages,
202                                                 (unsigned long long)hpi->hugepage_sz);
203                                 hpi->num_pages = 0;
204                         } else
205                                 num_sizes++;
206                 }
207                 dirent = readdir(dir);
208         }
209         closedir(dir);
210         internal_config.num_hugepage_sizes = num_sizes;
211
212         /* sort the page directory entries by size, largest to smallest */
213         for (i = 0; i < num_sizes; i++){
214                 unsigned j;
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]);
220         }
221
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)
226                         return 0;
227         /* no valid hugepage mounts available, return error */
228         return -1;
229 }