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 char *fifo_segment_mem_status_strings[] = {
19 #define _(sym,str) str,
20 foreach_segment_mem_status
25 * Fifo segment free space
27 * Queries the underlying memory manager, dlmalloc, for free space. Since this
28 * ends up walking the internal data structures, it should not be called
31 * @param fs fifo segment
32 * @return number of free bytes
35 fsh_free_space (fifo_segment_header_t * fsh)
37 struct dlmallinfo dlminfo;
39 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
40 return dlminfo.fordblks;
44 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
46 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
50 fsh_n_free_bytes (fifo_segment_header_t * fsh)
52 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
53 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
57 fsh_update_free_bytes (fifo_segment_header_t * fsh)
59 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
63 fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
65 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
69 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
71 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
75 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
77 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
82 fsh_check_mem (fifo_segment_header_t * fsh)
86 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
89 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
90 2 * fsh->n_reserved_bytes);
91 if (fsh->n_free_bytes > thresh)
94 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
95 fsh_update_free_bytes (fsh);
98 static inline fifo_segment_slice_t *
99 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
101 return &fsh->slices[slice_index];
105 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
107 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
111 * Initialize fifo segment shared header
114 fifo_segment_init (fifo_segment_t * fs)
116 fifo_segment_header_t *fsh;
117 fifo_segment_slice_t *fss;
118 ssvm_shared_header_t *sh;
119 u32 max_chunk_sz, max_chunks;
125 oldheap = ssvm_push_heap (sh);
128 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
129 * Long story made short: the "process-private" fifo segment
130 * is allocated from the main heap, not mmapped. dlmalloc
131 * only guarantees 4-byte alignment, and on aarch64
132 * the fsh can end up 4-byte but not 8-byte aligned.
133 * That eventually causes the atomic op in fifo_segment_update_free_bytes
136 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
137 clib_memset (fsh, 0, sizeof (*fsh));
138 fs->h = sh->opaque[0] = fsh;
139 fs->n_slices = clib_max (fs->n_slices, 1);
141 fsh->ssvm_sh = fs->ssvm.sh;
142 fsh->n_slices = fs->n_slices;
143 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
144 FIFO_SEGMENT_MAX_FIFO_SIZE);
145 fsh->max_log2_chunk_size = max_log2 (max_fifo);
147 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
148 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
149 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
151 for (i = 0; i < fs->n_slices; i++)
153 fss = fsh_slice_get (fsh, i);
154 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
155 clib_spinlock_init (&fss->chunk_lock);
158 ssvm_pop_heap (oldheap);
160 fsh->n_free_bytes = fsh_free_space (fsh);
161 fsh->n_cached_bytes = 0;
162 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
163 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
169 * Create a fifo segment and initialize as master
172 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
178 /* Allocate a fresh segment */
179 pool_get_zero (sm->segments, fs);
181 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
182 fs->ssvm.ssvm_size = a->segment_size;
183 fs->ssvm.i_am_master = 1;
184 fs->ssvm.my_pid = getpid ();
185 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
186 fs->ssvm.requested_va = baseva;
188 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
190 pool_put (sm->segments, fs);
194 /* Note: requested_va updated due to seg base addr randomization */
195 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
197 fifo_segment_init (fs);
198 vec_add1 (a->new_segment_indices, fs - sm->segments);
203 * Attach as slave to a fifo segment
206 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
211 pool_get_zero (sm->segments, fs);
213 fs->ssvm.ssvm_size = a->segment_size;
214 fs->ssvm.my_pid = getpid ();
215 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
216 fs->ssvm.requested_va = sm->next_baseva;
217 if (a->segment_type == SSVM_SEGMENT_MEMFD)
218 fs->ssvm.fd = a->memfd_fd;
220 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
222 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
224 _vec_len (fs) = vec_len (fs) - 1;
228 /* Fish the segment header */
229 fs->h = fs->ssvm.sh->opaque[0];
231 vec_add1 (a->new_segment_indices, fs - sm->segments);
236 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
238 ssvm_delete (&s->ssvm);
239 clib_memset (s, 0xfe, sizeof (*s));
240 pool_put (sm->segments, s);
244 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
246 return s - sm->segments;
250 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
252 return pool_elt_at_index (sm->segments, segment_index);
256 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
258 *address = (char *) seg->ssvm.sh->ssvm_va;
259 *size = seg->ssvm.ssvm_size;
263 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
264 u32 timeout_in_seconds)
266 sm->next_baseva = baseva;
267 sm->timeout_in_seconds = timeout_in_seconds;
271 fs_freelist_for_size (u32 size)
273 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
275 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
279 fs_freelist_index_to_size (u32 fl_index)
281 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
285 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
288 * 4K minimum. It's not likely that anything good will happen
289 * with a smaller FIFO.
291 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
292 && size <= (1 << fsh->max_log2_chunk_size);
296 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
302 c = fss->free_chunks[fl_index];
307 fss->free_fifos = f->next;
308 fss->free_chunks[fl_index] = c->next;
311 memset (f, 0, sizeof (*f));
315 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
320 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
321 fifo_segment_slice_t * fss, u32 data_bytes)
323 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
324 svm_fifo_chunk_t *c, *first = 0, *next;
326 fl_index = fs_freelist_for_size (req_bytes);
330 fl_size = fs_freelist_index_to_size (fl_index);
334 c = fss->free_chunks[fl_index];
337 fss->free_chunks[fl_index] = c->next;
341 req_bytes -= clib_min (fl_size, req_bytes);
345 /* Failed to allocate with smaller chunks */
348 /* free all chunks if any allocated */
352 fl_index = fs_freelist_for_size (c->length);
353 fl_size = fs_freelist_index_to_size (fl_index);
355 c->next = fss->free_chunks[fl_index];
356 fss->free_chunks[fl_index] = c;
357 fss->n_fl_chunk_bytes += fl_size;
362 fl_index = fs_freelist_for_size (data_bytes);
363 if (fss->free_chunks[fl_index + 1])
366 fl_size = fs_freelist_index_to_size (fl_index);
373 fl_size = fl_size >> 1;
377 fss->n_fl_chunk_bytes -= n_alloc;
378 fsh_cached_bytes_sub (fsh, n_alloc);
383 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
384 fifo_segment_slice_t * fss,
387 svm_fifo_chunk_t *c, *first = 0, *last = 0;
388 u32 fl_index, fl_size, n_alloc = 0;
394 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
395 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
396 ssvm_pop_heap (oldheap);
399 memset (f, 0, sizeof (*f));
400 fsh_free_bytes_sub (fsh, sizeof (*f));
404 fss->free_fifos = f->next;
407 fl_index = fs_freelist_for_size (data_bytes);
411 fl_size = fs_freelist_index_to_size (fl_index);
415 c = fss->free_chunks[fl_index];
418 fss->free_chunks[fl_index] = c->next;
424 data_bytes -= c->length;
428 /* Failed to allocate with smaller chunks */
431 /* free all chunks if any allocated */
435 fl_index = fs_freelist_for_size (c->length);
436 fl_size = fs_freelist_index_to_size (fl_index);
437 c->next = fss->free_chunks[fl_index];
438 fss->free_chunks[fl_index] = c;
439 fss->n_fl_chunk_bytes += fl_size;
441 data_bytes += fl_size;
444 fl_index = fs_freelist_for_size (data_bytes);
445 if (fss->free_chunks[fl_index + 1])
448 fl_size = fs_freelist_index_to_size (fl_index);
452 f->next = fss->free_fifos;
457 fl_size = fl_size >> 1;
461 f->start_chunk = first;
463 fss->n_fl_chunk_bytes -= n_alloc;
464 fsh_cached_bytes_sub (fsh, n_alloc);
469 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
470 fifo_segment_slice_t * fss,
471 u32 fl_index, u32 batch_size)
473 u32 rounded_data_size;
480 rounded_data_size = fs_freelist_index_to_size (fl_index);
481 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
483 oldheap = ssvm_push_heap (fsh->ssvm_sh);
484 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
485 0 /* align_offset */ ,
486 0 /* os_out_of_memory */ );
487 ssvm_pop_heap (oldheap);
493 /* Carve fifo + chunk space */
494 for (i = 0; i < batch_size; i++)
496 c = (svm_fifo_chunk_t *) cmem;
498 c->length = rounded_data_size;
499 c->enq_rb_index = RBTREE_TNIL_INDEX;
500 c->deq_rb_index = RBTREE_TNIL_INDEX;
501 c->next = fss->free_chunks[fl_index];
502 fss->free_chunks[fl_index] = c;
503 cmem += sizeof (*c) + rounded_data_size;
506 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
507 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
508 fsh_free_bytes_sub (fsh, size);
514 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
515 fifo_segment_slice_t * fss,
516 u32 fl_index, u32 batch_size)
518 u32 hdrs, rounded_data_size;
526 rounded_data_size = fs_freelist_index_to_size (fl_index);
527 hdrs = sizeof (*f) + sizeof (*c);
528 size = (uword) (hdrs + rounded_data_size) * batch_size;
530 oldheap = ssvm_push_heap (fsh->ssvm_sh);
531 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
532 0 /* align_offset */ ,
533 0 /* os_out_of_memory */ );
534 ssvm_pop_heap (oldheap);
540 /* Carve fifo + chunk space */
541 for (i = 0; i < batch_size; i++)
543 f = (svm_fifo_t *) fmem;
544 memset (f, 0, sizeof (*f));
545 f->next = fss->free_fifos;
547 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
549 c->length = rounded_data_size;
550 c->enq_rb_index = RBTREE_TNIL_INDEX;
551 c->deq_rb_index = RBTREE_TNIL_INDEX;
552 c->next = fss->free_chunks[fl_index];
553 fss->free_chunks[fl_index] = c;
554 fmem += hdrs + rounded_data_size;
557 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
558 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
559 fsh_free_bytes_sub (fsh, size);
565 * Try to allocate new fifo
567 * Tries the following steps in order:
568 * - grab fifo and chunk from freelists
569 * - batch fifo and chunk allocation
570 * - single fifo allocation
571 * - grab multiple fifo chunks from freelists
574 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
577 u32 fifo_sz, fl_index;
582 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
583 fl_index = fs_freelist_for_size (min_size);
584 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
585 fifo_sz += 1 << max_log2 (min_size);
587 if (fss->free_fifos && fss->free_chunks[fl_index])
589 f = fs_try_alloc_fifo_freelist (fss, fl_index);
592 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
598 n_free_bytes = fsh_n_free_bytes (fsh);
599 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
601 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
602 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
605 f = fs_try_alloc_fifo_freelist (fss, fl_index);
607 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
610 if (fifo_sz <= n_free_bytes)
612 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
613 f = svm_fifo_alloc (min_size);
614 ssvm_pop_heap (oldheap);
617 fsh_free_bytes_sub (fsh, fifo_sz);
621 if (data_bytes <= fss->n_fl_chunk_bytes)
622 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
632 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
634 fifo_segment_slice_t *fss;
640 fl_index = fs_freelist_for_size (chunk_size);
641 fss = fsh_slice_get (fsh, slice_index);
643 clib_spinlock_lock (&fss->chunk_lock);
645 c = fss->free_chunks[fl_index];
649 fss->free_chunks[fl_index] = c->next;
651 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
652 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
654 else if (chunk_size <= (n_free = fsh_n_free_bytes (fsh)))
658 chunk_size = fs_freelist_index_to_size (fl_index);
659 if (n_free < chunk_size)
662 oldheap = ssvm_push_heap (fsh->ssvm_sh);
663 c = svm_fifo_chunk_alloc (chunk_size);
664 ssvm_pop_heap (oldheap);
669 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
671 else if (chunk_size <= fss->n_fl_chunk_bytes)
673 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
675 else if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
677 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
681 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
682 batch = clib_min (batch + 1, n_free / min_size);
683 if (!fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
684 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
689 clib_spinlock_unlock (&fss->chunk_lock);
695 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
696 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
698 svm_fifo_chunk_t *next;
702 clib_spinlock_lock (&fss->chunk_lock);
707 fl_index = fs_freelist_for_size (c->length);
708 c->next = fss->free_chunks[fl_index];
709 c->enq_rb_index = RBTREE_TNIL_INDEX;
710 c->deq_rb_index = RBTREE_TNIL_INDEX;
711 fss->free_chunks[fl_index] = c;
712 n_collect += fs_freelist_index_to_size (fl_index);
716 fss->n_fl_chunk_bytes += n_collect;
717 fsh_cached_bytes_add (fsh, n_collect);
719 clib_spinlock_unlock (&fss->chunk_lock);
723 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
724 svm_fifo_chunk_t * c)
726 fifo_segment_slice_t *fss;
727 fss = fsh_slice_get (fsh, slice_index);
728 fsh_slice_collect_chunks (fsh, fss, c);
732 * Allocate fifo in fifo segment
735 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
736 u32 data_bytes, fifo_segment_ftype_t ftype)
738 fifo_segment_header_t *fsh = fs->h;
739 fifo_segment_slice_t *fss;
742 ASSERT (slice_index < fs->n_slices);
744 fss = fsh_slice_get (fsh, slice_index);
745 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
749 f->slice_index = slice_index;
751 svm_fifo_init (f, data_bytes);
753 /* If rx fifo type add to active fifos list. When cleaning up segment,
754 * we need a list of active sessions that should be disconnected. Since
755 * both rx and tx fifos keep pointers to the session, it's enough to track
757 if (ftype == FIFO_SEGMENT_RX_FIFO)
761 fss->fifos->prev = f;
762 f->next = fss->fifos;
765 f->flags |= SVM_FIFO_F_LL_TRACKED;
767 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
771 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
774 fsh_active_fifos_update (fsh, 1);
781 * Free fifo allocated in fifo segment
784 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
786 fifo_segment_header_t *fsh = fs->h;
787 fifo_segment_slice_t *fss;
789 ASSERT (f->refcnt > 0);
794 fss = fsh_slice_get (fsh, f->slice_index);
796 /* Remove from active list. Only rx fifos are tracked */
797 if (f->flags & SVM_FIFO_F_LL_TRACKED)
800 f->prev->next = f->next;
802 fss->fifos = f->next;
804 f->next->prev = f->prev;
805 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
808 /* Add to free list */
809 f->next = fss->free_fifos;
813 /* Free fifo chunks */
814 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
816 f->start_chunk = f->end_chunk = 0;
817 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
819 /* not allocated on segment heap */
820 svm_fifo_free_chunk_lookup (f);
821 svm_fifo_free_ooo_data (f);
825 f->master_session_index = ~0;
826 f->master_thread_index = ~0;
829 fsh_active_fifos_update (fsh, -1);
833 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
836 fifo_segment_header_t *fsh = fs->h;
837 fifo_segment_slice_t *fss;
844 fss = fsh_slice_get (fsh, slice_index);
845 size = (uword) (sizeof (*f)) * batch_size;
847 oldheap = ssvm_push_heap (fsh->ssvm_sh);
848 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
849 0 /* align_offset */ ,
850 0 /* os_out_of_memory */ );
851 ssvm_pop_heap (oldheap);
857 /* Carve fifo + chunk space */
858 for (i = 0; i < batch_size; i++)
860 f = (svm_fifo_t *) fmem;
861 memset (f, 0, sizeof (*f));
862 f->next = fss->free_fifos;
867 fsh_free_bytes_sub (fsh, size);
873 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
874 u32 chunk_size, u32 batch_size)
876 fifo_segment_header_t *fsh = fs->h;
877 u32 rounded_data_size, fl_index;
878 fifo_segment_slice_t *fss;
885 if (!fs_chunk_size_is_valid (fsh, chunk_size))
887 clib_warning ("chunk size out of range %d", chunk_size);
891 fl_index = fs_freelist_for_size (chunk_size);
892 rounded_data_size = fs_freelist_index_to_size (fl_index);
893 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
895 oldheap = ssvm_push_heap (fsh->ssvm_sh);
896 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
897 0 /* align_offset */ ,
898 0 /* os_out_of_memory */ );
899 ssvm_pop_heap (oldheap);
905 fss = fsh_slice_get (fsh, slice_index);
907 /* Carve fifo + chunk space */
908 for (i = 0; i < batch_size; i++)
910 c = (svm_fifo_chunk_t *) cmem;
912 c->length = rounded_data_size;
913 c->next = fss->free_chunks[fl_index];
914 fss->free_chunks[fl_index] = c;
915 cmem += sizeof (*c) + rounded_data_size;
916 fsh_cached_bytes_add (fsh, rounded_data_size);
919 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
920 fsh_free_bytes_sub (fsh, size);
926 * Pre-allocates fifo pairs in fifo segment
929 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
930 u32 rx_fifo_size, u32 tx_fifo_size,
933 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
934 u32 hdrs, pairs_per_slice, alloc_now;
935 fifo_segment_header_t *fsh = fs->h;
936 int rx_fl_index, tx_fl_index, i;
937 fifo_segment_slice_t *fss;
938 uword space_available;
940 /* Parameter check */
941 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
944 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
946 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
950 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
952 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
956 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
957 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
958 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
959 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
961 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
963 /* Calculate space requirements */
964 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
965 space_available = fsh_free_space (fsh);
966 pairs_to_alloc = space_available / pair_size;
967 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
968 pairs_per_slice = pairs_to_alloc / fs->n_slices;
969 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
971 if (!pairs_per_slice)
974 for (i = 0; i < fs->n_slices; i++)
976 fss = fsh_slice_get (fsh, i);
977 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
978 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
979 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
980 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
981 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
983 /* Account for the pairs allocated */
984 *n_fifo_pairs -= alloc_now;
989 * Get number of active fifos
992 fifo_segment_num_fifos (fifo_segment_t * fs)
994 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
998 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1003 f = fss->free_fifos;
1016 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1018 fifo_segment_header_t *fsh = fs->h;
1019 fifo_segment_slice_t *fss;
1023 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1025 fss = fsh_slice_get (fsh, slice_index);
1026 count += fs_slice_num_free_fifos (fss);
1032 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1034 u32 count = 0, rounded_size, fl_index;
1035 svm_fifo_chunk_t *c;
1038 /* Count all free chunks? */
1041 for (i = 0; i < vec_len (fss->free_chunks); i++)
1043 c = fss->free_chunks[i];
1056 rounded_size = (1 << (max_log2 (size)));
1057 fl_index = fs_freelist_for_size (rounded_size);
1059 if (fl_index >= vec_len (fss->free_chunks))
1062 c = fss->free_chunks[fl_index];
1075 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1077 fifo_segment_header_t *fsh = fs->h;
1078 fifo_segment_slice_t *fss;
1082 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1084 fss = fsh_slice_get (fsh, slice_index);
1085 count += fs_slice_num_free_chunks (fss, size);
1091 fifo_segment_update_free_bytes (fifo_segment_t * fs)
1093 fsh_update_free_bytes (fs->h);
1097 fifo_segment_size (fifo_segment_t * fs)
1099 return fs->ssvm.ssvm_size;
1103 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1105 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1109 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1111 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1115 fifo_segment_free_bytes (fifo_segment_t * fs)
1117 return fsh_n_free_bytes (fs->h);
1121 fifo_segment_cached_bytes (fifo_segment_t * fs)
1123 return fsh_n_cached_bytes (fs->h);
1127 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1129 fifo_segment_header_t *fsh = fs->h;
1130 fifo_segment_slice_t *fss;
1134 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1136 fss = fsh_slice_get (fsh, slice_index);
1137 n_bytes += fss->n_fl_chunk_bytes;
1144 fifo_segment_has_fifos (fifo_segment_t * fs)
1146 fifo_segment_header_t *fsh = fs->h;
1147 fifo_segment_slice_t *fss;
1150 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1152 fss = fsh_slice_get (fsh, slice_index);
1160 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1162 fifo_segment_header_t *fsh = fs->h;
1163 fifo_segment_slice_t *fss;
1165 fss = fsh_slice_get (fsh, slice_index);
1170 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1174 size = fifo_segment_size (fs);
1176 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1177 return (in_use * 100) / size;
1180 fifo_segment_mem_status_t
1181 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1183 if (!fsh->high_watermark || !fsh->low_watermark)
1184 return MEMORY_PRESSURE_NO_PRESSURE;
1186 /* once the no-memory is detected, the status continues
1187 * until memory usage gets below the high watermark
1189 if (fsh_has_reached_mem_limit (fsh))
1191 if (usage >= fsh->high_watermark)
1192 return MEMORY_PRESSURE_NO_MEMORY;
1194 fsh_reset_mem_limit (fsh);
1197 if (usage >= fsh->high_watermark)
1198 return MEMORY_PRESSURE_HIGH_PRESSURE;
1200 else if (usage >= fsh->low_watermark)
1201 return MEMORY_PRESSURE_LOW_PRESSURE;
1203 return MEMORY_PRESSURE_NO_PRESSURE;
1206 fifo_segment_mem_status_t
1207 fifo_segment_get_mem_status (fifo_segment_t * fs)
1209 fifo_segment_header_t *fsh = fs->h;
1210 u8 usage = fifo_segment_get_mem_usage (fs);
1212 return fifo_segment_determine_status (fsh, usage);
1216 format_fifo_segment_type (u8 * s, va_list * args)
1219 sp = va_arg (*args, fifo_segment_t *);
1220 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1222 if (st == SSVM_SEGMENT_PRIVATE)
1223 s = format (s, "%s", "private-heap");
1224 else if (st == SSVM_SEGMENT_MEMFD)
1225 s = format (s, "%s", "memfd");
1226 else if (st == SSVM_SEGMENT_SHM)
1227 s = format (s, "%s", "shm");
1229 s = format (s, "%s", "unknown");
1234 * Segment format function
1237 format_fifo_segment (u8 * s, va_list * args)
1239 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
1240 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1241 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1242 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1243 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1244 uword tracked_cached_bytes;
1245 fifo_segment_header_t *fsh;
1246 fifo_segment_slice_t *fss;
1247 svm_fifo_chunk_t *c;
1252 uword allocated, in_use;
1254 fifo_segment_mem_status_t mem_st;
1256 indent = format_get_indent (s) + 2;
1260 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1261 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1265 fifo_segment_info (fs, &address, &size);
1266 active_fifos = fifo_segment_num_fifos (fs);
1267 free_fifos = fifo_segment_num_free_fifos (fs);
1269 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1270 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1271 free_fifos, address);
1278 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1280 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1283 s = format (s, "\n");
1285 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1287 fss = fsh_slice_get (fsh, slice_index);
1288 for (i = 0; i < vec_len (fss->free_chunks); i++)
1290 c = fss->free_chunks[i];
1300 chunk_size = fs_freelist_index_to_size (i);
1301 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1302 chunk_size >> 10, count);
1304 chunk_bytes += count * chunk_size;
1308 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1309 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1310 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1311 fifo_segment_update_free_bytes (fs);
1312 free_seg_bytes = fifo_segment_free_bytes (fs);
1313 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1314 allocated = fifo_segment_size (fs);
1315 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1316 usage = (100.0 * in_use) / allocated;
1317 mem_st = fifo_segment_get_mem_status (fs);
1319 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
1320 format_white_space, indent + 2, format_memory_size,
1321 free_seg_bytes, free_seg_bytes, format_memory_size,
1322 est_free_seg_bytes, est_free_seg_bytes);
1325 "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n",
1326 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1327 chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes,
1328 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1330 format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1331 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1332 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1333 fsh->n_reserved_bytes);
1335 format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space,
1336 indent + 2, usage, format_memory_size, in_use, format_memory_size,
1337 allocated, fifo_segment_mem_status_strings[mem_st]);
1338 s = format (s, "\n");
1344 * fd.io coding-style-patch-verification: ON
1347 * eval: (c-set-style "gnu")