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 fs_free_space (fifo_segment_t * fs)
31 struct dlmallinfo dlminfo;
33 dlminfo = mspace_mallinfo (fs->ssvm.sh->heap);
34 return dlminfo.fordblks;
38 * Initialize fifo segment shared header
41 fifo_segment_init (fifo_segment_t * fs)
43 fifo_segment_header_t *fsh;
44 ssvm_shared_header_t *sh;
48 oldheap = ssvm_push_heap (sh);
50 fsh = clib_mem_alloc (sizeof (*fsh));
51 clib_memset (fsh, 0, sizeof (*fsh));
52 fs->h = sh->opaque[0] = fsh;
54 ssvm_pop_heap (oldheap);
56 fsh->n_free_bytes = fs_free_space (fs);
62 * Create a fifo segment and initialize as master
65 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
71 /* Allocate a fresh segment */
72 pool_get_zero (sm->segments, fs);
74 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
75 fs->ssvm.ssvm_size = a->segment_size;
76 fs->ssvm.i_am_master = 1;
77 fs->ssvm.my_pid = getpid ();
78 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
79 fs->ssvm.requested_va = baseva;
81 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
83 pool_put (sm->segments, fs);
87 /* Note: requested_va updated due to seg base addr randomization */
88 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
90 fifo_segment_init (fs);
91 vec_add1 (a->new_segment_indices, fs - sm->segments);
96 * Attach as slave to a fifo segment
99 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
104 pool_get_zero (sm->segments, s);
106 s->ssvm.ssvm_size = a->segment_size;
107 s->ssvm.my_pid = getpid ();
108 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
109 s->ssvm.requested_va = sm->next_baseva;
110 if (a->segment_type == SSVM_SEGMENT_MEMFD)
111 s->ssvm.fd = a->memfd_fd;
113 s->ssvm.attach_timeout = sm->timeout_in_seconds;
115 if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type)))
117 _vec_len (s) = vec_len (s) - 1;
121 /* Fish the segment header */
122 s->h = s->ssvm.sh->opaque[0];
124 vec_add1 (a->new_segment_indices, s - sm->segments);
129 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
131 ssvm_delete (&s->ssvm);
132 clib_memset (s, 0xfe, sizeof (*s));
133 pool_put (sm->segments, s);
137 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
139 return s - sm->segments;
143 svm_fifo_segment_heap (fifo_segment_t * seg)
145 return seg->ssvm.sh->heap;
149 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
151 return pool_elt_at_index (sm->segments, segment_index);
155 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
157 *address = (char *) seg->ssvm.sh->ssvm_va;
158 *size = seg->ssvm.ssvm_size;
162 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
163 u32 timeout_in_seconds)
165 sm->next_baseva = baseva;
166 sm->timeout_in_seconds = timeout_in_seconds;
170 fs_freelist_for_size (u32 size)
172 return max_log2 (size) - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
176 fs_freelist_index_to_size (u32 fl_index)
178 return 1 << (fl_index + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE));
182 fs_chunk_size_is_valid (u32 size)
185 * 4K minimum. It's not likely that anything good will happen
186 * with a smaller FIFO.
188 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
189 && size <= FIFO_SEGMENT_MAX_FIFO_SIZE;
193 fs_try_alloc_fifo_freelist (fifo_segment_t * fs, u32 fl_index, u32 data_bytes)
195 fifo_segment_header_t *fsh = fs->h;
200 c = fsh->free_chunks[fl_index];
205 fsh->free_fifos = f->next;
206 fsh->free_chunks[fl_index] = c->next;
209 c->length = data_bytes;
210 memset (f, 0, sizeof (*f));
214 fsh->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
219 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_t * fs, u32 data_bytes)
221 svm_fifo_chunk_t *c, *first = 0, *last = 0;
222 fifo_segment_header_t *fsh = fs->h;
223 u32 fl_index, fl_size, n_alloc = 0;
229 void *oldheap = ssvm_push_heap (fs->ssvm.sh);
230 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
231 ssvm_pop_heap (oldheap);
234 memset (f, 0, sizeof (*f));
235 fsh->n_free_bytes -= sizeof (*f);
239 fsh->free_fifos = f->next;
242 fl_index = fs_freelist_for_size (data_bytes) - 1;
243 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
244 fl_size = fs_freelist_index_to_size (fl_index);
248 c = fsh->free_chunks[fl_index];
251 fsh->free_chunks[fl_index] = c->next;
257 c->length = clib_min (fl_size, data_bytes);
258 data_bytes -= c->length;
262 ASSERT (fl_index > 0);
264 fl_size = fl_size >> 1;
267 f->start_chunk = first;
270 fsh->n_fl_chunk_bytes -= n_alloc;
275 fs_try_alloc_fifo_batch (fifo_segment_t * fs, u32 fl_index, u32 batch_size)
277 fifo_segment_header_t *fsh = fs->h;
278 u32 size, hdrs, rounded_data_size;
285 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
286 rounded_data_size = fs_freelist_index_to_size (fl_index);
287 hdrs = sizeof (*f) + sizeof (*c);
288 size = (hdrs + rounded_data_size) * batch_size;
290 oldheap = ssvm_push_heap (fs->ssvm.sh);
291 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
292 0 /* align_offset */ ,
293 0 /* os_out_of_memory */ );
294 ssvm_pop_heap (oldheap);
300 /* Carve fifo + chunk space */
301 for (i = 0; i < batch_size; i++)
303 f = (svm_fifo_t *) fmem;
304 memset (f, 0, sizeof (*f));
305 f->next = fsh->free_fifos;
307 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
309 c->length = rounded_data_size;
310 c->next = fsh->free_chunks[fl_index];
311 fsh->free_chunks[fl_index] = c;
312 fmem += hdrs + rounded_data_size;
315 fsh->n_fl_chunk_bytes += batch_size * rounded_data_size;
316 fsh->n_free_bytes -= size;
322 * Try to allocate new fifo
324 * Tries the following steps in order:
325 * - grab fifo and chunk from freelists
326 * - batch fifo and chunk allocation
327 * - single fifo allocation
328 * - grab multiple fifo chunks from freelists
331 fs_try_alloc_fifo (fifo_segment_t * fs, u32 data_bytes)
333 fifo_segment_header_t *fsh = fs->h;
334 u32 fifo_sz, fl_index;
337 fl_index = fs_freelist_for_size (data_bytes);
338 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
339 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
340 fifo_sz += 1 << max_log2 (data_bytes);
342 if (fsh->free_fifos && fsh->free_chunks[fl_index])
344 f = fs_try_alloc_fifo_freelist (fs, fl_index, data_bytes);
348 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < fsh->n_free_bytes)
350 if (fs_try_alloc_fifo_batch (fs, fl_index,
351 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
354 f = fs_try_alloc_fifo_freelist (fs, fl_index, data_bytes);
357 if (fifo_sz <= fsh->n_free_bytes)
359 void *oldheap = ssvm_push_heap (fs->ssvm.sh);
360 f = svm_fifo_create (data_bytes);
361 ssvm_pop_heap (oldheap);
364 fsh->n_free_bytes -= fifo_sz;
368 if (data_bytes <= fsh->n_fl_chunk_bytes)
369 f = fs_try_alloc_fifo_freelist_multi_chunk (fs, data_bytes);
377 * Allocate fifo in fifo segment
380 fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes,
381 fifo_segment_ftype_t ftype)
383 fifo_segment_header_t *fsh;
386 if (!fs_chunk_size_is_valid (data_bytes))
388 clib_warning ("fifo size out of range %d", data_bytes);
393 ssvm_lock_non_recursive (fs->ssvm.sh, 1);
395 f = fs_try_alloc_fifo (fs, data_bytes);
399 /* (re)initialize the fifo, as in svm_fifo_create */
400 svm_fifo_init (f, data_bytes);
402 /* Initialize chunks and rbtree for multi-chunk fifos */
403 if (f->start_chunk->next != f->start_chunk)
405 void *oldheap = ssvm_push_heap (fs->ssvm.sh);
406 svm_fifo_init_chunks (f);
407 ssvm_pop_heap (oldheap);
410 /* If rx fifo type add to active fifos list. When cleaning up segment,
411 * we need a list of active sessions that should be disconnected. Since
412 * both rx and tx fifos keep pointers to the session, it's enough to track
414 if (ftype == FIFO_SEGMENT_RX_FIFO)
418 fsh->fifos->prev = f;
419 f->next = fsh->fifos;
422 f->flags |= SVM_FIFO_F_LL_TRACKED;
424 fsh->n_active_fifos++;
427 ssvm_unlock_non_recursive (fs->ssvm.sh);
432 * Free fifo allocated in fifo segment
435 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
437 svm_fifo_chunk_t *cur, *next;
438 fifo_segment_header_t *fsh;
439 ssvm_shared_header_t *sh;
443 ASSERT (f->refcnt > 0);
451 ssvm_lock_non_recursive (sh, 2);
453 /* Remove from active list. Only rx fifos are tracked */
454 if (f->flags & SVM_FIFO_F_LL_TRACKED)
457 f->prev->next = f->next;
459 fsh->fifos = f->next;
461 f->next->prev = f->prev;
462 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
465 /* Add to free list */
466 f->next = fsh->free_fifos;
470 /* Free fifo chunks */
471 cur = f->start_chunk;
475 fl_index = fs_freelist_for_size (cur->length);
476 ASSERT (fl_index < vec_len (fsh->free_chunks));
477 cur->next = fsh->free_chunks[fl_index];
478 fsh->free_chunks[fl_index] = cur;
479 fsh->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index);
482 while (cur != f->start_chunk);
484 f->start_chunk = f->end_chunk = f->new_chunks = 0;
485 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
487 oldheap = ssvm_push_heap (sh);
488 svm_fifo_free_chunk_lookup (f);
489 ssvm_pop_heap (oldheap);
491 /* not allocated on segment heap */
492 svm_fifo_free_ooo_data (f);
496 f->master_session_index = ~0;
497 f->master_thread_index = ~0;
500 fsh->n_active_fifos--;
501 ssvm_unlock_non_recursive (sh);
505 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 batch_size)
507 fifo_segment_header_t *fsh = fs->h;
514 size = (sizeof (*f)) * batch_size;
516 oldheap = ssvm_push_heap (fs->ssvm.sh);
517 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
518 0 /* align_offset */ ,
519 0 /* os_out_of_memory */ );
520 ssvm_pop_heap (oldheap);
526 /* Carve fifo + chunk space */
527 for (i = 0; i < batch_size; i++)
529 f = (svm_fifo_t *) fmem;
530 memset (f, 0, sizeof (*f));
531 f->next = fsh->free_fifos;
536 fsh->n_free_bytes -= size;
542 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 chunk_size,
545 fifo_segment_header_t *fsh = fs->h;
546 u32 size, rounded_data_size, fl_index;
552 if (!fs_chunk_size_is_valid (chunk_size))
554 clib_warning ("chunk size out of range %d", chunk_size);
558 fl_index = fs_freelist_for_size (chunk_size);
559 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
560 rounded_data_size = fs_freelist_index_to_size (fl_index);
561 size = (sizeof (*c) + rounded_data_size) * batch_size;
563 oldheap = ssvm_push_heap (fs->ssvm.sh);
564 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
565 0 /* align_offset */ ,
566 0 /* os_out_of_memory */ );
567 ssvm_pop_heap (oldheap);
573 /* Carve fifo + chunk space */
574 for (i = 0; i < batch_size; i++)
576 c = (svm_fifo_chunk_t *) cmem;
578 c->length = rounded_data_size;
579 c->next = fsh->free_chunks[fl_index];
580 fsh->free_chunks[fl_index] = c;
581 cmem += sizeof (*c) + rounded_data_size;
584 fsh->n_fl_chunk_bytes += batch_size * rounded_data_size;
585 fsh->n_free_bytes -= size;
591 * Pre-allocates fifo pairs in fifo segment
594 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
595 u32 rx_fifo_size, u32 tx_fifo_size,
598 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
599 int rx_fl_index, tx_fl_index;
600 uword space_available;
603 /* Parameter check */
604 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
607 if (!fs_chunk_size_is_valid (rx_fifo_size))
609 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
613 if (!fs_chunk_size_is_valid (tx_fifo_size))
615 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
619 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
620 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
621 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
622 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
624 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
626 /* Calculate space requirements */
627 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
628 space_available = fs_free_space (fs);
629 pairs_to_alloc = space_available / pair_size;
630 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
635 if (fs_try_alloc_fifo_batch (fs, rx_fl_index, pairs_to_alloc))
636 clib_warning ("rx prealloc failed: pairs %u", pairs_to_alloc);
637 if (fs_try_alloc_fifo_batch (fs, tx_fl_index, pairs_to_alloc))
638 clib_warning ("tx prealloc failed: pairs %u", pairs_to_alloc);
640 /* Account for the pairs allocated */
641 *n_fifo_pairs -= pairs_to_alloc;
645 fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
647 ssvm_shared_header_t *sh;
652 if (!fs_chunk_size_is_valid (chunk_size))
654 clib_warning ("chunk size out of range %d", chunk_size);
658 fl_index = fs_freelist_for_size (chunk_size);
661 ssvm_lock_non_recursive (sh, 1);
663 vec_validate_init_empty (fs->h->free_chunks, fl_index, 0);
664 c = fs->h->free_chunks[fl_index];
666 oldheap = ssvm_push_heap (sh);
670 c = svm_fifo_chunk_alloc (chunk_size);
673 ssvm_pop_heap (oldheap);
674 ssvm_unlock_non_recursive (sh);
677 fs->h->n_free_bytes -= chunk_size + sizeof (*c);
681 fs->h->free_chunks[fl_index] = c->next;
683 fs->h->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
686 svm_fifo_add_chunk (f, c);
688 ssvm_pop_heap (oldheap);
689 ssvm_unlock_non_recursive (sh);
694 fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
696 svm_fifo_chunk_t *cur, *next;
697 ssvm_shared_header_t *sh;
702 ssvm_lock_non_recursive (sh, 1);
704 oldheap = ssvm_push_heap (sh);
705 cur = svm_fifo_collect_chunks (f);
710 fl_index = fs_freelist_for_size (cur->length);
711 cur->next = fs->h->free_chunks[fl_index];
712 fs->h->free_chunks[fl_index] = cur;
716 ssvm_pop_heap (oldheap);
717 ssvm_unlock_non_recursive (sh);
723 * Get number of active fifos
726 fifo_segment_num_fifos (fifo_segment_t * fs)
728 return fs->h->n_active_fifos;
732 fifo_segment_num_free_fifos (fifo_segment_t * fs)
734 fifo_segment_header_t *fsh = fs->h;
751 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
753 u32 count = 0, rounded_size, fl_index;
754 fifo_segment_header_t *fsh;
760 /* Count all free chunks? */
763 for (i = 0; i < vec_len (fsh->free_chunks); i++)
765 c = fsh->free_chunks[i];
778 rounded_size = (1 << (max_log2 (size)));
779 fl_index = fs_freelist_for_size (rounded_size);
781 if (fl_index >= vec_len (fsh->free_chunks))
784 c = fsh->free_chunks[fl_index];
797 fifo_segment_update_free_bytes (fifo_segment_t * fs)
799 fs->h->n_free_bytes = fs_free_space (fs);
803 fifo_segment_free_bytes (fifo_segment_t * fs)
805 return fs->h->n_free_bytes;
809 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
811 return fs->h->n_fl_chunk_bytes;
815 fifo_segment_has_fifos (fifo_segment_t * fs)
817 return fs->h->fifos != 0;
821 fifo_segment_get_fifo_list (fifo_segment_t * fs)
827 format_fifo_segment_type (u8 * s, va_list * args)
830 sp = va_arg (*args, fifo_segment_t *);
831 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
833 if (st == SSVM_SEGMENT_PRIVATE)
834 s = format (s, "%s", "private-heap");
835 else if (st == SSVM_SEGMENT_MEMFD)
836 s = format (s, "%s", "memfd");
837 else if (st == SSVM_SEGMENT_SHM)
838 s = format (s, "%s", "shm");
840 s = format (s, "%s", "unknown");
845 * Segment format function
848 format_fifo_segment (u8 * s, va_list * args)
850 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0, chunk_size;
851 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
852 int verbose __attribute__ ((unused)) = va_arg (*args, int);
853 u32 est_chunk_bytes, est_free_seg_bytes;
854 fifo_segment_header_t *fsh;
861 indent = format_get_indent (s) + 2;
862 #if USE_DLMALLOC == 0
863 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
864 format_mheap, svm_fifo_segment_heap (fs), verbose);
865 s = format (s, "%U segment has %u active fifos\n",
866 format_white_space, indent, fifo_segment_num_fifos (fs));
871 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
872 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
877 fifo_segment_info (fs, &address, &size);
878 active_fifos = fifo_segment_num_fifos (fs);
879 free_fifos = fifo_segment_num_free_fifos (fs);
881 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
882 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
883 free_fifos, address);
888 if (fsh->free_chunks)
889 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
892 s = format (s, "\n");
894 for (i = 0; i < vec_len (fsh->free_chunks); i++)
896 c = fsh->free_chunks[i];
906 chunk_size = fs_freelist_index_to_size (i);
907 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
908 chunk_size >> 10, count);
910 chunk_bytes += count * chunk_size;
913 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
914 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
915 est_free_seg_bytes = fsh->n_free_bytes;
916 fifo_segment_update_free_bytes (fs);
918 s = format (s, "\n%Useg free bytes: %U (%u) estimated: %U (%u)\n",
919 format_white_space, indent + 2, format_memory_size,
920 fsh->n_free_bytes, fsh->n_free_bytes, format_memory_size,
921 est_free_seg_bytes, est_free_seg_bytes);
922 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%u)\n",
923 format_white_space, indent + 2, format_memory_size, chunk_bytes,
924 chunk_bytes, format_memory_size, est_chunk_bytes,
926 s = format (s, "%Ufifo hdr free bytes: %U (%u)\n", format_white_space,
927 indent + 2, format_memory_size, fifo_hdr, fifo_hdr);
928 s = format (s, "\n");
934 * fd.io coding-style-patch-verification: ON
937 * eval: (c-set-style "gnu")