FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
fifo_segment.c
Go to the documentation of this file.
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 *
20 {
21  return &fsh->slices[slice_index];
22 }
23 
25 #define _(sym,str) str,
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
42 {
43  struct dlmallinfo dlminfo;
44 
45  dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
46  return dlminfo.fordblks;
47 }
48 
49 static inline void
51 {
53 }
54 
55 static inline uword
57 {
59  return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
60 }
61 
62 static inline void
64 {
66 }
67 
68 static inline void
70 {
72 }
73 
74 static inline void
76 {
78 }
79 
80 static inline uword
82 {
84  return n_cached;
85 }
86 
87 static inline void
89 {
91 }
92 
93 static inline u32
95 {
97 }
98 
99 static inline uword
101 {
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
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
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 
136  fsh_update_free_bytes (fsh);
137 }
138 
139 /**
140  * Initialize fifo segment shared header
141  */
142 int
144 {
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,
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);
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);
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
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
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;
247  fs->ssvm.fd = a->memfd_fd;
248  else
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
266 {
267  ssvm_delete (&s->ssvm);
268  clib_memset (s, 0xfe, sizeof (*s));
269  pool_put (sm->segments, s);
270 }
271 
272 u32
274 {
275  return s - sm->segments;
276 }
277 
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
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
301 {
303  return 0;
305 }
306 
307 static inline u32
309 {
310  return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
311 }
312 
313 static inline int
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 *
326 {
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 
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 *
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  if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t)))
424  return 0;
425 
426  void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
428  ssvm_pop_heap (oldheap);
429  if (!f)
430  return 0;
431  memset (f, 0, sizeof (*f));
432  fsh_free_bytes_sub (fsh, sizeof (*f));
433  }
434  else
435  {
436  fss->free_fifos = f->next;
437  }
438 
439  fl_index = fs_freelist_for_size (data_bytes);
440  if (fl_index > 0)
441  fl_index -= 1;
442 
443  fl_size = fs_freelist_index_to_size (fl_index);
444 
445  while (data_bytes)
446  {
447  c = fss->free_chunks[fl_index];
448  if (c)
449  {
450  fss->free_chunks[fl_index] = c->next;
451  if (!last)
452  last = c;
453  c->next = first;
454  first = c;
455  n_alloc += fl_size;
456  data_bytes -= clib_min (fl_size, data_bytes);
457  }
458  else
459  {
460  /* Failed to allocate with smaller chunks */
461  if (fl_index == 0)
462  {
463  /* free all chunks if any allocated */
464  c = first;
465  while (c)
466  {
467  fl_index = fs_freelist_for_size (c->length);
468  fl_size = fs_freelist_index_to_size (fl_index);
469  next = c->next;
470  c->next = fss->free_chunks[fl_index];
471  fss->free_chunks[fl_index] = c;
472  fss->n_fl_chunk_bytes += fl_size;
473  n_alloc -= fl_size;
474  data_bytes += fl_size;
475  c = next;
476  }
477  first = last = 0;
478  fl_index = fs_freelist_for_size (data_bytes);
479  if (fss->free_chunks[fl_index + 1])
480  {
481  fl_index += 1;
482  fl_size = fs_freelist_index_to_size (fl_index);
483  continue;
484  }
485 
486  f->next = fss->free_fifos;
487  fss->free_fifos = f;
488  return 0;
489  }
490  fl_index -= 1;
491  fl_size = fl_size >> 1;
492  }
493  }
494 
495  f->start_chunk = first;
496  f->end_chunk = last;
497  fss->n_fl_chunk_bytes -= n_alloc;
498  fsh_cached_bytes_sub (fsh, n_alloc);
499  return f;
500 }
501 
502 static int
504  fifo_segment_slice_t * fss,
505  u32 fl_index, u32 batch_size)
506 {
507  u32 rounded_data_size;
509  void *oldheap;
510  uword size;
511  u8 *cmem;
512  int i;
513 
514  rounded_data_size = fs_freelist_index_to_size (fl_index);
515  size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
516 
517  oldheap = ssvm_push_heap (fsh->ssvm_sh);
519  0 /* align_offset */ ,
520  0 /* os_out_of_memory */ );
521  ssvm_pop_heap (oldheap);
522 
523  /* Out of space.. */
524  if (cmem == 0)
525  return -1;
526 
527  /* Carve fifo + chunk space */
528  for (i = 0; i < batch_size; i++)
529  {
530  c = (svm_fifo_chunk_t *) cmem;
531  c->start_byte = 0;
532  c->length = rounded_data_size;
535  c->next = fss->free_chunks[fl_index];
536  fss->free_chunks[fl_index] = c;
537  cmem += sizeof (*c) + rounded_data_size;
538  }
539 
540  fss->num_chunks[fl_index] += batch_size;
541  fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
542  fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
543  fsh_free_bytes_sub (fsh, size);
544 
545  return 0;
546 }
547 
548 static int
550  fifo_segment_slice_t * fss,
551  u32 fl_index, u32 batch_size)
552 {
553  u32 hdrs, rounded_data_size;
555  svm_fifo_t *f;
556  void *oldheap;
557  uword size;
558  u8 *fmem;
559  int i;
560 
561  rounded_data_size = fs_freelist_index_to_size (fl_index);
562  hdrs = sizeof (*f) + sizeof (*c);
563  size = (uword) (hdrs + rounded_data_size) * batch_size;
564 
565  oldheap = ssvm_push_heap (fsh->ssvm_sh);
567  0 /* align_offset */ ,
568  0 /* os_out_of_memory */ );
569  ssvm_pop_heap (oldheap);
570 
571  /* Out of space.. */
572  if (fmem == 0)
573  return -1;
574 
575  /* Carve fifo hdr space */
576  for (i = 0; i < batch_size; i++)
577  {
578  f = (svm_fifo_t *) fmem;
579  memset (f, 0, sizeof (*f));
580  f->next = fss->free_fifos;
581  fss->free_fifos = f;
582  fmem += sizeof (*f);
583  }
584 
585  /* Carve chunk space */
586  for (i = 0; i < batch_size; i++)
587  {
588  c = (svm_fifo_chunk_t *) fmem;
589  c->start_byte = 0;
590  c->length = rounded_data_size;
593  c->next = fss->free_chunks[fl_index];
594  fss->free_chunks[fl_index] = c;
595  fmem += sizeof (svm_fifo_chunk_t) + rounded_data_size;
596  }
597 
598  fss->num_chunks[fl_index] += batch_size;
599  fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
600  fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
601  fsh_free_bytes_sub (fsh, size);
602 
603  return 0;
604 }
605 
606 /**
607  * Try to allocate new fifo
608  *
609  * Tries the following steps in order:
610  * - grab fifo and chunk from freelists
611  * - batch fifo and chunk allocation
612  * - single fifo allocation
613  * - grab multiple fifo chunks from freelists
614  */
615 static svm_fifo_t *
617  u32 data_bytes)
618 {
619  u32 fifo_sz, fl_index;
620  svm_fifo_t *f = 0;
621  uword n_free_bytes;
622  u32 min_size;
623 
624  min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
625  fl_index = fs_freelist_for_size (min_size);
626 
627  if (fl_index >= vec_len (fss->free_chunks))
628  return 0;
629 
631 
632  if (fss->free_fifos && fss->free_chunks[fl_index])
633  {
634  f = fs_try_alloc_fifo_freelist (fss, fl_index);
635  if (f)
636  {
638  goto done;
639  }
640  }
641 
642  fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
643  fifo_sz += 1 << max_log2 (min_size);
644  n_free_bytes = fsh_n_free_bytes (fsh);
645 
646  if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
647  {
648  if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
650  {
651  f = fs_try_alloc_fifo_freelist (fss, fl_index);
652  if (f)
653  {
655  fs_freelist_index_to_size (fl_index));
656  goto done;
657  }
658  }
659  else
660  {
661  fsh_check_mem (fsh);
662  n_free_bytes = fsh_n_free_bytes (fsh);
663  }
664  }
665  if (fifo_sz <= n_free_bytes)
666  {
667  void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
668  f = svm_fifo_alloc (min_size);
669  ssvm_pop_heap (oldheap);
670  if (f)
671  {
672  clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
673  fsh_free_bytes_sub (fsh, fifo_sz);
674  goto done;
675  }
676  fsh_check_mem (fsh);
677  }
678  /* All failed, try to allocate min of data bytes and fifo sz */
679  fifo_sz = clib_min (fifo_sz, data_bytes);
680  if (fifo_sz <= fss->n_fl_chunk_bytes)
681  f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
682 
683 done:
685 
686  if (f)
687  {
688  f->size = data_bytes;
689  f->fs_hdr = fsh;
690  }
691  return f;
692 }
693 
695 fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
696 {
699  int fl_index;
700 
701  fl_index = fs_freelist_for_size (chunk_size);
702  fss = fsh_slice_get (fsh, slice_index);
703 
705 
706  ASSERT (vec_len (fss->free_chunks) > fl_index);
707  c = fss->free_chunks[fl_index];
708 
709  if (c)
710  {
711  fss->free_chunks[fl_index] = c->next;
712  c->next = 0;
713  fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
715  }
716  else
717  {
718  void *oldheap;
719  uword n_free;
720  u32 batch;
721 
722  chunk_size = fs_freelist_index_to_size (fl_index);
723  n_free = fsh_n_free_bytes (fsh);
724 
725  if (chunk_size <= n_free)
726  {
727  oldheap = ssvm_push_heap (fsh->ssvm_sh);
728  c = svm_fifo_chunk_alloc (chunk_size);
729  ssvm_pop_heap (oldheap);
730 
731  if (c)
732  {
733  clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
734  fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
735  goto done;
736  }
737 
738  fsh_check_mem (fsh);
739  n_free = fsh_n_free_bytes (fsh);
740  }
741  if (chunk_size <= fss->n_fl_chunk_bytes)
742  {
743  c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
744  if (c)
745  goto done;
746  batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
747  if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
748  {
749  fsh_check_mem (fsh);
750  goto done;
751  }
752  }
753  if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
754  {
755  u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
756 
757  batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
758  batch = clib_min (batch + 1, n_free / min_size);
759  if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
760  {
761  fsh_check_mem (fsh);
762  goto done;
763  }
764  c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
765  }
766  }
767 
768 done:
769 
771 
772  return c;
773 }
774 
775 static void
778 {
779  svm_fifo_chunk_t *next;
780  int fl_index;
781  u32 n_collect = 0;
782 
784 
785  while (c)
786  {
787  CLIB_MEM_UNPOISON (c, sizeof (*c));
788  next = c->next;
789  fl_index = fs_freelist_for_size (c->length);
790  c->next = fss->free_chunks[fl_index];
793  fss->free_chunks[fl_index] = c;
794  n_collect += fs_freelist_index_to_size (fl_index);
795  c = next;
796  }
797 
798  fss->n_fl_chunk_bytes += n_collect;
799  fsh_cached_bytes_add (fsh, n_collect);
800 
802 }
803 
804 void
807 {
809  fss = fsh_slice_get (fsh, slice_index);
810  fsh_slice_collect_chunks (fsh, fss, c);
811 }
812 
813 static inline void
815 {
816  if (fss->fifos)
817  {
818  fss->fifos->prev = f;
819  f->next = fss->fifos;
820  }
821  fss->fifos = f;
822 }
823 
824 static inline void
826 {
827  if (f->flags & SVM_FIFO_F_LL_TRACKED)
828  {
829  if (f->prev)
830  f->prev->next = f->next;
831  else
832  fss->fifos = f->next;
833  if (f->next)
834  f->next->prev = f->prev;
835  }
836 }
837 
838 /**
839  * Allocate fifo in fifo segment
840  */
841 svm_fifo_t *
843  u32 data_bytes, fifo_segment_ftype_t ftype)
844 {
845  fifo_segment_header_t *fsh = fs->h;
847  svm_fifo_t *f = 0;
848 
849  ASSERT (slice_index < fs->n_slices);
850 
851  if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
852  return 0;
853 
854  fss = fsh_slice_get (fsh, slice_index);
855  f = fs_try_alloc_fifo (fsh, fss, data_bytes);
856  if (!f)
857  goto done;
858 
859  f->slice_index = slice_index;
860 
861  svm_fifo_init (f, data_bytes);
862 
863  /* If rx fifo type add to active fifos list. When cleaning up segment,
864  * we need a list of active sessions that should be disconnected. Since
865  * both rx and tx fifos keep pointers to the session, it's enough to track
866  * only one. */
867  if (ftype == FIFO_SEGMENT_RX_FIFO)
868  {
869  fss_fifo_add_active_list (fss, f);
870  f->flags |= SVM_FIFO_F_LL_TRACKED;
871 
872  svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
873  }
874  else
875  {
876  svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
877  }
878 
879  fsh_active_fifos_update (fsh, 1);
880  fss->virtual_mem += svm_fifo_size (f);
881 
882 done:
883  return (f);
884 }
885 
886 /**
887  * Free fifo allocated in fifo segment
888  */
889 void
891 {
892  fifo_segment_header_t *fsh = fs->h;
894 
895  ASSERT (f->refcnt > 0);
896 
897  if (--f->refcnt > 0)
898  return;
899 
900  fss = fsh_slice_get (fsh, f->slice_index);
901 
902  /* Remove from active list. Only rx fifos are tracked */
903  if (f->flags & SVM_FIFO_F_LL_TRACKED)
904  {
905  fss_fifo_del_active_list (fss, f);
906  f->flags &= ~SVM_FIFO_F_LL_TRACKED;
907  }
908 
909  /* Free fifo chunks */
910  fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
911 
912  f->start_chunk = f->end_chunk = 0;
913  f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
914 
915  /* not allocated on segment heap */
918 
919  if (CLIB_DEBUG)
920  {
921  f->master_session_index = ~0;
922  f->master_thread_index = ~0;
923  }
924 
925  fss->virtual_mem -= svm_fifo_size (f);
926 
927  /* Add to free list */
928  f->next = fss->free_fifos;
929  f->prev = 0;
930  fss->free_fifos = f;
931 
932  fsh_active_fifos_update (fsh, -1);
933 }
934 
935 void
937 {
940  u32 fl_index;
941 
942  ASSERT (f->refcnt == 1);
943 
944  fss = fsh_slice_get (fs->h, f->slice_index);
945  fss->virtual_mem -= svm_fifo_size (f);
946  if (f->flags & SVM_FIFO_F_LL_TRACKED)
947  fss_fifo_del_active_list (fss, f);
948 
949  c = f->start_chunk;
950  while (c)
951  {
952  fl_index = fs_freelist_for_size (c->length);
953  clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
954  c = c->next;
955  }
956 }
957 
958 void
960  u32 slice_index)
961 {
964  u32 fl_index;
965 
966  f->slice_index = slice_index;
967  fss = fsh_slice_get (fs->h, f->slice_index);
968  fss->virtual_mem += svm_fifo_size (f);
969  if (f->flags & SVM_FIFO_F_LL_TRACKED)
970  fss_fifo_add_active_list (fss, f);
971 
972  c = f->start_chunk;
973  while (c)
974  {
975  fl_index = fs_freelist_for_size (c->length);
976  clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
977  c = c->next;
978  }
979 }
980 
981 int
983  u32 batch_size)
984 {
985  fifo_segment_header_t *fsh = fs->h;
987  svm_fifo_t *f;
988  void *oldheap;
989  uword size;
990  u8 *fmem;
991  int i;
992 
993  fss = fsh_slice_get (fsh, slice_index);
994  size = (uword) (sizeof (*f)) * batch_size;
995 
996  oldheap = ssvm_push_heap (fsh->ssvm_sh);
998  0 /* align_offset */ ,
999  0 /* os_out_of_memory */ );
1000  ssvm_pop_heap (oldheap);
1001 
1002  /* Out of space.. */
1003  if (fmem == 0)
1004  return -1;
1005 
1006  /* Carve fifo + chunk space */
1007  for (i = 0; i < batch_size; i++)
1008  {
1009  f = (svm_fifo_t *) fmem;
1010  memset (f, 0, sizeof (*f));
1011  f->next = fss->free_fifos;
1012  fss->free_fifos = f;
1013  fmem += sizeof (*f);
1014  }
1015 
1016  fsh_free_bytes_sub (fsh, size);
1017 
1018  return 0;
1019 }
1020 
1021 int
1023  u32 chunk_size, u32 batch_size)
1024 {
1025  fifo_segment_header_t *fsh = fs->h;
1026  u32 rounded_data_size, fl_index;
1027  fifo_segment_slice_t *fss;
1029  void *oldheap;
1030  uword size;
1031  u8 *cmem;
1032  int i;
1033 
1034  if (!fs_chunk_size_is_valid (fsh, chunk_size))
1035  {
1036  clib_warning ("chunk size out of range %d", chunk_size);
1037  return -1;
1038  }
1039 
1040  fl_index = fs_freelist_for_size (chunk_size);
1041  rounded_data_size = fs_freelist_index_to_size (fl_index);
1042  size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
1043 
1044  oldheap = ssvm_push_heap (fsh->ssvm_sh);
1046  0 /* align_offset */ ,
1047  0 /* os_out_of_memory */ );
1048  ssvm_pop_heap (oldheap);
1049 
1050  /* Out of space.. */
1051  if (cmem == 0)
1052  return -1;
1053 
1054  fss = fsh_slice_get (fsh, slice_index);
1055 
1056  /* Carve fifo + chunk space */
1057  for (i = 0; i < batch_size; i++)
1058  {
1059  c = (svm_fifo_chunk_t *) cmem;
1060  c->start_byte = 0;
1061  c->length = rounded_data_size;
1062  c->next = fss->free_chunks[fl_index];
1063  fss->free_chunks[fl_index] = c;
1064  cmem += sizeof (*c) + rounded_data_size;
1065  fsh_cached_bytes_add (fsh, rounded_data_size);
1066  }
1067 
1068  fss->num_chunks[fl_index] += batch_size;
1069  fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
1070  fsh_free_bytes_sub (fsh, size);
1071 
1072  return 0;
1073 }
1074 
1075 /**
1076  * Pre-allocates fifo pairs in fifo segment
1077  */
1078 void
1080  u32 rx_fifo_size, u32 tx_fifo_size,
1081  u32 * n_fifo_pairs)
1082 {
1083  u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
1084  u32 hdrs, pairs_per_slice, alloc_now;
1085  fifo_segment_header_t *fsh = fs->h;
1086  int rx_fl_index, tx_fl_index, i;
1087  fifo_segment_slice_t *fss;
1088  uword space_available;
1089 
1090  /* Parameter check */
1091  if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1092  return;
1093 
1094  if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
1095  {
1096  clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1097  return;
1098  }
1099 
1100  if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
1101  {
1102  clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1103  return;
1104  }
1105 
1106  rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
1107  rx_fl_index = fs_freelist_for_size (rx_fifo_size);
1108  tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
1109  tx_fl_index = fs_freelist_for_size (tx_fifo_size);
1110 
1111  hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
1112 
1113  /* Calculate space requirements */
1114  pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
1115  space_available = fsh_free_space (fsh);
1116  pairs_to_alloc = space_available / pair_size;
1117  pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
1118  pairs_per_slice = pairs_to_alloc / fs->n_slices;
1119  pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
1120 
1121  if (!pairs_per_slice)
1122  return;
1123 
1124  for (i = 0; i < fs->n_slices; i++)
1125  {
1126  fss = fsh_slice_get (fsh, i);
1127  alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1128  if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1129  clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1130  if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1131  clib_warning ("tx prealloc failed: pairs %u", alloc_now);
1132 
1133  /* Account for the pairs allocated */
1134  *n_fifo_pairs -= alloc_now;
1135  }
1136 }
1137 
1138 /**
1139  * Get number of active fifos
1140  */
1141 u32
1143 {
1144  return fsh_n_active_fifos (fs->h);
1145 }
1146 
1147 static u32
1149 {
1150  svm_fifo_t *f;
1151  u32 count = 0;
1152 
1153  f = fss->free_fifos;
1154  if (f == 0)
1155  return 0;
1156 
1157  while (f)
1158  {
1159  f = f->next;
1160  count++;
1161  }
1162  return count;
1163 }
1164 
1165 u32
1167 {
1168  fifo_segment_header_t *fsh = fs->h;
1169  fifo_segment_slice_t *fss;
1170  int slice_index;
1171  u32 count = 0;
1172 
1173  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1174  {
1175  fss = fsh_slice_get (fsh, slice_index);
1176  count += fs_slice_num_free_fifos (fss);
1177  }
1178  return count;
1179 }
1180 
1181 static u32
1183 {
1184  u32 count = 0, rounded_size, fl_index;
1186  int i;
1187 
1188  /* Count all free chunks? */
1189  if (size == ~0)
1190  {
1191  for (i = 0; i < vec_len (fss->free_chunks); i++)
1192  {
1193  c = fss->free_chunks[i];
1194  if (c == 0)
1195  continue;
1196 
1197  while (c)
1198  {
1199  c = c->next;
1200  count++;
1201  }
1202  }
1203  return count;
1204  }
1205 
1206  rounded_size = (1 << (max_log2 (size)));
1207  fl_index = fs_freelist_for_size (rounded_size);
1208 
1209  if (fl_index >= vec_len (fss->free_chunks))
1210  return 0;
1211 
1212  c = fss->free_chunks[fl_index];
1213  if (c == 0)
1214  return 0;
1215 
1216  while (c)
1217  {
1218  c = c->next;
1219  count++;
1220  }
1221  return count;
1222 }
1223 
1224 u32
1226 {
1227  fifo_segment_header_t *fsh = fs->h;
1228  fifo_segment_slice_t *fss;
1229  int slice_index;
1230  u32 count = 0;
1231 
1232  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1233  {
1234  fss = fsh_slice_get (fsh, slice_index);
1235  count += fs_slice_num_free_chunks (fss, size);
1236  }
1237  return count;
1238 }
1239 
1240 void
1242 {
1243  fsh_update_free_bytes (fs->h);
1244 }
1245 
1246 uword
1248 {
1249  return fs->ssvm.ssvm_size;
1250 }
1251 
1252 u8
1254 {
1255  return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1256 }
1257 
1258 void
1260 {
1262 }
1263 
1264 uword
1266 {
1267  return fsh_n_free_bytes (fs->h);
1268 }
1269 
1270 uword
1272 {
1273  return fsh_n_cached_bytes (fs->h);
1274 }
1275 
1276 uword
1278 {
1279  return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1280 }
1281 
1282 uword
1284 {
1285  fifo_segment_header_t *fsh = fs->h;
1286  fifo_segment_slice_t *fss;
1287  uword n_bytes = 0;
1288  int slice_index;
1289 
1290  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1291  {
1292  fss = fsh_slice_get (fsh, slice_index);
1293  n_bytes += fss->n_fl_chunk_bytes;
1294  }
1295 
1296  return n_bytes;
1297 }
1298 
1299 u8
1301 {
1302  return (fsh_n_active_fifos (fs->h) != 0);
1303 }
1304 
1305 svm_fifo_t *
1307 {
1308  fifo_segment_header_t *fsh = fs->h;
1309  fifo_segment_slice_t *fss;
1310 
1311  fss = fsh_slice_get (fsh, slice_index);
1312  return fss->fifos;
1313 }
1314 
1315 u8
1317 {
1318  uword size, in_use;
1319 
1320  size = fifo_segment_size (fs);
1321  in_use =
1323  return (in_use * 100) / size;
1324 }
1325 
1328 {
1329  if (!fsh->high_watermark || !fsh->low_watermark)
1330  return MEMORY_PRESSURE_NO_PRESSURE;
1331 
1332  /* once the no-memory is detected, the status continues
1333  * until memory usage gets below the high watermark
1334  */
1335  if (fsh_has_reached_mem_limit (fsh))
1336  {
1337  if (usage >= fsh->high_watermark)
1338  return MEMORY_PRESSURE_NO_MEMORY;
1339  else
1340  fsh_reset_mem_limit (fsh);
1341  }
1342 
1343  if (usage >= fsh->high_watermark)
1344  return MEMORY_PRESSURE_HIGH_PRESSURE;
1345 
1346  else if (usage >= fsh->low_watermark)
1347  return MEMORY_PRESSURE_LOW_PRESSURE;
1348 
1349  return MEMORY_PRESSURE_NO_PRESSURE;
1350 }
1351 
1354 {
1355  fifo_segment_header_t *fsh = fs->h;
1357 
1358  return fifo_segment_determine_status (fsh, usage);
1359 }
1360 
1361 u8 *
1362 format_fifo_segment_type (u8 * s, va_list * args)
1363 {
1364  fifo_segment_t *sp;
1365  sp = va_arg (*args, fifo_segment_t *);
1366  ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1367 
1368  if (st == SSVM_SEGMENT_PRIVATE)
1369  s = format (s, "%s", "private-heap");
1370  else if (st == SSVM_SEGMENT_MEMFD)
1371  s = format (s, "%s", "memfd");
1372  else if (st == SSVM_SEGMENT_SHM)
1373  s = format (s, "%s", "shm");
1374  else
1375  s = format (s, "%s", "unknown");
1376  return s;
1377 }
1378 
1379 /**
1380  * Segment format function
1381  */
1382 u8 *
1383 format_fifo_segment (u8 * s, va_list * args)
1384 {
1385  u32 count, indent, active_fifos, free_fifos;
1386  fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
1387  int verbose __attribute__ ((unused)) = va_arg (*args, int);
1388  uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1389  uword chunk_bytes = 0, free_seg_bytes, chunk_size;
1390  uword tracked_cached_bytes;
1391  uword fifo_hdr = 0, reserved;
1392  fifo_segment_header_t *fsh;
1393  fifo_segment_slice_t *fss;
1395  u32 slice_index;
1396  char *address;
1397  size_t size;
1398  int i;
1399  uword allocated, in_use, virt;
1400  f64 usage;
1402 
1403  indent = format_get_indent (s) + 2;
1404 
1405  if (fs == 0)
1406  {
1407  s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1408  "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1409  return s;
1410  }
1411 
1412  fifo_segment_info (fs, &address, &size);
1413  active_fifos = fifo_segment_num_fifos (fs);
1414  free_fifos = fifo_segment_num_free_fifos (fs);
1415 
1416  s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1417  format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1418  free_fifos, address);
1419 
1420  if (!verbose)
1421  return s;
1422 
1423  fsh = fs->h;
1424 
1425  free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1426  if (free_chunks)
1427  s =
1428  format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1429  indent + 2);
1430  else
1431  s = format (s, "\n");
1432 
1433  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1434  {
1435  fss = fsh_slice_get (fsh, slice_index);
1436  for (i = 0; i < vec_len (fss->free_chunks); i++)
1437  {
1438  c = fss->free_chunks[i];
1439  if (c == 0 && fss->num_chunks[i] == 0)
1440  continue;
1441  count = 0;
1442  while (c)
1443  {
1444  c = c->next;
1445  count++;
1446  }
1447 
1448  chunk_size = fs_freelist_index_to_size (i);
1449  s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1450  chunk_size >> 10, count, fss->num_chunks[i]);
1451 
1452  chunk_bytes += count * chunk_size;
1453  }
1454  }
1455 
1456  fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1457  est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
1458  est_free_seg_bytes = fifo_segment_free_bytes (fs);
1460  free_seg_bytes = fifo_segment_free_bytes (fs);
1461  tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1462  allocated = fifo_segment_size (fs);
1463  in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1464  usage = (100.0 * in_use) / allocated;
1465  mem_st = fifo_segment_get_mem_status (fs);
1466  virt = fsh_virtual_mem (fsh);
1467  reserved = fsh->n_reserved_bytes;
1468 
1469  s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1470  " %U (%lu)\n", format_white_space, indent + 2,
1471  format_memory_size, free_seg_bytes, free_seg_bytes,
1472  format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1473  format_memory_size, reserved, reserved);
1474  s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1475  " %U (%lu)\n", format_white_space, indent + 2,
1476  format_memory_size, chunk_bytes, chunk_bytes,
1477  format_memory_size, est_chunk_bytes, est_chunk_bytes,
1478  format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1479  s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1480  format_white_space, indent + 2, fsh->n_active_fifos,
1481  format_memory_size, fifo_hdr, fifo_hdr);
1482  s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1483  format_white_space, indent + 2, usage, format_memory_size,
1484  in_use, format_memory_size, allocated, format_memory_size, virt,
1486  s = format (s, "\n");
1487 
1488  return s;
1489 }
1490 
1491 /*
1492  * fd.io coding-style-patch-verification: ON
1493  *
1494  * Local Variables:
1495  * eval: (c-set-style "gnu")
1496  * End:
1497  */
u32 length
length of chunk in bytes
Definition: fifo_types.h:32
void fifo_segment_info(fifo_segment_t *seg, char **address, size_t *size)
Definition: fifo_segment.c:285
u8 count
Definition: dhcp.api:208
u8 low_watermark
Memory pressure watermark low.
Definition: fifo_types.h:125
#define FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE
4kB min fifo size
Definition: fifo_segment.h:30
fifo_segment_header_t * h
fifo segment data
Definition: fifo_segment.h:69
#define CLIB_MEM_UNPOISON(a, s)
Definition: sanitizer.h:47
#define clib_min(x, y)
Definition: clib.h:327
static void fsh_check_mem(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:123
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:119
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
uword requested_va
Definition: ssvm.h:86
static uword fsh_n_free_bytes(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:56
static void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset, int os_out_of_memory_on_failure)
Definition: mem.h:125
a
Definition: bitmap.h:538
uword ssvm_size
Definition: ssvm.h:85
svm_fifo_t * fifo_segment_get_slice_fifo_list(fifo_segment_t *fs, u32 slice_index)
static void fss_fifo_del_active_list(fifo_segment_slice_t *fss, svm_fifo_t *f)
Definition: fifo_segment.c:825
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:255
void svm_fifo_free_chunk_lookup(svm_fifo_t *f)
Cleanup fifo chunk lookup rb tree.
Definition: svm_fifo.c:753
static u32 svm_fifo_size(svm_fifo_t *f)
Definition: svm_fifo.h:666
volatile u32 ready
Definition: ssvm.h:77
ssvm_shared_header_t * ssvm_sh
Fixed array of slices.
Definition: fifo_types.h:116
uword fifo_segment_cached_bytes(fifo_segment_t *fs)
Fifo segment number of cached bytes.
unsigned long u64
Definition: types.h:89
u8 pct_first_alloc
Pct of fifo size to alloc.
Definition: fifo_types.h:126
u8 n_slices
Number of slices.
Definition: fifo_types.h:123
void fifo_segment_delete(fifo_segment_main_t *sm, fifo_segment_t *s)
Definition: fifo_segment.c:265
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
fifo_segment_mem_status_t
Definition: fifo_segment.h:48
svm_fifo_t * fifo_segment_alloc_fifo_w_slice(fifo_segment_t *fs, u32 slice_index, u32 data_bytes, fifo_segment_ftype_t ftype)
Allocate fifo in fifo segment.
Definition: fifo_segment.c:842
u8 fifo_segment_get_mem_usage(fifo_segment_t *fs)
#define foreach_segment_mem_status
Definition: fifo_segment.h:42
u32 max_log2_chunk_size
Max log2(chunk size) for fs.
Definition: fifo_types.h:121
void * opaque[SSVM_N_OPAQUE]
Definition: ssvm.h:74
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
svm_fifo_chunk_t * fsh_alloc_chunk(fifo_segment_header_t *fsh, u32 slice_index, u32 chunk_size)
Allocate chunks in fifo segment.
Definition: fifo_segment.c:695
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
void svm_fifo_init(svm_fifo_t *f, u32 size)
Initialize fifo.
Definition: svm_fifo.c:370
static void usage(void)
Definition: health_check.c:14
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define RBTREE_TNIL_INDEX
Definition: rbtree.h:22
void fsh_virtual_mem_update(fifo_segment_header_t *fsh, u32 slice_index, int n_bytes)
Definition: fifo_segment.c:115
ssvm_shared_header_t * sh
Definition: ssvm.h:84
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
DLMALLOC_EXPORT struct dlmallinfo mspace_mallinfo(mspace msp)
static u32 fs_slice_num_free_fifos(fifo_segment_slice_t *fss)
static u32 fs_freelist_index_to_size(u32 fl_index)
Definition: fifo_segment.c:308
static int fs_try_alloc_fifo_batch(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, u32 fl_index, u32 batch_size)
Definition: fifo_segment.c:549
unsigned char u8
Definition: types.h:56
u32 fifo_segment_num_free_fifos(fifo_segment_t *fs)
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:427
static void fsh_update_free_bytes(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:63
double f64
Definition: types.h:142
uword next_baseva
Where to put the next one.
Definition: fifo_segment.h:76
u32 fifo_segment_num_free_chunks(fifo_segment_t *fs, u32 size)
Find number of free chunks of given size.
fifo_segment_t * fifo_segment_get_segment(fifo_segment_main_t *sm, u32 segment_index)
Definition: fifo_segment.c:279
static fifo_segment_slice_t * fsh_slice_get(fifo_segment_header_t *fsh, u32 slice_index)
Definition: fifo_segment.c:19
u8 * ssvm_name(const ssvm_private_t *ssvm)
Definition: ssvm.c:439
enum ssvm_segment_type_ ssvm_segment_type_t
u32 n_active_fifos
Number of active fifos.
Definition: fifo_types.h:119
uword virtual_mem
Slice sum of all fifo sizes.
Definition: fifo_types.h:109
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
svm_fifo_chunk_t * svm_fifo_chunk_alloc(u32 size)
Creates a fifo chunk in the current heap.
Definition: svm_fifo.c:457
static u32 fs_freelist_for_size(u32 size)
Definition: fifo_segment.c:300
static svm_fifo_t * fs_try_alloc_fifo_freelist(fifo_segment_slice_t *fss, u32 fl_index)
Definition: fifo_segment.c:325
static int fs_chunk_size_is_valid(fifo_segment_header_t *fsh, u32 size)
Definition: fifo_segment.c:314
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:209
void svm_fifo_free_ooo_data(svm_fifo_t *f)
Cleanup fifo ooo data.
Definition: svm_fifo.c:113
static svm_fifo_t * fs_try_alloc_fifo_freelist_multi_chunk(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, u32 data_bytes)
Definition: fifo_segment.c:412
uword n_fl_chunk_bytes
Chunk bytes on freelist.
Definition: fifo_types.h:108
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:144
static void fsh_cached_bytes_add(fifo_segment_header_t *fsh, int size)
Definition: fifo_segment.c:69
unsigned int u32
Definition: types.h:88
int attach_timeout
shm segments attach timeout (sec)
Definition: ssvm.h:95
static void fsh_cached_bytes_sub(fifo_segment_header_t *fsh, int size)
Definition: fifo_segment.c:75
uword n_cached_bytes
Cached bytes.
Definition: fifo_types.h:118
int ssvm_master_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:415
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:152
char * segment_name
segment name
Definition: fifo_segment.h:85
u8 high_watermark
Memory pressure watermark high.
Definition: fifo_types.h:124
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
fifo_segment_mem_status_t fifo_segment_get_mem_status(fifo_segment_t *fs)
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
int fifo_segment_attach(fifo_segment_main_t *sm, fifo_segment_create_args_t *a)
Attach as slave to a fifo segment.
Definition: fifo_segment.c:235
void fifo_segment_preallocate_fifo_pairs(fifo_segment_t *fs, u32 rx_fifo_size, u32 tx_fifo_size, u32 *n_fifo_pairs)
Pre-allocates fifo pairs in fifo segment.
int fifo_segment_prealloc_fifo_chunks(fifo_segment_t *fs, u32 slice_index, u32 chunk_size, u32 batch_size)
Try to preallocate fifo chunks on segment.
u8 * format_fifo_segment_type(u8 *s, va_list *args)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
static void fss_fifo_add_active_list(fifo_segment_slice_t *fss, svm_fifo_t *f)
Definition: fifo_segment.c:814
struct svm_fifo_chunk_ * next
pointer to next chunk in linked-lists
Definition: fifo_types.h:33
void fsh_collect_chunks(fifo_segment_header_t *fsh, u32 slice_index, svm_fifo_chunk_t *c)
Return chunks to fifo segment.
Definition: fifo_segment.c:805
static uword fsh_free_space(fifo_segment_header_t *fsh)
Fifo segment free space.
Definition: fifo_segment.c:41
svm_fifo_t * free_fifos
Freelists by fifo size.
Definition: fifo_types.h:105
u32 n_reserved_bytes
Bytes not to be allocated.
Definition: fifo_types.h:120
u8 * format_fifo_segment(u8 *s, va_list *args)
Segment format function.
u32 size
Definition: vhost_user.h:106
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
fifo_segment_slice_t * slices
Definition: fifo_types.h:115
#define PREDICT_FALSE(x)
Definition: clib.h:120
#define clib_atomic_fetch_add_rel(a, b)
Definition: atomics.h:54
clib_spinlock_t chunk_lock
Definition: fifo_types.h:110
static u32 fsh_n_active_fifos(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:94
int fifo_segment_create(fifo_segment_main_t *sm, fifo_segment_create_args_t *a)
Create a fifo segment and initialize as master.
Definition: fifo_segment.c:201
u32 * num_chunks
Allocated chunks by chunk size.
Definition: fifo_types.h:107
void fifo_segment_detach_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Definition: fifo_segment.c:936
#define FIFO_SEGMENT_MIN_FIFO_SIZE
4kB min fifo size
Definition: fifo_segment.h:31
svm_fifo_t * fifos
Linked list of active RX fifos.
Definition: fifo_types.h:104
static int fsh_try_alloc_chunk_batch(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, u32 fl_index, u32 batch_size)
Definition: fifo_segment.c:503
u32 segment_size
size of the segment
Definition: fifo_segment.h:83
ssvm_private_t ssvm
ssvm segment data
Definition: fifo_segment.h:68
svmdb_client_t * c
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
static uword fsh_n_cached_bytes(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:81
void fifo_segment_main_init(fifo_segment_main_t *sm, u64 baseva, u32 timeout_in_seconds)
Definition: fifo_segment.c:292
#define clib_warning(format, args...)
Definition: error.h:59
uword fifo_segment_fl_chunk_bytes(fifo_segment_t *fs)
Number of bytes on chunk free lists.
int ssvm_slave_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:421
u32 my_pid
Definition: ssvm.h:87
u32 start_byte
chunk start byte
Definition: fifo_types.h:31
int memfd_fd
fd for memfd segments
Definition: fifo_segment.h:84
int fd
memfd segments
Definition: ssvm.h:94
u8 n_slices
number of fifo segment slices
Definition: fifo_segment.h:70
#define clib_atomic_load_relax_n(a)
Definition: atomics.h:47
void fsh_reset_mem_limit(fifo_segment_header_t *fsh)
Fifo segment reset mem limit flag.
u32 fifo_segment_index(fifo_segment_main_t *sm, fifo_segment_t *s)
Definition: fifo_segment.c:273
static void * clib_mem_alloc_aligned_or_null(uword size, uword align)
Definition: mem.h:181
#define ASSERT(truth)
manual_print typedef address
Definition: ip_types.api:85
void fifo_segment_free_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Free fifo allocated in fifo segment.
Definition: fifo_segment.c:890
rb_node_index_t deq_rb_index
deq node index if chunk in rbtree
Definition: fifo_types.h:35
fifo_segment_t * segments
pool of fifo segments
Definition: fifo_segment.h:75
fifo_segment_ftype_t
Definition: fifo_segment.h:22
#define FIFO_SEGMENT_MAX_FIFO_SIZE
2GB max fifo size
Definition: fifo_segment.h:32
static uword fsh_virtual_mem(fifo_segment_header_t *fsh)
Definition: fifo_segment.c:100
static u32 fs_slice_num_free_chunks(fifo_segment_slice_t *fss, u32 size)
static svm_fifo_t * fs_try_alloc_fifo(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, u32 data_bytes)
Try to allocate new fifo.
Definition: fifo_segment.c:616
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
uword fifo_segment_size(fifo_segment_t *fs)
Fifo segment allocated size.
#define clib_atomic_store_rel_n(a, b)
Definition: atomics.h:49
#define clib_max(x, y)
Definition: clib.h:320
u8 * name
Definition: ssvm.h:88
#define clib_atomic_fetch_sub_rel(a, b)
Definition: atomics.h:55
static void fsh_slice_collect_chunks(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, svm_fifo_chunk_t *c)
Definition: fifo_segment.c:776
ssvm_segment_type_t segment_type
type of segment requested
Definition: fifo_segment.h:82
u32 fifo_segment_num_fifos(fifo_segment_t *fs)
Get number of active fifos.
uword ssvm_size
Definition: ssvm.h:70
static void fsh_free_bytes_sub(fifo_segment_header_t *fsh, int size)
Definition: fifo_segment.c:50
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword max_log2(uword x)
Definition: clib.h:208
u8 flags
Segment flags.
Definition: fifo_types.h:122
u64 uword
Definition: types.h:112
int fifo_segment_prealloc_fifo_hdrs(fifo_segment_t *fs, u32 slice_index, u32 batch_size)
Try to preallocate fifo headers.
Definition: fifo_segment.c:982
rb_node_index_t enq_rb_index
enq node index if chunk in rbtree
Definition: fifo_types.h:34
void fifo_segment_update_free_bytes(fifo_segment_t *fs)
Update fifo segment free bytes estimate.
u8 fsh_has_reached_mem_limit(fifo_segment_header_t *fsh)
Fifo segment has reached mem limit.
u8 fifo_segment_has_fifos(fifo_segment_t *fs)
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:165
static char * fifo_segment_mem_status_strings[]
Definition: fifo_segment.c:24
#define FIFO_SEGMENT_ALLOC_BATCH_SIZE
Definition: fifo_segment.h:33
void fifo_segment_attach_fifo(fifo_segment_t *fs, svm_fifo_t *f, u32 slice_index)
Definition: fifo_segment.c:959
uword fifo_segment_available_bytes(fifo_segment_t *fs)
u32 timeout_in_seconds
Time to wait during attach.
Definition: fifo_segment.h:77
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
int fifo_segment_init(fifo_segment_t *fs)
Initialize fifo segment shared header.
Definition: fifo_segment.c:143
static void fsh_active_fifos_update(fifo_segment_header_t *fsh, int inc)
Definition: fifo_segment.c:88
struct _svm_fifo svm_fifo_t
fifo_segment_mem_status_t fifo_segment_determine_status(fifo_segment_header_t *fsh, u8 usage)
svm_fifo_chunk_t * fs_try_alloc_multi_chunk(fifo_segment_header_t *fsh, fifo_segment_slice_t *fss, u32 data_bytes)
Definition: fifo_segment.c:349
int i_am_master
Definition: ssvm.h:90
uword fifo_segment_free_bytes(fifo_segment_t *fs)
Fifo segment estimate of number of free bytes.
svm_fifo_chunk_t ** free_chunks
Freelists by chunk size.
Definition: fifo_types.h:106
uword n_free_bytes
Segment free bytes.
Definition: fifo_types.h:117
void svm_fifo_init_ooo_lookup(svm_fifo_t *f, u8 ooo_type)
Initialize rbtrees used for ooo lookups.
Definition: svm_fifo.c:403
struct svm_fifo_chunk_ svm_fifo_chunk_t
u32 * new_segment_indices
return vec of new seg indices
Definition: fifo_segment.h:86
svm_fifo_t * svm_fifo_alloc(u32 data_size_in_bytes)
Creates a fifo in the current heap.
Definition: svm_fifo.c:421
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:433