aboutsummaryrefslogtreecommitdiffstats
path: root/src/svm
diff options
context:
space:
mode:
Diffstat (limited to 'src/svm')
-rw-r--r--src/svm/ssvm.h17
-rw-r--r--src/svm/svm_fifo.c56
-rw-r--r--src/svm/svm_fifo.h16
-rw-r--r--src/svm/svm_fifo_segment.c114
-rw-r--r--src/svm/svm_fifo_segment.h4
-rw-r--r--src/svm/test_svm_fifo1.c10
6 files changed, 158 insertions, 59 deletions
diff --git a/src/svm/ssvm.h b/src/svm/ssvm.h
index bccfc164..8466e155 100644
--- a/src/svm/ssvm.h
+++ b/src/svm/ssvm.h
@@ -102,6 +102,15 @@ ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
}
always_inline void
+ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
+{
+ while (__sync_lock_test_and_set (&h->lock, 1))
+ ;
+
+ h->tag = tag;
+}
+
+always_inline void
ssvm_unlock (ssvm_shared_header_t * h)
{
if (--h->recursion_count == 0)
@@ -113,6 +122,14 @@ ssvm_unlock (ssvm_shared_header_t * h)
}
}
+always_inline void
+ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
+{
+ h->tag = 0;
+ CLIB_MEMORY_BARRIER ();
+ h->lock = 0;
+}
+
static inline void *
ssvm_push_heap (ssvm_shared_header_t * sh)
{
diff --git a/src/svm/svm_fifo.c b/src/svm/svm_fifo.c
index aed5d6a7..da60fee5 100644
--- a/src/svm/svm_fifo.c
+++ b/src/svm/svm_fifo.c
@@ -19,29 +19,29 @@
static inline u8
position_lt (svm_fifo_t * f, u32 a, u32 b)
{
- return (ooo_segment_distance_to_tail (f, a)
- < ooo_segment_distance_to_tail (f, b));
+ return (ooo_segment_distance_from_tail (f, a)
+ < ooo_segment_distance_from_tail (f, b));
}
static inline u8
position_leq (svm_fifo_t * f, u32 a, u32 b)
{
- return (ooo_segment_distance_to_tail (f, a)
- <= ooo_segment_distance_to_tail (f, b));
+ return (ooo_segment_distance_from_tail (f, a)
+ <= ooo_segment_distance_from_tail (f, b));
}
static inline u8
position_gt (svm_fifo_t * f, u32 a, u32 b)
{
- return (ooo_segment_distance_to_tail (f, a)
- > ooo_segment_distance_to_tail (f, b));
+ return (ooo_segment_distance_from_tail (f, a)
+ > ooo_segment_distance_from_tail (f, b));
}
static inline u32
position_diff (svm_fifo_t * f, u32 posa, u32 posb)
{
- return ooo_segment_distance_to_tail (f, posa)
- - ooo_segment_distance_to_tail (f, posb);
+ return ooo_segment_distance_from_tail (f, posa)
+ - ooo_segment_distance_from_tail (f, posb);
}
static inline u32
@@ -113,7 +113,7 @@ svm_fifo_create (u32 data_size_in_bytes)
if (f == 0)
return 0;
- memset (f, 0, sizeof (*f) + data_size_in_bytes);
+ memset (f, 0, sizeof (*f));
f->nitems = data_size_in_bytes;
f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
@@ -204,7 +204,19 @@ ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
{
s = prev;
s_end_pos = ooo_segment_end_pos (f, s);
- goto merge;
+
+ /* Check head and tail now since segment may be wider at both ends so
+ * merge tests lower won't work */
+ if (position_lt (f, normalized_position, s->start))
+ {
+ s->start = normalized_position;
+ s->length = position_diff (f, s_end_pos, s->start);
+ }
+ if (position_gt (f, normalized_end_position, s_end_pos))
+ {
+ s->length = position_diff (f, normalized_end_position, s->start);
+ }
+ goto check_tail;
}
s_index = s - f->ooo_segments;
@@ -257,8 +269,6 @@ ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
* Merge needed
*/
-merge:
-
/* Merge at head */
if (position_lt (f, normalized_position, s->start))
{
@@ -278,6 +288,7 @@ merge:
goto done;
}
+check_tail:
/* The new segment's tail may cover multiple smaller ones */
if (position_gt (f, normalized_end_position, s_end_pos))
{
@@ -296,7 +307,8 @@ merge:
/* If partial overlap with last, merge */
if (it && position_leq (f, it->start, normalized_end_position))
{
- s->length = ooo_segment_end_pos (f, it) - s->start;
+ s->length =
+ position_diff (f, ooo_segment_end_pos (f, it), s->start);
ooo_segment_del (f, it - f->ooo_segments);
}
}
@@ -319,9 +331,9 @@ ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
i32 diff;
s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
+ diff = ooo_segment_distance_to_tail (f, s->start);
- diff = (f->tail >= s->start) ?
- f->tail - s->start : f->nitems + f->tail - s->start;
+ ASSERT (diff != n_bytes_enqueued);
if (diff > n_bytes_enqueued)
return 0;
@@ -345,8 +357,7 @@ ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
if (s->next != OOO_SEGMENT_INVALID_INDEX)
{
s = pool_elt_at_index (f->ooo_segments, s->next);
- diff = (f->tail >= s->start) ?
- f->tail - s->start : f->nitems + f->tail - s->start;
+ diff = ooo_segment_distance_to_tail (f, s->start);
ooo_segment_del (f, index);
}
/* End of search */
@@ -357,6 +368,7 @@ ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
}
}
+ ASSERT (bytes >= 0 && bytes <= f->nitems);
return bytes;
}
@@ -401,6 +413,8 @@ svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
}
else
{
+ ASSERT (0);
+
/* Account for a zero-copy enqueue done elsewhere */
ASSERT (max_bytes <= (nitems - cursize));
f->tail += max_bytes;
@@ -413,6 +427,7 @@ svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
/* Atomically increase the queue length */
+ ASSERT (cursize + total_copy_bytes <= nitems);
__sync_fetch_and_add (&f->cursize, total_copy_bytes);
return (total_copy_bytes);
@@ -475,6 +490,8 @@ svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
cursize = svm_fifo_max_dequeue (f);
nitems = f->nitems;
+ ASSERT (required_bytes < nitems);
+
normalized_offset = (f->tail + offset) % nitems;
/* Will this request fit? */
@@ -557,6 +574,7 @@ svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
}
else
{
+ ASSERT (0);
/* Account for a zero-copy dequeue done elsewhere */
ASSERT (max_bytes <= cursize);
f->head += max_bytes;
@@ -565,6 +583,8 @@ svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
total_copy_bytes = max_bytes;
}
+ ASSERT (f->head <= nitems);
+ ASSERT (cursize >= total_copy_bytes);
__sync_fetch_and_sub (&f->cursize, total_copy_bytes);
return (total_copy_bytes);
@@ -702,6 +722,8 @@ svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
f->head = (f->head == nitems) ? 0 : f->head;
}
+ ASSERT (f->head <= nitems);
+ ASSERT (cursize >= total_drop_bytes);
__sync_fetch_and_sub (&f->cursize, total_drop_bytes);
return total_drop_bytes;
diff --git a/src/svm/svm_fifo.h b/src/svm/svm_fifo.h
index f32ef41d..fe21de47 100644
--- a/src/svm/svm_fifo.h
+++ b/src/svm/svm_fifo.h
@@ -133,25 +133,31 @@ svm_fifo_newest_ooo_segment (svm_fifo_t * f)
}
always_inline u32
-ooo_segment_distance_to_tail (svm_fifo_t * f, u32 a)
+ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
{
/* Ambiguous. Assumption is that ooo segments don't touch tail */
- if (a == f->tail && f->tail == f->head)
+ if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
return f->nitems;
- return ((f->nitems + a - f->tail) % f->nitems);
+ return (((f->nitems + pos) - f->tail) % f->nitems);
+}
+
+always_inline u32
+ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
+{
+ return (((f->nitems + f->tail) - pos) % f->nitems);
}
always_inline u32
ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
{
- return ooo_segment_distance_to_tail (f, s->start);
+ return ooo_segment_distance_from_tail (f, s->start);
}
always_inline u32
ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
{
- return ooo_segment_distance_to_tail (f, s->start) + s->length;
+ return ooo_segment_distance_from_tail (f, s->start) + s->length;
}
always_inline u32
diff --git a/src/svm/svm_fifo_segment.c b/src/svm/svm_fifo_segment.c
index c4ac2352..69d4ecb9 100644
--- a/src/svm/svm_fifo_segment.c
+++ b/src/svm/svm_fifo_segment.c
@@ -35,6 +35,11 @@ preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
rx_fifo_size = (sizeof (*f) + a->rx_fifo_size) * a->preallocated_fifo_pairs;
tx_fifo_size = (sizeof (*f) + a->tx_fifo_size) * a->preallocated_fifo_pairs;
+ if (0)
+ clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)",
+ rx_fifo_size, rx_fifo_size >> 20,
+ tx_fifo_size, tx_fifo_size >> 20);
+
/* 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 */ ,
@@ -129,7 +134,7 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
ssvm_pop_heap (oldheap);
sh->ready = 1;
- a->new_segment_index = s - sm->segments;
+ vec_add1 (a->new_segment_indices, s - sm->segments);
return (0);
}
@@ -141,35 +146,81 @@ svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
ssvm_shared_header_t *sh;
svm_fifo_segment_header_t *fsh;
+ void *oldheap;
+ u8 **heaps = 0;
+ mheap_t *heap_header;
+ int segment_count = 1;
+ int i;
- /* Allocate a fresh segment */
- pool_get (sm->segments, s);
- memset (s, 0, sizeof (*s));
-
- s->ssvm.ssvm_size = ~0;
- s->ssvm.i_am_master = 1;
- s->ssvm.my_pid = getpid ();
- s->ssvm.name = (u8 *) a->segment_name;
- 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;
+ if (a->private_segment_count && a->private_segment_size)
+ {
+ void *mem;
+ u8 *heap;
+ u32 pagesize = clib_mem_get_page_size ();
+ u32 rnd_size;
- memset (sh, 0, sizeof (*sh));
- sh->heap = clib_mem_get_heap ();
+ for (i = 0; i < a->private_segment_count; i++)
+ {
+ rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize;
+
+ mem = mmap (0, rnd_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS,
+ -1 /* fd */ , 0 /* offset */ );
+
+ if (mem == MAP_FAILED)
+ {
+ clib_unix_warning ("mmap");
+ return -1;
+ }
+ heap = mheap_alloc (mem, rnd_size);
+ heap_header = mheap_header (heap);
+ heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
+ vec_add1 (heaps, heap);
+ }
+ segment_count = a->private_segment_count;
+ }
- /* Set up svm_fifo_segment shared header */
- fsh = clib_mem_alloc (sizeof (*fsh));
- memset (fsh, 0, sizeof (*fsh));
- sh->opaque[0] = fsh;
- s->h = fsh;
- fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
+ /* Spread preallocated fifo pairs across segments */
+ a->preallocated_fifo_pairs /= segment_count;
- preallocate_fifo_pairs (fsh, a);
+ /* Allocate segments */
+ for (i = 0; i < segment_count; i++)
+ {
+ pool_get (sm->segments, s);
+ memset (s, 0, sizeof (*s));
+
+ s->ssvm.ssvm_size = ~0;
+ s->ssvm.i_am_master = 1;
+ s->ssvm.my_pid = getpid ();
+ s->ssvm.name = (u8 *) a->segment_name;
+ 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));
+ sh->opaque[0] = fsh;
+ s->h = fsh;
+ fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
+
+ if (a->private_segment_count)
+ {
+ oldheap = clib_mem_get_heap ();
+ clib_mem_set_heap (sh->heap);
+ preallocate_fifo_pairs (fsh, a);
+ clib_mem_set_heap (oldheap);
+ }
- sh->ready = 1;
- a->new_segment_index = s - sm->segments;
+ sh->ready = 1;
+ vec_add1 (a->new_segment_indices, s - sm->segments);
+ }
+ vec_free (heaps);
return (0);
}
@@ -205,7 +256,7 @@ svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
s->h = fsh;
- a->new_segment_index = s - sm->segments;
+ vec_add1 (a->new_segment_indices, s - sm->segments);
return (0);
}
@@ -230,7 +281,7 @@ svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
sh = s->ssvm.sh;
fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
- ssvm_lock (sh, 1, 0);
+ ssvm_lock_non_recursive (sh, 1);
oldheap = ssvm_push_heap (sh);
switch (list_index)
@@ -261,7 +312,7 @@ svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
if (PREDICT_FALSE (f == 0))
{
ssvm_pop_heap (oldheap);
- ssvm_unlock (sh);
+ ssvm_unlock_non_recursive (sh);
return (0);
}
@@ -281,7 +332,7 @@ found:
}
ssvm_pop_heap (oldheap);
- ssvm_unlock (sh);
+ ssvm_unlock_non_recursive (sh);
return (f);
}
@@ -293,10 +344,11 @@ svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
svm_fifo_segment_header_t *fsh;
void *oldheap;
+
sh = s->ssvm.sh;
fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
- ssvm_lock (sh, 1, 0);
+ ssvm_lock_non_recursive (sh, 2);
oldheap = ssvm_push_heap (sh);
switch (list_index)
@@ -325,7 +377,7 @@ svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
}
ssvm_pop_heap (oldheap);
- ssvm_unlock (sh);
+ ssvm_unlock_non_recursive (sh);
}
void
diff --git a/src/svm/svm_fifo_segment.h b/src/svm/svm_fifo_segment.h
index 31e14db5..a7a3f469 100644
--- a/src/svm/svm_fifo_segment.h
+++ b/src/svm/svm_fifo_segment.h
@@ -57,10 +57,12 @@ typedef struct
{
char *segment_name;
u32 segment_size;
- u32 new_segment_index;
+ u32 *new_segment_indices;
u32 rx_fifo_size;
u32 tx_fifo_size;
u32 preallocated_fifo_pairs;
+ u32 private_segment_count;
+ u32 private_segment_size;
} svm_fifo_segment_create_args_t;
static inline svm_fifo_segment_private_t *
diff --git a/src/svm/test_svm_fifo1.c b/src/svm/test_svm_fifo1.c
index 63b4a9b7..63d75845 100644
--- a/src/svm/test_svm_fifo1.c
+++ b/src/svm/test_svm_fifo1.c
@@ -39,7 +39,7 @@ hello_world (int verbose)
if (rv)
return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
- sp = svm_fifo_get_segment (a->new_segment_index);
+ sp = svm_fifo_get_segment (a->new_segment_indices[0]);
f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
@@ -92,7 +92,7 @@ master (int verbose)
if (rv)
return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
- sp = svm_fifo_get_segment (a->new_segment_index);
+ sp = svm_fifo_get_segment (a->new_segment_indices[0]);
f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
@@ -128,7 +128,7 @@ mempig (int verbose)
if (rv)
return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
- sp = svm_fifo_get_segment (a->new_segment_index);
+ sp = svm_fifo_get_segment (a->new_segment_indices[0]);
for (i = 0; i < 1000; i++)
{
@@ -186,7 +186,7 @@ offset (int verbose)
if (rv)
return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
- sp = svm_fifo_get_segment (a->new_segment_index);
+ sp = svm_fifo_get_segment (a->new_segment_indices[0]);
f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST);
@@ -246,7 +246,7 @@ slave (int verbose)
if (rv)
return clib_error_return (0, "svm_fifo_segment_attach returned %d", rv);
- sp = svm_fifo_get_segment (a->new_segment_index);
+ sp = svm_fifo_get_segment (a->new_segment_indices[0]);
sh = sp->ssvm.sh;
fsh = (svm_fifo_segment_header_t *) sh->opaque[0];