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>
19 * Fifo segment free space
21 * Queries the underlying memory manager, dlmalloc, for free space. Since this
22 * ends up walking the internal data structures, it should not be called
25 * @param fs fifo segment
26 * @return number of free bytes
29 fsh_free_space (fifo_segment_header_t * fsh)
31 struct dlmallinfo dlminfo;
33 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
34 return dlminfo.fordblks;
38 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
40 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
44 fsh_n_free_bytes (fifo_segment_header_t * fsh)
46 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
47 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
51 fsh_update_free_bytes (fifo_segment_header_t * fsh)
53 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
57 fsh_check_mem (fifo_segment_header_t * fsh)
61 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
64 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
65 2 * fsh->n_reserved_bytes);
66 if (fsh->n_free_bytes > thresh)
69 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
70 fsh_update_free_bytes (fsh);
73 static inline fifo_segment_slice_t *
74 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
76 return &fsh->slices[slice_index];
80 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
82 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
86 * Initialize fifo segment shared header
89 fifo_segment_init (fifo_segment_t * fs)
91 fifo_segment_header_t *fsh;
92 fifo_segment_slice_t *fss;
93 ssvm_shared_header_t *sh;
94 u32 max_chunk_sz, max_chunks;
100 oldheap = ssvm_push_heap (sh);
103 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
104 * Long story made short: the "process-private" fifo segment
105 * is allocated from the main heap, not mmapped. dlmalloc
106 * only guarantees 4-byte alignment, and on aarch64
107 * the fsh can end up 4-byte but not 8-byte aligned.
108 * That eventually causes the atomic op in fifo_segment_update_free_bytes
111 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
112 clib_memset (fsh, 0, sizeof (*fsh));
113 fs->h = sh->opaque[0] = fsh;
114 fs->n_slices = clib_max (fs->n_slices, 1);
116 fsh->ssvm_sh = fs->ssvm.sh;
117 fsh->n_slices = fs->n_slices;
118 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
119 FIFO_SEGMENT_MAX_FIFO_SIZE);
120 fsh->max_log2_chunk_size = max_log2 (max_fifo);
122 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
123 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
124 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
126 for (i = 0; i < fs->n_slices; i++)
128 fss = fsh_slice_get (fsh, i);
129 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
130 clib_spinlock_init (&fss->chunk_lock);
133 ssvm_pop_heap (oldheap);
135 fsh->n_free_bytes = fsh_free_space (fsh);
136 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
137 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
143 * Create a fifo segment and initialize as master
146 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
152 /* Allocate a fresh segment */
153 pool_get_zero (sm->segments, fs);
155 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
156 fs->ssvm.ssvm_size = a->segment_size;
157 fs->ssvm.i_am_master = 1;
158 fs->ssvm.my_pid = getpid ();
159 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
160 fs->ssvm.requested_va = baseva;
162 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
164 pool_put (sm->segments, fs);
168 /* Note: requested_va updated due to seg base addr randomization */
169 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
171 fifo_segment_init (fs);
172 vec_add1 (a->new_segment_indices, fs - sm->segments);
177 * Attach as slave to a fifo segment
180 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
185 pool_get_zero (sm->segments, fs);
187 fs->ssvm.ssvm_size = a->segment_size;
188 fs->ssvm.my_pid = getpid ();
189 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
190 fs->ssvm.requested_va = sm->next_baseva;
191 if (a->segment_type == SSVM_SEGMENT_MEMFD)
192 fs->ssvm.fd = a->memfd_fd;
194 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
196 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
198 _vec_len (fs) = vec_len (fs) - 1;
202 /* Fish the segment header */
203 fs->h = fs->ssvm.sh->opaque[0];
205 vec_add1 (a->new_segment_indices, fs - sm->segments);
210 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
212 ssvm_delete (&s->ssvm);
213 clib_memset (s, 0xfe, sizeof (*s));
214 pool_put (sm->segments, s);
218 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
220 return s - sm->segments;
224 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
226 return pool_elt_at_index (sm->segments, segment_index);
230 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
232 *address = (char *) seg->ssvm.sh->ssvm_va;
233 *size = seg->ssvm.ssvm_size;
237 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
238 u32 timeout_in_seconds)
240 sm->next_baseva = baseva;
241 sm->timeout_in_seconds = timeout_in_seconds;
245 fs_freelist_for_size (u32 size)
247 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
249 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
253 fs_freelist_index_to_size (u32 fl_index)
255 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
259 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
262 * 4K minimum. It's not likely that anything good will happen
263 * with a smaller FIFO.
265 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
266 && size <= (1 << fsh->max_log2_chunk_size);
270 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss,
271 u32 fl_index, u32 data_bytes)
277 c = fss->free_chunks[fl_index];
282 fss->free_fifos = f->next;
283 fss->free_chunks[fl_index] = c->next;
286 memset (f, 0, sizeof (*f));
290 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
295 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
296 fifo_segment_slice_t * fss,
299 svm_fifo_chunk_t *c, *first = 0, *last = 0;
300 u32 fl_index, fl_size, n_alloc = 0;
306 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
307 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
308 ssvm_pop_heap (oldheap);
311 memset (f, 0, sizeof (*f));
312 fsh_free_bytes_sub (fsh, sizeof (*f));
316 fss->free_fifos = f->next;
319 fl_index = fs_freelist_for_size (data_bytes);
323 fl_size = fs_freelist_index_to_size (fl_index);
327 c = fss->free_chunks[fl_index];
330 fss->free_chunks[fl_index] = c->next;
336 data_bytes -= c->length;
340 /* Failed to allocate with smaller chunks */
343 /* free all chunks if any allocated */
347 fl_index = fs_freelist_for_size (c->length);
348 fl_size = fs_freelist_index_to_size (fl_index);
349 c->next = fss->free_chunks[fl_index];
350 fss->free_chunks[fl_index] = c;
351 fss->n_fl_chunk_bytes += fl_size;
352 data_bytes += fl_size;
355 fl_index = fs_freelist_for_size (data_bytes);
356 if (fss->free_chunks[fl_index + 1])
359 fl_size = fs_freelist_index_to_size (fl_index);
363 f->next = fss->free_fifos;
368 fl_size = fl_size >> 1;
372 f->start_chunk = first;
374 fss->n_fl_chunk_bytes -= n_alloc;
379 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
380 fifo_segment_slice_t * fss,
381 u32 fl_index, u32 batch_size)
383 u32 hdrs, rounded_data_size;
391 rounded_data_size = fs_freelist_index_to_size (fl_index);
392 hdrs = sizeof (*f) + sizeof (*c);
393 size = (uword) (hdrs + rounded_data_size) * batch_size;
395 oldheap = ssvm_push_heap (fsh->ssvm_sh);
396 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
397 0 /* align_offset */ ,
398 0 /* os_out_of_memory */ );
399 ssvm_pop_heap (oldheap);
405 /* Carve fifo + chunk space */
406 for (i = 0; i < batch_size; i++)
408 f = (svm_fifo_t *) fmem;
409 memset (f, 0, sizeof (*f));
410 f->next = fss->free_fifos;
412 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
414 c->length = rounded_data_size;
415 c->enq_rb_index = RBTREE_TNIL_INDEX;
416 c->deq_rb_index = RBTREE_TNIL_INDEX;
417 c->next = fss->free_chunks[fl_index];
418 fss->free_chunks[fl_index] = c;
419 fmem += hdrs + rounded_data_size;
422 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
423 fsh_free_bytes_sub (fsh, size);
429 * Try to allocate new fifo
431 * Tries the following steps in order:
432 * - grab fifo and chunk from freelists
433 * - batch fifo and chunk allocation
434 * - single fifo allocation
435 * - grab multiple fifo chunks from freelists
438 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
441 u32 fifo_sz, fl_index;
445 fl_index = fs_freelist_for_size (data_bytes);
446 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
447 fifo_sz += 1 << max_log2 (data_bytes);
449 if (fss->free_fifos && fss->free_chunks[fl_index])
451 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
457 n_free_bytes = fsh_n_free_bytes (fsh);
458 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
460 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
461 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
464 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
467 if (fifo_sz <= n_free_bytes)
469 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
470 f = svm_fifo_alloc (data_bytes);
471 ssvm_pop_heap (oldheap);
474 fsh_free_bytes_sub (fsh, fifo_sz);
478 if (data_bytes <= fss->n_fl_chunk_bytes)
479 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
489 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
491 fifo_segment_slice_t *fss;
496 fl_index = fs_freelist_for_size (chunk_size);
497 fss = fsh_slice_get (fsh, slice_index);
499 clib_spinlock_lock (&fss->chunk_lock);
500 c = fss->free_chunks[fl_index];
505 chunk_size = fs_freelist_index_to_size (fl_index);
506 if (fsh_n_free_bytes (fsh) < chunk_size)
509 oldheap = ssvm_push_heap (fsh->ssvm_sh);
510 c = svm_fifo_chunk_alloc (chunk_size);
511 ssvm_pop_heap (oldheap);
516 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
520 fss->free_chunks[fl_index] = c->next;
522 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
527 clib_spinlock_unlock (&fss->chunk_lock);
533 fsh_slice_collect_chunks (fifo_segment_slice_t * fss, svm_fifo_chunk_t * cur)
535 svm_fifo_chunk_t *next;
538 clib_spinlock_lock (&fss->chunk_lock);
543 fl_index = fs_freelist_for_size (cur->length);
544 cur->next = fss->free_chunks[fl_index];
545 cur->enq_rb_index = RBTREE_TNIL_INDEX;
546 cur->deq_rb_index = RBTREE_TNIL_INDEX;
547 fss->free_chunks[fl_index] = cur;
548 fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index);
552 clib_spinlock_unlock (&fss->chunk_lock);
556 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
557 svm_fifo_chunk_t * cur)
559 fifo_segment_slice_t *fss;
560 fss = fsh_slice_get (fsh, slice_index);
561 fsh_slice_collect_chunks (fss, cur);
565 * Allocate fifo in fifo segment
568 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
569 u32 data_bytes, fifo_segment_ftype_t ftype)
571 fifo_segment_header_t *fsh = fs->h;
572 fifo_segment_slice_t *fss;
575 ASSERT (slice_index < fs->n_slices);
577 fss = fsh_slice_get (fsh, slice_index);
578 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
582 f->slice_index = slice_index;
584 svm_fifo_init (f, data_bytes);
586 /* If rx fifo type add to active fifos list. When cleaning up segment,
587 * we need a list of active sessions that should be disconnected. Since
588 * both rx and tx fifos keep pointers to the session, it's enough to track
590 if (ftype == FIFO_SEGMENT_RX_FIFO)
594 fss->fifos->prev = f;
595 f->next = fss->fifos;
598 f->flags |= SVM_FIFO_F_LL_TRACKED;
600 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
604 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
607 fsh_active_fifos_update (fsh, 1);
614 * Free fifo allocated in fifo segment
617 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
619 fifo_segment_header_t *fsh = fs->h;
620 fifo_segment_slice_t *fss;
622 ASSERT (f->refcnt > 0);
627 fss = fsh_slice_get (fsh, f->slice_index);
629 /* Remove from active list. Only rx fifos are tracked */
630 if (f->flags & SVM_FIFO_F_LL_TRACKED)
633 f->prev->next = f->next;
635 fss->fifos = f->next;
637 f->next->prev = f->prev;
638 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
641 /* Add to free list */
642 f->next = fss->free_fifos;
646 /* Free fifo chunks */
647 fsh_slice_collect_chunks (fss, f->start_chunk);
649 f->start_chunk = f->end_chunk = 0;
650 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
652 /* not allocated on segment heap */
653 svm_fifo_free_chunk_lookup (f);
654 svm_fifo_free_ooo_data (f);
658 f->master_session_index = ~0;
659 f->master_thread_index = ~0;
662 fsh_active_fifos_update (fsh, -1);
666 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
669 fifo_segment_header_t *fsh = fs->h;
670 fifo_segment_slice_t *fss;
677 fss = fsh_slice_get (fsh, slice_index);
678 size = (uword) (sizeof (*f)) * batch_size;
680 oldheap = ssvm_push_heap (fsh->ssvm_sh);
681 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
682 0 /* align_offset */ ,
683 0 /* os_out_of_memory */ );
684 ssvm_pop_heap (oldheap);
690 /* Carve fifo + chunk space */
691 for (i = 0; i < batch_size; i++)
693 f = (svm_fifo_t *) fmem;
694 memset (f, 0, sizeof (*f));
695 f->next = fss->free_fifos;
700 fsh_free_bytes_sub (fsh, size);
706 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
707 u32 chunk_size, u32 batch_size)
709 fifo_segment_header_t *fsh = fs->h;
710 u32 rounded_data_size, fl_index;
711 fifo_segment_slice_t *fss;
718 if (!fs_chunk_size_is_valid (fsh, chunk_size))
720 clib_warning ("chunk size out of range %d", chunk_size);
724 fl_index = fs_freelist_for_size (chunk_size);
725 rounded_data_size = fs_freelist_index_to_size (fl_index);
726 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
728 oldheap = ssvm_push_heap (fsh->ssvm_sh);
729 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
730 0 /* align_offset */ ,
731 0 /* os_out_of_memory */ );
732 ssvm_pop_heap (oldheap);
738 fss = fsh_slice_get (fsh, slice_index);
740 /* Carve fifo + chunk space */
741 for (i = 0; i < batch_size; i++)
743 c = (svm_fifo_chunk_t *) cmem;
745 c->length = rounded_data_size;
746 c->next = fss->free_chunks[fl_index];
747 fss->free_chunks[fl_index] = c;
748 cmem += sizeof (*c) + rounded_data_size;
751 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
752 fsh_free_bytes_sub (fsh, size);
758 * Pre-allocates fifo pairs in fifo segment
761 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
762 u32 rx_fifo_size, u32 tx_fifo_size,
765 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
766 u32 hdrs, pairs_per_slice, alloc_now;
767 fifo_segment_header_t *fsh = fs->h;
768 int rx_fl_index, tx_fl_index, i;
769 fifo_segment_slice_t *fss;
770 uword space_available;
772 /* Parameter check */
773 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
776 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
778 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
782 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
784 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
788 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
789 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
790 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
791 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
793 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
795 /* Calculate space requirements */
796 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
797 space_available = fsh_free_space (fsh);
798 pairs_to_alloc = space_available / pair_size;
799 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
800 pairs_per_slice = pairs_to_alloc / fs->n_slices;
801 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
803 if (!pairs_per_slice)
806 for (i = 0; i < fs->n_slices; i++)
808 fss = fsh_slice_get (fsh, i);
809 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
810 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
811 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
812 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
813 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
815 /* Account for the pairs allocated */
816 *n_fifo_pairs -= alloc_now;
821 * Get number of active fifos
824 fifo_segment_num_fifos (fifo_segment_t * fs)
826 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
830 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
848 fifo_segment_num_free_fifos (fifo_segment_t * fs)
850 fifo_segment_header_t *fsh = fs->h;
851 fifo_segment_slice_t *fss;
855 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
857 fss = fsh_slice_get (fsh, slice_index);
858 count += fs_slice_num_free_fifos (fss);
864 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
866 u32 count = 0, rounded_size, fl_index;
870 /* Count all free chunks? */
873 for (i = 0; i < vec_len (fss->free_chunks); i++)
875 c = fss->free_chunks[i];
888 rounded_size = (1 << (max_log2 (size)));
889 fl_index = fs_freelist_for_size (rounded_size);
891 if (fl_index >= vec_len (fss->free_chunks))
894 c = fss->free_chunks[fl_index];
907 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
909 fifo_segment_header_t *fsh = fs->h;
910 fifo_segment_slice_t *fss;
914 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
916 fss = fsh_slice_get (fsh, slice_index);
917 count += fs_slice_num_free_chunks (fss, size);
923 fifo_segment_update_free_bytes (fifo_segment_t * fs)
925 fsh_update_free_bytes (fs->h);
929 fifo_segment_free_bytes (fifo_segment_t * fs)
931 return fsh_n_free_bytes (fs->h);
935 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
937 fifo_segment_header_t *fsh = fs->h;
938 fifo_segment_slice_t *fss;
942 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
944 fss = fsh_slice_get (fsh, slice_index);
945 n_bytes += fss->n_fl_chunk_bytes;
952 fifo_segment_has_fifos (fifo_segment_t * fs)
954 fifo_segment_header_t *fsh = fs->h;
955 fifo_segment_slice_t *fss;
958 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
960 fss = fsh_slice_get (fsh, slice_index);
968 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
970 fifo_segment_header_t *fsh = fs->h;
971 fifo_segment_slice_t *fss;
973 fss = fsh_slice_get (fsh, slice_index);
978 format_fifo_segment_type (u8 * s, va_list * args)
981 sp = va_arg (*args, fifo_segment_t *);
982 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
984 if (st == SSVM_SEGMENT_PRIVATE)
985 s = format (s, "%s", "private-heap");
986 else if (st == SSVM_SEGMENT_MEMFD)
987 s = format (s, "%s", "memfd");
988 else if (st == SSVM_SEGMENT_SHM)
989 s = format (s, "%s", "shm");
991 s = format (s, "%s", "unknown");
996 * Segment format function
999 format_fifo_segment (u8 * s, va_list * args)
1001 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
1002 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1003 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1004 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1005 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1006 fifo_segment_header_t *fsh;
1007 fifo_segment_slice_t *fss;
1008 svm_fifo_chunk_t *c;
1014 indent = format_get_indent (s) + 2;
1018 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1019 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1023 fifo_segment_info (fs, &address, &size);
1024 active_fifos = fifo_segment_num_fifos (fs);
1025 free_fifos = fifo_segment_num_free_fifos (fs);
1027 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1028 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1029 free_fifos, address);
1036 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1038 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1041 s = format (s, "\n");
1043 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1045 fss = fsh_slice_get (fsh, slice_index);
1046 for (i = 0; i < vec_len (fss->free_chunks); i++)
1048 c = fss->free_chunks[i];
1058 chunk_size = fs_freelist_index_to_size (i);
1059 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1060 chunk_size >> 10, count);
1062 chunk_bytes += count * chunk_size;
1066 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1067 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1068 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1069 fifo_segment_update_free_bytes (fs);
1070 free_seg_bytes = fifo_segment_free_bytes (fs);
1072 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
1073 format_white_space, indent + 2, format_memory_size,
1074 free_seg_bytes, free_seg_bytes, format_memory_size,
1075 est_free_seg_bytes, est_free_seg_bytes);
1076 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n",
1077 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1078 chunk_bytes, format_memory_size, est_chunk_bytes,
1080 s = format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1081 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1082 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1083 fsh->n_reserved_bytes);
1084 s = format (s, "\n");
1090 * fd.io coding-style-patch-verification: ON
1093 * eval: (c-set-style "gnu")