blob: ec6f082398e824549e90772c80005b5cca188157 [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);
78 ASSERT (n_cached >= 0);
79 return n_cached;
80}
81
Florin Coras8122cc22019-12-18 13:06:41 -080082static void
83fsh_check_mem (fifo_segment_header_t * fsh)
84{
85 uword thresh;
86
87 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
88 return;
89
90 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
91 2 * fsh->n_reserved_bytes);
92 if (fsh->n_free_bytes > thresh)
93 return;
94
95 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -080096 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -080097}
98
99static inline fifo_segment_slice_t *
100fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
101{
102 return &fsh->slices[slice_index];
103}
104
105static inline void
106fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
107{
108 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
109}
110
Florin Corasf9d4ab42019-05-11 16:55:53 -0700111/**
Florin Coras88001c62019-04-24 14:44:46 -0700112 * Initialize fifo segment shared header
113 */
114int
115fifo_segment_init (fifo_segment_t * fs)
116{
117 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800118 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700119 ssvm_shared_header_t *sh;
Florin Coras8122cc22019-12-18 13:06:41 -0800120 u32 max_chunk_sz, max_chunks;
Florin Coras62ddc032019-12-08 18:30:42 -0800121 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700122 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800123 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700124
125 sh = fs->ssvm.sh;
126 oldheap = ssvm_push_heap (sh);
127
Florin Coras62ddc032019-12-08 18:30:42 -0800128 /*
129 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
130 * Long story made short: the "process-private" fifo segment
131 * is allocated from the main heap, not mmapped. dlmalloc
132 * only guarantees 4-byte alignment, and on aarch64
133 * the fsh can end up 4-byte but not 8-byte aligned.
134 * That eventually causes the atomic op in fifo_segment_update_free_bytes
135 * to backfire.
136 */
137 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700138 clib_memset (fsh, 0, sizeof (*fsh));
139 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800140 fs->n_slices = clib_max (fs->n_slices, 1);
141
142 fsh->ssvm_sh = fs->ssvm.sh;
143 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800144 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800145 FIFO_SEGMENT_MAX_FIFO_SIZE);
146 fsh->max_log2_chunk_size = max_log2 (max_fifo);
147
148 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
149 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
150 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
151
152 for (i = 0; i < fs->n_slices; i++)
153 {
154 fss = fsh_slice_get (fsh, i);
155 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800156 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800157 }
Florin Coras88001c62019-04-24 14:44:46 -0700158
159 ssvm_pop_heap (oldheap);
160
Florin Coras4453c092019-12-19 10:13:15 -0800161 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000162 fsh->n_cached_bytes = 0;
Florin Coras8122cc22019-12-18 13:06:41 -0800163 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
164 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
Florin Coras88001c62019-04-24 14:44:46 -0700165 sh->ready = 1;
166 return (0);
167}
168
169/**
Florin Coras88001c62019-04-24 14:44:46 -0700170 * Create a fifo segment and initialize as master
171 */
172int
173fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
174{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700175 fifo_segment_t *fs;
176 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700177 int rv;
178
Florin Coras88001c62019-04-24 14:44:46 -0700179 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700180 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700181
Florin Corasf9d4ab42019-05-11 16:55:53 -0700182 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
183 fs->ssvm.ssvm_size = a->segment_size;
184 fs->ssvm.i_am_master = 1;
185 fs->ssvm.my_pid = getpid ();
186 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
187 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700188
Florin Corasf9d4ab42019-05-11 16:55:53 -0700189 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700190 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700191 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700192 return (rv);
193 }
194
195 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700196 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700197
Florin Corasf9d4ab42019-05-11 16:55:53 -0700198 fifo_segment_init (fs);
199 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700200 return (0);
201}
202
203/**
204 * Attach as slave to a fifo segment
205 */
206int
207fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
208{
Florin Coras62ddc032019-12-08 18:30:42 -0800209 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700210 int rv;
211
Florin Coras62ddc032019-12-08 18:30:42 -0800212 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700213
Florin Coras62ddc032019-12-08 18:30:42 -0800214 fs->ssvm.ssvm_size = a->segment_size;
215 fs->ssvm.my_pid = getpid ();
216 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
217 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700218 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800219 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700220 else
Florin Coras62ddc032019-12-08 18:30:42 -0800221 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700222
Florin Coras62ddc032019-12-08 18:30:42 -0800223 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700224 {
Florin Coras62ddc032019-12-08 18:30:42 -0800225 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700226 return (rv);
227 }
228
229 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800230 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700231
Florin Coras62ddc032019-12-08 18:30:42 -0800232 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700233 return (0);
234}
235
236void
237fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
238{
239 ssvm_delete (&s->ssvm);
240 clib_memset (s, 0xfe, sizeof (*s));
241 pool_put (sm->segments, s);
242}
243
244u32
245fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
246{
247 return s - sm->segments;
248}
249
Florin Coras88001c62019-04-24 14:44:46 -0700250fifo_segment_t *
251fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
252{
253 return pool_elt_at_index (sm->segments, segment_index);
254}
255
256void
257fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
258{
Florin Coras404b8a32019-05-09 12:08:06 -0700259 *address = (char *) seg->ssvm.sh->ssvm_va;
260 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700261}
262
263void
264fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
265 u32 timeout_in_seconds)
266{
267 sm->next_baseva = baseva;
268 sm->timeout_in_seconds = timeout_in_seconds;
269}
270
Florin Corasf9d4ab42019-05-11 16:55:53 -0700271static inline u32
272fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700273{
Florin Corasf22f4e52019-12-19 16:10:58 -0800274 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
275 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800276 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700277}
278
Florin Corasf9d4ab42019-05-11 16:55:53 -0700279static inline u32
280fs_freelist_index_to_size (u32 fl_index)
281{
Florin Coras62ddc032019-12-08 18:30:42 -0800282 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700283}
284
Florin Coras88001c62019-04-24 14:44:46 -0700285static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800286fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700287{
288 /*
289 * 4K minimum. It's not likely that anything good will happen
290 * with a smaller FIFO.
291 */
292 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Coras62ddc032019-12-08 18:30:42 -0800293 && size <= (1 << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700294}
295
Florin Corascefd5d82019-05-05 13:19:57 -0700296static svm_fifo_t *
Florin Coras2de9c0f2020-02-02 19:30:39 +0000297fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
Florin Corascefd5d82019-05-05 13:19:57 -0700298{
299 svm_fifo_chunk_t *c;
300 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700301
Florin Coras62ddc032019-12-08 18:30:42 -0800302 f = fss->free_fifos;
303 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700304
305 if (!f || !c)
306 return 0;
307
Florin Coras62ddc032019-12-08 18:30:42 -0800308 fss->free_fifos = f->next;
309 fss->free_chunks[fl_index] = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800310 c->next = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700311 c->start_byte = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700312 memset (f, 0, sizeof (*f));
313 f->start_chunk = c;
314 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700315
Florin Coras62ddc032019-12-08 18:30:42 -0800316 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700317 return f;
318}
319
Florin Coras8e755a12020-02-06 16:59:31 +0000320svm_fifo_chunk_t *
321fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
322 fifo_segment_slice_t * fss, u32 data_bytes)
323{
324 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
325 svm_fifo_chunk_t *c, *first = 0, *next;
326
327 fl_index = fs_freelist_for_size (req_bytes);
328 if (fl_index > 0)
329 fl_index -= 1;
330
331 fl_size = fs_freelist_index_to_size (fl_index);
332
333 while (req_bytes)
334 {
335 c = fss->free_chunks[fl_index];
336 if (c)
337 {
338 fss->free_chunks[fl_index] = c->next;
339 c->next = first;
340 first = c;
341 n_alloc += fl_size;
342 req_bytes -= clib_min (fl_size, req_bytes);
343 }
344 else
345 {
346 /* Failed to allocate with smaller chunks */
347 if (fl_index == 0)
348 {
349 /* free all chunks if any allocated */
350 c = first;
351 while (c)
352 {
353 fl_index = fs_freelist_for_size (c->length);
354 fl_size = fs_freelist_index_to_size (fl_index);
355 next = c->next;
356 c->next = fss->free_chunks[fl_index];
357 fss->free_chunks[fl_index] = c;
358 fss->n_fl_chunk_bytes += fl_size;
359 c = next;
360 }
361 n_alloc = 0;
362 first = 0;
363 fl_index = fs_freelist_for_size (data_bytes);
364 if (fss->free_chunks[fl_index + 1])
365 {
366 fl_index += 1;
367 fl_size = fs_freelist_index_to_size (fl_index);
368 continue;
369 }
370
371 return 0;
372 }
373 fl_index -= 1;
374 fl_size = fl_size >> 1;
375 }
376 }
377
378 fss->n_fl_chunk_bytes -= n_alloc;
379 fsh_cached_bytes_sub (fsh, n_alloc);
380 return first;
381}
382
Florin Corascefd5d82019-05-05 13:19:57 -0700383static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800384fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
385 fifo_segment_slice_t * fss,
386 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700387{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700388 svm_fifo_chunk_t *c, *first = 0, *last = 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700389 u32 fl_index, fl_size, n_alloc = 0;
390 svm_fifo_t *f;
391
Florin Coras62ddc032019-12-08 18:30:42 -0800392 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700393 if (!f)
394 {
Florin Coras62ddc032019-12-08 18:30:42 -0800395 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700396 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
397 ssvm_pop_heap (oldheap);
398 if (!f)
399 return 0;
400 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800401 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700402 }
Florin Coras73cad332019-08-28 17:12:32 -0700403 else
404 {
Florin Coras62ddc032019-12-08 18:30:42 -0800405 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700406 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700407
Florin Corasd849edf2019-12-20 18:48:20 -0800408 fl_index = fs_freelist_for_size (data_bytes);
409 if (fl_index > 0)
410 fl_index -= 1;
411
Florin Corasf9d4ab42019-05-11 16:55:53 -0700412 fl_size = fs_freelist_index_to_size (fl_index);
413
414 while (data_bytes)
415 {
Florin Coras62ddc032019-12-08 18:30:42 -0800416 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700417 if (c)
418 {
Florin Coras62ddc032019-12-08 18:30:42 -0800419 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700420 if (!last)
421 last = c;
422 c->next = first;
423 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700424 n_alloc += fl_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700425 data_bytes -= c->length;
426 }
427 else
428 {
Florin Corasd849edf2019-12-20 18:48:20 -0800429 /* Failed to allocate with smaller chunks */
430 if (fl_index == 0)
431 {
432 /* free all chunks if any allocated */
433 c = first;
434 while (c)
435 {
436 fl_index = fs_freelist_for_size (c->length);
437 fl_size = fs_freelist_index_to_size (fl_index);
438 c->next = fss->free_chunks[fl_index];
439 fss->free_chunks[fl_index] = c;
440 fss->n_fl_chunk_bytes += fl_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000441 n_alloc -= fl_size;
Florin Corasd849edf2019-12-20 18:48:20 -0800442 data_bytes += fl_size;
443 }
444 first = last = 0;
445 fl_index = fs_freelist_for_size (data_bytes);
446 if (fss->free_chunks[fl_index + 1])
447 {
448 fl_index += 1;
449 fl_size = fs_freelist_index_to_size (fl_index);
450 continue;
451 }
452
453 f->next = fss->free_fifos;
454 fss->free_fifos = f;
455 return 0;
456 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700457 fl_index -= 1;
458 fl_size = fl_size >> 1;
459 }
460 }
Florin Corasd849edf2019-12-20 18:48:20 -0800461
Florin Corasf9d4ab42019-05-11 16:55:53 -0700462 f->start_chunk = first;
463 f->end_chunk = last;
Florin Coras62ddc032019-12-08 18:30:42 -0800464 fss->n_fl_chunk_bytes -= n_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000465 fsh_cached_bytes_sub (fsh, n_alloc);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700466 return f;
467}
468
469static int
Florin Coras8e755a12020-02-06 16:59:31 +0000470fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
471 fifo_segment_slice_t * fss,
472 u32 fl_index, u32 batch_size)
473{
474 u32 rounded_data_size;
475 svm_fifo_chunk_t *c;
476 void *oldheap;
477 uword size;
478 u8 *cmem;
479 int i;
480
481 rounded_data_size = fs_freelist_index_to_size (fl_index);
482 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
483
484 oldheap = ssvm_push_heap (fsh->ssvm_sh);
485 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
486 0 /* align_offset */ ,
487 0 /* os_out_of_memory */ );
488 ssvm_pop_heap (oldheap);
489
490 /* Out of space.. */
491 if (cmem == 0)
492 return -1;
493
494 /* Carve fifo + chunk space */
495 for (i = 0; i < batch_size; i++)
496 {
497 c = (svm_fifo_chunk_t *) cmem;
498 c->start_byte = 0;
499 c->length = rounded_data_size;
500 c->enq_rb_index = RBTREE_TNIL_INDEX;
501 c->deq_rb_index = RBTREE_TNIL_INDEX;
502 c->next = fss->free_chunks[fl_index];
503 fss->free_chunks[fl_index] = c;
504 cmem += sizeof (*c) + rounded_data_size;
505 }
506
507 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
508 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
509 fsh_free_bytes_sub (fsh, size);
510
511 return 0;
512}
513
514static int
Florin Coras62ddc032019-12-08 18:30:42 -0800515fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
516 fifo_segment_slice_t * fss,
517 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700518{
Florin Coras36a0c4d2020-01-31 08:28:02 -0800519 u32 hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700520 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700521 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700522 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800523 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700524 u8 *fmem;
525 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700526
Florin Corasf9d4ab42019-05-11 16:55:53 -0700527 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700528 hdrs = sizeof (*f) + sizeof (*c);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800529 size = (uword) (hdrs + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700530
Florin Coras62ddc032019-12-08 18:30:42 -0800531 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700532 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
533 0 /* align_offset */ ,
534 0 /* os_out_of_memory */ );
535 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700536
537 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700538 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700539 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700540
Florin Corascefd5d82019-05-05 13:19:57 -0700541 /* Carve fifo + chunk space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700542 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700543 {
Florin Corascefd5d82019-05-05 13:19:57 -0700544 f = (svm_fifo_t *) fmem;
545 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800546 f->next = fss->free_fifos;
547 fss->free_fifos = f;
Florin Corascefd5d82019-05-05 13:19:57 -0700548 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
549 c->start_byte = 0;
550 c->length = rounded_data_size;
Florin Corasf22f4e52019-12-19 16:10:58 -0800551 c->enq_rb_index = RBTREE_TNIL_INDEX;
552 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Coras62ddc032019-12-08 18:30:42 -0800553 c->next = fss->free_chunks[fl_index];
554 fss->free_chunks[fl_index] = c;
Florin Corascefd5d82019-05-05 13:19:57 -0700555 fmem += hdrs + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700556 }
Florin Corascefd5d82019-05-05 13:19:57 -0700557
Florin Coras62ddc032019-12-08 18:30:42 -0800558 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000559 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800560 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700561
562 return 0;
563}
564
565/**
566 * Try to allocate new fifo
567 *
568 * Tries the following steps in order:
569 * - grab fifo and chunk from freelists
570 * - batch fifo and chunk allocation
571 * - single fifo allocation
572 * - grab multiple fifo chunks from freelists
573 */
574static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800575fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
576 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700577{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700578 u32 fifo_sz, fl_index;
579 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800580 uword n_free_bytes;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000581 u32 min_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700582
Florin Coras2de9c0f2020-02-02 19:30:39 +0000583 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
584 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700585 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000586 fifo_sz += 1 << max_log2 (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700587
Florin Coras62ddc032019-12-08 18:30:42 -0800588 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700589 {
Florin Coras2de9c0f2020-02-02 19:30:39 +0000590 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700591 if (f)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000592 {
593 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
594 goto done;
595 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700596 }
Florin Coras8122cc22019-12-18 13:06:41 -0800597
598 fsh_check_mem (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800599 n_free_bytes = fsh_n_free_bytes (fsh);
600 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700601 {
Florin Coras62ddc032019-12-08 18:30:42 -0800602 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
Florin Corasf9d4ab42019-05-11 16:55:53 -0700603 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
604 goto done;
605
Florin Coras2de9c0f2020-02-02 19:30:39 +0000606 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000607 if (f)
608 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700609 goto done;
610 }
Florin Coras62ddc032019-12-08 18:30:42 -0800611 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700612 {
Florin Coras62ddc032019-12-08 18:30:42 -0800613 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000614 f = svm_fifo_alloc (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700615 ssvm_pop_heap (oldheap);
616 if (f)
617 {
Florin Coras62ddc032019-12-08 18:30:42 -0800618 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700619 goto done;
620 }
621 }
Florin Coras62ddc032019-12-08 18:30:42 -0800622 if (data_bytes <= fss->n_fl_chunk_bytes)
623 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700624
625done:
626
Florin Corasf22f4e52019-12-19 16:10:58 -0800627 if (f)
628 f->fs_hdr = fsh;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700629 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700630}
631
Florin Corasf22f4e52019-12-19 16:10:58 -0800632svm_fifo_chunk_t *
633fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
634{
635 fifo_segment_slice_t *fss;
636 svm_fifo_chunk_t *c;
637 void *oldheap;
638 int fl_index;
Florin Coras8e755a12020-02-06 16:59:31 +0000639 uword n_free;
Florin Corasf22f4e52019-12-19 16:10:58 -0800640
641 fl_index = fs_freelist_for_size (chunk_size);
642 fss = fsh_slice_get (fsh, slice_index);
643
644 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras8e755a12020-02-06 16:59:31 +0000645
Florin Corasf22f4e52019-12-19 16:10:58 -0800646 c = fss->free_chunks[fl_index];
647
Florin Coras8e755a12020-02-06 16:59:31 +0000648 if (c)
649 {
650 fss->free_chunks[fl_index] = c->next;
651 c->next = 0;
652 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
653 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
654 }
655 else if (chunk_size <= (n_free = fsh_n_free_bytes (fsh)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800656 {
657 fsh_check_mem (fsh);
Florin Coras8e755a12020-02-06 16:59:31 +0000658
Florin Corasf22f4e52019-12-19 16:10:58 -0800659 chunk_size = fs_freelist_index_to_size (fl_index);
Florin Coras8e755a12020-02-06 16:59:31 +0000660 if (n_free < chunk_size)
Florin Corasf22f4e52019-12-19 16:10:58 -0800661 goto done;
662
663 oldheap = ssvm_push_heap (fsh->ssvm_sh);
664 c = svm_fifo_chunk_alloc (chunk_size);
665 ssvm_pop_heap (oldheap);
666
667 if (!c)
668 goto done;
669
670 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
671 }
Florin Coras8e755a12020-02-06 16:59:31 +0000672 else if (chunk_size <= fss->n_fl_chunk_bytes)
Florin Corasf22f4e52019-12-19 16:10:58 -0800673 {
Florin Coras8e755a12020-02-06 16:59:31 +0000674 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
675 }
676 else if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
677 {
678 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
679 u32 batch;
680
681 fsh_check_mem (fsh);
682 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
683 batch = clib_min (batch + 1, n_free / min_size);
684 if (!fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
685 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800686 }
687
688done:
689
690 clib_spinlock_unlock (&fss->chunk_lock);
691
692 return c;
693}
694
695static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000696fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000697 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800698{
699 svm_fifo_chunk_t *next;
700 int fl_index;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000701 u32 n_collect = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800702
703 clib_spinlock_lock (&fss->chunk_lock);
704
Florin Coras9e61d9a2020-02-05 21:13:18 +0000705 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800706 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000707 next = c->next;
708 fl_index = fs_freelist_for_size (c->length);
709 c->next = fss->free_chunks[fl_index];
710 c->enq_rb_index = RBTREE_TNIL_INDEX;
711 c->deq_rb_index = RBTREE_TNIL_INDEX;
712 fss->free_chunks[fl_index] = c;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000713 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000714 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800715 }
716
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000717 fss->n_fl_chunk_bytes += n_collect;
718 fsh_cached_bytes_add (fsh, n_collect);
719
Florin Corasf22f4e52019-12-19 16:10:58 -0800720 clib_spinlock_unlock (&fss->chunk_lock);
721}
722
723void
724fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000725 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800726{
727 fifo_segment_slice_t *fss;
728 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000729 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800730}
731
Florin Coras88001c62019-04-24 14:44:46 -0700732/**
733 * Allocate fifo in fifo segment
734 */
735svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800736fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
737 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700738{
Florin Coras62ddc032019-12-08 18:30:42 -0800739 fifo_segment_header_t *fsh = fs->h;
740 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700741 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700742
Florin Coras62ddc032019-12-08 18:30:42 -0800743 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700744
Florin Coras62ddc032019-12-08 18:30:42 -0800745 fss = fsh_slice_get (fsh, slice_index);
746 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700747 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700748 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700749
Florin Coras62ddc032019-12-08 18:30:42 -0800750 f->slice_index = slice_index;
751
Florin Corascefd5d82019-05-05 13:19:57 -0700752 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700753
Florin Coras88001c62019-04-24 14:44:46 -0700754 /* If rx fifo type add to active fifos list. When cleaning up segment,
755 * we need a list of active sessions that should be disconnected. Since
756 * both rx and tx fifos keep pointers to the session, it's enough to track
757 * only one. */
758 if (ftype == FIFO_SEGMENT_RX_FIFO)
759 {
Florin Coras62ddc032019-12-08 18:30:42 -0800760 if (fss->fifos)
Florin Coras88001c62019-04-24 14:44:46 -0700761 {
Florin Coras62ddc032019-12-08 18:30:42 -0800762 fss->fifos->prev = f;
763 f->next = fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700764 }
Florin Coras62ddc032019-12-08 18:30:42 -0800765 fss->fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700766 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800767
768 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
Florin Coras88001c62019-04-24 14:44:46 -0700769 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800770 else
771 {
772 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
773 }
774
Florin Coras62ddc032019-12-08 18:30:42 -0800775 fsh_active_fifos_update (fsh, 1);
Florin Coras88001c62019-04-24 14:44:46 -0700776
777done:
Florin Coras88001c62019-04-24 14:44:46 -0700778 return (f);
779}
780
781/**
782 * Free fifo allocated in fifo segment
783 */
784void
Florin Corasb095a3c2019-04-25 12:58:46 -0700785fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700786{
Florin Coras62ddc032019-12-08 18:30:42 -0800787 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800788 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700789
790 ASSERT (f->refcnt > 0);
791
792 if (--f->refcnt > 0)
793 return;
794
Florin Coras62ddc032019-12-08 18:30:42 -0800795 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700796
797 /* Remove from active list. Only rx fifos are tracked */
798 if (f->flags & SVM_FIFO_F_LL_TRACKED)
799 {
800 if (f->prev)
801 f->prev->next = f->next;
802 else
Florin Coras62ddc032019-12-08 18:30:42 -0800803 fss->fifos = f->next;
Florin Coras88001c62019-04-24 14:44:46 -0700804 if (f->next)
805 f->next->prev = f->prev;
806 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
807 }
808
809 /* Add to free list */
Florin Coras62ddc032019-12-08 18:30:42 -0800810 f->next = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700811 f->prev = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800812 fss->free_fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700813
Florin Corascefd5d82019-05-05 13:19:57 -0700814 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000815 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700816
Florin Corasf22f4e52019-12-19 16:10:58 -0800817 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700818 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
819
Florin Corasb095a3c2019-04-25 12:58:46 -0700820 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800821 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700822 svm_fifo_free_ooo_data (f);
823
Florin Coras88001c62019-04-24 14:44:46 -0700824 if (CLIB_DEBUG)
825 {
826 f->master_session_index = ~0;
827 f->master_thread_index = ~0;
828 }
829
Florin Coras62ddc032019-12-08 18:30:42 -0800830 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700831}
832
Florin Corasf9d4ab42019-05-11 16:55:53 -0700833int
Florin Coras62ddc032019-12-08 18:30:42 -0800834fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
835 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700836{
837 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800838 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700839 svm_fifo_t *f;
840 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800841 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700842 u8 *fmem;
843 int i;
844
Florin Coras62ddc032019-12-08 18:30:42 -0800845 fss = fsh_slice_get (fsh, slice_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800846 size = (uword) (sizeof (*f)) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700847
Florin Coras62ddc032019-12-08 18:30:42 -0800848 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700849 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
850 0 /* align_offset */ ,
851 0 /* os_out_of_memory */ );
852 ssvm_pop_heap (oldheap);
853
854 /* Out of space.. */
855 if (fmem == 0)
856 return -1;
857
858 /* Carve fifo + chunk space */
859 for (i = 0; i < batch_size; i++)
860 {
861 f = (svm_fifo_t *) fmem;
862 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800863 f->next = fss->free_fifos;
864 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700865 fmem += sizeof (*f);
866 }
867
Florin Coras62ddc032019-12-08 18:30:42 -0800868 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700869
870 return 0;
871}
872
873int
Florin Coras62ddc032019-12-08 18:30:42 -0800874fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
875 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700876{
Florin Coras62ddc032019-12-08 18:30:42 -0800877 fifo_segment_header_t *fsh = fs->h;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800878 u32 rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -0800879 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700880 svm_fifo_chunk_t *c;
881 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800882 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700883 u8 *cmem;
884 int i;
885
Florin Coras62ddc032019-12-08 18:30:42 -0800886 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700887 {
888 clib_warning ("chunk size out of range %d", chunk_size);
889 return -1;
890 }
891
892 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700893 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800894 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700895
Florin Coras62ddc032019-12-08 18:30:42 -0800896 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700897 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
898 0 /* align_offset */ ,
899 0 /* os_out_of_memory */ );
900 ssvm_pop_heap (oldheap);
901
902 /* Out of space.. */
903 if (cmem == 0)
904 return -1;
905
Florin Coras62ddc032019-12-08 18:30:42 -0800906 fss = fsh_slice_get (fsh, slice_index);
907
Florin Corasf9d4ab42019-05-11 16:55:53 -0700908 /* Carve fifo + chunk space */
909 for (i = 0; i < batch_size; i++)
910 {
911 c = (svm_fifo_chunk_t *) cmem;
912 c->start_byte = 0;
913 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800914 c->next = fss->free_chunks[fl_index];
915 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700916 cmem += sizeof (*c) + rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000917 fsh_cached_bytes_add (fsh, rounded_data_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700918 }
919
Florin Coras62ddc032019-12-08 18:30:42 -0800920 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
921 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700922
923 return 0;
924}
925
Florin Coras88001c62019-04-24 14:44:46 -0700926/**
927 * Pre-allocates fifo pairs in fifo segment
928 */
929void
930fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
931 u32 rx_fifo_size, u32 tx_fifo_size,
932 u32 * n_fifo_pairs)
933{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700934 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +0000935 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -0800936 fifo_segment_header_t *fsh = fs->h;
937 int rx_fl_index, tx_fl_index, i;
938 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700939 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700940
941 /* Parameter check */
942 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
943 return;
944
Florin Coras62ddc032019-12-08 18:30:42 -0800945 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700946 {
947 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
948 return;
949 }
950
Florin Coras62ddc032019-12-08 18:30:42 -0800951 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700952 {
953 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
954 return;
955 }
956
957 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700958 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700959 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700960 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700961
Florin Corasf9d4ab42019-05-11 16:55:53 -0700962 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -0700963
Florin Coras88001c62019-04-24 14:44:46 -0700964 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -0700965 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -0800966 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -0700967 pairs_to_alloc = space_available / pair_size;
968 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -0800969 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +0000970 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700971
Florin Coras62ddc032019-12-08 18:30:42 -0800972 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -0700973 return;
Florin Coras88001c62019-04-24 14:44:46 -0700974
Florin Coras62ddc032019-12-08 18:30:42 -0800975 for (i = 0; i < fs->n_slices; i++)
976 {
977 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +0000978 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
979 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
980 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
981 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
982 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -0700983
Florin Coras8d0149d2020-01-02 19:22:51 +0000984 /* Account for the pairs allocated */
985 *n_fifo_pairs -= alloc_now;
986 }
Florin Coras88001c62019-04-24 14:44:46 -0700987}
988
989/**
990 * Get number of active fifos
991 */
992u32
993fifo_segment_num_fifos (fifo_segment_t * fs)
994{
Florin Coras62ddc032019-12-08 18:30:42 -0800995 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
Florin Coras88001c62019-04-24 14:44:46 -0700996}
997
Florin Coras62ddc032019-12-08 18:30:42 -0800998static u32
999fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -07001000{
Florin Coras88001c62019-04-24 14:44:46 -07001001 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001002 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001003
Florin Coras62ddc032019-12-08 18:30:42 -08001004 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001005 if (f == 0)
1006 return 0;
1007
1008 while (f)
1009 {
1010 f = f->next;
1011 count++;
1012 }
1013 return count;
1014}
1015
Florin Corasb095a3c2019-04-25 12:58:46 -07001016u32
Florin Coras62ddc032019-12-08 18:30:42 -08001017fifo_segment_num_free_fifos (fifo_segment_t * fs)
1018{
1019 fifo_segment_header_t *fsh = fs->h;
1020 fifo_segment_slice_t *fss;
1021 int slice_index;
1022 u32 count = 0;
1023
1024 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1025 {
1026 fss = fsh_slice_get (fsh, slice_index);
1027 count += fs_slice_num_free_fifos (fss);
1028 }
1029 return count;
1030}
1031
1032static u32
1033fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001034{
1035 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001036 svm_fifo_chunk_t *c;
1037 int i;
1038
Florin Corasb095a3c2019-04-25 12:58:46 -07001039 /* Count all free chunks? */
1040 if (size == ~0)
1041 {
Florin Coras62ddc032019-12-08 18:30:42 -08001042 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001043 {
Florin Coras62ddc032019-12-08 18:30:42 -08001044 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001045 if (c == 0)
1046 continue;
1047
1048 while (c)
1049 {
1050 c = c->next;
1051 count++;
1052 }
1053 }
1054 return count;
1055 }
1056
1057 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001058 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001059
Florin Coras62ddc032019-12-08 18:30:42 -08001060 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001061 return 0;
1062
Florin Coras62ddc032019-12-08 18:30:42 -08001063 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001064 if (c == 0)
1065 return 0;
1066
1067 while (c)
1068 {
1069 c = c->next;
1070 count++;
1071 }
1072 return count;
1073}
1074
Florin Coras62ddc032019-12-08 18:30:42 -08001075u32
1076fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1077{
1078 fifo_segment_header_t *fsh = fs->h;
1079 fifo_segment_slice_t *fss;
1080 int slice_index;
1081 u32 count = 0;
1082
1083 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1084 {
1085 fss = fsh_slice_get (fsh, slice_index);
1086 count += fs_slice_num_free_chunks (fss, size);
1087 }
1088 return count;
1089}
1090
Florin Corasf9d4ab42019-05-11 16:55:53 -07001091void
1092fifo_segment_update_free_bytes (fifo_segment_t * fs)
1093{
Florin Coras4453c092019-12-19 10:13:15 -08001094 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001095}
1096
Florin Corasef4f3e72019-12-11 14:27:53 -08001097uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001098fifo_segment_size (fifo_segment_t * fs)
1099{
1100 return fs->ssvm.ssvm_size;
1101}
1102
1103u8
1104fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1105{
1106 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1107}
1108
1109void
1110fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1111{
1112 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1113}
1114
1115uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001116fifo_segment_free_bytes (fifo_segment_t * fs)
1117{
Florin Coras62ddc032019-12-08 18:30:42 -08001118 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001119}
1120
Florin Coras62ddc032019-12-08 18:30:42 -08001121uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001122fifo_segment_cached_bytes (fifo_segment_t * fs)
1123{
1124 return fsh_n_cached_bytes (fs->h);
1125}
1126
1127uword
Florin Coras1c91c772019-06-25 18:14:13 -07001128fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001129{
Florin Coras62ddc032019-12-08 18:30:42 -08001130 fifo_segment_header_t *fsh = fs->h;
1131 fifo_segment_slice_t *fss;
1132 uword n_bytes = 0;
1133 int slice_index;
1134
1135 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1136 {
1137 fss = fsh_slice_get (fsh, slice_index);
1138 n_bytes += fss->n_fl_chunk_bytes;
1139 }
1140
1141 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001142}
1143
Florin Coras88001c62019-04-24 14:44:46 -07001144u8
1145fifo_segment_has_fifos (fifo_segment_t * fs)
1146{
Florin Coras62ddc032019-12-08 18:30:42 -08001147 fifo_segment_header_t *fsh = fs->h;
1148 fifo_segment_slice_t *fss;
1149 int slice_index;
1150
1151 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1152 {
1153 fss = fsh_slice_get (fsh, slice_index);
1154 if (fss->fifos)
1155 return 1;
1156 }
1157 return 0;
Florin Coras88001c62019-04-24 14:44:46 -07001158}
1159
1160svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001161fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001162{
Florin Coras62ddc032019-12-08 18:30:42 -08001163 fifo_segment_header_t *fsh = fs->h;
1164 fifo_segment_slice_t *fss;
1165
1166 fss = fsh_slice_get (fsh, slice_index);
1167 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001168}
1169
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001170u8
1171fifo_segment_get_mem_usage (fifo_segment_t * fs)
1172{
1173 uword size, in_use;
1174
1175 size = fifo_segment_size (fs);
1176 in_use =
1177 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1178 return (in_use * 100) / size;
1179}
1180
1181fifo_segment_mem_status_t
1182fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1183{
1184 if (!fsh->high_watermark || !fsh->low_watermark)
1185 return MEMORY_PRESSURE_NO_PRESSURE;
1186
1187 /* once the no-memory is detected, the status continues
1188 * until memory usage gets below the high watermark
1189 */
1190 if (fsh_has_reached_mem_limit (fsh))
1191 {
1192 if (usage >= fsh->high_watermark)
1193 return MEMORY_PRESSURE_NO_MEMORY;
1194 else
1195 fsh_reset_mem_limit (fsh);
1196 }
1197
1198 if (usage >= fsh->high_watermark)
1199 return MEMORY_PRESSURE_HIGH_PRESSURE;
1200
1201 else if (usage >= fsh->low_watermark)
1202 return MEMORY_PRESSURE_LOW_PRESSURE;
1203
1204 return MEMORY_PRESSURE_NO_PRESSURE;
1205}
1206
1207fifo_segment_mem_status_t
1208fifo_segment_get_mem_status (fifo_segment_t * fs)
1209{
1210 fifo_segment_header_t *fsh = fs->h;
1211 u8 usage = fifo_segment_get_mem_usage (fs);
1212
1213 return fifo_segment_determine_status (fsh, usage);
1214}
1215
Florin Coras88001c62019-04-24 14:44:46 -07001216u8 *
1217format_fifo_segment_type (u8 * s, va_list * args)
1218{
1219 fifo_segment_t *sp;
1220 sp = va_arg (*args, fifo_segment_t *);
1221 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1222
1223 if (st == SSVM_SEGMENT_PRIVATE)
1224 s = format (s, "%s", "private-heap");
1225 else if (st == SSVM_SEGMENT_MEMFD)
1226 s = format (s, "%s", "memfd");
1227 else if (st == SSVM_SEGMENT_SHM)
1228 s = format (s, "%s", "shm");
1229 else
1230 s = format (s, "%s", "unknown");
1231 return s;
1232}
1233
1234/**
1235 * Segment format function
1236 */
1237u8 *
1238format_fifo_segment (u8 * s, va_list * args)
1239{
Florin Corasef4f3e72019-12-11 14:27:53 -08001240 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001241 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001242 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001243 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1244 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001245 uword tracked_cached_bytes;
Florin Coras5368bb02019-06-09 09:24:33 -07001246 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001247 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001248 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001249 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001250 char *address;
1251 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001252 int i;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001253 uword allocated, in_use;
1254 f64 usage;
1255 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001256
1257 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001258
Florin Coras5368bb02019-06-09 09:24:33 -07001259 if (fs == 0)
1260 {
1261 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1262 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1263 return s;
1264 }
1265
Florin Coras5368bb02019-06-09 09:24:33 -07001266 fifo_segment_info (fs, &address, &size);
1267 active_fifos = fifo_segment_num_fifos (fs);
1268 free_fifos = fifo_segment_num_free_fifos (fs);
1269
1270 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1271 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1272 free_fifos, address);
1273
1274 if (!verbose)
1275 return s;
1276
Florin Coras62ddc032019-12-08 18:30:42 -08001277 fsh = fs->h;
1278
1279 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1280 if (free_chunks)
Florin Corasf8461bf2019-11-07 17:00:15 -08001281 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1282 indent + 2);
1283 else
1284 s = format (s, "\n");
1285
Florin Coras62ddc032019-12-08 18:30:42 -08001286 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001287 {
Florin Coras62ddc032019-12-08 18:30:42 -08001288 fss = fsh_slice_get (fsh, slice_index);
1289 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001290 {
Florin Coras62ddc032019-12-08 18:30:42 -08001291 c = fss->free_chunks[i];
1292 if (c == 0)
1293 continue;
1294 count = 0;
1295 while (c)
1296 {
1297 c = c->next;
1298 count++;
1299 }
1300
1301 chunk_size = fs_freelist_index_to_size (i);
1302 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1303 chunk_size >> 10, count);
1304
1305 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001306 }
Florin Coras88001c62019-04-24 14:44:46 -07001307 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001308
1309 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1310 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001311 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001312 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001313 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001314 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1315 allocated = fifo_segment_size (fs);
1316 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1317 usage = (100.0 * in_use) / allocated;
1318 mem_st = fifo_segment_get_mem_status (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001319
Florin Corasef4f3e72019-12-11 14:27:53 -08001320 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001321 format_white_space, indent + 2, format_memory_size,
Florin Coras62ddc032019-12-08 18:30:42 -08001322 free_seg_bytes, free_seg_bytes, format_memory_size,
Florin Corasf8461bf2019-11-07 17:00:15 -08001323 est_free_seg_bytes, est_free_seg_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001324 s =
1325 format (s,
1326 "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n",
1327 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1328 chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes,
1329 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1330 s =
1331 format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1332 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1333 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1334 fsh->n_reserved_bytes);
1335 s =
1336 format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space,
1337 indent + 2, usage, format_memory_size, in_use, format_memory_size,
1338 allocated, fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001339 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001340
Florin Coras88001c62019-04-24 14:44:46 -07001341 return s;
1342}
1343
1344/*
1345 * fd.io coding-style-patch-verification: ON
1346 *
1347 * Local Variables:
1348 * eval: (c-set-style "gnu")
1349 * End:
1350 */