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