remove version in all files
[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  */
34
35 #include <string.h>
36 #include <sys/types.h>
37 #include <dirent.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <inttypes.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <sys/queue.h>
45
46 #include "rte_memory.h"
47 #include "rte_memzone.h"
48 #include "rte_tailq.h"
49 #include "rte_eal.h"
50 #include "rte_launch.h"
51 #include "rte_per_lcore.h"
52 #include "rte_lcore.h"
53 #include "rte_debug.h"
54 #include "rte_log.h"
55 #include "rte_common.h"
56 #include "rte_string_fns.h"
57 #include "eal_internal_cfg.h"
58 #include "eal_hugepages.h"
59
60 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
61
62 static int32_t
63 get_num_hugepages(const char *subdir)
64 {
65         const char nr_hp_file[] = "nr_hugepages";
66         char path[BUFSIZ];
67         unsigned num_pages = 0;
68
69         rte_snprintf(path, sizeof(path), "%s/%s/%s",
70                         sys_dir_path, subdir, nr_hp_file);
71         FILE *fd = fopen(path, "r");
72         if (fd == NULL || fscanf(fd, "%u", &num_pages) != 1)
73                 rte_panic("Error reading file '%s'\n", path);
74         fclose(fd);
75
76         return num_pages;
77 }
78
79 static uint64_t
80 get_default_hp_size(void)
81 {
82         const char proc_meminfo[] = "/proc/meminfo";
83         const char str_hugepagesz[] = "Hugepagesize:";
84         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
85         char buffer[256];
86         unsigned long long size = 0;
87
88         FILE *fd = fopen(proc_meminfo, "r");
89         if (fd == NULL)
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]);
94                         break;
95                 }
96         }
97         fclose(fd);
98         if (size == 0)
99                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
100         return size;
101 }
102
103 static const char *
104 get_hugepage_dir(uint64_t hugepage_sz)
105 {
106         enum proc_mount_fieldnames {
107                 DEVICE = 0,
108                 MOUNTPT,
109                 FSTYPE,
110                 OPTIONS,
111                 _FIELDNAME_MAX
112         };
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];
121         char buf[BUFSIZ];
122         char *retval = NULL;
123
124         FILE *fd = fopen(proc_mounts, "r");
125         if (fd == NULL)
126                 rte_panic("Cannot open %s\n", proc_mounts);
127
128         if (default_size == 0)
129                 default_size = get_default_hp_size();
130
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 */
136                 }
137
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)
141                         continue;
142
143                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
144                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
145
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]);
150                                         break;
151                                 }
152                         }
153                         /* there is an explicit page size, so check it */
154                         else {
155                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
156                                 if (pagesz == hugepage_sz) {
157                                         retval = strdup(splitstr[MOUNTPT]);
158                                         break;
159                                 }
160                         }
161                 } /* end if strncmp hugetlbfs */
162         } /* end while fgets */
163
164         fclose(fd);
165         return retval;
166 }
167
168 static inline void
169 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
170 {
171         char buf[sizeof(*a)];
172         memcpy(buf, a, sizeof(*a));
173         memcpy(a, b, sizeof(*a));
174         memcpy(b, buf, sizeof(*a));
175 }
176
177 int
178 eal_hugepage_info_init(void)
179 {
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;
183
184         DIR *dir = opendir(sys_dir_path);
185         if (dir == NULL)
186                 rte_panic("Cannot open directory %s to read system hugepage info\n",
187                                 sys_dir_path);
188
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",
200                                                 hpi->num_pages,
201                                                 (unsigned long long)hpi->hugepage_sz);
202                                 hpi->num_pages = 0;
203                         } else
204                                 num_sizes++;
205                 }
206                 dirent = readdir(dir);
207         }
208         closedir(dir);
209         internal_config.num_hugepage_sizes = num_sizes;
210
211         /* sort the page directory entries by size, largest to smallest */
212         for (i = 0; i < num_sizes; i++){
213                 unsigned j;
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]);
219         }
220
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)
225                         return 0;
226         /* no valid hugepage mounts available, return error */
227         return -1;
228 }