blob: 3fd7d9d6ede76a8f2de6e1647c76b4cc0f42fa7a [file] [log] [blame]
Florin Coras88001c62019-04-24 14:44:46 -07001/*
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
Florin Coras06d47752020-03-06 02:23:58 +000018static inline fifo_segment_slice_t *
19fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
20{
21 return &fsh->slices[slice_index];
22}
23
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000024static char *fifo_segment_mem_status_strings[] = {
25#define _(sym,str) str,
26 foreach_segment_mem_status
27#undef _
28};
29
Florin Coras88001c62019-04-24 14:44:46 -070030/**
Florin Corasf9d4ab42019-05-11 16:55:53 -070031 * 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 */
Florin Corasef4f3e72019-12-11 14:27:53 -080040static uword
Florin Coras4453c092019-12-19 10:13:15 -080041fsh_free_space (fifo_segment_header_t * fsh)
Florin Corasf9d4ab42019-05-11 16:55:53 -070042{
43 struct dlmallinfo dlminfo;
44
Florin Coras4453c092019-12-19 10:13:15 -080045 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070046 return dlminfo.fordblks;
47}
48
Florin Coras62ddc032019-12-08 18:30:42 -080049static inline void
50fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
51{
52 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
53}
54
55static inline uword
56fsh_n_free_bytes (fifo_segment_header_t * fsh)
57{
Florin Coras8122cc22019-12-18 13:06:41 -080058 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
62static inline void
Florin Coras4453c092019-12-19 10:13:15 -080063fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080064{
Florin Coras4453c092019-12-19 10:13:15 -080065 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080066}
67
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000068static inline void
69fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
70{
71 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
72}
73
74static inline void
75fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
76{
77 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
78}
79
80static inline uword
81fsh_n_cached_bytes (fifo_segment_header_t * fsh)
82{
83 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000084 return n_cached;
85}
86
Florin Coras06d47752020-03-06 02:23:58 +000087static inline void
88fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
89{
90 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
91}
92
Florin Coras7a90e502020-04-09 04:46:14 +000093static inline u32
94fsh_n_active_fifos (fifo_segment_header_t * fsh)
95{
96 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
97}
98
Florin Coras06d47752020-03-06 02:23:58 +000099static inline uword
100fsh_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
114void
115fsh_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
Florin Coras8122cc22019-12-18 13:06:41 -0800122static void
123fsh_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;
Florin Coras4453c092019-12-19 10:13:15 -0800136 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800137}
138
Florin Corasf9d4ab42019-05-11 16:55:53 -0700139/**
Florin Coras88001c62019-04-24 14:44:46 -0700140 * Initialize fifo segment shared header
141 */
142int
143fifo_segment_init (fifo_segment_t * fs)
144{
145 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800146 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700147 ssvm_shared_header_t *sh;
Florin Coras05c73172020-03-05 20:36:40 +0000148 u32 max_chunk_sz;
Florin Coras62ddc032019-12-08 18:30:42 -0800149 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700150 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800151 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700152
153 sh = fs->ssvm.sh;
154 oldheap = ssvm_push_heap (sh);
155
Florin Coras62ddc032019-12-08 18:30:42 -0800156 /*
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));
Florin Coras88001c62019-04-24 14:44:46 -0700166 clib_memset (fsh, 0, sizeof (*fsh));
167 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800168 fs->n_slices = clib_max (fs->n_slices, 1);
169
170 fsh->ssvm_sh = fs->ssvm.sh;
171 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800172 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800173 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);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000184 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800185 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800186 }
Florin Coras88001c62019-04-24 14:44:46 -0700187
188 ssvm_pop_heap (oldheap);
189
Florin Coras4453c092019-12-19 10:13:15 -0800190 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000191 fsh->n_cached_bytes = 0;
Florin Coras05c73172020-03-05 20:36:40 +0000192 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700193 sh->ready = 1;
194 return (0);
195}
196
197/**
Florin Coras88001c62019-04-24 14:44:46 -0700198 * Create a fifo segment and initialize as master
199 */
200int
201fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
202{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700203 fifo_segment_t *fs;
204 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700205 int rv;
206
Florin Coras88001c62019-04-24 14:44:46 -0700207 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700208 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700209
Florin Corasf9d4ab42019-05-11 16:55:53 -0700210 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;
Florin Coras88001c62019-04-24 14:44:46 -0700216
Florin Corasf9d4ab42019-05-11 16:55:53 -0700217 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700218 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700219 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700220 return (rv);
221 }
222
223 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700224 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700225
Florin Corasf9d4ab42019-05-11 16:55:53 -0700226 fifo_segment_init (fs);
227 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700228 return (0);
229}
230
231/**
232 * Attach as slave to a fifo segment
233 */
234int
235fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
236{
Florin Coras62ddc032019-12-08 18:30:42 -0800237 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700238 int rv;
239
Florin Coras62ddc032019-12-08 18:30:42 -0800240 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700241
Florin Coras62ddc032019-12-08 18:30:42 -0800242 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;
Florin Coras88001c62019-04-24 14:44:46 -0700246 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800247 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700248 else
Florin Coras62ddc032019-12-08 18:30:42 -0800249 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700250
Florin Coras62ddc032019-12-08 18:30:42 -0800251 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700252 {
Florin Coras62ddc032019-12-08 18:30:42 -0800253 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700254 return (rv);
255 }
256
257 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800258 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700259
Florin Coras62ddc032019-12-08 18:30:42 -0800260 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700261 return (0);
262}
263
264void
265fifo_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
272u32
273fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
274{
275 return s - sm->segments;
276}
277
Florin Coras88001c62019-04-24 14:44:46 -0700278fifo_segment_t *
279fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
280{
281 return pool_elt_at_index (sm->segments, segment_index);
282}
283
284void
285fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
286{
Florin Coras404b8a32019-05-09 12:08:06 -0700287 *address = (char *) seg->ssvm.sh->ssvm_va;
288 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700289}
290
291void
292fifo_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
Florin Corasf9d4ab42019-05-11 16:55:53 -0700299static inline u32
300fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700301{
Florin Corasf22f4e52019-12-19 16:10:58 -0800302 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
303 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800304 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700305}
306
Florin Corasf9d4ab42019-05-11 16:55:53 -0700307static inline u32
308fs_freelist_index_to_size (u32 fl_index)
309{
Florin Coras62ddc032019-12-08 18:30:42 -0800310 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700311}
312
Florin Coras88001c62019-04-24 14:44:46 -0700313static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800314fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700315{
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
Florin Corasd18528f2020-03-27 18:41:54 +0000321 && size <= (1ULL << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700322}
323
Florin Corascefd5d82019-05-05 13:19:57 -0700324static svm_fifo_t *
Florin Coras2de9c0f2020-02-02 19:30:39 +0000325fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
Florin Corascefd5d82019-05-05 13:19:57 -0700326{
327 svm_fifo_chunk_t *c;
328 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700329
Florin Coras62ddc032019-12-08 18:30:42 -0800330 f = fss->free_fifos;
331 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700332
333 if (!f || !c)
334 return 0;
335
Florin Coras62ddc032019-12-08 18:30:42 -0800336 fss->free_fifos = f->next;
337 fss->free_chunks[fl_index] = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800338 c->next = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700339 c->start_byte = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700340 memset (f, 0, sizeof (*f));
341 f->start_chunk = c;
342 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700343
Florin Coras62ddc032019-12-08 18:30:42 -0800344 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700345 return f;
346}
347
Florin Coras8e755a12020-02-06 16:59:31 +0000348svm_fifo_chunk_t *
349fs_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
Florin Corascefd5d82019-05-05 13:19:57 -0700411static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800412fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
413 fifo_segment_slice_t * fss,
414 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700415{
Florin Corasc75ce4d2020-02-28 21:51:24 +0000416 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700417 u32 fl_index, fl_size, n_alloc = 0;
418 svm_fifo_t *f;
419
Florin Coras62ddc032019-12-08 18:30:42 -0800420 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700421 if (!f)
422 {
Florin Coras5226d262020-04-27 22:41:39 +0000423 if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t)))
424 return 0;
425
Florin Coras62ddc032019-12-08 18:30:42 -0800426 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras5226d262020-04-27 22:41:39 +0000427 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700428 ssvm_pop_heap (oldheap);
429 if (!f)
430 return 0;
431 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800432 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700433 }
Florin Coras73cad332019-08-28 17:12:32 -0700434 else
435 {
Florin Coras62ddc032019-12-08 18:30:42 -0800436 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700437 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700438
Florin Corasd849edf2019-12-20 18:48:20 -0800439 fl_index = fs_freelist_for_size (data_bytes);
440 if (fl_index > 0)
441 fl_index -= 1;
442
Florin Corasf9d4ab42019-05-11 16:55:53 -0700443 fl_size = fs_freelist_index_to_size (fl_index);
444
445 while (data_bytes)
446 {
Florin Coras62ddc032019-12-08 18:30:42 -0800447 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700448 if (c)
449 {
Florin Coras62ddc032019-12-08 18:30:42 -0800450 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700451 if (!last)
452 last = c;
453 c->next = first;
454 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700455 n_alloc += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000456 data_bytes -= clib_min (fl_size, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700457 }
458 else
459 {
Florin Corasd849edf2019-12-20 18:48:20 -0800460 /* 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);
Florin Corasc75ce4d2020-02-28 21:51:24 +0000469 next = c->next;
Florin Corasd849edf2019-12-20 18:48:20 -0800470 c->next = fss->free_chunks[fl_index];
471 fss->free_chunks[fl_index] = c;
472 fss->n_fl_chunk_bytes += fl_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000473 n_alloc -= fl_size;
Florin Corasd849edf2019-12-20 18:48:20 -0800474 data_bytes += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000475 c = next;
Florin Corasd849edf2019-12-20 18:48:20 -0800476 }
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 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700490 fl_index -= 1;
491 fl_size = fl_size >> 1;
492 }
493 }
Florin Corasd849edf2019-12-20 18:48:20 -0800494
Florin Corasf9d4ab42019-05-11 16:55:53 -0700495 f->start_chunk = first;
496 f->end_chunk = last;
Florin Coras62ddc032019-12-08 18:30:42 -0800497 fss->n_fl_chunk_bytes -= n_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000498 fsh_cached_bytes_sub (fsh, n_alloc);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700499 return f;
500}
501
502static int
Florin Coras8e755a12020-02-06 16:59:31 +0000503fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
504 fifo_segment_slice_t * fss,
505 u32 fl_index, u32 batch_size)
506{
507 u32 rounded_data_size;
508 svm_fifo_chunk_t *c;
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);
518 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
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;
533 c->enq_rb_index = RBTREE_TNIL_INDEX;
534 c->deq_rb_index = RBTREE_TNIL_INDEX;
535 c->next = fss->free_chunks[fl_index];
536 fss->free_chunks[fl_index] = c;
537 cmem += sizeof (*c) + rounded_data_size;
538 }
539
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000540 fss->num_chunks[fl_index] += batch_size;
Florin Coras8e755a12020-02-06 16:59:31 +0000541 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
548static int
Florin Coras62ddc032019-12-08 18:30:42 -0800549fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
550 fifo_segment_slice_t * fss,
551 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700552{
Florin Coras36a0c4d2020-01-31 08:28:02 -0800553 u32 hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700554 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700555 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700556 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800557 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700558 u8 *fmem;
559 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700560
Florin Corasf9d4ab42019-05-11 16:55:53 -0700561 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700562 hdrs = sizeof (*f) + sizeof (*c);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800563 size = (uword) (hdrs + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700564
Florin Coras62ddc032019-12-08 18:30:42 -0800565 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700566 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
567 0 /* align_offset */ ,
568 0 /* os_out_of_memory */ );
569 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700570
571 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700572 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700573 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700574
Florin Coras635f5062020-04-28 20:40:57 +0000575 /* Carve fifo hdr space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700576 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700577 {
Florin Corascefd5d82019-05-05 13:19:57 -0700578 f = (svm_fifo_t *) fmem;
579 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800580 f->next = fss->free_fifos;
581 fss->free_fifos = f;
Florin Coras635f5062020-04-28 20:40:57 +0000582 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;
Florin Corascefd5d82019-05-05 13:19:57 -0700589 c->start_byte = 0;
590 c->length = rounded_data_size;
Florin Corasf22f4e52019-12-19 16:10:58 -0800591 c->enq_rb_index = RBTREE_TNIL_INDEX;
592 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Coras62ddc032019-12-08 18:30:42 -0800593 c->next = fss->free_chunks[fl_index];
594 fss->free_chunks[fl_index] = c;
Florin Coras635f5062020-04-28 20:40:57 +0000595 fmem += sizeof (svm_fifo_chunk_t) + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700596 }
Florin Corascefd5d82019-05-05 13:19:57 -0700597
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000598 fss->num_chunks[fl_index] += batch_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800599 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000600 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800601 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700602
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 */
615static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800616fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
617 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700618{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700619 u32 fifo_sz, fl_index;
620 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800621 uword n_free_bytes;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000622 u32 min_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700623
Florin Coras2de9c0f2020-02-02 19:30:39 +0000624 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
625 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700626
Florin Coras0e6199d2020-04-17 20:15:22 +0000627 if (fl_index >= vec_len (fss->free_chunks))
628 return 0;
629
Florin Corasc75ce4d2020-02-28 21:51:24 +0000630 clib_spinlock_lock (&fss->chunk_lock);
631
Florin Coras62ddc032019-12-08 18:30:42 -0800632 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700633 {
Florin Coras2de9c0f2020-02-02 19:30:39 +0000634 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700635 if (f)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000636 {
637 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
638 goto done;
639 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700640 }
Florin Coras8122cc22019-12-18 13:06:41 -0800641
Florin Coras0269fa12020-03-10 04:46:32 +0000642 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
643 fifo_sz += 1 << max_log2 (min_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800644 n_free_bytes = fsh_n_free_bytes (fsh);
Florin Coras0269fa12020-03-10 04:46:32 +0000645
Florin Coras62ddc032019-12-08 18:30:42 -0800646 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700647 {
Florin Coras0269fa12020-03-10 04:46:32 +0000648 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
649 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
650 {
651 f = fs_try_alloc_fifo_freelist (fss, fl_index);
652 if (f)
653 {
654 fsh_cached_bytes_sub (fsh,
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 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700664 }
Florin Coras62ddc032019-12-08 18:30:42 -0800665 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700666 {
Florin Coras62ddc032019-12-08 18:30:42 -0800667 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000668 f = svm_fifo_alloc (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700669 ssvm_pop_heap (oldheap);
670 if (f)
671 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000672 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
Florin Coras62ddc032019-12-08 18:30:42 -0800673 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700674 goto done;
675 }
Florin Coras0269fa12020-03-10 04:46:32 +0000676 fsh_check_mem (fsh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700677 }
Florin Corasc75ce4d2020-02-28 21:51:24 +0000678 /* 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);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700682
683done:
Florin Corasc75ce4d2020-02-28 21:51:24 +0000684 clib_spinlock_unlock (&fss->chunk_lock);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700685
Florin Corasf22f4e52019-12-19 16:10:58 -0800686 if (f)
Florin Corasc75ce4d2020-02-28 21:51:24 +0000687 {
688 f->size = data_bytes;
689 f->fs_hdr = fsh;
690 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700691 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700692}
693
Florin Corasf22f4e52019-12-19 16:10:58 -0800694svm_fifo_chunk_t *
695fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
696{
697 fifo_segment_slice_t *fss;
698 svm_fifo_chunk_t *c;
Florin Corasf22f4e52019-12-19 16:10:58 -0800699 int fl_index;
700
701 fl_index = fs_freelist_for_size (chunk_size);
702 fss = fsh_slice_get (fsh, slice_index);
703
704 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras8e755a12020-02-06 16:59:31 +0000705
Florin Coras0e6199d2020-04-17 20:15:22 +0000706 ASSERT (vec_len (fss->free_chunks) > fl_index);
Florin Corasf22f4e52019-12-19 16:10:58 -0800707 c = fss->free_chunks[fl_index];
708
Florin Coras8e755a12020-02-06 16:59:31 +0000709 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);
714 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
715 }
Florin Coras0269fa12020-03-10 04:46:32 +0000716 else
Florin Corasf22f4e52019-12-19 16:10:58 -0800717 {
Florin Coras0269fa12020-03-10 04:46:32 +0000718 void *oldheap;
719 uword n_free;
Florin Coras8e755a12020-02-06 16:59:31 +0000720 u32 batch;
721
Florin Coras0269fa12020-03-10 04:46:32 +0000722 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 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000733 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
Florin Coras0269fa12020-03-10 04:46:32 +0000734 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 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800766 }
767
768done:
769
770 clib_spinlock_unlock (&fss->chunk_lock);
771
772 return c;
773}
774
775static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000776fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000777 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800778{
779 svm_fifo_chunk_t *next;
780 int fl_index;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000781 u32 n_collect = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800782
783 clib_spinlock_lock (&fss->chunk_lock);
784
Florin Coras9e61d9a2020-02-05 21:13:18 +0000785 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800786 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000787 next = c->next;
788 fl_index = fs_freelist_for_size (c->length);
789 c->next = fss->free_chunks[fl_index];
790 c->enq_rb_index = RBTREE_TNIL_INDEX;
791 c->deq_rb_index = RBTREE_TNIL_INDEX;
792 fss->free_chunks[fl_index] = c;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000793 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000794 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800795 }
796
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000797 fss->n_fl_chunk_bytes += n_collect;
798 fsh_cached_bytes_add (fsh, n_collect);
799
Florin Corasf22f4e52019-12-19 16:10:58 -0800800 clib_spinlock_unlock (&fss->chunk_lock);
801}
802
803void
804fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000805 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800806{
807 fifo_segment_slice_t *fss;
808 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000809 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800810}
811
Florin Coras6d7552c2020-04-09 01:49:45 +0000812static inline void
813fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
814{
815 if (fss->fifos)
816 {
817 fss->fifos->prev = f;
818 f->next = fss->fifos;
819 }
820 fss->fifos = f;
821}
822
823static inline void
824fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
825{
826 if (f->flags & SVM_FIFO_F_LL_TRACKED)
827 {
828 if (f->prev)
829 f->prev->next = f->next;
830 else
831 fss->fifos = f->next;
832 if (f->next)
833 f->next->prev = f->prev;
834 }
835}
836
Florin Coras88001c62019-04-24 14:44:46 -0700837/**
838 * Allocate fifo in fifo segment
839 */
840svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800841fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
842 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700843{
Florin Coras62ddc032019-12-08 18:30:42 -0800844 fifo_segment_header_t *fsh = fs->h;
845 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700846 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700847
Florin Coras62ddc032019-12-08 18:30:42 -0800848 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700849
Florin Coras0e6199d2020-04-17 20:15:22 +0000850 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
851 return 0;
852
Florin Coras62ddc032019-12-08 18:30:42 -0800853 fss = fsh_slice_get (fsh, slice_index);
854 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700855 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700856 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700857
Florin Coras62ddc032019-12-08 18:30:42 -0800858 f->slice_index = slice_index;
859
Florin Corascefd5d82019-05-05 13:19:57 -0700860 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700861
Florin Coras88001c62019-04-24 14:44:46 -0700862 /* If rx fifo type add to active fifos list. When cleaning up segment,
863 * we need a list of active sessions that should be disconnected. Since
864 * both rx and tx fifos keep pointers to the session, it's enough to track
865 * only one. */
866 if (ftype == FIFO_SEGMENT_RX_FIFO)
867 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000868 fss_fifo_add_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700869 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800870
871 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
Florin Coras88001c62019-04-24 14:44:46 -0700872 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800873 else
874 {
875 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
876 }
877
Florin Coras62ddc032019-12-08 18:30:42 -0800878 fsh_active_fifos_update (fsh, 1);
Florin Coras06d47752020-03-06 02:23:58 +0000879 fss->virtual_mem += svm_fifo_size (f);
Florin Coras88001c62019-04-24 14:44:46 -0700880
881done:
Florin Coras88001c62019-04-24 14:44:46 -0700882 return (f);
883}
884
885/**
886 * Free fifo allocated in fifo segment
887 */
888void
Florin Corasb095a3c2019-04-25 12:58:46 -0700889fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700890{
Florin Coras62ddc032019-12-08 18:30:42 -0800891 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800892 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700893
894 ASSERT (f->refcnt > 0);
895
896 if (--f->refcnt > 0)
897 return;
898
Florin Coras62ddc032019-12-08 18:30:42 -0800899 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700900
901 /* Remove from active list. Only rx fifos are tracked */
902 if (f->flags & SVM_FIFO_F_LL_TRACKED)
903 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000904 fss_fifo_del_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700905 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
906 }
907
Florin Corascefd5d82019-05-05 13:19:57 -0700908 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000909 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700910
Florin Corasf22f4e52019-12-19 16:10:58 -0800911 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700912 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
913
Florin Corasb095a3c2019-04-25 12:58:46 -0700914 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800915 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700916 svm_fifo_free_ooo_data (f);
917
Florin Coras88001c62019-04-24 14:44:46 -0700918 if (CLIB_DEBUG)
919 {
920 f->master_session_index = ~0;
921 f->master_thread_index = ~0;
922 }
923
Florin Coras06d47752020-03-06 02:23:58 +0000924 fss->virtual_mem -= svm_fifo_size (f);
925
926 /* Add to free list */
927 f->next = fss->free_fifos;
928 f->prev = 0;
929 fss->free_fifos = f;
930
Florin Coras62ddc032019-12-08 18:30:42 -0800931 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700932}
933
Florin Coras6d7552c2020-04-09 01:49:45 +0000934void
935fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
936{
937 fifo_segment_slice_t *fss;
938 svm_fifo_chunk_t *c;
939 u32 fl_index;
940
941 ASSERT (f->refcnt == 1);
942
943 fss = fsh_slice_get (fs->h, f->slice_index);
944 fss->virtual_mem -= svm_fifo_size (f);
945 if (f->flags & SVM_FIFO_F_LL_TRACKED)
946 fss_fifo_del_active_list (fss, f);
947
948 c = f->start_chunk;
949 while (c)
950 {
951 fl_index = fs_freelist_for_size (c->length);
952 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
953 c = c->next;
954 }
955}
956
957void
958fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
959 u32 slice_index)
960{
961 fifo_segment_slice_t *fss;
962 svm_fifo_chunk_t *c;
963 u32 fl_index;
964
965 f->slice_index = slice_index;
966 fss = fsh_slice_get (fs->h, f->slice_index);
967 fss->virtual_mem += svm_fifo_size (f);
968 if (f->flags & SVM_FIFO_F_LL_TRACKED)
969 fss_fifo_add_active_list (fss, f);
970
971 c = f->start_chunk;
972 while (c)
973 {
974 fl_index = fs_freelist_for_size (c->length);
975 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
976 c = c->next;
977 }
978}
979
Florin Corasf9d4ab42019-05-11 16:55:53 -0700980int
Florin Coras62ddc032019-12-08 18:30:42 -0800981fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
982 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700983{
984 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800985 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700986 svm_fifo_t *f;
987 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800988 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700989 u8 *fmem;
990 int i;
991
Florin Coras62ddc032019-12-08 18:30:42 -0800992 fss = fsh_slice_get (fsh, slice_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800993 size = (uword) (sizeof (*f)) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700994
Florin Coras62ddc032019-12-08 18:30:42 -0800995 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700996 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
997 0 /* align_offset */ ,
998 0 /* os_out_of_memory */ );
999 ssvm_pop_heap (oldheap);
1000
1001 /* Out of space.. */
1002 if (fmem == 0)
1003 return -1;
1004
1005 /* Carve fifo + chunk space */
1006 for (i = 0; i < batch_size; i++)
1007 {
1008 f = (svm_fifo_t *) fmem;
1009 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -08001010 f->next = fss->free_fifos;
1011 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001012 fmem += sizeof (*f);
1013 }
1014
Florin Coras62ddc032019-12-08 18:30:42 -08001015 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001016
1017 return 0;
1018}
1019
1020int
Florin Coras62ddc032019-12-08 18:30:42 -08001021fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1022 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001023{
Florin Coras62ddc032019-12-08 18:30:42 -08001024 fifo_segment_header_t *fsh = fs->h;
Florin Coras36a0c4d2020-01-31 08:28:02 -08001025 u32 rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -08001026 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001027 svm_fifo_chunk_t *c;
1028 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -08001029 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001030 u8 *cmem;
1031 int i;
1032
Florin Coras62ddc032019-12-08 18:30:42 -08001033 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -07001034 {
1035 clib_warning ("chunk size out of range %d", chunk_size);
1036 return -1;
1037 }
1038
1039 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001040 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -08001041 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001042
Florin Coras62ddc032019-12-08 18:30:42 -08001043 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001044 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
1045 0 /* align_offset */ ,
1046 0 /* os_out_of_memory */ );
1047 ssvm_pop_heap (oldheap);
1048
1049 /* Out of space.. */
1050 if (cmem == 0)
1051 return -1;
1052
Florin Coras62ddc032019-12-08 18:30:42 -08001053 fss = fsh_slice_get (fsh, slice_index);
1054
Florin Corasf9d4ab42019-05-11 16:55:53 -07001055 /* Carve fifo + chunk space */
1056 for (i = 0; i < batch_size; i++)
1057 {
1058 c = (svm_fifo_chunk_t *) cmem;
1059 c->start_byte = 0;
1060 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -08001061 c->next = fss->free_chunks[fl_index];
1062 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001063 cmem += sizeof (*c) + rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001064 fsh_cached_bytes_add (fsh, rounded_data_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001065 }
1066
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001067 fss->num_chunks[fl_index] += batch_size;
Florin Coras62ddc032019-12-08 18:30:42 -08001068 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
1069 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001070
1071 return 0;
1072}
1073
Florin Coras88001c62019-04-24 14:44:46 -07001074/**
1075 * Pre-allocates fifo pairs in fifo segment
1076 */
1077void
1078fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1079 u32 rx_fifo_size, u32 tx_fifo_size,
1080 u32 * n_fifo_pairs)
1081{
Florin Corasf9d4ab42019-05-11 16:55:53 -07001082 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +00001083 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -08001084 fifo_segment_header_t *fsh = fs->h;
1085 int rx_fl_index, tx_fl_index, i;
1086 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -07001087 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -07001088
1089 /* Parameter check */
1090 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1091 return;
1092
Florin Coras62ddc032019-12-08 18:30:42 -08001093 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001094 {
1095 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1096 return;
1097 }
1098
Florin Coras62ddc032019-12-08 18:30:42 -08001099 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001100 {
1101 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1102 return;
1103 }
1104
1105 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001106 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001107 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001108 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001109
Florin Corasf9d4ab42019-05-11 16:55:53 -07001110 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -07001111
Florin Coras88001c62019-04-24 14:44:46 -07001112 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -07001113 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -08001114 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -07001115 pairs_to_alloc = space_available / pair_size;
1116 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -08001117 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +00001118 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001119
Florin Coras62ddc032019-12-08 18:30:42 -08001120 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -07001121 return;
Florin Coras88001c62019-04-24 14:44:46 -07001122
Florin Coras62ddc032019-12-08 18:30:42 -08001123 for (i = 0; i < fs->n_slices; i++)
1124 {
1125 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +00001126 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1127 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1128 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1129 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1130 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -07001131
Florin Coras8d0149d2020-01-02 19:22:51 +00001132 /* Account for the pairs allocated */
1133 *n_fifo_pairs -= alloc_now;
1134 }
Florin Coras88001c62019-04-24 14:44:46 -07001135}
1136
1137/**
1138 * Get number of active fifos
1139 */
1140u32
1141fifo_segment_num_fifos (fifo_segment_t * fs)
1142{
Florin Coras7a90e502020-04-09 04:46:14 +00001143 return fsh_n_active_fifos (fs->h);
Florin Coras88001c62019-04-24 14:44:46 -07001144}
1145
Florin Coras62ddc032019-12-08 18:30:42 -08001146static u32
1147fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -07001148{
Florin Coras88001c62019-04-24 14:44:46 -07001149 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001150 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001151
Florin Coras62ddc032019-12-08 18:30:42 -08001152 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001153 if (f == 0)
1154 return 0;
1155
1156 while (f)
1157 {
1158 f = f->next;
1159 count++;
1160 }
1161 return count;
1162}
1163
Florin Corasb095a3c2019-04-25 12:58:46 -07001164u32
Florin Coras62ddc032019-12-08 18:30:42 -08001165fifo_segment_num_free_fifos (fifo_segment_t * fs)
1166{
1167 fifo_segment_header_t *fsh = fs->h;
1168 fifo_segment_slice_t *fss;
1169 int slice_index;
1170 u32 count = 0;
1171
1172 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1173 {
1174 fss = fsh_slice_get (fsh, slice_index);
1175 count += fs_slice_num_free_fifos (fss);
1176 }
1177 return count;
1178}
1179
1180static u32
1181fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001182{
1183 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001184 svm_fifo_chunk_t *c;
1185 int i;
1186
Florin Corasb095a3c2019-04-25 12:58:46 -07001187 /* Count all free chunks? */
1188 if (size == ~0)
1189 {
Florin Coras62ddc032019-12-08 18:30:42 -08001190 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001191 {
Florin Coras62ddc032019-12-08 18:30:42 -08001192 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001193 if (c == 0)
1194 continue;
1195
1196 while (c)
1197 {
1198 c = c->next;
1199 count++;
1200 }
1201 }
1202 return count;
1203 }
1204
1205 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001206 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001207
Florin Coras62ddc032019-12-08 18:30:42 -08001208 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001209 return 0;
1210
Florin Coras62ddc032019-12-08 18:30:42 -08001211 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001212 if (c == 0)
1213 return 0;
1214
1215 while (c)
1216 {
1217 c = c->next;
1218 count++;
1219 }
1220 return count;
1221}
1222
Florin Coras62ddc032019-12-08 18:30:42 -08001223u32
1224fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1225{
1226 fifo_segment_header_t *fsh = fs->h;
1227 fifo_segment_slice_t *fss;
1228 int slice_index;
1229 u32 count = 0;
1230
1231 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1232 {
1233 fss = fsh_slice_get (fsh, slice_index);
1234 count += fs_slice_num_free_chunks (fss, size);
1235 }
1236 return count;
1237}
1238
Florin Corasf9d4ab42019-05-11 16:55:53 -07001239void
1240fifo_segment_update_free_bytes (fifo_segment_t * fs)
1241{
Florin Coras4453c092019-12-19 10:13:15 -08001242 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001243}
1244
Florin Corasef4f3e72019-12-11 14:27:53 -08001245uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001246fifo_segment_size (fifo_segment_t * fs)
1247{
1248 return fs->ssvm.ssvm_size;
1249}
1250
1251u8
1252fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1253{
1254 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1255}
1256
1257void
1258fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1259{
1260 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1261}
1262
1263uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001264fifo_segment_free_bytes (fifo_segment_t * fs)
1265{
Florin Coras62ddc032019-12-08 18:30:42 -08001266 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001267}
1268
Florin Coras62ddc032019-12-08 18:30:42 -08001269uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001270fifo_segment_cached_bytes (fifo_segment_t * fs)
1271{
1272 return fsh_n_cached_bytes (fs->h);
1273}
1274
1275uword
Florin Coras75ccf7b2020-03-05 19:44:02 +00001276fifo_segment_available_bytes (fifo_segment_t * fs)
1277{
1278 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1279}
1280
1281uword
Florin Coras1c91c772019-06-25 18:14:13 -07001282fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001283{
Florin Coras62ddc032019-12-08 18:30:42 -08001284 fifo_segment_header_t *fsh = fs->h;
1285 fifo_segment_slice_t *fss;
1286 uword n_bytes = 0;
1287 int slice_index;
1288
1289 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1290 {
1291 fss = fsh_slice_get (fsh, slice_index);
1292 n_bytes += fss->n_fl_chunk_bytes;
1293 }
1294
1295 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001296}
1297
Florin Coras88001c62019-04-24 14:44:46 -07001298u8
1299fifo_segment_has_fifos (fifo_segment_t * fs)
1300{
Florin Coras7a90e502020-04-09 04:46:14 +00001301 return (fsh_n_active_fifos (fs->h) != 0);
Florin Coras88001c62019-04-24 14:44:46 -07001302}
1303
1304svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001305fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001306{
Florin Coras62ddc032019-12-08 18:30:42 -08001307 fifo_segment_header_t *fsh = fs->h;
1308 fifo_segment_slice_t *fss;
1309
1310 fss = fsh_slice_get (fsh, slice_index);
1311 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001312}
1313
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001314u8
1315fifo_segment_get_mem_usage (fifo_segment_t * fs)
1316{
1317 uword size, in_use;
1318
1319 size = fifo_segment_size (fs);
1320 in_use =
1321 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1322 return (in_use * 100) / size;
1323}
1324
1325fifo_segment_mem_status_t
1326fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1327{
1328 if (!fsh->high_watermark || !fsh->low_watermark)
1329 return MEMORY_PRESSURE_NO_PRESSURE;
1330
1331 /* once the no-memory is detected, the status continues
1332 * until memory usage gets below the high watermark
1333 */
1334 if (fsh_has_reached_mem_limit (fsh))
1335 {
1336 if (usage >= fsh->high_watermark)
1337 return MEMORY_PRESSURE_NO_MEMORY;
1338 else
1339 fsh_reset_mem_limit (fsh);
1340 }
1341
1342 if (usage >= fsh->high_watermark)
1343 return MEMORY_PRESSURE_HIGH_PRESSURE;
1344
1345 else if (usage >= fsh->low_watermark)
1346 return MEMORY_PRESSURE_LOW_PRESSURE;
1347
1348 return MEMORY_PRESSURE_NO_PRESSURE;
1349}
1350
1351fifo_segment_mem_status_t
1352fifo_segment_get_mem_status (fifo_segment_t * fs)
1353{
1354 fifo_segment_header_t *fsh = fs->h;
1355 u8 usage = fifo_segment_get_mem_usage (fs);
1356
1357 return fifo_segment_determine_status (fsh, usage);
1358}
1359
Florin Coras88001c62019-04-24 14:44:46 -07001360u8 *
1361format_fifo_segment_type (u8 * s, va_list * args)
1362{
1363 fifo_segment_t *sp;
1364 sp = va_arg (*args, fifo_segment_t *);
1365 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1366
1367 if (st == SSVM_SEGMENT_PRIVATE)
1368 s = format (s, "%s", "private-heap");
1369 else if (st == SSVM_SEGMENT_MEMFD)
1370 s = format (s, "%s", "memfd");
1371 else if (st == SSVM_SEGMENT_SHM)
1372 s = format (s, "%s", "shm");
1373 else
1374 s = format (s, "%s", "unknown");
1375 return s;
1376}
1377
1378/**
1379 * Segment format function
1380 */
1381u8 *
1382format_fifo_segment (u8 * s, va_list * args)
1383{
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001384 u32 count, indent, active_fifos, free_fifos;
Florin Coras5368bb02019-06-09 09:24:33 -07001385 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001386 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001387 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1388 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001389 uword tracked_cached_bytes;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001390 uword fifo_hdr = 0, reserved;
Florin Coras5368bb02019-06-09 09:24:33 -07001391 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001392 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001393 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001394 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001395 char *address;
1396 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001397 int i;
Florin Coras06d47752020-03-06 02:23:58 +00001398 uword allocated, in_use, virt;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001399 f64 usage;
1400 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001401
1402 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001403
Florin Coras5368bb02019-06-09 09:24:33 -07001404 if (fs == 0)
1405 {
1406 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1407 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1408 return s;
1409 }
1410
Florin Coras5368bb02019-06-09 09:24:33 -07001411 fifo_segment_info (fs, &address, &size);
1412 active_fifos = fifo_segment_num_fifos (fs);
1413 free_fifos = fifo_segment_num_free_fifos (fs);
1414
1415 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1416 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1417 free_fifos, address);
1418
1419 if (!verbose)
1420 return s;
1421
Florin Coras62ddc032019-12-08 18:30:42 -08001422 fsh = fs->h;
1423
1424 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1425 if (free_chunks)
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001426 s =
1427 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1428 indent + 2);
Florin Corasf8461bf2019-11-07 17:00:15 -08001429 else
1430 s = format (s, "\n");
1431
Florin Coras62ddc032019-12-08 18:30:42 -08001432 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001433 {
Florin Coras62ddc032019-12-08 18:30:42 -08001434 fss = fsh_slice_get (fsh, slice_index);
1435 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001436 {
Florin Coras62ddc032019-12-08 18:30:42 -08001437 c = fss->free_chunks[i];
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001438 if (c == 0 && fss->num_chunks[i] == 0)
Florin Coras62ddc032019-12-08 18:30:42 -08001439 continue;
1440 count = 0;
1441 while (c)
1442 {
1443 c = c->next;
1444 count++;
1445 }
1446
1447 chunk_size = fs_freelist_index_to_size (i);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001448 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1449 chunk_size >> 10, count, fss->num_chunks[i]);
Florin Coras62ddc032019-12-08 18:30:42 -08001450
1451 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001452 }
Florin Coras88001c62019-04-24 14:44:46 -07001453 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001454
1455 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1456 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001457 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001458 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001459 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001460 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1461 allocated = fifo_segment_size (fs);
1462 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1463 usage = (100.0 * in_use) / allocated;
1464 mem_st = fifo_segment_get_mem_status (fs);
Florin Coras06d47752020-03-06 02:23:58 +00001465 virt = fsh_virtual_mem (fsh);
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001466 reserved = fsh->n_reserved_bytes;
Florin Corasf8461bf2019-11-07 17:00:15 -08001467
Florin Coras06d47752020-03-06 02:23:58 +00001468 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1469 " %U (%lu)\n", format_white_space, indent + 2,
1470 format_memory_size, free_seg_bytes, free_seg_bytes,
1471 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001472 format_memory_size, reserved, reserved);
Florin Coras06d47752020-03-06 02:23:58 +00001473 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1474 " %U (%lu)\n", format_white_space, indent + 2,
1475 format_memory_size, chunk_bytes, chunk_bytes,
1476 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1477 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1478 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1479 format_white_space, indent + 2, fsh->n_active_fifos,
1480 format_memory_size, fifo_hdr, fifo_hdr);
1481 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1482 format_white_space, indent + 2, usage, format_memory_size,
1483 in_use, format_memory_size, allocated, format_memory_size, virt,
1484 fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001485 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001486
Florin Coras88001c62019-04-24 14:44:46 -07001487 return s;
1488}
1489
1490/*
1491 * fd.io coding-style-patch-verification: ON
1492 *
1493 * Local Variables:
1494 * eval: (c-set-style "gnu")
1495 * End:
1496 */