2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <svm/fifo_segment.h>
18 static inline fifo_segment_slice_t *
19 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
21 return &fsh->slices[slice_index];
24 static char *fifo_segment_mem_status_strings[] = {
25 #define _(sym,str) str,
26 foreach_segment_mem_status
31 * Fifo segment free space
33 * Queries the underlying memory manager, dlmalloc, for free space. Since this
34 * ends up walking the internal data structures, it should not be called
37 * @param fs fifo segment
38 * @return number of free bytes
41 fsh_free_space (fifo_segment_header_t * fsh)
43 struct dlmallinfo dlminfo;
45 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
46 return dlminfo.fordblks;
50 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
52 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
56 fsh_n_free_bytes (fifo_segment_header_t * fsh)
58 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
59 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
63 fsh_update_free_bytes (fifo_segment_header_t * fsh)
65 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
69 fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
71 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
75 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
77 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
81 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
83 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
88 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
90 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
94 fsh_n_active_fifos (fifo_segment_header_t * fsh)
96 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
100 fsh_virtual_mem (fifo_segment_header_t * fsh)
102 fifo_segment_slice_t *fss;
106 for (i = 0; i < fsh->n_slices; i++)
108 fss = fsh_slice_get (fsh, i);
109 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
115 fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
118 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
119 fss->virtual_mem += n_bytes;
123 fsh_check_mem (fifo_segment_header_t * fsh)
127 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
130 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
131 2 * fsh->n_reserved_bytes);
132 if (fsh->n_free_bytes > thresh)
135 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
136 fsh_update_free_bytes (fsh);
140 * Initialize fifo segment shared header
143 fifo_segment_init (fifo_segment_t * fs)
145 fifo_segment_header_t *fsh;
146 fifo_segment_slice_t *fss;
147 ssvm_shared_header_t *sh;
154 oldheap = ssvm_push_heap (sh);
157 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
158 * Long story made short: the "process-private" fifo segment
159 * is allocated from the main heap, not mmapped. dlmalloc
160 * only guarantees 4-byte alignment, and on aarch64
161 * the fsh can end up 4-byte but not 8-byte aligned.
162 * That eventually causes the atomic op in fifo_segment_update_free_bytes
165 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
166 clib_memset (fsh, 0, sizeof (*fsh));
167 fs->h = sh->opaque[0] = fsh;
168 fs->n_slices = clib_max (fs->n_slices, 1);
170 fsh->ssvm_sh = fs->ssvm.sh;
171 fsh->n_slices = fs->n_slices;
172 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
173 FIFO_SEGMENT_MAX_FIFO_SIZE);
174 fsh->max_log2_chunk_size = max_log2 (max_fifo);
176 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
177 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
178 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
180 for (i = 0; i < fs->n_slices; i++)
182 fss = fsh_slice_get (fsh, i);
183 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
184 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
185 clib_spinlock_init (&fss->chunk_lock);
188 ssvm_pop_heap (oldheap);
190 fsh->n_free_bytes = fsh_free_space (fsh);
191 fsh->n_cached_bytes = 0;
192 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
198 * Create a fifo segment and initialize as master
201 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
207 /* Allocate a fresh segment */
208 pool_get_zero (sm->segments, fs);
210 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
211 fs->ssvm.ssvm_size = a->segment_size;
212 fs->ssvm.i_am_master = 1;
213 fs->ssvm.my_pid = getpid ();
214 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
215 fs->ssvm.requested_va = baseva;
217 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
219 pool_put (sm->segments, fs);
223 /* Note: requested_va updated due to seg base addr randomization */
224 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
226 fifo_segment_init (fs);
227 vec_add1 (a->new_segment_indices, fs - sm->segments);
232 * Attach as slave to a fifo segment
235 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
240 pool_get_zero (sm->segments, fs);
242 fs->ssvm.ssvm_size = a->segment_size;
243 fs->ssvm.my_pid = getpid ();
244 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
245 fs->ssvm.requested_va = sm->next_baseva;
246 if (a->segment_type == SSVM_SEGMENT_MEMFD)
247 fs->ssvm.fd = a->memfd_fd;
249 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
251 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
253 _vec_len (fs) = vec_len (fs) - 1;
257 /* Fish the segment header */
258 fs->h = fs->ssvm.sh->opaque[0];
260 vec_add1 (a->new_segment_indices, fs - sm->segments);
265 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
267 ssvm_delete (&s->ssvm);
268 clib_memset (s, 0xfe, sizeof (*s));
269 pool_put (sm->segments, s);
273 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
275 return s - sm->segments;
279 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
281 return pool_elt_at_index (sm->segments, segment_index);
285 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
287 *address = (char *) seg->ssvm.sh->ssvm_va;
288 *size = seg->ssvm.ssvm_size;
292 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
293 u32 timeout_in_seconds)
295 sm->next_baseva = baseva;
296 sm->timeout_in_seconds = timeout_in_seconds;
300 fs_freelist_for_size (u32 size)
302 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
304 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
308 fs_freelist_index_to_size (u32 fl_index)
310 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
314 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
317 * 4K minimum. It's not likely that anything good will happen
318 * with a smaller FIFO.
320 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
321 && size <= (1ULL << fsh->max_log2_chunk_size);
325 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
331 c = fss->free_chunks[fl_index];
336 fss->free_fifos = f->next;
337 fss->free_chunks[fl_index] = c->next;
340 memset (f, 0, sizeof (*f));
344 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
349 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
350 fifo_segment_slice_t * fss, u32 data_bytes)
352 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
353 svm_fifo_chunk_t *c, *first = 0, *next;
355 fl_index = fs_freelist_for_size (req_bytes);
359 fl_size = fs_freelist_index_to_size (fl_index);
363 c = fss->free_chunks[fl_index];
366 fss->free_chunks[fl_index] = c->next;
370 req_bytes -= clib_min (fl_size, req_bytes);
374 /* Failed to allocate with smaller chunks */
377 /* free all chunks if any allocated */
381 fl_index = fs_freelist_for_size (c->length);
382 fl_size = fs_freelist_index_to_size (fl_index);
384 c->next = fss->free_chunks[fl_index];
385 fss->free_chunks[fl_index] = c;
386 fss->n_fl_chunk_bytes += fl_size;
391 fl_index = fs_freelist_for_size (data_bytes);
392 if (fss->free_chunks[fl_index + 1])
395 fl_size = fs_freelist_index_to_size (fl_index);
402 fl_size = fl_size >> 1;
406 fss->n_fl_chunk_bytes -= n_alloc;
407 fsh_cached_bytes_sub (fsh, n_alloc);
412 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
413 fifo_segment_slice_t * fss,
416 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
417 u32 fl_index, fl_size, n_alloc = 0;
423 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
424 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
425 ssvm_pop_heap (oldheap);
428 memset (f, 0, sizeof (*f));
429 fsh_free_bytes_sub (fsh, sizeof (*f));
433 fss->free_fifos = f->next;
436 fl_index = fs_freelist_for_size (data_bytes);
440 fl_size = fs_freelist_index_to_size (fl_index);
444 c = fss->free_chunks[fl_index];
447 fss->free_chunks[fl_index] = c->next;
453 data_bytes -= clib_min (fl_size, data_bytes);
457 /* Failed to allocate with smaller chunks */
460 /* free all chunks if any allocated */
464 fl_index = fs_freelist_for_size (c->length);
465 fl_size = fs_freelist_index_to_size (fl_index);
467 c->next = fss->free_chunks[fl_index];
468 fss->free_chunks[fl_index] = c;
469 fss->n_fl_chunk_bytes += fl_size;
471 data_bytes += fl_size;
475 fl_index = fs_freelist_for_size (data_bytes);
476 if (fss->free_chunks[fl_index + 1])
479 fl_size = fs_freelist_index_to_size (fl_index);
483 f->next = fss->free_fifos;
488 fl_size = fl_size >> 1;
492 f->start_chunk = first;
494 fss->n_fl_chunk_bytes -= n_alloc;
495 fsh_cached_bytes_sub (fsh, n_alloc);
500 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
501 fifo_segment_slice_t * fss,
502 u32 fl_index, u32 batch_size)
504 u32 rounded_data_size;
511 rounded_data_size = fs_freelist_index_to_size (fl_index);
512 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
514 oldheap = ssvm_push_heap (fsh->ssvm_sh);
515 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
516 0 /* align_offset */ ,
517 0 /* os_out_of_memory */ );
518 ssvm_pop_heap (oldheap);
524 /* Carve fifo + chunk space */
525 for (i = 0; i < batch_size; i++)
527 c = (svm_fifo_chunk_t *) cmem;
529 c->length = rounded_data_size;
530 c->enq_rb_index = RBTREE_TNIL_INDEX;
531 c->deq_rb_index = RBTREE_TNIL_INDEX;
532 c->next = fss->free_chunks[fl_index];
533 fss->free_chunks[fl_index] = c;
534 cmem += sizeof (*c) + rounded_data_size;
537 fss->num_chunks[fl_index] += batch_size;
538 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
539 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
540 fsh_free_bytes_sub (fsh, size);
546 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
547 fifo_segment_slice_t * fss,
548 u32 fl_index, u32 batch_size)
550 u32 hdrs, rounded_data_size;
558 rounded_data_size = fs_freelist_index_to_size (fl_index);
559 hdrs = sizeof (*f) + sizeof (*c);
560 size = (uword) (hdrs + rounded_data_size) * batch_size;
562 oldheap = ssvm_push_heap (fsh->ssvm_sh);
563 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
564 0 /* align_offset */ ,
565 0 /* os_out_of_memory */ );
566 ssvm_pop_heap (oldheap);
572 /* Carve fifo + chunk space */
573 for (i = 0; i < batch_size; i++)
575 f = (svm_fifo_t *) fmem;
576 memset (f, 0, sizeof (*f));
577 f->next = fss->free_fifos;
579 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
581 c->length = rounded_data_size;
582 c->enq_rb_index = RBTREE_TNIL_INDEX;
583 c->deq_rb_index = RBTREE_TNIL_INDEX;
584 c->next = fss->free_chunks[fl_index];
585 fss->free_chunks[fl_index] = c;
586 fmem += hdrs + rounded_data_size;
589 fss->num_chunks[fl_index] += batch_size;
590 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
591 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
592 fsh_free_bytes_sub (fsh, size);
598 * Try to allocate new fifo
600 * Tries the following steps in order:
601 * - grab fifo and chunk from freelists
602 * - batch fifo and chunk allocation
603 * - single fifo allocation
604 * - grab multiple fifo chunks from freelists
607 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
610 u32 fifo_sz, fl_index;
615 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
616 fl_index = fs_freelist_for_size (min_size);
618 clib_spinlock_lock (&fss->chunk_lock);
620 if (fss->free_fifos && fss->free_chunks[fl_index])
622 f = fs_try_alloc_fifo_freelist (fss, fl_index);
625 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
630 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
631 fifo_sz += 1 << max_log2 (min_size);
632 n_free_bytes = fsh_n_free_bytes (fsh);
634 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
636 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
637 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
639 f = fs_try_alloc_fifo_freelist (fss, fl_index);
642 fsh_cached_bytes_sub (fsh,
643 fs_freelist_index_to_size (fl_index));
650 n_free_bytes = fsh_n_free_bytes (fsh);
653 if (fifo_sz <= n_free_bytes)
655 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
656 f = svm_fifo_alloc (min_size);
657 ssvm_pop_heap (oldheap);
660 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
661 fsh_free_bytes_sub (fsh, fifo_sz);
666 /* All failed, try to allocate min of data bytes and fifo sz */
667 fifo_sz = clib_min (fifo_sz, data_bytes);
668 if (fifo_sz <= fss->n_fl_chunk_bytes)
669 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
672 clib_spinlock_unlock (&fss->chunk_lock);
676 f->size = data_bytes;
683 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
685 fifo_segment_slice_t *fss;
689 fl_index = fs_freelist_for_size (chunk_size);
690 fss = fsh_slice_get (fsh, slice_index);
692 clib_spinlock_lock (&fss->chunk_lock);
694 c = fss->free_chunks[fl_index];
698 fss->free_chunks[fl_index] = c->next;
700 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
701 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
709 chunk_size = fs_freelist_index_to_size (fl_index);
710 n_free = fsh_n_free_bytes (fsh);
712 if (chunk_size <= n_free)
714 oldheap = ssvm_push_heap (fsh->ssvm_sh);
715 c = svm_fifo_chunk_alloc (chunk_size);
716 ssvm_pop_heap (oldheap);
720 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
721 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
726 n_free = fsh_n_free_bytes (fsh);
728 if (chunk_size <= fss->n_fl_chunk_bytes)
730 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
733 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
734 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
740 if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
742 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
744 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
745 batch = clib_min (batch + 1, n_free / min_size);
746 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
751 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
757 clib_spinlock_unlock (&fss->chunk_lock);
763 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
764 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
766 svm_fifo_chunk_t *next;
770 clib_spinlock_lock (&fss->chunk_lock);
775 fl_index = fs_freelist_for_size (c->length);
776 c->next = fss->free_chunks[fl_index];
777 c->enq_rb_index = RBTREE_TNIL_INDEX;
778 c->deq_rb_index = RBTREE_TNIL_INDEX;
779 fss->free_chunks[fl_index] = c;
780 n_collect += fs_freelist_index_to_size (fl_index);
784 fss->n_fl_chunk_bytes += n_collect;
785 fsh_cached_bytes_add (fsh, n_collect);
787 clib_spinlock_unlock (&fss->chunk_lock);
791 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
792 svm_fifo_chunk_t * c)
794 fifo_segment_slice_t *fss;
795 fss = fsh_slice_get (fsh, slice_index);
796 fsh_slice_collect_chunks (fsh, fss, c);
800 fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
804 fss->fifos->prev = f;
805 f->next = fss->fifos;
811 fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
813 if (f->flags & SVM_FIFO_F_LL_TRACKED)
816 f->prev->next = f->next;
818 fss->fifos = f->next;
820 f->next->prev = f->prev;
825 * Allocate fifo in fifo segment
828 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
829 u32 data_bytes, fifo_segment_ftype_t ftype)
831 fifo_segment_header_t *fsh = fs->h;
832 fifo_segment_slice_t *fss;
835 ASSERT (slice_index < fs->n_slices);
837 fss = fsh_slice_get (fsh, slice_index);
838 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
842 f->slice_index = slice_index;
844 svm_fifo_init (f, data_bytes);
846 /* If rx fifo type add to active fifos list. When cleaning up segment,
847 * we need a list of active sessions that should be disconnected. Since
848 * both rx and tx fifos keep pointers to the session, it's enough to track
850 if (ftype == FIFO_SEGMENT_RX_FIFO)
852 fss_fifo_add_active_list (fss, f);
853 f->flags |= SVM_FIFO_F_LL_TRACKED;
855 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
859 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
862 fsh_active_fifos_update (fsh, 1);
863 fss->virtual_mem += svm_fifo_size (f);
870 * Free fifo allocated in fifo segment
873 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
875 fifo_segment_header_t *fsh = fs->h;
876 fifo_segment_slice_t *fss;
878 ASSERT (f->refcnt > 0);
883 fss = fsh_slice_get (fsh, f->slice_index);
885 /* Remove from active list. Only rx fifos are tracked */
886 if (f->flags & SVM_FIFO_F_LL_TRACKED)
888 fss_fifo_del_active_list (fss, f);
889 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
892 /* Free fifo chunks */
893 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
895 f->start_chunk = f->end_chunk = 0;
896 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
898 /* not allocated on segment heap */
899 svm_fifo_free_chunk_lookup (f);
900 svm_fifo_free_ooo_data (f);
904 f->master_session_index = ~0;
905 f->master_thread_index = ~0;
908 fss->virtual_mem -= svm_fifo_size (f);
910 /* Add to free list */
911 f->next = fss->free_fifos;
915 fsh_active_fifos_update (fsh, -1);
919 fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
921 fifo_segment_slice_t *fss;
925 ASSERT (f->refcnt == 1);
927 fss = fsh_slice_get (fs->h, f->slice_index);
928 fss->virtual_mem -= svm_fifo_size (f);
929 if (f->flags & SVM_FIFO_F_LL_TRACKED)
930 fss_fifo_del_active_list (fss, f);
935 fl_index = fs_freelist_for_size (c->length);
936 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
942 fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
945 fifo_segment_slice_t *fss;
949 f->slice_index = slice_index;
950 fss = fsh_slice_get (fs->h, f->slice_index);
951 fss->virtual_mem += svm_fifo_size (f);
952 if (f->flags & SVM_FIFO_F_LL_TRACKED)
953 fss_fifo_add_active_list (fss, f);
958 fl_index = fs_freelist_for_size (c->length);
959 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
965 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
968 fifo_segment_header_t *fsh = fs->h;
969 fifo_segment_slice_t *fss;
976 fss = fsh_slice_get (fsh, slice_index);
977 size = (uword) (sizeof (*f)) * batch_size;
979 oldheap = ssvm_push_heap (fsh->ssvm_sh);
980 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
981 0 /* align_offset */ ,
982 0 /* os_out_of_memory */ );
983 ssvm_pop_heap (oldheap);
989 /* Carve fifo + chunk space */
990 for (i = 0; i < batch_size; i++)
992 f = (svm_fifo_t *) fmem;
993 memset (f, 0, sizeof (*f));
994 f->next = fss->free_fifos;
999 fsh_free_bytes_sub (fsh, size);
1005 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1006 u32 chunk_size, u32 batch_size)
1008 fifo_segment_header_t *fsh = fs->h;
1009 u32 rounded_data_size, fl_index;
1010 fifo_segment_slice_t *fss;
1011 svm_fifo_chunk_t *c;
1017 if (!fs_chunk_size_is_valid (fsh, chunk_size))
1019 clib_warning ("chunk size out of range %d", chunk_size);
1023 fl_index = fs_freelist_for_size (chunk_size);
1024 rounded_data_size = fs_freelist_index_to_size (fl_index);
1025 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
1027 oldheap = ssvm_push_heap (fsh->ssvm_sh);
1028 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
1029 0 /* align_offset */ ,
1030 0 /* os_out_of_memory */ );
1031 ssvm_pop_heap (oldheap);
1033 /* Out of space.. */
1037 fss = fsh_slice_get (fsh, slice_index);
1039 /* Carve fifo + chunk space */
1040 for (i = 0; i < batch_size; i++)
1042 c = (svm_fifo_chunk_t *) cmem;
1044 c->length = rounded_data_size;
1045 c->next = fss->free_chunks[fl_index];
1046 fss->free_chunks[fl_index] = c;
1047 cmem += sizeof (*c) + rounded_data_size;
1048 fsh_cached_bytes_add (fsh, rounded_data_size);
1051 fss->num_chunks[fl_index] += batch_size;
1052 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
1053 fsh_free_bytes_sub (fsh, size);
1059 * Pre-allocates fifo pairs in fifo segment
1062 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1063 u32 rx_fifo_size, u32 tx_fifo_size,
1066 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
1067 u32 hdrs, pairs_per_slice, alloc_now;
1068 fifo_segment_header_t *fsh = fs->h;
1069 int rx_fl_index, tx_fl_index, i;
1070 fifo_segment_slice_t *fss;
1071 uword space_available;
1073 /* Parameter check */
1074 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1077 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
1079 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1083 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
1085 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1089 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
1090 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
1091 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
1092 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
1094 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
1096 /* Calculate space requirements */
1097 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
1098 space_available = fsh_free_space (fsh);
1099 pairs_to_alloc = space_available / pair_size;
1100 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
1101 pairs_per_slice = pairs_to_alloc / fs->n_slices;
1102 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
1104 if (!pairs_per_slice)
1107 for (i = 0; i < fs->n_slices; i++)
1109 fss = fsh_slice_get (fsh, i);
1110 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1111 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1112 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1113 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1114 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
1116 /* Account for the pairs allocated */
1117 *n_fifo_pairs -= alloc_now;
1122 * Get number of active fifos
1125 fifo_segment_num_fifos (fifo_segment_t * fs)
1127 return fsh_n_active_fifos (fs->h);
1131 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1136 f = fss->free_fifos;
1149 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1151 fifo_segment_header_t *fsh = fs->h;
1152 fifo_segment_slice_t *fss;
1156 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1158 fss = fsh_slice_get (fsh, slice_index);
1159 count += fs_slice_num_free_fifos (fss);
1165 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1167 u32 count = 0, rounded_size, fl_index;
1168 svm_fifo_chunk_t *c;
1171 /* Count all free chunks? */
1174 for (i = 0; i < vec_len (fss->free_chunks); i++)
1176 c = fss->free_chunks[i];
1189 rounded_size = (1 << (max_log2 (size)));
1190 fl_index = fs_freelist_for_size (rounded_size);
1192 if (fl_index >= vec_len (fss->free_chunks))
1195 c = fss->free_chunks[fl_index];
1208 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1210 fifo_segment_header_t *fsh = fs->h;
1211 fifo_segment_slice_t *fss;
1215 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1217 fss = fsh_slice_get (fsh, slice_index);
1218 count += fs_slice_num_free_chunks (fss, size);
1224 fifo_segment_update_free_bytes (fifo_segment_t * fs)
1226 fsh_update_free_bytes (fs->h);
1230 fifo_segment_size (fifo_segment_t * fs)
1232 return fs->ssvm.ssvm_size;
1236 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1238 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1242 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1244 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1248 fifo_segment_free_bytes (fifo_segment_t * fs)
1250 return fsh_n_free_bytes (fs->h);
1254 fifo_segment_cached_bytes (fifo_segment_t * fs)
1256 return fsh_n_cached_bytes (fs->h);
1260 fifo_segment_available_bytes (fifo_segment_t * fs)
1262 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1266 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1268 fifo_segment_header_t *fsh = fs->h;
1269 fifo_segment_slice_t *fss;
1273 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1275 fss = fsh_slice_get (fsh, slice_index);
1276 n_bytes += fss->n_fl_chunk_bytes;
1283 fifo_segment_has_fifos (fifo_segment_t * fs)
1285 return (fsh_n_active_fifos (fs->h) != 0);
1289 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1291 fifo_segment_header_t *fsh = fs->h;
1292 fifo_segment_slice_t *fss;
1294 fss = fsh_slice_get (fsh, slice_index);
1299 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1303 size = fifo_segment_size (fs);
1305 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1306 return (in_use * 100) / size;
1309 fifo_segment_mem_status_t
1310 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1312 if (!fsh->high_watermark || !fsh->low_watermark)
1313 return MEMORY_PRESSURE_NO_PRESSURE;
1315 /* once the no-memory is detected, the status continues
1316 * until memory usage gets below the high watermark
1318 if (fsh_has_reached_mem_limit (fsh))
1320 if (usage >= fsh->high_watermark)
1321 return MEMORY_PRESSURE_NO_MEMORY;
1323 fsh_reset_mem_limit (fsh);
1326 if (usage >= fsh->high_watermark)
1327 return MEMORY_PRESSURE_HIGH_PRESSURE;
1329 else if (usage >= fsh->low_watermark)
1330 return MEMORY_PRESSURE_LOW_PRESSURE;
1332 return MEMORY_PRESSURE_NO_PRESSURE;
1335 fifo_segment_mem_status_t
1336 fifo_segment_get_mem_status (fifo_segment_t * fs)
1338 fifo_segment_header_t *fsh = fs->h;
1339 u8 usage = fifo_segment_get_mem_usage (fs);
1341 return fifo_segment_determine_status (fsh, usage);
1345 format_fifo_segment_type (u8 * s, va_list * args)
1348 sp = va_arg (*args, fifo_segment_t *);
1349 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1351 if (st == SSVM_SEGMENT_PRIVATE)
1352 s = format (s, "%s", "private-heap");
1353 else if (st == SSVM_SEGMENT_MEMFD)
1354 s = format (s, "%s", "memfd");
1355 else if (st == SSVM_SEGMENT_SHM)
1356 s = format (s, "%s", "shm");
1358 s = format (s, "%s", "unknown");
1363 * Segment format function
1366 format_fifo_segment (u8 * s, va_list * args)
1368 u32 count, indent, active_fifos, free_fifos;
1369 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1370 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1371 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1372 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1373 uword tracked_cached_bytes;
1374 uword fifo_hdr = 0, reserved;
1375 fifo_segment_header_t *fsh;
1376 fifo_segment_slice_t *fss;
1377 svm_fifo_chunk_t *c;
1382 uword allocated, in_use, virt;
1384 fifo_segment_mem_status_t mem_st;
1386 indent = format_get_indent (s) + 2;
1390 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1391 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1395 fifo_segment_info (fs, &address, &size);
1396 active_fifos = fifo_segment_num_fifos (fs);
1397 free_fifos = fifo_segment_num_free_fifos (fs);
1399 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1400 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1401 free_fifos, address);
1408 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1411 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1414 s = format (s, "\n");
1416 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1418 fss = fsh_slice_get (fsh, slice_index);
1419 for (i = 0; i < vec_len (fss->free_chunks); i++)
1421 c = fss->free_chunks[i];
1422 if (c == 0 && fss->num_chunks[i] == 0)
1431 chunk_size = fs_freelist_index_to_size (i);
1432 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1433 chunk_size >> 10, count, fss->num_chunks[i]);
1435 chunk_bytes += count * chunk_size;
1439 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1440 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1441 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1442 fifo_segment_update_free_bytes (fs);
1443 free_seg_bytes = fifo_segment_free_bytes (fs);
1444 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1445 allocated = fifo_segment_size (fs);
1446 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1447 usage = (100.0 * in_use) / allocated;
1448 mem_st = fifo_segment_get_mem_status (fs);
1449 virt = fsh_virtual_mem (fsh);
1450 reserved = fsh->n_reserved_bytes;
1452 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1453 " %U (%lu)\n", format_white_space, indent + 2,
1454 format_memory_size, free_seg_bytes, free_seg_bytes,
1455 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1456 format_memory_size, reserved, reserved);
1457 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1458 " %U (%lu)\n", format_white_space, indent + 2,
1459 format_memory_size, chunk_bytes, chunk_bytes,
1460 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1461 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1462 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1463 format_white_space, indent + 2, fsh->n_active_fifos,
1464 format_memory_size, fifo_hdr, fifo_hdr);
1465 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1466 format_white_space, indent + 2, usage, format_memory_size,
1467 in_use, format_memory_size, allocated, format_memory_size, virt,
1468 fifo_segment_mem_status_strings[mem_st]);
1469 s = format (s, "\n");
1475 * fd.io coding-style-patch-verification: ON
1478 * eval: (c-set-style "gnu")