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 return clib_mem_get_heap_free_space (fsh->ssvm_sh->heap);
47 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
49 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
53 fsh_n_free_bytes (fifo_segment_header_t * fsh)
55 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
56 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
60 fsh_update_free_bytes (fifo_segment_header_t * fsh)
62 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
66 fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
68 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
72 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
74 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
78 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
80 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
85 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
87 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
91 fsh_n_active_fifos (fifo_segment_header_t * fsh)
93 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
97 fsh_virtual_mem (fifo_segment_header_t * fsh)
99 fifo_segment_slice_t *fss;
103 for (i = 0; i < fsh->n_slices; i++)
105 fss = fsh_slice_get (fsh, i);
106 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
112 fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
115 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
116 fss->virtual_mem += n_bytes;
120 fsh_check_mem (fifo_segment_header_t * fsh)
124 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
127 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
128 2 * fsh->n_reserved_bytes);
129 if (fsh->n_free_bytes > thresh)
132 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
133 fsh_update_free_bytes (fsh);
137 * Initialize fifo segment shared header
140 fifo_segment_init (fifo_segment_t * fs)
142 fifo_segment_header_t *fsh;
143 fifo_segment_slice_t *fss;
144 ssvm_shared_header_t *sh;
151 oldheap = ssvm_push_heap (sh);
154 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
155 * Long story made short: the "process-private" fifo segment
156 * is allocated from the main heap, not mmapped. dlmalloc
157 * only guarantees 4-byte alignment, and on aarch64
158 * the fsh can end up 4-byte but not 8-byte aligned.
159 * That eventually causes the atomic op in fifo_segment_update_free_bytes
162 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
163 clib_memset (fsh, 0, sizeof (*fsh));
164 fs->h = sh->opaque[0] = fsh;
165 fs->n_slices = clib_max (fs->n_slices, 1);
167 fsh->ssvm_sh = fs->ssvm.sh;
168 fsh->n_slices = fs->n_slices;
169 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
170 FIFO_SEGMENT_MAX_FIFO_SIZE);
171 fsh->max_log2_chunk_size = max_log2 (max_fifo);
173 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
174 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
175 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
177 for (i = 0; i < fs->n_slices; i++)
179 fss = fsh_slice_get (fsh, i);
180 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
181 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
182 clib_spinlock_init (&fss->chunk_lock);
185 ssvm_pop_heap (oldheap);
187 fsh->n_free_bytes = fsh_free_space (fsh);
188 fsh->n_cached_bytes = 0;
189 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
195 * Create a fifo segment and initialize as master
198 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
204 /* Allocate a fresh segment */
205 pool_get_zero (sm->segments, fs);
207 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
208 fs->ssvm.ssvm_size = a->segment_size;
209 fs->ssvm.is_server = 1;
210 fs->ssvm.my_pid = getpid ();
211 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
212 fs->ssvm.requested_va = baseva;
214 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
216 pool_put (sm->segments, fs);
220 /* Note: requested_va updated due to seg base addr randomization */
221 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
223 fifo_segment_init (fs);
224 vec_add1 (a->new_segment_indices, fs - sm->segments);
229 * Attach as slave to a fifo segment
232 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
237 pool_get_zero (sm->segments, fs);
239 fs->ssvm.ssvm_size = a->segment_size;
240 fs->ssvm.my_pid = getpid ();
241 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
242 fs->ssvm.requested_va = sm->next_baseva;
243 if (a->segment_type == SSVM_SEGMENT_MEMFD)
244 fs->ssvm.fd = a->memfd_fd;
246 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
248 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
250 _vec_len (fs) = vec_len (fs) - 1;
254 /* Fish the segment header */
255 fs->h = fs->ssvm.sh->opaque[0];
257 vec_add1 (a->new_segment_indices, fs - sm->segments);
262 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
264 ssvm_delete (&s->ssvm);
265 clib_memset (s, 0xfe, sizeof (*s));
266 pool_put (sm->segments, s);
270 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
272 return s - sm->segments;
276 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
278 return pool_elt_at_index (sm->segments, segment_index);
282 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
284 *address = (char *) seg->ssvm.sh->ssvm_va;
285 *size = seg->ssvm.ssvm_size;
289 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
290 u32 timeout_in_seconds)
292 sm->next_baseva = baseva;
293 sm->timeout_in_seconds = timeout_in_seconds;
297 fs_freelist_for_size (u32 size)
299 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
301 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
305 fs_freelist_index_to_size (u32 fl_index)
307 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
311 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
314 * 4K minimum. It's not likely that anything good will happen
315 * with a smaller FIFO.
317 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
318 && size <= (1ULL << fsh->max_log2_chunk_size);
322 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
328 c = fss->free_chunks[fl_index];
333 fss->free_fifos = f->next;
334 fss->free_chunks[fl_index] = c->next;
337 memset (f, 0, sizeof (*f));
341 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
346 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
347 fifo_segment_slice_t * fss, u32 data_bytes)
349 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
350 svm_fifo_chunk_t *c, *first = 0, *next;
352 fl_index = fs_freelist_for_size (req_bytes);
356 fl_size = fs_freelist_index_to_size (fl_index);
360 c = fss->free_chunks[fl_index];
363 fss->free_chunks[fl_index] = c->next;
367 req_bytes -= clib_min (fl_size, req_bytes);
371 /* Failed to allocate with smaller chunks */
374 /* free all chunks if any allocated */
378 fl_index = fs_freelist_for_size (c->length);
379 fl_size = fs_freelist_index_to_size (fl_index);
381 c->next = fss->free_chunks[fl_index];
382 fss->free_chunks[fl_index] = c;
383 fss->n_fl_chunk_bytes += fl_size;
388 fl_index = fs_freelist_for_size (data_bytes);
389 if (fss->free_chunks[fl_index + 1])
392 fl_size = fs_freelist_index_to_size (fl_index);
399 fl_size = fl_size >> 1;
403 fss->n_fl_chunk_bytes -= n_alloc;
404 fsh_cached_bytes_sub (fsh, n_alloc);
409 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
410 fifo_segment_slice_t * fss,
413 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
414 u32 fl_index, fl_size, n_alloc = 0;
420 if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t)))
423 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
424 f = clib_mem_alloc_aligned_or_null (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 hdr 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;
582 /* Carve chunk space */
583 for (i = 0; i < batch_size; i++)
585 c = (svm_fifo_chunk_t *) fmem;
587 c->length = rounded_data_size;
588 c->enq_rb_index = RBTREE_TNIL_INDEX;
589 c->deq_rb_index = RBTREE_TNIL_INDEX;
590 c->next = fss->free_chunks[fl_index];
591 fss->free_chunks[fl_index] = c;
592 fmem += sizeof (svm_fifo_chunk_t) + rounded_data_size;
595 fss->num_chunks[fl_index] += batch_size;
596 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
597 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
598 fsh_free_bytes_sub (fsh, size);
604 * Try to allocate new fifo
606 * Tries the following steps in order:
607 * - grab fifo and chunk from freelists
608 * - batch fifo and chunk allocation
609 * - single fifo allocation
610 * - grab multiple fifo chunks from freelists
613 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
616 u32 fifo_sz, fl_index;
621 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
622 fl_index = fs_freelist_for_size (min_size);
624 if (fl_index >= vec_len (fss->free_chunks))
627 clib_spinlock_lock (&fss->chunk_lock);
629 if (fss->free_fifos && fss->free_chunks[fl_index])
631 f = fs_try_alloc_fifo_freelist (fss, fl_index);
634 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
639 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
640 fifo_sz += 1 << max_log2 (min_size);
641 n_free_bytes = fsh_n_free_bytes (fsh);
643 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
645 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
646 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
648 f = fs_try_alloc_fifo_freelist (fss, fl_index);
651 fsh_cached_bytes_sub (fsh,
652 fs_freelist_index_to_size (fl_index));
659 n_free_bytes = fsh_n_free_bytes (fsh);
662 if (fifo_sz <= n_free_bytes)
664 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
665 f = svm_fifo_alloc (min_size);
666 ssvm_pop_heap (oldheap);
669 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
670 fsh_free_bytes_sub (fsh, fifo_sz);
675 /* All failed, try to allocate min of data bytes and fifo sz */
676 fifo_sz = clib_min (fifo_sz, data_bytes);
677 if (fifo_sz <= fss->n_fl_chunk_bytes)
678 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
681 clib_spinlock_unlock (&fss->chunk_lock);
685 f->size = data_bytes;
692 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
694 fifo_segment_slice_t *fss;
698 fl_index = fs_freelist_for_size (chunk_size);
699 fss = fsh_slice_get (fsh, slice_index);
701 clib_spinlock_lock (&fss->chunk_lock);
703 ASSERT (vec_len (fss->free_chunks) > fl_index);
704 c = fss->free_chunks[fl_index];
708 fss->free_chunks[fl_index] = c->next;
710 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
711 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
719 chunk_size = fs_freelist_index_to_size (fl_index);
720 n_free = fsh_n_free_bytes (fsh);
722 if (chunk_size <= n_free)
724 oldheap = ssvm_push_heap (fsh->ssvm_sh);
725 c = svm_fifo_chunk_alloc (chunk_size);
726 ssvm_pop_heap (oldheap);
730 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
731 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
736 n_free = fsh_n_free_bytes (fsh);
738 if (chunk_size <= fss->n_fl_chunk_bytes)
740 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
743 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
744 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
750 if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
752 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
754 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
755 batch = clib_min (batch + 1, n_free / min_size);
756 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
761 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
767 clib_spinlock_unlock (&fss->chunk_lock);
773 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
774 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
776 svm_fifo_chunk_t *next;
780 clib_spinlock_lock (&fss->chunk_lock);
784 CLIB_MEM_UNPOISON (c, sizeof (*c));
786 fl_index = fs_freelist_for_size (c->length);
787 c->next = fss->free_chunks[fl_index];
788 c->enq_rb_index = RBTREE_TNIL_INDEX;
789 c->deq_rb_index = RBTREE_TNIL_INDEX;
790 fss->free_chunks[fl_index] = c;
791 n_collect += fs_freelist_index_to_size (fl_index);
795 fss->n_fl_chunk_bytes += n_collect;
796 fsh_cached_bytes_add (fsh, n_collect);
798 clib_spinlock_unlock (&fss->chunk_lock);
802 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
803 svm_fifo_chunk_t * c)
805 fifo_segment_slice_t *fss;
806 fss = fsh_slice_get (fsh, slice_index);
807 fsh_slice_collect_chunks (fsh, fss, c);
811 fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
815 fss->fifos->prev = f;
816 f->next = fss->fifos;
822 fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
824 if (f->flags & SVM_FIFO_F_LL_TRACKED)
827 f->prev->next = f->next;
829 fss->fifos = f->next;
831 f->next->prev = f->prev;
836 * Allocate fifo in fifo segment
839 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
840 u32 data_bytes, fifo_segment_ftype_t ftype)
842 fifo_segment_header_t *fsh = fs->h;
843 fifo_segment_slice_t *fss;
846 ASSERT (slice_index < fs->n_slices);
848 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
851 fss = fsh_slice_get (fsh, slice_index);
852 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
856 f->slice_index = slice_index;
858 svm_fifo_init (f, data_bytes);
860 /* If rx fifo type add to active fifos list. When cleaning up segment,
861 * we need a list of active sessions that should be disconnected. Since
862 * both rx and tx fifos keep pointers to the session, it's enough to track
864 if (ftype == FIFO_SEGMENT_RX_FIFO)
866 fss_fifo_add_active_list (fss, f);
867 f->flags |= SVM_FIFO_F_LL_TRACKED;
870 fsh_active_fifos_update (fsh, 1);
871 fss->virtual_mem += svm_fifo_size (f);
878 * Free fifo allocated in fifo segment
881 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
883 fifo_segment_header_t *fsh = fs->h;
884 fifo_segment_slice_t *fss;
886 ASSERT (f->refcnt > 0);
891 fss = fsh_slice_get (fsh, f->slice_index);
893 /* Remove from active list. Only rx fifos are tracked */
894 if (f->flags & SVM_FIFO_F_LL_TRACKED)
896 fss_fifo_del_active_list (fss, f);
897 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
900 /* Free fifo chunks */
901 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
903 f->start_chunk = f->end_chunk = 0;
904 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
906 /* not allocated on segment heap */
907 svm_fifo_free_chunk_lookup (f);
908 svm_fifo_free_ooo_data (f);
912 f->master_session_index = ~0;
913 f->master_thread_index = ~0;
916 fss->virtual_mem -= svm_fifo_size (f);
918 /* Add to free list */
919 f->next = fss->free_fifos;
923 fsh_active_fifos_update (fsh, -1);
927 fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
929 fifo_segment_slice_t *fss;
933 ASSERT (f->refcnt == 1);
935 fss = fsh_slice_get (fs->h, f->slice_index);
936 fss->virtual_mem -= svm_fifo_size (f);
937 if (f->flags & SVM_FIFO_F_LL_TRACKED)
938 fss_fifo_del_active_list (fss, f);
943 fl_index = fs_freelist_for_size (c->length);
944 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
950 fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
953 fifo_segment_slice_t *fss;
957 f->slice_index = slice_index;
958 fss = fsh_slice_get (fs->h, f->slice_index);
959 fss->virtual_mem += svm_fifo_size (f);
960 if (f->flags & SVM_FIFO_F_LL_TRACKED)
961 fss_fifo_add_active_list (fss, f);
966 fl_index = fs_freelist_for_size (c->length);
967 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
973 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
976 fifo_segment_header_t *fsh = fs->h;
977 fifo_segment_slice_t *fss;
984 fss = fsh_slice_get (fsh, slice_index);
985 size = (uword) (sizeof (*f)) * batch_size;
987 oldheap = ssvm_push_heap (fsh->ssvm_sh);
988 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
989 0 /* align_offset */ ,
990 0 /* os_out_of_memory */ );
991 ssvm_pop_heap (oldheap);
997 /* Carve fifo + chunk space */
998 for (i = 0; i < batch_size; i++)
1000 f = (svm_fifo_t *) fmem;
1001 memset (f, 0, sizeof (*f));
1002 f->next = fss->free_fifos;
1003 fss->free_fifos = f;
1004 fmem += sizeof (*f);
1007 fsh_free_bytes_sub (fsh, size);
1013 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1014 u32 chunk_size, u32 batch_size)
1016 fifo_segment_header_t *fsh = fs->h;
1017 u32 rounded_data_size, fl_index;
1018 fifo_segment_slice_t *fss;
1019 svm_fifo_chunk_t *c;
1025 if (!fs_chunk_size_is_valid (fsh, chunk_size))
1027 clib_warning ("chunk size out of range %d", chunk_size);
1031 fl_index = fs_freelist_for_size (chunk_size);
1032 rounded_data_size = fs_freelist_index_to_size (fl_index);
1033 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
1035 oldheap = ssvm_push_heap (fsh->ssvm_sh);
1036 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
1037 0 /* align_offset */ ,
1038 0 /* os_out_of_memory */ );
1039 ssvm_pop_heap (oldheap);
1041 /* Out of space.. */
1045 fss = fsh_slice_get (fsh, slice_index);
1047 /* Carve fifo + chunk space */
1048 for (i = 0; i < batch_size; i++)
1050 c = (svm_fifo_chunk_t *) cmem;
1052 c->length = rounded_data_size;
1053 c->next = fss->free_chunks[fl_index];
1054 fss->free_chunks[fl_index] = c;
1055 cmem += sizeof (*c) + rounded_data_size;
1056 fsh_cached_bytes_add (fsh, rounded_data_size);
1059 fss->num_chunks[fl_index] += batch_size;
1060 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
1061 fsh_free_bytes_sub (fsh, size);
1067 * Pre-allocates fifo pairs in fifo segment
1070 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1071 u32 rx_fifo_size, u32 tx_fifo_size,
1074 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
1075 u32 hdrs, pairs_per_slice, alloc_now;
1076 fifo_segment_header_t *fsh = fs->h;
1077 int rx_fl_index, tx_fl_index, i;
1078 fifo_segment_slice_t *fss;
1079 uword space_available;
1081 /* Parameter check */
1082 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1085 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
1087 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1091 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
1093 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1097 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
1098 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
1099 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
1100 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
1102 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
1104 /* Calculate space requirements */
1105 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
1106 space_available = fsh_free_space (fsh);
1107 pairs_to_alloc = space_available / pair_size;
1108 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
1109 pairs_per_slice = pairs_to_alloc / fs->n_slices;
1110 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
1112 if (!pairs_per_slice)
1115 for (i = 0; i < fs->n_slices; i++)
1117 fss = fsh_slice_get (fsh, i);
1118 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1119 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1120 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1121 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1122 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
1124 /* Account for the pairs allocated */
1125 *n_fifo_pairs -= alloc_now;
1130 * Get number of active fifos
1133 fifo_segment_num_fifos (fifo_segment_t * fs)
1135 return fsh_n_active_fifos (fs->h);
1139 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1144 f = fss->free_fifos;
1157 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1159 fifo_segment_header_t *fsh = fs->h;
1160 fifo_segment_slice_t *fss;
1164 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1166 fss = fsh_slice_get (fsh, slice_index);
1167 count += fs_slice_num_free_fifos (fss);
1173 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1175 u32 count = 0, rounded_size, fl_index;
1176 svm_fifo_chunk_t *c;
1179 /* Count all free chunks? */
1182 for (i = 0; i < vec_len (fss->free_chunks); i++)
1184 c = fss->free_chunks[i];
1197 rounded_size = (1 << (max_log2 (size)));
1198 fl_index = fs_freelist_for_size (rounded_size);
1200 if (fl_index >= vec_len (fss->free_chunks))
1203 c = fss->free_chunks[fl_index];
1216 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1218 fifo_segment_header_t *fsh = fs->h;
1219 fifo_segment_slice_t *fss;
1223 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1225 fss = fsh_slice_get (fsh, slice_index);
1226 count += fs_slice_num_free_chunks (fss, size);
1232 fifo_segment_update_free_bytes (fifo_segment_t * fs)
1234 fsh_update_free_bytes (fs->h);
1238 fifo_segment_size (fifo_segment_t * fs)
1240 return fs->ssvm.ssvm_size;
1244 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1246 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1250 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1252 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1256 fifo_segment_free_bytes (fifo_segment_t * fs)
1258 return fsh_n_free_bytes (fs->h);
1262 fifo_segment_cached_bytes (fifo_segment_t * fs)
1264 return fsh_n_cached_bytes (fs->h);
1268 fifo_segment_available_bytes (fifo_segment_t * fs)
1270 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1274 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1276 fifo_segment_header_t *fsh = fs->h;
1277 fifo_segment_slice_t *fss;
1281 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1283 fss = fsh_slice_get (fsh, slice_index);
1284 n_bytes += fss->n_fl_chunk_bytes;
1291 fifo_segment_has_fifos (fifo_segment_t * fs)
1293 return (fsh_n_active_fifos (fs->h) != 0);
1297 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1299 fifo_segment_header_t *fsh = fs->h;
1300 fifo_segment_slice_t *fss;
1302 fss = fsh_slice_get (fsh, slice_index);
1307 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1311 size = fifo_segment_size (fs);
1313 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1314 return (in_use * 100) / size;
1317 fifo_segment_mem_status_t
1318 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1320 if (!fsh->high_watermark || !fsh->low_watermark)
1321 return MEMORY_PRESSURE_NO_PRESSURE;
1323 /* once the no-memory is detected, the status continues
1324 * until memory usage gets below the high watermark
1326 if (fsh_has_reached_mem_limit (fsh))
1328 if (usage >= fsh->high_watermark)
1329 return MEMORY_PRESSURE_NO_MEMORY;
1331 fsh_reset_mem_limit (fsh);
1334 if (usage >= fsh->high_watermark)
1335 return MEMORY_PRESSURE_HIGH_PRESSURE;
1337 else if (usage >= fsh->low_watermark)
1338 return MEMORY_PRESSURE_LOW_PRESSURE;
1340 return MEMORY_PRESSURE_NO_PRESSURE;
1343 fifo_segment_mem_status_t
1344 fifo_segment_get_mem_status (fifo_segment_t * fs)
1346 fifo_segment_header_t *fsh = fs->h;
1347 u8 usage = fifo_segment_get_mem_usage (fs);
1349 return fifo_segment_determine_status (fsh, usage);
1353 format_fifo_segment_type (u8 * s, va_list * args)
1356 sp = va_arg (*args, fifo_segment_t *);
1357 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1359 if (st == SSVM_SEGMENT_PRIVATE)
1360 s = format (s, "%s", "private");
1361 else if (st == SSVM_SEGMENT_MEMFD)
1362 s = format (s, "%s", "memfd");
1363 else if (st == SSVM_SEGMENT_SHM)
1364 s = format (s, "%s", "shm");
1366 s = format (s, "%s", "unknown");
1371 * Segment format function
1374 format_fifo_segment (u8 * s, va_list * args)
1376 u32 count, indent, active_fifos, free_fifos;
1377 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1378 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1379 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1380 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1381 uword tracked_cached_bytes;
1382 uword fifo_hdr = 0, reserved;
1383 fifo_segment_header_t *fsh;
1384 fifo_segment_slice_t *fss;
1385 svm_fifo_chunk_t *c;
1390 uword allocated, in_use, virt;
1392 fifo_segment_mem_status_t mem_st;
1394 indent = format_get_indent (s) + 2;
1398 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
1399 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1403 fifo_segment_info (fs, &address, &size);
1404 active_fifos = fifo_segment_num_fifos (fs);
1405 free_fifos = fifo_segment_num_free_fifos (fs);
1407 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1408 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1409 free_fifos, address);
1416 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1419 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1422 s = format (s, "\n");
1424 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1426 fss = fsh_slice_get (fsh, slice_index);
1427 for (i = 0; i < vec_len (fss->free_chunks); i++)
1429 c = fss->free_chunks[i];
1430 if (c == 0 && fss->num_chunks[i] == 0)
1439 chunk_size = fs_freelist_index_to_size (i);
1440 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1441 chunk_size >> 10, count, fss->num_chunks[i]);
1443 chunk_bytes += count * chunk_size;
1447 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1448 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1449 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1450 fifo_segment_update_free_bytes (fs);
1451 free_seg_bytes = fifo_segment_free_bytes (fs);
1452 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1453 allocated = fifo_segment_size (fs);
1454 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1455 usage = (100.0 * in_use) / allocated;
1456 mem_st = fifo_segment_get_mem_status (fs);
1457 virt = fsh_virtual_mem (fsh);
1458 reserved = fsh->n_reserved_bytes;
1460 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1461 " %U (%lu)\n", format_white_space, indent + 2,
1462 format_memory_size, free_seg_bytes, free_seg_bytes,
1463 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1464 format_memory_size, reserved, reserved);
1465 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1466 " %U (%lu)\n", format_white_space, indent + 2,
1467 format_memory_size, chunk_bytes, chunk_bytes,
1468 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1469 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1470 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1471 format_white_space, indent + 2, fsh->n_active_fifos,
1472 format_memory_size, fifo_hdr, fifo_hdr);
1473 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1474 format_white_space, indent + 2, usage, format_memory_size,
1475 in_use, format_memory_size, allocated, format_memory_size, virt,
1476 fifo_segment_mem_status_strings[mem_st]);
1477 s = format (s, "\n");
1483 * fd.io coding-style-patch-verification: ON
1486 * eval: (c-set-style "gnu")