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_virtual_mem (fifo_segment_header_t * fsh)
96 fifo_segment_slice_t *fss;
100 for (i = 0; i < fsh->n_slices; i++)
102 fss = fsh_slice_get (fsh, i);
103 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
109 fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
112 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
113 fss->virtual_mem += n_bytes;
117 fsh_check_mem (fifo_segment_header_t * fsh)
121 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
124 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
125 2 * fsh->n_reserved_bytes);
126 if (fsh->n_free_bytes > thresh)
129 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
130 fsh_update_free_bytes (fsh);
134 * Initialize fifo segment shared header
137 fifo_segment_init (fifo_segment_t * fs)
139 fifo_segment_header_t *fsh;
140 fifo_segment_slice_t *fss;
141 ssvm_shared_header_t *sh;
148 oldheap = ssvm_push_heap (sh);
151 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
152 * Long story made short: the "process-private" fifo segment
153 * is allocated from the main heap, not mmapped. dlmalloc
154 * only guarantees 4-byte alignment, and on aarch64
155 * the fsh can end up 4-byte but not 8-byte aligned.
156 * That eventually causes the atomic op in fifo_segment_update_free_bytes
159 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
160 clib_memset (fsh, 0, sizeof (*fsh));
161 fs->h = sh->opaque[0] = fsh;
162 fs->n_slices = clib_max (fs->n_slices, 1);
164 fsh->ssvm_sh = fs->ssvm.sh;
165 fsh->n_slices = fs->n_slices;
166 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
167 FIFO_SEGMENT_MAX_FIFO_SIZE);
168 fsh->max_log2_chunk_size = max_log2 (max_fifo);
170 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
171 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
172 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
174 for (i = 0; i < fs->n_slices; i++)
176 fss = fsh_slice_get (fsh, i);
177 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
178 clib_spinlock_init (&fss->chunk_lock);
181 ssvm_pop_heap (oldheap);
183 fsh->n_free_bytes = fsh_free_space (fsh);
184 fsh->n_cached_bytes = 0;
185 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
191 * Create a fifo segment and initialize as master
194 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
200 /* Allocate a fresh segment */
201 pool_get_zero (sm->segments, fs);
203 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
204 fs->ssvm.ssvm_size = a->segment_size;
205 fs->ssvm.i_am_master = 1;
206 fs->ssvm.my_pid = getpid ();
207 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
208 fs->ssvm.requested_va = baseva;
210 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
212 pool_put (sm->segments, fs);
216 /* Note: requested_va updated due to seg base addr randomization */
217 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
219 fifo_segment_init (fs);
220 vec_add1 (a->new_segment_indices, fs - sm->segments);
225 * Attach as slave to a fifo segment
228 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
233 pool_get_zero (sm->segments, fs);
235 fs->ssvm.ssvm_size = a->segment_size;
236 fs->ssvm.my_pid = getpid ();
237 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
238 fs->ssvm.requested_va = sm->next_baseva;
239 if (a->segment_type == SSVM_SEGMENT_MEMFD)
240 fs->ssvm.fd = a->memfd_fd;
242 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
244 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
246 _vec_len (fs) = vec_len (fs) - 1;
250 /* Fish the segment header */
251 fs->h = fs->ssvm.sh->opaque[0];
253 vec_add1 (a->new_segment_indices, fs - sm->segments);
258 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
260 ssvm_delete (&s->ssvm);
261 clib_memset (s, 0xfe, sizeof (*s));
262 pool_put (sm->segments, s);
266 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
268 return s - sm->segments;
272 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
274 return pool_elt_at_index (sm->segments, segment_index);
278 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
280 *address = (char *) seg->ssvm.sh->ssvm_va;
281 *size = seg->ssvm.ssvm_size;
285 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
286 u32 timeout_in_seconds)
288 sm->next_baseva = baseva;
289 sm->timeout_in_seconds = timeout_in_seconds;
293 fs_freelist_for_size (u32 size)
295 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
297 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
301 fs_freelist_index_to_size (u32 fl_index)
303 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
307 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
310 * 4K minimum. It's not likely that anything good will happen
311 * with a smaller FIFO.
313 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
314 && size <= (1 << fsh->max_log2_chunk_size);
318 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
324 c = fss->free_chunks[fl_index];
329 fss->free_fifos = f->next;
330 fss->free_chunks[fl_index] = c->next;
333 memset (f, 0, sizeof (*f));
337 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
342 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
343 fifo_segment_slice_t * fss, u32 data_bytes)
345 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
346 svm_fifo_chunk_t *c, *first = 0, *next;
348 fl_index = fs_freelist_for_size (req_bytes);
352 fl_size = fs_freelist_index_to_size (fl_index);
356 c = fss->free_chunks[fl_index];
359 fss->free_chunks[fl_index] = c->next;
363 req_bytes -= clib_min (fl_size, req_bytes);
367 /* Failed to allocate with smaller chunks */
370 /* free all chunks if any allocated */
374 fl_index = fs_freelist_for_size (c->length);
375 fl_size = fs_freelist_index_to_size (fl_index);
377 c->next = fss->free_chunks[fl_index];
378 fss->free_chunks[fl_index] = c;
379 fss->n_fl_chunk_bytes += fl_size;
384 fl_index = fs_freelist_for_size (data_bytes);
385 if (fss->free_chunks[fl_index + 1])
388 fl_size = fs_freelist_index_to_size (fl_index);
395 fl_size = fl_size >> 1;
399 fss->n_fl_chunk_bytes -= n_alloc;
400 fsh_cached_bytes_sub (fsh, n_alloc);
405 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
406 fifo_segment_slice_t * fss,
409 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
410 u32 fl_index, fl_size, n_alloc = 0;
416 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
417 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
418 ssvm_pop_heap (oldheap);
421 memset (f, 0, sizeof (*f));
422 fsh_free_bytes_sub (fsh, sizeof (*f));
426 fss->free_fifos = f->next;
429 fl_index = fs_freelist_for_size (data_bytes);
433 fl_size = fs_freelist_index_to_size (fl_index);
437 c = fss->free_chunks[fl_index];
440 fss->free_chunks[fl_index] = c->next;
446 data_bytes -= clib_min (fl_size, data_bytes);
450 /* Failed to allocate with smaller chunks */
453 /* free all chunks if any allocated */
457 fl_index = fs_freelist_for_size (c->length);
458 fl_size = fs_freelist_index_to_size (fl_index);
460 c->next = fss->free_chunks[fl_index];
461 fss->free_chunks[fl_index] = c;
462 fss->n_fl_chunk_bytes += fl_size;
464 data_bytes += fl_size;
468 fl_index = fs_freelist_for_size (data_bytes);
469 if (fss->free_chunks[fl_index + 1])
472 fl_size = fs_freelist_index_to_size (fl_index);
476 f->next = fss->free_fifos;
481 fl_size = fl_size >> 1;
485 f->start_chunk = first;
487 fss->n_fl_chunk_bytes -= n_alloc;
488 fsh_cached_bytes_sub (fsh, n_alloc);
493 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
494 fifo_segment_slice_t * fss,
495 u32 fl_index, u32 batch_size)
497 u32 rounded_data_size;
504 rounded_data_size = fs_freelist_index_to_size (fl_index);
505 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
507 oldheap = ssvm_push_heap (fsh->ssvm_sh);
508 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
509 0 /* align_offset */ ,
510 0 /* os_out_of_memory */ );
511 ssvm_pop_heap (oldheap);
517 /* Carve fifo + chunk space */
518 for (i = 0; i < batch_size; i++)
520 c = (svm_fifo_chunk_t *) cmem;
522 c->length = rounded_data_size;
523 c->enq_rb_index = RBTREE_TNIL_INDEX;
524 c->deq_rb_index = RBTREE_TNIL_INDEX;
525 c->next = fss->free_chunks[fl_index];
526 fss->free_chunks[fl_index] = c;
527 cmem += sizeof (*c) + rounded_data_size;
530 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
531 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
532 fsh_free_bytes_sub (fsh, size);
538 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
539 fifo_segment_slice_t * fss,
540 u32 fl_index, u32 batch_size)
542 u32 hdrs, rounded_data_size;
550 rounded_data_size = fs_freelist_index_to_size (fl_index);
551 hdrs = sizeof (*f) + sizeof (*c);
552 size = (uword) (hdrs + rounded_data_size) * batch_size;
554 oldheap = ssvm_push_heap (fsh->ssvm_sh);
555 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
556 0 /* align_offset */ ,
557 0 /* os_out_of_memory */ );
558 ssvm_pop_heap (oldheap);
564 /* Carve fifo + chunk space */
565 for (i = 0; i < batch_size; i++)
567 f = (svm_fifo_t *) fmem;
568 memset (f, 0, sizeof (*f));
569 f->next = fss->free_fifos;
571 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
573 c->length = rounded_data_size;
574 c->enq_rb_index = RBTREE_TNIL_INDEX;
575 c->deq_rb_index = RBTREE_TNIL_INDEX;
576 c->next = fss->free_chunks[fl_index];
577 fss->free_chunks[fl_index] = c;
578 fmem += hdrs + rounded_data_size;
581 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
582 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
583 fsh_free_bytes_sub (fsh, size);
589 * Try to allocate new fifo
591 * Tries the following steps in order:
592 * - grab fifo and chunk from freelists
593 * - batch fifo and chunk allocation
594 * - single fifo allocation
595 * - grab multiple fifo chunks from freelists
598 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
601 u32 fifo_sz, fl_index;
606 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
607 fl_index = fs_freelist_for_size (min_size);
609 clib_spinlock_lock (&fss->chunk_lock);
611 if (fss->free_fifos && fss->free_chunks[fl_index])
613 f = fs_try_alloc_fifo_freelist (fss, fl_index);
616 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
621 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
622 fifo_sz += 1 << max_log2 (min_size);
623 n_free_bytes = fsh_n_free_bytes (fsh);
625 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
627 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
628 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
630 f = fs_try_alloc_fifo_freelist (fss, fl_index);
633 fsh_cached_bytes_sub (fsh,
634 fs_freelist_index_to_size (fl_index));
641 n_free_bytes = fsh_n_free_bytes (fsh);
644 if (fifo_sz <= n_free_bytes)
646 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
647 f = svm_fifo_alloc (min_size);
648 ssvm_pop_heap (oldheap);
651 fsh_free_bytes_sub (fsh, fifo_sz);
656 /* All failed, try to allocate min of data bytes and fifo sz */
657 fifo_sz = clib_min (fifo_sz, data_bytes);
658 if (fifo_sz <= fss->n_fl_chunk_bytes)
659 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
662 clib_spinlock_unlock (&fss->chunk_lock);
666 f->size = data_bytes;
673 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
675 fifo_segment_slice_t *fss;
679 fl_index = fs_freelist_for_size (chunk_size);
680 fss = fsh_slice_get (fsh, slice_index);
682 clib_spinlock_lock (&fss->chunk_lock);
684 c = fss->free_chunks[fl_index];
688 fss->free_chunks[fl_index] = c->next;
690 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
691 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
699 chunk_size = fs_freelist_index_to_size (fl_index);
700 n_free = fsh_n_free_bytes (fsh);
702 if (chunk_size <= n_free)
704 oldheap = ssvm_push_heap (fsh->ssvm_sh);
705 c = svm_fifo_chunk_alloc (chunk_size);
706 ssvm_pop_heap (oldheap);
710 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
715 n_free = fsh_n_free_bytes (fsh);
717 if (chunk_size <= fss->n_fl_chunk_bytes)
719 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
722 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
723 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
729 if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
731 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
733 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
734 batch = clib_min (batch + 1, n_free / min_size);
735 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
740 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
746 clib_spinlock_unlock (&fss->chunk_lock);
752 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
753 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
755 svm_fifo_chunk_t *next;
759 clib_spinlock_lock (&fss->chunk_lock);
764 fl_index = fs_freelist_for_size (c->length);
765 c->next = fss->free_chunks[fl_index];
766 c->enq_rb_index = RBTREE_TNIL_INDEX;
767 c->deq_rb_index = RBTREE_TNIL_INDEX;
768 fss->free_chunks[fl_index] = c;
769 n_collect += fs_freelist_index_to_size (fl_index);
773 fss->n_fl_chunk_bytes += n_collect;
774 fsh_cached_bytes_add (fsh, n_collect);
776 clib_spinlock_unlock (&fss->chunk_lock);
780 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
781 svm_fifo_chunk_t * c)
783 fifo_segment_slice_t *fss;
784 fss = fsh_slice_get (fsh, slice_index);
785 fsh_slice_collect_chunks (fsh, fss, c);
789 * Allocate fifo in fifo segment
792 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
793 u32 data_bytes, fifo_segment_ftype_t ftype)
795 fifo_segment_header_t *fsh = fs->h;
796 fifo_segment_slice_t *fss;
799 ASSERT (slice_index < fs->n_slices);
801 fss = fsh_slice_get (fsh, slice_index);
802 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
806 f->slice_index = slice_index;
808 svm_fifo_init (f, data_bytes);
810 /* If rx fifo type add to active fifos list. When cleaning up segment,
811 * we need a list of active sessions that should be disconnected. Since
812 * both rx and tx fifos keep pointers to the session, it's enough to track
814 if (ftype == FIFO_SEGMENT_RX_FIFO)
818 fss->fifos->prev = f;
819 f->next = fss->fifos;
822 f->flags |= SVM_FIFO_F_LL_TRACKED;
824 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
828 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
831 fsh_active_fifos_update (fsh, 1);
832 fss->virtual_mem += svm_fifo_size (f);
839 * Free fifo allocated in fifo segment
842 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
844 fifo_segment_header_t *fsh = fs->h;
845 fifo_segment_slice_t *fss;
847 ASSERT (f->refcnt > 0);
852 fss = fsh_slice_get (fsh, f->slice_index);
854 /* Remove from active list. Only rx fifos are tracked */
855 if (f->flags & SVM_FIFO_F_LL_TRACKED)
858 f->prev->next = f->next;
860 fss->fifos = f->next;
862 f->next->prev = f->prev;
863 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
866 /* Free fifo chunks */
867 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
869 f->start_chunk = f->end_chunk = 0;
870 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
872 /* not allocated on segment heap */
873 svm_fifo_free_chunk_lookup (f);
874 svm_fifo_free_ooo_data (f);
878 f->master_session_index = ~0;
879 f->master_thread_index = ~0;
882 fss->virtual_mem -= svm_fifo_size (f);
884 /* Add to free list */
885 f->next = fss->free_fifos;
889 fsh_active_fifos_update (fsh, -1);
893 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
896 fifo_segment_header_t *fsh = fs->h;
897 fifo_segment_slice_t *fss;
904 fss = fsh_slice_get (fsh, slice_index);
905 size = (uword) (sizeof (*f)) * batch_size;
907 oldheap = ssvm_push_heap (fsh->ssvm_sh);
908 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
909 0 /* align_offset */ ,
910 0 /* os_out_of_memory */ );
911 ssvm_pop_heap (oldheap);
917 /* Carve fifo + chunk space */
918 for (i = 0; i < batch_size; i++)
920 f = (svm_fifo_t *) fmem;
921 memset (f, 0, sizeof (*f));
922 f->next = fss->free_fifos;
927 fsh_free_bytes_sub (fsh, size);
933 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
934 u32 chunk_size, u32 batch_size)
936 fifo_segment_header_t *fsh = fs->h;
937 u32 rounded_data_size, fl_index;
938 fifo_segment_slice_t *fss;
945 if (!fs_chunk_size_is_valid (fsh, chunk_size))
947 clib_warning ("chunk size out of range %d", chunk_size);
951 fl_index = fs_freelist_for_size (chunk_size);
952 rounded_data_size = fs_freelist_index_to_size (fl_index);
953 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
955 oldheap = ssvm_push_heap (fsh->ssvm_sh);
956 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
957 0 /* align_offset */ ,
958 0 /* os_out_of_memory */ );
959 ssvm_pop_heap (oldheap);
965 fss = fsh_slice_get (fsh, slice_index);
967 /* Carve fifo + chunk space */
968 for (i = 0; i < batch_size; i++)
970 c = (svm_fifo_chunk_t *) cmem;
972 c->length = rounded_data_size;
973 c->next = fss->free_chunks[fl_index];
974 fss->free_chunks[fl_index] = c;
975 cmem += sizeof (*c) + rounded_data_size;
976 fsh_cached_bytes_add (fsh, rounded_data_size);
979 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
980 fsh_free_bytes_sub (fsh, size);
986 * Pre-allocates fifo pairs in fifo segment
989 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
990 u32 rx_fifo_size, u32 tx_fifo_size,
993 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
994 u32 hdrs, pairs_per_slice, alloc_now;
995 fifo_segment_header_t *fsh = fs->h;
996 int rx_fl_index, tx_fl_index, i;
997 fifo_segment_slice_t *fss;
998 uword space_available;
1000 /* Parameter check */
1001 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1004 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
1006 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1010 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
1012 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1016 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
1017 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
1018 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
1019 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
1021 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
1023 /* Calculate space requirements */
1024 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
1025 space_available = fsh_free_space (fsh);
1026 pairs_to_alloc = space_available / pair_size;
1027 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
1028 pairs_per_slice = pairs_to_alloc / fs->n_slices;
1029 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
1031 if (!pairs_per_slice)
1034 for (i = 0; i < fs->n_slices; i++)
1036 fss = fsh_slice_get (fsh, i);
1037 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1038 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1039 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1040 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1041 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
1043 /* Account for the pairs allocated */
1044 *n_fifo_pairs -= alloc_now;
1049 * Get number of active fifos
1052 fifo_segment_num_fifos (fifo_segment_t * fs)
1054 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
1058 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1063 f = fss->free_fifos;
1076 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1078 fifo_segment_header_t *fsh = fs->h;
1079 fifo_segment_slice_t *fss;
1083 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1085 fss = fsh_slice_get (fsh, slice_index);
1086 count += fs_slice_num_free_fifos (fss);
1092 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1094 u32 count = 0, rounded_size, fl_index;
1095 svm_fifo_chunk_t *c;
1098 /* Count all free chunks? */
1101 for (i = 0; i < vec_len (fss->free_chunks); i++)
1103 c = fss->free_chunks[i];
1116 rounded_size = (1 << (max_log2 (size)));
1117 fl_index = fs_freelist_for_size (rounded_size);
1119 if (fl_index >= vec_len (fss->free_chunks))
1122 c = fss->free_chunks[fl_index];
1135 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1137 fifo_segment_header_t *fsh = fs->h;
1138 fifo_segment_slice_t *fss;
1142 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1144 fss = fsh_slice_get (fsh, slice_index);
1145 count += fs_slice_num_free_chunks (fss, size);
1151 fifo_segment_update_free_bytes (fifo_segment_t * fs)
1153 fsh_update_free_bytes (fs->h);
1157 fifo_segment_size (fifo_segment_t * fs)
1159 return fs->ssvm.ssvm_size;
1163 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1165 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1169 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1171 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1175 fifo_segment_free_bytes (fifo_segment_t * fs)
1177 return fsh_n_free_bytes (fs->h);
1181 fifo_segment_cached_bytes (fifo_segment_t * fs)
1183 return fsh_n_cached_bytes (fs->h);
1187 fifo_segment_available_bytes (fifo_segment_t * fs)
1189 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1193 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1195 fifo_segment_header_t *fsh = fs->h;
1196 fifo_segment_slice_t *fss;
1200 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1202 fss = fsh_slice_get (fsh, slice_index);
1203 n_bytes += fss->n_fl_chunk_bytes;
1210 fifo_segment_has_fifos (fifo_segment_t * fs)
1212 fifo_segment_header_t *fsh = fs->h;
1213 fifo_segment_slice_t *fss;
1216 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1218 fss = fsh_slice_get (fsh, slice_index);
1226 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1228 fifo_segment_header_t *fsh = fs->h;
1229 fifo_segment_slice_t *fss;
1231 fss = fsh_slice_get (fsh, slice_index);
1236 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1240 size = fifo_segment_size (fs);
1242 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1243 return (in_use * 100) / size;
1246 fifo_segment_mem_status_t
1247 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1249 if (!fsh->high_watermark || !fsh->low_watermark)
1250 return MEMORY_PRESSURE_NO_PRESSURE;
1252 /* once the no-memory is detected, the status continues
1253 * until memory usage gets below the high watermark
1255 if (fsh_has_reached_mem_limit (fsh))
1257 if (usage >= fsh->high_watermark)
1258 return MEMORY_PRESSURE_NO_MEMORY;
1260 fsh_reset_mem_limit (fsh);
1263 if (usage >= fsh->high_watermark)
1264 return MEMORY_PRESSURE_HIGH_PRESSURE;
1266 else if (usage >= fsh->low_watermark)
1267 return MEMORY_PRESSURE_LOW_PRESSURE;
1269 return MEMORY_PRESSURE_NO_PRESSURE;
1272 fifo_segment_mem_status_t
1273 fifo_segment_get_mem_status (fifo_segment_t * fs)
1275 fifo_segment_header_t *fsh = fs->h;
1276 u8 usage = fifo_segment_get_mem_usage (fs);
1278 return fifo_segment_determine_status (fsh, usage);
1282 format_fifo_segment_type (u8 * s, va_list * args)
1285 sp = va_arg (*args, fifo_segment_t *);
1286 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1288 if (st == SSVM_SEGMENT_PRIVATE)
1289 s = format (s, "%s", "private-heap");
1290 else if (st == SSVM_SEGMENT_MEMFD)
1291 s = format (s, "%s", "memfd");
1292 else if (st == SSVM_SEGMENT_SHM)
1293 s = format (s, "%s", "shm");
1295 s = format (s, "%s", "unknown");
1300 * Segment format function
1303 format_fifo_segment (u8 * s, va_list * args)
1305 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
1306 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1307 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1308 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1309 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1310 uword tracked_cached_bytes;
1311 fifo_segment_header_t *fsh;
1312 fifo_segment_slice_t *fss;
1313 svm_fifo_chunk_t *c;
1318 uword allocated, in_use, virt;
1320 fifo_segment_mem_status_t mem_st;
1322 indent = format_get_indent (s) + 2;
1326 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1327 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1331 fifo_segment_info (fs, &address, &size);
1332 active_fifos = fifo_segment_num_fifos (fs);
1333 free_fifos = fifo_segment_num_free_fifos (fs);
1335 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1336 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1337 free_fifos, address);
1344 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1346 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1349 s = format (s, "\n");
1351 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1353 fss = fsh_slice_get (fsh, slice_index);
1354 for (i = 0; i < vec_len (fss->free_chunks); i++)
1356 c = fss->free_chunks[i];
1366 chunk_size = fs_freelist_index_to_size (i);
1367 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1368 chunk_size >> 10, count);
1370 chunk_bytes += count * chunk_size;
1374 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1375 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1376 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1377 fifo_segment_update_free_bytes (fs);
1378 free_seg_bytes = fifo_segment_free_bytes (fs);
1379 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1380 allocated = fifo_segment_size (fs);
1381 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1382 usage = (100.0 * in_use) / allocated;
1383 mem_st = fifo_segment_get_mem_status (fs);
1384 virt = fsh_virtual_mem (fsh);
1386 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1387 " %U (%lu)\n", format_white_space, indent + 2,
1388 format_memory_size, free_seg_bytes, free_seg_bytes,
1389 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1390 format_memory_size, fsh->n_reserved_bytes,
1391 fsh->n_reserved_bytes);
1392 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1393 " %U (%lu)\n", format_white_space, indent + 2,
1394 format_memory_size, chunk_bytes, chunk_bytes,
1395 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1396 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1397 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1398 format_white_space, indent + 2, fsh->n_active_fifos,
1399 format_memory_size, fifo_hdr, fifo_hdr);
1400 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1401 format_white_space, indent + 2, usage, format_memory_size,
1402 in_use, format_memory_size, allocated, format_memory_size, virt,
1403 fifo_segment_mem_status_strings[mem_st]);
1404 s = format (s, "\n");
1410 * fd.io coding-style-patch-verification: ON
1413 * eval: (c-set-style "gnu")