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 fsh_alloc_aligned (fifo_segment_header_t *fsh, uword size, uword align)
21 uword cur_pos, cur_pos_align, new_pos;
23 cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
24 cur_pos_align = round_pow2_u64 (cur_pos, align);
25 size = round_pow2_u64 (size, align);
26 new_pos = cur_pos_align + size;
28 if (new_pos >= fsh->max_byte_index)
31 while (!clib_atomic_cmp_and_swap_acq_relax (&fsh->byte_index, &cur_pos,
32 &new_pos, 1 /* weak */))
34 cur_pos_align = round_pow2_u64 (cur_pos, align);
35 new_pos = cur_pos_align + size;
36 if (new_pos >= fsh->max_byte_index)
39 return uword_to_pointer ((u8 *) fsh + cur_pos_align, void *);
43 fsh_alloc (fifo_segment_header_t *fsh, uword size)
45 return fsh_alloc_aligned (fsh, size, 8);
48 static inline fifo_segment_slice_t *
49 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
51 return &fsh->slices[slice_index];
54 static char *fifo_segment_mem_status_strings[] = {
55 #define _(sym,str) str,
56 foreach_segment_mem_status
61 fsh_n_free_bytes (fifo_segment_header_t * fsh)
63 uword cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
64 ASSERT (fsh->max_byte_index > cur_pos);
65 return fsh->max_byte_index - cur_pos;
69 fsh_cached_bytes_add (fifo_segment_header_t * fsh, uword size)
71 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
75 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, uword size)
77 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
81 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
83 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
88 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
90 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
94 fsh_n_active_fifos (fifo_segment_header_t * fsh)
96 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
100 fsh_virtual_mem (fifo_segment_header_t * fsh)
102 fifo_segment_slice_t *fss;
106 for (i = 0; i < fsh->n_slices; i++)
108 fss = fsh_slice_get (fsh, i);
109 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
115 fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
118 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
119 fss->virtual_mem += n_bytes;
123 fss_chunk_freelist_lock (fifo_segment_slice_t *fss)
126 while (!clib_atomic_cmp_and_swap_acq_relax_n (&fss->chunk_lock, &free, 1, 0))
128 /* atomic load limits number of compare_exchange executions */
129 while (clib_atomic_load_relax_n (&fss->chunk_lock))
131 /* on failure, compare_exchange writes (*p)->lock into free */
137 fss_chunk_freelist_unlock (fifo_segment_slice_t *fss)
139 /* Make sure all reads/writes are complete before releasing the lock */
140 clib_atomic_release (&fss->chunk_lock);
144 fss_chunk_fl_index_is_valid (fifo_segment_slice_t * fss, u32 fl_index)
146 return (fl_index < FS_CHUNK_VEC_LEN);
150 fss_chunk_free_list_push (fifo_segment_slice_t * fss, u32 fl_index,
151 svm_fifo_chunk_t * c)
153 fss_chunk_freelist_lock (fss);
154 c->next = fss->free_chunks[fl_index];
155 fss->free_chunks[fl_index] = c;
156 fss_chunk_freelist_unlock (fss);
160 fss_chunk_free_list_push_list (fifo_segment_slice_t * fss, u32 fl_index,
161 svm_fifo_chunk_t * head,
162 svm_fifo_chunk_t * tail)
164 fss_chunk_freelist_lock (fss);
165 tail->next = fss->free_chunks[fl_index];
166 fss->free_chunks[fl_index] = head;
167 fss_chunk_freelist_unlock (fss);
170 static svm_fifo_chunk_t *
171 fss_chunk_free_list_pop (fifo_segment_slice_t * fss, u32 fl_index)
175 ASSERT (fss_chunk_fl_index_is_valid (fss, fl_index));
177 fss_chunk_freelist_lock (fss);
179 if (!fss->free_chunks[fl_index])
181 fss_chunk_freelist_unlock (fss);
185 c = fss->free_chunks[fl_index];
186 fss->free_chunks[fl_index] = c->next;
188 fss_chunk_freelist_unlock (fss);
194 fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
198 fss->fifos->prev = f;
199 f->next = fss->fifos;
205 fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
207 if (f->flags & SVM_FIFO_F_LL_TRACKED)
210 f->prev->next = f->next;
212 fss->fifos = f->next;
214 f->next->prev = f->prev;
219 fss_fl_chunk_bytes (fifo_segment_slice_t * fss)
221 return clib_atomic_load_relax_n (&fss->n_fl_chunk_bytes);
225 fss_fl_chunk_bytes_add (fifo_segment_slice_t * fss, uword size)
227 clib_atomic_fetch_add_relax (&fss->n_fl_chunk_bytes, size);
231 fss_fl_chunk_bytes_sub (fifo_segment_slice_t * fss, uword size)
233 clib_atomic_fetch_sub_relax (&fss->n_fl_chunk_bytes, size);
237 * Initialize fifo segment shared header
240 fifo_segment_init (fifo_segment_t * fs)
242 u32 align = 8, offset = 2 * 4096, slices_sz;
243 uword max_fifo, seg_start, seg_sz;
244 fifo_segment_header_t *fsh;
245 ssvm_shared_header_t *sh;
248 /* TODO remove ssvm heap entirely */
251 seg_data = (u8 *) sh + offset;
252 seg_sz = sh->ssvm_size - offset;
254 fs->n_slices = clib_max (fs->n_slices, 1);
255 slices_sz = sizeof (fifo_segment_slice_t) * fs->n_slices;
257 seg_start = round_pow2_u64 (pointer_to_uword (seg_data), align);
258 fsh = uword_to_pointer (seg_start, void *);
259 memset (fsh, 0, sizeof (*fsh) + slices_sz);
261 fsh->byte_index = sizeof (*fsh) + slices_sz;
262 fsh->max_byte_index = seg_sz;
263 fsh->n_slices = fs->n_slices;
264 max_fifo = clib_min ((seg_sz - slices_sz) / 2, FIFO_SEGMENT_MAX_FIFO_SIZE);
265 fsh->max_log2_fifo_size = min_log2 (max_fifo);
266 fsh->n_cached_bytes = 0;
267 fsh->n_reserved_bytes = fsh->byte_index;
268 ASSERT (fsh->max_byte_index <= sh->ssvm_size - offset);
270 fs->max_byte_index = fsh->max_byte_index;
271 fs->h = sh->opaque[0] = fsh;
278 * Create a fifo segment and initialize as master
281 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
287 /* Allocate a fresh segment */
288 pool_get_zero (sm->segments, fs);
290 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
291 fs->ssvm.ssvm_size = a->segment_size;
292 fs->ssvm.is_server = 1;
293 fs->ssvm.my_pid = getpid ();
294 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
295 fs->ssvm.requested_va = baseva;
297 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
299 pool_put (sm->segments, fs);
303 /* Note: requested_va updated due to seg base addr randomization */
304 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
306 fifo_segment_init (fs);
307 vec_add1 (a->new_segment_indices, fs - sm->segments);
312 * Attach as slave to a fifo segment
315 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
317 fifo_segment_header_t *fsh;
321 pool_get_zero (sm->segments, fs);
323 fs->ssvm.ssvm_size = a->segment_size;
324 fs->ssvm.my_pid = getpid ();
325 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
326 fs->ssvm.requested_va = sm->next_baseva;
327 if (a->segment_type == SSVM_SEGMENT_MEMFD)
328 fs->ssvm.fd = a->memfd_fd;
330 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
332 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
334 _vec_len (fs) = vec_len (fs) - 1;
338 /* Fish the segment header */
339 fsh = fs->h = fs->ssvm.sh->opaque[0];
341 /* Probably a segment without fifos */
345 fs->max_byte_index = fsh->max_byte_index;
348 vec_add1 (a->new_segment_indices, fs - sm->segments);
353 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
355 ssvm_delete (&s->ssvm);
356 clib_memset (s, 0xfe, sizeof (*s));
357 pool_put (sm->segments, s);
361 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
363 return s - sm->segments;
367 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
369 return pool_elt_at_index (sm->segments, segment_index);
373 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
375 *address = (char *) seg->ssvm.sh->ssvm_va;
376 *size = seg->ssvm.ssvm_size;
380 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
381 u32 timeout_in_seconds)
383 sm->next_baseva = baseva;
384 sm->timeout_in_seconds = timeout_in_seconds;
388 fs_freelist_for_size (u32 size)
390 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
392 return clib_min (max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE,
393 FS_CHUNK_VEC_LEN - 1);
397 fs_freelist_index_to_size (u32 fl_index)
399 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
403 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
406 * 4K minimum. It's not likely that anything good will happen
407 * with a smaller FIFO.
409 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE &&
410 size <= (1ULL << fsh->max_log2_fifo_size);
414 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
415 fifo_segment_slice_t * fss, u32 data_bytes)
417 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
418 svm_fifo_chunk_t *c, *first = 0, *next;
420 fl_index = fs_freelist_for_size (req_bytes);
424 fl_size = fs_freelist_index_to_size (fl_index);
428 c = fss_chunk_free_list_pop (fss, fl_index);
434 req_bytes -= clib_min (fl_size, req_bytes);
438 /* Failed to allocate with smaller chunks */
441 /* Free all chunks if any allocated */
445 fl_index = fs_freelist_for_size (c->length);
447 fss_chunk_free_list_push (fss, fl_index, c);
452 /* As last attempt, try allocating a chunk larger than
453 * the requested size, if possible */
454 fl_index = fs_freelist_for_size (data_bytes) + 1;
455 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
457 first = fss_chunk_free_list_pop (fss, fl_index);
461 n_alloc = fs_freelist_index_to_size (fl_index);
467 fl_size = fl_size >> 1;
472 fss_fl_chunk_bytes_sub (fss, n_alloc);
473 fsh_cached_bytes_sub (fsh, n_alloc);
478 fsh_try_alloc_fifo_hdr_batch (fifo_segment_header_t * fsh,
479 fifo_segment_slice_t * fss, u32 batch_size)
486 ASSERT (batch_size != 0);
488 size = (uword) sizeof (*f) * batch_size;
490 fmem = fsh_alloc_aligned (fsh, size, CLIB_CACHE_LINE_BYTES);
494 /* Carve fifo hdr space */
495 for (i = 0; i < batch_size; i++)
497 f = (svm_fifo_t *) fmem;
498 memset (f, 0, sizeof (*f));
499 f->next = fss->free_fifos;
508 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
509 fifo_segment_slice_t * fss,
510 u32 fl_index, u32 batch_size)
512 svm_fifo_chunk_t *c, *head = 0, *tail;
513 uword size, total_chunk_bytes;
514 u32 rounded_data_size;
518 ASSERT (batch_size != 0);
520 rounded_data_size = fs_freelist_index_to_size (fl_index);
521 total_chunk_bytes = (uword) batch_size *rounded_data_size;
522 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
524 cmem = fsh_alloc_aligned (fsh, size, 8 /* chunk hdr is 24B */);
528 /* Carve fifo + chunk space */
529 tail = c = (svm_fifo_chunk_t *) cmem;
530 for (i = 0; i < batch_size; i++)
533 c->length = rounded_data_size;
536 cmem += sizeof (*c) + rounded_data_size;
537 c = (svm_fifo_chunk_t *) cmem;
540 fss_chunk_free_list_push_list (fss, fl_index, head, tail);
541 fss->num_chunks[fl_index] += batch_size;
542 fss_fl_chunk_bytes_add (fss, total_chunk_bytes);
543 fsh_cached_bytes_add (fsh, total_chunk_bytes);
549 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
550 fifo_segment_slice_t * fss,
551 u32 fl_index, u32 batch_size)
553 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
555 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
559 fsh_try_alloc_fifo_hdr (fifo_segment_header_t * fsh,
560 fifo_segment_slice_t * fss)
564 if (!fss->free_fifos)
566 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
567 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
572 fss->free_fifos = f->next;
573 memset (f, 0, sizeof (*f));
577 static svm_fifo_chunk_t *
578 fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
579 fifo_segment_slice_t * fss, u32 data_bytes)
584 fl_index = fs_freelist_for_size (data_bytes);
587 c = fss_chunk_free_list_pop (fss, fl_index);
591 fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
592 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
596 u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
599 chunk_size = fs_freelist_index_to_size (fl_index);
600 n_free = fsh_n_free_bytes (fsh);
602 if (chunk_size <= n_free)
604 batch = chunk_size * batch <= n_free ? batch : 1;
605 if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
608 /* Failed to allocate larger chunk, try to allocate multi-chunk
609 * that is close to what was actually requested */
610 if (data_bytes <= fss_fl_chunk_bytes (fss))
612 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
615 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
616 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
619 if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
621 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
623 batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
624 batch = clib_min (batch + 1, n_free / min_size);
625 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
627 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
637 * Try to allocate new fifo
639 * Tries the following steps in order:
640 * - grab fifo and chunk from freelists
641 * - batch fifo and chunk allocation
642 * - single fifo allocation
643 * - grab multiple fifo chunks from freelists
646 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
649 u32 fl_index, min_size;
653 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
654 fl_index = fs_freelist_for_size (min_size);
656 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
659 f = fsh_try_alloc_fifo_hdr (fsh, fss);
663 c = fsh_try_alloc_chunk (fsh, fss, min_size);
666 f->next = fss->free_fifos;
675 f->size = data_bytes;
682 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
684 fifo_segment_slice_t *fss;
687 fss = fsh_slice_get (fsh, slice_index);
688 c = fsh_try_alloc_chunk (fsh, fss, chunk_size);
694 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
695 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
697 u32 n_collect = 0, fl_index;
698 svm_fifo_chunk_t *next;
702 CLIB_MEM_UNPOISON (c, sizeof (*c));
704 fl_index = fs_freelist_for_size (c->length);
705 fss_chunk_free_list_push (fss, fl_index, c);
706 n_collect += fs_freelist_index_to_size (fl_index);
710 fss_fl_chunk_bytes_add (fss, n_collect);
711 fsh_cached_bytes_add (fsh, n_collect);
715 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
716 svm_fifo_chunk_t * c)
718 fifo_segment_slice_t *fss;
719 fss = fsh_slice_get (fsh, slice_index);
720 fsh_slice_collect_chunks (fsh, fss, c);
724 * Allocate fifo in fifo segment
727 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
728 u32 data_bytes, fifo_segment_ftype_t ftype)
730 fifo_segment_header_t *fsh = fs->h;
731 fifo_segment_slice_t *fss;
734 ASSERT (slice_index < fs->n_slices);
736 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_fifo_size))
739 fss = fsh_slice_get (fsh, slice_index);
740 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
744 f->slice_index = slice_index;
746 svm_fifo_init (f, data_bytes);
748 /* If rx fifo type add to active fifos list. When cleaning up segment,
749 * we need a list of active sessions that should be disconnected. Since
750 * both rx and tx fifos keep pointers to the session, it's enough to track
752 if (ftype == FIFO_SEGMENT_RX_FIFO)
754 fss_fifo_add_active_list (fss, f);
755 f->flags |= SVM_FIFO_F_LL_TRACKED;
758 fsh_active_fifos_update (fsh, 1);
759 fss->virtual_mem += svm_fifo_size (f);
766 * Free fifo allocated in fifo segment
769 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
771 fifo_segment_header_t *fsh = fs->h;
772 fifo_segment_slice_t *fss;
774 ASSERT (f->refcnt > 0);
779 fss = fsh_slice_get (fsh, f->slice_index);
781 /* Remove from active list. Only rx fifos are tracked */
782 if (f->flags & SVM_FIFO_F_LL_TRACKED)
784 fss_fifo_del_active_list (fss, f);
785 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
788 /* Free fifo chunks */
789 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
791 f->start_chunk = f->end_chunk = 0;
792 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
794 /* not allocated on segment heap */
795 svm_fifo_free_chunk_lookup (f);
796 svm_fifo_free_ooo_data (f);
800 f->master_session_index = ~0;
801 f->master_thread_index = ~0;
804 fss->virtual_mem -= svm_fifo_size (f);
806 /* Add to free list */
807 f->next = fss->free_fifos;
811 fsh_active_fifos_update (fsh, -1);
815 fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
817 fifo_segment_slice_t *fss;
821 ASSERT (f->refcnt == 1);
823 fss = fsh_slice_get (fs->h, f->slice_index);
824 fss->virtual_mem -= svm_fifo_size (f);
825 if (f->flags & SVM_FIFO_F_LL_TRACKED)
826 fss_fifo_del_active_list (fss, f);
831 fl_index = fs_freelist_for_size (c->length);
832 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
838 fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
841 fifo_segment_slice_t *fss;
845 f->slice_index = slice_index;
846 fss = fsh_slice_get (fs->h, f->slice_index);
847 fss->virtual_mem += svm_fifo_size (f);
848 if (f->flags & SVM_FIFO_F_LL_TRACKED)
849 fss_fifo_add_active_list (fss, f);
854 fl_index = fs_freelist_for_size (c->length);
855 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
861 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
864 fifo_segment_header_t *fsh = fs->h;
865 fifo_segment_slice_t *fss;
867 fss = fsh_slice_get (fsh, slice_index);
868 return fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size);
872 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
873 u32 chunk_size, u32 batch_size)
875 fifo_segment_header_t *fsh = fs->h;
876 fifo_segment_slice_t *fss;
879 if (!fs_chunk_size_is_valid (fsh, chunk_size))
881 clib_warning ("chunk size out of range %d", chunk_size);
885 fl_index = fs_freelist_for_size (chunk_size);
886 fss = fsh_slice_get (fsh, slice_index);
888 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
892 * Pre-allocates fifo pairs in fifo segment
895 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
896 u32 rx_fifo_size, u32 tx_fifo_size,
899 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
900 u32 hdrs, pairs_per_slice, alloc_now;
901 fifo_segment_header_t *fsh = fs->h;
902 int rx_fl_index, tx_fl_index, i;
903 fifo_segment_slice_t *fss;
904 uword space_available;
906 /* Parameter check */
907 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
910 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
912 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
916 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
918 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
922 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
923 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
924 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
925 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
927 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
929 /* Calculate space requirements */
930 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
931 space_available = fsh_n_free_bytes (fsh);
932 pairs_to_alloc = space_available / pair_size;
933 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
934 pairs_per_slice = pairs_to_alloc / fs->n_slices;
935 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
937 if (!pairs_per_slice)
940 for (i = 0; i < fs->n_slices; i++)
942 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
946 fss = fsh_slice_get (fsh, i);
947 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
948 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
949 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
950 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
952 /* Account for the pairs allocated */
953 *n_fifo_pairs -= alloc_now;
958 * Get number of active fifos
961 fifo_segment_num_fifos (fifo_segment_t * fs)
963 return fsh_n_active_fifos (fs->h);
967 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
985 fifo_segment_num_free_fifos (fifo_segment_t * fs)
987 fifo_segment_header_t *fsh = fs->h;
988 fifo_segment_slice_t *fss;
992 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
994 fss = fsh_slice_get (fsh, slice_index);
995 count += fs_slice_num_free_fifos (fss);
1001 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
1003 u32 count = 0, rounded_size, fl_index;
1004 svm_fifo_chunk_t *c;
1007 /* Count all free chunks? */
1010 for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
1012 c = fss->free_chunks[i];
1025 rounded_size = (1 << (max_log2 (size)));
1026 fl_index = fs_freelist_for_size (rounded_size);
1028 if (fl_index >= FS_CHUNK_VEC_LEN)
1031 c = fss->free_chunks[fl_index];
1044 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1046 fifo_segment_header_t *fsh = fs->h;
1047 fifo_segment_slice_t *fss;
1051 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1053 fss = fsh_slice_get (fsh, slice_index);
1054 count += fs_slice_num_free_chunks (fss, size);
1060 fifo_segment_size (fifo_segment_t * fs)
1062 return fs->h->max_byte_index - fs->h->n_reserved_bytes;
1066 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1068 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1072 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1074 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1078 fifo_segment_alloc (fifo_segment_t *fs, uword size)
1080 void *rv = fsh_alloc (fs->h, size);
1081 /* Mark externally allocated bytes as reserved. This helps
1082 * @ref fifo_segment_size report bytes used only for fifos */
1083 fs->h->n_reserved_bytes += size;
1088 fifo_segment_free_bytes (fifo_segment_t * fs)
1090 return fsh_n_free_bytes (fs->h);
1094 fifo_segment_cached_bytes (fifo_segment_t * fs)
1096 return fsh_n_cached_bytes (fs->h);
1100 fifo_segment_available_bytes (fifo_segment_t * fs)
1102 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1106 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1108 fifo_segment_header_t *fsh = fs->h;
1109 fifo_segment_slice_t *fss;
1113 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1115 fss = fsh_slice_get (fsh, slice_index);
1116 n_bytes += fss_fl_chunk_bytes (fss);
1123 fifo_segment_has_fifos (fifo_segment_t * fs)
1125 return (fsh_n_active_fifos (fs->h) != 0);
1129 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1131 fifo_segment_header_t *fsh = fs->h;
1132 fifo_segment_slice_t *fss;
1134 fss = fsh_slice_get (fsh, slice_index);
1139 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1143 size = fifo_segment_size (fs);
1145 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1146 return (in_use * 100) / size;
1149 fifo_segment_mem_status_t
1150 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1152 if (!fsh->high_watermark || !fsh->low_watermark)
1153 return MEMORY_PRESSURE_NO_PRESSURE;
1155 /* once the no-memory is detected, the status continues
1156 * until memory usage gets below the high watermark
1158 if (fsh_has_reached_mem_limit (fsh))
1160 if (usage >= fsh->high_watermark)
1161 return MEMORY_PRESSURE_NO_MEMORY;
1163 fsh_reset_mem_limit (fsh);
1166 if (usage >= fsh->high_watermark)
1167 return MEMORY_PRESSURE_HIGH_PRESSURE;
1169 else if (usage >= fsh->low_watermark)
1170 return MEMORY_PRESSURE_LOW_PRESSURE;
1172 return MEMORY_PRESSURE_NO_PRESSURE;
1175 fifo_segment_mem_status_t
1176 fifo_segment_get_mem_status (fifo_segment_t * fs)
1178 fifo_segment_header_t *fsh = fs->h;
1179 u8 usage = fifo_segment_get_mem_usage (fs);
1181 return fifo_segment_determine_status (fsh, usage);
1185 format_fifo_segment_type (u8 * s, va_list * args)
1188 sp = va_arg (*args, fifo_segment_t *);
1189 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1191 if (st == SSVM_SEGMENT_PRIVATE)
1192 s = format (s, "%s", "private");
1193 else if (st == SSVM_SEGMENT_MEMFD)
1194 s = format (s, "%s", "memfd");
1195 else if (st == SSVM_SEGMENT_SHM)
1196 s = format (s, "%s", "shm");
1198 s = format (s, "%s", "unknown");
1203 * Segment format function
1206 format_fifo_segment (u8 * s, va_list * args)
1208 u32 count, indent, active_fifos, free_fifos;
1209 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1210 int verbose __attribute__ ((unused)) = va_arg (*args, int);
1211 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1212 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1213 uword tracked_cached_bytes;
1214 uword fifo_hdr = 0, reserved;
1215 fifo_segment_header_t *fsh;
1216 fifo_segment_slice_t *fss;
1217 svm_fifo_chunk_t *c;
1222 uword allocated, in_use, virt;
1224 fifo_segment_mem_status_t mem_st;
1226 indent = format_get_indent (s) + 2;
1230 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
1231 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1235 fifo_segment_info (fs, &address, &size);
1236 active_fifos = fifo_segment_num_fifos (fs);
1237 free_fifos = fifo_segment_num_free_fifos (fs);
1239 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1240 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1241 free_fifos, address);
1248 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1251 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1254 s = format (s, "\n");
1256 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1258 fss = fsh_slice_get (fsh, slice_index);
1259 for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
1261 c = fss->free_chunks[i];
1262 if (c == 0 && fss->num_chunks[i] == 0)
1271 chunk_size = fs_freelist_index_to_size (i);
1272 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1273 chunk_size >> 10, count, fss->num_chunks[i]);
1275 chunk_bytes += count * chunk_size;
1279 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1280 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1281 est_free_seg_bytes = fifo_segment_free_bytes (fs);
1282 free_seg_bytes = fifo_segment_free_bytes (fs);
1283 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1284 allocated = fifo_segment_size (fs);
1285 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1286 usage = (100.0 * in_use) / allocated;
1287 mem_st = fifo_segment_get_mem_status (fs);
1288 virt = fsh_virtual_mem (fsh);
1289 reserved = fsh->n_reserved_bytes;
1291 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1292 " %U (%lu)\n", format_white_space, indent + 2,
1293 format_memory_size, free_seg_bytes, free_seg_bytes,
1294 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1295 format_memory_size, reserved, reserved);
1296 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1297 " %U (%lu)\n", format_white_space, indent + 2,
1298 format_memory_size, chunk_bytes, chunk_bytes,
1299 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1300 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1301 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1302 format_white_space, indent + 2, fsh->n_active_fifos,
1303 format_memory_size, fifo_hdr, fifo_hdr);
1304 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1305 format_white_space, indent + 2, usage, format_memory_size,
1306 in_use, format_memory_size, allocated, format_memory_size, virt,
1307 fifo_segment_mem_status_strings[mem_st]);
1308 s = format (s, "\n");
1314 * fd.io coding-style-patch-verification: ON
1317 * eval: (c-set-style "gnu")