blob: 1d8d073a65b32c7d50f724fb2064978145b7e54c [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 Coras4453c092019-12-19 10:13:15 -080029fsh_free_space (fifo_segment_header_t * fsh)
Florin Corasf9d4ab42019-05-11 16:55:53 -070030{
31 struct dlmallinfo dlminfo;
32
Florin Coras4453c092019-12-19 10:13:15 -080033 dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070034 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{
Florin Coras8122cc22019-12-18 13:06:41 -080046 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
47 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
48}
49
50static inline void
Florin Coras4453c092019-12-19 10:13:15 -080051fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080052{
Florin Coras4453c092019-12-19 10:13:15 -080053 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080054}
55
56static void
57fsh_check_mem (fifo_segment_header_t * fsh)
58{
59 uword thresh;
60
61 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
62 return;
63
64 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
65 2 * fsh->n_reserved_bytes);
66 if (fsh->n_free_bytes > thresh)
67 return;
68
69 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -080070 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -080071}
72
73static inline fifo_segment_slice_t *
74fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
75{
76 return &fsh->slices[slice_index];
77}
78
79static inline void
80fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
81{
82 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
83}
84
Florin Corasf9d4ab42019-05-11 16:55:53 -070085/**
Florin Coras88001c62019-04-24 14:44:46 -070086 * Initialize fifo segment shared header
87 */
88int
89fifo_segment_init (fifo_segment_t * fs)
90{
91 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -080092 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -070093 ssvm_shared_header_t *sh;
Florin Coras8122cc22019-12-18 13:06:41 -080094 u32 max_chunk_sz, max_chunks;
Florin Coras62ddc032019-12-08 18:30:42 -080095 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -070096 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -080097 int i;
Florin Coras88001c62019-04-24 14:44:46 -070098
99 sh = fs->ssvm.sh;
100 oldheap = ssvm_push_heap (sh);
101
Florin Coras62ddc032019-12-08 18:30:42 -0800102 /*
103 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
104 * Long story made short: the "process-private" fifo segment
105 * is allocated from the main heap, not mmapped. dlmalloc
106 * only guarantees 4-byte alignment, and on aarch64
107 * the fsh can end up 4-byte but not 8-byte aligned.
108 * That eventually causes the atomic op in fifo_segment_update_free_bytes
109 * to backfire.
110 */
111 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700112 clib_memset (fsh, 0, sizeof (*fsh));
113 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800114 fs->n_slices = clib_max (fs->n_slices, 1);
115
116 fsh->ssvm_sh = fs->ssvm.sh;
117 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800118 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800119 FIFO_SEGMENT_MAX_FIFO_SIZE);
120 fsh->max_log2_chunk_size = max_log2 (max_fifo);
121
122 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
123 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
124 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
125
126 for (i = 0; i < fs->n_slices; i++)
127 {
128 fss = fsh_slice_get (fsh, i);
129 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
130 }
Florin Coras88001c62019-04-24 14:44:46 -0700131
132 ssvm_pop_heap (oldheap);
133
Florin Coras4453c092019-12-19 10:13:15 -0800134 fsh->n_free_bytes = fsh_free_space (fsh);
Florin Coras8122cc22019-12-18 13:06:41 -0800135 max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE;
136 fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t);
Florin Coras88001c62019-04-24 14:44:46 -0700137 sh->ready = 1;
138 return (0);
139}
140
141/**
Florin Coras88001c62019-04-24 14:44:46 -0700142 * Create a fifo segment and initialize as master
143 */
144int
145fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
146{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700147 fifo_segment_t *fs;
148 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700149 int rv;
150
Florin Coras88001c62019-04-24 14:44:46 -0700151 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700152 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700153
Florin Corasf9d4ab42019-05-11 16:55:53 -0700154 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
155 fs->ssvm.ssvm_size = a->segment_size;
156 fs->ssvm.i_am_master = 1;
157 fs->ssvm.my_pid = getpid ();
158 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
159 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700160
Florin Corasf9d4ab42019-05-11 16:55:53 -0700161 if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700162 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700163 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700164 return (rv);
165 }
166
167 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700168 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700169
Florin Corasf9d4ab42019-05-11 16:55:53 -0700170 fifo_segment_init (fs);
171 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700172 return (0);
173}
174
175/**
176 * Attach as slave to a fifo segment
177 */
178int
179fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
180{
Florin Coras62ddc032019-12-08 18:30:42 -0800181 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700182 int rv;
183
Florin Coras62ddc032019-12-08 18:30:42 -0800184 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700185
Florin Coras62ddc032019-12-08 18:30:42 -0800186 fs->ssvm.ssvm_size = a->segment_size;
187 fs->ssvm.my_pid = getpid ();
188 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
189 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700190 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800191 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700192 else
Florin Coras62ddc032019-12-08 18:30:42 -0800193 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700194
Florin Coras62ddc032019-12-08 18:30:42 -0800195 if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700196 {
Florin Coras62ddc032019-12-08 18:30:42 -0800197 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700198 return (rv);
199 }
200
201 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800202 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700203
Florin Coras62ddc032019-12-08 18:30:42 -0800204 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700205 return (0);
206}
207
208void
209fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
210{
211 ssvm_delete (&s->ssvm);
212 clib_memset (s, 0xfe, sizeof (*s));
213 pool_put (sm->segments, s);
214}
215
216u32
217fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
218{
219 return s - sm->segments;
220}
221
Florin Coras88001c62019-04-24 14:44:46 -0700222fifo_segment_t *
223fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
224{
225 return pool_elt_at_index (sm->segments, segment_index);
226}
227
228void
229fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
230{
Florin Coras404b8a32019-05-09 12:08:06 -0700231 *address = (char *) seg->ssvm.sh->ssvm_va;
232 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700233}
234
235void
236fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
237 u32 timeout_in_seconds)
238{
239 sm->next_baseva = baseva;
240 sm->timeout_in_seconds = timeout_in_seconds;
241}
242
Florin Corasf9d4ab42019-05-11 16:55:53 -0700243static inline u32
244fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700245{
Florin Coras62ddc032019-12-08 18:30:42 -0800246 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700247}
248
Florin Corasf9d4ab42019-05-11 16:55:53 -0700249static inline u32
250fs_freelist_index_to_size (u32 fl_index)
251{
Florin Coras62ddc032019-12-08 18:30:42 -0800252 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700253}
254
Florin Coras88001c62019-04-24 14:44:46 -0700255static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800256fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700257{
258 /*
259 * 4K minimum. It's not likely that anything good will happen
260 * with a smaller FIFO.
261 */
262 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Coras62ddc032019-12-08 18:30:42 -0800263 && size <= (1 << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700264}
265
Florin Corascefd5d82019-05-05 13:19:57 -0700266static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800267fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss,
268 u32 fl_index, u32 data_bytes)
Florin Corascefd5d82019-05-05 13:19:57 -0700269{
270 svm_fifo_chunk_t *c;
271 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700272
Florin Coras62ddc032019-12-08 18:30:42 -0800273 f = fss->free_fifos;
274 c = fss->free_chunks[fl_index];
Florin Corascefd5d82019-05-05 13:19:57 -0700275
276 if (!f || !c)
277 return 0;
278
Florin Coras62ddc032019-12-08 18:30:42 -0800279 fss->free_fifos = f->next;
280 fss->free_chunks[fl_index] = c->next;
Florin Corascefd5d82019-05-05 13:19:57 -0700281 c->next = c;
282 c->start_byte = 0;
283 c->length = data_bytes;
284 memset (f, 0, sizeof (*f));
285 f->start_chunk = c;
286 f->end_chunk = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700287
Florin Coras62ddc032019-12-08 18:30:42 -0800288 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700289 return f;
290}
291
292static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800293fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh,
294 fifo_segment_slice_t * fss,
295 u32 data_bytes)
Florin Coras88001c62019-04-24 14:44:46 -0700296{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700297 svm_fifo_chunk_t *c, *first = 0, *last = 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700298 u32 fl_index, fl_size, n_alloc = 0;
299 svm_fifo_t *f;
300
Florin Coras62ddc032019-12-08 18:30:42 -0800301 f = fss->free_fifos;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700302 if (!f)
303 {
Florin Coras62ddc032019-12-08 18:30:42 -0800304 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700305 f = clib_mem_alloc_aligned (sizeof (*f), CLIB_CACHE_LINE_BYTES);
306 ssvm_pop_heap (oldheap);
307 if (!f)
308 return 0;
309 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800310 fsh_free_bytes_sub (fsh, sizeof (*f));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700311 }
Florin Coras73cad332019-08-28 17:12:32 -0700312 else
313 {
Florin Coras62ddc032019-12-08 18:30:42 -0800314 fss->free_fifos = f->next;
Florin Coras73cad332019-08-28 17:12:32 -0700315 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700316
Florin Corasd849edf2019-12-20 18:48:20 -0800317 fl_index = fs_freelist_for_size (data_bytes);
318 if (fl_index > 0)
319 fl_index -= 1;
320
Florin Corasf9d4ab42019-05-11 16:55:53 -0700321 fl_size = fs_freelist_index_to_size (fl_index);
322
323 while (data_bytes)
324 {
Florin Coras62ddc032019-12-08 18:30:42 -0800325 c = fss->free_chunks[fl_index];
Florin Corasf9d4ab42019-05-11 16:55:53 -0700326 if (c)
327 {
Florin Coras62ddc032019-12-08 18:30:42 -0800328 fss->free_chunks[fl_index] = c->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700329 if (!last)
330 last = c;
331 c->next = first;
332 first = c;
Florin Coras1c91c772019-06-25 18:14:13 -0700333 n_alloc += fl_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700334 c->length = clib_min (fl_size, data_bytes);
335 data_bytes -= c->length;
336 }
337 else
338 {
Florin Corasd849edf2019-12-20 18:48:20 -0800339 /* Failed to allocate with smaller chunks */
340 if (fl_index == 0)
341 {
342 /* free all chunks if any allocated */
343 c = first;
344 while (c)
345 {
346 fl_index = fs_freelist_for_size (c->length);
347 fl_size = fs_freelist_index_to_size (fl_index);
348 c->next = fss->free_chunks[fl_index];
349 fss->free_chunks[fl_index] = c;
350 fss->n_fl_chunk_bytes += fl_size;
351 data_bytes += fl_size;
352 }
353 first = last = 0;
354 fl_index = fs_freelist_for_size (data_bytes);
355 if (fss->free_chunks[fl_index + 1])
356 {
357 fl_index += 1;
358 fl_size = fs_freelist_index_to_size (fl_index);
359 continue;
360 }
361
362 f->next = fss->free_fifos;
363 fss->free_fifos = f;
364 return 0;
365 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700366 fl_index -= 1;
367 fl_size = fl_size >> 1;
368 }
369 }
Florin Corasd849edf2019-12-20 18:48:20 -0800370
Florin Corasf9d4ab42019-05-11 16:55:53 -0700371 f->start_chunk = first;
372 f->end_chunk = last;
373 last->next = first;
Florin Coras62ddc032019-12-08 18:30:42 -0800374 fss->n_fl_chunk_bytes -= n_alloc;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700375 return f;
376}
377
378static int
Florin Coras62ddc032019-12-08 18:30:42 -0800379fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
380 fifo_segment_slice_t * fss,
381 u32 fl_index, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700382{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700383 u32 size, hdrs, rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700384 svm_fifo_chunk_t *c;
Florin Coras88001c62019-04-24 14:44:46 -0700385 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700386 void *oldheap;
387 u8 *fmem;
388 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700389
Florin Corasf9d4ab42019-05-11 16:55:53 -0700390 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700391 hdrs = sizeof (*f) + sizeof (*c);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700392 size = (hdrs + rounded_data_size) * batch_size;
393
Florin Coras62ddc032019-12-08 18:30:42 -0800394 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700395 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
396 0 /* align_offset */ ,
397 0 /* os_out_of_memory */ );
398 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700399
400 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700401 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700402 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700403
Florin Corascefd5d82019-05-05 13:19:57 -0700404 /* Carve fifo + chunk space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700405 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700406 {
Florin Corascefd5d82019-05-05 13:19:57 -0700407 f = (svm_fifo_t *) fmem;
408 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800409 f->next = fss->free_fifos;
410 fss->free_fifos = f;
Florin Corascefd5d82019-05-05 13:19:57 -0700411 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
412 c->start_byte = 0;
413 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800414 c->next = fss->free_chunks[fl_index];
415 fss->free_chunks[fl_index] = c;
Florin Corascefd5d82019-05-05 13:19:57 -0700416 fmem += hdrs + rounded_data_size;
Florin Coras88001c62019-04-24 14:44:46 -0700417 }
Florin Corascefd5d82019-05-05 13:19:57 -0700418
Florin Coras62ddc032019-12-08 18:30:42 -0800419 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
420 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700421
422 return 0;
423}
424
425/**
426 * Try to allocate new fifo
427 *
428 * Tries the following steps in order:
429 * - grab fifo and chunk from freelists
430 * - batch fifo and chunk allocation
431 * - single fifo allocation
432 * - grab multiple fifo chunks from freelists
433 */
434static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800435fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
436 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700437{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700438 u32 fifo_sz, fl_index;
439 svm_fifo_t *f = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800440 uword n_free_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700441
442 fl_index = fs_freelist_for_size (data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700443 fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
444 fifo_sz += 1 << max_log2 (data_bytes);
445
Florin Coras62ddc032019-12-08 18:30:42 -0800446 if (fss->free_fifos && fss->free_chunks[fl_index])
Florin Corasf9d4ab42019-05-11 16:55:53 -0700447 {
Florin Coras62ddc032019-12-08 18:30:42 -0800448 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700449 if (f)
450 goto done;
451 }
Florin Coras8122cc22019-12-18 13:06:41 -0800452
453 fsh_check_mem (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800454 n_free_bytes = fsh_n_free_bytes (fsh);
455 if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700456 {
Florin Coras62ddc032019-12-08 18:30:42 -0800457 if (fs_try_alloc_fifo_batch (fsh, fss, fl_index,
Florin Corasf9d4ab42019-05-11 16:55:53 -0700458 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
459 goto done;
460
Florin Coras62ddc032019-12-08 18:30:42 -0800461 f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700462 goto done;
463 }
Florin Coras62ddc032019-12-08 18:30:42 -0800464 if (fifo_sz <= n_free_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700465 {
Florin Coras62ddc032019-12-08 18:30:42 -0800466 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700467 f = svm_fifo_create (data_bytes);
468 ssvm_pop_heap (oldheap);
469 if (f)
470 {
Florin Coras62ddc032019-12-08 18:30:42 -0800471 fsh_free_bytes_sub (fsh, fifo_sz);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700472 goto done;
473 }
474 }
Florin Coras62ddc032019-12-08 18:30:42 -0800475 if (data_bytes <= fss->n_fl_chunk_bytes)
476 f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700477
478done:
479
480 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700481}
482
483/**
484 * Allocate fifo in fifo segment
485 */
486svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800487fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
488 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700489{
Florin Coras62ddc032019-12-08 18:30:42 -0800490 fifo_segment_header_t *fsh = fs->h;
491 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700492 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700493
Florin Coras62ddc032019-12-08 18:30:42 -0800494 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700495
Florin Coras62ddc032019-12-08 18:30:42 -0800496 fss = fsh_slice_get (fsh, slice_index);
497 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700498 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700499 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700500
Florin Coras62ddc032019-12-08 18:30:42 -0800501 f->slice_index = slice_index;
502
Florin Corascefd5d82019-05-05 13:19:57 -0700503 /* (re)initialize the fifo, as in svm_fifo_create */
504 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700505
Florin Coraseaacce42019-07-02 13:07:37 -0700506 /* Initialize chunks and rbtree for multi-chunk fifos */
507 if (f->start_chunk->next != f->start_chunk)
508 {
Florin Coras62ddc032019-12-08 18:30:42 -0800509 void *oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coraseaacce42019-07-02 13:07:37 -0700510 svm_fifo_init_chunks (f);
511 ssvm_pop_heap (oldheap);
512 }
513
Florin Coras88001c62019-04-24 14:44:46 -0700514 /* If rx fifo type add to active fifos list. When cleaning up segment,
515 * we need a list of active sessions that should be disconnected. Since
516 * both rx and tx fifos keep pointers to the session, it's enough to track
517 * only one. */
518 if (ftype == FIFO_SEGMENT_RX_FIFO)
519 {
Florin Coras62ddc032019-12-08 18:30:42 -0800520 if (fss->fifos)
Florin Coras88001c62019-04-24 14:44:46 -0700521 {
Florin Coras62ddc032019-12-08 18:30:42 -0800522 fss->fifos->prev = f;
523 f->next = fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700524 }
Florin Coras62ddc032019-12-08 18:30:42 -0800525 fss->fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700526 f->flags |= SVM_FIFO_F_LL_TRACKED;
527 }
Florin Coras62ddc032019-12-08 18:30:42 -0800528 fsh_active_fifos_update (fsh, 1);
Florin Coras88001c62019-04-24 14:44:46 -0700529
530done:
Florin Coras88001c62019-04-24 14:44:46 -0700531 return (f);
532}
533
534/**
535 * Free fifo allocated in fifo segment
536 */
537void
Florin Corasb095a3c2019-04-25 12:58:46 -0700538fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700539{
Florin Coras62ddc032019-12-08 18:30:42 -0800540 fifo_segment_header_t *fsh = fs->h;
Florin Corascefd5d82019-05-05 13:19:57 -0700541 svm_fifo_chunk_t *cur, *next;
Florin Coras62ddc032019-12-08 18:30:42 -0800542 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -0700543 void *oldheap;
Florin Coras88001c62019-04-24 14:44:46 -0700544 int fl_index;
545
546 ASSERT (f->refcnt > 0);
547
548 if (--f->refcnt > 0)
549 return;
550
Florin Coras62ddc032019-12-08 18:30:42 -0800551 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700552
553 /* Remove from active list. Only rx fifos are tracked */
554 if (f->flags & SVM_FIFO_F_LL_TRACKED)
555 {
556 if (f->prev)
557 f->prev->next = f->next;
558 else
Florin Coras62ddc032019-12-08 18:30:42 -0800559 fss->fifos = f->next;
Florin Coras88001c62019-04-24 14:44:46 -0700560 if (f->next)
561 f->next->prev = f->prev;
562 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
563 }
564
565 /* Add to free list */
Florin Coras62ddc032019-12-08 18:30:42 -0800566 f->next = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700567 f->prev = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800568 fss->free_fifos = f;
Florin Coras88001c62019-04-24 14:44:46 -0700569
Florin Corascefd5d82019-05-05 13:19:57 -0700570 /* Free fifo chunks */
571 cur = f->start_chunk;
572 do
Florin Corasb095a3c2019-04-25 12:58:46 -0700573 {
Florin Corascefd5d82019-05-05 13:19:57 -0700574 next = cur->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700575 fl_index = fs_freelist_for_size (cur->length);
Florin Coras62ddc032019-12-08 18:30:42 -0800576 ASSERT (fl_index < vec_len (fss->free_chunks));
577 cur->next = fss->free_chunks[fl_index];
578 fss->free_chunks[fl_index] = cur;
579 fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index);
Florin Corascefd5d82019-05-05 13:19:57 -0700580 cur = next;
Florin Corasb095a3c2019-04-25 12:58:46 -0700581 }
Florin Corascefd5d82019-05-05 13:19:57 -0700582 while (cur != f->start_chunk);
583
Florin Coras1c91c772019-06-25 18:14:13 -0700584 f->start_chunk = f->end_chunk = f->new_chunks = 0;
585 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
586
Florin Coras62ddc032019-12-08 18:30:42 -0800587 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700588 svm_fifo_free_chunk_lookup (f);
589 ssvm_pop_heap (oldheap);
Florin Corasb095a3c2019-04-25 12:58:46 -0700590
591 /* not allocated on segment heap */
592 svm_fifo_free_ooo_data (f);
593
Florin Coras88001c62019-04-24 14:44:46 -0700594 if (CLIB_DEBUG)
595 {
596 f->master_session_index = ~0;
597 f->master_thread_index = ~0;
598 }
599
Florin Coras62ddc032019-12-08 18:30:42 -0800600 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700601}
602
Florin Corasf9d4ab42019-05-11 16:55:53 -0700603int
Florin Coras62ddc032019-12-08 18:30:42 -0800604fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
605 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700606{
607 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800608 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700609 svm_fifo_t *f;
610 void *oldheap;
611 u32 size;
612 u8 *fmem;
613 int i;
614
Florin Coras62ddc032019-12-08 18:30:42 -0800615 fss = fsh_slice_get (fsh, slice_index);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700616 size = (sizeof (*f)) * batch_size;
617
Florin Coras62ddc032019-12-08 18:30:42 -0800618 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700619 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
620 0 /* align_offset */ ,
621 0 /* os_out_of_memory */ );
622 ssvm_pop_heap (oldheap);
623
624 /* Out of space.. */
625 if (fmem == 0)
626 return -1;
627
628 /* Carve fifo + chunk space */
629 for (i = 0; i < batch_size; i++)
630 {
631 f = (svm_fifo_t *) fmem;
632 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800633 f->next = fss->free_fifos;
634 fss->free_fifos = f;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700635 fmem += sizeof (*f);
636 }
637
Florin Coras62ddc032019-12-08 18:30:42 -0800638 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700639
640 return 0;
641}
642
643int
Florin Coras62ddc032019-12-08 18:30:42 -0800644fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
645 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700646{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700647 u32 size, rounded_data_size, fl_index;
Florin Coras62ddc032019-12-08 18:30:42 -0800648 fifo_segment_header_t *fsh = fs->h;
649 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700650 svm_fifo_chunk_t *c;
651 void *oldheap;
652 u8 *cmem;
653 int i;
654
Florin Coras62ddc032019-12-08 18:30:42 -0800655 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700656 {
657 clib_warning ("chunk size out of range %d", chunk_size);
658 return -1;
659 }
660
661 fl_index = fs_freelist_for_size (chunk_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700662 rounded_data_size = fs_freelist_index_to_size (fl_index);
663 size = (sizeof (*c) + rounded_data_size) * batch_size;
664
Florin Coras62ddc032019-12-08 18:30:42 -0800665 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700666 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
667 0 /* align_offset */ ,
668 0 /* os_out_of_memory */ );
669 ssvm_pop_heap (oldheap);
670
671 /* Out of space.. */
672 if (cmem == 0)
673 return -1;
674
Florin Coras62ddc032019-12-08 18:30:42 -0800675 fss = fsh_slice_get (fsh, slice_index);
676
Florin Corasf9d4ab42019-05-11 16:55:53 -0700677 /* Carve fifo + chunk space */
678 for (i = 0; i < batch_size; i++)
679 {
680 c = (svm_fifo_chunk_t *) cmem;
681 c->start_byte = 0;
682 c->length = rounded_data_size;
Florin Coras62ddc032019-12-08 18:30:42 -0800683 c->next = fss->free_chunks[fl_index];
684 fss->free_chunks[fl_index] = c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700685 cmem += sizeof (*c) + rounded_data_size;
686 }
687
Florin Coras62ddc032019-12-08 18:30:42 -0800688 fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
689 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700690
691 return 0;
692}
693
Florin Coras88001c62019-04-24 14:44:46 -0700694/**
695 * Pre-allocates fifo pairs in fifo segment
696 */
697void
698fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
699 u32 rx_fifo_size, u32 tx_fifo_size,
700 u32 * n_fifo_pairs)
701{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700702 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +0000703 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -0800704 fifo_segment_header_t *fsh = fs->h;
705 int rx_fl_index, tx_fl_index, i;
706 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700707 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700708
709 /* Parameter check */
710 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
711 return;
712
Florin Coras62ddc032019-12-08 18:30:42 -0800713 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700714 {
715 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
716 return;
717 }
718
Florin Coras62ddc032019-12-08 18:30:42 -0800719 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700720 {
721 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
722 return;
723 }
724
725 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700726 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700727 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700728 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700729
Florin Corasf9d4ab42019-05-11 16:55:53 -0700730 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -0700731
Florin Coras88001c62019-04-24 14:44:46 -0700732 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -0700733 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -0800734 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -0700735 pairs_to_alloc = space_available / pair_size;
736 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -0800737 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +0000738 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700739
Florin Coras62ddc032019-12-08 18:30:42 -0800740 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -0700741 return;
Florin Coras88001c62019-04-24 14:44:46 -0700742
Florin Coras62ddc032019-12-08 18:30:42 -0800743 for (i = 0; i < fs->n_slices; i++)
744 {
745 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +0000746 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
747 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
748 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
749 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
750 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -0700751
Florin Coras8d0149d2020-01-02 19:22:51 +0000752 /* Account for the pairs allocated */
753 *n_fifo_pairs -= alloc_now;
754 }
Florin Coras88001c62019-04-24 14:44:46 -0700755}
756
Florin Corasb095a3c2019-04-25 12:58:46 -0700757int
758fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
759{
Florin Coras62ddc032019-12-08 18:30:42 -0800760 fifo_segment_header_t *fsh = fs->h;
761 fifo_segment_slice_t *fss;
Florin Corasb095a3c2019-04-25 12:58:46 -0700762 svm_fifo_chunk_t *c;
763 void *oldheap;
764 int fl_index;
765
Florin Corasf9d4ab42019-05-11 16:55:53 -0700766 fl_index = fs_freelist_for_size (chunk_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800767 fss = fsh_slice_get (fsh, f->slice_index);
Florin Corasb095a3c2019-04-25 12:58:46 -0700768
Florin Coras62ddc032019-12-08 18:30:42 -0800769 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corasb095a3c2019-04-25 12:58:46 -0700770
Florin Coras62ddc032019-12-08 18:30:42 -0800771 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -0700772
773 if (!c)
774 {
Florin Coras8122cc22019-12-18 13:06:41 -0800775 fsh_check_mem (fsh);
776 if (fsh_n_free_bytes (fsh) < chunk_size)
777 {
778 ssvm_pop_heap (oldheap);
779 return -1;
780 }
781
Florin Corasb095a3c2019-04-25 12:58:46 -0700782 c = svm_fifo_chunk_alloc (chunk_size);
783 if (!c)
784 {
785 ssvm_pop_heap (oldheap);
786 return -1;
787 }
Florin Coras62ddc032019-12-08 18:30:42 -0800788 fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c));
Florin Corasb095a3c2019-04-25 12:58:46 -0700789 }
790 else
791 {
Florin Coras62ddc032019-12-08 18:30:42 -0800792 fss->free_chunks[fl_index] = c->next;
Florin Corascefd5d82019-05-05 13:19:57 -0700793 c->next = 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800794 fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index);
Florin Corasb095a3c2019-04-25 12:58:46 -0700795 }
796
797 svm_fifo_add_chunk (f, c);
798
799 ssvm_pop_heap (oldheap);
Florin Corasb095a3c2019-04-25 12:58:46 -0700800 return 0;
801}
802
Florin Coras344ce422019-05-03 11:46:55 -0700803int
804fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
805{
Florin Coras62ddc032019-12-08 18:30:42 -0800806 fifo_segment_header_t *fsh = fs->h;
Florin Coras344ce422019-05-03 11:46:55 -0700807 svm_fifo_chunk_t *cur, *next;
Florin Coras62ddc032019-12-08 18:30:42 -0800808 fifo_segment_slice_t *fss;
Florin Coras344ce422019-05-03 11:46:55 -0700809 void *oldheap;
810 int fl_index;
811
Florin Coras62ddc032019-12-08 18:30:42 -0800812 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Coras344ce422019-05-03 11:46:55 -0700813 cur = svm_fifo_collect_chunks (f);
814
Florin Coras62ddc032019-12-08 18:30:42 -0800815 fss = fsh_slice_get (fsh, f->slice_index);
816
Florin Coras344ce422019-05-03 11:46:55 -0700817 while (cur)
818 {
819 next = cur->next;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700820 fl_index = fs_freelist_for_size (cur->length);
Florin Coras62ddc032019-12-08 18:30:42 -0800821 cur->next = fss->free_chunks[fl_index];
822 fss->free_chunks[fl_index] = cur;
Florin Coras344ce422019-05-03 11:46:55 -0700823 cur = next;
824 }
825
826 ssvm_pop_heap (oldheap);
Florin Coras344ce422019-05-03 11:46:55 -0700827
828 return 0;
829}
830
Florin Coras88001c62019-04-24 14:44:46 -0700831/**
832 * Get number of active fifos
833 */
834u32
835fifo_segment_num_fifos (fifo_segment_t * fs)
836{
Florin Coras62ddc032019-12-08 18:30:42 -0800837 return clib_atomic_load_relax_n (&fs->h->n_active_fifos);
Florin Coras88001c62019-04-24 14:44:46 -0700838}
839
Florin Coras62ddc032019-12-08 18:30:42 -0800840static u32
841fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -0700842{
Florin Coras88001c62019-04-24 14:44:46 -0700843 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700844 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700845
Florin Coras62ddc032019-12-08 18:30:42 -0800846 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700847 if (f == 0)
848 return 0;
849
850 while (f)
851 {
852 f = f->next;
853 count++;
854 }
855 return count;
856}
857
Florin Corasb095a3c2019-04-25 12:58:46 -0700858u32
Florin Coras62ddc032019-12-08 18:30:42 -0800859fifo_segment_num_free_fifos (fifo_segment_t * fs)
860{
861 fifo_segment_header_t *fsh = fs->h;
862 fifo_segment_slice_t *fss;
863 int slice_index;
864 u32 count = 0;
865
866 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
867 {
868 fss = fsh_slice_get (fsh, slice_index);
869 count += fs_slice_num_free_fifos (fss);
870 }
871 return count;
872}
873
874static u32
875fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -0700876{
877 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -0700878 svm_fifo_chunk_t *c;
879 int i;
880
Florin Corasb095a3c2019-04-25 12:58:46 -0700881 /* Count all free chunks? */
882 if (size == ~0)
883 {
Florin Coras62ddc032019-12-08 18:30:42 -0800884 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -0700885 {
Florin Coras62ddc032019-12-08 18:30:42 -0800886 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -0700887 if (c == 0)
888 continue;
889
890 while (c)
891 {
892 c = c->next;
893 count++;
894 }
895 }
896 return count;
897 }
898
899 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700900 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -0700901
Florin Coras62ddc032019-12-08 18:30:42 -0800902 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -0700903 return 0;
904
Florin Coras62ddc032019-12-08 18:30:42 -0800905 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -0700906 if (c == 0)
907 return 0;
908
909 while (c)
910 {
911 c = c->next;
912 count++;
913 }
914 return count;
915}
916
Florin Coras62ddc032019-12-08 18:30:42 -0800917u32
918fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
919{
920 fifo_segment_header_t *fsh = fs->h;
921 fifo_segment_slice_t *fss;
922 int slice_index;
923 u32 count = 0;
924
925 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
926 {
927 fss = fsh_slice_get (fsh, slice_index);
928 count += fs_slice_num_free_chunks (fss, size);
929 }
930 return count;
931}
932
Florin Corasf9d4ab42019-05-11 16:55:53 -0700933void
934fifo_segment_update_free_bytes (fifo_segment_t * fs)
935{
Florin Coras4453c092019-12-19 10:13:15 -0800936 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700937}
938
Florin Corasef4f3e72019-12-11 14:27:53 -0800939uword
Florin Corasf9d4ab42019-05-11 16:55:53 -0700940fifo_segment_free_bytes (fifo_segment_t * fs)
941{
Florin Coras62ddc032019-12-08 18:30:42 -0800942 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700943}
944
Florin Coras62ddc032019-12-08 18:30:42 -0800945uword
Florin Coras1c91c772019-06-25 18:14:13 -0700946fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700947{
Florin Coras62ddc032019-12-08 18:30:42 -0800948 fifo_segment_header_t *fsh = fs->h;
949 fifo_segment_slice_t *fss;
950 uword n_bytes = 0;
951 int slice_index;
952
953 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
954 {
955 fss = fsh_slice_get (fsh, slice_index);
956 n_bytes += fss->n_fl_chunk_bytes;
957 }
958
959 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700960}
961
Florin Coras88001c62019-04-24 14:44:46 -0700962u8
963fifo_segment_has_fifos (fifo_segment_t * fs)
964{
Florin Coras62ddc032019-12-08 18:30:42 -0800965 fifo_segment_header_t *fsh = fs->h;
966 fifo_segment_slice_t *fss;
967 int slice_index;
968
969 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
970 {
971 fss = fsh_slice_get (fsh, slice_index);
972 if (fss->fifos)
973 return 1;
974 }
975 return 0;
Florin Coras88001c62019-04-24 14:44:46 -0700976}
977
978svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800979fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -0700980{
Florin Coras62ddc032019-12-08 18:30:42 -0800981 fifo_segment_header_t *fsh = fs->h;
982 fifo_segment_slice_t *fss;
983
984 fss = fsh_slice_get (fsh, slice_index);
985 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700986}
987
988u8 *
989format_fifo_segment_type (u8 * s, va_list * args)
990{
991 fifo_segment_t *sp;
992 sp = va_arg (*args, fifo_segment_t *);
993 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
994
995 if (st == SSVM_SEGMENT_PRIVATE)
996 s = format (s, "%s", "private-heap");
997 else if (st == SSVM_SEGMENT_MEMFD)
998 s = format (s, "%s", "memfd");
999 else if (st == SSVM_SEGMENT_SHM)
1000 s = format (s, "%s", "shm");
1001 else
1002 s = format (s, "%s", "unknown");
1003 return s;
1004}
1005
1006/**
1007 * Segment format function
1008 */
1009u8 *
1010format_fifo_segment (u8 * s, va_list * args)
1011{
Florin Corasef4f3e72019-12-11 14:27:53 -08001012 u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001013 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001014 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001015 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1016 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Florin Coras5368bb02019-06-09 09:24:33 -07001017 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001018 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001019 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001020 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001021 char *address;
1022 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001023 int i;
1024
1025 indent = format_get_indent (s) + 2;
1026#if USE_DLMALLOC == 0
1027 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
Florin Coras62ddc032019-12-08 18:30:42 -08001028 format_mheap, fsh->ssvm_sh->heap, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001029 s = format (s, "%U segment has %u active fifos\n",
Florin Coras62ddc032019-12-08 18:30:42 -08001030 format_white_space, indent, fifo_segment_num_fifos (fsh));
Florin Coras88001c62019-04-24 14:44:46 -07001031#endif
1032
Florin Coras5368bb02019-06-09 09:24:33 -07001033 if (fs == 0)
1034 {
1035 s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
1036 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1037 return s;
1038 }
1039
Florin Coras5368bb02019-06-09 09:24:33 -07001040 fifo_segment_info (fs, &address, &size);
1041 active_fifos = fifo_segment_num_fifos (fs);
1042 free_fifos = fifo_segment_num_free_fifos (fs);
1043
1044 s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
1045 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1046 free_fifos, address);
1047
1048 if (!verbose)
1049 return s;
1050
Florin Coras62ddc032019-12-08 18:30:42 -08001051 fsh = fs->h;
1052
1053 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1054 if (free_chunks)
Florin Corasf8461bf2019-11-07 17:00:15 -08001055 s = format (s, "\n\n%UFree chunks by size:\n", format_white_space,
1056 indent + 2);
1057 else
1058 s = format (s, "\n");
1059
Florin Coras62ddc032019-12-08 18:30:42 -08001060 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001061 {
Florin Coras62ddc032019-12-08 18:30:42 -08001062 fss = fsh_slice_get (fsh, slice_index);
1063 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001064 {
Florin Coras62ddc032019-12-08 18:30:42 -08001065 c = fss->free_chunks[i];
1066 if (c == 0)
1067 continue;
1068 count = 0;
1069 while (c)
1070 {
1071 c = c->next;
1072 count++;
1073 }
1074
1075 chunk_size = fs_freelist_index_to_size (i);
1076 s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2,
1077 chunk_size >> 10, count);
1078
1079 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001080 }
Florin Coras88001c62019-04-24 14:44:46 -07001081 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001082
1083 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1084 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001085 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001086 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001087 free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001088
Florin Corasef4f3e72019-12-11 14:27:53 -08001089 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001090 format_white_space, indent + 2, format_memory_size,
Florin Coras62ddc032019-12-08 18:30:42 -08001091 free_seg_bytes, free_seg_bytes, format_memory_size,
Florin Corasf8461bf2019-11-07 17:00:15 -08001092 est_free_seg_bytes, est_free_seg_bytes);
Florin Corasef4f3e72019-12-11 14:27:53 -08001093 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n",
Florin Corasf8461bf2019-11-07 17:00:15 -08001094 format_white_space, indent + 2, format_memory_size, chunk_bytes,
1095 chunk_bytes, format_memory_size, est_chunk_bytes,
1096 est_chunk_bytes);
Florin Coras8122cc22019-12-18 13:06:41 -08001097 s = format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n",
1098 format_white_space, indent + 2, format_memory_size, fifo_hdr,
1099 fifo_hdr, format_memory_size, fsh->n_reserved_bytes,
1100 fsh->n_reserved_bytes);
Florin Corasf8461bf2019-11-07 17:00:15 -08001101 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001102
Florin Coras88001c62019-04-24 14:44:46 -07001103 return s;
1104}
1105
1106/*
1107 * fd.io coding-style-patch-verification: ON
1108 *
1109 * Local Variables:
1110 * eval: (c-set-style "gnu")
1111 * End:
1112 */