From 2a7c0b6fc027e86727c08d98b66e19ad8768c82b Mon Sep 17 00:00:00 2001 From: Florin Coras Date: Mon, 28 Sep 2020 23:40:28 -0700 Subject: svm session: use new clib mem apis for segments "sh memory map" now reports shared memory segments mapped by session layer for applications. Type: improvement Signed-off-by: Florin Coras Change-Id: I7debdcd06c7728abfb5cf817a1ccc37de495472c --- src/svm/fifo_segment.c | 6 +- src/svm/ssvm.c | 107 +++++++++++++++++++++------------- src/svm/ssvm.h | 2 +- src/vnet/session/application.c | 7 +-- src/vnet/session/application_worker.c | 8 +-- src/vnet/session/segment_manager.c | 14 ++++- src/vnet/session/session.c | 8 ++- 7 files changed, 91 insertions(+), 61 deletions(-) diff --git a/src/svm/fifo_segment.c b/src/svm/fifo_segment.c index 5fb2758606e..a247929a058 100644 --- a/src/svm/fifo_segment.c +++ b/src/svm/fifo_segment.c @@ -1362,7 +1362,7 @@ format_fifo_segment_type (u8 * s, va_list * args) ssvm_segment_type_t st = ssvm_type (&sp->ssvm); if (st == SSVM_SEGMENT_PRIVATE) - s = format (s, "%s", "private-heap"); + s = format (s, "%s", "private"); else if (st == SSVM_SEGMENT_MEMFD) s = format (s, "%s", "memfd"); else if (st == SSVM_SEGMENT_SHM) @@ -1400,7 +1400,7 @@ format_fifo_segment (u8 * s, va_list * args) if (fs == 0) { - s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type", + s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type", "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address"); return s; } @@ -1409,7 +1409,7 @@ format_fifo_segment (u8 * s, va_list * args) active_fifos = fifo_segment_num_fifos (fs); free_fifos = fifo_segment_num_free_fifos (fs); - s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm), + s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm), format_fifo_segment_type, fs, size >> 20ULL, active_fifos, free_fifos, address); diff --git a/src/svm/ssvm.c b/src/svm/ssvm.c index d8831ec1c48..191dbe571c7 100644 --- a/src/svm/ssvm.c +++ b/src/svm/ssvm.c @@ -29,7 +29,6 @@ int ssvm_master_init_shm (ssvm_private_t * ssvm) { int ssvm_fd; - clib_mem_vm_map_t mapa = { 0 }; u8 junk = 0, *ssvm_filename; ssvm_shared_header_t *sh; uword page_size, requested_va = 0; @@ -84,19 +83,18 @@ ssvm_master_init_shm (ssvm_private_t * ssvm) clib_mem_vm_randomize_va (&requested_va, min_log2 (page_size)); } - mapa.requested_va = requested_va; - mapa.size = ssvm->ssvm_size; - mapa.fd = ssvm_fd; - mapa.numa_node = ssvm->numa; - if (clib_mem_vm_ext_map (&mapa)) + sh = clib_mem_vm_map_shared (uword_to_pointer (requested_va, void *), + ssvm->ssvm_size, ssvm_fd, 0, + (char *) ssvm->name); + if (sh == CLIB_MEM_VM_MAP_FAILED) { clib_unix_warning ("mmap"); close (ssvm_fd); return SSVM_API_ERROR_MMAP; } + close (ssvm_fd); - sh = mapa.addr; CLIB_MEM_UNPOISON (sh, sizeof (*sh)); sh->master_pid = ssvm->my_pid; sh->ssvm_size = ssvm->ssvm_size; @@ -206,7 +204,10 @@ ssvm_delete_shm (ssvm_private_t * ssvm) vec_free (fn); vec_free (ssvm->name); - munmap ((void *) ssvm->sh, ssvm->ssvm_size); + if (ssvm->i_am_master) + clib_mem_vm_unmap (ssvm->sh); + else + munmap ((void *) ssvm->sh, ssvm->ssvm_size); } /** @@ -215,44 +216,60 @@ ssvm_delete_shm (ssvm_private_t * ssvm) int ssvm_master_init_memfd (ssvm_private_t * memfd) { + int log2_page_size, n_pages; uword page_size; ssvm_shared_header_t *sh; void *oldheap; - clib_mem_vm_alloc_t alloc = { 0 }; - clib_error_t *err; if (memfd->ssvm_size == 0) return SSVM_API_ERROR_NO_SIZE; ASSERT (vec_c_string_is_terminated (memfd->name)); - alloc.name = (char *) memfd->name; - alloc.size = memfd->ssvm_size; - alloc.flags = CLIB_MEM_VM_F_SHARED; - alloc.requested_va = memfd->requested_va; - if (memfd->numa) + memfd->fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, + (char *) memfd->name); + + if (memfd->fd == CLIB_MEM_ERROR) + { + clib_unix_warning (0, "failed to create memfd"); + return SSVM_API_ERROR_CREATE_FAILURE; + } + + log2_page_size = clib_mem_get_fd_log2_page_size (memfd->fd); + if (log2_page_size == 0) + { + clib_unix_warning (0, "cannot determine page size"); + return SSVM_API_ERROR_CREATE_FAILURE; + } + + n_pages = ((memfd->ssvm_size - 1) >> log2_page_size) + 1; + + if ((ftruncate (memfd->fd, n_pages << log2_page_size)) == -1) { - alloc.numa_node = memfd->numa; - alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER; + clib_unix_warning (0, "memfd ftruncate failure"); + return SSVM_API_ERROR_CREATE_FAILURE; } - if ((err = clib_mem_vm_ext_alloc (&alloc))) + + sh = clib_mem_vm_map_shared (uword_to_pointer (memfd->requested_va, void *), + memfd->ssvm_size, memfd->fd, 0, + (char *) memfd->name); + if (sh == CLIB_MEM_VM_MAP_FAILED) { - clib_error_report (err); + clib_unix_warning ("memfd map (fd %d)", memfd->fd); + close (memfd->fd); return SSVM_API_ERROR_CREATE_FAILURE; } - memfd->fd = alloc.fd; - memfd->sh = (ssvm_shared_header_t *) alloc.addr; + memfd->sh = sh; memfd->my_pid = getpid (); memfd->i_am_master = 1; - page_size = 1ull << alloc.log2_page_size; - sh = memfd->sh; sh->master_pid = memfd->my_pid; sh->ssvm_size = memfd->ssvm_size; sh->ssvm_va = pointer_to_uword (sh); sh->type = SSVM_SEGMENT_MEMFD; + page_size = 1 << log2_page_size; sh->heap = clib_mem_create_heap (((u8 *) sh) + page_size, memfd->ssvm_size - page_size, 1 /* locked */ , "ssvm master memfd"); @@ -327,7 +344,10 @@ void ssvm_delete_memfd (ssvm_private_t * memfd) { vec_free (memfd->name); - clib_mem_vm_free (memfd->sh, memfd->ssvm_size); + if (memfd->i_am_master) + clib_mem_vm_unmap (memfd->sh); + else + clib_mem_vm_free (memfd->sh, memfd->ssvm_size); close (memfd->fd); } @@ -337,34 +357,35 @@ ssvm_delete_memfd (ssvm_private_t * memfd) int ssvm_master_init_private (ssvm_private_t * ssvm) { - uword pagesize = clib_mem_get_page_size (), rnd_size = 0; - clib_mem_vm_alloc_t alloc = { 0 }; + uword page_size, log2_page_size, rnd_size = 0; ssvm_shared_header_t *sh; - clib_error_t *err; + void *oldheap; u8 *heap; - rnd_size = clib_max (ssvm->ssvm_size + (pagesize - 1), ssvm->ssvm_size); - rnd_size &= ~(pagesize - 1); - - alloc.name = (char *) ssvm->name; - alloc.size = rnd_size + pagesize; - if (ssvm->numa) + log2_page_size = clib_mem_get_log2_page_size (); + if (log2_page_size == 0) { - alloc.numa_node = ssvm->numa; - alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER; + clib_unix_warning (0, "cannot determine page size"); + return SSVM_API_ERROR_CREATE_FAILURE; } - if ((err = clib_mem_vm_ext_alloc (&alloc))) + page_size = 1 << log2_page_size; + rnd_size = clib_max (ssvm->ssvm_size + (page_size - 1), ssvm->ssvm_size); + rnd_size &= ~(page_size - 1); + + sh = clib_mem_vm_map (0, rnd_size + page_size, log2_page_size, + (char *) ssvm->name); + if (sh == CLIB_MEM_VM_MAP_FAILED) { - clib_error_report (err); + clib_unix_warning ("private map failed"); return SSVM_API_ERROR_CREATE_FAILURE; } - heap = clib_mem_create_heap ((u8 *) alloc.addr + pagesize, rnd_size, + heap = clib_mem_create_heap ((u8 *) sh + page_size, rnd_size, 1 /* locked */ , "ssvm master private"); if (heap == 0) { - clib_unix_warning ("mheap alloc"); + clib_unix_warning ("heap alloc"); return -1; } @@ -376,7 +397,6 @@ ssvm_master_init_private (ssvm_private_t * ssvm) ssvm->requested_va = ~0; /* First page in allocated memory is set aside for the shared header */ - sh = alloc.addr; ssvm->sh = sh; clib_memset (sh, 0, sizeof (*sh)); @@ -384,7 +404,10 @@ ssvm_master_init_private (ssvm_private_t * ssvm) sh->ssvm_size = rnd_size; sh->ssvm_va = pointer_to_uword (heap); sh->type = SSVM_SEGMENT_PRIVATE; - sh->name = ssvm->name; + + oldheap = ssvm_push_heap (sh); + sh->name = format (0, "%s", ssvm->name, 0); + ssvm_pop_heap (oldheap); return 0; } @@ -401,7 +424,7 @@ ssvm_delete_private (ssvm_private_t * ssvm) { vec_free (ssvm->name); clib_mem_destroy_heap (ssvm->sh->heap); - clib_mem_vm_free (ssvm->sh, ssvm->ssvm_size + clib_mem_get_page_size ()); + clib_mem_vm_unmap (ssvm->sh); } int diff --git a/src/svm/ssvm.h b/src/svm/ssvm.h index 2a9da3d2f32..3a277cb5c5f 100644 --- a/src/svm/ssvm.h +++ b/src/svm/ssvm.h @@ -85,7 +85,7 @@ typedef struct uword requested_va; u32 my_pid; u8 *name; - u8 numa; /**< Numa requested at alloc time */ + u8 numa; /**< UNUSED: numa requested at alloc time */ int i_am_master; union diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c index a0ddaed90cf..0b5c951fd70 100644 --- a/src/vnet/session/application.c +++ b/src/vnet/session/application.c @@ -876,11 +876,8 @@ vnet_application_attach (vnet_app_attach_args_t * a) if (application_is_proxy (app)) { application_setup_proxy (app); - /* - * I suspect the segment manager pool gets reallocated (because a new listener - * is added, and each listener has a segment manager) so sm becomes a dangling - * reference. That's why we need to re-grab sm. - */ + /* The segment manager pool is reallocated because a new listener + * is added. Re-grab segment manager to avoid dangling reference */ sm = segment_manager_get (app_wrk->first_segment_manager); } diff --git a/src/vnet/session/application_worker.c b/src/vnet/session/application_worker.c index 17aeb57d49b..2b2cc344f05 100644 --- a/src/vnet/session/application_worker.c +++ b/src/vnet/session/application_worker.c @@ -180,12 +180,12 @@ app_worker_alloc_segment_manager (app_worker_t * app_wrk) { sm = segment_manager_get (app_wrk->first_segment_manager); app_wrk->first_segment_manager_in_use = 1; - return sm; } - - sm = segment_manager_alloc (); + else + { + sm = segment_manager_alloc (); + } sm->app_wrk_index = app_wrk->wrk_index; - return sm; } diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c index dbeff6e4acf..44ff5fefd70 100644 --- a/src/vnet/session/segment_manager.c +++ b/src/vnet/session/segment_manager.c @@ -138,11 +138,16 @@ segment_manager_add_segment (segment_manager_t * sm, uword segment_size) } } else - seg_name = format (0, "%s%c", "process-private", 0); + { + app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index); + application_t *app = application_get (app_wrk->app_index); + seg_name = format (0, "%v segment%c", app->name, 0); + } fs->ssvm.ssvm_size = segment_size; fs->ssvm.name = seg_name; - fs->ssvm.requested_va = baseva; + /* clib_mem_vm_map_shared consumes first page before requested_va */ + fs->ssvm.requested_va = baseva + page_size; if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type))) { @@ -192,7 +197,9 @@ segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs) if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE) { - clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va); + /* clib_mem_vm_map_shared consumes first page before requested_va */ + clib_valloc_free (&smm->va_allocator, + fs->ssvm.requested_va - clib_mem_get_page_size ()); if (!segment_manager_app_detached (sm)) { @@ -947,6 +954,7 @@ segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, })); /* *INDENT-ON* */ + vlib_cli_output (vm, "\n"); } if (show_segments) { diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c index d27d1bbab23..5065f7834b1 100644 --- a/src/vnet/session/session.c +++ b/src/vnet/session/session.c @@ -1496,8 +1496,9 @@ session_vpp_event_queues_allocate (session_main_t * smm) eqs->ssvm_size = eqs_size; eqs->i_am_master = 1; eqs->my_pid = vpp_pid; - eqs->name = format (0, "%s%c", "evt-qs-segment", 0); - eqs->requested_va = smm->session_baseva; + eqs->name = format (0, "%s%c", "session: evt-qs-segment", 0); + /* clib_mem_vm_map_shared consumes first page before requested_va */ + eqs->requested_va = smm->session_baseva + clib_mem_get_page_size (); if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD)) { @@ -1661,6 +1662,7 @@ session_manager_main_enable (vlib_main_t * vm) session_main_t *smm = &session_main; vlib_thread_main_t *vtm = vlib_get_thread_main (); u32 num_threads, preallocated_sessions_per_worker; + uword margin = 8 << 12; session_worker_t *wrk; int i; @@ -1691,7 +1693,7 @@ session_manager_main_enable (vlib_main_t * vm) session_vpp_event_queues_allocate (smm); /* Initialize fifo segment main baseva and timeout */ - sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size; + sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size + margin; sm_args->size = smm->session_va_space_size; segment_manager_main_init (sm_args); -- cgit 1.2.3-korg