svm: allow mq attachments at random offsets
[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 #include <vppinfra/mem.h>
18
19 static inline void *
20 fsh_alloc_aligned (fifo_segment_header_t *fsh, uword size, uword align)
21 {
22   uword cur_pos, cur_pos_align, new_pos;
23
24   cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
25   cur_pos_align = round_pow2_u64 (cur_pos, align);
26   size = round_pow2_u64 (size, align);
27   new_pos = cur_pos_align + size;
28
29   if (new_pos >= fsh->max_byte_index)
30     return 0;
31
32   while (!clib_atomic_cmp_and_swap_acq_relax (&fsh->byte_index, &cur_pos,
33                                               &new_pos, 1 /* weak */))
34     {
35       cur_pos_align = round_pow2_u64 (cur_pos, align);
36       new_pos = cur_pos_align + size;
37       if (new_pos >= fsh->max_byte_index)
38         return 0;
39     }
40   return uword_to_pointer ((u8 *) fsh + cur_pos_align, void *);
41 }
42
43 static inline void *
44 fsh_alloc (fifo_segment_header_t *fsh, uword size)
45 {
46   return fsh_alloc_aligned (fsh, size, 8);
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 fifo_slice_private_t *
56 fs_slice_private_get (fifo_segment_t *fs, u32 slice_index)
57 {
58   ASSERT (slice_index < fs->n_slices);
59   return &fs->slices[slice_index];
60 }
61
62 static char *fifo_segment_mem_status_strings[] = {
63 #define _(sym,str) str,
64   foreach_segment_mem_status
65 #undef _
66 };
67
68 static inline uword
69 fsh_n_free_bytes (fifo_segment_header_t * fsh)
70 {
71   uword cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
72   ASSERT (fsh->max_byte_index > cur_pos);
73   return fsh->max_byte_index - cur_pos;
74 }
75
76 static inline void
77 fsh_cached_bytes_add (fifo_segment_header_t * fsh, uword size)
78 {
79   clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
80 }
81
82 static inline void
83 fsh_cached_bytes_sub (fifo_segment_header_t * fsh, uword size)
84 {
85   clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
86 }
87
88 static inline uword
89 fsh_n_cached_bytes (fifo_segment_header_t * fsh)
90 {
91   uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
92   return n_cached;
93 }
94
95 static inline void
96 fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
97 {
98   clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
99 }
100
101 static inline u32
102 fsh_n_active_fifos (fifo_segment_header_t * fsh)
103 {
104   return clib_atomic_load_relax_n (&fsh->n_active_fifos);
105 }
106
107 static inline uword
108 fsh_virtual_mem (fifo_segment_header_t * fsh)
109 {
110   fifo_segment_slice_t *fss;
111   uword total_vm = 0;
112   int i;
113
114   for (i = 0; i < fsh->n_slices; i++)
115     {
116       fss = fsh_slice_get (fsh, i);
117       total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
118     }
119   return total_vm;
120 }
121
122 void
123 fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
124                         int n_bytes)
125 {
126   fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
127   fss->virtual_mem += n_bytes;
128 }
129
130 static inline void
131 fss_chunk_freelist_lock (fifo_segment_slice_t *fss)
132 {
133   u32 free = 0;
134   while (!clib_atomic_cmp_and_swap_acq_relax_n (&fss->chunk_lock, &free, 1, 0))
135     {
136       /* atomic load limits number of compare_exchange executions */
137       while (clib_atomic_load_relax_n (&fss->chunk_lock))
138         CLIB_PAUSE ();
139       /* on failure, compare_exchange writes (*p)->lock into free */
140       free = 0;
141     }
142 }
143
144 static inline void
145 fss_chunk_freelist_unlock (fifo_segment_slice_t *fss)
146 {
147   /* Make sure all reads/writes are complete before releasing the lock */
148   clib_atomic_release (&fss->chunk_lock);
149 }
150
151 static inline int
152 fss_chunk_fl_index_is_valid (fifo_segment_slice_t * fss, u32 fl_index)
153 {
154   return (fl_index < FS_CHUNK_VEC_LEN);
155 }
156
157 static void
158 fss_chunk_free_list_push (fifo_segment_header_t *fsh,
159                           fifo_segment_slice_t *fss, u32 fl_index,
160                           svm_fifo_chunk_t *c)
161 {
162   fss_chunk_freelist_lock (fss);
163   c->next = fss->free_chunks[fl_index];
164   fss->free_chunks[fl_index] = fs_chunk_sptr (fsh, c);
165   fss_chunk_freelist_unlock (fss);
166 }
167
168 static void
169 fss_chunk_free_list_push_list (fifo_segment_header_t *fsh,
170                                fifo_segment_slice_t *fss, u32 fl_index,
171                                svm_fifo_chunk_t *head, svm_fifo_chunk_t *tail)
172 {
173   fss_chunk_freelist_lock (fss);
174   tail->next = fss->free_chunks[fl_index];
175   fss->free_chunks[fl_index] = fs_chunk_sptr (fsh, head);
176   fss_chunk_freelist_unlock (fss);
177 }
178
179 static svm_fifo_chunk_t *
180 fss_chunk_free_list_pop (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss,
181                          u32 fl_index)
182 {
183   svm_fifo_chunk_t *c;
184
185   ASSERT (fss_chunk_fl_index_is_valid (fss, fl_index));
186
187   fss_chunk_freelist_lock (fss);
188
189   if (!fss->free_chunks[fl_index])
190     {
191       fss_chunk_freelist_unlock (fss);
192       return 0;
193     }
194
195   c = fs_chunk_ptr (fsh, fss->free_chunks[fl_index]);
196   fss->free_chunks[fl_index] = c->next;
197
198   fss_chunk_freelist_unlock (fss);
199
200   return c;
201 }
202
203 static inline void
204 pfss_fifo_add_active_list (fifo_slice_private_t *pfss, svm_fifo_t *f)
205 {
206   if (pfss->active_fifos)
207     {
208       pfss->active_fifos->prev = f;
209       f->next = pfss->active_fifos;
210     }
211   pfss->active_fifos = f;
212 }
213
214 static inline void
215 pfss_fifo_del_active_list (fifo_slice_private_t *pfss, svm_fifo_t *f)
216 {
217   if (f->flags & SVM_FIFO_F_LL_TRACKED)
218     {
219       if (f->prev)
220         f->prev->next = f->next;
221       else
222         pfss->active_fifos = f->next;
223       if (f->next)
224         f->next->prev = f->prev;
225     }
226 }
227
228 static inline uword
229 fss_fl_chunk_bytes (fifo_segment_slice_t * fss)
230 {
231   return clib_atomic_load_relax_n (&fss->n_fl_chunk_bytes);
232 }
233
234 static inline void
235 fss_fl_chunk_bytes_add (fifo_segment_slice_t * fss, uword size)
236 {
237   clib_atomic_fetch_add_relax (&fss->n_fl_chunk_bytes, size);
238 }
239
240 static inline void
241 fss_fl_chunk_bytes_sub (fifo_segment_slice_t * fss, uword size)
242 {
243   clib_atomic_fetch_sub_relax (&fss->n_fl_chunk_bytes, size);
244 }
245
246 /**
247  * Initialize fifo segment shared header
248  */
249 int
250 fifo_segment_init (fifo_segment_t * fs)
251 {
252   u32 align = 8, offset = 2 * 4096, slices_sz, i;
253   uword max_fifo, seg_start, seg_sz;
254   fifo_segment_header_t *fsh;
255   ssvm_shared_header_t *sh;
256   void *seg_data;
257
258   /* TODO remove ssvm heap entirely */
259   sh = fs->ssvm.sh;
260
261   seg_data = (u8 *) sh + offset;
262   seg_sz = sh->ssvm_size - offset;
263
264   fs->n_slices = clib_max (fs->n_slices, 1);
265   slices_sz = sizeof (fifo_segment_slice_t) * fs->n_slices;
266
267   seg_start = round_pow2_u64 (pointer_to_uword (seg_data), align);
268   fsh = uword_to_pointer (seg_start, void *);
269   memset (fsh, 0, sizeof (*fsh) + slices_sz);
270
271   fsh->byte_index = sizeof (*fsh) + slices_sz;
272   fsh->max_byte_index = seg_sz;
273   fsh->n_slices = fs->n_slices;
274   max_fifo = clib_min ((seg_sz - slices_sz) / 2, FIFO_SEGMENT_MAX_FIFO_SIZE);
275   fsh->max_log2_fifo_size = min_log2 (max_fifo);
276   fsh->n_cached_bytes = 0;
277   fsh->n_reserved_bytes = fsh->byte_index;
278   ASSERT (fsh->max_byte_index <= sh->ssvm_size - offset);
279
280   fs->max_byte_index = fsh->max_byte_index;
281   fs->h = sh->opaque[0] = fsh;
282
283   vec_validate (fs->slices, fs->n_slices - 1);
284   for (i = 0; i < fs->n_slices; i++)
285     fs->slices[i].fifos =
286       clib_mem_bulk_init (sizeof (svm_fifo_t), CLIB_CACHE_LINE_BYTES, 32);
287
288   sh->ready = 1;
289   return (0);
290 }
291
292 /**
293  * Create a fifo segment and initialize as master
294  */
295 int
296 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
297 {
298   fifo_segment_t *fs;
299   uword baseva;
300   int rv;
301
302   /* Allocate a fresh segment */
303   pool_get_zero (sm->segments, fs);
304
305   baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
306   fs->ssvm.ssvm_size = a->segment_size;
307   fs->ssvm.is_server = 1;
308   fs->ssvm.my_pid = getpid ();
309   fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
310   fs->ssvm.requested_va = baseva;
311
312   if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
313     {
314       pool_put (sm->segments, fs);
315       return (rv);
316     }
317
318   /* Note: requested_va updated due to seg base addr randomization */
319   sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
320
321   fifo_segment_init (fs);
322   vec_add1 (a->new_segment_indices, fs - sm->segments);
323   return (0);
324 }
325
326 /**
327  * Attach as slave to a fifo segment
328  */
329 int
330 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
331 {
332   fifo_segment_header_t *fsh;
333   fifo_segment_t *fs;
334   int rv;
335
336   pool_get_zero (sm->segments, fs);
337
338   fs->ssvm.ssvm_size = a->segment_size;
339   fs->ssvm.my_pid = getpid ();
340   fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
341   fs->ssvm.requested_va = sm->next_baseva;
342   if (a->segment_type == SSVM_SEGMENT_MEMFD)
343     fs->ssvm.fd = a->memfd_fd;
344   else
345     fs->ssvm.attach_timeout = sm->timeout_in_seconds;
346
347   if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
348     {
349       _vec_len (fs) = vec_len (fs) - 1;
350       return (rv);
351     }
352
353   /* Fish the segment header */
354   fsh = fs->h = fs->ssvm.sh->opaque[0];
355
356   /* Probably a segment without fifos */
357   if (!fsh)
358     goto done;
359
360   fs->max_byte_index = fsh->max_byte_index;
361   vec_validate (fs->slices, 0);
362   fs->slices[0].fifos =
363     clib_mem_bulk_init (sizeof (svm_fifo_t), CLIB_CACHE_LINE_BYTES, 32);
364
365 done:
366   vec_add1 (a->new_segment_indices, fs - sm->segments);
367   return (0);
368 }
369
370 void
371 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
372 {
373   fifo_segment_cleanup (s);
374   ssvm_delete (&s->ssvm);
375   clib_memset (s, 0xfe, sizeof (*s));
376   pool_put (sm->segments, s);
377 }
378
379 u32
380 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
381 {
382   return s - sm->segments;
383 }
384
385 fifo_segment_t *
386 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
387 {
388   return pool_elt_at_index (sm->segments, segment_index);
389 }
390
391 void
392 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
393 {
394   *address = (char *) seg->ssvm.sh->ssvm_va;
395   *size = seg->ssvm.ssvm_size;
396 }
397
398 void
399 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
400                         u32 timeout_in_seconds)
401 {
402   sm->next_baseva = baseva;
403   sm->timeout_in_seconds = timeout_in_seconds;
404 }
405
406 static inline u32
407 fs_freelist_for_size (u32 size)
408 {
409   if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
410     return 0;
411   return clib_min (max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE,
412                    FS_CHUNK_VEC_LEN - 1);
413 }
414
415 static inline u32
416 fs_freelist_index_to_size (u32 fl_index)
417 {
418   return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
419 }
420
421 static inline int
422 fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
423 {
424   /*
425    * 4K minimum. It's not likely that anything good will happen
426    * with a smaller FIFO.
427    */
428   return size >= FIFO_SEGMENT_MIN_FIFO_SIZE &&
429          size <= (1ULL << fsh->max_log2_fifo_size);
430 }
431
432 svm_fifo_chunk_t *
433 fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
434                           fifo_segment_slice_t * fss, u32 data_bytes)
435 {
436   u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
437   svm_fifo_chunk_t *c, *first = 0, *next;
438
439   fl_index = fs_freelist_for_size (req_bytes);
440   if (fl_index > 0)
441     fl_index -= 1;
442
443   fl_size = fs_freelist_index_to_size (fl_index);
444
445   while (req_bytes)
446     {
447       c = fss_chunk_free_list_pop (fsh, fss, fl_index);
448       if (c)
449         {
450           c->next = fs_chunk_sptr (fsh, first);
451           first = c;
452           n_alloc += fl_size;
453           req_bytes -= clib_min (fl_size, req_bytes);
454         }
455       else
456         {
457           /* Failed to allocate with smaller chunks */
458           if (fl_index == 0)
459             {
460               /* Free all chunks if any allocated */
461               c = first;
462               while (c)
463                 {
464                   fl_index = fs_freelist_for_size (c->length);
465                   next = fs_chunk_ptr (fsh, c->next);
466                   fss_chunk_free_list_push (fsh, fss, fl_index, c);
467                   c = next;
468                 }
469               n_alloc = 0;
470               first = 0;
471               /* As last attempt, try allocating a chunk larger than
472                * the requested size, if possible */
473               fl_index = fs_freelist_for_size (data_bytes) + 1;
474               if (!fss_chunk_fl_index_is_valid (fss, fl_index))
475                 return 0;
476               first = fss_chunk_free_list_pop (fsh, fss, fl_index);
477               if (first)
478                 {
479                   first->next = 0;
480                   n_alloc = fs_freelist_index_to_size (fl_index);
481                   goto done;
482                 }
483               return 0;
484             }
485           fl_index -= 1;
486           fl_size = fl_size >> 1;
487         }
488     }
489
490 done:
491   fss_fl_chunk_bytes_sub (fss, n_alloc);
492   fsh_cached_bytes_sub (fsh, n_alloc);
493   return first;
494 }
495
496 static int
497 fsh_try_alloc_fifo_hdr_batch (fifo_segment_header_t * fsh,
498                               fifo_segment_slice_t * fss, u32 batch_size)
499 {
500   svm_fifo_shared_t *f;
501   uword size;
502   u8 *fmem;
503   int i;
504
505   ASSERT (batch_size != 0);
506
507   size = (uword) sizeof (*f) * batch_size;
508
509   fmem = fsh_alloc_aligned (fsh, size, CLIB_CACHE_LINE_BYTES);
510   if (fmem == 0)
511     return -1;
512
513   /* Carve fifo hdr space */
514   for (i = 0; i < batch_size; i++)
515     {
516       f = (svm_fifo_shared_t *) fmem;
517       memset (f, 0, sizeof (*f));
518       f->next = fss->free_fifos;
519       fss->free_fifos = f;
520       fmem += sizeof (*f);
521     }
522
523   return 0;
524 }
525
526 static int
527 fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
528                            fifo_segment_slice_t * fss,
529                            u32 fl_index, u32 batch_size)
530 {
531   svm_fifo_chunk_t *c, *head = 0, *tail;
532   uword size, total_chunk_bytes;
533   u32 rounded_data_size;
534   u8 *cmem;
535   int i;
536
537   ASSERT (batch_size != 0);
538
539   rounded_data_size = fs_freelist_index_to_size (fl_index);
540   total_chunk_bytes = (uword) batch_size *rounded_data_size;
541   size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
542
543   cmem = fsh_alloc_aligned (fsh, size, 8 /* chunk hdr is 24B */);
544   if (cmem == 0)
545     return -1;
546
547   /* Carve fifo + chunk space */
548   tail = c = (svm_fifo_chunk_t *) cmem;
549   for (i = 0; i < batch_size; i++)
550     {
551       c->start_byte = 0;
552       c->length = rounded_data_size;
553       c->next = fs_chunk_sptr (fsh, head);
554       head = c;
555       cmem += sizeof (*c) + rounded_data_size;
556       c = (svm_fifo_chunk_t *) cmem;
557     }
558
559   fss_chunk_free_list_push_list (fsh, fss, fl_index, head, tail);
560   fss->num_chunks[fl_index] += batch_size;
561   fss_fl_chunk_bytes_add (fss, total_chunk_bytes);
562   fsh_cached_bytes_add (fsh, total_chunk_bytes);
563
564   return 0;
565 }
566
567 static int
568 fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
569                          fifo_segment_slice_t * fss,
570                          u32 fl_index, u32 batch_size)
571 {
572   if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
573     return 0;
574   return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
575 }
576
577 static svm_fifo_shared_t *
578 fsh_try_alloc_fifo_hdr (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss)
579 {
580   svm_fifo_shared_t *f;
581
582   if (!fss->free_fifos)
583     {
584       if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
585                                         FIFO_SEGMENT_ALLOC_BATCH_SIZE))
586         return 0;
587     }
588
589   f = fss->free_fifos;
590   fss->free_fifos = f->next;
591   memset (f, 0, sizeof (*f));
592   return f;
593 }
594
595 static svm_fifo_chunk_t *
596 fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
597                      fifo_segment_slice_t * fss, u32 data_bytes)
598 {
599   svm_fifo_chunk_t *c;
600   u32 fl_index;
601
602   fl_index = fs_freelist_for_size (data_bytes);
603
604 free_list:
605   c = fss_chunk_free_list_pop (fsh, fss, fl_index);
606   if (c)
607     {
608       c->next = 0;
609       fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
610       fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
611     }
612   else
613     {
614       u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
615       uword n_free;
616
617       chunk_size = fs_freelist_index_to_size (fl_index);
618       n_free = fsh_n_free_bytes (fsh);
619
620       if (chunk_size <= n_free)
621         {
622           batch = chunk_size * batch <= n_free ? batch : 1;
623           if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
624             goto free_list;
625         }
626       /* Failed to allocate larger chunk, try to allocate multi-chunk
627        * that is close to what was actually requested */
628       if (data_bytes <= fss_fl_chunk_bytes (fss))
629         {
630           c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
631           if (c)
632             goto done;
633           batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
634           if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
635             goto done;
636         }
637       if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
638         {
639           u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
640
641           batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
642           batch = clib_min (batch + 1, n_free / min_size);
643           if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
644             goto done;
645           c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
646         }
647     }
648
649 done:
650
651   return c;
652 }
653
654 /**
655  * Try to allocate new fifo
656  *
657  * Tries the following steps in order:
658  * - grab fifo and chunk from freelists
659  * - batch fifo and chunk allocation
660  * - single fifo allocation
661  * - grab multiple fifo chunks from freelists
662  */
663 static svm_fifo_shared_t *
664 fs_try_alloc_fifo (fifo_segment_header_t *fsh, u32 slice_index, u32 data_bytes)
665 {
666   fifo_segment_slice_t *fss;
667   u32 fl_index, min_size;
668   svm_fifo_chunk_t *c;
669   svm_fifo_shared_t *sf = 0;
670
671   fss = fsh_slice_get (fsh, slice_index);
672   min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
673   fl_index = fs_freelist_for_size (min_size);
674
675   if (!fss_chunk_fl_index_is_valid (fss, fl_index))
676     return 0;
677
678   sf = fsh_try_alloc_fifo_hdr (fsh, fss);
679   if (!sf)
680     return 0;
681
682   c = fsh_try_alloc_chunk (fsh, fss, min_size);
683   if (!c)
684     {
685       sf->next = fss->free_fifos;
686       fss->free_fifos = sf;
687       return 0;
688     }
689
690   sf->start_chunk = fs_chunk_sptr (fsh, c);
691   while (c->next)
692     c = fs_chunk_ptr (fsh, c->next);
693   sf->end_chunk = fs_chunk_sptr (fsh, c);
694   sf->size = data_bytes;
695   sf->slice_index = slice_index;
696
697   return sf;
698 }
699
700 svm_fifo_chunk_t *
701 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
702 {
703   fifo_segment_slice_t *fss;
704   svm_fifo_chunk_t *c;
705
706   fss = fsh_slice_get (fsh, slice_index);
707   c = fsh_try_alloc_chunk (fsh, fss, chunk_size);
708
709   return c;
710 }
711
712 static void
713 fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
714                           fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
715 {
716   u32 n_collect = 0, fl_index;
717   svm_fifo_chunk_t *next;
718
719   while (c)
720     {
721       CLIB_MEM_UNPOISON (c, sizeof (*c));
722       next = fs_chunk_ptr (fsh, c->next);
723       fl_index = fs_freelist_for_size (c->length);
724       fss_chunk_free_list_push (fsh, fss, fl_index, c);
725       n_collect += fs_freelist_index_to_size (fl_index);
726       c = next;
727     }
728
729   fss_fl_chunk_bytes_add (fss, n_collect);
730   fsh_cached_bytes_add (fsh, n_collect);
731 }
732
733 void
734 fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
735                     svm_fifo_chunk_t * c)
736 {
737   fifo_segment_slice_t *fss;
738   fss = fsh_slice_get (fsh, slice_index);
739   fsh_slice_collect_chunks (fsh, fss, c);
740 }
741
742 svm_fifo_t *
743 fs_fifo_alloc (fifo_segment_t *fs, u32 slice_index)
744 {
745   fifo_slice_private_t *pfss = &fs->slices[slice_index];
746   svm_fifo_t *f;
747
748   f = clib_mem_bulk_alloc (pfss->fifos);
749   clib_memset (f, 0, sizeof (*f));
750   return f;
751 }
752
753 void
754 fs_fifo_free (fifo_segment_t *fs, svm_fifo_t *f)
755 {
756   u32 slice_index = f->shr->slice_index;
757   fifo_slice_private_t *pfss;
758
759   pfss = &fs->slices[slice_index];
760   clib_mem_bulk_free (pfss->fifos, f);
761 }
762
763 void
764 fifo_segment_cleanup (fifo_segment_t *fs)
765 {
766   int slice_index;
767   svm_msg_q_t *mq = 0;
768
769   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
770     clib_mem_bulk_destroy (fs->slices[slice_index].fifos);
771
772   vec_foreach (fs->mqs, mq)
773     vec_free (mq->rings);
774
775   vec_free (fs->mqs);
776 }
777
778 /**
779  * Allocate fifo in fifo segment
780  */
781 svm_fifo_t *
782 fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
783                                  u32 data_bytes, fifo_segment_ftype_t ftype)
784 {
785   fifo_segment_header_t *fsh = fs->h;
786   fifo_slice_private_t *pfss;
787   fifo_segment_slice_t *fss;
788   svm_fifo_shared_t *sf;
789   svm_fifo_t *f = 0;
790
791   ASSERT (slice_index < fs->n_slices);
792
793   if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_fifo_size))
794     return 0;
795
796   sf = fs_try_alloc_fifo (fsh, slice_index, data_bytes);
797   if (!sf)
798     goto done;
799
800   f = fs_fifo_alloc (fs, slice_index);
801   f->fs_hdr = fsh;
802   f->shr = sf;
803
804   svm_fifo_init (f, data_bytes);
805
806   fss = fsh_slice_get (fsh, slice_index);
807   pfss = fs_slice_private_get (fs, slice_index);
808
809   /* If rx fifo type add to active fifos list. When cleaning up segment,
810    * we need a list of active sessions that should be disconnected. Since
811    * both rx and tx fifos keep pointers to the session, it's enough to track
812    * only one. */
813   if (ftype == FIFO_SEGMENT_RX_FIFO)
814     {
815       pfss_fifo_add_active_list (pfss, f);
816       f->flags |= SVM_FIFO_F_LL_TRACKED;
817     }
818
819   fsh_active_fifos_update (fsh, 1);
820   fss->virtual_mem += svm_fifo_size (f);
821
822 done:
823   return (f);
824 }
825
826 svm_fifo_t *
827 fifo_segment_alloc_fifo_w_shared (fifo_segment_t *fs, svm_fifo_shared_t *sf)
828 {
829   svm_fifo_t *f = fs_fifo_alloc (fs, 0);
830   f->fs_hdr = fs->h;
831   f->shr = sf;
832
833   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
834   f->segment_index = SVM_FIFO_INVALID_INDEX;
835   f->refcnt = 1;
836   return f;
837 }
838
839 /**
840  * Free fifo allocated in fifo segment
841  */
842 void
843 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
844 {
845   fifo_segment_header_t *fsh = fs->h;
846   fifo_slice_private_t *pfss;
847   fifo_segment_slice_t *fss;
848   svm_fifo_shared_t *sf;
849
850   ASSERT (f->refcnt > 0);
851
852   if (--f->refcnt > 0)
853     return;
854
855   /*
856    * Cleanup shared state
857    */
858
859   sf = f->shr;
860   fss = fsh_slice_get (fsh, sf->slice_index);
861   pfss = fs_slice_private_get (fs, sf->slice_index);
862
863   /* Free fifo chunks */
864   fsh_slice_collect_chunks (fsh, fss, fs_chunk_ptr (fsh, f->shr->start_chunk));
865
866   sf->start_chunk = sf->end_chunk = 0;
867   sf->head_chunk = sf->tail_chunk = 0;
868
869   /* Add to free list */
870   sf->next = fss->free_fifos;
871   fss->free_fifos = sf;
872
873   fss->virtual_mem -= svm_fifo_size (f);
874
875   /*
876    *  Cleanup private state
877    */
878
879   /* Remove from active list. Only rx fifos are tracked */
880   if (f->flags & SVM_FIFO_F_LL_TRACKED)
881     {
882       pfss_fifo_del_active_list (pfss, f);
883       f->flags &= ~SVM_FIFO_F_LL_TRACKED;
884     }
885
886   svm_fifo_free_chunk_lookup (f);
887   svm_fifo_free_ooo_data (f);
888
889   if (CLIB_DEBUG)
890     {
891       sf->master_session_index = ~0;
892       f->master_thread_index = ~0;
893     }
894
895   f->ooo_enq = f->ooo_deq = 0;
896   f->prev = 0;
897
898   fs_fifo_free (fs, f);
899
900   fsh_active_fifos_update (fsh, -1);
901 }
902
903 void
904 fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
905 {
906   fifo_slice_private_t *pfss;
907   fifo_segment_slice_t *fss;
908   svm_fifo_chunk_t *c;
909   u32 fl_index;
910
911   ASSERT (f->refcnt == 1);
912
913   fss = fsh_slice_get (fs->h, f->shr->slice_index);
914   pfss = fs_slice_private_get (fs, f->shr->slice_index);
915   fss->virtual_mem -= svm_fifo_size (f);
916   if (f->flags & SVM_FIFO_F_LL_TRACKED)
917     pfss_fifo_del_active_list (pfss, f);
918
919   c = fs_chunk_ptr (fs->h, f->shr->start_chunk);
920   while (c)
921     {
922       fl_index = fs_freelist_for_size (c->length);
923       clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
924       c = fs_chunk_ptr (fs->h, c->next);
925     }
926 }
927
928 void
929 fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
930                           u32 slice_index)
931 {
932   fifo_slice_private_t *pfss;
933   fifo_segment_slice_t *fss;
934   svm_fifo_chunk_t *c;
935   u32 fl_index;
936
937   f->shr->slice_index = slice_index;
938   fss = fsh_slice_get (fs->h, f->shr->slice_index);
939   pfss = fs_slice_private_get (fs, f->shr->slice_index);
940   fss->virtual_mem += svm_fifo_size (f);
941   if (f->flags & SVM_FIFO_F_LL_TRACKED)
942     pfss_fifo_add_active_list (pfss, f);
943
944   c = fs_chunk_ptr (fs->h, f->shr->start_chunk);
945   while (c)
946     {
947       fl_index = fs_freelist_for_size (c->length);
948       clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
949       c = fs_chunk_ptr (fs->h, c->next);
950     }
951 }
952
953 svm_msg_q_t *
954 fifo_segment_msg_q_alloc (fifo_segment_t *fs, u32 mq_index,
955                           svm_msg_q_cfg_t *cfg)
956 {
957   fifo_segment_header_t *fsh = fs->h;
958   svm_msg_q_shared_t *smq;
959   svm_msg_q_t *mq;
960   void *base;
961   u32 size;
962
963   if (!fs->mqs)
964     {
965       u32 n_mqs = clib_max (fs->h->n_mqs, 1);
966       vec_validate (fs->mqs, n_mqs - 1);
967     }
968
969   size = svm_msg_q_size_to_alloc (cfg);
970   base = fsh_alloc_aligned (fsh, size, 8);
971   fsh->n_reserved_bytes += size;
972
973   smq = svm_msg_q_init (base, cfg);
974   mq = vec_elt_at_index (fs->mqs, mq_index);
975   svm_msg_q_attach (mq, smq);
976
977   return mq;
978 }
979
980 svm_msg_q_t *
981 fifo_segment_msg_q_attach (fifo_segment_t *fs, uword offset, u32 mq_index)
982 {
983   svm_msg_q_t *mq;
984
985   if (!fs->mqs)
986     {
987       u32 n_mqs = clib_max (fs->h->n_mqs, 1);
988       vec_validate (fs->mqs, n_mqs - 1);
989     }
990
991   mq = vec_elt_at_index (fs->mqs, mq_index);
992
993   if (!mq->q)
994     {
995       svm_msg_q_shared_t *smq;
996       smq = (svm_msg_q_shared_t *) ((u8 *) fs->h + offset);
997       svm_msg_q_attach (mq, smq);
998     }
999
1000   ASSERT (fifo_segment_msg_q_offset (fs, mq_index) == offset);
1001
1002   return mq;
1003 }
1004
1005 uword
1006 fifo_segment_msg_q_offset (fifo_segment_t *fs, u32 mq_index)
1007 {
1008   svm_msg_q_t *mq = vec_elt_at_index (fs->mqs, mq_index);
1009
1010   if (mq->q == 0)
1011     return ~0ULL;
1012
1013   return (uword) ((u8 *) mq->q - (u8 *) fs->h) - sizeof (svm_msg_q_shared_t);
1014 }
1015
1016 int
1017 fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
1018                                  u32 batch_size)
1019 {
1020   fifo_segment_header_t *fsh = fs->h;
1021   fifo_segment_slice_t *fss;
1022
1023   fss = fsh_slice_get (fsh, slice_index);
1024   return fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size);
1025 }
1026
1027 int
1028 fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1029                                    u32 chunk_size, u32 batch_size)
1030 {
1031   fifo_segment_header_t *fsh = fs->h;
1032   fifo_segment_slice_t *fss;
1033   u32 fl_index;
1034
1035   if (!fs_chunk_size_is_valid (fsh, chunk_size))
1036     {
1037       clib_warning ("chunk size out of range %d", chunk_size);
1038       return -1;
1039     }
1040
1041   fl_index = fs_freelist_for_size (chunk_size);
1042   fss = fsh_slice_get (fsh, slice_index);
1043
1044   return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
1045 }
1046
1047 /**
1048  * Pre-allocates fifo pairs in fifo segment
1049  */
1050 void
1051 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1052                                      u32 rx_fifo_size, u32 tx_fifo_size,
1053                                      u32 * n_fifo_pairs)
1054 {
1055   u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
1056   u32 hdrs, pairs_per_slice, alloc_now;
1057   fifo_segment_header_t *fsh = fs->h;
1058   int rx_fl_index, tx_fl_index, i;
1059   fifo_segment_slice_t *fss;
1060   uword space_available;
1061
1062   /* Parameter check */
1063   if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1064     return;
1065
1066   if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
1067     {
1068       clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1069       return;
1070     }
1071
1072   if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
1073     {
1074       clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1075       return;
1076     }
1077
1078   rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
1079   rx_fl_index = fs_freelist_for_size (rx_fifo_size);
1080   tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
1081   tx_fl_index = fs_freelist_for_size (tx_fifo_size);
1082
1083   hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
1084
1085   /* Calculate space requirements */
1086   pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
1087   space_available = fsh_n_free_bytes (fsh);
1088   pairs_to_alloc = space_available / pair_size;
1089   pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
1090   pairs_per_slice = pairs_to_alloc / fs->n_slices;
1091   pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
1092
1093   if (!pairs_per_slice)
1094     return;
1095
1096   for (i = 0; i < fs->n_slices; i++)
1097     {
1098       alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1099       if (0 == alloc_now)
1100         break;
1101
1102       fss = fsh_slice_get (fsh, i);
1103       if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1104         clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1105       if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1106         clib_warning ("tx prealloc failed: pairs %u", alloc_now);
1107
1108       /* Account for the pairs allocated */
1109       *n_fifo_pairs -= alloc_now;
1110     }
1111 }
1112
1113 /**
1114  * Get number of active fifos
1115  */
1116 u32
1117 fifo_segment_num_fifos (fifo_segment_t * fs)
1118 {
1119   return fsh_n_active_fifos (fs->h);
1120 }
1121
1122 static u32
1123 fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
1124 {
1125   svm_fifo_shared_t *f;
1126   u32 count = 0;
1127
1128   f = fss->free_fifos;
1129   if (f == 0)
1130     return 0;
1131
1132   while (f)
1133     {
1134       f = f->next;
1135       count++;
1136     }
1137   return count;
1138 }
1139
1140 u32
1141 fifo_segment_num_free_fifos (fifo_segment_t * fs)
1142 {
1143   fifo_segment_header_t *fsh = fs->h;
1144   fifo_segment_slice_t *fss;
1145   int slice_index;
1146   u32 count = 0;
1147
1148   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1149     {
1150       fss = fsh_slice_get (fsh, slice_index);
1151       count += fs_slice_num_free_fifos (fss);
1152     }
1153   return count;
1154 }
1155
1156 static u32
1157 fs_slice_num_free_chunks (fifo_segment_header_t *fsh,
1158                           fifo_segment_slice_t *fss, u32 size)
1159 {
1160   u32 count = 0, rounded_size, fl_index;
1161   svm_fifo_chunk_t *c;
1162   int i;
1163
1164   /* Count all free chunks? */
1165   if (size == ~0)
1166     {
1167       for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
1168         {
1169           c = fs_chunk_ptr (fsh, fss->free_chunks[i]);
1170           if (c == 0)
1171             continue;
1172
1173           while (c)
1174             {
1175               c = fs_chunk_ptr (fsh, c->next);
1176               count++;
1177             }
1178         }
1179       return count;
1180     }
1181
1182   rounded_size = (1 << (max_log2 (size)));
1183   fl_index = fs_freelist_for_size (rounded_size);
1184
1185   if (fl_index >= FS_CHUNK_VEC_LEN)
1186     return 0;
1187
1188   c = fs_chunk_ptr (fsh, fss->free_chunks[fl_index]);
1189   if (c == 0)
1190     return 0;
1191
1192   while (c)
1193     {
1194       c = fs_chunk_ptr (fsh, c->next);
1195       count++;
1196     }
1197   return count;
1198 }
1199
1200 u32
1201 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1202 {
1203   fifo_segment_header_t *fsh = fs->h;
1204   fifo_segment_slice_t *fss;
1205   int slice_index;
1206   u32 count = 0;
1207
1208   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1209     {
1210       fss = fsh_slice_get (fsh, slice_index);
1211       count += fs_slice_num_free_chunks (fsh, fss, size);
1212     }
1213   return count;
1214 }
1215
1216 uword
1217 fifo_segment_size (fifo_segment_t * fs)
1218 {
1219   return fs->h->max_byte_index - fs->h->n_reserved_bytes;
1220 }
1221
1222 u8
1223 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1224 {
1225   return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1226 }
1227
1228 void
1229 fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1230 {
1231   fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1232 }
1233
1234 void *
1235 fifo_segment_alloc (fifo_segment_t *fs, uword size)
1236 {
1237   void *rv = fsh_alloc (fs->h, size);
1238   /* Mark externally allocated bytes as reserved. This helps
1239    * @ref fifo_segment_size report bytes used only for fifos */
1240   fs->h->n_reserved_bytes += size;
1241   return rv;
1242 }
1243
1244 uword
1245 fifo_segment_free_bytes (fifo_segment_t * fs)
1246 {
1247   return fsh_n_free_bytes (fs->h);
1248 }
1249
1250 uword
1251 fifo_segment_cached_bytes (fifo_segment_t * fs)
1252 {
1253   return fsh_n_cached_bytes (fs->h);
1254 }
1255
1256 uword
1257 fifo_segment_available_bytes (fifo_segment_t * fs)
1258 {
1259   return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1260 }
1261
1262 uword
1263 fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
1264 {
1265   fifo_segment_header_t *fsh = fs->h;
1266   fifo_segment_slice_t *fss;
1267   uword n_bytes = 0;
1268   int slice_index;
1269
1270   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1271     {
1272       fss = fsh_slice_get (fsh, slice_index);
1273       n_bytes += fss_fl_chunk_bytes (fss);
1274     }
1275
1276   return n_bytes;
1277 }
1278
1279 u8
1280 fifo_segment_has_fifos (fifo_segment_t * fs)
1281 {
1282   return (fsh_n_active_fifos (fs->h) != 0);
1283 }
1284
1285 svm_fifo_t *
1286 fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
1287 {
1288   fifo_slice_private_t *pfss;
1289
1290   pfss = fs_slice_private_get (fs, slice_index);
1291   return pfss->active_fifos;
1292 }
1293
1294 u8
1295 fifo_segment_get_mem_usage (fifo_segment_t * fs)
1296 {
1297   uword size, in_use;
1298
1299   size = fifo_segment_size (fs);
1300   in_use =
1301     size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1302   return (in_use * 100) / size;
1303 }
1304
1305 fifo_segment_mem_status_t
1306 fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1307 {
1308   if (!fsh->high_watermark || !fsh->low_watermark)
1309     return MEMORY_PRESSURE_NO_PRESSURE;
1310
1311   /* once the no-memory is detected, the status continues
1312    * until memory usage gets below the high watermark
1313    */
1314   if (fsh_has_reached_mem_limit (fsh))
1315     {
1316       if (usage >= fsh->high_watermark)
1317         return MEMORY_PRESSURE_NO_MEMORY;
1318       else
1319         fsh_reset_mem_limit (fsh);
1320     }
1321
1322   if (usage >= fsh->high_watermark)
1323     return MEMORY_PRESSURE_HIGH_PRESSURE;
1324
1325   else if (usage >= fsh->low_watermark)
1326     return MEMORY_PRESSURE_LOW_PRESSURE;
1327
1328   return MEMORY_PRESSURE_NO_PRESSURE;
1329 }
1330
1331 fifo_segment_mem_status_t
1332 fifo_segment_get_mem_status (fifo_segment_t * fs)
1333 {
1334   fifo_segment_header_t *fsh = fs->h;
1335   u8 usage = fifo_segment_get_mem_usage (fs);
1336
1337   return fifo_segment_determine_status (fsh, usage);
1338 }
1339
1340 u8 *
1341 format_fifo_segment_type (u8 * s, va_list * args)
1342 {
1343   fifo_segment_t *sp;
1344   sp = va_arg (*args, fifo_segment_t *);
1345   ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1346
1347   if (st == SSVM_SEGMENT_PRIVATE)
1348     s = format (s, "%s", "private");
1349   else if (st == SSVM_SEGMENT_MEMFD)
1350     s = format (s, "%s", "memfd");
1351   else if (st == SSVM_SEGMENT_SHM)
1352     s = format (s, "%s", "shm");
1353   else
1354     s = format (s, "%s", "unknown");
1355   return s;
1356 }
1357
1358 /**
1359  * Segment format function
1360  */
1361 u8 *
1362 format_fifo_segment (u8 * s, va_list * args)
1363 {
1364   u32 count, indent, active_fifos, free_fifos;
1365   fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1366   int verbose __attribute__ ((unused)) = va_arg (*args, int);
1367   uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1368   uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1369   uword tracked_cached_bytes;
1370   uword fifo_hdr = 0, reserved;
1371   fifo_segment_header_t *fsh;
1372   fifo_segment_slice_t *fss;
1373   svm_fifo_chunk_t *c;
1374   u32 slice_index;
1375   char *address;
1376   size_t size;
1377   int i;
1378   uword allocated, in_use, virt;
1379   f64 usage;
1380   fifo_segment_mem_status_t mem_st;
1381
1382   indent = format_get_indent (s) + 2;
1383
1384   if (fs == 0)
1385     {
1386       s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
1387                   "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1388       return s;
1389     }
1390
1391   fifo_segment_info (fs, &address, &size);
1392   active_fifos = fifo_segment_num_fifos (fs);
1393   free_fifos = fifo_segment_num_free_fifos (fs);
1394
1395   s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1396               format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1397               free_fifos, address);
1398
1399   if (!verbose)
1400     return s;
1401
1402   fsh = fs->h;
1403
1404   free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1405   if (free_chunks)
1406     s =
1407       format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1408               indent + 2);
1409   else
1410     s = format (s, "\n");
1411
1412   for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1413     {
1414       fss = fsh_slice_get (fsh, slice_index);
1415       for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
1416         {
1417           c = fs_chunk_ptr (fsh, fss->free_chunks[i]);
1418           if (c == 0 && fss->num_chunks[i] == 0)
1419             continue;
1420           count = 0;
1421           while (c)
1422             {
1423               c = fs_chunk_ptr (fsh, c->next);
1424               count++;
1425             }
1426
1427           chunk_size = fs_freelist_index_to_size (i);
1428           s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1429                       chunk_size >> 10, count, fss->num_chunks[i]);
1430
1431           chunk_bytes += count * chunk_size;
1432         }
1433     }
1434
1435   fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1436   est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1437   est_free_seg_bytes = fifo_segment_free_bytes (fs);
1438   free_seg_bytes = fifo_segment_free_bytes (fs);
1439   tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1440   allocated = fifo_segment_size (fs);
1441   in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1442   usage = (100.0 * in_use) / allocated;
1443   mem_st = fifo_segment_get_mem_status (fs);
1444   virt = fsh_virtual_mem (fsh);
1445   reserved = fsh->n_reserved_bytes;
1446
1447   s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1448               " %U (%lu)\n", format_white_space, indent + 2,
1449               format_memory_size, free_seg_bytes, free_seg_bytes,
1450               format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1451               format_memory_size, reserved, reserved);
1452   s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1453               " %U (%lu)\n", format_white_space, indent + 2,
1454               format_memory_size, chunk_bytes, chunk_bytes,
1455               format_memory_size, est_chunk_bytes, est_chunk_bytes,
1456               format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1457   s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1458               format_white_space, indent + 2, fsh->n_active_fifos,
1459               format_memory_size, fifo_hdr, fifo_hdr);
1460   s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1461               format_white_space, indent + 2, usage, format_memory_size,
1462               in_use, format_memory_size, allocated, format_memory_size, virt,
1463               fifo_segment_mem_status_strings[mem_st]);
1464   s = format (s, "\n");
1465
1466   return s;
1467 }
1468
1469 /*
1470  * fd.io coding-style-patch-verification: ON
1471  *
1472  * Local Variables:
1473  * eval: (c-set-style "gnu")
1474  * End:
1475  */