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