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