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 | |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 18 | static inline fifo_segment_slice_t * |
| 19 | fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index) |
| 20 | { |
| 21 | return &fsh->slices[slice_index]; |
| 22 | } |
| 23 | |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 24 | static char *fifo_segment_mem_status_strings[] = { |
| 25 | #define _(sym,str) str, |
| 26 | foreach_segment_mem_status |
| 27 | #undef _ |
| 28 | }; |
| 29 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 30 | /** |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 31 | * Fifo segment free space |
| 32 | * |
| 33 | * Queries the underlying memory manager, dlmalloc, for free space. Since this |
| 34 | * ends up walking the internal data structures, it should not be called |
| 35 | * indiscriminately. |
| 36 | * |
| 37 | * @param fs fifo segment |
| 38 | * @return number of free bytes |
| 39 | */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 40 | static uword |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 41 | fsh_free_space (fifo_segment_header_t * fsh) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 42 | { |
| 43 | struct dlmallinfo dlminfo; |
| 44 | |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 45 | dlminfo = mspace_mallinfo (fsh->ssvm_sh->heap); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 46 | return dlminfo.fordblks; |
| 47 | } |
| 48 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 49 | static inline void |
| 50 | fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size) |
| 51 | { |
| 52 | clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size); |
| 53 | } |
| 54 | |
| 55 | static inline uword |
| 56 | fsh_n_free_bytes (fifo_segment_header_t * fsh) |
| 57 | { |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 58 | uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes); |
| 59 | return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0; |
| 60 | } |
| 61 | |
| 62 | static inline void |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 63 | fsh_update_free_bytes (fifo_segment_header_t * fsh) |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 64 | { |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 65 | 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] | 66 | } |
| 67 | |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 68 | static inline void |
| 69 | fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size) |
| 70 | { |
| 71 | clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size); |
| 72 | } |
| 73 | |
| 74 | static inline void |
| 75 | fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size) |
| 76 | { |
| 77 | clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size); |
| 78 | } |
| 79 | |
| 80 | static inline uword |
| 81 | fsh_n_cached_bytes (fifo_segment_header_t * fsh) |
| 82 | { |
| 83 | uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes); |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 84 | return n_cached; |
| 85 | } |
| 86 | |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 87 | static inline void |
| 88 | fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc) |
| 89 | { |
| 90 | clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc); |
| 91 | } |
| 92 | |
Florin Coras | 7a90e50 | 2020-04-09 04:46:14 +0000 | [diff] [blame] | 93 | static inline u32 |
| 94 | fsh_n_active_fifos (fifo_segment_header_t * fsh) |
| 95 | { |
| 96 | return clib_atomic_load_relax_n (&fsh->n_active_fifos); |
| 97 | } |
| 98 | |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 99 | static inline uword |
| 100 | fsh_virtual_mem (fifo_segment_header_t * fsh) |
| 101 | { |
| 102 | fifo_segment_slice_t *fss; |
| 103 | uword total_vm = 0; |
| 104 | int i; |
| 105 | |
| 106 | for (i = 0; i < fsh->n_slices; i++) |
| 107 | { |
| 108 | fss = fsh_slice_get (fsh, i); |
| 109 | total_vm += clib_atomic_load_relax_n (&fss->virtual_mem); |
| 110 | } |
| 111 | return total_vm; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index, |
| 116 | int n_bytes) |
| 117 | { |
| 118 | fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index); |
| 119 | fss->virtual_mem += n_bytes; |
| 120 | } |
| 121 | |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 122 | static void |
| 123 | fsh_check_mem (fifo_segment_header_t * fsh) |
| 124 | { |
| 125 | uword thresh; |
| 126 | |
| 127 | if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) |
| 128 | return; |
| 129 | |
| 130 | thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size, |
| 131 | 2 * fsh->n_reserved_bytes); |
| 132 | if (fsh->n_free_bytes > thresh) |
| 133 | return; |
| 134 | |
| 135 | fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 136 | fsh_update_free_bytes (fsh); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 139 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 140 | * Initialize fifo segment shared header |
| 141 | */ |
| 142 | int |
| 143 | fifo_segment_init (fifo_segment_t * fs) |
| 144 | { |
| 145 | fifo_segment_header_t *fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 146 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 147 | ssvm_shared_header_t *sh; |
Florin Coras | 05c7317 | 2020-03-05 20:36:40 +0000 | [diff] [blame] | 148 | u32 max_chunk_sz; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 149 | uword max_fifo; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 150 | void *oldheap; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 151 | int i; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 152 | |
| 153 | sh = fs->ssvm.sh; |
| 154 | oldheap = ssvm_push_heap (sh); |
| 155 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 156 | /* |
| 157 | * Manually align the fifo segment header to sizeof(uword) = 8 bytes. |
| 158 | * Long story made short: the "process-private" fifo segment |
| 159 | * is allocated from the main heap, not mmapped. dlmalloc |
| 160 | * only guarantees 4-byte alignment, and on aarch64 |
| 161 | * the fsh can end up 4-byte but not 8-byte aligned. |
| 162 | * That eventually causes the atomic op in fifo_segment_update_free_bytes |
| 163 | * to backfire. |
| 164 | */ |
| 165 | fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword)); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 166 | clib_memset (fsh, 0, sizeof (*fsh)); |
| 167 | fs->h = sh->opaque[0] = fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 168 | fs->n_slices = clib_max (fs->n_slices, 1); |
| 169 | |
| 170 | fsh->ssvm_sh = fs->ssvm.sh; |
| 171 | fsh->n_slices = fs->n_slices; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 172 | max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 173 | FIFO_SEGMENT_MAX_FIFO_SIZE); |
| 174 | fsh->max_log2_chunk_size = max_log2 (max_fifo); |
| 175 | |
| 176 | fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices); |
| 177 | clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices); |
| 178 | max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE; |
| 179 | |
| 180 | for (i = 0; i < fs->n_slices; i++) |
| 181 | { |
| 182 | fss = fsh_slice_get (fsh, i); |
| 183 | vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0); |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 184 | vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0); |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 185 | clib_spinlock_init (&fss->chunk_lock); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 186 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 187 | |
| 188 | ssvm_pop_heap (oldheap); |
| 189 | |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 190 | fsh->n_free_bytes = fsh_free_space (fsh); |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 191 | fsh->n_cached_bytes = 0; |
Florin Coras | 05c7317 | 2020-03-05 20:36:40 +0000 | [diff] [blame] | 192 | fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 193 | sh->ready = 1; |
| 194 | return (0); |
| 195 | } |
| 196 | |
| 197 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 198 | * Create a fifo segment and initialize as master |
| 199 | */ |
| 200 | int |
| 201 | fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a) |
| 202 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 203 | fifo_segment_t *fs; |
| 204 | uword baseva; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 205 | int rv; |
| 206 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 207 | /* Allocate a fresh segment */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 208 | pool_get_zero (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 209 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 210 | baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva; |
| 211 | fs->ssvm.ssvm_size = a->segment_size; |
| 212 | fs->ssvm.i_am_master = 1; |
| 213 | fs->ssvm.my_pid = getpid (); |
| 214 | fs->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
| 215 | fs->ssvm.requested_va = baseva; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 216 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 217 | if ((rv = ssvm_master_init (&fs->ssvm, a->segment_type))) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 218 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 219 | pool_put (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 220 | return (rv); |
| 221 | } |
| 222 | |
| 223 | /* Note: requested_va updated due to seg base addr randomization */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 224 | sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 225 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 226 | fifo_segment_init (fs); |
| 227 | vec_add1 (a->new_segment_indices, fs - sm->segments); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 228 | return (0); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Attach as slave to a fifo segment |
| 233 | */ |
| 234 | int |
| 235 | fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a) |
| 236 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 237 | fifo_segment_t *fs; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 238 | int rv; |
| 239 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 240 | pool_get_zero (sm->segments, fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 241 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 242 | fs->ssvm.ssvm_size = a->segment_size; |
| 243 | fs->ssvm.my_pid = getpid (); |
| 244 | fs->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
| 245 | fs->ssvm.requested_va = sm->next_baseva; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 246 | if (a->segment_type == SSVM_SEGMENT_MEMFD) |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 247 | fs->ssvm.fd = a->memfd_fd; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 248 | else |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 249 | fs->ssvm.attach_timeout = sm->timeout_in_seconds; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 250 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 251 | if ((rv = ssvm_slave_init (&fs->ssvm, a->segment_type))) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 252 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 253 | _vec_len (fs) = vec_len (fs) - 1; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 254 | return (rv); |
| 255 | } |
| 256 | |
| 257 | /* Fish the segment header */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 258 | fs->h = fs->ssvm.sh->opaque[0]; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 259 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 260 | vec_add1 (a->new_segment_indices, fs - sm->segments); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 261 | return (0); |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s) |
| 266 | { |
| 267 | ssvm_delete (&s->ssvm); |
| 268 | clib_memset (s, 0xfe, sizeof (*s)); |
| 269 | pool_put (sm->segments, s); |
| 270 | } |
| 271 | |
| 272 | u32 |
| 273 | fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s) |
| 274 | { |
| 275 | return s - sm->segments; |
| 276 | } |
| 277 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 278 | fifo_segment_t * |
| 279 | fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index) |
| 280 | { |
| 281 | return pool_elt_at_index (sm->segments, segment_index); |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size) |
| 286 | { |
Florin Coras | 404b8a3 | 2019-05-09 12:08:06 -0700 | [diff] [blame] | 287 | *address = (char *) seg->ssvm.sh->ssvm_va; |
| 288 | *size = seg->ssvm.ssvm_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void |
| 292 | fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva, |
| 293 | u32 timeout_in_seconds) |
| 294 | { |
| 295 | sm->next_baseva = baseva; |
| 296 | sm->timeout_in_seconds = timeout_in_seconds; |
| 297 | } |
| 298 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 299 | static inline u32 |
| 300 | fs_freelist_for_size (u32 size) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 301 | { |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 302 | if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE)) |
| 303 | return 0; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 304 | return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 307 | static inline u32 |
| 308 | fs_freelist_index_to_size (u32 fl_index) |
| 309 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 310 | return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 313 | static inline int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 314 | fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 315 | { |
| 316 | /* |
| 317 | * 4K minimum. It's not likely that anything good will happen |
| 318 | * with a smaller FIFO. |
| 319 | */ |
| 320 | return size >= FIFO_SEGMENT_MIN_FIFO_SIZE |
Florin Coras | d18528f | 2020-03-27 18:41:54 +0000 | [diff] [blame] | 321 | && size <= (1ULL << fsh->max_log2_chunk_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 324 | static svm_fifo_t * |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 325 | fs_try_alloc_fifo_freelist (fifo_segment_slice_t * fss, u32 fl_index) |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 326 | { |
| 327 | svm_fifo_chunk_t *c; |
| 328 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 329 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 330 | f = fss->free_fifos; |
| 331 | c = fss->free_chunks[fl_index]; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 332 | |
| 333 | if (!f || !c) |
| 334 | return 0; |
| 335 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 336 | fss->free_fifos = f->next; |
| 337 | fss->free_chunks[fl_index] = c->next; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 338 | c->next = 0; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 339 | c->start_byte = 0; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 340 | memset (f, 0, sizeof (*f)); |
| 341 | f->start_chunk = c; |
| 342 | f->end_chunk = c; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 343 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 344 | fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 345 | return f; |
| 346 | } |
| 347 | |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 348 | svm_fifo_chunk_t * |
| 349 | fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh, |
| 350 | fifo_segment_slice_t * fss, u32 data_bytes) |
| 351 | { |
| 352 | u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes; |
| 353 | svm_fifo_chunk_t *c, *first = 0, *next; |
| 354 | |
| 355 | fl_index = fs_freelist_for_size (req_bytes); |
| 356 | if (fl_index > 0) |
| 357 | fl_index -= 1; |
| 358 | |
| 359 | fl_size = fs_freelist_index_to_size (fl_index); |
| 360 | |
| 361 | while (req_bytes) |
| 362 | { |
| 363 | c = fss->free_chunks[fl_index]; |
| 364 | if (c) |
| 365 | { |
| 366 | fss->free_chunks[fl_index] = c->next; |
| 367 | c->next = first; |
| 368 | first = c; |
| 369 | n_alloc += fl_size; |
| 370 | req_bytes -= clib_min (fl_size, req_bytes); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | /* Failed to allocate with smaller chunks */ |
| 375 | if (fl_index == 0) |
| 376 | { |
| 377 | /* free all chunks if any allocated */ |
| 378 | c = first; |
| 379 | while (c) |
| 380 | { |
| 381 | fl_index = fs_freelist_for_size (c->length); |
| 382 | fl_size = fs_freelist_index_to_size (fl_index); |
| 383 | next = c->next; |
| 384 | c->next = fss->free_chunks[fl_index]; |
| 385 | fss->free_chunks[fl_index] = c; |
| 386 | fss->n_fl_chunk_bytes += fl_size; |
| 387 | c = next; |
| 388 | } |
| 389 | n_alloc = 0; |
| 390 | first = 0; |
| 391 | fl_index = fs_freelist_for_size (data_bytes); |
| 392 | if (fss->free_chunks[fl_index + 1]) |
| 393 | { |
| 394 | fl_index += 1; |
| 395 | fl_size = fs_freelist_index_to_size (fl_index); |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | fl_index -= 1; |
| 402 | fl_size = fl_size >> 1; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fss->n_fl_chunk_bytes -= n_alloc; |
| 407 | fsh_cached_bytes_sub (fsh, n_alloc); |
| 408 | return first; |
| 409 | } |
| 410 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 411 | static svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 412 | fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh, |
| 413 | fifo_segment_slice_t * fss, |
| 414 | u32 data_bytes) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 415 | { |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 416 | svm_fifo_chunk_t *c, *first = 0, *last = 0, *next; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 417 | u32 fl_index, fl_size, n_alloc = 0; |
| 418 | svm_fifo_t *f; |
| 419 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 420 | f = fss->free_fifos; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 421 | if (!f) |
| 422 | { |
Florin Coras | 5226d26 | 2020-04-27 22:41:39 +0000 | [diff] [blame^] | 423 | if (PREDICT_FALSE (fsh_n_free_bytes (fsh) < sizeof (svm_fifo_t))) |
| 424 | return 0; |
| 425 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 426 | void *oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | 5226d26 | 2020-04-27 22:41:39 +0000 | [diff] [blame^] | 427 | f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 428 | ssvm_pop_heap (oldheap); |
| 429 | if (!f) |
| 430 | return 0; |
| 431 | memset (f, 0, sizeof (*f)); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 432 | fsh_free_bytes_sub (fsh, sizeof (*f)); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 433 | } |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 434 | else |
| 435 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 436 | fss->free_fifos = f->next; |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 437 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 438 | |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 439 | fl_index = fs_freelist_for_size (data_bytes); |
| 440 | if (fl_index > 0) |
| 441 | fl_index -= 1; |
| 442 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 443 | fl_size = fs_freelist_index_to_size (fl_index); |
| 444 | |
| 445 | while (data_bytes) |
| 446 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 447 | c = fss->free_chunks[fl_index]; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 448 | if (c) |
| 449 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 450 | fss->free_chunks[fl_index] = c->next; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 451 | if (!last) |
| 452 | last = c; |
| 453 | c->next = first; |
| 454 | first = c; |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 455 | n_alloc += fl_size; |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 456 | data_bytes -= clib_min (fl_size, data_bytes); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 457 | } |
| 458 | else |
| 459 | { |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 460 | /* Failed to allocate with smaller chunks */ |
| 461 | if (fl_index == 0) |
| 462 | { |
| 463 | /* free all chunks if any allocated */ |
| 464 | c = first; |
| 465 | while (c) |
| 466 | { |
| 467 | fl_index = fs_freelist_for_size (c->length); |
| 468 | fl_size = fs_freelist_index_to_size (fl_index); |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 469 | next = c->next; |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 470 | c->next = fss->free_chunks[fl_index]; |
| 471 | fss->free_chunks[fl_index] = c; |
| 472 | fss->n_fl_chunk_bytes += fl_size; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 473 | n_alloc -= fl_size; |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 474 | data_bytes += fl_size; |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 475 | c = next; |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 476 | } |
| 477 | first = last = 0; |
| 478 | fl_index = fs_freelist_for_size (data_bytes); |
| 479 | if (fss->free_chunks[fl_index + 1]) |
| 480 | { |
| 481 | fl_index += 1; |
| 482 | fl_size = fs_freelist_index_to_size (fl_index); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | f->next = fss->free_fifos; |
| 487 | fss->free_fifos = f; |
| 488 | return 0; |
| 489 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 490 | fl_index -= 1; |
| 491 | fl_size = fl_size >> 1; |
| 492 | } |
| 493 | } |
Florin Coras | d849edf | 2019-12-20 18:48:20 -0800 | [diff] [blame] | 494 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 495 | f->start_chunk = first; |
| 496 | f->end_chunk = last; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 497 | fss->n_fl_chunk_bytes -= n_alloc; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 498 | fsh_cached_bytes_sub (fsh, n_alloc); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 499 | return f; |
| 500 | } |
| 501 | |
| 502 | static int |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 503 | fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh, |
| 504 | fifo_segment_slice_t * fss, |
| 505 | u32 fl_index, u32 batch_size) |
| 506 | { |
| 507 | u32 rounded_data_size; |
| 508 | svm_fifo_chunk_t *c; |
| 509 | void *oldheap; |
| 510 | uword size; |
| 511 | u8 *cmem; |
| 512 | int i; |
| 513 | |
| 514 | rounded_data_size = fs_freelist_index_to_size (fl_index); |
| 515 | size = (uword) (sizeof (*c) + rounded_data_size) * batch_size; |
| 516 | |
| 517 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
| 518 | cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 519 | 0 /* align_offset */ , |
| 520 | 0 /* os_out_of_memory */ ); |
| 521 | ssvm_pop_heap (oldheap); |
| 522 | |
| 523 | /* Out of space.. */ |
| 524 | if (cmem == 0) |
| 525 | return -1; |
| 526 | |
| 527 | /* Carve fifo + chunk space */ |
| 528 | for (i = 0; i < batch_size; i++) |
| 529 | { |
| 530 | c = (svm_fifo_chunk_t *) cmem; |
| 531 | c->start_byte = 0; |
| 532 | c->length = rounded_data_size; |
| 533 | c->enq_rb_index = RBTREE_TNIL_INDEX; |
| 534 | c->deq_rb_index = RBTREE_TNIL_INDEX; |
| 535 | c->next = fss->free_chunks[fl_index]; |
| 536 | fss->free_chunks[fl_index] = c; |
| 537 | cmem += sizeof (*c) + rounded_data_size; |
| 538 | } |
| 539 | |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 540 | fss->num_chunks[fl_index] += batch_size; |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 541 | fss->n_fl_chunk_bytes += batch_size * rounded_data_size; |
| 542 | fsh_cached_bytes_add (fsh, batch_size * rounded_data_size); |
| 543 | fsh_free_bytes_sub (fsh, size); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 549 | fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh, |
| 550 | fifo_segment_slice_t * fss, |
| 551 | u32 fl_index, u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 552 | { |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 553 | u32 hdrs, rounded_data_size; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 554 | svm_fifo_chunk_t *c; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 555 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 556 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 557 | uword size; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 558 | u8 *fmem; |
| 559 | int i; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 560 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 561 | rounded_data_size = fs_freelist_index_to_size (fl_index); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 562 | hdrs = sizeof (*f) + sizeof (*c); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 563 | size = (uword) (hdrs + rounded_data_size) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 564 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 565 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 566 | fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 567 | 0 /* align_offset */ , |
| 568 | 0 /* os_out_of_memory */ ); |
| 569 | ssvm_pop_heap (oldheap); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 570 | |
| 571 | /* Out of space.. */ |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 572 | if (fmem == 0) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 573 | return -1; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 574 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 575 | /* Carve fifo + chunk space */ |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 576 | for (i = 0; i < batch_size; i++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 577 | { |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 578 | f = (svm_fifo_t *) fmem; |
| 579 | memset (f, 0, sizeof (*f)); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 580 | f->next = fss->free_fifos; |
| 581 | fss->free_fifos = f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 582 | c = (svm_fifo_chunk_t *) (fmem + sizeof (*f)); |
| 583 | c->start_byte = 0; |
| 584 | c->length = rounded_data_size; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 585 | c->enq_rb_index = RBTREE_TNIL_INDEX; |
| 586 | c->deq_rb_index = RBTREE_TNIL_INDEX; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 587 | c->next = fss->free_chunks[fl_index]; |
| 588 | fss->free_chunks[fl_index] = c; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 589 | fmem += hdrs + rounded_data_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 590 | } |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 591 | |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 592 | fss->num_chunks[fl_index] += batch_size; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 593 | fss->n_fl_chunk_bytes += batch_size * rounded_data_size; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 594 | fsh_cached_bytes_add (fsh, batch_size * rounded_data_size); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 595 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Try to allocate new fifo |
| 602 | * |
| 603 | * Tries the following steps in order: |
| 604 | * - grab fifo and chunk from freelists |
| 605 | * - batch fifo and chunk allocation |
| 606 | * - single fifo allocation |
| 607 | * - grab multiple fifo chunks from freelists |
| 608 | */ |
| 609 | static svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 610 | fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, |
| 611 | u32 data_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 612 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 613 | u32 fifo_sz, fl_index; |
| 614 | svm_fifo_t *f = 0; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 615 | uword n_free_bytes; |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 616 | u32 min_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 617 | |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 618 | min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096); |
| 619 | fl_index = fs_freelist_for_size (min_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 620 | |
Florin Coras | 0e6199d | 2020-04-17 20:15:22 +0000 | [diff] [blame] | 621 | if (fl_index >= vec_len (fss->free_chunks)) |
| 622 | return 0; |
| 623 | |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 624 | clib_spinlock_lock (&fss->chunk_lock); |
| 625 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 626 | if (fss->free_fifos && fss->free_chunks[fl_index]) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 627 | { |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 628 | f = fs_try_alloc_fifo_freelist (fss, fl_index); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 629 | if (f) |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 630 | { |
| 631 | fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); |
| 632 | goto done; |
| 633 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 634 | } |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 635 | |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 636 | fifo_sz = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); |
| 637 | fifo_sz += 1 << max_log2 (min_size); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 638 | n_free_bytes = fsh_n_free_bytes (fsh); |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 639 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 640 | if (fifo_sz * FIFO_SEGMENT_ALLOC_BATCH_SIZE < n_free_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 641 | { |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 642 | if (!fs_try_alloc_fifo_batch (fsh, fss, fl_index, |
| 643 | FIFO_SEGMENT_ALLOC_BATCH_SIZE)) |
| 644 | { |
| 645 | f = fs_try_alloc_fifo_freelist (fss, fl_index); |
| 646 | if (f) |
| 647 | { |
| 648 | fsh_cached_bytes_sub (fsh, |
| 649 | fs_freelist_index_to_size (fl_index)); |
| 650 | goto done; |
| 651 | } |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | fsh_check_mem (fsh); |
| 656 | n_free_bytes = fsh_n_free_bytes (fsh); |
| 657 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 658 | } |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 659 | if (fifo_sz <= n_free_bytes) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 660 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 661 | void *oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 662 | f = svm_fifo_alloc (min_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 663 | ssvm_pop_heap (oldheap); |
| 664 | if (f) |
| 665 | { |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 666 | clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 667 | fsh_free_bytes_sub (fsh, fifo_sz); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 668 | goto done; |
| 669 | } |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 670 | fsh_check_mem (fsh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 671 | } |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 672 | /* All failed, try to allocate min of data bytes and fifo sz */ |
| 673 | fifo_sz = clib_min (fifo_sz, data_bytes); |
| 674 | if (fifo_sz <= fss->n_fl_chunk_bytes) |
| 675 | f = fs_try_alloc_fifo_freelist_multi_chunk (fsh, fss, fifo_sz); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 676 | |
| 677 | done: |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 678 | clib_spinlock_unlock (&fss->chunk_lock); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 679 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 680 | if (f) |
Florin Coras | c75ce4d | 2020-02-28 21:51:24 +0000 | [diff] [blame] | 681 | { |
| 682 | f->size = data_bytes; |
| 683 | f->fs_hdr = fsh; |
| 684 | } |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 685 | return f; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 688 | svm_fifo_chunk_t * |
| 689 | fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size) |
| 690 | { |
| 691 | fifo_segment_slice_t *fss; |
| 692 | svm_fifo_chunk_t *c; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 693 | int fl_index; |
| 694 | |
| 695 | fl_index = fs_freelist_for_size (chunk_size); |
| 696 | fss = fsh_slice_get (fsh, slice_index); |
| 697 | |
| 698 | clib_spinlock_lock (&fss->chunk_lock); |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 699 | |
Florin Coras | 0e6199d | 2020-04-17 20:15:22 +0000 | [diff] [blame] | 700 | ASSERT (vec_len (fss->free_chunks) > fl_index); |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 701 | c = fss->free_chunks[fl_index]; |
| 702 | |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 703 | if (c) |
| 704 | { |
| 705 | fss->free_chunks[fl_index] = c->next; |
| 706 | c->next = 0; |
| 707 | fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); |
| 708 | fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); |
| 709 | } |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 710 | else |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 711 | { |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 712 | void *oldheap; |
| 713 | uword n_free; |
Florin Coras | 8e755a1 | 2020-02-06 16:59:31 +0000 | [diff] [blame] | 714 | u32 batch; |
| 715 | |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 716 | chunk_size = fs_freelist_index_to_size (fl_index); |
| 717 | n_free = fsh_n_free_bytes (fsh); |
| 718 | |
| 719 | if (chunk_size <= n_free) |
| 720 | { |
| 721 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
| 722 | c = svm_fifo_chunk_alloc (chunk_size); |
| 723 | ssvm_pop_heap (oldheap); |
| 724 | |
| 725 | if (c) |
| 726 | { |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 727 | clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); |
Florin Coras | 0269fa1 | 2020-03-10 04:46:32 +0000 | [diff] [blame] | 728 | fsh_free_bytes_sub (fsh, chunk_size + sizeof (*c)); |
| 729 | goto done; |
| 730 | } |
| 731 | |
| 732 | fsh_check_mem (fsh); |
| 733 | n_free = fsh_n_free_bytes (fsh); |
| 734 | } |
| 735 | if (chunk_size <= fss->n_fl_chunk_bytes) |
| 736 | { |
| 737 | c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); |
| 738 | if (c) |
| 739 | goto done; |
| 740 | batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE; |
| 741 | if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch)) |
| 742 | { |
| 743 | fsh_check_mem (fsh); |
| 744 | goto done; |
| 745 | } |
| 746 | } |
| 747 | if (chunk_size <= fss->n_fl_chunk_bytes + n_free) |
| 748 | { |
| 749 | u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE; |
| 750 | |
| 751 | batch = (chunk_size - fss->n_fl_chunk_bytes) / min_size; |
| 752 | batch = clib_min (batch + 1, n_free / min_size); |
| 753 | if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch)) |
| 754 | { |
| 755 | fsh_check_mem (fsh); |
| 756 | goto done; |
| 757 | } |
| 758 | c = fs_try_alloc_multi_chunk (fsh, fss, chunk_size); |
| 759 | } |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | done: |
| 763 | |
| 764 | clib_spinlock_unlock (&fss->chunk_lock); |
| 765 | |
| 766 | return c; |
| 767 | } |
| 768 | |
| 769 | static void |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 770 | fsh_slice_collect_chunks (fifo_segment_header_t * fsh, |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 771 | fifo_segment_slice_t * fss, svm_fifo_chunk_t * c) |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 772 | { |
| 773 | svm_fifo_chunk_t *next; |
| 774 | int fl_index; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 775 | u32 n_collect = 0; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 776 | |
| 777 | clib_spinlock_lock (&fss->chunk_lock); |
| 778 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 779 | while (c) |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 780 | { |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 781 | next = c->next; |
| 782 | fl_index = fs_freelist_for_size (c->length); |
| 783 | c->next = fss->free_chunks[fl_index]; |
| 784 | c->enq_rb_index = RBTREE_TNIL_INDEX; |
| 785 | c->deq_rb_index = RBTREE_TNIL_INDEX; |
| 786 | fss->free_chunks[fl_index] = c; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 787 | n_collect += fs_freelist_index_to_size (fl_index); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 788 | c = next; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 791 | fss->n_fl_chunk_bytes += n_collect; |
| 792 | fsh_cached_bytes_add (fsh, n_collect); |
| 793 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 794 | clib_spinlock_unlock (&fss->chunk_lock); |
| 795 | } |
| 796 | |
| 797 | void |
| 798 | fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index, |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 799 | svm_fifo_chunk_t * c) |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 800 | { |
| 801 | fifo_segment_slice_t *fss; |
| 802 | fss = fsh_slice_get (fsh, slice_index); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 803 | fsh_slice_collect_chunks (fsh, fss, c); |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 806 | static inline void |
| 807 | fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f) |
| 808 | { |
| 809 | if (fss->fifos) |
| 810 | { |
| 811 | fss->fifos->prev = f; |
| 812 | f->next = fss->fifos; |
| 813 | } |
| 814 | fss->fifos = f; |
| 815 | } |
| 816 | |
| 817 | static inline void |
| 818 | fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f) |
| 819 | { |
| 820 | if (f->flags & SVM_FIFO_F_LL_TRACKED) |
| 821 | { |
| 822 | if (f->prev) |
| 823 | f->prev->next = f->next; |
| 824 | else |
| 825 | fss->fifos = f->next; |
| 826 | if (f->next) |
| 827 | f->next->prev = f->prev; |
| 828 | } |
| 829 | } |
| 830 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 831 | /** |
| 832 | * Allocate fifo in fifo segment |
| 833 | */ |
| 834 | svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 835 | fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index, |
| 836 | u32 data_bytes, fifo_segment_ftype_t ftype) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 837 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 838 | fifo_segment_header_t *fsh = fs->h; |
| 839 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 840 | svm_fifo_t *f = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 841 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 842 | ASSERT (slice_index < fs->n_slices); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 843 | |
Florin Coras | 0e6199d | 2020-04-17 20:15:22 +0000 | [diff] [blame] | 844 | if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size)) |
| 845 | return 0; |
| 846 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 847 | fss = fsh_slice_get (fsh, slice_index); |
| 848 | f = fs_try_alloc_fifo (fsh, fss, data_bytes); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 849 | if (!f) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 850 | goto done; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 851 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 852 | f->slice_index = slice_index; |
| 853 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 854 | svm_fifo_init (f, data_bytes); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 855 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 856 | /* If rx fifo type add to active fifos list. When cleaning up segment, |
| 857 | * we need a list of active sessions that should be disconnected. Since |
| 858 | * both rx and tx fifos keep pointers to the session, it's enough to track |
| 859 | * only one. */ |
| 860 | if (ftype == FIFO_SEGMENT_RX_FIFO) |
| 861 | { |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 862 | fss_fifo_add_active_list (fss, f); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 863 | f->flags |= SVM_FIFO_F_LL_TRACKED; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 864 | |
| 865 | svm_fifo_init_ooo_lookup (f, 0 /* ooo enq */ ); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 866 | } |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 867 | else |
| 868 | { |
| 869 | svm_fifo_init_ooo_lookup (f, 1 /* ooo deq */ ); |
| 870 | } |
| 871 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 872 | fsh_active_fifos_update (fsh, 1); |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 873 | fss->virtual_mem += svm_fifo_size (f); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 874 | |
| 875 | done: |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 876 | return (f); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Free fifo allocated in fifo segment |
| 881 | */ |
| 882 | void |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 883 | fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 884 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 885 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 886 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 887 | |
| 888 | ASSERT (f->refcnt > 0); |
| 889 | |
| 890 | if (--f->refcnt > 0) |
| 891 | return; |
| 892 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 893 | fss = fsh_slice_get (fsh, f->slice_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 894 | |
| 895 | /* Remove from active list. Only rx fifos are tracked */ |
| 896 | if (f->flags & SVM_FIFO_F_LL_TRACKED) |
| 897 | { |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 898 | fss_fifo_del_active_list (fss, f); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 899 | f->flags &= ~SVM_FIFO_F_LL_TRACKED; |
| 900 | } |
| 901 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 902 | /* Free fifo chunks */ |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 903 | fsh_slice_collect_chunks (fsh, fss, f->start_chunk); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 904 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 905 | f->start_chunk = f->end_chunk = 0; |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 906 | f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0; |
| 907 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 908 | /* not allocated on segment heap */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 909 | svm_fifo_free_chunk_lookup (f); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 910 | svm_fifo_free_ooo_data (f); |
| 911 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 912 | if (CLIB_DEBUG) |
| 913 | { |
| 914 | f->master_session_index = ~0; |
| 915 | f->master_thread_index = ~0; |
| 916 | } |
| 917 | |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 918 | fss->virtual_mem -= svm_fifo_size (f); |
| 919 | |
| 920 | /* Add to free list */ |
| 921 | f->next = fss->free_fifos; |
| 922 | f->prev = 0; |
| 923 | fss->free_fifos = f; |
| 924 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 925 | fsh_active_fifos_update (fsh, -1); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 928 | void |
| 929 | fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f) |
| 930 | { |
| 931 | fifo_segment_slice_t *fss; |
| 932 | svm_fifo_chunk_t *c; |
| 933 | u32 fl_index; |
| 934 | |
| 935 | ASSERT (f->refcnt == 1); |
| 936 | |
| 937 | fss = fsh_slice_get (fs->h, f->slice_index); |
| 938 | fss->virtual_mem -= svm_fifo_size (f); |
| 939 | if (f->flags & SVM_FIFO_F_LL_TRACKED) |
| 940 | fss_fifo_del_active_list (fss, f); |
| 941 | |
| 942 | c = f->start_chunk; |
| 943 | while (c) |
| 944 | { |
| 945 | fl_index = fs_freelist_for_size (c->length); |
| 946 | clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1); |
| 947 | c = c->next; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void |
| 952 | fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f, |
| 953 | u32 slice_index) |
| 954 | { |
| 955 | fifo_segment_slice_t *fss; |
| 956 | svm_fifo_chunk_t *c; |
| 957 | u32 fl_index; |
| 958 | |
| 959 | f->slice_index = slice_index; |
| 960 | fss = fsh_slice_get (fs->h, f->slice_index); |
| 961 | fss->virtual_mem += svm_fifo_size (f); |
| 962 | if (f->flags & SVM_FIFO_F_LL_TRACKED) |
| 963 | fss_fifo_add_active_list (fss, f); |
| 964 | |
| 965 | c = f->start_chunk; |
| 966 | while (c) |
| 967 | { |
| 968 | fl_index = fs_freelist_for_size (c->length); |
| 969 | clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1); |
| 970 | c = c->next; |
| 971 | } |
| 972 | } |
| 973 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 974 | int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 975 | fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index, |
| 976 | u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 977 | { |
| 978 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 979 | fifo_segment_slice_t *fss; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 980 | svm_fifo_t *f; |
| 981 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 982 | uword size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 983 | u8 *fmem; |
| 984 | int i; |
| 985 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 986 | fss = fsh_slice_get (fsh, slice_index); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 987 | size = (uword) (sizeof (*f)) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 988 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 989 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 990 | fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 991 | 0 /* align_offset */ , |
| 992 | 0 /* os_out_of_memory */ ); |
| 993 | ssvm_pop_heap (oldheap); |
| 994 | |
| 995 | /* Out of space.. */ |
| 996 | if (fmem == 0) |
| 997 | return -1; |
| 998 | |
| 999 | /* Carve fifo + chunk space */ |
| 1000 | for (i = 0; i < batch_size; i++) |
| 1001 | { |
| 1002 | f = (svm_fifo_t *) fmem; |
| 1003 | memset (f, 0, sizeof (*f)); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1004 | f->next = fss->free_fifos; |
| 1005 | fss->free_fifos = f; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1006 | fmem += sizeof (*f); |
| 1007 | } |
| 1008 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1009 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | int |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1015 | fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index, |
| 1016 | u32 chunk_size, u32 batch_size) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1017 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1018 | fifo_segment_header_t *fsh = fs->h; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 1019 | u32 rounded_data_size, fl_index; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1020 | fifo_segment_slice_t *fss; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1021 | svm_fifo_chunk_t *c; |
| 1022 | void *oldheap; |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 1023 | uword size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1024 | u8 *cmem; |
| 1025 | int i; |
| 1026 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1027 | if (!fs_chunk_size_is_valid (fsh, chunk_size)) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1028 | { |
| 1029 | clib_warning ("chunk size out of range %d", chunk_size); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | fl_index = fs_freelist_for_size (chunk_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1034 | rounded_data_size = fs_freelist_index_to_size (fl_index); |
Florin Coras | 36a0c4d | 2020-01-31 08:28:02 -0800 | [diff] [blame] | 1035 | size = (uword) (sizeof (*c) + rounded_data_size) * batch_size; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1036 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1037 | oldheap = ssvm_push_heap (fsh->ssvm_sh); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1038 | cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES, |
| 1039 | 0 /* align_offset */ , |
| 1040 | 0 /* os_out_of_memory */ ); |
| 1041 | ssvm_pop_heap (oldheap); |
| 1042 | |
| 1043 | /* Out of space.. */ |
| 1044 | if (cmem == 0) |
| 1045 | return -1; |
| 1046 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1047 | fss = fsh_slice_get (fsh, slice_index); |
| 1048 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1049 | /* Carve fifo + chunk space */ |
| 1050 | for (i = 0; i < batch_size; i++) |
| 1051 | { |
| 1052 | c = (svm_fifo_chunk_t *) cmem; |
| 1053 | c->start_byte = 0; |
| 1054 | c->length = rounded_data_size; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1055 | c->next = fss->free_chunks[fl_index]; |
| 1056 | fss->free_chunks[fl_index] = c; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1057 | cmem += sizeof (*c) + rounded_data_size; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1058 | fsh_cached_bytes_add (fsh, rounded_data_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 1061 | fss->num_chunks[fl_index] += batch_size; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1062 | fss->n_fl_chunk_bytes += batch_size * rounded_data_size; |
| 1063 | fsh_free_bytes_sub (fsh, size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1068 | /** |
| 1069 | * Pre-allocates fifo pairs in fifo segment |
| 1070 | */ |
| 1071 | void |
| 1072 | fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs, |
| 1073 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 1074 | u32 * n_fifo_pairs) |
| 1075 | { |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1076 | 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] | 1077 | u32 hdrs, pairs_per_slice, alloc_now; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1078 | fifo_segment_header_t *fsh = fs->h; |
| 1079 | int rx_fl_index, tx_fl_index, i; |
| 1080 | fifo_segment_slice_t *fss; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1081 | uword space_available; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1082 | |
| 1083 | /* Parameter check */ |
| 1084 | if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0) |
| 1085 | return; |
| 1086 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1087 | if (!fs_chunk_size_is_valid (fsh, rx_fifo_size)) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1088 | { |
| 1089 | clib_warning ("rx fifo_size out of range %d", rx_fifo_size); |
| 1090 | return; |
| 1091 | } |
| 1092 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1093 | if (!fs_chunk_size_is_valid (fsh, tx_fifo_size)) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1094 | { |
| 1095 | clib_warning ("tx fifo_size out of range %d", tx_fifo_size); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1100 | rx_fl_index = fs_freelist_for_size (rx_fifo_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1101 | tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1102 | tx_fl_index = fs_freelist_for_size (tx_fifo_size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1103 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1104 | hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 1105 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1106 | /* Calculate space requirements */ |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 1107 | pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 1108 | space_available = fsh_free_space (fsh); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1109 | pairs_to_alloc = space_available / pair_size; |
| 1110 | pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1111 | pairs_per_slice = pairs_to_alloc / fs->n_slices; |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 1112 | pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0; |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1113 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1114 | if (!pairs_per_slice) |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1115 | return; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1116 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1117 | for (i = 0; i < fs->n_slices; i++) |
| 1118 | { |
| 1119 | fss = fsh_slice_get (fsh, i); |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 1120 | alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs); |
| 1121 | if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now)) |
| 1122 | clib_warning ("rx prealloc failed: pairs %u", alloc_now); |
| 1123 | if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now)) |
| 1124 | clib_warning ("tx prealloc failed: pairs %u", alloc_now); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1125 | |
Florin Coras | 8d0149d | 2020-01-02 19:22:51 +0000 | [diff] [blame] | 1126 | /* Account for the pairs allocated */ |
| 1127 | *n_fifo_pairs -= alloc_now; |
| 1128 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Get number of active fifos |
| 1133 | */ |
| 1134 | u32 |
| 1135 | fifo_segment_num_fifos (fifo_segment_t * fs) |
| 1136 | { |
Florin Coras | 7a90e50 | 2020-04-09 04:46:14 +0000 | [diff] [blame] | 1137 | return fsh_n_active_fifos (fs->h); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1140 | static u32 |
| 1141 | fs_slice_num_free_fifos (fifo_segment_slice_t * fss) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1142 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1143 | svm_fifo_t *f; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 1144 | u32 count = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1145 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1146 | f = fss->free_fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1147 | if (f == 0) |
| 1148 | return 0; |
| 1149 | |
| 1150 | while (f) |
| 1151 | { |
| 1152 | f = f->next; |
| 1153 | count++; |
| 1154 | } |
| 1155 | return count; |
| 1156 | } |
| 1157 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1158 | u32 |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1159 | fifo_segment_num_free_fifos (fifo_segment_t * fs) |
| 1160 | { |
| 1161 | fifo_segment_header_t *fsh = fs->h; |
| 1162 | fifo_segment_slice_t *fss; |
| 1163 | int slice_index; |
| 1164 | u32 count = 0; |
| 1165 | |
| 1166 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 1167 | { |
| 1168 | fss = fsh_slice_get (fsh, slice_index); |
| 1169 | count += fs_slice_num_free_fifos (fss); |
| 1170 | } |
| 1171 | return count; |
| 1172 | } |
| 1173 | |
| 1174 | static u32 |
| 1175 | fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1176 | { |
| 1177 | u32 count = 0, rounded_size, fl_index; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1178 | svm_fifo_chunk_t *c; |
| 1179 | int i; |
| 1180 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1181 | /* Count all free chunks? */ |
| 1182 | if (size == ~0) |
| 1183 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1184 | for (i = 0; i < vec_len (fss->free_chunks); i++) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1185 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1186 | c = fss->free_chunks[i]; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1187 | if (c == 0) |
| 1188 | continue; |
| 1189 | |
| 1190 | while (c) |
| 1191 | { |
| 1192 | c = c->next; |
| 1193 | count++; |
| 1194 | } |
| 1195 | } |
| 1196 | return count; |
| 1197 | } |
| 1198 | |
| 1199 | rounded_size = (1 << (max_log2 (size))); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1200 | fl_index = fs_freelist_for_size (rounded_size); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1201 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1202 | if (fl_index >= vec_len (fss->free_chunks)) |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1203 | return 0; |
| 1204 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1205 | c = fss->free_chunks[fl_index]; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 1206 | if (c == 0) |
| 1207 | return 0; |
| 1208 | |
| 1209 | while (c) |
| 1210 | { |
| 1211 | c = c->next; |
| 1212 | count++; |
| 1213 | } |
| 1214 | return count; |
| 1215 | } |
| 1216 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1217 | u32 |
| 1218 | fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size) |
| 1219 | { |
| 1220 | fifo_segment_header_t *fsh = fs->h; |
| 1221 | fifo_segment_slice_t *fss; |
| 1222 | int slice_index; |
| 1223 | u32 count = 0; |
| 1224 | |
| 1225 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 1226 | { |
| 1227 | fss = fsh_slice_get (fsh, slice_index); |
| 1228 | count += fs_slice_num_free_chunks (fss, size); |
| 1229 | } |
| 1230 | return count; |
| 1231 | } |
| 1232 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1233 | void |
| 1234 | fifo_segment_update_free_bytes (fifo_segment_t * fs) |
| 1235 | { |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 1236 | fsh_update_free_bytes (fs->h); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1239 | uword |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1240 | fifo_segment_size (fifo_segment_t * fs) |
| 1241 | { |
| 1242 | return fs->ssvm.ssvm_size; |
| 1243 | } |
| 1244 | |
| 1245 | u8 |
| 1246 | fsh_has_reached_mem_limit (fifo_segment_header_t * fsh) |
| 1247 | { |
| 1248 | return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0; |
| 1249 | } |
| 1250 | |
| 1251 | void |
| 1252 | fsh_reset_mem_limit (fifo_segment_header_t * fsh) |
| 1253 | { |
| 1254 | fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT; |
| 1255 | } |
| 1256 | |
| 1257 | uword |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1258 | fifo_segment_free_bytes (fifo_segment_t * fs) |
| 1259 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1260 | return fsh_n_free_bytes (fs->h); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1263 | uword |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1264 | fifo_segment_cached_bytes (fifo_segment_t * fs) |
| 1265 | { |
| 1266 | return fsh_n_cached_bytes (fs->h); |
| 1267 | } |
| 1268 | |
| 1269 | uword |
Florin Coras | 75ccf7b | 2020-03-05 19:44:02 +0000 | [diff] [blame] | 1270 | fifo_segment_available_bytes (fifo_segment_t * fs) |
| 1271 | { |
| 1272 | return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h); |
| 1273 | } |
| 1274 | |
| 1275 | uword |
Florin Coras | 1c91c77 | 2019-06-25 18:14:13 -0700 | [diff] [blame] | 1276 | fifo_segment_fl_chunk_bytes (fifo_segment_t * fs) |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1277 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1278 | fifo_segment_header_t *fsh = fs->h; |
| 1279 | fifo_segment_slice_t *fss; |
| 1280 | uword n_bytes = 0; |
| 1281 | int slice_index; |
| 1282 | |
| 1283 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
| 1284 | { |
| 1285 | fss = fsh_slice_get (fsh, slice_index); |
| 1286 | n_bytes += fss->n_fl_chunk_bytes; |
| 1287 | } |
| 1288 | |
| 1289 | return n_bytes; |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1292 | u8 |
| 1293 | fifo_segment_has_fifos (fifo_segment_t * fs) |
| 1294 | { |
Florin Coras | 7a90e50 | 2020-04-09 04:46:14 +0000 | [diff] [blame] | 1295 | return (fsh_n_active_fifos (fs->h) != 0); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | svm_fifo_t * |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1299 | 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] | 1300 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1301 | fifo_segment_header_t *fsh = fs->h; |
| 1302 | fifo_segment_slice_t *fss; |
| 1303 | |
| 1304 | fss = fsh_slice_get (fsh, slice_index); |
| 1305 | return fss->fifos; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1308 | u8 |
| 1309 | fifo_segment_get_mem_usage (fifo_segment_t * fs) |
| 1310 | { |
| 1311 | uword size, in_use; |
| 1312 | |
| 1313 | size = fifo_segment_size (fs); |
| 1314 | in_use = |
| 1315 | size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs); |
| 1316 | return (in_use * 100) / size; |
| 1317 | } |
| 1318 | |
| 1319 | fifo_segment_mem_status_t |
| 1320 | fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage) |
| 1321 | { |
| 1322 | if (!fsh->high_watermark || !fsh->low_watermark) |
| 1323 | return MEMORY_PRESSURE_NO_PRESSURE; |
| 1324 | |
| 1325 | /* once the no-memory is detected, the status continues |
| 1326 | * until memory usage gets below the high watermark |
| 1327 | */ |
| 1328 | if (fsh_has_reached_mem_limit (fsh)) |
| 1329 | { |
| 1330 | if (usage >= fsh->high_watermark) |
| 1331 | return MEMORY_PRESSURE_NO_MEMORY; |
| 1332 | else |
| 1333 | fsh_reset_mem_limit (fsh); |
| 1334 | } |
| 1335 | |
| 1336 | if (usage >= fsh->high_watermark) |
| 1337 | return MEMORY_PRESSURE_HIGH_PRESSURE; |
| 1338 | |
| 1339 | else if (usage >= fsh->low_watermark) |
| 1340 | return MEMORY_PRESSURE_LOW_PRESSURE; |
| 1341 | |
| 1342 | return MEMORY_PRESSURE_NO_PRESSURE; |
| 1343 | } |
| 1344 | |
| 1345 | fifo_segment_mem_status_t |
| 1346 | fifo_segment_get_mem_status (fifo_segment_t * fs) |
| 1347 | { |
| 1348 | fifo_segment_header_t *fsh = fs->h; |
| 1349 | u8 usage = fifo_segment_get_mem_usage (fs); |
| 1350 | |
| 1351 | return fifo_segment_determine_status (fsh, usage); |
| 1352 | } |
| 1353 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1354 | u8 * |
| 1355 | format_fifo_segment_type (u8 * s, va_list * args) |
| 1356 | { |
| 1357 | fifo_segment_t *sp; |
| 1358 | sp = va_arg (*args, fifo_segment_t *); |
| 1359 | ssvm_segment_type_t st = ssvm_type (&sp->ssvm); |
| 1360 | |
| 1361 | if (st == SSVM_SEGMENT_PRIVATE) |
| 1362 | s = format (s, "%s", "private-heap"); |
| 1363 | else if (st == SSVM_SEGMENT_MEMFD) |
| 1364 | s = format (s, "%s", "memfd"); |
| 1365 | else if (st == SSVM_SEGMENT_SHM) |
| 1366 | s = format (s, "%s", "shm"); |
| 1367 | else |
| 1368 | s = format (s, "%s", "unknown"); |
| 1369 | return s; |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Segment format function |
| 1374 | */ |
| 1375 | u8 * |
| 1376 | format_fifo_segment (u8 * s, va_list * args) |
| 1377 | { |
Ryujiro Shibuya | 65c30ce | 2020-03-26 07:29:09 +0000 | [diff] [blame] | 1378 | u32 count, indent, active_fifos, free_fifos; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1379 | fifo_segment_t *fs = va_arg (*args, fifo_segment_t *); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1380 | int verbose __attribute__ ((unused)) = va_arg (*args, int); |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 1381 | uword est_chunk_bytes, est_free_seg_bytes, free_chunks; |
| 1382 | uword chunk_bytes = 0, free_seg_bytes, chunk_size; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1383 | uword tracked_cached_bytes; |
Ryujiro Shibuya | 65c30ce | 2020-03-26 07:29:09 +0000 | [diff] [blame] | 1384 | uword fifo_hdr = 0, reserved; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1385 | fifo_segment_header_t *fsh; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1386 | fifo_segment_slice_t *fss; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 1387 | svm_fifo_chunk_t *c; |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1388 | u32 slice_index; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1389 | char *address; |
| 1390 | size_t size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1391 | int i; |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 1392 | uword allocated, in_use, virt; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1393 | f64 usage; |
| 1394 | fifo_segment_mem_status_t mem_st; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1395 | |
| 1396 | indent = format_get_indent (s) + 2; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1397 | |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1398 | if (fs == 0) |
| 1399 | { |
| 1400 | s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type", |
| 1401 | "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address"); |
| 1402 | return s; |
| 1403 | } |
| 1404 | |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1405 | fifo_segment_info (fs, &address, &size); |
| 1406 | active_fifos = fifo_segment_num_fifos (fs); |
| 1407 | free_fifos = fifo_segment_num_free_fifos (fs); |
| 1408 | |
| 1409 | s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm), |
| 1410 | format_fifo_segment_type, fs, size >> 20ULL, active_fifos, |
| 1411 | free_fifos, address); |
| 1412 | |
| 1413 | if (!verbose) |
| 1414 | return s; |
| 1415 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1416 | fsh = fs->h; |
| 1417 | |
| 1418 | free_chunks = fifo_segment_num_free_chunks (fs, ~0); |
| 1419 | if (free_chunks) |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 1420 | s = |
| 1421 | format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space, |
| 1422 | indent + 2); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1423 | else |
| 1424 | s = format (s, "\n"); |
| 1425 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1426 | for (slice_index = 0; slice_index < fs->n_slices; slice_index++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1427 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1428 | fss = fsh_slice_get (fsh, slice_index); |
| 1429 | for (i = 0; i < vec_len (fss->free_chunks); i++) |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1430 | { |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1431 | c = fss->free_chunks[i]; |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 1432 | if (c == 0 && fss->num_chunks[i] == 0) |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1433 | continue; |
| 1434 | count = 0; |
| 1435 | while (c) |
| 1436 | { |
| 1437 | c = c->next; |
| 1438 | count++; |
| 1439 | } |
| 1440 | |
| 1441 | chunk_size = fs_freelist_index_to_size (i); |
Ryujiro Shibuya | d358872 | 2020-03-13 12:45:06 +0000 | [diff] [blame] | 1442 | s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2, |
| 1443 | chunk_size >> 10, count, fss->num_chunks[i]); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1444 | |
| 1445 | chunk_bytes += count * chunk_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1446 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1447 | } |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1448 | |
| 1449 | fifo_hdr = free_fifos * sizeof (svm_fifo_t); |
| 1450 | est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1451 | est_free_seg_bytes = fifo_segment_free_bytes (fs); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1452 | fifo_segment_update_free_bytes (fs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 1453 | free_seg_bytes = fifo_segment_free_bytes (fs); |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 1454 | tracked_cached_bytes = fifo_segment_cached_bytes (fs); |
| 1455 | allocated = fifo_segment_size (fs); |
| 1456 | in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes; |
| 1457 | usage = (100.0 * in_use) / allocated; |
| 1458 | mem_st = fifo_segment_get_mem_status (fs); |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 1459 | virt = fsh_virtual_mem (fsh); |
Ryujiro Shibuya | 65c30ce | 2020-03-26 07:29:09 +0000 | [diff] [blame] | 1460 | reserved = fsh->n_reserved_bytes; |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1461 | |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 1462 | s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:" |
| 1463 | " %U (%lu)\n", format_white_space, indent + 2, |
| 1464 | format_memory_size, free_seg_bytes, free_seg_bytes, |
| 1465 | format_memory_size, est_free_seg_bytes, est_free_seg_bytes, |
Ryujiro Shibuya | 65c30ce | 2020-03-26 07:29:09 +0000 | [diff] [blame] | 1466 | format_memory_size, reserved, reserved); |
Florin Coras | 06d4775 | 2020-03-06 02:23:58 +0000 | [diff] [blame] | 1467 | s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:" |
| 1468 | " %U (%lu)\n", format_white_space, indent + 2, |
| 1469 | format_memory_size, chunk_bytes, chunk_bytes, |
| 1470 | format_memory_size, est_chunk_bytes, est_chunk_bytes, |
| 1471 | format_memory_size, tracked_cached_bytes, tracked_cached_bytes); |
| 1472 | s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n", |
| 1473 | format_white_space, indent + 2, fsh->n_active_fifos, |
| 1474 | format_memory_size, fifo_hdr, fifo_hdr); |
| 1475 | s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n", |
| 1476 | format_white_space, indent + 2, usage, format_memory_size, |
| 1477 | in_use, format_memory_size, allocated, format_memory_size, virt, |
| 1478 | fifo_segment_mem_status_strings[mem_st]); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 1479 | s = format (s, "\n"); |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 1480 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1481 | return s; |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * fd.io coding-style-patch-verification: ON |
| 1486 | * |
| 1487 | * Local Variables: |
| 1488 | * eval: (c-set-style "gnu") |
| 1489 | * End: |
| 1490 | */ |