summaryrefslogtreecommitdiffstats
path: root/src/svm/svm_fifo_segment.c
diff options
context:
space:
mode:
authorFlorin Coras <fcoras@cisco.com>2018-01-31 06:52:17 -0800
committerDave Barach <openvpp@barachs.net>2018-02-05 21:45:28 +0000
commita332c46a51f9b4e13963340dfee1318e7513c124 (patch)
treefde1056972c5b164987bd15ce0530a3d48d1bbd7 /src/svm/svm_fifo_segment.c
parente71eb5922a293eca36dbd323970741daaca3c5c7 (diff)
session: segment manager refactor
- use valloc as a 'central' segment baseva manager - use per segment manager segment pools and use rwlocks to guard them - add session test that exercises segment creation - embed segment manager properties into application since they're shared - fix rw locks Change-Id: I761164c147275d9e8a926f1eda395e090d231f9a Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/svm/svm_fifo_segment.c')
-rw-r--r--src/svm/svm_fifo_segment.c233
1 files changed, 101 insertions, 132 deletions
diff --git a/src/svm/svm_fifo_segment.c b/src/svm/svm_fifo_segment.c
index 4d1833eee7e..459b1d1d8e5 100644
--- a/src/svm/svm_fifo_segment.c
+++ b/src/svm/svm_fifo_segment.c
@@ -56,69 +56,75 @@ allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
}
}
-static void
-preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
- svm_fifo_segment_create_args_t * a, u32 protect_size)
+/**
+ * Pre-allocates fifo pairs in fifo segment
+ *
+ * The number of fifos pre-allocated is the minimum of the requested number
+ * of pairs and the maximum number that fit within the segment. If the maximum
+ * is hit, the number of fifo pairs requested is updated by subtracting the
+ * number of fifos that have been successfully allocated.
+ */
+void
+svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
+ u32 rx_fifo_size, u32 tx_fifo_size,
+ u32 * n_fifo_pairs)
{
- svm_fifo_segment_header_t *fsh = s->h;
- u32 rx_fifo_size, tx_fifo_size, pairs_to_allocate;
u32 rx_rounded_data_size, tx_rounded_data_size, pair_size;
- svm_fifo_t *f;
- u8 *rx_fifo_space, *tx_fifo_space;
+ u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate;
int rx_freelist_index, tx_freelist_index;
+ ssvm_shared_header_t *sh = s->ssvm.sh;
+ svm_fifo_segment_header_t *fsh = s->h;
+ u8 *rx_fifo_space, *tx_fifo_space;
+ uword space_available;
+ void *oldheap;
+ svm_fifo_t *f;
int i;
/* Parameter check */
- if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
- || a->preallocated_fifo_pairs == 0)
+ if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
return;
- if (a->rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
- a->rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
+ if (rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
+ rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
{
- clib_warning ("rx fifo_size out of range %d", a->rx_fifo_size);
+ clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
return;
}
- if (a->tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
- a->tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
+ if (tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
+ tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
{
- clib_warning ("tx fifo_size out of range %d", a->rx_fifo_size);
+ clib_warning ("tx fifo_size out of range %d", rx_fifo_size);
return;
}
- rx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
-
- rx_freelist_index = max_log2 (a->rx_fifo_size)
+ rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
+ rx_freelist_index = max_log2 (rx_fifo_size)
- max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
-
- tx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
-
- tx_freelist_index = max_log2 (a->tx_fifo_size)
+ tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
+ tx_freelist_index = max_log2 (tx_fifo_size)
- max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
/* Calculate space requirements */
pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size;
- if (protect_size)
- protect_size += mheap_bytes (s->ssvm.sh->heap);
- pairs_to_allocate =
- clib_min ((s->ssvm.ssvm_size - protect_size) / pair_size,
- a->preallocated_fifo_pairs);
- rx_fifo_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate;
- tx_fifo_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate;
+ space_available = s->ssvm.ssvm_size - mheap_bytes (sh->heap);
+ pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs);
+ rx_fifos_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate;
+ tx_fifos_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate;
vec_validate_init_empty (fsh->free_fifos,
clib_max (rx_freelist_index, tx_freelist_index),
0);
+ oldheap = ssvm_push_heap (sh);
/* Allocate rx fifo space. May fail. */
rx_fifo_space = clib_mem_alloc_aligned_at_offset
- (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
+ (rx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
0 /* os_out_of_memory */ );
/* Same for TX */
tx_fifo_space = clib_mem_alloc_aligned_at_offset
- (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
+ (tx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
0 /* os_out_of_memory */ );
/* Make sure it worked. Clean up if it didn't... */
@@ -128,13 +134,14 @@ preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
clib_mem_free (rx_fifo_space);
else
clib_warning ("rx fifo preallocation failure: size %d npairs %d",
- a->rx_fifo_size, a->preallocated_fifo_pairs);
+ rx_fifo_size, *n_fifo_pairs);
if (tx_fifo_space)
clib_mem_free (tx_fifo_space);
else
clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
- a->tx_fifo_size, a->preallocated_fifo_pairs);
+ tx_fifo_size, *n_fifo_pairs);
+ ssvm_pop_heap (oldheap);
return;
}
@@ -160,7 +167,31 @@ preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
}
/* Account for the pairs allocated */
- a->preallocated_fifo_pairs -= pairs_to_allocate;
+ *n_fifo_pairs -= pairs_to_allocate;
+ ssvm_pop_heap (oldheap);
+}
+
+/**
+ * Initialize svm fifo segment shared header
+ */
+int
+svm_fifo_segment_init (svm_fifo_segment_private_t * s)
+{
+ svm_fifo_segment_header_t *fsh;
+ ssvm_shared_header_t *sh;
+ void *oldheap;
+
+ sh = s->ssvm.sh;
+ oldheap = ssvm_push_heap (sh);
+
+ fsh = clib_mem_alloc (sizeof (*fsh));
+ memset (fsh, 0, sizeof (*fsh));
+ s->h = sh->opaque[0] = fsh;
+
+ ssvm_pop_heap (oldheap);
+
+ sh->ready = 1;
+ return (0);
}
/**
@@ -171,9 +202,6 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
{
svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
svm_fifo_segment_private_t *s;
- ssvm_shared_header_t *sh;
- svm_fifo_segment_header_t *fsh;
- void *oldheap;
int rv;
/* Allocate a fresh segment */
@@ -195,18 +223,7 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
/* Note: requested_va updated due to seg base addr randomization */
sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size;
- sh = s->ssvm.sh;
- oldheap = ssvm_push_heap (sh);
-
- /* Set up svm_fifo_segment shared header */
- fsh = clib_mem_alloc (sizeof (*fsh));
- memset (fsh, 0, sizeof (*fsh));
- s->h = sh->opaque[0] = fsh;
- preallocate_fifo_pairs (s, a, a->seg_protected_space);
-
- ssvm_pop_heap (oldheap);
-
- sh->ready = 1;
+ svm_fifo_segment_init (s);
vec_add1 (a->new_segment_indices, s - sm->segments);
return (0);
}
@@ -217,78 +234,45 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
int
svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
{
- svm_fifo_segment_private_t *s;
svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
+ svm_fifo_segment_private_t *s;
ssvm_shared_header_t *sh;
- svm_fifo_segment_header_t *fsh;
- void *oldheap;
- u8 **heaps = 0;
mheap_t *heap_header;
- int segment_count = 1;
u32 rnd_size = 0;
- int i;
+ u8 *heap;
+ u32 pagesize = clib_mem_get_page_size ();
- if (a->private_segment_count)
- {
- u8 *heap;
- u32 pagesize = clib_mem_get_page_size ();
- rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize;
+ pool_get (sm->segments, s);
+ memset (s, 0, sizeof (*s));
- for (i = 0; i < a->private_segment_count; i++)
- {
- heap = mheap_alloc (0, rnd_size);
- if (heap == 0)
- {
- clib_unix_warning ("mheap alloc");
- return -1;
- }
- heap_header = mheap_header (heap);
- heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
- vec_add1 (heaps, heap);
- }
- segment_count = a->private_segment_count;
- }
+ rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize;
- /* Allocate segments */
- for (i = 0; i < segment_count; i++)
+ heap = mheap_alloc (0, rnd_size);
+ if (heap == 0)
{
- pool_get (sm->segments, s);
- memset (s, 0, sizeof (*s));
-
- s->ssvm.ssvm_size = rnd_size;
- s->ssvm.i_am_master = 1;
- s->ssvm.my_pid = getpid ();
- s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
- s->ssvm.requested_va = ~0;
-
- /* Allocate a [sic] shared memory header, in process memory... */
- sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
- s->ssvm.sh = sh;
-
- memset (sh, 0, sizeof (*sh));
- sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap ();
-
- /* Set up svm_fifo_segment shared header */
- fsh = clib_mem_alloc (sizeof (*fsh));
- memset (fsh, 0, sizeof (*fsh));
- fsh->flags = FIFO_SEGMENT_F_IS_PRIVATE;
- s->h = sh->opaque[0] = fsh;
- if (!a->private_segment_count)
- fsh->flags |= FIFO_SEGMENT_F_IS_MAIN_HEAP;
-
- if (a->private_segment_count)
- {
- if (i != 0)
- fsh->flags |= FIFO_SEGMENT_F_IS_PREALLOCATED;
- oldheap = clib_mem_get_heap ();
- clib_mem_set_heap (sh->heap);
- preallocate_fifo_pairs (s, a, i == 0 ? a->seg_protected_space : 0);
- clib_mem_set_heap (oldheap);
- }
- sh->ready = 1;
- vec_add1 (a->new_segment_indices, s - sm->segments);
+ clib_unix_warning ("mheap alloc");
+ pool_put (sm->segments, s);
+ return -1;
}
- vec_free (heaps);
+ heap_header = mheap_header (heap);
+ heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
+
+ s->ssvm.ssvm_size = rnd_size;
+ s->ssvm.i_am_master = 1;
+ s->ssvm.my_pid = getpid ();
+ s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
+ s->ssvm.requested_va = ~0;
+
+ /* Allocate a [sic] shared memory header, in process memory... */
+ sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
+ s->ssvm.sh = sh;
+
+ memset (sh, 0, sizeof (*sh));
+ sh->heap = heap;
+
+ svm_fifo_segment_init (s);
+ vec_add1 (a->new_segment_indices, s - sm->segments);
+
return (0);
}
@@ -333,18 +317,7 @@ svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
{
svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
- if (s->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
- {
- /* Don't try to free vpp's heap! */
- if (!(s->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
- mheap_free (s->ssvm.sh->heap);
- clib_mem_free (s->ssvm.sh);
- clib_mem_free (s->h);
- }
- else
- {
- ssvm_delete (&s->ssvm);
- }
+ ssvm_delete (&s->ssvm);
memset (s, 0xfe, sizeof (*s));
pool_put (sm->segments, s);
}
@@ -378,10 +351,10 @@ svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
- max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
sh = s->ssvm.sh;
- fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
ssvm_lock_non_recursive (sh, 1);
oldheap = ssvm_push_heap (sh);
+ fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
switch (list_index)
{
@@ -508,7 +481,7 @@ svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
}
void
-svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
+svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds)
{
svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
@@ -595,7 +568,7 @@ void
svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address,
u64 * size)
{
- if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+ if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE)
{
mheap_t *heap_header;
@@ -623,12 +596,8 @@ format_svm_fifo_segment_type (u8 * s, va_list * args)
sp = va_arg (*args, svm_fifo_segment_private_t *);
ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
- if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
- && !(sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
+ if (st == SSVM_SEGMENT_PRIVATE)
s = format (s, "%s", "private-heap");
- else if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
- && (sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
- s = format (s, "%s", "main-heap");
else if (st == SSVM_SEGMENT_MEMFD)
s = format (s, "%s", "memfd");
else if (st == SSVM_SEGMENT_SHM)