2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <svm/fifo_segment.h>
18 static char *fifo_segment_mem_status_strings[] = {
19 #define _(sym,str) str,
20 foreach_segment_mem_status
25 * Fifo segment free space
27 * Queries the underlying memory manager, dlmalloc, for free space. Since this
28 * ends up walking the internal data structures, it should not be called
31 * @param fs fifo segment
32 * @return number of free bytes
35 fsh_free_space (fifo_segment_header_t * fsh)
37 struct dlmallinfo dlminfo;
39 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
40 return dlminfo.fordblks;
44 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
46 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
50 fsh_n_free_bytes (fifo_segment_header_t * fsh)
52 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
53 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
57 fsh_update_free_bytes (fifo_segment_header_t * fsh)
59 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
63 fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
65 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
69 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
71 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
75 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
77 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
78 ASSERT (n_cached >= 0);
83 fsh_check_mem (fifo_segment_header_t * fsh)
87 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
90 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
91 2 * fsh->n_reserved_bytes);
92 if (fsh->n_free_bytes > thresh)
95 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
96 fsh_update_free_bytes (fsh);
99 static inline fifo_segment_slice_t *
100 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
102 return &fsh->slices[slice_index];
106 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
108 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
112 * Initialize fifo segment shared header
115 fifo_segment_init (fifo_segment_t * fs)
117 fifo_segment_header_t *fsh;
118 fifo_segment_slice_t *fss;
119 ssvm_shared_header_t *sh;
120 u32 max_chunk_sz, max_chunks;
126 oldheap = ssvm_push_heap (sh);
129 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
130 * Long story made short: the "process-private" fifo segment
131 * is allocated from the main heap, not mmapped. dlmalloc
132 * only guarantees 4-byte alignment, and on aarch64
133 * the fsh can end up 4-byte but not 8-byte aligned.
134 * That eventually causes the atomic op in fifo_segment_update_free_bytes
137 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
138 clib_memset (fsh, 0, sizeof (*fsh));
139 fs->h = sh->opaque[0] = fsh;
140 fs->n_slices = clib_max (fs->n_slices, 1);
142 fsh->ssvm_sh = fs->ssvm.sh;
143 fsh->n_slices = fs->n_slices;
144 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
145 FIFO_SEGMENT_MAX_FIFO_SIZE);
146 fsh->max_log2_chunk_size = max_log2 (max_fifo);
148 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
149 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
150 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
152 for (i = 0; i < fs->n_slices; i++)
154 fss = fsh_slice_get (fsh, i);
155 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
156 clib_spinlock_init (&fss->chunk_lock);
159 ssvm_pop_heap (oldheap);
161 fsh->n_free_bytes = fsh_free_space (fsh);
162 fsh->n_cached_bytes = 0;
163 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
164 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
170 * Create a fifo segment and initialize as master
173 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
179 /* Allocate a fresh segment */
180 pool_get_zero (sm->segments, fs);
182 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
183 fs->ssvm.ssvm_size = a->segment_size;
184 fs->ssvm.i_am_master = 1;
185 fs->ssvm.my_pid = getpid ();
186 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
187 fs->ssvm.requested_va = baseva;
189 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
191 pool_put (sm->segments, fs);
195 /* Note: requested_va updated due to seg base addr randomization */
196 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
198 fifo_segment_init (fs);
199 vec_add1 (a->new_segment_indices, fs - sm->segments);
204 * Attach as slave to a fifo segment
207 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
212 pool_get_zero (sm->segments, fs);
214 fs->ssvm.ssvm_size = a->segment_size;
215 fs->ssvm.my_pid = getpid ();
216 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
217 fs->ssvm.requested_va = sm->next_baseva;
218 if (a->segment_type == SSVM_SEGMENT_MEMFD)
219 fs->ssvm.fd = a->memfd_fd;
221 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
223 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
225 _vec_len (fs) = vec_len (fs) - 1;
229 /* Fish the segment header */
230 fs->h = fs->ssvm.sh->opaque[0];
232 vec_add1 (a->new_segment_indices, fs - sm->segments);
237 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
239 ssvm_delete (&s->ssvm);
240 clib_memset (s, 0xfe, sizeof (*s));
241 pool_put (sm->segments, s);
245 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
247 return s - sm->segments;
251 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
253 return pool_elt_at_index (sm->segments, segment_index);
257 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
259 *address = (char *) seg->ssvm.sh->ssvm_va;
260 *size = seg->ssvm.ssvm_size;
264 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
265 u32 timeout_in_seconds)
267 sm->next_baseva = baseva;
268 sm->timeout_in_seconds = timeout_in_seconds;
272 fs_freelist_for_size (u32 size)
274 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
276 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
280 fs_freelist_index_to_size (u32 fl_index)
282 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
286 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
289 * 4K minimum. It's not likely that anything good will happen
290 * with a smaller FIFO.
292 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
293 && size <= (1 << fsh->max_log2_chunk_size);
297 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss,
298 u32 fl_index, u32 data_bytes)
304 c = fss->free_chunks[fl_index];
309 fss->free_fifos = f->next;
310 fss->free_chunks[fl_index] = c->next;
313 memset (f, 0, sizeof (*f));
317 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
322 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
323 fifo_segment_slice_t * fss,
326 svm_fifo_chunk_t *c, *first = 0, *last = 0;
327 u32 fl_index, fl_size, n_alloc = 0;
333 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
334 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
335 ssvm_pop_heap (oldheap);
338 memset (f, 0, sizeof (*f));
339 fsh_free_bytes_sub (fsh, sizeof (*f));
343 fss->free_fifos = f->next;
346 fl_index = fs_freelist_for_size (data_bytes);
350 fl_size = fs_freelist_index_to_size (fl_index);
354 c = fss->free_chunks[fl_index];
357 fss->free_chunks[fl_index] = c->next;
363 data_bytes -= c->length;
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);
376 c->next = fss->free_chunks[fl_index];
377 fss->free_chunks[fl_index] = c;
378 fss->n_fl_chunk_bytes += fl_size;
380 data_bytes += fl_size;
383 fl_index = fs_freelist_for_size (data_bytes);
384 if (fss->free_chunks[fl_index + 1])
387 fl_size = fs_freelist_index_to_size (fl_index);
391 f->next = fss->free_fifos;
396 fl_size = fl_size >> 1;
400 f->start_chunk = first;
402 fss->n_fl_chunk_bytes -= n_alloc;
403 fsh_cached_bytes_sub (fsh, n_alloc);
408 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
409 fifo_segment_slice_t * fss,
410 u32 fl_index, u32 batch_size)
412 u32 hdrs, rounded_data_size;
420 rounded_data_size = fs_freelist_index_to_size (fl_index);
421 hdrs = sizeof (*f) + sizeof (*c);
422 size = (uword) (hdrs + rounded_data_size) * batch_size;
424 oldheap = ssvm_push_heap (fsh->ssvm_sh);
425 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
426 0 /* align_offset */ ,
427 0 /* os_out_of_memory */ );
428 ssvm_pop_heap (oldheap);
434 /* Carve fifo + chunk space */
435 for (i = 0; i < batch_size; i++)
437 f = (svm_fifo_t *) fmem;
438 memset (f, 0, sizeof (*f));
439 f->next = fss->free_fifos;
441 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
443 c->length = rounded_data_size;
444 c->enq_rb_index = RBTREE_TNIL_INDEX;
445 c->deq_rb_index = RBTREE_TNIL_INDEX;
446 c->next = fss->free_chunks[fl_index];
447 fss->free_chunks[fl_index] = c;
448 fmem += hdrs + rounded_data_size;
451 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
452 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
453 fsh_free_bytes_sub (fsh, size);
459 * Try to allocate new fifo
461 * Tries the following steps in order:
462 * - grab fifo and chunk from freelists
463 * - batch fifo and chunk allocation
464 * - single fifo allocation
465 * - grab multiple fifo chunks from freelists
468 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
471 u32 fifo_sz, fl_index;
475 fl_index = fs_freelist_for_size (data_bytes);
476 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
477 fifo_sz += 1 << max_log2 (data_bytes);
479 if (fss->free_fifos && fss->free_chunks[fl_index])
481 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
484 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
490 n_free_bytes = fsh_n_free_bytes (fsh);
491 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
493 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
494 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
497 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
499 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
502 if (fifo_sz <= n_free_bytes)
504 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
505 f = svm_fifo_alloc (data_bytes);
506 ssvm_pop_heap (oldheap);
509 fsh_free_bytes_sub (fsh, fifo_sz);
513 if (data_bytes <= fss->n_fl_chunk_bytes)
514 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
524 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
526 fifo_segment_slice_t *fss;
531 fl_index = fs_freelist_for_size (chunk_size);
532 fss = fsh_slice_get (fsh, slice_index);
534 clib_spinlock_lock (&fss->chunk_lock);
535 c = fss->free_chunks[fl_index];
540 chunk_size = fs_freelist_index_to_size (fl_index);
541 if (fsh_n_free_bytes (fsh) < chunk_size)
544 oldheap = ssvm_push_heap (fsh->ssvm_sh);
545 c = svm_fifo_chunk_alloc (chunk_size);
546 ssvm_pop_heap (oldheap);
551 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
555 fss->free_chunks[fl_index] = c->next;
557 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
558 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
563 clib_spinlock_unlock (&fss->chunk_lock);
569 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
570 fifo_segment_slice_t * fss, svm_fifo_chunk_t * cur)
572 svm_fifo_chunk_t *next;
576 clib_spinlock_lock (&fss->chunk_lock);
581 fl_index = fs_freelist_for_size (cur->length);
582 cur->next = fss->free_chunks[fl_index];
583 cur->enq_rb_index = RBTREE_TNIL_INDEX;
584 cur->deq_rb_index = RBTREE_TNIL_INDEX;
585 fss->free_chunks[fl_index] = cur;
586 n_collect += fs_freelist_index_to_size (fl_index);
590 fss->n_fl_chunk_bytes += n_collect;
591 fsh_cached_bytes_add (fsh, n_collect);
593 clib_spinlock_unlock (&fss->chunk_lock);
597 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
598 svm_fifo_chunk_t * cur)
600 fifo_segment_slice_t *fss;
601 fss = fsh_slice_get (fsh, slice_index);
602 fsh_slice_collect_chunks (fsh, fss, cur);
606 * Allocate fifo in fifo segment
609 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
610 u32 data_bytes, fifo_segment_ftype_t ftype)
612 fifo_segment_header_t *fsh = fs->h;
613 fifo_segment_slice_t *fss;
616 ASSERT (slice_index < fs->n_slices);
618 fss = fsh_slice_get (fsh, slice_index);
619 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
623 f->slice_index = slice_index;
625 svm_fifo_init (f, data_bytes);
627 /* If rx fifo type add to active fifos list. When cleaning up segment,
628 * we need a list of active sessions that should be disconnected. Since
629 * both rx and tx fifos keep pointers to the session, it's enough to track
631 if (ftype == FIFO_SEGMENT_RX_FIFO)
635 fss->fifos->prev = f;
636 f->next = fss->fifos;
639 f->flags |= SVM_FIFO_F_LL_TRACKED;
641 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
645 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
648 fsh_active_fifos_update (fsh, 1);
655 * Free fifo allocated in fifo segment
658 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
660 fifo_segment_header_t *fsh = fs->h;
661 fifo_segment_slice_t *fss;
663 ASSERT (f->refcnt > 0);
668 fss = fsh_slice_get (fsh, f->slice_index);
670 /* Remove from active list. Only rx fifos are tracked */
671 if (f->flags & SVM_FIFO_F_LL_TRACKED)
674 f->prev->next = f->next;
676 fss->fifos = f->next;
678 f->next->prev = f->prev;
679 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
682 /* Add to free list */
683 f->next = fss->free_fifos;
687 /* Free fifo chunks */
688 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
690 f->start_chunk = f->end_chunk = 0;
691 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
693 /* not allocated on segment heap */
694 svm_fifo_free_chunk_lookup (f);
695 svm_fifo_free_ooo_data (f);
699 f->master_session_index = ~0;
700 f->master_thread_index = ~0;
703 fsh_active_fifos_update (fsh, -1);
707 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
710 fifo_segment_header_t *fsh = fs->h;
711 fifo_segment_slice_t *fss;
718 fss = fsh_slice_get (fsh, slice_index);
719 size = (uword) (sizeof (*f)) * batch_size;
721 oldheap = ssvm_push_heap (fsh->ssvm_sh);
722 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
723 0 /* align_offset */ ,
724 0 /* os_out_of_memory */ );
725 ssvm_pop_heap (oldheap);
731 /* Carve fifo + chunk space */
732 for (i = 0; i < batch_size; i++)
734 f = (svm_fifo_t *) fmem;
735 memset (f, 0, sizeof (*f));
736 f->next = fss->free_fifos;
741 fsh_free_bytes_sub (fsh, size);
747 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
748 u32 chunk_size, u32 batch_size)
750 fifo_segment_header_t *fsh = fs->h;
751 u32 rounded_data_size, fl_index;
752 fifo_segment_slice_t *fss;
759 if (!fs_chunk_size_is_valid (fsh, chunk_size))
761 clib_warning ("chunk size out of range %d", chunk_size);
765 fl_index = fs_freelist_for_size (chunk_size);
766 rounded_data_size = fs_freelist_index_to_size (fl_index);
767 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
769 oldheap = ssvm_push_heap (fsh->ssvm_sh);
770 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
771 0 /* align_offset */ ,
772 0 /* os_out_of_memory */ );
773 ssvm_pop_heap (oldheap);
779 fss = fsh_slice_get (fsh, slice_index);
781 /* Carve fifo + chunk space */
782 for (i = 0; i < batch_size; i++)
784 c = (svm_fifo_chunk_t *) cmem;
786 c->length = rounded_data_size;
787 c->next = fss->free_chunks[fl_index];
788 fss->free_chunks[fl_index] = c;
789 cmem += sizeof (*c) + rounded_data_size;
790 fsh_cached_bytes_add (fsh, rounded_data_size);
793 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
794 fsh_free_bytes_sub (fsh, size);
800 * Pre-allocates fifo pairs in fifo segment
803 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
804 u32 rx_fifo_size, u32 tx_fifo_size,
807 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
808 u32 hdrs, pairs_per_slice, alloc_now;
809 fifo_segment_header_t *fsh = fs->h;
810 int rx_fl_index, tx_fl_index, i;
811 fifo_segment_slice_t *fss;
812 uword space_available;
814 /* Parameter check */
815 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
818 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
820 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
824 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
826 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
830 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
831 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
832 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
833 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
835 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
837 /* Calculate space requirements */
838 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
839 space_available = fsh_free_space (fsh);
840 pairs_to_alloc = space_available / pair_size;
841 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
842 pairs_per_slice = pairs_to_alloc / fs->n_slices;
843 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
845 if (!pairs_per_slice)
848 for (i = 0; i < fs->n_slices; i++)
850 fss = fsh_slice_get (fsh, i);
851 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
852 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
853 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
854 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
855 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
857 /* Account for the pairs allocated */
858 *n_fifo_pairs -= alloc_now;
863 * Get number of active fifos
866 fifo_segment_num_fifos (fifo_segment_t * fs)
868 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
872 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
890 fifo_segment_num_free_fifos (fifo_segment_t * fs)
892 fifo_segment_header_t *fsh = fs->h;
893 fifo_segment_slice_t *fss;
897 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
899 fss = fsh_slice_get (fsh, slice_index);
900 count += fs_slice_num_free_fifos (fss);
906 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
908 u32 count = 0, rounded_size, fl_index;
912 /* Count all free chunks? */
915 for (i = 0; i < vec_len (fss->free_chunks); i++)
917 c = fss->free_chunks[i];
930 rounded_size = (1 << (max_log2 (size)));
931 fl_index = fs_freelist_for_size (rounded_size);
933 if (fl_index >= vec_len (fss->free_chunks))
936 c = fss->free_chunks[fl_index];
949 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
951 fifo_segment_header_t *fsh = fs->h;
952 fifo_segment_slice_t *fss;
956 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
958 fss = fsh_slice_get (fsh, slice_index);
959 count += fs_slice_num_free_chunks (fss, size);
965 fifo_segment_update_free_bytes (fifo_segment_t * fs)
967 fsh_update_free_bytes (fs->h);
971 fifo_segment_size (fifo_segment_t * fs)
973 return fs->ssvm.ssvm_size;
977 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
979 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
983 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
985 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
989 fifo_segment_free_bytes (fifo_segment_t * fs)
991 return fsh_n_free_bytes (fs->h);
995 fifo_segment_cached_bytes (fifo_segment_t * fs)
997 return fsh_n_cached_bytes (fs->h);
1001 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1003 fifo_segment_header_t *fsh = fs->h;
1004 fifo_segment_slice_t *fss;
1008 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1010 fss = fsh_slice_get (fsh, slice_index);
1011 n_bytes += fss->n_fl_chunk_bytes;
1018 fifo_segment_has_fifos (fifo_segment_t * fs)
1020 fifo_segment_header_t *fsh = fs->h;
1021 fifo_segment_slice_t *fss;
1024 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1026 fss = fsh_slice_get (fsh, slice_index);
1034 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1036 fifo_segment_header_t *fsh = fs->h;
1037 fifo_segment_slice_t *fss;
1039 fss = fsh_slice_get (fsh, slice_index);
1044 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1048 size = fifo_segment_size (fs);
1050 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1051 return (in_use * 100) / size;
1054 fifo_segment_mem_status_t
1055 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1057 if (!fsh->high_watermark || !fsh->low_watermark)
1058 return MEMORY_PRESSURE_NO_PRESSURE;
1060 /* once the no-memory is detected, the status continues
1061 * until memory usage gets below the high watermark
1063 if (fsh_has_reached_mem_limit (fsh))
1065 if (usage >= fsh->high_watermark)
1066 return MEMORY_PRESSURE_NO_MEMORY;
1068 fsh_reset_mem_limit (fsh);
1071 if (usage >= fsh->high_watermark)
1072 return MEMORY_PRESSURE_HIGH_PRESSURE;
1074 else if (usage >= fsh->low_watermark)
1075 return MEMORY_PRESSURE_LOW_PRESSURE;
1077 return MEMORY_PRESSURE_NO_PRESSURE;
1080 fifo_segment_mem_status_t
1081 fifo_segment_get_mem_status (fifo_segment_t * fs)
1083 fifo_segment_header_t *fsh = fs->h;
1084 u8 usage = fifo_segment_get_mem_usage (fs);
1086 return fifo_segment_determine_status (fsh, usage);
1090 format_fifo_segment_type (u8 * s, va_list * args)
1093 sp = va_arg (*args, fifo_segment_t *);
1094 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1096 if (st == SSVM_SEGMENT_PRIVATE)
1097 s = format (s, "%s", "private-heap");
1098 else if (st == SSVM_SEGMENT_MEMFD)
1099 s = format (s, "%s", "memfd");
1100 else if (st == SSVM_SEGMENT_SHM)
1101 s = format (s, "%s", "shm");
1103 s = format (s, "%s", "unknown");
1108 * Segment format function
1111 format_fifo_segment (u8 * s, va_list * args)
1113 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
1114 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1115 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1116 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1117 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1118 uword tracked_cached_bytes;
1119 fifo_segment_header_t *fsh;
1120 fifo_segment_slice_t *fss;
1121 svm_fifo_chunk_t *c;
1126 uword allocated, in_use;
1128 fifo_segment_mem_status_t mem_st;
1130 indent = format_get_indent (s) + 2;
1134 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1135 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1139 fifo_segment_info (fs, &address, &size);
1140 active_fifos = fifo_segment_num_fifos (fs);
1141 free_fifos = fifo_segment_num_free_fifos (fs);
1143 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1144 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1145 free_fifos, address);
1152 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1154 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1157 s = format (s, "\n");
1159 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1161 fss = fsh_slice_get (fsh, slice_index);
1162 for (i = 0; i < vec_len (fss->free_chunks); i++)
1164 c = fss->free_chunks[i];
1174 chunk_size = fs_freelist_index_to_size (i);
1175 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1176 chunk_size >> 10, count);
1178 chunk_bytes += count * chunk_size;
1182 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1183 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1184 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1185 fifo_segment_update_free_bytes (fs);
1186 free_seg_bytes = fifo_segment_free_bytes (fs);
1187 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1188 allocated = fifo_segment_size (fs);
1189 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1190 usage = (100.0 * in_use) / allocated;
1191 mem_st = fifo_segment_get_mem_status (fs);
1193 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
1194 format_white_space, indent + 2, format_memory_size,
1195 free_seg_bytes, free_seg_bytes, format_memory_size,
1196 est_free_seg_bytes, est_free_seg_bytes);
1199 "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n",
1200 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1201 chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes,
1202 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1204 format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1205 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1206 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1207 fsh->n_reserved_bytes);
1209 format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space,
1210 indent + 2, usage, format_memory_size, in_use, format_memory_size,
1211 allocated, fifo_segment_mem_status_strings[mem_st]);
1212 s = format (s, "\n");
1218 * fd.io coding-style-patch-verification: ON
1221 * eval: (c-set-style "gnu")