- /* check if available enough 1GB pages for each socket */
- /* *INDENT-OFF* */
- clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
- {
- int pages_avail, page_size, mem;
-
- vec_validate(mem_by_socket, c);
- mem = mem_by_socket[c];
-
- page_size = 1024;
- pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
-
- if (pages_avail < 0 || page_size * pages_avail < mem)
- use_1g = 0;
-
- page_size = 2;
- pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
-
- if (pages_avail < 0 || page_size * pages_avail < mem)
- use_2m = 0;
- }));
- /* *INDENT-ON* */
-
- if (mem_by_socket == 0)
- {
- error = clib_error_return (0, "mem_by_socket NULL");
- goto done;
- }
- _vec_len (mem_by_socket) = c + 1;
-
- /* regenerate socket_mem string */
- vec_foreach_index (x, mem_by_socket)
- socket_mem = format (socket_mem, "%s%u",
- socket_mem ? "," : "", mem_by_socket[x]);
- socket_mem = format (socket_mem, "%c", 0);
-
- vec_free (mem_by_socket);
-
- rv = mkdir (VPP_RUN_DIR, 0755);
- if (rv && errno != EEXIST)
- {
- error = clib_error_return (0, "mkdir '%s' failed errno %d",
- VPP_RUN_DIR, errno);
- goto done;
- }
-
- rv = mkdir (DEFAULT_HUGE_DIR, 0755);
- if (rv && errno != EEXIST)
- {
- error = clib_error_return (0, "mkdir '%s' failed errno %d",
- DEFAULT_HUGE_DIR, errno);
- goto done;
- }
-
- if (use_1g && !(less_than_1g && use_2m))
- {
- rv =
- mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
- }
- else if (use_2m)
- {
- rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
- }
- else
- {
- return clib_error_return (0, "not enough free huge pages");
- }
-
- if (rv)
- {
- error = clib_error_return (0, "mount failed %d", errno);
- goto done;
- }
+ /* *INDENT-OFF* */
+ clib_bitmap_foreach (x, tm->cpu_socket_bitmap, (
+ {
+ clib_error_t *e;
+ uword n_pages;
+ /* preallocate at least 16MB of hugepages per socket,
+ if more is needed it is up to consumer to preallocate more */
+ n_pages = round_pow2 ((uword) 16 << 20, default_hugepage_sz);
+ n_pages /= default_hugepage_sz;
+
+ if ((e = clib_sysfs_prealloc_hugepages(x, 0, n_pages)))
+ clib_error_report (e);
+ }));
+ /* *INDENT-ON* */