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