blob: 8503819f42f19bd2820d9c6d7464ae96ef614a04 [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{
Damjan Marion4537c302020-09-28 19:03:37 +020043 return clib_mem_get_heap_free_space (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070044}
45
Florin Coras62ddc032019-12-08 18:30:42 -080046static inline void
47fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
48{
49 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
50}
51
52static inline uword
53fsh_n_free_bytes (fifo_segment_header_t * fsh)
54{
Florin Coras8122cc22019-12-18 13:06:41 -080055 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
56 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
57}
58
59static inline void
Florin Coras4453c092019-12-19 10:13:15 -080060fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080061{
Florin Coras4453c092019-12-19 10:13:15 -080062 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080063}
64
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000065static inline void
66fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size)
67{
68 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
69}
70
71static inline void
72fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size)
73{
74 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
75}
76
77static inline uword
78fsh_n_cached_bytes (fifo_segment_header_t * fsh)
79{
80 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000081 return n_cached;
82}
83
Florin Coras06d47752020-03-06 02:23:58 +000084static inline void
85fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
86{
87 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
88}
89
Florin Coras7a90e502020-04-09 04:46:14 +000090static inline u32
91fsh_n_active_fifos (fifo_segment_header_t * fsh)
92{
93 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
94}
95
Florin Coras06d47752020-03-06 02:23:58 +000096static inline uword
97fsh_virtual_mem (fifo_segment_header_t * fsh)
98{
99 fifo_segment_slice_t *fss;
100 uword total_vm = 0;
101 int i;
102
103 for (i = 0; i < fsh->n_slices; i++)
104 {
105 fss = fsh_slice_get (fsh, i);
106 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
107 }
108 return total_vm;
109}
110
111void
112fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
113 int n_bytes)
114{
115 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
116 fss->virtual_mem += n_bytes;
117}
118
Florin Coras8122cc22019-12-18 13:06:41 -0800119static void
120fsh_check_mem (fifo_segment_header_t * fsh)
121{
122 uword thresh;
123
124 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
125 return;
126
127 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
128 2 * fsh->n_reserved_bytes);
129 if (fsh->n_free_bytes > thresh)
130 return;
131
132 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -0800133 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800134}
135
Florin Corasf9d4ab42019-05-11 16:55:53 -0700136/**
Florin Coras88001c62019-04-24 14:44:46 -0700137 * Initialize fifo segment shared header
138 */
139int
140fifo_segment_init (fifo_segment_t * fs)
141{
142 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800143 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700144 ssvm_shared_header_t *sh;
Florin Coras05c73172020-03-05 20:36:40 +0000145 u32 max_chunk_sz;
Florin Coras62ddc032019-12-08 18:30:42 -0800146 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700147 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800148 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700149
150 sh = fs->ssvm.sh;
151 oldheap = ssvm_push_heap (sh);
152
Florin Coras62ddc032019-12-08 18:30:42 -0800153 /*
154 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
155 * Long story made short: the "process-private" fifo segment
156 * is allocated from the main heap, not mmapped. dlmalloc
157 * only guarantees 4-byte alignment, and on aarch64
158 * the fsh can end up 4-byte but not 8-byte aligned.
159 * That eventually causes the atomic op in fifo_segment_update_free_bytes
160 * to backfire.
161 */
162 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700163 clib_memset (fsh, 0, sizeof (*fsh));
164 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800165 fs->n_slices = clib_max (fs->n_slices, 1);
166
167 fsh->ssvm_sh = fs->ssvm.sh;
168 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800169 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800170 FIFO_SEGMENT_MAX_FIFO_SIZE);
171 fsh->max_log2_chunk_size = max_log2 (max_fifo);
172
173 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
174 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
175 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
176
177 for (i = 0; i < fs->n_slices; i++)
178 {
179 fss = fsh_slice_get (fsh, i);
180 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000181 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800182 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800183 }
Florin Coras88001c62019-04-24 14:44:46 -0700184
185 ssvm_pop_heap (oldheap);
186
Florin Coras4453c092019-12-19 10:13:15 -0800187 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000188 fsh->n_cached_bytes = 0;
Florin Coras05c73172020-03-05 20:36:40 +0000189 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700190 sh->ready = 1;
191 return (0);
192}
193
194/**
Florin Coras88001c62019-04-24 14:44:46 -0700195 * Create a fifo segment and initialize as master
196 */
197int
198fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
199{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700200 fifo_segment_t *fs;
201 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700202 int rv;
203
Florin Coras88001c62019-04-24 14:44:46 -0700204 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700205 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700206
Florin Corasf9d4ab42019-05-11 16:55:53 -0700207 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
208 fs->ssvm.ssvm_size = a->segment_size;
Florin Coras5220a262020-09-29 18:11:24 -0700209 fs->ssvm.is_server = 1;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700210 fs->ssvm.my_pid = getpid ();
211 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
212 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700213
Florin Coras5220a262020-09-29 18:11:24 -0700214 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700215 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700216 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700217 return (rv);
218 }
219
220 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700221 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700222
Florin Corasf9d4ab42019-05-11 16:55:53 -0700223 fifo_segment_init (fs);
224 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700225 return (0);
226}
227
228/**
229 * Attach as slave to a fifo segment
230 */
231int
232fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
233{
Florin Coras62ddc032019-12-08 18:30:42 -0800234 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700235 int rv;
236
Florin Coras62ddc032019-12-08 18:30:42 -0800237 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700238
Florin Coras62ddc032019-12-08 18:30:42 -0800239 fs->ssvm.ssvm_size = a->segment_size;
240 fs->ssvm.my_pid = getpid ();
241 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
242 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700243 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800244 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700245 else
Florin Coras62ddc032019-12-08 18:30:42 -0800246 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700247
Florin Coras5220a262020-09-29 18:11:24 -0700248 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700249 {
Florin Coras62ddc032019-12-08 18:30:42 -0800250 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700251 return (rv);
252 }
253
254 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800255 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700256
Florin Coras62ddc032019-12-08 18:30:42 -0800257 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700258 return (0);
259}
260
261void
262fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
263{
264 ssvm_delete (&s->ssvm);
265 clib_memset (s, 0xfe, sizeof (*s));
266 pool_put (sm->segments, s);
267}
268
269u32
270fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
271{
272 return s - sm->segments;
273}
274
Florin Coras88001c62019-04-24 14:44:46 -0700275fifo_segment_t *
276fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
277{
278 return pool_elt_at_index (sm->segments, segment_index);
279}
280
281void
282fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
283{
Florin Coras404b8a32019-05-09 12:08:06 -0700284 *address = (char *) seg->ssvm.sh->ssvm_va;
285 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700286}
287
288void
289fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
290 u32 timeout_in_seconds)
291{
292 sm->next_baseva = baseva;
293 sm->timeout_in_seconds = timeout_in_seconds;
294}
295
Florin Corasf9d4ab42019-05-11 16:55:53 -0700296static inline u32
297fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700298{
Florin Corasf22f4e52019-12-19 16:10:58 -0800299 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
300 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800301 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700302}
303
Florin Corasf9d4ab42019-05-11 16:55:53 -0700304static inline u32
305fs_freelist_index_to_size (u32 fl_index)
306{
Florin Coras62ddc032019-12-08 18:30:42 -0800307 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700308}
309
Florin Coras88001c62019-04-24 14:44:46 -0700310static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800311fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700312{
313 /*
314 * 4K minimum. It's not likely that anything good will happen
315 * with a smaller FIFO.
316 */
317 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Corasd18528f2020-03-27 18:41:54 +0000318 && size <= (1ULL << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700319}
320
Florin Corascefd5d82019-05-05 13:19:57 -0700321static svm_fifo_t *
Florin Coras2de9c0f2020-02-02 19:30:39 +0000322fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index)
Florin Corascefd5d82019-05-05 13:19:57 -0700323{
324 svm_fifo_chunk_t *c;
325 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700326
Florin Coras62ddc032019-12-08 18:30:42 -0800327 f = fss->free_fifos;
328 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700329
330 if (!f || !c)
331 return 0;
332
Florin Coras62ddc032019-12-08 18:30:42 -0800333 fss->free_fifos = f->next;
334 fss->free_chunks[fl_index] = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800335 c->next = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700336 c->start_byte = 0;
Florin Corascefd5d82019-05-05 13:19:57 -0700337 memset (f, 0, sizeof (*f));
338 f->start_chunk = c;
339 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700340
Florin Coras62ddc032019-12-08 18:30:42 -0800341 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700342 return f;
343}
344
Florin Coras8e755a12020-02-06 16:59:31 +0000345svm_fifo_chunk_t *
346fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
347 fifo_segment_slice_t * fss, u32 data_bytes)
348{
349 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
350 svm_fifo_chunk_t *c, *first = 0, *next;
351
352 fl_index = fs_freelist_for_size (req_bytes);
353 if (fl_index > 0)
354 fl_index -= 1;
355
356 fl_size = fs_freelist_index_to_size (fl_index);
357
358 while (req_bytes)
359 {
360 c = fss->free_chunks[fl_index];
361 if (c)
362 {
363 fss->free_chunks[fl_index] = c->next;
364 c->next = first;
365 first = c;
366 n_alloc += fl_size;
367 req_bytes -= clib_min (fl_size, req_bytes);
368 }
369 else
370 {
371 /* Failed to allocate with smaller chunks */
372 if (fl_index == 0)
373 {
374 /* free all chunks if any allocated */
375 c = first;
376 while (c)
377 {
378 fl_index = fs_freelist_for_size (c->length);
379 fl_size = fs_freelist_index_to_size (fl_index);
380 next = c->next;
381 c->next = fss->free_chunks[fl_index];
382 fss->free_chunks[fl_index] = c;
383 fss->n_fl_chunk_bytes += fl_size;
384 c = next;
385 }
386 n_alloc = 0;
387 first = 0;
388 fl_index = fs_freelist_for_size (data_bytes);
389 if (fss->free_chunks[fl_index + 1])
390 {
391 fl_index += 1;
392 fl_size = fs_freelist_index_to_size (fl_index);
393 continue;
394 }
395
396 return 0;
397 }
398 fl_index -= 1;
399 fl_size = fl_size >> 1;
400 }
401 }
402
403 fss->n_fl_chunk_bytes -= n_alloc;
404 fsh_cached_bytes_sub (fsh, n_alloc);
405 return first;
406}
407
Florin Corascefd5d82019-05-05 13:19:57 -0700408static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800409fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
410 fifo_segment_slice_t * fss,
411 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700412{
Florin Corasc75ce4d2020-02-28 21:51:24 +0000413 svm_fifo_chunk_t *c, *first = 0, *last = 0, *next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700414 u32 fl_index, fl_size, n_alloc = 0;
415 svm_fifo_t *f;
416
Florin Coras62ddc032019-12-08 18:30:42 -0800417 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700418 if (!f)
419 {
Florin Coras5226d262020-04-27 22:41:39 +0000420 if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t)))
421 return 0;
422
Florin Coras62ddc032019-12-08 18:30:42 -0800423 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras5226d262020-04-27 22:41:39 +0000424 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700425 ssvm_pop_heap (oldheap);
426 if (!f)
427 return 0;
428 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800429 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700430 }
Florin Coras73cad332019-08-28 17:12:32 -0700431 else
432 {
Florin Coras62ddc032019-12-08 18:30:42 -0800433 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700434 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700435
Florin Corasd849edf2019-12-20 18:48:20 -0800436 fl_index = fs_freelist_for_size (data_bytes);
437 if (fl_index > 0)
438 fl_index -= 1;
439
Florin Corasf9d4ab42019-05-11 16:55:53 -0700440 fl_size = fs_freelist_index_to_size (fl_index);
441
442 while (data_bytes)
443 {
Florin Coras62ddc032019-12-08 18:30:42 -0800444 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700445 if (c)
446 {
Florin Coras62ddc032019-12-08 18:30:42 -0800447 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700448 if (!last)
449 last = c;
450 c->next = first;
451 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700452 n_alloc += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000453 data_bytes -= clib_min (fl_size, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700454 }
455 else
456 {
Florin Corasd849edf2019-12-20 18:48:20 -0800457 /* Failed to allocate with smaller chunks */
458 if (fl_index == 0)
459 {
460 /* free all chunks if any allocated */
461 c = first;
462 while (c)
463 {
464 fl_index = fs_freelist_for_size (c->length);
465 fl_size = fs_freelist_index_to_size (fl_index);
Florin Corasc75ce4d2020-02-28 21:51:24 +0000466 next = c->next;
Florin Corasd849edf2019-12-20 18:48:20 -0800467 c->next = fss->free_chunks[fl_index];
468 fss->free_chunks[fl_index] = c;
469 fss->n_fl_chunk_bytes += fl_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000470 n_alloc -= fl_size;
Florin Corasd849edf2019-12-20 18:48:20 -0800471 data_bytes += fl_size;
Florin Corasc75ce4d2020-02-28 21:51:24 +0000472 c = next;
Florin Corasd849edf2019-12-20 18:48:20 -0800473 }
474 first = last = 0;
475 fl_index = fs_freelist_for_size (data_bytes);
476 if (fss->free_chunks[fl_index + 1])
477 {
478 fl_index += 1;
479 fl_size = fs_freelist_index_to_size (fl_index);
480 continue;
481 }
482
483 f->next = fss->free_fifos;
484 fss->free_fifos = f;
485 return 0;
486 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700487 fl_index -= 1;
488 fl_size = fl_size >> 1;
489 }
490 }
Florin Corasd849edf2019-12-20 18:48:20 -0800491
Florin Corasf9d4ab42019-05-11 16:55:53 -0700492 f->start_chunk = first;
493 f->end_chunk = last;
Florin Coras62ddc032019-12-08 18:30:42 -0800494 fss->n_fl_chunk_bytes -= n_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000495 fsh_cached_bytes_sub (fsh, n_alloc);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700496 return f;
497}
498
499static int
Florin Coras8e755a12020-02-06 16:59:31 +0000500fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
501 fifo_segment_slice_t * fss,
502 u32 fl_index, u32 batch_size)
503{
504 u32 rounded_data_size;
505 svm_fifo_chunk_t *c;
506 void *oldheap;
507 uword size;
508 u8 *cmem;
509 int i;
510
511 rounded_data_size = fs_freelist_index_to_size (fl_index);
512 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
513
514 oldheap = ssvm_push_heap (fsh->ssvm_sh);
515 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
516 0 /* align_offset */ ,
517 0 /* os_out_of_memory */ );
518 ssvm_pop_heap (oldheap);
519
520 /* Out of space.. */
521 if (cmem == 0)
522 return -1;
523
524 /* Carve fifo + chunk space */
525 for (i = 0; i < batch_size; i++)
526 {
527 c = (svm_fifo_chunk_t *) cmem;
528 c->start_byte = 0;
529 c->length = rounded_data_size;
530 c->enq_rb_index = RBTREE_TNIL_INDEX;
531 c->deq_rb_index = RBTREE_TNIL_INDEX;
532 c->next = fss->free_chunks[fl_index];
533 fss->free_chunks[fl_index] = c;
534 cmem += sizeof (*c) + rounded_data_size;
535 }
536
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000537 fss->num_chunks[fl_index] += batch_size;
Florin Coras8e755a12020-02-06 16:59:31 +0000538 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
539 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
540 fsh_free_bytes_sub (fsh, size);
541
542 return 0;
543}
544
545static int
Florin Coras62ddc032019-12-08 18:30:42 -0800546fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
547 fifo_segment_slice_t * fss,
548 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700549{
Florin Coras36a0c4d2020-01-31 08:28:02 -0800550 u32 hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700551 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700552 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700553 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800554 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700555 u8 *fmem;
556 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700557
Florin Corasf9d4ab42019-05-11 16:55:53 -0700558 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700559 hdrs = sizeof (*f) + sizeof (*c);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800560 size = (uword) (hdrs + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700561
Florin Coras62ddc032019-12-08 18:30:42 -0800562 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700563 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
564 0 /* align_offset */ ,
565 0 /* os_out_of_memory */ );
566 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700567
568 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700569 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700570 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700571
Florin Coras635f5062020-04-28 20:40:57 +0000572 /* Carve fifo hdr space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700573 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700574 {
Florin Corascefd5d82019-05-05 13:19:57 -0700575 f = (svm_fifo_t *) fmem;
576 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800577 f->next = fss->free_fifos;
578 fss->free_fifos = f;
Florin Coras635f5062020-04-28 20:40:57 +0000579 fmem += sizeof (*f);
580 }
581
582 /* Carve chunk space */
583 for (i = 0; i < batch_size; i++)
584 {
585 c = (svm_fifo_chunk_t *) fmem;
Florin Corascefd5d82019-05-05 13:19:57 -0700586 c->start_byte = 0;
587 c->length = rounded_data_size;
Florin Corasf22f4e52019-12-19 16:10:58 -0800588 c->enq_rb_index = RBTREE_TNIL_INDEX;
589 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Coras62ddc032019-12-08 18:30:42 -0800590 c->next = fss->free_chunks[fl_index];
591 fss->free_chunks[fl_index] = c;
Florin Coras635f5062020-04-28 20:40:57 +0000592 fmem += sizeof (svm_fifo_chunk_t) + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700593 }
Florin Corascefd5d82019-05-05 13:19:57 -0700594
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000595 fss->num_chunks[fl_index] += batch_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800596 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000597 fsh_cached_bytes_add (fsh, batch_size * rounded_data_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800598 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700599
600 return 0;
601}
602
603/**
604 * Try to allocate new fifo
605 *
606 * Tries the following steps in order:
607 * - grab fifo and chunk from freelists
608 * - batch fifo and chunk allocation
609 * - single fifo allocation
610 * - grab multiple fifo chunks from freelists
611 */
612static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800613fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
614 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700615{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700616 u32 fifo_sz, fl_index;
617 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800618 uword n_free_bytes;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000619 u32 min_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700620
Florin Coras2de9c0f2020-02-02 19:30:39 +0000621 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
622 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700623
Florin Coras0e6199d2020-04-17 20:15:22 +0000624 if (fl_index >= vec_len (fss->free_chunks))
625 return 0;
626
Florin Corasc75ce4d2020-02-28 21:51:24 +0000627 clib_spinlock_lock (&fss->chunk_lock);
628
Florin Coras62ddc032019-12-08 18:30:42 -0800629 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700630 {
Florin Coras2de9c0f2020-02-02 19:30:39 +0000631 f = fs_try_alloc_fifo_freelist (fss, fl_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700632 if (f)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000633 {
634 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
635 goto done;
636 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700637 }
Florin Coras8122cc22019-12-18 13:06:41 -0800638
Florin Coras0269fa12020-03-10 04:46:32 +0000639 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
640 fifo_sz += 1 << max_log2 (min_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800641 n_free_bytes = fsh_n_free_bytes (fsh);
Florin Coras0269fa12020-03-10 04:46:32 +0000642
Florin Coras62ddc032019-12-08 18:30:42 -0800643 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700644 {
Florin Coras0269fa12020-03-10 04:46:32 +0000645 if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index,
646 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
647 {
648 f = fs_try_alloc_fifo_freelist (fss, fl_index);
649 if (f)
650 {
651 fsh_cached_bytes_sub (fsh,
652 fs_freelist_index_to_size (fl_index));
653 goto done;
654 }
655 }
656 else
657 {
658 fsh_check_mem (fsh);
659 n_free_bytes = fsh_n_free_bytes (fsh);
660 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700661 }
Florin Coras62ddc032019-12-08 18:30:42 -0800662 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700663 {
Florin Coras62ddc032019-12-08 18:30:42 -0800664 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000665 f = svm_fifo_alloc (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700666 ssvm_pop_heap (oldheap);
667 if (f)
668 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000669 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
Florin Coras62ddc032019-12-08 18:30:42 -0800670 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700671 goto done;
672 }
Florin Coras0269fa12020-03-10 04:46:32 +0000673 fsh_check_mem (fsh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700674 }
Florin Corasc75ce4d2020-02-28 21:51:24 +0000675 /* All failed, try to allocate min of data bytes and fifo sz */
676 fifo_sz = clib_min (fifo_sz, data_bytes);
677 if (fifo_sz <= fss->n_fl_chunk_bytes)
678 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700679
680done:
Florin Corasc75ce4d2020-02-28 21:51:24 +0000681 clib_spinlock_unlock (&fss->chunk_lock);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700682
Florin Corasf22f4e52019-12-19 16:10:58 -0800683 if (f)
Florin Corasc75ce4d2020-02-28 21:51:24 +0000684 {
685 f->size = data_bytes;
686 f->fs_hdr = fsh;
687 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700688 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700689}
690
Florin Corasf22f4e52019-12-19 16:10:58 -0800691svm_fifo_chunk_t *
692fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
693{
694 fifo_segment_slice_t *fss;
695 svm_fifo_chunk_t *c;
Florin Corasf22f4e52019-12-19 16:10:58 -0800696 int fl_index;
697
698 fl_index = fs_freelist_for_size (chunk_size);
699 fss = fsh_slice_get (fsh, slice_index);
700
701 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras8e755a12020-02-06 16:59:31 +0000702
Florin Coras0e6199d2020-04-17 20:15:22 +0000703 ASSERT (vec_len (fss->free_chunks) > fl_index);
Florin Corasf22f4e52019-12-19 16:10:58 -0800704 c = fss->free_chunks[fl_index];
705
Florin Coras8e755a12020-02-06 16:59:31 +0000706 if (c)
707 {
708 fss->free_chunks[fl_index] = c->next;
709 c->next = 0;
710 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
711 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
712 }
Florin Coras0269fa12020-03-10 04:46:32 +0000713 else
Florin Corasf22f4e52019-12-19 16:10:58 -0800714 {
Florin Coras0269fa12020-03-10 04:46:32 +0000715 void *oldheap;
716 uword n_free;
Florin Coras8e755a12020-02-06 16:59:31 +0000717 u32 batch;
718
Florin Coras0269fa12020-03-10 04:46:32 +0000719 chunk_size = fs_freelist_index_to_size (fl_index);
720 n_free = fsh_n_free_bytes (fsh);
721
722 if (chunk_size <= n_free)
723 {
724 oldheap = ssvm_push_heap (fsh->ssvm_sh);
725 c = svm_fifo_chunk_alloc (chunk_size);
726 ssvm_pop_heap (oldheap);
727
728 if (c)
729 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000730 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
Florin Coras0269fa12020-03-10 04:46:32 +0000731 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
732 goto done;
733 }
734
735 fsh_check_mem (fsh);
736 n_free = fsh_n_free_bytes (fsh);
737 }
738 if (chunk_size <= fss->n_fl_chunk_bytes)
739 {
740 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
741 if (c)
742 goto done;
743 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
744 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
745 {
746 fsh_check_mem (fsh);
747 goto done;
748 }
749 }
750 if (chunk_size <= fss->n_fl_chunk_bytes + n_free)
751 {
752 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
753
754 batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size;
755 batch = clib_min (batch + 1, n_free / min_size);
756 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
757 {
758 fsh_check_mem (fsh);
759 goto done;
760 }
761 c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size);
762 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800763 }
764
765done:
766
767 clib_spinlock_unlock (&fss->chunk_lock);
768
769 return c;
770}
771
772static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000773fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000774 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800775{
776 svm_fifo_chunk_t *next;
777 int fl_index;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000778 u32 n_collect = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800779
780 clib_spinlock_lock (&fss->chunk_lock);
781
Florin Coras9e61d9a2020-02-05 21:13:18 +0000782 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800783 {
Benoît Gannedf601ae2020-10-20 14:31:55 +0200784 CLIB_MEM_UNPOISON (c, sizeof (*c));
Florin Coras9e61d9a2020-02-05 21:13:18 +0000785 next = c->next;
786 fl_index = fs_freelist_for_size (c->length);
787 c->next = fss->free_chunks[fl_index];
788 c->enq_rb_index = RBTREE_TNIL_INDEX;
789 c->deq_rb_index = RBTREE_TNIL_INDEX;
790 fss->free_chunks[fl_index] = c;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000791 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000792 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800793 }
794
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000795 fss->n_fl_chunk_bytes += n_collect;
796 fsh_cached_bytes_add (fsh, n_collect);
797
Florin Corasf22f4e52019-12-19 16:10:58 -0800798 clib_spinlock_unlock (&fss->chunk_lock);
799}
800
801void
802fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000803 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800804{
805 fifo_segment_slice_t *fss;
806 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000807 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800808}
809
Florin Coras6d7552c2020-04-09 01:49:45 +0000810static inline void
811fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
812{
813 if (fss->fifos)
814 {
815 fss->fifos->prev = f;
816 f->next = fss->fifos;
817 }
818 fss->fifos = f;
819}
820
821static inline void
822fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
823{
824 if (f->flags & SVM_FIFO_F_LL_TRACKED)
825 {
826 if (f->prev)
827 f->prev->next = f->next;
828 else
829 fss->fifos = f->next;
830 if (f->next)
831 f->next->prev = f->prev;
832 }
833}
834
Florin Coras88001c62019-04-24 14:44:46 -0700835/**
836 * Allocate fifo in fifo segment
837 */
838svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800839fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
840 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700841{
Florin Coras62ddc032019-12-08 18:30:42 -0800842 fifo_segment_header_t *fsh = fs->h;
843 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700844 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700845
Florin Coras62ddc032019-12-08 18:30:42 -0800846 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700847
Florin Coras0e6199d2020-04-17 20:15:22 +0000848 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
849 return 0;
850
Florin Coras62ddc032019-12-08 18:30:42 -0800851 fss = fsh_slice_get (fsh, slice_index);
852 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700853 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700854 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700855
Florin Coras62ddc032019-12-08 18:30:42 -0800856 f->slice_index = slice_index;
857
Florin Corascefd5d82019-05-05 13:19:57 -0700858 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700859
Florin Coras88001c62019-04-24 14:44:46 -0700860 /* If rx fifo type add to active fifos list. When cleaning up segment,
861 * we need a list of active sessions that should be disconnected. Since
862 * both rx and tx fifos keep pointers to the session, it's enough to track
863 * only one. */
864 if (ftype == FIFO_SEGMENT_RX_FIFO)
865 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000866 fss_fifo_add_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700867 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800868
869 svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ );
Florin Coras88001c62019-04-24 14:44:46 -0700870 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800871 else
872 {
873 svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ );
874 }
875
Florin Coras62ddc032019-12-08 18:30:42 -0800876 fsh_active_fifos_update (fsh, 1);
Florin Coras06d47752020-03-06 02:23:58 +0000877 fss->virtual_mem += svm_fifo_size (f);
Florin Coras88001c62019-04-24 14:44:46 -0700878
879done:
Florin Coras88001c62019-04-24 14:44:46 -0700880 return (f);
881}
882
883/**
884 * Free fifo allocated in fifo segment
885 */
886void
Florin Corasb095a3c2019-04-25 12:58:46 -0700887fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700888{
Florin Coras62ddc032019-12-08 18:30:42 -0800889 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800890 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700891
892 ASSERT (f->refcnt > 0);
893
894 if (--f->refcnt > 0)
895 return;
896
Florin Coras62ddc032019-12-08 18:30:42 -0800897 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700898
899 /* Remove from active list. Only rx fifos are tracked */
900 if (f->flags & SVM_FIFO_F_LL_TRACKED)
901 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000902 fss_fifo_del_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700903 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
904 }
905
Florin Corascefd5d82019-05-05 13:19:57 -0700906 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000907 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700908
Florin Corasf22f4e52019-12-19 16:10:58 -0800909 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700910 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
911
Florin Corasb095a3c2019-04-25 12:58:46 -0700912 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800913 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700914 svm_fifo_free_ooo_data (f);
915
Florin Coras88001c62019-04-24 14:44:46 -0700916 if (CLIB_DEBUG)
917 {
918 f->master_session_index = ~0;
919 f->master_thread_index = ~0;
920 }
921
Florin Coras06d47752020-03-06 02:23:58 +0000922 fss->virtual_mem -= svm_fifo_size (f);
923
924 /* Add to free list */
925 f->next = fss->free_fifos;
926 f->prev = 0;
927 fss->free_fifos = f;
928
Florin Coras62ddc032019-12-08 18:30:42 -0800929 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700930}
931
Florin Coras6d7552c2020-04-09 01:49:45 +0000932void
933fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
934{
935 fifo_segment_slice_t *fss;
936 svm_fifo_chunk_t *c;
937 u32 fl_index;
938
939 ASSERT (f->refcnt == 1);
940
941 fss = fsh_slice_get (fs->h, f->slice_index);
942 fss->virtual_mem -= svm_fifo_size (f);
943 if (f->flags & SVM_FIFO_F_LL_TRACKED)
944 fss_fifo_del_active_list (fss, f);
945
946 c = f->start_chunk;
947 while (c)
948 {
949 fl_index = fs_freelist_for_size (c->length);
950 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
951 c = c->next;
952 }
953}
954
955void
956fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
957 u32 slice_index)
958{
959 fifo_segment_slice_t *fss;
960 svm_fifo_chunk_t *c;
961 u32 fl_index;
962
963 f->slice_index = slice_index;
964 fss = fsh_slice_get (fs->h, f->slice_index);
965 fss->virtual_mem += svm_fifo_size (f);
966 if (f->flags & SVM_FIFO_F_LL_TRACKED)
967 fss_fifo_add_active_list (fss, f);
968
969 c = f->start_chunk;
970 while (c)
971 {
972 fl_index = fs_freelist_for_size (c->length);
973 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
974 c = c->next;
975 }
976}
977
Florin Corasf9d4ab42019-05-11 16:55:53 -0700978int
Florin Coras62ddc032019-12-08 18:30:42 -0800979fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
980 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700981{
982 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800983 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700984 svm_fifo_t *f;
985 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800986 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700987 u8 *fmem;
988 int i;
989
Florin Coras62ddc032019-12-08 18:30:42 -0800990 fss = fsh_slice_get (fsh, slice_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -0800991 size = (uword) (sizeof (*f)) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700992
Florin Coras62ddc032019-12-08 18:30:42 -0800993 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700994 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
995 0 /* align_offset */ ,
996 0 /* os_out_of_memory */ );
997 ssvm_pop_heap (oldheap);
998
999 /* Out of space.. */
1000 if (fmem == 0)
1001 return -1;
1002
1003 /* Carve fifo + chunk space */
1004 for (i = 0; i < batch_size; i++)
1005 {
1006 f = (svm_fifo_t *) fmem;
1007 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -08001008 f->next = fss->free_fifos;
1009 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001010 fmem += sizeof (*f);
1011 }
1012
Florin Coras62ddc032019-12-08 18:30:42 -08001013 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001014
1015 return 0;
1016}
1017
1018int
Florin Coras62ddc032019-12-08 18:30:42 -08001019fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1020 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001021{
Florin Coras62ddc032019-12-08 18:30:42 -08001022 fifo_segment_header_t *fsh = fs->h;
Florin Coras36a0c4d2020-01-31 08:28:02 -08001023 u32 rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -08001024 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001025 svm_fifo_chunk_t *c;
1026 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -08001027 uword size;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001028 u8 *cmem;
1029 int i;
1030
Florin Coras62ddc032019-12-08 18:30:42 -08001031 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -07001032 {
1033 clib_warning ("chunk size out of range %d", chunk_size);
1034 return -1;
1035 }
1036
1037 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001038 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras36a0c4d2020-01-31 08:28:02 -08001039 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001040
Florin Coras62ddc032019-12-08 18:30:42 -08001041 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001042 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
1043 0 /* align_offset */ ,
1044 0 /* os_out_of_memory */ );
1045 ssvm_pop_heap (oldheap);
1046
1047 /* Out of space.. */
1048 if (cmem == 0)
1049 return -1;
1050
Florin Coras62ddc032019-12-08 18:30:42 -08001051 fss = fsh_slice_get (fsh, slice_index);
1052
Florin Corasf9d4ab42019-05-11 16:55:53 -07001053 /* Carve fifo + chunk space */
1054 for (i = 0; i < batch_size; i++)
1055 {
1056 c = (svm_fifo_chunk_t *) cmem;
1057 c->start_byte = 0;
1058 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -08001059 c->next = fss->free_chunks[fl_index];
1060 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001061 cmem += sizeof (*c) + rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001062 fsh_cached_bytes_add (fsh, rounded_data_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001063 }
1064
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001065 fss->num_chunks[fl_index] += batch_size;
Florin Coras62ddc032019-12-08 18:30:42 -08001066 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
1067 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001068
1069 return 0;
1070}
1071
Florin Coras88001c62019-04-24 14:44:46 -07001072/**
1073 * Pre-allocates fifo pairs in fifo segment
1074 */
1075void
1076fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1077 u32 rx_fifo_size, u32 tx_fifo_size,
1078 u32 * n_fifo_pairs)
1079{
Florin Corasf9d4ab42019-05-11 16:55:53 -07001080 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +00001081 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -08001082 fifo_segment_header_t *fsh = fs->h;
1083 int rx_fl_index, tx_fl_index, i;
1084 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -07001085 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -07001086
1087 /* Parameter check */
1088 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1089 return;
1090
Florin Coras62ddc032019-12-08 18:30:42 -08001091 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001092 {
1093 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1094 return;
1095 }
1096
Florin Coras62ddc032019-12-08 18:30:42 -08001097 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001098 {
1099 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1100 return;
1101 }
1102
1103 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001104 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001105 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001106 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001107
Florin Corasf9d4ab42019-05-11 16:55:53 -07001108 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -07001109
Florin Coras88001c62019-04-24 14:44:46 -07001110 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -07001111 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -08001112 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -07001113 pairs_to_alloc = space_available / pair_size;
1114 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -08001115 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +00001116 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001117
Florin Coras62ddc032019-12-08 18:30:42 -08001118 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -07001119 return;
Florin Coras88001c62019-04-24 14:44:46 -07001120
Florin Coras62ddc032019-12-08 18:30:42 -08001121 for (i = 0; i < fs->n_slices; i++)
1122 {
1123 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +00001124 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
1125 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1126 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1127 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1128 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -07001129
Florin Coras8d0149d2020-01-02 19:22:51 +00001130 /* Account for the pairs allocated */
1131 *n_fifo_pairs -= alloc_now;
1132 }
Florin Coras88001c62019-04-24 14:44:46 -07001133}
1134
1135/**
1136 * Get number of active fifos
1137 */
1138u32
1139fifo_segment_num_fifos (fifo_segment_t * fs)
1140{
Florin Coras7a90e502020-04-09 04:46:14 +00001141 return fsh_n_active_fifos (fs->h);
Florin Coras88001c62019-04-24 14:44:46 -07001142}
1143
Florin Coras62ddc032019-12-08 18:30:42 -08001144static u32
1145fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -07001146{
Florin Coras88001c62019-04-24 14:44:46 -07001147 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001148 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001149
Florin Coras62ddc032019-12-08 18:30:42 -08001150 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001151 if (f == 0)
1152 return 0;
1153
1154 while (f)
1155 {
1156 f = f->next;
1157 count++;
1158 }
1159 return count;
1160}
1161
Florin Corasb095a3c2019-04-25 12:58:46 -07001162u32
Florin Coras62ddc032019-12-08 18:30:42 -08001163fifo_segment_num_free_fifos (fifo_segment_t * fs)
1164{
1165 fifo_segment_header_t *fsh = fs->h;
1166 fifo_segment_slice_t *fss;
1167 int slice_index;
1168 u32 count = 0;
1169
1170 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1171 {
1172 fss = fsh_slice_get (fsh, slice_index);
1173 count += fs_slice_num_free_fifos (fss);
1174 }
1175 return count;
1176}
1177
1178static u32
1179fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001180{
1181 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001182 svm_fifo_chunk_t *c;
1183 int i;
1184
Florin Corasb095a3c2019-04-25 12:58:46 -07001185 /* Count all free chunks? */
1186 if (size == ~0)
1187 {
Florin Coras62ddc032019-12-08 18:30:42 -08001188 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001189 {
Florin Coras62ddc032019-12-08 18:30:42 -08001190 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001191 if (c == 0)
1192 continue;
1193
1194 while (c)
1195 {
1196 c = c->next;
1197 count++;
1198 }
1199 }
1200 return count;
1201 }
1202
1203 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001204 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001205
Florin Coras62ddc032019-12-08 18:30:42 -08001206 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001207 return 0;
1208
Florin Coras62ddc032019-12-08 18:30:42 -08001209 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001210 if (c == 0)
1211 return 0;
1212
1213 while (c)
1214 {
1215 c = c->next;
1216 count++;
1217 }
1218 return count;
1219}
1220
Florin Coras62ddc032019-12-08 18:30:42 -08001221u32
1222fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1223{
1224 fifo_segment_header_t *fsh = fs->h;
1225 fifo_segment_slice_t *fss;
1226 int slice_index;
1227 u32 count = 0;
1228
1229 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1230 {
1231 fss = fsh_slice_get (fsh, slice_index);
1232 count += fs_slice_num_free_chunks (fss, size);
1233 }
1234 return count;
1235}
1236
Florin Corasf9d4ab42019-05-11 16:55:53 -07001237void
1238fifo_segment_update_free_bytes (fifo_segment_t * fs)
1239{
Florin Coras4453c092019-12-19 10:13:15 -08001240 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001241}
1242
Florin Corasef4f3e72019-12-11 14:27:53 -08001243uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001244fifo_segment_size (fifo_segment_t * fs)
1245{
1246 return fs->ssvm.ssvm_size;
1247}
1248
1249u8
1250fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1251{
1252 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1253}
1254
1255void
1256fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1257{
1258 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1259}
1260
1261uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001262fifo_segment_free_bytes (fifo_segment_t * fs)
1263{
Florin Coras62ddc032019-12-08 18:30:42 -08001264 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001265}
1266
Florin Coras62ddc032019-12-08 18:30:42 -08001267uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001268fifo_segment_cached_bytes (fifo_segment_t * fs)
1269{
1270 return fsh_n_cached_bytes (fs->h);
1271}
1272
1273uword
Florin Coras75ccf7b2020-03-05 19:44:02 +00001274fifo_segment_available_bytes (fifo_segment_t * fs)
1275{
1276 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1277}
1278
1279uword
Florin Coras1c91c772019-06-25 18:14:13 -07001280fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001281{
Florin Coras62ddc032019-12-08 18:30:42 -08001282 fifo_segment_header_t *fsh = fs->h;
1283 fifo_segment_slice_t *fss;
1284 uword n_bytes = 0;
1285 int slice_index;
1286
1287 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1288 {
1289 fss = fsh_slice_get (fsh, slice_index);
1290 n_bytes += fss->n_fl_chunk_bytes;
1291 }
1292
1293 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001294}
1295
Florin Coras88001c62019-04-24 14:44:46 -07001296u8
1297fifo_segment_has_fifos (fifo_segment_t * fs)
1298{
Florin Coras7a90e502020-04-09 04:46:14 +00001299 return (fsh_n_active_fifos (fs->h) != 0);
Florin Coras88001c62019-04-24 14:44:46 -07001300}
1301
1302svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001303fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001304{
Florin Coras62ddc032019-12-08 18:30:42 -08001305 fifo_segment_header_t *fsh = fs->h;
1306 fifo_segment_slice_t *fss;
1307
1308 fss = fsh_slice_get (fsh, slice_index);
1309 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001310}
1311
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001312u8
1313fifo_segment_get_mem_usage (fifo_segment_t * fs)
1314{
1315 uword size, in_use;
1316
1317 size = fifo_segment_size (fs);
1318 in_use =
1319 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1320 return (in_use * 100) / size;
1321}
1322
1323fifo_segment_mem_status_t
1324fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1325{
1326 if (!fsh->high_watermark || !fsh->low_watermark)
1327 return MEMORY_PRESSURE_NO_PRESSURE;
1328
1329 /* once the no-memory is detected, the status continues
1330 * until memory usage gets below the high watermark
1331 */
1332 if (fsh_has_reached_mem_limit (fsh))
1333 {
1334 if (usage >= fsh->high_watermark)
1335 return MEMORY_PRESSURE_NO_MEMORY;
1336 else
1337 fsh_reset_mem_limit (fsh);
1338 }
1339
1340 if (usage >= fsh->high_watermark)
1341 return MEMORY_PRESSURE_HIGH_PRESSURE;
1342
1343 else if (usage >= fsh->low_watermark)
1344 return MEMORY_PRESSURE_LOW_PRESSURE;
1345
1346 return MEMORY_PRESSURE_NO_PRESSURE;
1347}
1348
1349fifo_segment_mem_status_t
1350fifo_segment_get_mem_status (fifo_segment_t * fs)
1351{
1352 fifo_segment_header_t *fsh = fs->h;
1353 u8 usage = fifo_segment_get_mem_usage (fs);
1354
1355 return fifo_segment_determine_status (fsh, usage);
1356}
1357
Florin Coras88001c62019-04-24 14:44:46 -07001358u8 *
1359format_fifo_segment_type (u8 * s, va_list * args)
1360{
1361 fifo_segment_t *sp;
1362 sp = va_arg (*args, fifo_segment_t *);
1363 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1364
1365 if (st == SSVM_SEGMENT_PRIVATE)
Florin Coras2a7c0b62020-09-28 23:40:28 -07001366 s = format (s, "%s", "private");
Florin Coras88001c62019-04-24 14:44:46 -07001367 else if (st == SSVM_SEGMENT_MEMFD)
1368 s = format (s, "%s", "memfd");
1369 else if (st == SSVM_SEGMENT_SHM)
1370 s = format (s, "%s", "shm");
1371 else
1372 s = format (s, "%s", "unknown");
1373 return s;
1374}
1375
1376/**
1377 * Segment format function
1378 */
1379u8 *
1380format_fifo_segment (u8 * s, va_list * args)
1381{
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001382 u32 count, indent, active_fifos, free_fifos;
Florin Coras5368bb02019-06-09 09:24:33 -07001383 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001384 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001385 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1386 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001387 uword tracked_cached_bytes;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001388 uword fifo_hdr = 0, reserved;
Florin Coras5368bb02019-06-09 09:24:33 -07001389 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001390 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001391 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001392 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001393 char *address;
1394 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001395 int i;
Florin Coras06d47752020-03-06 02:23:58 +00001396 uword allocated, in_use, virt;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001397 f64 usage;
1398 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001399
1400 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001401
Florin Coras5368bb02019-06-09 09:24:33 -07001402 if (fs == 0)
1403 {
Florin Coras2a7c0b62020-09-28 23:40:28 -07001404 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
Florin Coras5368bb02019-06-09 09:24:33 -07001405 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1406 return s;
1407 }
1408
Florin Coras5368bb02019-06-09 09:24:33 -07001409 fifo_segment_info (fs, &address, &size);
1410 active_fifos = fifo_segment_num_fifos (fs);
1411 free_fifos = fifo_segment_num_free_fifos (fs);
1412
Florin Coras2a7c0b62020-09-28 23:40:28 -07001413 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
Florin Coras5368bb02019-06-09 09:24:33 -07001414 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1415 free_fifos, address);
1416
1417 if (!verbose)
1418 return s;
1419
Florin Coras62ddc032019-12-08 18:30:42 -08001420 fsh = fs->h;
1421
1422 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1423 if (free_chunks)
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001424 s =
1425 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1426 indent + 2);
Florin Corasf8461bf2019-11-07 17:00:15 -08001427 else
1428 s = format (s, "\n");
1429
Florin Coras62ddc032019-12-08 18:30:42 -08001430 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001431 {
Florin Coras62ddc032019-12-08 18:30:42 -08001432 fss = fsh_slice_get (fsh, slice_index);
1433 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001434 {
Florin Coras62ddc032019-12-08 18:30:42 -08001435 c = fss->free_chunks[i];
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001436 if (c == 0 && fss->num_chunks[i] == 0)
Florin Coras62ddc032019-12-08 18:30:42 -08001437 continue;
1438 count = 0;
1439 while (c)
1440 {
1441 c = c->next;
1442 count++;
1443 }
1444
1445 chunk_size = fs_freelist_index_to_size (i);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001446 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1447 chunk_size >> 10, count, fss->num_chunks[i]);
Florin Coras62ddc032019-12-08 18:30:42 -08001448
1449 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001450 }
Florin Coras88001c62019-04-24 14:44:46 -07001451 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001452
1453 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1454 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001455 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001456 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001457 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001458 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1459 allocated = fifo_segment_size (fs);
1460 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1461 usage = (100.0 * in_use) / allocated;
1462 mem_st = fifo_segment_get_mem_status (fs);
Florin Coras06d47752020-03-06 02:23:58 +00001463 virt = fsh_virtual_mem (fsh);
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001464 reserved = fsh->n_reserved_bytes;
Florin Corasf8461bf2019-11-07 17:00:15 -08001465
Florin Coras06d47752020-03-06 02:23:58 +00001466 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1467 " %U (%lu)\n", format_white_space, indent + 2,
1468 format_memory_size, free_seg_bytes, free_seg_bytes,
1469 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001470 format_memory_size, reserved, reserved);
Florin Coras06d47752020-03-06 02:23:58 +00001471 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1472 " %U (%lu)\n", format_white_space, indent + 2,
1473 format_memory_size, chunk_bytes, chunk_bytes,
1474 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1475 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1476 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1477 format_white_space, indent + 2, fsh->n_active_fifos,
1478 format_memory_size, fifo_hdr, fifo_hdr);
1479 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1480 format_white_space, indent + 2, usage, format_memory_size,
1481 in_use, format_memory_size, allocated, format_memory_size, virt,
1482 fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001483 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001484
Florin Coras88001c62019-04-24 14:44:46 -07001485 return s;
1486}
1487
1488/*
1489 * fd.io coding-style-patch-verification: ON
1490 *
1491 * Local Variables:
1492 * eval: (c-set-style "gnu")
1493 * End:
1494 */