blob: e19113fd44921dc016e128c2176748bf64c8f06a [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
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000018static char *fifo_segment_mem_status_strings[] = {
19#define _(sym,str) str,
20 foreach_segment_mem_status
21#undef _
22};
23
Florin Coras88001c62019-04-24 14:44:46 -070024/**
Florin Corasf9d4ab42019-05-11 16:55:53 -070025 * Fifo segment free space
26 *
27 * Queries the underlying memory manager, dlmalloc, for free space. Since this
28 * ends up walking the internal data structures, it should not be called
29 * indiscriminately.
30 *
31 * @param fs fifo segment
32 * @return number of free bytes
33 */
Florin Corasef4f3e72019-12-11 14:27:53 -080034static uword
Florin Coras4453c092019-12-19 10:13:15 -080035fsh_free_space (fifo_segment_header_t * fsh)
Florin Corasf9d4ab42019-05-11 16:55:53 -070036{
37 struct dlmallinfo dlminfo;
38
Florin Coras4453c092019-12-19 10:13:15 -080039 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070040 return dlminfo.fordblks;
41}
42
Florin Coras62ddc032019-12-08 18:30:42 -080043static inline void
44fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
45{
46 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
47}
48
49static inline uword
50fsh_n_free_bytes (fifo_segment_header_t * fsh)
51{
Florin Coras8122cc22019-12-18 13:06:41 -080052 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
53 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
54}
55
56static inline void
Florin Coras4453c092019-12-19 10:13:15 -080057fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080058{
Florin Coras4453c092019-12-19 10:13:15 -080059 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080060}
61
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000062static inline void
63fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
64{
65 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
66}
67
68static inline void
69fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
70{
71 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
72}
73
74static inline uword
75fsh_n_cached_bytes (fifo_segment_header_t * fsh)
76{
77 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000078 return n_cached;
79}
80
Florin Coras8122cc22019-12-18 13:06:41 -080081static void
82fsh_check_mem (fifo_segment_header_t * fsh)
83{
84 uword thresh;
85
86 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
87 return;
88
89 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
90 2 * fsh->n_reserved_bytes);
91 if (fsh->n_free_bytes > thresh)
92 return;
93
94 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -080095 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -080096}
97
98static inline fifo_segment_slice_t *
99fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
100{
101 return &fsh->slices[slice_index];
102}
103
104static inline void
105fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
106{
107 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
108}
109
Florin Corasf9d4ab42019-05-11 16:55:53 -0700110/**
Florin Coras88001c62019-04-24 14:44:46 -0700111 * Initialize fifo segment shared header
112 */
113int
114fifo_segment_init (fifo_segment_t * fs)
115{
116 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800117 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700118 ssvm_shared_header_t *sh;
Florin Coras8122cc22019-12-18 13:06:41 -0800119 u32 max_chunk_sz, max_chunks;
Florin Coras62ddc032019-12-08 18:30:42 -0800120 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700121 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800122 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700123
124 sh = fs->ssvm.sh;
125 oldheap = ssvm_push_heap (sh);
126
Florin Coras62ddc032019-12-08 18:30:42 -0800127 /*
128 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
129 * Long story made short: the "process-private" fifo segment
130 * is allocated from the main heap, not mmapped. dlmalloc
131 * only guarantees 4-byte alignment, and on aarch64
132 * the fsh can end up 4-byte but not 8-byte aligned.
133 * That eventually causes the atomic op in fifo_segment_update_free_bytes
134 * to backfire.
135 */
136 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700137 clib_memset (fsh, 0, sizeof (*fsh));
138 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800139 fs->n_slices = clib_max (fs->n_slices, 1);
140
141 fsh->ssvm_sh = fs->ssvm.sh;
142 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800143 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800144 FIFO_SEGMENT_MAX_FIFO_SIZE);
145 fsh->max_log2_chunk_size = max_log2 (max_fifo);
146
147 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
148 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
149 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
150
151 for (i = 0; i < fs->n_slices; i++)
152 {
153 fss = fsh_slice_get (fsh, i);
154 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800155 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800156 }
Florin Coras88001c62019-04-24 14:44:46 -0700157
158 ssvm_pop_heap (oldheap);
159
Florin Coras4453c092019-12-19 10:13:15 -0800160 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000161 fsh->n_cached_bytes = 0;
Florin Coras8122cc22019-12-18 13:06:41 -0800162 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
163 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
Florin Coras88001c62019-04-24 14:44:46 -0700164 sh->ready = 1;
165 return (0);
166}
167
168/**
Florin Coras88001c62019-04-24 14:44:46 -0700169 * Create a fifo segment and initialize as master
170 */
171int
172fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
173{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700174 fifo_segment_t *fs;
175 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700176 int rv;
177
Florin Coras88001c62019-04-24 14:44:46 -0700178 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700179 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700180
Florin Corasf9d4ab42019-05-11 16:55:53 -0700181 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
182 fs->ssvm.ssvm_size = a->segment_size;
183 fs->ssvm.i_am_master = 1;
184 fs->ssvm.my_pid = getpid ();
185 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
186 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700187
Florin Corasf9d4ab42019-05-11 16:55:53 -0700188 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700189 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700190 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700191 return (rv);
192 }
193
194 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700195 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700196
Florin Corasf9d4ab42019-05-11 16:55:53 -0700197 fifo_segment_init (fs);
198 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700199 return (0);
200}
201
202/**
203 * Attach as slave to a fifo segment
204 */
205int
206fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
207{
Florin Coras62ddc032019-12-08 18:30:42 -0800208 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700209 int rv;
210
Florin Coras62ddc032019-12-08 18:30:42 -0800211 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700212
Florin Coras62ddc032019-12-08 18:30:42 -0800213 fs->ssvm.ssvm_size = a->segment_size;
214 fs->ssvm.my_pid = getpid ();
215 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
216 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700217 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800218 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700219 else
Florin Coras62ddc032019-12-08 18:30:42 -0800220 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700221
Florin Coras62ddc032019-12-08 18:30:42 -0800222 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700223 {
Florin Coras62ddc032019-12-08 18:30:42 -0800224 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700225 return (rv);
226 }
227
228 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800229 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700230
Florin Coras62ddc032019-12-08 18:30:42 -0800231 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700232 return (0);
233}
234
235void
236fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
237{
238 ssvm_delete (&s->ssvm);
239 clib_memset (s, 0xfe, sizeof (*s));
240 pool_put (sm->segments, s);
241}
242
243u32
244fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
245{
246 return s - sm->segments;
247}
248
Florin Coras88001c62019-04-24 14:44:46 -0700249fifo_segment_t *
250fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
251{
252 return pool_elt_at_index (sm->segments, segment_index);
253}
254
255void
256fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
257{
Florin Coras404b8a32019-05-09 12:08:06 -0700258 *address = (char *) seg->ssvm.sh->ssvm_va;
259 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700260}
261
262void
263fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
264 u32 timeout_in_seconds)
265{
266 sm->next_baseva = baseva;
267 sm->timeout_in_seconds = timeout_in_seconds;
268}
269
Florin Corasf9d4ab42019-05-11 16:55:53 -0700270static inline u32
271fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700272{
Florin Corasf22f4e52019-12-19 16:10:58 -0800273 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
274 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800275 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700276}
277
Florin Corasf9d4ab42019-05-11 16:55:53 -0700278static inline u32
279fs_freelist_index_to_size (u32 fl_index)
280{
Florin Coras62ddc032019-12-08 18:30:42 -0800281 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700282}
283
Florin Coras88001c62019-04-24 14:44:46 -0700284static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800285fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700286{
287 /*
288 * 4K minimum. It's not likely that anything good will happen
289 * with a smaller FIFO.
290 */
291 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Coras62ddc032019-12-08 18:30:42 -0800292 && size <= (1 << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700293}
294
Florin Corascefd5d82019-05-05 13:19:57 -0700295static svm_fifo_t *
Florin Coras2de9c0f2020-02-02 19:30:39 +0000296fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
Florin Corascefd5d82019-05-05 13:19:57 -0700297{
298 svm_fifo_chunk_t *c;
299 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700300
Florin Coras62ddc032019-12-08 18:30:42 -0800301 f = fss->free_fifos;
302 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700303
304 if (!f || !c)
305 return 0;
306
Florin Coras62ddc032019-12-08 18:30:42 -0800307 fss->free_fifos = f->next;
308 fss->free_chunks[fl_index] = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800309 c->next = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700310 c->start_byte = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700311 memset (f, 0, sizeof (*f));
312 f->start_chunk = c;
313 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700314
Florin Coras62ddc032019-12-08 18:30:42 -0800315 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700316 return f;
317}
318
Florin Coras8e755a12020-02-06 16:59:31 +0000319svm_fifo_chunk_t *
320fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
321 fifo_segment_slice_t * fss, u32 data_bytes)
322{
323 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
324 svm_fifo_chunk_t *c, *first = 0, *next;
325
326 fl_index = fs_freelist_for_size (req_bytes);
327 if (fl_index > 0)
328 fl_index -= 1;
329
330 fl_size = fs_freelist_index_to_size (fl_index);
331
332 while (req_bytes)
333 {
334 c = fss->free_chunks[fl_index];
335 if (c)
336 {
337 fss->free_chunks[fl_index] = c->next;
338 c->next = first;
339 first = c;
340 n_alloc += fl_size;
341 req_bytes -= clib_min (fl_size, req_bytes);
342 }
343 else
344 {
345 /* Failed to allocate with smaller chunks */
346 if (fl_index == 0)
347 {
348 /* free all chunks if any allocated */
349 c = first;
350 while (c)
351 {
352 fl_index = fs_freelist_for_size (c->length);
353 fl_size = fs_freelist_index_to_size (fl_index);
354 next = c->next;
355 c->next = fss->free_chunks[fl_index];
356 fss->free_chunks[fl_index] = c;
357 fss->n_fl_chunk_bytes += fl_size;
358 c = next;
359 }
360 n_alloc = 0;
361 first = 0;
362 fl_index = fs_freelist_for_size (data_bytes);
363 if (fss->free_chunks[fl_index + 1])
364 {
365 fl_index += 1;
366 fl_size = fs_freelist_index_to_size (fl_index);
367 continue;
368 }
369
370 return 0;
371 }
372 fl_index -= 1;
373 fl_size = fl_size >> 1;
374 }
375 }
376
377 fss->n_fl_chunk_bytes -= n_alloc;
378 fsh_cached_bytes_sub (fsh, n_alloc);
379 return first;
380}
381
Florin Corascefd5d82019-05-05 13:19:57 -0700382static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800383fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
384 fifo_segment_slice_t * fss,
385 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700386{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700387 svm_fifo_chunk_t *c, *first = 0, *last = 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700388 u32 fl_index, fl_size, n_alloc = 0;
389 svm_fifo_t *f;
390
Florin Coras62ddc032019-12-08 18:30:42 -0800391 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700392 if (!f)
393 {
Florin Coras62ddc032019-12-08 18:30:42 -0800394 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700395 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
396 ssvm_pop_heap (oldheap);
397 if (!f)
398 return 0;
399 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800400 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700401 }
Florin Coras73cad332019-08-28 17:12:32 -0700402 else
403 {
Florin Coras62ddc032019-12-08 18:30:42 -0800404 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700405 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700406
Florin Corasd849edf2019-12-20 18:48:20 -0800407 fl_index = fs_freelist_for_size (data_bytes);
408 if (fl_index > 0)
409 fl_index -= 1;
410
Florin Corasf9d4ab42019-05-11 16:55:53 -0700411 fl_size = fs_freelist_index_to_size (fl_index);
412
413 while (data_bytes)
414 {
Florin Coras62ddc032019-12-08 18:30:42 -0800415 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700416 if (c)
417 {
Florin Coras62ddc032019-12-08 18:30:42 -0800418 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700419 if (!last)
420 last = c;
421 c->next = first;
422 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700423 n_alloc += fl_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700424 data_bytes -= c->length;
425 }
426 else
427 {
Florin Corasd849edf2019-12-20 18:48:20 -0800428 /* Failed to allocate with smaller chunks */
429 if (fl_index == 0)
430 {
431 /* free all chunks if any allocated */
432 c = first;
433 while (c)
434 {
435 fl_index = fs_freelist_for_size (c->length);
436 fl_size = fs_freelist_index_to_size (fl_index);
437 c->next = fss->free_chunks[fl_index];
438 fss->free_chunks[fl_index] = c;
439 fss->n_fl_chunk_bytes += fl_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000440 n_alloc -= fl_size;
Florin Corasd849edf2019-12-20 18:48:20 -0800441 data_bytes += fl_size;
442 }
443 first = last = 0;
444 fl_index = fs_freelist_for_size (data_bytes);
445 if (fss->free_chunks[fl_index + 1])
446 {
447 fl_index += 1;
448 fl_size = fs_freelist_index_to_size (fl_index);
449 continue;
450 }
451
452 f->next = fss->free_fifos;
453 fss->free_fifos = f;
454 return 0;
455 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700456 fl_index -= 1;
457 fl_size = fl_size >> 1;
458 }
459 }
Florin Corasd849edf2019-12-20 18:48:20 -0800460
Florin Corasf9d4ab42019-05-11 16:55:53 -0700461 f->start_chunk = first;
462 f->end_chunk = last;
Florin Coras62ddc032019-12-08 18:30:42 -0800463 fss->n_fl_chunk_bytes -= n_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000464 fsh_cached_bytes_sub (fsh, n_alloc);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700465 return f;
466}
467
468static int
Florin Coras8e755a12020-02-06 16:59:31 +0000469fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
470 fifo_segment_slice_t * fss,
471 u32 fl_index, u32 batch_size)
472{
473 u32 rounded_data_size;
474 svm_fifo_chunk_t *c;
475 void *oldheap;
476 uword size;
477 u8 *cmem;
478 int i;
479
480 rounded_data_size = fs_freelist_index_to_size (fl_index);
481 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
482
483 oldheap = ssvm_push_heap (fsh->ssvm_sh);
484 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
485 0 /* align_offset */ ,
486 0 /* os_out_of_memory */ );
487 ssvm_pop_heap (oldheap);
488
489 /* Out of space.. */
490 if (cmem == 0)
491 return -1;
492
493 /* Carve fifo + chunk space */
494 for (i = 0; i < batch_size; i++)
495 {
496 c = (svm_fifo_chunk_t *) cmem;
497 c->start_byte = 0;
498 c->length = rounded_data_size;
499 c->enq_rb_index = RBTREE_TNIL_INDEX;
500 c->deq_rb_index = RBTREE_TNIL_INDEX;
501 c->next = fss->free_chunks[fl_index];
502 fss->free_chunks[fl_index] = c;
503 cmem += sizeof (*c) + rounded_data_size;
504 }
505
506 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
507 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
508 fsh_free_bytes_sub (fsh, size);
509
510 return 0;
511}
512
513static int
Florin Coras62ddc032019-12-08 18:30:42 -0800514fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
515 fifo_segment_slice_t * fss,
516 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700517{
Florin Coras36a0c4d2020-01-31 08:28:02 -0800518 u32 hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700519 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700520 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700521 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800522 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700523 u8 *fmem;
524 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700525
Florin Corasf9d4ab42019-05-11 16:55:53 -0700526 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700527 hdrs = sizeof (*f) + sizeof (*c);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800528 size = (uword) (hdrs + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700529
Florin Coras62ddc032019-12-08 18:30:42 -0800530 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700531 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
532 0 /* align_offset */ ,
533 0 /* os_out_of_memory */ );
534 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700535
536 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700537 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700538 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700539
Florin Corascefd5d82019-05-05 13:19:57 -0700540 /* Carve fifo + chunk space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700541 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700542 {
Florin Corascefd5d82019-05-05 13:19:57 -0700543 f = (svm_fifo_t *) fmem;
544 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800545 f->next = fss->free_fifos;
546 fss->free_fifos = f;
Florin Corascefd5d82019-05-05 13:19:57 -0700547 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
548 c->start_byte = 0;
549 c->length = rounded_data_size;
Florin Corasf22f4e52019-12-19 16:10:58 -0800550 c->enq_rb_index = RBTREE_TNIL_INDEX;
551 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Coras62ddc032019-12-08 18:30:42 -0800552 c->next = fss->free_chunks[fl_index];
553 fss->free_chunks[fl_index] = c;
Florin Corascefd5d82019-05-05 13:19:57 -0700554 fmem += hdrs + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700555 }
Florin Corascefd5d82019-05-05 13:19:57 -0700556
Florin Coras62ddc032019-12-08 18:30:42 -0800557 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000558 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800559 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700560
561 return 0;
562}
563
564/**
565 * Try to allocate new fifo
566 *
567 * Tries the following steps in order:
568 * - grab fifo and chunk from freelists
569 * - batch fifo and chunk allocation
570 * - single fifo allocation
571 * - grab multiple fifo chunks from freelists
572 */
573static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800574fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
575 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700576{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700577 u32 fifo_sz, fl_index;
578 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800579 uword n_free_bytes;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000580 u32 min_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700581
Florin Coras2de9c0f2020-02-02 19:30:39 +0000582 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
583 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700584 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000585 fifo_sz += 1 << max_log2 (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700586
Florin Coras62ddc032019-12-08 18:30:42 -0800587 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700588 {
Florin Coras2de9c0f2020-02-02 19:30:39 +0000589 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700590 if (f)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000591 {
592 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
593 goto done;
594 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700595 }
Florin Coras8122cc22019-12-18 13:06:41 -0800596
597 fsh_check_mem (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800598 n_free_bytes = fsh_n_free_bytes (fsh);
599 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700600 {
Florin Coras62ddc032019-12-08 18:30:42 -0800601 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
Florin Corasf9d4ab42019-05-11 16:55:53 -0700602 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
603 goto done;
604
Florin Coras2de9c0f2020-02-02 19:30:39 +0000605 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000606 if (f)
607 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700608 goto done;
609 }
Florin Coras62ddc032019-12-08 18:30:42 -0800610 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700611 {
Florin Coras62ddc032019-12-08 18:30:42 -0800612 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000613 f = svm_fifo_alloc (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700614 ssvm_pop_heap (oldheap);
615 if (f)
616 {
Florin Coras62ddc032019-12-08 18:30:42 -0800617 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700618 goto done;
619 }
620 }
Florin Coras62ddc032019-12-08 18:30:42 -0800621 if (data_bytes <= fss->n_fl_chunk_bytes)
622 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700623
624done:
625
Florin Corasf22f4e52019-12-19 16:10:58 -0800626 if (f)
627 f->fs_hdr = fsh;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700628 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700629}
630
Florin Corasf22f4e52019-12-19 16:10:58 -0800631svm_fifo_chunk_t *
632fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
633{
634 fifo_segment_slice_t *fss;
635 svm_fifo_chunk_t *c;
636 void *oldheap;
637 int fl_index;
Florin Coras8e755a12020-02-06 16:59:31 +0000638 uword n_free;
Florin Corasf22f4e52019-12-19 16:10:58 -0800639
640 fl_index = fs_freelist_for_size (chunk_size);
641 fss = fsh_slice_get (fsh, slice_index);
642
643 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras8e755a12020-02-06 16:59:31 +0000644
Florin Corasf22f4e52019-12-19 16:10:58 -0800645 c = fss->free_chunks[fl_index];
646
Florin Coras8e755a12020-02-06 16:59:31 +0000647 if (c)
648 {
649 fss->free_chunks[fl_index] = c->next;
650 c->next = 0;
651 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
652 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
653 }
654 else if (chunk_size <= (n_free = fsh_n_free_bytes (fsh)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800655 {
656 fsh_check_mem (fsh);
Florin Coras8e755a12020-02-06 16:59:31 +0000657
Florin Corasf22f4e52019-12-19 16:10:58 -0800658 chunk_size = fs_freelist_index_to_size (fl_index);
Florin Coras8e755a12020-02-06 16:59:31 +0000659 if (n_free < chunk_size)
Florin Corasf22f4e52019-12-19 16:10:58 -0800660 goto done;
661
662 oldheap = ssvm_push_heap (fsh->ssvm_sh);
663 c = svm_fifo_chunk_alloc (chunk_size);
664 ssvm_pop_heap (oldheap);
665
666 if (!c)
667 goto done;
668
669 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
670 }
Florin Coras8e755a12020-02-06 16:59:31 +0000671 else if (chunk_size <= fss->n_fl_chunk_bytes)
Florin Corasf22f4e52019-12-19 16:10:58 -0800672 {
Florin Coras8e755a12020-02-06 16:59:31 +0000673 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
674 }
675 else if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
676 {
677 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
678 u32 batch;
679
680 fsh_check_mem (fsh);
681 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
682 batch = clib_min (batch + 1, n_free / min_size);
683 if (!fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
684 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800685 }
686
687done:
688
689 clib_spinlock_unlock (&fss->chunk_lock);
690
691 return c;
692}
693
694static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000695fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000696 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800697{
698 svm_fifo_chunk_t *next;
699 int fl_index;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000700 u32 n_collect = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800701
702 clib_spinlock_lock (&fss->chunk_lock);
703
Florin Coras9e61d9a2020-02-05 21:13:18 +0000704 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800705 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000706 next = c->next;
707 fl_index = fs_freelist_for_size (c->length);
708 c->next = fss->free_chunks[fl_index];
709 c->enq_rb_index = RBTREE_TNIL_INDEX;
710 c->deq_rb_index = RBTREE_TNIL_INDEX;
711 fss->free_chunks[fl_index] = c;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000712 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000713 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800714 }
715
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000716 fss->n_fl_chunk_bytes += n_collect;
717 fsh_cached_bytes_add (fsh, n_collect);
718
Florin Corasf22f4e52019-12-19 16:10:58 -0800719 clib_spinlock_unlock (&fss->chunk_lock);
720}
721
722void
723fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000724 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800725{
726 fifo_segment_slice_t *fss;
727 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000728 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800729}
730
Florin Coras88001c62019-04-24 14:44:46 -0700731/**
732 * Allocate fifo in fifo segment
733 */
734svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800735fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
736 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700737{
Florin Coras62ddc032019-12-08 18:30:42 -0800738 fifo_segment_header_t *fsh = fs->h;
739 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700740 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700741
Florin Coras62ddc032019-12-08 18:30:42 -0800742 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700743
Florin Coras62ddc032019-12-08 18:30:42 -0800744 fss = fsh_slice_get (fsh, slice_index);
745 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700746 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700747 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700748
Florin Coras62ddc032019-12-08 18:30:42 -0800749 f->slice_index = slice_index;
750
Florin Corascefd5d82019-05-05 13:19:57 -0700751 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700752
Florin Coras88001c62019-04-24 14:44:46 -0700753 /* If rx fifo type add to active fifos list. When cleaning up segment,
754 * we need a list of active sessions that should be disconnected. Since
755 * both rx and tx fifos keep pointers to the session, it's enough to track
756 * only one. */
757 if (ftype == FIFO_SEGMENT_RX_FIFO)
758 {
Florin Coras62ddc032019-12-08 18:30:42 -0800759 if (fss->fifos)
Florin Coras88001c62019-04-24 14:44:46 -0700760 {
Florin Coras62ddc032019-12-08 18:30:42 -0800761 fss->fifos->prev = f;
762 f->next = fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700763 }
Florin Coras62ddc032019-12-08 18:30:42 -0800764 fss->fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700765 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800766
767 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
Florin Coras88001c62019-04-24 14:44:46 -0700768 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800769 else
770 {
771 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
772 }
773
Florin Coras62ddc032019-12-08 18:30:42 -0800774 fsh_active_fifos_update (fsh, 1);
Florin Coras88001c62019-04-24 14:44:46 -0700775
776done:
Florin Coras88001c62019-04-24 14:44:46 -0700777 return (f);
778}
779
780/**
781 * Free fifo allocated in fifo segment
782 */
783void
Florin Corasb095a3c2019-04-25 12:58:46 -0700784fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700785{
Florin Coras62ddc032019-12-08 18:30:42 -0800786 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800787 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700788
789 ASSERT (f->refcnt > 0);
790
791 if (--f->refcnt > 0)
792 return;
793
Florin Coras62ddc032019-12-08 18:30:42 -0800794 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700795
796 /* Remove from active list. Only rx fifos are tracked */
797 if (f->flags & SVM_FIFO_F_LL_TRACKED)
798 {
799 if (f->prev)
800 f->prev->next = f->next;
801 else
Florin Coras62ddc032019-12-08 18:30:42 -0800802 fss->fifos = f->next;
Florin Coras88001c62019-04-24 14:44:46 -0700803 if (f->next)
804 f->next->prev = f->prev;
805 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
806 }
807
808 /* Add to free list */
Florin Coras62ddc032019-12-08 18:30:42 -0800809 f->next = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700810 f->prev = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800811 fss->free_fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700812
Florin Corascefd5d82019-05-05 13:19:57 -0700813 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000814 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700815
Florin Corasf22f4e52019-12-19 16:10:58 -0800816 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700817 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
818
Florin Corasb095a3c2019-04-25 12:58:46 -0700819 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800820 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700821 svm_fifo_free_ooo_data (f);
822
Florin Coras88001c62019-04-24 14:44:46 -0700823 if (CLIB_DEBUG)
824 {
825 f->master_session_index = ~0;
826 f->master_thread_index = ~0;
827 }
828
Florin Coras62ddc032019-12-08 18:30:42 -0800829 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700830}
831
Florin Corasf9d4ab42019-05-11 16:55:53 -0700832int
Florin Coras62ddc032019-12-08 18:30:42 -0800833fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
834 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700835{
836 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800837 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700838 svm_fifo_t *f;
839 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800840 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700841 u8 *fmem;
842 int i;
843
Florin Coras62ddc032019-12-08 18:30:42 -0800844 fss = fsh_slice_get (fsh, slice_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800845 size = (uword) (sizeof (*f)) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700846
Florin Coras62ddc032019-12-08 18:30:42 -0800847 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700848 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
849 0 /* align_offset */ ,
850 0 /* os_out_of_memory */ );
851 ssvm_pop_heap (oldheap);
852
853 /* Out of space.. */
854 if (fmem == 0)
855 return -1;
856
857 /* Carve fifo + chunk space */
858 for (i = 0; i < batch_size; i++)
859 {
860 f = (svm_fifo_t *) fmem;
861 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800862 f->next = fss->free_fifos;
863 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700864 fmem += sizeof (*f);
865 }
866
Florin Coras62ddc032019-12-08 18:30:42 -0800867 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700868
869 return 0;
870}
871
872int
Florin Coras62ddc032019-12-08 18:30:42 -0800873fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
874 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700875{
Florin Coras62ddc032019-12-08 18:30:42 -0800876 fifo_segment_header_t *fsh = fs->h;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800877 u32 rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -0800878 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700879 svm_fifo_chunk_t *c;
880 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800881 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700882 u8 *cmem;
883 int i;
884
Florin Coras62ddc032019-12-08 18:30:42 -0800885 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700886 {
887 clib_warning ("chunk size out of range %d", chunk_size);
888 return -1;
889 }
890
891 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700892 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800893 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700894
Florin Coras62ddc032019-12-08 18:30:42 -0800895 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700896 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
897 0 /* align_offset */ ,
898 0 /* os_out_of_memory */ );
899 ssvm_pop_heap (oldheap);
900
901 /* Out of space.. */
902 if (cmem == 0)
903 return -1;
904
Florin Coras62ddc032019-12-08 18:30:42 -0800905 fss = fsh_slice_get (fsh, slice_index);
906
Florin Corasf9d4ab42019-05-11 16:55:53 -0700907 /* Carve fifo + chunk space */
908 for (i = 0; i < batch_size; i++)
909 {
910 c = (svm_fifo_chunk_t *) cmem;
911 c->start_byte = 0;
912 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800913 c->next = fss->free_chunks[fl_index];
914 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700915 cmem += sizeof (*c) + rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000916 fsh_cached_bytes_add (fsh, rounded_data_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700917 }
918
Florin Coras62ddc032019-12-08 18:30:42 -0800919 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
920 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700921
922 return 0;
923}
924
Florin Coras88001c62019-04-24 14:44:46 -0700925/**
926 * Pre-allocates fifo pairs in fifo segment
927 */
928void
929fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
930 u32 rx_fifo_size, u32 tx_fifo_size,
931 u32 * n_fifo_pairs)
932{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700933 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +0000934 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -0800935 fifo_segment_header_t *fsh = fs->h;
936 int rx_fl_index, tx_fl_index, i;
937 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700938 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700939
940 /* Parameter check */
941 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
942 return;
943
Florin Coras62ddc032019-12-08 18:30:42 -0800944 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700945 {
946 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
947 return;
948 }
949
Florin Coras62ddc032019-12-08 18:30:42 -0800950 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700951 {
952 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
953 return;
954 }
955
956 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700957 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700958 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700959 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700960
Florin Corasf9d4ab42019-05-11 16:55:53 -0700961 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -0700962
Florin Coras88001c62019-04-24 14:44:46 -0700963 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -0700964 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -0800965 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -0700966 pairs_to_alloc = space_available / pair_size;
967 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -0800968 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +0000969 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700970
Florin Coras62ddc032019-12-08 18:30:42 -0800971 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -0700972 return;
Florin Coras88001c62019-04-24 14:44:46 -0700973
Florin Coras62ddc032019-12-08 18:30:42 -0800974 for (i = 0; i < fs->n_slices; i++)
975 {
976 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +0000977 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
978 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
979 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
980 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
981 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -0700982
Florin Coras8d0149d2020-01-02 19:22:51 +0000983 /* Account for the pairs allocated */
984 *n_fifo_pairs -= alloc_now;
985 }
Florin Coras88001c62019-04-24 14:44:46 -0700986}
987
988/**
989 * Get number of active fifos
990 */
991u32
992fifo_segment_num_fifos (fifo_segment_t * fs)
993{
Florin Coras62ddc032019-12-08 18:30:42 -0800994 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
Florin Coras88001c62019-04-24 14:44:46 -0700995}
996
Florin Coras62ddc032019-12-08 18:30:42 -0800997static u32
998fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -0700999{
Florin Coras88001c62019-04-24 14:44:46 -07001000 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001001 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001002
Florin Coras62ddc032019-12-08 18:30:42 -08001003 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001004 if (f == 0)
1005 return 0;
1006
1007 while (f)
1008 {
1009 f = f->next;
1010 count++;
1011 }
1012 return count;
1013}
1014
Florin Corasb095a3c2019-04-25 12:58:46 -07001015u32
Florin Coras62ddc032019-12-08 18:30:42 -08001016fifo_segment_num_free_fifos (fifo_segment_t * fs)
1017{
1018 fifo_segment_header_t *fsh = fs->h;
1019 fifo_segment_slice_t *fss;
1020 int slice_index;
1021 u32 count = 0;
1022
1023 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1024 {
1025 fss = fsh_slice_get (fsh, slice_index);
1026 count += fs_slice_num_free_fifos (fss);
1027 }
1028 return count;
1029}
1030
1031static u32
1032fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001033{
1034 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001035 svm_fifo_chunk_t *c;
1036 int i;
1037
Florin Corasb095a3c2019-04-25 12:58:46 -07001038 /* Count all free chunks? */
1039 if (size == ~0)
1040 {
Florin Coras62ddc032019-12-08 18:30:42 -08001041 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001042 {
Florin Coras62ddc032019-12-08 18:30:42 -08001043 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001044 if (c == 0)
1045 continue;
1046
1047 while (c)
1048 {
1049 c = c->next;
1050 count++;
1051 }
1052 }
1053 return count;
1054 }
1055
1056 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001057 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001058
Florin Coras62ddc032019-12-08 18:30:42 -08001059 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001060 return 0;
1061
Florin Coras62ddc032019-12-08 18:30:42 -08001062 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001063 if (c == 0)
1064 return 0;
1065
1066 while (c)
1067 {
1068 c = c->next;
1069 count++;
1070 }
1071 return count;
1072}
1073
Florin Coras62ddc032019-12-08 18:30:42 -08001074u32
1075fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1076{
1077 fifo_segment_header_t *fsh = fs->h;
1078 fifo_segment_slice_t *fss;
1079 int slice_index;
1080 u32 count = 0;
1081
1082 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1083 {
1084 fss = fsh_slice_get (fsh, slice_index);
1085 count += fs_slice_num_free_chunks (fss, size);
1086 }
1087 return count;
1088}
1089
Florin Corasf9d4ab42019-05-11 16:55:53 -07001090void
1091fifo_segment_update_free_bytes (fifo_segment_t * fs)
1092{
Florin Coras4453c092019-12-19 10:13:15 -08001093 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001094}
1095
Florin Corasef4f3e72019-12-11 14:27:53 -08001096uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001097fifo_segment_size (fifo_segment_t * fs)
1098{
1099 return fs->ssvm.ssvm_size;
1100}
1101
1102u8
1103fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1104{
1105 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1106}
1107
1108void
1109fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1110{
1111 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1112}
1113
1114uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001115fifo_segment_free_bytes (fifo_segment_t * fs)
1116{
Florin Coras62ddc032019-12-08 18:30:42 -08001117 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001118}
1119
Florin Coras62ddc032019-12-08 18:30:42 -08001120uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001121fifo_segment_cached_bytes (fifo_segment_t * fs)
1122{
1123 return fsh_n_cached_bytes (fs->h);
1124}
1125
1126uword
Florin Coras1c91c772019-06-25 18:14:13 -07001127fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001128{
Florin Coras62ddc032019-12-08 18:30:42 -08001129 fifo_segment_header_t *fsh = fs->h;
1130 fifo_segment_slice_t *fss;
1131 uword n_bytes = 0;
1132 int slice_index;
1133
1134 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1135 {
1136 fss = fsh_slice_get (fsh, slice_index);
1137 n_bytes += fss->n_fl_chunk_bytes;
1138 }
1139
1140 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001141}
1142
Florin Coras88001c62019-04-24 14:44:46 -07001143u8
1144fifo_segment_has_fifos (fifo_segment_t * fs)
1145{
Florin Coras62ddc032019-12-08 18:30:42 -08001146 fifo_segment_header_t *fsh = fs->h;
1147 fifo_segment_slice_t *fss;
1148 int slice_index;
1149
1150 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1151 {
1152 fss = fsh_slice_get (fsh, slice_index);
1153 if (fss->fifos)
1154 return 1;
1155 }
1156 return 0;
Florin Coras88001c62019-04-24 14:44:46 -07001157}
1158
1159svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001160fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001161{
Florin Coras62ddc032019-12-08 18:30:42 -08001162 fifo_segment_header_t *fsh = fs->h;
1163 fifo_segment_slice_t *fss;
1164
1165 fss = fsh_slice_get (fsh, slice_index);
1166 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001167}
1168
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001169u8
1170fifo_segment_get_mem_usage (fifo_segment_t * fs)
1171{
1172 uword size, in_use;
1173
1174 size = fifo_segment_size (fs);
1175 in_use =
1176 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1177 return (in_use * 100) / size;
1178}
1179
1180fifo_segment_mem_status_t
1181fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1182{
1183 if (!fsh->high_watermark || !fsh->low_watermark)
1184 return MEMORY_PRESSURE_NO_PRESSURE;
1185
1186 /* once the no-memory is detected, the status continues
1187 * until memory usage gets below the high watermark
1188 */
1189 if (fsh_has_reached_mem_limit (fsh))
1190 {
1191 if (usage >= fsh->high_watermark)
1192 return MEMORY_PRESSURE_NO_MEMORY;
1193 else
1194 fsh_reset_mem_limit (fsh);
1195 }
1196
1197 if (usage >= fsh->high_watermark)
1198 return MEMORY_PRESSURE_HIGH_PRESSURE;
1199
1200 else if (usage >= fsh->low_watermark)
1201 return MEMORY_PRESSURE_LOW_PRESSURE;
1202
1203 return MEMORY_PRESSURE_NO_PRESSURE;
1204}
1205
1206fifo_segment_mem_status_t
1207fifo_segment_get_mem_status (fifo_segment_t * fs)
1208{
1209 fifo_segment_header_t *fsh = fs->h;
1210 u8 usage = fifo_segment_get_mem_usage (fs);
1211
1212 return fifo_segment_determine_status (fsh, usage);
1213}
1214
Florin Coras88001c62019-04-24 14:44:46 -07001215u8 *
1216format_fifo_segment_type (u8 * s, va_list * args)
1217{
1218 fifo_segment_t *sp;
1219 sp = va_arg (*args, fifo_segment_t *);
1220 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1221
1222 if (st == SSVM_SEGMENT_PRIVATE)
1223 s = format (s, "%s", "private-heap");
1224 else if (st == SSVM_SEGMENT_MEMFD)
1225 s = format (s, "%s", "memfd");
1226 else if (st == SSVM_SEGMENT_SHM)
1227 s = format (s, "%s", "shm");
1228 else
1229 s = format (s, "%s", "unknown");
1230 return s;
1231}
1232
1233/**
1234 * Segment format function
1235 */
1236u8 *
1237format_fifo_segment (u8 * s, va_list * args)
1238{
Florin Corasef4f3e72019-12-11 14:27:53 -08001239 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001240 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001241 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001242 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1243 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001244 uword tracked_cached_bytes;
Florin Coras5368bb02019-06-09 09:24:33 -07001245 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001246 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001247 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001248 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001249 char *address;
1250 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001251 int i;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001252 uword allocated, in_use;
1253 f64 usage;
1254 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001255
1256 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001257
Florin Coras5368bb02019-06-09 09:24:33 -07001258 if (fs == 0)
1259 {
1260 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1261 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1262 return s;
1263 }
1264
Florin Coras5368bb02019-06-09 09:24:33 -07001265 fifo_segment_info (fs, &address, &size);
1266 active_fifos = fifo_segment_num_fifos (fs);
1267 free_fifos = fifo_segment_num_free_fifos (fs);
1268
1269 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1270 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1271 free_fifos, address);
1272
1273 if (!verbose)
1274 return s;
1275
Florin Coras62ddc032019-12-08 18:30:42 -08001276 fsh = fs->h;
1277
1278 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1279 if (free_chunks)
Florin Corasf8461bf2019-11-07 17:00:15 -08001280 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1281 indent + 2);
1282 else
1283 s = format (s, "\n");
1284
Florin Coras62ddc032019-12-08 18:30:42 -08001285 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001286 {
Florin Coras62ddc032019-12-08 18:30:42 -08001287 fss = fsh_slice_get (fsh, slice_index);
1288 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001289 {
Florin Coras62ddc032019-12-08 18:30:42 -08001290 c = fss->free_chunks[i];
1291 if (c == 0)
1292 continue;
1293 count = 0;
1294 while (c)
1295 {
1296 c = c->next;
1297 count++;
1298 }
1299
1300 chunk_size = fs_freelist_index_to_size (i);
1301 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1302 chunk_size >> 10, count);
1303
1304 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001305 }
Florin Coras88001c62019-04-24 14:44:46 -07001306 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001307
1308 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1309 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001310 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001311 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001312 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001313 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1314 allocated = fifo_segment_size (fs);
1315 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1316 usage = (100.0 * in_use) / allocated;
1317 mem_st = fifo_segment_get_mem_status (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001318
Florin Corasef4f3e72019-12-11 14:27:53 -08001319 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001320 format_white_space, indent + 2, format_memory_size,
Florin Coras62ddc032019-12-08 18:30:42 -08001321 free_seg_bytes, free_seg_bytes, format_memory_size,
Florin Corasf8461bf2019-11-07 17:00:15 -08001322 est_free_seg_bytes, est_free_seg_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001323 s =
1324 format (s,
1325 "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n",
1326 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1327 chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes,
1328 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1329 s =
1330 format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1331 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1332 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1333 fsh->n_reserved_bytes);
1334 s =
1335 format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space,
1336 indent + 2, usage, format_memory_size, in_use, format_memory_size,
1337 allocated, fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001338 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001339
Florin Coras88001c62019-04-24 14:44:46 -07001340 return s;
1341}
1342
1343/*
1344 * fd.io coding-style-patch-verification: ON
1345 *
1346 * Local Variables:
1347 * eval: (c-set-style "gnu")
1348 * End:
1349 */