blob: dbfa153b55ea101e4ff26d4b42e2129188b04c22 [file] [log] [blame]
Florin Coras88001c62019-04-24 14:44:46 -07001/*
2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <svm/fifo_segment.h>
17
Florin Coras06d47752020-03-06 02:23:58 +000018static inline fifo_segment_slice_t *
19fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
20{
21 return &fsh->slices[slice_index];
22}
23
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000024static char *fifo_segment_mem_status_strings[] = {
25#define _(sym,str) str,
26 foreach_segment_mem_status
27#undef _
28};
29
Florin Coras88001c62019-04-24 14:44:46 -070030/**
Florin Corasf9d4ab42019-05-11 16:55:53 -070031 * Fifo segment free space
32 *
33 * Queries the underlying memory manager, dlmalloc, for free space. Since this
34 * ends up walking the internal data structures, it should not be called
35 * indiscriminately.
36 *
37 * @param fs fifo segment
38 * @return number of free bytes
39 */
Florin Corasef4f3e72019-12-11 14:27:53 -080040static uword
Florin Coras4453c092019-12-19 10:13:15 -080041fsh_free_space (fifo_segment_header_t * fsh)
Florin Corasf9d4ab42019-05-11 16:55:53 -070042{
43 struct dlmallinfo dlminfo;
44
Florin Coras4453c092019-12-19 10:13:15 -080045 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070046 return dlminfo.fordblks;
47}
48
Florin Coras62ddc032019-12-08 18:30:42 -080049static inline void
50fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
51{
52 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
53}
54
55static inline uword
56fsh_n_free_bytes (fifo_segment_header_t * fsh)
57{
Florin Coras8122cc22019-12-18 13:06:41 -080058 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
59 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
60}
61
62static inline void
Florin Coras4453c092019-12-19 10:13:15 -080063fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080064{
Florin Coras4453c092019-12-19 10:13:15 -080065 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080066}
67
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000068static inline void
69fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
70{
71 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
72}
73
74static inline void
75fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
76{
77 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
78}
79
80static inline uword
81fsh_n_cached_bytes (fifo_segment_header_t * fsh)
82{
83 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000084 return n_cached;
85}
86
Florin Coras06d47752020-03-06 02:23:58 +000087static inline void
88fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
89{
90 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
91}
92
93static inline uword
94fsh_virtual_mem (fifo_segment_header_t * fsh)
95{
96 fifo_segment_slice_t *fss;
97 uword total_vm = 0;
98 int i;
99
100 for (i = 0; i < fsh->n_slices; i++)
101 {
102 fss = fsh_slice_get (fsh, i);
103 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
104 }
105 return total_vm;
106}
107
108void
109fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
110 int n_bytes)
111{
112 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
113 fss->virtual_mem += n_bytes;
114}
115
Florin Coras8122cc22019-12-18 13:06:41 -0800116static void
117fsh_check_mem (fifo_segment_header_t * fsh)
118{
119 uword thresh;
120
121 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
122 return;
123
124 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
125 2 * fsh->n_reserved_bytes);
126 if (fsh->n_free_bytes > thresh)
127 return;
128
129 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -0800130 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800131}
132
Florin Corasf9d4ab42019-05-11 16:55:53 -0700133/**
Florin Coras88001c62019-04-24 14:44:46 -0700134 * Initialize fifo segment shared header
135 */
136int
137fifo_segment_init (fifo_segment_t * fs)
138{
139 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800140 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700141 ssvm_shared_header_t *sh;
Florin Coras05c73172020-03-05 20:36:40 +0000142 u32 max_chunk_sz;
Florin Coras62ddc032019-12-08 18:30:42 -0800143 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700144 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800145 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700146
147 sh = fs->ssvm.sh;
148 oldheap = ssvm_push_heap (sh);
149
Florin Coras62ddc032019-12-08 18:30:42 -0800150 /*
151 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
152 * Long story made short: the "process-private" fifo segment
153 * is allocated from the main heap, not mmapped. dlmalloc
154 * only guarantees 4-byte alignment, and on aarch64
155 * the fsh can end up 4-byte but not 8-byte aligned.
156 * That eventually causes the atomic op in fifo_segment_update_free_bytes
157 * to backfire.
158 */
159 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700160 clib_memset (fsh, 0, sizeof (*fsh));
161 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800162 fs->n_slices = clib_max (fs->n_slices, 1);
163
164 fsh->ssvm_sh = fs->ssvm.sh;
165 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800166 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800167 FIFO_SEGMENT_MAX_FIFO_SIZE);
168 fsh->max_log2_chunk_size = max_log2 (max_fifo);
169
170 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
171 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
172 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
173
174 for (i = 0; i < fs->n_slices; i++)
175 {
176 fss = fsh_slice_get (fsh, i);
177 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800178 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800179 }
Florin Coras88001c62019-04-24 14:44:46 -0700180
181 ssvm_pop_heap (oldheap);
182
Florin Coras4453c092019-12-19 10:13:15 -0800183 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000184 fsh->n_cached_bytes = 0;
Florin Coras05c73172020-03-05 20:36:40 +0000185 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700186 sh->ready = 1;
187 return (0);
188}
189
190/**
Florin Coras88001c62019-04-24 14:44:46 -0700191 * Create a fifo segment and initialize as master
192 */
193int
194fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
195{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700196 fifo_segment_t *fs;
197 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700198 int rv;
199
Florin Coras88001c62019-04-24 14:44:46 -0700200 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700201 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700202
Florin Corasf9d4ab42019-05-11 16:55:53 -0700203 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
204 fs->ssvm.ssvm_size = a->segment_size;
205 fs->ssvm.i_am_master = 1;
206 fs->ssvm.my_pid = getpid ();
207 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
208 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700209
Florin Corasf9d4ab42019-05-11 16:55:53 -0700210 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700211 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700212 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700213 return (rv);
214 }
215
216 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700217 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700218
Florin Corasf9d4ab42019-05-11 16:55:53 -0700219 fifo_segment_init (fs);
220 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700221 return (0);
222}
223
224/**
225 * Attach as slave to a fifo segment
226 */
227int
228fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
229{
Florin Coras62ddc032019-12-08 18:30:42 -0800230 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700231 int rv;
232
Florin Coras62ddc032019-12-08 18:30:42 -0800233 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700234
Florin Coras62ddc032019-12-08 18:30:42 -0800235 fs->ssvm.ssvm_size = a->segment_size;
236 fs->ssvm.my_pid = getpid ();
237 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
238 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700239 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800240 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700241 else
Florin Coras62ddc032019-12-08 18:30:42 -0800242 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700243
Florin Coras62ddc032019-12-08 18:30:42 -0800244 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700245 {
Florin Coras62ddc032019-12-08 18:30:42 -0800246 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700247 return (rv);
248 }
249
250 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800251 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700252
Florin Coras62ddc032019-12-08 18:30:42 -0800253 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700254 return (0);
255}
256
257void
258fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
259{
260 ssvm_delete (&s->ssvm);
261 clib_memset (s, 0xfe, sizeof (*s));
262 pool_put (sm->segments, s);
263}
264
265u32
266fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
267{
268 return s - sm->segments;
269}
270
Florin Coras88001c62019-04-24 14:44:46 -0700271fifo_segment_t *
272fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
273{
274 return pool_elt_at_index (sm->segments, segment_index);
275}
276
277void
278fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
279{
Florin Coras404b8a32019-05-09 12:08:06 -0700280 *address = (char *) seg->ssvm.sh->ssvm_va;
281 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700282}
283
284void
285fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
286 u32 timeout_in_seconds)
287{
288 sm->next_baseva = baseva;
289 sm->timeout_in_seconds = timeout_in_seconds;
290}
291
Florin Corasf9d4ab42019-05-11 16:55:53 -0700292static inline u32
293fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700294{
Florin Corasf22f4e52019-12-19 16:10:58 -0800295 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
296 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800297 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700298}
299
Florin Corasf9d4ab42019-05-11 16:55:53 -0700300static inline u32
301fs_freelist_index_to_size (u32 fl_index)
302{
Florin Coras62ddc032019-12-08 18:30:42 -0800303 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700304}
305
Florin Coras88001c62019-04-24 14:44:46 -0700306static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800307fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700308{
309 /*
310 * 4K minimum. It's not likely that anything good will happen
311 * with a smaller FIFO.
312 */
313 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Coras62ddc032019-12-08 18:30:42 -0800314 && size <= (1 << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700315}
316
Florin Corascefd5d82019-05-05 13:19:57 -0700317static svm_fifo_t *
Florin Coras2de9c0f2020-02-02 19:30:39 +0000318fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
Florin Corascefd5d82019-05-05 13:19:57 -0700319{
320 svm_fifo_chunk_t *c;
321 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700322
Florin Coras62ddc032019-12-08 18:30:42 -0800323 f = fss->free_fifos;
324 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700325
326 if (!f || !c)
327 return 0;
328
Florin Coras62ddc032019-12-08 18:30:42 -0800329 fss->free_fifos = f->next;
330 fss->free_chunks[fl_index] = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800331 c->next = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700332 c->start_byte = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700333 memset (f, 0, sizeof (*f));
334 f->start_chunk = c;
335 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700336
Florin Coras62ddc032019-12-08 18:30:42 -0800337 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700338 return f;
339}
340
Florin Coras8e755a12020-02-06 16:59:31 +0000341svm_fifo_chunk_t *
342fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
343 fifo_segment_slice_t * fss, u32 data_bytes)
344{
345 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
346 svm_fifo_chunk_t *c, *first = 0, *next;
347
348 fl_index = fs_freelist_for_size (req_bytes);
349 if (fl_index > 0)
350 fl_index -= 1;
351
352 fl_size = fs_freelist_index_to_size (fl_index);
353
354 while (req_bytes)
355 {
356 c = fss->free_chunks[fl_index];
357 if (c)
358 {
359 fss->free_chunks[fl_index] = c->next;
360 c->next = first;
361 first = c;
362 n_alloc += fl_size;
363 req_bytes -= clib_min (fl_size, req_bytes);
364 }
365 else
366 {
367 /* Failed to allocate with smaller chunks */
368 if (fl_index == 0)
369 {
370 /* free all chunks if any allocated */
371 c = first;
372 while (c)
373 {
374 fl_index = fs_freelist_for_size (c->length);
375 fl_size = fs_freelist_index_to_size (fl_index);
376 next = c->next;
377 c->next = fss->free_chunks[fl_index];
378 fss->free_chunks[fl_index] = c;
379 fss->n_fl_chunk_bytes += fl_size;
380 c = next;
381 }
382 n_alloc = 0;
383 first = 0;
384 fl_index = fs_freelist_for_size (data_bytes);
385 if (fss->free_chunks[fl_index + 1])
386 {
387 fl_index += 1;
388 fl_size = fs_freelist_index_to_size (fl_index);
389 continue;
390 }
391
392 return 0;
393 }
394 fl_index -= 1;
395 fl_size = fl_size >> 1;
396 }
397 }
398
399 fss->n_fl_chunk_bytes -= n_alloc;
400 fsh_cached_bytes_sub (fsh, n_alloc);
401 return first;
402}
403
Florin Corascefd5d82019-05-05 13:19:57 -0700404static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800405fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
406 fifo_segment_slice_t * fss,
407 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700408{
Florin Corasc75ce4d2020-02-28 21:51:24 +0000409 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700410 u32 fl_index, fl_size, n_alloc = 0;
411 svm_fifo_t *f;
412
Florin Coras62ddc032019-12-08 18:30:42 -0800413 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700414 if (!f)
415 {
Florin Coras62ddc032019-12-08 18:30:42 -0800416 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700417 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
418 ssvm_pop_heap (oldheap);
419 if (!f)
420 return 0;
421 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800422 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700423 }
Florin Coras73cad332019-08-28 17:12:32 -0700424 else
425 {
Florin Coras62ddc032019-12-08 18:30:42 -0800426 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700427 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700428
Florin Corasd849edf2019-12-20 18:48:20 -0800429 fl_index = fs_freelist_for_size (data_bytes);
430 if (fl_index > 0)
431 fl_index -= 1;
432
Florin Corasf9d4ab42019-05-11 16:55:53 -0700433 fl_size = fs_freelist_index_to_size (fl_index);
434
435 while (data_bytes)
436 {
Florin Coras62ddc032019-12-08 18:30:42 -0800437 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700438 if (c)
439 {
Florin Coras62ddc032019-12-08 18:30:42 -0800440 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700441 if (!last)
442 last = c;
443 c->next = first;
444 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700445 n_alloc += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000446 data_bytes -= clib_min (fl_size, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700447 }
448 else
449 {
Florin Corasd849edf2019-12-20 18:48:20 -0800450 /* Failed to allocate with smaller chunks */
451 if (fl_index == 0)
452 {
453 /* free all chunks if any allocated */
454 c = first;
455 while (c)
456 {
457 fl_index = fs_freelist_for_size (c->length);
458 fl_size = fs_freelist_index_to_size (fl_index);
Florin Corasc75ce4d2020-02-28 21:51:24 +0000459 next = c->next;
Florin Corasd849edf2019-12-20 18:48:20 -0800460 c->next = fss->free_chunks[fl_index];
461 fss->free_chunks[fl_index] = c;
462 fss->n_fl_chunk_bytes += fl_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000463 n_alloc -= fl_size;
Florin Corasd849edf2019-12-20 18:48:20 -0800464 data_bytes += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000465 c = next;
Florin Corasd849edf2019-12-20 18:48:20 -0800466 }
467 first = last = 0;
468 fl_index = fs_freelist_for_size (data_bytes);
469 if (fss->free_chunks[fl_index + 1])
470 {
471 fl_index += 1;
472 fl_size = fs_freelist_index_to_size (fl_index);
473 continue;
474 }
475
476 f->next = fss->free_fifos;
477 fss->free_fifos = f;
478 return 0;
479 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700480 fl_index -= 1;
481 fl_size = fl_size >> 1;
482 }
483 }
Florin Corasd849edf2019-12-20 18:48:20 -0800484
Florin Corasf9d4ab42019-05-11 16:55:53 -0700485 f->start_chunk = first;
486 f->end_chunk = last;
Florin Coras62ddc032019-12-08 18:30:42 -0800487 fss->n_fl_chunk_bytes -= n_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000488 fsh_cached_bytes_sub (fsh, n_alloc);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700489 return f;
490}
491
492static int
Florin Coras8e755a12020-02-06 16:59:31 +0000493fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
494 fifo_segment_slice_t * fss,
495 u32 fl_index, u32 batch_size)
496{
497 u32 rounded_data_size;
498 svm_fifo_chunk_t *c;
499 void *oldheap;
500 uword size;
501 u8 *cmem;
502 int i;
503
504 rounded_data_size = fs_freelist_index_to_size (fl_index);
505 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
506
507 oldheap = ssvm_push_heap (fsh->ssvm_sh);
508 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
509 0 /* align_offset */ ,
510 0 /* os_out_of_memory */ );
511 ssvm_pop_heap (oldheap);
512
513 /* Out of space.. */
514 if (cmem == 0)
515 return -1;
516
517 /* Carve fifo + chunk space */
518 for (i = 0; i < batch_size; i++)
519 {
520 c = (svm_fifo_chunk_t *) cmem;
521 c->start_byte = 0;
522 c->length = rounded_data_size;
523 c->enq_rb_index = RBTREE_TNIL_INDEX;
524 c->deq_rb_index = RBTREE_TNIL_INDEX;
525 c->next = fss->free_chunks[fl_index];
526 fss->free_chunks[fl_index] = c;
527 cmem += sizeof (*c) + rounded_data_size;
528 }
529
530 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
531 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
532 fsh_free_bytes_sub (fsh, size);
533
534 return 0;
535}
536
537static int
Florin Coras62ddc032019-12-08 18:30:42 -0800538fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
539 fifo_segment_slice_t * fss,
540 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700541{
Florin Coras36a0c4d2020-01-31 08:28:02 -0800542 u32 hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700543 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700544 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700545 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800546 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700547 u8 *fmem;
548 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700549
Florin Corasf9d4ab42019-05-11 16:55:53 -0700550 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700551 hdrs = sizeof (*f) + sizeof (*c);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800552 size = (uword) (hdrs + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700553
Florin Coras62ddc032019-12-08 18:30:42 -0800554 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700555 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
556 0 /* align_offset */ ,
557 0 /* os_out_of_memory */ );
558 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700559
560 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700561 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700562 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700563
Florin Corascefd5d82019-05-05 13:19:57 -0700564 /* Carve fifo + chunk space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700565 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700566 {
Florin Corascefd5d82019-05-05 13:19:57 -0700567 f = (svm_fifo_t *) fmem;
568 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800569 f->next = fss->free_fifos;
570 fss->free_fifos = f;
Florin Corascefd5d82019-05-05 13:19:57 -0700571 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
572 c->start_byte = 0;
573 c->length = rounded_data_size;
Florin Corasf22f4e52019-12-19 16:10:58 -0800574 c->enq_rb_index = RBTREE_TNIL_INDEX;
575 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Coras62ddc032019-12-08 18:30:42 -0800576 c->next = fss->free_chunks[fl_index];
577 fss->free_chunks[fl_index] = c;
Florin Corascefd5d82019-05-05 13:19:57 -0700578 fmem += hdrs + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700579 }
Florin Corascefd5d82019-05-05 13:19:57 -0700580
Florin Coras62ddc032019-12-08 18:30:42 -0800581 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000582 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800583 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700584
585 return 0;
586}
587
588/**
589 * Try to allocate new fifo
590 *
591 * Tries the following steps in order:
592 * - grab fifo and chunk from freelists
593 * - batch fifo and chunk allocation
594 * - single fifo allocation
595 * - grab multiple fifo chunks from freelists
596 */
597static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800598fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
599 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700600{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700601 u32 fifo_sz, fl_index;
602 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800603 uword n_free_bytes;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000604 u32 min_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700605
Florin Coras2de9c0f2020-02-02 19:30:39 +0000606 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
607 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700608
Florin Corasc75ce4d2020-02-28 21:51:24 +0000609 clib_spinlock_lock (&fss->chunk_lock);
610
Florin Coras62ddc032019-12-08 18:30:42 -0800611 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700612 {
Florin Coras2de9c0f2020-02-02 19:30:39 +0000613 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700614 if (f)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000615 {
616 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
617 goto done;
618 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700619 }
Florin Coras8122cc22019-12-18 13:06:41 -0800620
Florin Coras0269fa12020-03-10 04:46:32 +0000621 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
622 fifo_sz += 1 << max_log2 (min_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800623 n_free_bytes = fsh_n_free_bytes (fsh);
Florin Coras0269fa12020-03-10 04:46:32 +0000624
Florin Coras62ddc032019-12-08 18:30:42 -0800625 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700626 {
Florin Coras0269fa12020-03-10 04:46:32 +0000627 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
628 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
629 {
630 f = fs_try_alloc_fifo_freelist (fss, fl_index);
631 if (f)
632 {
633 fsh_cached_bytes_sub (fsh,
634 fs_freelist_index_to_size (fl_index));
635 goto done;
636 }
637 }
638 else
639 {
640 fsh_check_mem (fsh);
641 n_free_bytes = fsh_n_free_bytes (fsh);
642 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700643 }
Florin Coras62ddc032019-12-08 18:30:42 -0800644 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700645 {
Florin Coras62ddc032019-12-08 18:30:42 -0800646 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000647 f = svm_fifo_alloc (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700648 ssvm_pop_heap (oldheap);
649 if (f)
650 {
Florin Coras62ddc032019-12-08 18:30:42 -0800651 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700652 goto done;
653 }
Florin Coras0269fa12020-03-10 04:46:32 +0000654 fsh_check_mem (fsh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700655 }
Florin Corasc75ce4d2020-02-28 21:51:24 +0000656 /* All failed, try to allocate min of data bytes and fifo sz */
657 fifo_sz = clib_min (fifo_sz, data_bytes);
658 if (fifo_sz <= fss->n_fl_chunk_bytes)
659 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700660
661done:
Florin Corasc75ce4d2020-02-28 21:51:24 +0000662 clib_spinlock_unlock (&fss->chunk_lock);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700663
Florin Corasf22f4e52019-12-19 16:10:58 -0800664 if (f)
Florin Corasc75ce4d2020-02-28 21:51:24 +0000665 {
666 f->size = data_bytes;
667 f->fs_hdr = fsh;
668 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700669 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700670}
671
Florin Corasf22f4e52019-12-19 16:10:58 -0800672svm_fifo_chunk_t *
673fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
674{
675 fifo_segment_slice_t *fss;
676 svm_fifo_chunk_t *c;
Florin Corasf22f4e52019-12-19 16:10:58 -0800677 int fl_index;
678
679 fl_index = fs_freelist_for_size (chunk_size);
680 fss = fsh_slice_get (fsh, slice_index);
681
682 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras8e755a12020-02-06 16:59:31 +0000683
Florin Corasf22f4e52019-12-19 16:10:58 -0800684 c = fss->free_chunks[fl_index];
685
Florin Coras8e755a12020-02-06 16:59:31 +0000686 if (c)
687 {
688 fss->free_chunks[fl_index] = c->next;
689 c->next = 0;
690 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
691 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
692 }
Florin Coras0269fa12020-03-10 04:46:32 +0000693 else
Florin Corasf22f4e52019-12-19 16:10:58 -0800694 {
Florin Coras0269fa12020-03-10 04:46:32 +0000695 void *oldheap;
696 uword n_free;
Florin Coras8e755a12020-02-06 16:59:31 +0000697 u32 batch;
698
Florin Coras0269fa12020-03-10 04:46:32 +0000699 chunk_size = fs_freelist_index_to_size (fl_index);
700 n_free = fsh_n_free_bytes (fsh);
701
702 if (chunk_size <= n_free)
703 {
704 oldheap = ssvm_push_heap (fsh->ssvm_sh);
705 c = svm_fifo_chunk_alloc (chunk_size);
706 ssvm_pop_heap (oldheap);
707
708 if (c)
709 {
710 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
711 goto done;
712 }
713
714 fsh_check_mem (fsh);
715 n_free = fsh_n_free_bytes (fsh);
716 }
717 if (chunk_size <= fss->n_fl_chunk_bytes)
718 {
719 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
720 if (c)
721 goto done;
722 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
723 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
724 {
725 fsh_check_mem (fsh);
726 goto done;
727 }
728 }
729 if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
730 {
731 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
732
733 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
734 batch = clib_min (batch + 1, n_free / min_size);
735 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
736 {
737 fsh_check_mem (fsh);
738 goto done;
739 }
740 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
741 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800742 }
743
744done:
745
746 clib_spinlock_unlock (&fss->chunk_lock);
747
748 return c;
749}
750
751static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000752fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000753 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800754{
755 svm_fifo_chunk_t *next;
756 int fl_index;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000757 u32 n_collect = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800758
759 clib_spinlock_lock (&fss->chunk_lock);
760
Florin Coras9e61d9a2020-02-05 21:13:18 +0000761 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800762 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000763 next = c->next;
764 fl_index = fs_freelist_for_size (c->length);
765 c->next = fss->free_chunks[fl_index];
766 c->enq_rb_index = RBTREE_TNIL_INDEX;
767 c->deq_rb_index = RBTREE_TNIL_INDEX;
768 fss->free_chunks[fl_index] = c;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000769 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000770 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800771 }
772
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000773 fss->n_fl_chunk_bytes += n_collect;
774 fsh_cached_bytes_add (fsh, n_collect);
775
Florin Corasf22f4e52019-12-19 16:10:58 -0800776 clib_spinlock_unlock (&fss->chunk_lock);
777}
778
779void
780fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000781 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800782{
783 fifo_segment_slice_t *fss;
784 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000785 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800786}
787
Florin Coras88001c62019-04-24 14:44:46 -0700788/**
789 * Allocate fifo in fifo segment
790 */
791svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800792fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
793 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700794{
Florin Coras62ddc032019-12-08 18:30:42 -0800795 fifo_segment_header_t *fsh = fs->h;
796 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700797 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700798
Florin Coras62ddc032019-12-08 18:30:42 -0800799 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700800
Florin Coras62ddc032019-12-08 18:30:42 -0800801 fss = fsh_slice_get (fsh, slice_index);
802 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700803 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700804 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700805
Florin Coras62ddc032019-12-08 18:30:42 -0800806 f->slice_index = slice_index;
807
Florin Corascefd5d82019-05-05 13:19:57 -0700808 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700809
Florin Coras88001c62019-04-24 14:44:46 -0700810 /* If rx fifo type add to active fifos list. When cleaning up segment,
811 * we need a list of active sessions that should be disconnected. Since
812 * both rx and tx fifos keep pointers to the session, it's enough to track
813 * only one. */
814 if (ftype == FIFO_SEGMENT_RX_FIFO)
815 {
Florin Coras62ddc032019-12-08 18:30:42 -0800816 if (fss->fifos)
Florin Coras88001c62019-04-24 14:44:46 -0700817 {
Florin Coras62ddc032019-12-08 18:30:42 -0800818 fss->fifos->prev = f;
819 f->next = fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700820 }
Florin Coras62ddc032019-12-08 18:30:42 -0800821 fss->fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700822 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800823
824 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
Florin Coras88001c62019-04-24 14:44:46 -0700825 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800826 else
827 {
828 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
829 }
830
Florin Coras62ddc032019-12-08 18:30:42 -0800831 fsh_active_fifos_update (fsh, 1);
Florin Coras06d47752020-03-06 02:23:58 +0000832 fss->virtual_mem += svm_fifo_size (f);
Florin Coras88001c62019-04-24 14:44:46 -0700833
834done:
Florin Coras88001c62019-04-24 14:44:46 -0700835 return (f);
836}
837
838/**
839 * Free fifo allocated in fifo segment
840 */
841void
Florin Corasb095a3c2019-04-25 12:58:46 -0700842fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700843{
Florin Coras62ddc032019-12-08 18:30:42 -0800844 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800845 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700846
847 ASSERT (f->refcnt > 0);
848
849 if (--f->refcnt > 0)
850 return;
851
Florin Coras62ddc032019-12-08 18:30:42 -0800852 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700853
854 /* Remove from active list. Only rx fifos are tracked */
855 if (f->flags & SVM_FIFO_F_LL_TRACKED)
856 {
857 if (f->prev)
858 f->prev->next = f->next;
859 else
Florin Coras62ddc032019-12-08 18:30:42 -0800860 fss->fifos = f->next;
Florin Coras88001c62019-04-24 14:44:46 -0700861 if (f->next)
862 f->next->prev = f->prev;
863 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
864 }
865
Florin Corascefd5d82019-05-05 13:19:57 -0700866 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000867 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700868
Florin Corasf22f4e52019-12-19 16:10:58 -0800869 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700870 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
871
Florin Corasb095a3c2019-04-25 12:58:46 -0700872 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800873 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700874 svm_fifo_free_ooo_data (f);
875
Florin Coras88001c62019-04-24 14:44:46 -0700876 if (CLIB_DEBUG)
877 {
878 f->master_session_index = ~0;
879 f->master_thread_index = ~0;
880 }
881
Florin Coras06d47752020-03-06 02:23:58 +0000882 fss->virtual_mem -= svm_fifo_size (f);
883
884 /* Add to free list */
885 f->next = fss->free_fifos;
886 f->prev = 0;
887 fss->free_fifos = f;
888
Florin Coras62ddc032019-12-08 18:30:42 -0800889 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700890}
891
Florin Corasf9d4ab42019-05-11 16:55:53 -0700892int
Florin Coras62ddc032019-12-08 18:30:42 -0800893fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
894 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700895{
896 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800897 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700898 svm_fifo_t *f;
899 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800900 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700901 u8 *fmem;
902 int i;
903
Florin Coras62ddc032019-12-08 18:30:42 -0800904 fss = fsh_slice_get (fsh, slice_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800905 size = (uword) (sizeof (*f)) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700906
Florin Coras62ddc032019-12-08 18:30:42 -0800907 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700908 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
909 0 /* align_offset */ ,
910 0 /* os_out_of_memory */ );
911 ssvm_pop_heap (oldheap);
912
913 /* Out of space.. */
914 if (fmem == 0)
915 return -1;
916
917 /* Carve fifo + chunk space */
918 for (i = 0; i < batch_size; i++)
919 {
920 f = (svm_fifo_t *) fmem;
921 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800922 f->next = fss->free_fifos;
923 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700924 fmem += sizeof (*f);
925 }
926
Florin Coras62ddc032019-12-08 18:30:42 -0800927 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700928
929 return 0;
930}
931
932int
Florin Coras62ddc032019-12-08 18:30:42 -0800933fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
934 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700935{
Florin Coras62ddc032019-12-08 18:30:42 -0800936 fifo_segment_header_t *fsh = fs->h;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800937 u32 rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -0800938 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700939 svm_fifo_chunk_t *c;
940 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800941 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700942 u8 *cmem;
943 int i;
944
Florin Coras62ddc032019-12-08 18:30:42 -0800945 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700946 {
947 clib_warning ("chunk size out of range %d", chunk_size);
948 return -1;
949 }
950
951 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700952 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800953 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700954
Florin Coras62ddc032019-12-08 18:30:42 -0800955 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700956 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
957 0 /* align_offset */ ,
958 0 /* os_out_of_memory */ );
959 ssvm_pop_heap (oldheap);
960
961 /* Out of space.. */
962 if (cmem == 0)
963 return -1;
964
Florin Coras62ddc032019-12-08 18:30:42 -0800965 fss = fsh_slice_get (fsh, slice_index);
966
Florin Corasf9d4ab42019-05-11 16:55:53 -0700967 /* Carve fifo + chunk space */
968 for (i = 0; i < batch_size; i++)
969 {
970 c = (svm_fifo_chunk_t *) cmem;
971 c->start_byte = 0;
972 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800973 c->next = fss->free_chunks[fl_index];
974 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700975 cmem += sizeof (*c) + rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000976 fsh_cached_bytes_add (fsh, rounded_data_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700977 }
978
Florin Coras62ddc032019-12-08 18:30:42 -0800979 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
980 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700981
982 return 0;
983}
984
Florin Coras88001c62019-04-24 14:44:46 -0700985/**
986 * Pre-allocates fifo pairs in fifo segment
987 */
988void
989fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
990 u32 rx_fifo_size, u32 tx_fifo_size,
991 u32 * n_fifo_pairs)
992{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700993 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +0000994 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -0800995 fifo_segment_header_t *fsh = fs->h;
996 int rx_fl_index, tx_fl_index, i;
997 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700998 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700999
1000 /* Parameter check */
1001 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1002 return;
1003
Florin Coras62ddc032019-12-08 18:30:42 -08001004 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001005 {
1006 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1007 return;
1008 }
1009
Florin Coras62ddc032019-12-08 18:30:42 -08001010 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001011 {
1012 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1013 return;
1014 }
1015
1016 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001017 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001018 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001019 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001020
Florin Corasf9d4ab42019-05-11 16:55:53 -07001021 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -07001022
Florin Coras88001c62019-04-24 14:44:46 -07001023 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -07001024 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -08001025 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -07001026 pairs_to_alloc = space_available / pair_size;
1027 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -08001028 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +00001029 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001030
Florin Coras62ddc032019-12-08 18:30:42 -08001031 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -07001032 return;
Florin Coras88001c62019-04-24 14:44:46 -07001033
Florin Coras62ddc032019-12-08 18:30:42 -08001034 for (i = 0; i < fs->n_slices; i++)
1035 {
1036 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +00001037 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1038 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1039 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1040 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1041 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -07001042
Florin Coras8d0149d2020-01-02 19:22:51 +00001043 /* Account for the pairs allocated */
1044 *n_fifo_pairs -= alloc_now;
1045 }
Florin Coras88001c62019-04-24 14:44:46 -07001046}
1047
1048/**
1049 * Get number of active fifos
1050 */
1051u32
1052fifo_segment_num_fifos (fifo_segment_t * fs)
1053{
Florin Coras62ddc032019-12-08 18:30:42 -08001054 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
Florin Coras88001c62019-04-24 14:44:46 -07001055}
1056
Florin Coras62ddc032019-12-08 18:30:42 -08001057static u32
1058fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -07001059{
Florin Coras88001c62019-04-24 14:44:46 -07001060 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001061 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001062
Florin Coras62ddc032019-12-08 18:30:42 -08001063 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001064 if (f == 0)
1065 return 0;
1066
1067 while (f)
1068 {
1069 f = f->next;
1070 count++;
1071 }
1072 return count;
1073}
1074
Florin Corasb095a3c2019-04-25 12:58:46 -07001075u32
Florin Coras62ddc032019-12-08 18:30:42 -08001076fifo_segment_num_free_fifos (fifo_segment_t * fs)
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_fifos (fss);
1087 }
1088 return count;
1089}
1090
1091static u32
1092fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001093{
1094 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001095 svm_fifo_chunk_t *c;
1096 int i;
1097
Florin Corasb095a3c2019-04-25 12:58:46 -07001098 /* Count all free chunks? */
1099 if (size == ~0)
1100 {
Florin Coras62ddc032019-12-08 18:30:42 -08001101 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001102 {
Florin Coras62ddc032019-12-08 18:30:42 -08001103 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001104 if (c == 0)
1105 continue;
1106
1107 while (c)
1108 {
1109 c = c->next;
1110 count++;
1111 }
1112 }
1113 return count;
1114 }
1115
1116 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001117 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001118
Florin Coras62ddc032019-12-08 18:30:42 -08001119 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001120 return 0;
1121
Florin Coras62ddc032019-12-08 18:30:42 -08001122 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001123 if (c == 0)
1124 return 0;
1125
1126 while (c)
1127 {
1128 c = c->next;
1129 count++;
1130 }
1131 return count;
1132}
1133
Florin Coras62ddc032019-12-08 18:30:42 -08001134u32
1135fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1136{
1137 fifo_segment_header_t *fsh = fs->h;
1138 fifo_segment_slice_t *fss;
1139 int slice_index;
1140 u32 count = 0;
1141
1142 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1143 {
1144 fss = fsh_slice_get (fsh, slice_index);
1145 count += fs_slice_num_free_chunks (fss, size);
1146 }
1147 return count;
1148}
1149
Florin Corasf9d4ab42019-05-11 16:55:53 -07001150void
1151fifo_segment_update_free_bytes (fifo_segment_t * fs)
1152{
Florin Coras4453c092019-12-19 10:13:15 -08001153 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001154}
1155
Florin Corasef4f3e72019-12-11 14:27:53 -08001156uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001157fifo_segment_size (fifo_segment_t * fs)
1158{
1159 return fs->ssvm.ssvm_size;
1160}
1161
1162u8
1163fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1164{
1165 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1166}
1167
1168void
1169fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1170{
1171 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1172}
1173
1174uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001175fifo_segment_free_bytes (fifo_segment_t * fs)
1176{
Florin Coras62ddc032019-12-08 18:30:42 -08001177 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001178}
1179
Florin Coras62ddc032019-12-08 18:30:42 -08001180uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001181fifo_segment_cached_bytes (fifo_segment_t * fs)
1182{
1183 return fsh_n_cached_bytes (fs->h);
1184}
1185
1186uword
Florin Coras75ccf7b2020-03-05 19:44:02 +00001187fifo_segment_available_bytes (fifo_segment_t * fs)
1188{
1189 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1190}
1191
1192uword
Florin Coras1c91c772019-06-25 18:14:13 -07001193fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001194{
Florin Coras62ddc032019-12-08 18:30:42 -08001195 fifo_segment_header_t *fsh = fs->h;
1196 fifo_segment_slice_t *fss;
1197 uword n_bytes = 0;
1198 int slice_index;
1199
1200 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1201 {
1202 fss = fsh_slice_get (fsh, slice_index);
1203 n_bytes += fss->n_fl_chunk_bytes;
1204 }
1205
1206 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001207}
1208
Florin Coras88001c62019-04-24 14:44:46 -07001209u8
1210fifo_segment_has_fifos (fifo_segment_t * fs)
1211{
Florin Coras62ddc032019-12-08 18:30:42 -08001212 fifo_segment_header_t *fsh = fs->h;
1213 fifo_segment_slice_t *fss;
1214 int slice_index;
1215
1216 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1217 {
1218 fss = fsh_slice_get (fsh, slice_index);
1219 if (fss->fifos)
1220 return 1;
1221 }
1222 return 0;
Florin Coras88001c62019-04-24 14:44:46 -07001223}
1224
1225svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001226fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001227{
Florin Coras62ddc032019-12-08 18:30:42 -08001228 fifo_segment_header_t *fsh = fs->h;
1229 fifo_segment_slice_t *fss;
1230
1231 fss = fsh_slice_get (fsh, slice_index);
1232 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001233}
1234
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001235u8
1236fifo_segment_get_mem_usage (fifo_segment_t * fs)
1237{
1238 uword size, in_use;
1239
1240 size = fifo_segment_size (fs);
1241 in_use =
1242 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1243 return (in_use * 100) / size;
1244}
1245
1246fifo_segment_mem_status_t
1247fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1248{
1249 if (!fsh->high_watermark || !fsh->low_watermark)
1250 return MEMORY_PRESSURE_NO_PRESSURE;
1251
1252 /* once the no-memory is detected, the status continues
1253 * until memory usage gets below the high watermark
1254 */
1255 if (fsh_has_reached_mem_limit (fsh))
1256 {
1257 if (usage >= fsh->high_watermark)
1258 return MEMORY_PRESSURE_NO_MEMORY;
1259 else
1260 fsh_reset_mem_limit (fsh);
1261 }
1262
1263 if (usage >= fsh->high_watermark)
1264 return MEMORY_PRESSURE_HIGH_PRESSURE;
1265
1266 else if (usage >= fsh->low_watermark)
1267 return MEMORY_PRESSURE_LOW_PRESSURE;
1268
1269 return MEMORY_PRESSURE_NO_PRESSURE;
1270}
1271
1272fifo_segment_mem_status_t
1273fifo_segment_get_mem_status (fifo_segment_t * fs)
1274{
1275 fifo_segment_header_t *fsh = fs->h;
1276 u8 usage = fifo_segment_get_mem_usage (fs);
1277
1278 return fifo_segment_determine_status (fsh, usage);
1279}
1280
Florin Coras88001c62019-04-24 14:44:46 -07001281u8 *
1282format_fifo_segment_type (u8 * s, va_list * args)
1283{
1284 fifo_segment_t *sp;
1285 sp = va_arg (*args, fifo_segment_t *);
1286 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1287
1288 if (st == SSVM_SEGMENT_PRIVATE)
1289 s = format (s, "%s", "private-heap");
1290 else if (st == SSVM_SEGMENT_MEMFD)
1291 s = format (s, "%s", "memfd");
1292 else if (st == SSVM_SEGMENT_SHM)
1293 s = format (s, "%s", "shm");
1294 else
1295 s = format (s, "%s", "unknown");
1296 return s;
1297}
1298
1299/**
1300 * Segment format function
1301 */
1302u8 *
1303format_fifo_segment (u8 * s, va_list * args)
1304{
Florin Corasef4f3e72019-12-11 14:27:53 -08001305 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001306 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001307 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001308 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1309 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001310 uword tracked_cached_bytes;
Florin Coras5368bb02019-06-09 09:24:33 -07001311 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001312 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001313 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001314 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001315 char *address;
1316 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001317 int i;
Florin Coras06d47752020-03-06 02:23:58 +00001318 uword allocated, in_use, virt;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001319 f64 usage;
1320 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001321
1322 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001323
Florin Coras5368bb02019-06-09 09:24:33 -07001324 if (fs == 0)
1325 {
1326 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1327 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1328 return s;
1329 }
1330
Florin Coras5368bb02019-06-09 09:24:33 -07001331 fifo_segment_info (fs, &address, &size);
1332 active_fifos = fifo_segment_num_fifos (fs);
1333 free_fifos = fifo_segment_num_free_fifos (fs);
1334
1335 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1336 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1337 free_fifos, address);
1338
1339 if (!verbose)
1340 return s;
1341
Florin Coras62ddc032019-12-08 18:30:42 -08001342 fsh = fs->h;
1343
1344 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1345 if (free_chunks)
Florin Corasf8461bf2019-11-07 17:00:15 -08001346 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1347 indent + 2);
1348 else
1349 s = format (s, "\n");
1350
Florin Coras62ddc032019-12-08 18:30:42 -08001351 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001352 {
Florin Coras62ddc032019-12-08 18:30:42 -08001353 fss = fsh_slice_get (fsh, slice_index);
1354 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001355 {
Florin Coras62ddc032019-12-08 18:30:42 -08001356 c = fss->free_chunks[i];
1357 if (c == 0)
1358 continue;
1359 count = 0;
1360 while (c)
1361 {
1362 c = c->next;
1363 count++;
1364 }
1365
1366 chunk_size = fs_freelist_index_to_size (i);
1367 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1368 chunk_size >> 10, count);
1369
1370 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001371 }
Florin Coras88001c62019-04-24 14:44:46 -07001372 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001373
1374 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1375 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001376 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001377 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001378 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001379 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1380 allocated = fifo_segment_size (fs);
1381 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1382 usage = (100.0 * in_use) / allocated;
1383 mem_st = fifo_segment_get_mem_status (fs);
Florin Coras06d47752020-03-06 02:23:58 +00001384 virt = fsh_virtual_mem (fsh);
Florin Corasf8461bf2019-11-07 17:00:15 -08001385
Florin Coras06d47752020-03-06 02:23:58 +00001386 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1387 " %U (%lu)\n", format_white_space, indent + 2,
1388 format_memory_size, free_seg_bytes, free_seg_bytes,
1389 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
1390 format_memory_size, fsh->n_reserved_bytes,
1391 fsh->n_reserved_bytes);
1392 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1393 " %U (%lu)\n", format_white_space, indent + 2,
1394 format_memory_size, chunk_bytes, chunk_bytes,
1395 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1396 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1397 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1398 format_white_space, indent + 2, fsh->n_active_fifos,
1399 format_memory_size, fifo_hdr, fifo_hdr);
1400 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1401 format_white_space, indent + 2, usage, format_memory_size,
1402 in_use, format_memory_size, allocated, format_memory_size, virt,
1403 fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001404 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001405
Florin Coras88001c62019-04-24 14:44:46 -07001406 return s;
1407}
1408
1409/*
1410 * fd.io coding-style-patch-verification: ON
1411 *
1412 * Local Variables:
1413 * eval: (c-set-style "gnu")
1414 * End:
1415 */