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, uword size)
68 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
72 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, uword 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 fss_chunk_fl_index_is_valid (fifo_segment_slice_t * fss, u32 fl_index)
139 return (fl_index < vec_len (fss->free_chunks));
143 fss_chunk_free_list_push (fifo_segment_slice_t * fss, u32 fl_index,
144 svm_fifo_chunk_t * c)
146 clib_spinlock_lock (&fss->chunk_lock);
147 c->next = fss->free_chunks[fl_index];
148 fss->free_chunks[fl_index] = c;
149 clib_spinlock_unlock (&fss->chunk_lock);
153 fss_chunk_free_list_push_list (fifo_segment_slice_t * fss, u32 fl_index,
154 svm_fifo_chunk_t * head,
155 svm_fifo_chunk_t * tail)
157 clib_spinlock_lock (&fss->chunk_lock);
158 tail->next = fss->free_chunks[fl_index];
159 fss->free_chunks[fl_index] = head;
160 clib_spinlock_unlock (&fss->chunk_lock);
163 static svm_fifo_chunk_t *
164 fss_chunk_free_list_pop (fifo_segment_slice_t * fss, u32 fl_index)
168 ASSERT (fss_chunk_fl_index_is_valid (fss, fl_index));
170 clib_spinlock_lock (&fss->chunk_lock);
172 if (!fss->free_chunks[fl_index])
174 clib_spinlock_unlock (&fss->chunk_lock);
178 c = fss->free_chunks[fl_index];
179 fss->free_chunks[fl_index] = c->next;
181 clib_spinlock_unlock (&fss->chunk_lock);
187 fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
191 fss->fifos->prev = f;
192 f->next = fss->fifos;
198 fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
200 if (f->flags & SVM_FIFO_F_LL_TRACKED)
203 f->prev->next = f->next;
205 fss->fifos = f->next;
207 f->next->prev = f->prev;
212 fss_fl_chunk_bytes (fifo_segment_slice_t * fss)
214 return clib_atomic_load_relax_n (&fss->n_fl_chunk_bytes);
218 fss_fl_chunk_bytes_add (fifo_segment_slice_t * fss, uword size)
220 clib_atomic_fetch_add_relax (&fss->n_fl_chunk_bytes, size);
224 fss_fl_chunk_bytes_sub (fifo_segment_slice_t * fss, uword size)
226 clib_atomic_fetch_sub_relax (&fss->n_fl_chunk_bytes, size);
230 * Initialize fifo segment shared header
233 fifo_segment_init (fifo_segment_t * fs)
235 fifo_segment_header_t *fsh;
236 fifo_segment_slice_t *fss;
237 ssvm_shared_header_t *sh;
244 oldheap = ssvm_push_heap (sh);
247 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
248 * Long story made short: the "process-private" fifo segment
249 * is allocated from the main heap, not mmapped. dlmalloc
250 * only guarantees 4-byte alignment, and on aarch64
251 * the fsh can end up 4-byte but not 8-byte aligned.
252 * That eventually causes the atomic op in fifo_segment_update_free_bytes
255 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
256 clib_memset (fsh, 0, sizeof (*fsh));
257 fs->h = sh->opaque[0] = fsh;
258 fs->n_slices = clib_max (fs->n_slices, 1);
260 fsh->ssvm_sh = fs->ssvm.sh;
261 fsh->n_slices = fs->n_slices;
262 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
263 FIFO_SEGMENT_MAX_FIFO_SIZE);
264 fsh->max_log2_chunk_size = max_log2 (max_fifo);
266 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
267 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
268 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
270 for (i = 0; i < fs->n_slices; i++)
272 fss = fsh_slice_get (fsh, i);
273 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
274 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
275 clib_spinlock_init (&fss->chunk_lock);
278 ssvm_pop_heap (oldheap);
280 fsh->n_free_bytes = fsh_free_space (fsh);
281 fsh->n_cached_bytes = 0;
282 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
288 * Create a fifo segment and initialize as master
291 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
297 /* Allocate a fresh segment */
298 pool_get_zero (sm->segments, fs);
300 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
301 fs->ssvm.ssvm_size = a->segment_size;
302 fs->ssvm.is_server = 1;
303 fs->ssvm.my_pid = getpid ();
304 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
305 fs->ssvm.requested_va = baseva;
307 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
309 pool_put (sm->segments, fs);
313 /* Note: requested_va updated due to seg base addr randomization */
314 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
316 fifo_segment_init (fs);
317 vec_add1 (a->new_segment_indices, fs - sm->segments);
322 * Attach as slave to a fifo segment
325 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
330 pool_get_zero (sm->segments, fs);
332 fs->ssvm.ssvm_size = a->segment_size;
333 fs->ssvm.my_pid = getpid ();
334 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
335 fs->ssvm.requested_va = sm->next_baseva;
336 if (a->segment_type == SSVM_SEGMENT_MEMFD)
337 fs->ssvm.fd = a->memfd_fd;
339 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
341 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
343 _vec_len (fs) = vec_len (fs) - 1;
347 /* Fish the segment header */
348 fs->h = fs->ssvm.sh->opaque[0];
350 vec_add1 (a->new_segment_indices, fs - sm->segments);
355 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
357 ssvm_delete (&s->ssvm);
358 clib_memset (s, 0xfe, sizeof (*s));
359 pool_put (sm->segments, s);
363 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
365 return s - sm->segments;
369 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
371 return pool_elt_at_index (sm->segments, segment_index);
375 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
377 *address = (char *) seg->ssvm.sh->ssvm_va;
378 *size = seg->ssvm.ssvm_size;
382 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
383 u32 timeout_in_seconds)
385 sm->next_baseva = baseva;
386 sm->timeout_in_seconds = timeout_in_seconds;
390 fs_freelist_for_size (u32 size)
392 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
394 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
398 fs_freelist_index_to_size (u32 fl_index)
400 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
404 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
407 * 4K minimum. It's not likely that anything good will happen
408 * with a smaller FIFO.
410 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
411 && size <= (1ULL << fsh->max_log2_chunk_size);
415 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
416 fifo_segment_slice_t * fss, u32 data_bytes)
418 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
419 svm_fifo_chunk_t *c, *first = 0, *next;
421 fl_index = fs_freelist_for_size (req_bytes);
425 fl_size = fs_freelist_index_to_size (fl_index);
429 c = fss_chunk_free_list_pop (fss, fl_index);
435 req_bytes -= clib_min (fl_size, req_bytes);
439 /* Failed to allocate with smaller chunks */
442 /* Free all chunks if any allocated */
446 fl_index = fs_freelist_for_size (c->length);
448 fss_chunk_free_list_push (fss, fl_index, c);
453 /* As last attempt, try allocating a chunk larger than
454 * the requested size, if possible */
455 fl_index = fs_freelist_for_size (data_bytes) + 1;
456 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
458 first = fss_chunk_free_list_pop (fss, fl_index);
462 n_alloc = fs_freelist_index_to_size (fl_index);
468 fl_size = fl_size >> 1;
473 fss_fl_chunk_bytes_sub (fss, n_alloc);
474 fsh_cached_bytes_sub (fsh, n_alloc);
479 fsh_try_alloc_fifo_hdr_batch (fifo_segment_header_t * fsh,
480 fifo_segment_slice_t * fss, u32 batch_size)
488 size = (uword) sizeof (*f) * batch_size;
490 oldheap = ssvm_push_heap (fsh->ssvm_sh);
491 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
492 0 /* align_offset */ ,
493 0 /* os_out_of_memory */ );
494 ssvm_pop_heap (oldheap);
500 /* Carve fifo hdr space */
501 for (i = 0; i < batch_size; i++)
503 f = (svm_fifo_t *) fmem;
504 memset (f, 0, sizeof (*f));
505 f->next = fss->free_fifos;
514 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
515 fifo_segment_slice_t * fss,
516 u32 fl_index, u32 batch_size)
518 svm_fifo_chunk_t *c, *head = 0, *tail;
519 uword size, total_chunk_bytes;
520 u32 rounded_data_size;
525 rounded_data_size = fs_freelist_index_to_size (fl_index);
526 total_chunk_bytes = (uword) batch_size *rounded_data_size;
527 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
529 oldheap = ssvm_push_heap (fsh->ssvm_sh);
530 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
531 0 /* align_offset */ ,
532 0 /* os_out_of_memory */ );
533 ssvm_pop_heap (oldheap);
539 /* Carve fifo + chunk space */
540 tail = c = (svm_fifo_chunk_t *) cmem;
541 for (i = 0; i < batch_size; i++)
544 c->length = rounded_data_size;
547 cmem += sizeof (*c) + rounded_data_size;
548 c = (svm_fifo_chunk_t *) cmem;
551 fss_chunk_free_list_push_list (fss, fl_index, head, tail);
552 fss->num_chunks[fl_index] += batch_size;
553 fss_fl_chunk_bytes_add (fss, total_chunk_bytes);
554 fsh_cached_bytes_add (fsh, total_chunk_bytes);
555 fsh_free_bytes_sub (fsh, size);
561 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
562 fifo_segment_slice_t * fss,
563 u32 fl_index, u32 batch_size)
565 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
567 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
571 fsh_try_alloc_fifo_hdr (fifo_segment_header_t * fsh,
572 fifo_segment_slice_t * fss)
576 if (!fss->free_fifos)
578 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
579 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
584 fss->free_fifos = f->next;
585 memset (f, 0, sizeof (*f));
589 static svm_fifo_chunk_t *
590 fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
591 fifo_segment_slice_t * fss, u32 data_bytes)
596 fl_index = fs_freelist_for_size (data_bytes);
599 c = fss_chunk_free_list_pop (fss, fl_index);
603 fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
604 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
608 u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
611 chunk_size = fs_freelist_index_to_size (fl_index);
612 n_free = fsh_n_free_bytes (fsh);
614 if (chunk_size <= n_free)
616 batch = chunk_size * batch <= n_free ? batch : 1;
617 if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
620 /* Failed to allocate larger chunk, try to allocate multi-chunk
621 * that is close to what was actually requested */
622 if (data_bytes <= fss_fl_chunk_bytes (fss))
624 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
627 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
628 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
634 if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
636 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
638 batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
639 batch = clib_min (batch + 1, n_free / min_size);
640 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
645 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
655 * Try to allocate new fifo
657 * Tries the following steps in order:
658 * - grab fifo and chunk from freelists
659 * - batch fifo and chunk allocation
660 * - single fifo allocation
661 * - grab multiple fifo chunks from freelists
664 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
667 u32 fl_index, min_size;
671 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
672 fl_index = fs_freelist_for_size (min_size);
674 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
677 f = fsh_try_alloc_fifo_hdr (fsh, fss);
681 c = fsh_try_alloc_chunk (fsh, fss, min_size);
684 f->next = fss->free_fifos;
693 f->size = data_bytes;
700 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
702 fifo_segment_slice_t *fss;
705 fss = fsh_slice_get (fsh, slice_index);
706 c = fsh_try_alloc_chunk (fsh, fss, chunk_size);
712 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
713 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
715 u32 n_collect = 0, fl_index;
716 svm_fifo_chunk_t *next;
720 CLIB_MEM_UNPOISON (c, sizeof (*c));
722 fl_index = fs_freelist_for_size (c->length);
723 fss_chunk_free_list_push (fss, fl_index, c);
724 n_collect += fs_freelist_index_to_size (fl_index);
728 fss_fl_chunk_bytes_add (fss, n_collect);
729 fsh_cached_bytes_add (fsh, n_collect);
733 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
734 svm_fifo_chunk_t * c)
736 fifo_segment_slice_t *fss;
737 fss = fsh_slice_get (fsh, slice_index);
738 fsh_slice_collect_chunks (fsh, fss, c);
742 * Allocate fifo in fifo segment
745 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
746 u32 data_bytes, fifo_segment_ftype_t ftype)
748 fifo_segment_header_t *fsh = fs->h;
749 fifo_segment_slice_t *fss;
752 ASSERT (slice_index < fs->n_slices);
754 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
757 fss = fsh_slice_get (fsh, slice_index);
758 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
762 f->slice_index = slice_index;
764 svm_fifo_init (f, data_bytes);
766 /* If rx fifo type add to active fifos list. When cleaning up segment,
767 * we need a list of active sessions that should be disconnected. Since
768 * both rx and tx fifos keep pointers to the session, it's enough to track
770 if (ftype == FIFO_SEGMENT_RX_FIFO)
772 fss_fifo_add_active_list (fss, f);
773 f->flags |= SVM_FIFO_F_LL_TRACKED;
776 fsh_active_fifos_update (fsh, 1);
777 fss->virtual_mem += svm_fifo_size (f);
784 * Free fifo allocated in fifo segment
787 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
789 fifo_segment_header_t *fsh = fs->h;
790 fifo_segment_slice_t *fss;
792 ASSERT (f->refcnt > 0);
797 fss = fsh_slice_get (fsh, f->slice_index);
799 /* Remove from active list. Only rx fifos are tracked */
800 if (f->flags & SVM_FIFO_F_LL_TRACKED)
802 fss_fifo_del_active_list (fss, f);
803 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
806 /* Free fifo chunks */
807 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
809 f->start_chunk = f->end_chunk = 0;
810 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
812 /* not allocated on segment heap */
813 svm_fifo_free_chunk_lookup (f);
814 svm_fifo_free_ooo_data (f);
818 f->master_session_index = ~0;
819 f->master_thread_index = ~0;
822 fss->virtual_mem -= svm_fifo_size (f);
824 /* Add to free list */
825 f->next = fss->free_fifos;
829 fsh_active_fifos_update (fsh, -1);
833 fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
835 fifo_segment_slice_t *fss;
839 ASSERT (f->refcnt == 1);
841 fss = fsh_slice_get (fs->h, f->slice_index);
842 fss->virtual_mem -= svm_fifo_size (f);
843 if (f->flags & SVM_FIFO_F_LL_TRACKED)
844 fss_fifo_del_active_list (fss, f);
849 fl_index = fs_freelist_for_size (c->length);
850 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
856 fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
859 fifo_segment_slice_t *fss;
863 f->slice_index = slice_index;
864 fss = fsh_slice_get (fs->h, f->slice_index);
865 fss->virtual_mem += svm_fifo_size (f);
866 if (f->flags & SVM_FIFO_F_LL_TRACKED)
867 fss_fifo_add_active_list (fss, f);
872 fl_index = fs_freelist_for_size (c->length);
873 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
879 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
882 fifo_segment_header_t *fsh = fs->h;
883 fifo_segment_slice_t *fss;
890 fss = fsh_slice_get (fsh, slice_index);
891 size = (uword) (sizeof (*f)) * batch_size;
893 oldheap = ssvm_push_heap (fsh->ssvm_sh);
894 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
895 0 /* align_offset */ ,
896 0 /* os_out_of_memory */ );
897 ssvm_pop_heap (oldheap);
903 /* Carve fifo + chunk space */
904 for (i = 0; i < batch_size; i++)
906 f = (svm_fifo_t *) fmem;
907 memset (f, 0, sizeof (*f));
908 f->next = fss->free_fifos;
913 fsh_free_bytes_sub (fsh, size);
919 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
920 u32 chunk_size, u32 batch_size)
922 fifo_segment_header_t *fsh = fs->h;
923 fifo_segment_slice_t *fss;
926 if (!fs_chunk_size_is_valid (fsh, chunk_size))
928 clib_warning ("chunk size out of range %d", chunk_size);
932 fl_index = fs_freelist_for_size (chunk_size);
933 fss = fsh_slice_get (fsh, slice_index);
935 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
939 * Pre-allocates fifo pairs in fifo segment
942 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
943 u32 rx_fifo_size, u32 tx_fifo_size,
946 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
947 u32 hdrs, pairs_per_slice, alloc_now;
948 fifo_segment_header_t *fsh = fs->h;
949 int rx_fl_index, tx_fl_index, i;
950 fifo_segment_slice_t *fss;
951 uword space_available;
953 /* Parameter check */
954 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
957 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
959 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
963 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
965 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
969 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
970 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
971 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
972 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
974 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
976 /* Calculate space requirements */
977 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
978 space_available = fsh_free_space (fsh);
979 pairs_to_alloc = space_available / pair_size;
980 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
981 pairs_per_slice = pairs_to_alloc / fs->n_slices;
982 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
984 if (!pairs_per_slice)
987 for (i = 0; i < fs->n_slices; i++)
989 fss = fsh_slice_get (fsh, i);
990 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
991 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
992 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
993 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
994 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
996 /* Account for the pairs allocated */
997 *n_fifo_pairs -= alloc_now;
1002 * Get number of active fifos
1005 fifo_segment_num_fifos (fifo_segment_t * fs)
1007 return fsh_n_active_fifos (fs->h);
1011 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1016 f = fss->free_fifos;
1029 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1031 fifo_segment_header_t *fsh = fs->h;
1032 fifo_segment_slice_t *fss;
1036 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1038 fss = fsh_slice_get (fsh, slice_index);
1039 count += fs_slice_num_free_fifos (fss);
1045 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1047 u32 count = 0, rounded_size, fl_index;
1048 svm_fifo_chunk_t *c;
1051 /* Count all free chunks? */
1054 for (i = 0; i < vec_len (fss->free_chunks); i++)
1056 c = fss->free_chunks[i];
1069 rounded_size = (1 << (max_log2 (size)));
1070 fl_index = fs_freelist_for_size (rounded_size);
1072 if (fl_index >= vec_len (fss->free_chunks))
1075 c = fss->free_chunks[fl_index];
1088 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1090 fifo_segment_header_t *fsh = fs->h;
1091 fifo_segment_slice_t *fss;
1095 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1097 fss = fsh_slice_get (fsh, slice_index);
1098 count += fs_slice_num_free_chunks (fss, size);
1104 fifo_segment_update_free_bytes (fifo_segment_t * fs)
1106 fsh_update_free_bytes (fs->h);
1110 fifo_segment_size (fifo_segment_t * fs)
1112 return fs->ssvm.ssvm_size;
1116 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1118 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1122 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1124 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1128 fifo_segment_free_bytes (fifo_segment_t * fs)
1130 return fsh_n_free_bytes (fs->h);
1134 fifo_segment_cached_bytes (fifo_segment_t * fs)
1136 return fsh_n_cached_bytes (fs->h);
1140 fifo_segment_available_bytes (fifo_segment_t * fs)
1142 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1146 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1148 fifo_segment_header_t *fsh = fs->h;
1149 fifo_segment_slice_t *fss;
1153 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1155 fss = fsh_slice_get (fsh, slice_index);
1156 n_bytes += fss_fl_chunk_bytes (fss);
1163 fifo_segment_has_fifos (fifo_segment_t * fs)
1165 return (fsh_n_active_fifos (fs->h) != 0);
1169 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1171 fifo_segment_header_t *fsh = fs->h;
1172 fifo_segment_slice_t *fss;
1174 fss = fsh_slice_get (fsh, slice_index);
1179 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1183 size = fifo_segment_size (fs);
1185 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1186 return (in_use * 100) / size;
1189 fifo_segment_mem_status_t
1190 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1192 if (!fsh->high_watermark || !fsh->low_watermark)
1193 return MEMORY_PRESSURE_NO_PRESSURE;
1195 /* once the no-memory is detected, the status continues
1196 * until memory usage gets below the high watermark
1198 if (fsh_has_reached_mem_limit (fsh))
1200 if (usage >= fsh->high_watermark)
1201 return MEMORY_PRESSURE_NO_MEMORY;
1203 fsh_reset_mem_limit (fsh);
1206 if (usage >= fsh->high_watermark)
1207 return MEMORY_PRESSURE_HIGH_PRESSURE;
1209 else if (usage >= fsh->low_watermark)
1210 return MEMORY_PRESSURE_LOW_PRESSURE;
1212 return MEMORY_PRESSURE_NO_PRESSURE;
1215 fifo_segment_mem_status_t
1216 fifo_segment_get_mem_status (fifo_segment_t * fs)
1218 fifo_segment_header_t *fsh = fs->h;
1219 u8 usage = fifo_segment_get_mem_usage (fs);
1221 return fifo_segment_determine_status (fsh, usage);
1225 format_fifo_segment_type (u8 * s, va_list * args)
1228 sp = va_arg (*args, fifo_segment_t *);
1229 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1231 if (st == SSVM_SEGMENT_PRIVATE)
1232 s = format (s, "%s", "private");
1233 else if (st == SSVM_SEGMENT_MEMFD)
1234 s = format (s, "%s", "memfd");
1235 else if (st == SSVM_SEGMENT_SHM)
1236 s = format (s, "%s", "shm");
1238 s = format (s, "%s", "unknown");
1243 * Segment format function
1246 format_fifo_segment (u8 * s, va_list * args)
1248 u32 count, indent, active_fifos, free_fifos;
1249 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1250 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1251 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1252 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1253 uword tracked_cached_bytes;
1254 uword fifo_hdr = 0, reserved;
1255 fifo_segment_header_t *fsh;
1256 fifo_segment_slice_t *fss;
1257 svm_fifo_chunk_t *c;
1262 uword allocated, in_use, virt;
1264 fifo_segment_mem_status_t mem_st;
1266 indent = format_get_indent (s) + 2;
1270 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
1271 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1275 fifo_segment_info (fs, &address, &size);
1276 active_fifos = fifo_segment_num_fifos (fs);
1277 free_fifos = fifo_segment_num_free_fifos (fs);
1279 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1280 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1281 free_fifos, address);
1288 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1291 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1294 s = format (s, "\n");
1296 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1298 fss = fsh_slice_get (fsh, slice_index);
1299 for (i = 0; i < vec_len (fss->free_chunks); i++)
1301 c = fss->free_chunks[i];
1302 if (c == 0 && fss->num_chunks[i] == 0)
1311 chunk_size = fs_freelist_index_to_size (i);
1312 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1313 chunk_size >> 10, count, fss->num_chunks[i]);
1315 chunk_bytes += count * chunk_size;
1319 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1320 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1321 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1322 fifo_segment_update_free_bytes (fs);
1323 free_seg_bytes = fifo_segment_free_bytes (fs);
1324 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1325 allocated = fifo_segment_size (fs);
1326 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1327 usage = (100.0 * in_use) / allocated;
1328 mem_st = fifo_segment_get_mem_status (fs);
1329 virt = fsh_virtual_mem (fsh);
1330 reserved = fsh->n_reserved_bytes;
1332 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1333 " %U (%lu)\n", format_white_space, indent + 2,
1334 format_memory_size, free_seg_bytes, free_seg_bytes,
1335 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1336 format_memory_size, reserved, reserved);
1337 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1338 " %U (%lu)\n", format_white_space, indent + 2,
1339 format_memory_size, chunk_bytes, chunk_bytes,
1340 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1341 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1342 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1343 format_white_space, indent + 2, fsh->n_active_fifos,
1344 format_memory_size, fifo_hdr, fifo_hdr);
1345 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1346 format_white_space, indent + 2, usage, format_memory_size,
1347 in_use, format_memory_size, allocated, format_memory_size, virt,
1348 fifo_segment_mem_status_strings[mem_st]);
1349 s = format (s, "\n");
1355 * fd.io coding-style-patch-verification: ON
1358 * eval: (c-set-style "gnu")