session svm: support for segments larger than 4GB
[vpp.git] / src / svm / fifo_segment.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <svm/fifo_segment.h>
17
18 /**
19  * Fifo segment free space
20  *
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
23  * indiscriminately.
24  *
25  * @param fs            fifo segment
26  * @return              number of free bytes
27  */
28 static uword
29 fs_free_space (fifo_segment_t * fs)
30 {
31   struct dlmallinfo dlminfo;
32
33   dlminfo = mspace_mallinfo (fs->ssvm.sh->heap);
34   return dlminfo.fordblks;
35 }
36
37 static inline void
38 fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
39 {
40   clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
41 }
42
43 static inline uword
44 fsh_n_free_bytes (fifo_segment_header_t * fsh)
45 {
46   return clib_atomic_load_relax_n (&fsh->n_free_bytes);
47 }
48
49 static inline fifo_segment_slice_t *
50 fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
51 {
52   return &fsh->slices[slice_index];
53 }
54
55 static inline void
56 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
57 {
58   clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
59 }
60
61 /**
62  * Initialize fifo segment shared header
63  */
64 int
65 fifo_segment_init (fifo_segment_t * fs)
66 {
67   fifo_segment_header_t *fsh;
68   fifo_segment_slice_t *fss;
69   ssvm_shared_header_t *sh;
70   u32 max_chunk_sz;
71   uword max_fifo;
72   void *oldheap;
73   int i;
74
75   sh = fs->ssvm.sh;
76   oldheap = ssvm_push_heap (sh);
77
78   /*
79    * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
80    * Long story made short: the "process-private" fifo segment
81    * is allocated from the main heap, not mmapped. dlmalloc
82    * only guarantees 4-byte alignment, and on aarch64
83    * the fsh can end up 4-byte but not 8-byte aligned.
84    * That eventually causes the atomic op in fifo_segment_update_free_bytes
85    * to backfire.
86    */
87   fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
88   clib_memset (fsh, 0, sizeof (*fsh));
89   fs->h = sh->opaque[0] = fsh;
90   fs->n_slices = clib_max (fs->n_slices, 1);
91
92   fsh->ssvm_sh = fs->ssvm.sh;
93   fsh->n_slices = fs->n_slices;
94   max_fifo = clib_min ((fs_free_space (fs) - 4096) / 2,
95                        FIFO_SEGMENT_MAX_FIFO_SIZE);
96   fsh->max_log2_chunk_size = max_log2 (max_fifo);
97
98   fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
99   clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
100   max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
101
102   for (i = 0; i < fs->n_slices; i++)
103     {
104       fss = fsh_slice_get (fsh, i);
105       vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
106     }
107
108   ssvm_pop_heap (oldheap);
109
110   fsh->n_free_bytes = fs_free_space (fs);
111   sh->ready = 1;
112   return (0);
113 }
114
115 /**
116  * Create a fifo segment and initialize as master
117  */
118 int
119 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
120 {
121   fifo_segment_t *fs;
122   uword baseva;
123   int rv;
124
125   /* Allocate a fresh segment */
126   pool_get_zero (sm->segments, fs);
127
128   baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
129   fs->ssvm.ssvm_size = a->segment_size;
130   fs->ssvm.i_am_master = 1;
131   fs->ssvm.my_pid = getpid ();
132   fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
133   fs->ssvm.requested_va = baseva;
134
135   if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
136     {
137       pool_put (sm->segments, fs);
138       return (rv);
139     }
140
141   /* Note: requested_va updated due to seg base addr randomization */
142   sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
143
144   fifo_segment_init (fs);
145   vec_add1 (a->new_segment_indices, fs - sm->segments);
146   return (0);
147 }
148
149 /**
150  * Attach as slave to a fifo segment
151  */
152 int
153 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
154 {
155   fifo_segment_t *fs;
156   int rv;
157
158   pool_get_zero (sm->segments, fs);
159
160   fs->ssvm.ssvm_size = a->segment_size;
161   fs->ssvm.my_pid = getpid ();
162   fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
163   fs->ssvm.requested_va = sm->next_baseva;
164   if (a->segment_type == SSVM_SEGMENT_MEMFD)
165     fs->ssvm.fd = a->memfd_fd;
166   else
167     fs->ssvm.attach_timeout = sm->timeout_in_seconds;
168
169   if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
170     {
171       _vec_len (fs) = vec_len (fs) - 1;
172       return (rv);
173     }
174
175   /* Fish the segment header */
176   fs->h = fs->ssvm.sh->opaque[0];
177
178   vec_add1 (a->new_segment_indices, fs - sm->segments);
179   return (0);
180 }
181
182 void
183 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
184 {
185   ssvm_delete (&s->ssvm);
186   clib_memset (s, 0xfe, sizeof (*s));
187   pool_put (sm->segments, s);
188 }
189
190 u32
191 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
192 {
193   return s - sm->segments;
194 }
195
196 fifo_segment_t *
197 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
198 {
199   return pool_elt_at_index (sm->segments, segment_index);
200 }
201
202 void
203 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
204 {
205   *address = (char *) seg->ssvm.sh->ssvm_va;
206   *size = seg->ssvm.ssvm_size;
207 }
208
209 void
210 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
211                         u32 timeout_in_seconds)
212 {
213   sm->next_baseva = baseva;
214   sm->timeout_in_seconds = timeout_in_seconds;
215 }
216
217 static inline u32
218 fs_freelist_for_size (u32 size)
219 {
220   return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
221 }
222
223 static inline u32
224 fs_freelist_index_to_size (u32 fl_index)
225 {
226   return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
227 }
228
229 static inline int
230 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
231 {
232   /*
233    * 4K minimum. It's not likely that anything good will happen
234    * with a smaller FIFO.
235    */
236   return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
237     && size <= (1 << fsh->max_log2_chunk_size);
238 }
239
240 static svm_fifo_t *
241 fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss,
242                             u32 fl_index, u32 data_bytes)
243 {
244   svm_fifo_chunk_t *c;
245   svm_fifo_t *f;
246
247   f = fss->free_fifos;
248   c = fss->free_chunks[fl_index];
249
250   if (!f || !c)
251     return 0;
252
253   fss->free_fifos = f->next;
254   fss->free_chunks[fl_index] = c->next;
255   c->next = c;
256   c->start_byte = 0;
257   c->length = data_bytes;
258   memset (f, 0, sizeof (*f));
259   f->start_chunk = c;
260   f->end_chunk = c;
261
262   fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
263   return f;
264 }
265
266 static svm_fifo_t *
267 fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
268                                         fifo_segment_slice_t * fss,
269                                         u32 data_bytes)
270 {
271   svm_fifo_chunk_t *c, *first = 0, *last = 0;
272   u32 fl_index, fl_size, n_alloc = 0;
273   svm_fifo_t *f;
274
275   f = fss->free_fifos;
276   if (!f)
277     {
278       void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
279       f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
280       ssvm_pop_heap (oldheap);
281       if (!f)
282         return 0;
283       memset (f, 0, sizeof (*f));
284       fsh_free_bytes_sub (fsh, sizeof (*f));
285     }
286   else
287     {
288       fss->free_fifos = f->next;
289     }
290
291   fl_index = fs_freelist_for_size (data_bytes) - 1;
292   fl_size = fs_freelist_index_to_size (fl_index);
293
294   while (data_bytes)
295     {
296       c = fss->free_chunks[fl_index];
297       if (c)
298         {
299           fss->free_chunks[fl_index] = c->next;
300           if (!last)
301             last = c;
302           c->next = first;
303           first = c;
304           n_alloc += fl_size;
305           c->length = clib_min (fl_size, data_bytes);
306           data_bytes -= c->length;
307         }
308       else
309         {
310           ASSERT (fl_index > 0);
311           fl_index -= 1;
312           fl_size = fl_size >> 1;
313         }
314     }
315   f->start_chunk = first;
316   f->end_chunk = last;
317   last->next = first;
318   fss->n_fl_chunk_bytes -= n_alloc;
319   return f;
320 }
321
322 static int
323 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
324                          fifo_segment_slice_t * fss,
325                          u32 fl_index, u32 batch_size)
326 {
327   u32 size, hdrs, rounded_data_size;
328   svm_fifo_chunk_t *c;
329   svm_fifo_t *f;
330   void *oldheap;
331   u8 *fmem;
332   int i;
333
334   rounded_data_size = fs_freelist_index_to_size (fl_index);
335   hdrs = sizeof (*f) + sizeof (*c);
336   size = (hdrs + rounded_data_size) * batch_size;
337
338   oldheap = ssvm_push_heap (fsh->ssvm_sh);
339   fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
340                                            0 /* align_offset */ ,
341                                            0 /* os_out_of_memory */ );
342   ssvm_pop_heap (oldheap);
343
344   /* Out of space.. */
345   if (fmem == 0)
346     return -1;
347
348   /* Carve fifo + chunk space */
349   for (i = 0; i < batch_size; i++)
350     {
351       f = (svm_fifo_t *) fmem;
352       memset (f, 0, sizeof (*f));
353       f->next = fss->free_fifos;
354       fss->free_fifos = f;
355       c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
356       c->start_byte = 0;
357       c->length = rounded_data_size;
358       c->next = fss->free_chunks[fl_index];
359       fss->free_chunks[fl_index] = c;
360       fmem += hdrs + rounded_data_size;
361     }
362
363   fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
364   fsh_free_bytes_sub (fsh, size);
365
366   return 0;
367 }
368
369 /**
370  * Try to allocate new fifo
371  *
372  * Tries the following steps in order:
373  * - grab fifo and chunk from freelists
374  * - batch fifo and chunk allocation
375  * - single fifo allocation
376  * - grab multiple fifo chunks from freelists
377  */
378 static svm_fifo_t *
379 fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
380                    u32 data_bytes)
381 {
382   u32 fifo_sz, fl_index;
383   svm_fifo_t *f = 0;
384   uword n_free_bytes;
385
386   fl_index = fs_freelist_for_size (data_bytes);
387   fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
388   fifo_sz += 1 << max_log2 (data_bytes);
389
390   if (fss->free_fifos && fss->free_chunks[fl_index])
391     {
392       f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
393       if (f)
394         goto done;
395     }
396   n_free_bytes = fsh_n_free_bytes (fsh);
397   if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
398     {
399       if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
400                                    FIFO_SEGMENT_ALLOC_BATCH_SIZE))
401         goto done;
402
403       f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
404       goto done;
405     }
406   if (fifo_sz <= n_free_bytes)
407     {
408       void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
409       f = svm_fifo_create (data_bytes);
410       ssvm_pop_heap (oldheap);
411       if (f)
412         {
413           fsh_free_bytes_sub (fsh, fifo_sz);
414           goto done;
415         }
416     }
417   if (data_bytes <= fss->n_fl_chunk_bytes)
418     f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
419
420 done:
421
422   return f;
423 }
424
425 /**
426  * Allocate fifo in fifo segment
427  */
428 svm_fifo_t *
429 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
430                                  u32 data_bytes, fifo_segment_ftype_t ftype)
431 {
432   fifo_segment_header_t *fsh = fs->h;
433   fifo_segment_slice_t *fss;
434   svm_fifo_t *f = 0;
435
436   ASSERT (slice_index < fs->n_slices);
437
438   fss = fsh_slice_get (fsh, slice_index);
439   f = fs_try_alloc_fifo (fsh, fss, data_bytes);
440   if (!f)
441     goto done;
442
443   f->slice_index = slice_index;
444
445   /* (re)initialize the fifo, as in svm_fifo_create */
446   svm_fifo_init (f, data_bytes);
447
448   /* Initialize chunks and rbtree for multi-chunk fifos */
449   if (f->start_chunk->next != f->start_chunk)
450     {
451       void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
452       svm_fifo_init_chunks (f);
453       ssvm_pop_heap (oldheap);
454     }
455
456   /* If rx fifo type add to active fifos list. When cleaning up segment,
457    * we need a list of active sessions that should be disconnected. Since
458    * both rx and tx fifos keep pointers to the session, it's enough to track
459    * only one. */
460   if (ftype == FIFO_SEGMENT_RX_FIFO)
461     {
462       if (fss->fifos)
463         {
464           fss->fifos->prev = f;
465           f->next = fss->fifos;
466         }
467       fss->fifos = f;
468       f->flags |= SVM_FIFO_F_LL_TRACKED;
469     }
470   fsh_active_fifos_update (fsh, 1);
471
472 done:
473   return (f);
474 }
475
476 /**
477  * Free fifo allocated in fifo segment
478  */
479 void
480 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
481 {
482   fifo_segment_header_t *fsh = fs->h;
483   svm_fifo_chunk_t *cur, *next;
484   fifo_segment_slice_t *fss;
485   void *oldheap;
486   int fl_index;
487
488   ASSERT (f->refcnt > 0);
489
490   if (--f->refcnt > 0)
491     return;
492
493   fss = fsh_slice_get (fsh, f->slice_index);
494
495   /* Remove from active list. Only rx fifos are tracked */
496   if (f->flags & SVM_FIFO_F_LL_TRACKED)
497     {
498       if (f->prev)
499         f->prev->next = f->next;
500       else
501         fss->fifos = f->next;
502       if (f->next)
503         f->next->prev = f->prev;
504       f->flags &= ~SVM_FIFO_F_LL_TRACKED;
505     }
506
507   /* Add to free list */
508   f->next = fss->free_fifos;
509   f->prev = 0;
510   fss->free_fifos = f;
511
512   /* Free fifo chunks */
513   cur = f->start_chunk;
514   do
515     {
516       next = cur->next;
517       fl_index = fs_freelist_for_size (cur->length);
518       ASSERT (fl_index < vec_len (fss->free_chunks));
519       cur->next = fss->free_chunks[fl_index];
520       fss->free_chunks[fl_index] = cur;
521       fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index);
522       cur = next;
523     }
524   while (cur != f->start_chunk);
525
526   f->start_chunk = f->end_chunk = f->new_chunks = 0;
527   f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
528
529   oldheap = ssvm_push_heap (fsh->ssvm_sh);
530   svm_fifo_free_chunk_lookup (f);
531   ssvm_pop_heap (oldheap);
532
533   /* not allocated on segment heap */
534   svm_fifo_free_ooo_data (f);
535
536   if (CLIB_DEBUG)
537     {
538       f->master_session_index = ~0;
539       f->master_thread_index = ~0;
540     }
541
542   fsh_active_fifos_update (fsh, -1);
543 }
544
545 int
546 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
547                                  u32 batch_size)
548 {
549   fifo_segment_header_t *fsh = fs->h;
550   fifo_segment_slice_t *fss;
551   svm_fifo_t *f;
552   void *oldheap;
553   u32 size;
554   u8 *fmem;
555   int i;
556
557   fss = fsh_slice_get (fsh, slice_index);
558   size = (sizeof (*f)) * batch_size;
559
560   oldheap = ssvm_push_heap (fsh->ssvm_sh);
561   fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
562                                            0 /* align_offset */ ,
563                                            0 /* os_out_of_memory */ );
564   ssvm_pop_heap (oldheap);
565
566   /* Out of space.. */
567   if (fmem == 0)
568     return -1;
569
570   /* Carve fifo + chunk space */
571   for (i = 0; i < batch_size; i++)
572     {
573       f = (svm_fifo_t *) fmem;
574       memset (f, 0, sizeof (*f));
575       f->next = fss->free_fifos;
576       fss->free_fifos = f;
577       fmem += sizeof (*f);
578     }
579
580   fsh_free_bytes_sub (fsh, size);
581
582   return 0;
583 }
584
585 int
586 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
587                                    u32 chunk_size, u32 batch_size)
588 {
589   u32 size, rounded_data_size, fl_index;
590   fifo_segment_header_t *fsh = fs->h;
591   fifo_segment_slice_t *fss;
592   svm_fifo_chunk_t *c;
593   void *oldheap;
594   u8 *cmem;
595   int i;
596
597   if (!fs_chunk_size_is_valid (fsh, chunk_size))
598     {
599       clib_warning ("chunk size out of range %d", chunk_size);
600       return -1;
601     }
602
603   fl_index = fs_freelist_for_size (chunk_size);
604   rounded_data_size = fs_freelist_index_to_size (fl_index);
605   size = (sizeof (*c) + rounded_data_size) * batch_size;
606
607   oldheap = ssvm_push_heap (fsh->ssvm_sh);
608   cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
609                                            0 /* align_offset */ ,
610                                            0 /* os_out_of_memory */ );
611   ssvm_pop_heap (oldheap);
612
613   /* Out of space.. */
614   if (cmem == 0)
615     return -1;
616
617   fss = fsh_slice_get (fsh, slice_index);
618
619   /* Carve fifo + chunk space */
620   for (i = 0; i < batch_size; i++)
621     {
622       c = (svm_fifo_chunk_t *) cmem;
623       c->start_byte = 0;
624       c->length = rounded_data_size;
625       c->next = fss->free_chunks[fl_index];
626       fss->free_chunks[fl_index] = c;
627       cmem += sizeof (*c) + rounded_data_size;
628     }
629
630   fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
631   fsh_free_bytes_sub (fsh, size);
632
633   return 0;
634 }
635
636 /**
637  * Pre-allocates fifo pairs in fifo segment
638  */
639 void
640 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
641                                      u32 rx_fifo_size, u32 tx_fifo_size,
642                                      u32 * n_fifo_pairs)
643 {
644   u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
645   fifo_segment_header_t *fsh = fs->h;
646   int rx_fl_index, tx_fl_index, i;
647   fifo_segment_slice_t *fss;
648   u32 hdrs, pairs_per_slice;
649   uword space_available;
650
651   /* Parameter check */
652   if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
653     return;
654
655   if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
656     {
657       clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
658       return;
659     }
660
661   if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
662     {
663       clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
664       return;
665     }
666
667   rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
668   rx_fl_index = fs_freelist_for_size (rx_fifo_size);
669   tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
670   tx_fl_index = fs_freelist_for_size (tx_fifo_size);
671
672   hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
673
674   /* Calculate space requirements */
675   pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
676   space_available = fs_free_space (fs);
677   pairs_to_alloc = space_available / pair_size;
678   pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
679   pairs_per_slice = pairs_to_alloc / fs->n_slices;
680
681   if (!pairs_per_slice)
682     return;
683
684   for (i = 0; i < fs->n_slices; i++)
685     {
686       fss = fsh_slice_get (fsh, i);
687       if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, pairs_to_alloc))
688         clib_warning ("rx prealloc failed: pairs %u", pairs_to_alloc);
689       if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, pairs_to_alloc))
690         clib_warning ("tx prealloc failed: pairs %u", pairs_to_alloc);
691     }
692
693   /* Account for the pairs allocated */
694   *n_fifo_pairs -= pairs_per_slice * fs->n_slices;
695 }
696
697 int
698 fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
699 {
700   fifo_segment_header_t *fsh = fs->h;
701   fifo_segment_slice_t *fss;
702   svm_fifo_chunk_t *c;
703   void *oldheap;
704   int fl_index;
705
706   fl_index = fs_freelist_for_size (chunk_size);
707   fss = fsh_slice_get (fsh, f->slice_index);
708
709   oldheap = ssvm_push_heap (fsh->ssvm_sh);
710
711   c = fss->free_chunks[fl_index];
712
713   if (!c)
714     {
715       c = svm_fifo_chunk_alloc (chunk_size);
716       if (!c)
717         {
718           ssvm_pop_heap (oldheap);
719           return -1;
720         }
721       fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
722     }
723   else
724     {
725       fss->free_chunks[fl_index] = c->next;
726       c->next = 0;
727       fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
728     }
729
730   svm_fifo_add_chunk (f, c);
731
732   ssvm_pop_heap (oldheap);
733   return 0;
734 }
735
736 int
737 fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
738 {
739   fifo_segment_header_t *fsh = fs->h;
740   svm_fifo_chunk_t *cur, *next;
741   fifo_segment_slice_t *fss;
742   void *oldheap;
743   int fl_index;
744
745   oldheap = ssvm_push_heap (fsh->ssvm_sh);
746   cur = svm_fifo_collect_chunks (f);
747
748   fss = fsh_slice_get (fsh, f->slice_index);
749
750   while (cur)
751     {
752       next = cur->next;
753       fl_index = fs_freelist_for_size (cur->length);
754       cur->next = fss->free_chunks[fl_index];
755       fss->free_chunks[fl_index] = cur;
756       cur = next;
757     }
758
759   ssvm_pop_heap (oldheap);
760
761   return 0;
762 }
763
764 /**
765  * Get number of active fifos
766  */
767 u32
768 fifo_segment_num_fifos (fifo_segment_t * fs)
769 {
770   return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
771 }
772
773 static u32
774 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
775 {
776   svm_fifo_t *f;
777   u32 count = 0;
778
779   f = fss->free_fifos;
780   if (f == 0)
781     return 0;
782
783   while (f)
784     {
785       f = f->next;
786       count++;
787     }
788   return count;
789 }
790
791 u32
792 fifo_segment_num_free_fifos (fifo_segment_t * fs)
793 {
794   fifo_segment_header_t *fsh = fs->h;
795   fifo_segment_slice_t *fss;
796   int slice_index;
797   u32 count = 0;
798
799   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
800     {
801       fss = fsh_slice_get (fsh, slice_index);
802       count += fs_slice_num_free_fifos (fss);
803     }
804   return count;
805 }
806
807 static u32
808 fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
809 {
810   u32 count = 0, rounded_size, fl_index;
811   svm_fifo_chunk_t *c;
812   int i;
813
814   /* Count all free chunks? */
815   if (size == ~0)
816     {
817       for (i = 0; i < vec_len (fss->free_chunks); i++)
818         {
819           c = fss->free_chunks[i];
820           if (c == 0)
821             continue;
822
823           while (c)
824             {
825               c = c->next;
826               count++;
827             }
828         }
829       return count;
830     }
831
832   rounded_size = (1 << (max_log2 (size)));
833   fl_index = fs_freelist_for_size (rounded_size);
834
835   if (fl_index >= vec_len (fss->free_chunks))
836     return 0;
837
838   c = fss->free_chunks[fl_index];
839   if (c == 0)
840     return 0;
841
842   while (c)
843     {
844       c = c->next;
845       count++;
846     }
847   return count;
848 }
849
850 u32
851 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
852 {
853   fifo_segment_header_t *fsh = fs->h;
854   fifo_segment_slice_t *fss;
855   int slice_index;
856   u32 count = 0;
857
858   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
859     {
860       fss = fsh_slice_get (fsh, slice_index);
861       count += fs_slice_num_free_chunks (fss, size);
862     }
863   return count;
864 }
865
866 void
867 fifo_segment_update_free_bytes (fifo_segment_t * fs)
868 {
869   fifo_segment_header_t *fsh = fs->h;
870   clib_atomic_store_rel_n (&fsh->n_free_bytes, fs_free_space (fs));
871 }
872
873 uword
874 fifo_segment_free_bytes (fifo_segment_t * fs)
875 {
876   return fsh_n_free_bytes (fs->h);
877 }
878
879 uword
880 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
881 {
882   fifo_segment_header_t *fsh = fs->h;
883   fifo_segment_slice_t *fss;
884   uword n_bytes = 0;
885   int slice_index;
886
887   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
888     {
889       fss = fsh_slice_get (fsh, slice_index);
890       n_bytes += fss->n_fl_chunk_bytes;
891     }
892
893   return n_bytes;
894 }
895
896 u8
897 fifo_segment_has_fifos (fifo_segment_t * fs)
898 {
899   fifo_segment_header_t *fsh = fs->h;
900   fifo_segment_slice_t *fss;
901   int slice_index;
902
903   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
904     {
905       fss = fsh_slice_get (fsh, slice_index);
906       if (fss->fifos)
907         return 1;
908     }
909   return 0;
910 }
911
912 svm_fifo_t *
913 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
914 {
915   fifo_segment_header_t *fsh = fs->h;
916   fifo_segment_slice_t *fss;
917
918   fss = fsh_slice_get (fsh, slice_index);
919   return fss->fifos;
920 }
921
922 u8 *
923 format_fifo_segment_type (u8 * s, va_list * args)
924 {
925   fifo_segment_t *sp;
926   sp = va_arg (*args, fifo_segment_t *);
927   ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
928
929   if (st == SSVM_SEGMENT_PRIVATE)
930     s = format (s, "%s", "private-heap");
931   else if (st == SSVM_SEGMENT_MEMFD)
932     s = format (s, "%s", "memfd");
933   else if (st == SSVM_SEGMENT_SHM)
934     s = format (s, "%s", "shm");
935   else
936     s = format (s, "%s", "unknown");
937   return s;
938 }
939
940 /**
941  * Segment format function
942  */
943 u8 *
944 format_fifo_segment (u8 * s, va_list * args)
945 {
946   u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
947   fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
948   int verbose __attribute__ ((unused)) = va_arg (*args, int);
949   uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
950   uword chunk_bytes = 0, free_seg_bytes, chunk_size;
951   fifo_segment_header_t *fsh;
952   fifo_segment_slice_t *fss;
953   svm_fifo_chunk_t *c;
954   u32 slice_index;
955   char *address;
956   size_t size;
957   int i;
958
959   indent = format_get_indent (s) + 2;
960 #if USE_DLMALLOC == 0
961   s = format (s, "%U segment heap: %U\n", format_white_space, indent,
962               format_mheap, fsh->ssvm_sh->heap, verbose);
963   s = format (s, "%U segment has %u active fifos\n",
964               format_white_space, indent, fifo_segment_num_fifos (fsh));
965 #endif
966
967   if (fs == 0)
968     {
969       s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
970                   "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
971       return s;
972     }
973
974   fifo_segment_info (fs, &address, &size);
975   active_fifos = fifo_segment_num_fifos (fs);
976   free_fifos = fifo_segment_num_free_fifos (fs);
977
978   s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
979               format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
980               free_fifos, address);
981
982   if (!verbose)
983     return s;
984
985   fsh = fs->h;
986
987   free_chunks = fifo_segment_num_free_chunks (fs, ~0);
988   if (free_chunks)
989     s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
990                 indent + 2);
991   else
992     s = format (s, "\n");
993
994   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
995     {
996       fss = fsh_slice_get (fsh, slice_index);
997       for (i = 0; i < vec_len (fss->free_chunks); i++)
998         {
999           c = fss->free_chunks[i];
1000           if (c == 0)
1001             continue;
1002           count = 0;
1003           while (c)
1004             {
1005               c = c->next;
1006               count++;
1007             }
1008
1009           chunk_size = fs_freelist_index_to_size (i);
1010           s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1011                       chunk_size >> 10, count);
1012
1013           chunk_bytes += count * chunk_size;
1014         }
1015     }
1016
1017   fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1018   est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1019   est_free_seg_bytes = fifo_segment_free_bytes (fs);
1020   fifo_segment_update_free_bytes (fs);
1021   free_seg_bytes = fifo_segment_free_bytes (fs);
1022
1023   s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
1024               format_white_space, indent + 2, format_memory_size,
1025               free_seg_bytes, free_seg_bytes, format_memory_size,
1026               est_free_seg_bytes, est_free_seg_bytes);
1027   s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n",
1028               format_white_space, indent + 2, format_memory_size, chunk_bytes,
1029               chunk_bytes, format_memory_size, est_chunk_bytes,
1030               est_chunk_bytes);
1031   s = format (s, "%Ufifo hdr free bytes: %U (%u)\n", format_white_space,
1032               indent + 2, format_memory_size, fifo_hdr, fifo_hdr);
1033   s = format (s, "\n");
1034
1035   return s;
1036 }
1037
1038 /*
1039  * fd.io coding-style-patch-verification: ON
1040  *
1041  * Local Variables:
1042  * eval: (c-set-style "gnu")
1043  * End:
1044  */