eal/linux: remove useless process type check in hugepage init
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_hugepage_info.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <dirent.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <fnmatch.h>
42 #include <inttypes.h>
43 #include <stdarg.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <sys/queue.h>
47
48 #include <rte_memory.h>
49 #include <rte_memzone.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 #include "eal_filesystem.h"
61
62 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
63
64 /* this function is only called from eal_hugepage_info_init which itself
65  * is only called from a primary process */
66 static int32_t
67 get_num_hugepages(const char *subdir)
68 {
69         char path[PATH_MAX];
70         long unsigned resv_pages, num_pages = 0;
71         const char *nr_hp_file = "free_hugepages";
72         const char *nr_rsvd_file = "resv_hugepages";
73
74         /* first, check how many reserved pages kernel reports */
75         snprintf(path, sizeof(path), "%s/%s/%s",
76                         sys_dir_path, subdir, nr_rsvd_file);
77         if (eal_parse_sysfs_value(path, &resv_pages) < 0)
78                 return 0;
79
80         snprintf(path, sizeof(path), "%s/%s/%s",
81                         sys_dir_path, subdir, nr_hp_file);
82         if (eal_parse_sysfs_value(path, &num_pages) < 0)
83                 return 0;
84
85         if (num_pages == 0)
86                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
87                                 subdir);
88
89         /* adjust num_pages */
90         if (num_pages > 0)
91                 num_pages -= resv_pages;
92
93         return (int32_t)num_pages;
94 }
95
96 static uint64_t
97 get_default_hp_size(void)
98 {
99         const char proc_meminfo[] = "/proc/meminfo";
100         const char str_hugepagesz[] = "Hugepagesize:";
101         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
102         char buffer[256];
103         unsigned long long size = 0;
104
105         FILE *fd = fopen(proc_meminfo, "r");
106         if (fd == NULL)
107                 rte_panic("Cannot open %s\n", proc_meminfo);
108         while(fgets(buffer, sizeof(buffer), fd)){
109                 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
110                         size = rte_str_to_size(&buffer[hugepagesz_len]);
111                         break;
112                 }
113         }
114         fclose(fd);
115         if (size == 0)
116                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
117         return size;
118 }
119
120 static const char *
121 get_hugepage_dir(uint64_t hugepage_sz)
122 {
123         enum proc_mount_fieldnames {
124                 DEVICE = 0,
125                 MOUNTPT,
126                 FSTYPE,
127                 OPTIONS,
128                 _FIELDNAME_MAX
129         };
130         static uint64_t default_size = 0;
131         const char proc_mounts[] = "/proc/mounts";
132         const char hugetlbfs_str[] = "hugetlbfs";
133         const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
134         const char pagesize_opt[] = "pagesize=";
135         const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
136         const char split_tok = ' ';
137         char *splitstr[_FIELDNAME_MAX];
138         char buf[BUFSIZ];
139         char *retval = NULL;
140
141         FILE *fd = fopen(proc_mounts, "r");
142         if (fd == NULL)
143                 rte_panic("Cannot open %s\n", proc_mounts);
144
145         if (default_size == 0)
146                 default_size = get_default_hp_size();
147
148         while (fgets(buf, sizeof(buf), fd)){
149                 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
150                                 split_tok) != _FIELDNAME_MAX) {
151                         RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
152                         break; /* return NULL */
153                 }
154
155                 /* we have a specified --huge-dir option, only examine that dir */
156                 if (internal_config.hugepage_dir != NULL &&
157                                 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
158                         continue;
159
160                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
161                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
162
163                         /* if no explicit page size, the default page size is compared */
164                         if (pagesz_str == NULL){
165                                 if (hugepage_sz == default_size){
166                                         retval = strdup(splitstr[MOUNTPT]);
167                                         break;
168                                 }
169                         }
170                         /* there is an explicit page size, so check it */
171                         else {
172                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
173                                 if (pagesz == hugepage_sz) {
174                                         retval = strdup(splitstr[MOUNTPT]);
175                                         break;
176                                 }
177                         }
178                 } /* end if strncmp hugetlbfs */
179         } /* end while fgets */
180
181         fclose(fd);
182         return retval;
183 }
184
185 static inline void
186 swap_hpi(struct hugepage_info *a, struct hugepage_info *b)
187 {
188         char buf[sizeof(*a)];
189         memcpy(buf, a, sizeof(buf));
190         memcpy(a, b, sizeof(buf));
191         memcpy(b, buf, sizeof(buf));
192 }
193
194 /*
195  * Clear the hugepage directory of whatever hugepage files
196  * there are. Checks if the file is locked (i.e.
197  * if it's in use by another DPDK process).
198  */
199 static int
200 clear_hugedir(const char * hugedir)
201 {
202         DIR *dir;
203         struct dirent *dirent;
204         int dir_fd, fd, lck_result;
205         const char filter[] = "*map_*"; /* matches hugepage files */
206
207         /* open directory */
208         dir = opendir(hugedir);
209         if (!dir) {
210                 RTE_LOG(INFO, EAL, "Unable to open hugepage directory %s\n",
211                                 hugedir);
212                 goto error;
213         }
214         dir_fd = dirfd(dir);
215
216         dirent = readdir(dir);
217         if (!dirent) {
218                 RTE_LOG(INFO, EAL, "Unable to read hugepage directory %s\n",
219                                 hugedir);
220                 goto error;
221         }
222
223         while(dirent != NULL){
224                 /* skip files that don't match the hugepage pattern */
225                 if (fnmatch(filter, dirent->d_name, 0) > 0) {
226                         dirent = readdir(dir);
227                         continue;
228                 }
229
230                 /* try and lock the file */
231                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
232
233                 /* skip to next file */
234                 if (fd == -1) {
235                         dirent = readdir(dir);
236                         continue;
237                 }
238
239                 /* non-blocking lock */
240                 lck_result = flock(fd, LOCK_EX | LOCK_NB);
241
242                 /* if lock succeeds, unlock and remove the file */
243                 if (lck_result != -1) {
244                         flock(fd, LOCK_UN);
245                         unlinkat(dir_fd, dirent->d_name, 0);
246                 }
247                 close (fd);
248                 dirent = readdir(dir);
249         }
250
251         closedir(dir);
252         return 0;
253
254 error:
255         if (dir)
256                 closedir(dir);
257
258         RTE_LOG(INFO, EAL, "Error while clearing hugepage dir: %s\n",
259                 strerror(errno));
260
261         return -1;
262 }
263
264 /*
265  * when we initialize the hugepage info, everything goes
266  * to socket 0 by default. it will later get sorted by memory
267  * initialization procedure.
268  */
269 int
270 eal_hugepage_info_init(void)
271 {
272         const char dirent_start_text[] = "hugepages-";
273         const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
274         unsigned i, num_sizes = 0;
275
276         DIR *dir = opendir(sys_dir_path);
277         if (dir == NULL)
278                 rte_panic("Cannot open directory %s to read system hugepage info\n",
279                                 sys_dir_path);
280
281         struct dirent *dirent = readdir(dir);
282         while(dirent != NULL){
283                 if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) == 0){
284                         struct hugepage_info *hpi = \
285                                         &internal_config.hugepage_info[num_sizes];
286                         hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]);
287                         hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
288
289                         /* first, check if we have a mountpoint */
290                         if (hpi->hugedir == NULL){
291                                 int32_t num_pages;
292                                 if ((num_pages = get_num_hugepages(dirent->d_name)) > 0)
293                                         RTE_LOG(INFO, EAL, "%u hugepages of size %llu reserved, "\
294                                                         "but no mounted hugetlbfs found for that size\n",
295                                                         (unsigned)num_pages,
296                                                         (unsigned long long)hpi->hugepage_sz);
297                         } else {
298                                 /* try to obtain a writelock */
299                                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
300
301                                 /* if blocking lock failed */
302                                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
303                                         RTE_LOG(CRIT, EAL, "Failed to lock hugepage directory!\n");
304                                         closedir(dir);
305                                         return -1;
306                                 }
307                                 /* clear out the hugepages dir from unused pages */
308                                 if (clear_hugedir(hpi->hugedir) == -1) {
309                                         closedir(dir);
310                                         return -1;
311                                 }
312
313                                 /* for now, put all pages into socket 0,
314                                  * later they will be sorted */
315                                 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
316
317 #ifndef RTE_ARCH_64
318                                 /* for 32-bit systems, limit number of hugepages to 1GB per page size */
319                                 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
320                                                 RTE_PGSIZE_1G / hpi->hugepage_sz);
321 #endif
322
323                                 num_sizes++;
324                         }
325                 }
326                 dirent = readdir(dir);
327         }
328         closedir(dir);
329         internal_config.num_hugepage_sizes = num_sizes;
330
331         /* sort the page directory entries by size, largest to smallest */
332         for (i = 0; i < num_sizes; i++){
333                 unsigned j;
334                 for (j = i+1; j < num_sizes; j++)
335                         if (internal_config.hugepage_info[j-1].hugepage_sz < \
336                                         internal_config.hugepage_info[j].hugepage_sz)
337                                 swap_hpi(&internal_config.hugepage_info[j-1],
338                                                 &internal_config.hugepage_info[j]);
339         }
340
341         /* now we have all info, check we have at least one valid size */
342         for (i = 0; i < num_sizes; i++)
343                 if (internal_config.hugepage_info[i].hugedir != NULL &&
344                                 internal_config.hugepage_info[i].num_pages[0] > 0)
345                         return 0;
346
347         /* no valid hugepage mounts available, return error */
348         return -1;
349 }