Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 19 | * 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 Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 28 | static uword |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 29 | fsh_free_space (fifo_segment_header_t * fsh) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 30 | { |
| 31 | struct dlmallinfo dlminfo; |
| 32 | |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 33 | dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 34 | return dlminfo.fordblks; |
| 35 | } |
| 36 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 37 | static inline void |
| 38 | fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size) |
| 39 | { |
| 40 | clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size); |
| 41 | } |
| 42 | |
| 43 | static inline uword |
| 44 | fsh_n_free_bytes (fifo_segment_header_t * fsh) |
| 45 | { |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 46 | 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 | |
| 50 | static inline void |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 51 | fsh_update_free_bytes (fifo_segment_header_t * fsh) |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 52 | { |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 53 | clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh)); |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void |
| 57 | fsh_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 Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 70 | fsh_update_free_bytes (fsh); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static inline fifo_segment_slice_t * |
| 74 | fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index) |
| 75 | { |
| 76 | return &fsh->slices[slice_index]; |
| 77 | } |
| 78 | |
| 79 | static inline void |
| 80 | fsh_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 Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 85 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 86 | * Initialize fifo segment shared header |
| 87 | */ |
| 88 | int |
| 89 | fifo_segment_init (fifo_segment_t * fs) |
| 90 | { |
| 91 | fifo_segment_header_t *fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 92 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 93 | ssvm_shared_header_t *sh; |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 94 | u32 max_chunk_sz, max_chunks; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 95 | uword max_fifo; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 96 | void *oldheap; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 97 | int i; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 98 | |
| 99 | sh = fs->ssvm.sh; |
| 100 | oldheap = ssvm_push_heap (sh); |
| 101 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 102 | /* |
| 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 Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 112 | clib_memset (fsh, 0, sizeof (*fsh)); |
| 113 | fs->h = sh->opaque[0] = fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 114 | 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 Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 118 | max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 119 | 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 Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 131 | |
| 132 | ssvm_pop_heap (oldheap); |
| 133 | |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 134 | fsh->n_free_bytes = fsh_free_space (fsh); |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 135 | max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE; |
| 136 | fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 137 | sh->ready = 1; |
| 138 | return (0); |
| 139 | } |
| 140 | |
| 141 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 142 | * Create a fifo segment and initialize as master |
| 143 | */ |
| 144 | int |
| 145 | fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a) |
| 146 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 147 | fifo_segment_t *fs; |
| 148 | uword baseva; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 149 | int rv; |
| 150 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 151 | /* Allocate a fresh segment */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 152 | pool_get_zero (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 153 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 154 | 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 Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 160 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 161 | if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type))) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 162 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 163 | pool_put (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 164 | return (rv); |
| 165 | } |
| 166 | |
| 167 | /* Note: requested_va updated due to seg base addr randomization */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 168 | sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 169 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 170 | fifo_segment_init (fs); |
| 171 | vec_add1 (a->new_segment_indices, fs - sm->segments); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 172 | return (0); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Attach as slave to a fifo segment |
| 177 | */ |
| 178 | int |
| 179 | fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a) |
| 180 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 181 | fifo_segment_t *fs; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 182 | int rv; |
| 183 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 184 | pool_get_zero (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 185 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 186 | 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 Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 190 | if (a->segment_type == SSVM_SEGMENT_MEMFD) |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 191 | fs->ssvm.fd = a->memfd_fd; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 192 | else |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 193 | fs->ssvm.attach_timeout = sm->timeout_in_seconds; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 194 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 195 | if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type))) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 196 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 197 | _vec_len (fs) = vec_len (fs) - 1; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 198 | return (rv); |
| 199 | } |
| 200 | |
| 201 | /* Fish the segment header */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 202 | fs->h = fs->ssvm.sh->opaque[0]; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 203 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 204 | vec_add1 (a->new_segment_indices, fs - sm->segments); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 205 | return (0); |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | fifo_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 | |
| 216 | u32 |
| 217 | fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s) |
| 218 | { |
| 219 | return s - sm->segments; |
| 220 | } |
| 221 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 222 | fifo_segment_t * |
| 223 | fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index) |
| 224 | { |
| 225 | return pool_elt_at_index (sm->segments, segment_index); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size) |
| 230 | { |
Florin Coras | 404b8a3 | 2019-05-09 12:08:06 -0700 | [diff] [blame] | 231 | *address = (char *) seg->ssvm.sh->ssvm_va; |
| 232 | *size = seg->ssvm.ssvm_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
| 236 | fifo_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 Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 243 | static inline u32 |
| 244 | fs_freelist_for_size (u32 size) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 245 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 246 | return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 249 | static inline u32 |
| 250 | fs_freelist_index_to_size (u32 fl_index) |
| 251 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 252 | return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 255 | static inline int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 256 | fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 257 | { |
| 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 Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 263 | && size <= (1 << fsh->max_log2_chunk_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 266 | static svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 267 | fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, |
| 268 | u32 fl_index, u32 data_bytes) |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 269 | { |
| 270 | svm_fifo_chunk_t *c; |
| 271 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 272 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 273 | f = fss->free_fifos; |
| 274 | c = fss->free_chunks[fl_index]; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 275 | |
| 276 | if (!f || !c) |
| 277 | return 0; |
| 278 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 279 | fss->free_fifos = f->next; |
| 280 | fss->free_chunks[fl_index] = c->next; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 281 | 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 Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 287 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 288 | fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 289 | return f; |
| 290 | } |
| 291 | |
| 292 | static svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 293 | fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh, |
| 294 | fifo_segment_slice_t * fss, |
| 295 | u32 data_bytes) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 296 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 297 | svm_fifo_chunk_t *c, *first = 0, *last = 0; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 298 | u32 fl_index, fl_size, n_alloc = 0; |
| 299 | svm_fifo_t *f; |
| 300 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 301 | f = fss->free_fifos; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 302 | if (!f) |
| 303 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 304 | void *oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 305 | 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 Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 310 | fsh_free_bytes_sub (fsh, sizeof (*f)); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 311 | } |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 312 | else |
| 313 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 314 | fss->free_fifos = f->next; |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 315 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 316 | |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 317 | fl_index = fs_freelist_for_size (data_bytes); |
| 318 | if (fl_index > 0) |
| 319 | fl_index -= 1; |
| 320 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 321 | fl_size = fs_freelist_index_to_size (fl_index); |
| 322 | |
| 323 | while (data_bytes) |
| 324 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 325 | c = fss->free_chunks[fl_index]; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 326 | if (c) |
| 327 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 328 | fss->free_chunks[fl_index] = c->next; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 329 | if (!last) |
| 330 | last = c; |
| 331 | c->next = first; |
| 332 | first = c; |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 333 | n_alloc += fl_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 334 | c->length = clib_min (fl_size, data_bytes); |
| 335 | data_bytes -= c->length; |
| 336 | } |
| 337 | else |
| 338 | { |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 339 | /* 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 Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 366 | fl_index -= 1; |
| 367 | fl_size = fl_size >> 1; |
| 368 | } |
| 369 | } |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 370 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 371 | f->start_chunk = first; |
| 372 | f->end_chunk = last; |
| 373 | last->next = first; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 374 | fss->n_fl_chunk_bytes -= n_alloc; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 375 | return f; |
| 376 | } |
| 377 | |
| 378 | static int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 379 | fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh, |
| 380 | fifo_segment_slice_t * fss, |
| 381 | u32 fl_index, u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 382 | { |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 383 | u32 hdrs, rounded_data_size; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 384 | svm_fifo_chunk_t *c; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 385 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 386 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 387 | uword size; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 388 | u8 *fmem; |
| 389 | int i; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 390 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 391 | rounded_data_size = fs_freelist_index_to_size (fl_index); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 392 | hdrs = sizeof (*f) + sizeof (*c); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 393 | size = (uword) (hdrs + rounded_data_size) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 394 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 395 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 396 | fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 397 | 0 /* align_offset */ , |
| 398 | 0 /* os_out_of_memory */ ); |
| 399 | ssvm_pop_heap (oldheap); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 400 | |
| 401 | /* Out of space.. */ |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 402 | if (fmem == 0) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 403 | return -1; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 404 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 405 | /* Carve fifo + chunk space */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 406 | for (i = 0; i < batch_size; i++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 407 | { |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 408 | f = (svm_fifo_t *) fmem; |
| 409 | memset (f, 0, sizeof (*f)); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 410 | f->next = fss->free_fifos; |
| 411 | fss->free_fifos = f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 412 | c = (svm_fifo_chunk_t *) (fmem + sizeof (*f)); |
| 413 | c->start_byte = 0; |
| 414 | c->length = rounded_data_size; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 415 | c->next = fss->free_chunks[fl_index]; |
| 416 | fss->free_chunks[fl_index] = c; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 417 | fmem += hdrs + rounded_data_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 418 | } |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 419 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 420 | fss->n_fl_chunk_bytes += batch_size * rounded_data_size; |
| 421 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Try to allocate new fifo |
| 428 | * |
| 429 | * Tries the following steps in order: |
| 430 | * - grab fifo and chunk from freelists |
| 431 | * - batch fifo and chunk allocation |
| 432 | * - single fifo allocation |
| 433 | * - grab multiple fifo chunks from freelists |
| 434 | */ |
| 435 | static svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 436 | fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, |
| 437 | u32 data_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 438 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 439 | u32 fifo_sz, fl_index; |
| 440 | svm_fifo_t *f = 0; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 441 | uword n_free_bytes; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 442 | |
| 443 | fl_index = fs_freelist_for_size (data_bytes); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 444 | fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); |
| 445 | fifo_sz += 1 << max_log2 (data_bytes); |
| 446 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 447 | if (fss->free_fifos && fss->free_chunks[fl_index]) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 448 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 449 | f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 450 | if (f) |
| 451 | goto done; |
| 452 | } |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 453 | |
| 454 | fsh_check_mem (fsh); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 455 | n_free_bytes = fsh_n_free_bytes (fsh); |
| 456 | if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 457 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 458 | if (fs_try_alloc_fifo_batch (fsh, fss, fl_index, |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 459 | FIFO_SEGMENT_ALLOC_BATCH_SIZE)) |
| 460 | goto done; |
| 461 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 462 | f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 463 | goto done; |
| 464 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 465 | if (fifo_sz <= n_free_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 466 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 467 | void *oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 468 | f = svm_fifo_create (data_bytes); |
| 469 | ssvm_pop_heap (oldheap); |
| 470 | if (f) |
| 471 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 472 | fsh_free_bytes_sub (fsh, fifo_sz); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 473 | goto done; |
| 474 | } |
| 475 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 476 | if (data_bytes <= fss->n_fl_chunk_bytes) |
| 477 | f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, data_bytes); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 478 | |
| 479 | done: |
| 480 | |
| 481 | return f; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Allocate fifo in fifo segment |
| 486 | */ |
| 487 | svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 488 | fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index, |
| 489 | u32 data_bytes, fifo_segment_ftype_t ftype) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 490 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 491 | fifo_segment_header_t *fsh = fs->h; |
| 492 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 493 | svm_fifo_t *f = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 494 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 495 | ASSERT (slice_index < fs->n_slices); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 496 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 497 | fss = fsh_slice_get (fsh, slice_index); |
| 498 | f = fs_try_alloc_fifo (fsh, fss, data_bytes); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 499 | if (!f) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 500 | goto done; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 501 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 502 | f->slice_index = slice_index; |
| 503 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 504 | /* (re)initialize the fifo, as in svm_fifo_create */ |
| 505 | svm_fifo_init (f, data_bytes); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 506 | |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 507 | /* Initialize chunks and rbtree for multi-chunk fifos */ |
| 508 | if (f->start_chunk->next != f->start_chunk) |
| 509 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 510 | void *oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 511 | svm_fifo_init_chunks (f); |
| 512 | ssvm_pop_heap (oldheap); |
| 513 | } |
| 514 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 515 | /* If rx fifo type add to active fifos list. When cleaning up segment, |
| 516 | * we need a list of active sessions that should be disconnected. Since |
| 517 | * both rx and tx fifos keep pointers to the session, it's enough to track |
| 518 | * only one. */ |
| 519 | if (ftype == FIFO_SEGMENT_RX_FIFO) |
| 520 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 521 | if (fss->fifos) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 522 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 523 | fss->fifos->prev = f; |
| 524 | f->next = fss->fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 525 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 526 | fss->fifos = f; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 527 | f->flags |= SVM_FIFO_F_LL_TRACKED; |
| 528 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 529 | fsh_active_fifos_update (fsh, 1); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 530 | |
| 531 | done: |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 532 | return (f); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Free fifo allocated in fifo segment |
| 537 | */ |
| 538 | void |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 539 | fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 540 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 541 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 542 | svm_fifo_chunk_t *cur, *next; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 543 | fifo_segment_slice_t *fss; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 544 | void *oldheap; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 545 | int fl_index; |
| 546 | |
| 547 | ASSERT (f->refcnt > 0); |
| 548 | |
| 549 | if (--f->refcnt > 0) |
| 550 | return; |
| 551 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 552 | fss = fsh_slice_get (fsh, f->slice_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 553 | |
| 554 | /* Remove from active list. Only rx fifos are tracked */ |
| 555 | if (f->flags & SVM_FIFO_F_LL_TRACKED) |
| 556 | { |
| 557 | if (f->prev) |
| 558 | f->prev->next = f->next; |
| 559 | else |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 560 | fss->fifos = f->next; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 561 | if (f->next) |
| 562 | f->next->prev = f->prev; |
| 563 | f->flags &= ~SVM_FIFO_F_LL_TRACKED; |
| 564 | } |
| 565 | |
| 566 | /* Add to free list */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 567 | f->next = fss->free_fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 568 | f->prev = 0; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 569 | fss->free_fifos = f; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 570 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 571 | /* Free fifo chunks */ |
| 572 | cur = f->start_chunk; |
| 573 | do |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 574 | { |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 575 | next = cur->next; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 576 | fl_index = fs_freelist_for_size (cur->length); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 577 | ASSERT (fl_index < vec_len (fss->free_chunks)); |
| 578 | cur->next = fss->free_chunks[fl_index]; |
| 579 | fss->free_chunks[fl_index] = cur; |
| 580 | fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 581 | cur = next; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 582 | } |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 583 | while (cur != f->start_chunk); |
| 584 | |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 585 | f->start_chunk = f->end_chunk = f->new_chunks = 0; |
| 586 | f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0; |
| 587 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 588 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 589 | svm_fifo_free_chunk_lookup (f); |
| 590 | ssvm_pop_heap (oldheap); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 591 | |
| 592 | /* not allocated on segment heap */ |
| 593 | svm_fifo_free_ooo_data (f); |
| 594 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 595 | if (CLIB_DEBUG) |
| 596 | { |
| 597 | f->master_session_index = ~0; |
| 598 | f->master_thread_index = ~0; |
| 599 | } |
| 600 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 601 | fsh_active_fifos_update (fsh, -1); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 604 | int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 605 | fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index, |
| 606 | u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 607 | { |
| 608 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 609 | fifo_segment_slice_t *fss; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 610 | svm_fifo_t *f; |
| 611 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 612 | uword size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 613 | u8 *fmem; |
| 614 | int i; |
| 615 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 616 | fss = fsh_slice_get (fsh, slice_index); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 617 | size = (uword) (sizeof (*f)) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 618 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 619 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 620 | fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 621 | 0 /* align_offset */ , |
| 622 | 0 /* os_out_of_memory */ ); |
| 623 | ssvm_pop_heap (oldheap); |
| 624 | |
| 625 | /* Out of space.. */ |
| 626 | if (fmem == 0) |
| 627 | return -1; |
| 628 | |
| 629 | /* Carve fifo + chunk space */ |
| 630 | for (i = 0; i < batch_size; i++) |
| 631 | { |
| 632 | f = (svm_fifo_t *) fmem; |
| 633 | memset (f, 0, sizeof (*f)); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 634 | f->next = fss->free_fifos; |
| 635 | fss->free_fifos = f; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 636 | fmem += sizeof (*f); |
| 637 | } |
| 638 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 639 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 645 | fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index, |
| 646 | u32 chunk_size, u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 647 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 648 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 649 | u32 rounded_data_size, fl_index; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 650 | fifo_segment_slice_t *fss; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 651 | svm_fifo_chunk_t *c; |
| 652 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 653 | uword size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 654 | u8 *cmem; |
| 655 | int i; |
| 656 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 657 | if (!fs_chunk_size_is_valid (fsh, chunk_size)) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 658 | { |
| 659 | clib_warning ("chunk size out of range %d", chunk_size); |
| 660 | return -1; |
| 661 | } |
| 662 | |
| 663 | fl_index = fs_freelist_for_size (chunk_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 664 | rounded_data_size = fs_freelist_index_to_size (fl_index); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 665 | size = (uword) (sizeof (*c) + rounded_data_size) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 666 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 667 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 668 | cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 669 | 0 /* align_offset */ , |
| 670 | 0 /* os_out_of_memory */ ); |
| 671 | ssvm_pop_heap (oldheap); |
| 672 | |
| 673 | /* Out of space.. */ |
| 674 | if (cmem == 0) |
| 675 | return -1; |
| 676 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 677 | fss = fsh_slice_get (fsh, slice_index); |
| 678 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 679 | /* Carve fifo + chunk space */ |
| 680 | for (i = 0; i < batch_size; i++) |
| 681 | { |
| 682 | c = (svm_fifo_chunk_t *) cmem; |
| 683 | c->start_byte = 0; |
| 684 | c->length = rounded_data_size; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 685 | c->next = fss->free_chunks[fl_index]; |
| 686 | fss->free_chunks[fl_index] = c; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 687 | cmem += sizeof (*c) + rounded_data_size; |
| 688 | } |
| 689 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 690 | fss->n_fl_chunk_bytes += batch_size * rounded_data_size; |
| 691 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 696 | /** |
| 697 | * Pre-allocates fifo pairs in fifo segment |
| 698 | */ |
| 699 | void |
| 700 | fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs, |
| 701 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 702 | u32 * n_fifo_pairs) |
| 703 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 704 | u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc; |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 705 | u32 hdrs, pairs_per_slice, alloc_now; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 706 | fifo_segment_header_t *fsh = fs->h; |
| 707 | int rx_fl_index, tx_fl_index, i; |
| 708 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 709 | uword space_available; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 710 | |
| 711 | /* Parameter check */ |
| 712 | if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0) |
| 713 | return; |
| 714 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 715 | if (!fs_chunk_size_is_valid (fsh, rx_fifo_size)) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 716 | { |
| 717 | clib_warning ("rx fifo_size out of range %d", rx_fifo_size); |
| 718 | return; |
| 719 | } |
| 720 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 721 | if (!fs_chunk_size_is_valid (fsh, tx_fifo_size)) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 722 | { |
| 723 | clib_warning ("tx fifo_size out of range %d", tx_fifo_size); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 728 | rx_fl_index = fs_freelist_for_size (rx_fifo_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 729 | tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 730 | tx_fl_index = fs_freelist_for_size (tx_fifo_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 731 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 732 | hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 733 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 734 | /* Calculate space requirements */ |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 735 | pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 736 | space_available = fsh_free_space (fsh); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 737 | pairs_to_alloc = space_available / pair_size; |
| 738 | pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 739 | pairs_per_slice = pairs_to_alloc / fs->n_slices; |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 740 | pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0; |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 741 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 742 | if (!pairs_per_slice) |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 743 | return; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 744 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 745 | for (i = 0; i < fs->n_slices; i++) |
| 746 | { |
| 747 | fss = fsh_slice_get (fsh, i); |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 748 | alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs); |
| 749 | if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now)) |
| 750 | clib_warning ("rx prealloc failed: pairs %u", alloc_now); |
| 751 | if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now)) |
| 752 | clib_warning ("tx prealloc failed: pairs %u", alloc_now); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 753 | |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 754 | /* Account for the pairs allocated */ |
| 755 | *n_fifo_pairs -= alloc_now; |
| 756 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 759 | int |
| 760 | fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size) |
| 761 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 762 | fifo_segment_header_t *fsh = fs->h; |
| 763 | fifo_segment_slice_t *fss; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 764 | svm_fifo_chunk_t *c; |
| 765 | void *oldheap; |
| 766 | int fl_index; |
| 767 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 768 | fl_index = fs_freelist_for_size (chunk_size); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 769 | fss = fsh_slice_get (fsh, f->slice_index); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 770 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 771 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 772 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 773 | c = fss->free_chunks[fl_index]; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 774 | |
| 775 | if (!c) |
| 776 | { |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 777 | fsh_check_mem (fsh); |
| 778 | if (fsh_n_free_bytes (fsh) < chunk_size) |
| 779 | { |
| 780 | ssvm_pop_heap (oldheap); |
| 781 | return -1; |
| 782 | } |
| 783 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 784 | c = svm_fifo_chunk_alloc (chunk_size); |
| 785 | if (!c) |
| 786 | { |
| 787 | ssvm_pop_heap (oldheap); |
| 788 | return -1; |
| 789 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 790 | fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c)); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 791 | } |
| 792 | else |
| 793 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 794 | fss->free_chunks[fl_index] = c->next; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 795 | c->next = 0; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 796 | fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | svm_fifo_add_chunk (f, c); |
| 800 | |
| 801 | ssvm_pop_heap (oldheap); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 805 | int |
| 806 | fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f) |
| 807 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 808 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 809 | svm_fifo_chunk_t *cur, *next; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 810 | fifo_segment_slice_t *fss; |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 811 | void *oldheap; |
| 812 | int fl_index; |
| 813 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 814 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 815 | cur = svm_fifo_collect_chunks (f); |
| 816 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 817 | fss = fsh_slice_get (fsh, f->slice_index); |
| 818 | |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 819 | while (cur) |
| 820 | { |
| 821 | next = cur->next; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 822 | fl_index = fs_freelist_for_size (cur->length); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 823 | cur->next = fss->free_chunks[fl_index]; |
| 824 | fss->free_chunks[fl_index] = cur; |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 825 | cur = next; |
| 826 | } |
| 827 | |
| 828 | ssvm_pop_heap (oldheap); |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 833 | /** |
| 834 | * Get number of active fifos |
| 835 | */ |
| 836 | u32 |
| 837 | fifo_segment_num_fifos (fifo_segment_t * fs) |
| 838 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 839 | return clib_atomic_load_relax_n (&fs->h->n_active_fifos); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 842 | static u32 |
| 843 | fs_slice_num_free_fifos (fifo_segment_slice_t * fss) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 844 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 845 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 846 | u32 count = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 847 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 848 | f = fss->free_fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 849 | if (f == 0) |
| 850 | return 0; |
| 851 | |
| 852 | while (f) |
| 853 | { |
| 854 | f = f->next; |
| 855 | count++; |
| 856 | } |
| 857 | return count; |
| 858 | } |
| 859 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 860 | u32 |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 861 | fifo_segment_num_free_fifos (fifo_segment_t * fs) |
| 862 | { |
| 863 | fifo_segment_header_t *fsh = fs->h; |
| 864 | fifo_segment_slice_t *fss; |
| 865 | int slice_index; |
| 866 | u32 count = 0; |
| 867 | |
| 868 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 869 | { |
| 870 | fss = fsh_slice_get (fsh, slice_index); |
| 871 | count += fs_slice_num_free_fifos (fss); |
| 872 | } |
| 873 | return count; |
| 874 | } |
| 875 | |
| 876 | static u32 |
| 877 | fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 878 | { |
| 879 | u32 count = 0, rounded_size, fl_index; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 880 | svm_fifo_chunk_t *c; |
| 881 | int i; |
| 882 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 883 | /* Count all free chunks? */ |
| 884 | if (size == ~0) |
| 885 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 886 | for (i = 0; i < vec_len (fss->free_chunks); i++) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 887 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 888 | c = fss->free_chunks[i]; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 889 | if (c == 0) |
| 890 | continue; |
| 891 | |
| 892 | while (c) |
| 893 | { |
| 894 | c = c->next; |
| 895 | count++; |
| 896 | } |
| 897 | } |
| 898 | return count; |
| 899 | } |
| 900 | |
| 901 | rounded_size = (1 << (max_log2 (size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 902 | fl_index = fs_freelist_for_size (rounded_size); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 903 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 904 | if (fl_index >= vec_len (fss->free_chunks)) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 905 | return 0; |
| 906 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 907 | c = fss->free_chunks[fl_index]; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 908 | if (c == 0) |
| 909 | return 0; |
| 910 | |
| 911 | while (c) |
| 912 | { |
| 913 | c = c->next; |
| 914 | count++; |
| 915 | } |
| 916 | return count; |
| 917 | } |
| 918 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 919 | u32 |
| 920 | fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size) |
| 921 | { |
| 922 | fifo_segment_header_t *fsh = fs->h; |
| 923 | fifo_segment_slice_t *fss; |
| 924 | int slice_index; |
| 925 | u32 count = 0; |
| 926 | |
| 927 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 928 | { |
| 929 | fss = fsh_slice_get (fsh, slice_index); |
| 930 | count += fs_slice_num_free_chunks (fss, size); |
| 931 | } |
| 932 | return count; |
| 933 | } |
| 934 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 935 | void |
| 936 | fifo_segment_update_free_bytes (fifo_segment_t * fs) |
| 937 | { |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 938 | fsh_update_free_bytes (fs->h); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 941 | uword |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 942 | fifo_segment_free_bytes (fifo_segment_t * fs) |
| 943 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 944 | return fsh_n_free_bytes (fs->h); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 947 | uword |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 948 | fifo_segment_fl_chunk_bytes (fifo_segment_t * fs) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 949 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 950 | fifo_segment_header_t *fsh = fs->h; |
| 951 | fifo_segment_slice_t *fss; |
| 952 | uword n_bytes = 0; |
| 953 | int slice_index; |
| 954 | |
| 955 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 956 | { |
| 957 | fss = fsh_slice_get (fsh, slice_index); |
| 958 | n_bytes += fss->n_fl_chunk_bytes; |
| 959 | } |
| 960 | |
| 961 | return n_bytes; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 964 | u8 |
| 965 | fifo_segment_has_fifos (fifo_segment_t * fs) |
| 966 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 967 | fifo_segment_header_t *fsh = fs->h; |
| 968 | fifo_segment_slice_t *fss; |
| 969 | int slice_index; |
| 970 | |
| 971 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 972 | { |
| 973 | fss = fsh_slice_get (fsh, slice_index); |
| 974 | if (fss->fifos) |
| 975 | return 1; |
| 976 | } |
| 977 | return 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 981 | fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 982 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 983 | fifo_segment_header_t *fsh = fs->h; |
| 984 | fifo_segment_slice_t *fss; |
| 985 | |
| 986 | fss = fsh_slice_get (fsh, slice_index); |
| 987 | return fss->fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | u8 * |
| 991 | format_fifo_segment_type (u8 * s, va_list * args) |
| 992 | { |
| 993 | fifo_segment_t *sp; |
| 994 | sp = va_arg (*args, fifo_segment_t *); |
| 995 | ssvm_segment_type_t st = ssvm_type (&sp->ssvm); |
| 996 | |
| 997 | if (st == SSVM_SEGMENT_PRIVATE) |
| 998 | s = format (s, "%s", "private-heap"); |
| 999 | else if (st == SSVM_SEGMENT_MEMFD) |
| 1000 | s = format (s, "%s", "memfd"); |
| 1001 | else if (st == SSVM_SEGMENT_SHM) |
| 1002 | s = format (s, "%s", "shm"); |
| 1003 | else |
| 1004 | s = format (s, "%s", "unknown"); |
| 1005 | return s; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Segment format function |
| 1010 | */ |
| 1011 | u8 * |
| 1012 | format_fifo_segment (u8 * s, va_list * args) |
| 1013 | { |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1014 | u32 count, indent, active_fifos, free_fifos, fifo_hdr = 0; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1015 | fifo_segment_t *fs = va_arg (*args, fifo_segment_t *); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1016 | int verbose __attribute__ ((unused)) = va_arg (*args, int); |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1017 | uword est_chunk_bytes, est_free_seg_bytes, free_chunks; |
| 1018 | uword chunk_bytes = 0, free_seg_bytes, chunk_size; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1019 | fifo_segment_header_t *fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1020 | fifo_segment_slice_t *fss; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 1021 | svm_fifo_chunk_t *c; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1022 | u32 slice_index; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1023 | char *address; |
| 1024 | size_t size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1025 | int i; |
| 1026 | |
| 1027 | indent = format_get_indent (s) + 2; |
| 1028 | #if USE_DLMALLOC == 0 |
| 1029 | s = format (s, "%U segment heap: %U\n", format_white_space, indent, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1030 | format_mheap, fsh->ssvm_sh->heap, verbose); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1031 | s = format (s, "%U segment has %u active fifos\n", |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1032 | format_white_space, indent, fifo_segment_num_fifos (fsh)); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1033 | #endif |
| 1034 | |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1035 | if (fs == 0) |
| 1036 | { |
| 1037 | s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type", |
| 1038 | "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address"); |
| 1039 | return s; |
| 1040 | } |
| 1041 | |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1042 | fifo_segment_info (fs, &address, &size); |
| 1043 | active_fifos = fifo_segment_num_fifos (fs); |
| 1044 | free_fifos = fifo_segment_num_free_fifos (fs); |
| 1045 | |
| 1046 | s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm), |
| 1047 | format_fifo_segment_type, fs, size >> 20ULL, active_fifos, |
| 1048 | free_fifos, address); |
| 1049 | |
| 1050 | if (!verbose) |
| 1051 | return s; |
| 1052 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1053 | fsh = fs->h; |
| 1054 | |
| 1055 | free_chunks = fifo_segment_num_free_chunks (fs, ~0); |
| 1056 | if (free_chunks) |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1057 | s = format (s, "\n\n%UFree chunks by size:\n", format_white_space, |
| 1058 | indent + 2); |
| 1059 | else |
| 1060 | s = format (s, "\n"); |
| 1061 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1062 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1063 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1064 | fss = fsh_slice_get (fsh, slice_index); |
| 1065 | for (i = 0; i < vec_len (fss->free_chunks); i++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1066 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1067 | c = fss->free_chunks[i]; |
| 1068 | if (c == 0) |
| 1069 | continue; |
| 1070 | count = 0; |
| 1071 | while (c) |
| 1072 | { |
| 1073 | c = c->next; |
| 1074 | count++; |
| 1075 | } |
| 1076 | |
| 1077 | chunk_size = fs_freelist_index_to_size (i); |
| 1078 | s = format (s, "%U%-5u kB: %u\n", format_white_space, indent + 2, |
| 1079 | chunk_size >> 10, count); |
| 1080 | |
| 1081 | chunk_bytes += count * chunk_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1082 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1083 | } |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1084 | |
| 1085 | fifo_hdr = free_fifos * sizeof (svm_fifo_t); |
| 1086 | est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1087 | est_free_seg_bytes = fifo_segment_free_bytes (fs); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1088 | fifo_segment_update_free_bytes (fs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1089 | free_seg_bytes = fifo_segment_free_bytes (fs); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1090 | |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1091 | s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n", |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1092 | format_white_space, indent + 2, format_memory_size, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1093 | free_seg_bytes, free_seg_bytes, format_memory_size, |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1094 | est_free_seg_bytes, est_free_seg_bytes); |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1095 | s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n", |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1096 | format_white_space, indent + 2, format_memory_size, chunk_bytes, |
| 1097 | chunk_bytes, format_memory_size, est_chunk_bytes, |
| 1098 | est_chunk_bytes); |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 1099 | s = format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n", |
| 1100 | format_white_space, indent + 2, format_memory_size, fifo_hdr, |
| 1101 | fifo_hdr, format_memory_size, fsh->n_reserved_bytes, |
| 1102 | fsh->n_reserved_bytes); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1103 | s = format (s, "\n"); |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1104 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1105 | return s; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * fd.io coding-style-patch-verification: ON |
| 1110 | * |
| 1111 | * Local Variables: |
| 1112 | * eval: (c-set-style "gnu") |
| 1113 | * End: |
| 1114 | */ |