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