blob: 01fa65623c52b96ca4db4890cda21f4c456ac50b [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
18/**
Florin Corasf9d4ab42019-05-11 16:55:53 -070019 * Fifo segment free space
20 *
21 * Queries the underlying memory manager, dlmalloc, for free space. Since this
22 * ends up walking the internal data structures, it should not be called
23 * indiscriminately.
24 *
25 * @param fs fifo segment
26 * @return number of free bytes
27 */
Florin Corasef4f3e72019-12-11 14:27:53 -080028static uword
Florin Corasf9d4ab42019-05-11 16:55:53 -070029fs_free_space (fifo_segment_t * fs)
30{
31 struct dlmallinfo dlminfo;
32
33 dlminfo = mspace_mallinfo (fs->ssvm.sh->heap);
34 return dlminfo.fordblks;
35}
36
Florin Coras62ddc032019-12-08 18:30:42 -080037static inline void
38fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
39{
40 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
41}
42
43static inline uword
44fsh_n_free_bytes (fifo_segment_header_t * fsh)
45{
46 return clib_atomic_load_relax_n (&fsh->n_free_bytes);
47}
48
49static inline fifo_segment_slice_t *
50fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
51{
52 return &fsh->slices[slice_index];
53}
54
55static inline void
56fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
57{
58 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
59}
60
Florin Corasf9d4ab42019-05-11 16:55:53 -070061/**
Florin Coras88001c62019-04-24 14:44:46 -070062 * Initialize fifo segment shared header
63 */
64int
65fifo_segment_init (fifo_segment_t * fs)
66{
67 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -080068 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -070069 ssvm_shared_header_t *sh;
Florin Coras62ddc032019-12-08 18:30:42 -080070 u32 max_chunk_sz;
71 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -070072 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -080073 int i;
Florin Coras88001c62019-04-24 14:44:46 -070074
75 sh = fs->ssvm.sh;
76 oldheap = ssvm_push_heap (sh);
77
Florin Coras62ddc032019-12-08 18:30:42 -080078 /*
79 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
80 * Long story made short: the "process-private" fifo segment
81 * is allocated from the main heap, not mmapped. dlmalloc
82 * only guarantees 4-byte alignment, and on aarch64
83 * the fsh can end up 4-byte but not 8-byte aligned.
84 * That eventually causes the atomic op in fifo_segment_update_free_bytes
85 * to backfire.
86 */
87 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -070088 clib_memset (fsh, 0, sizeof (*fsh));
89 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -080090 fs->n_slices = clib_max (fs->n_slices, 1);
91
92 fsh->ssvm_sh = fs->ssvm.sh;
93 fsh->n_slices = fs->n_slices;
94 max_fifo = clib_min ((fs_free_space (fs) - 4096) / 2,
95 FIFO_SEGMENT_MAX_FIFO_SIZE);
96 fsh->max_log2_chunk_size = max_log2 (max_fifo);
97
98 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
99 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
100 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
101
102 for (i = 0; i < fs->n_slices; i++)
103 {
104 fss = fsh_slice_get (fsh, i);
105 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
106 }
Florin Coras88001c62019-04-24 14:44:46 -0700107
108 ssvm_pop_heap (oldheap);
109
Florin Corasf9d4ab42019-05-11 16:55:53 -0700110 fsh->n_free_bytes = fs_free_space (fs);
Florin Coras88001c62019-04-24 14:44:46 -0700111 sh->ready = 1;
112 return (0);
113}
114
115/**
Florin Coras88001c62019-04-24 14:44:46 -0700116 * Create a fifo segment and initialize as master
117 */
118int
119fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
120{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700121 fifo_segment_t *fs;
122 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700123 int rv;
124
Florin Coras88001c62019-04-24 14:44:46 -0700125 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700126 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700127
Florin Corasf9d4ab42019-05-11 16:55:53 -0700128 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
129 fs->ssvm.ssvm_size = a->segment_size;
130 fs->ssvm.i_am_master = 1;
131 fs->ssvm.my_pid = getpid ();
132 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
133 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700134
Florin Corasf9d4ab42019-05-11 16:55:53 -0700135 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700136 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700137 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700138 return (rv);
139 }
140
141 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700142 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700143
Florin Corasf9d4ab42019-05-11 16:55:53 -0700144 fifo_segment_init (fs);
145 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700146 return (0);
147}
148
149/**
150 * Attach as slave to a fifo segment
151 */
152int
153fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
154{
Florin Coras62ddc032019-12-08 18:30:42 -0800155 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700156 int rv;
157
Florin Coras62ddc032019-12-08 18:30:42 -0800158 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700159
Florin Coras62ddc032019-12-08 18:30:42 -0800160 fs->ssvm.ssvm_size = a->segment_size;
161 fs->ssvm.my_pid = getpid ();
162 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
163 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700164 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800165 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700166 else
Florin Coras62ddc032019-12-08 18:30:42 -0800167 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700168
Florin Coras62ddc032019-12-08 18:30:42 -0800169 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700170 {
Florin Coras62ddc032019-12-08 18:30:42 -0800171 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700172 return (rv);
173 }
174
175 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800176 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700177
Florin Coras62ddc032019-12-08 18:30:42 -0800178 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700179 return (0);
180}
181
182void
183fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
184{
185 ssvm_delete (&s->ssvm);
186 clib_memset (s, 0xfe, sizeof (*s));
187 pool_put (sm->segments, s);
188}
189
190u32
191fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
192{
193 return s - sm->segments;
194}
195
Florin Coras88001c62019-04-24 14:44:46 -0700196fifo_segment_t *
197fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
198{
199 return pool_elt_at_index (sm->segments, segment_index);
200}
201
202void
203fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
204{
Florin Coras404b8a32019-05-09 12:08:06 -0700205 *address = (char *) seg->ssvm.sh->ssvm_va;
206 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700207}
208
209void
210fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
211 u32 timeout_in_seconds)
212{
213 sm->next_baseva = baseva;
214 sm->timeout_in_seconds = timeout_in_seconds;
215}
216
Florin Corasf9d4ab42019-05-11 16:55:53 -0700217static inline u32
218fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700219{
Florin Coras62ddc032019-12-08 18:30:42 -0800220 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700221}
222
Florin Corasf9d4ab42019-05-11 16:55:53 -0700223static inline u32
224fs_freelist_index_to_size (u32 fl_index)
225{
Florin Coras62ddc032019-12-08 18:30:42 -0800226 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700227}
228
Florin Coras88001c62019-04-24 14:44:46 -0700229static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800230fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700231{
232 /*
233 * 4K minimum. It's not likely that anything good will happen
234 * with a smaller FIFO.
235 */
236 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Coras62ddc032019-12-08 18:30:42 -0800237 && size <= (1 << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700238}
239
Florin Corascefd5d82019-05-05 13:19:57 -0700240static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800241fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss,
242 u32 fl_index, u32 data_bytes)
Florin Corascefd5d82019-05-05 13:19:57 -0700243{
244 svm_fifo_chunk_t *c;
245 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700246
Florin Coras62ddc032019-12-08 18:30:42 -0800247 f = fss->free_fifos;
248 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700249
250 if (!f || !c)
251 return 0;
252
Florin Coras62ddc032019-12-08 18:30:42 -0800253 fss->free_fifos = f->next;
254 fss->free_chunks[fl_index] = c->next;
Florin Corascefd5d82019-05-05 13:19:57 -0700255 c->next = c;
256 c->start_byte = 0;
257 c->length = data_bytes;
258 memset (f, 0, sizeof (*f));
259 f->start_chunk = c;
260 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700261
Florin Coras62ddc032019-12-08 18:30:42 -0800262 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700263 return f;
264}
265
266static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800267fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
268 fifo_segment_slice_t * fss,
269 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700270{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700271 svm_fifo_chunk_t *c, *first = 0, *last = 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700272 u32 fl_index, fl_size, n_alloc = 0;
273 svm_fifo_t *f;
274
Florin Coras62ddc032019-12-08 18:30:42 -0800275 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700276 if (!f)
277 {
Florin Coras62ddc032019-12-08 18:30:42 -0800278 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700279 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
280 ssvm_pop_heap (oldheap);
281 if (!f)
282 return 0;
283 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800284 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700285 }
Florin Coras73cad332019-08-28 17:12:32 -0700286 else
287 {
Florin Coras62ddc032019-12-08 18:30:42 -0800288 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700289 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700290
291 fl_index = fs_freelist_for_size (data_bytes) - 1;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700292 fl_size = fs_freelist_index_to_size (fl_index);
293
294 while (data_bytes)
295 {
Florin Coras62ddc032019-12-08 18:30:42 -0800296 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700297 if (c)
298 {
Florin Coras62ddc032019-12-08 18:30:42 -0800299 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700300 if (!last)
301 last = c;
302 c->next = first;
303 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700304 n_alloc += fl_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700305 c->length = clib_min (fl_size, data_bytes);
306 data_bytes -= c->length;
307 }
308 else
309 {
310 ASSERT (fl_index > 0);
311 fl_index -= 1;
312 fl_size = fl_size >> 1;
313 }
314 }
315 f->start_chunk = first;
316 f->end_chunk = last;
317 last->next = first;
Florin Coras62ddc032019-12-08 18:30:42 -0800318 fss->n_fl_chunk_bytes -= n_alloc;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700319 return f;
320}
321
322static int
Florin Coras62ddc032019-12-08 18:30:42 -0800323fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
324 fifo_segment_slice_t * fss,
325 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700326{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700327 u32 size, hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700328 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700329 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700330 void *oldheap;
331 u8 *fmem;
332 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700333
Florin Corasf9d4ab42019-05-11 16:55:53 -0700334 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700335 hdrs = sizeof (*f) + sizeof (*c);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700336 size = (hdrs + rounded_data_size) * batch_size;
337
Florin Coras62ddc032019-12-08 18:30:42 -0800338 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700339 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
340 0 /* align_offset */ ,
341 0 /* os_out_of_memory */ );
342 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700343
344 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700345 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700346 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700347
Florin Corascefd5d82019-05-05 13:19:57 -0700348 /* Carve fifo + chunk space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700349 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700350 {
Florin Corascefd5d82019-05-05 13:19:57 -0700351 f = (svm_fifo_t *) fmem;
352 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800353 f->next = fss->free_fifos;
354 fss->free_fifos = f;
Florin Corascefd5d82019-05-05 13:19:57 -0700355 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
356 c->start_byte = 0;
357 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800358 c->next = fss->free_chunks[fl_index];
359 fss->free_chunks[fl_index] = c;
Florin Corascefd5d82019-05-05 13:19:57 -0700360 fmem += hdrs + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700361 }
Florin Corascefd5d82019-05-05 13:19:57 -0700362
Florin Coras62ddc032019-12-08 18:30:42 -0800363 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
364 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700365
366 return 0;
367}
368
369/**
370 * Try to allocate new fifo
371 *
372 * Tries the following steps in order:
373 * - grab fifo and chunk from freelists
374 * - batch fifo and chunk allocation
375 * - single fifo allocation
376 * - grab multiple fifo chunks from freelists
377 */
378static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800379fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
380 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700381{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700382 u32 fifo_sz, fl_index;
383 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800384 uword n_free_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700385
386 fl_index = fs_freelist_for_size (data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700387 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
388 fifo_sz += 1 << max_log2 (data_bytes);
389
Florin Coras62ddc032019-12-08 18:30:42 -0800390 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700391 {
Florin Coras62ddc032019-12-08 18:30:42 -0800392 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700393 if (f)
394 goto done;
395 }
Florin Coras62ddc032019-12-08 18:30:42 -0800396 n_free_bytes = fsh_n_free_bytes (fsh);
397 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700398 {
Florin Coras62ddc032019-12-08 18:30:42 -0800399 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
Florin Corasf9d4ab42019-05-11 16:55:53 -0700400 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
401 goto done;
402
Florin Coras62ddc032019-12-08 18:30:42 -0800403 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700404 goto done;
405 }
Florin Coras62ddc032019-12-08 18:30:42 -0800406 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700407 {
Florin Coras62ddc032019-12-08 18:30:42 -0800408 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700409 f = svm_fifo_create (data_bytes);
410 ssvm_pop_heap (oldheap);
411 if (f)
412 {
Florin Coras62ddc032019-12-08 18:30:42 -0800413 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700414 goto done;
415 }
416 }
Florin Coras62ddc032019-12-08 18:30:42 -0800417 if (data_bytes <= fss->n_fl_chunk_bytes)
418 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700419
420done:
421
422 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700423}
424
425/**
426 * Allocate fifo in fifo segment
427 */
428svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800429fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
430 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700431{
Florin Coras62ddc032019-12-08 18:30:42 -0800432 fifo_segment_header_t *fsh = fs->h;
433 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700434 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700435
Florin Coras62ddc032019-12-08 18:30:42 -0800436 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700437
Florin Coras62ddc032019-12-08 18:30:42 -0800438 fss = fsh_slice_get (fsh, slice_index);
439 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700440 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700441 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700442
Florin Coras62ddc032019-12-08 18:30:42 -0800443 f->slice_index = slice_index;
444
Florin Corascefd5d82019-05-05 13:19:57 -0700445 /* (re)initialize the fifo, as in svm_fifo_create */
446 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700447
Florin Coraseaacce42019-07-02 13:07:37 -0700448 /* Initialize chunks and rbtree for multi-chunk fifos */
449 if (f->start_chunk->next != f->start_chunk)
450 {
Florin Coras62ddc032019-12-08 18:30:42 -0800451 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coraseaacce42019-07-02 13:07:37 -0700452 svm_fifo_init_chunks (f);
453 ssvm_pop_heap (oldheap);
454 }
455
Florin Coras88001c62019-04-24 14:44:46 -0700456 /* If rx fifo type add to active fifos list. When cleaning up segment,
457 * we need a list of active sessions that should be disconnected. Since
458 * both rx and tx fifos keep pointers to the session, it's enough to track
459 * only one. */
460 if (ftype == FIFO_SEGMENT_RX_FIFO)
461 {
Florin Coras62ddc032019-12-08 18:30:42 -0800462 if (fss->fifos)
Florin Coras88001c62019-04-24 14:44:46 -0700463 {
Florin Coras62ddc032019-12-08 18:30:42 -0800464 fss->fifos->prev = f;
465 f->next = fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700466 }
Florin Coras62ddc032019-12-08 18:30:42 -0800467 fss->fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700468 f->flags |= SVM_FIFO_F_LL_TRACKED;
469 }
Florin Coras62ddc032019-12-08 18:30:42 -0800470 fsh_active_fifos_update (fsh, 1);
Florin Coras88001c62019-04-24 14:44:46 -0700471
472done:
Florin Coras88001c62019-04-24 14:44:46 -0700473 return (f);
474}
475
476/**
477 * Free fifo allocated in fifo segment
478 */
479void
Florin Corasb095a3c2019-04-25 12:58:46 -0700480fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700481{
Florin Coras62ddc032019-12-08 18:30:42 -0800482 fifo_segment_header_t *fsh = fs->h;
Florin Corascefd5d82019-05-05 13:19:57 -0700483 svm_fifo_chunk_t *cur, *next;
Florin Coras62ddc032019-12-08 18:30:42 -0800484 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -0700485 void *oldheap;
Florin Coras88001c62019-04-24 14:44:46 -0700486 int fl_index;
487
488 ASSERT (f->refcnt > 0);
489
490 if (--f->refcnt > 0)
491 return;
492
Florin Coras62ddc032019-12-08 18:30:42 -0800493 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700494
495 /* Remove from active list. Only rx fifos are tracked */
496 if (f->flags & SVM_FIFO_F_LL_TRACKED)
497 {
498 if (f->prev)
499 f->prev->next = f->next;
500 else
Florin Coras62ddc032019-12-08 18:30:42 -0800501 fss->fifos = f->next;
Florin Coras88001c62019-04-24 14:44:46 -0700502 if (f->next)
503 f->next->prev = f->prev;
504 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
505 }
506
507 /* Add to free list */
Florin Coras62ddc032019-12-08 18:30:42 -0800508 f->next = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700509 f->prev = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800510 fss->free_fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700511
Florin Corascefd5d82019-05-05 13:19:57 -0700512 /* Free fifo chunks */
513 cur = f->start_chunk;
514 do
Florin Corasb095a3c2019-04-25 12:58:46 -0700515 {
Florin Corascefd5d82019-05-05 13:19:57 -0700516 next = cur->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700517 fl_index = fs_freelist_for_size (cur->length);
Florin Coras62ddc032019-12-08 18:30:42 -0800518 ASSERT (fl_index < vec_len (fss->free_chunks));
519 cur->next = fss->free_chunks[fl_index];
520 fss->free_chunks[fl_index] = cur;
521 fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700522 cur = next;
Florin Corasb095a3c2019-04-25 12:58:46 -0700523 }
Florin Corascefd5d82019-05-05 13:19:57 -0700524 while (cur != f->start_chunk);
525
Florin Coras1c91c772019-06-25 18:14:13 -0700526 f->start_chunk = f->end_chunk = f->new_chunks = 0;
527 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
528
Florin Coras62ddc032019-12-08 18:30:42 -0800529 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700530 svm_fifo_free_chunk_lookup (f);
531 ssvm_pop_heap (oldheap);
Florin Corasb095a3c2019-04-25 12:58:46 -0700532
533 /* not allocated on segment heap */
534 svm_fifo_free_ooo_data (f);
535
Florin Coras88001c62019-04-24 14:44:46 -0700536 if (CLIB_DEBUG)
537 {
538 f->master_session_index = ~0;
539 f->master_thread_index = ~0;
540 }
541
Florin Coras62ddc032019-12-08 18:30:42 -0800542 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700543}
544
Florin Corasf9d4ab42019-05-11 16:55:53 -0700545int
Florin Coras62ddc032019-12-08 18:30:42 -0800546fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
547 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700548{
549 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800550 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700551 svm_fifo_t *f;
552 void *oldheap;
553 u32 size;
554 u8 *fmem;
555 int i;
556
Florin Coras62ddc032019-12-08 18:30:42 -0800557 fss = fsh_slice_get (fsh, slice_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700558 size = (sizeof (*f)) * batch_size;
559
Florin Coras62ddc032019-12-08 18:30:42 -0800560 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700561 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
562 0 /* align_offset */ ,
563 0 /* os_out_of_memory */ );
564 ssvm_pop_heap (oldheap);
565
566 /* Out of space.. */
567 if (fmem == 0)
568 return -1;
569
570 /* Carve fifo + chunk space */
571 for (i = 0; i < batch_size; i++)
572 {
573 f = (svm_fifo_t *) fmem;
574 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800575 f->next = fss->free_fifos;
576 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700577 fmem += sizeof (*f);
578 }
579
Florin Coras62ddc032019-12-08 18:30:42 -0800580 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700581
582 return 0;
583}
584
585int
Florin Coras62ddc032019-12-08 18:30:42 -0800586fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
587 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700588{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700589 u32 size, rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -0800590 fifo_segment_header_t *fsh = fs->h;
591 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700592 svm_fifo_chunk_t *c;
593 void *oldheap;
594 u8 *cmem;
595 int i;
596
Florin Coras62ddc032019-12-08 18:30:42 -0800597 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700598 {
599 clib_warning ("chunk size out of range %d", chunk_size);
600 return -1;
601 }
602
603 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700604 rounded_data_size = fs_freelist_index_to_size (fl_index);
605 size = (sizeof (*c) + rounded_data_size) * batch_size;
606
Florin Coras62ddc032019-12-08 18:30:42 -0800607 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700608 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
609 0 /* align_offset */ ,
610 0 /* os_out_of_memory */ );
611 ssvm_pop_heap (oldheap);
612
613 /* Out of space.. */
614 if (cmem == 0)
615 return -1;
616
Florin Coras62ddc032019-12-08 18:30:42 -0800617 fss = fsh_slice_get (fsh, slice_index);
618
Florin Corasf9d4ab42019-05-11 16:55:53 -0700619 /* Carve fifo + chunk space */
620 for (i = 0; i < batch_size; i++)
621 {
622 c = (svm_fifo_chunk_t *) cmem;
623 c->start_byte = 0;
624 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800625 c->next = fss->free_chunks[fl_index];
626 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700627 cmem += sizeof (*c) + rounded_data_size;
628 }
629
Florin Coras62ddc032019-12-08 18:30:42 -0800630 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
631 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700632
633 return 0;
634}
635
Florin Coras88001c62019-04-24 14:44:46 -0700636/**
637 * Pre-allocates fifo pairs in fifo segment
638 */
639void
640fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
641 u32 rx_fifo_size, u32 tx_fifo_size,
642 u32 * n_fifo_pairs)
643{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700644 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras62ddc032019-12-08 18:30:42 -0800645 fifo_segment_header_t *fsh = fs->h;
646 int rx_fl_index, tx_fl_index, i;
647 fifo_segment_slice_t *fss;
648 u32 hdrs, pairs_per_slice;
Florin Coras88001c62019-04-24 14:44:46 -0700649 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700650
651 /* Parameter check */
652 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
653 return;
654
Florin Coras62ddc032019-12-08 18:30:42 -0800655 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700656 {
657 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
658 return;
659 }
660
Florin Coras62ddc032019-12-08 18:30:42 -0800661 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700662 {
663 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
664 return;
665 }
666
667 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700668 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700669 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700670 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700671
Florin Corasf9d4ab42019-05-11 16:55:53 -0700672 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -0700673
Florin Coras88001c62019-04-24 14:44:46 -0700674 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -0700675 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700676 space_available = fs_free_space (fs);
Florin Coraseaacce42019-07-02 13:07:37 -0700677 pairs_to_alloc = space_available / pair_size;
678 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -0800679 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coraseaacce42019-07-02 13:07:37 -0700680
Florin Coras62ddc032019-12-08 18:30:42 -0800681 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -0700682 return;
Florin Coras88001c62019-04-24 14:44:46 -0700683
Florin Coras62ddc032019-12-08 18:30:42 -0800684 for (i = 0; i < fs->n_slices; i++)
685 {
686 fss = fsh_slice_get (fsh, i);
687 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, pairs_to_alloc))
688 clib_warning ("rx prealloc failed: pairs %u", pairs_to_alloc);
689 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, pairs_to_alloc))
690 clib_warning ("tx prealloc failed: pairs %u", pairs_to_alloc);
691 }
Florin Coras88001c62019-04-24 14:44:46 -0700692
693 /* Account for the pairs allocated */
Florin Coras62ddc032019-12-08 18:30:42 -0800694 *n_fifo_pairs -= pairs_per_slice * fs->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700695}
696
Florin Corasb095a3c2019-04-25 12:58:46 -0700697int
698fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
699{
Florin Coras62ddc032019-12-08 18:30:42 -0800700 fifo_segment_header_t *fsh = fs->h;
701 fifo_segment_slice_t *fss;
Florin Corasb095a3c2019-04-25 12:58:46 -0700702 svm_fifo_chunk_t *c;
703 void *oldheap;
704 int fl_index;
705
Florin Corasf9d4ab42019-05-11 16:55:53 -0700706 fl_index = fs_freelist_for_size (chunk_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800707 fss = fsh_slice_get (fsh, f->slice_index);
Florin Corasb095a3c2019-04-25 12:58:46 -0700708
Florin Coras62ddc032019-12-08 18:30:42 -0800709 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasb095a3c2019-04-25 12:58:46 -0700710
Florin Coras62ddc032019-12-08 18:30:42 -0800711 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -0700712
713 if (!c)
714 {
715 c = svm_fifo_chunk_alloc (chunk_size);
716 if (!c)
717 {
718 ssvm_pop_heap (oldheap);
719 return -1;
720 }
Florin Coras62ddc032019-12-08 18:30:42 -0800721 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
Florin Corasb095a3c2019-04-25 12:58:46 -0700722 }
723 else
724 {
Florin Coras62ddc032019-12-08 18:30:42 -0800725 fss->free_chunks[fl_index] = c->next;
Florin Corascefd5d82019-05-05 13:19:57 -0700726 c->next = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800727 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corasb095a3c2019-04-25 12:58:46 -0700728 }
729
730 svm_fifo_add_chunk (f, c);
731
732 ssvm_pop_heap (oldheap);
Florin Corasb095a3c2019-04-25 12:58:46 -0700733 return 0;
734}
735
Florin Coras344ce422019-05-03 11:46:55 -0700736int
737fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
738{
Florin Coras62ddc032019-12-08 18:30:42 -0800739 fifo_segment_header_t *fsh = fs->h;
Florin Coras344ce422019-05-03 11:46:55 -0700740 svm_fifo_chunk_t *cur, *next;
Florin Coras62ddc032019-12-08 18:30:42 -0800741 fifo_segment_slice_t *fss;
Florin Coras344ce422019-05-03 11:46:55 -0700742 void *oldheap;
743 int fl_index;
744
Florin Coras62ddc032019-12-08 18:30:42 -0800745 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras344ce422019-05-03 11:46:55 -0700746 cur = svm_fifo_collect_chunks (f);
747
Florin Coras62ddc032019-12-08 18:30:42 -0800748 fss = fsh_slice_get (fsh, f->slice_index);
749
Florin Coras344ce422019-05-03 11:46:55 -0700750 while (cur)
751 {
752 next = cur->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700753 fl_index = fs_freelist_for_size (cur->length);
Florin Coras62ddc032019-12-08 18:30:42 -0800754 cur->next = fss->free_chunks[fl_index];
755 fss->free_chunks[fl_index] = cur;
Florin Coras344ce422019-05-03 11:46:55 -0700756 cur = next;
757 }
758
759 ssvm_pop_heap (oldheap);
Florin Coras344ce422019-05-03 11:46:55 -0700760
761 return 0;
762}
763
Florin Coras88001c62019-04-24 14:44:46 -0700764/**
765 * Get number of active fifos
766 */
767u32
768fifo_segment_num_fifos (fifo_segment_t * fs)
769{
Florin Coras62ddc032019-12-08 18:30:42 -0800770 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
Florin Coras88001c62019-04-24 14:44:46 -0700771}
772
Florin Coras62ddc032019-12-08 18:30:42 -0800773static u32
774fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -0700775{
Florin Coras88001c62019-04-24 14:44:46 -0700776 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700777 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700778
Florin Coras62ddc032019-12-08 18:30:42 -0800779 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700780 if (f == 0)
781 return 0;
782
783 while (f)
784 {
785 f = f->next;
786 count++;
787 }
788 return count;
789}
790
Florin Corasb095a3c2019-04-25 12:58:46 -0700791u32
Florin Coras62ddc032019-12-08 18:30:42 -0800792fifo_segment_num_free_fifos (fifo_segment_t * fs)
793{
794 fifo_segment_header_t *fsh = fs->h;
795 fifo_segment_slice_t *fss;
796 int slice_index;
797 u32 count = 0;
798
799 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
800 {
801 fss = fsh_slice_get (fsh, slice_index);
802 count += fs_slice_num_free_fifos (fss);
803 }
804 return count;
805}
806
807static u32
808fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -0700809{
810 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -0700811 svm_fifo_chunk_t *c;
812 int i;
813
Florin Corasb095a3c2019-04-25 12:58:46 -0700814 /* Count all free chunks? */
815 if (size == ~0)
816 {
Florin Coras62ddc032019-12-08 18:30:42 -0800817 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -0700818 {
Florin Coras62ddc032019-12-08 18:30:42 -0800819 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -0700820 if (c == 0)
821 continue;
822
823 while (c)
824 {
825 c = c->next;
826 count++;
827 }
828 }
829 return count;
830 }
831
832 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700833 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -0700834
Florin Coras62ddc032019-12-08 18:30:42 -0800835 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -0700836 return 0;
837
Florin Coras62ddc032019-12-08 18:30:42 -0800838 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -0700839 if (c == 0)
840 return 0;
841
842 while (c)
843 {
844 c = c->next;
845 count++;
846 }
847 return count;
848}
849
Florin Coras62ddc032019-12-08 18:30:42 -0800850u32
851fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
852{
853 fifo_segment_header_t *fsh = fs->h;
854 fifo_segment_slice_t *fss;
855 int slice_index;
856 u32 count = 0;
857
858 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
859 {
860 fss = fsh_slice_get (fsh, slice_index);
861 count += fs_slice_num_free_chunks (fss, size);
862 }
863 return count;
864}
865
Florin Corasf9d4ab42019-05-11 16:55:53 -0700866void
867fifo_segment_update_free_bytes (fifo_segment_t * fs)
868{
Florin Coras62ddc032019-12-08 18:30:42 -0800869 fifo_segment_header_t *fsh = fs->h;
870 clib_atomic_store_rel_n (&fsh->n_free_bytes, fs_free_space (fs));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700871}
872
Florin Corasef4f3e72019-12-11 14:27:53 -0800873uword
Florin Corasf9d4ab42019-05-11 16:55:53 -0700874fifo_segment_free_bytes (fifo_segment_t * fs)
875{
Florin Coras62ddc032019-12-08 18:30:42 -0800876 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700877}
878
Florin Coras62ddc032019-12-08 18:30:42 -0800879uword
Florin Coras1c91c772019-06-25 18:14:13 -0700880fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700881{
Florin Coras62ddc032019-12-08 18:30:42 -0800882 fifo_segment_header_t *fsh = fs->h;
883 fifo_segment_slice_t *fss;
884 uword n_bytes = 0;
885 int slice_index;
886
887 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
888 {
889 fss = fsh_slice_get (fsh, slice_index);
890 n_bytes += fss->n_fl_chunk_bytes;
891 }
892
893 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700894}
895
Florin Coras88001c62019-04-24 14:44:46 -0700896u8
897fifo_segment_has_fifos (fifo_segment_t * fs)
898{
Florin Coras62ddc032019-12-08 18:30:42 -0800899 fifo_segment_header_t *fsh = fs->h;
900 fifo_segment_slice_t *fss;
901 int slice_index;
902
903 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
904 {
905 fss = fsh_slice_get (fsh, slice_index);
906 if (fss->fifos)
907 return 1;
908 }
909 return 0;
Florin Coras88001c62019-04-24 14:44:46 -0700910}
911
912svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800913fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -0700914{
Florin Coras62ddc032019-12-08 18:30:42 -0800915 fifo_segment_header_t *fsh = fs->h;
916 fifo_segment_slice_t *fss;
917
918 fss = fsh_slice_get (fsh, slice_index);
919 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700920}
921
922u8 *
923format_fifo_segment_type (u8 * s, va_list * args)
924{
925 fifo_segment_t *sp;
926 sp = va_arg (*args, fifo_segment_t *);
927 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
928
929 if (st == SSVM_SEGMENT_PRIVATE)
930 s = format (s, "%s", "private-heap");
931 else if (st == SSVM_SEGMENT_MEMFD)
932 s = format (s, "%s", "memfd");
933 else if (st == SSVM_SEGMENT_SHM)
934 s = format (s, "%s", "shm");
935 else
936 s = format (s, "%s", "unknown");
937 return s;
938}
939
940/**
941 * Segment format function
942 */
943u8 *
944format_fifo_segment (u8 * s, va_list * args)
945{
Florin Corasef4f3e72019-12-11 14:27:53 -0800946 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
Florin Coras5368bb02019-06-09 09:24:33 -0700947 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -0700948 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -0800949 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
950 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Florin Coras5368bb02019-06-09 09:24:33 -0700951 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800952 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -0700953 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -0800954 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -0700955 char *address;
956 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -0700957 int i;
958
959 indent = format_get_indent (s) + 2;
960#if USE_DLMALLOC == 0
961 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
Florin Coras62ddc032019-12-08 18:30:42 -0800962 format_mheap, fsh->ssvm_sh->heap, verbose);
Florin Coras88001c62019-04-24 14:44:46 -0700963 s = format (s, "%U segment has %u active fifos\n",
Florin Coras62ddc032019-12-08 18:30:42 -0800964 format_white_space, indent, fifo_segment_num_fifos (fsh));
Florin Coras88001c62019-04-24 14:44:46 -0700965#endif
966
Florin Coras5368bb02019-06-09 09:24:33 -0700967 if (fs == 0)
968 {
969 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
970 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
971 return s;
972 }
973
Florin Coras5368bb02019-06-09 09:24:33 -0700974 fifo_segment_info (fs, &address, &size);
975 active_fifos = fifo_segment_num_fifos (fs);
976 free_fifos = fifo_segment_num_free_fifos (fs);
977
978 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
979 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
980 free_fifos, address);
981
982 if (!verbose)
983 return s;
984
Florin Coras62ddc032019-12-08 18:30:42 -0800985 fsh = fs->h;
986
987 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
988 if (free_chunks)
Florin Corasf8461bf2019-11-07 17:00:15 -0800989 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
990 indent + 2);
991 else
992 s = format (s, "\n");
993
Florin Coras62ddc032019-12-08 18:30:42 -0800994 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700995 {
Florin Coras62ddc032019-12-08 18:30:42 -0800996 fss = fsh_slice_get (fsh, slice_index);
997 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -0700998 {
Florin Coras62ddc032019-12-08 18:30:42 -0800999 c = fss->free_chunks[i];
1000 if (c == 0)
1001 continue;
1002 count = 0;
1003 while (c)
1004 {
1005 c = c->next;
1006 count++;
1007 }
1008
1009 chunk_size = fs_freelist_index_to_size (i);
1010 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1011 chunk_size >> 10, count);
1012
1013 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001014 }
Florin Coras88001c62019-04-24 14:44:46 -07001015 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001016
1017 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1018 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001019 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001020 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001021 free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001022
Florin Corasef4f3e72019-12-11 14:27:53 -08001023 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001024 format_white_space, indent + 2, format_memory_size,
Florin Coras62ddc032019-12-08 18:30:42 -08001025 free_seg_bytes, free_seg_bytes, format_memory_size,
Florin Corasf8461bf2019-11-07 17:00:15 -08001026 est_free_seg_bytes, est_free_seg_bytes);
Florin Corasef4f3e72019-12-11 14:27:53 -08001027 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001028 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1029 chunk_bytes, format_memory_size, est_chunk_bytes,
1030 est_chunk_bytes);
1031 s = format (s, "%Ufifo hdr free bytes: %U (%u)\n", format_white_space,
1032 indent + 2, format_memory_size, fifo_hdr, fifo_hdr);
1033 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001034
Florin Coras88001c62019-04-24 14:44:46 -07001035 return s;
1036}
1037
1038/*
1039 * fd.io coding-style-patch-verification: ON
1040 *
1041 * Local Variables:
1042 * eval: (c-set-style "gnu")
1043 * End:
1044 */