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