X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fsvm%2Ffifo_segment.c;h=3fd7d9d6ede76a8f2de6e1647c76b4cc0f42fa7a;hb=67fe778546683d678015cc0089bbe475cb6a6f22;hp=816f7113fd6d69db47d5ebf7244dd157a479a6e2;hpb=05c7317d07b878b8b5795fed5c1593616a1c1fae;p=vpp.git diff --git a/src/svm/fifo_segment.c b/src/svm/fifo_segment.c index 816f7113fd6..3fd7d9d6ede 100644 --- a/src/svm/fifo_segment.c +++ b/src/svm/fifo_segment.c @@ -15,6 +15,12 @@ #include +static inline fifo_segment_slice_t * +fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index) +{ + return &fsh->slices[slice_index]; +} + static char *fifo_segment_mem_status_strings[] = { #define _(sym,str) str, foreach_segment_mem_status @@ -78,6 +84,41 @@ fsh_n_cached_bytes (fifo_segment_header_t * fsh) return n_cached; } +static inline void +fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc) +{ + clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc); +} + +static inline u32 +fsh_n_active_fifos (fifo_segment_header_t * fsh) +{ + return clib_atomic_load_relax_n (&fsh->n_active_fifos); +} + +static inline uword +fsh_virtual_mem (fifo_segment_header_t * fsh) +{ + fifo_segment_slice_t *fss; + uword total_vm = 0; + int i; + + for (i = 0; i < fsh->n_slices; i++) + { + fss = fsh_slice_get (fsh, i); + total_vm += clib_atomic_load_relax_n (&fss->virtual_mem); + } + return total_vm; +} + +void +fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index, + int n_bytes) +{ + fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index); + fss->virtual_mem += n_bytes; +} + static void fsh_check_mem (fifo_segment_header_t * fsh) { @@ -95,18 +136,6 @@ fsh_check_mem (fifo_segment_header_t * fsh) fsh_update_free_bytes (fsh); } -static inline fifo_segment_slice_t * -fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index) -{ - return &fsh->slices[slice_index]; -} - -static inline void -fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc) -{ - clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc); -} - /** * Initialize fifo segment shared header */ @@ -152,6 +181,7 @@ fifo_segment_init (fifo_segment_t * fs) { fss = fsh_slice_get (fsh, i); vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0); + vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0); clib_spinlock_init (&fss->chunk_lock); } @@ -288,7 +318,7 @@ fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size) * with a smaller FIFO. */ return size >= FIFO_SEGMENT_MIN_FIFO_SIZE - && size <= (1 << fsh->max_log2_chunk_size); + && size <= (1ULL << fsh->max_log2_chunk_size); } static svm_fifo_t * @@ -390,8 +420,11 @@ fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh, f = fss->free_fifos; if (!f) { + if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t))) + return 0; + void *oldheap = ssvm_push_heap (fsh->ssvm_sh); - f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES); + f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES); ssvm_pop_heap (oldheap); if (!f) return 0; @@ -504,6 +537,7 @@ fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh, cmem += sizeof (*c) + rounded_data_size; } + fss->num_chunks[fl_index] += batch_size; fss->n_fl_chunk_bytes += batch_size * rounded_data_size; fsh_cached_bytes_add (fsh, batch_size * rounded_data_size); fsh_free_bytes_sub (fsh, size); @@ -538,23 +572,30 @@ fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh, if (fmem == 0) return -1; - /* Carve fifo + chunk space */ + /* Carve fifo hdr space */ for (i = 0; i < batch_size; i++) { f = (svm_fifo_t *) fmem; memset (f, 0, sizeof (*f)); f->next = fss->free_fifos; fss->free_fifos = f; - c = (svm_fifo_chunk_t *) (fmem + sizeof (*f)); + fmem += sizeof (*f); + } + + /* Carve chunk space */ + for (i = 0; i < batch_size; i++) + { + c = (svm_fifo_chunk_t *) fmem; c->start_byte = 0; c->length = rounded_data_size; c->enq_rb_index = RBTREE_TNIL_INDEX; c->deq_rb_index = RBTREE_TNIL_INDEX; c->next = fss->free_chunks[fl_index]; fss->free_chunks[fl_index] = c; - fmem += hdrs + rounded_data_size; + fmem += sizeof (svm_fifo_chunk_t) + rounded_data_size; } + fss->num_chunks[fl_index] += batch_size; fss->n_fl_chunk_bytes += batch_size * rounded_data_size; fsh_cached_bytes_add (fsh, batch_size * rounded_data_size); fsh_free_bytes_sub (fsh, size); @@ -582,8 +623,9 @@ fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096); fl_index = fs_freelist_for_size (min_size); - fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); - fifo_sz += 1 << max_log2 (min_size); + + if (fl_index >= vec_len (fss->free_chunks)) + return 0; clib_spinlock_lock (&fss->chunk_lock); @@ -597,18 +639,28 @@ fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, } } - fsh_check_mem (fsh); + fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); + fifo_sz += 1 << max_log2 (min_size); n_free_bytes = fsh_n_free_bytes (fsh); + if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes) { - if (fs_try_alloc_fifo_batch (fsh, fss, fl_index, - FIFO_SEGMENT_ALLOC_BATCH_SIZE)) - goto done; - - f = fs_try_alloc_fifo_freelist (fss, fl_index); - if (f) - fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); - goto done; + if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index, + FIFO_SEGMENT_ALLOC_BATCH_SIZE)) + { + f = fs_try_alloc_fifo_freelist (fss, fl_index); + if (f) + { + fsh_cached_bytes_sub (fsh, + fs_freelist_index_to_size (fl_index)); + goto done; + } + } + else + { + fsh_check_mem (fsh); + n_free_bytes = fsh_n_free_bytes (fsh); + } } if (fifo_sz <= n_free_bytes) { @@ -617,9 +669,11 @@ fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, ssvm_pop_heap (oldheap); if (f) { + clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); fsh_free_bytes_sub (fsh, fifo_sz); goto done; } + fsh_check_mem (fsh); } /* All failed, try to allocate min of data bytes and fifo sz */ fifo_sz = clib_min (fifo_sz, data_bytes); @@ -642,15 +696,14 @@ fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size) { fifo_segment_slice_t *fss; svm_fifo_chunk_t *c; - void *oldheap; int fl_index; - uword n_free; fl_index = fs_freelist_for_size (chunk_size); fss = fsh_slice_get (fsh, slice_index); clib_spinlock_lock (&fss->chunk_lock); + ASSERT (vec_len (fss->free_chunks) > fl_index); c = fss->free_chunks[fl_index]; if (c) @@ -660,37 +713,56 @@ fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size) fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); } - else if (chunk_size <= (n_free = fsh_n_free_bytes (fsh))) + else { - fsh_check_mem (fsh); + void *oldheap; + uword n_free; + u32 batch; chunk_size = fs_freelist_index_to_size (fl_index); - if (n_free < chunk_size) - goto done; + n_free = fsh_n_free_bytes (fsh); - oldheap = ssvm_push_heap (fsh->ssvm_sh); - c = svm_fifo_chunk_alloc (chunk_size); - ssvm_pop_heap (oldheap); + if (chunk_size <= n_free) + { + oldheap = ssvm_push_heap (fsh->ssvm_sh); + c = svm_fifo_chunk_alloc (chunk_size); + ssvm_pop_heap (oldheap); - if (!c) - goto done; + if (c) + { + clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); + fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c)); + goto done; + } - fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c)); - } - else if (chunk_size <= fss->n_fl_chunk_bytes) - { - c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); - } - else if (chunk_size <= fss->n_fl_chunk_bytes + n_free) - { - u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE; - u32 batch; + fsh_check_mem (fsh); + n_free = fsh_n_free_bytes (fsh); + } + if (chunk_size <= fss->n_fl_chunk_bytes) + { + c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); + if (c) + goto done; + batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE; + if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch)) + { + fsh_check_mem (fsh); + goto done; + } + } + if (chunk_size <= fss->n_fl_chunk_bytes + n_free) + { + u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE; - fsh_check_mem (fsh); - batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size; - batch = clib_min (batch + 1, n_free / min_size); - if (!fsh_try_alloc_chunk_batch (fsh, fss, 0, batch)) - c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); + batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size; + batch = clib_min (batch + 1, n_free / min_size); + if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch)) + { + fsh_check_mem (fsh); + goto done; + } + c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); + } } done: @@ -737,6 +809,31 @@ fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index, fsh_slice_collect_chunks (fsh, fss, c); } +static inline void +fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f) +{ + if (fss->fifos) + { + fss->fifos->prev = f; + f->next = fss->fifos; + } + fss->fifos = f; +} + +static inline void +fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f) +{ + if (f->flags & SVM_FIFO_F_LL_TRACKED) + { + if (f->prev) + f->prev->next = f->next; + else + fss->fifos = f->next; + if (f->next) + f->next->prev = f->prev; + } +} + /** * Allocate fifo in fifo segment */ @@ -750,6 +847,9 @@ fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index, ASSERT (slice_index < fs->n_slices); + if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size)) + return 0; + fss = fsh_slice_get (fsh, slice_index); f = fs_try_alloc_fifo (fsh, fss, data_bytes); if (!f) @@ -765,12 +865,7 @@ fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index, * only one. */ if (ftype == FIFO_SEGMENT_RX_FIFO) { - if (fss->fifos) - { - fss->fifos->prev = f; - f->next = fss->fifos; - } - fss->fifos = f; + fss_fifo_add_active_list (fss, f); f->flags |= SVM_FIFO_F_LL_TRACKED; svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ ); @@ -781,6 +876,7 @@ fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index, } fsh_active_fifos_update (fsh, 1); + fss->virtual_mem += svm_fifo_size (f); done: return (f); @@ -805,20 +901,10 @@ fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f) /* Remove from active list. Only rx fifos are tracked */ if (f->flags & SVM_FIFO_F_LL_TRACKED) { - if (f->prev) - f->prev->next = f->next; - else - fss->fifos = f->next; - if (f->next) - f->next->prev = f->prev; + fss_fifo_del_active_list (fss, f); f->flags &= ~SVM_FIFO_F_LL_TRACKED; } - /* Add to free list */ - f->next = fss->free_fifos; - f->prev = 0; - fss->free_fifos = f; - /* Free fifo chunks */ fsh_slice_collect_chunks (fsh, fss, f->start_chunk); @@ -835,9 +921,62 @@ fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f) f->master_thread_index = ~0; } + fss->virtual_mem -= svm_fifo_size (f); + + /* Add to free list */ + f->next = fss->free_fifos; + f->prev = 0; + fss->free_fifos = f; + fsh_active_fifos_update (fsh, -1); } +void +fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f) +{ + fifo_segment_slice_t *fss; + svm_fifo_chunk_t *c; + u32 fl_index; + + ASSERT (f->refcnt == 1); + + fss = fsh_slice_get (fs->h, f->slice_index); + fss->virtual_mem -= svm_fifo_size (f); + if (f->flags & SVM_FIFO_F_LL_TRACKED) + fss_fifo_del_active_list (fss, f); + + c = f->start_chunk; + while (c) + { + fl_index = fs_freelist_for_size (c->length); + clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1); + c = c->next; + } +} + +void +fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f, + u32 slice_index) +{ + fifo_segment_slice_t *fss; + svm_fifo_chunk_t *c; + u32 fl_index; + + f->slice_index = slice_index; + fss = fsh_slice_get (fs->h, f->slice_index); + fss->virtual_mem += svm_fifo_size (f); + if (f->flags & SVM_FIFO_F_LL_TRACKED) + fss_fifo_add_active_list (fss, f); + + c = f->start_chunk; + while (c) + { + fl_index = fs_freelist_for_size (c->length); + clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); + c = c->next; + } +} + int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index, u32 batch_size) @@ -925,6 +1064,7 @@ fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index, fsh_cached_bytes_add (fsh, rounded_data_size); } + fss->num_chunks[fl_index] += batch_size; fss->n_fl_chunk_bytes += batch_size * rounded_data_size; fsh_free_bytes_sub (fsh, size); @@ -1000,7 +1140,7 @@ fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs, u32 fifo_segment_num_fifos (fifo_segment_t * fs) { - return clib_atomic_load_relax_n (&fs->h->n_active_fifos); + return fsh_n_active_fifos (fs->h); } static u32 @@ -1158,17 +1298,7 @@ fifo_segment_fl_chunk_bytes (fifo_segment_t * fs) u8 fifo_segment_has_fifos (fifo_segment_t * fs) { - fifo_segment_header_t *fsh = fs->h; - fifo_segment_slice_t *fss; - int slice_index; - - for (slice_index = 0; slice_index < fs->n_slices; slice_index++) - { - fss = fsh_slice_get (fsh, slice_index); - if (fss->fifos) - return 1; - } - return 0; + return (fsh_n_active_fifos (fs->h) != 0); } svm_fifo_t * @@ -1251,12 +1381,13 @@ format_fifo_segment_type (u8 * s, va_list * args) u8 * format_fifo_segment (u8 * s, va_list * args) { - u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0; + u32 count, indent, active_fifos, free_fifos; fifo_segment_t *fs = va_arg (*args, fifo_segment_t *); int verbose __attribute__ ((unused)) = va_arg (*args, int); uword est_chunk_bytes, est_free_seg_bytes, free_chunks; uword chunk_bytes = 0, free_seg_bytes, chunk_size; uword tracked_cached_bytes; + uword fifo_hdr = 0, reserved; fifo_segment_header_t *fsh; fifo_segment_slice_t *fss; svm_fifo_chunk_t *c; @@ -1264,7 +1395,7 @@ format_fifo_segment (u8 * s, va_list * args) char *address; size_t size; int i; - uword allocated, in_use; + uword allocated, in_use, virt; f64 usage; fifo_segment_mem_status_t mem_st; @@ -1292,8 +1423,9 @@ format_fifo_segment (u8 * s, va_list * args) free_chunks = fifo_segment_num_free_chunks (fs, ~0); if (free_chunks) - s = format (s, "\n\n%UFree chunks by size:\n", format_white_space, - indent + 2); + s = + format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space, + indent + 2); else s = format (s, "\n"); @@ -1303,7 +1435,7 @@ format_fifo_segment (u8 * s, va_list * args) for (i = 0; i < vec_len (fss->free_chunks); i++) { c = fss->free_chunks[i]; - if (c == 0) + if (c == 0 && fss->num_chunks[i] == 0) continue; count = 0; while (c) @@ -1313,8 +1445,8 @@ format_fifo_segment (u8 * s, va_list * args) } chunk_size = fs_freelist_index_to_size (i); - s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2, - chunk_size >> 10, count); + s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2, + chunk_size >> 10, count, fss->num_chunks[i]); chunk_bytes += count * chunk_size; } @@ -1330,26 +1462,26 @@ format_fifo_segment (u8 * s, va_list * args) in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes; usage = (100.0 * in_use) / allocated; mem_st = fifo_segment_get_mem_status (fs); - - s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n", - format_white_space, indent + 2, format_memory_size, - free_seg_bytes, free_seg_bytes, format_memory_size, - est_free_seg_bytes, est_free_seg_bytes); - s = - format (s, - "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n", - format_white_space, indent + 2, format_memory_size, chunk_bytes, - chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes, - format_memory_size, tracked_cached_bytes, tracked_cached_bytes); - s = - format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n", - format_white_space, indent + 2, format_memory_size, fifo_hdr, - fifo_hdr, format_memory_size, fsh->n_reserved_bytes, - fsh->n_reserved_bytes); - s = - format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space, - indent + 2, usage, format_memory_size, in_use, format_memory_size, - allocated, fifo_segment_mem_status_strings[mem_st]); + virt = fsh_virtual_mem (fsh); + reserved = fsh->n_reserved_bytes; + + s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:" + " %U (%lu)\n", format_white_space, indent + 2, + format_memory_size, free_seg_bytes, free_seg_bytes, + format_memory_size, est_free_seg_bytes, est_free_seg_bytes, + format_memory_size, reserved, reserved); + s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:" + " %U (%lu)\n", format_white_space, indent + 2, + format_memory_size, chunk_bytes, chunk_bytes, + format_memory_size, est_chunk_bytes, est_chunk_bytes, + format_memory_size, tracked_cached_bytes, tracked_cached_bytes); + s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n", + format_white_space, indent + 2, fsh->n_active_fifos, + format_memory_size, fifo_hdr, fifo_hdr); + s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n", + format_white_space, indent + 2, usage, format_memory_size, + in_use, format_memory_size, allocated, format_memory_size, virt, + fifo_segment_mem_status_strings[mem_st]); s = format (s, "\n"); return s;