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